modelbuilder: force `standard` to be imported
[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 model
26 import phase
27
28 private import more_collections
29
30 ###
31
32 redef class ToolContext
33 # Option --path
34 var opt_path = new OptionArray("Set include path for loaders (may be used more than once)", "-I", "--path")
35
36 # Option --only-metamodel
37 var opt_only_metamodel = new OptionBool("Stop after meta-model processing", "--only-metamodel")
38
39 # Option --only-parse
40 var opt_only_parse = new OptionBool("Only proceed to parse step of loaders", "--only-parse")
41
42 # Option --ignore-visibility
43 var opt_ignore_visibility = new OptionBool("Do not check, and produce errors, on visibility issues.", "--ignore-visibility")
44
45 redef init
46 do
47 super
48 option_context.add_option(opt_path, opt_only_parse, opt_only_metamodel, opt_ignore_visibility)
49 end
50
51 # The modelbuilder 1-to-1 associated with the toolcontext
52 fun modelbuilder: ModelBuilder do return modelbuilder_real.as(not null)
53
54 private var modelbuilder_real: nullable ModelBuilder = null
55
56 # Combine module to make a single one if required.
57 fun make_main_module(mmodules: Array[MModule]): MModule
58 do
59 assert not mmodules.is_empty
60 var mainmodule
61 if mmodules.length == 1 then
62 mainmodule = mmodules.first
63 else
64 # We need a main module, so we build it by importing all modules
65 mainmodule = new MModule(modelbuilder.model, null, mmodules.first.name + "-m", new Location(mmodules.first.location.file, 0, 0, 0, 0))
66 mainmodule.is_fictive = true
67 mainmodule.set_imported_mmodules(mmodules)
68 end
69 return mainmodule
70 end
71
72 # Run `process_mainmodule` on all phases
73 fun run_global_phases(mmodules: Array[MModule])
74 do
75 var mainmodule = make_main_module(mmodules)
76 for phase in phases_list do
77 if phase.disabled then continue
78 phase.process_mainmodule(mainmodule, mmodules)
79 end
80 end
81 end
82
83 redef class Phase
84 # Specific action to execute on the whole program.
85 # Called by the `ToolContext::run_global_phases`.
86 #
87 # `mainmodule` is the main module of the program.
88 # It could be an implicit module (called like the first given_mmodules).
89 #
90 # `given_modules` is the list of explicitely requested modules.
91 # from the command-line for instance.
92 #
93 # REQUIRE: `not given_modules.is_empty`
94 # REQUIRE: `(given_modules.length == 1) == (mainmodule == given_modules.first)`
95 #
96 # @toimplement
97 fun process_mainmodule(mainmodule: MModule, given_mmodules: SequenceRead[MModule]) do end
98 end
99
100
101 # A model builder knows how to load nit source files and build the associated model
102 class ModelBuilder
103 # The model where new modules, classes and properties are added
104 var model: Model
105
106 # The toolcontext used to control the interaction with the user (getting options and displaying messages)
107 var toolcontext: ToolContext
108
109 # Run phases on all loaded modules
110 fun run_phases
111 do
112 var mmodules = model.mmodules.to_a
113 model.mmodule_importation_hierarchy.sort(mmodules)
114 var nmodules = new Array[AModule]
115 for mm in mmodules do
116 nmodules.add(mmodule2nmodule[mm])
117 end
118 toolcontext.run_phases(nmodules)
119
120 if toolcontext.opt_only_metamodel.value then
121 self.toolcontext.info("*** ONLY METAMODEL", 1)
122 exit(0)
123 end
124 end
125
126 # Instantiate a modelbuilder for a model and a toolcontext
127 # Important, the options of the toolcontext must be correctly set (parse_option already called)
128 init
129 do
130 assert toolcontext.modelbuilder_real == null
131 toolcontext.modelbuilder_real = self
132
133 # Setup the paths value
134 paths.append(toolcontext.opt_path.value)
135
136 var path_env = "NIT_PATH".environ
137 if not path_env.is_empty then
138 paths.append(path_env.split_with(':'))
139 end
140
141 var nit_dir = toolcontext.nit_dir
142 var libname = "{nit_dir}/lib"
143 if libname.file_exists then paths.add(libname)
144 end
145
146 # Load a bunch of modules.
147 # `modules` can contains filenames or module names.
148 # Imported modules are automatically loaded and modelized.
149 # The result is the corresponding model elements.
150 # Errors and warnings are printed with the toolcontext.
151 #
152 # Note: class and property model element are not analysed.
153 fun parse(modules: Sequence[String]): Array[MModule]
154 do
155 var time0 = get_time
156 # Parse and recursively load
157 self.toolcontext.info("*** PARSE ***", 1)
158 var mmodules = new ArraySet[MModule]
159 for a in modules do
160 var nmodule = self.load_module(a)
161 if nmodule == null then continue # Skip error
162 mmodules.add(nmodule.mmodule.as(not null))
163 end
164 var time1 = get_time
165 self.toolcontext.info("*** END PARSE: {time1-time0} ***", 2)
166
167 self.toolcontext.check_errors
168
169 if toolcontext.opt_only_parse.value then
170 self.toolcontext.info("*** ONLY PARSE...", 1)
171 exit(0)
172 end
173
174 return mmodules.to_a
175 end
176
177 # Return a class named `name` visible by the module `mmodule`.
178 # Visibility in modules is correctly handled.
179 # If no such a class exists, then null is returned.
180 # If more than one class exists, then an error on `anode` is displayed and null is returned.
181 # FIXME: add a way to handle class name conflict
182 fun try_get_mclass_by_name(anode: ANode, mmodule: MModule, name: String): nullable MClass
183 do
184 var classes = model.get_mclasses_by_name(name)
185 if classes == null then
186 return null
187 end
188
189 var res: nullable MClass = null
190 for mclass in classes do
191 if not mmodule.in_importation <= mclass.intro_mmodule then continue
192 if not mmodule.is_visible(mclass.intro_mmodule, mclass.visibility) then continue
193 if res == null then
194 res = mclass
195 else
196 error(anode, "Ambigous class name '{name}'; conflict between {mclass.full_name} and {res.full_name}")
197 return null
198 end
199 end
200 return res
201 end
202
203 # Return a property named `name` on the type `mtype` visible in the module `mmodule`.
204 # Visibility in modules is correctly handled.
205 # Protected properties are returned (it is up to the caller to check and reject protected properties).
206 # If no such a property exists, then null is returned.
207 # If more than one property exists, then an error on `anode` is displayed and null is returned.
208 # FIXME: add a way to handle property name conflict
209 fun try_get_mproperty_by_name2(anode: ANode, mmodule: MModule, mtype: MType, name: String): nullable MProperty
210 do
211 var props = self.model.get_mproperties_by_name(name)
212 if props == null then
213 return null
214 end
215
216 var cache = self.try_get_mproperty_by_name2_cache[mmodule, mtype, name]
217 if cache != null then return cache
218
219 var res: nullable MProperty = null
220 var ress: nullable Array[MProperty] = null
221 for mprop in props do
222 if not mtype.has_mproperty(mmodule, mprop) then continue
223 if not mmodule.is_visible(mprop.intro_mclassdef.mmodule, mprop.visibility) then continue
224
225 # new-factories are invisible outside of the class
226 if mprop isa MMethod and mprop.is_new and (not mtype isa MClassType or mprop.intro_mclassdef.mclass != mtype.mclass) then
227 continue
228 end
229
230 if res == null then
231 res = mprop
232 continue
233 end
234
235 # Two global properties?
236 # First, special case for init, keep the most specific ones
237 if res isa MMethod and mprop isa MMethod and res.is_init and mprop.is_init then
238 var restype = res.intro_mclassdef.bound_mtype
239 var mproptype = mprop.intro_mclassdef.bound_mtype
240 if mproptype.is_subtype(mmodule, null, restype) then
241 # found a most specific constructor, so keep it
242 res = mprop
243 continue
244 end
245 end
246
247 # Ok, just keep all prop in the ress table
248 if ress == null then
249 ress = new Array[MProperty]
250 ress.add(res)
251 end
252 ress.add(mprop)
253 end
254
255 # There is conflict?
256 if ress != null and res isa MMethod and res.is_init then
257 # special case forinit again
258 var restype = res.intro_mclassdef.bound_mtype
259 var ress2 = new Array[MProperty]
260 for mprop in ress do
261 var mproptype = mprop.intro_mclassdef.bound_mtype
262 if not restype.is_subtype(mmodule, null, mproptype) then
263 ress2.add(mprop)
264 else if not mprop isa MMethod or not mprop.is_init then
265 ress2.add(mprop)
266 end
267 end
268 if ress2.is_empty then
269 ress = null
270 else
271 ress = ress2
272 ress.add(res)
273 end
274 end
275
276 if ress != null then
277 assert ress.length > 1
278 var s = new Array[String]
279 for mprop in ress do s.add mprop.full_name
280 self.error(anode, "Ambigous property name '{name}' for {mtype}; conflict between {s.join(" and ")}")
281 end
282
283 self.try_get_mproperty_by_name2_cache[mmodule, mtype, name] = res
284 return res
285 end
286
287 private var try_get_mproperty_by_name2_cache = new HashMap3[MModule, MType, String, nullable MProperty]
288
289
290 # Alias for try_get_mproperty_by_name2(anode, mclassdef.mmodule, mclassdef.mtype, name)
291 fun try_get_mproperty_by_name(anode: ANode, mclassdef: MClassDef, name: String): nullable MProperty
292 do
293 return try_get_mproperty_by_name2(anode, mclassdef.mmodule, mclassdef.bound_mtype, name)
294 end
295
296 # The list of directories to search for top level modules
297 # The list is initially set with :
298 # * the toolcontext --path option
299 # * the NIT_PATH environment variable
300 # * `toolcontext.nit_dir`
301 # Path can be added (or removed) by the client
302 var paths = new Array[String]
303
304 # Like (an used by) `get_mmodule_by_name` but just return the ModulePath
305 private fun search_mmodule_by_name(anode: ANode, mgroup: nullable MGroup, name: String): nullable ModulePath
306 do
307 # First, look in groups
308 var c = mgroup
309 while c != null do
310 var dirname = c.filepath
311 if dirname == null then break # virtual group
312 if dirname.has_suffix(".nit") then break # singleton project
313
314 # Second, try the directory to find a file
315 var try_file = dirname + "/" + name + ".nit"
316 if try_file.file_exists then
317 var res = self.identify_file(try_file.simplify_path)
318 assert res != null
319 return res
320 end
321
322 # Third, try if the requested module is itself a group
323 try_file = dirname + "/" + name + "/" + name + ".nit"
324 if try_file.file_exists then
325 var res = self.identify_file(try_file.simplify_path)
326 assert res != null
327 return res
328 end
329
330 c = c.parent
331 end
332
333 # Look at some known directories
334 var lookpaths = self.paths
335
336 # Look in the directory of the group project also (even if not explicitely in the path)
337 if mgroup != null then
338 # path of the root group
339 var dirname = mgroup.mproject.root.filepath
340 if dirname != null then
341 dirname = dirname.join_path("..").simplify_path
342 if not lookpaths.has(dirname) and dirname.file_exists then
343 lookpaths = lookpaths.to_a
344 lookpaths.add(dirname)
345 end
346 end
347 end
348
349 var candidate = search_module_in_paths(anode.hot_location, name, lookpaths)
350
351 if candidate == null then
352 if mgroup != null then
353 error(anode, "Error: cannot find module {name} from {mgroup.name}. tried {lookpaths.join(", ")}")
354 else
355 error(anode, "Error: cannot find module {name}. tried {lookpaths.join(", ")}")
356 end
357 return null
358 end
359 return candidate
360 end
361
362 # Get a module by its short name; if required, the module is loaded, parsed and its hierarchies computed.
363 # If `mgroup` is set, then the module search starts from it up to the top level (see `paths`);
364 # if `mgroup` is null then the module is searched in the top level only.
365 # If no module exists or there is a name conflict, then an error on `anode` is displayed and null is returned.
366 fun get_mmodule_by_name(anode: ANode, mgroup: nullable MGroup, name: String): nullable MModule
367 do
368 var path = search_mmodule_by_name(anode, mgroup, name)
369 if path == null then return null # Forward error
370 var res = self.load_module(path.filepath)
371 if res == null then return null # Forward error
372 return res.mmodule.as(not null)
373 end
374
375 # Search a module `name` from path `lookpaths`.
376 # If found, the path of the file is returned
377 private fun search_module_in_paths(location: nullable Location, name: String, lookpaths: Collection[String]): nullable ModulePath
378 do
379 var candidate: nullable String = null
380 for dirname in lookpaths do
381 var try_file = (dirname + "/" + name + ".nit").simplify_path
382 if try_file.file_exists then
383 if candidate == null then
384 candidate = try_file
385 else if candidate != try_file then
386 # try to disambiguate conflicting modules
387 var abs_candidate = module_absolute_path(candidate)
388 var abs_try_file = module_absolute_path(try_file)
389 if abs_candidate != abs_try_file then
390 toolcontext.error(location, "Error: conflicting module file for {name}: {candidate} {try_file}")
391 end
392 end
393 end
394 try_file = (dirname + "/" + name + "/" + name + ".nit").simplify_path
395 if try_file.file_exists then
396 if candidate == null then
397 candidate = try_file
398 else if candidate != try_file then
399 # try to disambiguate conflicting modules
400 var abs_candidate = module_absolute_path(candidate)
401 var abs_try_file = module_absolute_path(try_file)
402 if abs_candidate != abs_try_file then
403 toolcontext.error(location, "Error: conflicting module file for {name}: {candidate} {try_file}")
404 end
405 end
406 end
407 end
408 if candidate == null then return null
409 return identify_file(candidate)
410 end
411
412 # cache for `identify_file` by realpath
413 private var identified_files = new HashMap[String, nullable ModulePath]
414
415 # Identify a source file
416 # Load the associated project and groups if required
417 private fun identify_file(path: String): nullable ModulePath
418 do
419 # special case for not a nit file
420 if path.file_extension != "nit" then
421 # search in known -I paths
422 var res = search_module_in_paths(null, path, self.paths)
423 if res != null then return res
424
425 # Found nothins? maybe it is a group...
426 var candidate = null
427 if path.file_exists then
428 var mgroup = get_mgroup(path)
429 if mgroup != null then
430 var owner_path = mgroup.filepath.join_path(mgroup.name + ".nit")
431 if owner_path.file_exists then candidate = owner_path
432 end
433 end
434
435 if candidate == null then
436 toolcontext.error(null, "Error: cannot find module `{path}`.")
437 return null
438 end
439 path = candidate
440 end
441
442 # Fast track, the path is already known
443 var pn = path.basename(".nit")
444 var rp = module_absolute_path(path)
445 if identified_files.has_key(rp) then return identified_files[rp]
446
447 # Search for a group
448 var mgrouppath = path.join_path("..").simplify_path
449 var mgroup = get_mgroup(mgrouppath)
450
451 if mgroup == null then
452 # singleton project
453 var mproject = new MProject(pn, model)
454 mgroup = new MGroup(pn, mproject, null) # same name for the root group
455 mgroup.filepath = path
456 mproject.root = mgroup
457 toolcontext.info("found project `{pn}` at {path}", 2)
458 end
459
460 var res = new ModulePath(pn, path, mgroup)
461 mgroup.module_paths.add(res)
462
463 identified_files[rp] = res
464 return res
465 end
466
467 # groups by path
468 private var mgroups = new HashMap[String, nullable MGroup]
469
470 # return the mgroup associated to a directory path
471 # if the directory is not a group null is returned
472 private fun get_mgroup(dirpath: String): nullable MGroup
473 do
474 var rdp = module_absolute_path(dirpath)
475 if mgroups.has_key(rdp) then
476 return mgroups[rdp]
477 end
478
479 # Hack, a group is determined by:
480 # * the presence of a honomymous nit file
481 # * the fact that the directory is named `src`
482 var pn = rdp.basename(".nit")
483 var mp = dirpath.join_path(pn + ".nit").simplify_path
484
485 var dirpath2 = dirpath
486 if not mp.file_exists then
487 if pn == "src" then
488 # With a src directory, the group name is the name of the parent directory
489 dirpath2 = rdp.dirname
490 pn = dirpath2.basename("")
491 else
492 return null
493 end
494 end
495
496 # check parent directory
497 var parentpath = dirpath.join_path("..").simplify_path
498 var parent = get_mgroup(parentpath)
499
500 var mgroup
501 if parent == null then
502 # no parent, thus new project
503 var mproject = new MProject(pn, model)
504 mgroup = new MGroup(pn, mproject, null) # same name for the root group
505 mproject.root = mgroup
506 toolcontext.info("found project `{mproject}` at {dirpath}", 2)
507 else
508 mgroup = new MGroup(pn, parent.mproject, parent)
509 toolcontext.info("found sub group `{mgroup.full_name}` at {dirpath}", 2)
510 end
511 var readme = dirpath2.join_path("README.md")
512 if not readme.file_exists then readme = dirpath2.join_path("README")
513 if readme.file_exists then
514 var mdoc = new MDoc
515 var s = new IFStream.open(readme)
516 while not s.eof do
517 mdoc.content.add(s.read_line)
518 end
519 mgroup.mdoc = mdoc
520 mdoc.original_mentity = mgroup
521 end
522 mgroup.filepath = dirpath
523 mgroups[rdp] = mgroup
524 return mgroup
525 end
526
527 # Transform relative paths (starting with '../') into absolute paths
528 private fun module_absolute_path(path: String): String do
529 return getcwd.join_path(path).simplify_path
530 end
531
532 # Try to load a module AST using a path.
533 # Display an error if there is a problem (IO / lexer / parser) and return null
534 fun load_module_ast(filename: String): nullable AModule
535 do
536 if filename.file_extension != "nit" then
537 self.toolcontext.error(null, "Error: file {filename} is not a valid nit module.")
538 return null
539 end
540 if not filename.file_exists then
541 self.toolcontext.error(null, "Error: file {filename} not found.")
542 return null
543 end
544
545 self.toolcontext.info("load module {filename}", 2)
546
547 # Load the file
548 var file = new IFStream.open(filename)
549 var lexer = new Lexer(new SourceFile(filename, file))
550 var parser = new Parser(lexer)
551 var tree = parser.parse
552 file.close
553
554 # Handle lexer and parser error
555 var nmodule = tree.n_base
556 if nmodule == null then
557 var neof = tree.n_eof
558 assert neof isa AError
559 error(neof, neof.message)
560 return null
561 end
562
563 return nmodule
564 end
565
566 # Try to load a module and its imported modules using a path.
567 # Display an error if there is a problem (IO / lexer / parser / importation) and return null
568 # Note: usually, you do not need this method, use `get_mmodule_by_name` instead.
569 fun load_module(filename: String): nullable AModule
570 do
571 # Look for the module
572 var file = identify_file(filename)
573 if file == null then return null # forward error
574
575 # Already known and loaded? then return it
576 var mmodule = file.mmodule
577 if mmodule != null then
578 return mmodule2nmodule[mmodule]
579 end
580
581 # Load it manually
582 var nmodule = load_module_ast(file.filepath)
583 if nmodule == null then return null # forward error
584
585 # build the mmodule and load imported modules
586 mmodule = build_a_mmodule(file.mgroup, file.name, nmodule)
587
588 if mmodule == null then return null # forward error
589
590 # Update the file information
591 file.mmodule = mmodule
592
593 # Load imported module
594 build_module_importation(nmodule)
595
596 return nmodule
597 end
598
599 # Injection of a new module without source.
600 # Used by the interpreted
601 fun load_rt_module(parent: nullable MModule, nmodule: AModule, mod_name: String): nullable AModule
602 do
603 # Create the module
604
605 var mgroup = null
606 if parent != null then mgroup = parent.mgroup
607 var mmodule = new MModule(model, mgroup, mod_name, nmodule.location)
608 nmodule.mmodule = mmodule
609 nmodules.add(nmodule)
610 self.mmodule2nmodule[mmodule] = nmodule
611
612 if parent!= null then
613 var imported_modules = new Array[MModule]
614 imported_modules.add(parent)
615 mmodule.set_visibility_for(parent, intrude_visibility)
616 mmodule.set_imported_mmodules(imported_modules)
617 else
618 build_module_importation(nmodule)
619 end
620
621 return nmodule
622 end
623
624 # Visit the AST and create the `MModule` object
625 private fun build_a_mmodule(mgroup: nullable MGroup, mod_name: String, nmodule: AModule): nullable MModule
626 do
627 # Check the module name
628 var decl = nmodule.n_moduledecl
629 if decl == null then
630 #warning(nmodule, "Warning: Missing 'module' keyword") #FIXME: NOT YET FOR COMPATIBILITY
631 else
632 var decl_name = decl.n_name.n_id.text
633 if decl_name != mod_name then
634 error(decl.n_name, "Error: module name missmatch; declared {decl_name} file named {mod_name}")
635 end
636 end
637
638 # Create the module
639 var mmodule = new MModule(model, mgroup, mod_name, nmodule.location)
640 nmodule.mmodule = mmodule
641 nmodules.add(nmodule)
642 self.mmodule2nmodule[mmodule] = nmodule
643
644 if decl != null then
645 var ndoc = decl.n_doc
646 if ndoc != null then
647 var mdoc = ndoc.to_mdoc
648 mmodule.mdoc = mdoc
649 mdoc.original_mentity = mmodule
650 else
651 advice(decl, "missing-doc", "Documentation warning: Undocumented module `{mmodule}`")
652 end
653 end
654
655 return mmodule
656 end
657
658 # Analysis the module importation and fill the module_importation_hierarchy
659 private fun build_module_importation(nmodule: AModule)
660 do
661 if nmodule.is_importation_done then return
662 nmodule.is_importation_done = true
663 var mmodule = nmodule.mmodule.as(not null)
664 var stdimport = true
665 var imported_modules = new Array[MModule]
666 for aimport in nmodule.n_imports do
667 stdimport = false
668 if not aimport isa AStdImport then
669 continue
670 end
671 var mgroup = mmodule.mgroup
672 if aimport.n_name.n_quad != null then mgroup = null # Start from top level
673 for grp in aimport.n_name.n_path do
674 var path = search_mmodule_by_name(grp, mgroup, grp.text)
675 if path == null then return # Skip error
676 mgroup = path.mgroup
677 end
678 var mod_name = aimport.n_name.n_id.text
679 var sup = self.get_mmodule_by_name(aimport.n_name, mgroup, mod_name)
680 if sup == null then continue # Skip error
681 aimport.mmodule = sup
682 imported_modules.add(sup)
683 var mvisibility = aimport.n_visibility.mvisibility
684 if mvisibility == protected_visibility then
685 error(aimport.n_visibility, "Error: only properties can be protected.")
686 return
687 end
688 if sup == mmodule then
689 error(aimport.n_name, "Error: Dependency loop in module {mmodule}.")
690 end
691 if sup.in_importation < mmodule then
692 error(aimport.n_name, "Error: Dependency loop between modules {mmodule} and {sup}.")
693 return
694 end
695 mmodule.set_visibility_for(sup, mvisibility)
696 end
697 if stdimport then
698 var mod_name = "standard"
699 var sup = self.get_mmodule_by_name(nmodule, null, mod_name)
700 if sup != null then # Skip error
701 imported_modules.add(sup)
702 mmodule.set_visibility_for(sup, public_visibility)
703 end
704 end
705 self.toolcontext.info("{mmodule} imports {imported_modules.join(", ")}", 3)
706 mmodule.set_imported_mmodules(imported_modules)
707
708 # Force standard to be public if imported
709 for sup in mmodule.in_importation.greaters do
710 if sup.name == "standard" then
711 mmodule.set_visibility_for(sup, public_visibility)
712 end
713 end
714
715 # TODO: Correctly check for useless importation
716 # It is even doable?
717 var directs = mmodule.in_importation.direct_greaters
718 for nim in nmodule.n_imports do
719 if not nim isa AStdImport then continue
720 var im = nim.mmodule
721 if im == null then continue
722 if directs.has(im) then continue
723 # This generates so much noise that it is simpler to just comment it
724 #warning(nim, "Warning: possible useless importation of {im}")
725 end
726 end
727
728 # All the loaded modules
729 var nmodules = new Array[AModule]
730
731 # Register the nmodule associated to each mmodule
732 # FIXME: why not refine the `MModule` class with a nullable attribute?
733 var mmodule2nmodule = new HashMap[MModule, AModule]
734
735 # Helper function to display an error on a node.
736 # Alias for `self.toolcontext.error(n.hot_location, text)`
737 fun error(n: ANode, text: String)
738 do
739 self.toolcontext.error(n.hot_location, text)
740 end
741
742 # Helper function to display a warning on a node.
743 # Alias for: `self.toolcontext.warning(n.hot_location, text)`
744 fun warning(n: ANode, tag, text: String)
745 do
746 self.toolcontext.warning(n.hot_location, tag, text)
747 end
748
749 # Helper function to display an advice on a node.
750 # Alias for: `self.toolcontext.advice(n.hot_location, text)`
751 fun advice(n: ANode, tag, text: String)
752 do
753 self.toolcontext.advice(n.hot_location, tag, text)
754 end
755
756 # Force to get the primitive method named `name` on the type `recv` or do a fatal error on `n`
757 fun force_get_primitive_method(n: nullable ANode, name: String, recv: MClass, mmodule: MModule): MMethod
758 do
759 var res = mmodule.try_get_primitive_method(name, recv)
760 if res == null then
761 var l = null
762 if n != null then l = n.hot_location
763 self.toolcontext.fatal_error(l, "Fatal Error: {recv} must have a property named {name}.")
764 abort
765 end
766 return res
767 end
768 end
769
770 # placeholder to a module file identified but not always loaded in a project
771 private class ModulePath
772 # The name of the module
773 # (it's the basename of the filepath)
774 var name: String
775
776 # The human path of the module
777 var filepath: String
778
779 # The group (and the project) of the possible module
780 var mgroup: MGroup
781
782 # The loaded module (if any)
783 var mmodule: nullable MModule = null
784
785 redef fun to_s do return filepath
786 end
787
788 redef class MGroup
789 # modules paths associated with the group
790 private var module_paths = new Array[ModulePath]
791 end
792
793 redef class AStdImport
794 # The imported module once determined
795 var mmodule: nullable MModule = null
796 end
797
798 redef class AModule
799 # The associated MModule once build by a `ModelBuilder`
800 var mmodule: nullable MModule
801 # Flag that indicate if the importation is already completed
802 var is_importation_done: Bool = false
803 end
804
805 redef class AVisibility
806 # The visibility level associated with the AST node class
807 fun mvisibility: MVisibility is abstract
808 end
809 redef class AIntrudeVisibility
810 redef fun mvisibility do return intrude_visibility
811 end
812 redef class APublicVisibility
813 redef fun mvisibility do return public_visibility
814 end
815 redef class AProtectedVisibility
816 redef fun mvisibility do return protected_visibility
817 end
818 redef class APrivateVisibility
819 redef fun mvisibility do return private_visibility
820 end
821
822 redef class ADoc
823 private var mdoc_cache: nullable MDoc
824 fun to_mdoc: MDoc
825 do
826 var res = mdoc_cache
827 if res != null then return res
828 res = new MDoc
829 for c in n_comment do
830 var text = c.text
831 if text.length < 2 then
832 res.content.add ""
833 continue
834 end
835 assert text.chars[0] == '#'
836 if text.chars[1] == ' ' then
837 text = text.substring_from(2) # eat starting `#` and space
838 else
839 text = text.substring_from(1) # eat atarting `#` only
840 end
841 if text.chars.last == '\n' then text = text.substring(0, text.length-1) # drop \n
842 res.content.add(text)
843 end
844 mdoc_cache = res
845 return res
846 end
847 end