Rename REAMDE to README.md
[nit.git] / src / modelbuilder_base.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 # Load nit source files and build the associated model
18 #
19 # FIXME better doc
20 #
21 # FIXME split this module into submodules
22 # FIXME add missing error checks
23 module modelbuilder_base
24
25 import model
26 import toolcontext
27 import parser
28
29 private import more_collections
30
31 ###
32
33 redef class ToolContext
34
35 # The modelbuilder 1-to-1 associated with the toolcontext
36 fun modelbuilder: ModelBuilder do return modelbuilder_real.as(not null)
37
38 private var modelbuilder_real: nullable ModelBuilder = null
39
40 end
41
42 # A model builder knows how to load nit source files and build the associated model
43 class ModelBuilder
44 # The model where new modules, classes and properties are added
45 var model: Model
46
47 # The toolcontext used to control the interaction with the user (getting options and displaying messages)
48 var toolcontext: ToolContext
49
50 # Instantiate a modelbuilder for a model and a toolcontext
51 # Important, the options of the toolcontext must be correctly set (parse_option already called)
52 init
53 do
54 assert toolcontext.modelbuilder_real == null
55 toolcontext.modelbuilder_real = self
56 end
57
58 # Return a class named `name` visible by the module `mmodule`.
59 # Visibility in modules is correctly handled.
60 # If no such a class exists, then null is returned.
61 # If more than one class exists, then an error on `anode` is displayed and null is returned.
62 # FIXME: add a way to handle class name conflict
63 fun try_get_mclass_by_name(anode: ANode, mmodule: MModule, name: String): nullable MClass
64 do
65 var classes = model.get_mclasses_by_name(name)
66 if classes == null then
67 return null
68 end
69
70 var res: nullable MClass = null
71 for mclass in classes do
72 if not mmodule.in_importation <= mclass.intro_mmodule then continue
73 if not mmodule.is_visible(mclass.intro_mmodule, mclass.visibility) then continue
74 if res == null then
75 res = mclass
76 else
77 error(anode, "Error: ambiguous class name `{name}`; conflict between `{mclass.full_name}` and `{res.full_name}`.")
78 return null
79 end
80 end
81 return res
82 end
83
84 # Like `try_get_mclass_by_name` but display an error message when the class is not found
85 fun get_mclass_by_name(node: ANode, mmodule: MModule, name: String): nullable MClass
86 do
87 var mclass = try_get_mclass_by_name(node, mmodule, name)
88 if mclass == null then
89 error(node, "Type Error: missing primitive class `{name}'.")
90 end
91 return mclass
92 end
93
94 # Return a property named `name` on the type `mtype` visible in the module `mmodule`.
95 # Visibility in modules is correctly handled.
96 # Protected properties are returned (it is up to the caller to check and reject protected properties).
97 # If no such a property exists, then null is returned.
98 # If more than one property exists, then an error on `anode` is displayed and null is returned.
99 # FIXME: add a way to handle property name conflict
100 fun try_get_mproperty_by_name2(anode: ANode, mmodule: MModule, mtype: MType, name: String): nullable MProperty
101 do
102 var props = self.model.get_mproperties_by_name(name)
103 if props == null then
104 return null
105 end
106
107 var cache = self.try_get_mproperty_by_name2_cache[mmodule, mtype, name]
108 if cache != null then return cache
109
110 var res: nullable MProperty = null
111 var ress: nullable Array[MProperty] = null
112 for mprop in props do
113 if not mtype.has_mproperty(mmodule, mprop) then continue
114 if not mmodule.is_visible(mprop.intro_mclassdef.mmodule, mprop.visibility) then continue
115
116 # new-factories are invisible outside of the class
117 if mprop isa MMethod and mprop.is_new and (not mtype isa MClassType or mprop.intro_mclassdef.mclass != mtype.mclass) then
118 continue
119 end
120
121 if res == null then
122 res = mprop
123 continue
124 end
125
126 # Two global properties?
127 # First, special case for init, keep the most specific ones
128 if res isa MMethod and mprop isa MMethod and res.is_init and mprop.is_init then
129 var restype = res.intro_mclassdef.bound_mtype
130 var mproptype = mprop.intro_mclassdef.bound_mtype
131 if mproptype.is_subtype(mmodule, null, restype) then
132 # found a most specific constructor, so keep it
133 res = mprop
134 continue
135 end
136 end
137
138 # Ok, just keep all prop in the ress table
139 if ress == null then
140 ress = new Array[MProperty]
141 ress.add(res)
142 end
143 ress.add(mprop)
144 end
145
146 # There is conflict?
147 if ress != null and res isa MMethod and res.is_init then
148 # special case forinit again
149 var restype = res.intro_mclassdef.bound_mtype
150 var ress2 = new Array[MProperty]
151 for mprop in ress do
152 var mproptype = mprop.intro_mclassdef.bound_mtype
153 if not restype.is_subtype(mmodule, null, mproptype) then
154 ress2.add(mprop)
155 else if not mprop isa MMethod or not mprop.is_init then
156 ress2.add(mprop)
157 end
158 end
159 if ress2.is_empty then
160 ress = null
161 else
162 ress = ress2
163 ress.add(res)
164 end
165 end
166
167 if ress != null then
168 assert ress.length > 1
169 var s = new Array[String]
170 for mprop in ress do s.add mprop.full_name
171 self.error(anode, "Error: ambiguous property name `{name}` for `{mtype}`; conflict between {s.join(" and ")}.")
172 end
173
174 self.try_get_mproperty_by_name2_cache[mmodule, mtype, name] = res
175 return res
176 end
177
178 private var try_get_mproperty_by_name2_cache = new HashMap3[MModule, MType, String, nullable MProperty]
179
180
181 # Alias for try_get_mproperty_by_name2(anode, mclassdef.mmodule, mclassdef.mtype, name)
182 fun try_get_mproperty_by_name(anode: ANode, mclassdef: MClassDef, name: String): nullable MProperty
183 do
184 return try_get_mproperty_by_name2(anode, mclassdef.mmodule, mclassdef.bound_mtype, name)
185 end
186
187 # Helper function to display an error on a node.
188 # Alias for `self.toolcontext.error(n.hot_location, text)`
189 fun error(n: nullable ANode, text: String)
190 do
191 var l = null
192 if n != null then l = n.hot_location
193 self.toolcontext.error(l, text)
194 end
195
196 # Helper function to display a warning on a node.
197 # Alias for: `self.toolcontext.warning(n.hot_location, text)`
198 fun warning(n: nullable ANode, tag, text: String)
199 do
200 var l = null
201 if n != null then l = n.hot_location
202 self.toolcontext.warning(l, tag, text)
203 end
204
205 # Helper function to display an advice on a node.
206 # Alias for: `self.toolcontext.advice(n.hot_location, text)`
207 fun advice(n: nullable ANode, tag, text: String)
208 do
209 var l = null
210 if n != null then l = n.hot_location
211 self.toolcontext.advice(l, tag, text)
212 end
213
214 # Force to get the primitive method named `name` on the type `recv` or do a fatal error on `n`
215 fun force_get_primitive_method(n: nullable ANode, name: String, recv: MClass, mmodule: MModule): MMethod
216 do
217 var res = mmodule.try_get_primitive_method(name, recv)
218 if res == null then
219 var l = null
220 if n != null then l = n.hot_location
221 self.toolcontext.fatal_error(l, "Fatal Error: `{recv}` must have a property named `{name}`.")
222 abort
223 end
224 return res
225 end
226
227 # Return the static type associated to the node `ntype`.
228 # `mmodule` and `mclassdef` is the context where the call is made (used to understand formal types)
229 # In case of problem, an error is displayed on `ntype` and null is returned.
230 # FIXME: the name "resolve_mtype" is awful
231 fun resolve_mtype_unchecked(mmodule: MModule, mclassdef: nullable MClassDef, ntype: AType, with_virtual: Bool): nullable MType
232 do
233 var name = ntype.n_id.text
234 var res: MType
235
236 # Check virtual type
237 if mclassdef != null and with_virtual then
238 var prop = try_get_mproperty_by_name(ntype, mclassdef, name).as(nullable MVirtualTypeProp)
239 if prop != null then
240 if not ntype.n_types.is_empty then
241 error(ntype, "Type Error: formal type `{name}` cannot have formal parameters.")
242 end
243 res = prop.mvirtualtype
244 if ntype.n_kwnullable != null then res = res.as_nullable
245 ntype.mtype = res
246 return res
247 end
248 end
249
250 # Check parameter type
251 if mclassdef != null then
252 for p in mclassdef.mclass.mparameters do
253 if p.name != name then continue
254
255 if not ntype.n_types.is_empty then
256 error(ntype, "Type Error: formal type `{name}` cannot have formal parameters.")
257 end
258
259 res = p
260 if ntype.n_kwnullable != null then res = res.as_nullable
261 ntype.mtype = res
262 return res
263 end
264 end
265
266 # Check class
267 var mclass = try_get_mclass_by_name(ntype, mmodule, name)
268 if mclass != null then
269 var arity = ntype.n_types.length
270 if arity != mclass.arity then
271 if arity == 0 then
272 error(ntype, "Type Error: `{mclass.signature_to_s}` is a generic class.")
273 else if mclass.arity == 0 then
274 error(ntype, "Type Error: `{name}` is not a generic class.")
275 else
276 error(ntype, "Type Error: expected {mclass.arity} formal argument(s) for `{mclass.signature_to_s}`; got {arity}.")
277 end
278 return null
279 end
280 if arity == 0 then
281 res = mclass.mclass_type
282 if ntype.n_kwnullable != null then res = res.as_nullable
283 ntype.mtype = res
284 return res
285 else
286 var mtypes = new Array[MType]
287 for nt in ntype.n_types do
288 var mt = resolve_mtype_unchecked(mmodule, mclassdef, nt, with_virtual)
289 if mt == null then return null # Forward error
290 mtypes.add(mt)
291 end
292 res = mclass.get_mtype(mtypes)
293 if ntype.n_kwnullable != null then res = res.as_nullable
294 ntype.mtype = res
295 return res
296 end
297 end
298
299 # If everything fail, then give up :(
300 error(ntype, "Error: class `{name}` not found in module `{mmodule}`.")
301 return null
302 end
303
304 # Return the static type associated to the node `ntype`.
305 # `mmodule` and `mclassdef` is the context where the call is made (used to understand formal types)
306 # In case of problem, an error is displayed on `ntype` and null is returned.
307 # FIXME: the name "resolve_mtype" is awful
308 fun resolve_mtype(mmodule: MModule, mclassdef: nullable MClassDef, ntype: AType): nullable MType
309 do
310 var mtype = ntype.mtype
311 if mtype == null then mtype = resolve_mtype_unchecked(mmodule, mclassdef, ntype, true)
312 if mtype == null then return null # Forward error
313
314 if ntype.checked_mtype then return mtype
315 if mtype isa MGenericType then
316 var mclass = mtype.mclass
317 for i in [0..mclass.arity[ do
318 var intro = mclass.try_intro
319 if intro == null then return null # skip error
320 var bound = intro.bound_mtype.arguments[i]
321 var nt = ntype.n_types[i]
322 var mt = resolve_mtype(mmodule, mclassdef, nt)
323 if mt == null then return null # forward error
324 var anchor
325 if mclassdef != null then anchor = mclassdef.bound_mtype else anchor = null
326 if not check_subtype(nt, mmodule, anchor, mt, bound) then
327 error(nt, "Type Error: expected `{bound}`, got `{mt}`.")
328 return null
329 end
330 end
331 end
332 ntype.checked_mtype = true
333 return mtype
334 end
335
336 # Check that `sub` is a subtype of `sup`.
337 # Do not display an error message.
338 #
339 # This method is used a an entry point for the modelize phase to test static subtypes.
340 # Some refinements could redefine it to collect statictics.
341 fun check_subtype(node: ANode, mmodule: MModule, anchor: nullable MClassType, sub, sup: MType): Bool
342 do
343 return sub.is_subtype(mmodule, anchor, sup)
344 end
345
346 # Check that `sub` and `sup` are equvalent types.
347 # Do not display an error message.
348 #
349 # This method is used a an entry point for the modelize phase to test static equivalent types.
350 # Some refinements could redefine it to collect statictics.
351 fun check_sametype(node: ANode, mmodule: MModule, anchor: nullable MClassType, sub, sup: MType): Bool
352 do
353 return sub.is_subtype(mmodule, anchor, sup) and sup.is_subtype(mmodule, anchor, sub)
354 end
355 end
356
357 redef class AType
358 # The mtype associated to the node
359 var mtype: nullable MType = null
360
361 # Is the mtype a valid one?
362 var checked_mtype: Bool = false
363 end
364
365 redef class AVisibility
366 # The visibility level associated with the AST node class
367 fun mvisibility: MVisibility is abstract
368 end
369 redef class AIntrudeVisibility
370 redef fun mvisibility do return intrude_visibility
371 end
372 redef class APublicVisibility
373 redef fun mvisibility do return public_visibility
374 end
375 redef class AProtectedVisibility
376 redef fun mvisibility do return protected_visibility
377 end
378 redef class APrivateVisibility
379 redef fun mvisibility do return private_visibility
380 end
381
382 redef class ADoc
383 private var mdoc_cache: nullable MDoc
384
385 # Convert `self` to a `MDoc`
386 fun to_mdoc: MDoc
387 do
388 var res = mdoc_cache
389 if res != null then return res
390 res = new MDoc(location)
391 for c in n_comment do
392 var text = c.text
393 if text.length < 2 then
394 res.content.add ""
395 continue
396 end
397 assert text.chars[0] == '#'
398 if text.chars[1] == ' ' then
399 text = text.substring_from(2) # eat starting `#` and space
400 else
401 text = text.substring_from(1) # eat atarting `#` only
402 end
403 if text.chars.last == '\n' then text = text.substring(0, text.length-1) # drop \n
404 res.content.add(text)
405 end
406 mdoc_cache = res
407 return res
408 end
409 end