1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2012 Jean Privat <jean@pryen.org>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Loading of Nit source files
20 import modelbuilder_base
23 redef class ToolContext
25 var opt_path
= new OptionArray("Set include path for loaders (may be used more than once)", "-I", "--path")
27 # Option --only-metamodel
28 var opt_only_metamodel
= new OptionBool("Stop after meta-model processing", "--only-metamodel")
31 var opt_only_parse
= new OptionBool("Only proceed to parse step of loaders", "--only-parse")
36 option_context
.add_option
(opt_path
, opt_only_parse
, opt_only_metamodel
)
40 redef class ModelBuilder
45 # Setup the paths value
46 paths
.append
(toolcontext
.opt_path
.value
)
48 var path_env
= "NIT_PATH".environ
49 if not path_env
.is_empty
then
50 paths
.append
(path_env
.split_with
(':'))
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
)
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.
66 # Note: class and property model elements are not analysed.
67 fun parse
(modules
: Sequence[String]): Array[MModule]
70 # Parse and recursively load
71 self.toolcontext
.info
("*** PARSE ***", 1)
72 var mmodules
= new ArraySet[MModule]
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
83 self.toolcontext
.info
("*** END PARSE: {time1-time0} ***", 2)
85 self.toolcontext
.check_errors
87 if toolcontext
.opt_only_parse
.value
then
88 self.toolcontext
.info
("*** ONLY PARSE...", 1)
95 # Load recursively all modules of the group `mgroup`.
96 # See `parse` for details.
97 fun parse_group
(mgroup
: MGroup): Array[MModule]
99 var res
= new Array[MModule]
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
115 # Load a bunch of modules and groups.
119 # * a path to a module, a group or a directory of projects.
120 # * a short name of a module or a group that are looked in the `paths` (-I)
122 # Then, for each entry, if it is:
124 # * a module, then is it parser and returned.
125 # * a group then recursively all its modules are parsed.
126 # * a directory of projects then all the modules of all projects are parsed.
127 # * else an error is displayed.
129 # See `parse` for details.
130 fun parse_full
(names
: Sequence[String]): Array[MModule]
133 # Parse and recursively load
134 self.toolcontext
.info
("*** PARSE ***", 1)
135 var mmodules
= new ArraySet[MModule]
138 var mgroup
= self.get_mgroup
(a
)
139 if mgroup
!= null then
140 mmodules
.add_all parse_group
(mgroup
)
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)
149 # Try each entry as a group or a module
152 mgroup
= get_mgroup
(af
)
153 if mgroup
!= null then
154 mmodules
.add_all parse_group
(mgroup
)
157 var mp
= identify_file
(af
)
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
166 self.toolcontext
.info
("ignore file {af}", 2)
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
181 self.toolcontext
.info
("*** END PARSE: {time1-time0} ***", 2)
183 self.toolcontext
.check_errors
185 if toolcontext
.opt_only_parse
.value
then
186 self.toolcontext
.info
("*** ONLY PARSE...", 1)
193 # The list of directories to search for top level modules
194 # The list is initially set with:
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]
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
205 # First, look in groups
208 var r
= c
.mproject
.root
211 var res
= r
.mmodule_paths_by_name
(name
)
212 if res
.not_empty
then return res
.first
215 # Look at some known directories
216 var lookpaths
= self.paths
218 # Look in the directory of the group project also (even if not explicitly in the path)
219 if mgroup
!= null then
220 # path of the root group
221 var dirname
= mgroup
.mproject
.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
)
231 var candidate
= search_module_in_paths
(anode
.hot_location
, name
, lookpaths
)
233 if candidate
== null then
234 if mgroup
!= null then
235 error
(anode
, "Error: cannot find module `{name}` from `{mgroup.name}`. Tried: {lookpaths.join(", ")}.")
237 error
(anode
, "Error: cannot find module `{name}`. Tried: {lookpaths.join(", ")}.")
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
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
)
255 # Load and process importation of a given ModulePath.
257 # Basically chains `load_module` and `build_module_importation`.
258 fun load_module_path
(path
: ModulePath): nullable MModule
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
)
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
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
= get_mgroup
((dirname
/name
).simplify_path
)
280 res
.add_all g
.mmodule_paths_by_name
(name
)
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(",")}`")
290 # Cache for `identify_file` by realpath
291 private var identified_files_by_path
= new HashMap[String, nullable ModulePath]
293 # All the currently identified modules.
294 # See `identify_file`.
295 var identified_files
= new Array[ModulePath]
297 # Identify a source file and load the associated project and groups if required.
299 # This method does what the user expects when giving an argument to a Nit tool.
301 # * If `path` is an existing Nit source file (with the `.nit` extension),
302 # then the associated ModulePath is returned
303 # * If `path` is a directory (with a `/`),
304 # then the ModulePath of its default module is returned (if any)
305 # * If `path` is a simple identifier (eg. `digraph`),
306 # then the main module of the project `digraph` is searched in `paths` and returned.
308 # Silently return `null` if `path` does not exists or cannot be identified.
309 fun identify_file
(path
: String): nullable ModulePath
311 # special case for not a nit file
312 if path
.file_extension
!= "nit" then
313 # search dirless files in known -I paths
314 if not path
.chars
.has
('/') then
315 var res
= search_module_in_paths
(null, path
, self.paths
)
316 if res
!= null then return res
319 # Found nothing? maybe it is a group...
321 if path
.file_exists
then
322 var mgroup
= get_mgroup
(path
)
323 if mgroup
!= null then
324 var owner_path
= mgroup
.filepath
.join_path
(mgroup
.name
+ ".nit")
325 if owner_path
.file_exists
then candidate
= owner_path
329 if candidate
== null then
335 # Does the file exists?
336 if not path
.file_exists
then
340 # Fast track, the path is already known
341 var pn
= path
.basename
(".nit")
342 var rp
= module_absolute_path
(path
)
343 if identified_files_by_path
.has_key
(rp
) then return identified_files_by_path
[rp
]
346 var mgrouppath
= path
.join_path
("..").simplify_path
347 var mgroup
= get_mgroup
(mgrouppath
)
349 if mgroup
== null then
351 var mproject
= new MProject(pn
, model
)
352 mgroup
= new MGroup(pn
, mproject
, null) # same name for the root group
353 mgroup
.filepath
= path
354 mproject
.root
= mgroup
355 toolcontext
.info
("found singleton project `{pn}` at {path}", 2)
358 var res
= new ModulePath(pn
, path
, mgroup
)
359 mgroup
.module_paths
.add
(res
)
361 identified_files_by_path
[rp
] = res
362 identified_files
.add
(res
)
367 private var mgroups
= new HashMap[String, nullable MGroup]
369 # Return the mgroup associated to a directory path.
370 # If the directory is not a group null is returned.
372 # Note: `paths` is also used to look for mgroups
373 fun get_mgroup
(dirpath
: String): nullable MGroup
375 if not dirpath
.file_exists
then do
377 var try
= p
/ dirpath
378 if try
.file_exists
then
386 var rdp
= module_absolute_path
(dirpath
)
387 if mgroups
.has_key
(rdp
) then
391 # Hack, a group is determined by one of the following:
392 # * the presence of a honomymous nit file
393 # * the fact that the directory is named `src`
394 # * the fact that there is a sub-directory named `src`
395 var pn
= rdp
.basename
(".nit")
396 var mp
= dirpath
.join_path
(pn
+ ".nit").simplify_path
398 # dirpath2 is the root directory
399 # dirpath is the src subdirectory directory, if any, else it is the same that dirpath2
400 var dirpath2
= dirpath
401 if not mp
.file_exists
then
403 # With a src directory, the group name is the name of the parent directory
404 dirpath2
= rdp
.dirname
405 pn
= dirpath2
.basename
407 # Check a `src` subdirectory
408 dirpath
= dirpath2
/ "src"
409 if not dirpath
.file_exists
then
410 # All rules failed, so return null
416 # check parent directory
417 var parentpath
= dirpath2
.join_path
("..").simplify_path
418 var parent
= get_mgroup
(parentpath
)
421 if parent
== null then
422 # no parent, thus new project
423 var mproject
= new MProject(pn
, model
)
424 mgroup
= new MGroup(pn
, mproject
, null) # same name for the root group
425 mproject
.root
= mgroup
426 toolcontext
.info
("found project `{mproject}` at {dirpath}", 2)
428 mgroup
= new MGroup(pn
, parent
.mproject
, parent
)
429 toolcontext
.info
("found sub group `{mgroup.full_name}` at {dirpath}", 2)
432 # search documentation
433 # in src first so the documentation of the project code can be distinct for the documentation of the project usage
434 var readme
= dirpath
.join_path
("README.md")
435 if not readme
.file_exists
then readme
= dirpath
.join_path
("README")
436 if not readme
.file_exists
then readme
= dirpath2
.join_path
("README.md")
437 if not readme
.file_exists
then readme
= dirpath2
.join_path
("README")
438 if readme
.file_exists
then
439 var mdoc
= load_markdown
(readme
)
441 mdoc
.original_mentity
= mgroup
444 mgroup
.filepath
= dirpath
445 mgroups
[module_absolute_path
(dirpath
)] = mgroup
446 mgroups
[module_absolute_path
(dirpath2
)] = mgroup
450 # Load a markdown file as a documentation object
451 fun load_markdown
(filepath
: String): MDoc
453 var s
= new FileReader.open
(filepath
)
454 var lines
= new Array[String]
455 var line_starts
= new Array[Int]
458 var line
= s
.read_line
461 len
+= line
.length
+ 1
464 var source
= new SourceFile.from_string
(filepath
, lines
.join
("\n"))
465 source
.line_starts
.add_all line_starts
466 var mdoc
= new MDoc(new Location(source
, 1, lines
.length
, 0, 0))
467 mdoc
.content
.add_all
(lines
)
471 # Force the identification of all ModulePath of the group and sub-groups in the file system.
473 # When a group is scanned, its sub-groups hierarchy is filled (see `MGroup::in_nesting`)
474 # and the potential modules (and nested modules) are identified (see `MGroup::module_paths`).
476 # Basically, this recursively call `get_mgroup` and `identify_file` on each directory entry.
478 # No-op if the group was already scanned (see `MGroup::scanned`).
479 fun scan_group
(mgroup
: MGroup) do
480 if mgroup
.scanned
then return
481 mgroup
.scanned
= true
482 var p
= mgroup
.filepath
483 # a virtual group has nothing to scan
484 if p
== null then return
487 var g
= get_mgroup
(fp
)
488 # Recursively scan for groups of the same project
489 if g
!= null and g
.mproject
== mgroup
.mproject
then
496 # Transform relative paths (starting with '../') into absolute paths
497 private fun module_absolute_path
(path
: String): String do
498 return getcwd
.join_path
(path
).simplify_path
501 # Try to load a module AST using a path.
502 # Display an error if there is a problem (IO / lexer / parser) and return null
503 fun load_module_ast
(filename
: String): nullable AModule
505 if filename
.file_extension
!= "nit" then
506 self.toolcontext
.error
(null, "Error: file `{filename}` is not a valid nit module.")
509 if not filename
.file_exists
then
510 self.toolcontext
.error
(null, "Error: file `{filename}` not found.")
514 self.toolcontext
.info
("load module {filename}", 2)
517 var file
= new FileReader.open
(filename
)
518 var lexer
= new Lexer(new SourceFile(filename
, file
))
519 var parser
= new Parser(lexer
)
520 var tree
= parser
.parse
523 # Handle lexer and parser error
524 var nmodule
= tree
.n_base
525 if nmodule
== null then
526 var neof
= tree
.n_eof
527 assert neof
isa AError
528 error
(neof
, neof
.message
)
535 # Remove Nit source files from a list of arguments.
537 # Items of `args` that can be loaded as a nit file will be removed from `args` and returned.
538 fun filter_nit_source
(args
: Array[String]): Array[String]
540 var keep
= new Array[String]
541 var res
= new Array[String]
543 var l
= identify_file
(a
)
555 # Try to load a module using a path.
556 # Display an error if there is a problem (IO / lexer / parser) and return null.
557 # Note: usually, you do not need this method, use `get_mmodule_by_name` instead.
559 # The MModule is created however, the importation is not performed,
560 # therefore you should call `build_module_importation`.
561 fun load_module
(filename
: String): nullable AModule
563 # Look for the module
564 var file
= identify_file
(filename
)
566 if filename
.file_exists
then
567 toolcontext
.error
(null, "Error: `{filename}` is not a Nit source file.")
569 toolcontext
.error
(null, "Error: cannot find module `{filename}`.")
574 # Already known and loaded? then return it
575 var mmodule
= file
.mmodule
576 if mmodule
!= null then
577 return mmodule2nmodule
[mmodule
]
581 var nmodule
= load_module_ast
(file
.filepath
)
582 if nmodule
== null then return null # forward error
584 # build the mmodule and load imported modules
585 mmodule
= build_a_mmodule
(file
.mgroup
, file
.name
, nmodule
)
587 if mmodule
== null then return null # forward error
589 # Update the file information
590 file
.mmodule
= mmodule
595 # Injection of a new module without source.
596 # Used by the interpreter.
597 fun load_rt_module
(parent
: nullable MModule, nmodule
: AModule, mod_name
: String): nullable AModule
602 if parent
!= null then mgroup
= parent
.mgroup
603 var mmodule
= new MModule(model
, mgroup
, mod_name
, nmodule
.location
)
604 nmodule
.mmodule
= mmodule
605 nmodules
.add
(nmodule
)
606 self.mmodule2nmodule
[mmodule
] = nmodule
608 if parent
!= null then
609 var imported_modules
= new Array[MModule]
610 imported_modules
.add
(parent
)
611 mmodule
.set_visibility_for
(parent
, intrude_visibility
)
612 mmodule
.set_imported_mmodules
(imported_modules
)
614 build_module_importation
(nmodule
)
620 # Visit the AST and create the `MModule` object
621 private fun build_a_mmodule
(mgroup
: nullable MGroup, mod_name
: String, nmodule
: AModule): nullable MModule
623 # Check the module name
624 var decl
= nmodule
.n_moduledecl
626 var decl_name
= decl
.n_name
.n_id
.text
627 if decl_name
!= mod_name
then
628 error
(decl
.n_name
, "Error: module name mismatch; declared {decl_name} file named {mod_name}.")
632 # Check for conflicting module names in the project
633 if mgroup
!= null then
634 var others
= model
.get_mmodules_by_name
(mod_name
)
635 if others
!= null then for other
in others
do
636 if other
.mgroup
!= null and other
.mgroup
.mproject
== mgroup
.mproject
then
638 if decl
== null then node
= nmodule
else node
= decl
.n_name
639 error
(node
, "Error: a module named `{other.full_name}` already exists at {other.location}.")
646 var mmodule
= new MModule(model
, mgroup
, mod_name
, nmodule
.location
)
647 nmodule
.mmodule
= mmodule
648 nmodules
.add
(nmodule
)
649 self.mmodule2nmodule
[mmodule
] = nmodule
651 var source
= nmodule
.location
.file
652 if source
!= null then
653 assert source
.mmodule
== null
654 source
.mmodule
= mmodule
658 # Extract documentation
659 var ndoc
= decl
.n_doc
661 var mdoc
= ndoc
.to_mdoc
663 mdoc
.original_mentity
= mmodule
665 advice
(decl
, "missing-doc", "Documentation warning: Undocumented module `{mmodule}`")
667 # Is the module a test suite?
668 mmodule
.is_test_suite
= not decl
.get_annotations
("test_suite").is_empty
674 # Resolve the module identification for a given `AModuleName`.
676 # This method handles qualified names as used in `AModuleName`.
677 fun seach_module_by_amodule_name
(n_name
: AModuleName, mgroup
: nullable MGroup): nullable ModulePath
679 if n_name
.n_quad
!= null then mgroup
= null # Start from top level
680 for grp
in n_name
.n_path
do
681 var path
= search_mmodule_by_name
(grp
, mgroup
, grp
.text
)
682 if path
== null then return null # Forward error
685 var mod_name
= n_name
.n_id
.text
686 return search_mmodule_by_name
(n_name
, mgroup
, mod_name
)
689 # Analyze the module importation and fill the module_importation_hierarchy
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)
694 if nmodule
.is_importation_done
then return
695 nmodule
.is_importation_done
= true
696 var mmodule
= nmodule
.mmodule
.as(not null)
698 var imported_modules
= new Array[MModule]
699 for aimport
in nmodule
.n_imports
do
700 # Do not imports conditional
701 var atconditionals
= aimport
.get_annotations
("conditional")
702 if atconditionals
.not_empty
then continue
705 if not aimport
isa AStdImport then
709 # Load the imported module
710 var suppath
= seach_module_by_amodule_name
(aimport
.n_name
, mmodule
.mgroup
)
711 if suppath
== null then
712 nmodule
.mmodule
= null # invalidate the module
713 continue # Skip error
715 var sup
= load_module_path
(suppath
)
717 nmodule
.mmodule
= null # invalidate the module
718 continue # Skip error
721 aimport
.mmodule
= sup
722 imported_modules
.add
(sup
)
723 var mvisibility
= aimport
.n_visibility
.mvisibility
724 if mvisibility
== protected_visibility
then
725 error
(aimport
.n_visibility
, "Error: only properties can be protected.")
726 nmodule
.mmodule
= null # invalidate the module
729 if sup
== mmodule
then
730 error
(aimport
.n_name
, "Error: dependency loop in module {mmodule}.")
731 nmodule
.mmodule
= null # invalidate the module
733 if sup
.in_importation
< mmodule
then
734 error
(aimport
.n_name
, "Error: dependency loop between modules {mmodule} and {sup}.")
735 nmodule
.mmodule
= null # invalidate the module
738 mmodule
.set_visibility_for
(sup
, mvisibility
)
741 var mod_name
= "standard"
742 var sup
= self.get_mmodule_by_name
(nmodule
, null, mod_name
)
744 nmodule
.mmodule
= null # invalidate the module
746 imported_modules
.add
(sup
)
747 mmodule
.set_visibility_for
(sup
, public_visibility
)
751 # Declare conditional importation
752 for aimport
in nmodule
.n_imports
do
753 if not aimport
isa AStdImport then continue
754 var atconditionals
= aimport
.get_annotations
("conditional")
755 if atconditionals
.is_empty
then continue
757 var suppath
= seach_module_by_amodule_name
(aimport
.n_name
, mmodule
.mgroup
)
758 if suppath
== null then continue # skip error
760 for atconditional
in atconditionals
do
761 var nargs
= atconditional
.n_args
762 if nargs
.is_empty
then
763 error
(atconditional
, "Syntax Error: `conditional` expects module identifiers as arguments.")
768 var rule
= new Array[Object]
770 # First element is the goal, thus
773 # Second element is the first condition, that is to be a client of the current module
776 # Other condition are to be also a client of each modules indicated as arguments of the annotation
780 error
(narg
, "Syntax Error: `conditional` expects module identifier as arguments.")
784 var mp
= search_mmodule_by_name
(narg
, mmodule
.mgroup
, id
)
785 if mp
== null then continue
790 conditional_importations
.add rule
794 mmodule
.set_imported_mmodules
(imported_modules
)
796 apply_conditional_importations
(mmodule
)
798 self.toolcontext
.info
("{mmodule} imports {mmodule.in_importation.direct_greaters.join(", ")}", 3)
800 # Force standard to be public if imported
801 for sup
in mmodule
.in_importation
.greaters
do
802 if sup
.name
== "standard" then
803 mmodule
.set_visibility_for
(sup
, public_visibility
)
807 # TODO: Correctly check for useless importation
809 var directs
= mmodule
.in_importation
.direct_greaters
810 for nim
in nmodule
.n_imports
do
811 if not nim
isa AStdImport then continue
813 if im
== null then continue
814 if directs
.has
(im
) then continue
815 # This generates so much noise that it is simpler to just comment it
816 #warning(nim, "Warning: possible useless importation of {im}")
820 # Global list of conditional importation rules.
822 # Each rule is a "Horn clause"-like sequence of modules.
823 # It means that the first module is the module to automatically import.
824 # The remaining modules are the conditions of the rule.
826 # Each module is either represented by a MModule (if the module is already loaded)
827 # or by a ModulePath (if the module is not yet loaded).
829 # Rules are declared by `build_module_importation` and are applied by `apply_conditional_importations`
830 # (and `build_module_importation` that calls it).
832 # TODO (when the loader will be rewritten): use a better representation and move up rules in the model.
833 private var conditional_importations
= new Array[SequenceRead[Object]]
835 # Extends the current importations according to imported rules about conditional importation
836 fun apply_conditional_importations
(mmodule
: MModule)
838 # Because a conditional importation may cause additional conditional importation, use a fixed point
839 # The rules are checked naively because we assume that it does not worth to be optimized
840 var check_conditional_importations
= true
841 while check_conditional_importations
do
842 check_conditional_importations
= false
844 for ci
in conditional_importations
do
846 for i
in [1..ci
.length
[ do
847 var rule_element
= ci
[i
]
848 # An element of a rule is either a MModule or a ModulePath
849 # We need the mmodule to resonate on the importation
851 if rule_element
isa MModule then
853 else if rule_element
isa ModulePath then
854 m
= rule_element
.mmodule
856 if m
== null then continue label
861 if not mmodule
.in_importation
.greaters
.has
(m
) then continue label
863 # Still here? It means that all conditions modules are loaded and imported
865 # Identify the module to automatically import
866 var suppath
= ci
.first
.as(ModulePath)
867 var sup
= load_module_path
(suppath
)
868 if sup
== null then continue
870 # Do nothing if already imported
871 if mmodule
.in_importation
.greaters
.has
(sup
) then continue label
874 self.toolcontext
.info
("{mmodule} conditionally imports {sup}", 3)
875 # TODO visibility rules (currently always public)
876 mmodule
.set_visibility_for
(sup
, public_visibility
)
877 # TODO linearization rules (currently added at the end in the order of the rules)
878 mmodule
.set_imported_mmodules
([sup
])
880 # Prepare to reapply the rules
881 check_conditional_importations
= true
886 # All the loaded modules
887 var nmodules
= new Array[AModule]
889 # Register the nmodule associated to each mmodule
891 # Public clients need to use `mmodule2node` to access stuff.
892 private var mmodule2nmodule
= new HashMap[MModule, AModule]
894 # Retrieve the associated AST node of a mmodule.
895 # This method is used to associate model entity with syntactic entities.
897 # If the module is not associated with a node, returns null.
898 fun mmodule2node
(mmodule
: MModule): nullable AModule
900 return mmodule2nmodule
.get_or_null
(mmodule
)
904 # File-system location of a module (file) that is identified but not always loaded.
906 # The name of the module
907 # (it's the basename of the filepath)
910 # The human path of the module
913 # The group (and the project) of the possible module
916 # The loaded module (if any)
917 var mmodule
: nullable MModule = null
919 redef fun to_s
do return filepath
923 # The associated `.ini` file, if any
925 # The `ini` file is given as is and might contain invalid or missing information.
927 # Some projects, like stand-alone projects or virtual projects have no `ini` file associated.
928 var ini
: nullable ConfigTree = null
932 # Modules paths associated with the group
933 var module_paths
= new Array[ModulePath]
935 # Is the group interesting for a final user?
937 # Groups are mandatory in the model but for simple projects they are not
938 # always interesting.
940 # A interesting group has, at least, one of the following true:
942 # * it has 2 modules or more
943 # * it has a subgroup
944 # * it has a documentation
945 fun is_interesting
: Bool
947 return module_paths
.length
> 1 or
948 mmodules
.length
> 1 or
949 not in_nesting
.direct_smallers
.is_empty
or
951 (mmodules
.length
== 1 and default_mmodule
== null)
954 # Are files and directories in self scanned?
956 # See `ModelBuilder::scan_group`.
959 # Return the modules in self and subgroups named `name`.
961 # If `self` is not scanned (see `ModelBuilder::scan_group`) the
962 # results might be partial.
963 fun mmodule_paths_by_name
(name
: String): Array[ModulePath]
965 var res
= new Array[ModulePath]
966 for g
in in_nesting
.smallers
do
967 for mp
in g
.module_paths
do
968 if mp
.name
== name
then
977 redef class SourceFile
978 # Associated mmodule, once created
979 var mmodule
: nullable MModule = null
982 redef class AStdImport
983 # The imported module once determined
984 var mmodule
: nullable MModule = null
988 # The associated MModule once build by a `ModelBuilder`
989 var mmodule
: nullable MModule
990 # Flag that indicate if the importation is already completed
991 var is_importation_done
: Bool = false