typing: add `ARangeExpr::init_callsite` and use it everywhere
[nit.git] / src / modelbuilder.nit
index 619edbf..b916077 100644 (file)
@@ -52,19 +52,39 @@ redef class ToolContext
        fun modelbuilder: ModelBuilder do return modelbuilder_real.as(not null)
        private var modelbuilder_real: nullable ModelBuilder = null
 
-       fun run_global_phases(mainmodule: MModule)
+       # Run `process_mainmodule` on all phases
+       fun run_global_phases(mmodules: Array[MModule])
        do
+               assert not mmodules.is_empty
+               var mainmodule
+               if mmodules.length == 1 then
+                       mainmodule = mmodules.first
+               else
+                       # We need a main module, so we build it by importing all modules
+                       mainmodule = new MModule(modelbuilder.model, null, "<main>", new Location(null, 0, 0, 0, 0))
+                       mainmodule.set_imported_mmodules(mmodules)
+               end
                for phase in phases_list do
-                       phase.process_mainmodule(mainmodule)
+                       phase.process_mainmodule(mainmodule, mmodules)
                end
        end
 end
 
 redef class Phase
-       # Specific action to execute on the whole program
-       # Called by the `ToolContext::run_global_phases`
+       # Specific action to execute on the whole program.
+       # Called by the `ToolContext::run_global_phases`.
+       #
+       # `mainmodule` is the main module of the program.
+       # It could be an implicit module (called "<main>").
+       #
+       # `given_modules` is the list of explicitely requested modules.
+       # from the command-line for instance.
+       #
+       # REQUIRE: `not given_modules.is_empty`
+       # REQUIRE: `(given_modules.length == 1) == (mainmodule == given_modules.first)`
+       #
        # @toimplement
-       fun process_mainmodule(mainmodule: MModule) do end
+       fun process_mainmodule(mainmodule: MModule, given_mmodules: SequenceRead[MModule]) do end
 end
 
 
@@ -395,6 +415,7 @@ class ModelBuilder
                end
 
                var res = new ModulePath(pn, path, mgroup)
+               mgroup.module_paths.add(res)
 
                identified_files[rp] = res
                return res
@@ -549,6 +570,11 @@ class ModelBuilder
                nmodules.add(nmodule)
                self.mmodule2nmodule[mmodule] = nmodule
 
+               if decl != null then
+                       var ndoc = decl.n_doc
+                       if ndoc != null then mmodule.mdoc = ndoc.to_mdoc
+               end
+
                return mmodule
        end
 
@@ -647,6 +673,10 @@ private class ModulePath
        redef fun to_s do return filepath
 end
 
+redef class MGroup
+       # modules paths associated with the group
+       private var module_paths = new Array[ModulePath]
+end
 
 redef class AStdImport
        # The imported module once determined
@@ -676,3 +706,30 @@ end
 redef class APrivateVisibility
        redef fun mvisibility do return private_visibility
 end
+
+redef class ADoc
+       private var mdoc_cache: nullable MDoc
+       fun to_mdoc: MDoc
+       do
+               var res = mdoc_cache
+               if res != null then return res
+               res = new MDoc
+               for c in n_comment do
+                       var text = c.text
+                       if text.length < 2 then
+                               res.content.add ""
+                               continue
+                       end
+                       assert text.chars[0] == '#'
+                       if text.chars[1] == ' ' then
+                               text = text.substring_from(2) # eat starting `#` and space
+                       else
+                               text = text.substring_from(1) # eat atarting `#` only
+                       end
+                       if text.chars.last == '\n' then text = text.substring(0, text.length-1) # drop \n
+                       res.content.add(text)
+               end
+               mdoc_cache = res
+               return res
+       end
+end