98d5eb2f3f454e1a8ec84aac6e26b4340ca19cb8
[nit.git] / src / contracts.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
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.
17 #
18 # FIXME Split the module in three parts: extension of the modele, building phase and the "re-driving"
19 module contracts
20
21 import astbuilder
22 import parse_annotations
23 import phase
24 import semantize
25 intrude import modelize_property
26 intrude import scope
27 intrude import typing
28
29 redef class ToolContext
30 # Parses contracts annotations.
31 var contracts_phase: Phase = new ContractsPhase(self, [modelize_property_phase,typing_phase])
32 end
33
34 private class ContractsPhase
35 super Phase
36
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)
44 end
45 end
46
47 redef class AModule
48 # Compile all contracts
49 #
50 # The implementation of the contracts is done in two visits.
51 #
52 # - First step, the visitor analyzes and constructs the contracts
53 # for each class (invariant) and method (expects, ensures).
54 #
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
59 #
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)
62 #
63 var callsite_visitor = new CallSiteVisitor(toolcontext)
64 callsite_visitor.enter_visit(self)
65 end
66 end
67
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
70 super Visitor
71
72 var toolcontext: ToolContext
73
74 # The main module
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
77
78 # Actual visited module
79 var visited_module: AModule
80
81 var ast_builder: ASTBuilder
82
83 # The `ASignature` of the actual build contract
84 var n_signature: ASignature is noinit
85
86 # The `MSignature` of the actual build contract
87 var m_signature: MSignature is noinit
88
89 # The `current_location` can corresponding of the annotation or method location.
90 var current_location: Location is noinit
91
92 # Is the contrat is an introduction or not?
93 var is_intro_contract: Bool is noinit
94
95 # Actual visited class
96 var visited_class: nullable AClassdef
97
98 # is `no_contract` annotation was found
99 var find_no_contract = false
100
101 redef fun visit(node)
102 do
103 node.create_contracts(self)
104 node.visit_all(self)
105 end
106
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)
110 do
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)
114 end
115
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
118 do
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)
126 else
127 # Create a redef of contract to take in consideration the new condition
128 m_contractdef = mcontract.create_subcontract(self, n_condition, mclassdef)
129 end
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
134 end
135
136 # Verification if the construction of the contract is necessary.
137 # Three cases are checked for `expects`:
138 #
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.
142 #
143 fun check_usage_expects(actual_mmodule: MModule): Bool
144 do
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
150 end
151 return false
152 end
153
154 # Verification if the construction of the contract is necessary.
155 # Two cases are checked for `ensures`:
156 #
157 # - Is the `--full-contract` option it's use?
158 # - Is the method is in the main package
159 #
160 fun check_usage_ensures(actual_mmodule: MModule): Bool
161 do
162 return toolcontext.opt_full_contract.value or mainmodule.mpackage == actual_mmodule.mpackage
163 end
164
165 end
166
167 # This visitor checks the `callsite` to see if the target `mpropdef` has a contract.
168 private class CallSiteVisitor
169 super Visitor
170
171 var toolcontext: ToolContext
172
173 # Actual visited method
174 var visited_method: APropdef is noinit
175
176 redef fun visit(node)
177 do
178 node.check_callsite(self)
179 node.visit_all(self)
180 end
181
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
185 do
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
190
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
196 end
197 return callsite
198 end
199 end
200
201 redef class ANode
202 private fun create_contracts(v: ContractsVisitor) do end
203 private fun check_callsite(v: CallSiteVisitor) do end
204 end
205
206 redef class AAnnotation
207
208 # Returns the conditions of annotation parameters in the form of and expr
209 # exemple:
210 # the contract ensures(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
213 do
214 var n_condition = n_args.first
215 n_args.remove_at(0)
216 for n_arg in n_args do n_condition = v.ast_builder.make_and(n_condition, n_arg)
217 return n_condition
218 end
219 end
220
221 # The root of all contracts
222 #
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
226 super MMethod
227
228 # Define the name of the contract
229 fun contract_name: String is abstract
230
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
233
234 # Creating specific inheritance block contract
235 private fun create_nblock(v: ContractsVisitor, n_condition: AExpr, args: Array[AExpr]): ABlockExpr is abstract
236
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
239
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
242
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)
245
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
249 do
250 if m_signature == null then return new MSignature(new Array[MParameter])
251 return adapt_specific_msignature(m_signature)
252 end
253
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
257 do
258 if n_signature == null then return new ASignature
259 return adapt_specific_nsignature(n_signature)
260 end
261
262 # Create a new empty contract
263 private fun create_empty_contract(v: ContractsVisitor, mclassdef: MClassDef, msignature: nullable MSignature, n_signature: ASignature)
264 do
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)
267 end
268
269 # Create the initial contract (intro)
270 # All contracts have the same implementation for the introduction.
271 #
272 # fun contrat([...])
273 # do
274 # assert contract_condition
275 # end
276 #
277 private fun create_intro_contract(v: ContractsVisitor, n_condition: nullable AExpr, mclassdef: MClassDef): AMethPropdef
278 do
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)
285 end
286 return make_contract(v, n_block, mclassdef)
287 end
288
289 # Create a contract with old (super) and the new conditions
290 private fun create_subcontract(v: ContractsVisitor, ncondition: nullable AExpr, mclassdef: MClassDef): AMethPropdef
291 do
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)
296 end
297
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
300 do
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)
306 return n_contractdef
307 end
308 end
309
310 # A expect (precondition) contract representation
311 # This method check if the requirements of the called method is true.
312 class MExpect
313 super MContract
314
315 # Define the name of the contract
316 redef fun contract_name: String do return "expects"
317
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.
321 #
322 # exemple
323 # ~~~nitish
324 # class A
325 # fun bar [...]
326 # fun _bar_expects([...])
327 # do
328 # [empty contract]
329 # end
330 # end
331 #
332 # redef class A
333 # redef fun bar is expects(contract_condition)
334 # redef fun _bar_expects([...])
335 # do
336 # if not (contract_condition) then super
337 # end
338 # end
339 # ~~~~
340 #
341 redef fun no_intro_contract(v: ContractsVisitor, a: AAnnotation)
342 do
343 v.toolcontext.warning(a.location,"","Useless contract: No contract defined at the introduction of the method")
344 end
345
346 redef fun create_nblock(v: ContractsVisitor, n_condition: AExpr, args: Array[AExpr]): ABlockExpr
347 do
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
355 n_block.add if_block
356 return n_block
357 end
358
359 redef fun adapt_block_to_contract(v: ContractsVisitor, n_mpropdef: AMethPropdef)
360 do
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)
373 end
374 n_mpropdef.n_block = new_block
375 end
376 end
377
378 # The root of all contracts where the call is after the execution of the original method (`invariants` and `ensures`).
379 abstract class BottomMContract
380 super MContract
381
382 redef fun create_nblock(v: ContractsVisitor, n_condition: AExpr, args: Array[AExpr]): ABlockExpr
383 do
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
395 return n_block
396 end
397
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
400 do
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
404
405 var return_var: nullable Variable = null
406
407 var return_expr = actual_block.n_expr.last.as(AReturnExpr)
408
409 var returned_expr = return_expr.n_expr
410 # The return node has no returned expression
411 assert returned_expr != null
412
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
417 end
418 end
419
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
434 return return_var
435 end
436 end
437
438 # A ensure (postcondition) representation
439 # This method check if the called method respects the expectations of the caller.
440 class MEnsure
441 super BottomMContract
442
443 # Define the name of the contract
444 redef fun contract_name: String do return "ensures"
445
446 redef fun adapt_specific_msignature(m_signature: MSignature): MSignature
447 do
448 return m_signature.adapt_to_ensurecondition
449 end
450
451 redef fun adapt_specific_nsignature(n_signature: ASignature): ASignature
452 do
453 return n_signature.adapt_to_ensurecondition
454 end
455
456 redef fun adapt_block_to_contract(v: ContractsVisitor, n_mpropdef: AMethPropdef)
457 do
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
464
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
468
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])
479 else
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
483 end
484 end
485 end
486
487 redef class MClass
488
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
491 do
492 # new methoddef definition
493 var m_def = new MMethodDef(mclassdef, mproperty, v.current_location)
494 # set the signature
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)
504 return n_def
505 end
506
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
510 do
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
514 return n_def
515 end
516 end
517
518 redef class MMethod
519
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
524
525 # The expected contract method
526 var mexpect: nullable MExpect = null
527
528 # The ensure contract method
529 var mensure: nullable MEnsure = null
530
531 # Check if the MMethod has no ensure contract
532 # if this is the case returns false and built it
533 # else return true
534 private fun check_exist_ensure: Bool
535 do
536 if self.mensure != null then return true
537 # build a new `MEnsure` contract
538 self.mensure = new MEnsure(intro_mclassdef, "_ensures_{name}", intro_mclassdef.location, public_visibility)
539 return false
540 end
541
542 # Check if the MMethod has no expect contract
543 # if this is the case returns false and built it
544 # else return true
545 private fun check_exist_expect: Bool
546 do
547 if self.mexpect != null then return true
548 # build a new `MExpect` contract
549 self.mexpect = new MExpect(intro_mclassdef, "_expects_{name}", intro_mclassdef.location, public_visibility)
550 return false
551 end
552
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
556 do
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)
560 return false
561 end
562 end
563
564 redef class MMethodDef
565
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)
569 do
570 var exist_contract_facet = mproperty.check_exist_contract_facet(self)
571 if exist_contract_facet and exist_contract then return
572
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)
579 else
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
582 # get the define
583 contract_facet = v.toolcontext.modelbuilder.mpropdef2node(classdef.mpropdefs_by_property[mproperty.mcontract_facet]).as(AMethPropdef)
584 else
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))
593 else
594 block.add(n_super_call)
595 end
596 contract_facet.n_block = block
597 end
598 end
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)
602 end
603
604 # Method to create a contract facet of the method
605 private fun create_contract_facet(v: ContractsVisitor, classdef: MClassDef, n_signature: ASignature): AMethPropdef
606 do
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
612
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
616
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
621
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)
627
628 if m_signature.return_mtype == null then
629 block.add(n_call)
630 else
631 block.add(v.ast_builder.make_return(n_call))
632 end
633
634 n_contractdef.n_block = block
635 n_contractdef.do_all(v.toolcontext)
636 return n_contractdef
637 end
638
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)
641 do
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)
645
646 var conditiondef = v.build_contract(n_annotation, mcontract, mclassdef)
647 check_contract_facet(v, n_signature.clone, mclassdef, mcontract, exist_contract)
648 has_contract = true
649 end
650
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)
654 do
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
658 end
659
660 # Is the contract already defined in the context
661 #
662 # Exemple :
663 # fun foo is expects([...]), expects([...])
664 #
665 # Here `check_same_contract` display an error when the second expects is processed
666 private fun check_same_contract(v: ContractsVisitor, n_annotation: AAnnotation ,mcontract: MContract): Bool
667 do
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}")
670 return true
671 end
672 return false
673 end
674 end
675
676 redef class MPropDef
677 # flag to indicate is the `MPropDef` has a contract
678 var has_contract = false
679 end
680
681 redef class APropdef
682 redef fun check_callsite(v)
683 do
684 v.visited_method = self
685 end
686 end
687
688 redef class AMethPropdef
689
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)
693 do
694 self.validate
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)
699 end
700
701 # Entry point to create a contract (verification of inheritance, or new contract).
702 redef fun create_contracts(v)
703 do
704 v.ast_builder.check_mmodule(mpropdef.mclassdef.mmodule)
705
706 v.current_location = self.location
707 v.is_intro_contract = mpropdef.is_intro
708
709 if n_annotations != null then
710 for n_annotation in n_annotations.n_items do
711 check_annotation(v,n_annotation)
712 end
713 end
714
715 if not mpropdef.is_intro and not v.find_no_contract then
716 self.check_redef(v)
717 end
718
719 # reset the flag
720 v.find_no_contract = false
721 end
722
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)
726 do
727 if n_annotation.name == "expects" then
728 if not v.check_usage_expects(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 == "ensures" then
732 if not v.check_usage_ensures(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
738 end
739 end
740
741 # Verification if the method have an inherited contract to apply it.
742 private fun check_redef(v: ContractsVisitor)
743 do
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
748 end
749 end
750 end
751
752 # Adapt the block to use the contracts
753 private fun adapt_block_to_contract(v: ContractsVisitor, contract: MContract, n_mpropdef: AMethPropdef)
754 do
755 contract.adapt_block_to_contract(v, n_mpropdef)
756 mpropdef.has_contract = true
757 n_mpropdef.do_all(v.toolcontext)
758 end
759 end
760
761 redef class MSignature
762
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)
766
767 # Adapt signature for a ensure condition
768 #
769 # Create new parameter with the return type
770 private fun adapt_to_ensurecondition: MSignature
771 do
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))
776 end
777 return msignature
778 end
779
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)
783 end
784
785 redef class ASignature
786
787 # Create an array of AVarExpr representing the read of every parameters
788 private fun make_parameter_read(ast_builder: ASTBuilder): Array[AVarExpr]
789 do
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)
796 end
797 end
798 return args
799 end
800
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
804 do
805 var adapt_nsignature = self.clone
806 adapt_nsignature.n_type = return_type
807 return adapt_nsignature
808 end
809
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
814 var n_id = new TId
815 n_id.text = "result"
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
821 end
822 return nsignature
823 end
824 end
825
826 redef class ASendExpr
827 redef fun check_callsite(v)
828 do
829 var actual_callsite = callsite
830 if actual_callsite != null then
831 callsite = v.drive_method_contract(actual_callsite)
832 end
833 end
834 end
835
836 redef class ANewExpr
837 redef fun check_callsite(v)
838 do
839 var actual_callsite = callsite
840 if actual_callsite != null then
841 callsite = v.drive_method_contract(actual_callsite)
842 end
843 end
844 end