src: introduce new constructors
[nit.git] / src / modelize_property.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2012 Jean Privat <jean@pryen.org>
4 #
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
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16
17 # Analysis and verification of property definitions to instantiate model element
18 module modelize_property
19
20 import modelize_class
21 import annotation
22
23 redef class ToolContext
24 var modelize_property_phase: Phase = new ModelizePropertyPhase(self, [modelize_class_phase])
25 end
26
27 private class ModelizePropertyPhase
28 super Phase
29 redef fun process_nmodule(nmodule)
30 do
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)
34 end
35 end
36 end
37
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]
42
43 # Build the properties of `nclassdef`.
44 # REQUIRE: all superclasses are built.
45 private fun build_properties(nclassdef: AClassdef)
46 do
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])
55 end
56
57 for nclassdef2 in nclassdef.all_defs do
58 for npropdef in nclassdef2.n_propdefs do
59 npropdef.build_property(self, mclassdef)
60 end
61 for npropdef in nclassdef2.n_propdefs do
62 npropdef.build_signature(self)
63 end
64 for npropdef in nclassdef2.n_propdefs do
65 npropdef.check_signature(self)
66 end
67 end
68 process_default_constructors(nclassdef)
69 end
70
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
75
76 # Introduce or inherit default constructor
77 # This is the last part of `build_properties`.
78 private fun process_default_constructors(nclassdef: AClassdef)
79 do
80 var mclassdef = nclassdef.mclassdef.as(not null)
81
82 # Are we a refinement
83 if not mclassdef.is_intro then return
84
85 var mmodule = nclassdef.mclassdef.mmodule
86
87 # Look for the init in Object, or create it
88 if mclassdef.mclass.name == "Object" and the_root_init_mmethod == null then
89 # Create the implicit root-init method
90 var mprop = new MMethod(mclassdef, "init", mclassdef.mclass.visibility)
91 mprop.is_root_init = true
92 var mpropdef = new MMethodDef(mclassdef, mprop, nclassdef.location)
93 var mparameters = new Array[MParameter]
94 var msignature = new MSignature(mparameters, null)
95 mpropdef.msignature = msignature
96 mpropdef.new_msignature = msignature
97 mprop.is_init = true
98 nclassdef.mfree_init = mpropdef
99 self.toolcontext.info("{mclassdef} gets a free empty constructor {mpropdef}{msignature}", 3)
100 the_root_init_mmethod = mprop
101 return
102 end
103
104 # Is the class forbid constructors?
105 if not mclassdef.mclass.kind.need_init then return
106
107 # Is there already a constructor defined?
108 var defined_init: nullable MMethodDef = null
109 for mpropdef in mclassdef.mpropdefs do
110 if not mpropdef isa MMethodDef then continue
111 if not mpropdef.mproperty.is_init then continue
112 if mpropdef.mproperty.is_root_init then
113 assert defined_init == null
114 defined_init = mpropdef
115 else
116 # An explicit old-style init, so return
117 return
118 end
119 end
120
121 if not nclassdef isa AStdClassdef then return
122
123 # Do we inherit a old-style constructor?
124 var combine = new Array[MMethod] # old-style constructors without arguments
125 var inhc: nullable MClass = null # single super-class with a constructor with arguments
126 if defined_init == null then for st in mclassdef.supertypes do
127 var c = st.mclass
128 if not c.kind.need_init then continue
129 st = st.anchor_to(mmodule, mclassdef.bound_mtype)
130 var candidate = self.try_get_mproperty_by_name2(nclassdef, mmodule, st, "init").as(nullable MMethod)
131 if candidate != null then
132 if candidate.is_root_init then continue
133 if candidate.intro.msignature != null then
134 if candidate.intro.msignature.arity == 0 then
135 combine.add(candidate)
136 continue
137 end
138 end
139 end
140 var inhc2 = c.inherit_init_from
141 if inhc2 == null then inhc2 = c
142 if inhc2 == inhc then continue
143 if inhc != null then
144 self.error(nclassdef, "Error: Cannot provide a defaut constructor: conflict for {inhc} and {c}")
145 else
146 inhc = inhc2
147 end
148 end
149
150 # Collect undefined attributes
151 var mparameters = new Array[MParameter]
152 var initializers = new Array[MProperty]
153 var anode: nullable ANode = null
154 for npropdef in nclassdef.n_propdefs do
155 if npropdef isa AAttrPropdef then
156 if npropdef.mpropdef == null then return # Skip broken attribute
157 var at = npropdef.get_single_annotation("noinit", self)
158 if at != null then
159 npropdef.noinit = true
160 if npropdef.n_expr != null then
161 self.error(at, "Error: `noinit` attributes cannot have an initial value")
162 end
163 continue # Skip noinit attributes
164 end
165 if npropdef.n_expr != null then continue
166 var paramname = npropdef.mpropdef.mproperty.name.substring_from(1)
167 var ret_type = npropdef.mpropdef.static_mtype
168 if ret_type == null then return
169 var mparameter = new MParameter(paramname, ret_type, false)
170 mparameters.add(mparameter)
171 var msetter = npropdef.mwritepropdef
172 if msetter == null then
173 # No setter, it is a old-style attribute, so just add it
174 initializers.add(npropdef.mpropdef.mproperty)
175 else
176 # Add the setter to the list
177 initializers.add(msetter.mproperty)
178 end
179 if anode == null then anode = npropdef
180 end
181 end
182 if anode == null then anode = nclassdef
183
184 if combine.is_empty and inhc != null then
185 if not mparameters.is_empty then
186 self.error(anode,"Error: {mclassdef} cannot inherit constructors from {inhc} because there is attributes without initial values: {mparameters.join(", ")}")
187 return
188 end
189
190 # TODO: actively inherit the consturctor
191 self.toolcontext.info("{mclassdef} inherits all constructors from {inhc}", 3)
192 #mclassdef.mclass.inherit_init_from = inhc
193 #return
194 end
195
196 if not combine.is_empty and inhc != null then
197 self.error(nclassdef, "Error: Cannot provide a defaut constructor: conflict for {combine.join(", ")} and {inhc}")
198 return
199 end
200 if not combine.is_empty then
201 if mparameters.is_empty and combine.length == 1 then
202 # No need to create a local init, the inherited one is enough
203 inhc = combine.first.intro_mclassdef.mclass
204 mclassdef.mclass.inherit_init_from = inhc
205 self.toolcontext.info("{mclassdef} inherits all constructors from {inhc}", 3)
206 return
207 end
208 nclassdef.super_inits = combine
209 var mprop = new MMethod(mclassdef, "init", mclassdef.mclass.visibility)
210 var mpropdef = new MMethodDef(mclassdef, mprop, nclassdef.location)
211 var msignature = new MSignature(mparameters, null)
212 mpropdef.msignature = msignature
213 mprop.is_init = true
214 nclassdef.mfree_init = mpropdef
215 self.toolcontext.info("{mclassdef} gets a free empty constructor {mpropdef}{msignature}", 3)
216 return
217 end
218
219 if the_root_init_mmethod == null then return
220
221 # Look for nost-specific new-stype init definitions
222 var spropdefs = the_root_init_mmethod.lookup_super_definitions(mclassdef.mmodule, mclassdef.bound_mtype)
223 if spropdefs.is_empty then
224 toolcontext.fatal_error(nclassdef.location, "Fatal error: {mclassdef} does not specialize {the_root_init_mmethod.intro_mclassdef}. Possible duplication of the root class `Object`?")
225 end
226
227 # Search the longest-one and checks for conflict
228 var longest = spropdefs.first
229 if spropdefs.length > 1 then
230 # Check for conflict in the order of initializers
231 # Each initializer list must me a prefix of the longest list
232 # part 1. find the longest list
233 for spd in spropdefs do
234 if spd.initializers.length > longest.initializers.length then longest = spd
235 end
236 # part 2. compare
237 for spd in spropdefs do
238 var i = 0
239 for p in spd.initializers do
240 if p != longest.initializers[i] then
241 self.error(nclassdef, "Error: conflict for inherited inits {spd}({spd.initializers.join(", ")}) and {longest}({longest.initializers.join(", ")})")
242 return
243 end
244 i += 1
245 end
246 end
247 end
248
249 # Can we just inherit?
250 if spropdefs.length == 1 and mparameters.is_empty and defined_init == null then
251 self.toolcontext.info("{mclassdef} inherits the basic constructor {longest}", 3)
252 return
253 end
254
255 # Combine the inherited list to what is collected
256 if longest.initializers.length > 0 then
257 mparameters.prepend longest.new_msignature.mparameters
258 initializers.prepend longest.initializers
259 end
260
261 # If we already have a basic init definition, then setup its initializers
262 if defined_init != null then
263 defined_init.initializers.add_all(initializers)
264 var msignature = new MSignature(mparameters, null)
265 defined_init.new_msignature = msignature
266 self.toolcontext.info("{mclassdef} extends its basic constructor signature to {defined_init}{msignature}", 3)
267 return
268 end
269
270 # Else create the local implicit basic init definition
271 var mprop = the_root_init_mmethod.as(not null)
272 var mpropdef = new MMethodDef(mclassdef, mprop, nclassdef.location)
273 mpropdef.has_supercall = true
274 mpropdef.initializers.add_all(initializers)
275 var msignature = new MSignature(mparameters, null)
276 mpropdef.new_msignature = msignature
277 mpropdef.msignature = new MSignature(new Array[MParameter], null) # always an empty real signature
278 nclassdef.mfree_init = mpropdef
279 self.toolcontext.info("{mclassdef} gets a free constructor for attributes {mpropdef}{msignature}", 3)
280 end
281
282 # Check the visibility of `mtype` as an element of the signature of `mpropdef`.
283 fun check_visibility(node: ANode, mtype: MType, mpropdef: MPropDef)
284 do
285 var mmodule = mpropdef.mclassdef.mmodule
286 var mproperty = mpropdef.mproperty
287
288 # Extract visibility information of the main part of `mtype`
289 # It is a case-by case
290 var vis_type: nullable MVisibility = null # The own visibility of the type
291 var mmodule_type: nullable MModule = null # The origial module of the type
292 mtype = mtype.as_notnullable
293 if mtype isa MClassType then
294 vis_type = mtype.mclass.visibility
295 mmodule_type = mtype.mclass.intro.mmodule
296 else if mtype isa MVirtualType then
297 vis_type = mtype.mproperty.visibility
298 mmodule_type = mtype.mproperty.intro_mclassdef.mmodule
299 else if mtype isa MParameterType then
300 # nothing, always visible
301 else
302 node.debug "Unexpected type {mtype}"
303 abort
304 end
305
306 if vis_type != null then
307 assert mmodule_type != null
308 var vis_module_type = mmodule.visibility_for(mmodule_type) # the visibility of the original module
309 if mproperty.visibility > vis_type then
310 error(node, "Error: The {mproperty.visibility} property `{mproperty}` cannot contain the {vis_type} type `{mtype}`")
311 return
312 else if mproperty.visibility > vis_module_type then
313 error(node, "Error: The {mproperty.visibility} property `{mproperty}` cannot contain the type `{mtype}` from the {vis_module_type} module `{mmodule_type}`")
314 return
315 end
316 end
317
318 # No error, try to go deeper in generic types
319 if node isa AType then
320 for a in node.n_types do
321 var t = a.mtype
322 if t == null then continue # Error, thus skipped
323 check_visibility(a, t, mpropdef)
324 end
325 else if mtype isa MGenericType then
326 for t in mtype.arguments do check_visibility(node, t, mpropdef)
327 end
328 end
329 end
330
331 redef class MClass
332 # The class whose self inherit all the constructors.
333 # 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
334 var inherit_init_from: nullable MClass = null
335 end
336
337 redef class MPropDef
338 # Does the MPropDef contains a call to super or a call of a super-constructor?
339 # Subsequent phases of the frontend (esp. typing) set it if required
340 var has_supercall: Bool writable = false
341 end
342
343 redef class AClassdef
344 var build_properties_is_done: Bool = false
345 # The list of super-constructor to call at the start of the free constructor
346 # 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
347 var super_inits: nullable Collection[MMethod] = null
348
349 # The free init (implicitely constructed by the class if required)
350 var mfree_init: nullable MMethodDef = null
351 end
352
353 redef class MClassDef
354 # What is the `APropdef` associated to a `MProperty`?
355 # Used to check multiple definition of a property.
356 var mprop2npropdef: Map[MProperty, APropdef] = new HashMap[MProperty, APropdef]
357 end
358
359 redef class Prod
360 # Join the text of all tokens
361 # Used to get the 'real name' of method definitions.
362 fun collect_text: String
363 do
364 var v = new TextCollectorVisitor
365 v.enter_visit(self)
366 assert v.text != ""
367 return v.text
368 end
369 end
370
371 private class TextCollectorVisitor
372 super Visitor
373 var text: String = ""
374 redef fun visit(n)
375 do
376 if n isa Token then text += n.text
377 n.visit_all(self)
378 end
379 end
380
381 redef class APropdef
382 # The associated main model entity
383 type MPROPDEF: MPropDef
384
385 # The associated propdef once build by a `ModelBuilder`
386 var mpropdef: nullable MPROPDEF writable
387
388 private fun build_property(modelbuilder: ModelBuilder, mclassdef: MClassDef) is abstract
389 private fun build_signature(modelbuilder: ModelBuilder) is abstract
390 private fun check_signature(modelbuilder: ModelBuilder) is abstract
391 private fun new_property_visibility(modelbuilder: ModelBuilder, mclassdef: MClassDef, nvisibility: nullable AVisibility): MVisibility
392 do
393 var mvisibility = public_visibility
394 if nvisibility != null then
395 mvisibility = nvisibility.mvisibility
396 if mvisibility == intrude_visibility then
397 modelbuilder.error(nvisibility, "Error: intrude is not a legal visibility for properties.")
398 mvisibility = public_visibility
399 end
400 end
401 if mclassdef.mclass.visibility == private_visibility then
402 if mvisibility == protected_visibility then
403 assert nvisibility != null
404 modelbuilder.error(nvisibility, "Error: The only legal visibility for properties in a private class is private.")
405 else if mvisibility == private_visibility then
406 assert nvisibility != null
407 # Not yet
408 # modelbuilder.warning(nvisibility, "Warning: private is unrequired since the only legal visibility for properties in a private class is private.")
409 end
410 mvisibility = private_visibility
411 end
412 return mvisibility
413 end
414
415 private fun set_doc(mpropdef: MPropDef)
416 do
417 var ndoc = self.n_doc
418 if ndoc != null then
419 var mdoc = ndoc.to_mdoc
420 mpropdef.mdoc = mdoc
421 mdoc.original_mentity = mpropdef
422 end
423 end
424
425 private fun check_redef_property_visibility(modelbuilder: ModelBuilder, nvisibility: nullable AVisibility, mprop: MProperty)
426 do
427 if nvisibility == null then return
428 var mvisibility = nvisibility.mvisibility
429 if mvisibility != mprop.visibility and mvisibility != public_visibility then
430 modelbuilder.error(nvisibility, "Error: redefinition changed the visibility from a {mprop.visibility} to a {mvisibility}")
431 end
432 end
433
434 private fun check_redef_keyword(modelbuilder: ModelBuilder, mclassdef: MClassDef, kwredef: nullable Token, need_redef: Bool, mprop: MProperty): Bool
435 do
436 if mclassdef.mprop2npropdef.has_key(mprop) then
437 modelbuilder.error(self, "Error: A property {mprop} is already defined in class {mclassdef.mclass} at line {mclassdef.mprop2npropdef[mprop].location.line_start}.")
438 return false
439 end
440 if mprop isa MMethod and mprop.is_toplevel != (parent isa ATopClassdef) then
441 if mprop.is_toplevel then
442 modelbuilder.error(self, "Error: {mprop} is a top level method.")
443 else
444 modelbuilder.error(self, "Error: {mprop} is not a top level method.")
445 end
446 return false
447
448 end
449 if kwredef == null then
450 if need_redef then
451 modelbuilder.error(self, "Redef error: {mclassdef.mclass}::{mprop.name} is an inherited property. To redefine it, add the redef keyword.")
452 return false
453 end
454 else
455 if not need_redef then
456 modelbuilder.error(self, "Error: No property {mclassdef.mclass}::{mprop.name} is inherited. Remove the redef keyword to define a new property.")
457 return false
458 end
459 end
460 return true
461 end
462
463 end
464
465 redef class ASignature
466 # Is the model builder has correctly visited the signature
467 var is_visited = false
468 # Names of parameters from the AST
469 # REQUIRE: is_visited
470 var param_names = new Array[String]
471 # Types of parameters from the AST
472 # REQUIRE: is_visited
473 var param_types = new Array[MType]
474 # Rank of the vararg (of -1 if none)
475 # REQUIRE: is_visited
476 var vararg_rank: Int = -1
477 # Return type
478 var ret_type: nullable MType = null
479
480 # Visit and fill information about a signature
481 private fun visit_signature(modelbuilder: ModelBuilder, mclassdef: MClassDef): Bool
482 do
483 var mmodule = mclassdef.mmodule
484 var param_names = self.param_names
485 var param_types = self.param_types
486 for np in self.n_params do
487 param_names.add(np.n_id.text)
488 var ntype = np.n_type
489 if ntype != null then
490 var mtype = modelbuilder.resolve_mtype(mmodule, mclassdef, ntype)
491 if mtype == null then return false # Skip error
492 for i in [0..param_names.length-param_types.length[ do
493 param_types.add(mtype)
494 end
495 if np.n_dotdotdot != null then
496 if self.vararg_rank != -1 then
497 modelbuilder.error(np, "Error: {param_names[self.vararg_rank]} is already a vararg")
498 return false
499 else
500 self.vararg_rank = param_names.length - 1
501 end
502 end
503 end
504 end
505 var ntype = self.n_type
506 if ntype != null then
507 self.ret_type = modelbuilder.resolve_mtype(mmodule, mclassdef, ntype)
508 if self.ret_type == null then return false # Skip errir
509 end
510
511 self.is_visited = true
512 return true
513 end
514
515 # Build a visited signature
516 fun build_signature(modelbuilder: ModelBuilder): nullable MSignature
517 do
518 if param_names.length != param_types.length then
519 # Some parameters are typed, other parameters are not typed.
520 modelbuilder.error(self.n_params[param_types.length], "Error: Untyped parameter `{param_names[param_types.length]}'.")
521 return null
522 end
523
524 var mparameters = new Array[MParameter]
525 for i in [0..param_names.length[ do
526 var mparameter = new MParameter(param_names[i], param_types[i], i == vararg_rank)
527 self.n_params[i].mparameter = mparameter
528 mparameters.add(mparameter)
529 end
530
531 var msignature = new MSignature(mparameters, ret_type)
532 return msignature
533 end
534 end
535
536 redef class AParam
537 # The associated mparameter if any
538 var mparameter: nullable MParameter = null
539 end
540
541 redef class AMethPropdef
542 redef type MPROPDEF: MMethodDef
543
544 redef fun build_property(modelbuilder, mclassdef)
545 do
546 var n_kwinit = n_kwinit
547 var n_kwnew = n_kwnew
548 var is_init = n_kwinit != null or n_kwnew != null
549 var name: String
550 var amethodid = self.n_methid
551 var name_node: ANode
552 if amethodid == null then
553 if not is_init then
554 name = "main"
555 name_node = self
556 else if n_kwinit != null then
557 name = "init"
558 name_node = n_kwinit
559 else if n_kwnew != null then
560 name = "init"
561 name_node = n_kwnew
562 else
563 abort
564 end
565 else if amethodid isa AIdMethid then
566 name = amethodid.n_id.text
567 name_node = amethodid
568 else
569 # operator, bracket or assign
570 name = amethodid.collect_text
571 name_node = amethodid
572
573 if name == "-" and self.n_signature.n_params.length == 0 then
574 name = "unary -"
575 end
576 end
577
578 var mprop: nullable MMethod = null
579 if not is_init or n_kwredef != null then mprop = modelbuilder.try_get_mproperty_by_name(name_node, mclassdef, name).as(nullable MMethod)
580 if mprop == null then
581 var mvisibility = new_property_visibility(modelbuilder, mclassdef, self.n_visibility)
582 mprop = new MMethod(mclassdef, name, mvisibility)
583 mprop.is_init = is_init
584 mprop.is_new = n_kwnew != null
585 if parent isa ATopClassdef then mprop.is_toplevel = true
586 if not self.check_redef_keyword(modelbuilder, mclassdef, n_kwredef, false, mprop) then return
587 else
588 if not self.check_redef_keyword(modelbuilder, mclassdef, n_kwredef, not self isa AMainMethPropdef, mprop) then return
589 check_redef_property_visibility(modelbuilder, self.n_visibility, mprop)
590 end
591 mclassdef.mprop2npropdef[mprop] = self
592
593 var mpropdef = new MMethodDef(mclassdef, mprop, self.location)
594
595 set_doc(mpropdef)
596
597 self.mpropdef = mpropdef
598 modelbuilder.mpropdef2npropdef[mpropdef] = self
599 if mpropdef.is_intro then
600 modelbuilder.toolcontext.info("{mpropdef} introduces new method {mprop.full_name}", 3)
601 else
602 modelbuilder.toolcontext.info("{mpropdef} redefines method {mprop.full_name}", 3)
603 end
604 end
605
606 redef fun build_signature(modelbuilder)
607 do
608 var mpropdef = self.mpropdef
609 if mpropdef == null then return # Error thus skiped
610 var mclassdef = mpropdef.mclassdef
611 var mmodule = mclassdef.mmodule
612 var nsig = self.n_signature
613
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
617 var vararg_rank = -1
618 var ret_type: nullable MType = null # Return type from the AST
619 if nsig != null then
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
625 end
626
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
633
634 # Check inherited signature arity
635 if param_names.length != msignature.arity then
636 var node: ANode
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}")
639 return
640 end
641 else if mpropdef.mproperty.is_init 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
649 end
650 end
651 end
652 end
653
654
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)
661 end
662 vararg_rank = msignature.vararg_rank
663 end
664 if msignature != null and ret_type == null then
665 ret_type = msignature.return_mtype
666 end
667
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]}'.")
671 return
672 end
673
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)
679 end
680
681 msignature = new MSignature(mparameters, ret_type)
682 mpropdef.msignature = msignature
683 mpropdef.is_abstract = self isa ADeferredMethPropdef
684 mpropdef.is_intern = self isa AInternMethPropdef
685 mpropdef.is_extern = self isa AExternPropdef
686 end
687
688 redef fun check_signature(modelbuilder)
689 do
690 var mpropdef = self.mpropdef
691 if mpropdef == null then return # Error thus skiped
692 var mclassdef = mpropdef.mclassdef
693 var mmodule = mclassdef.mmodule
694 var nsig = self.n_signature
695 var mysignature = self.mpropdef.msignature
696 if mysignature == null then return # Error thus skiped
697
698 # Lookup for signature in the precursor
699 # FIXME all precursors should be considered
700 if not mpropdef.is_intro then
701 var msignature = mpropdef.mproperty.intro.msignature
702 if msignature == null then return
703
704 var precursor_ret_type = msignature.return_mtype
705 var ret_type = mysignature.return_mtype
706 if ret_type != null and precursor_ret_type == null then
707 modelbuilder.error(nsig.n_type.as(not null), "Redef Error: {mpropdef.mproperty} is a procedure, not a function.")
708 return
709 end
710
711 if mysignature.arity > 0 then
712 # Check parameters types
713 for i in [0..mysignature.arity[ do
714 var myt = mysignature.mparameters[i].mtype
715 var prt = msignature.mparameters[i].mtype
716 if not myt.is_subtype(mmodule, mclassdef.bound_mtype, prt) or
717 not prt.is_subtype(mmodule, mclassdef.bound_mtype, myt) then
718 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}.")
719 end
720 end
721 end
722 if precursor_ret_type != null then
723 if ret_type == null then
724 # Inherit the return type
725 ret_type = precursor_ret_type
726 else if not ret_type.is_subtype(mmodule, mclassdef.bound_mtype, precursor_ret_type) then
727 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}.")
728 end
729 end
730 end
731
732 if mysignature.arity > 0 then
733 # Check parameters visibility
734 for i in [0..mysignature.arity[ do
735 var nt = nsig.n_params[i].n_type
736 if nt != null then modelbuilder.check_visibility(nt, nt.mtype.as(not null), mpropdef)
737 end
738 var nt = nsig.n_type
739 if nt != null then modelbuilder.check_visibility(nt, nt.mtype.as(not null), mpropdef)
740 end
741 end
742 end
743
744 redef class AAttrPropdef
745 redef type MPROPDEF: MAttributeDef
746
747 # Is the node tagged `noinit`?
748 var noinit = false
749
750 # Is the node taggeg lazy?
751 var is_lazy = false
752
753 # The guard associated to a lasy attribute.
754 # Because some engines does not have a working `isset`,
755 # this additionnal attribute is used to guard the lazy initialization.
756 # TODO: to remove once isset is correctly implemented
757 var mlazypropdef: nullable MAttributeDef
758
759 # The associated getter (read accessor) if any
760 var mreadpropdef: nullable MMethodDef writable
761 # The associated setter (write accessor) if any
762 var mwritepropdef: nullable MMethodDef writable
763
764 redef fun build_property(modelbuilder, mclassdef)
765 do
766 var mclass = mclassdef.mclass
767
768 var name: String
769 if self.n_id != null then
770 name = self.n_id.text
771 else
772 name = self.n_id2.text
773 end
774
775 if mclass.kind == interface_kind or mclassdef.mclass.kind == enum_kind then
776 modelbuilder.error(self, "Error: Attempt to define attribute {name} in the interface {mclass}.")
777 else if mclass.kind == enum_kind then
778 modelbuilder.error(self, "Error: Attempt to define attribute {name} in the enum class {mclass}.")
779 else if mclass.kind == extern_kind then
780 modelbuilder.error(self, "Error: Attempt to define attribute {name} in the extern class {mclass}.")
781 end
782
783 var nid = self.n_id
784 if nid != null then
785 # Old attribute style
786 var mprop = modelbuilder.try_get_mproperty_by_name(nid, mclassdef, name)
787 if mprop == null then
788 var mvisibility = new_property_visibility(modelbuilder, mclassdef, self.n_visibility)
789 mprop = new MAttribute(mclassdef, name, mvisibility)
790 if not self.check_redef_keyword(modelbuilder, mclassdef, self.n_kwredef, false, mprop) then return
791 else
792 assert mprop isa MAttribute
793 check_redef_property_visibility(modelbuilder, self.n_visibility, mprop)
794 if not self.check_redef_keyword(modelbuilder, mclassdef, self.n_kwredef, true, mprop) then return
795 end
796 mclassdef.mprop2npropdef[mprop] = self
797
798 var mpropdef = new MAttributeDef(mclassdef, mprop, self.location)
799 self.mpropdef = mpropdef
800 modelbuilder.mpropdef2npropdef[mpropdef] = self
801 set_doc(mpropdef)
802
803 var nreadable = self.n_readable
804 if nreadable != null then modelbuilder.error(nreadable, "Error: old-style getter no more supported")
805 var nwritable = self.n_writable
806 if nwritable != null then modelbuilder.error(nwritable, "Error: old-style setter no more supported")
807 else
808 # New attribute style
809 var nid2 = self.n_id2.as(not null)
810 var mprop = new MAttribute(mclassdef, "_" + name, private_visibility)
811 var mpropdef = new MAttributeDef(mclassdef, mprop, self.location)
812 self.mpropdef = mpropdef
813 modelbuilder.mpropdef2npropdef[mpropdef] = self
814 set_doc(mpropdef)
815
816 var readname = name
817 var mreadprop = modelbuilder.try_get_mproperty_by_name(nid2, mclassdef, readname).as(nullable MMethod)
818 if mreadprop == null then
819 var mvisibility = new_property_visibility(modelbuilder, mclassdef, self.n_visibility)
820 mreadprop = new MMethod(mclassdef, readname, mvisibility)
821 if not self.check_redef_keyword(modelbuilder, mclassdef, n_kwredef, false, mreadprop) then return
822 else
823 if not self.check_redef_keyword(modelbuilder, mclassdef, n_kwredef, true, mreadprop) then return
824 check_redef_property_visibility(modelbuilder, self.n_visibility, mreadprop)
825 end
826 mclassdef.mprop2npropdef[mreadprop] = self
827
828 var mreadpropdef = new MMethodDef(mclassdef, mreadprop, self.location)
829 self.mreadpropdef = mreadpropdef
830 modelbuilder.mpropdef2npropdef[mreadpropdef] = self
831 mreadpropdef.mdoc = mpropdef.mdoc
832
833 var atlazy = self.get_single_annotation("lazy", modelbuilder)
834 if atlazy != null then
835 if n_expr == null then
836 modelbuilder.error(atlazy, "Error: a lazy attribute needs a value")
837 end
838 is_lazy = true
839 var mlazyprop = new MAttribute(mclassdef, "lazy _" + name, none_visibility)
840 var mlazypropdef = new MAttributeDef(mclassdef, mlazyprop, self.location)
841 self.mlazypropdef = mlazypropdef
842 end
843
844 var atreadonly = self.get_single_annotation("readonly", modelbuilder)
845 if atreadonly != null then
846 if n_expr == null then
847 modelbuilder.error(atreadonly, "Error: a readonly attribute needs a value")
848 end
849 # No setter, so just leave
850 return
851 end
852
853 var writename = name + "="
854 var nwritable = self.n_writable
855 var atwritable = self.get_single_annotation("writable", modelbuilder)
856 if atwritable != null then
857 if not atwritable.n_args.is_empty then
858 writename = atwritable.arg_as_id(modelbuilder) or else writename
859 end
860 end
861 var mwriteprop = modelbuilder.try_get_mproperty_by_name(nid2, mclassdef, writename).as(nullable MMethod)
862 var nwkwredef: nullable Token = null
863 if nwritable != null then nwkwredef = nwritable.n_kwredef
864 if atwritable != null then nwkwredef = atwritable.n_kwredef
865 if mwriteprop == null then
866 var mvisibility
867 if nwritable != null then
868 mvisibility = new_property_visibility(modelbuilder, mclassdef, nwritable.n_visibility)
869 else if atwritable != null then
870 mvisibility = new_property_visibility(modelbuilder, mclassdef, atwritable.n_visibility)
871 else
872 mvisibility = private_visibility
873 end
874 mwriteprop = new MMethod(mclassdef, writename, mvisibility)
875 if not self.check_redef_keyword(modelbuilder, mclassdef, nwkwredef, false, mwriteprop) then return
876 else
877 if not self.check_redef_keyword(modelbuilder, mclassdef, nwkwredef or else n_kwredef, true, mwriteprop) then return
878 if nwritable != null then
879 check_redef_property_visibility(modelbuilder, nwritable.n_visibility, mwriteprop)
880 else if atwritable != null then
881 check_redef_property_visibility(modelbuilder, atwritable.n_visibility, mwriteprop)
882 end
883 end
884 mclassdef.mprop2npropdef[mwriteprop] = self
885
886 var mwritepropdef = new MMethodDef(mclassdef, mwriteprop, self.location)
887 self.mwritepropdef = mwritepropdef
888 modelbuilder.mpropdef2npropdef[mwritepropdef] = self
889 mwritepropdef.mdoc = mpropdef.mdoc
890 end
891 end
892
893 redef fun build_signature(modelbuilder)
894 do
895 var mpropdef = self.mpropdef
896 if mpropdef == null then return # Error thus skiped
897 var mclassdef = mpropdef.mclassdef
898 var mmodule = mclassdef.mmodule
899 var mtype: nullable MType = null
900
901 var mreadpropdef = self.mreadpropdef
902
903 var ntype = self.n_type
904 if ntype != null then
905 mtype = modelbuilder.resolve_mtype(mmodule, mclassdef, ntype)
906 if mtype == null then return
907 end
908
909 # Inherit the type from the getter (usually an abstact getter)
910 if mtype == null and mreadpropdef != null and not mreadpropdef.is_intro then
911 var msignature = mreadpropdef.mproperty.intro.msignature
912 if msignature == null then return # Error, thus skiped
913 mtype = msignature.return_mtype
914 end
915
916 var nexpr = self.n_expr
917 if mtype == null then
918 if nexpr != null then
919 if nexpr isa ANewExpr then
920 mtype = modelbuilder.resolve_mtype(mmodule, mclassdef, nexpr.n_type)
921 else if nexpr isa AIntExpr then
922 var cla = modelbuilder.try_get_mclass_by_name(nexpr, mmodule, "Int")
923 if cla != null then mtype = cla.mclass_type
924 else if nexpr isa AFloatExpr then
925 var cla = modelbuilder.try_get_mclass_by_name(nexpr, mmodule, "Float")
926 if cla != null then mtype = cla.mclass_type
927 else if nexpr isa ACharExpr then
928 var cla = modelbuilder.try_get_mclass_by_name(nexpr, mmodule, "Char")
929 if cla != null then mtype = cla.mclass_type
930 else if nexpr isa ABoolExpr then
931 var cla = modelbuilder.try_get_mclass_by_name(nexpr, mmodule, "Bool")
932 if cla != null then mtype = cla.mclass_type
933 else if nexpr isa ASuperstringExpr then
934 var cla = modelbuilder.try_get_mclass_by_name(nexpr, mmodule, "String")
935 if cla != null then mtype = cla.mclass_type
936 else if nexpr isa AStringFormExpr then
937 var cla = modelbuilder.try_get_mclass_by_name(nexpr, mmodule, "String")
938 if cla != null then mtype = cla.mclass_type
939 else
940 modelbuilder.error(self, "Error: Untyped attribute {mpropdef}. Implicit typing allowed only for literals and new.")
941 end
942
943 if mtype == null then return
944 end
945 else if ntype != null then
946 if nexpr isa ANewExpr then
947 var xmtype = modelbuilder.resolve_mtype(mmodule, mclassdef, nexpr.n_type)
948 if xmtype == mtype and modelbuilder.toolcontext.opt_warn.value >= 2 then
949 modelbuilder.warning(ntype, "Warning: useless type definition")
950 end
951 end
952 end
953
954 if mtype == null then
955 modelbuilder.error(self, "Error: Untyped attribute {mpropdef}")
956 return
957 end
958
959 mpropdef.static_mtype = mtype
960
961 if mreadpropdef != null then
962 var msignature = new MSignature(new Array[MParameter], mtype)
963 mreadpropdef.msignature = msignature
964 end
965
966 var mwritepropdef = self.mwritepropdef
967 if mwritepropdef != null then
968 var name: String
969 if n_id != null then
970 name = n_id.text.substring_from(1)
971 else
972 name = n_id2.text
973 end
974 var mparameter = new MParameter(name, mtype, false)
975 var msignature = new MSignature([mparameter], null)
976 mwritepropdef.msignature = msignature
977 end
978
979 var mlazypropdef = self.mlazypropdef
980 if mlazypropdef != null then
981 mlazypropdef.static_mtype = modelbuilder.model.get_mclasses_by_name("Bool").first.mclass_type
982 end
983 end
984
985 redef fun check_signature(modelbuilder)
986 do
987 var mpropdef = self.mpropdef
988 if mpropdef == null then return # Error thus skiped
989 var mclassdef = mpropdef.mclassdef
990 var mmodule = mclassdef.mmodule
991 var ntype = self.n_type
992 var mtype = self.mpropdef.static_mtype
993 if mtype == null then return # Error thus skiped
994
995 # Lookup for signature in the precursor
996 # FIXME all precursors should be considered
997 if not mpropdef.is_intro then
998 var precursor_type = mpropdef.mproperty.intro.static_mtype
999 if precursor_type == null then return
1000
1001 if mtype != precursor_type then
1002 modelbuilder.error(ntype.as(not null), "Redef Error: Wrong static type. found {mtype}, expected {precursor_type}.")
1003 return
1004 end
1005 end
1006
1007 # Check getter and setter
1008 var meth = self.mreadpropdef
1009 if meth != null then
1010 self.check_method_signature(modelbuilder, meth)
1011 var node: nullable ANode = ntype
1012 if node == null then node = self
1013 modelbuilder.check_visibility(node, mtype, meth)
1014 end
1015 meth = self.mwritepropdef
1016 if meth != null then
1017 self.check_method_signature(modelbuilder, meth)
1018 var node: nullable ANode = ntype
1019 if node == null then node = self
1020 modelbuilder.check_visibility(node, mtype, meth)
1021 end
1022 end
1023
1024 private fun check_method_signature(modelbuilder: ModelBuilder, mpropdef: MMethodDef)
1025 do
1026 var mclassdef = mpropdef.mclassdef
1027 var mmodule = mclassdef.mmodule
1028 var nsig = self.n_type
1029 var mysignature = mpropdef.msignature
1030 if mysignature == null then return # Error thus skiped
1031
1032 # Lookup for signature in the precursor
1033 # FIXME all precursors should be considered
1034 if not mpropdef.is_intro then
1035 var msignature = mpropdef.mproperty.intro.msignature
1036 if msignature == null then return
1037
1038 if mysignature.arity != msignature.arity then
1039 var node: ANode
1040 if nsig != null then node = nsig else node = self
1041 modelbuilder.error(node, "Redef Error: {mysignature.arity} parameters found, {msignature.arity} expected. Signature is {mpropdef}{msignature}")
1042 return
1043 end
1044 var precursor_ret_type = msignature.return_mtype
1045 var ret_type = mysignature.return_mtype
1046 if ret_type != null and precursor_ret_type == null then
1047 var node: ANode
1048 if nsig != null then node = nsig else node = self
1049 modelbuilder.error(node, "Redef Error: {mpropdef.mproperty} is a procedure, not a function.")
1050 return
1051 end
1052
1053 if mysignature.arity > 0 then
1054 # Check parameters types
1055 for i in [0..mysignature.arity[ do
1056 var myt = mysignature.mparameters[i].mtype
1057 var prt = msignature.mparameters[i].mtype
1058 if not myt.is_subtype(mmodule, mclassdef.bound_mtype, prt) or
1059 not prt.is_subtype(mmodule, mclassdef.bound_mtype, myt) then
1060 var node: ANode
1061 if nsig != null then node = nsig else node = self
1062 modelbuilder.error(node, "Redef Error: Wrong type for parameter `{mysignature.mparameters[i].name}'. found {myt}, expected {prt}.")
1063 end
1064 end
1065 end
1066 if precursor_ret_type != null then
1067 if ret_type == null then
1068 # Inherit the return type
1069 ret_type = precursor_ret_type
1070 else if not ret_type.is_subtype(mmodule, mclassdef.bound_mtype, precursor_ret_type) then
1071 var node: ANode
1072 if nsig != null then node = nsig else node = self
1073 modelbuilder.error(node, "Redef Error: Wrong return type. found {ret_type}, expected {precursor_ret_type}.")
1074 end
1075 end
1076 end
1077 end
1078 end
1079
1080 redef class ATypePropdef
1081 redef type MPROPDEF: MVirtualTypeDef
1082
1083 redef fun build_property(modelbuilder, mclassdef)
1084 do
1085 var name = self.n_id.text
1086 var mprop = modelbuilder.try_get_mproperty_by_name(self.n_id, mclassdef, name)
1087 if mprop == null then
1088 var mvisibility = new_property_visibility(modelbuilder, mclassdef, self.n_visibility)
1089 mprop = new MVirtualTypeProp(mclassdef, name, mvisibility)
1090 for c in name.chars do if c >= 'a' and c<= 'z' then
1091 modelbuilder.warning(n_id, "Warning: lowercase in the virtual type {name}")
1092 break
1093 end
1094 if not self.check_redef_keyword(modelbuilder, mclassdef, self.n_kwredef, false, mprop) then return
1095 else
1096 if not self.check_redef_keyword(modelbuilder, mclassdef, self.n_kwredef, true, mprop) then return
1097 assert mprop isa MVirtualTypeProp
1098 check_redef_property_visibility(modelbuilder, self.n_visibility, mprop)
1099 end
1100 mclassdef.mprop2npropdef[mprop] = self
1101
1102 var mpropdef = new MVirtualTypeDef(mclassdef, mprop, self.location)
1103 self.mpropdef = mpropdef
1104 modelbuilder.mpropdef2npropdef[mpropdef] = self
1105 set_doc(mpropdef)
1106
1107 var atfixed = get_single_annotation("fixed", modelbuilder)
1108 if atfixed != null then
1109 mpropdef.is_fixed = true
1110 end
1111 end
1112
1113 redef fun build_signature(modelbuilder)
1114 do
1115 var mpropdef = self.mpropdef
1116 if mpropdef == null then return # Error thus skiped
1117 var mclassdef = mpropdef.mclassdef
1118 var mmodule = mclassdef.mmodule
1119 var mtype: nullable MType = null
1120
1121 var ntype = self.n_type
1122 mtype = modelbuilder.resolve_mtype(mmodule, mclassdef, ntype)
1123 if mtype == null then return
1124
1125 mpropdef.bound = mtype
1126 # print "{mpropdef}: {mtype}"
1127 end
1128
1129 redef fun check_signature(modelbuilder)
1130 do
1131 var mpropdef = self.mpropdef
1132 if mpropdef == null then return # Error thus skiped
1133
1134 var bound = self.mpropdef.bound
1135 if bound == null then return # Error thus skiped
1136
1137 modelbuilder.check_visibility(n_type, bound, mpropdef)
1138
1139 var mclassdef = mpropdef.mclassdef
1140 var mmodule = mclassdef.mmodule
1141 var anchor = mclassdef.bound_mtype
1142
1143 # Check circularity
1144 if bound isa MVirtualType then
1145 # Slow case: progress on each resolution until: (i) we loop, or (ii) we found a non formal type
1146 var seen = [self.mpropdef.mproperty.mvirtualtype]
1147 loop
1148 if seen.has(bound) then
1149 seen.add(bound)
1150 modelbuilder.error(self, "Error: circularity of virtual type definition: {seen.join(" -> ")}")
1151 return
1152 end
1153 seen.add(bound)
1154 var next = bound.lookup_bound(mmodule, anchor)
1155 if not next isa MVirtualType then break
1156 bound = next
1157 end
1158 end
1159
1160 # Check redefinitions
1161 bound = mpropdef.bound.as(not null)
1162 for p in mpropdef.mproperty.lookup_super_definitions(mmodule, anchor) do
1163 var supbound = p.bound.as(not null)
1164 if p.is_fixed then
1165 modelbuilder.error(self, "Redef Error: Virtual type {mpropdef.mproperty} is fixed in super-class {p.mclassdef.mclass}")
1166 break
1167 end
1168 if p.mclassdef.mclass == mclassdef.mclass then
1169 # Still a warning to pass existing bad code
1170 modelbuilder.warning(n_type, "Redef Error: a virtual type cannot be refined.")
1171 break
1172 end
1173 if not bound.is_subtype(mmodule, anchor, supbound) then
1174 modelbuilder.error(n_type, "Redef Error: Wrong bound type. Found {bound}, expected a subtype of {supbound}, as in {p}.")
1175 break
1176 end
1177 end
1178 end
1179 end