d80abeed8ce9176dfe842d2b1d695c48e9274f9f
[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.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
190
191 var unsafe_mtype = callsite.recv.resolve_for(visited_mpropdef.mclassdef.bound_mtype, callsite.anchor, visited_mpropdef.mclassdef.mmodule, true)
192
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
200 end
201 end
202 return callsite
203 end
204 end
205
206 redef class ANode
207 private fun create_contracts(v: ContractsVisitor) do end
208 private fun check_callsite(v: CallSiteVisitor) do end
209 end
210
211 redef class AAnnotation
212
213 # Returns the conditions of annotation parameters in the form of and expr
214 # exemple:
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
218 do
219 var n_condition = n_args.first
220 n_args.remove_at(0)
221 for n_arg in n_args do n_condition = v.ast_builder.make_and(n_condition, n_arg)
222 return n_condition
223 end
224 end
225
226 # The root of all contracts
227 #
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
231 super MMethod
232
233 # Define the name of the contract
234 fun contract_name: String is abstract
235
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
238
239 # Creating specific inheritance block contract
240 private fun create_nblock(v: ContractsVisitor, n_condition: AExpr, args: Array[AExpr]): ABlockExpr is abstract
241
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
244
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
247
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)
250
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
254 do
255 if m_signature == null then return new MSignature(new Array[MParameter])
256 return adapt_specific_msignature(m_signature)
257 end
258
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
262 do
263 if n_signature == null then return new ASignature
264 return adapt_specific_nsignature(n_signature)
265 end
266
267 # Create a new empty contract
268 private fun create_empty_contract(v: ContractsVisitor, mclassdef: MClassDef, msignature: nullable MSignature, n_signature: ASignature)
269 do
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)
272 end
273
274 # Create the initial contract (intro)
275 # All contracts have the same implementation for the introduction.
276 #
277 # fun contrat([...])
278 # do
279 # assert contract_condition
280 # end
281 #
282 private fun create_intro_contract(v: ContractsVisitor, n_condition: nullable AExpr, mclassdef: MClassDef): AMethPropdef
283 do
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)
290 end
291 return make_contract(v, n_block, mclassdef)
292 end
293
294 # Create a contract with old (super) and the new conditions
295 private fun create_subcontract(v: ContractsVisitor, ncondition: nullable AExpr, mclassdef: MClassDef): AMethPropdef
296 do
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)
301 end
302
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
305 do
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)
311 return n_contractdef
312 end
313 end
314
315 # A expect (precondition) contract representation
316 # This method check if the requirements of the called method is true.
317 class MExpect
318 super MContract
319
320 # Define the name of the contract
321 redef fun contract_name: String do return "expects"
322
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.
326 #
327 # exemple
328 # ~~~nitish
329 # class A
330 # fun bar [...]
331 # fun _bar_expects([...])
332 # do
333 # [empty contract]
334 # end
335 # end
336 #
337 # redef class A
338 # redef fun bar is expects(contract_condition)
339 # redef fun _bar_expects([...])
340 # do
341 # if not (contract_condition) then super
342 # end
343 # end
344 # ~~~~
345 #
346 redef fun no_intro_contract(v: ContractsVisitor, a: AAnnotation)
347 do
348 v.toolcontext.warning(a.location,"","Useless contract: No contract defined at the introduction of the method")
349 end
350
351 redef fun create_nblock(v: ContractsVisitor, n_condition: AExpr, args: Array[AExpr]): ABlockExpr
352 do
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
360 n_block.add if_block
361 return n_block
362 end
363
364 redef fun adapt_block_to_contract(v: ContractsVisitor, n_mpropdef: AMethPropdef)
365 do
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)
378 end
379 n_mpropdef.n_block = new_block
380 end
381 end
382
383 # The root of all contracts where the call is after the execution of the original method (`invariants` and `ensures`).
384 abstract class BottomMContract
385 super MContract
386
387 redef fun create_nblock(v: ContractsVisitor, n_condition: AExpr, args: Array[AExpr]): ABlockExpr
388 do
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
400 return n_block
401 end
402
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
405 do
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
409
410 var return_var: nullable Variable = null
411
412 var return_expr = actual_block.n_expr.last.as(AReturnExpr)
413
414 var returned_expr = return_expr.n_expr
415 # The return node has no returned expression
416 assert returned_expr != null
417
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
422 end
423 end
424
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
439 return return_var
440 end
441 end
442
443 # A ensure (postcondition) representation
444 # This method check if the called method respects the expectations of the caller.
445 class MEnsure
446 super BottomMContract
447
448 # Define the name of the contract
449 redef fun contract_name: String do return "ensures"
450
451 redef fun adapt_specific_msignature(m_signature: MSignature): MSignature
452 do
453 return m_signature.adapt_to_ensurecondition
454 end
455
456 redef fun adapt_specific_nsignature(n_signature: ASignature): ASignature
457 do
458 return n_signature.adapt_to_ensurecondition
459 end
460
461 redef fun adapt_block_to_contract(v: ContractsVisitor, n_mpropdef: AMethPropdef)
462 do
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
469
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
473
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])
484 else
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
488 end
489 end
490 end
491
492 redef class MClass
493
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
496 do
497 # new methoddef definition
498 var m_def = new MMethodDef(mclassdef, mproperty, v.current_location)
499 # set the signature
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)
509 return n_def
510 end
511
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
515 do
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
519 return n_def
520 end
521 end
522
523 redef class MMethod
524
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
529
530 # The expected contract method
531 var mexpect: nullable MExpect = null
532
533 # The ensure contract method
534 var mensure: nullable MEnsure = null
535
536 # Check if the MMethod has no ensure contract
537 # if this is the case returns false and built it
538 # else return true
539 private fun check_exist_ensure: Bool
540 do
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)
544 return false
545 end
546
547 # Check if the MMethod has no expect contract
548 # if this is the case returns false and built it
549 # else return true
550 private fun check_exist_expect: Bool
551 do
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)
555 return false
556 end
557
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
561 do
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)
565 return false
566 end
567 end
568
569 redef class MMethodDef
570
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)
574 do
575 var exist_contract_facet = mproperty.check_exist_contract_facet(self)
576 if exist_contract_facet and exist_contract then return
577
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)
582 else
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
585 # get the define
586 contract_facet = v.toolcontext.modelbuilder.mpropdef2node(mclassdef.mpropdefs_by_property[mproperty.mcontract_facet]).as(AMethPropdef)
587 else
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))
596 else
597 block.add(n_super_call)
598 end
599 contract_facet.n_block = block
600 end
601 end
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)
605 end
606
607 # Method to create a contract facet of the method
608 private fun create_contract_facet(v: ContractsVisitor, n_signature: ASignature): AMethPropdef
609 do
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
615
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
619
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
624
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)
630
631 if m_signature.return_mtype == null then
632 block.add(n_call)
633 else
634 block.add(v.ast_builder.make_return(n_call))
635 end
636
637 n_contractdef.n_block = block
638 n_contractdef.do_all(v.toolcontext)
639 return n_contractdef
640 end
641
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)
644 do
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)
648
649 var conditiondef = v.build_contract(n_annotation, mcontract, mclassdef)
650 check_contract_facet(v, n_signature.clone, mcontract, exist_contract)
651 has_contract = true
652 end
653
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)
657 do
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
661 end
662
663 # Is the contract already defined in the context
664 #
665 # Exemple :
666 # fun foo is expects([...]), expects([...])
667 #
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
670 do
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}")
673 return true
674 end
675 return false
676 end
677 end
678
679 redef class MPropDef
680 # flag to indicate is the `MPropDef` has a contract
681 var has_contract = false
682 end
683
684 redef class APropdef
685 redef fun check_callsite(v)
686 do
687 v.visited_method = self
688 end
689 end
690
691 redef class AMethPropdef
692
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)
696 do
697 self.validate
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)
702 end
703
704 # Entry point to create a contract (verification of inheritance, or new contract).
705 redef fun create_contracts(v)
706 do
707 v.ast_builder.check_mmodule(mpropdef.mclassdef.mmodule)
708
709 v.current_location = self.location
710 v.is_intro_contract = mpropdef.is_intro
711
712 if n_annotations != null then
713 for n_annotation in n_annotations.n_items do
714 check_annotation(v,n_annotation)
715 end
716 end
717
718 if not mpropdef.is_intro and not v.find_no_contract then
719 self.check_redef(v)
720 end
721
722 # reset the flag
723 v.find_no_contract = false
724 end
725
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)
729 do
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
741 end
742 end
743
744 # Verification if the method have an inherited contract to apply it.
745 private fun check_redef(v: ContractsVisitor)
746 do
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
751 end
752 end
753 end
754
755 # Adapt the block to use the contracts
756 private fun adapt_block_to_contract(v: ContractsVisitor, contract: MContract, n_mpropdef: AMethPropdef)
757 do
758 contract.adapt_block_to_contract(v, n_mpropdef)
759 mpropdef.has_contract = true
760 n_mpropdef.do_all(v.toolcontext)
761 end
762 end
763
764 redef class MSignature
765
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)
769
770 # Adapt signature for a ensure condition
771 #
772 # Create new parameter with the return type
773 private fun adapt_to_ensurecondition: MSignature
774 do
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))
779 end
780 return msignature
781 end
782
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)
786 end
787
788 redef class ASignature
789
790 # Create an array of AVarExpr representing the read of every parameters
791 private fun make_parameter_read(ast_builder: ASTBuilder): Array[AVarExpr]
792 do
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)
799 end
800 end
801 return args
802 end
803
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
807 do
808 var adapt_nsignature = self.clone
809 adapt_nsignature.n_type = return_type
810 return adapt_nsignature
811 end
812
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
817 var n_id = new TId
818 n_id.text = "result"
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
824 end
825 return nsignature
826 end
827 end
828
829 redef class ASendExpr
830 redef fun check_callsite(v)
831 do
832 var actual_callsite = callsite
833 if actual_callsite != null then
834 callsite = v.drive_method_contract(actual_callsite)
835 end
836 end
837 end
838
839 redef class ANewExpr
840 redef fun check_callsite(v)
841 do
842 var actual_callsite = callsite
843 if actual_callsite != null then
844 callsite = v.drive_method_contract(actual_callsite)
845 end
846 end
847 end