modelbuilder: add `inject_module_subimportation` to inject importation.
authorJean Privat <jean@pryen.org>
Tue, 31 Jan 2017 19:35:11 +0000 (14:35 -0500)
committerJean Privat <jean@pryen.org>
Thu, 2 Feb 2017 18:51:15 +0000 (13:51 -0500)
Signed-off-by: Jean Privat <jean@pryen.org>

src/loader.nit
src/modelbuilder.nit

index deb895a..32749aa 100644 (file)
@@ -1045,7 +1045,7 @@ redef class ModelBuilder
        # (and `build_module_importation` that calls it).
        #
        # TODO (when the loader will be rewritten): use a better representation and move up rules in the model.
-       private var conditional_importations = new Array[SequenceRead[MModule]]
+       var conditional_importations = new Array[SequenceRead[MModule]]
 
        # Extends the current importations according to imported rules about conditional importation
        fun apply_conditional_importations(mmodule: MModule)
index a924c63..132c024 100644 (file)
@@ -101,4 +101,22 @@ redef class ModelBuilder
                end
        end
 
+       # Load module `filename` and add it as a conditional importation of `mmodule`.
+       #
+       # This means that current (and future) submodules of `module` will also import `filename`.
+       fun inject_module_subimportation(mmodule: MModule, filename: String)
+       do
+               var am = load_module(filename)
+               if am == null then return # forward error
+               var mm = am.mmodule
+               if mm == null then return # forward error
+               # Add the new module before the existing submodules in the hierarchy
+               for subm in mmodule.in_importation.direct_smallers do
+                       subm.set_imported_mmodules([mm])
+               end
+               # Register the new module as a conditional_importations for future submodules
+               conditional_importations.add([mm, mmodule])
+               # Register the new amodule to be processed by `run_phases`
+               toolcontext.todo_nmodules.unshift am
+       end
 end