Merge: Lazy semantize
[nit.git] / src / loader.nit
index b9e8055..14ec4fe 100644 (file)
@@ -14,7 +14,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-# Load nit source files and build the associated model
+# Loading of Nit source files
 module loader
 
 import modelbuilder_base
@@ -60,7 +60,7 @@ redef class ModelBuilder
        # The result is the corresponding model elements.
        # Errors and warnings are printed with the toolcontext.
        #
-       # Note: class and property model element are not analysed.
+       # Note: class and property model elements are not analysed.
        fun parse(modules: Sequence[String]): Array[MModule]
        do
                var time0 = get_time
@@ -89,15 +89,16 @@ redef class ModelBuilder
        end
 
        # The list of directories to search for top level modules
-       # The list is initially set with :
+       # The list is initially set with:
+       #
        #   * the toolcontext --path option
        #   * the NIT_PATH environment variable
        #   * `toolcontext.nit_dir`
        # Path can be added (or removed) by the client
        var paths = new Array[String]
 
-       # Like (an used by) `get_mmodule_by_name` but just return the ModulePath
-       private fun search_mmodule_by_name(anode: nullable ANode, mgroup: nullable MGroup, name: String): nullable ModulePath
+       # Like (and used by) `get_mmodule_by_name` but just return the ModulePath
+       fun search_mmodule_by_name(anode: nullable ANode, mgroup: nullable MGroup, name: String): nullable ModulePath
        do
                # First, look in groups
                var c = mgroup
@@ -128,7 +129,7 @@ redef class ModelBuilder
                # Look at some known directories
                var lookpaths = self.paths
 
-               # Look in the directory of the group project also (even if not explicitely in the path)
+               # Look in the directory of the group project also (even if not explicitly in the path)
                if mgroup != null then
                        # path of the root group
                        var dirname = mgroup.mproject.root.filepath
@@ -206,14 +207,18 @@ redef class ModelBuilder
                return identify_file(candidate)
        end
 
-       # cache for `identify_file` by realpath
-       private var identified_files = new HashMap[String, nullable ModulePath]
+       # Cache for `identify_file` by realpath
+       private var identified_files_by_path = new HashMap[String, nullable ModulePath]
+
+       # All the currently identified modules.
+       # See `identify_file`.
+       var identified_files = new Array[ModulePath]
 
        # Identify a source file
        # Load the associated project and groups if required
        #
        # Silently return `null` if `path` is not a valid module path.
-       private fun identify_file(path: String): nullable ModulePath
+       fun identify_file(path: String): nullable ModulePath
        do
                # special case for not a nit file
                if path.file_extension != "nit" then
@@ -223,7 +228,7 @@ redef class ModelBuilder
                                if res != null then return res
                        end
 
-                       # Found nothins? maybe it is a group...
+                       # Found nothing? maybe it is a group...
                        var candidate = null
                        if path.file_exists then
                                var mgroup = get_mgroup(path)
@@ -242,7 +247,7 @@ redef class ModelBuilder
                # Fast track, the path is already known
                var pn = path.basename(".nit")
                var rp = module_absolute_path(path)
-               if identified_files.has_key(rp) then return identified_files[rp]
+               if identified_files_by_path.has_key(rp) then return identified_files_by_path[rp]
 
                # Search for a group
                var mgrouppath = path.join_path("..").simplify_path
@@ -260,16 +265,17 @@ redef class ModelBuilder
                var res = new ModulePath(pn, path, mgroup)
                mgroup.module_paths.add(res)
 
-               identified_files[rp] = res
+               identified_files_by_path[rp] = res
+               identified_files.add(res)
                return res
        end
 
-       # groups by path
+       # Groups by path
        private var mgroups = new HashMap[String, nullable MGroup]
 
-       # return the mgroup associated to a directory path
-       # if the directory is not a group null is returned
-       private fun get_mgroup(dirpath: String): nullable MGroup
+       # Return the mgroup associated to a directory path.
+       # If the directory is not a group null is returned.
+       fun get_mgroup(dirpath: String): nullable MGroup
        do
                var rdp = module_absolute_path(dirpath)
                if mgroups.has_key(rdp) then
@@ -324,6 +330,17 @@ redef class ModelBuilder
                return mgroup
        end
 
+       # Force the identification of all ModulePath of the group and sub-groups.
+       fun visit_group(mgroup: MGroup) do
+               var p = mgroup.filepath
+               for f in p.files do
+                       var fp = p/f
+                       var g = get_mgroup(fp)
+                       if g != null then visit_group(g)
+                       identify_file(fp)
+               end
+       end
+
        # Transform relative paths (starting with '../') into absolute paths
        private fun module_absolute_path(path: String): String do
                return getcwd.join_path(path).simplify_path
@@ -363,7 +380,7 @@ redef class ModelBuilder
                return nmodule
        end
 
-       # Try to load a module modules using a path.
+       # Try to load a module using a path.
        # Display an error if there is a problem (IO / lexer / parser) and return null.
        # Note: usually, you do not need this method, use `get_mmodule_by_name` instead.
        #
@@ -400,7 +417,7 @@ redef class ModelBuilder
        end
 
        # Injection of a new module without source.
-       # Used by the interpreted
+       # Used by the interpreter.
        fun load_rt_module(parent: nullable MModule, nmodule: AModule, mod_name: String): nullable AModule
        do
                # Create the module
@@ -458,7 +475,7 @@ redef class ModelBuilder
                return mmodule
        end
 
-       # Analysis the module importation and fill the module_importation_hierarchy
+       # Analyze the module importation and fill the module_importation_hierarchy
        #
        # Unless you used `load_module`, the importation is already done and this method does a no-op.
        fun build_module_importation(nmodule: AModule)
@@ -538,8 +555,8 @@ redef class ModelBuilder
        var mmodule2nmodule = new HashMap[MModule, AModule]
 end
 
-# placeholder to a module file identified but not always loaded in a project
-private class ModulePath
+# File-system location of a module (file) that is identified but not always loaded.
+class ModulePath
        # The name of the module
        # (it's the basename of the filepath)
        var name: String
@@ -557,12 +574,12 @@ private class ModulePath
 end
 
 redef class MGroup
-       # modules paths associated with the group
-       private var module_paths = new Array[ModulePath]
+       # Modules paths associated with the group
+       var module_paths = new Array[ModulePath]
 
        # Is the group interesting for a final user?
        #
-       # groups are mandatory in the model but for simple projects they are not
+       # Groups are mandatory in the model but for simple projects they are not
        # always interesting.
        #
        # A interesting group has, at least, one of the following true: