tools: use nitg to compile most tools
[nit.git] / src / modelize_class.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 #Analysis and verification of class definitions to instantiate model element
18
19 module modelize_class
20
21 import modelbuilder
22
23 redef class ToolContext
24 var modelize_class_phase: Phase = new ModelizeClassPhase(self, null)
25 end
26
27 private class ModelizeClassPhase
28 super Phase
29
30 redef fun process_nmodule(nmodule)
31 do
32 toolcontext.modelbuilder.build_classes(nmodule)
33 end
34 end
35
36 redef class ModelBuilder
37 # Visit the AST and create the MClass objects
38 private fun build_a_mclass(nmodule: AModule, nclassdef: AClassdef)
39 do
40 var mmodule = nmodule.mmodule.as(not null)
41
42 var name: String
43 var nkind: nullable AClasskind
44 var mkind: MClassKind
45 var nvisibility: nullable AVisibility
46 var mvisibility: nullable MVisibility
47 var arity = 0
48 if nclassdef isa AStdClassdef then
49 name = nclassdef.n_id.text
50 nkind = nclassdef.n_classkind
51 mkind = nkind.mkind
52 nvisibility = nclassdef.n_visibility
53 mvisibility = nvisibility.mvisibility
54 arity = nclassdef.n_formaldefs.length
55 else if nclassdef isa ATopClassdef then
56 name = "Object"
57 nkind = null
58 mkind = interface_kind
59 nvisibility = null
60 mvisibility = public_visibility
61 else if nclassdef isa AMainClassdef then
62 name = "Sys"
63 nkind = null
64 mkind = concrete_kind
65 nvisibility = null
66 mvisibility = public_visibility
67 else
68 abort
69 end
70
71 var mclass = try_get_mclass_by_name(nclassdef, mmodule, name)
72 if mclass == null then
73 mclass = new MClass(mmodule, name, arity, mkind, mvisibility)
74 #print "new class {mclass}"
75 else if nclassdef isa AStdClassdef and nmodule.mclass2nclassdef.has_key(mclass) then
76 error(nclassdef, "Error: A class {name} is already defined at line {nmodule.mclass2nclassdef[mclass].location.line_start}.")
77 return
78 else if nclassdef isa AStdClassdef and nclassdef.n_kwredef == null then
79 error(nclassdef, "Redef error: {name} is an imported class. Add the redef keyword to refine it.")
80 return
81 else if mclass.arity != arity then
82 error(nclassdef, "Redef error: Formal parameter arity missmatch; got {arity}, expected {mclass.arity}.")
83 return
84 else if nkind != null and mkind != concrete_kind and mclass.kind != mkind then
85 error(nkind, "Error: refinement changed the kind from a {mclass.kind} to a {mkind}")
86 else if nvisibility != null and mvisibility != public_visibility and mclass.visibility != mvisibility then
87 error(nvisibility, "Error: refinement changed the visibility from a {mclass.visibility} to a {mvisibility}")
88 end
89 nclassdef.mclass = mclass
90 nmodule.mclass2nclassdef[mclass] = nclassdef
91 end
92
93 # Visit the AST and create the MClassDef objects
94 private fun build_a_mclassdef(nmodule: AModule, nclassdef: AClassdef)
95 do
96 var mmodule = nmodule.mmodule.as(not null)
97 var objectclass = try_get_mclass_by_name(nmodule, mmodule, "Object")
98 var mclass = nclassdef.mclass
99 if mclass == null then return # Skip error
100 #var mclassdef = nclassdef.mclassdef.as(not null)
101
102 var names = new Array[String]
103 var bounds = new Array[MType]
104 if nclassdef isa AStdClassdef and mclass.arity > 0 then
105 # Collect formal parameter names
106 for i in [0..mclass.arity[ do
107 var nfd = nclassdef.n_formaldefs[i]
108 var ptname = nfd.n_id.text
109 if names.has(ptname) then
110 error(nfd, "Error: A formal parameter type `{ptname}' already exists")
111 return
112 end
113 names.add(ptname)
114 end
115
116 # Revolve bound for formal parameter names
117 for i in [0..mclass.arity[ do
118 var nfd = nclassdef.n_formaldefs[i]
119 var nfdt = nfd.n_type
120 if nfdt != null then
121 var bound = resolve_mtype_unchecked(nclassdef, nfdt, false)
122 if bound == null then return # Forward error
123 if bound.need_anchor then
124 # No F-bounds!
125 error(nfd, "Error: Formal parameter type `{names[i]}' bounded with a formal parameter type")
126 else
127 bounds.add(bound)
128 end
129 else if mclass.mclassdefs.is_empty then
130 # No bound, then implicitely bound by nullable Object
131 bounds.add(objectclass.mclass_type.as_nullable)
132 else
133 # Inherit the bound
134 bounds.add(mclass.intro.bound_mtype.arguments[i])
135 end
136 end
137 end
138
139 var bound_mtype = mclass.get_mtype(bounds)
140 var mclassdef = new MClassDef(mmodule, bound_mtype, nclassdef.location, names)
141 nclassdef.mclassdef = mclassdef
142 self.mclassdef2nclassdef[mclassdef] = nclassdef
143
144 if mclassdef.is_intro then
145 self.toolcontext.info("{mclassdef} introduces new {mclass.kind} {mclass.full_name}", 3)
146 else
147 self.toolcontext.info("{mclassdef} refine {mclass.kind} {mclass.full_name}", 3)
148 end
149 end
150
151 # Visit the AST and set the super-types of the MClassdef objects
152 private fun collect_a_mclassdef_inheritance(nmodule: AModule, nclassdef: AClassdef)
153 do
154 var mmodule = nmodule.mmodule.as(not null)
155 var objectclass = try_get_mclass_by_name(nmodule, mmodule, "Object")
156 var mclass = nclassdef.mclass.as(not null)
157 var mclassdef = nclassdef.mclassdef.as(not null)
158
159 var specobject = true
160 var supertypes = new Array[MClassType]
161 if nclassdef isa AStdClassdef then
162 for nsc in nclassdef.n_superclasses do
163 specobject = false
164 var ntype = nsc.n_type
165 var mtype = resolve_mtype_unchecked(nclassdef, ntype, false)
166 if mtype == null then continue # Skip because of error
167 if not mtype isa MClassType then
168 error(ntype, "Error: supertypes cannot be a formal type")
169 return
170 end
171 supertypes.add mtype
172 #print "new super : {mclass} < {mtype}"
173 end
174 end
175 if specobject and mclass.name != "Object" and objectclass != null and mclassdef.is_intro then
176 supertypes.add objectclass.mclass_type
177 end
178
179 mclassdef.set_supertypes(supertypes)
180 if not supertypes.is_empty then self.toolcontext.info("{mclassdef} new super-types: {supertypes.join(", ")}", 3)
181 end
182
183 # Check the validity of the specialization heirarchy
184 private fun check_supertypes(nmodule: AModule, nclassdef: AClassdef)
185 do
186 var mmodule = nmodule.mmodule.as(not null)
187 var objectclass = try_get_mclass_by_name(nmodule, mmodule, "Object")
188 var mclass = nclassdef.mclass.as(not null)
189 var mclassdef = nclassdef.mclassdef.as(not null)
190
191 for s in mclassdef.supertypes do
192 if s.is_subtype(mmodule, mclassdef.bound_mtype, mclassdef.bound_mtype) then
193 error(nclassdef, "Error: Inheritance loop for class {mclass} with type {s}")
194 end
195 end
196 end
197
198 # Build the classes of the module `nmodule'.
199 # REQUIRE: classes of imported modules are already build. (let `phase' do the job)
200 private fun build_classes(nmodule: AModule)
201 do
202 # Force building recursively
203 if nmodule.build_classes_is_done then return
204 nmodule.build_classes_is_done = true
205 var mmodule = nmodule.mmodule.as(not null)
206 for imp in mmodule.in_importation.direct_greaters do
207
208 build_classes(mmodule2nmodule[imp])
209 end
210
211 # Create all classes
212 for nclassdef in nmodule.n_classdefs do
213 self.build_a_mclass(nmodule, nclassdef)
214 end
215
216 # Create all classdefs
217 for nclassdef in nmodule.n_classdefs do
218 self.build_a_mclassdef(nmodule, nclassdef)
219 end
220
221 for nclassdef in nmodule.n_classdefs do
222 if nclassdef.mclassdef == null then return # forward error
223 end
224
225 # Create inheritance on all classdefs
226 for nclassdef in nmodule.n_classdefs do
227 self.collect_a_mclassdef_inheritance(nmodule, nclassdef)
228 end
229
230 # Create the mclassdef hierarchy
231 for nclassdef in nmodule.n_classdefs do
232 var mclassdef = nclassdef.mclassdef.as(not null)
233 mclassdef.add_in_hierarchy
234 end
235
236 # Check inheritance
237 for nclassdef in nmodule.n_classdefs do
238 self.check_supertypes(nmodule, nclassdef)
239 end
240
241 # Check unchecked ntypes
242 for nclassdef in nmodule.n_classdefs do
243 if nclassdef isa AStdClassdef then
244 # check bound of formal parameter
245 for nfd in nclassdef.n_formaldefs do
246 var nfdt = nfd.n_type
247 if nfdt != null and nfdt.mtype != null then
248 var bound = resolve_mtype(nclassdef, nfdt)
249 if bound == null then return # Forward error
250 end
251 end
252 # check declared super types
253 for nsc in nclassdef.n_superclasses do
254 var ntype = nsc.n_type
255 if ntype.mtype != null then
256 var mtype = resolve_mtype(nclassdef, ntype)
257 if mtype == null then return # Forward error
258 end
259 end
260 end
261
262 end
263
264 # TODO: Check that the super-class is not intrusive
265
266 # TODO: Check that the super-class is not already known (by transitivity)
267 end
268
269 # Register the nclassdef associated to each mclassdef
270 # FIXME: why not refine the MClassDef class with a nullable attribute?
271 var mclassdef2nclassdef: HashMap[MClassDef, AClassdef] = new HashMap[MClassDef, AClassdef]
272
273 # Return the static type associated to the node `ntype'.
274 # `classdef' is the context where the call is made (used to understand formal types)
275 # The mmodule used as context is `nclassdef.mmodule'
276 # In case of problem, an error is displayed on `ntype' and null is returned.
277 # FIXME: the name "resolve_mtype" is awful
278 fun resolve_mtype_unchecked(nclassdef: AClassdef, ntype: AType, with_virtual: Bool): nullable MType
279 do
280 var name = ntype.n_id.text
281 var mclassdef = nclassdef.mclassdef
282 var mmodule = nclassdef.parent.as(AModule).mmodule.as(not null)
283 var res: MType
284
285 # Check virtual type
286 if mclassdef != null and with_virtual then
287 var prop = try_get_mproperty_by_name(ntype, mclassdef, name).as(nullable MVirtualTypeProp)
288 if prop != null then
289 if not ntype.n_types.is_empty then
290 error(ntype, "Type error: formal type {name} cannot have formal parameters.")
291 end
292 res = prop.mvirtualtype
293 if ntype.n_kwnullable != null then res = res.as_nullable
294 ntype.mtype = res
295 return res
296 end
297 end
298
299 # Check parameter type
300 if mclassdef != null and mclassdef.parameter_names.has(name) then
301 if not ntype.n_types.is_empty then
302 error(ntype, "Type error: formal type {name} cannot have formal parameters.")
303 end
304 for i in [0..mclassdef.parameter_names.length[ do
305 if mclassdef.parameter_names[i] == name then
306 res = mclassdef.mclass.mclass_type.arguments[i]
307 if ntype.n_kwnullable != null then res = res.as_nullable
308 ntype.mtype = res
309 return res
310 end
311 end
312 abort
313 end
314
315 # Check class
316 var mclass = try_get_mclass_by_name(ntype, mmodule, name)
317 if mclass != null then
318 var arity = ntype.n_types.length
319 if arity != mclass.arity then
320 if arity == 0 then
321 error(ntype, "Type error: '{name}' is a generic class.")
322 else if mclass.arity == 0 then
323 error(ntype, "Type error: '{name}' is not a generic class.")
324 else
325 error(ntype, "Type error: '{name}' has {mclass.arity} parameters ({arity} are provided).")
326 end
327 return null
328 end
329 if arity == 0 then
330 res = mclass.mclass_type
331 if ntype.n_kwnullable != null then res = res.as_nullable
332 ntype.mtype = res
333 return res
334 else
335 var mtypes = new Array[MType]
336 for nt in ntype.n_types do
337 var mt = resolve_mtype_unchecked(nclassdef, nt, with_virtual)
338 if mt == null then return null # Forward error
339 mtypes.add(mt)
340 end
341 res = mclass.get_mtype(mtypes)
342 if ntype.n_kwnullable != null then res = res.as_nullable
343 ntype.mtype = res
344 return res
345 end
346 end
347
348 # If everything fail, then give up :(
349 error(ntype, "Type error: class {name} not found in module {mmodule}.")
350 return null
351 end
352
353 # Return the static type associated to the node `ntype'.
354 # `classdef' is the context where the call is made (used to understand formal types)
355 # The mmodule used as context is `nclassdef.mmodule'
356 # In case of problem, an error is displayed on `ntype' and null is returned.
357 # FIXME: the name "resolve_mtype" is awful
358 fun resolve_mtype(nclassdef: AClassdef, ntype: AType): nullable MType
359 do
360 var mtype = ntype.mtype
361 if mtype == null then mtype = resolve_mtype_unchecked(nclassdef, ntype, true)
362 if mtype == null then return null # Forward error
363
364 if ntype.checked_mtype then return mtype
365 if mtype isa MGenericType then
366 var mmodule = nclassdef.parent.as(AModule).mmodule.as(not null)
367 var mclassdef = nclassdef.mclassdef
368 var mclass = mtype.mclass
369 for i in [0..mclass.arity[ do
370 var bound = mclass.intro.bound_mtype.arguments[i]
371 var nt = ntype.n_types[i]
372 var mt = resolve_mtype(nclassdef, nt)
373 if mt == null then return null # forward error
374 if not mt.is_subtype(mmodule, mclassdef.bound_mtype, bound) then
375 error(nt, "Type error: expected {bound}, got {mt}")
376 return null
377 end
378 end
379 end
380 ntype.checked_mtype = true
381 return mtype
382 end
383
384 end
385
386 redef class AModule
387 # Flag that indicate if the class building is already completed
388 var build_classes_is_done: Bool = false
389 # What is the AClassdef associated to a MClass?
390 # Used to check multiple definition of a class.
391 var mclass2nclassdef: Map[MClass, AClassdef] = new HashMap[MClass, AClassdef]
392 end
393
394 redef class AClassdef
395 # The associated MClass once build by a `ModelBuilder'
396 var mclass: nullable MClass
397 # The associated MClassDef once build by a `ModelBuilder'
398 var mclassdef: nullable MClassDef
399 end
400
401 redef class AClasskind
402 # The class kind associated with the AST node class
403 private fun mkind: MClassKind is abstract
404 end
405 redef class AConcreteClasskind
406 redef fun mkind do return concrete_kind
407 end
408 redef class AAbstractClasskind
409 redef fun mkind do return abstract_kind
410 end
411 redef class AInterfaceClasskind
412 redef fun mkind do return interface_kind
413 end
414 redef class AEnumClasskind
415 redef fun mkind do return enum_kind
416 end
417 redef class AExternClasskind
418 redef fun mkind do return extern_kind
419 end
420
421 redef class AType
422 # The mtype associated to the node
423 var mtype: nullable MType = null
424
425 # Is the mtype a valid one?
426 var checked_mtype: Bool = false
427 end