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
: HashMap[MPropDef, APropdef] = 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 var at
= npropdef
.get_single_annotation
("noinit", self)
149 npropdef
.noinit
= true
150 if npropdef
.n_expr
!= null then
151 self.error
(at
, "Error: `noinit` attributes cannot have an initial value")
153 continue # Skip noinit attributes
155 if npropdef
.n_expr
!= null 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)
208 # Combine the inherited list to what is collected
209 if longest
.initializers
.length
> 0 then
210 mparameters
.prepend longest
.new_msignature
.mparameters
211 initializers
.prepend longest
.initializers
214 # If we already have a basic init definition, then setup its initializers
215 if defined_init
!= null then
216 defined_init
.initializers
.add_all
(initializers
)
217 var msignature
= new MSignature(mparameters
, null)
218 defined_init
.new_msignature
= msignature
219 self.toolcontext
.info
("{mclassdef} extends its basic constructor signature to {defined_init}{msignature}", 3)
223 # Else create the local implicit basic init definition
224 var mprop
= the_root_init_mmethod
.as(not null)
225 var mpropdef
= new MMethodDef(mclassdef
, mprop
, nclassdef
.location
)
226 mpropdef
.has_supercall
= true
227 mpropdef
.initializers
.add_all
(initializers
)
228 var msignature
= new MSignature(mparameters
, null)
229 mpropdef
.new_msignature
= msignature
230 mpropdef
.msignature
= new MSignature(new Array[MParameter], null) # always an empty real signature
231 nclassdef
.mfree_init
= mpropdef
232 self.toolcontext
.info
("{mclassdef} gets a free constructor for attributes {mpropdef}{msignature}", 3)
235 # Check the visibility of `mtype` as an element of the signature of `mpropdef`.
236 fun check_visibility
(node
: ANode, mtype
: MType, mpropdef
: MPropDef)
238 var mmodule
= mpropdef
.mclassdef
.mmodule
239 var mproperty
= mpropdef
.mproperty
241 # Extract visibility information of the main part of `mtype`
242 # It is a case-by case
243 var vis_type
: nullable MVisibility = null # The own visibility of the type
244 var mmodule_type
: nullable MModule = null # The origial module of the type
245 mtype
= mtype
.as_notnullable
246 if mtype
isa MClassType then
247 vis_type
= mtype
.mclass
.visibility
248 mmodule_type
= mtype
.mclass
.intro
.mmodule
249 else if mtype
isa MVirtualType then
250 vis_type
= mtype
.mproperty
.visibility
251 mmodule_type
= mtype
.mproperty
.intro_mclassdef
.mmodule
252 else if mtype
isa MParameterType then
253 # nothing, always visible
255 node
.debug
"Unexpected type {mtype}"
259 if vis_type
!= null then
260 assert mmodule_type
!= null
261 var vis_module_type
= mmodule
.visibility_for
(mmodule_type
) # the visibility of the original module
262 if mproperty
.visibility
> vis_type
then
263 error
(node
, "Error: The {mproperty.visibility} property `{mproperty}` cannot contain the {vis_type} type `{mtype}`")
265 else if mproperty
.visibility
> vis_module_type
then
266 error
(node
, "Error: The {mproperty.visibility} property `{mproperty}` cannot contain the type `{mtype}` from the {vis_module_type} module `{mmodule_type}`")
271 # No error, try to go deeper in generic types
272 if node
isa AType then
273 for a
in node
.n_types
do
275 if t
== null then continue # Error, thus skipped
276 check_visibility
(a
, t
, mpropdef
)
278 else if mtype
isa MGenericType then
279 for t
in mtype
.arguments
do check_visibility
(node
, t
, mpropdef
)
285 # Does the MPropDef contains a call to super or a call of a super-constructor?
286 # Subsequent phases of the frontend (esp. typing) set it if required
287 var has_supercall
: Bool = false is writable
290 redef class AClassdef
291 var build_properties_is_done
: Bool = false
293 # The free init (implicitely constructed by the class if required)
294 var mfree_init
: nullable MMethodDef = null
297 redef class MClassDef
298 # What is the `APropdef` associated to a `MProperty`?
299 # Used to check multiple definition of a property.
300 var mprop2npropdef
: Map[MProperty, APropdef] = new HashMap[MProperty, APropdef]
304 # Join the text of all tokens
305 # Used to get the 'real name' of method definitions.
306 fun collect_text
: String
308 var v
= new TextCollectorVisitor
315 private class TextCollectorVisitor
317 var text
: String = ""
320 if n
isa Token then text
+= n
.text
326 # The associated main model entity
327 type MPROPDEF: MPropDef
329 # The associated propdef once build by a `ModelBuilder`
330 var mpropdef
: nullable MPROPDEF is writable
332 private fun build_property
(modelbuilder
: ModelBuilder, mclassdef
: MClassDef) is abstract
333 private fun build_signature
(modelbuilder
: ModelBuilder) is abstract
334 private fun check_signature
(modelbuilder
: ModelBuilder) is abstract
335 private fun new_property_visibility
(modelbuilder
: ModelBuilder, mclassdef
: MClassDef, nvisibility
: nullable AVisibility): MVisibility
337 var mvisibility
= public_visibility
338 if nvisibility
!= null then
339 mvisibility
= nvisibility
.mvisibility
340 if mvisibility
== intrude_visibility
then
341 modelbuilder
.error
(nvisibility
, "Error: intrude is not a legal visibility for properties.")
342 mvisibility
= public_visibility
345 if mclassdef
.mclass
.visibility
== private_visibility
then
346 if mvisibility
== protected_visibility
then
347 assert nvisibility
!= null
348 modelbuilder
.error
(nvisibility
, "Error: The only legal visibility for properties in a private class is private.")
349 else if mvisibility
== private_visibility
then
350 assert nvisibility
!= null
351 modelbuilder
.advice
(nvisibility
, "useless-visibility", "Warning: private is superfluous since the only legal visibility for properties in a private class is private.")
353 mvisibility
= private_visibility
358 private fun set_doc
(mpropdef
: MPropDef, modelbuilder
: ModelBuilder)
360 var ndoc
= self.n_doc
362 var mdoc
= ndoc
.to_mdoc
364 mdoc
.original_mentity
= mpropdef
365 else if mpropdef
.is_intro
and mpropdef
.mproperty
.visibility
>= protected_visibility
then
366 modelbuilder
.advice
(self, "missing-doc", "Documentation warning: Undocumented property `{mpropdef.mproperty}`")
369 var at_deprecated
= get_single_annotation
("deprecated", modelbuilder
)
370 if at_deprecated
!= null then
371 if not mpropdef
.is_intro
then
372 modelbuilder
.error
(self, "Error: method redefinition cannot be deprecated.")
374 var info
= new MDeprecationInfo
375 ndoc
= at_deprecated
.n_doc
376 if ndoc
!= null then info
.mdoc
= ndoc
.to_mdoc
377 mpropdef
.mproperty
.deprecation
= info
382 private fun check_redef_property_visibility
(modelbuilder
: ModelBuilder, nvisibility
: nullable AVisibility, mprop
: MProperty)
384 if nvisibility
== null then return
385 var mvisibility
= nvisibility
.mvisibility
386 if mvisibility
!= mprop
.visibility
and mvisibility
!= public_visibility
then
387 modelbuilder
.error
(nvisibility
, "Error: redefinition changed the visibility from a {mprop.visibility} to a {mvisibility}")
391 private fun check_redef_keyword
(modelbuilder
: ModelBuilder, mclassdef
: MClassDef, kwredef
: nullable Token, need_redef
: Bool, mprop
: MProperty): Bool
393 if mclassdef
.mprop2npropdef
.has_key
(mprop
) then
394 modelbuilder
.error
(self, "Error: A property {mprop} is already defined in class {mclassdef.mclass} at line {mclassdef.mprop2npropdef[mprop].location.line_start}.")
397 if mprop
isa MMethod and mprop
.is_toplevel
!= (parent
isa ATopClassdef) then
398 if mprop
.is_toplevel
then
399 modelbuilder
.error
(self, "Error: {mprop} is a top level method.")
401 modelbuilder
.error
(self, "Error: {mprop} is not a top level method.")
406 if kwredef
== null then
408 modelbuilder
.error
(self, "Redef error: {mclassdef.mclass}::{mprop.name} is an inherited property. To redefine it, add the redef keyword.")
412 if not need_redef
then
413 modelbuilder
.error
(self, "Error: No property {mclassdef.mclass}::{mprop.name} is inherited. Remove the redef keyword to define a new property.")
422 redef class ASignature
423 # Is the model builder has correctly visited the signature
424 var is_visited
= false
425 # Names of parameters from the AST
426 # REQUIRE: is_visited
427 var param_names
= new Array[String]
428 # Types of parameters from the AST
429 # REQUIRE: is_visited
430 var param_types
= new Array[MType]
431 # Rank of the vararg (of -1 if none)
432 # REQUIRE: is_visited
433 var vararg_rank
: Int = -1
435 var ret_type
: nullable MType = null
437 # Visit and fill information about a signature
438 private fun visit_signature
(modelbuilder
: ModelBuilder, mclassdef
: MClassDef): Bool
440 var mmodule
= mclassdef
.mmodule
441 var param_names
= self.param_names
442 var param_types
= self.param_types
443 for np
in self.n_params
do
444 param_names
.add
(np
.n_id
.text
)
445 var ntype
= np
.n_type
446 if ntype
!= null then
447 var mtype
= modelbuilder
.resolve_mtype
(mmodule
, mclassdef
, ntype
)
448 if mtype
== null then return false # Skip error
449 for i
in [0..param_names
.length-param_types
.length
[ do
450 param_types
.add
(mtype
)
452 if np
.n_dotdotdot
!= null then
453 if self.vararg_rank
!= -1 then
454 modelbuilder
.error
(np
, "Error: {param_names[self.vararg_rank]} is already a vararg")
457 self.vararg_rank
= param_names
.length
- 1
462 var ntype
= self.n_type
463 if ntype
!= null then
464 self.ret_type
= modelbuilder
.resolve_mtype
(mmodule
, mclassdef
, ntype
)
465 if self.ret_type
== null then return false # Skip errir
468 self.is_visited
= true
472 # Build a visited signature
473 fun build_signature
(modelbuilder
: ModelBuilder): nullable MSignature
475 if param_names
.length
!= param_types
.length
then
476 # Some parameters are typed, other parameters are not typed.
477 modelbuilder
.error
(self.n_params
[param_types
.length
], "Error: Untyped parameter `{param_names[param_types.length]}'.")
481 var mparameters
= new Array[MParameter]
482 for i
in [0..param_names
.length
[ do
483 var mparameter
= new MParameter(param_names
[i
], param_types
[i
], i
== vararg_rank
)
484 self.n_params
[i
].mparameter
= mparameter
485 mparameters
.add
(mparameter
)
488 var msignature
= new MSignature(mparameters
, ret_type
)
494 # The associated mparameter if any
495 var mparameter
: nullable MParameter = null
498 redef class AMethPropdef
499 redef type MPROPDEF: MMethodDef
502 # Can self be used as a root init?
503 private fun look_like_a_root_init
(modelbuilder
: ModelBuilder): Bool
505 # Need the `init` keyword
506 if n_kwinit
== null then return false
507 # Need to by anonymous
508 if self.n_methid
!= null then return false
510 if self.n_signature
.n_params
.length
> 0 then return false
511 # Cannot be private or something
512 if not self.n_visibility
isa APublicVisibility then return false
513 # No annotation on itself
514 if get_single_annotation
("old_style_init", modelbuilder
) != null then return false
516 var amod
= self.parent
.parent
.as(AModule)
517 var amoddecl
= amod
.n_moduledecl
518 if amoddecl
!= null then
519 var old
= amoddecl
.get_single_annotation
("old_style_init", modelbuilder
)
520 if old
!= null then return false
526 redef fun build_property
(modelbuilder
, mclassdef
)
528 var n_kwinit
= n_kwinit
529 var n_kwnew
= n_kwnew
530 var is_init
= n_kwinit
!= null or n_kwnew
!= null
532 var amethodid
= self.n_methid
534 if amethodid
== null then
538 else if n_kwinit
!= null then
541 else if n_kwnew
!= null then
547 else if amethodid
isa AIdMethid then
548 name
= amethodid
.n_id
.text
549 name_node
= amethodid
551 # operator, bracket or assign
552 name
= amethodid
.collect_text
553 name_node
= amethodid
555 if name
== "-" and self.n_signature
.n_params
.length
== 0 then
560 var mprop
: nullable MMethod = null
561 if not is_init
or n_kwredef
!= null then mprop
= modelbuilder
.try_get_mproperty_by_name
(name_node
, mclassdef
, name
).as(nullable MMethod)
562 if mprop
== null and look_like_a_root_init
(modelbuilder
) then
563 mprop
= modelbuilder
.the_root_init_mmethod
565 if nb
isa ABlockExpr and nb
.n_expr
.is_empty
and n_doc
== null then
566 modelbuilder
.advice
(self, "useless-init", "Warning: useless empty init in {mclassdef}")
569 if mprop
== null then
570 var mvisibility
= new_property_visibility
(modelbuilder
, mclassdef
, self.n_visibility
)
571 mprop
= new MMethod(mclassdef
, name
, mvisibility
)
572 if look_like_a_root_init
(modelbuilder
) and modelbuilder
.the_root_init_mmethod
== null then
573 modelbuilder
.the_root_init_mmethod
= mprop
574 mprop
.is_root_init
= true
576 mprop
.is_init
= is_init
577 mprop
.is_new
= n_kwnew
!= null
578 if parent
isa ATopClassdef then mprop
.is_toplevel
= true
579 if not self.check_redef_keyword
(modelbuilder
, mclassdef
, n_kwredef
, false, mprop
) then return
581 if not mprop
.is_root_init
and not self.check_redef_keyword
(modelbuilder
, mclassdef
, n_kwredef
, not self isa AMainMethPropdef, mprop
) then return
582 check_redef_property_visibility
(modelbuilder
, self.n_visibility
, mprop
)
584 mclassdef
.mprop2npropdef
[mprop
] = self
586 var mpropdef
= new MMethodDef(mclassdef
, mprop
, self.location
)
588 set_doc
(mpropdef
, modelbuilder
)
590 self.mpropdef
= mpropdef
591 modelbuilder
.mpropdef2npropdef
[mpropdef
] = self
592 if mpropdef
.is_intro
then
593 modelbuilder
.toolcontext
.info
("{mpropdef} introduces new method {mprop.full_name}", 3)
595 modelbuilder
.toolcontext
.info
("{mpropdef} redefines method {mprop.full_name}", 3)
599 redef fun build_signature
(modelbuilder
)
601 var mpropdef
= self.mpropdef
602 if mpropdef
== null then return # Error thus skiped
603 var mclassdef
= mpropdef
.mclassdef
604 var mmodule
= mclassdef
.mmodule
605 var nsig
= self.n_signature
607 # Retrieve info from the signature AST
608 var param_names
= new Array[String] # Names of parameters from the AST
609 var param_types
= new Array[MType] # Types of parameters from the AST
611 var ret_type
: nullable MType = null # Return type from the AST
613 if not nsig
.visit_signature
(modelbuilder
, mclassdef
) then return
614 param_names
= nsig
.param_names
615 param_types
= nsig
.param_types
616 vararg_rank
= nsig
.vararg_rank
617 ret_type
= nsig
.ret_type
620 # Look for some signature to inherit
621 # FIXME: do not inherit from the intro, but from the most specific
622 var msignature
: nullable MSignature = null
623 if not mpropdef
.is_intro
then
624 msignature
= mpropdef
.mproperty
.intro
.msignature
625 if msignature
== null then return # Skip error
627 # Check inherited signature arity
628 if param_names
.length
!= msignature
.arity
then
630 if nsig
!= null then node
= nsig
else node
= self
631 modelbuilder
.error
(node
, "Redef error: {mpropdef} redefines {mpropdef.mproperty.intro} with {param_names.length} parameter(s), {msignature.arity} expected. Signature is {mpropdef}{msignature}")
634 else if mpropdef
.mproperty
.is_init
then
635 # FIXME UGLY: inherit signature from a super-constructor
636 for msupertype
in mclassdef
.supertypes
do
637 msupertype
= msupertype
.anchor_to
(mmodule
, mclassdef
.bound_mtype
)
638 var candidate
= modelbuilder
.try_get_mproperty_by_name2
(self, mmodule
, msupertype
, mpropdef
.mproperty
.name
)
639 if candidate
!= null then
640 if msignature
== null then
641 msignature
= candidate
.intro
.as(MMethodDef).msignature
648 # Inherit the signature
649 if msignature
!= null and param_names
.length
!= param_types
.length
and param_names
.length
== msignature
.arity
and param_types
.length
== 0 then
650 # Parameters are untyped, thus inherit them
651 param_types
= new Array[MType]
652 for mparameter
in msignature
.mparameters
do
653 param_types
.add
(mparameter
.mtype
)
655 vararg_rank
= msignature
.vararg_rank
657 if msignature
!= null and ret_type
== null then
658 ret_type
= msignature
.return_mtype
661 if param_names
.length
!= param_types
.length
then
662 # Some parameters are typed, other parameters are not typed.
663 modelbuilder
.error
(nsig
.n_params
[param_types
.length
], "Error: Untyped parameter `{param_names[param_types.length]}'.")
667 var mparameters
= new Array[MParameter]
668 for i
in [0..param_names
.length
[ do
669 var mparameter
= new MParameter(param_names
[i
], param_types
[i
], i
== vararg_rank
)
670 if nsig
!= null then nsig
.n_params
[i
].mparameter
= mparameter
671 mparameters
.add
(mparameter
)
674 msignature
= new MSignature(mparameters
, ret_type
)
675 mpropdef
.msignature
= msignature
676 mpropdef
.is_abstract
= self.get_single_annotation
("abstract", modelbuilder
) != null
677 mpropdef
.is_intern
= self.get_single_annotation
("intern", modelbuilder
) != null
678 mpropdef
.is_extern
= self.n_extern_code_block
!= null or self.get_single_annotation
("extern", modelbuilder
) != null
681 redef fun check_signature
(modelbuilder
)
683 var mpropdef
= self.mpropdef
684 if mpropdef
== null then return # Error thus skiped
685 var mclassdef
= mpropdef
.mclassdef
686 var mmodule
= mclassdef
.mmodule
687 var nsig
= self.n_signature
688 var mysignature
= self.mpropdef
.msignature
689 if mysignature
== null then return # Error thus skiped
691 # Lookup for signature in the precursor
692 # FIXME all precursors should be considered
693 if not mpropdef
.is_intro
then
694 var msignature
= mpropdef
.mproperty
.intro
.msignature
695 if msignature
== null then return
697 var precursor_ret_type
= msignature
.return_mtype
698 var ret_type
= mysignature
.return_mtype
699 if ret_type
!= null and precursor_ret_type
== null then
700 modelbuilder
.error
(nsig
.n_type
.as(not null), "Redef Error: {mpropdef.mproperty} is a procedure, not a function.")
704 if mysignature
.arity
> 0 then
705 # Check parameters types
706 for i
in [0..mysignature
.arity
[ do
707 var myt
= mysignature
.mparameters
[i
].mtype
708 var prt
= msignature
.mparameters
[i
].mtype
709 if not myt
.is_subtype
(mmodule
, mclassdef
.bound_mtype
, prt
) or
710 not prt
.is_subtype
(mmodule
, mclassdef
.bound_mtype
, myt
) then
711 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}.")
715 if precursor_ret_type
!= null then
716 if ret_type
== null then
717 # Inherit the return type
718 ret_type
= precursor_ret_type
719 else if not ret_type
.is_subtype
(mmodule
, mclassdef
.bound_mtype
, precursor_ret_type
) then
720 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}.")
725 if mysignature
.arity
> 0 then
726 # Check parameters visibility
727 for i
in [0..mysignature
.arity
[ do
728 var nt
= nsig
.n_params
[i
].n_type
729 if nt
!= null then modelbuilder
.check_visibility
(nt
, nt
.mtype
.as(not null), mpropdef
)
732 if nt
!= null then modelbuilder
.check_visibility
(nt
, nt
.mtype
.as(not null), mpropdef
)
737 redef class AAttrPropdef
738 redef type MPROPDEF: MAttributeDef
740 # Is the node tagged `noinit`?
743 # Is the node taggeg lazy?
746 # The guard associated to a lasy attribute.
747 # Because some engines does not have a working `isset`,
748 # this additionnal attribute is used to guard the lazy initialization.
749 # TODO: to remove once isset is correctly implemented
750 var mlazypropdef
: nullable MAttributeDef
752 # The associated getter (read accessor) if any
753 var mreadpropdef
: nullable MMethodDef is writable
754 # The associated setter (write accessor) if any
755 var mwritepropdef
: nullable MMethodDef is writable
757 redef fun build_property
(modelbuilder
, mclassdef
)
759 var mclass
= mclassdef
.mclass
762 name
= self.n_id2
.text
764 if mclass
.kind
== interface_kind
or mclassdef
.mclass
.kind
== enum_kind
then
765 modelbuilder
.error
(self, "Error: Attempt to define attribute {name} in the interface {mclass}.")
766 else if mclass
.kind
== enum_kind
then
767 modelbuilder
.error
(self, "Error: Attempt to define attribute {name} in the enum class {mclass}.")
768 else if mclass
.kind
== extern_kind
then
769 modelbuilder
.error
(self, "Error: Attempt to define attribute {name} in the extern class {mclass}.")
772 # New attribute style
773 var nid2
= self.n_id2
774 var mprop
= new MAttribute(mclassdef
, "_" + name
, private_visibility
)
775 var mpropdef
= new MAttributeDef(mclassdef
, mprop
, self.location
)
776 self.mpropdef
= mpropdef
777 modelbuilder
.mpropdef2npropdef
[mpropdef
] = self
778 set_doc
(mpropdef
, modelbuilder
)
781 var mreadprop
= modelbuilder
.try_get_mproperty_by_name
(nid2
, mclassdef
, readname
).as(nullable MMethod)
782 if mreadprop
== null then
783 var mvisibility
= new_property_visibility
(modelbuilder
, mclassdef
, self.n_visibility
)
784 mreadprop
= new MMethod(mclassdef
, readname
, mvisibility
)
785 if not self.check_redef_keyword
(modelbuilder
, mclassdef
, n_kwredef
, false, mreadprop
) then return
786 mreadprop
.deprecation
= mprop
.deprecation
788 if not self.check_redef_keyword
(modelbuilder
, mclassdef
, n_kwredef
, true, mreadprop
) then return
789 check_redef_property_visibility
(modelbuilder
, self.n_visibility
, mreadprop
)
791 mclassdef
.mprop2npropdef
[mreadprop
] = self
793 var mreadpropdef
= new MMethodDef(mclassdef
, mreadprop
, self.location
)
794 self.mreadpropdef
= mreadpropdef
795 modelbuilder
.mpropdef2npropdef
[mreadpropdef
] = self
796 mreadpropdef
.mdoc
= mpropdef
.mdoc
798 var atlazy
= self.get_single_annotation
("lazy", modelbuilder
)
799 if atlazy
!= null then
800 if n_expr
== null then
801 modelbuilder
.error
(atlazy
, "Error: a lazy attribute needs a value")
804 var mlazyprop
= new MAttribute(mclassdef
, "lazy _" + name
, none_visibility
)
805 var mlazypropdef
= new MAttributeDef(mclassdef
, mlazyprop
, self.location
)
806 self.mlazypropdef
= mlazypropdef
809 var atreadonly
= self.get_single_annotation
("readonly", modelbuilder
)
810 if atreadonly
!= null then
811 if n_expr
== null then
812 modelbuilder
.error
(atreadonly
, "Error: a readonly attribute needs a value")
814 # No setter, so just leave
818 var writename
= name
+ "="
819 var atwritable
= self.get_single_annotation
("writable", modelbuilder
)
820 if atwritable
!= null then
821 if not atwritable
.n_args
.is_empty
then
822 writename
= atwritable
.arg_as_id
(modelbuilder
) or else writename
825 var mwriteprop
= modelbuilder
.try_get_mproperty_by_name
(nid2
, mclassdef
, writename
).as(nullable MMethod)
826 var nwkwredef
: nullable Token = null
827 if atwritable
!= null then nwkwredef
= atwritable
.n_kwredef
828 if mwriteprop
== null then
830 if atwritable
!= null then
831 mvisibility
= new_property_visibility
(modelbuilder
, mclassdef
, atwritable
.n_visibility
)
833 mvisibility
= private_visibility
835 mwriteprop
= new MMethod(mclassdef
, writename
, mvisibility
)
836 if not self.check_redef_keyword
(modelbuilder
, mclassdef
, nwkwredef
, false, mwriteprop
) then return
837 mwriteprop
.deprecation
= mprop
.deprecation
839 if not self.check_redef_keyword
(modelbuilder
, mclassdef
, nwkwredef
or else n_kwredef
, true, mwriteprop
) then return
840 if atwritable
!= null then
841 check_redef_property_visibility
(modelbuilder
, atwritable
.n_visibility
, mwriteprop
)
844 mclassdef
.mprop2npropdef
[mwriteprop
] = self
846 var mwritepropdef
= new MMethodDef(mclassdef
, mwriteprop
, self.location
)
847 self.mwritepropdef
= mwritepropdef
848 modelbuilder
.mpropdef2npropdef
[mwritepropdef
] = self
849 mwritepropdef
.mdoc
= mpropdef
.mdoc
852 redef fun build_signature
(modelbuilder
)
854 var mpropdef
= self.mpropdef
855 if mpropdef
== null then return # Error thus skiped
856 var mclassdef
= mpropdef
.mclassdef
857 var mmodule
= mclassdef
.mmodule
858 var mtype
: nullable MType = null
860 var mreadpropdef
= self.mreadpropdef
862 var ntype
= self.n_type
863 if ntype
!= null then
864 mtype
= modelbuilder
.resolve_mtype
(mmodule
, mclassdef
, ntype
)
865 if mtype
== null then return
868 # Inherit the type from the getter (usually an abstact getter)
869 if mtype
== null and mreadpropdef
!= null and not mreadpropdef
.is_intro
then
870 var msignature
= mreadpropdef
.mproperty
.intro
.msignature
871 if msignature
== null then return # Error, thus skiped
872 mtype
= msignature
.return_mtype
875 var nexpr
= self.n_expr
876 if mtype
== null then
877 if nexpr
!= null then
878 if nexpr
isa ANewExpr then
879 mtype
= modelbuilder
.resolve_mtype
(mmodule
, mclassdef
, nexpr
.n_type
)
880 else if nexpr
isa AIntExpr then
881 var cla
= modelbuilder
.try_get_mclass_by_name
(nexpr
, mmodule
, "Int")
882 if cla
!= null then mtype
= cla
.mclass_type
883 else if nexpr
isa AFloatExpr then
884 var cla
= modelbuilder
.try_get_mclass_by_name
(nexpr
, mmodule
, "Float")
885 if cla
!= null then mtype
= cla
.mclass_type
886 else if nexpr
isa ACharExpr then
887 var cla
= modelbuilder
.try_get_mclass_by_name
(nexpr
, mmodule
, "Char")
888 if cla
!= null then mtype
= cla
.mclass_type
889 else if nexpr
isa ABoolExpr then
890 var cla
= modelbuilder
.try_get_mclass_by_name
(nexpr
, mmodule
, "Bool")
891 if cla
!= null then mtype
= cla
.mclass_type
892 else if nexpr
isa ASuperstringExpr then
893 var cla
= modelbuilder
.try_get_mclass_by_name
(nexpr
, mmodule
, "String")
894 if cla
!= null then mtype
= cla
.mclass_type
895 else if nexpr
isa AStringFormExpr then
896 var cla
= modelbuilder
.try_get_mclass_by_name
(nexpr
, mmodule
, "String")
897 if cla
!= null then mtype
= cla
.mclass_type
899 modelbuilder
.error
(self, "Error: Untyped attribute {mpropdef}. Implicit typing allowed only for literals and new.")
902 if mtype
== null then return
904 else if ntype
!= null then
905 if nexpr
isa ANewExpr then
906 var xmtype
= modelbuilder
.resolve_mtype
(mmodule
, mclassdef
, nexpr
.n_type
)
907 if xmtype
== mtype
then
908 modelbuilder
.advice
(ntype
, "useless-type", "Warning: useless type definition")
913 if mtype
== null then
914 modelbuilder
.error
(self, "Error: Untyped attribute {mpropdef}")
918 mpropdef
.static_mtype
= mtype
920 if mreadpropdef
!= null then
921 var msignature
= new MSignature(new Array[MParameter], mtype
)
922 mreadpropdef
.msignature
= msignature
925 var mwritepropdef
= self.mwritepropdef
926 if mwritepropdef
!= null then
929 var mparameter
= new MParameter(name
, mtype
, false)
930 var msignature
= new MSignature([mparameter
], null)
931 mwritepropdef
.msignature
= msignature
934 var mlazypropdef
= self.mlazypropdef
935 if mlazypropdef
!= null then
936 mlazypropdef
.static_mtype
= modelbuilder
.model
.get_mclasses_by_name
("Bool").first
.mclass_type
940 redef fun check_signature
(modelbuilder
)
942 var mpropdef
= self.mpropdef
943 if mpropdef
== null then return # Error thus skiped
944 var mclassdef
= mpropdef
.mclassdef
945 var mmodule
= mclassdef
.mmodule
946 var ntype
= self.n_type
947 var mtype
= self.mpropdef
.static_mtype
948 if mtype
== null then return # Error thus skiped
950 # Lookup for signature in the precursor
951 # FIXME all precursors should be considered
952 if not mpropdef
.is_intro
then
953 var precursor_type
= mpropdef
.mproperty
.intro
.static_mtype
954 if precursor_type
== null then return
956 if mtype
!= precursor_type
then
957 modelbuilder
.error
(ntype
.as(not null), "Redef Error: Wrong static type. found {mtype}, expected {precursor_type}.")
962 # Check getter and setter
963 var meth
= self.mreadpropdef
965 self.check_method_signature
(modelbuilder
, meth
)
966 var node
: nullable ANode = ntype
967 if node
== null then node
= self
968 modelbuilder
.check_visibility
(node
, mtype
, meth
)
970 meth
= self.mwritepropdef
972 self.check_method_signature
(modelbuilder
, meth
)
973 var node
: nullable ANode = ntype
974 if node
== null then node
= self
975 modelbuilder
.check_visibility
(node
, mtype
, meth
)
979 private fun check_method_signature
(modelbuilder
: ModelBuilder, mpropdef
: MMethodDef)
981 var mclassdef
= mpropdef
.mclassdef
982 var mmodule
= mclassdef
.mmodule
983 var nsig
= self.n_type
984 var mysignature
= mpropdef
.msignature
985 if mysignature
== null then return # Error thus skiped
987 # Lookup for signature in the precursor
988 # FIXME all precursors should be considered
989 if not mpropdef
.is_intro
then
990 var msignature
= mpropdef
.mproperty
.intro
.msignature
991 if msignature
== null then return
993 if mysignature
.arity
!= msignature
.arity
then
995 if nsig
!= null then node
= nsig
else node
= self
996 modelbuilder
.error
(node
, "Redef Error: {mysignature.arity} parameters found, {msignature.arity} expected. Signature is {mpropdef}{msignature}")
999 var precursor_ret_type
= msignature
.return_mtype
1000 var ret_type
= mysignature
.return_mtype
1001 if ret_type
!= null and precursor_ret_type
== null then
1003 if nsig
!= null then node
= nsig
else node
= self
1004 modelbuilder
.error
(node
, "Redef Error: {mpropdef.mproperty} is a procedure, not a function.")
1008 if mysignature
.arity
> 0 then
1009 # Check parameters types
1010 for i
in [0..mysignature
.arity
[ do
1011 var myt
= mysignature
.mparameters
[i
].mtype
1012 var prt
= msignature
.mparameters
[i
].mtype
1013 if not myt
.is_subtype
(mmodule
, mclassdef
.bound_mtype
, prt
) or
1014 not prt
.is_subtype
(mmodule
, mclassdef
.bound_mtype
, myt
) then
1016 if nsig
!= null then node
= nsig
else node
= self
1017 modelbuilder
.error
(node
, "Redef Error: Wrong type for parameter `{mysignature.mparameters[i].name}'. found {myt}, expected {prt}.")
1021 if precursor_ret_type
!= null then
1022 if ret_type
== null then
1023 # Inherit the return type
1024 ret_type
= precursor_ret_type
1025 else if not ret_type
.is_subtype
(mmodule
, mclassdef
.bound_mtype
, precursor_ret_type
) then
1027 if nsig
!= null then node
= nsig
else node
= self
1028 modelbuilder
.error
(node
, "Redef Error: Wrong return type. found {ret_type}, expected {precursor_ret_type}.")
1035 redef class ATypePropdef
1036 redef type MPROPDEF: MVirtualTypeDef
1038 redef fun build_property
(modelbuilder
, mclassdef
)
1040 var name
= self.n_id
.text
1041 var mprop
= modelbuilder
.try_get_mproperty_by_name
(self.n_id
, mclassdef
, name
)
1042 if mprop
== null then
1043 var mvisibility
= new_property_visibility
(modelbuilder
, mclassdef
, self.n_visibility
)
1044 mprop
= new MVirtualTypeProp(mclassdef
, name
, mvisibility
)
1045 for c
in name
.chars
do if c
>= 'a' and c
<= 'z' then
1046 modelbuilder
.warning
(n_id
, "bad-type-name", "Warning: lowercase in the virtual type {name}")
1049 if not self.check_redef_keyword
(modelbuilder
, mclassdef
, self.n_kwredef
, false, mprop
) then return
1051 if not self.check_redef_keyword
(modelbuilder
, mclassdef
, self.n_kwredef
, true, mprop
) then return
1052 assert mprop
isa MVirtualTypeProp
1053 check_redef_property_visibility
(modelbuilder
, self.n_visibility
, mprop
)
1055 mclassdef
.mprop2npropdef
[mprop
] = self
1057 var mpropdef
= new MVirtualTypeDef(mclassdef
, mprop
, self.location
)
1058 self.mpropdef
= mpropdef
1059 modelbuilder
.mpropdef2npropdef
[mpropdef
] = self
1060 set_doc
(mpropdef
, modelbuilder
)
1062 var atfixed
= get_single_annotation
("fixed", modelbuilder
)
1063 if atfixed
!= null then
1064 mpropdef
.is_fixed
= true
1068 redef fun build_signature
(modelbuilder
)
1070 var mpropdef
= self.mpropdef
1071 if mpropdef
== null then return # Error thus skiped
1072 var mclassdef
= mpropdef
.mclassdef
1073 var mmodule
= mclassdef
.mmodule
1074 var mtype
: nullable MType = null
1076 var ntype
= self.n_type
1077 mtype
= modelbuilder
.resolve_mtype
(mmodule
, mclassdef
, ntype
)
1078 if mtype
== null then return
1080 mpropdef
.bound
= mtype
1081 # print "{mpropdef}: {mtype}"
1084 redef fun check_signature
(modelbuilder
)
1086 var mpropdef
= self.mpropdef
1087 if mpropdef
== null then return # Error thus skiped
1089 var bound
= self.mpropdef
.bound
1090 if bound
== null then return # Error thus skiped
1092 modelbuilder
.check_visibility
(n_type
, bound
, mpropdef
)
1094 var mclassdef
= mpropdef
.mclassdef
1095 var mmodule
= mclassdef
.mmodule
1096 var anchor
= mclassdef
.bound_mtype
1099 if bound
isa MVirtualType then
1100 # Slow case: progress on each resolution until: (i) we loop, or (ii) we found a non formal type
1101 var seen
= [self.mpropdef
.mproperty
.mvirtualtype
]
1103 if seen
.has
(bound
) then
1105 modelbuilder
.error
(self, "Error: circularity of virtual type definition: {seen.join(" -> ")}")
1109 var next
= bound
.lookup_bound
(mmodule
, anchor
)
1110 if not next
isa MVirtualType then break
1115 # Check redefinitions
1116 bound
= mpropdef
.bound
.as(not null)
1117 for p
in mpropdef
.mproperty
.lookup_super_definitions
(mmodule
, anchor
) do
1118 var supbound
= p
.bound
.as(not null)
1120 modelbuilder
.error
(self, "Redef Error: Virtual type {mpropdef.mproperty} is fixed in super-class {p.mclassdef.mclass}")
1123 if p
.mclassdef
.mclass
== mclassdef
.mclass
then
1124 # Still a warning to pass existing bad code
1125 modelbuilder
.warning
(n_type
, "refine-type", "Redef Error: a virtual type cannot be refined.")
1128 if not bound
.is_subtype
(mmodule
, anchor
, supbound
) then
1129 modelbuilder
.error
(n_type
, "Redef Error: Wrong bound type. Found {bound}, expected a subtype of {supbound}, as in {p}.")