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