contracts: Remove useless return parameter
[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 parse_annotations
22 import phase
23 import semantize
24 intrude import model_contract
25 intrude import astbuilder
26 intrude import modelize_property
27 intrude import scope
28 intrude import typing
29
30 redef class ToolContext
31 # Parses contracts annotations.
32 var contracts_phase: Phase = new ContractsPhase(self, [modelize_property_phase,typing_phase])
33 end
34
35 private class ContractsPhase
36 super Phase
37
38 # The entry point of the contract phase
39 # In reality, the contract phase is executed on each module
40 # FIXME: Actually all method is checked to add method if any contract is inherited
41 redef fun process_nmodule(nmodule)do
42 # Check if the contracts are disabled
43 if toolcontext.opt_no_contract.value then return
44 nmodule.do_contracts(self.toolcontext)
45 end
46 end
47
48 redef class AModule
49 # Compile all contracts
50 #
51 # The implementation of the contracts is done in two visits.
52 #
53 # - First step, the visitor analyzes and constructs the contracts
54 # for each class (invariant) and method (expect, ensure).
55 #
56 # - Second step the visitor analyzes each `ASendExpr` to see
57 # if the callsite calls a method with a contract. If this is
58 # the case the callsite is replaced by another callsite to the contract method.
59 fun do_contracts(toolcontext: ToolContext) do
60 var ast_builder = new ASTBuilder(mmodule.as(not null))
61 #
62 var contract_visitor = new ContractsVisitor(toolcontext, toolcontext.modelbuilder.identified_modules.first, self, ast_builder)
63 contract_visitor.enter_visit(self)
64 #
65 var callsite_visitor = new CallSiteVisitor(toolcontext, ast_builder)
66 callsite_visitor.enter_visit(self)
67 end
68 end
69
70 # Visitor to build all contracts.
71 private class ContractsVisitor
72 super Visitor
73
74 # Instance of the toolcontext
75 var toolcontext: ToolContext
76
77 # The main module
78 # It's strore to define if the contract need to be build depending on the selected policy `--no-contract` `--full-contract` or default
79 var mainmodule: MModule
80
81 # Actual visited module
82 var visited_module: AModule
83
84 var ast_builder: ASTBuilder
85
86 # The `ASignature` of the actual build contract
87 var n_signature: ASignature is noinit
88
89 # The `MSignature` of the actual build contract
90 var m_signature: MSignature is noinit
91
92 # The `current_location` can corresponding of the annotation or method location.
93 var current_location: Location is noinit
94
95 # Is the contrat is an introduction or not?
96 # This attribute has the same value as the `is_intro` of the propdef attached to the contract.
97 # 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.
98 var is_intro_contract: Bool is noinit
99
100 # Actual visited class
101 var visited_class: nullable AClassdef
102
103 # is `no_contract` annotation was found
104 var find_no_contract = false
105
106 # The reference to the `in_contract` attribute.
107 # This attribute is used to disable contract verification when you are already in a contract verification.
108 # Keep the `in_contract` attribute to avoid searching at each contrat
109 var in_contract_attribute: nullable MAttribute = null
110
111 redef fun visit(node)
112 do
113 node.create_contracts(self)
114 node.visit_all(self)
115 end
116
117 # Method use to define the signature part of the method `ASignature` and `MSignature`
118 # The given `mcontract` provided `adapt_nsignature` and `adapt_msignature` to copy and adapt the given signature (`nsignature` and `msignature`)
119 fun define_signature(mcontract: MContract, nsignature: nullable ASignature, msignature: nullable MSignature)
120 do
121 if nsignature != null and msignature != null then nsignature.ret_type = msignature.return_mtype
122 self.n_signature = mcontract.adapt_nsignature(nsignature)
123 self.m_signature = mcontract.adapt_msignature(msignature)
124 end
125
126 # Define the new contract take in consideration that an old contract exist or not
127 private fun build_contract(n_annotation: AAnnotation, mcontract: MContract, mclassdef: MClassDef)
128 do
129 self.current_location = n_annotation.location
130 # Retrieving the expression provided in the annotation
131 var n_condition = n_annotation.construct_condition(self)
132 var m_contractdef: AMethPropdef
133 if is_intro_contract then
134 # Create new contract method
135 mcontract.create_intro_contract(self, n_condition, mclassdef)
136 else
137 # Create a redef of contract to take in consideration the new condition
138 mcontract.create_subcontract(self, n_condition, mclassdef)
139 end
140 end
141
142 # Verification if the construction of the contract is necessary.
143 # Three cases are checked for `expect`:
144 #
145 # - Was the `--full-contract` option passed?
146 # - Is the method is in the main package?
147 # - Is the method is in a direct imported package?
148 #
149 fun check_usage_expect(actual_mmodule: MModule): Bool
150 do
151 var main_package = mainmodule.mpackage
152 var actual_package = actual_mmodule.mpackage
153 if main_package != null and actual_package != null then
154 var condition_direct_arc = toolcontext.modelbuilder.model.mpackage_importation_graph.has_arc(main_package, actual_package)
155 return toolcontext.opt_full_contract.value or condition_direct_arc or main_package == actual_package
156 end
157 return false
158 end
159
160 # Verification if the construction of the contract is necessary.
161 # Two cases are checked for `ensure`:
162 #
163 # - Was the `--full-contract` option passed?
164 # - Is the method is in the main package?
165 #
166 fun check_usage_ensure(actual_mmodule: MModule): Bool
167 do
168 return toolcontext.opt_full_contract.value or mainmodule.mpackage == actual_mmodule.mpackage
169 end
170
171 # Inject the incontract attribute (`_in_contract_`) in the `Sys` class
172 # This attribute allows to check if the contract need to be executed
173 private fun inject_incontract_in_sys
174 do
175 # If the `in_contract_attribute` already know just return
176 if in_contract_attribute != null then return
177
178 var sys_class = toolcontext.modelbuilder.get_mclass_by_name(visited_module, mainmodule, "Sys")
179
180 # Try to get the `in_contract` property, if it has already defined in a previously visited module.
181 var in_contract_property = toolcontext.modelbuilder.try_get_mproperty_by_name(visited_module, sys_class.intro, "__in_contract_")
182 if in_contract_property != null then
183 self.in_contract_attribute = in_contract_property.as(MAttribute)
184 return
185 end
186
187 var bool_false = new AFalseExpr.make(mainmodule.bool_type)
188 var n_in_contract_attribute = toolcontext.modelbuilder.create_attribute_from_name("__in_contract_", sys_class.intro, mainmodule.bool_type, public_visibility).create_setter(toolcontext.modelbuilder, true).define_default(bool_false)
189
190 in_contract_attribute = n_in_contract_attribute.mpropdef.mproperty
191 end
192
193 # Return the `_in_contract_` attribute.
194 # If the attribute `_in_contract_` does not exist it's injected with `inject_incontract_in_sys`
195 private fun get_incontract: MAttribute
196 do
197 if self.in_contract_attribute == null then inject_incontract_in_sys
198 return in_contract_attribute.as(not null)
199 end
200
201 # Return an `AIfExpr` with the contract encapsulated by an `if` to check if it's already in a contract verification.
202 #
203 # Example:
204 # ~~~nitish
205 # class A
206 # fun bar([...]) is except([...])
207 #
208 # fun _contract_bar([...])
209 # do
210 # if not sys._in_contract_ then
211 # sys._in_contract_ = true
212 # _bar_expect([...])
213 # sys._in_contract_ = false
214 # end
215 # bar([...])
216 # end
217 #
218 # fun _bar_expect([...])
219 # end
220 # ~~~~
221 #
222 private fun encapsulated_contract_call(visited_method: AMethPropdef, call_to_contracts: Array[ACallExpr]): AIfExpr
223 do
224 var sys_property = toolcontext.modelbuilder.model.get_mproperties_by_name("sys").first.as(MMethod)
225 var callsite_sys = ast_builder.create_callsite(toolcontext.modelbuilder, visited_method, sys_property, true)
226
227 var incontract_attribute = get_incontract
228
229 var callsite_get_incontract = ast_builder.create_callsite(toolcontext.modelbuilder, visited_method, incontract_attribute.getter.as(MMethod), false)
230 var callsite_set_incontract = ast_builder.create_callsite(toolcontext.modelbuilder, visited_method, incontract_attribute.setter.as(MMethod), false)
231
232 var n_condition = ast_builder.make_not(ast_builder.make_call(ast_builder.make_call(new ASelfExpr, callsite_sys, null), callsite_get_incontract, null))
233
234 var n_if = ast_builder.make_if(n_condition, null)
235
236 var if_then_block = n_if.n_then.as(ABlockExpr)
237
238 if_then_block.add(ast_builder.make_call(ast_builder.make_call(new ASelfExpr, callsite_sys, null), callsite_set_incontract, [new ATrueExpr.make(mainmodule.bool_type)]))
239 if_then_block.add_all(call_to_contracts)
240 if_then_block.add(ast_builder.make_call(ast_builder.make_call(new ASelfExpr, callsite_sys, null), callsite_set_incontract, [new AFalseExpr.make(mainmodule.bool_type)]))
241
242 return n_if
243 end
244 end
245
246 # This visitor checks the `callsite` to see if the target `mpropdef` has a contract.
247 private class CallSiteVisitor
248 super Visitor
249
250 # Instance of the toolcontext
251 var toolcontext: ToolContext
252
253 var ast_builder: ASTBuilder
254
255 # Actual visited method
256 var visited_propdef: APropdef is noinit
257
258 redef fun visit(node)
259 do
260 node.check_callsite(self)
261 node.visit_all(self)
262 end
263
264 # Check if the callsite calls a method with a contract.
265 # If it's the case the callsite is replaced by another callsite to the contract method.
266 private fun drive_method_contract(callsite: CallSite): CallSite
267 do
268 var contract_facet = callsite.mproperty.mcontract_facet
269 var visited_mpropdef = visited_propdef.mpropdef
270
271 if visited_mpropdef isa MContract or visited_mpropdef isa MFacet then return callsite
272 if visited_mpropdef == null or contract_facet == null then return callsite
273
274 return ast_builder.create_callsite(toolcontext.modelbuilder, visited_propdef, contract_facet, callsite.recv_is_self)
275 end
276 end
277
278 redef class ANode
279 private fun create_contracts(v: ContractsVisitor) do end
280 private fun check_callsite(v: CallSiteVisitor) do end
281 end
282
283 redef class AAnnotation
284
285 # Returns the conditions of annotation parameters. If there are several parameters, the result is an `AAndExpr`
286 # Example:
287 # the contract `ensure(true, i == 10, f >= 1.0)`
288 # return this condition `(true and i == 10 and f >= 1.0)`
289 private fun construct_condition(v : ContractsVisitor): AExpr
290 do
291 var n_condition = n_args.first
292 n_args.remove_at(0)
293 for n_arg in n_args do n_condition = v.ast_builder.make_and(n_condition, n_arg)
294 return n_condition
295 end
296 end
297
298 redef class MContract
299
300 # Define the name of the contract
301 fun contract_name: String is abstract
302
303 # Method use to diplay warning when the contract is not present at the introduction
304 private fun no_intro_contract(v: ContractsVisitor, a: AAnnotation)do end
305
306 # Creating specific inheritance block contract
307 private fun create_nblock(v: ContractsVisitor, n_condition: AExpr, args: Array[AExpr]): ABlockExpr is abstract
308
309 # Method to adapt the `n_mpropdef.n_block` to the contract
310 private fun adapt_block_to_contract(v: ContractsVisitor, n_mpropdef: AMethPropdef) is abstract
311
312 # Adapt the msignature specifically for the contract method
313 private fun adapt_specific_msignature(m_signature: MSignature): MSignature do return m_signature.adapt_to_contract
314
315 # Adapt the nsignature specifically for the contract method
316 private fun adapt_specific_nsignature(n_signature: ASignature): ASignature do return n_signature.adapt_to_contract
317
318 # Adapt the `m_signature` to the contract
319 # If it is not null call the specific adapt `m_signature` for the contract
320 private fun adapt_msignature(m_signature: nullable MSignature): MSignature
321 do
322 if m_signature == null then return new MSignature(new Array[MParameter])
323 return adapt_specific_msignature(m_signature)
324 end
325
326 # Adapt the `n_signature` to the contract
327 # If it is not null call the specific adapt `n_signature` for the contract
328 private fun adapt_nsignature(n_signature: nullable ASignature): ASignature
329 do
330 if n_signature == null then return new ASignature
331 return adapt_specific_nsignature(n_signature)
332 end
333
334 # Create a new empty contract
335 private fun create_empty_contract(v: ContractsVisitor, mclassdef: MClassDef, msignature: nullable MSignature, n_signature: ASignature)
336 do
337 var n_contract_def = intro_mclassdef.mclass.create_empty_method(v, self, mclassdef, msignature, n_signature)
338 n_contract_def.do_all(v.toolcontext)
339 end
340
341 # Create the initial contract (intro)
342 # All contracts have the same implementation for the introduction.
343 #
344 # Example:
345 # ~~~nitish
346 # fun contrat([...])
347 # do
348 # assert contract_condition
349 # end
350 # ~~~
351 #
352 private fun create_intro_contract(v: ContractsVisitor, n_condition: nullable AExpr, mclassdef: MClassDef)
353 do
354 var n_block = v.ast_builder.make_block
355 if n_condition != null then
356 # Create a new tid to set the name of the assert for more explicit error
357 var tid = new TId.init_tk(self.location)
358 tid.text = "{self.contract_name}"
359 n_block.add v.ast_builder.make_assert(tid, n_condition, null)
360 end
361 make_contract(v, n_block, mclassdef)
362 end
363
364 # Create a contract with old (super) and the new conditions
365 private fun create_subcontract(v: ContractsVisitor, ncondition: nullable AExpr, mclassdef: MClassDef)
366 do
367 var args = v.n_signature.make_parameter_read(v.ast_builder)
368 var n_block = v.ast_builder.make_block
369 if ncondition != null then n_block = self.create_nblock(v, ncondition, args)
370 make_contract(v, n_block, mclassdef)
371 end
372
373 # Build a method with a specific block `n_block` in a specified `mclassdef`
374 private fun make_contract(v: ContractsVisitor, n_block: AExpr, mclassdef: MClassDef)
375 do
376 var n_contractdef = intro_mclassdef.mclass.create_empty_method(v, self, mclassdef, v.m_signature, v.n_signature)
377 n_contractdef.n_block = n_block
378 # Define the location of the new method for corresponding of the annotation location
379 n_contractdef.location = v.current_location
380 n_contractdef.do_all(v.toolcontext)
381 end
382 end
383
384 redef class MExpect
385
386 # Define the name of the contract
387 redef fun contract_name: String do return "expect"
388
389 # Display warning if no contract is defined at introduction `expect`,
390 # because if no contract is defined at the introduction the added
391 # contracts will not cause any error even if they are not satisfied.
392 #
393 # Example:
394 # ~~~nitish
395 # class A
396 # fun bar [...]
397 # fun _bar_expect([...])
398 # do
399 # [empty contract]
400 # end
401 # end
402 #
403 # redef class A
404 # redef fun bar is expect(contract_condition)
405 # redef fun _bar_expect([...])
406 # do
407 # if (contract_condition) then return
408 # super
409 # end
410 # end
411 # ~~~~
412 #
413 redef fun no_intro_contract(v: ContractsVisitor, a: AAnnotation)
414 do
415 v.toolcontext.warning(a.location,"","Useless contract: No contract defined at the introduction of the method")
416 end
417
418 redef fun create_nblock(v: ContractsVisitor, n_condition: AExpr, args: Array[AExpr]): ABlockExpr
419 do
420 # Creating the if expression with the new condition
421 var if_block = v.ast_builder.make_if(n_condition, n_condition.mtype)
422 # Creating and adding return expr to the then case
423 if_block.n_then = v.ast_builder.make_return(null)
424 # Creating the super call to the contract and adding this to else case
425 if_block.n_else = v.ast_builder.make_super_call(args,null)
426 var n_block = v.ast_builder.make_block
427 n_block.add if_block
428 return n_block
429 end
430
431 redef fun adapt_block_to_contract(v: ContractsVisitor, n_mpropdef: AMethPropdef)
432 do
433 var callsite = v.ast_builder.create_callsite(v.toolcontext.modelbuilder, n_mpropdef, self, true)
434 var n_self = new ASelfExpr
435 var args = n_mpropdef.n_signature.make_parameter_read(v.ast_builder)
436 var n_callexpect = v.ast_builder.make_call(n_self,callsite,args)
437 # Creation of the new instruction block with the call to expect condition
438 var actual_expr = n_mpropdef.n_block
439 var new_block = new ABlockExpr
440 new_block.n_expr.push n_callexpect
441 if actual_expr isa ABlockExpr then
442 new_block.n_expr.add_all(actual_expr.n_expr)
443 else if actual_expr != null then
444 new_block.n_expr.push(actual_expr)
445 end
446 n_mpropdef.n_block = new_block
447 end
448 end
449
450 redef class BottomMContract
451
452 redef fun create_nblock(v: ContractsVisitor, n_condition: AExpr, args: Array[AExpr]): ABlockExpr
453 do
454 var tid = new TId.init_tk(v.current_location)
455 tid.text = "{contract_name}"
456 # Creating the assert expression with the new condition
457 var assert_block = v.ast_builder.make_assert(tid,n_condition,null)
458 # Creating the super call to the contract
459 var super_call = v.ast_builder.make_super_call(args,null)
460 # Define contract block
461 var n_block = v.ast_builder.make_block
462 # Adding the expressions to the block
463 n_block.add super_call
464 n_block.add assert_block
465 return n_block
466 end
467
468 # Inject the `result` variable into the `n_block` of the given n_mpropdef`.
469 #
470 # The purpose of the variable is to capture return values to use it in contracts.
471 private fun inject_result(v: ContractsVisitor, n_mpropdef: AMethPropdef, ret_type: MType): Variable
472 do
473 var actual_block = n_mpropdef.n_block
474 # never happen. If it's the case the problem is in the contract facet building
475 assert actual_block isa ABlockExpr
476
477 var return_var: nullable Variable = null
478
479 var return_expr = actual_block.n_expr.last.as(AReturnExpr)
480
481 var returned_expr = return_expr.n_expr
482 # The return node has no returned expression
483 assert returned_expr != null
484
485 # Check if the result variable already exit
486 if returned_expr isa AVarExpr then
487 if returned_expr.variable.name == "result" then
488 return_var = returned_expr.variable
489 end
490 end
491
492 return_var = new Variable("result")
493 # Creating a new variable to keep the old return of the method
494 var assign_result = v.ast_builder.make_var_assign(return_var, returned_expr)
495 # Remove the actual return
496 actual_block.n_expr.pop
497 # Set the return type
498 return_var.declared_type = ret_type
499 # Adding the reading expr of result variable to the block
500 actual_block.add assign_result
501 # Expr to read the result variable
502 var read_result = v.ast_builder.make_var_read(return_var, ret_type)
503 # Definition of the new return
504 return_expr = v.ast_builder.make_return(read_result)
505 actual_block.add return_expr
506 return return_var
507 end
508 end
509
510 redef class MEnsure
511
512 # Define the name of the contract
513 redef fun contract_name: String do return "ensure"
514
515 redef fun adapt_specific_msignature(m_signature: MSignature): MSignature
516 do
517 return m_signature.adapt_to_ensurecondition
518 end
519
520 redef fun adapt_specific_nsignature(n_signature: ASignature): ASignature
521 do
522 return n_signature.adapt_to_ensurecondition
523 end
524
525 redef fun adapt_block_to_contract(v: ContractsVisitor, n_mpropdef: AMethPropdef)
526 do
527 var callsite = v.ast_builder.create_callsite(v.toolcontext.modelbuilder, n_mpropdef, self, true)
528 var n_self = new ASelfExpr
529 # argument to call the contract method
530 var args = n_mpropdef.n_signature.make_parameter_read(v.ast_builder)
531 # Inject the variable result
532 # The cast is always safe because the online adapted method is the contract facet
533
534 var actual_block = n_mpropdef.n_block
535 # never happen. If it's the case the problem is in the contract facet building
536 assert actual_block isa ABlockExpr
537
538 var ret_type = n_mpropdef.mpropdef.mproperty.intro.msignature.return_mtype
539 if ret_type != null then
540 var result_var = inject_result(v, n_mpropdef, ret_type)
541 # Expr to read the result variable
542 var read_result = v.ast_builder.make_var_read(result_var, ret_type)
543 var return_expr = actual_block.n_expr.pop
544 # Adding the read return to argument
545 args.add(read_result)
546 var n_callcontract = v.ast_builder.make_call(n_self,callsite,args)
547 actual_block.add_all([n_callcontract,return_expr])
548 else
549 # build the call to the contract method
550 var n_callcontract = v.ast_builder.make_call(n_self,callsite,args)
551 actual_block.add n_callcontract
552 end
553 end
554 end
555
556 redef class MClass
557
558 # This method create an abstract method representation with this MMethodDef an this AMethoddef
559 private fun create_abstract_method(v: ContractsVisitor, mproperty: MMethod, mclassdef: MClassDef, msignature: nullable MSignature, n_signature: ASignature): AMethPropdef
560 do
561 # new methoddef definition
562 var m_def = new MMethodDef(mclassdef, mproperty, v.current_location)
563 # set the signature
564 if msignature != null then m_def.msignature = msignature.clone
565 # set the abstract flag
566 m_def.is_abstract = true
567 # Build the new node method
568 var n_def = v.ast_builder.make_method(null,null,m_def,n_signature,null,null,null,null)
569 # Define the location of the new method for corresponding of the actual method
570 n_def.location = v.current_location
571 # Association new npropdef to mpropdef
572 v.toolcontext.modelbuilder.unsafe_add_mpropdef2npropdef(m_def,n_def)
573 return n_def
574 end
575
576 # Create method with an empty block
577 # 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
578 private fun create_empty_method(v: ContractsVisitor, mproperty: MMethod, mclassdef: MClassDef, msignature: nullable MSignature, n_signature: ASignature): AMethPropdef
579 do
580 var n_def = create_abstract_method(v, mproperty, mclassdef, msignature, n_signature)
581 n_def.mpropdef.is_abstract = false
582 n_def.n_block = v.ast_builder.make_block
583 return n_def
584 end
585 end
586
587 redef class MMethodDef
588
589 # Verification of the contract facet
590 # Check if a contract facet already exists to use it again or if it is necessary to create it.
591 private fun check_contract_facet(v: ContractsVisitor, n_signature: ASignature, classdef: MClassDef, mcontract: MContract, exist_contract: Bool)
592 do
593 var exist_contract_facet = mproperty.has_contract_facet
594 if exist_contract_facet and exist_contract then return
595
596 var contract_facet: AMethPropdef
597 if not exist_contract_facet then
598 # If has no contract facet in intro just create it
599 if classdef != mproperty.intro_mclassdef then create_contract_facet(v, mproperty.intro_mclassdef, n_signature)
600 # If has no contract facet just create it
601 contract_facet = create_contract_facet(v, classdef, n_signature)
602 else
603 # Check if the contract facet already exist in this context (in this classdef)
604 if classdef.mpropdefs_by_property.has_key(mproperty.mcontract_facet) then
605 # get the define
606 contract_facet = v.toolcontext.modelbuilder.mpropdef2node(classdef.mpropdefs_by_property[mproperty.mcontract_facet]).as(AMethPropdef)
607 else
608 # create a new contract facet definition
609 contract_facet = create_contract_facet(v, classdef, n_signature)
610 var block = v.ast_builder.make_block
611 # super call to the contract facet
612 var n_super_call = v.ast_builder.make_super_call(n_signature.make_parameter_read(v.ast_builder), null)
613 # verification for add a return or not
614 if contract_facet.mpropdef.msignature.return_mtype != null then
615 block.add(v.ast_builder.make_return(n_super_call))
616 else
617 block.add(n_super_call)
618 end
619 contract_facet.n_block = block
620 end
621 end
622 contract_facet.adapt_block_to_contract(v, mcontract, contract_facet)
623 contract_facet.location = v.current_location
624 contract_facet.do_all(v.toolcontext)
625 end
626
627 # Method to create a contract facet of the method
628 private fun create_contract_facet(v: ContractsVisitor, classdef: MClassDef, n_signature: ASignature): AMethPropdef
629 do
630 var contract_facet = mproperty.build_contract_facet
631 # Defines the contract phase is an init or not
632 # it is necessary to use the contracts on constructor
633 contract_facet.is_init = self.mproperty.is_init
634
635 # check if the method has an `msignature` to copy it.
636 var m_signature: nullable MSignature = null
637 if mproperty.intro.msignature != null then m_signature = mproperty.intro.msignature.clone
638
639 var n_contractdef = classdef.mclass.create_empty_method(v, contract_facet, classdef, m_signature, n_signature)
640 # FIXME set the location because the callsite creation need the node location
641 n_contractdef.location = v.current_location
642 n_contractdef.validate
643
644 var block = v.ast_builder.make_block
645 var n_self = new ASelfExpr
646 var args = n_contractdef.n_signature.make_parameter_read(v.ast_builder)
647 var callsite = v.ast_builder.create_callsite(v.toolcontext.modelbuilder, n_contractdef, mproperty, true)
648 var n_call = v.ast_builder.make_call(n_self, callsite, args)
649
650 if m_signature.return_mtype == null then
651 block.add(n_call)
652 else
653 block.add(v.ast_builder.make_return(n_call))
654 end
655
656 n_contractdef.n_block = block
657 n_contractdef.do_all(v.toolcontext)
658 return n_contractdef
659 end
660
661 # Entry point to build contract (define the contract facet and define the contract method verification)
662 private fun construct_contract(v: ContractsVisitor, n_signature: ASignature, n_annotation: AAnnotation, mcontract: MContract, exist_contract: Bool)
663 do
664 if check_same_contract(v, n_annotation, mcontract) then return
665 if not exist_contract and not is_intro then no_intro_contract(v, n_signature, mcontract, n_annotation)
666 v.define_signature(mcontract, n_signature, mproperty.intro.msignature)
667
668 v.build_contract(n_annotation, mcontract, mclassdef)
669 check_contract_facet(v, n_signature.clone, mclassdef, mcontract, exist_contract)
670 has_contract = true
671 end
672
673 # Create a contract on the introduction classdef of the property.
674 # Display an warning message if necessary
675 private fun no_intro_contract(v: ContractsVisitor, n_signature: ASignature, mcontract: MContract, n_annotation: AAnnotation)
676 do
677 mcontract.create_empty_contract(v, mcontract.intro_mclassdef, mcontract.adapt_msignature(self.mproperty.intro.msignature), mcontract.adapt_nsignature(n_signature))
678 mcontract.no_intro_contract(v, n_annotation)
679 mproperty.intro.has_contract = true
680 end
681
682 # Is the contract already defined in the context
683 #
684 # Exemple :
685 # fun foo is expect([...]), expect([...])
686 #
687 # Here `check_same_contract` display an error when the second expect is processed
688 private fun check_same_contract(v: ContractsVisitor, n_annotation: AAnnotation ,mcontract: MContract): Bool
689 do
690 if self.mclassdef.mpropdefs_by_property.has_key(mcontract) then
691 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}")
692 return true
693 end
694 return false
695 end
696 end
697
698 redef class MPropDef
699 # flag to indicate is the `MPropDef` has a contract
700 var has_contract = false
701 end
702
703 redef class APropdef
704 redef fun check_callsite(v)
705 do
706 v.visited_propdef = self
707 end
708 end
709
710 redef class AMethPropdef
711
712 # Entry point to create a contract (verification of inheritance, or new contract).
713 redef fun create_contracts(v)
714 do
715 v.ast_builder.check_mmodule(mpropdef.mclassdef.mmodule)
716
717 v.current_location = self.location
718 v.is_intro_contract = mpropdef.is_intro
719
720 if n_annotations != null then
721 for n_annotation in n_annotations.n_items do
722 check_annotation(v,n_annotation)
723 end
724 end
725
726 if not mpropdef.is_intro and not v.find_no_contract then
727 self.check_redef(v)
728 end
729
730 # reset the flag
731 v.find_no_contract = false
732 end
733
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)
737 do
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.has_expect
741 mpropdef.construct_contract(v, self.n_signature.as(not null), n_annotation, mpropdef.mproperty.build_expect, 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.has_ensure
745 mpropdef.construct_contract(v, self.n_signature.as(not null), n_annotation, mpropdef.mproperty.build_ensure, 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
749 end
750 end
751
752 # Verification if the method have an inherited contract to apply it.
753 private fun check_redef(v: ContractsVisitor)
754 do
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
759 end
760 end
761 end
762
763 # Adapt the block to use the contracts
764 private fun adapt_block_to_contract(v: ContractsVisitor, contract: MContract, n_mpropdef: AMethPropdef)
765 do
766 contract.adapt_block_to_contract(v, n_mpropdef)
767 mpropdef.has_contract = true
768 n_mpropdef.do_all(v.toolcontext)
769 end
770 end
771
772 redef class MSignature
773
774 # Adapt signature for an contract
775 #
776 # The returned `MSignature` is the copy of `self` without return type.
777 private fun adapt_to_contract: MSignature do return new MSignature(mparameters.to_a, null)
778
779 # Adapt signature for a ensure contract
780 #
781 # The returned `MSignature` is the copy of `self` without return type.
782 # The return type is replaced by a new parameter `result`
783 private fun adapt_to_ensurecondition: MSignature
784 do
785 var rtype = return_mtype
786 var msignature = adapt_to_contract
787 if rtype != null then
788 msignature.mparameters.add(new MParameter("result", rtype, false))
789 end
790 return msignature
791 end
792
793 # The returned `MSignature` is the exact copy of `self`.
794 private fun clone: MSignature do return new MSignature(mparameters.to_a, return_mtype)
795 end
796
797 redef class ASignature
798
799 # Create an array of AVarExpr representing the read of every parameters
800 private fun make_parameter_read(ast_builder: ASTBuilder): Array[AVarExpr]
801 do
802 var args = new Array[AVarExpr]
803 for n_param in self.n_params do
804 var mtype = n_param.variable.declared_type
805 var variable = n_param.variable
806 if variable != null and mtype != null then
807 args.push ast_builder.make_var_read(variable, mtype)
808 end
809 end
810 return args
811 end
812
813 # Create a new ASignature adapted for contract
814 #
815 # The returned `ASignature` is the copy of `self` without return type.
816 private fun adapt_to_contract: ASignature
817 do
818 var adapt_nsignature = self.clone
819 if adapt_nsignature.n_type != null then adapt_nsignature.n_type.detach
820 return adapt_nsignature
821 end
822
823 # Create a new ASignature adapted for ensure
824 #
825 # The returned `ASignature` is the copy of `self` without return type.
826 # The return type is replaced by a new parameter `result`
827 private fun adapt_to_ensurecondition: ASignature do
828 var nsignature = adapt_to_contract
829 if ret_type != null then
830 var variable = new Variable("result")
831 variable.declared_type = ret_type
832 nsignature.n_params.add new AParam.make(variable, ret_type.create_ast_representation)
833 end
834 return nsignature
835 end
836 end
837
838 redef class ASendExpr
839 redef fun check_callsite(v)
840 do
841 var actual_callsite = callsite
842 if actual_callsite != null then
843 callsite = v.drive_method_contract(actual_callsite)
844 end
845 end
846 end
847
848 redef class ANewExpr
849 redef fun check_callsite(v)
850 do
851 var actual_callsite = callsite
852 if actual_callsite != null then
853 callsite = v.drive_method_contract(actual_callsite)
854 end
855 end
856 end