1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Loading and additional module refinements at link-time.
17 # Used to factorize some code used by the engines.
22 redef class ToolContext
24 var opt_mixins
= new OptionArray("Additionals module to min-in", "-m", "--mixin")
26 var opt_defines
= new OptionArray("Define a specific property", "-D", "--define")
31 option_context
.add_option
(opt_mixins
, opt_defines
)
34 redef fun make_main_module
(mmodules
)
36 var mixins
= opt_mixins
.value
37 if not mixins
.is_empty
then
38 mmodules
.add_all modelbuilder
.parse
(opt_mixins
.value
)
39 modelbuilder
.run_phases
42 var mainmodule
= super
44 var defines
= opt_defines
.value
45 if not defines
.is_empty
then
46 var location
= mainmodule
.location
47 var model
= mainmodule
.model
49 if mainmodule
== mmodules
.first
then
50 mainmodule
= new MModule(model
, null, mainmodule
.name
+ "-d", location
)
51 mainmodule
.set_imported_mmodules
(mmodules
)
52 mainmodule
.is_fictive
= true
55 var recv
= mainmodule
.sys_type
57 var mclassdef
= new MClassDef(mainmodule
, recv
, location
)
58 mclassdef
.add_in_hierarchy
60 for define
in defines
do
61 var spl
= define
.split_once_on
('=')
64 if spl
.length
> 1 then val
= spl
[1]
65 var prop
= mainmodule
.try_get_primitive_method
(name
, recv
.mclass
)
67 error
(null, "Error: --define: no top-level function `{name}`.")
70 var ret
= prop
.intro
.msignature
.return_mtype
73 error
(null, "Error: --define: method `{prop}` is not a function.")
75 else if ret
.to_s
== "Bool" then
76 if val
== null or val
== "true" then
78 else if val
== "false" then
81 error
(null, "Error: --define: method `{prop}` needs a Bool.")
84 else if ret
.to_s
== "Int" then
85 if val
!= null and val
.is_numeric
then
88 error
(null, "Error: --define: method `{prop}` needs a Int.")
91 else if ret
.to_s
== "String" then
95 error
(null, "Error: --define: method `{prop}` needs a String.")
99 error
(null, "Error: --define: method `{prop}` returns an unsupported type `{ret}`.")
102 var pd
= new MMethodDef(mclassdef
, prop
, location
)
103 pd
.msignature
= prop
.intro
.msignature
104 pd
.constant_value
= v