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 (expects, ensures).
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 `expects`:
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_expects
(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 `ensures`:
157 # - Is the `--full-contract` option it's use?
158 # - Is the method is in the main package
160 fun check_usage_ensures
(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
.mpropdef
.has_contract
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 unsafe_mtype
= callsite
.recv
.resolve_for
(visited_mpropdef
.mclassdef
.bound_mtype
, callsite
.anchor
, visited_mpropdef
.mclassdef
.mmodule
, true)
193 # This check is needed because the contract can appear after the introduction.
194 if unsafe_mtype
.has_mproperty
(visited_method
.mpropdef
.mclassdef
.mmodule
, contract_facet
) then
195 var type_visitor
= new TypeVisitor(toolcontext
.modelbuilder
, visited_mpropdef
)
196 var drived_callsite
= type_visitor
.build_callsite_by_property
(visited_method
, callsite
.recv
, contract_facet
, callsite
.recv_is_self
)
197 # This never happen this check is here for debug
198 assert drived_callsite
!= null
199 return drived_callsite
207 private fun create_contracts
(v
: ContractsVisitor) do end
208 private fun check_callsite
(v
: CallSiteVisitor) do end
211 redef class AAnnotation
213 # Returns the conditions of annotation parameters in the form of and expr
215 # the contract ensures(true, i == 10, f >= 1.0)
216 # return this condition (true and i == 10 and f >= 1.0)
217 private fun construct_condition
(v
: ContractsVisitor): AExpr
219 var n_condition
= n_args
.first
221 for n_arg
in n_args
do n_condition
= v
.ast_builder
.make_and
(n_condition
, n_arg
)
226 # The root of all contracts
228 # The objective of this class is to provide the set
229 # of services must be implemented or provided by a contract
230 abstract class MContract
233 # Define the name of the contract
234 fun contract_name
: String is abstract
236 # Method use to diplay warning when the contract is not present at the introduction
237 private fun no_intro_contract
(v
: ContractsVisitor, a
: AAnnotation)do end
239 # Creating specific inheritance block contract
240 private fun create_nblock
(v
: ContractsVisitor, n_condition
: AExpr, args
: Array[AExpr]): ABlockExpr is abstract
242 # Method to adapt the `n_mpropdef.n_block` to the contract
243 private fun adapt_block_to_contract
(v
: ContractsVisitor, n_mpropdef
: AMethPropdef) is abstract
245 # Adapt the msignature specifically for the contract method
246 private fun adapt_specific_msignature
(m_signature
: MSignature): MSignature do return m_signature
.adapt_to_condition
248 # Adapt the nsignature specifically for the contract method
249 private fun adapt_specific_nsignature
(n_signature
: ASignature): ASignature do return n_signature
.adapt_to_condition
(null)
251 # Adapt the `m_signature` to the contract
252 # If it is not null call the specific adapt `m_signature` for the contract
253 private fun adapt_msignature
(m_signature
: nullable MSignature): MSignature
255 if m_signature
== null then return new MSignature(new Array[MParameter])
256 return adapt_specific_msignature
(m_signature
)
259 # Adapt the `n_signature` to the contract
260 # If it is not null call the specific adapt `n_signature` for the contract
261 private fun adapt_nsignature
(n_signature
: nullable ASignature): ASignature
263 if n_signature
== null then return new ASignature
264 return adapt_specific_nsignature
(n_signature
)
267 # Create a new empty contract
268 private fun create_empty_contract
(v
: ContractsVisitor, mclassdef
: MClassDef, msignature
: nullable MSignature, n_signature
: ASignature)
270 var n_contract_def
= intro_mclassdef
.mclass
.create_empty_method
(v
, self, mclassdef
, msignature
, n_signature
)
271 n_contract_def
.do_all
(v
.toolcontext
)
274 # Create the initial contract (intro)
275 # All contracts have the same implementation for the introduction.
279 # assert contract_condition
282 private fun create_intro_contract
(v
: ContractsVisitor, n_condition
: nullable AExpr, mclassdef
: MClassDef): AMethPropdef
284 var n_block
= v
.ast_builder
.make_block
285 if n_condition
!= null then
286 # Create a new tid to set the name of the assert for more explicit error
287 var tid
= new TId.init_tk
(self.location
)
288 tid
.text
= "{self.contract_name}"
289 n_block
.add v
.ast_builder
.make_assert
(tid
, n_condition
, null)
291 return make_contract
(v
, n_block
, mclassdef
)
294 # Create a contract with old (super) and the new conditions
295 private fun create_subcontract
(v
: ContractsVisitor, ncondition
: nullable AExpr, mclassdef
: MClassDef): AMethPropdef
297 var args
= v
.n_signature
.make_parameter_read
(v
.ast_builder
)
298 var n_block
= v
.ast_builder
.make_block
299 if ncondition
!= null then n_block
= self.create_nblock
(v
, ncondition
, args
)
300 return make_contract
(v
, n_block
, mclassdef
)
303 # Build a method with a specific block `n_block` in a specified `mclassdef`
304 private fun make_contract
(v
: ContractsVisitor, n_block
: AExpr, mclassdef
: MClassDef): AMethPropdef
306 var n_contractdef
= intro_mclassdef
.mclass
.create_empty_method
(v
, self, mclassdef
, v
.m_signature
, v
.n_signature
)
307 n_contractdef
.n_block
= n_block
308 # Define the location of the new method for corresponding of the annotation location
309 n_contractdef
.location
= v
.current_location
310 n_contractdef
.do_all
(v
.toolcontext
)
315 # A expect (precondition) contract representation
316 # This method check if the requirements of the called method is true.
320 # Define the name of the contract
321 redef fun contract_name
: String do return "expects"
323 # Display warning if no contract is defined at introduction `expect`,
324 # because if no contract is defined at the introduction the added
325 # contracts will not cause any error even if they are not satisfied.
331 # fun _bar_expects([...])
338 # redef fun bar is expects(contract_condition)
339 # redef fun _bar_expects([...])
341 # if not (contract_condition) then super
346 redef fun no_intro_contract
(v
: ContractsVisitor, a
: AAnnotation)
348 v
.toolcontext
.warning
(a
.location
,"","Useless contract: No contract defined at the introduction of the method")
351 redef fun create_nblock
(v
: ContractsVisitor, n_condition
: AExpr, args
: Array[AExpr]): ABlockExpr
353 # Creating the if expression with the new condition
354 var if_block
= v
.ast_builder
.make_if
(n_condition
, n_condition
.mtype
)
355 # Creating and adding return expr to the then case
356 if_block
.n_then
= v
.ast_builder
.make_return
(null)
357 # Creating the super call to the contract and adding this to else case
358 if_block
.n_else
= v
.ast_builder
.make_super_call
(args
,null)
359 var n_block
= v
.ast_builder
.make_block
364 redef fun adapt_block_to_contract
(v
: ContractsVisitor, n_mpropdef
: AMethPropdef)
366 var callsite
= v
.ast_builder
.create_callsite
(v
.toolcontext
.modelbuilder
, n_mpropdef
, self, true)
367 var n_self
= new ASelfExpr
368 var args
= n_mpropdef
.n_signature
.make_parameter_read
(v
.ast_builder
)
369 var n_callexpect
= v
.ast_builder
.make_call
(n_self
,callsite
,args
)
370 # Creation of the new instruction block with the call to expect condition
371 var actual_expr
= n_mpropdef
.n_block
372 var new_block
= new ABlockExpr
373 new_block
.n_expr
.push n_callexpect
374 if actual_expr
isa ABlockExpr then
375 new_block
.n_expr
.add_all
(actual_expr
.n_expr
)
376 else if actual_expr
!= null then
377 new_block
.n_expr
.push
(actual_expr
)
379 n_mpropdef
.n_block
= new_block
383 # The root of all contracts where the call is after the execution of the original method (`invariants` and `ensures`).
384 abstract class BottomMContract
387 redef fun create_nblock
(v
: ContractsVisitor, n_condition
: AExpr, args
: Array[AExpr]): ABlockExpr
389 var tid
= new TId.init_tk
(v
.current_location
)
390 tid
.text
= "{contract_name}"
391 # Creating the assert expression with the new condition
392 var assert_block
= v
.ast_builder
.make_assert
(tid
,n_condition
,null)
393 # Creating the super call to the contract
394 var super_call
= v
.ast_builder
.make_super_call
(args
,null)
395 # Define contract block
396 var n_block
= v
.ast_builder
.make_block
397 # Adding the expressions to the block
398 n_block
.add super_call
399 n_block
.add assert_block
403 # Inject the result variable in the `n_block` of the given `n_mpropdef`.
404 private fun inject_result
(v
: ContractsVisitor, n_mpropdef
: AMethPropdef, ret_type
: MType): Variable
406 var actual_block
= n_mpropdef
.n_block
407 # never happen. If it's the case the problem is in the contract facet building
408 assert actual_block
isa ABlockExpr
410 var return_var
: nullable Variable = null
412 var return_expr
= actual_block
.n_expr
.last
.as(AReturnExpr)
414 var returned_expr
= return_expr
.n_expr
415 # The return node has no returned expression
416 assert returned_expr
!= null
418 # Check if the result variable already exit
419 if returned_expr
isa AVarExpr then
420 if returned_expr
.variable
.name
== "result" then
421 return_var
= returned_expr
.variable
425 return_var
= new Variable("result")
426 # Creating a new variable to keep the old return of the method
427 var assign_result
= v
.ast_builder
.make_var_assign
(return_var
, returned_expr
)
428 # Remove the actual return
429 actual_block
.n_expr
.pop
430 # Set the return type
431 return_var
.declared_type
= ret_type
432 # Adding the reading expr of result variable to the block
433 actual_block
.add assign_result
434 # Expr to read the result variable
435 var read_result
= v
.ast_builder
.make_var_read
(return_var
, ret_type
)
436 # Definition of the new return
437 return_expr
= v
.ast_builder
.make_return
(read_result
)
438 actual_block
.add return_expr
443 # A ensure (postcondition) representation
444 # This method check if the called method respects the expectations of the caller.
446 super BottomMContract
448 # Define the name of the contract
449 redef fun contract_name
: String do return "ensures"
451 redef fun adapt_specific_msignature
(m_signature
: MSignature): MSignature
453 return m_signature
.adapt_to_ensurecondition
456 redef fun adapt_specific_nsignature
(n_signature
: ASignature): ASignature
458 return n_signature
.adapt_to_ensurecondition
461 redef fun adapt_block_to_contract
(v
: ContractsVisitor, n_mpropdef
: AMethPropdef)
463 var callsite
= v
.ast_builder
.create_callsite
(v
.toolcontext
.modelbuilder
, n_mpropdef
, self, true)
464 var n_self
= new ASelfExpr
465 # argument to call the contract method
466 var args
= n_mpropdef
.n_signature
.make_parameter_read
(v
.ast_builder
)
467 # Inject the variable result
468 # The cast is always safe because the online adapted method is the contract facet
470 var actual_block
= n_mpropdef
.n_block
471 # never happen. If it's the case the problem is in the contract facet building
472 assert actual_block
isa ABlockExpr
474 var ret_type
= n_mpropdef
.mpropdef
.mproperty
.intro
.msignature
.return_mtype
475 if ret_type
!= null then
476 var result_var
= inject_result
(v
, n_mpropdef
, ret_type
)
477 # Expr to read the result variable
478 var read_result
= v
.ast_builder
.make_var_read
(result_var
, ret_type
)
479 var return_expr
= actual_block
.n_expr
.pop
480 # Adding the read return to argument
481 args
.add
(read_result
)
482 var n_callcontract
= v
.ast_builder
.make_call
(n_self
,callsite
,args
)
483 actual_block
.add_all
([n_callcontract
,return_expr
])
485 # build the call to the contract method
486 var n_callcontract
= v
.ast_builder
.make_call
(n_self
,callsite
,args
)
487 actual_block
.add n_callcontract
494 # This method create an abstract method representation with this MMethodDef an this AMethoddef
495 private fun create_abstract_method
(v
: ContractsVisitor, mproperty
: MMethod, mclassdef
: MClassDef, msignature
: nullable MSignature, n_signature
: ASignature): AMethPropdef
497 # new methoddef definition
498 var m_def
= new MMethodDef(mclassdef
, mproperty
, v
.current_location
)
500 if msignature
!= null then m_def
.msignature
= msignature
.clone
501 # set the abstract flag
502 m_def
.is_abstract
= true
503 # Build the new node method
504 var n_def
= v
.ast_builder
.make_method
(null,null,m_def
,n_signature
,null,null,null,null)
505 # Define the location of the new method for corresponding of the actual method
506 n_def
.location
= v
.current_location
507 # Association new npropdef to mpropdef
508 v
.toolcontext
.modelbuilder
.unsafe_add_mpropdef2npropdef
(m_def
,n_def
)
512 # Create method with an empty block
513 # 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
514 private fun create_empty_method
(v
: ContractsVisitor, mproperty
: MMethod, mclassdef
: MClassDef, msignature
: nullable MSignature, n_signature
: ASignature): AMethPropdef
516 var n_def
= create_abstract_method
(v
, mproperty
, mclassdef
, msignature
, n_signature
)
517 n_def
.mpropdef
.is_abstract
= false
518 n_def
.n_block
= v
.ast_builder
.make_block
525 # The contract facet of the method
526 # it's representing the method with contract
527 # This method call the contract and the method
528 var mcontract_facet
: nullable MMethod = null
530 # The expected contract method
531 var mexpect
: nullable MExpect = null
533 # The ensure contract method
534 var mensure
: nullable MEnsure = null
536 # Check if the MMethod has no ensure contract
537 # if this is the case returns false and built it
539 private fun check_exist_ensure
: Bool
541 if self.mensure
!= null then return true
542 # build a new `MEnsure` contract
543 self.mensure
= new MEnsure(intro_mclassdef
, "_ensures_{name}", intro_mclassdef
.location
, public_visibility
)
547 # Check if the MMethod has no expect contract
548 # if this is the case returns false and built it
550 private fun check_exist_expect
: Bool
552 if self.mexpect
!= null then return true
553 # build a new `MExpect` contract
554 self.mexpect
= new MExpect(intro_mclassdef
, "_expects_{name}", intro_mclassdef
.location
, public_visibility
)
558 # Check if the MMethod has an contract facet
559 # If the method has no contract facet she create it
560 private fun check_exist_contract_facet
(mpropdef
: MMethodDef): Bool
562 if self.mcontract_facet
!= null then return true
563 # build a new `MMethod` contract
564 self.mcontract_facet
= new MMethod(mpropdef
.mclassdef
, "_contract_{name}", mpropdef
.mclassdef
.location
, public_visibility
)
569 redef class MMethodDef
571 # Verification of the contract facet
572 # Check if a contract facet already exists to use it again or if it is necessary to create it.
573 private fun check_contract_facet
(v
: ContractsVisitor, n_signature
: ASignature, mcontract
: MContract, exist_contract
: Bool)
575 var exist_contract_facet
= mproperty
.check_exist_contract_facet
(self)
576 if exist_contract_facet
and exist_contract
then return
578 var contract_facet
: AMethPropdef
579 if not exist_contract_facet
then
580 # If has no contract facet just create it
581 contract_facet
= create_contract_facet
(v
, n_signature
)
583 # Check if the contract facet already exist in this context (in this mclassdef)
584 if mclassdef
.mpropdefs_by_property
.has_key
(mproperty
.mcontract_facet
) then
586 contract_facet
= v
.toolcontext
.modelbuilder
.mpropdef2node
(mclassdef
.mpropdefs_by_property
[mproperty
.mcontract_facet
]).as(AMethPropdef)
588 # create a new contract facet definition
589 contract_facet
= create_contract_facet
(v
, n_signature
)
590 var block
= v
.ast_builder
.make_block
591 # super call to the contract facet
592 var n_super_call
= v
.ast_builder
.make_super_call
(n_signature
.make_parameter_read
(v
.ast_builder
), null)
593 # verification for add a return or not
594 if contract_facet
.mpropdef
.msignature
.return_mtype
!= null then
595 block
.add
(v
.ast_builder
.make_return
(n_super_call
))
597 block
.add
(n_super_call
)
599 contract_facet
.n_block
= block
602 contract_facet
.adapt_block_to_contract
(v
, mcontract
, contract_facet
)
603 contract_facet
.location
= v
.current_location
604 contract_facet
.do_all
(v
.toolcontext
)
607 # Method to create a contract facet of the method
608 private fun create_contract_facet
(v
: ContractsVisitor, n_signature
: ASignature): AMethPropdef
610 var contract_facet
= mproperty
.mcontract_facet
611 assert contract_facet
!= null
612 # Defines the contract phase is an init or not
613 # it is necessary to use the contracts on constructor
614 contract_facet
.is_init
= self.mproperty
.is_init
616 # check if the method has an `msignature` to copy it.
617 var m_signature
: nullable MSignature = null
618 if mproperty
.intro
.msignature
!= null then m_signature
= mproperty
.intro
.msignature
.clone
620 var n_contractdef
= mclassdef
.mclass
.create_empty_method
(v
, contract_facet
, mclassdef
, m_signature
, n_signature
)
621 # FIXME set the location because the callsite creation need the node location
622 n_contractdef
.location
= v
.current_location
623 n_contractdef
.validate
625 var block
= v
.ast_builder
.make_block
626 var n_self
= new ASelfExpr
627 var args
= n_contractdef
.n_signature
.make_parameter_read
(v
.ast_builder
)
628 var callsite
= v
.ast_builder
.create_callsite
(v
.toolcontext
.modelbuilder
, n_contractdef
, mproperty
, true)
629 var n_call
= v
.ast_builder
.make_call
(n_self
, callsite
, args
)
631 if m_signature
.return_mtype
== null then
634 block
.add
(v
.ast_builder
.make_return
(n_call
))
637 n_contractdef
.n_block
= block
638 n_contractdef
.do_all
(v
.toolcontext
)
642 # Entry point to build contract (define the contract facet and define the contract method verification)
643 private fun construct_contract
(v
: ContractsVisitor, n_signature
: ASignature, n_annotation
: AAnnotation, mcontract
: MContract, exist_contract
: Bool)
645 if check_same_contract
(v
, n_annotation
, mcontract
) then return
646 if not exist_contract
and not is_intro
then no_intro_contract
(v
, n_signature
, mcontract
, n_annotation
)
647 v
.define_signature
(mcontract
, n_signature
, mproperty
.intro
.msignature
)
649 var conditiondef
= v
.build_contract
(n_annotation
, mcontract
, mclassdef
)
650 check_contract_facet
(v
, n_signature
.clone
, mcontract
, exist_contract
)
654 # Create a contract on the introduction classdef of the property.
655 # Display an warning message if necessary
656 private fun no_intro_contract
(v
: ContractsVisitor, n_signature
: ASignature, mcontract
: MContract, n_annotation
: AAnnotation)
658 mcontract
.create_empty_contract
(v
, mcontract
.intro_mclassdef
, mcontract
.adapt_msignature
(self.mproperty
.intro
.msignature
), mcontract
.adapt_nsignature
(n_signature
))
659 mcontract
.no_intro_contract
(v
, n_annotation
)
660 mproperty
.intro
.has_contract
= true
663 # Is the contract already defined in the context
666 # fun foo is expects([...]), expects([...])
668 # Here `check_same_contract` display an error when the second expects is processed
669 private fun check_same_contract
(v
: ContractsVisitor, n_annotation
: AAnnotation ,mcontract
: MContract): Bool
671 if self.mclassdef
.mpropdefs_by_property
.has_key
(mcontract
) then
672 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}")
680 # flag to indicate is the `MPropDef` has a contract
681 var has_contract
= false
685 redef fun check_callsite
(v
)
687 v
.visited_method
= self
691 redef class AMethPropdef
693 # Execute all method verification scope flow and typing.
694 # It also execute an ast validation to define all parents and all locations
695 private fun do_all
(toolcontext
: ToolContext)
698 # FIXME: The `do_` usage it is maybe to much (verification...). Solution: Cut the `do_` methods into simpler parts
699 self.do_scope
(toolcontext
)
700 self.do_flow
(toolcontext
)
701 self.do_typing
(toolcontext
.modelbuilder
)
704 # Entry point to create a contract (verification of inheritance, or new contract).
705 redef fun create_contracts
(v
)
707 v
.ast_builder
.check_mmodule
(mpropdef
.mclassdef
.mmodule
)
709 v
.current_location
= self.location
710 v
.is_intro_contract
= mpropdef
.is_intro
712 if n_annotations
!= null then
713 for n_annotation
in n_annotations
.n_items
do
714 check_annotation
(v
,n_annotation
)
718 if not mpropdef
.is_intro
and not v
.find_no_contract
then
723 v
.find_no_contract
= false
726 # Verification of the annotation to know if it is a contract annotation.
727 # If this is the case, we built the appropriate contract.
728 private fun check_annotation
(v
: ContractsVisitor, n_annotation
: AAnnotation)
730 if n_annotation
.name
== "expects" then
731 if not v
.check_usage_expects
(mpropdef
.mclassdef
.mmodule
) then return
732 var exist_contract
= mpropdef
.mproperty
.check_exist_expect
733 mpropdef
.construct_contract
(v
, self.n_signature
.as(not null), n_annotation
, mpropdef
.mproperty
.mexpect
.as(not null), exist_contract
)
734 else if n_annotation
.name
== "ensures" then
735 if not v
.check_usage_ensures
(mpropdef
.mclassdef
.mmodule
) then return
736 var exist_contract
= mpropdef
.mproperty
.check_exist_ensure
737 mpropdef
.construct_contract
(v
, self.n_signature
.as(not null), n_annotation
, mpropdef
.mproperty
.mensure
.as(not null), exist_contract
)
738 else if n_annotation
.name
== "no_contract" then
739 # no_contract found set the flag to true
740 v
.find_no_contract
= true
744 # Verification if the method have an inherited contract to apply it.
745 private fun check_redef
(v
: ContractsVisitor)
747 # Check if the method has an attached contract
748 if not mpropdef
.has_contract
then
749 if mpropdef
.mproperty
.intro
.has_contract
then
750 mpropdef
.has_contract
= true
755 # Adapt the block to use the contracts
756 private fun adapt_block_to_contract
(v
: ContractsVisitor, contract
: MContract, n_mpropdef
: AMethPropdef)
758 contract
.adapt_block_to_contract
(v
, n_mpropdef
)
759 mpropdef
.has_contract
= true
760 n_mpropdef
.do_all
(v
.toolcontext
)
764 redef class MSignature
766 # Adapt signature for a expect condition
767 # Removed the return type is it not necessary
768 private fun adapt_to_condition
: MSignature do return new MSignature(mparameters
.to_a
, null)
770 # Adapt signature for a ensure condition
772 # Create new parameter with the return type
773 private fun adapt_to_ensurecondition
: MSignature
775 var rtype
= return_mtype
776 var msignature
= adapt_to_condition
777 if rtype
!= null then
778 msignature
.mparameters
.add
(new MParameter("result", rtype
, false))
783 # Adapt signature for a expect condition
784 # Removed the return type is it not necessary
785 private fun clone
: MSignature do return new MSignature(mparameters
.to_a
, return_mtype
)
788 redef class ASignature
790 # Create an array of AVarExpr representing the read of every parameters
791 private fun make_parameter_read
(ast_builder
: ASTBuilder): Array[AVarExpr]
793 var args
= new Array[AVarExpr]
794 for n_param
in self.n_params
do
795 var mtype
= n_param
.variable
.declared_type
796 var variable
= n_param
.variable
797 if variable
!= null and mtype
!= null then
798 args
.push ast_builder
.make_var_read
(variable
, mtype
)
804 # Return a copy of self adapted for the expect condition
805 # npropdef it is use to define the parent of the parameters
806 private fun adapt_to_condition
(return_type
: nullable AType): ASignature
808 var adapt_nsignature
= self.clone
809 adapt_nsignature
.n_type
= return_type
810 return adapt_nsignature
813 # Return a copy of self adapted for postcondition on npropdef
814 private fun adapt_to_ensurecondition
: ASignature do
815 var nsignature
= adapt_to_condition
(null)
816 if ret_type
!= null then
819 var new_param
= new AParam
820 new_param
.n_id
= n_id
821 new_param
.variable
= new Variable(n_id
.text
)
822 new_param
.variable
.declared_type
= ret_type
823 nsignature
.n_params
.add new_param
829 redef class ASendExpr
830 redef fun check_callsite
(v
)
832 var actual_callsite
= callsite
833 if actual_callsite
!= null then
834 callsite
= v
.drive_method_contract
(actual_callsite
)
840 redef fun check_callsite
(v
)
842 var actual_callsite
= callsite
843 if actual_callsite
!= null then
844 callsite
= v
.drive_method_contract
(actual_callsite
)