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