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