1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Module to build contract
16 # This module provide extension of the modele to add contracts, the building phase and the "re-driving" to call the contract.
18 # FIXME Split the module in three parts: extension of the modele, building phase and the "re-driving"
22 import parse_annotations
25 intrude import modelize_property
29 redef class ToolContext
30 # Parses contracts annotations.
31 var contracts_phase
: Phase = new ContractsPhase(self, [modelize_property_phase
,typing_phase
])
34 private class ContractsPhase
37 # The entry point of the contract phase
38 # In reality, the contract phase is executed on each module
39 # FIXME: Actually all method is checked to add method if any contract is inherited
40 redef fun process_nmodule
(nmodule
)do
41 # Check if the contracts are disabled
42 if toolcontext
.opt_no_contract
.value
then return
43 nmodule
.do_contracts
(self.toolcontext
)
48 # Compile all contracts
50 # The implementation of the contracts is done in two visits.
52 # - First step, the visitor analyzes and constructs the contracts
53 # for each class (invariant) and method (expect, ensure).
55 # - Second step the visitor analyzes each `ASendExpr` to see
56 # if the callsite calls a method with a contract. If this is
57 # the case the callsite is replaced by another callsite to the contract method.
58 fun do_contracts
(toolcontext
: ToolContext) do
60 var contract_visitor
= new ContractsVisitor(toolcontext
, toolcontext
.modelbuilder
.identified_modules
.first
, self, new ASTBuilder(mmodule
.as(not null)))
61 contract_visitor
.enter_visit
(self)
63 var callsite_visitor
= new CallSiteVisitor(toolcontext
)
64 callsite_visitor
.enter_visit
(self)
68 # This visitor checks the `AMethPropdef` and the `AClassDef` to check if they have a contract annotation or it's a redefinition with a inheritance contract
69 private class ContractsVisitor
72 var toolcontext
: ToolContext
75 # It's strore to define if the contract need to be build depending on the selected policy `--no-contract` `--full-contract` or default
76 var mainmodule
: MModule
78 # Actual visited module
79 var visited_module
: AModule
81 var ast_builder
: ASTBuilder
83 # The `ASignature` of the actual build contract
84 var n_signature
: ASignature is noinit
86 # The `MSignature` of the actual build contract
87 var m_signature
: MSignature is noinit
89 # The `current_location` can corresponding of the annotation or method location.
90 var current_location
: Location is noinit
92 # Is the contrat is an introduction or not?
93 var is_intro_contract
: Bool is noinit
95 # Actual visited class
96 var visited_class
: nullable AClassdef
98 # is `no_contract` annotation was found
99 var find_no_contract
= false
101 redef fun visit
(node
)
103 node
.create_contracts
(self)
107 # Method use to define the signature part of the method `ASignature` and `MSignature`
108 # The given `mcontract` provided `adapt_nsignature` and `adapt_msignature` to copy and adapt the given signature (`nsignature` and `msignature`)
109 fun define_signature
(mcontract
: MContract, nsignature
: nullable ASignature, msignature
: nullable MSignature)
111 if nsignature
!= null and msignature
!= null then nsignature
.ret_type
= msignature
.return_mtype
112 self.n_signature
= mcontract
.adapt_nsignature
(nsignature
)
113 self.m_signature
= mcontract
.adapt_msignature
(msignature
)
116 # Define the new contract take in consideration that an old contract exist or not
117 private fun build_contract
(n_annotation
: AAnnotation, mcontract
: MContract, mclassdef
: MClassDef): MMethodDef
119 self.current_location
= n_annotation
.location
120 # Retrieving the expression provided in the annotation
121 var n_condition
= n_annotation
.construct_condition
(self)
122 var m_contractdef
: AMethPropdef
123 if is_intro_contract
then
124 # Create new contract method
125 m_contractdef
= mcontract
.create_intro_contract
(self, n_condition
, mclassdef
)
127 # Create a redef of contract to take in consideration the new condition
128 m_contractdef
= mcontract
.create_subcontract
(self, n_condition
, mclassdef
)
130 var contract_propdef
= m_contractdef
.mpropdef
131 # The contract has a null mpropdef, this should never happen
132 assert contract_propdef
!= null
133 return contract_propdef
136 # Verification if the construction of the contract is necessary.
137 # Three cases are checked for `expect`:
139 # - Is the `--full-contract` option it's use?
140 # - Is the method is in the main package
141 # - Is the method is in a direct imported package.
143 fun check_usage_expect
(actual_mmodule
: MModule): Bool
145 var main_package
= mainmodule
.mpackage
146 var actual_package
= actual_mmodule
.mpackage
147 if main_package
!= null and actual_package
!= null then
148 var condition_direct_arc
= toolcontext
.modelbuilder
.model
.mpackage_importation_graph
.has_arc
(main_package
, actual_package
)
149 return toolcontext
.opt_full_contract
.value
or condition_direct_arc
or main_package
== actual_package
154 # Verification if the construction of the contract is necessary.
155 # Two cases are checked for `ensure`:
157 # - Is the `--full-contract` option it's use?
158 # - Is the method is in the main package
160 fun check_usage_ensure
(actual_mmodule
: MModule): Bool
162 return toolcontext
.opt_full_contract
.value
or mainmodule
.mpackage
== actual_mmodule
.mpackage
167 # This visitor checks the `callsite` to see if the target `mpropdef` has a contract.
168 private class CallSiteVisitor
171 var toolcontext
: ToolContext
173 # Actual visited method
174 var visited_method
: APropdef is noinit
176 redef fun visit
(node
)
178 node
.check_callsite
(self)
182 # Check if the callsite calls a method with a contract.
183 # If it's the case the callsite is replaced by another callsite to the contract method.
184 private fun drive_method_contract
(callsite
: CallSite): CallSite
186 if callsite
.mproperty
.mcontract_facet
!= null then
187 var contract_facet
= callsite
.mproperty
.mcontract_facet
188 var visited_mpropdef
= visited_method
.mpropdef
189 assert contract_facet
!= null and visited_mpropdef
!= null
191 var type_visitor
= new TypeVisitor(toolcontext
.modelbuilder
, visited_mpropdef
)
192 var drived_callsite
= type_visitor
.build_callsite_by_property
(visited_method
, callsite
.recv
, contract_facet
, callsite
.recv_is_self
)
193 # This never happen this check is here for debug
194 assert drived_callsite
!= null
195 return drived_callsite
202 private fun create_contracts
(v
: ContractsVisitor) do end
203 private fun check_callsite
(v
: CallSiteVisitor) do end
206 redef class AAnnotation
208 # Returns the conditions of annotation parameters in the form of and expr
210 # the contract ensure(true, i == 10, f >= 1.0)
211 # return this condition (true and i == 10 and f >= 1.0)
212 private fun construct_condition
(v
: ContractsVisitor): AExpr
214 var n_condition
= n_args
.first
216 for n_arg
in n_args
do n_condition
= v
.ast_builder
.make_and
(n_condition
, n_arg
)
221 # The root of all contracts
223 # The objective of this class is to provide the set
224 # of services must be implemented or provided by a contract
225 abstract class MContract
228 # Define the name of the contract
229 fun contract_name
: String is abstract
231 # Method use to diplay warning when the contract is not present at the introduction
232 private fun no_intro_contract
(v
: ContractsVisitor, a
: AAnnotation)do end
234 # Creating specific inheritance block contract
235 private fun create_nblock
(v
: ContractsVisitor, n_condition
: AExpr, args
: Array[AExpr]): ABlockExpr is abstract
237 # Method to adapt the `n_mpropdef.n_block` to the contract
238 private fun adapt_block_to_contract
(v
: ContractsVisitor, n_mpropdef
: AMethPropdef) is abstract
240 # Adapt the msignature specifically for the contract method
241 private fun adapt_specific_msignature
(m_signature
: MSignature): MSignature do return m_signature
.adapt_to_condition
243 # Adapt the nsignature specifically for the contract method
244 private fun adapt_specific_nsignature
(n_signature
: ASignature): ASignature do return n_signature
.adapt_to_condition
(null)
246 # Adapt the `m_signature` to the contract
247 # If it is not null call the specific adapt `m_signature` for the contract
248 private fun adapt_msignature
(m_signature
: nullable MSignature): MSignature
250 if m_signature
== null then return new MSignature(new Array[MParameter])
251 return adapt_specific_msignature
(m_signature
)
254 # Adapt the `n_signature` to the contract
255 # If it is not null call the specific adapt `n_signature` for the contract
256 private fun adapt_nsignature
(n_signature
: nullable ASignature): ASignature
258 if n_signature
== null then return new ASignature
259 return adapt_specific_nsignature
(n_signature
)
262 # Create a new empty contract
263 private fun create_empty_contract
(v
: ContractsVisitor, mclassdef
: MClassDef, msignature
: nullable MSignature, n_signature
: ASignature)
265 var n_contract_def
= intro_mclassdef
.mclass
.create_empty_method
(v
, self, mclassdef
, msignature
, n_signature
)
266 n_contract_def
.do_all
(v
.toolcontext
)
269 # Create the initial contract (intro)
270 # All contracts have the same implementation for the introduction.
274 # assert contract_condition
277 private fun create_intro_contract
(v
: ContractsVisitor, n_condition
: nullable AExpr, mclassdef
: MClassDef): AMethPropdef
279 var n_block
= v
.ast_builder
.make_block
280 if n_condition
!= null then
281 # Create a new tid to set the name of the assert for more explicit error
282 var tid
= new TId.init_tk
(self.location
)
283 tid
.text
= "{self.contract_name}"
284 n_block
.add v
.ast_builder
.make_assert
(tid
, n_condition
, null)
286 return make_contract
(v
, n_block
, mclassdef
)
289 # Create a contract with old (super) and the new conditions
290 private fun create_subcontract
(v
: ContractsVisitor, ncondition
: nullable AExpr, mclassdef
: MClassDef): AMethPropdef
292 var args
= v
.n_signature
.make_parameter_read
(v
.ast_builder
)
293 var n_block
= v
.ast_builder
.make_block
294 if ncondition
!= null then n_block
= self.create_nblock
(v
, ncondition
, args
)
295 return make_contract
(v
, n_block
, mclassdef
)
298 # Build a method with a specific block `n_block` in a specified `mclassdef`
299 private fun make_contract
(v
: ContractsVisitor, n_block
: AExpr, mclassdef
: MClassDef): AMethPropdef
301 var n_contractdef
= intro_mclassdef
.mclass
.create_empty_method
(v
, self, mclassdef
, v
.m_signature
, v
.n_signature
)
302 n_contractdef
.n_block
= n_block
303 # Define the location of the new method for corresponding of the annotation location
304 n_contractdef
.location
= v
.current_location
305 n_contractdef
.do_all
(v
.toolcontext
)
310 # A expect (precondition) contract representation
311 # This method check if the requirements of the called method is true.
315 # Define the name of the contract
316 redef fun contract_name
: String do return "expect"
318 # Display warning if no contract is defined at introduction `expect`,
319 # because if no contract is defined at the introduction the added
320 # contracts will not cause any error even if they are not satisfied.
326 # fun _bar_expect([...])
333 # redef fun bar is expect(contract_condition)
334 # redef fun _bar_expect([...])
336 # if not (contract_condition) then super
341 redef fun no_intro_contract
(v
: ContractsVisitor, a
: AAnnotation)
343 v
.toolcontext
.warning
(a
.location
,"","Useless contract: No contract defined at the introduction of the method")
346 redef fun create_nblock
(v
: ContractsVisitor, n_condition
: AExpr, args
: Array[AExpr]): ABlockExpr
348 # Creating the if expression with the new condition
349 var if_block
= v
.ast_builder
.make_if
(n_condition
, n_condition
.mtype
)
350 # Creating and adding return expr to the then case
351 if_block
.n_then
= v
.ast_builder
.make_return
(null)
352 # Creating the super call to the contract and adding this to else case
353 if_block
.n_else
= v
.ast_builder
.make_super_call
(args
,null)
354 var n_block
= v
.ast_builder
.make_block
359 redef fun adapt_block_to_contract
(v
: ContractsVisitor, n_mpropdef
: AMethPropdef)
361 var callsite
= v
.ast_builder
.create_callsite
(v
.toolcontext
.modelbuilder
, n_mpropdef
, self, true)
362 var n_self
= new ASelfExpr
363 var args
= n_mpropdef
.n_signature
.make_parameter_read
(v
.ast_builder
)
364 var n_callexpect
= v
.ast_builder
.make_call
(n_self
,callsite
,args
)
365 # Creation of the new instruction block with the call to expect condition
366 var actual_expr
= n_mpropdef
.n_block
367 var new_block
= new ABlockExpr
368 new_block
.n_expr
.push n_callexpect
369 if actual_expr
isa ABlockExpr then
370 new_block
.n_expr
.add_all
(actual_expr
.n_expr
)
371 else if actual_expr
!= null then
372 new_block
.n_expr
.push
(actual_expr
)
374 n_mpropdef
.n_block
= new_block
378 # The root of all contracts where the call is after the execution of the original method (`invariant` and `ensure`).
379 abstract class BottomMContract
382 redef fun create_nblock
(v
: ContractsVisitor, n_condition
: AExpr, args
: Array[AExpr]): ABlockExpr
384 var tid
= new TId.init_tk
(v
.current_location
)
385 tid
.text
= "{contract_name}"
386 # Creating the assert expression with the new condition
387 var assert_block
= v
.ast_builder
.make_assert
(tid
,n_condition
,null)
388 # Creating the super call to the contract
389 var super_call
= v
.ast_builder
.make_super_call
(args
,null)
390 # Define contract block
391 var n_block
= v
.ast_builder
.make_block
392 # Adding the expressions to the block
393 n_block
.add super_call
394 n_block
.add assert_block
398 # Inject the result variable in the `n_block` of the given `n_mpropdef`.
399 private fun inject_result
(v
: ContractsVisitor, n_mpropdef
: AMethPropdef, ret_type
: MType): Variable
401 var actual_block
= n_mpropdef
.n_block
402 # never happen. If it's the case the problem is in the contract facet building
403 assert actual_block
isa ABlockExpr
405 var return_var
: nullable Variable = null
407 var return_expr
= actual_block
.n_expr
.last
.as(AReturnExpr)
409 var returned_expr
= return_expr
.n_expr
410 # The return node has no returned expression
411 assert returned_expr
!= null
413 # Check if the result variable already exit
414 if returned_expr
isa AVarExpr then
415 if returned_expr
.variable
.name
== "result" then
416 return_var
= returned_expr
.variable
420 return_var
= new Variable("result")
421 # Creating a new variable to keep the old return of the method
422 var assign_result
= v
.ast_builder
.make_var_assign
(return_var
, returned_expr
)
423 # Remove the actual return
424 actual_block
.n_expr
.pop
425 # Set the return type
426 return_var
.declared_type
= ret_type
427 # Adding the reading expr of result variable to the block
428 actual_block
.add assign_result
429 # Expr to read the result variable
430 var read_result
= v
.ast_builder
.make_var_read
(return_var
, ret_type
)
431 # Definition of the new return
432 return_expr
= v
.ast_builder
.make_return
(read_result
)
433 actual_block
.add return_expr
438 # A ensure (postcondition) representation
439 # This method check if the called method respects the expectations of the caller.
441 super BottomMContract
443 # Define the name of the contract
444 redef fun contract_name
: String do return "ensure"
446 redef fun adapt_specific_msignature
(m_signature
: MSignature): MSignature
448 return m_signature
.adapt_to_ensurecondition
451 redef fun adapt_specific_nsignature
(n_signature
: ASignature): ASignature
453 return n_signature
.adapt_to_ensurecondition
456 redef fun adapt_block_to_contract
(v
: ContractsVisitor, n_mpropdef
: AMethPropdef)
458 var callsite
= v
.ast_builder
.create_callsite
(v
.toolcontext
.modelbuilder
, n_mpropdef
, self, true)
459 var n_self
= new ASelfExpr
460 # argument to call the contract method
461 var args
= n_mpropdef
.n_signature
.make_parameter_read
(v
.ast_builder
)
462 # Inject the variable result
463 # The cast is always safe because the online adapted method is the contract facet
465 var actual_block
= n_mpropdef
.n_block
466 # never happen. If it's the case the problem is in the contract facet building
467 assert actual_block
isa ABlockExpr
469 var ret_type
= n_mpropdef
.mpropdef
.mproperty
.intro
.msignature
.return_mtype
470 if ret_type
!= null then
471 var result_var
= inject_result
(v
, n_mpropdef
, ret_type
)
472 # Expr to read the result variable
473 var read_result
= v
.ast_builder
.make_var_read
(result_var
, ret_type
)
474 var return_expr
= actual_block
.n_expr
.pop
475 # Adding the read return to argument
476 args
.add
(read_result
)
477 var n_callcontract
= v
.ast_builder
.make_call
(n_self
,callsite
,args
)
478 actual_block
.add_all
([n_callcontract
,return_expr
])
480 # build the call to the contract method
481 var n_callcontract
= v
.ast_builder
.make_call
(n_self
,callsite
,args
)
482 actual_block
.add n_callcontract
489 # This method create an abstract method representation with this MMethodDef an this AMethoddef
490 private fun create_abstract_method
(v
: ContractsVisitor, mproperty
: MMethod, mclassdef
: MClassDef, msignature
: nullable MSignature, n_signature
: ASignature): AMethPropdef
492 # new methoddef definition
493 var m_def
= new MMethodDef(mclassdef
, mproperty
, v
.current_location
)
495 if msignature
!= null then m_def
.msignature
= msignature
.clone
496 # set the abstract flag
497 m_def
.is_abstract
= true
498 # Build the new node method
499 var n_def
= v
.ast_builder
.make_method
(null,null,m_def
,n_signature
,null,null,null,null)
500 # Define the location of the new method for corresponding of the actual method
501 n_def
.location
= v
.current_location
502 # Association new npropdef to mpropdef
503 v
.toolcontext
.modelbuilder
.unsafe_add_mpropdef2npropdef
(m_def
,n_def
)
507 # Create method with an empty block
508 # the `mproperty` i.e the global property definition. The mclassdef to set where the method is declared and it's model `msignature` and ast `n_signature` signature
509 private fun create_empty_method
(v
: ContractsVisitor, mproperty
: MMethod, mclassdef
: MClassDef, msignature
: nullable MSignature, n_signature
: ASignature): AMethPropdef
511 var n_def
= create_abstract_method
(v
, mproperty
, mclassdef
, msignature
, n_signature
)
512 n_def
.mpropdef
.is_abstract
= false
513 n_def
.n_block
= v
.ast_builder
.make_block
520 # The contract facet of the method
521 # it's representing the method with contract
522 # This method call the contract and the method
523 var mcontract_facet
: nullable MMethod = null
525 # The expected contract method
526 var mexpect
: nullable MExpect = null
528 # The ensure contract method
529 var mensure
: nullable MEnsure = null
531 # Check if the MMethod has no ensure contract
532 # if this is the case returns false and built it
534 private fun check_exist_ensure
: Bool
536 if self.mensure
!= null then return true
537 # build a new `MEnsure` contract
538 self.mensure
= new MEnsure(intro_mclassdef
, "_ensure_{name}", intro_mclassdef
.location
, public_visibility
)
542 # Check if the MMethod has no expect contract
543 # if this is the case returns false and built it
545 private fun check_exist_expect
: Bool
547 if self.mexpect
!= null then return true
548 # build a new `MExpect` contract
549 self.mexpect
= new MExpect(intro_mclassdef
, "_expect_{name}", intro_mclassdef
.location
, public_visibility
)
553 # Check if the MMethod has an contract facet
554 # If the method has no contract facet she create it
555 private fun check_exist_contract_facet
(mpropdef
: MMethodDef): Bool
557 if self.mcontract_facet
!= null then return true
558 # build a new `MMethod` contract
559 self.mcontract_facet
= new MMethod(intro_mclassdef
, "_contract_{name}", intro_mclassdef
.location
, public_visibility
)
564 redef class MMethodDef
566 # Verification of the contract facet
567 # Check if a contract facet already exists to use it again or if it is necessary to create it.
568 private fun check_contract_facet
(v
: ContractsVisitor, n_signature
: ASignature, classdef
: MClassDef, mcontract
: MContract, exist_contract
: Bool)
570 var exist_contract_facet
= mproperty
.check_exist_contract_facet
(self)
571 if exist_contract_facet
and exist_contract
then return
573 var contract_facet
: AMethPropdef
574 if not exist_contract_facet
then
575 # If has no contract facet in intro just create it
576 if classdef
!= mproperty
.intro_mclassdef
then create_contract_facet
(v
, mproperty
.intro_mclassdef
, n_signature
)
577 # If has no contract facet just create it
578 contract_facet
= create_contract_facet
(v
, classdef
, n_signature
)
580 # Check if the contract facet already exist in this context (in this classdef)
581 if classdef
.mpropdefs_by_property
.has_key
(mproperty
.mcontract_facet
) then
583 contract_facet
= v
.toolcontext
.modelbuilder
.mpropdef2node
(classdef
.mpropdefs_by_property
[mproperty
.mcontract_facet
]).as(AMethPropdef)
585 # create a new contract facet definition
586 contract_facet
= create_contract_facet
(v
, classdef
, n_signature
)
587 var block
= v
.ast_builder
.make_block
588 # super call to the contract facet
589 var n_super_call
= v
.ast_builder
.make_super_call
(n_signature
.make_parameter_read
(v
.ast_builder
), null)
590 # verification for add a return or not
591 if contract_facet
.mpropdef
.msignature
.return_mtype
!= null then
592 block
.add
(v
.ast_builder
.make_return
(n_super_call
))
594 block
.add
(n_super_call
)
596 contract_facet
.n_block
= block
599 contract_facet
.adapt_block_to_contract
(v
, mcontract
, contract_facet
)
600 contract_facet
.location
= v
.current_location
601 contract_facet
.do_all
(v
.toolcontext
)
604 # Method to create a contract facet of the method
605 private fun create_contract_facet
(v
: ContractsVisitor, classdef
: MClassDef, n_signature
: ASignature): AMethPropdef
607 var contract_facet
= mproperty
.mcontract_facet
608 assert contract_facet
!= null
609 # Defines the contract phase is an init or not
610 # it is necessary to use the contracts on constructor
611 contract_facet
.is_init
= self.mproperty
.is_init
613 # check if the method has an `msignature` to copy it.
614 var m_signature
: nullable MSignature = null
615 if mproperty
.intro
.msignature
!= null then m_signature
= mproperty
.intro
.msignature
.clone
617 var n_contractdef
= classdef
.mclass
.create_empty_method
(v
, contract_facet
, classdef
, m_signature
, n_signature
)
618 # FIXME set the location because the callsite creation need the node location
619 n_contractdef
.location
= v
.current_location
620 n_contractdef
.validate
622 var block
= v
.ast_builder
.make_block
623 var n_self
= new ASelfExpr
624 var args
= n_contractdef
.n_signature
.make_parameter_read
(v
.ast_builder
)
625 var callsite
= v
.ast_builder
.create_callsite
(v
.toolcontext
.modelbuilder
, n_contractdef
, mproperty
, true)
626 var n_call
= v
.ast_builder
.make_call
(n_self
, callsite
, args
)
628 if m_signature
.return_mtype
== null then
631 block
.add
(v
.ast_builder
.make_return
(n_call
))
634 n_contractdef
.n_block
= block
635 n_contractdef
.do_all
(v
.toolcontext
)
639 # Entry point to build contract (define the contract facet and define the contract method verification)
640 private fun construct_contract
(v
: ContractsVisitor, n_signature
: ASignature, n_annotation
: AAnnotation, mcontract
: MContract, exist_contract
: Bool)
642 if check_same_contract
(v
, n_annotation
, mcontract
) then return
643 if not exist_contract
and not is_intro
then no_intro_contract
(v
, n_signature
, mcontract
, n_annotation
)
644 v
.define_signature
(mcontract
, n_signature
, mproperty
.intro
.msignature
)
646 var conditiondef
= v
.build_contract
(n_annotation
, mcontract
, mclassdef
)
647 check_contract_facet
(v
, n_signature
.clone
, mclassdef
, mcontract
, exist_contract
)
651 # Create a contract on the introduction classdef of the property.
652 # Display an warning message if necessary
653 private fun no_intro_contract
(v
: ContractsVisitor, n_signature
: ASignature, mcontract
: MContract, n_annotation
: AAnnotation)
655 mcontract
.create_empty_contract
(v
, mcontract
.intro_mclassdef
, mcontract
.adapt_msignature
(self.mproperty
.intro
.msignature
), mcontract
.adapt_nsignature
(n_signature
))
656 mcontract
.no_intro_contract
(v
, n_annotation
)
657 mproperty
.intro
.has_contract
= true
660 # Is the contract already defined in the context
663 # fun foo is expect([...]), expect([...])
665 # Here `check_same_contract` display an error when the second expect is processed
666 private fun check_same_contract
(v
: ContractsVisitor, n_annotation
: AAnnotation ,mcontract
: MContract): Bool
668 if self.mclassdef
.mpropdefs_by_property
.has_key
(mcontract
) then
669 v
.toolcontext
.error
(n_annotation
.location
, "The method already has a defined `{mcontract.contract_name}` contract at line {self.mclassdef.mpropdefs_by_property[mcontract].location.line_start}")
677 # flag to indicate is the `MPropDef` has a contract
678 var has_contract
= false
682 redef fun check_callsite
(v
)
684 v
.visited_method
= self
688 redef class AMethPropdef
690 # Execute all method verification scope flow and typing.
691 # It also execute an ast validation to define all parents and all locations
692 private fun do_all
(toolcontext
: ToolContext)
695 # FIXME: The `do_` usage it is maybe to much (verification...). Solution: Cut the `do_` methods into simpler parts
696 self.do_scope
(toolcontext
)
697 self.do_flow
(toolcontext
)
698 self.do_typing
(toolcontext
.modelbuilder
)
701 # Entry point to create a contract (verification of inheritance, or new contract).
702 redef fun create_contracts
(v
)
704 v
.ast_builder
.check_mmodule
(mpropdef
.mclassdef
.mmodule
)
706 v
.current_location
= self.location
707 v
.is_intro_contract
= mpropdef
.is_intro
709 if n_annotations
!= null then
710 for n_annotation
in n_annotations
.n_items
do
711 check_annotation
(v
,n_annotation
)
715 if not mpropdef
.is_intro
and not v
.find_no_contract
then
720 v
.find_no_contract
= false
723 # Verification of the annotation to know if it is a contract annotation.
724 # If this is the case, we built the appropriate contract.
725 private fun check_annotation
(v
: ContractsVisitor, n_annotation
: AAnnotation)
727 if n_annotation
.name
== "expect" then
728 if not v
.check_usage_expect
(mpropdef
.mclassdef
.mmodule
) then return
729 var exist_contract
= mpropdef
.mproperty
.check_exist_expect
730 mpropdef
.construct_contract
(v
, self.n_signature
.as(not null), n_annotation
, mpropdef
.mproperty
.mexpect
.as(not null), exist_contract
)
731 else if n_annotation
.name
== "ensure" then
732 if not v
.check_usage_ensure
(mpropdef
.mclassdef
.mmodule
) then return
733 var exist_contract
= mpropdef
.mproperty
.check_exist_ensure
734 mpropdef
.construct_contract
(v
, self.n_signature
.as(not null), n_annotation
, mpropdef
.mproperty
.mensure
.as(not null), exist_contract
)
735 else if n_annotation
.name
== "no_contract" then
736 # no_contract found set the flag to true
737 v
.find_no_contract
= true
741 # Verification if the method have an inherited contract to apply it.
742 private fun check_redef
(v
: ContractsVisitor)
744 # Check if the method has an attached contract
745 if not mpropdef
.has_contract
then
746 if mpropdef
.mproperty
.intro
.has_contract
then
747 mpropdef
.has_contract
= true
752 # Adapt the block to use the contracts
753 private fun adapt_block_to_contract
(v
: ContractsVisitor, contract
: MContract, n_mpropdef
: AMethPropdef)
755 contract
.adapt_block_to_contract
(v
, n_mpropdef
)
756 mpropdef
.has_contract
= true
757 n_mpropdef
.do_all
(v
.toolcontext
)
761 redef class MSignature
763 # Adapt signature for a expect condition
764 # Removed the return type is it not necessary
765 private fun adapt_to_condition
: MSignature do return new MSignature(mparameters
.to_a
, null)
767 # Adapt signature for a ensure condition
769 # Create new parameter with the return type
770 private fun adapt_to_ensurecondition
: MSignature
772 var rtype
= return_mtype
773 var msignature
= adapt_to_condition
774 if rtype
!= null then
775 msignature
.mparameters
.add
(new MParameter("result", rtype
, false))
780 # Adapt signature for a expect condition
781 # Removed the return type is it not necessary
782 private fun clone
: MSignature do return new MSignature(mparameters
.to_a
, return_mtype
)
785 redef class ASignature
787 # Create an array of AVarExpr representing the read of every parameters
788 private fun make_parameter_read
(ast_builder
: ASTBuilder): Array[AVarExpr]
790 var args
= new Array[AVarExpr]
791 for n_param
in self.n_params
do
792 var mtype
= n_param
.variable
.declared_type
793 var variable
= n_param
.variable
794 if variable
!= null and mtype
!= null then
795 args
.push ast_builder
.make_var_read
(variable
, mtype
)
801 # Return a copy of self adapted for the expect condition
802 # npropdef it is use to define the parent of the parameters
803 private fun adapt_to_condition
(return_type
: nullable AType): ASignature
805 var adapt_nsignature
= self.clone
806 adapt_nsignature
.n_type
= return_type
807 return adapt_nsignature
810 # Return a copy of self adapted for postcondition on npropdef
811 private fun adapt_to_ensurecondition
: ASignature do
812 var nsignature
= adapt_to_condition
(null)
813 if ret_type
!= null then
816 var new_param
= new AParam
817 new_param
.n_id
= n_id
818 new_param
.variable
= new Variable(n_id
.text
)
819 new_param
.variable
.declared_type
= ret_type
820 nsignature
.n_params
.add new_param
826 redef class ASendExpr
827 redef fun check_callsite
(v
)
829 var actual_callsite
= callsite
830 if actual_callsite
!= null then
831 callsite
= v
.drive_method_contract
(actual_callsite
)
837 redef fun check_callsite
(v
)
839 var actual_callsite
= callsite
840 if actual_callsite
!= null then
841 callsite
= v
.drive_method_contract
(actual_callsite
)