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