modelbuilder: new method ASignature::visit_signature
[nit.git] / src / modelbuilder.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 # Load nit source files and build the associated model
18 #
19 # FIXME better doc
20 #
21 # FIXME split this module into submodules
22 # FIXME add missing error checks
23 module modelbuilder
24
25 import parser
26 import model
27 import poset
28 import opts
29 import toolcontext
30
31 ###
32
33 redef class ToolContext
34 # Option --path
35 readable var _opt_path: OptionArray = new OptionArray("Set include path for loaders (may be used more than once)", "-I", "--path")
36
37 # Option --only-metamodel
38 readable var _opt_only_metamodel: OptionBool = new OptionBool("Stop after meta-model processing", "--only-metamodel")
39
40 # Option --only-parse
41 readable var _opt_only_parse: OptionBool = new OptionBool("Only proceed to parse step of loaders", "--only-parse")
42
43 redef init
44 do
45 super
46 option_context.add_option(opt_path, opt_only_parse, opt_only_metamodel)
47 end
48 end
49
50 # A model builder knows how to load nit source files and build the associated model
51 # The important function is `parse_and_build' that does all the job.
52 # The others function can be used for specific tasks
53 class ModelBuilder
54 # The model where new modules, classes and properties are added
55 var model: Model
56
57 # The toolcontext used to control the interaction with the user (getting options and displaying messages)
58 var toolcontext: ToolContext
59
60 # Instantiate a modelbuilder for a model and a toolcontext
61 # Important, the options of the toolcontext must be correctly set (parse_option already called)
62 init(model: Model, toolcontext: ToolContext)
63 do
64 self.model = model
65 self.toolcontext = toolcontext
66
67 # Setup the paths value
68 paths.append(toolcontext.opt_path.value)
69
70 var path_env = "NIT_PATH".environ
71 if not path_env.is_empty then
72 paths.append(path_env.split_with(':'))
73 end
74
75 path_env = "NIT_DIR".environ
76 if not path_env.is_empty then
77 var libname = "{path_env}/lib"
78 if libname.file_exists then paths.add(libname)
79 end
80
81 var libname = "{sys.program_name.dirname}/../lib"
82 if libname.file_exists then paths.add(libname.simplify_path)
83 end
84
85 # Load and analyze a bunch of modules.
86 # `modules' can contains filenames or module names.
87 # Imported modules are automatically loaded, builds and analysed.
88 # The result is the corresponding built modules.
89 # Errors and warnings are printed with the toolcontext.
90 #
91 # FIXME: Maybe just let the client do the loop (instead of playing with Sequences)
92 fun parse_and_build(modules: Sequence[String]): Array[MModule]
93 do
94 var time0 = get_time
95 # Parse and recursively load
96 self.toolcontext.info("*** PARSE ***", 1)
97 var mmodules = new Array[MModule]
98 for a in modules do
99 var nmodule = self.load_module(null, a)
100 if nmodule == null then continue # Skip error
101 mmodules.add(nmodule.mmodule.as(not null))
102 end
103 var time1 = get_time
104 self.toolcontext.info("*** END PARSE: {time1-time0} ***", 2)
105
106 self.toolcontext.check_errors
107
108 if self.toolcontext.opt_only_parse.value then
109 self.toolcontext.info("--only-parse: stop processing", 2)
110 return new Array[MModule]
111 end
112
113 # Build the model
114 self.toolcontext.info("*** BUILD MODEL ***", 1)
115 self.build_all_classes
116 var time2 = get_time
117 self.toolcontext.info("*** END BUILD MODEL: {time2-time1} ***", 2)
118
119 self.toolcontext.check_errors
120
121 return mmodules
122 end
123
124 # Return a class named `name' visible by the module `mmodule'.
125 # Visibility in modules is correctly handled.
126 # If no such a class exists, then null is returned.
127 # If more than one class exists, then an error on `anode' is displayed and null is returned.
128 # FIXME: add a way to handle class name conflict
129 fun try_get_mclass_by_name(anode: ANode, mmodule: MModule, name: String): nullable MClass
130 do
131 var classes = model.get_mclasses_by_name(name)
132 if classes == null then
133 return null
134 end
135
136 var res: nullable MClass = null
137 for mclass in classes do
138 if not mmodule.in_importation <= mclass.intro_mmodule then continue
139 if not mmodule.is_visible(mclass.intro_mmodule, mclass.visibility) then continue
140 if res == null then
141 res = mclass
142 else
143 error(anode, "Ambigous class name '{name}'; conflict between {mclass.full_name} and {res.full_name}")
144 return null
145 end
146 end
147 return res
148 end
149
150 # Return a property named `name' on the type `mtype' visible in the module `mmodule'.
151 # Visibility in modules is correctly handled.
152 # Protected properties are returned (it is up to the caller to check and reject protected properties).
153 # If no such a property exists, then null is returned.
154 # If more than one property exists, then an error on `anode' is displayed and null is returned.
155 # FIXME: add a way to handle property name conflict
156 fun try_get_mproperty_by_name2(anode: ANode, mmodule: MModule, mtype: MType, name: String): nullable MProperty
157 do
158 var props = self.model.get_mproperties_by_name(name)
159 if props == null then
160 return null
161 end
162
163 var cache = self.try_get_mproperty_by_name2_cache[mmodule, mtype, name]
164 if cache != null then return cache
165
166 var res: nullable MProperty = null
167 var ress: nullable Array[MProperty] = null
168 for mprop in props do
169 if not mtype.has_mproperty(mmodule, mprop) then continue
170 if not mmodule.is_visible(mprop.intro_mclassdef.mmodule, mprop.visibility) then continue
171 if res == null then
172 res = mprop
173 else
174 var restype = res.intro_mclassdef.bound_mtype
175 var mproptype = mprop.intro_mclassdef.bound_mtype
176 if restype.is_subtype(mmodule, null, mproptype) then
177 # we keep res
178 else if mproptype.is_subtype(mmodule, null, restype) then
179 res = mprop
180 else
181 if ress == null then ress = new Array[MProperty]
182 ress.add(mprop)
183 end
184 end
185 end
186 if ress != null then
187 var restype = res.intro_mclassdef.bound_mtype
188 for mprop in ress do
189 var mproptype = mprop.intro_mclassdef.bound_mtype
190 if not restype.is_subtype(mmodule, null, mproptype) then
191 self.error(anode, "Ambigous property name '{name}' for {mtype}; conflict between {mprop.full_name} and {res.full_name}")
192 return null
193 end
194 end
195 end
196
197 self.try_get_mproperty_by_name2_cache[mmodule, mtype, name] = res
198 return res
199 end
200
201 private var try_get_mproperty_by_name2_cache: HashMap3[MModule, MType, String, nullable MProperty] = new HashMap3[MModule, MType, String, nullable MProperty]
202
203
204 # Alias for try_get_mproperty_by_name2(anode, mclassdef.mmodule, mclassdef.mtype, name)
205 fun try_get_mproperty_by_name(anode: ANode, mclassdef: MClassDef, name: String): nullable MProperty
206 do
207 return try_get_mproperty_by_name2(anode, mclassdef.mmodule, mclassdef.bound_mtype, name)
208 end
209
210 # The list of directories to search for top level modules
211 # The list is initially set with :
212 # * the toolcontext --path option
213 # * the NIT_PATH environment variable
214 # * some heuristics including the NIT_DIR environment variable and the progname of the process
215 # Path can be added (or removed) by the client
216 var paths: Array[String] = new Array[String]
217
218 # Get a module by its short name; if required, the module is loaded, parsed and its hierarchies computed.
219 # If `mmodule' is set, then the module search starts from it up to the top level (see `paths');
220 # if `mmodule' is null then the module is searched in the top level only.
221 # If no module exists or there is a name conflict, then an error on `anode' is displayed and null is returned.
222 # FIXME: add a way to handle module name conflict
223 fun get_mmodule_by_name(anode: ANode, mmodule: nullable MModule, name: String): nullable MModule
224 do
225 var origmmodule = mmodule
226 var modules = model.get_mmodules_by_name(name)
227
228 var tries = new Array[String]
229
230 var lastmodule = mmodule
231 while mmodule != null do
232 var dirname = mmodule.location.file.filename.dirname
233
234 # Determine the owner
235 var owner: nullable MModule
236 if dirname.basename("") != mmodule.name then
237 owner = mmodule.direct_owner
238 else
239 owner = mmodule
240 end
241
242 # First, try the already known nested modules
243 if modules != null then
244 for candidate in modules do
245 if candidate.direct_owner == owner then
246 return candidate
247 end
248 end
249 end
250
251 # Second, try the directory to find a file
252 var try_file = dirname + "/" + name + ".nit"
253 tries.add try_file
254 if try_file.file_exists then
255 var res = self.load_module(owner, try_file.simplify_path)
256 if res == null then return null # Forward error
257 return res.mmodule.as(not null)
258 end
259
260 # Third, try if the requested module is itself an owner
261 try_file = dirname + "/" + name + "/" + name + ".nit"
262 if try_file.file_exists then
263 var res = self.load_module(owner, try_file.simplify_path)
264 if res == null then return null # Forward error
265 return res.mmodule.as(not null)
266 end
267
268 lastmodule = mmodule
269 mmodule = mmodule.direct_owner
270 end
271
272 if modules != null then
273 for candidate in modules do
274 if candidate.direct_owner == null then
275 return candidate
276 end
277 end
278 end
279
280 # Look at some known directories
281 var lookpaths = self.paths
282
283 # Look in the directory of the last module also (event if not in the path)
284 if lastmodule != null then
285 var dirname = lastmodule.location.file.filename.dirname
286 if dirname.basename("") == lastmodule.name then
287 dirname = dirname.dirname
288 end
289 if not lookpaths.has(dirname) then
290 lookpaths = lookpaths.to_a
291 lookpaths.add(dirname)
292 end
293 end
294
295 var candidate: nullable String = null
296 for dirname in lookpaths do
297 var try_file = (dirname + "/" + name + ".nit").simplify_path
298 tries.add try_file
299 if try_file.file_exists then
300 if candidate == null then
301 candidate = try_file
302 else if candidate != try_file then
303 error(anode, "Error: conflicting module file for {name}: {candidate} {try_file}")
304 end
305 end
306 try_file = (dirname + "/" + name + "/" + name + ".nit").simplify_path
307 if try_file.file_exists then
308 if candidate == null then
309 candidate = try_file
310 else if candidate != try_file then
311 error(anode, "Error: conflicting module file for {name}: {candidate} {try_file}")
312 end
313 end
314 end
315 if candidate == null then
316 if origmmodule != null then
317 error(anode, "Error: cannot find module {name} from {origmmodule}. tried {tries.join(", ")}")
318 else
319 error(anode, "Error: cannot find module {name}. tried {tries.join(", ")}")
320 end
321 return null
322 end
323 var res = self.load_module(mmodule, candidate)
324 if res == null then return null # Forward error
325 return res.mmodule.as(not null)
326 end
327
328 # Try to load a module using a path.
329 # Display an error if there is a problem (IO / lexer / parser) and return null
330 # Note: usually, you do not need this method, use `get_mmodule_by_name` instead.
331 fun load_module(owner: nullable MModule, filename: String): nullable AModule
332 do
333 if not filename.file_exists then
334 self.toolcontext.error(null, "Error: file {filename} not found.")
335 return null
336 end
337
338 var x = if owner != null then owner.to_s else "."
339 self.toolcontext.info("load module {filename} in {x}", 2)
340
341 # Load the file
342 var file = new IFStream.open(filename)
343 var lexer = new Lexer(new SourceFile(filename, file))
344 var parser = new Parser(lexer)
345 var tree = parser.parse
346 file.close
347
348 # Handle lexer and parser error
349 var nmodule = tree.n_base
350 if nmodule == null then
351 var neof = tree.n_eof
352 assert neof isa AError
353 error(neof, neof.message)
354 return null
355 end
356
357 # Check the module name
358 var mod_name = filename.basename(".nit")
359 var decl = nmodule.n_moduledecl
360 if decl == null then
361 #warning(nmodule, "Warning: Missing 'module' keyword") #FIXME: NOT YET FOR COMPATIBILITY
362 else
363 var decl_name = decl.n_name.n_id.text
364 if decl_name != mod_name then
365 error(decl.n_name, "Error: module name missmatch; declared {decl_name} file named {mod_name}")
366 end
367 end
368
369 # Create the module
370 var mmodule = new MModule(model, owner, mod_name, nmodule.location)
371 nmodule.mmodule = mmodule
372 nmodules.add(nmodule)
373 self.mmodule2nmodule[mmodule] = nmodule
374
375 build_module_importation(nmodule)
376
377 return nmodule
378 end
379
380 # Analysis the module importation and fill the module_importation_hierarchy
381 private fun build_module_importation(nmodule: AModule)
382 do
383 if nmodule.is_importation_done then return
384 var mmodule = nmodule.mmodule.as(not null)
385 var stdimport = true
386 var imported_modules = new Array[MModule]
387 for aimport in nmodule.n_imports do
388 stdimport = false
389 if not aimport isa AStdImport then
390 continue
391 end
392 var mod_name = aimport.n_name.n_id.text
393 var sup = self.get_mmodule_by_name(aimport.n_name, mmodule, mod_name)
394 if sup == null then continue # Skip error
395 imported_modules.add(sup)
396 var mvisibility = aimport.n_visibility.mvisibility
397 mmodule.set_visibility_for(sup, mvisibility)
398 end
399 if stdimport then
400 var mod_name = "standard"
401 var sup = self.get_mmodule_by_name(nmodule, null, mod_name)
402 if sup != null then # Skip error
403 imported_modules.add(sup)
404 mmodule.set_visibility_for(sup, public_visibility)
405 end
406 end
407 self.toolcontext.info("{mmodule} imports {imported_modules.join(", ")}", 3)
408 mmodule.set_imported_mmodules(imported_modules)
409 nmodule.is_importation_done = true
410 end
411
412 # All the loaded modules
413 var nmodules: Array[AModule] = new Array[AModule]
414
415 # Build the classes of all modules `nmodules'.
416 private fun build_all_classes
417 do
418 for nmodule in self.nmodules do
419 build_classes(nmodule)
420 end
421 end
422
423 # Visit the AST and create the MClass objects
424 private fun build_a_mclass(nmodule: AModule, nclassdef: AClassdef)
425 do
426 var mmodule = nmodule.mmodule.as(not null)
427
428 var name: String
429 var nkind: nullable AClasskind
430 var mkind: MClassKind
431 var nvisibility: nullable AVisibility
432 var mvisibility: nullable MVisibility
433 var arity = 0
434 if nclassdef isa AStdClassdef then
435 name = nclassdef.n_id.text
436 nkind = nclassdef.n_classkind
437 mkind = nkind.mkind
438 nvisibility = nclassdef.n_visibility
439 mvisibility = nvisibility.mvisibility
440 arity = nclassdef.n_formaldefs.length
441 else if nclassdef isa ATopClassdef then
442 name = "Object"
443 nkind = null
444 mkind = interface_kind
445 nvisibility = null
446 mvisibility = public_visibility
447 else if nclassdef isa AMainClassdef then
448 name = "Sys"
449 nkind = null
450 mkind = concrete_kind
451 nvisibility = null
452 mvisibility = public_visibility
453 else
454 abort
455 end
456
457 var mclass = try_get_mclass_by_name(nclassdef, mmodule, name)
458 if mclass == null then
459 mclass = new MClass(mmodule, name, arity, mkind, mvisibility)
460 #print "new class {mclass}"
461 else if nclassdef isa AStdClassdef and nclassdef.n_kwredef == null then
462 error(nclassdef, "Redef error: {name} is an imported class. Add the redef keyword to refine it.")
463 return
464 else if mclass.arity != arity then
465 error(nclassdef, "Redef error: Formal parameter arity missmatch; got {arity}, expected {mclass.arity}.")
466 return
467 else if nkind != null and mkind != concrete_kind and mclass.kind != mkind then
468 error(nkind, "Error: refinement changed the kind from a {mclass.kind} to a {mkind}")
469 else if nvisibility != null and mvisibility != public_visibility and mclass.visibility != mvisibility then
470 error(nvisibility, "Error: refinement changed the visibility from a {mclass.visibility} to a {mvisibility}")
471 end
472 nclassdef.mclass = mclass
473 end
474
475 # Visit the AST and create the MClassDef objects
476 private fun build_a_mclassdef(nmodule: AModule, nclassdef: AClassdef)
477 do
478 var mmodule = nmodule.mmodule.as(not null)
479 var objectclass = try_get_mclass_by_name(nmodule, mmodule, "Object")
480 var mclass = nclassdef.mclass.as(not null)
481 #var mclassdef = nclassdef.mclassdef.as(not null)
482
483 var names = new Array[String]
484 var bounds = new Array[MType]
485 if nclassdef isa AStdClassdef and mclass.arity > 0 then
486 # Collect formal parameter names
487 for i in [0..mclass.arity[ do
488 var nfd = nclassdef.n_formaldefs[i]
489 var ptname = nfd.n_id.text
490 if names.has(ptname) then
491 error(nfd, "Error: A formal parameter type `{ptname}' already exists")
492 return
493 end
494 names.add(ptname)
495 end
496
497 # Revolve bound for formal parameter names
498 for i in [0..mclass.arity[ do
499 var nfd = nclassdef.n_formaldefs[i]
500 var nfdt = nfd.n_type
501 if nfdt != null then
502 var bound = resolve_mtype(nclassdef, nfdt)
503 if bound == null then return # Forward error
504 if bound.need_anchor then
505 # No F-bounds!
506 error(nfd, "Error: Formal parameter type `{names[i]}' bounded with a formal parameter type")
507 else
508 bounds.add(bound)
509 end
510 else if mclass.mclassdefs.is_empty then
511 # No bound, then implicitely bound by nullable Object
512 bounds.add(objectclass.mclass_type.as_nullable)
513 else
514 # Inherit the bound
515 bounds.add(mclass.mclassdefs.first.bound_mtype.as(MGenericType).arguments[i])
516 end
517 end
518 end
519
520 var bound_mtype = mclass.get_mtype(bounds)
521 var mclassdef = new MClassDef(mmodule, bound_mtype, nclassdef.location, names)
522 nclassdef.mclassdef = mclassdef
523 self.mclassdef2nclassdef[mclassdef] = nclassdef
524
525 if mclassdef.is_intro then
526 self.toolcontext.info("{mclassdef} introduces new {mclass.kind} {mclass.full_name}", 3)
527 else
528 self.toolcontext.info("{mclassdef} refine {mclass.kind} {mclass.full_name}", 3)
529 end
530 end
531
532 # Visit the AST and set the super-types of the MClass objects (ie compute the inheritance)
533 private fun build_a_mclassdef_inheritance(nmodule: AModule, nclassdef: AClassdef)
534 do
535 var mmodule = nmodule.mmodule.as(not null)
536 var objectclass = try_get_mclass_by_name(nmodule, mmodule, "Object")
537 var mclass = nclassdef.mclass.as(not null)
538 var mclassdef = nclassdef.mclassdef.as(not null)
539
540 var specobject = true
541 var supertypes = new Array[MClassType]
542 if nclassdef isa AStdClassdef then
543 for nsc in nclassdef.n_superclasses do
544 specobject = false
545 var ntype = nsc.n_type
546 var mtype = resolve_mtype(nclassdef, ntype)
547 if mtype == null then continue # Skip because of error
548 if not mtype isa MClassType then
549 error(ntype, "Error: supertypes cannot be a formal type")
550 return
551 end
552 supertypes.add mtype
553 #print "new super : {mclass} < {mtype}"
554 end
555 end
556 if specobject and mclass.name != "Object" and objectclass != null and mclassdef.is_intro then
557 supertypes.add objectclass.mclass_type
558 end
559
560 mclassdef.set_supertypes(supertypes)
561 if not supertypes.is_empty then self.toolcontext.info("{mclassdef} new super-types: {supertypes.join(", ")}", 3)
562 end
563
564 # Check the validity of the specialization heirarchy
565 # FIXME Stub implementation
566 private fun check_supertypes(nmodule: AModule, nclassdef: AClassdef)
567 do
568 var mmodule = nmodule.mmodule.as(not null)
569 var objectclass = try_get_mclass_by_name(nmodule, mmodule, "Object")
570 var mclass = nclassdef.mclass.as(not null)
571 var mclassdef = nclassdef.mclassdef.as(not null)
572 end
573
574 # Build the classes of the module `nmodule'.
575 # REQUIRE: classes of imported modules are already build. (let `build_all_classes' do the job)
576 private fun build_classes(nmodule: AModule)
577 do
578 # Force building recursively
579 if nmodule.build_classes_is_done then return
580 var mmodule = nmodule.mmodule.as(not null)
581 for imp in mmodule.in_importation.direct_greaters do
582 build_classes(mmodule2nmodule[imp])
583 end
584
585 # Create all classes
586 for nclassdef in nmodule.n_classdefs do
587 self.build_a_mclass(nmodule, nclassdef)
588 end
589
590 # Create all classdefs
591 for nclassdef in nmodule.n_classdefs do
592 self.build_a_mclassdef(nmodule, nclassdef)
593 end
594
595 # Create inheritance on all classdefs
596 for nclassdef in nmodule.n_classdefs do
597 self.build_a_mclassdef_inheritance(nmodule, nclassdef)
598 end
599
600 # TODO: Check that the super-class is not intrusive
601
602 # TODO: Check that the super-class is not already known (by transitivity)
603
604 for nclassdef in nmodule.n_classdefs do
605 self.build_properties(nclassdef)
606 end
607
608 nmodule.build_classes_is_done = true
609 end
610
611 # Register the nmodule associated to each mmodule
612 # FIXME: why not refine the MModule class with a nullable attribute?
613 var mmodule2nmodule: HashMap[MModule, AModule] = new HashMap[MModule, AModule]
614 # Register the nclassdef associated to each mclassdef
615 # FIXME: why not refine the MClassDef class with a nullable attribute?
616 var mclassdef2nclassdef: HashMap[MClassDef, AClassdef] = new HashMap[MClassDef, AClassdef]
617 # Register the npropdef associated to each mpropdef
618 # FIXME: why not refine the MPropDef class with a nullable attribute?
619 var mpropdef2npropdef: HashMap[MPropDef, APropdef] = new HashMap[MPropDef, APropdef]
620
621 # Build the properties of `nclassdef'.
622 # REQUIRE: all superclasses are built.
623 private fun build_properties(nclassdef: AClassdef)
624 do
625 # Force building recursively
626 if nclassdef.build_properties_is_done then return
627 var mclassdef = nclassdef.mclassdef.as(not null)
628 if mclassdef.in_hierarchy == null then return # Skip error
629 for superclassdef in mclassdef.in_hierarchy.direct_greaters do
630 build_properties(mclassdef2nclassdef[superclassdef])
631 end
632
633 for npropdef in nclassdef.n_propdefs do
634 npropdef.build_property(self, nclassdef)
635 end
636 for npropdef in nclassdef.n_propdefs do
637 npropdef.build_signature(self, nclassdef)
638 end
639 for npropdef in nclassdef.n_propdefs do
640 npropdef.check_signature(self, nclassdef)
641 end
642 process_default_constructors(nclassdef)
643 nclassdef.build_properties_is_done = true
644 end
645
646 # Introduce or inherit default constructor
647 # This is the last part of `build_properties'.
648 private fun process_default_constructors(nclassdef: AClassdef)
649 do
650 var mclassdef = nclassdef.mclassdef.as(not null)
651
652 # Are we a refinement
653 if not mclassdef.is_intro then return
654
655 # Is the class forbid constructors?
656 if not mclassdef.mclass.kind.need_init then return
657
658 # Is there already a constructor defined?
659 for mpropdef in mclassdef.mpropdefs do
660 if not mpropdef isa MMethodDef then continue
661 if mpropdef.mproperty.is_init then return
662 end
663
664 if not nclassdef isa AStdClassdef then return
665
666 var mmodule = nclassdef.mclassdef.mmodule
667 # Do we inherit for a constructor?
668 var combine = new Array[MMethod]
669 var inhc: nullable MClass = null
670 for st in mclassdef.supertypes do
671 var c = st.mclass
672 if not c.kind.need_init then continue
673 st = st.anchor_to(mmodule, nclassdef.mclassdef.bound_mtype)
674 var candidate = self.try_get_mproperty_by_name2(nclassdef, mmodule, st, "init").as(nullable MMethod)
675 if candidate != null and candidate.intro.msignature.arity == 0 then
676 combine.add(candidate)
677 continue
678 end
679 var inhc2 = c.inherit_init_from
680 if inhc2 == null then inhc2 = c
681 if inhc2 == inhc then continue
682 if inhc != null then
683 self.error(nclassdef, "Cannot provide a defaut constructor: conflict for {inhc} and {c}")
684 else
685 inhc = inhc2
686 end
687 end
688 if combine.is_empty and inhc != null then
689 # TODO: actively inherit the consturctor
690 self.toolcontext.info("{mclassdef} inherits all constructors from {inhc}", 3)
691 mclassdef.mclass.inherit_init_from = inhc
692 return
693 end
694 if not combine.is_empty and inhc != null then
695 self.error(nclassdef, "Cannot provide a defaut constructor: conflict for {combine.join(", ")} and {inhc}")
696 return
697 end
698
699 if not combine.is_empty then
700 nclassdef.super_inits = combine
701 var mprop = new MMethod(mclassdef, "init", mclassdef.mclass.visibility)
702 var mpropdef = new MMethodDef(mclassdef, mprop, nclassdef.location)
703 var param_names = new Array[String]
704 var param_types = new Array[MType]
705 var msignature = new MSignature(param_names, param_types, null, -1)
706 mpropdef.msignature = msignature
707 mprop.is_init = true
708 nclassdef.mfree_init = mpropdef
709 self.toolcontext.info("{mclassdef} gets a free empty constructor {mpropdef}{msignature}", 3)
710 return
711 end
712
713 # Collect undefined attributes
714 var param_names = new Array[String]
715 var param_types = new Array[MType]
716 for npropdef in nclassdef.n_propdefs do
717 if npropdef isa AAttrPropdef and npropdef.n_expr == null then
718 param_names.add(npropdef.mpropdef.mproperty.name.substring_from(1))
719 var ret_type = npropdef.mpropdef.static_mtype
720 if ret_type == null then return
721 param_types.add(ret_type)
722 end
723 end
724
725 var mprop = new MMethod(mclassdef, "init", mclassdef.mclass.visibility)
726 var mpropdef = new MMethodDef(mclassdef, mprop, nclassdef.location)
727 var msignature = new MSignature(param_names, param_types, null, -1)
728 mpropdef.msignature = msignature
729 mprop.is_init = true
730 nclassdef.mfree_init = mpropdef
731 self.toolcontext.info("{mclassdef} gets a free constructor for attributes {mpropdef}{msignature}", 3)
732 end
733
734 # Return the static type associated to the node `ntype'.
735 # `classdef' is the context where the call is made (used to understand formal types)
736 # The mmodule used as context is `nclassdef.mmodule'
737 # In case of problem, an error is displayed on `ntype' and null is returned.
738 # FIXME: the name "resolve_mtype" is awful
739 fun resolve_mtype(nclassdef: AClassdef, ntype: AType): nullable MType
740 do
741 var name = ntype.n_id.text
742 var mclassdef = nclassdef.mclassdef
743 var mmodule = nclassdef.parent.as(AModule).mmodule.as(not null)
744 var res: MType
745
746 # Check virtual type
747 if mclassdef != null then
748 var prop = try_get_mproperty_by_name(ntype, mclassdef, name).as(nullable MVirtualTypeProp)
749 if prop != null then
750 if not ntype.n_types.is_empty then
751 error(ntype, "Type error: formal type {name} cannot have formal parameters.")
752 end
753 res = prop.mvirtualtype
754 if ntype.n_kwnullable != null then res = res.as_nullable
755 return res
756 end
757 end
758
759 # Check parameter type
760 if mclassdef != null and mclassdef.parameter_names.has(name) then
761 if not ntype.n_types.is_empty then
762 error(ntype, "Type error: formal type {name} cannot have formal parameters.")
763 end
764 for i in [0..mclassdef.parameter_names.length[ do
765 if mclassdef.parameter_names[i] == name then
766 res = mclassdef.mclass.mclass_type.as(MGenericType).arguments[i]
767 if ntype.n_kwnullable != null then res = res.as_nullable
768 return res
769 end
770 end
771 abort
772 end
773
774 # Check class
775 var mclass = try_get_mclass_by_name(ntype, mmodule, name)
776 if mclass != null then
777 var arity = ntype.n_types.length
778 if arity != mclass.arity then
779 if arity == 0 then
780 error(ntype, "Type error: '{name}' is a generic class.")
781 else if mclass.arity == 0 then
782 error(ntype, "Type error: '{name}' is not a generic class.")
783 else
784 error(ntype, "Type error: '{name}' has {mclass.arity} parameters ({arity} are provided).")
785 end
786 return null
787 end
788 if arity == 0 then
789 res = mclass.mclass_type
790 if ntype.n_kwnullable != null then res = res.as_nullable
791 return res
792 else
793 var mtypes = new Array[MType]
794 for nt in ntype.n_types do
795 var mt = resolve_mtype(nclassdef, nt)
796 if mt == null then return null # Forward error
797 mtypes.add(mt)
798 end
799 res = mclass.get_mtype(mtypes)
800 if ntype.n_kwnullable != null then res = res.as_nullable
801 return res
802 end
803 end
804
805 # If everything fail, then give up :(
806 error(ntype, "Type error: class {name} not found in module {mmodule}.")
807 return null
808 end
809
810 # Helper function to display an error on a node.
811 # Alias for `self.toolcontext.error(n.hot_location, text)'
812 fun error(n: ANode, text: String)
813 do
814 self.toolcontext.error(n.hot_location, text)
815 end
816
817 # Helper function to display a warning on a node.
818 # Alias for: `self.toolcontext.warning(n.hot_location, text)'
819 fun warning(n: ANode, text: String)
820 do
821 self.toolcontext.warning(n.hot_location, text)
822 end
823 end
824
825 redef class AModule
826 # The associated MModule once build by a `ModelBuilder'
827 var mmodule: nullable MModule
828 # Flag that indicate if the importation is already completed
829 var is_importation_done: Bool = false
830 # Flag that indicate if the class and prop building is already completed
831 var build_classes_is_done: Bool = false
832 end
833
834 redef class MClass
835 # The class whose self inherit all the constructors.
836 # 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
837 var inherit_init_from: nullable MClass = null
838 end
839
840 redef class AClassdef
841 # The associated MClass once build by a `ModelBuilder'
842 var mclass: nullable MClass
843 # The associated MClassDef once build by a `ModelBuilder'
844 var mclassdef: nullable MClassDef
845 var build_properties_is_done: Bool = false
846 # The list of super-constructor to call at the start of the free constructor
847 # 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
848 var super_inits: nullable Collection[MMethod] = null
849
850 # The free init (implicitely constructed by the class if required)
851 var mfree_init: nullable MMethodDef = null
852 end
853
854 redef class AClasskind
855 # The class kind associated with the AST node class
856 private fun mkind: MClassKind is abstract
857 end
858 redef class AConcreteClasskind
859 redef fun mkind do return concrete_kind
860 end
861 redef class AAbstractClasskind
862 redef fun mkind do return abstract_kind
863 end
864 redef class AInterfaceClasskind
865 redef fun mkind do return interface_kind
866 end
867 redef class AEnumClasskind
868 redef fun mkind do return enum_kind
869 end
870 redef class AExternClasskind
871 redef fun mkind do return extern_kind
872 end
873
874 redef class AVisibility
875 # The visibility level associated with the AST node class
876 private fun mvisibility: MVisibility is abstract
877 end
878 redef class AIntrudeVisibility
879 redef fun mvisibility do return intrude_visibility
880 end
881 redef class APublicVisibility
882 redef fun mvisibility do return public_visibility
883 end
884 redef class AProtectedVisibility
885 redef fun mvisibility do return protected_visibility
886 end
887 redef class APrivateVisibility
888 redef fun mvisibility do return private_visibility
889 end
890
891
892 #
893
894 redef class Prod
895 # Join the text of all tokens
896 # Used to get the 'real name' of method definitions.
897 fun collect_text: String
898 do
899 var v = new TextCollectorVisitor
900 v.enter_visit(self)
901 assert v.text != ""
902 return v.text
903 end
904 end
905
906 private class TextCollectorVisitor
907 super Visitor
908 var text: String = ""
909 redef fun visit(n)
910 do
911 if n isa Token then text += n.text
912 n.visit_all(self)
913 end
914 end
915
916 redef class APropdef
917 private fun build_property(modelbuilder: ModelBuilder, nclassdef: AClassdef)
918 do
919 end
920 private fun build_signature(modelbuilder: ModelBuilder, nclassdef: AClassdef)
921 do
922 end
923 private fun check_signature(modelbuilder: ModelBuilder, nclassdef: AClassdef)
924 do
925 end
926 private fun new_property_visibility(modelbuilder: ModelBuilder, nclassdef: AClassdef, nvisibility: nullable AVisibility): MVisibility
927 do
928 var mvisibility = public_visibility
929 if nvisibility != null then mvisibility = nvisibility.mvisibility
930 if nclassdef.mclassdef.mclass.visibility == private_visibility then
931 if mvisibility == protected_visibility then
932 assert nvisibility != null
933 modelbuilder.error(nvisibility, "Error: The only legal visibility for properties in a private class is private.")
934 else if mvisibility == private_visibility then
935 assert nvisibility != null
936 # Not yet
937 # modelbuilder.warning(nvisibility, "Warning: private is unrequired since the only legal visibility for properties in a private class is private.")
938 end
939 mvisibility = private_visibility
940 end
941 return mvisibility
942 end
943
944 private fun check_redef_property_visibility(modelbuilder: ModelBuilder, nclassdef: AClassdef, nvisibility: nullable AVisibility, mprop: MProperty)
945 do
946 if nvisibility == null then return
947 var mvisibility = nvisibility.mvisibility
948 if mvisibility != mprop.visibility and mvisibility != public_visibility then
949 modelbuilder.error(nvisibility, "Error: redefinition changed the visibility from a {mprop.visibility} to a {mvisibility}")
950 end
951 end
952
953 private fun check_redef_keyword(modelbuilder: ModelBuilder, nclassdef: AClassdef, kwredef: nullable Token, need_redef: Bool, mprop: MProperty)
954 do
955 if kwredef == null then
956 if need_redef then
957 modelbuilder.error(self, "Redef error: {nclassdef.mclassdef.mclass}::{mprop.name} is an inherited property. To redefine it, add the redef keyword.")
958 end
959 else
960 if not need_redef then
961 modelbuilder.error(self, "Error: No property {nclassdef.mclassdef.mclass}::{mprop.name} is inherited. Remove the redef keyword to define a new property.")
962 end
963 end
964 end
965 end
966
967 redef class ASignature
968 # Is the model builder has correctly visited the signature
969 var is_visited = false
970 # Names of parameters from the AST
971 # REQUIRE: is_visited
972 var param_names = new Array[String]
973 # Types of parameters from the AST
974 # REQUIRE: is_visited
975 var param_types = new Array[MType]
976 # Rank of the vararg (of -1 if none)
977 # REQUIRE: is_visited
978 var vararg_rank: Int = -1
979 # Return type
980 var ret_type: nullable MType = null
981
982 # Visit and fill information about a signature
983 private fun visit_signature(modelbuilder: ModelBuilder, nclassdef: AClassdef): Bool
984 do
985 var param_names = self.param_names
986 var param_types = self.param_types
987 for np in self.n_params do
988 param_names.add(np.n_id.text)
989 var ntype = np.n_type
990 if ntype != null then
991 var mtype = modelbuilder.resolve_mtype(nclassdef, ntype)
992 if mtype == null then return false # Skip error
993 for i in [0..param_names.length-param_types.length[ do
994 param_types.add(mtype)
995 end
996 if np.n_dotdotdot != null then
997 if self.vararg_rank != -1 then
998 modelbuilder.error(np, "Error: {param_names[self.vararg_rank]} is already a vararg")
999 return false
1000 else
1001 self.vararg_rank = param_names.length - 1
1002 end
1003 end
1004 end
1005 end
1006 var ntype = self.n_type
1007 if ntype != null then
1008 self.ret_type = modelbuilder.resolve_mtype(nclassdef, ntype)
1009 if self.ret_type == null then return false # Skip errir
1010 end
1011 self.is_visited = true
1012 return true
1013 end
1014 end
1015
1016 redef class AMethPropdef
1017 # The associated MMethodDef once build by a `ModelBuilder'
1018 var mpropdef: nullable MMethodDef
1019
1020 # The associated super init if any
1021 var super_init: nullable MMethod
1022 redef fun build_property(modelbuilder, nclassdef)
1023 do
1024 var is_init = self isa AInitPropdef
1025 var mclassdef = nclassdef.mclassdef.as(not null)
1026 var name: String
1027 var amethodid = self.n_methid
1028 var name_node: ANode
1029 if amethodid == null then
1030 if self isa AMainMethPropdef then
1031 name = "main"
1032 name_node = self
1033 else if self isa AConcreteInitPropdef then
1034 name = "init"
1035 name_node = self.n_kwinit
1036 else if self isa AExternInitPropdef then
1037 name = "new"
1038 name_node = self.n_kwnew
1039 else
1040 abort
1041 end
1042 else if amethodid isa AIdMethid then
1043 name = amethodid.n_id.text
1044 name_node = amethodid
1045 else
1046 # operator, bracket or assign
1047 name = amethodid.collect_text
1048 name_node = amethodid
1049
1050 if name == "-" and self.n_signature.n_params.length == 0 then
1051 name = "unary -"
1052 end
1053 end
1054
1055 var mprop: nullable MMethod = null
1056 if not is_init or n_kwredef != null then mprop = modelbuilder.try_get_mproperty_by_name(name_node, mclassdef, name).as(nullable MMethod)
1057 if mprop == null then
1058 var mvisibility = new_property_visibility(modelbuilder, nclassdef, self.n_visibility)
1059 mprop = new MMethod(mclassdef, name, mvisibility)
1060 mprop.is_init = is_init
1061 mprop.is_new = self isa AExternInitPropdef
1062 self.check_redef_keyword(modelbuilder, nclassdef, n_kwredef, false, mprop)
1063 else
1064 if n_kwredef == null then
1065 if self isa AMainMethPropdef then
1066 # no warning
1067 else
1068 self.check_redef_keyword(modelbuilder, nclassdef, n_kwredef, true, mprop)
1069 end
1070 end
1071 check_redef_property_visibility(modelbuilder, nclassdef, self.n_visibility, mprop)
1072 end
1073
1074 var mpropdef = new MMethodDef(mclassdef, mprop, self.location)
1075
1076 self.mpropdef = mpropdef
1077 modelbuilder.mpropdef2npropdef[mpropdef] = self
1078 if mpropdef.is_intro then
1079 modelbuilder.toolcontext.info("{mpropdef} introduces new method {mprop.full_name}", 3)
1080 else
1081 modelbuilder.toolcontext.info("{mpropdef} redefines method {mprop.full_name}", 3)
1082 end
1083 end
1084
1085 redef fun build_signature(modelbuilder, nclassdef)
1086 do
1087 var mpropdef = self.mpropdef
1088 if mpropdef == null then return # Error thus skiped
1089 var mmodule = mpropdef.mclassdef.mmodule
1090 var nsig = self.n_signature
1091
1092 # Retrieve info from the signature AST
1093 var param_names = new Array[String] # Names of parameters from the AST
1094 var param_types = new Array[MType] # Types of parameters from the AST
1095 var vararg_rank = -1
1096 var ret_type: nullable MType = null # Return type from the AST
1097 if nsig != null then
1098 if not nsig.visit_signature(modelbuilder, nclassdef) then return
1099 param_names = nsig.param_names
1100 param_types = nsig.param_types
1101 vararg_rank = nsig.vararg_rank
1102 ret_type = nsig.ret_type
1103 end
1104
1105 # Look for some signature to inherit
1106 # FIXME: do not inherit from the intro, but from the most specific
1107 var msignature: nullable MSignature = null
1108 if not mpropdef.is_intro then
1109 msignature = mpropdef.mproperty.intro.msignature
1110 if msignature == null then return # Skip error
1111 else if mpropdef.mproperty.is_init then
1112 # FIXME UGLY: inherit signature from a super-constructor
1113 for msupertype in nclassdef.mclassdef.supertypes do
1114 msupertype = msupertype.anchor_to(mmodule, nclassdef.mclassdef.bound_mtype)
1115 var candidate = modelbuilder.try_get_mproperty_by_name2(self, mmodule, msupertype, mpropdef.mproperty.name)
1116 if candidate != null then
1117 if msignature == null then
1118 msignature = candidate.intro.as(MMethodDef).msignature
1119 end
1120 end
1121 end
1122 end
1123
1124 # Inherit the signature
1125 if msignature != null and param_names.length != param_types.length and param_names.length == msignature.arity and param_types.length == 0 then
1126 # Parameters are untyped, thus inherit them
1127 param_types = msignature.parameter_mtypes
1128 vararg_rank = msignature.vararg_rank
1129 end
1130 if msignature != null and ret_type == null then
1131 ret_type = msignature.return_mtype
1132 end
1133
1134 if param_names.length != param_types.length then
1135 # Some parameters are typed, other parameters are not typed.
1136 modelbuilder.warning(nsig.n_params[param_types.length], "Error: Untyped parameter `{param_names[param_types.length]}'.")
1137 return
1138 end
1139
1140 msignature = new MSignature(param_names, param_types, ret_type, vararg_rank)
1141 mpropdef.msignature = msignature
1142 end
1143
1144 redef fun check_signature(modelbuilder, nclassdef)
1145 do
1146 var mpropdef = self.mpropdef
1147 if mpropdef == null then return # Error thus skiped
1148 var mmodule = mpropdef.mclassdef.mmodule
1149 var nsig = self.n_signature
1150 var mysignature = self.mpropdef.msignature
1151 if mysignature == null then return # Error thus skiped
1152
1153 # Lookup for signature in the precursor
1154 # FIXME all precursors should be considered
1155 if not mpropdef.is_intro then
1156 var msignature = mpropdef.mproperty.intro.msignature
1157 if msignature == null then return
1158
1159 if mysignature.arity != msignature.arity then
1160 var node: ANode
1161 if nsig != null then node = nsig else node = self
1162 modelbuilder.error(node, "Redef Error: {mysignature.arity} parameters found, {msignature.arity} expected. Signature is {mpropdef}{msignature}")
1163 return
1164 end
1165 var precursor_ret_type = msignature.return_mtype
1166 var ret_type = mysignature.return_mtype
1167 if ret_type != null and precursor_ret_type == null then
1168 modelbuilder.error(nsig.n_type.as(not null), "Redef Error: {mpropdef.mproperty} is a procedure, not a function.")
1169 return
1170 end
1171
1172 if mysignature.arity > 0 then
1173 # Check parameters types
1174 for i in [0..mysignature.arity[ do
1175 var myt = mysignature.parameter_mtypes[i]
1176 var prt = msignature.parameter_mtypes[i]
1177 if not myt.is_subtype(mmodule, nclassdef.mclassdef.bound_mtype, prt) and
1178 not prt.is_subtype(mmodule, nclassdef.mclassdef.bound_mtype, myt) then
1179 modelbuilder.error(nsig.n_params[i], "Redef Error: Wrong type for parameter `{mysignature.parameter_names[i]}'. found {myt}, expected {prt}.")
1180 end
1181 end
1182 end
1183 if precursor_ret_type != null then
1184 if ret_type == null then
1185 # Inherit the return type
1186 ret_type = precursor_ret_type
1187 else if not ret_type.is_subtype(mmodule, nclassdef.mclassdef.bound_mtype, precursor_ret_type) then
1188 modelbuilder.error(nsig.n_type.as(not null), "Redef Error: Wrong return type. found {ret_type}, expected {precursor_ret_type}.")
1189 end
1190 end
1191 end
1192 end
1193 end
1194
1195 redef class AAttrPropdef
1196 # The associated MAttributeDef once build by a `ModelBuilder'
1197 var mpropdef: nullable MAttributeDef
1198 # The associated getter (read accessor) if any
1199 var mreadpropdef: nullable MMethodDef
1200 # The associated setter (write accessor) if any
1201 var mwritepropdef: nullable MMethodDef
1202 redef fun build_property(modelbuilder, nclassdef)
1203 do
1204 var mclassdef = nclassdef.mclassdef.as(not null)
1205 var mclass = mclassdef.mclass
1206
1207 var name: String
1208 if self.n_id != null then
1209 name = self.n_id.text
1210 else
1211 name = self.n_id2.text
1212 end
1213
1214 if mclass.kind == interface_kind or mclassdef.mclass.kind == enum_kind then
1215 modelbuilder.error(self, "Error: Attempt to define attribute {name} in the interface {mclass}.")
1216 else if mclass.kind == enum_kind then
1217 modelbuilder.error(self, "Error: Attempt to define attribute {name} in the enum class {mclass}.")
1218 end
1219
1220 var nid = self.n_id
1221 if nid != null then
1222 # Old attribute style
1223 var mprop = modelbuilder.try_get_mproperty_by_name(nid, mclassdef, name)
1224 if mprop == null then
1225 var mvisibility = new_property_visibility(modelbuilder, nclassdef, self.n_visibility)
1226 mprop = new MAttribute(mclassdef, name, mvisibility)
1227 self.check_redef_keyword(modelbuilder, nclassdef, self.n_kwredef, false, mprop)
1228 else
1229 assert mprop isa MAttribute
1230 check_redef_property_visibility(modelbuilder, nclassdef, self.n_visibility, mprop)
1231 self.check_redef_keyword(modelbuilder, nclassdef, self.n_kwredef, true, mprop)
1232 end
1233 var mpropdef = new MAttributeDef(mclassdef, mprop, self.location)
1234 self.mpropdef = mpropdef
1235 modelbuilder.mpropdef2npropdef[mpropdef] = self
1236
1237 var nreadable = self.n_readable
1238 if nreadable != null then
1239 var readname = name.substring_from(1)
1240 var mreadprop = modelbuilder.try_get_mproperty_by_name(nid, mclassdef, readname).as(nullable MMethod)
1241 if mreadprop == null then
1242 var mvisibility = new_property_visibility(modelbuilder, nclassdef, nreadable.n_visibility)
1243 mreadprop = new MMethod(mclassdef, readname, mvisibility)
1244 self.check_redef_keyword(modelbuilder, nclassdef, nreadable.n_kwredef, false, mreadprop)
1245 else
1246 self.check_redef_keyword(modelbuilder, nclassdef, nreadable.n_kwredef, true, mreadprop)
1247 check_redef_property_visibility(modelbuilder, nclassdef, nreadable.n_visibility, mreadprop)
1248 end
1249 var mreadpropdef = new MMethodDef(mclassdef, mreadprop, self.location)
1250 self.mreadpropdef = mreadpropdef
1251 modelbuilder.mpropdef2npropdef[mreadpropdef] = self
1252 end
1253
1254 var nwritable = self.n_writable
1255 if nwritable != null then
1256 var writename = name.substring_from(1) + "="
1257 var mwriteprop = modelbuilder.try_get_mproperty_by_name(nid, mclassdef, writename).as(nullable MMethod)
1258 if mwriteprop == null then
1259 var mvisibility = new_property_visibility(modelbuilder, nclassdef, nwritable.n_visibility)
1260 mwriteprop = new MMethod(mclassdef, writename, mvisibility)
1261 self.check_redef_keyword(modelbuilder, nclassdef, nwritable.n_kwredef, false, mwriteprop)
1262 else
1263 self.check_redef_keyword(modelbuilder, nclassdef, nwritable.n_kwredef, true, mwriteprop)
1264 check_redef_property_visibility(modelbuilder, nclassdef, nwritable.n_visibility, mwriteprop)
1265 end
1266 var mwritepropdef = new MMethodDef(mclassdef, mwriteprop, self.location)
1267 self.mwritepropdef = mwritepropdef
1268 modelbuilder.mpropdef2npropdef[mwritepropdef] = self
1269 end
1270 else
1271 # New attribute style
1272 var nid2 = self.n_id2.as(not null)
1273 var mprop = new MAttribute(mclassdef, "@" + name, none_visibility)
1274 var mpropdef = new MAttributeDef(mclassdef, mprop, self.location)
1275 self.mpropdef = mpropdef
1276 modelbuilder.mpropdef2npropdef[mpropdef] = self
1277
1278 var readname = name
1279 var mreadprop = modelbuilder.try_get_mproperty_by_name(nid2, mclassdef, readname).as(nullable MMethod)
1280 if mreadprop == null then
1281 var mvisibility = new_property_visibility(modelbuilder, nclassdef, self.n_visibility)
1282 mreadprop = new MMethod(mclassdef, readname, mvisibility)
1283 self.check_redef_keyword(modelbuilder, nclassdef, n_kwredef, false, mreadprop)
1284 else
1285 self.check_redef_keyword(modelbuilder, nclassdef, n_kwredef, true, mreadprop)
1286 check_redef_property_visibility(modelbuilder, nclassdef, self.n_visibility, mreadprop)
1287 end
1288 var mreadpropdef = new MMethodDef(mclassdef, mreadprop, self.location)
1289 self.mreadpropdef = mreadpropdef
1290 modelbuilder.mpropdef2npropdef[mreadpropdef] = self
1291
1292 var writename = name + "="
1293 var nwritable = self.n_writable
1294 var mwriteprop = modelbuilder.try_get_mproperty_by_name(nid2, mclassdef, writename).as(nullable MMethod)
1295 var nwkwredef: nullable Token = null
1296 if nwritable != null then nwkwredef = nwritable.n_kwredef
1297 if mwriteprop == null then
1298 var mvisibility
1299 if nwritable != null then
1300 mvisibility = new_property_visibility(modelbuilder, nclassdef, nwritable.n_visibility)
1301 else
1302 mvisibility = private_visibility
1303 end
1304 mwriteprop = new MMethod(mclassdef, writename, mvisibility)
1305 self.check_redef_keyword(modelbuilder, nclassdef, nwkwredef, false, mwriteprop)
1306 else
1307 self.check_redef_keyword(modelbuilder, nclassdef, nwkwredef, true, mwriteprop)
1308 if nwritable != null then
1309 check_redef_property_visibility(modelbuilder, nclassdef, nwritable.n_visibility, mwriteprop)
1310 end
1311 end
1312 var mwritepropdef = new MMethodDef(mclassdef, mwriteprop, self.location)
1313 self.mwritepropdef = mwritepropdef
1314 modelbuilder.mpropdef2npropdef[mwritepropdef] = self
1315 end
1316 end
1317
1318 redef fun build_signature(modelbuilder, nclassdef)
1319 do
1320 var mpropdef = self.mpropdef
1321 if mpropdef == null then return # Error thus skiped
1322 var mmodule = mpropdef.mclassdef.mmodule
1323 var mtype: nullable MType = null
1324
1325 var ntype = self.n_type
1326 if ntype != null then
1327 mtype = modelbuilder.resolve_mtype(nclassdef, ntype)
1328 if mtype == null then return
1329 end
1330
1331 if mtype == null then
1332 var nexpr = self.n_expr
1333 if nexpr != null then
1334 if nexpr isa ANewExpr then
1335 mtype = modelbuilder.resolve_mtype(nclassdef, nexpr.n_type)
1336 else if nexpr isa AIntExpr then
1337 var cla = modelbuilder.try_get_mclass_by_name(nexpr, mmodule, "Int")
1338 if cla != null then mtype = cla.mclass_type
1339 else if nexpr isa AFloatExpr then
1340 var cla = modelbuilder.try_get_mclass_by_name(nexpr, mmodule, "Float")
1341 if cla != null then mtype = cla.mclass_type
1342 else if nexpr isa ACharExpr then
1343 var cla = modelbuilder.try_get_mclass_by_name(nexpr, mmodule, "Char")
1344 if cla != null then mtype = cla.mclass_type
1345 else if nexpr isa ABoolExpr then
1346 var cla = modelbuilder.try_get_mclass_by_name(nexpr, mmodule, "Bool")
1347 if cla != null then mtype = cla.mclass_type
1348 else if nexpr isa ASuperstringExpr then
1349 var cla = modelbuilder.try_get_mclass_by_name(nexpr, mmodule, "String")
1350 if cla != null then mtype = cla.mclass_type
1351 else if nexpr isa AStringFormExpr then
1352 var cla = modelbuilder.try_get_mclass_by_name(nexpr, mmodule, "String")
1353 if cla != null then mtype = cla.mclass_type
1354 else
1355 modelbuilder.error(self, "Error: Untyped attribute {mpropdef}. Implicit typing allowed only for literals and new.")
1356 end
1357
1358 else
1359 modelbuilder.error(self, "Error: Untyped attribute {mpropdef}")
1360 end
1361 end
1362
1363 if mtype == null then return
1364
1365 mpropdef.static_mtype = mtype
1366
1367 var mreadpropdef = self.mreadpropdef
1368 if mreadpropdef != null then
1369 var msignature = new MSignature(new Array[String], new Array[MType], mtype, -1)
1370 mreadpropdef.msignature = msignature
1371 end
1372
1373 var msritepropdef = self.mwritepropdef
1374 if mwritepropdef != null then
1375 var name: String
1376 if n_id != null then
1377 name = n_id.text.substring_from(1)
1378 else
1379 name = n_id2.text
1380 end
1381 var msignature = new MSignature([name], [mtype], null, -1)
1382 mwritepropdef.msignature = msignature
1383 end
1384 end
1385
1386 redef fun check_signature(modelbuilder, nclassdef)
1387 do
1388 var mpropdef = self.mpropdef
1389 if mpropdef == null then return # Error thus skiped
1390 var mmodule = mpropdef.mclassdef.mmodule
1391 var ntype = self.n_type
1392 var mtype = self.mpropdef.static_mtype
1393 if mtype == null then return # Error thus skiped
1394
1395 # Lookup for signature in the precursor
1396 # FIXME all precursors should be considered
1397 if not mpropdef.is_intro then
1398 var precursor_type = mpropdef.mproperty.intro.static_mtype
1399 if precursor_type == null then return
1400
1401 if mtype != precursor_type then
1402 modelbuilder.error(ntype.as(not null), "Redef Error: Wrong static type. found {mtype}, expected {precursor_type}.")
1403 return
1404 end
1405 end
1406
1407 # FIXME: Check getter ans setter
1408 end
1409 end
1410
1411 redef class ATypePropdef
1412 # The associated MVirtualTypeDef once build by a `ModelBuilder'
1413 var mpropdef: nullable MVirtualTypeDef
1414 redef fun build_property(modelbuilder, nclassdef)
1415 do
1416 var mclassdef = nclassdef.mclassdef.as(not null)
1417 var name = self.n_id.text
1418 var mprop = modelbuilder.try_get_mproperty_by_name(self.n_id, mclassdef, name)
1419 if mprop == null then
1420 var mvisibility = new_property_visibility(modelbuilder, nclassdef, self.n_visibility)
1421 mprop = new MVirtualTypeProp(mclassdef, name, mvisibility)
1422 self.check_redef_keyword(modelbuilder, nclassdef, self.n_kwredef, false, mprop)
1423 else
1424 self.check_redef_keyword(modelbuilder, nclassdef, self.n_kwredef, true, mprop)
1425 assert mprop isa MVirtualTypeProp
1426 check_redef_property_visibility(modelbuilder, nclassdef, self.n_visibility, mprop)
1427 end
1428 var mpropdef = new MVirtualTypeDef(mclassdef, mprop, self.location)
1429 self.mpropdef = mpropdef
1430 end
1431
1432 redef fun build_signature(modelbuilder, nclassdef)
1433 do
1434 var mpropdef = self.mpropdef
1435 if mpropdef == null then return # Error thus skiped
1436 var mmodule = mpropdef.mclassdef.mmodule
1437 var mtype: nullable MType = null
1438
1439 var ntype = self.n_type
1440 mtype = modelbuilder.resolve_mtype(nclassdef, ntype)
1441 if mtype == null then return
1442
1443 mpropdef.bound = mtype
1444 # print "{mpropdef}: {mtype}"
1445 end
1446
1447 redef fun check_signature(modelbuilder, nclassdef)
1448 do
1449 var bound = self.mpropdef.bound
1450
1451 # Fast case: the bound is not a formal type
1452 if not bound isa MVirtualType then return
1453
1454 var mmodule = nclassdef.mclassdef.mmodule
1455 var anchor = nclassdef.mclassdef.bound_mtype
1456
1457 # Slow case: progress on each resolution until: (i) we loop, or (ii) we found a non formal type
1458 var seen = [self.mpropdef.mproperty.mvirtualtype]
1459 loop
1460 if seen.has(bound) then
1461 seen.add(bound)
1462 modelbuilder.error(self, "Error: circularity of virtual type definition: {seen.join(" -> ")}")
1463 return
1464 end
1465 seen.add(bound)
1466 var next = bound.lookup_bound(mmodule, anchor)
1467 if not next isa MVirtualType then return
1468 bound = next
1469 end
1470 end
1471 end