mixin: look for top-level methods in `Sys`.
[nit.git] / src / mixin.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 # Loading and additional module refinements at link-time.
16 #
17 # Used to factorize some code used by the engines.
18 module mixin
19
20 import modelbuilder
21
22 redef class ToolContext
23 # --mixin
24 var opt_mixins = new OptionArray("Additionals module to min-in", "-m", "--mixin")
25 # --define
26 var opt_defines = new OptionArray("Define a specific property", "-D", "--define")
27
28 redef init
29 do
30 super
31 option_context.add_option(opt_mixins, opt_defines)
32 end
33
34 redef fun make_main_module(mmodules)
35 do
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
40 end
41
42 var mainmodule = super
43
44 var defines = opt_defines.value
45 if not defines.is_empty then
46 var location = mainmodule.location
47 var model = mainmodule.model
48
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
53 end
54
55 var recv = mainmodule.sys_type
56 assert recv != null
57 var mclassdef = new MClassDef(mainmodule, recv, location)
58 mclassdef.add_in_hierarchy
59
60 for define in defines do
61 var spl = define.split_once_on('=')
62 var name = spl.first
63 var val = null
64 if spl.length > 1 then val = spl[1]
65 var prop = mainmodule.try_get_primitive_method(name, recv.mclass)
66 if prop == null then
67 error(null, "Error: --define: no top-level function `{name}`")
68 continue
69 end
70 var ret = prop.intro.msignature.return_mtype
71 var v
72 if ret == null then
73 error(null, "Error: --define: Method `{prop}` is not a function")
74 continue
75 else if ret.to_s == "Bool" then
76 if val == null or val == "true" then
77 v = true
78 else if val == "false" then
79 v = false
80 else
81 error(null, "Error: --define: Method `{prop}` need a Bool.")
82 continue
83 end
84 else if ret.to_s == "Int" then
85 if val != null and val.is_numeric then
86 v = val.to_i
87 else
88 error(null, "Error: --define: Method `{prop}` need a Int.")
89 continue
90 end
91 else if ret.to_s == "String" then
92 if val != null then
93 v = val
94 else
95 error(null, "Error: --define: Method `{prop}` need a String.")
96 continue
97 end
98 else
99 error(null, "Error: --define: Method `{prop}` return an unmanaged type {ret}.")
100 continue
101 end
102 var pd = new MMethodDef(mclassdef, prop, location)
103 pd.msignature = prop.intro.msignature
104 pd.constant_value = v
105 end
106 check_errors
107 end
108
109 return mainmodule
110 end
111 end