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