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