1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2012 Jean Privat <jean@pryen.org>
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 # Analysis and verification of property definitions to instantiate model element
18 module modelize_property
20 intrude import modelize_class
21 private import annotation
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
])
28 private class ModelizePropertyPhase
30 redef fun process_nmodule
(nmodule
)
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
)
39 redef class ModelBuilder
40 # Registration of the npropdef associated to each mpropdef.
42 # Public clients need to use `mpropdef2node` to access stuff.
43 private var mpropdef2npropdef
= new HashMap[MPropDef, APropdef]
45 # Retrieve the associated AST node of a mpropertydef.
46 # This method is used to associate model entity with syntactic entities.
48 # If the property definition is not associated with a node, returns `null`.
49 fun mpropdef2node
(mpropdef
: MPropDef): nullable ANode
52 res
= mpropdef2npropdef
.get_or_null
(mpropdef
)
54 # Run the phases on it
55 toolcontext
.run_phases_on_npropdef
(res
)
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
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]
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
)
82 # Build the properties of `nclassdef`.
83 # REQUIRE: all superclasses are built.
84 private fun build_properties
(nclassdef
: AClassdef)
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
])
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
)
101 for npropdef
in nclassdef2
.n_propdefs
do
102 npropdef
.build_signature
(self)
104 for npropdef
in nclassdef2
.n_propdefs
do
105 npropdef
.check_signature
(self)
108 process_default_constructors
(nclassdef
)
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
116 # Introduce or inherit default constructor
117 # This is the last part of `build_properties`.
118 private fun process_default_constructors
(nclassdef
: AClassdef)
120 var mclassdef
= nclassdef
.mclassdef
.as(not null)
122 # Are we a refinement
123 if not mclassdef
.is_intro
then return
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
136 nclassdef
.mfree_init
= mpropdef
137 self.toolcontext
.info
("{mclassdef} gets a free empty constructor {mpropdef}{msignature}", 3)
138 the_root_init_mmethod
= mprop
142 # Is the class forbid constructors?
143 if not mclassdef
.mclass
.kind
.need_init
then return
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
159 if not nclassdef
isa AStdClassdef then return
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
170 var sig
= npropdef
.mpropdef
.msignature
171 if sig
== null then continue # Skip broken method
173 if not npropdef
.mpropdef
.is_intro
then
174 self.error
(at
, "Error: `autoinit` cannot be set on redefinitions")
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
)
183 initializers
.add
(npropdef
.mpropdef
.mproperty
)
184 npropdef
.mpropdef
.mproperty
.is_autoinit
= true
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
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
209 # Add the setter to the list
210 initializers
.add
(msetter
.mproperty
)
211 msetter
.mproperty
.is_autoinit
= true
216 if the_root_init_mmethod
== null then return
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`?")
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
235 for spd
in spropdefs
do
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(", ")})")
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
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
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
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
283 # Check the visibility of `mtype` as an element of the signature of `mpropdef`.
284 fun check_visibility
(node
: ANode, mtype
: MType, mpropdef
: MPropDef)
286 var mmodule
= mpropdef
.mclassdef
.mmodule
287 var mproperty
= mpropdef
.mproperty
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
303 node
.debug
"Unexpected type {mtype}"
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}`")
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}`")
319 # No error, try to go deeper in generic types
320 if node
isa AType then
321 for a
in node
.n_types
do
323 if t
== null then continue # Error, thus skipped
324 check_visibility
(a
, t
, mpropdef
)
326 else if mtype
isa MGenericType then
327 for t
in mtype
.arguments
do check_visibility
(node
, t
, 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
338 redef class AClassdef
339 # Marker used in `ModelBuilder::build_properties`
340 private var build_properties_is_done
= false
342 # The free init (implicitely constructed by the class if required)
343 var mfree_init
: nullable MMethodDef = null
347 # The base init of the class.
348 # Used to get the common new_msignature and initializers
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
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]
360 # Build the virtual type `SELF` only for introduction `MClassDef`
361 fun build_self_type
(modelbuilder
: ModelBuilder, nclassdef
: AClassdef)
363 if not is_intro
then return
366 var mprop
= modelbuilder
.try_get_mproperty_by_name
(nclassdef
, self, name
)
368 # If SELF type is declared nowherer?
369 if mprop
== null then return
371 # SELF is not a virtual type? it is weird but we ignore it
372 if not mprop
isa MVirtualTypeProp then return
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
]
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.")
385 # SELF must be public
386 if mprop
.visibility
!= public_visibility
then
387 modelbuilder
.error
(nintro
, "Error: the virtual type SELF must be public.")
390 # SELF must not be fixed
391 if intro
.is_fixed
then
392 modelbuilder
.error
(nintro
, "Error: the virtual type SELF cannot be fixed.")
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
406 # The associated main model entity
407 type MPROPDEF: MPropDef
409 # The associated propdef once build by a `ModelBuilder`
410 var mpropdef
: nullable MPROPDEF is writable
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
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
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.")
433 mvisibility
= private_visibility
438 private fun set_doc
(mpropdef
: MPropDef, modelbuilder
: ModelBuilder)
440 var ndoc
= self.n_doc
442 var mdoc
= ndoc
.to_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}`")
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.")
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
462 private fun check_redef_property_visibility
(modelbuilder
: ModelBuilder, nvisibility
: nullable AVisibility, mprop
: MProperty)
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}")
471 private fun check_redef_keyword
(modelbuilder
: ModelBuilder, mclassdef
: MClassDef, kwredef
: nullable Token, need_redef
: Bool, mprop
: MProperty): Bool
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}.")
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.")
481 modelbuilder
.error
(self, "Error: {mprop} is not a top level method.")
486 if mprop
isa MMethod and mprop
.is_root_init
then return true
487 if kwredef
== null then
489 modelbuilder
.error
(self, "Redef error: {mclassdef.mclass}::{mprop.name} is an inherited property. To redefine it, add the redef keyword.")
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}`.")
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.")
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
528 var ret_type
: nullable MType = null
530 # Visit and fill information about a signature
531 private fun visit_signature
(modelbuilder
: ModelBuilder, mclassdef
: MClassDef): Bool
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
)
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")
550 self.vararg_rank
= param_names
.length
- 1
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
561 self.is_visited
= true
565 # Build a visited signature
566 fun build_signature
(modelbuilder
: ModelBuilder): nullable MSignature
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]}'.")
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
)
581 var msignature
= new MSignature(mparameters
, ret_type
)
587 # The associated mparameter if any
588 var mparameter
: nullable MParameter = null
591 redef class AMethPropdef
592 redef type MPROPDEF: MMethodDef
595 # Can self be used as a root init?
596 private fun look_like_a_root_init
(modelbuilder
: ModelBuilder, mclassdef
: MClassDef): Bool
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
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
612 if self.n_signature
.n_params
.length
> 0 then
613 modelbuilder
.advice
(self, "old-init", "Warning: init with signature in {mclassdef}")
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}")
625 redef fun build_property
(modelbuilder
, mclassdef
)
627 var n_kwinit
= n_kwinit
628 var n_kwnew
= n_kwnew
629 var is_init
= n_kwinit
!= null or n_kwnew
!= null
631 var amethodid
= self.n_methid
633 if amethodid
== null then
637 else if n_kwinit
!= null then
640 else if n_kwnew
!= null then
646 else if amethodid
isa AIdMethid then
647 name
= amethodid
.n_id
.text
648 name_node
= amethodid
650 # operator, bracket or assign
651 name
= amethodid
.collect_text
652 name_node
= amethodid
654 if name
== "-" and self.n_signature
.n_params
.length
== 0 then
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
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}")
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
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
)
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
)
685 # Check name conflicts in the local class for constructors.
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
)
695 mclassdef
.mprop2npropdef
[mprop
] = self
697 var mpropdef
= new MMethodDef(mclassdef
, mprop
, self.location
)
699 set_doc
(mpropdef
, modelbuilder
)
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)
706 modelbuilder
.toolcontext
.info
("{mpropdef} redefines method {mprop.full_name}", 4)
710 redef fun build_signature
(modelbuilder
)
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
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
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
732 var ret_type
: nullable MType = null # Return type from the AST
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
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
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)
751 # Check inherited signature arity
752 if param_names
.length
!= msignature
.arity
then
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}")
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
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
)
779 vararg_rank
= msignature
.vararg_rank
781 if msignature
!= null and ret_type
== null then
782 ret_type
= msignature
.return_mtype
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]}'.")
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
)
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
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
808 redef fun check_signature
(modelbuilder
)
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
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
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.")
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}.")
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}.")
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
)
862 if nt
!= null then modelbuilder
.check_visibility
(nt
, nt
.mtype
.as(not null), mpropdef
)
867 redef class AAttrPropdef
868 redef type MPROPDEF: MAttributeDef
870 # Is the node tagged `noinit`?
873 # Is the node tagged lazy?
876 # Has the node a default value?
877 # Could be through `n_expr` or `n_block`
878 var has_value
= false
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
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
891 redef fun build_property
(modelbuilder
, mclassdef
)
893 var mclass
= mclassdef
.mclass
896 name
= self.n_id2
.text
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}.")
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
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
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
)
924 mclassdef
.mprop2npropdef
[mreadprop
] = self
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
932 has_value
= n_expr
!= null or n_block
!= null
934 var atnoinit
= self.get_single_annotation
("noinit", modelbuilder
)
935 if atnoinit
!= null then
938 modelbuilder
.error
(atnoinit
, "Error: `noinit` attributes cannot have an initial value")
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")
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")
959 var mlazyprop
= new MAttribute(mclassdef
, "lazy _" + name
, none_visibility
)
960 var mlazypropdef
= new MAttributeDef(mclassdef
, mlazyprop
, self.location
)
961 self.mlazypropdef
= mlazypropdef
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")
969 # No setter, so just leave
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
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
985 if atwritable
!= null then
986 mvisibility
= new_property_visibility
(modelbuilder
, mclassdef
, atwritable
.n_visibility
)
988 mvisibility
= private_visibility
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
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
)
999 mclassdef
.mprop2npropdef
[mwriteprop
] = self
1001 var mwritepropdef
= new MMethodDef(mclassdef
, mwriteprop
, self.location
)
1002 self.mwritepropdef
= mwritepropdef
1003 modelbuilder
.mpropdef2npropdef
[mwritepropdef
] = self
1004 mwritepropdef
.mdoc
= mpropdef
.mdoc
1007 redef fun build_signature
(modelbuilder
)
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
1015 var mreadpropdef
= self.mreadpropdef
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
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
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
1060 modelbuilder
.error
(self, "Error: Untyped attribute {mpropdef}. Implicit typing allowed only for literals and new.")
1063 if mtype
== null then return
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")
1074 if mtype
== null then
1075 modelbuilder
.error
(self, "Error: Untyped attribute {mpropdef}")
1079 mpropdef
.static_mtype
= mtype
1081 if mreadpropdef
!= null then
1082 var msignature
= new MSignature(new Array[MParameter], mtype
)
1083 mreadpropdef
.msignature
= msignature
1086 var mwritepropdef
= self.mwritepropdef
1087 if mwritepropdef
!= null then
1090 var mparameter
= new MParameter(name
, mtype
, false)
1091 var msignature
= new MSignature([mparameter
], null)
1092 mwritepropdef
.msignature
= msignature
1095 var mlazypropdef
= self.mlazypropdef
1096 if mlazypropdef
!= null then
1097 mlazypropdef
.static_mtype
= modelbuilder
.model
.get_mclasses_by_name
("Bool").first
.mclass_type
1101 redef fun check_signature
(modelbuilder
)
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
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
1115 if mtype
!= precursor_type
then
1116 modelbuilder
.error
(ntype
.as(not null), "Redef Error: Wrong static type. found {mtype}, expected {precursor_type}.")
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
)
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
)
1138 private fun check_method_signature
(modelbuilder
: ModelBuilder, mpropdef
: MMethodDef)
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
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
1152 if mysignature
.arity
!= msignature
.arity
then
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}")
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
1162 if nsig
!= null then node
= nsig
else node
= self
1163 modelbuilder
.error
(node
, "Redef Error: {mpropdef.mproperty} is a procedure, not a function.")
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
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}.")
1179 if precursor_ret_type
!= null then
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}.")
1193 redef class ATypePropdef
1194 redef type MPROPDEF: MVirtualTypeDef
1196 redef fun build_property
(modelbuilder
, mclassdef
)
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}")
1207 if not self.check_redef_keyword
(modelbuilder
, mclassdef
, self.n_kwredef
, false, mprop
) then return
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
)
1213 mclassdef
.mprop2npropdef
[mprop
] = self
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)
1221 modelbuilder
.toolcontext
.info
("{mpropdef} redefines type {mprop.full_name}", 4)
1223 set_doc
(mpropdef
, modelbuilder
)
1225 var atfixed
= get_single_annotation
("fixed", modelbuilder
)
1226 if atfixed
!= null then
1227 mpropdef
.is_fixed
= true
1231 redef fun build_signature
(modelbuilder
)
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
1239 var ntype
= self.n_type
1240 mtype
= modelbuilder
.resolve_mtype
(mmodule
, mclassdef
, ntype
)
1241 if mtype
== null then return
1243 mpropdef
.bound
= mtype
1244 # print "{mpropdef}: {mtype}"
1247 redef fun check_signature
(modelbuilder
)
1249 var mpropdef
= self.mpropdef
1250 if mpropdef
== null then return # Error thus skipped
1252 var bound
= self.mpropdef
.bound
1253 if bound
== null then return # Error thus skipped
1255 modelbuilder
.check_visibility
(n_type
, bound
, mpropdef
)
1257 var mclassdef
= mpropdef
.mclassdef
1258 var mmodule
= mclassdef
.mmodule
1259 var anchor
= mclassdef
.bound_mtype
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
]
1266 if seen
.has
(bound
) then
1268 modelbuilder
.error
(self, "Error: circularity of virtual type definition: {seen.join(" -> ")}")
1272 var next
= bound
.lookup_bound
(mmodule
, anchor
)
1273 if not next
isa MVirtualType then break
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
1284 modelbuilder
.error
(self, "Redef Error: Virtual type {mpropdef.mproperty} is fixed in super-class {p.mclassdef.mclass}")
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.")
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}.")