nitc: first_real_mmodule is now explicitly set.
[nit.git] / src / modelbuilder.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 module modelbuilder
18
19 import loader
20 import phase
21
22 private import more_collections
23
24 ###
25
26 redef class ToolContext
27 # Option --ignore-visibility
28 var opt_ignore_visibility = new OptionBool("Do not check, and produce errors, on visibility issues", "--ignore-visibility")
29
30 redef init
31 do
32 super
33 option_context.add_option(opt_ignore_visibility)
34 end
35
36 # Combine module to make a single one if required.
37 fun make_main_module(mmodules: Array[MModule]): MModule
38 do
39 assert not mmodules.is_empty
40 var mainmodule
41 if mmodules.length == 1 then
42 mainmodule = mmodules.first
43 else
44 # We need a main module, so we build it by importing all modules
45 mainmodule = new MModule(modelbuilder.model, null, mmodules.first.name + "-m", new Location(mmodules.first.location.file, 0, 0, 0, 0))
46 mainmodule.is_fictive = true
47 mainmodule.first_real_mmodule = mmodules.first.first_real_mmodule
48 mainmodule.set_imported_mmodules(mmodules)
49 modelbuilder.apply_conditional_importations(mainmodule)
50 modelbuilder.run_phases
51 end
52 return mainmodule
53 end
54
55 # Run `process_mainmodule` on all phases
56 fun run_global_phases(mmodules: Array[MModule])
57 do
58 if not mmodules.is_empty then
59 var mainmodule = make_main_module(mmodules)
60 for phase in phases_list do
61 if phase.disabled then continue
62 phase.process_mainmodule(mainmodule, mmodules)
63 end
64 end
65
66 check_errors
67 errors_info
68 end
69 end
70
71 redef class Phase
72 # Specific action to execute on the whole program.
73 # Called by the `ToolContext::run_global_phases`.
74 #
75 # `mainmodule` is the main module of the program.
76 # It could be an implicit module (called like the first given_mmodules).
77 #
78 # `given_modules` is the list of explicitely requested modules.
79 # from the command-line for instance.
80 #
81 # REQUIRE: `not given_modules.is_empty`
82 # REQUIRE: `(given_modules.length == 1) == (mainmodule == given_modules.first)`
83 #
84 # @toimplement
85 fun process_mainmodule(mainmodule: MModule, given_mmodules: SequenceRead[MModule]) do end
86 end
87
88
89 redef class ModelBuilder
90 # Run phases on all loaded modules
91 fun run_phases
92 do
93 var mmodules = parsed_modules.to_a
94 model.mmodule_importation_hierarchy.sort(mmodules)
95 var nmodules = new Array[AModule]
96 for mm in mmodules do
97 if mm.is_fictive then continue
98 nmodules.add(mmodule2node(mm).as(not null))
99 end
100 toolcontext.run_phases(nmodules)
101
102 if toolcontext.opt_only_metamodel.value then
103 self.toolcontext.info("*** ONLY METAMODEL", 1)
104 toolcontext.quit
105 end
106 end
107
108 end