loader: rename `get_group` to `identify_group`
[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 import ini
22
23 redef class ToolContext
24 # Option --path
25 var opt_path = new OptionArray("Set include path for loaders (may be used more than once)", "-I", "--path")
26
27 # Option --only-metamodel
28 var opt_only_metamodel = new OptionBool("Stop after meta-model processing", "--only-metamodel")
29
30 # Option --only-parse
31 var opt_only_parse = new OptionBool("Only proceed to parse step of loaders", "--only-parse")
32
33 redef init
34 do
35 super
36 option_context.add_option(opt_path, opt_only_parse, opt_only_metamodel)
37 end
38 end
39
40 redef class ModelBuilder
41 redef init
42 do
43 super
44
45 # Setup the paths value
46 paths.append(toolcontext.opt_path.value)
47
48 var path_env = "NIT_PATH".environ
49 if not path_env.is_empty then
50 paths.append(path_env.split_with(':'))
51 end
52
53 var nit_dir = toolcontext.nit_dir
54 var libname = nit_dir/"lib"
55 if libname.file_exists then paths.add(libname)
56 libname = nit_dir/"contrib"
57 if libname.file_exists then paths.add(libname)
58 end
59
60 # Load a bunch of modules.
61 # `modules` can contains filenames or module names.
62 # Imported modules are automatically loaded and modelized.
63 # The result is the corresponding model elements.
64 # Errors and warnings are printed with the toolcontext.
65 #
66 # Note: class and property model elements are not analysed.
67 fun parse(modules: Sequence[String]): Array[MModule]
68 do
69 var time0 = get_time
70 # Parse and recursively load
71 self.toolcontext.info("*** PARSE ***", 1)
72 var mmodules = new ArraySet[MModule]
73 for a in modules do
74 var nmodule = self.load_module(a)
75 if nmodule == null then continue # Skip error
76 # Load imported module
77 build_module_importation(nmodule)
78 var mmodule = nmodule.mmodule
79 if mmodule == null then continue # skip error
80 mmodules.add mmodule
81 end
82 var time1 = get_time
83 self.toolcontext.info("*** END PARSE: {time1-time0} ***", 2)
84
85 self.toolcontext.check_errors
86
87 if toolcontext.opt_only_parse.value then
88 self.toolcontext.info("*** ONLY PARSE...", 1)
89 exit(0)
90 end
91
92 return mmodules.to_a
93 end
94
95 # Load recursively all modules of the group `mgroup`.
96 # See `parse` for details.
97 fun parse_group(mgroup: MGroup): Array[MModule]
98 do
99 var res = new Array[MModule]
100 scan_group(mgroup)
101 for mg in mgroup.in_nesting.smallers do
102 for mp in mg.module_paths do
103 var nmodule = self.load_module(mp.filepath)
104 if nmodule == null then continue # Skip error
105 # Load imported module
106 build_module_importation(nmodule)
107 var mmodule = nmodule.mmodule
108 if mmodule == null then continue # Skip error
109 res.add mmodule
110 end
111 end
112 return res
113 end
114
115 # Load a bunch of modules and groups.
116 #
117 # Each name can be:
118 #
119 # * a path to a module, a group or a directory of packages.
120 # * a short name of a module or a group that are looked in the `paths` (-I)
121 #
122 # Then, for each entry, if it is:
123 #
124 # * a module, then is it parser and returned.
125 # * a group then recursively all its modules are parsed.
126 # * a directory of packages then all the modules of all packages are parsed.
127 # * else an error is displayed.
128 #
129 # See `parse` for details.
130 fun parse_full(names: Sequence[String]): Array[MModule]
131 do
132 var time0 = get_time
133 # Parse and recursively load
134 self.toolcontext.info("*** PARSE ***", 1)
135 var mmodules = new ArraySet[MModule]
136 for a in names do
137 # Case of a group (root or sub-directory)
138 var mgroup = self.identify_group(a)
139 if mgroup != null then
140 mmodules.add_all parse_group(mgroup)
141 continue
142 end
143
144 # Case of a directory that is not a group
145 var stat = a.to_path.stat
146 if stat != null and stat.is_dir then
147 self.toolcontext.info("look in directory {a}", 2)
148 var fs = a.files
149 # Try each entry as a group or a module
150 for f in fs do
151 var af = a/f
152 mgroup = identify_group(af)
153 if mgroup != null then
154 mmodules.add_all parse_group(mgroup)
155 continue
156 end
157 var mp = identify_file(af)
158 if mp != null then
159 var nmodule = self.load_module(af)
160 if nmodule == null then continue # Skip error
161 build_module_importation(nmodule)
162 var mmodule = nmodule.mmodule
163 if mmodule == null then continue # Skip error
164 mmodules.add mmodule
165 else
166 self.toolcontext.info("ignore file {af}", 2)
167 end
168 end
169 continue
170 end
171
172 var nmodule = self.load_module(a)
173 if nmodule == null then continue # Skip error
174 # Load imported module
175 build_module_importation(nmodule)
176 var mmodule = nmodule.mmodule
177 if mmodule == null then continue # Skip error
178 mmodules.add mmodule
179 end
180 var time1 = get_time
181 self.toolcontext.info("*** END PARSE: {time1-time0} ***", 2)
182
183 self.toolcontext.check_errors
184
185 if toolcontext.opt_only_parse.value then
186 self.toolcontext.info("*** ONLY PARSE...", 1)
187 exit(0)
188 end
189
190 return mmodules.to_a
191 end
192
193 # The list of directories to search for top level modules
194 # The list is initially set with:
195 #
196 # * the toolcontext --path option
197 # * the NIT_PATH environment variable
198 # * `toolcontext.nit_dir`
199 # Path can be added (or removed) by the client
200 var paths = new Array[String]
201
202 # Like (and used by) `get_mmodule_by_name` but just return the ModulePath
203 fun search_mmodule_by_name(anode: nullable ANode, mgroup: nullable MGroup, name: String): nullable ModulePath
204 do
205 # First, look in groups
206 var c = mgroup
207 if c != null then
208 var r = c.mpackage.root
209 assert r != null
210 scan_group(r)
211 var res = r.mmodule_paths_by_name(name)
212 if res.not_empty then return res.first
213 end
214
215 # Look at some known directories
216 var lookpaths = self.paths
217
218 # Look in the directory of the group package also (even if not explicitly in the path)
219 if mgroup != null then
220 # path of the root group
221 var dirname = mgroup.mpackage.root.filepath
222 if dirname != null then
223 dirname = dirname.join_path("..").simplify_path
224 if not lookpaths.has(dirname) and dirname.file_exists then
225 lookpaths = lookpaths.to_a
226 lookpaths.add(dirname)
227 end
228 end
229 end
230
231 var candidate = search_module_in_paths(anode.hot_location, name, lookpaths)
232
233 if candidate == null then
234 if mgroup != null then
235 error(anode, "Error: cannot find module `{name}` from `{mgroup.name}`. Tried: {lookpaths.join(", ")}.")
236 else
237 error(anode, "Error: cannot find module `{name}`. Tried: {lookpaths.join(", ")}.")
238 end
239 return null
240 end
241 return candidate
242 end
243
244 # Get a module by its short name; if required, the module is loaded, parsed and its hierarchies computed.
245 # If `mgroup` is set, then the module search starts from it up to the top level (see `paths`);
246 # if `mgroup` is null then the module is searched in the top level only.
247 # If no module exists or there is a name conflict, then an error on `anode` is displayed and null is returned.
248 fun get_mmodule_by_name(anode: nullable ANode, mgroup: nullable MGroup, name: String): nullable MModule
249 do
250 var path = search_mmodule_by_name(anode, mgroup, name)
251 if path == null then return null # Forward error
252 return load_module_path(path)
253 end
254
255 # Load and process importation of a given ModulePath.
256 #
257 # Basically chains `load_module` and `build_module_importation`.
258 fun load_module_path(path: ModulePath): nullable MModule
259 do
260 var res = self.load_module(path.filepath)
261 if res == null then return null # Forward error
262 # Load imported module
263 build_module_importation(res)
264 return res.mmodule
265 end
266
267 # Search a module `name` from path `lookpaths`.
268 # If found, the path of the file is returned
269 private fun search_module_in_paths(location: nullable Location, name: String, lookpaths: Collection[String]): nullable ModulePath
270 do
271 var res = new ArraySet[ModulePath]
272 for dirname in lookpaths do
273 # Try a single module file
274 var mp = identify_file((dirname/"{name}.nit").simplify_path)
275 if mp != null then res.add mp
276 # Try the default module of a group
277 var g = identify_group((dirname/name).simplify_path)
278 if g != null then
279 scan_group(g)
280 res.add_all g.mmodule_paths_by_name(name)
281 end
282 end
283 if res.is_empty then return null
284 if res.length > 1 then
285 toolcontext.error(location, "Error: conflicting module files for `{name}`: `{res.join(",")}`")
286 end
287 return res.first
288 end
289
290 # Search groups named `name` from paths `lookpaths`.
291 private fun search_group_in_paths(name: String, lookpaths: Collection[String]): ArraySet[MGroup]
292 do
293 var res = new ArraySet[MGroup]
294 for dirname in lookpaths do
295 # try a single group directory
296 var mg = identify_group(dirname/name)
297 if mg != null then
298 res.add mg
299 end
300 end
301 return res
302 end
303
304 # Cache for `identify_file` by realpath
305 private var identified_files_by_path = new HashMap[String, nullable ModulePath]
306
307 # All the currently identified modules.
308 # See `identify_file`.
309 var identified_files = new Array[ModulePath]
310
311 # Identify a source file and load the associated package and groups if required.
312 #
313 # This method does what the user expects when giving an argument to a Nit tool.
314 #
315 # * If `path` is an existing Nit source file (with the `.nit` extension),
316 # then the associated ModulePath is returned
317 # * If `path` is a directory (with a `/`),
318 # then the ModulePath of its default module is returned (if any)
319 # * If `path` is a simple identifier (eg. `digraph`),
320 # then the main module of the package `digraph` is searched in `paths` and returned.
321 #
322 # Silently return `null` if `path` does not exists or cannot be identified.
323 fun identify_file(path: String): nullable ModulePath
324 do
325 # special case for not a nit file
326 if not path.has_suffix(".nit") then
327 # search dirless files in known -I paths
328 if not path.chars.has('/') then
329 var res = search_module_in_paths(null, path, self.paths)
330 if res != null then return res
331 end
332
333 # Found nothing? maybe it is a group...
334 var candidate = null
335 if path.file_exists then
336 var mgroup = identify_group(path)
337 if mgroup != null then
338 var owner_path = mgroup.filepath.join_path(mgroup.name + ".nit")
339 if owner_path.file_exists then candidate = owner_path
340 end
341 end
342
343 if candidate == null then
344 return null
345 end
346 path = candidate
347 end
348
349 # Does the file exists?
350 if not path.file_exists then
351 return null
352 end
353
354 # Fast track, the path is already known
355 if identified_files_by_path.has_key(path) then return identified_files_by_path[path]
356 var rp = module_absolute_path(path)
357 if identified_files_by_path.has_key(rp) then return identified_files_by_path[rp]
358
359 var pn = path.basename(".nit")
360
361 # Search for a group
362 var mgrouppath = path.join_path("..").simplify_path
363 var mgroup = identify_group(mgrouppath)
364
365 if mgroup == null then
366 # singleton package
367 var mpackage = new MPackage(pn, model)
368 mgroup = new MGroup(pn, mpackage, null) # same name for the root group
369 mgroup.filepath = path
370 mpackage.root = mgroup
371 toolcontext.info("found singleton package `{pn}` at {path}", 2)
372
373 # Attach homonymous `ini` file to the package
374 var inipath = path.dirname / "{pn}.ini"
375 if inipath.file_exists then
376 var ini = new ConfigTree(inipath)
377 mpackage.ini = ini
378 end
379 end
380
381 var res = new ModulePath(pn, path, mgroup)
382 mgroup.module_paths.add(res)
383
384 identified_files_by_path[rp] = res
385 identified_files_by_path[path] = res
386 identified_files.add(res)
387 return res
388 end
389
390 # Groups by path
391 private var mgroups = new HashMap[String, nullable MGroup]
392
393 # Return the mgroup associated to a directory path.
394 # If the directory is not a group null is returned.
395 #
396 # Note: `paths` is also used to look for mgroups
397 fun identify_group(dirpath: String): nullable MGroup
398 do
399 var stat = dirpath.file_stat
400
401 if stat == null then do
402 # search dirless directories in known -I paths
403 if dirpath.chars.has('/') then return null
404 for p in paths do
405 var try = p / dirpath
406 stat = try.file_stat
407 if stat != null then
408 dirpath = try
409 break label
410 end
411 end
412 return null
413 end label
414
415 # Filter out non-directories
416 if not stat.is_dir then
417 return null
418 end
419
420 # Fast track, the path is already known
421 var rdp = module_absolute_path(dirpath)
422 if mgroups.has_key(rdp) then
423 return mgroups[rdp]
424 end
425
426 # By default, the name of the package or group is the base_name of the directory
427 var pn = rdp.basename
428
429 # Check `package.ini` that indicate a package
430 var ini = null
431 var parent = null
432 var inipath = dirpath / "package.ini"
433 if inipath.file_exists then
434 ini = new ConfigTree(inipath)
435 end
436
437 if ini == null then
438 # No ini, multiple course of action
439
440 # The root of the directory hierarchy in the file system.
441 if rdp == "/" then
442 mgroups[rdp] = null
443 return null
444 end
445
446 # Special stopper `packages.ini`
447 if (dirpath/"packages.ini").file_exists then
448 # dirpath cannot be a package since it is a package directory
449 mgroups[rdp] = null
450 return null
451 end
452
453 # check the parent directory (if it does not contain the stopper file)
454 var parentpath = dirpath.join_path("..").simplify_path
455 var stopper = parentpath / "packages.ini"
456 if not stopper.file_exists then
457 # Recursively get the parent group
458 parent = identify_group(parentpath)
459 if parent == null then
460 # Parent is not a group, thus we are not a group either
461 mgroups[rdp] = null
462 return null
463 end
464 end
465 end
466
467 var mgroup
468 if parent == null then
469 # no parent, thus new package
470 if ini != null then pn = ini["package.name"] or else pn
471 var mpackage = new MPackage(pn, model)
472 mgroup = new MGroup(pn, mpackage, null) # same name for the root group
473 mpackage.root = mgroup
474 toolcontext.info("found package `{mpackage}` at {dirpath}", 2)
475 mpackage.ini = ini
476 else
477 mgroup = new MGroup(pn, parent.mpackage, parent)
478 toolcontext.info("found sub group `{mgroup.full_name}` at {dirpath}", 2)
479 end
480
481 # search documentation
482 # in src first so the documentation of the package code can be distinct for the documentation of the package usage
483 var readme = dirpath.join_path("README.md")
484 if not readme.file_exists then readme = dirpath.join_path("README")
485 if readme.file_exists then
486 var mdoc = load_markdown(readme)
487 mgroup.mdoc = mdoc
488 mdoc.original_mentity = mgroup
489 end
490
491 mgroup.filepath = dirpath
492 mgroups[rdp] = mgroup
493 return mgroup
494 end
495
496 # Load a markdown file as a documentation object
497 fun load_markdown(filepath: String): MDoc
498 do
499 var s = new FileReader.open(filepath)
500 var lines = new Array[String]
501 var line_starts = new Array[Int]
502 var len = 1
503 while not s.eof do
504 var line = s.read_line
505 lines.add(line)
506 line_starts.add(len)
507 len += line.length + 1
508 end
509 s.close
510 var source = new SourceFile.from_string(filepath, lines.join("\n"))
511 source.line_starts.add_all line_starts
512 var mdoc = new MDoc(new Location(source, 1, lines.length, 0, 0))
513 mdoc.content.add_all(lines)
514 return mdoc
515 end
516
517 # Force the identification of all ModulePath of the group and sub-groups in the file system.
518 #
519 # When a group is scanned, its sub-groups hierarchy is filled (see `MGroup::in_nesting`)
520 # and the potential modules (and nested modules) are identified (see `MGroup::module_paths`).
521 #
522 # Basically, this recursively call `get_mgroup` and `identify_file` on each directory entry.
523 #
524 # No-op if the group was already scanned (see `MGroup::scanned`).
525 fun scan_group(mgroup: MGroup) do
526 if mgroup.scanned then return
527 mgroup.scanned = true
528 var p = mgroup.filepath
529 # a virtual group has nothing to scan
530 if p == null then return
531 for f in p.files do
532 var fp = p/f
533 var g = identify_group(fp)
534 # Recursively scan for groups of the same package
535 if g == null then
536 identify_file(fp)
537 else if g.mpackage == mgroup.mpackage then
538 scan_group(g)
539 end
540 end
541 end
542
543 # Transform relative paths (starting with '../') into absolute paths
544 private fun module_absolute_path(path: String): String do
545 return path.realpath
546 end
547
548 # Try to load a module AST using a path.
549 # Display an error if there is a problem (IO / lexer / parser) and return null
550 fun load_module_ast(filename: String): nullable AModule
551 do
552 if not filename.has_suffix(".nit") then
553 self.toolcontext.error(null, "Error: file `{filename}` is not a valid nit module.")
554 return null
555 end
556 if not filename.file_exists then
557 self.toolcontext.error(null, "Error: file `{filename}` not found.")
558 return null
559 end
560
561 self.toolcontext.info("load module {filename}", 2)
562
563 # Load the file
564 var file = new FileReader.open(filename)
565 var lexer = new Lexer(new SourceFile(filename, file))
566 var parser = new Parser(lexer)
567 var tree = parser.parse
568 file.close
569
570 # Handle lexer and parser error
571 var nmodule = tree.n_base
572 if nmodule == null then
573 var neof = tree.n_eof
574 assert neof isa AError
575 error(neof, neof.message)
576 return null
577 end
578
579 return nmodule
580 end
581
582 # Remove Nit source files from a list of arguments.
583 #
584 # Items of `args` that can be loaded as a nit file will be removed from `args` and returned.
585 fun filter_nit_source(args: Array[String]): Array[String]
586 do
587 var keep = new Array[String]
588 var res = new Array[String]
589 for a in args do
590 var l = identify_file(a)
591 if l == null then
592 keep.add a
593 else
594 res.add a
595 end
596 end
597 args.clear
598 args.add_all(keep)
599 return res
600 end
601
602 # Try to load a module using a path.
603 # Display an error if there is a problem (IO / lexer / parser) and return null.
604 # Note: usually, you do not need this method, use `get_mmodule_by_name` instead.
605 #
606 # The MModule is created however, the importation is not performed,
607 # therefore you should call `build_module_importation`.
608 fun load_module(filename: String): nullable AModule
609 do
610 # Look for the module
611 var file = identify_file(filename)
612 if file == null then
613 if filename.file_exists then
614 toolcontext.error(null, "Error: `{filename}` is not a Nit source file.")
615 else
616 toolcontext.error(null, "Error: cannot find module `{filename}`.")
617 end
618 return null
619 end
620
621 # Already known and loaded? then return it
622 var mmodule = file.mmodule
623 if mmodule != null then
624 return mmodule2nmodule[mmodule]
625 end
626
627 # Load it manually
628 var nmodule = load_module_ast(file.filepath)
629 if nmodule == null then return null # forward error
630
631 # build the mmodule and load imported modules
632 mmodule = build_a_mmodule(file.mgroup, file.name, nmodule)
633
634 if mmodule == null then return null # forward error
635
636 # Update the file information
637 file.mmodule = mmodule
638
639 return nmodule
640 end
641
642 # Injection of a new module without source.
643 # Used by the interpreter.
644 fun load_rt_module(parent: nullable MModule, nmodule: AModule, mod_name: String): nullable AModule
645 do
646 # Create the module
647
648 var mgroup = null
649 if parent != null then mgroup = parent.mgroup
650 var mmodule = new MModule(model, mgroup, mod_name, nmodule.location)
651 nmodule.mmodule = mmodule
652 nmodules.add(nmodule)
653 self.mmodule2nmodule[mmodule] = nmodule
654
655 if parent!= null then
656 var imported_modules = new Array[MModule]
657 imported_modules.add(parent)
658 mmodule.set_visibility_for(parent, intrude_visibility)
659 mmodule.set_imported_mmodules(imported_modules)
660 else
661 build_module_importation(nmodule)
662 end
663
664 return nmodule
665 end
666
667 # Visit the AST and create the `MModule` object
668 private fun build_a_mmodule(mgroup: nullable MGroup, mod_name: String, nmodule: AModule): nullable MModule
669 do
670 # Check the module name
671 var decl = nmodule.n_moduledecl
672 if decl != null then
673 var decl_name = decl.n_name.n_id.text
674 if decl_name != mod_name then
675 error(decl.n_name, "Error: module name mismatch; declared {decl_name} file named {mod_name}.")
676 end
677 end
678
679 # Check for conflicting module names in the package
680 if mgroup != null then
681 var others = model.get_mmodules_by_name(mod_name)
682 if others != null then for other in others do
683 if other.mgroup!= null and other.mgroup.mpackage == mgroup.mpackage then
684 var node: ANode
685 if decl == null then node = nmodule else node = decl.n_name
686 error(node, "Error: a module named `{other.full_name}` already exists at {other.location}.")
687 break
688 end
689 end
690 end
691
692 # Create the module
693 var mmodule = new MModule(model, mgroup, mod_name, nmodule.location)
694 nmodule.mmodule = mmodule
695 nmodules.add(nmodule)
696 self.mmodule2nmodule[mmodule] = nmodule
697
698 var source = nmodule.location.file
699 if source != null then
700 assert source.mmodule == null
701 source.mmodule = mmodule
702 end
703
704 if decl != null then
705 # Extract documentation
706 var ndoc = decl.n_doc
707 if ndoc != null then
708 var mdoc = ndoc.to_mdoc
709 mmodule.mdoc = mdoc
710 mdoc.original_mentity = mmodule
711 else
712 advice(decl, "missing-doc", "Documentation warning: Undocumented module `{mmodule}`")
713 end
714 # Is the module a test suite?
715 mmodule.is_test_suite = not decl.get_annotations("test_suite").is_empty
716 end
717
718 return mmodule
719 end
720
721 # Resolve the module identification for a given `AModuleName`.
722 #
723 # This method handles qualified names as used in `AModuleName`.
724 fun seach_module_by_amodule_name(n_name: AModuleName, mgroup: nullable MGroup): nullable ModulePath
725 do
726 var mod_name = n_name.n_id.text
727
728 # If a quad is given, we ignore the starting group (go from path)
729 if n_name.n_quad != null then mgroup = null
730
731 # If name not qualified, just search the name
732 if n_name.n_path.is_empty then
733 # Fast search if no n_path
734 return search_mmodule_by_name(n_name, mgroup, mod_name)
735 end
736
737 # If qualified and in a group
738 if mgroup != null then
739 # First search in the package
740 var r = mgroup.mpackage.root
741 assert r != null
742 scan_group(r)
743 # Get all modules with the final name
744 var res = r.mmodule_paths_by_name(mod_name)
745 # Filter out the name that does not match the qualifiers
746 res = [for x in res do if match_amodulename(n_name, x) then x]
747 if res.not_empty then
748 if res.length > 1 then
749 error(n_name, "Error: conflicting module files for `{mod_name}`: `{res.join(",")}`")
750 end
751 return res.first
752 end
753 end
754
755 # If no module yet, then assume that the first element of the path
756 # Is to be searched in the path.
757 var root_name = n_name.n_path.first.text
758 var roots = search_group_in_paths(root_name, paths)
759 if roots.is_empty then
760 error(n_name, "Error: cannot find `{root_name}`. Tried: {paths.join(", ")}.")
761 return null
762 end
763
764 var res = new ArraySet[ModulePath]
765 for r in roots do
766 # Then, for each root, collect modules that matches the qualifiers
767 scan_group(r)
768 var root_res = r.mmodule_paths_by_name(mod_name)
769 for x in root_res do if match_amodulename(n_name, x) then res.add x
770 end
771 if res.not_empty then
772 if res.length > 1 then
773 error(n_name, "Error: conflicting module files for `{mod_name}`: `{res.join(",")}`")
774 end
775 return res.first
776 end
777 # If still nothing, just call a basic search that will fail and will produce an error message
778 error(n_name, "Error: cannot find module `{mod_name}` from `{root_name}`. Tried: {paths.join(", ")}.")
779 return null
780 end
781
782 # Is elements of `n_name` correspond to the group nesting of `m`?
783 #
784 # Basically it check that `bar::foo` matches `bar/foo.nit` and `bar/baz/foo.nit`
785 # but not `baz/foo.nit` nor `foo/bar.nit`
786 #
787 # Is used by `seach_module_by_amodule_name` to validate qualified names.
788 private fun match_amodulename(n_name: AModuleName, m: ModulePath): Bool
789 do
790 var g: nullable MGroup = m.mgroup
791 for grp in n_name.n_path.reverse_iterator do
792 while g != null and grp.text != g.name do
793 g = g.parent
794 end
795 end
796 return g != null
797 end
798
799 # Analyze the module importation and fill the module_importation_hierarchy
800 #
801 # Unless you used `load_module`, the importation is already done and this method does a no-op.
802 fun build_module_importation(nmodule: AModule)
803 do
804 if nmodule.is_importation_done then return
805 nmodule.is_importation_done = true
806 var mmodule = nmodule.mmodule.as(not null)
807 var stdimport = true
808 var imported_modules = new Array[MModule]
809 for aimport in nmodule.n_imports do
810 # Do not imports conditional
811 var atconditionals = aimport.get_annotations("conditional")
812 if atconditionals.not_empty then continue
813
814 stdimport = false
815 if not aimport isa AStdImport then
816 continue
817 end
818
819 # Load the imported module
820 var suppath = seach_module_by_amodule_name(aimport.n_name, mmodule.mgroup)
821 if suppath == null then
822 mmodule.is_broken = true
823 nmodule.mmodule = null # invalidate the module
824 continue # Skip error
825 end
826 var sup = load_module_path(suppath)
827 if sup == null then
828 mmodule.is_broken = true
829 nmodule.mmodule = null # invalidate the module
830 continue # Skip error
831 end
832
833 aimport.mmodule = sup
834 imported_modules.add(sup)
835 var mvisibility = aimport.n_visibility.mvisibility
836 if mvisibility == protected_visibility then
837 mmodule.is_broken = true
838 error(aimport.n_visibility, "Error: only properties can be protected.")
839 mmodule.is_broken = true
840 nmodule.mmodule = null # invalidate the module
841 return
842 end
843 if sup == mmodule then
844 error(aimport.n_name, "Error: dependency loop in module {mmodule}.")
845 mmodule.is_broken = true
846 nmodule.mmodule = null # invalidate the module
847 end
848 if sup.in_importation < mmodule then
849 error(aimport.n_name, "Error: dependency loop between modules {mmodule} and {sup}.")
850 mmodule.is_broken = true
851 nmodule.mmodule = null # invalidate the module
852 return
853 end
854 mmodule.set_visibility_for(sup, mvisibility)
855 end
856 if stdimport then
857 var mod_name = "core"
858 var sup = self.get_mmodule_by_name(nmodule, null, mod_name)
859 if sup == null then
860 mmodule.is_broken = true
861 nmodule.mmodule = null # invalidate the module
862 else # Skip error
863 imported_modules.add(sup)
864 mmodule.set_visibility_for(sup, public_visibility)
865 end
866 end
867
868 # Declare conditional importation
869 for aimport in nmodule.n_imports do
870 if not aimport isa AStdImport then continue
871 var atconditionals = aimport.get_annotations("conditional")
872 if atconditionals.is_empty then continue
873
874 var suppath = seach_module_by_amodule_name(aimport.n_name, mmodule.mgroup)
875 if suppath == null then continue # skip error
876
877 for atconditional in atconditionals do
878 var nargs = atconditional.n_args
879 if nargs.is_empty then
880 error(atconditional, "Syntax Error: `conditional` expects module identifiers as arguments.")
881 continue
882 end
883
884 # The rule
885 var rule = new Array[Object]
886
887 # First element is the goal, thus
888 rule.add suppath
889
890 # Second element is the first condition, that is to be a client of the current module
891 rule.add mmodule
892
893 # Other condition are to be also a client of each modules indicated as arguments of the annotation
894 for narg in nargs do
895 var id = narg.as_id
896 if id == null then
897 error(narg, "Syntax Error: `conditional` expects module identifier as arguments.")
898 continue
899 end
900
901 var mp = search_mmodule_by_name(narg, mmodule.mgroup, id)
902 if mp == null then continue
903
904 rule.add mp
905 end
906
907 conditional_importations.add rule
908 end
909 end
910
911 mmodule.set_imported_mmodules(imported_modules)
912
913 apply_conditional_importations(mmodule)
914
915 self.toolcontext.info("{mmodule} imports {mmodule.in_importation.direct_greaters.join(", ")}", 3)
916
917 # Force `core` to be public if imported
918 for sup in mmodule.in_importation.greaters do
919 if sup.name == "core" then
920 mmodule.set_visibility_for(sup, public_visibility)
921 end
922 end
923
924 # TODO: Correctly check for useless importation
925 # It is even doable?
926 var directs = mmodule.in_importation.direct_greaters
927 for nim in nmodule.n_imports do
928 if not nim isa AStdImport then continue
929 var im = nim.mmodule
930 if im == null then continue
931 if directs.has(im) then continue
932 # This generates so much noise that it is simpler to just comment it
933 #warning(nim, "Warning: possible useless importation of {im}")
934 end
935 end
936
937 # Global list of conditional importation rules.
938 #
939 # Each rule is a "Horn clause"-like sequence of modules.
940 # It means that the first module is the module to automatically import.
941 # The remaining modules are the conditions of the rule.
942 #
943 # Each module is either represented by a MModule (if the module is already loaded)
944 # or by a ModulePath (if the module is not yet loaded).
945 #
946 # Rules are declared by `build_module_importation` and are applied by `apply_conditional_importations`
947 # (and `build_module_importation` that calls it).
948 #
949 # TODO (when the loader will be rewritten): use a better representation and move up rules in the model.
950 private var conditional_importations = new Array[SequenceRead[Object]]
951
952 # Extends the current importations according to imported rules about conditional importation
953 fun apply_conditional_importations(mmodule: MModule)
954 do
955 # Because a conditional importation may cause additional conditional importation, use a fixed point
956 # The rules are checked naively because we assume that it does not worth to be optimized
957 var check_conditional_importations = true
958 while check_conditional_importations do
959 check_conditional_importations = false
960
961 for ci in conditional_importations do
962 # Check conditions
963 for i in [1..ci.length[ do
964 var rule_element = ci[i]
965 # An element of a rule is either a MModule or a ModulePath
966 # We need the mmodule to resonate on the importation
967 var m
968 if rule_element isa MModule then
969 m = rule_element
970 else if rule_element isa ModulePath then
971 m = rule_element.mmodule
972 # Is loaded?
973 if m == null then continue label
974 else
975 abort
976 end
977 # Is imported?
978 if not mmodule.in_importation.greaters.has(m) then continue label
979 end
980 # Still here? It means that all conditions modules are loaded and imported
981
982 # Identify the module to automatically import
983 var suppath = ci.first.as(ModulePath)
984 var sup = load_module_path(suppath)
985 if sup == null then continue
986
987 # Do nothing if already imported
988 if mmodule.in_importation.greaters.has(sup) then continue label
989
990 # Import it
991 self.toolcontext.info("{mmodule} conditionally imports {sup}", 3)
992 # TODO visibility rules (currently always public)
993 mmodule.set_visibility_for(sup, public_visibility)
994 # TODO linearization rules (currently added at the end in the order of the rules)
995 mmodule.set_imported_mmodules([sup])
996
997 # Prepare to reapply the rules
998 check_conditional_importations = true
999 end label
1000 end
1001 end
1002
1003 # All the loaded modules
1004 var nmodules = new Array[AModule]
1005
1006 # Register the nmodule associated to each mmodule
1007 #
1008 # Public clients need to use `mmodule2node` to access stuff.
1009 private var mmodule2nmodule = new HashMap[MModule, AModule]
1010
1011 # Retrieve the associated AST node of a mmodule.
1012 # This method is used to associate model entity with syntactic entities.
1013 #
1014 # If the module is not associated with a node, returns null.
1015 fun mmodule2node(mmodule: MModule): nullable AModule
1016 do
1017 return mmodule2nmodule.get_or_null(mmodule)
1018 end
1019 end
1020
1021 # File-system location of a module (file) that is identified but not always loaded.
1022 class ModulePath
1023 # The name of the module
1024 # (it's the basename of the filepath)
1025 var name: String
1026
1027 # The human path of the module
1028 var filepath: String
1029
1030 # The group (and the package) of the possible module
1031 var mgroup: MGroup
1032
1033 # The loaded module (if any)
1034 var mmodule: nullable MModule = null
1035
1036 redef fun to_s do return filepath
1037 end
1038
1039 redef class MPackage
1040 # The associated `.ini` file, if any
1041 #
1042 # The `ini` file is given as is and might contain invalid or missing information.
1043 #
1044 # Some packages, like stand-alone packages or virtual packages have no `ini` file associated.
1045 var ini: nullable ConfigTree = null
1046 end
1047
1048 redef class MGroup
1049 # Modules paths associated with the group
1050 var module_paths = new Array[ModulePath]
1051
1052 # Is the group interesting for a final user?
1053 #
1054 # Groups are mandatory in the model but for simple packages they are not
1055 # always interesting.
1056 #
1057 # A interesting group has, at least, one of the following true:
1058 #
1059 # * it has 2 modules or more
1060 # * it has a subgroup
1061 # * it has a documentation
1062 fun is_interesting: Bool
1063 do
1064 return module_paths.length > 1 or
1065 mmodules.length > 1 or
1066 not in_nesting.direct_smallers.is_empty or
1067 mdoc != null or
1068 (mmodules.length == 1 and default_mmodule == null)
1069 end
1070
1071 # Are files and directories in self scanned?
1072 #
1073 # See `ModelBuilder::scan_group`.
1074 var scanned = false
1075
1076 # Return the modules in self and subgroups named `name`.
1077 #
1078 # If `self` is not scanned (see `ModelBuilder::scan_group`) the
1079 # results might be partial.
1080 fun mmodule_paths_by_name(name: String): Array[ModulePath]
1081 do
1082 var res = new Array[ModulePath]
1083 for g in in_nesting.smallers do
1084 for mp in g.module_paths do
1085 if mp.name == name then
1086 res.add mp
1087 end
1088 end
1089 end
1090 return res
1091 end
1092 end
1093
1094 redef class SourceFile
1095 # Associated mmodule, once created
1096 var mmodule: nullable MModule = null
1097 end
1098
1099 redef class AStdImport
1100 # The imported module once determined
1101 var mmodule: nullable MModule = null
1102 end
1103
1104 redef class AModule
1105 # The associated MModule once build by a `ModelBuilder`
1106 var mmodule: nullable MModule
1107 # Flag that indicate if the importation is already completed
1108 var is_importation_done: Bool = false
1109 end