android: exit application on destroy request
[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 module modelize_class
19
20 import modelbuilder
21
22 redef class ToolContext
23 var modelize_class_phase: Phase = new ModelizeClassPhase(self, null)
24 end
25
26 private class ModelizeClassPhase
27 super Phase
28
29 redef fun process_nmodule(nmodule)
30 do
31 toolcontext.modelbuilder.build_classes(nmodule)
32 end
33 end
34
35 redef class ModelBuilder
36 # Visit the AST and create the `MClass` objects
37 private fun build_a_mclass(nmodule: AModule, nclassdef: AClassdef)
38 do
39 var mmodule = nmodule.mmodule.as(not null)
40
41 var name: String
42 var nkind: nullable AClasskind
43 var mkind: MClassKind
44 var nvisibility: nullable AVisibility
45 var mvisibility: nullable MVisibility
46 var arity = 0
47 if nclassdef isa AStdClassdef then
48 name = nclassdef.n_id.text
49 nkind = nclassdef.n_classkind
50 mkind = nkind.mkind
51 nvisibility = nclassdef.n_visibility
52 mvisibility = nvisibility.mvisibility
53 arity = nclassdef.n_formaldefs.length
54 if mvisibility == protected_visibility then
55 error(nvisibility, "Error: only properties can be protected.")
56 return
57 else if mvisibility == intrude_visibility then
58 error(nvisibility, "Error: intrude is not a legal visibility for classes.")
59 return
60 end
61 else if nclassdef isa ATopClassdef then
62 name = "Object"
63 nkind = null
64 mkind = interface_kind
65 nvisibility = null
66 mvisibility = public_visibility
67 else if nclassdef isa AMainClassdef then
68 name = "Sys"
69 nkind = null
70 mkind = concrete_kind
71 nvisibility = null
72 mvisibility = public_visibility
73 else
74 abort
75 end
76
77 var mclass = try_get_mclass_by_name(nclassdef, mmodule, name)
78 if mclass == null then
79 if nclassdef isa AStdClassdef and nclassdef.n_kwredef != null then
80 error(nclassdef, "Redef error: No imported class {name} to refine.")
81 return
82 end
83 mclass = new MClass(mmodule, name, arity, mkind, mvisibility)
84 #print "new class {mclass}"
85 else if nclassdef isa AStdClassdef and nmodule.mclass2nclassdef.has_key(mclass) then
86 error(nclassdef, "Error: A class {name} is already defined at line {nmodule.mclass2nclassdef[mclass].location.line_start}.")
87 return
88 else if nclassdef isa AStdClassdef and nclassdef.n_kwredef == null then
89 error(nclassdef, "Redef error: {name} is an imported class. Add the redef keyword to refine it.")
90 return
91 else if mclass.arity != arity then
92 error(nclassdef, "Redef error: Formal parameter arity missmatch; got {arity}, expected {mclass.arity}.")
93 return
94 else if nkind != null and mkind != concrete_kind and mclass.kind != mkind then
95 error(nkind, "Error: refinement changed the kind from a {mclass.kind} to a {mkind}")
96 else if nvisibility != null and mvisibility != public_visibility and mclass.visibility != mvisibility then
97 error(nvisibility, "Error: refinement changed the visibility from a {mclass.visibility} to a {mvisibility}")
98 end
99 nclassdef.mclass = mclass
100 nmodule.mclass2nclassdef[mclass] = nclassdef
101 end
102
103 # Visit the AST and create the `MClassDef` objects
104 private fun build_a_mclassdef(nmodule: AModule, nclassdef: AClassdef)
105 do
106 var mmodule = nmodule.mmodule.as(not null)
107 var objectclass = try_get_mclass_by_name(nmodule, mmodule, "Object")
108 var mclass = nclassdef.mclass
109 if mclass == null then return # Skip error
110 #var mclassdef = nclassdef.mclassdef.as(not null)
111
112 var names = new Array[String]
113 var bounds = new Array[MType]
114 if nclassdef isa AStdClassdef and mclass.arity > 0 then
115 # Collect formal parameter names
116 for i in [0..mclass.arity[ do
117 var nfd = nclassdef.n_formaldefs[i]
118 var ptname = nfd.n_id.text
119 if names.has(ptname) then
120 error(nfd, "Error: A formal parameter type `{ptname}' already exists")
121 return
122 end
123 for c in ptname.chars do if c >= 'a' and c<= 'z' then
124 warning(nfd, "Warning: lowercase in the formal parameter type {ptname}")
125 break
126 end
127 names.add(ptname)
128 nfd.mtype = mclass.mclass_type.arguments[i].as(MParameterType)
129 end
130
131 # Revolve bound for formal parameter names
132 for i in [0..mclass.arity[ do
133 var nfd = nclassdef.n_formaldefs[i]
134 var nfdt = nfd.n_type
135 if nfdt != null then
136 var bound = resolve_mtype_unchecked(nclassdef, nfdt, false)
137 if bound == null then return # Forward error
138 if bound.need_anchor then
139 # No F-bounds!
140 error(nfd, "Error: Formal parameter type `{names[i]}' bounded with a formal parameter type")
141 else
142 bounds.add(bound)
143 nfd.bound = bound
144 end
145 else if mclass.mclassdefs.is_empty then
146 # No bound, then implicitely bound by nullable Object
147 var bound = objectclass.mclass_type.as_nullable
148 bounds.add(bound)
149 nfd.bound = bound
150 else
151 # Inherit the bound
152 var bound = mclass.intro.bound_mtype.arguments[i]
153 bounds.add(bound)
154 nfd.bound = bound
155 end
156 end
157 end
158
159 var bound_mtype = mclass.get_mtype(bounds)
160 var mclassdef = new MClassDef(mmodule, bound_mtype, nclassdef.location, names)
161 nclassdef.mclassdef = mclassdef
162 self.mclassdef2nclassdef[mclassdef] = nclassdef
163
164 if nclassdef isa AStdClassdef then
165 var ndoc = nclassdef.n_doc
166 if ndoc != null then mclassdef.mdoc = ndoc.to_mdoc
167 end
168
169 if mclassdef.is_intro then
170 self.toolcontext.info("{mclassdef} introduces new {mclass.kind} {mclass.full_name}", 3)
171 else
172 self.toolcontext.info("{mclassdef} refine {mclass.kind} {mclass.full_name}", 3)
173 end
174 end
175
176 # Visit the AST and set the super-types of the `MClassDef` objects
177 private fun collect_a_mclassdef_inheritance(nmodule: AModule, nclassdef: AClassdef)
178 do
179 var mmodule = nmodule.mmodule.as(not null)
180 var objectclass = try_get_mclass_by_name(nmodule, mmodule, "Object")
181 var pointerclass = try_get_mclass_by_name(nmodule, mmodule, "Pointer")
182 var mclass = nclassdef.mclass.as(not null)
183 var mclassdef = nclassdef.mclassdef.as(not null)
184
185 # Do we need to specify Object as a super class?
186 var specobject = true
187
188 # Do we need to specify Pointer as a super class? (is only valid
189 # if `nclassdef` is an extern class)
190 var specpointer = true
191
192 var supertypes = new Array[MClassType]
193 if nclassdef isa AStdClassdef then
194 for nsc in nclassdef.n_superclasses do
195 specobject = false
196 var ntype = nsc.n_type
197 var mtype = resolve_mtype_unchecked(nclassdef, ntype, false)
198 if mtype == null then continue # Skip because of error
199 if not mtype isa MClassType then
200 error(ntype, "Error: supertypes cannot be a formal type")
201 return
202 end
203 supertypes.add mtype
204 #print "new super : {mclass} < {mtype}"
205 if mtype.mclass.kind == extern_kind then specpointer = false
206 end
207 end
208
209 if mclassdef.is_intro and objectclass != null then
210 if mclass.kind == extern_kind and mclass.name != "Pointer" then
211 # it is an extern class, but not a Pointer
212 if specpointer then supertypes.add pointerclass.mclass_type
213 else if specobject and mclass.name != "Object" then
214 # it is a standard class without super class (but is not Object)
215 supertypes.add objectclass.mclass_type
216 end
217 end
218
219 mclassdef.set_supertypes(supertypes)
220 if not supertypes.is_empty then self.toolcontext.info("{mclassdef} new super-types: {supertypes.join(", ")}", 3)
221 end
222
223 # Check the validity of the specialization heirarchy
224 private fun check_supertypes(nmodule: AModule, nclassdef: AClassdef)
225 do
226 var mmodule = nmodule.mmodule.as(not null)
227 var objectclass = try_get_mclass_by_name(nmodule, mmodule, "Object")
228 var mclass = nclassdef.mclass.as(not null)
229 var mclassdef = nclassdef.mclassdef.as(not null)
230
231 for s in mclassdef.supertypes do
232 if s.is_subtype(mmodule, mclassdef.bound_mtype, mclassdef.bound_mtype) then
233 error(nclassdef, "Error: Inheritance loop for class {mclass} with type {s}")
234 end
235 end
236 end
237
238 # Build the classes of the module `nmodule`.
239 # REQUIRE: classes of imported modules are already build. (let `phase` do the job)
240 private fun build_classes(nmodule: AModule)
241 do
242 var errcount = toolcontext.error_count
243 # Force building recursively
244 if nmodule.build_classes_is_done then return
245 nmodule.build_classes_is_done = true
246 var mmodule = nmodule.mmodule.as(not null)
247 for imp in mmodule.in_importation.direct_greaters do
248
249 build_classes(mmodule2nmodule[imp])
250 end
251
252 if errcount != toolcontext.error_count then return
253
254 # Create all classes
255 for nclassdef in nmodule.n_classdefs do
256 self.build_a_mclass(nmodule, nclassdef)
257 end
258
259 if errcount != toolcontext.error_count then return
260
261 # Create all classdefs
262 for nclassdef in nmodule.n_classdefs do
263 self.build_a_mclassdef(nmodule, nclassdef)
264 end
265
266 if errcount != toolcontext.error_count then return
267
268 # Create inheritance on all classdefs
269 for nclassdef in nmodule.n_classdefs do
270 self.collect_a_mclassdef_inheritance(nmodule, nclassdef)
271 end
272
273 if errcount != toolcontext.error_count then return
274
275 # Create the mclassdef hierarchy
276 for nclassdef in nmodule.n_classdefs do
277 var mclassdef = nclassdef.mclassdef.as(not null)
278 mclassdef.add_in_hierarchy
279 end
280
281 if errcount != toolcontext.error_count then return
282
283 # Check inheritance
284 for nclassdef in nmodule.n_classdefs do
285 self.check_supertypes(nmodule, nclassdef)
286 end
287
288 if errcount != toolcontext.error_count then return
289
290 # Check unchecked ntypes
291 for nclassdef in nmodule.n_classdefs do
292 if nclassdef isa AStdClassdef then
293 # check bound of formal parameter
294 for nfd in nclassdef.n_formaldefs do
295 var nfdt = nfd.n_type
296 if nfdt != null and nfdt.mtype != null then
297 var bound = resolve_mtype(nclassdef, nfdt)
298 if bound == null then return # Forward error
299 end
300 end
301 # check declared super types
302 for nsc in nclassdef.n_superclasses do
303 var ntype = nsc.n_type
304 if ntype.mtype != null then
305 var mtype = resolve_mtype(nclassdef, ntype)
306 if mtype == null then return # Forward error
307 end
308 end
309 end
310 end
311
312 if errcount != toolcontext.error_count then return
313
314 # Check clash of ancestors
315 for nclassdef in nmodule.n_classdefs do
316 var mclassdef = nclassdef.mclassdef.as(not null)
317 var superclasses = new HashMap[MClass, MClassType]
318 for scd in mclassdef.in_hierarchy.greaters do
319 for st in scd.supertypes do
320 if not superclasses.has_key(st.mclass) then
321 superclasses[st.mclass] = st
322 else if superclasses[st.mclass] != st then
323 var st1 = superclasses[st.mclass].resolve_for(mclassdef.mclass.mclass_type, mclassdef.bound_mtype, mmodule, false)
324 var st2 = st.resolve_for(mclassdef.mclass.mclass_type, mclassdef.bound_mtype, mmodule, false)
325 if st1 != st2 then
326 error(nclassdef, "Error: Incompatibles ancestors for {mclassdef.mclass}: {st1}, {st2}")
327 end
328 end
329 end
330 end
331 end
332
333 if errcount != toolcontext.error_count then return
334
335 # TODO: Check that the super-class is not intrusive
336
337 # Check that the superclasses are not already known (by transitivity)
338 for nclassdef in nmodule.n_classdefs do
339 if not nclassdef isa AStdClassdef then continue
340 var mclassdef = nclassdef.mclassdef.as(not null)
341
342 # Get the direct superclasses
343 # Since we are a mclassdef, just look at the mclassdef hierarchy
344 var parents = new Array[MClass]
345 for sup in mclassdef.in_hierarchy.direct_greaters do
346 parents.add(sup.mclass)
347 end
348
349 # Used to track duplicates of superclasses
350 var seen_parents = new ArrayMap[MClass, AType]
351
352 # The Object class
353 var objectclass = try_get_mclass_by_name(nmodule, mmodule, "Object")
354
355 # Check each declared superclass to see if it belong to the direct superclass
356 for nsc in nclassdef.n_superclasses do
357 var ntype = nsc.n_type
358 var mtype = ntype.mtype
359 if mtype == null then continue
360 assert mtype isa MClassType
361 var sc = mtype.mclass
362 if not parents.has(sc) or sc == objectclass then
363 warning(ntype, "Warning: superfluous super-class {mtype} in class {mclassdef.mclass}.")
364 else if not seen_parents.has_key(sc) then
365 seen_parents[sc] = ntype
366 else
367 warning(ntype, "Warning: duplicated super-class {mtype} in class {mclassdef.mclass}.")
368 end
369 end
370 end
371 end
372
373 # Register the nclassdef associated to each mclassdef
374 # FIXME: why not refine the `MClassDef` class with a nullable attribute?
375 var mclassdef2nclassdef: HashMap[MClassDef, AClassdef] = new HashMap[MClassDef, AClassdef]
376
377 # Return the static type associated to the node `ntype`.
378 # `nclassdef` is the context where the call is made (used to understand formal types)
379 # The mmodule used as context is `nclassdef.mmodule`
380 # In case of problem, an error is displayed on `ntype` and null is returned.
381 # FIXME: the name "resolve_mtype" is awful
382 fun resolve_mtype_unchecked(nclassdef: AClassdef, ntype: AType, with_virtual: Bool): nullable MType
383 do
384 var name = ntype.n_id.text
385 var mclassdef = nclassdef.mclassdef
386 var mmodule = nclassdef.parent.as(AModule).mmodule.as(not null)
387 var res: MType
388
389 # Check virtual type
390 if mclassdef != null and with_virtual then
391 var prop = try_get_mproperty_by_name(ntype, mclassdef, name).as(nullable MVirtualTypeProp)
392 if prop != null then
393 if not ntype.n_types.is_empty then
394 error(ntype, "Type error: formal type {name} cannot have formal parameters.")
395 end
396 res = prop.mvirtualtype
397 if ntype.n_kwnullable != null then res = res.as_nullable
398 ntype.mtype = res
399 return res
400 end
401 end
402
403 # Check parameter type
404 if mclassdef != null and mclassdef.parameter_names.has(name) then
405 if not ntype.n_types.is_empty then
406 error(ntype, "Type error: formal type {name} cannot have formal parameters.")
407 end
408 for i in [0..mclassdef.parameter_names.length[ do
409 if mclassdef.parameter_names[i] == name then
410 res = mclassdef.mclass.mclass_type.arguments[i]
411 if ntype.n_kwnullable != null then res = res.as_nullable
412 ntype.mtype = res
413 return res
414 end
415 end
416 abort
417 end
418
419 # Check class
420 var mclass = try_get_mclass_by_name(ntype, mmodule, name)
421 if mclass != null then
422 var arity = ntype.n_types.length
423 if arity != mclass.arity then
424 if arity == 0 then
425 error(ntype, "Type error: '{name}' is a generic class.")
426 else if mclass.arity == 0 then
427 error(ntype, "Type error: '{name}' is not a generic class.")
428 else
429 error(ntype, "Type error: '{name}' has {mclass.arity} parameters ({arity} are provided).")
430 end
431 return null
432 end
433 if arity == 0 then
434 res = mclass.mclass_type
435 if ntype.n_kwnullable != null then res = res.as_nullable
436 ntype.mtype = res
437 return res
438 else
439 var mtypes = new Array[MType]
440 for nt in ntype.n_types do
441 var mt = resolve_mtype_unchecked(nclassdef, nt, with_virtual)
442 if mt == null then return null # Forward error
443 mtypes.add(mt)
444 end
445 res = mclass.get_mtype(mtypes)
446 if ntype.n_kwnullable != null then res = res.as_nullable
447 ntype.mtype = res
448 return res
449 end
450 end
451
452 # If everything fail, then give up :(
453 error(ntype, "Type error: class {name} not found in module {mmodule}.")
454 return null
455 end
456
457 # Return the static type associated to the node `ntype`.
458 # `nclassdef` is the context where the call is made (used to understand formal types)
459 # The mmodule used as context is `nclassdef.mmodule`
460 # In case of problem, an error is displayed on `ntype` and null is returned.
461 # FIXME: the name "resolve_mtype" is awful
462 fun resolve_mtype(nclassdef: AClassdef, ntype: AType): nullable MType
463 do
464 var mtype = ntype.mtype
465 if mtype == null then mtype = resolve_mtype_unchecked(nclassdef, ntype, true)
466 if mtype == null then return null # Forward error
467
468 if ntype.checked_mtype then return mtype
469 if mtype isa MGenericType then
470 var mmodule = nclassdef.parent.as(AModule).mmodule.as(not null)
471 var mclassdef = nclassdef.mclassdef
472 var mclass = mtype.mclass
473 for i in [0..mclass.arity[ do
474 var bound = mclass.intro.bound_mtype.arguments[i]
475 var nt = ntype.n_types[i]
476 var mt = resolve_mtype(nclassdef, nt)
477 if mt == null then return null # forward error
478 if not mt.is_subtype(mmodule, mclassdef.bound_mtype, bound) then
479 error(nt, "Type error: expected {bound}, got {mt}")
480 return null
481 end
482 end
483 end
484 ntype.checked_mtype = true
485 return mtype
486 end
487
488 end
489
490 redef class AModule
491 # Flag that indicate if the class building is already completed
492 var build_classes_is_done: Bool = false
493 # What is the AClassdef associated to a `MClass`?
494 # Used to check multiple definition of a class.
495 var mclass2nclassdef: Map[MClass, AClassdef] = new HashMap[MClass, AClassdef]
496 end
497
498 redef class AClassdef
499 # The associated MClass once build by a `ModelBuilder`
500 var mclass: nullable MClass
501 # The associated MClassDef once build by a `ModelBuilder`
502 var mclassdef: nullable MClassDef
503 end
504
505 redef class AClasskind
506 # The class kind associated with the AST node class
507 private fun mkind: MClassKind is abstract
508 end
509 redef class AConcreteClasskind
510 redef fun mkind do return concrete_kind
511 end
512 redef class AAbstractClasskind
513 redef fun mkind do return abstract_kind
514 end
515 redef class AInterfaceClasskind
516 redef fun mkind do return interface_kind
517 end
518 redef class AEnumClasskind
519 redef fun mkind do return enum_kind
520 end
521 redef class AExternClasskind
522 redef fun mkind do return extern_kind
523 end
524
525 redef class AFormaldef
526 # The associated parameter type
527 var mtype: nullable MParameterType = null
528
529 # The associated bound
530 var bound: nullable MType = null
531 end
532
533 redef class AType
534 # The mtype associated to the node
535 var mtype: nullable MType = null
536
537 # Is the mtype a valid one?
538 var checked_mtype: Bool = false
539 end