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
21 private import annotation
23 redef class ToolContext
24 var modelize_property_phase
: Phase = new ModelizePropertyPhase(self, [modelize_class_phase
])
27 private class ModelizePropertyPhase
29 redef fun process_nmodule
(nmodule
)
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
)
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]
43 # Retrieve the associated AST node of a mpropertydef.
44 # This method is used to associate model entity with syntactic entities.
46 # If the property definition is not associated with a node, returns node.
47 fun mpropdef2node
(mpropdef
: MPropDef): nullable ANode
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
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]
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
73 # Build the properties of `nclassdef`.
74 # REQUIRE: all superclasses are built.
75 private fun build_properties
(nclassdef
: AClassdef)
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
])
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
)
92 for npropdef
in nclassdef2
.n_propdefs
do
93 npropdef
.build_signature
(self)
95 for npropdef
in nclassdef2
.n_propdefs
do
96 npropdef
.check_signature
(self)
99 process_default_constructors
(nclassdef
)
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
107 # Introduce or inherit default constructor
108 # This is the last part of `build_properties`.
109 private fun process_default_constructors
(nclassdef
: AClassdef)
111 var mclassdef
= nclassdef
.mclassdef
.as(not null)
113 # Are we a refinement
114 if not mclassdef
.is_intro
then return
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
127 nclassdef
.mfree_init
= mpropdef
128 self.toolcontext
.info
("{mclassdef} gets a free empty constructor {mpropdef}{msignature}", 3)
129 the_root_init_mmethod
= mprop
133 # Is the class forbid constructors?
134 if not mclassdef
.mclass
.kind
.need_init
then return
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
150 if not nclassdef
isa AStdClassdef then return
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
161 var sig
= npropdef
.mpropdef
.msignature
162 if sig
== null then continue # Skip broken method
164 if not npropdef
.mpropdef
.is_intro
then
165 self.error
(at
, "Error: `autoinit` cannot be set on redefinitions")
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
)
174 initializers
.add
(npropdef
.mpropdef
.mproperty
)
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
)
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
)
197 # Add the setter to the list
198 initializers
.add
(msetter
.mproperty
)
203 if the_root_init_mmethod
== null then return
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`?")
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
222 for spd
in spropdefs
do
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(", ")})")
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
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
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
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
270 # Check the visibility of `mtype` as an element of the signature of `mpropdef`.
271 fun check_visibility
(node
: ANode, mtype
: MType, mpropdef
: MPropDef)
273 var mmodule
= mpropdef
.mclassdef
.mmodule
274 var mproperty
= mpropdef
.mproperty
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
290 node
.debug
"Unexpected type {mtype}"
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}`")
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}`")
306 # No error, try to go deeper in generic types
307 if node
isa AType then
308 for a
in node
.n_types
do
310 if t
== null then continue # Error, thus skipped
311 check_visibility
(a
, t
, mpropdef
)
313 else if mtype
isa MGenericType then
314 for t
in mtype
.arguments
do check_visibility
(node
, t
, 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
325 redef class AClassdef
326 var build_properties_is_done
= false
328 # The free init (implicitely constructed by the class if required)
329 var mfree_init
: nullable MMethodDef = null
333 # The base init of the class.
334 # Used to get the common new_msignature and initializers
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
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]
346 # Build the virtual type `SELF` only for introduction `MClassDef`
347 fun build_self_type
(modelbuilder
: ModelBuilder, nclassdef
: AClassdef)
349 if not is_intro
then return
352 var mprop
= modelbuilder
.try_get_mproperty_by_name
(nclassdef
, self, name
)
354 # If SELF type is declared nowherer?
355 if mprop
== null then return
357 # SELF is not a virtual type? it is weird but we ignore it
358 if not mprop
isa MVirtualTypeProp then return
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
]
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.")
371 # SELF must be public
372 if mprop
.visibility
!= public_visibility
then
373 modelbuilder
.error
(nintro
, "Error: the virtual type SELF must be public.")
376 # SELF must not be fixed
377 if intro
.is_fixed
then
378 modelbuilder
.error
(nintro
, "Error: the virtual type SELF cannot be fixed.")
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
392 # The associated main model entity
393 type MPROPDEF: MPropDef
395 # The associated propdef once build by a `ModelBuilder`
396 var mpropdef
: nullable MPROPDEF is writable
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
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
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.")
419 mvisibility
= private_visibility
424 private fun set_doc
(mpropdef
: MPropDef, modelbuilder
: ModelBuilder)
426 var ndoc
= self.n_doc
428 var mdoc
= ndoc
.to_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}`")
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.")
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
448 private fun check_redef_property_visibility
(modelbuilder
: ModelBuilder, nvisibility
: nullable AVisibility, mprop
: MProperty)
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}")
457 private fun check_redef_keyword
(modelbuilder
: ModelBuilder, mclassdef
: MClassDef, kwredef
: nullable Token, need_redef
: Bool, mprop
: MProperty): Bool
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}.")
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.")
467 modelbuilder
.error
(self, "Error: {mprop} is not a top level method.")
472 if kwredef
== null then
474 modelbuilder
.error
(self, "Redef error: {mclassdef.mclass}::{mprop.name} is an inherited property. To redefine it, add the redef keyword.")
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.")
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
501 var ret_type
: nullable MType = null
503 # Visit and fill information about a signature
504 private fun visit_signature
(modelbuilder
: ModelBuilder, mclassdef
: MClassDef): Bool
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
)
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")
523 self.vararg_rank
= param_names
.length
- 1
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
534 self.is_visited
= true
538 # Build a visited signature
539 fun build_signature
(modelbuilder
: ModelBuilder): nullable MSignature
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]}'.")
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
)
554 var msignature
= new MSignature(mparameters
, ret_type
)
560 # The associated mparameter if any
561 var mparameter
: nullable MParameter = null
564 redef class AMethPropdef
565 redef type MPROPDEF: MMethodDef
568 # Can self be used as a root init?
569 private fun look_like_a_root_init
(modelbuilder
: ModelBuilder, mclassdef
: MClassDef): Bool
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
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
585 if self.n_signature
.n_params
.length
> 0 then
586 modelbuilder
.advice
(self, "old-init", "Warning: init with signature in {mclassdef}")
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}")
598 redef fun build_property
(modelbuilder
, mclassdef
)
600 var n_kwinit
= n_kwinit
601 var n_kwnew
= n_kwnew
602 var is_init
= n_kwinit
!= null or n_kwnew
!= null
604 var amethodid
= self.n_methid
606 if amethodid
== null then
610 else if n_kwinit
!= null then
613 else if n_kwnew
!= null then
619 else if amethodid
isa AIdMethid then
620 name
= amethodid
.n_id
.text
621 name_node
= amethodid
623 # operator, bracket or assign
624 name
= amethodid
.collect_text
625 name_node
= amethodid
627 if name
== "-" and self.n_signature
.n_params
.length
== 0 then
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
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}")
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
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
)
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
)
657 mclassdef
.mprop2npropdef
[mprop
] = self
659 var mpropdef
= new MMethodDef(mclassdef
, mprop
, self.location
)
661 set_doc
(mpropdef
, modelbuilder
)
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)
668 modelbuilder
.toolcontext
.info
("{mpropdef} redefines method {mprop.full_name}", 4)
672 redef fun build_signature
(modelbuilder
)
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
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
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
694 var ret_type
: nullable MType = null # Return type from the AST
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
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
710 # Check inherited signature arity
711 if param_names
.length
!= msignature
.arity
then
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}")
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
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
)
738 vararg_rank
= msignature
.vararg_rank
740 if msignature
!= null and ret_type
== null then
741 ret_type
= msignature
.return_mtype
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]}'.")
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
)
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
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
767 redef fun check_signature
(modelbuilder
)
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
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
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.")
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}.")
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}.")
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
)
818 if nt
!= null then modelbuilder
.check_visibility
(nt
, nt
.mtype
.as(not null), mpropdef
)
823 redef class AAttrPropdef
824 redef type MPROPDEF: MAttributeDef
826 # Is the node tagged `noinit`?
829 # Is the node tagged lazy?
832 # Has the node a default value?
833 # Could be through `n_expr` or `n_block`
834 var has_value
= false
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
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
847 redef fun build_property
(modelbuilder
, mclassdef
)
849 var mclass
= mclassdef
.mclass
852 name
= self.n_id2
.text
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}.")
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
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
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
)
880 mclassdef
.mprop2npropdef
[mreadprop
] = self
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
888 has_value
= n_expr
!= null or n_block
!= null
890 var atnoinit
= self.get_single_annotation
("noinit", modelbuilder
)
891 if atnoinit
!= null then
894 modelbuilder
.error
(atnoinit
, "Error: `noinit` attributes cannot have an initial value")
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")
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")
915 var mlazyprop
= new MAttribute(mclassdef
, "lazy _" + name
, none_visibility
)
916 var mlazypropdef
= new MAttributeDef(mclassdef
, mlazyprop
, self.location
)
917 self.mlazypropdef
= mlazypropdef
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")
925 # No setter, so just leave
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
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
941 if atwritable
!= null then
942 mvisibility
= new_property_visibility
(modelbuilder
, mclassdef
, atwritable
.n_visibility
)
944 mvisibility
= private_visibility
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
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
)
955 mclassdef
.mprop2npropdef
[mwriteprop
] = self
957 var mwritepropdef
= new MMethodDef(mclassdef
, mwriteprop
, self.location
)
958 self.mwritepropdef
= mwritepropdef
959 modelbuilder
.mpropdef2npropdef
[mwritepropdef
] = self
960 mwritepropdef
.mdoc
= mpropdef
.mdoc
963 redef fun build_signature
(modelbuilder
)
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
971 var mreadpropdef
= self.mreadpropdef
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
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
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
1012 modelbuilder
.error
(self, "Error: Untyped attribute {mpropdef}. Implicit typing allowed only for literals and new.")
1015 if mtype
== null then return
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")
1026 if mtype
== null then
1027 modelbuilder
.error
(self, "Error: Untyped attribute {mpropdef}")
1031 mpropdef
.static_mtype
= mtype
1033 if mreadpropdef
!= null then
1034 var msignature
= new MSignature(new Array[MParameter], mtype
)
1035 mreadpropdef
.msignature
= msignature
1038 var mwritepropdef
= self.mwritepropdef
1039 if mwritepropdef
!= null then
1042 var mparameter
= new MParameter(name
, mtype
, false)
1043 var msignature
= new MSignature([mparameter
], null)
1044 mwritepropdef
.msignature
= msignature
1047 var mlazypropdef
= self.mlazypropdef
1048 if mlazypropdef
!= null then
1049 mlazypropdef
.static_mtype
= modelbuilder
.model
.get_mclasses_by_name
("Bool").first
.mclass_type
1053 redef fun check_signature
(modelbuilder
)
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
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
1067 if mtype
!= precursor_type
then
1068 modelbuilder
.error
(ntype
.as(not null), "Redef Error: Wrong static type. found {mtype}, expected {precursor_type}.")
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
)
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
)
1090 private fun check_method_signature
(modelbuilder
: ModelBuilder, mpropdef
: MMethodDef)
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
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
1104 if mysignature
.arity
!= msignature
.arity
then
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}")
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
1114 if nsig
!= null then node
= nsig
else node
= self
1115 modelbuilder
.error
(node
, "Redef Error: {mpropdef.mproperty} is a procedure, not a function.")
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
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}.")
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
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}.")
1146 redef class ATypePropdef
1147 redef type MPROPDEF: MVirtualTypeDef
1149 redef fun build_property
(modelbuilder
, mclassdef
)
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}")
1160 if not self.check_redef_keyword
(modelbuilder
, mclassdef
, self.n_kwredef
, false, mprop
) then return
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
)
1166 mclassdef
.mprop2npropdef
[mprop
] = self
1168 var mpropdef
= new MVirtualTypeDef(mclassdef
, mprop
, self.location
)
1169 self.mpropdef
= mpropdef
1170 modelbuilder
.mpropdef2npropdef
[mpropdef
] = self
1171 set_doc
(mpropdef
, modelbuilder
)
1173 var atfixed
= get_single_annotation
("fixed", modelbuilder
)
1174 if atfixed
!= null then
1175 mpropdef
.is_fixed
= true
1179 redef fun build_signature
(modelbuilder
)
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
1187 var ntype
= self.n_type
1188 mtype
= modelbuilder
.resolve_mtype
(mmodule
, mclassdef
, ntype
)
1189 if mtype
== null then return
1191 mpropdef
.bound
= mtype
1192 # print "{mpropdef}: {mtype}"
1195 redef fun check_signature
(modelbuilder
)
1197 var mpropdef
= self.mpropdef
1198 if mpropdef
== null then return # Error thus skipped
1200 var bound
= self.mpropdef
.bound
1201 if bound
== null then return # Error thus skipped
1203 modelbuilder
.check_visibility
(n_type
, bound
, mpropdef
)
1205 var mclassdef
= mpropdef
.mclassdef
1206 var mmodule
= mclassdef
.mmodule
1207 var anchor
= mclassdef
.bound_mtype
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
]
1214 if seen
.has
(bound
) then
1216 modelbuilder
.error
(self, "Error: circularity of virtual type definition: {seen.join(" -> ")}")
1220 var next
= bound
.lookup_bound
(mmodule
, anchor
)
1221 if not next
isa MVirtualType then break
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
1232 modelbuilder
.error
(self, "Redef Error: Virtual type {mpropdef.mproperty} is fixed in super-class {p.mclassdef.mclass}")
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.")
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}.")