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