Merge branch 'pu/parameter-names' into wip
[nit.git] / src / metamodel / static_type.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2004-2008 Jean Privat <jean@pryen.org>
4 # Copyright 2006-2008 Floréal Morandat <morandat@lirmm.fr>
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17
18 # Static types and property signatures
19 package static_type
20
21 intrude import abstractmetamodel
22
23 redef class MMLocalClass
24 # Cached result of get_type
25 var _base_type_cache: nullable MMType
26
27 # Return the type of self for this class
28 fun get_type: MMType
29 do
30 if _base_type_cache == null then _base_type_cache = new MMTypeSimpleClass(self)
31 return _base_type_cache.as(not null)
32 end
33
34 # Register a new ancestor
35 protected fun add_ancestor(a: MMAncestor)
36 do
37 assert not _ancestors.has_key(a.local_class)
38 assert a.local_class != self
39 _ancestors[a.local_class] = a
40 end
41
42 # Array of ancestor that associate each superclass with the corresponding ancestor
43 readable var _ancestors: nullable Map[MMLocalClass, MMAncestor]
44
45 # The ancestor type for a given superclass
46 fun ancestor(c: MMLocalClass): MMType
47 do
48 return _ancestors[c].stype
49 end
50 end
51
52 redef class MMLocalProperty
53 # The signature of the property (where it is declared)
54 readable writable var _signature: nullable MMSignature
55
56 var _signatures_cache: HashMap[MMType, MMSignature] = new HashMap[MMType, MMSignature]
57
58 # Return the adapted signature of self for a receiver of type t
59 fun signature_for(t: MMType): MMSignature do
60 if t == local_class.get_type then return signature.as(not null)
61
62 if _signatures_cache.has_key(t) then return _signatures_cache[t]
63
64 var res = signature.adaptation_to(t)
65 _signatures_cache[t] = res
66 return res
67 end
68 end
69
70 class MMParam
71 readable var _mmtype: MMType
72 readable var _name: Symbol
73
74 init ( t : MMType, n : Symbol )
75 do
76 _mmtype = t
77 _name = n
78 end
79
80 redef fun to_s do return "{name}: {mmtype}"
81 end
82
83 # Signature for local properties
84 class MMSignature
85 # The type of the reveiver
86 readable var _recv: MMType
87
88 # The parameter types
89 readable var _params: Array[MMParam]
90
91 # The return type
92 readable var _return_type: nullable MMType
93
94 # The closure parameters
95 readable var _closures: Array[MMClosure] = new Array[MMClosure]
96
97 # Return the closure named 'name'. Null if no such a closure exists.
98 fun closure_named(name: Symbol): nullable MMClosure
99 do
100 for c in _closures do
101 if c.name == name then return c
102 end
103 return null
104 end
105
106 # Number of parameters
107 fun arity: Int
108 do
109 return _params.length
110 end
111
112 # Is self a valid subtype of an other signature
113 fun <(s: MMSignature): Bool
114 do
115 if self == s then
116 return true
117 end
118 assert _recv.mmmodule == s.recv.mmmodule
119 var rt = _return_type
120 var srt = s.return_type
121 if arity != s.arity or (rt == null) != (srt == null) then return false
122 if rt != null and not rt < srt.as(not null) then
123 return false
124 end
125
126 for i in [0..arity[ do
127 if not s[i] < self[i] then
128 return false
129 end
130 end
131
132 if closures.length != s.closures.length then return false
133 for i in [0..closures.length[ do
134 if not s.closures[i] < closures[i] then return false
135 end
136 return true
137 end
138
139 # The type of the i-th parameter
140 fun [](i: Int): MMType
141 do
142 assert _params.length > i
143 return _params[i].mmtype
144 end
145
146 redef fun to_s
147 do
148 var s = new Buffer
149 if _params.length > 0 then
150 var tmp: String
151 var a = new Array[String].with_capacity(_params.length)
152 for i in [0.._params.length[ do
153 var p = _params[i]
154 a.add(p.to_s)
155 end
156 s.append("({a.join(", ")})")
157 end
158 var rt = _return_type
159 if rt != null then s.append(": {rt}")
160 return s.to_s
161 end
162
163 # Adapt the signature to a different receiver
164 fun adaptation_to(r: MMType): MMSignature
165 do
166 if _recv == r then
167 return self
168 end
169 var mod = r.mmmodule
170 var p = new Array[MMParam]
171 for i in _params do
172 var new_type = i.mmtype.for_module(mod).adapt_to(r)
173 var new_param
174 if new_type == i.mmtype then
175 new_param = i
176 else
177 new_param = new MMParam( new_type, i.name )
178 end
179
180 p.add( new_param )
181 end
182 var rv = _return_type
183 if rv != null then
184 rv = rv.for_module(mod).adapt_to(r)
185 end
186 var res = new MMSignature(p,rv,r)
187 for clos in _closures do
188 res.closures.add(clos.adaptation_to(r))
189 end
190 return res
191 end
192
193 var _not_for_self_cache: nullable MMSignature = null
194
195 # Return a type approximation if the reveiver is not self
196 # Useful for virtual types
197 fun not_for_self: MMSignature
198 do
199 if _not_for_self_cache != null then return _not_for_self_cache.as(not null)
200
201 var need_for_self = false
202 var p = new Array[MMParam]
203 for i in _params do
204 var new_type = i.mmtype.not_for_self
205 var new_param
206 if i.mmtype == new_type then
207 new_param = i
208 else
209 need_for_self = true
210 new_param = new MMParam( new_type, i.name )
211 end
212
213 p.add( new_param )
214 end
215
216 var rv = _return_type
217 if rv != null then
218 rv = rv.not_for_self
219 if rv != _return_type then need_for_self = true
220 end
221
222 var clos = new Array[MMClosure]
223 for c in _closures do
224 var c2 = c.not_for_self
225 if c2 != c then need_for_self = true
226 clos.add(c2)
227 end
228
229 var res: MMSignature
230 if need_for_self then
231 res = new MMSignature(p, rv, _recv)
232 res.closures.add_all(clos)
233 else
234 res = self
235 end
236
237 _not_for_self_cache = res
238 return res
239 end
240
241 init(params: Array[MMParam], return_type: nullable MMType, r: MMType)
242 do
243 _params = params
244 _return_type = return_type
245 _recv = r
246 end
247 end
248
249 # A closure in a signature
250 class MMClosure
251 # The name of the closure (without the !)
252 readable var _name: Symbol
253
254 # The signature of the closure
255 readable var _signature: MMSignature
256
257 # Is the closure a brek one
258 # aka is defined with the break keyword thus does not return
259 readable var _is_break: Bool
260
261 # Is the closure optional?
262 # ie is there a default definition
263 readable var _is_optional: Bool
264
265 # Adapt the signature to a different receiver
266 fun adaptation_to(r: MMType): MMClosure
267 do
268 return new MMClosure(_name, _signature.adaptation_to(r), _is_break, _is_optional)
269 end
270
271 init(name: Symbol, s: MMSignature, is_break: Bool, is_optional: Bool)
272 do
273 _name = name
274 _signature = s
275 _is_break = is_break
276 _is_optional = is_optional
277 end
278
279 fun not_for_self: MMClosure
280 do
281 var sig = _signature.not_for_self
282 if sig != _signature then
283 return new MMClosure(_name, sig, _is_break, _is_optional)
284 else
285 return self
286 end
287 end
288
289 fun <(c: MMClosure): Bool
290 do
291 if c.is_optional and not is_optional then return false
292 if not c.is_break and is_break then return false
293 return c.signature < signature
294 end
295 end
296
297 # Inheritance relation between two types
298 abstract class MMAncestor
299 # The inherited type
300 writable var _stype: nullable MMType = null
301
302 # The inherited type
303 fun stype: MMType do return _stype.as(not null)
304
305 # The inheriter (heir) type
306 writable var _inheriter: nullable MMType = null
307
308 # The inheriter (heir) type
309 fun inheriter: MMType do return _inheriter.as(not null)
310
311 fun is_reffinement: Bool do
312 return stype.mmmodule != stype.mmmodule
313 end
314
315 fun is_specialisation: Bool do
316 return stype.local_class.global != inheriter.local_class.global
317 end
318
319 # The inherited class
320 fun local_class: MMLocalClass is abstract
321
322 redef fun to_s
323 do
324 if _stype == null then
325 return local_class.to_s
326 else
327 return stype.to_s
328 end
329 end
330 end
331
332 # A static type
333 # Note that static type is related to a specific module
334 abstract class MMType
335 # The module where self makes sence
336 fun mmmodule: MMModule is abstract
337
338 # The local class that self direclty or indirectly refers to
339 fun local_class: MMLocalClass is abstract
340
341 # Is the type a valid one
342 # For instance, circular dependency on formal types is invalid
343 fun is_valid: Bool do return true
344
345 # Is self a valid subtype of t
346 fun <(t : MMType): Bool is abstract
347
348 # Is self a valid supertype of t
349 # This method must be only called within definition of < if
350 # a double dispatch is needed
351 fun is_supertype(t: MMType): Bool is abstract
352
353 # Adapt self to another module
354 fun for_module(mod: MMModule): MMType is abstract
355
356 # Get the type adapted to another receiver type
357 # Useful for formal types
358 fun adapt_to(recv: MMType): MMType is abstract
359
360 # Adapt self to another local class context
361 # Useful for genericity
362 # 'c' Must be a super-class of self
363 # Example:
364 # class A[E]
365 # class B[F] super A[F]
366 # class C[G] super B[String]
367 # class D super C[Float]
368 # 'A[Int]'.upcast_for('A') -> 'A[Int]'
369 # 'A[Int]'.upcast_for('B') -> abort
370 # 'B[Int]'.upcast_for('B') -> 'B[Int]'
371 # 'B[Int]'.upcast_for('A') -> 'A[Int]'
372 # 'B[Int]'.upcast_for('C') -> abort
373 # 'C[Int]'.upcast_for('C') -> 'C[Int]'
374 # 'C[Int]'.upcast_for('B') -> 'B[String]'
375 # 'C[Int]'.upcast_for('A') -> 'A[String]'
376 # 'D'.upcast_for('D') -> 'D'
377 # 'D'.upcast_for('C') -> 'C[Float]'
378 # 'D'.upcast_for('B') -> 'C[String]'
379 # 'D'.upcast_for('A') -> 'A[String]'
380 fun upcast_for(c: MMLocalClass): MMType is abstract
381
382 # Return a type approximation if the reveiver is not self
383 # Useful for virtual types
384 fun not_for_self: MMType do return self
385
386 # The nullable version of self (if needed)
387 var _as_nullable_cache: nullable MMType = null
388
389 # IS the type can accept null?
390 fun is_nullable: Bool do return false
391
392 # Return the nullable version of the type
393 # Noop if already nullable
394 fun as_nullable: MMType do
395 var cache = _as_nullable_cache
396 if cache != null then return cache
397 var res = new MMNullableType(self)
398 _as_nullable_cache = res
399 return res
400 end
401
402 # Return the not null version of the type
403 # Noop if already not null
404 fun as_notnull: MMType do return self
405 end
406
407 class MMNullableType
408 super MMType
409 var _base_type: MMType
410 redef fun is_valid do return _base_type.is_valid
411 redef fun is_nullable: Bool do return true
412 redef fun as_notnull do return _base_type
413 redef fun as_nullable do return self
414 init(t: MMType) do _base_type = t
415
416 redef fun mmmodule do return _base_type.mmmodule
417
418 redef fun local_class do return _base_type.local_class
419
420 redef fun <(t)
421 do
422 return t isa MMNullableType and _base_type < t.as_notnull
423 end
424
425 redef fun to_s
426 do
427 return "nullable {_base_type}"
428 end
429
430 redef fun is_supertype(t)
431 do
432 return _base_type.is_supertype(t)
433 end
434
435 redef fun for_module(mod)
436 do
437 return _base_type.for_module(mod).as_nullable
438 end
439
440 redef fun adapt_to(recv)
441 do
442 return _base_type.adapt_to(recv).as_nullable
443 end
444
445 redef fun upcast_for(c)
446 do
447 return _base_type.upcast_for(c)
448 end
449
450 redef fun not_for_self
451 do
452 return _base_type.not_for_self.as_nullable
453 end
454 end
455
456 class MMTypeClass
457 super MMType
458 redef readable var _local_class: MMLocalClass
459 redef fun mmmodule do return _local_class.mmmodule end
460 redef fun <(t) do return t.is_supertype(self)
461
462 redef fun to_s
463 do
464 return _local_class.to_s
465 end
466
467 redef fun upcast_for(c)
468 do
469 var t: MMType = self
470 if _local_class != c then
471 t = _local_class.ancestor(c)
472 end
473 return t
474 end
475
476 init(c : MMLocalClass)
477 do
478 _local_class = c
479 end
480 end
481
482 class MMTypeSimpleClass
483 super MMTypeClass
484 redef fun is_supertype(t)
485 do
486 return t.local_class.cshe <= _local_class
487 end
488
489 redef fun for_module(mod)
490 do
491 var t: MMType = self
492 if mmmodule != mod then
493 t = _local_class.for_module(mod).get_type
494 end
495 return t
496 end
497
498 redef fun adapt_to(recv) do return self
499
500 init(c: MMLocalClass)
501 do
502 super(c)
503 end
504 end
505
506 # The type of null
507 class MMTypeNone
508 super MMType
509 redef readable var _mmmodule: MMModule
510 redef fun is_nullable: Bool do return true
511 redef fun <(t) do return t isa MMTypeNone or t isa MMNullableType
512 redef fun to_s do return "null"
513 redef fun is_supertype(t) do return false
514 redef fun local_class do abort
515 redef fun upcast_for(c) do abort
516 redef fun as_nullable do return self
517 redef fun as_notnull do abort
518
519 private init(m: MMModule) do _mmmodule = m
520 end
521
522 redef class MMModule
523 # The type of null
524 readable var _type_none: MMTypeNone = new MMTypeNone(self)
525
526 # The type of bool
527 fun type_bool: MMType
528 do
529 return class_by_name(once ("Bool".to_symbol)).get_type
530 end
531 end