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 # Build the properties of `nclassdef`.
59 # REQUIRE: all superclasses are built.
60 private fun build_properties
(nclassdef
: AClassdef)
62 # Force building recursively
63 if nclassdef
.build_properties_is_done
then return
64 nclassdef
.build_properties_is_done
= true
65 var mclassdef
= nclassdef
.mclassdef
.as(not null)
66 if mclassdef
.in_hierarchy
== null then return # Skip error
67 for superclassdef
in mclassdef
.in_hierarchy
.direct_greaters
do
68 if not mclassdef2nclassdef
.has_key
(superclassdef
) then continue
69 build_properties
(mclassdef2nclassdef
[superclassdef
])
72 mclassdef
.build_self_type
(self, nclassdef
)
73 for nclassdef2
in nclassdef
.all_defs
do
74 for npropdef
in nclassdef2
.n_propdefs
do
75 npropdef
.build_property
(self, mclassdef
)
77 for npropdef
in nclassdef2
.n_propdefs
do
78 npropdef
.build_signature
(self)
80 for npropdef
in nclassdef2
.n_propdefs
do
81 npropdef
.check_signature
(self)
84 process_default_constructors
(nclassdef
)
87 # the root init of the Object class
88 # Is usually implicitly defined
89 # Then explicit or implicit definitions of root-init are attached to it
90 var the_root_init_mmethod
: nullable MMethod
92 # Introduce or inherit default constructor
93 # This is the last part of `build_properties`.
94 private fun process_default_constructors
(nclassdef
: AClassdef)
96 var mclassdef
= nclassdef
.mclassdef
.as(not null)
99 if not mclassdef
.is_intro
then return
101 # Look for the init in Object, or create it
102 if mclassdef
.mclass
.name
== "Object" and the_root_init_mmethod
== null then
103 # Create the implicit root-init method
104 var mprop
= new MMethod(mclassdef
, "init", mclassdef
.mclass
.visibility
)
105 mprop
.is_root_init
= true
106 var mpropdef
= new MMethodDef(mclassdef
, mprop
, nclassdef
.location
)
107 var mparameters
= new Array[MParameter]
108 var msignature
= new MSignature(mparameters
, null)
109 mpropdef
.msignature
= msignature
110 mpropdef
.new_msignature
= msignature
112 nclassdef
.mfree_init
= mpropdef
113 self.toolcontext
.info
("{mclassdef} gets a free empty constructor {mpropdef}{msignature}", 3)
114 the_root_init_mmethod
= mprop
118 # Is the class forbid constructors?
119 if not mclassdef
.mclass
.kind
.need_init
then return
121 # Is there already a constructor defined?
122 var defined_init
: nullable MMethodDef = null
123 for mpropdef
in mclassdef
.mpropdefs
do
124 if not mpropdef
isa MMethodDef then continue
125 if not mpropdef
.mproperty
.is_init
then continue
126 if mpropdef
.mproperty
.is_root_init
then
127 assert defined_init
== null
128 defined_init
= mpropdef
129 else if mpropdef
.mproperty
.name
== "init" then
130 # An explicit old-style init named "init", so return
135 if not nclassdef
isa AStdClassdef then return
137 # Collect undefined attributes
138 var mparameters
= new Array[MParameter]
139 var initializers
= new Array[MProperty]
140 for npropdef
in nclassdef
.n_propdefs
do
141 if npropdef
isa AMethPropdef then
142 if npropdef
.mpropdef
== null then return # Skip broken attribute
143 var at
= npropdef
.get_single_annotation
("autoinit", self)
144 if at
== null then continue # Skip non tagged init
146 var sig
= npropdef
.mpropdef
.msignature
147 if sig
== null then continue # Skip broken method
149 if not npropdef
.mpropdef
.is_intro
then
150 self.error
(at
, "Error: `autoinit` cannot be set on redefinitions")
154 for param
in sig
.mparameters
do
155 var ret_type
= param
.mtype
156 var mparameter
= new MParameter(param
.name
, ret_type
, false)
157 mparameters
.add
(mparameter
)
159 initializers
.add
(npropdef
.mpropdef
.mproperty
)
161 if npropdef
isa AAttrPropdef then
162 if npropdef
.mpropdef
== null then return # Skip broken attribute
163 if npropdef
.noinit
then continue # Skip noinit attribute
164 var atautoinit
= npropdef
.get_single_annotation
("autoinit", self)
165 if atautoinit
!= null then
166 # For autoinit attributes, call the reader to force
167 # the lazy initialization of the attribute.
168 initializers
.add
(npropdef
.mreadpropdef
.mproperty
)
171 if npropdef
.has_value
then continue
172 var paramname
= npropdef
.mpropdef
.mproperty
.name
.substring_from
(1)
173 var ret_type
= npropdef
.mpropdef
.static_mtype
174 if ret_type
== null then return
175 var mparameter
= new MParameter(paramname
, ret_type
, false)
176 mparameters
.add
(mparameter
)
177 var msetter
= npropdef
.mwritepropdef
178 if msetter
== null then
179 # No setter, it is a old-style attribute, so just add it
180 initializers
.add
(npropdef
.mpropdef
.mproperty
)
182 # Add the setter to the list
183 initializers
.add
(msetter
.mproperty
)
188 if the_root_init_mmethod
== null then return
190 # Look for most-specific new-stype init definitions
191 var spropdefs
= the_root_init_mmethod
.lookup_super_definitions
(mclassdef
.mmodule
, mclassdef
.bound_mtype
)
192 if spropdefs
.is_empty
then
193 toolcontext
.error
(nclassdef
.location
, "Error: {mclassdef} does not specialize {the_root_init_mmethod.intro_mclassdef}. Possible duplication of the root class `Object`?")
197 # Search the longest-one and checks for conflict
198 var longest
= spropdefs
.first
199 if spropdefs
.length
> 1 then
200 # Check for conflict in the order of initializers
201 # Each initializer list must me a prefix of the longest list
202 # part 1. find the longest list
203 for spd
in spropdefs
do
204 if spd
.initializers
.length
> longest
.initializers
.length
then longest
= spd
207 for spd
in spropdefs
do
209 for p
in spd
.initializers
do
210 if p
!= longest
.initializers
[i
] then
211 self.error
(nclassdef
, "Error: conflict for inherited inits {spd}({spd.initializers.join(", ")}) and {longest}({longest.initializers.join(", ")})")
219 # Can we just inherit?
220 if spropdefs
.length
== 1 and mparameters
.is_empty
and defined_init
== null then
221 self.toolcontext
.info
("{mclassdef} inherits the basic constructor {longest}", 3)
222 mclassdef
.mclass
.root_init
= longest
226 # Combine the inherited list to what is collected
227 if longest
.initializers
.length
> 0 then
228 mparameters
.prepend longest
.new_msignature
.mparameters
229 initializers
.prepend longest
.initializers
232 # If we already have a basic init definition, then setup its initializers
233 if defined_init
!= null then
234 defined_init
.initializers
.add_all
(initializers
)
235 var msignature
= new MSignature(mparameters
, null)
236 defined_init
.new_msignature
= msignature
237 self.toolcontext
.info
("{mclassdef} extends its basic constructor signature to {defined_init}{msignature}", 3)
238 mclassdef
.mclass
.root_init
= defined_init
242 # Else create the local implicit basic init definition
243 var mprop
= the_root_init_mmethod
.as(not null)
244 var mpropdef
= new MMethodDef(mclassdef
, mprop
, nclassdef
.location
)
245 mpropdef
.has_supercall
= true
246 mpropdef
.initializers
.add_all
(initializers
)
247 var msignature
= new MSignature(mparameters
, null)
248 mpropdef
.new_msignature
= msignature
249 mpropdef
.msignature
= new MSignature(new Array[MParameter], null) # always an empty real signature
250 nclassdef
.mfree_init
= mpropdef
251 self.toolcontext
.info
("{mclassdef} gets a free constructor for attributes {mpropdef}{msignature}", 3)
252 mclassdef
.mclass
.root_init
= mpropdef
255 # Check the visibility of `mtype` as an element of the signature of `mpropdef`.
256 fun check_visibility
(node
: ANode, mtype
: MType, mpropdef
: MPropDef)
258 var mmodule
= mpropdef
.mclassdef
.mmodule
259 var mproperty
= mpropdef
.mproperty
261 # Extract visibility information of the main part of `mtype`
262 # It is a case-by case
263 var vis_type
: nullable MVisibility = null # The own visibility of the type
264 var mmodule_type
: nullable MModule = null # The original module of the type
265 mtype
= mtype
.as_notnullable
266 if mtype
isa MClassType then
267 vis_type
= mtype
.mclass
.visibility
268 mmodule_type
= mtype
.mclass
.intro
.mmodule
269 else if mtype
isa MVirtualType then
270 vis_type
= mtype
.mproperty
.visibility
271 mmodule_type
= mtype
.mproperty
.intro_mclassdef
.mmodule
272 else if mtype
isa MParameterType then
273 # nothing, always visible
275 node
.debug
"Unexpected type {mtype}"
279 if vis_type
!= null then
280 assert mmodule_type
!= null
281 var vis_module_type
= mmodule
.visibility_for
(mmodule_type
) # the visibility of the original module
282 if mproperty
.visibility
> vis_type
then
283 error
(node
, "Error: The {mproperty.visibility} property `{mproperty}` cannot contain the {vis_type} type `{mtype}`")
285 else if mproperty
.visibility
> vis_module_type
then
286 error
(node
, "Error: The {mproperty.visibility} property `{mproperty}` cannot contain the type `{mtype}` from the {vis_module_type} module `{mmodule_type}`")
291 # No error, try to go deeper in generic types
292 if node
isa AType then
293 for a
in node
.n_types
do
295 if t
== null then continue # Error, thus skipped
296 check_visibility
(a
, t
, mpropdef
)
298 else if mtype
isa MGenericType then
299 for t
in mtype
.arguments
do check_visibility
(node
, t
, mpropdef
)
305 # Does the MPropDef contains a call to super or a call of a super-constructor?
306 # Subsequent phases of the frontend (esp. typing) set it if required
307 var has_supercall
: Bool = false is writable
310 redef class AClassdef
311 var build_properties_is_done
= false
313 # The free init (implicitely constructed by the class if required)
314 var mfree_init
: nullable MMethodDef = null
318 # The base init of the class.
319 # Used to get the common new_msignature and initializers
321 # TODO: Where to put this information is not clear because unlike other
322 # informations, the initialisers are stable in a same class.
323 var root_init
: nullable MMethodDef = null
326 redef class MClassDef
327 # What is the `APropdef` associated to a `MProperty`?
328 # Used to check multiple definition of a property.
329 var mprop2npropdef
: Map[MProperty, APropdef] = new HashMap[MProperty, APropdef]
331 # Build the virtual type `SELF` only for introduction `MClassDef`
332 fun build_self_type
(modelbuilder
: ModelBuilder, nclassdef
: AClassdef)
334 if not is_intro
then return
337 var mprop
= modelbuilder
.try_get_mproperty_by_name
(nclassdef
, self, name
)
339 # If SELF type is declared nowherer?
340 if mprop
== null then return
342 # SELF is not a virtual type? it is weird but we ignore it
343 if not mprop
isa MVirtualTypeProp then return
345 # Is this the intro of SELF in the library?
346 var intro
= mprop
.intro
347 var intro_mclassdef
= intro
.mclassdef
348 if intro_mclassdef
== self then
349 var nintro
= modelbuilder
.mpropdef2npropdef
[intro
]
351 # SELF must be declared in Object, otherwise this will create conflicts
352 if intro_mclassdef
.mclass
.name
!= "Object" then
353 modelbuilder
.error
(nintro
, "Error: the virtual type SELF must be declared in Object.")
356 # SELF must be public
357 if mprop
.visibility
!= public_visibility
then
358 modelbuilder
.error
(nintro
, "Error: the virtual type SELF must be public.")
361 # SELF must not be fixed
362 if intro
.is_fixed
then
363 modelbuilder
.error
(nintro
, "Error: the virtual type SELF cannot be fixed.")
369 # This class introduction inherits a SELF
370 # We insert an artificial property to update it
371 var mpropdef
= new MVirtualTypeDef(self, mprop
, self.location
)
372 mpropdef
.bound
= mclass
.mclass_type
377 # The associated main model entity
378 type MPROPDEF: MPropDef
380 # The associated propdef once build by a `ModelBuilder`
381 var mpropdef
: nullable MPROPDEF is writable
383 private fun build_property
(modelbuilder
: ModelBuilder, mclassdef
: MClassDef) is abstract
384 private fun build_signature
(modelbuilder
: ModelBuilder) is abstract
385 private fun check_signature
(modelbuilder
: ModelBuilder) is abstract
386 private fun new_property_visibility
(modelbuilder
: ModelBuilder, mclassdef
: MClassDef, nvisibility
: nullable AVisibility): MVisibility
388 var mvisibility
= public_visibility
389 if nvisibility
!= null then
390 mvisibility
= nvisibility
.mvisibility
391 if mvisibility
== intrude_visibility
then
392 modelbuilder
.error
(nvisibility
, "Error: intrude is not a legal visibility for properties.")
393 mvisibility
= public_visibility
396 if mclassdef
.mclass
.visibility
== private_visibility
then
397 if mvisibility
== protected_visibility
then
398 assert nvisibility
!= null
399 modelbuilder
.error
(nvisibility
, "Error: The only legal visibility for properties in a private class is private.")
400 else if mvisibility
== private_visibility
then
401 assert nvisibility
!= null
402 modelbuilder
.advice
(nvisibility
, "useless-visibility", "Warning: private is superfluous since the only legal visibility for properties in a private class is private.")
404 mvisibility
= private_visibility
409 private fun set_doc
(mpropdef
: MPropDef, modelbuilder
: ModelBuilder)
411 var ndoc
= self.n_doc
413 var mdoc
= ndoc
.to_mdoc
415 mdoc
.original_mentity
= mpropdef
416 else if mpropdef
.is_intro
and mpropdef
.mproperty
.visibility
>= protected_visibility
then
417 modelbuilder
.advice
(self, "missing-doc", "Documentation warning: Undocumented property `{mpropdef.mproperty}`")
420 var at_deprecated
= get_single_annotation
("deprecated", modelbuilder
)
421 if at_deprecated
!= null then
422 if not mpropdef
.is_intro
then
423 modelbuilder
.error
(self, "Error: method redefinition cannot be deprecated.")
425 var info
= new MDeprecationInfo
426 ndoc
= at_deprecated
.n_doc
427 if ndoc
!= null then info
.mdoc
= ndoc
.to_mdoc
428 mpropdef
.mproperty
.deprecation
= info
433 private fun check_redef_property_visibility
(modelbuilder
: ModelBuilder, nvisibility
: nullable AVisibility, mprop
: MProperty)
435 if nvisibility
== null then return
436 var mvisibility
= nvisibility
.mvisibility
437 if mvisibility
!= mprop
.visibility
and mvisibility
!= public_visibility
then
438 modelbuilder
.error
(nvisibility
, "Error: redefinition changed the visibility from a {mprop.visibility} to a {mvisibility}")
442 private fun check_redef_keyword
(modelbuilder
: ModelBuilder, mclassdef
: MClassDef, kwredef
: nullable Token, need_redef
: Bool, mprop
: MProperty): Bool
444 if mclassdef
.mprop2npropdef
.has_key
(mprop
) then
445 modelbuilder
.error
(self, "Error: A property {mprop} is already defined in class {mclassdef.mclass} at line {mclassdef.mprop2npropdef[mprop].location.line_start}.")
448 if mprop
isa MMethod and mprop
.is_toplevel
!= (parent
isa ATopClassdef) then
449 if mprop
.is_toplevel
then
450 modelbuilder
.error
(self, "Error: {mprop} is a top level method.")
452 modelbuilder
.error
(self, "Error: {mprop} is not a top level method.")
457 if kwredef
== null then
459 modelbuilder
.error
(self, "Redef error: {mclassdef.mclass}::{mprop.name} is an inherited property. To redefine it, add the redef keyword.")
463 if not need_redef
then
464 modelbuilder
.error
(self, "Error: No property {mclassdef.mclass}::{mprop.name} is inherited. Remove the redef keyword to define a new property.")
473 redef class ASignature
474 # Is the model builder has correctly visited the signature
475 var is_visited
= false
476 # Names of parameters from the AST
477 # REQUIRE: is_visited
478 var param_names
= new Array[String]
479 # Types of parameters from the AST
480 # REQUIRE: is_visited
481 var param_types
= new Array[MType]
482 # Rank of the vararg (of -1 if none)
483 # REQUIRE: is_visited
484 var vararg_rank
: Int = -1
486 var ret_type
: nullable MType = null
488 # Visit and fill information about a signature
489 private fun visit_signature
(modelbuilder
: ModelBuilder, mclassdef
: MClassDef): Bool
491 var mmodule
= mclassdef
.mmodule
492 var param_names
= self.param_names
493 var param_types
= self.param_types
494 for np
in self.n_params
do
495 param_names
.add
(np
.n_id
.text
)
496 var ntype
= np
.n_type
497 if ntype
!= null then
498 var mtype
= modelbuilder
.resolve_mtype
(mmodule
, mclassdef
, ntype
)
499 if mtype
== null then return false # Skip error
500 for i
in [0..param_names
.length-param_types
.length
[ do
501 param_types
.add
(mtype
)
503 if np
.n_dotdotdot
!= null then
504 if self.vararg_rank
!= -1 then
505 modelbuilder
.error
(np
, "Error: {param_names[self.vararg_rank]} is already a vararg")
508 self.vararg_rank
= param_names
.length
- 1
513 var ntype
= self.n_type
514 if ntype
!= null then
515 self.ret_type
= modelbuilder
.resolve_mtype
(mmodule
, mclassdef
, ntype
)
516 if self.ret_type
== null then return false # Skip error
519 self.is_visited
= true
523 # Build a visited signature
524 fun build_signature
(modelbuilder
: ModelBuilder): nullable MSignature
526 if param_names
.length
!= param_types
.length
then
527 # Some parameters are typed, other parameters are not typed.
528 modelbuilder
.error
(self.n_params
[param_types
.length
], "Error: Untyped parameter `{param_names[param_types.length]}'.")
532 var mparameters
= new Array[MParameter]
533 for i
in [0..param_names
.length
[ do
534 var mparameter
= new MParameter(param_names
[i
], param_types
[i
], i
== vararg_rank
)
535 self.n_params
[i
].mparameter
= mparameter
536 mparameters
.add
(mparameter
)
539 var msignature
= new MSignature(mparameters
, ret_type
)
545 # The associated mparameter if any
546 var mparameter
: nullable MParameter = null
549 redef class AMethPropdef
550 redef type MPROPDEF: MMethodDef
553 # Can self be used as a root init?
554 private fun look_like_a_root_init
(modelbuilder
: ModelBuilder, mclassdef
: MClassDef): Bool
556 # Need the `init` keyword
557 if n_kwinit
== null then return false
558 # Need to by anonymous
559 if self.n_methid
!= null then return false
560 # No annotation on itself
561 if get_single_annotation
("old_style_init", modelbuilder
) != null then return false
563 var amod
= self.parent
.parent
.as(AModule)
564 var amoddecl
= amod
.n_moduledecl
565 if amoddecl
!= null then
566 var old
= amoddecl
.get_single_annotation
("old_style_init", modelbuilder
)
567 if old
!= null then return false
570 if self.n_signature
.n_params
.length
> 0 then
571 modelbuilder
.advice
(self, "old-init", "Warning: init with signature in {mclassdef}")
574 # Cannot be private or something
575 if not self.n_visibility
isa APublicVisibility then
576 modelbuilder
.advice
(self, "old-init", "Warning: non-public init in {mclassdef}")
583 redef fun build_property
(modelbuilder
, mclassdef
)
585 var n_kwinit
= n_kwinit
586 var n_kwnew
= n_kwnew
587 var is_init
= n_kwinit
!= null or n_kwnew
!= null
589 var amethodid
= self.n_methid
591 if amethodid
== null then
595 else if n_kwinit
!= null then
598 else if n_kwnew
!= null then
604 else if amethodid
isa AIdMethid then
605 name
= amethodid
.n_id
.text
606 name_node
= amethodid
608 # operator, bracket or assign
609 name
= amethodid
.collect_text
610 name_node
= amethodid
612 if name
== "-" and self.n_signature
.n_params
.length
== 0 then
617 var look_like_a_root_init
= look_like_a_root_init
(modelbuilder
, mclassdef
)
618 var mprop
: nullable MMethod = null
619 if not is_init
or n_kwredef
!= null then mprop
= modelbuilder
.try_get_mproperty_by_name
(name_node
, mclassdef
, name
).as(nullable MMethod)
620 if mprop
== null and look_like_a_root_init
then
621 mprop
= modelbuilder
.the_root_init_mmethod
623 if nb
isa ABlockExpr and nb
.n_expr
.is_empty
and n_doc
== null then
624 modelbuilder
.advice
(self, "useless-init", "Warning: useless empty init in {mclassdef}")
627 if mprop
== null then
628 var mvisibility
= new_property_visibility
(modelbuilder
, mclassdef
, self.n_visibility
)
629 mprop
= new MMethod(mclassdef
, name
, mvisibility
)
630 if look_like_a_root_init
and modelbuilder
.the_root_init_mmethod
== null then
631 modelbuilder
.the_root_init_mmethod
= mprop
632 mprop
.is_root_init
= true
634 mprop
.is_init
= is_init
635 mprop
.is_new
= n_kwnew
!= null
636 if parent
isa ATopClassdef then mprop
.is_toplevel
= true
637 self.check_redef_keyword
(modelbuilder
, mclassdef
, n_kwredef
, false, mprop
)
639 if not mprop
.is_root_init
and not self.check_redef_keyword
(modelbuilder
, mclassdef
, n_kwredef
, not self isa AMainMethPropdef, mprop
) then return
640 check_redef_property_visibility
(modelbuilder
, self.n_visibility
, mprop
)
642 mclassdef
.mprop2npropdef
[mprop
] = self
644 var mpropdef
= new MMethodDef(mclassdef
, mprop
, self.location
)
646 set_doc
(mpropdef
, modelbuilder
)
648 self.mpropdef
= mpropdef
649 modelbuilder
.mpropdef2npropdef
[mpropdef
] = self
650 if mpropdef
.is_intro
then
651 modelbuilder
.toolcontext
.info
("{mpropdef} introduces new method {mprop.full_name}", 4)
653 modelbuilder
.toolcontext
.info
("{mpropdef} redefines method {mprop.full_name}", 4)
657 redef fun build_signature
(modelbuilder
)
659 var mpropdef
= self.mpropdef
660 if mpropdef
== null then return # Error thus skiped
661 var mclassdef
= mpropdef
.mclassdef
662 var mmodule
= mclassdef
.mmodule
663 var nsig
= self.n_signature
665 if mpropdef
.mproperty
.is_root_init
and not mclassdef
.is_intro
then
666 var root_init
= mclassdef
.mclass
.root_init
667 if root_init
!= null then
668 # Inherit the initializers by refinement
669 mpropdef
.new_msignature
= root_init
.new_msignature
670 assert mpropdef
.initializers
.is_empty
671 mpropdef
.initializers
.add_all root_init
.initializers
675 # Retrieve info from the signature AST
676 var param_names
= new Array[String] # Names of parameters from the AST
677 var param_types
= new Array[MType] # Types of parameters from the AST
679 var ret_type
: nullable MType = null # Return type from the AST
681 if not nsig
.visit_signature
(modelbuilder
, mclassdef
) then return
682 param_names
= nsig
.param_names
683 param_types
= nsig
.param_types
684 vararg_rank
= nsig
.vararg_rank
685 ret_type
= nsig
.ret_type
688 # Look for some signature to inherit
689 # FIXME: do not inherit from the intro, but from the most specific
690 var msignature
: nullable MSignature = null
691 if not mpropdef
.is_intro
then
692 msignature
= mpropdef
.mproperty
.intro
.msignature
693 if msignature
== null then return # Skip error
695 # Check inherited signature arity
696 if param_names
.length
!= msignature
.arity
then
698 if nsig
!= null then node
= nsig
else node
= self
699 modelbuilder
.error
(node
, "Redef error: {mpropdef} redefines {mpropdef.mproperty.intro} with {param_names.length} parameter(s), {msignature.arity} expected. Signature is {mpropdef}{msignature}")
702 else if mpropdef
.mproperty
.is_init
and not mpropdef
.mproperty
.is_new
then
703 # FIXME UGLY: inherit signature from a super-constructor
704 for msupertype
in mclassdef
.supertypes
do
705 msupertype
= msupertype
.anchor_to
(mmodule
, mclassdef
.bound_mtype
)
706 var candidate
= modelbuilder
.try_get_mproperty_by_name2
(self, mmodule
, msupertype
, mpropdef
.mproperty
.name
)
707 if candidate
!= null then
708 if msignature
== null then
709 msignature
= candidate
.intro
.as(MMethodDef).msignature
716 # Inherit the signature
717 if msignature
!= null and param_names
.length
!= param_types
.length
and param_names
.length
== msignature
.arity
and param_types
.length
== 0 then
718 # Parameters are untyped, thus inherit them
719 param_types
= new Array[MType]
720 for mparameter
in msignature
.mparameters
do
721 param_types
.add
(mparameter
.mtype
)
723 vararg_rank
= msignature
.vararg_rank
725 if msignature
!= null and ret_type
== null then
726 ret_type
= msignature
.return_mtype
729 if param_names
.length
!= param_types
.length
then
730 # Some parameters are typed, other parameters are not typed.
731 modelbuilder
.error
(nsig
.n_params
[param_types
.length
], "Error: Untyped parameter `{param_names[param_types.length]}'.")
735 var mparameters
= new Array[MParameter]
736 for i
in [0..param_names
.length
[ do
737 var mparameter
= new MParameter(param_names
[i
], param_types
[i
], i
== vararg_rank
)
738 if nsig
!= null then nsig
.n_params
[i
].mparameter
= mparameter
739 mparameters
.add
(mparameter
)
742 # In `new`-factories, the return type is by default the classtype.
743 if ret_type
== null and mpropdef
.mproperty
.is_new
then ret_type
= mclassdef
.mclass
.mclass_type
745 msignature
= new MSignature(mparameters
, ret_type
)
746 mpropdef
.msignature
= msignature
747 mpropdef
.is_abstract
= self.get_single_annotation
("abstract", modelbuilder
) != null
748 mpropdef
.is_intern
= self.get_single_annotation
("intern", modelbuilder
) != null
749 mpropdef
.is_extern
= self.n_extern_code_block
!= null or self.get_single_annotation
("extern", modelbuilder
) != null
752 redef fun check_signature
(modelbuilder
)
754 var mpropdef
= self.mpropdef
755 if mpropdef
== null then return # Error thus skiped
756 var mclassdef
= mpropdef
.mclassdef
757 var mmodule
= mclassdef
.mmodule
758 var nsig
= self.n_signature
759 var mysignature
= self.mpropdef
.msignature
760 if mysignature
== null then return # Error thus skiped
762 # Lookup for signature in the precursor
763 # FIXME all precursors should be considered
764 if not mpropdef
.is_intro
then
765 var msignature
= mpropdef
.mproperty
.intro
.msignature
766 if msignature
== null then return
768 var precursor_ret_type
= msignature
.return_mtype
769 var ret_type
= mysignature
.return_mtype
770 if ret_type
!= null and precursor_ret_type
== null then
771 modelbuilder
.error
(nsig
.n_type
.as(not null), "Redef Error: {mpropdef.mproperty} is a procedure, not a function.")
775 if mysignature
.arity
> 0 then
776 # Check parameters types
777 for i
in [0..mysignature
.arity
[ do
778 var myt
= mysignature
.mparameters
[i
].mtype
779 var prt
= msignature
.mparameters
[i
].mtype
780 if not myt
.is_subtype
(mmodule
, mclassdef
.bound_mtype
, prt
) or
781 not prt
.is_subtype
(mmodule
, mclassdef
.bound_mtype
, myt
) then
782 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}.")
786 if precursor_ret_type
!= null then
787 if ret_type
== null then
788 # Inherit the return type
789 ret_type
= precursor_ret_type
790 else if not ret_type
.is_subtype
(mmodule
, mclassdef
.bound_mtype
, precursor_ret_type
) then
791 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}.")
796 if mysignature
.arity
> 0 then
797 # Check parameters visibility
798 for i
in [0..mysignature
.arity
[ do
799 var nt
= nsig
.n_params
[i
].n_type
800 if nt
!= null then modelbuilder
.check_visibility
(nt
, nt
.mtype
.as(not null), mpropdef
)
803 if nt
!= null then modelbuilder
.check_visibility
(nt
, nt
.mtype
.as(not null), mpropdef
)
808 redef class AAttrPropdef
809 redef type MPROPDEF: MAttributeDef
811 # Is the node tagged `noinit`?
814 # Is the node tagged lazy?
817 # Has the node a default value?
818 # Could be through `n_expr` or `n_block`
819 var has_value
= false
821 # The guard associated to a lazy attribute.
822 # Because some engines does not have a working `isset`,
823 # this additional attribute is used to guard the lazy initialization.
824 # TODO: to remove once isset is correctly implemented
825 var mlazypropdef
: nullable MAttributeDef
827 # The associated getter (read accessor) if any
828 var mreadpropdef
: nullable MMethodDef is writable
829 # The associated setter (write accessor) if any
830 var mwritepropdef
: nullable MMethodDef is writable
832 redef fun build_property
(modelbuilder
, mclassdef
)
834 var mclass
= mclassdef
.mclass
837 name
= self.n_id2
.text
839 if mclass
.kind
== interface_kind
or mclassdef
.mclass
.kind
== enum_kind
then
840 modelbuilder
.error
(self, "Error: Attempt to define attribute {name} in the interface {mclass}.")
841 else if mclass
.kind
== enum_kind
then
842 modelbuilder
.error
(self, "Error: Attempt to define attribute {name} in the enum class {mclass}.")
843 else if mclass
.kind
== extern_kind
then
844 modelbuilder
.error
(self, "Error: Attempt to define attribute {name} in the extern class {mclass}.")
847 # New attribute style
848 var nid2
= self.n_id2
849 var mprop
= new MAttribute(mclassdef
, "_" + name
, private_visibility
)
850 var mpropdef
= new MAttributeDef(mclassdef
, mprop
, self.location
)
851 self.mpropdef
= mpropdef
852 modelbuilder
.mpropdef2npropdef
[mpropdef
] = self
855 var mreadprop
= modelbuilder
.try_get_mproperty_by_name
(nid2
, mclassdef
, readname
).as(nullable MMethod)
856 if mreadprop
== null then
857 var mvisibility
= new_property_visibility
(modelbuilder
, mclassdef
, self.n_visibility
)
858 mreadprop
= new MMethod(mclassdef
, readname
, mvisibility
)
859 if not self.check_redef_keyword
(modelbuilder
, mclassdef
, n_kwredef
, false, mreadprop
) then return
860 mreadprop
.deprecation
= mprop
.deprecation
862 if not self.check_redef_keyword
(modelbuilder
, mclassdef
, n_kwredef
, true, mreadprop
) then return
863 check_redef_property_visibility
(modelbuilder
, self.n_visibility
, mreadprop
)
865 mclassdef
.mprop2npropdef
[mreadprop
] = self
867 var mreadpropdef
= new MMethodDef(mclassdef
, mreadprop
, self.location
)
868 self.mreadpropdef
= mreadpropdef
869 modelbuilder
.mpropdef2npropdef
[mreadpropdef
] = self
870 set_doc
(mreadpropdef
, modelbuilder
)
871 mpropdef
.mdoc
= mreadpropdef
.mdoc
873 has_value
= n_expr
!= null or n_block
!= null
875 var atnoinit
= self.get_single_annotation
("noinit", modelbuilder
)
876 if atnoinit
!= null then
879 modelbuilder
.error
(atnoinit
, "Error: `noinit` attributes cannot have an initial value")
884 var atlazy
= self.get_single_annotation
("lazy", modelbuilder
)
885 var atautoinit
= self.get_single_annotation
("autoinit", modelbuilder
)
886 if atlazy
!= null or atautoinit
!= null then
887 if atlazy
!= null and atautoinit
!= null then
888 modelbuilder
.error
(atlazy
, "Error: lazy incompatible with autoinit")
891 if not has_value
then
892 if atlazy
!= null then
893 modelbuilder
.error
(atlazy
, "Error: a lazy attribute needs a value")
894 else if atautoinit
!= null then
895 modelbuilder
.error
(atautoinit
, "Error: a autoinit attribute needs a value")
900 var mlazyprop
= new MAttribute(mclassdef
, "lazy _" + name
, none_visibility
)
901 var mlazypropdef
= new MAttributeDef(mclassdef
, mlazyprop
, self.location
)
902 self.mlazypropdef
= mlazypropdef
905 var atreadonly
= self.get_single_annotation
("readonly", modelbuilder
)
906 if atreadonly
!= null then
907 if not has_value
then
908 modelbuilder
.error
(atreadonly
, "Error: a readonly attribute needs a value")
910 # No setter, so just leave
914 var writename
= name
+ "="
915 var atwritable
= self.get_single_annotation
("writable", modelbuilder
)
916 if atwritable
!= null then
917 if not atwritable
.n_args
.is_empty
then
918 writename
= atwritable
.arg_as_id
(modelbuilder
) or else writename
921 var mwriteprop
= modelbuilder
.try_get_mproperty_by_name
(nid2
, mclassdef
, writename
).as(nullable MMethod)
922 var nwkwredef
: nullable Token = null
923 if atwritable
!= null then nwkwredef
= atwritable
.n_kwredef
924 if mwriteprop
== null then
926 if atwritable
!= null then
927 mvisibility
= new_property_visibility
(modelbuilder
, mclassdef
, atwritable
.n_visibility
)
929 mvisibility
= private_visibility
931 mwriteprop
= new MMethod(mclassdef
, writename
, mvisibility
)
932 if not self.check_redef_keyword
(modelbuilder
, mclassdef
, nwkwredef
, false, mwriteprop
) then return
933 mwriteprop
.deprecation
= mprop
.deprecation
935 if not self.check_redef_keyword
(modelbuilder
, mclassdef
, nwkwredef
or else n_kwredef
, true, mwriteprop
) then return
936 if atwritable
!= null then
937 check_redef_property_visibility
(modelbuilder
, atwritable
.n_visibility
, mwriteprop
)
940 mclassdef
.mprop2npropdef
[mwriteprop
] = self
942 var mwritepropdef
= new MMethodDef(mclassdef
, mwriteprop
, self.location
)
943 self.mwritepropdef
= mwritepropdef
944 modelbuilder
.mpropdef2npropdef
[mwritepropdef
] = self
945 mwritepropdef
.mdoc
= mpropdef
.mdoc
948 redef fun build_signature
(modelbuilder
)
950 var mpropdef
= self.mpropdef
951 if mpropdef
== null then return # Error thus skipped
952 var mclassdef
= mpropdef
.mclassdef
953 var mmodule
= mclassdef
.mmodule
954 var mtype
: nullable MType = null
956 var mreadpropdef
= self.mreadpropdef
958 var ntype
= self.n_type
959 if ntype
!= null then
960 mtype
= modelbuilder
.resolve_mtype
(mmodule
, mclassdef
, ntype
)
961 if mtype
== null then return
964 var inherited_type
: nullable MType = null
965 # Inherit the type from the getter (usually an abstract getter)
966 if mreadpropdef
!= null and not mreadpropdef
.is_intro
then
967 var msignature
= mreadpropdef
.mproperty
.intro
.msignature
968 if msignature
== null then return # Error, thus skipped
969 inherited_type
= msignature
.return_mtype
970 if mtype
== null then mtype
= inherited_type
973 var nexpr
= self.n_expr
974 if mtype
== null then
975 if nexpr
!= null then
976 if nexpr
isa ANewExpr then
977 mtype
= modelbuilder
.resolve_mtype
(mmodule
, mclassdef
, nexpr
.n_type
)
978 else if nexpr
isa AIntExpr then
979 var cla
= modelbuilder
.try_get_mclass_by_name
(nexpr
, mmodule
, "Int")
980 if cla
!= null then mtype
= cla
.mclass_type
981 else if nexpr
isa AFloatExpr then
982 var cla
= modelbuilder
.try_get_mclass_by_name
(nexpr
, mmodule
, "Float")
983 if cla
!= null then mtype
= cla
.mclass_type
984 else if nexpr
isa ACharExpr then
985 var cla
= modelbuilder
.try_get_mclass_by_name
(nexpr
, mmodule
, "Char")
986 if cla
!= null then mtype
= cla
.mclass_type
987 else if nexpr
isa ABoolExpr then
988 var cla
= modelbuilder
.try_get_mclass_by_name
(nexpr
, mmodule
, "Bool")
989 if cla
!= null then mtype
= cla
.mclass_type
990 else if nexpr
isa ASuperstringExpr then
991 var cla
= modelbuilder
.try_get_mclass_by_name
(nexpr
, mmodule
, "String")
992 if cla
!= null then mtype
= cla
.mclass_type
993 else if nexpr
isa AStringFormExpr then
994 var cla
= modelbuilder
.try_get_mclass_by_name
(nexpr
, mmodule
, "String")
995 if cla
!= null then mtype
= cla
.mclass_type
997 modelbuilder
.error
(self, "Error: Untyped attribute {mpropdef}. Implicit typing allowed only for literals and new.")
1000 if mtype
== null then return
1002 else if ntype
!= null and inherited_type
== mtype
then
1003 if nexpr
isa ANewExpr then
1004 var xmtype
= modelbuilder
.resolve_mtype
(mmodule
, mclassdef
, nexpr
.n_type
)
1005 if xmtype
== mtype
then
1006 modelbuilder
.advice
(ntype
, "useless-type", "Warning: useless type definition")
1011 if mtype
== null then
1012 modelbuilder
.error
(self, "Error: Untyped attribute {mpropdef}")
1016 mpropdef
.static_mtype
= mtype
1018 if mreadpropdef
!= null then
1019 var msignature
= new MSignature(new Array[MParameter], mtype
)
1020 mreadpropdef
.msignature
= msignature
1023 var mwritepropdef
= self.mwritepropdef
1024 if mwritepropdef
!= null then
1027 var mparameter
= new MParameter(name
, mtype
, false)
1028 var msignature
= new MSignature([mparameter
], null)
1029 mwritepropdef
.msignature
= msignature
1032 var mlazypropdef
= self.mlazypropdef
1033 if mlazypropdef
!= null then
1034 mlazypropdef
.static_mtype
= modelbuilder
.model
.get_mclasses_by_name
("Bool").first
.mclass_type
1038 redef fun check_signature
(modelbuilder
)
1040 var mpropdef
= self.mpropdef
1041 if mpropdef
== null then return # Error thus skipped
1042 var ntype
= self.n_type
1043 var mtype
= self.mpropdef
.static_mtype
1044 if mtype
== null then return # Error thus skipped
1046 # Lookup for signature in the precursor
1047 # FIXME all precursors should be considered
1048 if not mpropdef
.is_intro
then
1049 var precursor_type
= mpropdef
.mproperty
.intro
.static_mtype
1050 if precursor_type
== null then return
1052 if mtype
!= precursor_type
then
1053 modelbuilder
.error
(ntype
.as(not null), "Redef Error: Wrong static type. found {mtype}, expected {precursor_type}.")
1058 # Check getter and setter
1059 var meth
= self.mreadpropdef
1060 if meth
!= null then
1061 self.check_method_signature
(modelbuilder
, meth
)
1062 var node
: nullable ANode = ntype
1063 if node
== null then node
= self
1064 modelbuilder
.check_visibility
(node
, mtype
, meth
)
1066 meth
= self.mwritepropdef
1067 if meth
!= null then
1068 self.check_method_signature
(modelbuilder
, meth
)
1069 var node
: nullable ANode = ntype
1070 if node
== null then node
= self
1071 modelbuilder
.check_visibility
(node
, mtype
, meth
)
1075 private fun check_method_signature
(modelbuilder
: ModelBuilder, mpropdef
: MMethodDef)
1077 var mclassdef
= mpropdef
.mclassdef
1078 var mmodule
= mclassdef
.mmodule
1079 var nsig
= self.n_type
1080 var mysignature
= mpropdef
.msignature
1081 if mysignature
== null then return # Error thus skiped
1083 # Lookup for signature in the precursor
1084 # FIXME all precursors should be considered
1085 if not mpropdef
.is_intro
then
1086 var msignature
= mpropdef
.mproperty
.intro
.msignature
1087 if msignature
== null then return
1089 if mysignature
.arity
!= msignature
.arity
then
1091 if nsig
!= null then node
= nsig
else node
= self
1092 modelbuilder
.error
(node
, "Redef Error: {mysignature.arity} parameters found, {msignature.arity} expected. Signature is {mpropdef}{msignature}")
1095 var precursor_ret_type
= msignature
.return_mtype
1096 var ret_type
= mysignature
.return_mtype
1097 if ret_type
!= null and precursor_ret_type
== null then
1099 if nsig
!= null then node
= nsig
else node
= self
1100 modelbuilder
.error
(node
, "Redef Error: {mpropdef.mproperty} is a procedure, not a function.")
1104 if mysignature
.arity
> 0 then
1105 # Check parameters types
1106 for i
in [0..mysignature
.arity
[ do
1107 var myt
= mysignature
.mparameters
[i
].mtype
1108 var prt
= msignature
.mparameters
[i
].mtype
1109 if not myt
.is_subtype
(mmodule
, mclassdef
.bound_mtype
, prt
) or
1110 not prt
.is_subtype
(mmodule
, mclassdef
.bound_mtype
, myt
) then
1112 if nsig
!= null then node
= nsig
else node
= self
1113 modelbuilder
.error
(node
, "Redef Error: Wrong type for parameter `{mysignature.mparameters[i].name}'. found {myt}, expected {prt}.")
1117 if precursor_ret_type
!= null then
1118 if ret_type
== null then
1119 # Inherit the return type
1120 ret_type
= precursor_ret_type
1121 else if not ret_type
.is_subtype
(mmodule
, mclassdef
.bound_mtype
, precursor_ret_type
) then
1123 if nsig
!= null then node
= nsig
else node
= self
1124 modelbuilder
.error
(node
, "Redef Error: Wrong return type. found {ret_type}, expected {precursor_ret_type}.")
1131 redef class ATypePropdef
1132 redef type MPROPDEF: MVirtualTypeDef
1134 redef fun build_property
(modelbuilder
, mclassdef
)
1136 var name
= self.n_id
.text
1137 var mprop
= modelbuilder
.try_get_mproperty_by_name
(self.n_id
, mclassdef
, name
)
1138 if mprop
== null then
1139 var mvisibility
= new_property_visibility
(modelbuilder
, mclassdef
, self.n_visibility
)
1140 mprop
= new MVirtualTypeProp(mclassdef
, name
, mvisibility
)
1141 for c
in name
.chars
do if c
>= 'a' and c
<= 'z' then
1142 modelbuilder
.warning
(n_id
, "bad-type-name", "Warning: lowercase in the virtual type {name}")
1145 if not self.check_redef_keyword
(modelbuilder
, mclassdef
, self.n_kwredef
, false, mprop
) then return
1147 if not self.check_redef_keyword
(modelbuilder
, mclassdef
, self.n_kwredef
, true, mprop
) then return
1148 assert mprop
isa MVirtualTypeProp
1149 check_redef_property_visibility
(modelbuilder
, self.n_visibility
, mprop
)
1151 mclassdef
.mprop2npropdef
[mprop
] = self
1153 var mpropdef
= new MVirtualTypeDef(mclassdef
, mprop
, self.location
)
1154 self.mpropdef
= mpropdef
1155 modelbuilder
.mpropdef2npropdef
[mpropdef
] = self
1156 set_doc
(mpropdef
, modelbuilder
)
1158 var atfixed
= get_single_annotation
("fixed", modelbuilder
)
1159 if atfixed
!= null then
1160 mpropdef
.is_fixed
= true
1164 redef fun build_signature
(modelbuilder
)
1166 var mpropdef
= self.mpropdef
1167 if mpropdef
== null then return # Error thus skipped
1168 var mclassdef
= mpropdef
.mclassdef
1169 var mmodule
= mclassdef
.mmodule
1170 var mtype
: nullable MType = null
1172 var ntype
= self.n_type
1173 mtype
= modelbuilder
.resolve_mtype
(mmodule
, mclassdef
, ntype
)
1174 if mtype
== null then return
1176 mpropdef
.bound
= mtype
1177 # print "{mpropdef}: {mtype}"
1180 redef fun check_signature
(modelbuilder
)
1182 var mpropdef
= self.mpropdef
1183 if mpropdef
== null then return # Error thus skipped
1185 var bound
= self.mpropdef
.bound
1186 if bound
== null then return # Error thus skipped
1188 modelbuilder
.check_visibility
(n_type
, bound
, mpropdef
)
1190 var mclassdef
= mpropdef
.mclassdef
1191 var mmodule
= mclassdef
.mmodule
1192 var anchor
= mclassdef
.bound_mtype
1195 if bound
isa MVirtualType then
1196 # Slow case: progress on each resolution until: (i) we loop, or (ii) we found a non formal type
1197 var seen
= [self.mpropdef
.mproperty
.mvirtualtype
]
1199 if seen
.has
(bound
) then
1201 modelbuilder
.error
(self, "Error: circularity of virtual type definition: {seen.join(" -> ")}")
1205 var next
= bound
.lookup_bound
(mmodule
, anchor
)
1206 if not next
isa MVirtualType then break
1211 # Check redefinitions
1212 bound
= mpropdef
.bound
.as(not null)
1213 for p
in mpropdef
.mproperty
.lookup_super_definitions
(mmodule
, anchor
) do
1214 var supbound
= p
.bound
1215 if supbound
== null then break # broken super bound, skip error
1217 modelbuilder
.error
(self, "Redef Error: Virtual type {mpropdef.mproperty} is fixed in super-class {p.mclassdef.mclass}")
1220 if p
.mclassdef
.mclass
== mclassdef
.mclass
then
1221 # Still a warning to pass existing bad code
1222 modelbuilder
.warning
(n_type
, "refine-type", "Redef Error: a virtual type cannot be refined.")
1225 if not bound
.is_subtype
(mmodule
, anchor
, supbound
) then
1226 modelbuilder
.error
(n_type
, "Redef Error: Wrong bound type. Found {bound}, expected a subtype of {supbound}, as in {p}.")