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