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 # Visitor to build all contracts.
69 private class ContractsVisitor
72 # Instance of the toolcontext
73 var toolcontext
: ToolContext
76 # It's strore to define if the contract need to be build depending on the selected policy `--no-contract` `--full-contract` or default
77 var mainmodule
: MModule
79 # Actual visited module
80 var visited_module
: AModule
82 var ast_builder
: ASTBuilder
84 # The `ASignature` of the actual build contract
85 var n_signature
: ASignature is noinit
87 # The `MSignature` of the actual build contract
88 var m_signature
: MSignature is noinit
90 # The `current_location` can corresponding of the annotation or method location.
91 var current_location
: Location is noinit
93 # Is the contrat is an introduction or not?
94 # This attribute has the same value as the `is_intro` of the propdef attached to the contract.
95 # Note : For MClassDef `is_intro_contract == false`. This is due to the fact that a method for checking invariants is systematically added to the root object class.
96 var is_intro_contract
: Bool is noinit
98 # Actual visited class
99 var visited_class
: nullable AClassdef
101 # is `no_contract` annotation was found
102 var find_no_contract
= false
104 redef fun visit
(node
)
106 node
.create_contracts
(self)
110 # Method use to define the signature part of the method `ASignature` and `MSignature`
111 # The given `mcontract` provided `adapt_nsignature` and `adapt_msignature` to copy and adapt the given signature (`nsignature` and `msignature`)
112 fun define_signature
(mcontract
: MContract, nsignature
: nullable ASignature, msignature
: nullable MSignature)
114 if nsignature
!= null and msignature
!= null then nsignature
.ret_type
= msignature
.return_mtype
115 self.n_signature
= mcontract
.adapt_nsignature
(nsignature
)
116 self.m_signature
= mcontract
.adapt_msignature
(msignature
)
119 # Define the new contract take in consideration that an old contract exist or not
120 private fun build_contract
(n_annotation
: AAnnotation, mcontract
: MContract, mclassdef
: MClassDef): MMethodDef
122 self.current_location
= n_annotation
.location
123 # Retrieving the expression provided in the annotation
124 var n_condition
= n_annotation
.construct_condition
(self)
125 var m_contractdef
: AMethPropdef
126 if is_intro_contract
then
127 # Create new contract method
128 m_contractdef
= mcontract
.create_intro_contract
(self, n_condition
, mclassdef
)
130 # Create a redef of contract to take in consideration the new condition
131 m_contractdef
= mcontract
.create_subcontract
(self, n_condition
, mclassdef
)
133 var contract_propdef
= m_contractdef
.mpropdef
134 # The contract has a null mpropdef, this should never happen
135 assert contract_propdef
!= null
136 return contract_propdef
139 # Verification if the construction of the contract is necessary.
140 # Three cases are checked for `expect`:
142 # - Was the `--full-contract` option passed?
143 # - Is the method is in the main package?
144 # - Is the method is in a direct imported package?
146 fun check_usage_expect
(actual_mmodule
: MModule): Bool
148 var main_package
= mainmodule
.mpackage
149 var actual_package
= actual_mmodule
.mpackage
150 if main_package
!= null and actual_package
!= null then
151 var condition_direct_arc
= toolcontext
.modelbuilder
.model
.mpackage_importation_graph
.has_arc
(main_package
, actual_package
)
152 return toolcontext
.opt_full_contract
.value
or condition_direct_arc
or main_package
== actual_package
157 # Verification if the construction of the contract is necessary.
158 # Two cases are checked for `ensure`:
160 # - Was the `--full-contract` option passed?
161 # - Is the method is in the main package?
163 fun check_usage_ensure
(actual_mmodule
: MModule): Bool
165 return toolcontext
.opt_full_contract
.value
or mainmodule
.mpackage
== actual_mmodule
.mpackage
170 # This visitor checks the `callsite` to see if the target `mpropdef` has a contract.
171 private class CallSiteVisitor
175 # Instance of the toolcontext
176 var toolcontext
: ToolContext
178 # Actual visited method
179 var visited_method
: APropdef is noinit
181 redef fun visit
(node
)
183 node
.check_callsite
(self)
187 # Check if the callsite calls a method with a contract.
188 # If it's the case the callsite is replaced by another callsite to the contract method.
189 private fun drive_method_contract
(callsite
: CallSite): CallSite
191 if callsite
.mproperty
.mcontract_facet
!= null then
192 var contract_facet
= callsite
.mproperty
.mcontract_facet
193 var visited_mpropdef
= visited_method
.mpropdef
194 assert contract_facet
!= null and visited_mpropdef
!= null
196 var type_visitor
= new TypeVisitor(toolcontext
.modelbuilder
, visited_mpropdef
)
197 var drived_callsite
= type_visitor
.build_callsite_by_property
(visited_method
, callsite
.recv
, contract_facet
, callsite
.recv_is_self
)
198 # This never happen this check is here for debug
199 assert drived_callsite
!= null
200 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. If there are several parameters, the result is an `AAndExpr`
215 # the contract `ensure(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.
281 # assert contract_condition
285 private fun create_intro_contract
(v
: ContractsVisitor, n_condition
: nullable AExpr, mclassdef
: MClassDef): AMethPropdef
287 var n_block
= v
.ast_builder
.make_block
288 if n_condition
!= null then
289 # Create a new tid to set the name of the assert for more explicit error
290 var tid
= new TId.init_tk
(self.location
)
291 tid
.text
= "{self.contract_name}"
292 n_block
.add v
.ast_builder
.make_assert
(tid
, n_condition
, null)
294 return make_contract
(v
, n_block
, mclassdef
)
297 # Create a contract with old (super) and the new conditions
298 private fun create_subcontract
(v
: ContractsVisitor, ncondition
: nullable AExpr, mclassdef
: MClassDef): AMethPropdef
300 var args
= v
.n_signature
.make_parameter_read
(v
.ast_builder
)
301 var n_block
= v
.ast_builder
.make_block
302 if ncondition
!= null then n_block
= self.create_nblock
(v
, ncondition
, args
)
303 return make_contract
(v
, n_block
, mclassdef
)
306 # Build a method with a specific block `n_block` in a specified `mclassdef`
307 private fun make_contract
(v
: ContractsVisitor, n_block
: AExpr, mclassdef
: MClassDef): AMethPropdef
309 var n_contractdef
= intro_mclassdef
.mclass
.create_empty_method
(v
, self, mclassdef
, v
.m_signature
, v
.n_signature
)
310 n_contractdef
.n_block
= n_block
311 # Define the location of the new method for corresponding of the annotation location
312 n_contractdef
.location
= v
.current_location
313 n_contractdef
.do_all
(v
.toolcontext
)
318 # A expect (precondition) contract representation
319 # This method check if the requirements of the called method is true.
323 # Define the name of the contract
324 redef fun contract_name
: String do return "expect"
326 # Display warning if no contract is defined at introduction `expect`,
327 # because if no contract is defined at the introduction the added
328 # contracts will not cause any error even if they are not satisfied.
334 # fun _bar_expect([...])
341 # redef fun bar is expect(contract_condition)
342 # redef fun _bar_expect([...])
344 # if (contract_condition) then return
350 redef fun no_intro_contract
(v
: ContractsVisitor, a
: AAnnotation)
352 v
.toolcontext
.warning
(a
.location
,"","Useless contract: No contract defined at the introduction of the method")
355 redef fun create_nblock
(v
: ContractsVisitor, n_condition
: AExpr, args
: Array[AExpr]): ABlockExpr
357 # Creating the if expression with the new condition
358 var if_block
= v
.ast_builder
.make_if
(n_condition
, n_condition
.mtype
)
359 # Creating and adding return expr to the then case
360 if_block
.n_then
= v
.ast_builder
.make_return
(null)
361 # Creating the super call to the contract and adding this to else case
362 if_block
.n_else
= v
.ast_builder
.make_super_call
(args
,null)
363 var n_block
= v
.ast_builder
.make_block
368 redef fun adapt_block_to_contract
(v
: ContractsVisitor, n_mpropdef
: AMethPropdef)
370 var callsite
= v
.ast_builder
.create_callsite
(v
.toolcontext
.modelbuilder
, n_mpropdef
, self, true)
371 var n_self
= new ASelfExpr
372 var args
= n_mpropdef
.n_signature
.make_parameter_read
(v
.ast_builder
)
373 var n_callexpect
= v
.ast_builder
.make_call
(n_self
,callsite
,args
)
374 # Creation of the new instruction block with the call to expect condition
375 var actual_expr
= n_mpropdef
.n_block
376 var new_block
= new ABlockExpr
377 new_block
.n_expr
.push n_callexpect
378 if actual_expr
isa ABlockExpr then
379 new_block
.n_expr
.add_all
(actual_expr
.n_expr
)
380 else if actual_expr
!= null then
381 new_block
.n_expr
.push
(actual_expr
)
383 n_mpropdef
.n_block
= new_block
387 # The root of all contracts where the call is after the execution of the original method (`invariant` and `ensure`).
388 abstract class BottomMContract
391 redef fun create_nblock
(v
: ContractsVisitor, n_condition
: AExpr, args
: Array[AExpr]): ABlockExpr
393 var tid
= new TId.init_tk
(v
.current_location
)
394 tid
.text
= "{contract_name}"
395 # Creating the assert expression with the new condition
396 var assert_block
= v
.ast_builder
.make_assert
(tid
,n_condition
,null)
397 # Creating the super call to the contract
398 var super_call
= v
.ast_builder
.make_super_call
(args
,null)
399 # Define contract block
400 var n_block
= v
.ast_builder
.make_block
401 # Adding the expressions to the block
402 n_block
.add super_call
403 n_block
.add assert_block
407 # Inject the `result` variable into the `n_block` of the given n_mpropdef`.
409 # The purpose of the variable is to capture return values to use it in contracts.
410 private fun inject_result
(v
: ContractsVisitor, n_mpropdef
: AMethPropdef, ret_type
: MType): Variable
412 var actual_block
= n_mpropdef
.n_block
413 # never happen. If it's the case the problem is in the contract facet building
414 assert actual_block
isa ABlockExpr
416 var return_var
: nullable Variable = null
418 var return_expr
= actual_block
.n_expr
.last
.as(AReturnExpr)
420 var returned_expr
= return_expr
.n_expr
421 # The return node has no returned expression
422 assert returned_expr
!= null
424 # Check if the result variable already exit
425 if returned_expr
isa AVarExpr then
426 if returned_expr
.variable
.name
== "result" then
427 return_var
= returned_expr
.variable
431 return_var
= new Variable("result")
432 # Creating a new variable to keep the old return of the method
433 var assign_result
= v
.ast_builder
.make_var_assign
(return_var
, returned_expr
)
434 # Remove the actual return
435 actual_block
.n_expr
.pop
436 # Set the return type
437 return_var
.declared_type
= ret_type
438 # Adding the reading expr of result variable to the block
439 actual_block
.add assign_result
440 # Expr to read the result variable
441 var read_result
= v
.ast_builder
.make_var_read
(return_var
, ret_type
)
442 # Definition of the new return
443 return_expr
= v
.ast_builder
.make_return
(read_result
)
444 actual_block
.add return_expr
449 # A ensure (postcondition) representation
450 # This method check if the called method respects the expectations of the caller.
452 super BottomMContract
454 # Define the name of the contract
455 redef fun contract_name
: String do return "ensure"
457 redef fun adapt_specific_msignature
(m_signature
: MSignature): MSignature
459 return m_signature
.adapt_to_ensurecondition
462 redef fun adapt_specific_nsignature
(n_signature
: ASignature): ASignature
464 return n_signature
.adapt_to_ensurecondition
467 redef fun adapt_block_to_contract
(v
: ContractsVisitor, n_mpropdef
: AMethPropdef)
469 var callsite
= v
.ast_builder
.create_callsite
(v
.toolcontext
.modelbuilder
, n_mpropdef
, self, true)
470 var n_self
= new ASelfExpr
471 # argument to call the contract method
472 var args
= n_mpropdef
.n_signature
.make_parameter_read
(v
.ast_builder
)
473 # Inject the variable result
474 # The cast is always safe because the online adapted method is the contract facet
476 var actual_block
= n_mpropdef
.n_block
477 # never happen. If it's the case the problem is in the contract facet building
478 assert actual_block
isa ABlockExpr
480 var ret_type
= n_mpropdef
.mpropdef
.mproperty
.intro
.msignature
.return_mtype
481 if ret_type
!= null then
482 var result_var
= inject_result
(v
, n_mpropdef
, ret_type
)
483 # Expr to read the result variable
484 var read_result
= v
.ast_builder
.make_var_read
(result_var
, ret_type
)
485 var return_expr
= actual_block
.n_expr
.pop
486 # Adding the read return to argument
487 args
.add
(read_result
)
488 var n_callcontract
= v
.ast_builder
.make_call
(n_self
,callsite
,args
)
489 actual_block
.add_all
([n_callcontract
,return_expr
])
491 # build the call to the contract method
492 var n_callcontract
= v
.ast_builder
.make_call
(n_self
,callsite
,args
)
493 actual_block
.add n_callcontract
500 # This method create an abstract method representation with this MMethodDef an this AMethoddef
501 private fun create_abstract_method
(v
: ContractsVisitor, mproperty
: MMethod, mclassdef
: MClassDef, msignature
: nullable MSignature, n_signature
: ASignature): AMethPropdef
503 # new methoddef definition
504 var m_def
= new MMethodDef(mclassdef
, mproperty
, v
.current_location
)
506 if msignature
!= null then m_def
.msignature
= msignature
.clone
507 # set the abstract flag
508 m_def
.is_abstract
= true
509 # Build the new node method
510 var n_def
= v
.ast_builder
.make_method
(null,null,m_def
,n_signature
,null,null,null,null)
511 # Define the location of the new method for corresponding of the actual method
512 n_def
.location
= v
.current_location
513 # Association new npropdef to mpropdef
514 v
.toolcontext
.modelbuilder
.unsafe_add_mpropdef2npropdef
(m_def
,n_def
)
518 # Create method with an empty block
519 # 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
520 private fun create_empty_method
(v
: ContractsVisitor, mproperty
: MMethod, mclassdef
: MClassDef, msignature
: nullable MSignature, n_signature
: ASignature): AMethPropdef
522 var n_def
= create_abstract_method
(v
, mproperty
, mclassdef
, msignature
, n_signature
)
523 n_def
.mpropdef
.is_abstract
= false
524 n_def
.n_block
= v
.ast_builder
.make_block
531 # The contract facet of the method
532 # it's representing the method with contract
533 # This method call the contract and the method
534 var mcontract_facet
: nullable MMethod = null
536 # The expected contract method
537 var mexpect
: nullable MExpect = null
539 # The ensure contract method
540 var mensure
: nullable MEnsure = null
542 # Check if the MMethod has no ensure contract
543 # if this is the case returns false and built it
545 private fun check_exist_ensure
: Bool
547 if self.mensure
!= null then return true
548 # build a new `MEnsure` contract
549 self.mensure
= new MEnsure(intro_mclassdef
, "_ensure_{name}", intro_mclassdef
.location
, public_visibility
)
553 # Check if the MMethod has no expect contract
554 # if this is the case returns false and built it
556 private fun check_exist_expect
: Bool
558 if self.mexpect
!= null then return true
559 # build a new `MExpect` contract
560 self.mexpect
= new MExpect(intro_mclassdef
, "_expect_{name}", intro_mclassdef
.location
, public_visibility
)
564 # Check if the MMethod has an contract facet
565 # If the method has no contract facet she create it
566 private fun check_exist_contract_facet
(mpropdef
: MMethodDef): Bool
568 if self.mcontract_facet
!= null then return true
569 # build a new `MMethod` contract
570 self.mcontract_facet
= new MMethod(intro_mclassdef
, "_contract_{name}", intro_mclassdef
.location
, public_visibility
)
575 redef class MMethodDef
577 # Verification of the contract facet
578 # Check if a contract facet already exists to use it again or if it is necessary to create it.
579 private fun check_contract_facet
(v
: ContractsVisitor, n_signature
: ASignature, classdef
: MClassDef, mcontract
: MContract, exist_contract
: Bool)
581 var exist_contract_facet
= mproperty
.check_exist_contract_facet
(self)
582 if exist_contract_facet
and exist_contract
then return
584 var contract_facet
: AMethPropdef
585 if not exist_contract_facet
then
586 # If has no contract facet in intro just create it
587 if classdef
!= mproperty
.intro_mclassdef
then create_contract_facet
(v
, mproperty
.intro_mclassdef
, n_signature
)
588 # If has no contract facet just create it
589 contract_facet
= create_contract_facet
(v
, classdef
, n_signature
)
591 # Check if the contract facet already exist in this context (in this classdef)
592 if classdef
.mpropdefs_by_property
.has_key
(mproperty
.mcontract_facet
) then
594 contract_facet
= v
.toolcontext
.modelbuilder
.mpropdef2node
(classdef
.mpropdefs_by_property
[mproperty
.mcontract_facet
]).as(AMethPropdef)
596 # create a new contract facet definition
597 contract_facet
= create_contract_facet
(v
, classdef
, n_signature
)
598 var block
= v
.ast_builder
.make_block
599 # super call to the contract facet
600 var n_super_call
= v
.ast_builder
.make_super_call
(n_signature
.make_parameter_read
(v
.ast_builder
), null)
601 # verification for add a return or not
602 if contract_facet
.mpropdef
.msignature
.return_mtype
!= null then
603 block
.add
(v
.ast_builder
.make_return
(n_super_call
))
605 block
.add
(n_super_call
)
607 contract_facet
.n_block
= block
610 contract_facet
.adapt_block_to_contract
(v
, mcontract
, contract_facet
)
611 contract_facet
.location
= v
.current_location
612 contract_facet
.do_all
(v
.toolcontext
)
615 # Method to create a contract facet of the method
616 private fun create_contract_facet
(v
: ContractsVisitor, classdef
: MClassDef, n_signature
: ASignature): AMethPropdef
618 var contract_facet
= mproperty
.mcontract_facet
619 assert contract_facet
!= null
620 # Defines the contract phase is an init or not
621 # it is necessary to use the contracts on constructor
622 contract_facet
.is_init
= self.mproperty
.is_init
624 # check if the method has an `msignature` to copy it.
625 var m_signature
: nullable MSignature = null
626 if mproperty
.intro
.msignature
!= null then m_signature
= mproperty
.intro
.msignature
.clone
628 var n_contractdef
= classdef
.mclass
.create_empty_method
(v
, contract_facet
, classdef
, m_signature
, n_signature
)
629 # FIXME set the location because the callsite creation need the node location
630 n_contractdef
.location
= v
.current_location
631 n_contractdef
.validate
633 var block
= v
.ast_builder
.make_block
634 var n_self
= new ASelfExpr
635 var args
= n_contractdef
.n_signature
.make_parameter_read
(v
.ast_builder
)
636 var callsite
= v
.ast_builder
.create_callsite
(v
.toolcontext
.modelbuilder
, n_contractdef
, mproperty
, true)
637 var n_call
= v
.ast_builder
.make_call
(n_self
, callsite
, args
)
639 if m_signature
.return_mtype
== null then
642 block
.add
(v
.ast_builder
.make_return
(n_call
))
645 n_contractdef
.n_block
= block
646 n_contractdef
.do_all
(v
.toolcontext
)
650 # Entry point to build contract (define the contract facet and define the contract method verification)
651 private fun construct_contract
(v
: ContractsVisitor, n_signature
: ASignature, n_annotation
: AAnnotation, mcontract
: MContract, exist_contract
: Bool)
653 if check_same_contract
(v
, n_annotation
, mcontract
) then return
654 if not exist_contract
and not is_intro
then no_intro_contract
(v
, n_signature
, mcontract
, n_annotation
)
655 v
.define_signature
(mcontract
, n_signature
, mproperty
.intro
.msignature
)
657 var conditiondef
= v
.build_contract
(n_annotation
, mcontract
, mclassdef
)
658 check_contract_facet
(v
, n_signature
.clone
, mclassdef
, mcontract
, exist_contract
)
662 # Create a contract on the introduction classdef of the property.
663 # Display an warning message if necessary
664 private fun no_intro_contract
(v
: ContractsVisitor, n_signature
: ASignature, mcontract
: MContract, n_annotation
: AAnnotation)
666 mcontract
.create_empty_contract
(v
, mcontract
.intro_mclassdef
, mcontract
.adapt_msignature
(self.mproperty
.intro
.msignature
), mcontract
.adapt_nsignature
(n_signature
))
667 mcontract
.no_intro_contract
(v
, n_annotation
)
668 mproperty
.intro
.has_contract
= true
671 # Is the contract already defined in the context
674 # fun foo is expect([...]), expect([...])
676 # Here `check_same_contract` display an error when the second expect is processed
677 private fun check_same_contract
(v
: ContractsVisitor, n_annotation
: AAnnotation ,mcontract
: MContract): Bool
679 if self.mclassdef
.mpropdefs_by_property
.has_key
(mcontract
) then
680 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}")
688 # flag to indicate is the `MPropDef` has a contract
689 var has_contract
= false
693 redef fun check_callsite
(v
)
695 v
.visited_method
= self
699 redef class AMethPropdef
701 # Execute all method verification scope flow and typing.
702 # It also execute an ast validation to define all parents and all locations
703 private fun do_all
(toolcontext
: ToolContext)
706 # FIXME: The `do_` usage it is maybe to much (verification...). Solution: Cut the `do_` methods into simpler parts
707 self.do_scope
(toolcontext
)
708 self.do_flow
(toolcontext
)
709 self.do_typing
(toolcontext
.modelbuilder
)
712 # Entry point to create a contract (verification of inheritance, or new contract).
713 redef fun create_contracts
(v
)
715 v
.ast_builder
.check_mmodule
(mpropdef
.mclassdef
.mmodule
)
717 v
.current_location
= self.location
718 v
.is_intro_contract
= mpropdef
.is_intro
720 if n_annotations
!= null then
721 for n_annotation
in n_annotations
.n_items
do
722 check_annotation
(v
,n_annotation
)
726 if not mpropdef
.is_intro
and not v
.find_no_contract
then
731 v
.find_no_contract
= false
734 # Verification of the annotation to know if it is a contract annotation.
735 # If this is the case, we built the appropriate contract.
736 private fun check_annotation
(v
: ContractsVisitor, n_annotation
: AAnnotation)
738 if n_annotation
.name
== "expect" then
739 if not v
.check_usage_expect
(mpropdef
.mclassdef
.mmodule
) then return
740 var exist_contract
= mpropdef
.mproperty
.check_exist_expect
741 mpropdef
.construct_contract
(v
, self.n_signature
.as(not null), n_annotation
, mpropdef
.mproperty
.mexpect
.as(not null), exist_contract
)
742 else if n_annotation
.name
== "ensure" then
743 if not v
.check_usage_ensure
(mpropdef
.mclassdef
.mmodule
) then return
744 var exist_contract
= mpropdef
.mproperty
.check_exist_ensure
745 mpropdef
.construct_contract
(v
, self.n_signature
.as(not null), n_annotation
, mpropdef
.mproperty
.mensure
.as(not null), exist_contract
)
746 else if n_annotation
.name
== "no_contract" then
747 # no_contract found set the flag to true
748 v
.find_no_contract
= true
752 # Verification if the method have an inherited contract to apply it.
753 private fun check_redef
(v
: ContractsVisitor)
755 # Check if the method has an attached contract
756 if not mpropdef
.has_contract
then
757 if mpropdef
.mproperty
.intro
.has_contract
then
758 mpropdef
.has_contract
= true
763 # Adapt the block to use the contracts
764 private fun adapt_block_to_contract
(v
: ContractsVisitor, contract
: MContract, n_mpropdef
: AMethPropdef)
766 contract
.adapt_block_to_contract
(v
, n_mpropdef
)
767 mpropdef
.has_contract
= true
768 n_mpropdef
.do_all
(v
.toolcontext
)
772 redef class MSignature
774 # Adapt signature for a expect condition
775 # Removed the return type is it not necessary
776 private fun adapt_to_condition
: MSignature do return new MSignature(mparameters
.to_a
, null)
778 # Adapt signature for a ensure contract
780 # The returned `MSignature` is the copy of `self` without return type.
781 # The return type is replaced by a new parameter `result`
782 private fun adapt_to_ensurecondition
: MSignature
784 var rtype
= return_mtype
785 var msignature
= adapt_to_condition
786 if rtype
!= null then
787 msignature
.mparameters
.add
(new MParameter("result", rtype
, false))
792 # The returned `MSignature` is the exact copy of `self`.
793 private fun clone
: MSignature do return new MSignature(mparameters
.to_a
, return_mtype
)
796 redef class ASignature
798 # Create an array of AVarExpr representing the read of every parameters
799 private fun make_parameter_read
(ast_builder
: ASTBuilder): Array[AVarExpr]
801 var args
= new Array[AVarExpr]
802 for n_param
in self.n_params
do
803 var mtype
= n_param
.variable
.declared_type
804 var variable
= n_param
.variable
805 if variable
!= null and mtype
!= null then
806 args
.push ast_builder
.make_var_read
(variable
, mtype
)
812 # Create a new ASignature adapted for contract
814 # The returned `ASignature` is the copy of `self` without return type.
815 private fun adapt_to_condition
(return_type
: nullable AType): ASignature
817 var adapt_nsignature
= self.clone
818 adapt_nsignature
.n_type
= return_type
819 return adapt_nsignature
822 # Create a new ASignature adapted for ensure
824 # The returned `ASignature` is the copy of `self` without return type.
825 # The return type is replaced by a new parameter `result`
826 private fun adapt_to_ensurecondition
: ASignature do
827 var nsignature
= adapt_to_condition
(null)
828 if ret_type
!= null then
831 var new_param
= new AParam
832 new_param
.n_id
= n_id
833 new_param
.variable
= new Variable(n_id
.text
)
834 new_param
.variable
.declared_type
= ret_type
835 nsignature
.n_params
.add new_param
841 redef class ASendExpr
842 redef fun check_callsite
(v
)
844 var actual_callsite
= callsite
845 if actual_callsite
!= null then
846 callsite
= v
.drive_method_contract
(actual_callsite
)
852 redef fun check_callsite
(v
)
854 var actual_callsite
= callsite
855 if actual_callsite
!= null then
856 callsite
= v
.drive_method_contract
(actual_callsite
)