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
22 redef class ToolContext
23 var modelize_property_phase
: Phase = new ModelizePropertyPhase(self, [modelize_class_phase
])
26 private class ModelizePropertyPhase
28 redef fun process_nmodule
(nmodule
)
30 for nclassdef
in nmodule
.n_classdefs
do
31 toolcontext
.modelbuilder
.build_properties
(nclassdef
)
36 redef class ModelBuilder
37 # Register the npropdef associated to each mpropdef
38 # FIXME: why not refine the `MPropDef` class with a nullable attribute?
39 var mpropdef2npropdef
: HashMap[MPropDef, APropdef] = new HashMap[MPropDef, APropdef]
41 # Build the properties of `nclassdef`.
42 # REQUIRE: all superclasses are built.
43 private fun build_properties
(nclassdef
: AClassdef)
45 # Force building recursively
46 if nclassdef
.build_properties_is_done
then return
47 nclassdef
.build_properties_is_done
= true
48 var mclassdef
= nclassdef
.mclassdef
.as(not null)
49 if mclassdef
.in_hierarchy
== null then return # Skip error
50 for superclassdef
in mclassdef
.in_hierarchy
.direct_greaters
do
51 build_properties
(mclassdef2nclassdef
[superclassdef
])
54 for npropdef
in nclassdef
.n_propdefs
do
55 npropdef
.build_property
(self, nclassdef
)
57 for npropdef
in nclassdef
.n_propdefs
do
58 npropdef
.build_signature
(self)
60 for npropdef
in nclassdef
.n_propdefs
do
61 npropdef
.check_signature
(self)
63 process_default_constructors
(nclassdef
)
66 # Introduce or inherit default constructor
67 # This is the last part of `build_properties`.
68 private fun process_default_constructors
(nclassdef
: AClassdef)
70 var mclassdef
= nclassdef
.mclassdef
.as(not null)
73 if not mclassdef
.is_intro
then return
75 # Is the class forbid constructors?
76 if not mclassdef
.mclass
.kind
.need_init
then return
78 # Is there already a constructor defined?
79 for mpropdef
in mclassdef
.mpropdefs
do
80 if not mpropdef
isa MMethodDef then continue
81 if mpropdef
.mproperty
.is_init
then return
84 if not nclassdef
isa AStdClassdef then return
86 var mmodule
= mclassdef
.mmodule
87 # Do we inherit for a constructor?
88 var combine
= new Array[MMethod]
89 var inhc
: nullable MClass = null
90 for st
in mclassdef
.supertypes
do
92 if not c
.kind
.need_init
then continue
93 st
= st
.anchor_to
(mmodule
, mclassdef
.bound_mtype
)
94 var candidate
= self.try_get_mproperty_by_name2
(nclassdef
, mmodule
, st
, "init").as(nullable MMethod)
95 if candidate
!= null and candidate
.intro
.msignature
.arity
== 0 then
96 combine
.add
(candidate
)
99 var inhc2
= c
.inherit_init_from
100 if inhc2
== null then inhc2
= c
101 if inhc2
== inhc
then continue
103 self.error
(nclassdef
, "Error: Cannot provide a defaut constructor: conflict for {inhc} and {c}")
109 # Collect undefined attributes
110 var mparameters
= new Array[MParameter]
111 var anode
: nullable ANode = null
112 for npropdef
in nclassdef
.n_propdefs
do
113 if npropdef
isa AAttrPropdef and npropdef
.n_expr
== null then
114 if npropdef
.mpropdef
== null then return # Skip broken attribute
115 var paramname
= npropdef
.mpropdef
.mproperty
.name
.substring_from
(1)
116 var ret_type
= npropdef
.mpropdef
.static_mtype
117 if ret_type
== null then return
118 var mparameter
= new MParameter(paramname
, ret_type
, false)
119 mparameters
.add
(mparameter
)
120 if anode
== null then anode
= npropdef
123 if anode
== null then anode
= nclassdef
125 if combine
.is_empty
and inhc
!= null then
126 if not mparameters
.is_empty
then
127 self.error
(anode
,"Error: {mclassdef} cannot inherit constructors from {inhc} because there is attributes without initial values: {mparameters.join(", ")}")
131 # TODO: actively inherit the consturctor
132 self.toolcontext
.info
("{mclassdef} inherits all constructors from {inhc}", 3)
133 mclassdef
.mclass
.inherit_init_from
= inhc
137 if not combine
.is_empty
and inhc
!= null then
138 self.error
(nclassdef
, "Error: Cannot provide a defaut constructor: conflict for {combine.join(", ")} and {inhc}")
142 if not combine
.is_empty
then
143 if mparameters
.is_empty
and combine
.length
== 1 then
144 # No need to create a local init, the inherited one is enough
145 inhc
= combine
.first
.intro_mclassdef
.mclass
146 mclassdef
.mclass
.inherit_init_from
= inhc
147 self.toolcontext
.info
("{mclassdef} inherits all constructors from {inhc}", 3)
150 nclassdef
.super_inits
= combine
153 var mprop
= new MMethod(mclassdef
, "init", mclassdef
.mclass
.visibility
)
154 var mpropdef
= new MMethodDef(mclassdef
, mprop
, nclassdef
.location
)
155 var msignature
= new MSignature(mparameters
, null)
156 mpropdef
.msignature
= msignature
158 nclassdef
.mfree_init
= mpropdef
159 self.toolcontext
.info
("{mclassdef} gets a free constructor for attributes {mpropdef}{msignature}", 3)
162 # Check the visibility of `mtype` as an element of the signature of `mpropdef`.
163 fun check_visibility
(node
: ANode, mtype
: MType, mpropdef
: MPropDef)
165 var mmodule
= mpropdef
.mclassdef
.mmodule
166 var mproperty
= mpropdef
.mproperty
168 # Extract visibility information of the main part of `mtype`
169 # It is a case-by case
170 var vis_type
: nullable MVisibility = null # The own visibility of the type
171 var mmodule_type
: nullable MModule = null # The origial module of the type
172 if mtype
isa MNullableType then mtype
= mtype
.mtype
173 if mtype
isa MClassType then
174 vis_type
= mtype
.mclass
.visibility
175 mmodule_type
= mtype
.mclass
.intro
.mmodule
176 else if mtype
isa MVirtualType then
177 vis_type
= mtype
.mproperty
.visibility
178 mmodule_type
= mtype
.mproperty
.intro_mclassdef
.mmodule
179 else if mtype
isa MParameterType then
180 # nothing, always visible
182 node
.debug
"Unexpected type {mtype}"
186 if vis_type
!= null then
187 assert mmodule_type
!= null
188 var vis_module_type
= mmodule
.visibility_for
(mmodule_type
) # the visibility of the original module
189 if mproperty
.visibility
> vis_type
then
190 error
(node
, "Error: The {mproperty.visibility} property `{mproperty}` cannot contain the {vis_type} type `{mtype}`")
192 else if mproperty
.visibility
> vis_module_type
then
193 error
(node
, "Error: The {mproperty.visibility} property `{mproperty}` cannot contain the type `{mtype}` from the {vis_module_type} module `{mmodule_type}`")
198 # No error, try to go deeper in generic types
199 if node
isa AType then
200 for a
in node
.n_types
do check_visibility
(a
, a
.mtype
.as(not null), mpropdef
)
201 else if mtype
isa MGenericType then
202 for t
in mtype
.arguments
do check_visibility
(node
, t
, mpropdef
)
208 # The class whose self inherit all the constructors.
209 # FIXME: this is needed to implement the crazy constructor mixin thing of the of old compiler. We need to think what to do with since this cannot stay in the modelbuilder
210 var inherit_init_from
: nullable MClass = null
213 redef class MClassDef
214 private var propdef_names
= new HashSet[String]
218 # Does the MPropDef contains a call to super or a call of a super-constructor?
219 # Subsequent phases of the frontend (esp. typing) set it if required
220 var has_supercall
: Bool writable = false
223 redef class AClassdef
224 var build_properties_is_done
: Bool = false
225 # The list of super-constructor to call at the start of the free constructor
226 # FIXME: this is needed to implement the crazy constructor thing of the of old compiler. We need to think what to do with since this cannot stay in the modelbuilder
227 var super_inits
: nullable Collection[MMethod] = null
229 # The free init (implicitely constructed by the class if required)
230 var mfree_init
: nullable MMethodDef = null
232 # What is the `APropdef` associated to a `MProperty`?
233 # Used to check multiple definition of a property.
234 var mprop2npropdef
: Map[MProperty, APropdef] = new HashMap[MProperty, APropdef]
238 # Join the text of all tokens
239 # Used to get the 'real name' of method definitions.
240 fun collect_text
: String
242 var v
= new TextCollectorVisitor
249 private class TextCollectorVisitor
251 var text
: String = ""
254 if n
isa Token then text
+= n
.text
260 # The associated main model entity
261 type MPROPDEF: MPropDef
263 # The associated propdef once build by a `ModelBuilder`
264 var mpropdef
: nullable MPROPDEF writable
266 private fun build_property
(modelbuilder
: ModelBuilder, nclassdef
: AClassdef) is abstract
267 private fun build_signature
(modelbuilder
: ModelBuilder) is abstract
268 private fun check_signature
(modelbuilder
: ModelBuilder) is abstract
269 private fun new_property_visibility
(modelbuilder
: ModelBuilder, mclassdef
: MClassDef, nvisibility
: nullable AVisibility): MVisibility
271 var mvisibility
= public_visibility
272 if nvisibility
!= null then
273 mvisibility
= nvisibility
.mvisibility
274 if mvisibility
== intrude_visibility
then
275 modelbuilder
.error
(nvisibility
, "Error: intrude is not a legal visibility for properties.")
276 mvisibility
= public_visibility
279 if mclassdef
.mclass
.visibility
== private_visibility
then
280 if mvisibility
== protected_visibility
then
281 assert nvisibility
!= null
282 modelbuilder
.error
(nvisibility
, "Error: The only legal visibility for properties in a private class is private.")
283 else if mvisibility
== private_visibility
then
284 assert nvisibility
!= null
286 # modelbuilder.warning(nvisibility, "Warning: private is unrequired since the only legal visibility for properties in a private class is private.")
288 mvisibility
= private_visibility
293 private fun set_doc
(mpropdef
: MPropDef)
295 var ndoc
= self.n_doc
296 if ndoc
!= null then mpropdef
.mdoc
= ndoc
.to_mdoc
299 private fun check_redef_property_visibility
(modelbuilder
: ModelBuilder, nvisibility
: nullable AVisibility, mprop
: MProperty)
301 if nvisibility
== null then return
302 var mvisibility
= nvisibility
.mvisibility
303 if mvisibility
!= mprop
.visibility
and mvisibility
!= public_visibility
then
304 modelbuilder
.error
(nvisibility
, "Error: redefinition changed the visibility from a {mprop.visibility} to a {mvisibility}")
308 private fun check_redef_keyword
(modelbuilder
: ModelBuilder, nclassdef
: AClassdef, kwredef
: nullable Token, need_redef
: Bool, mprop
: MProperty): Bool
310 if nclassdef
.mprop2npropdef
.has_key
(mprop
) then
311 modelbuilder
.error
(self, "Error: A property {mprop} is already defined in class {nclassdef.mclassdef.mclass}.")
314 if kwredef
== null then
316 modelbuilder
.error
(self, "Redef error: {nclassdef.mclassdef.mclass}::{mprop.name} is an inherited property. To redefine it, add the redef keyword.")
320 if not need_redef
then
321 modelbuilder
.error
(self, "Error: No property {nclassdef.mclassdef.mclass}::{mprop.name} is inherited. Remove the redef keyword to define a new property.")
330 redef class ASignature
331 # Is the model builder has correctly visited the signature
332 var is_visited
= false
333 # Names of parameters from the AST
334 # REQUIRE: is_visited
335 var param_names
= new Array[String]
336 # Types of parameters from the AST
337 # REQUIRE: is_visited
338 var param_types
= new Array[MType]
339 # Rank of the vararg (of -1 if none)
340 # REQUIRE: is_visited
341 var vararg_rank
: Int = -1
343 var ret_type
: nullable MType = null
345 # Visit and fill information about a signature
346 private fun visit_signature
(modelbuilder
: ModelBuilder, mclassdef
: MClassDef): Bool
348 var mmodule
= mclassdef
.mmodule
349 var param_names
= self.param_names
350 var param_types
= self.param_types
351 for np
in self.n_params
do
352 param_names
.add
(np
.n_id
.text
)
353 var ntype
= np
.n_type
354 if ntype
!= null then
355 var mtype
= modelbuilder
.resolve_mtype
(mmodule
, mclassdef
, ntype
)
356 if mtype
== null then return false # Skip error
357 for i
in [0..param_names
.length-param_types
.length
[ do
358 param_types
.add
(mtype
)
360 if np
.n_dotdotdot
!= null then
361 if self.vararg_rank
!= -1 then
362 modelbuilder
.error
(np
, "Error: {param_names[self.vararg_rank]} is already a vararg")
365 self.vararg_rank
= param_names
.length
- 1
370 var ntype
= self.n_type
371 if ntype
!= null then
372 self.ret_type
= modelbuilder
.resolve_mtype
(mmodule
, mclassdef
, ntype
)
373 if self.ret_type
== null then return false # Skip errir
376 self.is_visited
= true
380 # Build a visited signature
381 fun build_signature
(modelbuilder
: ModelBuilder): nullable MSignature
383 if param_names
.length
!= param_types
.length
then
384 # Some parameters are typed, other parameters are not typed.
385 modelbuilder
.error
(self.n_params
[param_types
.length
], "Error: Untyped parameter `{param_names[param_types.length]}'.")
389 var mparameters
= new Array[MParameter]
390 for i
in [0..param_names
.length
[ do
391 var mparameter
= new MParameter(param_names
[i
], param_types
[i
], i
== vararg_rank
)
392 self.n_params
[i
].mparameter
= mparameter
393 mparameters
.add
(mparameter
)
396 var msignature
= new MSignature(mparameters
, ret_type
)
402 # The associated mparameter if any
403 var mparameter
: nullable MParameter = null
406 redef class AMethPropdef
407 redef type MPROPDEF: MMethodDef
409 redef fun build_property
(modelbuilder
, nclassdef
)
411 var n_kwinit
= n_kwinit
412 var n_kwnew
= n_kwnew
413 var is_init
= n_kwinit
!= null or n_kwnew
!= null
414 var mclassdef
= nclassdef
.mclassdef
.as(not null)
416 var amethodid
= self.n_methid
418 if amethodid
== null then
422 else if n_kwinit
!= null then
425 else if n_kwnew
!= null then
431 else if amethodid
isa AIdMethid then
432 name
= amethodid
.n_id
.text
433 name_node
= amethodid
435 # operator, bracket or assign
436 name
= amethodid
.collect_text
437 name_node
= amethodid
439 if name
== "-" and self.n_signature
.n_params
.length
== 0 then
444 var mprop
: nullable MMethod = null
445 if not is_init
or n_kwredef
!= null then mprop
= modelbuilder
.try_get_mproperty_by_name
(name_node
, mclassdef
, name
).as(nullable MMethod)
446 if mprop
== null then
447 var mvisibility
= new_property_visibility
(modelbuilder
, mclassdef
, self.n_visibility
)
448 mprop
= new MMethod(mclassdef
, name
, mvisibility
)
449 mprop
.is_init
= is_init
450 mprop
.is_new
= n_kwnew
!= null
451 if not self.check_redef_keyword
(modelbuilder
, nclassdef
, n_kwredef
, false, mprop
) then return
453 if n_kwredef
== null then
454 if self isa AMainMethPropdef then
457 if not self.check_redef_keyword
(modelbuilder
, nclassdef
, n_kwredef
, true, mprop
) then return
460 check_redef_property_visibility
(modelbuilder
, self.n_visibility
, mprop
)
462 nclassdef
.mprop2npropdef
[mprop
] = self
464 var mpropdef
= new MMethodDef(mclassdef
, mprop
, self.location
)
466 if mclassdef
.propdef_names
.has
(mprop
.name
) then
467 var loc
: nullable Location = null
468 for i
in mclassdef
.mpropdefs
do
469 if i
.mproperty
.name
== mprop
.name
then
474 if loc
== null then abort
475 modelbuilder
.error
(self, "Error: a property {mprop} is already defined in class {mclassdef.mclass} at {loc}")
478 mclassdef
.propdef_names
.add
(mpropdef
.mproperty
.name
)
482 self.mpropdef
= mpropdef
483 modelbuilder
.mpropdef2npropdef
[mpropdef
] = self
484 if mpropdef
.is_intro
then
485 modelbuilder
.toolcontext
.info
("{mpropdef} introduces new method {mprop.full_name}", 3)
487 modelbuilder
.toolcontext
.info
("{mpropdef} redefines method {mprop.full_name}", 3)
491 redef fun build_signature
(modelbuilder
)
493 var mpropdef
= self.mpropdef
494 if mpropdef
== null then return # Error thus skiped
495 var mclassdef
= mpropdef
.mclassdef
496 var mmodule
= mclassdef
.mmodule
497 var nsig
= self.n_signature
499 # Retrieve info from the signature AST
500 var param_names
= new Array[String] # Names of parameters from the AST
501 var param_types
= new Array[MType] # Types of parameters from the AST
503 var ret_type
: nullable MType = null # Return type from the AST
505 if not nsig
.visit_signature
(modelbuilder
, mclassdef
) then return
506 param_names
= nsig
.param_names
507 param_types
= nsig
.param_types
508 vararg_rank
= nsig
.vararg_rank
509 ret_type
= nsig
.ret_type
512 # Look for some signature to inherit
513 # FIXME: do not inherit from the intro, but from the most specific
514 var msignature
: nullable MSignature = null
515 if not mpropdef
.is_intro
then
516 msignature
= mpropdef
.mproperty
.intro
.msignature
517 if msignature
== null then return # Skip error
519 # Check inherited signature arity
520 if param_names
.length
!= msignature
.arity
then
522 if nsig
!= null then node
= nsig
else node
= self
523 modelbuilder
.error
(node
, "Redef error: {mpropdef} redefines {mpropdef.mproperty.intro} with {param_names.length} parameter(s), {msignature.arity} expected. Signature is {mpropdef}{msignature}")
526 else if mpropdef
.mproperty
.is_init
then
527 # FIXME UGLY: inherit signature from a super-constructor
528 for msupertype
in mclassdef
.supertypes
do
529 msupertype
= msupertype
.anchor_to
(mmodule
, mclassdef
.bound_mtype
)
530 var candidate
= modelbuilder
.try_get_mproperty_by_name2
(self, mmodule
, msupertype
, mpropdef
.mproperty
.name
)
531 if candidate
!= null then
532 if msignature
== null then
533 msignature
= candidate
.intro
.as(MMethodDef).msignature
540 # Inherit the signature
541 if msignature
!= null and param_names
.length
!= param_types
.length
and param_names
.length
== msignature
.arity
and param_types
.length
== 0 then
542 # Parameters are untyped, thus inherit them
543 param_types
= new Array[MType]
544 for mparameter
in msignature
.mparameters
do
545 param_types
.add
(mparameter
.mtype
)
547 vararg_rank
= msignature
.vararg_rank
549 if msignature
!= null and ret_type
== null then
550 ret_type
= msignature
.return_mtype
553 if param_names
.length
!= param_types
.length
then
554 # Some parameters are typed, other parameters are not typed.
555 modelbuilder
.error
(nsig
.n_params
[param_types
.length
], "Error: Untyped parameter `{param_names[param_types.length]}'.")
559 var mparameters
= new Array[MParameter]
560 for i
in [0..param_names
.length
[ do
561 var mparameter
= new MParameter(param_names
[i
], param_types
[i
], i
== vararg_rank
)
562 if nsig
!= null then nsig
.n_params
[i
].mparameter
= mparameter
563 mparameters
.add
(mparameter
)
566 msignature
= new MSignature(mparameters
, ret_type
)
567 mpropdef
.msignature
= msignature
568 mpropdef
.is_abstract
= self isa ADeferredMethPropdef
569 mpropdef
.is_intern
= self isa AInternMethPropdef
570 mpropdef
.is_extern
= self isa AExternPropdef
573 redef fun check_signature
(modelbuilder
)
575 var mpropdef
= self.mpropdef
576 if mpropdef
== null then return # Error thus skiped
577 var mclassdef
= mpropdef
.mclassdef
578 var mmodule
= mclassdef
.mmodule
579 var nsig
= self.n_signature
580 var mysignature
= self.mpropdef
.msignature
581 if mysignature
== null then return # Error thus skiped
583 # Lookup for signature in the precursor
584 # FIXME all precursors should be considered
585 if not mpropdef
.is_intro
then
586 var msignature
= mpropdef
.mproperty
.intro
.msignature
587 if msignature
== null then return
589 var precursor_ret_type
= msignature
.return_mtype
590 var ret_type
= mysignature
.return_mtype
591 if ret_type
!= null and precursor_ret_type
== null then
592 modelbuilder
.error
(nsig
.n_type
.as(not null), "Redef Error: {mpropdef.mproperty} is a procedure, not a function.")
596 if mysignature
.arity
> 0 then
597 # Check parameters types
598 for i
in [0..mysignature
.arity
[ do
599 var myt
= mysignature
.mparameters
[i
].mtype
600 var prt
= msignature
.mparameters
[i
].mtype
601 if not myt
.is_subtype
(mmodule
, mclassdef
.bound_mtype
, prt
) or
602 not prt
.is_subtype
(mmodule
, mclassdef
.bound_mtype
, myt
) then
603 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}.")
607 if precursor_ret_type
!= null then
608 if ret_type
== null then
609 # Inherit the return type
610 ret_type
= precursor_ret_type
611 else if not ret_type
.is_subtype
(mmodule
, mclassdef
.bound_mtype
, precursor_ret_type
) then
612 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}.")
617 if mysignature
.arity
> 0 then
618 # Check parameters visibility
619 for i
in [0..mysignature
.arity
[ do
620 var nt
= nsig
.n_params
[i
].n_type
621 if nt
!= null then modelbuilder
.check_visibility
(nt
, nt
.mtype
.as(not null), mpropdef
)
624 if nt
!= null then modelbuilder
.check_visibility
(nt
, nt
.mtype
.as(not null), mpropdef
)
629 redef class AAttrPropdef
630 redef type MPROPDEF: MAttributeDef
632 # The associated getter (read accessor) if any
633 var mreadpropdef
: nullable MMethodDef writable
634 # The associated setter (write accessor) if any
635 var mwritepropdef
: nullable MMethodDef writable
636 redef fun build_property
(modelbuilder
, nclassdef
)
638 var mclassdef
= nclassdef
.mclassdef
.as(not null)
639 var mclass
= mclassdef
.mclass
642 if self.n_id
!= null then
643 name
= self.n_id
.text
645 name
= self.n_id2
.text
648 if mclass
.kind
== interface_kind
or mclassdef
.mclass
.kind
== enum_kind
then
649 modelbuilder
.error
(self, "Error: Attempt to define attribute {name} in the interface {mclass}.")
650 else if mclass
.kind
== enum_kind
then
651 modelbuilder
.error
(self, "Error: Attempt to define attribute {name} in the enum class {mclass}.")
652 else if mclass
.kind
== extern_kind
then
653 modelbuilder
.error
(self, "Error: Attempt to define attribute {name} in the extern class {mclass}.")
658 # Old attribute style
659 var mprop
= modelbuilder
.try_get_mproperty_by_name
(nid
, mclassdef
, name
)
660 if mprop
== null then
661 var mvisibility
= new_property_visibility
(modelbuilder
, mclassdef
, self.n_visibility
)
662 mprop
= new MAttribute(mclassdef
, name
, mvisibility
)
663 if not self.check_redef_keyword
(modelbuilder
, nclassdef
, self.n_kwredef
, false, mprop
) then return
665 assert mprop
isa MAttribute
666 check_redef_property_visibility
(modelbuilder
, self.n_visibility
, mprop
)
667 if not self.check_redef_keyword
(modelbuilder
, nclassdef
, self.n_kwredef
, true, mprop
) then return
669 nclassdef
.mprop2npropdef
[mprop
] = self
671 var mpropdef
= new MAttributeDef(mclassdef
, mprop
, self.location
)
672 self.mpropdef
= mpropdef
673 modelbuilder
.mpropdef2npropdef
[mpropdef
] = self
676 var nreadable
= self.n_readable
677 if nreadable
!= null then
678 var readname
= name
.substring_from
(1)
679 var mreadprop
= modelbuilder
.try_get_mproperty_by_name
(nid
, mclassdef
, readname
).as(nullable MMethod)
680 if mreadprop
== null then
681 var mvisibility
= new_property_visibility
(modelbuilder
, mclassdef
, nreadable
.n_visibility
)
682 mreadprop
= new MMethod(mclassdef
, readname
, mvisibility
)
683 if not self.check_redef_keyword
(modelbuilder
, nclassdef
, nreadable
.n_kwredef
, false, mreadprop
) then return
685 if not self.check_redef_keyword
(modelbuilder
, nclassdef
, nreadable
.n_kwredef
, true, mreadprop
) then return
686 check_redef_property_visibility
(modelbuilder
, nreadable
.n_visibility
, mreadprop
)
688 nclassdef
.mprop2npropdef
[mreadprop
] = self
690 var mreadpropdef
= new MMethodDef(mclassdef
, mreadprop
, self.location
)
691 self.mreadpropdef
= mreadpropdef
692 modelbuilder
.mpropdef2npropdef
[mreadpropdef
] = self
693 mreadpropdef
.mdoc
= mpropdef
.mdoc
696 var nwritable
= self.n_writable
697 if nwritable
!= null then
698 var writename
= name
.substring_from
(1) + "="
699 var mwriteprop
= modelbuilder
.try_get_mproperty_by_name
(nid
, mclassdef
, writename
).as(nullable MMethod)
700 if mwriteprop
== null then
701 var mvisibility
= new_property_visibility
(modelbuilder
, mclassdef
, nwritable
.n_visibility
)
702 mwriteprop
= new MMethod(mclassdef
, writename
, mvisibility
)
703 if not self.check_redef_keyword
(modelbuilder
, nclassdef
, nwritable
.n_kwredef
, false, mwriteprop
) then return
705 if not self.check_redef_keyword
(modelbuilder
, nclassdef
, nwritable
.n_kwredef
, true, mwriteprop
) then return
706 check_redef_property_visibility
(modelbuilder
, nwritable
.n_visibility
, mwriteprop
)
708 nclassdef
.mprop2npropdef
[mwriteprop
] = self
710 var mwritepropdef
= new MMethodDef(mclassdef
, mwriteprop
, self.location
)
711 self.mwritepropdef
= mwritepropdef
712 modelbuilder
.mpropdef2npropdef
[mwritepropdef
] = self
713 mwritepropdef
.mdoc
= mpropdef
.mdoc
716 # New attribute style
717 var nid2
= self.n_id2
.as(not null)
718 var mprop
= new MAttribute(mclassdef
, "@" + name
, none_visibility
)
719 var mpropdef
= new MAttributeDef(mclassdef
, mprop
, self.location
)
720 self.mpropdef
= mpropdef
721 modelbuilder
.mpropdef2npropdef
[mpropdef
] = self
725 var mreadprop
= modelbuilder
.try_get_mproperty_by_name
(nid2
, mclassdef
, readname
).as(nullable MMethod)
726 if mreadprop
== null then
727 var mvisibility
= new_property_visibility
(modelbuilder
, mclassdef
, self.n_visibility
)
728 mreadprop
= new MMethod(mclassdef
, readname
, mvisibility
)
729 if not self.check_redef_keyword
(modelbuilder
, nclassdef
, n_kwredef
, false, mreadprop
) then return
731 if not self.check_redef_keyword
(modelbuilder
, nclassdef
, n_kwredef
, true, mreadprop
) then return
732 check_redef_property_visibility
(modelbuilder
, self.n_visibility
, mreadprop
)
734 nclassdef
.mprop2npropdef
[mreadprop
] = self
736 var mreadpropdef
= new MMethodDef(mclassdef
, mreadprop
, self.location
)
737 self.mreadpropdef
= mreadpropdef
738 modelbuilder
.mpropdef2npropdef
[mreadpropdef
] = self
739 mreadpropdef
.mdoc
= mpropdef
.mdoc
741 var writename
= name
+ "="
742 var nwritable
= self.n_writable
743 var mwriteprop
= modelbuilder
.try_get_mproperty_by_name
(nid2
, mclassdef
, writename
).as(nullable MMethod)
744 var nwkwredef
: nullable Token = null
745 if nwritable
!= null then nwkwredef
= nwritable
.n_kwredef
746 if mwriteprop
== null then
748 if nwritable
!= null then
749 mvisibility
= new_property_visibility
(modelbuilder
, mclassdef
, nwritable
.n_visibility
)
751 mvisibility
= private_visibility
753 mwriteprop
= new MMethod(mclassdef
, writename
, mvisibility
)
754 if not self.check_redef_keyword
(modelbuilder
, nclassdef
, nwkwredef
, false, mwriteprop
) then return
756 if not self.check_redef_keyword
(modelbuilder
, nclassdef
, nwkwredef
, true, mwriteprop
) then return
757 if nwritable
!= null then
758 check_redef_property_visibility
(modelbuilder
, nwritable
.n_visibility
, mwriteprop
)
761 nclassdef
.mprop2npropdef
[mwriteprop
] = self
763 var mwritepropdef
= new MMethodDef(mclassdef
, mwriteprop
, self.location
)
764 self.mwritepropdef
= mwritepropdef
765 modelbuilder
.mpropdef2npropdef
[mwritepropdef
] = self
766 mwritepropdef
.mdoc
= mpropdef
.mdoc
770 redef fun build_signature
(modelbuilder
)
772 var mpropdef
= self.mpropdef
773 if mpropdef
== null then return # Error thus skiped
774 var mclassdef
= mpropdef
.mclassdef
775 var mmodule
= mclassdef
.mmodule
776 var mtype
: nullable MType = null
778 var ntype
= self.n_type
779 if ntype
!= null then
780 mtype
= modelbuilder
.resolve_mtype
(mmodule
, mclassdef
, ntype
)
781 if mtype
== null then return
784 var nexpr
= self.n_expr
785 if mtype
== null then
786 if nexpr
!= null then
787 if nexpr
isa ANewExpr then
788 mtype
= modelbuilder
.resolve_mtype
(mmodule
, mclassdef
, nexpr
.n_type
)
789 else if nexpr
isa AIntExpr then
790 var cla
= modelbuilder
.try_get_mclass_by_name
(nexpr
, mmodule
, "Int")
791 if cla
!= null then mtype
= cla
.mclass_type
792 else if nexpr
isa AFloatExpr then
793 var cla
= modelbuilder
.try_get_mclass_by_name
(nexpr
, mmodule
, "Float")
794 if cla
!= null then mtype
= cla
.mclass_type
795 else if nexpr
isa ACharExpr then
796 var cla
= modelbuilder
.try_get_mclass_by_name
(nexpr
, mmodule
, "Char")
797 if cla
!= null then mtype
= cla
.mclass_type
798 else if nexpr
isa ABoolExpr then
799 var cla
= modelbuilder
.try_get_mclass_by_name
(nexpr
, mmodule
, "Bool")
800 if cla
!= null then mtype
= cla
.mclass_type
801 else if nexpr
isa ASuperstringExpr then
802 var cla
= modelbuilder
.try_get_mclass_by_name
(nexpr
, mmodule
, "String")
803 if cla
!= null then mtype
= cla
.mclass_type
804 else if nexpr
isa AStringFormExpr then
805 var cla
= modelbuilder
.try_get_mclass_by_name
(nexpr
, mmodule
, "String")
806 if cla
!= null then mtype
= cla
.mclass_type
808 modelbuilder
.error
(self, "Error: Untyped attribute {mpropdef}. Implicit typing allowed only for literals and new.")
812 modelbuilder
.error
(self, "Error: Untyped attribute {mpropdef}")
816 if nexpr
isa ANewExpr then
817 var xmtype
= modelbuilder
.resolve_mtype
(mmodule
, mclassdef
, nexpr
.n_type
)
818 if xmtype
== mtype
and modelbuilder
.toolcontext
.opt_warn
.value
>= 2 then
819 modelbuilder
.warning
(ntype
, "Warning: useless type definition")
824 if mtype
== null then return
826 mpropdef
.static_mtype
= mtype
828 var mreadpropdef
= self.mreadpropdef
829 if mreadpropdef
!= null then
830 var msignature
= new MSignature(new Array[MParameter], mtype
)
831 mreadpropdef
.msignature
= msignature
834 var msritepropdef
= self.mwritepropdef
835 if mwritepropdef
!= null then
838 name
= n_id
.text
.substring_from
(1)
842 var mparameter
= new MParameter(name
, mtype
, false)
843 var msignature
= new MSignature([mparameter
], null)
844 mwritepropdef
.msignature
= msignature
848 redef fun check_signature
(modelbuilder
)
850 var mpropdef
= self.mpropdef
851 if mpropdef
== null then return # Error thus skiped
852 var mclassdef
= mpropdef
.mclassdef
853 var mmodule
= mclassdef
.mmodule
854 var ntype
= self.n_type
855 var mtype
= self.mpropdef
.static_mtype
856 if mtype
== null then return # Error thus skiped
858 # Lookup for signature in the precursor
859 # FIXME all precursors should be considered
860 if not mpropdef
.is_intro
then
861 var precursor_type
= mpropdef
.mproperty
.intro
.static_mtype
862 if precursor_type
== null then return
864 if mtype
!= precursor_type
then
865 modelbuilder
.error
(ntype
.as(not null), "Redef Error: Wrong static type. found {mtype}, expected {precursor_type}.")
870 # Check getter and setter
871 var meth
= self.mreadpropdef
873 self.check_method_signature
(modelbuilder
, meth
)
874 var node
: nullable ANode = ntype
875 if node
== null then node
= self
876 modelbuilder
.check_visibility
(node
, mtype
, meth
)
878 meth
= self.mwritepropdef
880 self.check_method_signature
(modelbuilder
, meth
)
881 var node
: nullable ANode = ntype
882 if node
== null then node
= self
883 modelbuilder
.check_visibility
(node
, mtype
, meth
)
887 private fun check_method_signature
(modelbuilder
: ModelBuilder, mpropdef
: MMethodDef)
889 var mclassdef
= mpropdef
.mclassdef
890 var mmodule
= mclassdef
.mmodule
891 var nsig
= self.n_type
892 var mysignature
= mpropdef
.msignature
893 if mysignature
== null then return # Error thus skiped
895 # Lookup for signature in the precursor
896 # FIXME all precursors should be considered
897 if not mpropdef
.is_intro
then
898 var msignature
= mpropdef
.mproperty
.intro
.msignature
899 if msignature
== null then return
901 if mysignature
.arity
!= msignature
.arity
then
903 if nsig
!= null then node
= nsig
else node
= self
904 modelbuilder
.error
(node
, "Redef Error: {mysignature.arity} parameters found, {msignature.arity} expected. Signature is {mpropdef}{msignature}")
907 var precursor_ret_type
= msignature
.return_mtype
908 var ret_type
= mysignature
.return_mtype
909 if ret_type
!= null and precursor_ret_type
== null then
911 if nsig
!= null then node
= nsig
else node
= self
912 modelbuilder
.error
(node
, "Redef Error: {mpropdef.mproperty} is a procedure, not a function.")
916 if mysignature
.arity
> 0 then
917 # Check parameters types
918 for i
in [0..mysignature
.arity
[ do
919 var myt
= mysignature
.mparameters
[i
].mtype
920 var prt
= msignature
.mparameters
[i
].mtype
921 if not myt
.is_subtype
(mmodule
, mclassdef
.bound_mtype
, prt
) and
922 not prt
.is_subtype
(mmodule
, mclassdef
.bound_mtype
, myt
) then
924 if nsig
!= null then node
= nsig
else node
= self
925 modelbuilder
.error
(node
, "Redef Error: Wrong type for parameter `{mysignature.mparameters[i].name}'. found {myt}, expected {prt}.")
929 if precursor_ret_type
!= null then
930 if ret_type
== null then
931 # Inherit the return type
932 ret_type
= precursor_ret_type
933 else if not ret_type
.is_subtype
(mmodule
, mclassdef
.bound_mtype
, precursor_ret_type
) then
935 if nsig
!= null then node
= nsig
else node
= self
936 modelbuilder
.error
(node
, "Redef Error: Wrong return type. found {ret_type}, expected {precursor_ret_type}.")
943 redef class ATypePropdef
944 redef type MPROPDEF: MVirtualTypeDef
946 redef fun build_property
(modelbuilder
, nclassdef
)
948 var mclassdef
= nclassdef
.mclassdef
.as(not null)
949 var name
= self.n_id
.text
950 var mprop
= modelbuilder
.try_get_mproperty_by_name
(self.n_id
, mclassdef
, name
)
951 if mprop
== null then
952 var mvisibility
= new_property_visibility
(modelbuilder
, mclassdef
, self.n_visibility
)
953 mprop
= new MVirtualTypeProp(mclassdef
, name
, mvisibility
)
954 for c
in name
.chars
do if c
>= 'a' and c
<= 'z' then
955 modelbuilder
.warning
(n_id
, "Warning: lowercase in the virtual type {name}")
958 if not self.check_redef_keyword
(modelbuilder
, nclassdef
, self.n_kwredef
, false, mprop
) then return
960 if not self.check_redef_keyword
(modelbuilder
, nclassdef
, self.n_kwredef
, true, mprop
) then return
961 assert mprop
isa MVirtualTypeProp
962 check_redef_property_visibility
(modelbuilder
, self.n_visibility
, mprop
)
964 nclassdef
.mprop2npropdef
[mprop
] = self
966 var mpropdef
= new MVirtualTypeDef(mclassdef
, mprop
, self.location
)
967 self.mpropdef
= mpropdef
968 modelbuilder
.mpropdef2npropdef
[mpropdef
] = self
972 redef fun build_signature
(modelbuilder
)
974 var mpropdef
= self.mpropdef
975 if mpropdef
== null then return # Error thus skiped
976 var mclassdef
= mpropdef
.mclassdef
977 var mmodule
= mclassdef
.mmodule
978 var mtype
: nullable MType = null
980 var ntype
= self.n_type
981 mtype
= modelbuilder
.resolve_mtype
(mmodule
, mclassdef
, ntype
)
982 if mtype
== null then return
984 mpropdef
.bound
= mtype
985 # print "{mpropdef}: {mtype}"
988 redef fun check_signature
(modelbuilder
)
990 var mpropdef
= self.mpropdef
991 if mpropdef
== null then return # Error thus skiped
993 var bound
= self.mpropdef
.bound
994 if bound
== null then return # Error thus skiped
996 modelbuilder
.check_visibility
(n_type
.as(not null), bound
, mpropdef
)
998 # Fast case: the bound is not a formal type
999 if not bound
isa MVirtualType then return
1001 var mclassdef
= mpropdef
.mclassdef
1002 var mmodule
= mclassdef
.mmodule
1003 var anchor
= mclassdef
.bound_mtype
1005 # Slow case: progress on each resolution until: (i) we loop, or (ii) we found a non formal type
1006 var seen
= [self.mpropdef
.mproperty
.mvirtualtype
]
1008 if seen
.has
(bound
) then
1010 modelbuilder
.error
(self, "Error: circularity of virtual type definition: {seen.join(" -> ")}")
1014 var next
= bound
.lookup_bound
(mmodule
, anchor
)
1015 if not next
isa MVirtualType then break