1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2012 Jean Privat <jean@pryen.org>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Load nit source files and build the associated model
21 # FIXME split this module into submodules
22 # FIXME add missing error checks
33 redef class ToolContext
35 readable var _opt_path
: OptionArray = new OptionArray("Set include path for loaders (may be used more than once)", "-I", "--path")
37 # Option --only-metamodel
38 readable var _opt_only_metamodel
: OptionBool = new OptionBool("Stop after meta-model processing", "--only-metamodel")
41 readable var _opt_only_parse
: OptionBool = new OptionBool("Only proceed to parse step of loaders", "--only-parse")
46 option_context
.add_option
(opt_path
, opt_only_parse
, opt_only_metamodel
)
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
54 # The model where new modules, classes and properties are added
57 # The toolcontext used to control the interaction with the user (getting options and displaying messages)
58 var toolcontext
: ToolContext
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)
65 self.toolcontext
= toolcontext
67 # Setup the paths value
68 paths
.append
(toolcontext
.opt_path
.value
)
70 var path_env
= "NIT_PATH".environ
71 if not path_env
.is_empty
then
72 paths
.append
(path_env
.split_with
(':'))
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
)
81 var libname
= "{sys.program_name.dirname}/../lib"
82 if libname
.file_exists
then paths
.add
(libname
.simplify_path
)
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.
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]
95 # Parse and recursively load
96 self.toolcontext
.info
("*** PARSE ***", 1)
97 var mmodules
= new Array[MModule]
99 var nmodule
= self.load_module
(null, a
)
100 if nmodule
== null then continue # Skip error
101 mmodules
.add
(nmodule
.mmodule
.as(not null))
104 self.toolcontext
.info
("*** END PARSE: {time1-time0} ***", 2)
106 self.toolcontext
.check_errors
108 if self.toolcontext
.opt_only_parse
.value
then
109 self.toolcontext
.info
("--only-parse: stop processing", 2)
110 return new Array[MModule]
114 self.toolcontext
.info
("*** BUILD MODEL ***", 1)
115 self.build_all_classes
117 self.toolcontext
.info
("*** END BUILD MODEL: {time2-time1} ***", 2)
119 self.toolcontext
.check_errors
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
131 var classes
= model
.get_mclasses_by_name
(name
)
132 if classes
== null then
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
143 error
(anode
, "Ambigous class name '{name}'; conflict between {mclass.full_name} and {res.full_name}")
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
158 var props
= self.model
.get_mproperties_by_name
(name
)
159 if props
== null then
163 var cache
= self.try_get_mproperty_by_name2_cache
[mmodule
, mtype
, name
]
164 if cache
!= null then return cache
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
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
178 else if mproptype
.is_subtype
(mmodule
, null, restype
) then
181 if ress
== null then ress
= new Array[MProperty]
187 var restype
= res
.intro_mclassdef
.bound_mtype
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}")
197 self.try_get_mproperty_by_name2_cache
[mmodule
, mtype
, name
] = res
201 private var try_get_mproperty_by_name2_cache
: HashMap3[MModule, MType, String, nullable MProperty] = new HashMap3[MModule, MType, String, nullable MProperty]
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
207 return try_get_mproperty_by_name2
(anode
, mclassdef
.mmodule
, mclassdef
.bound_mtype
, name
)
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]
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
225 var origmmodule
= mmodule
226 var modules
= model
.get_mmodules_by_name
(name
)
228 var tries
= new Array[String]
230 var lastmodule
= mmodule
231 while mmodule
!= null do
232 var dirname
= mmodule
.location
.file
.filename
.dirname
234 # Determine the owner
235 var owner
: nullable MModule
236 if dirname
.basename
("") != mmodule
.name
then
237 owner
= mmodule
.direct_owner
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
251 # Second, try the directory to find a file
252 var try_file
= dirname
+ "/" + name
+ ".nit"
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)
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)
269 mmodule
= mmodule
.direct_owner
272 if modules
!= null then
273 for candidate
in modules
do
274 if candidate
.direct_owner
== null then
280 # Look at some known directories
281 var lookpaths
= self.paths
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
289 if not lookpaths
.has
(dirname
) then
290 lookpaths
= lookpaths
.to_a
291 lookpaths
.add
(dirname
)
295 var candidate
: nullable String = null
296 for dirname
in lookpaths
do
297 var try_file
= (dirname
+ "/" + name
+ ".nit").simplify_path
299 if try_file
.file_exists
then
300 if candidate
== null then
302 else if candidate
!= try_file
then
303 error
(anode
, "Error: conflicting module file for {name}: {candidate} {try_file}")
306 try_file
= (dirname
+ "/" + name
+ "/" + name
+ ".nit").simplify_path
307 if try_file
.file_exists
then
308 if candidate
== null then
310 else if candidate
!= try_file
then
311 error
(anode
, "Error: conflicting module file for {name}: {candidate} {try_file}")
315 if candidate
== null then
316 if origmmodule
!= null then
317 error
(anode
, "Error: cannot find module {name} from {origmmodule}. tried {tries.join(", ")}")
319 error
(anode
, "Error: cannot find module {name}. tried {tries.join(", ")}")
323 var res
= self.load_module
(mmodule
, candidate
)
324 if res
== null then return null # Forward error
325 return res
.mmodule
.as(not null)
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
333 if not filename
.file_exists
then
334 self.toolcontext
.error
(null, "Error: file {filename} not found.")
338 var x
= if owner
!= null then owner
.to_s
else "."
339 self.toolcontext
.info
("load module {filename} in {x}", 2)
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
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
)
357 # Check the module name
358 var mod_name
= filename
.basename
(".nit")
359 var decl
= nmodule
.n_moduledecl
361 #warning(nmodule, "Warning: Missing 'module' keyword") #FIXME: NOT YET FOR COMPATIBILITY
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}")
370 var mmodule
= new MModule(model
, owner
, mod_name
, nmodule
.location
)
371 nmodule
.mmodule
= mmodule
372 nmodules
.add
(nmodule
)
373 self.mmodule2nmodule
[mmodule
] = nmodule
375 build_module_importation
(nmodule
)
380 # Analysis the module importation and fill the module_importation_hierarchy
381 private fun build_module_importation
(nmodule
: AModule)
383 if nmodule
.is_importation_done
then return
384 var mmodule
= nmodule
.mmodule
.as(not null)
386 var imported_modules
= new Array[MModule]
387 for aimport
in nmodule
.n_imports
do
389 if not aimport
isa AStdImport then
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
)
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
)
407 self.toolcontext
.info
("{mmodule} imports {imported_modules.join(", ")}", 3)
408 mmodule
.set_imported_mmodules
(imported_modules
)
409 nmodule
.is_importation_done
= true
412 # All the loaded modules
413 var nmodules
: Array[AModule] = new Array[AModule]
415 # Build the classes of all modules `nmodules'.
416 private fun build_all_classes
418 for nmodule
in self.nmodules
do
419 build_classes
(nmodule
)
423 # Visit the AST and create the MClass objects
424 private fun build_a_mclass
(nmodule
: AModule, nclassdef
: AClassdef)
426 var mmodule
= nmodule
.mmodule
.as(not null)
429 var nkind
: nullable AClasskind
430 var mkind
: MClassKind
431 var nvisibility
: nullable AVisibility
432 var mvisibility
: nullable MVisibility
434 if nclassdef
isa AStdClassdef then
435 name
= nclassdef
.n_id
.text
436 nkind
= nclassdef
.n_classkind
438 nvisibility
= nclassdef
.n_visibility
439 mvisibility
= nvisibility
.mvisibility
440 arity
= nclassdef
.n_formaldefs
.length
441 else if nclassdef
isa ATopClassdef then
444 mkind
= interface_kind
446 mvisibility
= public_visibility
447 else if nclassdef
isa AMainClassdef then
450 mkind
= concrete_kind
452 mvisibility
= public_visibility
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.")
464 else if mclass
.arity
!= arity
then
465 error
(nclassdef
, "Redef error: Formal parameter arity missmatch; got {arity}, expected {mclass.arity}.")
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}")
472 nclassdef
.mclass
= mclass
475 # Visit the AST and create the MClassDef objects
476 private fun build_a_mclassdef
(nmodule
: AModule, nclassdef
: AClassdef)
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)
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")
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
502 var bound
= resolve_mtype
(nclassdef
, nfdt
)
503 if bound
== null then return # Forward error
504 if bound
.need_anchor
then
506 error
(nfd
, "Error: Formal parameter type `{names[i]}' bounded with a formal parameter type")
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
)
515 bounds
.add
(mclass
.mclassdefs
.first
.bound_mtype
.as(MGenericType).arguments
[i
])
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
525 if mclassdef
.is_intro
then
526 self.toolcontext
.info
("{mclassdef} introduces new {mclass.kind} {mclass.full_name}", 3)
528 self.toolcontext
.info
("{mclassdef} refine {mclass.kind} {mclass.full_name}", 3)
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)
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)
540 var specobject
= true
541 var supertypes
= new Array[MClassType]
542 if nclassdef
isa AStdClassdef then
543 for nsc
in nclassdef
.n_superclasses
do
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")
553 #print "new super : {mclass} < {mtype}"
556 if specobject
and mclass
.name
!= "Object" and objectclass
!= null and mclassdef
.is_intro
then
557 supertypes
.add objectclass
.mclass_type
560 mclassdef
.set_supertypes
(supertypes
)
561 if not supertypes
.is_empty
then self.toolcontext
.info
("{mclassdef} new super-types: {supertypes.join(", ")}", 3)
564 # Check the validity of the specialization heirarchy
565 # FIXME Stub implementation
566 private fun check_supertypes
(nmodule
: AModule, nclassdef
: AClassdef)
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)
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)
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
])
586 for nclassdef
in nmodule
.n_classdefs
do
587 self.build_a_mclass
(nmodule
, nclassdef
)
590 # Create all classdefs
591 for nclassdef
in nmodule
.n_classdefs
do
592 self.build_a_mclassdef
(nmodule
, nclassdef
)
595 # Create inheritance on all classdefs
596 for nclassdef
in nmodule
.n_classdefs
do
597 self.build_a_mclassdef_inheritance
(nmodule
, nclassdef
)
600 # TODO: Check that the super-class is not intrusive
602 # TODO: Check that the super-class is not already known (by transitivity)
604 for nclassdef
in nmodule
.n_classdefs
do
605 self.build_properties
(nclassdef
)
608 nmodule
.build_classes_is_done
= true
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]
621 # Build the properties of `nclassdef'.
622 # REQUIRE: all superclasses are built.
623 private fun build_properties
(nclassdef
: AClassdef)
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
])
633 for npropdef
in nclassdef
.n_propdefs
do
634 npropdef
.build_property
(self, nclassdef
)
636 for npropdef
in nclassdef
.n_propdefs
do
637 npropdef
.build_signature
(self, nclassdef
)
639 for npropdef
in nclassdef
.n_propdefs
do
640 npropdef
.check_signature
(self, nclassdef
)
642 process_default_constructors
(nclassdef
)
643 nclassdef
.build_properties_is_done
= true
646 # Introduce or inherit default constructor
647 # This is the last part of `build_properties'.
648 private fun process_default_constructors
(nclassdef
: AClassdef)
650 var mclassdef
= nclassdef
.mclassdef
.as(not null)
652 # Are we a refinement
653 if not mclassdef
.is_intro
then return
655 # Is the class forbid constructors?
656 if not mclassdef
.mclass
.kind
.need_init
then return
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
664 if not nclassdef
isa AStdClassdef then return
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
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
)
679 var inhc2
= c
.inherit_init_from
680 if inhc2
== null then inhc2
= c
681 if inhc2
== inhc
then continue
683 self.error
(nclassdef
, "Cannot provide a defaut constructor: conflict for {inhc} and {c}")
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
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}")
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 mparameters
= new Array[MParameter]
704 var msignature
= new MSignature(mparameters
, null)
705 mpropdef
.msignature
= msignature
707 nclassdef
.mfree_init
= mpropdef
708 self.toolcontext
.info
("{mclassdef} gets a free empty constructor {mpropdef}{msignature}", 3)
712 # Collect undefined attributes
713 var mparameters
= new Array[MParameter]
714 for npropdef
in nclassdef
.n_propdefs
do
715 if npropdef
isa AAttrPropdef and npropdef
.n_expr
== null then
716 var paramname
= npropdef
.mpropdef
.mproperty
.name
.substring_from
(1)
717 var ret_type
= npropdef
.mpropdef
.static_mtype
718 if ret_type
== null then return
719 var mparameter
= new MParameter(paramname
, ret_type
, false)
720 mparameters
.add
(mparameter
)
724 var mprop
= new MMethod(mclassdef
, "init", mclassdef
.mclass
.visibility
)
725 var mpropdef
= new MMethodDef(mclassdef
, mprop
, nclassdef
.location
)
726 var msignature
= new MSignature(mparameters
, null)
727 mpropdef
.msignature
= msignature
729 nclassdef
.mfree_init
= mpropdef
730 self.toolcontext
.info
("{mclassdef} gets a free constructor for attributes {mpropdef}{msignature}", 3)
733 # Return the static type associated to the node `ntype'.
734 # `classdef' is the context where the call is made (used to understand formal types)
735 # The mmodule used as context is `nclassdef.mmodule'
736 # In case of problem, an error is displayed on `ntype' and null is returned.
737 # FIXME: the name "resolve_mtype" is awful
738 fun resolve_mtype
(nclassdef
: AClassdef, ntype
: AType): nullable MType
740 var name
= ntype
.n_id
.text
741 var mclassdef
= nclassdef
.mclassdef
742 var mmodule
= nclassdef
.parent
.as(AModule).mmodule
.as(not null)
746 if mclassdef
!= null then
747 var prop
= try_get_mproperty_by_name
(ntype
, mclassdef
, name
).as(nullable MVirtualTypeProp)
749 if not ntype
.n_types
.is_empty
then
750 error
(ntype
, "Type error: formal type {name} cannot have formal parameters.")
752 res
= prop
.mvirtualtype
753 if ntype
.n_kwnullable
!= null then res
= res
.as_nullable
758 # Check parameter type
759 if mclassdef
!= null and mclassdef
.parameter_names
.has
(name
) then
760 if not ntype
.n_types
.is_empty
then
761 error
(ntype
, "Type error: formal type {name} cannot have formal parameters.")
763 for i
in [0..mclassdef
.parameter_names
.length
[ do
764 if mclassdef
.parameter_names
[i
] == name
then
765 res
= mclassdef
.mclass
.mclass_type
.as(MGenericType).arguments
[i
]
766 if ntype
.n_kwnullable
!= null then res
= res
.as_nullable
774 var mclass
= try_get_mclass_by_name
(ntype
, mmodule
, name
)
775 if mclass
!= null then
776 var arity
= ntype
.n_types
.length
777 if arity
!= mclass
.arity
then
779 error
(ntype
, "Type error: '{name}' is a generic class.")
780 else if mclass
.arity
== 0 then
781 error
(ntype
, "Type error: '{name}' is not a generic class.")
783 error
(ntype
, "Type error: '{name}' has {mclass.arity} parameters ({arity} are provided).")
788 res
= mclass
.mclass_type
789 if ntype
.n_kwnullable
!= null then res
= res
.as_nullable
792 var mtypes
= new Array[MType]
793 for nt
in ntype
.n_types
do
794 var mt
= resolve_mtype
(nclassdef
, nt
)
795 if mt
== null then return null # Forward error
798 res
= mclass
.get_mtype
(mtypes
)
799 if ntype
.n_kwnullable
!= null then res
= res
.as_nullable
804 # If everything fail, then give up :(
805 error
(ntype
, "Type error: class {name} not found in module {mmodule}.")
809 # Helper function to display an error on a node.
810 # Alias for `self.toolcontext.error(n.hot_location, text)'
811 fun error
(n
: ANode, text
: String)
813 self.toolcontext
.error
(n
.hot_location
, text
)
816 # Helper function to display a warning on a node.
817 # Alias for: `self.toolcontext.warning(n.hot_location, text)'
818 fun warning
(n
: ANode, text
: String)
820 self.toolcontext
.warning
(n
.hot_location
, text
)
825 # The associated MModule once build by a `ModelBuilder'
826 var mmodule
: nullable MModule
827 # Flag that indicate if the importation is already completed
828 var is_importation_done
: Bool = false
829 # Flag that indicate if the class and prop building is already completed
830 var build_classes_is_done
: Bool = false
834 # The class whose self inherit all the constructors.
835 # 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
836 var inherit_init_from
: nullable MClass = null
839 redef class AClassdef
840 # The associated MClass once build by a `ModelBuilder'
841 var mclass
: nullable MClass
842 # The associated MClassDef once build by a `ModelBuilder'
843 var mclassdef
: nullable MClassDef
844 var build_properties_is_done
: Bool = false
845 # The list of super-constructor to call at the start of the free constructor
846 # 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
847 var super_inits
: nullable Collection[MMethod] = null
849 # The free init (implicitely constructed by the class if required)
850 var mfree_init
: nullable MMethodDef = null
853 redef class AClasskind
854 # The class kind associated with the AST node class
855 private fun mkind
: MClassKind is abstract
857 redef class AConcreteClasskind
858 redef fun mkind
do return concrete_kind
860 redef class AAbstractClasskind
861 redef fun mkind
do return abstract_kind
863 redef class AInterfaceClasskind
864 redef fun mkind
do return interface_kind
866 redef class AEnumClasskind
867 redef fun mkind
do return enum_kind
869 redef class AExternClasskind
870 redef fun mkind
do return extern_kind
873 redef class AVisibility
874 # The visibility level associated with the AST node class
875 private fun mvisibility
: MVisibility is abstract
877 redef class AIntrudeVisibility
878 redef fun mvisibility
do return intrude_visibility
880 redef class APublicVisibility
881 redef fun mvisibility
do return public_visibility
883 redef class AProtectedVisibility
884 redef fun mvisibility
do return protected_visibility
886 redef class APrivateVisibility
887 redef fun mvisibility
do return private_visibility
894 # Join the text of all tokens
895 # Used to get the 'real name' of method definitions.
896 fun collect_text
: String
898 var v
= new TextCollectorVisitor
905 private class TextCollectorVisitor
907 var text
: String = ""
910 if n
isa Token then text
+= n
.text
916 private fun build_property
(modelbuilder
: ModelBuilder, nclassdef
: AClassdef)
919 private fun build_signature
(modelbuilder
: ModelBuilder, nclassdef
: AClassdef)
922 private fun check_signature
(modelbuilder
: ModelBuilder, nclassdef
: AClassdef)
925 private fun new_property_visibility
(modelbuilder
: ModelBuilder, nclassdef
: AClassdef, nvisibility
: nullable AVisibility): MVisibility
927 var mvisibility
= public_visibility
928 if nvisibility
!= null then mvisibility
= nvisibility
.mvisibility
929 if nclassdef
.mclassdef
.mclass
.visibility
== private_visibility
then
930 if mvisibility
== protected_visibility
then
931 assert nvisibility
!= null
932 modelbuilder
.error
(nvisibility
, "Error: The only legal visibility for properties in a private class is private.")
933 else if mvisibility
== private_visibility
then
934 assert nvisibility
!= null
936 # modelbuilder.warning(nvisibility, "Warning: private is unrequired since the only legal visibility for properties in a private class is private.")
938 mvisibility
= private_visibility
943 private fun check_redef_property_visibility
(modelbuilder
: ModelBuilder, nclassdef
: AClassdef, nvisibility
: nullable AVisibility, mprop
: MProperty)
945 if nvisibility
== null then return
946 var mvisibility
= nvisibility
.mvisibility
947 if mvisibility
!= mprop
.visibility
and mvisibility
!= public_visibility
then
948 modelbuilder
.error
(nvisibility
, "Error: redefinition changed the visibility from a {mprop.visibility} to a {mvisibility}")
952 private fun check_redef_keyword
(modelbuilder
: ModelBuilder, nclassdef
: AClassdef, kwredef
: nullable Token, need_redef
: Bool, mprop
: MProperty)
954 if kwredef
== null then
956 modelbuilder
.error
(self, "Redef error: {nclassdef.mclassdef.mclass}::{mprop.name} is an inherited property. To redefine it, add the redef keyword.")
959 if not need_redef
then
960 modelbuilder
.error
(self, "Error: No property {nclassdef.mclassdef.mclass}::{mprop.name} is inherited. Remove the redef keyword to define a new property.")
966 redef class ASignature
967 # Is the model builder has correctly visited the signature
968 var is_visited
= false
969 # Names of parameters from the AST
970 # REQUIRE: is_visited
971 var param_names
= new Array[String]
972 # Types of parameters from the AST
973 # REQUIRE: is_visited
974 var param_types
= new Array[MType]
975 # Rank of the vararg (of -1 if none)
976 # REQUIRE: is_visited
977 var vararg_rank
: Int = -1
979 var ret_type
: nullable MType = null
981 # Visit and fill information about a signature
982 private fun visit_signature
(modelbuilder
: ModelBuilder, nclassdef
: AClassdef): Bool
984 var param_names
= self.param_names
985 var param_types
= self.param_types
986 for np
in self.n_params
do
987 param_names
.add
(np
.n_id
.text
)
988 var ntype
= np
.n_type
989 if ntype
!= null then
990 var mtype
= modelbuilder
.resolve_mtype
(nclassdef
, ntype
)
991 if mtype
== null then return false # Skip error
992 for i
in [0..param_names
.length-param_types
.length
[ do
993 param_types
.add
(mtype
)
995 if np
.n_dotdotdot
!= null then
996 if self.vararg_rank
!= -1 then
997 modelbuilder
.error
(np
, "Error: {param_names[self.vararg_rank]} is already a vararg")
1000 self.vararg_rank
= param_names
.length
- 1
1005 var ntype
= self.n_type
1006 if ntype
!= null then
1007 self.ret_type
= modelbuilder
.resolve_mtype
(nclassdef
, ntype
)
1008 if self.ret_type
== null then return false # Skip errir
1011 for nclosure
in self.n_closure_decls
do
1012 if not nclosure
.n_signature
.visit_signature
(modelbuilder
, nclassdef
) then return false
1015 self.is_visited
= true
1019 # Build a visited signature
1020 fun build_signature
(modelbuilder
: ModelBuilder, nclassdef
: AClassdef): nullable MSignature
1022 if param_names
.length
!= param_types
.length
then
1023 # Some parameters are typed, other parameters are not typed.
1024 modelbuilder
.warning
(self.n_params
[param_types
.length
], "Error: Untyped parameter `{param_names[param_types.length]}'.")
1028 var mparameters
= new Array[MParameter]
1029 for i
in [0..param_names
.length
[ do
1030 var mparameter
= new MParameter(param_names
[i
], param_types
[i
], i
== vararg_rank
)
1031 mparameters
.add
(mparameter
)
1034 var msignature
= new MSignature(mparameters
, ret_type
)
1039 redef class AMethPropdef
1040 # The associated MMethodDef once build by a `ModelBuilder'
1041 var mpropdef
: nullable MMethodDef
1043 # The associated super init if any
1044 var super_init
: nullable MMethod
1045 redef fun build_property
(modelbuilder
, nclassdef
)
1047 var is_init
= self isa AInitPropdef
1048 var mclassdef
= nclassdef
.mclassdef
.as(not null)
1050 var amethodid
= self.n_methid
1051 var name_node
: ANode
1052 if amethodid
== null then
1053 if self isa AMainMethPropdef then
1056 else if self isa AConcreteInitPropdef then
1058 name_node
= self.n_kwinit
1059 else if self isa AExternInitPropdef then
1061 name_node
= self.n_kwnew
1065 else if amethodid
isa AIdMethid then
1066 name
= amethodid
.n_id
.text
1067 name_node
= amethodid
1069 # operator, bracket or assign
1070 name
= amethodid
.collect_text
1071 name_node
= amethodid
1073 if name
== "-" and self.n_signature
.n_params
.length
== 0 then
1078 var mprop
: nullable MMethod = null
1079 if not is_init
or n_kwredef
!= null then mprop
= modelbuilder
.try_get_mproperty_by_name
(name_node
, mclassdef
, name
).as(nullable MMethod)
1080 if mprop
== null then
1081 var mvisibility
= new_property_visibility
(modelbuilder
, nclassdef
, self.n_visibility
)
1082 mprop
= new MMethod(mclassdef
, name
, mvisibility
)
1083 mprop
.is_init
= is_init
1084 mprop
.is_new
= self isa AExternInitPropdef
1085 self.check_redef_keyword
(modelbuilder
, nclassdef
, n_kwredef
, false, mprop
)
1087 if n_kwredef
== null then
1088 if self isa AMainMethPropdef then
1091 self.check_redef_keyword
(modelbuilder
, nclassdef
, n_kwredef
, true, mprop
)
1094 check_redef_property_visibility
(modelbuilder
, nclassdef
, self.n_visibility
, mprop
)
1097 var mpropdef
= new MMethodDef(mclassdef
, mprop
, self.location
)
1099 self.mpropdef
= mpropdef
1100 modelbuilder
.mpropdef2npropdef
[mpropdef
] = self
1101 if mpropdef
.is_intro
then
1102 modelbuilder
.toolcontext
.info
("{mpropdef} introduces new method {mprop.full_name}", 3)
1104 modelbuilder
.toolcontext
.info
("{mpropdef} redefines method {mprop.full_name}", 3)
1108 redef fun build_signature
(modelbuilder
, nclassdef
)
1110 var mpropdef
= self.mpropdef
1111 if mpropdef
== null then return # Error thus skiped
1112 var mmodule
= mpropdef
.mclassdef
.mmodule
1113 var nsig
= self.n_signature
1115 # Retrieve info from the signature AST
1116 var param_names
= new Array[String] # Names of parameters from the AST
1117 var param_types
= new Array[MType] # Types of parameters from the AST
1118 var vararg_rank
= -1
1119 var ret_type
: nullable MType = null # Return type from the AST
1120 if nsig
!= null then
1121 if not nsig
.visit_signature
(modelbuilder
, nclassdef
) then return
1122 param_names
= nsig
.param_names
1123 param_types
= nsig
.param_types
1124 vararg_rank
= nsig
.vararg_rank
1125 ret_type
= nsig
.ret_type
1128 # Look for some signature to inherit
1129 # FIXME: do not inherit from the intro, but from the most specific
1130 var msignature
: nullable MSignature = null
1131 if not mpropdef
.is_intro
then
1132 msignature
= mpropdef
.mproperty
.intro
.msignature
1133 if msignature
== null then return # Skip error
1134 else if mpropdef
.mproperty
.is_init
then
1135 # FIXME UGLY: inherit signature from a super-constructor
1136 for msupertype
in nclassdef
.mclassdef
.supertypes
do
1137 msupertype
= msupertype
.anchor_to
(mmodule
, nclassdef
.mclassdef
.bound_mtype
)
1138 var candidate
= modelbuilder
.try_get_mproperty_by_name2
(self, mmodule
, msupertype
, mpropdef
.mproperty
.name
)
1139 if candidate
!= null then
1140 if msignature
== null then
1141 msignature
= candidate
.intro
.as(MMethodDef).msignature
1147 # Inherit the signature
1148 if msignature
!= null and param_names
.length
!= param_types
.length
and param_names
.length
== msignature
.arity
and param_types
.length
== 0 then
1149 # Parameters are untyped, thus inherit them
1150 param_types
= new Array[MType]
1151 for mparameter
in msignature
.mparameters
do
1152 param_types
.add
(mparameter
.mtype
)
1154 vararg_rank
= msignature
.vararg_rank
1156 if msignature
!= null and ret_type
== null then
1157 ret_type
= msignature
.return_mtype
1160 if param_names
.length
!= param_types
.length
then
1161 # Some parameters are typed, other parameters are not typed.
1162 modelbuilder
.warning
(nsig
.n_params
[param_types
.length
], "Error: Untyped parameter `{param_names[param_types.length]}'.")
1166 var mparameters
= new Array[MParameter]
1167 for i
in [0..param_names
.length
[ do
1168 var mparameter
= new MParameter(param_names
[i
], param_types
[i
], i
== vararg_rank
)
1169 mparameters
.add
(mparameter
)
1172 msignature
= new MSignature(mparameters
, ret_type
)
1173 mpropdef
.msignature
= msignature
1175 if nsig
!= null then
1176 for nclosure
in nsig
.n_closure_decls
do
1177 var clos_signature
= nclosure
.n_signature
.build_signature
(modelbuilder
, nclassdef
)
1178 if clos_signature
== null then return
1179 var mparameter
= new MParameter(nclosure
.n_id
.text
, clos_signature
, false)
1180 msignature
.mclosures
.add
(mparameter
)
1186 redef fun check_signature
(modelbuilder
, nclassdef
)
1188 var mpropdef
= self.mpropdef
1189 if mpropdef
== null then return # Error thus skiped
1190 var mmodule
= mpropdef
.mclassdef
.mmodule
1191 var nsig
= self.n_signature
1192 var mysignature
= self.mpropdef
.msignature
1193 if mysignature
== null then return # Error thus skiped
1195 # Lookup for signature in the precursor
1196 # FIXME all precursors should be considered
1197 if not mpropdef
.is_intro
then
1198 var msignature
= mpropdef
.mproperty
.intro
.msignature
1199 if msignature
== null then return
1201 if mysignature
.arity
!= msignature
.arity
then
1203 if nsig
!= null then node
= nsig
else node
= self
1204 modelbuilder
.error
(node
, "Redef Error: {mysignature.arity} parameters found, {msignature.arity} expected. Signature is {mpropdef}{msignature}")
1207 var precursor_ret_type
= msignature
.return_mtype
1208 var ret_type
= mysignature
.return_mtype
1209 if ret_type
!= null and precursor_ret_type
== null then
1210 modelbuilder
.error
(nsig
.n_type
.as(not null), "Redef Error: {mpropdef.mproperty} is a procedure, not a function.")
1214 if mysignature
.arity
> 0 then
1215 # Check parameters types
1216 for i
in [0..mysignature
.arity
[ do
1217 var myt
= mysignature
.mparameters
[i
].mtype
1218 var prt
= msignature
.mparameters
[i
].mtype
1219 if not myt
.is_subtype
(mmodule
, nclassdef
.mclassdef
.bound_mtype
, prt
) and
1220 not prt
.is_subtype
(mmodule
, nclassdef
.mclassdef
.bound_mtype
, myt
) then
1221 modelbuilder
.error
(nsig
.n_params
[i
], "Redef Error: Wrong type for parameter `{mysignature.mparameters[i].name}'. found {myt}, expected {prt}.")
1225 if precursor_ret_type
!= null then
1226 if ret_type
== null then
1227 # Inherit the return type
1228 ret_type
= precursor_ret_type
1229 else if not ret_type
.is_subtype
(mmodule
, nclassdef
.mclassdef
.bound_mtype
, precursor_ret_type
) then
1230 modelbuilder
.error
(nsig
.n_type
.as(not null), "Redef Error: Wrong return type. found {ret_type}, expected {precursor_ret_type}.")
1237 redef class AAttrPropdef
1238 # The associated MAttributeDef once build by a `ModelBuilder'
1239 var mpropdef
: nullable MAttributeDef
1240 # The associated getter (read accessor) if any
1241 var mreadpropdef
: nullable MMethodDef
1242 # The associated setter (write accessor) if any
1243 var mwritepropdef
: nullable MMethodDef
1244 redef fun build_property
(modelbuilder
, nclassdef
)
1246 var mclassdef
= nclassdef
.mclassdef
.as(not null)
1247 var mclass
= mclassdef
.mclass
1250 if self.n_id
!= null then
1251 name
= self.n_id
.text
1253 name
= self.n_id2
.text
1256 if mclass
.kind
== interface_kind
or mclassdef
.mclass
.kind
== enum_kind
then
1257 modelbuilder
.error
(self, "Error: Attempt to define attribute {name} in the interface {mclass}.")
1258 else if mclass
.kind
== enum_kind
then
1259 modelbuilder
.error
(self, "Error: Attempt to define attribute {name} in the enum class {mclass}.")
1264 # Old attribute style
1265 var mprop
= modelbuilder
.try_get_mproperty_by_name
(nid
, mclassdef
, name
)
1266 if mprop
== null then
1267 var mvisibility
= new_property_visibility
(modelbuilder
, nclassdef
, self.n_visibility
)
1268 mprop
= new MAttribute(mclassdef
, name
, mvisibility
)
1269 self.check_redef_keyword
(modelbuilder
, nclassdef
, self.n_kwredef
, false, mprop
)
1271 assert mprop
isa MAttribute
1272 check_redef_property_visibility
(modelbuilder
, nclassdef
, self.n_visibility
, mprop
)
1273 self.check_redef_keyword
(modelbuilder
, nclassdef
, self.n_kwredef
, true, mprop
)
1275 var mpropdef
= new MAttributeDef(mclassdef
, mprop
, self.location
)
1276 self.mpropdef
= mpropdef
1277 modelbuilder
.mpropdef2npropdef
[mpropdef
] = self
1279 var nreadable
= self.n_readable
1280 if nreadable
!= null then
1281 var readname
= name
.substring_from
(1)
1282 var mreadprop
= modelbuilder
.try_get_mproperty_by_name
(nid
, mclassdef
, readname
).as(nullable MMethod)
1283 if mreadprop
== null then
1284 var mvisibility
= new_property_visibility
(modelbuilder
, nclassdef
, nreadable
.n_visibility
)
1285 mreadprop
= new MMethod(mclassdef
, readname
, mvisibility
)
1286 self.check_redef_keyword
(modelbuilder
, nclassdef
, nreadable
.n_kwredef
, false, mreadprop
)
1288 self.check_redef_keyword
(modelbuilder
, nclassdef
, nreadable
.n_kwredef
, true, mreadprop
)
1289 check_redef_property_visibility
(modelbuilder
, nclassdef
, nreadable
.n_visibility
, mreadprop
)
1291 var mreadpropdef
= new MMethodDef(mclassdef
, mreadprop
, self.location
)
1292 self.mreadpropdef
= mreadpropdef
1293 modelbuilder
.mpropdef2npropdef
[mreadpropdef
] = self
1296 var nwritable
= self.n_writable
1297 if nwritable
!= null then
1298 var writename
= name
.substring_from
(1) + "="
1299 var mwriteprop
= modelbuilder
.try_get_mproperty_by_name
(nid
, mclassdef
, writename
).as(nullable MMethod)
1300 if mwriteprop
== null then
1301 var mvisibility
= new_property_visibility
(modelbuilder
, nclassdef
, nwritable
.n_visibility
)
1302 mwriteprop
= new MMethod(mclassdef
, writename
, mvisibility
)
1303 self.check_redef_keyword
(modelbuilder
, nclassdef
, nwritable
.n_kwredef
, false, mwriteprop
)
1305 self.check_redef_keyword
(modelbuilder
, nclassdef
, nwritable
.n_kwredef
, true, mwriteprop
)
1306 check_redef_property_visibility
(modelbuilder
, nclassdef
, nwritable
.n_visibility
, mwriteprop
)
1308 var mwritepropdef
= new MMethodDef(mclassdef
, mwriteprop
, self.location
)
1309 self.mwritepropdef
= mwritepropdef
1310 modelbuilder
.mpropdef2npropdef
[mwritepropdef
] = self
1313 # New attribute style
1314 var nid2
= self.n_id2
.as(not null)
1315 var mprop
= new MAttribute(mclassdef
, "@" + name
, none_visibility
)
1316 var mpropdef
= new MAttributeDef(mclassdef
, mprop
, self.location
)
1317 self.mpropdef
= mpropdef
1318 modelbuilder
.mpropdef2npropdef
[mpropdef
] = self
1321 var mreadprop
= modelbuilder
.try_get_mproperty_by_name
(nid2
, mclassdef
, readname
).as(nullable MMethod)
1322 if mreadprop
== null then
1323 var mvisibility
= new_property_visibility
(modelbuilder
, nclassdef
, self.n_visibility
)
1324 mreadprop
= new MMethod(mclassdef
, readname
, mvisibility
)
1325 self.check_redef_keyword
(modelbuilder
, nclassdef
, n_kwredef
, false, mreadprop
)
1327 self.check_redef_keyword
(modelbuilder
, nclassdef
, n_kwredef
, true, mreadprop
)
1328 check_redef_property_visibility
(modelbuilder
, nclassdef
, self.n_visibility
, mreadprop
)
1330 var mreadpropdef
= new MMethodDef(mclassdef
, mreadprop
, self.location
)
1331 self.mreadpropdef
= mreadpropdef
1332 modelbuilder
.mpropdef2npropdef
[mreadpropdef
] = self
1334 var writename
= name
+ "="
1335 var nwritable
= self.n_writable
1336 var mwriteprop
= modelbuilder
.try_get_mproperty_by_name
(nid2
, mclassdef
, writename
).as(nullable MMethod)
1337 var nwkwredef
: nullable Token = null
1338 if nwritable
!= null then nwkwredef
= nwritable
.n_kwredef
1339 if mwriteprop
== null then
1341 if nwritable
!= null then
1342 mvisibility
= new_property_visibility
(modelbuilder
, nclassdef
, nwritable
.n_visibility
)
1344 mvisibility
= private_visibility
1346 mwriteprop
= new MMethod(mclassdef
, writename
, mvisibility
)
1347 self.check_redef_keyword
(modelbuilder
, nclassdef
, nwkwredef
, false, mwriteprop
)
1349 self.check_redef_keyword
(modelbuilder
, nclassdef
, nwkwredef
, true, mwriteprop
)
1350 if nwritable
!= null then
1351 check_redef_property_visibility
(modelbuilder
, nclassdef
, nwritable
.n_visibility
, mwriteprop
)
1354 var mwritepropdef
= new MMethodDef(mclassdef
, mwriteprop
, self.location
)
1355 self.mwritepropdef
= mwritepropdef
1356 modelbuilder
.mpropdef2npropdef
[mwritepropdef
] = self
1360 redef fun build_signature
(modelbuilder
, nclassdef
)
1362 var mpropdef
= self.mpropdef
1363 if mpropdef
== null then return # Error thus skiped
1364 var mmodule
= mpropdef
.mclassdef
.mmodule
1365 var mtype
: nullable MType = null
1367 var ntype
= self.n_type
1368 if ntype
!= null then
1369 mtype
= modelbuilder
.resolve_mtype
(nclassdef
, ntype
)
1370 if mtype
== null then return
1373 if mtype
== null then
1374 var nexpr
= self.n_expr
1375 if nexpr
!= null then
1376 if nexpr
isa ANewExpr then
1377 mtype
= modelbuilder
.resolve_mtype
(nclassdef
, nexpr
.n_type
)
1378 else if nexpr
isa AIntExpr then
1379 var cla
= modelbuilder
.try_get_mclass_by_name
(nexpr
, mmodule
, "Int")
1380 if cla
!= null then mtype
= cla
.mclass_type
1381 else if nexpr
isa AFloatExpr then
1382 var cla
= modelbuilder
.try_get_mclass_by_name
(nexpr
, mmodule
, "Float")
1383 if cla
!= null then mtype
= cla
.mclass_type
1384 else if nexpr
isa ACharExpr then
1385 var cla
= modelbuilder
.try_get_mclass_by_name
(nexpr
, mmodule
, "Char")
1386 if cla
!= null then mtype
= cla
.mclass_type
1387 else if nexpr
isa ABoolExpr then
1388 var cla
= modelbuilder
.try_get_mclass_by_name
(nexpr
, mmodule
, "Bool")
1389 if cla
!= null then mtype
= cla
.mclass_type
1390 else if nexpr
isa ASuperstringExpr then
1391 var cla
= modelbuilder
.try_get_mclass_by_name
(nexpr
, mmodule
, "String")
1392 if cla
!= null then mtype
= cla
.mclass_type
1393 else if nexpr
isa AStringFormExpr then
1394 var cla
= modelbuilder
.try_get_mclass_by_name
(nexpr
, mmodule
, "String")
1395 if cla
!= null then mtype
= cla
.mclass_type
1397 modelbuilder
.error
(self, "Error: Untyped attribute {mpropdef}. Implicit typing allowed only for literals and new.")
1401 modelbuilder
.error
(self, "Error: Untyped attribute {mpropdef}")
1405 if mtype
== null then return
1407 mpropdef
.static_mtype
= mtype
1409 var mreadpropdef
= self.mreadpropdef
1410 if mreadpropdef
!= null then
1411 var msignature
= new MSignature(new Array[MParameter], mtype
)
1412 mreadpropdef
.msignature
= msignature
1415 var msritepropdef
= self.mwritepropdef
1416 if mwritepropdef
!= null then
1418 if n_id
!= null then
1419 name
= n_id
.text
.substring_from
(1)
1423 var mparameter
= new MParameter(name
, mtype
, false)
1424 var msignature
= new MSignature([mparameter
], null)
1425 mwritepropdef
.msignature
= msignature
1429 redef fun check_signature
(modelbuilder
, nclassdef
)
1431 var mpropdef
= self.mpropdef
1432 if mpropdef
== null then return # Error thus skiped
1433 var mmodule
= mpropdef
.mclassdef
.mmodule
1434 var ntype
= self.n_type
1435 var mtype
= self.mpropdef
.static_mtype
1436 if mtype
== null then return # Error thus skiped
1438 # Lookup for signature in the precursor
1439 # FIXME all precursors should be considered
1440 if not mpropdef
.is_intro
then
1441 var precursor_type
= mpropdef
.mproperty
.intro
.static_mtype
1442 if precursor_type
== null then return
1444 if mtype
!= precursor_type
then
1445 modelbuilder
.error
(ntype
.as(not null), "Redef Error: Wrong static type. found {mtype}, expected {precursor_type}.")
1450 # FIXME: Check getter ans setter
1454 redef class ATypePropdef
1455 # The associated MVirtualTypeDef once build by a `ModelBuilder'
1456 var mpropdef
: nullable MVirtualTypeDef
1457 redef fun build_property
(modelbuilder
, nclassdef
)
1459 var mclassdef
= nclassdef
.mclassdef
.as(not null)
1460 var name
= self.n_id
.text
1461 var mprop
= modelbuilder
.try_get_mproperty_by_name
(self.n_id
, mclassdef
, name
)
1462 if mprop
== null then
1463 var mvisibility
= new_property_visibility
(modelbuilder
, nclassdef
, self.n_visibility
)
1464 mprop
= new MVirtualTypeProp(mclassdef
, name
, mvisibility
)
1465 self.check_redef_keyword
(modelbuilder
, nclassdef
, self.n_kwredef
, false, mprop
)
1467 self.check_redef_keyword
(modelbuilder
, nclassdef
, self.n_kwredef
, true, mprop
)
1468 assert mprop
isa MVirtualTypeProp
1469 check_redef_property_visibility
(modelbuilder
, nclassdef
, self.n_visibility
, mprop
)
1471 var mpropdef
= new MVirtualTypeDef(mclassdef
, mprop
, self.location
)
1472 self.mpropdef
= mpropdef
1475 redef fun build_signature
(modelbuilder
, nclassdef
)
1477 var mpropdef
= self.mpropdef
1478 if mpropdef
== null then return # Error thus skiped
1479 var mmodule
= mpropdef
.mclassdef
.mmodule
1480 var mtype
: nullable MType = null
1482 var ntype
= self.n_type
1483 mtype
= modelbuilder
.resolve_mtype
(nclassdef
, ntype
)
1484 if mtype
== null then return
1486 mpropdef
.bound
= mtype
1487 # print "{mpropdef}: {mtype}"
1490 redef fun check_signature
(modelbuilder
, nclassdef
)
1492 var bound
= self.mpropdef
.bound
1494 # Fast case: the bound is not a formal type
1495 if not bound
isa MVirtualType then return
1497 var mmodule
= nclassdef
.mclassdef
.mmodule
1498 var anchor
= nclassdef
.mclassdef
.bound_mtype
1500 # Slow case: progress on each resolution until: (i) we loop, or (ii) we found a non formal type
1501 var seen
= [self.mpropdef
.mproperty
.mvirtualtype
]
1503 if seen
.has
(bound
) then
1505 modelbuilder
.error
(self, "Error: circularity of virtual type definition: {seen.join(" -> ")}")
1509 var next
= bound
.lookup_bound
(mmodule
, anchor
)
1510 if not next
isa MVirtualType then return