modelize_property: inherit the initializers in the refinements of init
[nit.git] / src / modelize / modelize_property.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2012 Jean Privat <jean@pryen.org>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Analysis and verification of property definitions to instantiate model element
18 module modelize_property
19
20 import modelize_class
21 private import annotation
22
23 redef class ToolContext
24 var modelize_property_phase: Phase = new ModelizePropertyPhase(self, [modelize_class_phase])
25 end
26
27 private class ModelizePropertyPhase
28 super Phase
29 redef fun process_nmodule(nmodule)
30 do
31 for nclassdef in nmodule.n_classdefs do
32 if nclassdef.all_defs == null then continue # skip non principal classdef
33 toolcontext.modelbuilder.build_properties(nclassdef)
34 end
35 end
36 end
37
38 redef class ModelBuilder
39 # Register the npropdef associated to each mpropdef
40 # FIXME: why not refine the `MPropDef` class with a nullable attribute?
41 var mpropdef2npropdef: HashMap[MPropDef, APropdef] = new HashMap[MPropDef, APropdef]
42
43 # Build the properties of `nclassdef`.
44 # REQUIRE: all superclasses are built.
45 private fun build_properties(nclassdef: AClassdef)
46 do
47 # Force building recursively
48 if nclassdef.build_properties_is_done then return
49 nclassdef.build_properties_is_done = true
50 var mclassdef = nclassdef.mclassdef.as(not null)
51 if mclassdef.in_hierarchy == null then return # Skip error
52 for superclassdef in mclassdef.in_hierarchy.direct_greaters do
53 if not mclassdef2nclassdef.has_key(superclassdef) then continue
54 build_properties(mclassdef2nclassdef[superclassdef])
55 end
56
57 for nclassdef2 in nclassdef.all_defs do
58 for npropdef in nclassdef2.n_propdefs do
59 npropdef.build_property(self, mclassdef)
60 end
61 for npropdef in nclassdef2.n_propdefs do
62 npropdef.build_signature(self)
63 end
64 for npropdef in nclassdef2.n_propdefs do
65 npropdef.check_signature(self)
66 end
67 end
68 process_default_constructors(nclassdef)
69 end
70
71 # the root init of the Object class
72 # Is usually implicitly defined
73 # Then explicit or implicit definitions of root-init are attached to it
74 var the_root_init_mmethod: nullable MMethod
75
76 # Introduce or inherit default constructor
77 # This is the last part of `build_properties`.
78 private fun process_default_constructors(nclassdef: AClassdef)
79 do
80 var mclassdef = nclassdef.mclassdef.as(not null)
81
82 # Are we a refinement
83 if not mclassdef.is_intro then return
84
85 # Look for the init in Object, or create it
86 if mclassdef.mclass.name == "Object" and the_root_init_mmethod == null then
87 # Create the implicit root-init method
88 var mprop = new MMethod(mclassdef, "init", mclassdef.mclass.visibility)
89 mprop.is_root_init = true
90 var mpropdef = new MMethodDef(mclassdef, mprop, nclassdef.location)
91 var mparameters = new Array[MParameter]
92 var msignature = new MSignature(mparameters, null)
93 mpropdef.msignature = msignature
94 mpropdef.new_msignature = msignature
95 mprop.is_init = true
96 nclassdef.mfree_init = mpropdef
97 self.toolcontext.info("{mclassdef} gets a free empty constructor {mpropdef}{msignature}", 3)
98 the_root_init_mmethod = mprop
99 return
100 end
101
102 # Is the class forbid constructors?
103 if not mclassdef.mclass.kind.need_init then return
104
105 # Is there already a constructor defined?
106 var defined_init: nullable MMethodDef = null
107 for mpropdef in mclassdef.mpropdefs do
108 if not mpropdef isa MMethodDef then continue
109 if not mpropdef.mproperty.is_init then continue
110 if mpropdef.mproperty.is_root_init then
111 assert defined_init == null
112 defined_init = mpropdef
113 else if mpropdef.mproperty.name == "init" then
114 # An explicit old-style init named "init", so return
115 return
116 end
117 end
118
119 if not nclassdef isa AStdClassdef then return
120
121 # Collect undefined attributes
122 var mparameters = new Array[MParameter]
123 var initializers = new Array[MProperty]
124 for npropdef in nclassdef.n_propdefs do
125 if npropdef isa AMethPropdef then
126 if npropdef.mpropdef == null then return # Skip broken attribute
127 var at = npropdef.get_single_annotation("autoinit", self)
128 if at == null then continue # Skip non tagged init
129
130 var sig = npropdef.mpropdef.msignature
131 if sig == null then continue # Skip broken method
132
133 if not npropdef.mpropdef.is_intro then
134 self.error(at, "Error: `autoinit` cannot be set on redefinitions")
135 continue
136 end
137
138 for param in sig.mparameters do
139 var ret_type = param.mtype
140 var mparameter = new MParameter(param.name, ret_type, false)
141 mparameters.add(mparameter)
142 end
143 initializers.add(npropdef.mpropdef.mproperty)
144 end
145 if npropdef isa AAttrPropdef then
146 if npropdef.mpropdef == null then return # Skip broken attribute
147 var at = npropdef.get_single_annotation("noinit", self)
148 if at != null then
149 npropdef.noinit = true
150 if npropdef.n_expr != null then
151 self.error(at, "Error: `noinit` attributes cannot have an initial value")
152 end
153 continue # Skip noinit attributes
154 end
155 if npropdef.n_expr != null then continue
156 var paramname = npropdef.mpropdef.mproperty.name.substring_from(1)
157 var ret_type = npropdef.mpropdef.static_mtype
158 if ret_type == null then return
159 var mparameter = new MParameter(paramname, ret_type, false)
160 mparameters.add(mparameter)
161 var msetter = npropdef.mwritepropdef
162 if msetter == null then
163 # No setter, it is a old-style attribute, so just add it
164 initializers.add(npropdef.mpropdef.mproperty)
165 else
166 # Add the setter to the list
167 initializers.add(msetter.mproperty)
168 end
169 end
170 end
171
172 if the_root_init_mmethod == null then return
173
174 # Look for most-specific new-stype init definitions
175 var spropdefs = the_root_init_mmethod.lookup_super_definitions(mclassdef.mmodule, mclassdef.bound_mtype)
176 if spropdefs.is_empty then
177 toolcontext.fatal_error(nclassdef.location, "Fatal error: {mclassdef} does not specialize {the_root_init_mmethod.intro_mclassdef}. Possible duplication of the root class `Object`?")
178 end
179
180 # Search the longest-one and checks for conflict
181 var longest = spropdefs.first
182 if spropdefs.length > 1 then
183 # Check for conflict in the order of initializers
184 # Each initializer list must me a prefix of the longest list
185 # part 1. find the longest list
186 for spd in spropdefs do
187 if spd.initializers.length > longest.initializers.length then longest = spd
188 end
189 # part 2. compare
190 for spd in spropdefs do
191 var i = 0
192 for p in spd.initializers do
193 if p != longest.initializers[i] then
194 self.error(nclassdef, "Error: conflict for inherited inits {spd}({spd.initializers.join(", ")}) and {longest}({longest.initializers.join(", ")})")
195 return
196 end
197 i += 1
198 end
199 end
200 end
201
202 # Can we just inherit?
203 if spropdefs.length == 1 and mparameters.is_empty and defined_init == null then
204 self.toolcontext.info("{mclassdef} inherits the basic constructor {longest}", 3)
205 mclassdef.mclass.root_init = longest
206 return
207 end
208
209 # Combine the inherited list to what is collected
210 if longest.initializers.length > 0 then
211 mparameters.prepend longest.new_msignature.mparameters
212 initializers.prepend longest.initializers
213 end
214
215 # If we already have a basic init definition, then setup its initializers
216 if defined_init != null then
217 defined_init.initializers.add_all(initializers)
218 var msignature = new MSignature(mparameters, null)
219 defined_init.new_msignature = msignature
220 self.toolcontext.info("{mclassdef} extends its basic constructor signature to {defined_init}{msignature}", 3)
221 mclassdef.mclass.root_init = defined_init
222 return
223 end
224
225 # Else create the local implicit basic init definition
226 var mprop = the_root_init_mmethod.as(not null)
227 var mpropdef = new MMethodDef(mclassdef, mprop, nclassdef.location)
228 mpropdef.has_supercall = true
229 mpropdef.initializers.add_all(initializers)
230 var msignature = new MSignature(mparameters, null)
231 mpropdef.new_msignature = msignature
232 mpropdef.msignature = new MSignature(new Array[MParameter], null) # always an empty real signature
233 nclassdef.mfree_init = mpropdef
234 self.toolcontext.info("{mclassdef} gets a free constructor for attributes {mpropdef}{msignature}", 3)
235 mclassdef.mclass.root_init = mpropdef
236 end
237
238 # Check the visibility of `mtype` as an element of the signature of `mpropdef`.
239 fun check_visibility(node: ANode, mtype: MType, mpropdef: MPropDef)
240 do
241 var mmodule = mpropdef.mclassdef.mmodule
242 var mproperty = mpropdef.mproperty
243
244 # Extract visibility information of the main part of `mtype`
245 # It is a case-by case
246 var vis_type: nullable MVisibility = null # The own visibility of the type
247 var mmodule_type: nullable MModule = null # The origial module of the type
248 mtype = mtype.as_notnullable
249 if mtype isa MClassType then
250 vis_type = mtype.mclass.visibility
251 mmodule_type = mtype.mclass.intro.mmodule
252 else if mtype isa MVirtualType then
253 vis_type = mtype.mproperty.visibility
254 mmodule_type = mtype.mproperty.intro_mclassdef.mmodule
255 else if mtype isa MParameterType then
256 # nothing, always visible
257 else
258 node.debug "Unexpected type {mtype}"
259 abort
260 end
261
262 if vis_type != null then
263 assert mmodule_type != null
264 var vis_module_type = mmodule.visibility_for(mmodule_type) # the visibility of the original module
265 if mproperty.visibility > vis_type then
266 error(node, "Error: The {mproperty.visibility} property `{mproperty}` cannot contain the {vis_type} type `{mtype}`")
267 return
268 else if mproperty.visibility > vis_module_type then
269 error(node, "Error: The {mproperty.visibility} property `{mproperty}` cannot contain the type `{mtype}` from the {vis_module_type} module `{mmodule_type}`")
270 return
271 end
272 end
273
274 # No error, try to go deeper in generic types
275 if node isa AType then
276 for a in node.n_types do
277 var t = a.mtype
278 if t == null then continue # Error, thus skipped
279 check_visibility(a, t, mpropdef)
280 end
281 else if mtype isa MGenericType then
282 for t in mtype.arguments do check_visibility(node, t, mpropdef)
283 end
284 end
285 end
286
287 redef class MPropDef
288 # Does the MPropDef contains a call to super or a call of a super-constructor?
289 # Subsequent phases of the frontend (esp. typing) set it if required
290 var has_supercall: Bool = false is writable
291 end
292
293 redef class AClassdef
294 var build_properties_is_done: Bool = false
295
296 # The free init (implicitely constructed by the class if required)
297 var mfree_init: nullable MMethodDef = null
298 end
299
300 redef class MClass
301 # The base init of the class.
302 # Used to get the common new_msignature and initializers
303 #
304 # TODO: Where to put this information is not clear because unlike other
305 # informations, the initialisers are stable in a same class.
306 var root_init: nullable MMethodDef = null
307 end
308
309 redef class MClassDef
310 # What is the `APropdef` associated to a `MProperty`?
311 # Used to check multiple definition of a property.
312 var mprop2npropdef: Map[MProperty, APropdef] = new HashMap[MProperty, APropdef]
313 end
314
315 redef class Prod
316 # Join the text of all tokens
317 # Used to get the 'real name' of method definitions.
318 fun collect_text: String
319 do
320 var v = new TextCollectorVisitor
321 v.enter_visit(self)
322 assert v.text != ""
323 return v.text
324 end
325 end
326
327 private class TextCollectorVisitor
328 super Visitor
329 var text: String = ""
330 redef fun visit(n)
331 do
332 if n isa Token then text += n.text
333 n.visit_all(self)
334 end
335 end
336
337 redef class APropdef
338 # The associated main model entity
339 type MPROPDEF: MPropDef
340
341 # The associated propdef once build by a `ModelBuilder`
342 var mpropdef: nullable MPROPDEF is writable
343
344 private fun build_property(modelbuilder: ModelBuilder, mclassdef: MClassDef) is abstract
345 private fun build_signature(modelbuilder: ModelBuilder) is abstract
346 private fun check_signature(modelbuilder: ModelBuilder) is abstract
347 private fun new_property_visibility(modelbuilder: ModelBuilder, mclassdef: MClassDef, nvisibility: nullable AVisibility): MVisibility
348 do
349 var mvisibility = public_visibility
350 if nvisibility != null then
351 mvisibility = nvisibility.mvisibility
352 if mvisibility == intrude_visibility then
353 modelbuilder.error(nvisibility, "Error: intrude is not a legal visibility for properties.")
354 mvisibility = public_visibility
355 end
356 end
357 if mclassdef.mclass.visibility == private_visibility then
358 if mvisibility == protected_visibility then
359 assert nvisibility != null
360 modelbuilder.error(nvisibility, "Error: The only legal visibility for properties in a private class is private.")
361 else if mvisibility == private_visibility then
362 assert nvisibility != null
363 modelbuilder.advice(nvisibility, "useless-visibility", "Warning: private is superfluous since the only legal visibility for properties in a private class is private.")
364 end
365 mvisibility = private_visibility
366 end
367 return mvisibility
368 end
369
370 private fun set_doc(mpropdef: MPropDef, modelbuilder: ModelBuilder)
371 do
372 var ndoc = self.n_doc
373 if ndoc != null then
374 var mdoc = ndoc.to_mdoc
375 mpropdef.mdoc = mdoc
376 mdoc.original_mentity = mpropdef
377 else if mpropdef.is_intro and mpropdef.mproperty.visibility >= protected_visibility then
378 modelbuilder.advice(self, "missing-doc", "Documentation warning: Undocumented property `{mpropdef.mproperty}`")
379 end
380
381 var at_deprecated = get_single_annotation("deprecated", modelbuilder)
382 if at_deprecated != null then
383 if not mpropdef.is_intro then
384 modelbuilder.error(self, "Error: method redefinition cannot be deprecated.")
385 else
386 var info = new MDeprecationInfo
387 ndoc = at_deprecated.n_doc
388 if ndoc != null then info.mdoc = ndoc.to_mdoc
389 mpropdef.mproperty.deprecation = info
390 end
391 end
392 end
393
394 private fun check_redef_property_visibility(modelbuilder: ModelBuilder, nvisibility: nullable AVisibility, mprop: MProperty)
395 do
396 if nvisibility == null then return
397 var mvisibility = nvisibility.mvisibility
398 if mvisibility != mprop.visibility and mvisibility != public_visibility then
399 modelbuilder.error(nvisibility, "Error: redefinition changed the visibility from a {mprop.visibility} to a {mvisibility}")
400 end
401 end
402
403 private fun check_redef_keyword(modelbuilder: ModelBuilder, mclassdef: MClassDef, kwredef: nullable Token, need_redef: Bool, mprop: MProperty): Bool
404 do
405 if mclassdef.mprop2npropdef.has_key(mprop) then
406 modelbuilder.error(self, "Error: A property {mprop} is already defined in class {mclassdef.mclass} at line {mclassdef.mprop2npropdef[mprop].location.line_start}.")
407 return false
408 end
409 if mprop isa MMethod and mprop.is_toplevel != (parent isa ATopClassdef) then
410 if mprop.is_toplevel then
411 modelbuilder.error(self, "Error: {mprop} is a top level method.")
412 else
413 modelbuilder.error(self, "Error: {mprop} is not a top level method.")
414 end
415 return false
416
417 end
418 if kwredef == null then
419 if need_redef then
420 modelbuilder.error(self, "Redef error: {mclassdef.mclass}::{mprop.name} is an inherited property. To redefine it, add the redef keyword.")
421 return false
422 end
423 else
424 if not need_redef then
425 modelbuilder.error(self, "Error: No property {mclassdef.mclass}::{mprop.name} is inherited. Remove the redef keyword to define a new property.")
426 return false
427 end
428 end
429 return true
430 end
431
432 end
433
434 redef class ASignature
435 # Is the model builder has correctly visited the signature
436 var is_visited = false
437 # Names of parameters from the AST
438 # REQUIRE: is_visited
439 var param_names = new Array[String]
440 # Types of parameters from the AST
441 # REQUIRE: is_visited
442 var param_types = new Array[MType]
443 # Rank of the vararg (of -1 if none)
444 # REQUIRE: is_visited
445 var vararg_rank: Int = -1
446 # Return type
447 var ret_type: nullable MType = null
448
449 # Visit and fill information about a signature
450 private fun visit_signature(modelbuilder: ModelBuilder, mclassdef: MClassDef): Bool
451 do
452 var mmodule = mclassdef.mmodule
453 var param_names = self.param_names
454 var param_types = self.param_types
455 for np in self.n_params do
456 param_names.add(np.n_id.text)
457 var ntype = np.n_type
458 if ntype != null then
459 var mtype = modelbuilder.resolve_mtype(mmodule, mclassdef, ntype)
460 if mtype == null then return false # Skip error
461 for i in [0..param_names.length-param_types.length[ do
462 param_types.add(mtype)
463 end
464 if np.n_dotdotdot != null then
465 if self.vararg_rank != -1 then
466 modelbuilder.error(np, "Error: {param_names[self.vararg_rank]} is already a vararg")
467 return false
468 else
469 self.vararg_rank = param_names.length - 1
470 end
471 end
472 end
473 end
474 var ntype = self.n_type
475 if ntype != null then
476 self.ret_type = modelbuilder.resolve_mtype(mmodule, mclassdef, ntype)
477 if self.ret_type == null then return false # Skip errir
478 end
479
480 self.is_visited = true
481 return true
482 end
483
484 # Build a visited signature
485 fun build_signature(modelbuilder: ModelBuilder): nullable MSignature
486 do
487 if param_names.length != param_types.length then
488 # Some parameters are typed, other parameters are not typed.
489 modelbuilder.error(self.n_params[param_types.length], "Error: Untyped parameter `{param_names[param_types.length]}'.")
490 return null
491 end
492
493 var mparameters = new Array[MParameter]
494 for i in [0..param_names.length[ do
495 var mparameter = new MParameter(param_names[i], param_types[i], i == vararg_rank)
496 self.n_params[i].mparameter = mparameter
497 mparameters.add(mparameter)
498 end
499
500 var msignature = new MSignature(mparameters, ret_type)
501 return msignature
502 end
503 end
504
505 redef class AParam
506 # The associated mparameter if any
507 var mparameter: nullable MParameter = null
508 end
509
510 redef class AMethPropdef
511 redef type MPROPDEF: MMethodDef
512
513
514 # Can self be used as a root init?
515 private fun look_like_a_root_init(modelbuilder: ModelBuilder): Bool
516 do
517 # Need the `init` keyword
518 if n_kwinit == null then return false
519 # Need to by anonymous
520 if self.n_methid != null then return false
521 # No parameters
522 if self.n_signature.n_params.length > 0 then return false
523 # Cannot be private or something
524 if not self.n_visibility isa APublicVisibility then return false
525 # No annotation on itself
526 if get_single_annotation("old_style_init", modelbuilder) != null then return false
527 # Nor on its module
528 var amod = self.parent.parent.as(AModule)
529 var amoddecl = amod.n_moduledecl
530 if amoddecl != null then
531 var old = amoddecl.get_single_annotation("old_style_init", modelbuilder)
532 if old != null then return false
533 end
534
535 return true
536 end
537
538 redef fun build_property(modelbuilder, mclassdef)
539 do
540 var n_kwinit = n_kwinit
541 var n_kwnew = n_kwnew
542 var is_init = n_kwinit != null or n_kwnew != null
543 var name: String
544 var amethodid = self.n_methid
545 var name_node: ANode
546 if amethodid == null then
547 if not is_init then
548 name = "main"
549 name_node = self
550 else if n_kwinit != null then
551 name = "init"
552 name_node = n_kwinit
553 else if n_kwnew != null then
554 name = "init"
555 name_node = n_kwnew
556 else
557 abort
558 end
559 else if amethodid isa AIdMethid then
560 name = amethodid.n_id.text
561 name_node = amethodid
562 else
563 # operator, bracket or assign
564 name = amethodid.collect_text
565 name_node = amethodid
566
567 if name == "-" and self.n_signature.n_params.length == 0 then
568 name = "unary -"
569 end
570 end
571
572 var mprop: nullable MMethod = null
573 if not is_init or n_kwredef != null then mprop = modelbuilder.try_get_mproperty_by_name(name_node, mclassdef, name).as(nullable MMethod)
574 if mprop == null and look_like_a_root_init(modelbuilder) then
575 mprop = modelbuilder.the_root_init_mmethod
576 var nb = n_block
577 if nb isa ABlockExpr and nb.n_expr.is_empty and n_doc == null then
578 modelbuilder.advice(self, "useless-init", "Warning: useless empty init in {mclassdef}")
579 end
580 end
581 if mprop == null then
582 var mvisibility = new_property_visibility(modelbuilder, mclassdef, self.n_visibility)
583 mprop = new MMethod(mclassdef, name, mvisibility)
584 if look_like_a_root_init(modelbuilder) and modelbuilder.the_root_init_mmethod == null then
585 modelbuilder.the_root_init_mmethod = mprop
586 mprop.is_root_init = true
587 end
588 mprop.is_init = is_init
589 mprop.is_new = n_kwnew != null
590 if parent isa ATopClassdef then mprop.is_toplevel = true
591 if not self.check_redef_keyword(modelbuilder, mclassdef, n_kwredef, false, mprop) then return
592 else
593 if not mprop.is_root_init and not self.check_redef_keyword(modelbuilder, mclassdef, n_kwredef, not self isa AMainMethPropdef, mprop) then return
594 check_redef_property_visibility(modelbuilder, self.n_visibility, mprop)
595 end
596 mclassdef.mprop2npropdef[mprop] = self
597
598 var mpropdef = new MMethodDef(mclassdef, mprop, self.location)
599
600 set_doc(mpropdef, modelbuilder)
601
602 self.mpropdef = mpropdef
603 modelbuilder.mpropdef2npropdef[mpropdef] = self
604 if mpropdef.is_intro then
605 modelbuilder.toolcontext.info("{mpropdef} introduces new method {mprop.full_name}", 3)
606 else
607 modelbuilder.toolcontext.info("{mpropdef} redefines method {mprop.full_name}", 3)
608 end
609 end
610
611 redef fun build_signature(modelbuilder)
612 do
613 var mpropdef = self.mpropdef
614 if mpropdef == null then return # Error thus skiped
615 var mclassdef = mpropdef.mclassdef
616 var mmodule = mclassdef.mmodule
617 var nsig = self.n_signature
618
619 if mpropdef.mproperty.is_root_init and not mclassdef.is_intro then
620 var root_init = mclassdef.mclass.root_init
621 if root_init != null then
622 # Inherit the initializers by refinement
623 mpropdef.new_msignature = root_init.new_msignature
624 assert mpropdef.initializers.is_empty
625 mpropdef.initializers.add_all root_init.initializers
626 end
627 end
628
629 # Retrieve info from the signature AST
630 var param_names = new Array[String] # Names of parameters from the AST
631 var param_types = new Array[MType] # Types of parameters from the AST
632 var vararg_rank = -1
633 var ret_type: nullable MType = null # Return type from the AST
634 if nsig != null then
635 if not nsig.visit_signature(modelbuilder, mclassdef) then return
636 param_names = nsig.param_names
637 param_types = nsig.param_types
638 vararg_rank = nsig.vararg_rank
639 ret_type = nsig.ret_type
640 end
641
642 # Look for some signature to inherit
643 # FIXME: do not inherit from the intro, but from the most specific
644 var msignature: nullable MSignature = null
645 if not mpropdef.is_intro then
646 msignature = mpropdef.mproperty.intro.msignature
647 if msignature == null then return # Skip error
648
649 # Check inherited signature arity
650 if param_names.length != msignature.arity then
651 var node: ANode
652 if nsig != null then node = nsig else node = self
653 modelbuilder.error(node, "Redef error: {mpropdef} redefines {mpropdef.mproperty.intro} with {param_names.length} parameter(s), {msignature.arity} expected. Signature is {mpropdef}{msignature}")
654 return
655 end
656 else if mpropdef.mproperty.is_init then
657 # FIXME UGLY: inherit signature from a super-constructor
658 for msupertype in mclassdef.supertypes do
659 msupertype = msupertype.anchor_to(mmodule, mclassdef.bound_mtype)
660 var candidate = modelbuilder.try_get_mproperty_by_name2(self, mmodule, msupertype, mpropdef.mproperty.name)
661 if candidate != null then
662 if msignature == null then
663 msignature = candidate.intro.as(MMethodDef).msignature
664 end
665 end
666 end
667 end
668
669
670 # Inherit the signature
671 if msignature != null and param_names.length != param_types.length and param_names.length == msignature.arity and param_types.length == 0 then
672 # Parameters are untyped, thus inherit them
673 param_types = new Array[MType]
674 for mparameter in msignature.mparameters do
675 param_types.add(mparameter.mtype)
676 end
677 vararg_rank = msignature.vararg_rank
678 end
679 if msignature != null and ret_type == null then
680 ret_type = msignature.return_mtype
681 end
682
683 if param_names.length != param_types.length then
684 # Some parameters are typed, other parameters are not typed.
685 modelbuilder.error(nsig.n_params[param_types.length], "Error: Untyped parameter `{param_names[param_types.length]}'.")
686 return
687 end
688
689 var mparameters = new Array[MParameter]
690 for i in [0..param_names.length[ do
691 var mparameter = new MParameter(param_names[i], param_types[i], i == vararg_rank)
692 if nsig != null then nsig.n_params[i].mparameter = mparameter
693 mparameters.add(mparameter)
694 end
695
696 msignature = new MSignature(mparameters, ret_type)
697 mpropdef.msignature = msignature
698 mpropdef.is_abstract = self.get_single_annotation("abstract", modelbuilder) != null
699 mpropdef.is_intern = self.get_single_annotation("intern", modelbuilder) != null
700 mpropdef.is_extern = self.n_extern_code_block != null or self.get_single_annotation("extern", modelbuilder) != null
701 end
702
703 redef fun check_signature(modelbuilder)
704 do
705 var mpropdef = self.mpropdef
706 if mpropdef == null then return # Error thus skiped
707 var mclassdef = mpropdef.mclassdef
708 var mmodule = mclassdef.mmodule
709 var nsig = self.n_signature
710 var mysignature = self.mpropdef.msignature
711 if mysignature == null then return # Error thus skiped
712
713 # Lookup for signature in the precursor
714 # FIXME all precursors should be considered
715 if not mpropdef.is_intro then
716 var msignature = mpropdef.mproperty.intro.msignature
717 if msignature == null then return
718
719 var precursor_ret_type = msignature.return_mtype
720 var ret_type = mysignature.return_mtype
721 if ret_type != null and precursor_ret_type == null then
722 modelbuilder.error(nsig.n_type.as(not null), "Redef Error: {mpropdef.mproperty} is a procedure, not a function.")
723 return
724 end
725
726 if mysignature.arity > 0 then
727 # Check parameters types
728 for i in [0..mysignature.arity[ do
729 var myt = mysignature.mparameters[i].mtype
730 var prt = msignature.mparameters[i].mtype
731 if not myt.is_subtype(mmodule, mclassdef.bound_mtype, prt) or
732 not prt.is_subtype(mmodule, mclassdef.bound_mtype, myt) then
733 modelbuilder.error(nsig.n_params[i], "Redef Error: Wrong type for parameter `{mysignature.mparameters[i].name}'. found {myt}, expected {prt} as in {mpropdef.mproperty.intro}.")
734 end
735 end
736 end
737 if precursor_ret_type != null then
738 if ret_type == null then
739 # Inherit the return type
740 ret_type = precursor_ret_type
741 else if not ret_type.is_subtype(mmodule, mclassdef.bound_mtype, precursor_ret_type) then
742 modelbuilder.error(nsig.n_type.as(not null), "Redef Error: Wrong return type. found {ret_type}, expected {precursor_ret_type} as in {mpropdef.mproperty.intro}.")
743 end
744 end
745 end
746
747 if mysignature.arity > 0 then
748 # Check parameters visibility
749 for i in [0..mysignature.arity[ do
750 var nt = nsig.n_params[i].n_type
751 if nt != null then modelbuilder.check_visibility(nt, nt.mtype.as(not null), mpropdef)
752 end
753 var nt = nsig.n_type
754 if nt != null then modelbuilder.check_visibility(nt, nt.mtype.as(not null), mpropdef)
755 end
756 end
757 end
758
759 redef class AAttrPropdef
760 redef type MPROPDEF: MAttributeDef
761
762 # Is the node tagged `noinit`?
763 var noinit = false
764
765 # Is the node taggeg lazy?
766 var is_lazy = false
767
768 # The guard associated to a lasy attribute.
769 # Because some engines does not have a working `isset`,
770 # this additionnal attribute is used to guard the lazy initialization.
771 # TODO: to remove once isset is correctly implemented
772 var mlazypropdef: nullable MAttributeDef
773
774 # The associated getter (read accessor) if any
775 var mreadpropdef: nullable MMethodDef is writable
776 # The associated setter (write accessor) if any
777 var mwritepropdef: nullable MMethodDef is writable
778
779 redef fun build_property(modelbuilder, mclassdef)
780 do
781 var mclass = mclassdef.mclass
782
783 var name: String
784 name = self.n_id2.text
785
786 if mclass.kind == interface_kind or mclassdef.mclass.kind == enum_kind then
787 modelbuilder.error(self, "Error: Attempt to define attribute {name} in the interface {mclass}.")
788 else if mclass.kind == enum_kind then
789 modelbuilder.error(self, "Error: Attempt to define attribute {name} in the enum class {mclass}.")
790 else if mclass.kind == extern_kind then
791 modelbuilder.error(self, "Error: Attempt to define attribute {name} in the extern class {mclass}.")
792 end
793
794 # New attribute style
795 var nid2 = self.n_id2
796 var mprop = new MAttribute(mclassdef, "_" + name, private_visibility)
797 var mpropdef = new MAttributeDef(mclassdef, mprop, self.location)
798 self.mpropdef = mpropdef
799 modelbuilder.mpropdef2npropdef[mpropdef] = self
800
801 var readname = name
802 var mreadprop = modelbuilder.try_get_mproperty_by_name(nid2, mclassdef, readname).as(nullable MMethod)
803 if mreadprop == null then
804 var mvisibility = new_property_visibility(modelbuilder, mclassdef, self.n_visibility)
805 mreadprop = new MMethod(mclassdef, readname, mvisibility)
806 if not self.check_redef_keyword(modelbuilder, mclassdef, n_kwredef, false, mreadprop) then return
807 mreadprop.deprecation = mprop.deprecation
808 else
809 if not self.check_redef_keyword(modelbuilder, mclassdef, n_kwredef, true, mreadprop) then return
810 check_redef_property_visibility(modelbuilder, self.n_visibility, mreadprop)
811 end
812 mclassdef.mprop2npropdef[mreadprop] = self
813
814 var mreadpropdef = new MMethodDef(mclassdef, mreadprop, self.location)
815 self.mreadpropdef = mreadpropdef
816 modelbuilder.mpropdef2npropdef[mreadpropdef] = self
817 set_doc(mreadpropdef, modelbuilder)
818 mpropdef.mdoc = mreadpropdef.mdoc
819
820 var atlazy = self.get_single_annotation("lazy", modelbuilder)
821 if atlazy != null then
822 if n_expr == null then
823 modelbuilder.error(atlazy, "Error: a lazy attribute needs a value")
824 end
825 is_lazy = true
826 var mlazyprop = new MAttribute(mclassdef, "lazy _" + name, none_visibility)
827 var mlazypropdef = new MAttributeDef(mclassdef, mlazyprop, self.location)
828 self.mlazypropdef = mlazypropdef
829 end
830
831 var atreadonly = self.get_single_annotation("readonly", modelbuilder)
832 if atreadonly != null then
833 if n_expr == null then
834 modelbuilder.error(atreadonly, "Error: a readonly attribute needs a value")
835 end
836 # No setter, so just leave
837 return
838 end
839
840 var writename = name + "="
841 var atwritable = self.get_single_annotation("writable", modelbuilder)
842 if atwritable != null then
843 if not atwritable.n_args.is_empty then
844 writename = atwritable.arg_as_id(modelbuilder) or else writename
845 end
846 end
847 var mwriteprop = modelbuilder.try_get_mproperty_by_name(nid2, mclassdef, writename).as(nullable MMethod)
848 var nwkwredef: nullable Token = null
849 if atwritable != null then nwkwredef = atwritable.n_kwredef
850 if mwriteprop == null then
851 var mvisibility
852 if atwritable != null then
853 mvisibility = new_property_visibility(modelbuilder, mclassdef, atwritable.n_visibility)
854 else
855 mvisibility = private_visibility
856 end
857 mwriteprop = new MMethod(mclassdef, writename, mvisibility)
858 if not self.check_redef_keyword(modelbuilder, mclassdef, nwkwredef, false, mwriteprop) then return
859 mwriteprop.deprecation = mprop.deprecation
860 else
861 if not self.check_redef_keyword(modelbuilder, mclassdef, nwkwredef or else n_kwredef, true, mwriteprop) then return
862 if atwritable != null then
863 check_redef_property_visibility(modelbuilder, atwritable.n_visibility, mwriteprop)
864 end
865 end
866 mclassdef.mprop2npropdef[mwriteprop] = self
867
868 var mwritepropdef = new MMethodDef(mclassdef, mwriteprop, self.location)
869 self.mwritepropdef = mwritepropdef
870 modelbuilder.mpropdef2npropdef[mwritepropdef] = self
871 mwritepropdef.mdoc = mpropdef.mdoc
872 end
873
874 redef fun build_signature(modelbuilder)
875 do
876 var mpropdef = self.mpropdef
877 if mpropdef == null then return # Error thus skiped
878 var mclassdef = mpropdef.mclassdef
879 var mmodule = mclassdef.mmodule
880 var mtype: nullable MType = null
881
882 var mreadpropdef = self.mreadpropdef
883
884 var ntype = self.n_type
885 if ntype != null then
886 mtype = modelbuilder.resolve_mtype(mmodule, mclassdef, ntype)
887 if mtype == null then return
888 end
889
890 # Inherit the type from the getter (usually an abstact getter)
891 if mtype == null and mreadpropdef != null and not mreadpropdef.is_intro then
892 var msignature = mreadpropdef.mproperty.intro.msignature
893 if msignature == null then return # Error, thus skiped
894 mtype = msignature.return_mtype
895 end
896
897 var nexpr = self.n_expr
898 if mtype == null then
899 if nexpr != null then
900 if nexpr isa ANewExpr then
901 mtype = modelbuilder.resolve_mtype(mmodule, mclassdef, nexpr.n_type)
902 else if nexpr isa AIntExpr then
903 var cla = modelbuilder.try_get_mclass_by_name(nexpr, mmodule, "Int")
904 if cla != null then mtype = cla.mclass_type
905 else if nexpr isa AFloatExpr then
906 var cla = modelbuilder.try_get_mclass_by_name(nexpr, mmodule, "Float")
907 if cla != null then mtype = cla.mclass_type
908 else if nexpr isa ACharExpr then
909 var cla = modelbuilder.try_get_mclass_by_name(nexpr, mmodule, "Char")
910 if cla != null then mtype = cla.mclass_type
911 else if nexpr isa ABoolExpr then
912 var cla = modelbuilder.try_get_mclass_by_name(nexpr, mmodule, "Bool")
913 if cla != null then mtype = cla.mclass_type
914 else if nexpr isa ASuperstringExpr then
915 var cla = modelbuilder.try_get_mclass_by_name(nexpr, mmodule, "String")
916 if cla != null then mtype = cla.mclass_type
917 else if nexpr isa AStringFormExpr then
918 var cla = modelbuilder.try_get_mclass_by_name(nexpr, mmodule, "String")
919 if cla != null then mtype = cla.mclass_type
920 else
921 modelbuilder.error(self, "Error: Untyped attribute {mpropdef}. Implicit typing allowed only for literals and new.")
922 end
923
924 if mtype == null then return
925 end
926 else if ntype != null then
927 if nexpr isa ANewExpr then
928 var xmtype = modelbuilder.resolve_mtype(mmodule, mclassdef, nexpr.n_type)
929 if xmtype == mtype then
930 modelbuilder.advice(ntype, "useless-type", "Warning: useless type definition")
931 end
932 end
933 end
934
935 if mtype == null then
936 modelbuilder.error(self, "Error: Untyped attribute {mpropdef}")
937 return
938 end
939
940 mpropdef.static_mtype = mtype
941
942 if mreadpropdef != null then
943 var msignature = new MSignature(new Array[MParameter], mtype)
944 mreadpropdef.msignature = msignature
945 end
946
947 var mwritepropdef = self.mwritepropdef
948 if mwritepropdef != null then
949 var name: String
950 name = n_id2.text
951 var mparameter = new MParameter(name, mtype, false)
952 var msignature = new MSignature([mparameter], null)
953 mwritepropdef.msignature = msignature
954 end
955
956 var mlazypropdef = self.mlazypropdef
957 if mlazypropdef != null then
958 mlazypropdef.static_mtype = modelbuilder.model.get_mclasses_by_name("Bool").first.mclass_type
959 end
960 end
961
962 redef fun check_signature(modelbuilder)
963 do
964 var mpropdef = self.mpropdef
965 if mpropdef == null then return # Error thus skiped
966 var mclassdef = mpropdef.mclassdef
967 var mmodule = mclassdef.mmodule
968 var ntype = self.n_type
969 var mtype = self.mpropdef.static_mtype
970 if mtype == null then return # Error thus skiped
971
972 # Lookup for signature in the precursor
973 # FIXME all precursors should be considered
974 if not mpropdef.is_intro then
975 var precursor_type = mpropdef.mproperty.intro.static_mtype
976 if precursor_type == null then return
977
978 if mtype != precursor_type then
979 modelbuilder.error(ntype.as(not null), "Redef Error: Wrong static type. found {mtype}, expected {precursor_type}.")
980 return
981 end
982 end
983
984 # Check getter and setter
985 var meth = self.mreadpropdef
986 if meth != null then
987 self.check_method_signature(modelbuilder, meth)
988 var node: nullable ANode = ntype
989 if node == null then node = self
990 modelbuilder.check_visibility(node, mtype, meth)
991 end
992 meth = self.mwritepropdef
993 if meth != null then
994 self.check_method_signature(modelbuilder, meth)
995 var node: nullable ANode = ntype
996 if node == null then node = self
997 modelbuilder.check_visibility(node, mtype, meth)
998 end
999 end
1000
1001 private fun check_method_signature(modelbuilder: ModelBuilder, mpropdef: MMethodDef)
1002 do
1003 var mclassdef = mpropdef.mclassdef
1004 var mmodule = mclassdef.mmodule
1005 var nsig = self.n_type
1006 var mysignature = mpropdef.msignature
1007 if mysignature == null then return # Error thus skiped
1008
1009 # Lookup for signature in the precursor
1010 # FIXME all precursors should be considered
1011 if not mpropdef.is_intro then
1012 var msignature = mpropdef.mproperty.intro.msignature
1013 if msignature == null then return
1014
1015 if mysignature.arity != msignature.arity then
1016 var node: ANode
1017 if nsig != null then node = nsig else node = self
1018 modelbuilder.error(node, "Redef Error: {mysignature.arity} parameters found, {msignature.arity} expected. Signature is {mpropdef}{msignature}")
1019 return
1020 end
1021 var precursor_ret_type = msignature.return_mtype
1022 var ret_type = mysignature.return_mtype
1023 if ret_type != null and precursor_ret_type == null then
1024 var node: ANode
1025 if nsig != null then node = nsig else node = self
1026 modelbuilder.error(node, "Redef Error: {mpropdef.mproperty} is a procedure, not a function.")
1027 return
1028 end
1029
1030 if mysignature.arity > 0 then
1031 # Check parameters types
1032 for i in [0..mysignature.arity[ do
1033 var myt = mysignature.mparameters[i].mtype
1034 var prt = msignature.mparameters[i].mtype
1035 if not myt.is_subtype(mmodule, mclassdef.bound_mtype, prt) or
1036 not prt.is_subtype(mmodule, mclassdef.bound_mtype, myt) then
1037 var node: ANode
1038 if nsig != null then node = nsig else node = self
1039 modelbuilder.error(node, "Redef Error: Wrong type for parameter `{mysignature.mparameters[i].name}'. found {myt}, expected {prt}.")
1040 end
1041 end
1042 end
1043 if precursor_ret_type != null then
1044 if ret_type == null then
1045 # Inherit the return type
1046 ret_type = precursor_ret_type
1047 else if not ret_type.is_subtype(mmodule, mclassdef.bound_mtype, precursor_ret_type) then
1048 var node: ANode
1049 if nsig != null then node = nsig else node = self
1050 modelbuilder.error(node, "Redef Error: Wrong return type. found {ret_type}, expected {precursor_ret_type}.")
1051 end
1052 end
1053 end
1054 end
1055 end
1056
1057 redef class ATypePropdef
1058 redef type MPROPDEF: MVirtualTypeDef
1059
1060 redef fun build_property(modelbuilder, mclassdef)
1061 do
1062 var name = self.n_id.text
1063 var mprop = modelbuilder.try_get_mproperty_by_name(self.n_id, mclassdef, name)
1064 if mprop == null then
1065 var mvisibility = new_property_visibility(modelbuilder, mclassdef, self.n_visibility)
1066 mprop = new MVirtualTypeProp(mclassdef, name, mvisibility)
1067 for c in name.chars do if c >= 'a' and c<= 'z' then
1068 modelbuilder.warning(n_id, "bad-type-name", "Warning: lowercase in the virtual type {name}")
1069 break
1070 end
1071 if not self.check_redef_keyword(modelbuilder, mclassdef, self.n_kwredef, false, mprop) then return
1072 else
1073 if not self.check_redef_keyword(modelbuilder, mclassdef, self.n_kwredef, true, mprop) then return
1074 assert mprop isa MVirtualTypeProp
1075 check_redef_property_visibility(modelbuilder, self.n_visibility, mprop)
1076 end
1077 mclassdef.mprop2npropdef[mprop] = self
1078
1079 var mpropdef = new MVirtualTypeDef(mclassdef, mprop, self.location)
1080 self.mpropdef = mpropdef
1081 modelbuilder.mpropdef2npropdef[mpropdef] = self
1082 set_doc(mpropdef, modelbuilder)
1083
1084 var atfixed = get_single_annotation("fixed", modelbuilder)
1085 if atfixed != null then
1086 mpropdef.is_fixed = true
1087 end
1088 end
1089
1090 redef fun build_signature(modelbuilder)
1091 do
1092 var mpropdef = self.mpropdef
1093 if mpropdef == null then return # Error thus skiped
1094 var mclassdef = mpropdef.mclassdef
1095 var mmodule = mclassdef.mmodule
1096 var mtype: nullable MType = null
1097
1098 var ntype = self.n_type
1099 mtype = modelbuilder.resolve_mtype(mmodule, mclassdef, ntype)
1100 if mtype == null then return
1101
1102 mpropdef.bound = mtype
1103 # print "{mpropdef}: {mtype}"
1104 end
1105
1106 redef fun check_signature(modelbuilder)
1107 do
1108 var mpropdef = self.mpropdef
1109 if mpropdef == null then return # Error thus skiped
1110
1111 var bound = self.mpropdef.bound
1112 if bound == null then return # Error thus skiped
1113
1114 modelbuilder.check_visibility(n_type, bound, mpropdef)
1115
1116 var mclassdef = mpropdef.mclassdef
1117 var mmodule = mclassdef.mmodule
1118 var anchor = mclassdef.bound_mtype
1119
1120 # Check circularity
1121 if bound isa MVirtualType then
1122 # Slow case: progress on each resolution until: (i) we loop, or (ii) we found a non formal type
1123 var seen = [self.mpropdef.mproperty.mvirtualtype]
1124 loop
1125 if seen.has(bound) then
1126 seen.add(bound)
1127 modelbuilder.error(self, "Error: circularity of virtual type definition: {seen.join(" -> ")}")
1128 return
1129 end
1130 seen.add(bound)
1131 var next = bound.lookup_bound(mmodule, anchor)
1132 if not next isa MVirtualType then break
1133 bound = next
1134 end
1135 end
1136
1137 # Check redefinitions
1138 bound = mpropdef.bound.as(not null)
1139 for p in mpropdef.mproperty.lookup_super_definitions(mmodule, anchor) do
1140 var supbound = p.bound.as(not null)
1141 if p.is_fixed then
1142 modelbuilder.error(self, "Redef Error: Virtual type {mpropdef.mproperty} is fixed in super-class {p.mclassdef.mclass}")
1143 break
1144 end
1145 if p.mclassdef.mclass == mclassdef.mclass then
1146 # Still a warning to pass existing bad code
1147 modelbuilder.warning(n_type, "refine-type", "Redef Error: a virtual type cannot be refined.")
1148 break
1149 end
1150 if not bound.is_subtype(mmodule, anchor, supbound) then
1151 modelbuilder.error(n_type, "Redef Error: Wrong bound type. Found {bound}, expected a subtype of {supbound}, as in {p}.")
1152 break
1153 end
1154 end
1155 end
1156 end