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 # Build the properties of `nclassdef`.
44 # REQUIRE: all superclasses are built.
45 private fun build_properties
(nclassdef
: AClassdef)
47 # Force building recursively
48 if nclassdef
.build_properties_is_done
then return
49 nclassdef
.build_properties_is_done
= true
50 var mclassdef
= nclassdef
.mclassdef
.as(not null)
51 if mclassdef
.in_hierarchy
== null then return # Skip error
52 for superclassdef
in mclassdef
.in_hierarchy
.direct_greaters
do
53 if not mclassdef2nclassdef
.has_key
(superclassdef
) then continue
54 build_properties
(mclassdef2nclassdef
[superclassdef
])
57 for nclassdef2
in nclassdef
.all_defs
do
58 for npropdef
in nclassdef2
.n_propdefs
do
59 npropdef
.build_property
(self, mclassdef
)
61 for npropdef
in nclassdef2
.n_propdefs
do
62 npropdef
.build_signature
(self)
64 for npropdef
in nclassdef2
.n_propdefs
do
65 npropdef
.check_signature
(self)
68 process_default_constructors
(nclassdef
)
71 # the root init of the Object class
72 # Is usually implicitly defined
73 # Then explicit or implicit definitions of root-init are attached to it
74 var the_root_init_mmethod
: nullable MMethod
76 # Introduce or inherit default constructor
77 # This is the last part of `build_properties`.
78 private fun process_default_constructors
(nclassdef
: AClassdef)
80 var mclassdef
= nclassdef
.mclassdef
.as(not null)
83 if not mclassdef
.is_intro
then return
85 # Look for the init in Object, or create it
86 if mclassdef
.mclass
.name
== "Object" and the_root_init_mmethod
== null then
87 # Create the implicit root-init method
88 var mprop
= new MMethod(mclassdef
, "init", mclassdef
.mclass
.visibility
)
89 mprop
.is_root_init
= true
90 var mpropdef
= new MMethodDef(mclassdef
, mprop
, nclassdef
.location
)
91 var mparameters
= new Array[MParameter]
92 var msignature
= new MSignature(mparameters
, null)
93 mpropdef
.msignature
= msignature
94 mpropdef
.new_msignature
= msignature
96 nclassdef
.mfree_init
= mpropdef
97 self.toolcontext
.info
("{mclassdef} gets a free empty constructor {mpropdef}{msignature}", 3)
98 the_root_init_mmethod
= mprop
102 # Is the class forbid constructors?
103 if not mclassdef
.mclass
.kind
.need_init
then return
105 # Is there already a constructor defined?
106 var defined_init
: nullable MMethodDef = null
107 for mpropdef
in mclassdef
.mpropdefs
do
108 if not mpropdef
isa MMethodDef then continue
109 if not mpropdef
.mproperty
.is_init
then continue
110 if mpropdef
.mproperty
.is_root_init
then
111 assert defined_init
== null
112 defined_init
= mpropdef
113 else if mpropdef
.mproperty
.name
== "init" then
114 # An explicit old-style init named "init", so return
119 if not nclassdef
isa AStdClassdef then return
121 # Collect undefined attributes
122 var mparameters
= new Array[MParameter]
123 var initializers
= new Array[MProperty]
124 for npropdef
in nclassdef
.n_propdefs
do
125 if npropdef
isa AMethPropdef then
126 if npropdef
.mpropdef
== null then return # Skip broken attribute
127 var at
= npropdef
.get_single_annotation
("autoinit", self)
128 if at
== null then continue # Skip non tagged init
130 var sig
= npropdef
.mpropdef
.msignature
131 if sig
== null then continue # Skip broken method
133 if not npropdef
.mpropdef
.is_intro
then
134 self.error
(at
, "Error: `autoinit` cannot be set on redefinitions")
138 for param
in sig
.mparameters
do
139 var ret_type
= param
.mtype
140 var mparameter
= new MParameter(param
.name
, ret_type
, false)
141 mparameters
.add
(mparameter
)
143 initializers
.add
(npropdef
.mpropdef
.mproperty
)
145 if npropdef
isa AAttrPropdef then
146 if npropdef
.mpropdef
== null then return # Skip broken attribute
147 if npropdef
.noinit
then continue # Skip noinit attribute
148 var atautoinit
= npropdef
.get_single_annotation
("autoinit", self)
149 if atautoinit
!= null then
150 # For autoinit attributes, call the reader to force
151 # the lazy initialization of the attribute.
152 initializers
.add
(npropdef
.mreadpropdef
.mproperty
)
155 if npropdef
.has_value
then continue
156 var paramname
= npropdef
.mpropdef
.mproperty
.name
.substring_from
(1)
157 var ret_type
= npropdef
.mpropdef
.static_mtype
158 if ret_type
== null then return
159 var mparameter
= new MParameter(paramname
, ret_type
, false)
160 mparameters
.add
(mparameter
)
161 var msetter
= npropdef
.mwritepropdef
162 if msetter
== null then
163 # No setter, it is a old-style attribute, so just add it
164 initializers
.add
(npropdef
.mpropdef
.mproperty
)
166 # Add the setter to the list
167 initializers
.add
(msetter
.mproperty
)
172 if the_root_init_mmethod
== null then return
174 # Look for most-specific new-stype init definitions
175 var spropdefs
= the_root_init_mmethod
.lookup_super_definitions
(mclassdef
.mmodule
, mclassdef
.bound_mtype
)
176 if spropdefs
.is_empty
then
177 toolcontext
.fatal_error
(nclassdef
.location
, "Fatal error: {mclassdef} does not specialize {the_root_init_mmethod.intro_mclassdef}. Possible duplication of the root class `Object`?")
180 # Search the longest-one and checks for conflict
181 var longest
= spropdefs
.first
182 if spropdefs
.length
> 1 then
183 # Check for conflict in the order of initializers
184 # Each initializer list must me a prefix of the longest list
185 # part 1. find the longest list
186 for spd
in spropdefs
do
187 if spd
.initializers
.length
> longest
.initializers
.length
then longest
= spd
190 for spd
in spropdefs
do
192 for p
in spd
.initializers
do
193 if p
!= longest
.initializers
[i
] then
194 self.error
(nclassdef
, "Error: conflict for inherited inits {spd}({spd.initializers.join(", ")}) and {longest}({longest.initializers.join(", ")})")
202 # Can we just inherit?
203 if spropdefs
.length
== 1 and mparameters
.is_empty
and defined_init
== null then
204 self.toolcontext
.info
("{mclassdef} inherits the basic constructor {longest}", 3)
205 mclassdef
.mclass
.root_init
= longest
209 # Combine the inherited list to what is collected
210 if longest
.initializers
.length
> 0 then
211 mparameters
.prepend longest
.new_msignature
.mparameters
212 initializers
.prepend longest
.initializers
215 # If we already have a basic init definition, then setup its initializers
216 if defined_init
!= null then
217 defined_init
.initializers
.add_all
(initializers
)
218 var msignature
= new MSignature(mparameters
, null)
219 defined_init
.new_msignature
= msignature
220 self.toolcontext
.info
("{mclassdef} extends its basic constructor signature to {defined_init}{msignature}", 3)
221 mclassdef
.mclass
.root_init
= defined_init
225 # Else create the local implicit basic init definition
226 var mprop
= the_root_init_mmethod
.as(not null)
227 var mpropdef
= new MMethodDef(mclassdef
, mprop
, nclassdef
.location
)
228 mpropdef
.has_supercall
= true
229 mpropdef
.initializers
.add_all
(initializers
)
230 var msignature
= new MSignature(mparameters
, null)
231 mpropdef
.new_msignature
= msignature
232 mpropdef
.msignature
= new MSignature(new Array[MParameter], null) # always an empty real signature
233 nclassdef
.mfree_init
= mpropdef
234 self.toolcontext
.info
("{mclassdef} gets a free constructor for attributes {mpropdef}{msignature}", 3)
235 mclassdef
.mclass
.root_init
= mpropdef
238 # Check the visibility of `mtype` as an element of the signature of `mpropdef`.
239 fun check_visibility
(node
: ANode, mtype
: MType, mpropdef
: MPropDef)
241 var mmodule
= mpropdef
.mclassdef
.mmodule
242 var mproperty
= mpropdef
.mproperty
244 # Extract visibility information of the main part of `mtype`
245 # It is a case-by case
246 var vis_type
: nullable MVisibility = null # The own visibility of the type
247 var mmodule_type
: nullable MModule = null # The original module of the type
248 mtype
= mtype
.as_notnullable
249 if mtype
isa MClassType then
250 vis_type
= mtype
.mclass
.visibility
251 mmodule_type
= mtype
.mclass
.intro
.mmodule
252 else if mtype
isa MVirtualType then
253 vis_type
= mtype
.mproperty
.visibility
254 mmodule_type
= mtype
.mproperty
.intro_mclassdef
.mmodule
255 else if mtype
isa MParameterType then
256 # nothing, always visible
258 node
.debug
"Unexpected type {mtype}"
262 if vis_type
!= null then
263 assert mmodule_type
!= null
264 var vis_module_type
= mmodule
.visibility_for
(mmodule_type
) # the visibility of the original module
265 if mproperty
.visibility
> vis_type
then
266 error
(node
, "Error: The {mproperty.visibility} property `{mproperty}` cannot contain the {vis_type} type `{mtype}`")
268 else if mproperty
.visibility
> vis_module_type
then
269 error
(node
, "Error: The {mproperty.visibility} property `{mproperty}` cannot contain the type `{mtype}` from the {vis_module_type} module `{mmodule_type}`")
274 # No error, try to go deeper in generic types
275 if node
isa AType then
276 for a
in node
.n_types
do
278 if t
== null then continue # Error, thus skipped
279 check_visibility
(a
, t
, mpropdef
)
281 else if mtype
isa MGenericType then
282 for t
in mtype
.arguments
do check_visibility
(node
, t
, mpropdef
)
288 # Does the MPropDef contains a call to super or a call of a super-constructor?
289 # Subsequent phases of the frontend (esp. typing) set it if required
290 var has_supercall
: Bool = false is writable
293 redef class AClassdef
294 var build_properties_is_done
= false
296 # The free init (implicitely constructed by the class if required)
297 var mfree_init
: nullable MMethodDef = null
301 # The base init of the class.
302 # Used to get the common new_msignature and initializers
304 # TODO: Where to put this information is not clear because unlike other
305 # informations, the initialisers are stable in a same class.
306 var root_init
: nullable MMethodDef = null
309 redef class MClassDef
310 # What is the `APropdef` associated to a `MProperty`?
311 # Used to check multiple definition of a property.
312 var mprop2npropdef
: Map[MProperty, APropdef] = new HashMap[MProperty, APropdef]
316 # The associated main model entity
317 type MPROPDEF: MPropDef
319 # The associated propdef once build by a `ModelBuilder`
320 var mpropdef
: nullable MPROPDEF is writable
322 private fun build_property
(modelbuilder
: ModelBuilder, mclassdef
: MClassDef) is abstract
323 private fun build_signature
(modelbuilder
: ModelBuilder) is abstract
324 private fun check_signature
(modelbuilder
: ModelBuilder) is abstract
325 private fun new_property_visibility
(modelbuilder
: ModelBuilder, mclassdef
: MClassDef, nvisibility
: nullable AVisibility): MVisibility
327 var mvisibility
= public_visibility
328 if nvisibility
!= null then
329 mvisibility
= nvisibility
.mvisibility
330 if mvisibility
== intrude_visibility
then
331 modelbuilder
.error
(nvisibility
, "Error: intrude is not a legal visibility for properties.")
332 mvisibility
= public_visibility
335 if mclassdef
.mclass
.visibility
== private_visibility
then
336 if mvisibility
== protected_visibility
then
337 assert nvisibility
!= null
338 modelbuilder
.error
(nvisibility
, "Error: The only legal visibility for properties in a private class is private.")
339 else if mvisibility
== private_visibility
then
340 assert nvisibility
!= null
341 modelbuilder
.advice
(nvisibility
, "useless-visibility", "Warning: private is superfluous since the only legal visibility for properties in a private class is private.")
343 mvisibility
= private_visibility
348 private fun set_doc
(mpropdef
: MPropDef, modelbuilder
: ModelBuilder)
350 var ndoc
= self.n_doc
352 var mdoc
= ndoc
.to_mdoc
354 mdoc
.original_mentity
= mpropdef
355 else if mpropdef
.is_intro
and mpropdef
.mproperty
.visibility
>= protected_visibility
then
356 modelbuilder
.advice
(self, "missing-doc", "Documentation warning: Undocumented property `{mpropdef.mproperty}`")
359 var at_deprecated
= get_single_annotation
("deprecated", modelbuilder
)
360 if at_deprecated
!= null then
361 if not mpropdef
.is_intro
then
362 modelbuilder
.error
(self, "Error: method redefinition cannot be deprecated.")
364 var info
= new MDeprecationInfo
365 ndoc
= at_deprecated
.n_doc
366 if ndoc
!= null then info
.mdoc
= ndoc
.to_mdoc
367 mpropdef
.mproperty
.deprecation
= info
372 private fun check_redef_property_visibility
(modelbuilder
: ModelBuilder, nvisibility
: nullable AVisibility, mprop
: MProperty)
374 if nvisibility
== null then return
375 var mvisibility
= nvisibility
.mvisibility
376 if mvisibility
!= mprop
.visibility
and mvisibility
!= public_visibility
then
377 modelbuilder
.error
(nvisibility
, "Error: redefinition changed the visibility from a {mprop.visibility} to a {mvisibility}")
381 private fun check_redef_keyword
(modelbuilder
: ModelBuilder, mclassdef
: MClassDef, kwredef
: nullable Token, need_redef
: Bool, mprop
: MProperty): Bool
383 if mclassdef
.mprop2npropdef
.has_key
(mprop
) then
384 modelbuilder
.error
(self, "Error: A property {mprop} is already defined in class {mclassdef.mclass} at line {mclassdef.mprop2npropdef[mprop].location.line_start}.")
387 if mprop
isa MMethod and mprop
.is_toplevel
!= (parent
isa ATopClassdef) then
388 if mprop
.is_toplevel
then
389 modelbuilder
.error
(self, "Error: {mprop} is a top level method.")
391 modelbuilder
.error
(self, "Error: {mprop} is not a top level method.")
396 if kwredef
== null then
398 modelbuilder
.error
(self, "Redef error: {mclassdef.mclass}::{mprop.name} is an inherited property. To redefine it, add the redef keyword.")
402 if not need_redef
then
403 modelbuilder
.error
(self, "Error: No property {mclassdef.mclass}::{mprop.name} is inherited. Remove the redef keyword to define a new property.")
412 redef class ASignature
413 # Is the model builder has correctly visited the signature
414 var is_visited
= false
415 # Names of parameters from the AST
416 # REQUIRE: is_visited
417 var param_names
= new Array[String]
418 # Types of parameters from the AST
419 # REQUIRE: is_visited
420 var param_types
= new Array[MType]
421 # Rank of the vararg (of -1 if none)
422 # REQUIRE: is_visited
423 var vararg_rank
: Int = -1
425 var ret_type
: nullable MType = null
427 # Visit and fill information about a signature
428 private fun visit_signature
(modelbuilder
: ModelBuilder, mclassdef
: MClassDef): Bool
430 var mmodule
= mclassdef
.mmodule
431 var param_names
= self.param_names
432 var param_types
= self.param_types
433 for np
in self.n_params
do
434 param_names
.add
(np
.n_id
.text
)
435 var ntype
= np
.n_type
436 if ntype
!= null then
437 var mtype
= modelbuilder
.resolve_mtype
(mmodule
, mclassdef
, ntype
)
438 if mtype
== null then return false # Skip error
439 for i
in [0..param_names
.length-param_types
.length
[ do
440 param_types
.add
(mtype
)
442 if np
.n_dotdotdot
!= null then
443 if self.vararg_rank
!= -1 then
444 modelbuilder
.error
(np
, "Error: {param_names[self.vararg_rank]} is already a vararg")
447 self.vararg_rank
= param_names
.length
- 1
452 var ntype
= self.n_type
453 if ntype
!= null then
454 self.ret_type
= modelbuilder
.resolve_mtype
(mmodule
, mclassdef
, ntype
)
455 if self.ret_type
== null then return false # Skip error
458 self.is_visited
= true
462 # Build a visited signature
463 fun build_signature
(modelbuilder
: ModelBuilder): nullable MSignature
465 if param_names
.length
!= param_types
.length
then
466 # Some parameters are typed, other parameters are not typed.
467 modelbuilder
.error
(self.n_params
[param_types
.length
], "Error: Untyped parameter `{param_names[param_types.length]}'.")
471 var mparameters
= new Array[MParameter]
472 for i
in [0..param_names
.length
[ do
473 var mparameter
= new MParameter(param_names
[i
], param_types
[i
], i
== vararg_rank
)
474 self.n_params
[i
].mparameter
= mparameter
475 mparameters
.add
(mparameter
)
478 var msignature
= new MSignature(mparameters
, ret_type
)
484 # The associated mparameter if any
485 var mparameter
: nullable MParameter = null
488 redef class AMethPropdef
489 redef type MPROPDEF: MMethodDef
492 # Can self be used as a root init?
493 private fun look_like_a_root_init
(modelbuilder
: ModelBuilder, mclassdef
: MClassDef): Bool
495 # Need the `init` keyword
496 if n_kwinit
== null then return false
497 # Need to by anonymous
498 if self.n_methid
!= null then return false
499 # No annotation on itself
500 if get_single_annotation
("old_style_init", modelbuilder
) != null then return false
502 var amod
= self.parent
.parent
.as(AModule)
503 var amoddecl
= amod
.n_moduledecl
504 if amoddecl
!= null then
505 var old
= amoddecl
.get_single_annotation
("old_style_init", modelbuilder
)
506 if old
!= null then return false
509 if self.n_signature
.n_params
.length
> 0 then
510 modelbuilder
.advice
(self, "old-init", "Warning: init with signature in {mclassdef}")
513 # Cannot be private or something
514 if not self.n_visibility
isa APublicVisibility then
515 modelbuilder
.advice
(self, "old-init", "Warning: non-public init in {mclassdef}")
522 redef fun build_property
(modelbuilder
, mclassdef
)
524 var n_kwinit
= n_kwinit
525 var n_kwnew
= n_kwnew
526 var is_init
= n_kwinit
!= null or n_kwnew
!= null
528 var amethodid
= self.n_methid
530 if amethodid
== null then
534 else if n_kwinit
!= null then
537 else if n_kwnew
!= null then
543 else if amethodid
isa AIdMethid then
544 name
= amethodid
.n_id
.text
545 name_node
= amethodid
547 # operator, bracket or assign
548 name
= amethodid
.collect_text
549 name_node
= amethodid
551 if name
== "-" and self.n_signature
.n_params
.length
== 0 then
556 var look_like_a_root_init
= look_like_a_root_init
(modelbuilder
, mclassdef
)
557 var mprop
: nullable MMethod = null
558 if not is_init
or n_kwredef
!= null then mprop
= modelbuilder
.try_get_mproperty_by_name
(name_node
, mclassdef
, name
).as(nullable MMethod)
559 if mprop
== null and look_like_a_root_init
then
560 mprop
= modelbuilder
.the_root_init_mmethod
562 if nb
isa ABlockExpr and nb
.n_expr
.is_empty
and n_doc
== null then
563 modelbuilder
.advice
(self, "useless-init", "Warning: useless empty init in {mclassdef}")
566 if mprop
== null then
567 var mvisibility
= new_property_visibility
(modelbuilder
, mclassdef
, self.n_visibility
)
568 mprop
= new MMethod(mclassdef
, name
, mvisibility
)
569 if look_like_a_root_init
and modelbuilder
.the_root_init_mmethod
== null then
570 modelbuilder
.the_root_init_mmethod
= mprop
571 mprop
.is_root_init
= true
573 mprop
.is_init
= is_init
574 mprop
.is_new
= n_kwnew
!= null
575 if parent
isa ATopClassdef then mprop
.is_toplevel
= true
576 if not self.check_redef_keyword
(modelbuilder
, mclassdef
, n_kwredef
, false, mprop
) then return
578 if not mprop
.is_root_init
and not self.check_redef_keyword
(modelbuilder
, mclassdef
, n_kwredef
, not self isa AMainMethPropdef, mprop
) then return
579 check_redef_property_visibility
(modelbuilder
, self.n_visibility
, mprop
)
581 mclassdef
.mprop2npropdef
[mprop
] = self
583 var mpropdef
= new MMethodDef(mclassdef
, mprop
, self.location
)
585 set_doc
(mpropdef
, modelbuilder
)
587 self.mpropdef
= mpropdef
588 modelbuilder
.mpropdef2npropdef
[mpropdef
] = self
589 if mpropdef
.is_intro
then
590 modelbuilder
.toolcontext
.info
("{mpropdef} introduces new method {mprop.full_name}", 4)
592 modelbuilder
.toolcontext
.info
("{mpropdef} redefines method {mprop.full_name}", 4)
596 redef fun build_signature
(modelbuilder
)
598 var mpropdef
= self.mpropdef
599 if mpropdef
== null then return # Error thus skiped
600 var mclassdef
= mpropdef
.mclassdef
601 var mmodule
= mclassdef
.mmodule
602 var nsig
= self.n_signature
604 if mpropdef
.mproperty
.is_root_init
and not mclassdef
.is_intro
then
605 var root_init
= mclassdef
.mclass
.root_init
606 if root_init
!= null then
607 # Inherit the initializers by refinement
608 mpropdef
.new_msignature
= root_init
.new_msignature
609 assert mpropdef
.initializers
.is_empty
610 mpropdef
.initializers
.add_all root_init
.initializers
614 # Retrieve info from the signature AST
615 var param_names
= new Array[String] # Names of parameters from the AST
616 var param_types
= new Array[MType] # Types of parameters from the AST
618 var ret_type
: nullable MType = null # Return type from the AST
620 if not nsig
.visit_signature
(modelbuilder
, mclassdef
) then return
621 param_names
= nsig
.param_names
622 param_types
= nsig
.param_types
623 vararg_rank
= nsig
.vararg_rank
624 ret_type
= nsig
.ret_type
627 # Look for some signature to inherit
628 # FIXME: do not inherit from the intro, but from the most specific
629 var msignature
: nullable MSignature = null
630 if not mpropdef
.is_intro
then
631 msignature
= mpropdef
.mproperty
.intro
.msignature
632 if msignature
== null then return # Skip error
634 # Check inherited signature arity
635 if param_names
.length
!= msignature
.arity
then
637 if nsig
!= null then node
= nsig
else node
= self
638 modelbuilder
.error
(node
, "Redef error: {mpropdef} redefines {mpropdef.mproperty.intro} with {param_names.length} parameter(s), {msignature.arity} expected. Signature is {mpropdef}{msignature}")
641 else if mpropdef
.mproperty
.is_init
and not mpropdef
.mproperty
.is_new
then
642 # FIXME UGLY: inherit signature from a super-constructor
643 for msupertype
in mclassdef
.supertypes
do
644 msupertype
= msupertype
.anchor_to
(mmodule
, mclassdef
.bound_mtype
)
645 var candidate
= modelbuilder
.try_get_mproperty_by_name2
(self, mmodule
, msupertype
, mpropdef
.mproperty
.name
)
646 if candidate
!= null then
647 if msignature
== null then
648 msignature
= candidate
.intro
.as(MMethodDef).msignature
655 # Inherit the signature
656 if msignature
!= null and param_names
.length
!= param_types
.length
and param_names
.length
== msignature
.arity
and param_types
.length
== 0 then
657 # Parameters are untyped, thus inherit them
658 param_types
= new Array[MType]
659 for mparameter
in msignature
.mparameters
do
660 param_types
.add
(mparameter
.mtype
)
662 vararg_rank
= msignature
.vararg_rank
664 if msignature
!= null and ret_type
== null then
665 ret_type
= msignature
.return_mtype
668 if param_names
.length
!= param_types
.length
then
669 # Some parameters are typed, other parameters are not typed.
670 modelbuilder
.error
(nsig
.n_params
[param_types
.length
], "Error: Untyped parameter `{param_names[param_types.length]}'.")
674 var mparameters
= new Array[MParameter]
675 for i
in [0..param_names
.length
[ do
676 var mparameter
= new MParameter(param_names
[i
], param_types
[i
], i
== vararg_rank
)
677 if nsig
!= null then nsig
.n_params
[i
].mparameter
= mparameter
678 mparameters
.add
(mparameter
)
681 # In `new`-factories, the return type is by default the classtype.
682 if ret_type
== null and mpropdef
.mproperty
.is_new
then ret_type
= mclassdef
.mclass
.mclass_type
684 msignature
= new MSignature(mparameters
, ret_type
)
685 mpropdef
.msignature
= msignature
686 mpropdef
.is_abstract
= self.get_single_annotation
("abstract", modelbuilder
) != null
687 mpropdef
.is_intern
= self.get_single_annotation
("intern", modelbuilder
) != null
688 mpropdef
.is_extern
= self.n_extern_code_block
!= null or self.get_single_annotation
("extern", modelbuilder
) != null
691 redef fun check_signature
(modelbuilder
)
693 var mpropdef
= self.mpropdef
694 if mpropdef
== null then return # Error thus skiped
695 var mclassdef
= mpropdef
.mclassdef
696 var mmodule
= mclassdef
.mmodule
697 var nsig
= self.n_signature
698 var mysignature
= self.mpropdef
.msignature
699 if mysignature
== null then return # Error thus skiped
701 # Lookup for signature in the precursor
702 # FIXME all precursors should be considered
703 if not mpropdef
.is_intro
then
704 var msignature
= mpropdef
.mproperty
.intro
.msignature
705 if msignature
== null then return
707 var precursor_ret_type
= msignature
.return_mtype
708 var ret_type
= mysignature
.return_mtype
709 if ret_type
!= null and precursor_ret_type
== null then
710 modelbuilder
.error
(nsig
.n_type
.as(not null), "Redef Error: {mpropdef.mproperty} is a procedure, not a function.")
714 if mysignature
.arity
> 0 then
715 # Check parameters types
716 for i
in [0..mysignature
.arity
[ do
717 var myt
= mysignature
.mparameters
[i
].mtype
718 var prt
= msignature
.mparameters
[i
].mtype
719 if not myt
.is_subtype
(mmodule
, mclassdef
.bound_mtype
, prt
) or
720 not prt
.is_subtype
(mmodule
, mclassdef
.bound_mtype
, myt
) then
721 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}.")
725 if precursor_ret_type
!= null then
726 if ret_type
== null then
727 # Inherit the return type
728 ret_type
= precursor_ret_type
729 else if not ret_type
.is_subtype
(mmodule
, mclassdef
.bound_mtype
, precursor_ret_type
) then
730 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}.")
735 if mysignature
.arity
> 0 then
736 # Check parameters visibility
737 for i
in [0..mysignature
.arity
[ do
738 var nt
= nsig
.n_params
[i
].n_type
739 if nt
!= null then modelbuilder
.check_visibility
(nt
, nt
.mtype
.as(not null), mpropdef
)
742 if nt
!= null then modelbuilder
.check_visibility
(nt
, nt
.mtype
.as(not null), mpropdef
)
747 redef class AAttrPropdef
748 redef type MPROPDEF: MAttributeDef
750 # Is the node tagged `noinit`?
753 # Is the node tagged lazy?
756 # Has the node a default value?
757 # Could be through `n_expr` or `n_block`
758 var has_value
= false
760 # The guard associated to a lazy attribute.
761 # Because some engines does not have a working `isset`,
762 # this additional attribute is used to guard the lazy initialization.
763 # TODO: to remove once isset is correctly implemented
764 var mlazypropdef
: nullable MAttributeDef
766 # The associated getter (read accessor) if any
767 var mreadpropdef
: nullable MMethodDef is writable
768 # The associated setter (write accessor) if any
769 var mwritepropdef
: nullable MMethodDef is writable
771 redef fun build_property
(modelbuilder
, mclassdef
)
773 var mclass
= mclassdef
.mclass
776 name
= self.n_id2
.text
778 if mclass
.kind
== interface_kind
or mclassdef
.mclass
.kind
== enum_kind
then
779 modelbuilder
.error
(self, "Error: Attempt to define attribute {name} in the interface {mclass}.")
780 else if mclass
.kind
== enum_kind
then
781 modelbuilder
.error
(self, "Error: Attempt to define attribute {name} in the enum class {mclass}.")
782 else if mclass
.kind
== extern_kind
then
783 modelbuilder
.error
(self, "Error: Attempt to define attribute {name} in the extern class {mclass}.")
786 # New attribute style
787 var nid2
= self.n_id2
788 var mprop
= new MAttribute(mclassdef
, "_" + name
, private_visibility
)
789 var mpropdef
= new MAttributeDef(mclassdef
, mprop
, self.location
)
790 self.mpropdef
= mpropdef
791 modelbuilder
.mpropdef2npropdef
[mpropdef
] = self
794 var mreadprop
= modelbuilder
.try_get_mproperty_by_name
(nid2
, mclassdef
, readname
).as(nullable MMethod)
795 if mreadprop
== null then
796 var mvisibility
= new_property_visibility
(modelbuilder
, mclassdef
, self.n_visibility
)
797 mreadprop
= new MMethod(mclassdef
, readname
, mvisibility
)
798 if not self.check_redef_keyword
(modelbuilder
, mclassdef
, n_kwredef
, false, mreadprop
) then return
799 mreadprop
.deprecation
= mprop
.deprecation
801 if not self.check_redef_keyword
(modelbuilder
, mclassdef
, n_kwredef
, true, mreadprop
) then return
802 check_redef_property_visibility
(modelbuilder
, self.n_visibility
, mreadprop
)
804 mclassdef
.mprop2npropdef
[mreadprop
] = self
806 var mreadpropdef
= new MMethodDef(mclassdef
, mreadprop
, self.location
)
807 self.mreadpropdef
= mreadpropdef
808 modelbuilder
.mpropdef2npropdef
[mreadpropdef
] = self
809 set_doc
(mreadpropdef
, modelbuilder
)
810 mpropdef
.mdoc
= mreadpropdef
.mdoc
812 has_value
= n_expr
!= null or n_block
!= null
814 var atnoinit
= self.get_single_annotation
("noinit", modelbuilder
)
815 if atnoinit
!= null then
818 modelbuilder
.error
(atnoinit
, "Error: `noinit` attributes cannot have an initial value")
823 var atlazy
= self.get_single_annotation
("lazy", modelbuilder
)
824 var atautoinit
= self.get_single_annotation
("autoinit", modelbuilder
)
825 if atlazy
!= null or atautoinit
!= null then
826 if atlazy
!= null and atautoinit
!= null then
827 modelbuilder
.error
(atlazy
, "Error: lazy incompatible with autoinit")
830 if not has_value
then
831 if atlazy
!= null then
832 modelbuilder
.error
(atlazy
, "Error: a lazy attribute needs a value")
833 else if atautoinit
!= null then
834 modelbuilder
.error
(atautoinit
, "Error: a autoinit attribute needs a value")
839 var mlazyprop
= new MAttribute(mclassdef
, "lazy _" + name
, none_visibility
)
840 var mlazypropdef
= new MAttributeDef(mclassdef
, mlazyprop
, self.location
)
841 self.mlazypropdef
= mlazypropdef
844 var atreadonly
= self.get_single_annotation
("readonly", modelbuilder
)
845 if atreadonly
!= null then
846 if not has_value
then
847 modelbuilder
.error
(atreadonly
, "Error: a readonly attribute needs a value")
849 # No setter, so just leave
853 var writename
= name
+ "="
854 var atwritable
= self.get_single_annotation
("writable", modelbuilder
)
855 if atwritable
!= null then
856 if not atwritable
.n_args
.is_empty
then
857 writename
= atwritable
.arg_as_id
(modelbuilder
) or else writename
860 var mwriteprop
= modelbuilder
.try_get_mproperty_by_name
(nid2
, mclassdef
, writename
).as(nullable MMethod)
861 var nwkwredef
: nullable Token = null
862 if atwritable
!= null then nwkwredef
= atwritable
.n_kwredef
863 if mwriteprop
== null then
865 if atwritable
!= null then
866 mvisibility
= new_property_visibility
(modelbuilder
, mclassdef
, atwritable
.n_visibility
)
868 mvisibility
= private_visibility
870 mwriteprop
= new MMethod(mclassdef
, writename
, mvisibility
)
871 if not self.check_redef_keyword
(modelbuilder
, mclassdef
, nwkwredef
, false, mwriteprop
) then return
872 mwriteprop
.deprecation
= mprop
.deprecation
874 if not self.check_redef_keyword
(modelbuilder
, mclassdef
, nwkwredef
or else n_kwredef
, true, mwriteprop
) then return
875 if atwritable
!= null then
876 check_redef_property_visibility
(modelbuilder
, atwritable
.n_visibility
, mwriteprop
)
879 mclassdef
.mprop2npropdef
[mwriteprop
] = self
881 var mwritepropdef
= new MMethodDef(mclassdef
, mwriteprop
, self.location
)
882 self.mwritepropdef
= mwritepropdef
883 modelbuilder
.mpropdef2npropdef
[mwritepropdef
] = self
884 mwritepropdef
.mdoc
= mpropdef
.mdoc
887 redef fun build_signature
(modelbuilder
)
889 var mpropdef
= self.mpropdef
890 if mpropdef
== null then return # Error thus skipped
891 var mclassdef
= mpropdef
.mclassdef
892 var mmodule
= mclassdef
.mmodule
893 var mtype
: nullable MType = null
895 var mreadpropdef
= self.mreadpropdef
897 var ntype
= self.n_type
898 if ntype
!= null then
899 mtype
= modelbuilder
.resolve_mtype
(mmodule
, mclassdef
, ntype
)
900 if mtype
== null then return
903 # Inherit the type from the getter (usually an abstract getter)
904 if mtype
== null and mreadpropdef
!= null and not mreadpropdef
.is_intro
then
905 var msignature
= mreadpropdef
.mproperty
.intro
.msignature
906 if msignature
== null then return # Error, thus skipped
907 mtype
= msignature
.return_mtype
910 var nexpr
= self.n_expr
911 if mtype
== null then
912 if nexpr
!= null then
913 if nexpr
isa ANewExpr then
914 mtype
= modelbuilder
.resolve_mtype
(mmodule
, mclassdef
, nexpr
.n_type
)
915 else if nexpr
isa AIntExpr then
916 var cla
= modelbuilder
.try_get_mclass_by_name
(nexpr
, mmodule
, "Int")
917 if cla
!= null then mtype
= cla
.mclass_type
918 else if nexpr
isa AFloatExpr then
919 var cla
= modelbuilder
.try_get_mclass_by_name
(nexpr
, mmodule
, "Float")
920 if cla
!= null then mtype
= cla
.mclass_type
921 else if nexpr
isa ACharExpr then
922 var cla
= modelbuilder
.try_get_mclass_by_name
(nexpr
, mmodule
, "Char")
923 if cla
!= null then mtype
= cla
.mclass_type
924 else if nexpr
isa ABoolExpr then
925 var cla
= modelbuilder
.try_get_mclass_by_name
(nexpr
, mmodule
, "Bool")
926 if cla
!= null then mtype
= cla
.mclass_type
927 else if nexpr
isa ASuperstringExpr then
928 var cla
= modelbuilder
.try_get_mclass_by_name
(nexpr
, mmodule
, "String")
929 if cla
!= null then mtype
= cla
.mclass_type
930 else if nexpr
isa AStringFormExpr then
931 var cla
= modelbuilder
.try_get_mclass_by_name
(nexpr
, mmodule
, "String")
932 if cla
!= null then mtype
= cla
.mclass_type
934 modelbuilder
.error
(self, "Error: Untyped attribute {mpropdef}. Implicit typing allowed only for literals and new.")
937 if mtype
== null then return
939 else if ntype
!= null then
940 if nexpr
isa ANewExpr then
941 var xmtype
= modelbuilder
.resolve_mtype
(mmodule
, mclassdef
, nexpr
.n_type
)
942 if xmtype
== mtype
then
943 modelbuilder
.advice
(ntype
, "useless-type", "Warning: useless type definition")
948 if mtype
== null then
949 modelbuilder
.error
(self, "Error: Untyped attribute {mpropdef}")
953 mpropdef
.static_mtype
= mtype
955 if mreadpropdef
!= null then
956 var msignature
= new MSignature(new Array[MParameter], mtype
)
957 mreadpropdef
.msignature
= msignature
960 var mwritepropdef
= self.mwritepropdef
961 if mwritepropdef
!= null then
964 var mparameter
= new MParameter(name
, mtype
, false)
965 var msignature
= new MSignature([mparameter
], null)
966 mwritepropdef
.msignature
= msignature
969 var mlazypropdef
= self.mlazypropdef
970 if mlazypropdef
!= null then
971 mlazypropdef
.static_mtype
= modelbuilder
.model
.get_mclasses_by_name
("Bool").first
.mclass_type
975 redef fun check_signature
(modelbuilder
)
977 var mpropdef
= self.mpropdef
978 if mpropdef
== null then return # Error thus skipped
979 var ntype
= self.n_type
980 var mtype
= self.mpropdef
.static_mtype
981 if mtype
== null then return # Error thus skipped
983 # Lookup for signature in the precursor
984 # FIXME all precursors should be considered
985 if not mpropdef
.is_intro
then
986 var precursor_type
= mpropdef
.mproperty
.intro
.static_mtype
987 if precursor_type
== null then return
989 if mtype
!= precursor_type
then
990 modelbuilder
.error
(ntype
.as(not null), "Redef Error: Wrong static type. found {mtype}, expected {precursor_type}.")
995 # Check getter and setter
996 var meth
= self.mreadpropdef
998 self.check_method_signature
(modelbuilder
, meth
)
999 var node
: nullable ANode = ntype
1000 if node
== null then node
= self
1001 modelbuilder
.check_visibility
(node
, mtype
, meth
)
1003 meth
= self.mwritepropdef
1004 if meth
!= null then
1005 self.check_method_signature
(modelbuilder
, meth
)
1006 var node
: nullable ANode = ntype
1007 if node
== null then node
= self
1008 modelbuilder
.check_visibility
(node
, mtype
, meth
)
1012 private fun check_method_signature
(modelbuilder
: ModelBuilder, mpropdef
: MMethodDef)
1014 var mclassdef
= mpropdef
.mclassdef
1015 var mmodule
= mclassdef
.mmodule
1016 var nsig
= self.n_type
1017 var mysignature
= mpropdef
.msignature
1018 if mysignature
== null then return # Error thus skiped
1020 # Lookup for signature in the precursor
1021 # FIXME all precursors should be considered
1022 if not mpropdef
.is_intro
then
1023 var msignature
= mpropdef
.mproperty
.intro
.msignature
1024 if msignature
== null then return
1026 if mysignature
.arity
!= msignature
.arity
then
1028 if nsig
!= null then node
= nsig
else node
= self
1029 modelbuilder
.error
(node
, "Redef Error: {mysignature.arity} parameters found, {msignature.arity} expected. Signature is {mpropdef}{msignature}")
1032 var precursor_ret_type
= msignature
.return_mtype
1033 var ret_type
= mysignature
.return_mtype
1034 if ret_type
!= null and precursor_ret_type
== null then
1036 if nsig
!= null then node
= nsig
else node
= self
1037 modelbuilder
.error
(node
, "Redef Error: {mpropdef.mproperty} is a procedure, not a function.")
1041 if mysignature
.arity
> 0 then
1042 # Check parameters types
1043 for i
in [0..mysignature
.arity
[ do
1044 var myt
= mysignature
.mparameters
[i
].mtype
1045 var prt
= msignature
.mparameters
[i
].mtype
1046 if not myt
.is_subtype
(mmodule
, mclassdef
.bound_mtype
, prt
) or
1047 not prt
.is_subtype
(mmodule
, mclassdef
.bound_mtype
, myt
) then
1049 if nsig
!= null then node
= nsig
else node
= self
1050 modelbuilder
.error
(node
, "Redef Error: Wrong type for parameter `{mysignature.mparameters[i].name}'. found {myt}, expected {prt}.")
1054 if precursor_ret_type
!= null then
1055 if ret_type
== null then
1056 # Inherit the return type
1057 ret_type
= precursor_ret_type
1058 else if not ret_type
.is_subtype
(mmodule
, mclassdef
.bound_mtype
, precursor_ret_type
) then
1060 if nsig
!= null then node
= nsig
else node
= self
1061 modelbuilder
.error
(node
, "Redef Error: Wrong return type. found {ret_type}, expected {precursor_ret_type}.")
1068 redef class ATypePropdef
1069 redef type MPROPDEF: MVirtualTypeDef
1071 redef fun build_property
(modelbuilder
, mclassdef
)
1073 var name
= self.n_id
.text
1074 var mprop
= modelbuilder
.try_get_mproperty_by_name
(self.n_id
, mclassdef
, name
)
1075 if mprop
== null then
1076 var mvisibility
= new_property_visibility
(modelbuilder
, mclassdef
, self.n_visibility
)
1077 mprop
= new MVirtualTypeProp(mclassdef
, name
, mvisibility
)
1078 for c
in name
.chars
do if c
>= 'a' and c
<= 'z' then
1079 modelbuilder
.warning
(n_id
, "bad-type-name", "Warning: lowercase in the virtual type {name}")
1082 if not self.check_redef_keyword
(modelbuilder
, mclassdef
, self.n_kwredef
, false, mprop
) then return
1084 if not self.check_redef_keyword
(modelbuilder
, mclassdef
, self.n_kwredef
, true, mprop
) then return
1085 assert mprop
isa MVirtualTypeProp
1086 check_redef_property_visibility
(modelbuilder
, self.n_visibility
, mprop
)
1088 mclassdef
.mprop2npropdef
[mprop
] = self
1090 var mpropdef
= new MVirtualTypeDef(mclassdef
, mprop
, self.location
)
1091 self.mpropdef
= mpropdef
1092 modelbuilder
.mpropdef2npropdef
[mpropdef
] = self
1093 set_doc
(mpropdef
, modelbuilder
)
1095 var atfixed
= get_single_annotation
("fixed", modelbuilder
)
1096 if atfixed
!= null then
1097 mpropdef
.is_fixed
= true
1101 redef fun build_signature
(modelbuilder
)
1103 var mpropdef
= self.mpropdef
1104 if mpropdef
== null then return # Error thus skipped
1105 var mclassdef
= mpropdef
.mclassdef
1106 var mmodule
= mclassdef
.mmodule
1107 var mtype
: nullable MType = null
1109 var ntype
= self.n_type
1110 mtype
= modelbuilder
.resolve_mtype
(mmodule
, mclassdef
, ntype
)
1111 if mtype
== null then return
1113 mpropdef
.bound
= mtype
1114 # print "{mpropdef}: {mtype}"
1117 redef fun check_signature
(modelbuilder
)
1119 var mpropdef
= self.mpropdef
1120 if mpropdef
== null then return # Error thus skipped
1122 var bound
= self.mpropdef
.bound
1123 if bound
== null then return # Error thus skipped
1125 modelbuilder
.check_visibility
(n_type
, bound
, mpropdef
)
1127 var mclassdef
= mpropdef
.mclassdef
1128 var mmodule
= mclassdef
.mmodule
1129 var anchor
= mclassdef
.bound_mtype
1132 if bound
isa MVirtualType then
1133 # Slow case: progress on each resolution until: (i) we loop, or (ii) we found a non formal type
1134 var seen
= [self.mpropdef
.mproperty
.mvirtualtype
]
1136 if seen
.has
(bound
) then
1138 modelbuilder
.error
(self, "Error: circularity of virtual type definition: {seen.join(" -> ")}")
1142 var next
= bound
.lookup_bound
(mmodule
, anchor
)
1143 if not next
isa MVirtualType then break
1148 # Check redefinitions
1149 bound
= mpropdef
.bound
.as(not null)
1150 for p
in mpropdef
.mproperty
.lookup_super_definitions
(mmodule
, anchor
) do
1151 var supbound
= p
.bound
.as(not null)
1153 modelbuilder
.error
(self, "Redef Error: Virtual type {mpropdef.mproperty} is fixed in super-class {p.mclassdef.mclass}")
1156 if p
.mclassdef
.mclass
== mclassdef
.mclass
then
1157 # Still a warning to pass existing bad code
1158 modelbuilder
.warning
(n_type
, "refine-type", "Redef Error: a virtual type cannot be refined.")
1161 if not bound
.is_subtype
(mmodule
, anchor
, supbound
) then
1162 modelbuilder
.error
(n_type
, "Redef Error: Wrong bound type. Found {bound}, expected a subtype of {supbound}, as in {p}.")