Fix MMSignature::not_for_self
[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 attr _base_type_cache: MMType
26
27 # Return the type of self for this class
28 meth get_type: MMType
29 do
30 if _base_type_cache == null then _base_type_cache = new MMTypeSimpleClass(self)
31 return _base_type_cache
32 end
33
34 # Register a new ancestor
35 protected meth 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 attr _ancestors: Map[MMLocalClass, MMAncestor]
44
45 # The ancestor type for a given superclass
46 meth ancestor(c: MMLocalClass): MMType
47 do
48 assert _ancestors != null
49 if _ancestors.has_key(c) then
50 return _ancestors[c].stype
51 end
52 return null
53 end
54 end
55
56 redef class MMLocalProperty
57 # The signature of the property (where it is declared)
58 readable writable attr _signature: MMSignature
59
60 attr _signatures_cache: HashMap[MMType, MMSignature] = new HashMap[MMType, MMSignature]
61
62 # Return the adapted signature of self for a receiver of type t
63 meth signature_for(t: MMType): MMSignature do
64 if t == local_class.get_type then return signature
65
66 if _signatures_cache.has_key(t) then return _signatures_cache[t]
67
68 var res = signature.adaptation_to(t)
69 _signatures_cache[t] = res
70 return res
71 end
72 end
73
74 # Signature for local properties
75 class MMSignature
76 # The type of the reveiver
77 readable attr _recv: MMType
78
79 # The parameter types
80 attr _params: Array[MMType]
81
82 # The return type
83 readable attr _return_type: MMType
84
85 # The closure parameters
86 readable attr _closures: Array[MMClosure] = new Array[MMClosure]
87
88 # Number of parameters
89 meth arity: Int
90 do
91 assert _params != null
92 return _params.length
93 end
94
95 # Is self a valid subtype of an other signature
96 meth <(s: MMSignature): Bool
97 do
98 assert s != null
99 if self == s then
100 return true
101 end
102 assert _recv.module == s.recv.module
103 if arity != s.arity or (_return_type == null) != (s.return_type == null) then return false
104 if _return_type != null and not _return_type < s.return_type then
105 return false
106 end
107
108 for i in [0..arity[ do
109 if not s[i] < self[i] then
110 return false
111 end
112 end
113 return true
114 end
115
116 # The type of the i-th parameter
117 meth [](i: Int): MMType
118 do
119 assert _params.length > i
120 return _params[i]
121 end
122
123 redef meth to_s
124 do
125 var s: String
126 if _params != null and _params.length > 0 then
127 var tmp: String
128 var a = new Array[String].with_capacity(_params.length)
129 for i in [0.._params.length[ do
130 #var pn = _params_name[i]
131 var p = _params[i]
132 #a.add("{pn}: {p}")
133 a.add(p.to_s)
134 end
135 s = "({a.join(",")})"
136 else
137 s = ""
138 end
139 if _return_type != null then
140 s.append(": {_return_type}")
141 end
142 return s
143 end
144
145 # Adapt the signature to a different receiver
146 meth adaptation_to(r: MMType): MMSignature
147 do
148 if _recv == r then
149 return self
150 end
151 var mod = r.module
152 var p = _params
153 if p != null then
154 p = new Array[MMType]
155 for i in _params do
156 p.add(i.for_module(mod).adapt_to(r))
157 end
158 end
159 var rv = _return_type
160 if rv != null then
161 rv = rv.for_module(mod).adapt_to(r)
162 end
163 var res = new MMSignature(p,rv,r)
164 for clos in _closures do
165 res.closures.add(clos.adaptation_to(r))
166 end
167 return res
168 end
169
170 attr _not_for_self_cache: MMSignature = null
171
172 # Return a type approximation if the reveiver is not self
173 # Useful for virtual types
174 meth not_for_self: MMSignature
175 do
176 var res = _not_for_self_cache
177 if res != null then return res
178
179 var need_for_self = false
180 var p = _params
181 if p != null then
182 p = new Array[MMType]
183 for i in _params do
184 var i2 = i.not_for_self
185 if i != i2 then need_for_self = true
186 p.add(i2)
187 end
188 end
189
190 var rv = _return_type
191 if rv != null then
192 rv = rv.not_for_self
193 if rv != _return_type then need_for_self = true
194 end
195
196 var clos = _closures
197 if clos != null then
198 clos = new Array[MMClosure]
199 for c in _closures do
200 var c2 = c.not_for_self
201 if c2 != c then need_for_self = true
202 clos.add(c2)
203 end
204 end
205
206 if need_for_self then
207 res = new MMSignature(p, rv, _recv)
208 res.closures.add_all(clos)
209 else
210 res = self
211 end
212
213 _not_for_self_cache = res
214 return res
215 end
216
217 init(params: Array[MMType], return_type: MMType, r: MMType)
218 do
219 assert params != null
220 _params = params
221 _return_type = return_type
222 _recv = r
223 end
224 end
225
226 # A closure in a signature
227 class MMClosure
228 # The signature of the closure
229 readable attr _signature: MMSignature
230
231 # Is the closure a brek one
232 # aka is defined with the break keyword thus does not return
233 readable attr _is_break: Bool
234
235 # Adapt the signature to a different receiver
236 meth adaptation_to(r: MMType): MMClosure
237 do
238 return new MMClosure(_signature.adaptation_to(r), _is_break)
239 end
240
241 init(s: MMSignature, is_break: Bool)
242 do
243 _signature = s
244 _is_break = is_break
245 end
246
247 meth not_for_self: MMClosure
248 do
249 var sig = _signature.not_for_self
250 if sig != _signature then
251 return new MMClosure(sig, _is_break)
252 else
253 return self
254 end
255 end
256 end
257
258 # Inheritance relation between two types
259 abstract class MMAncestor
260 # The inherited type
261 readable writable attr _stype: MMType = null
262
263 # The inheriter (heir) type
264 readable writable attr _inheriter: MMType = null
265
266 meth is_reffinement: Bool do
267 return stype.module != stype.module
268 end
269
270 meth is_specialisation: Bool do
271 return stype.local_class.global != inheriter.local_class.global
272 end
273
274 # The inherited class
275 meth local_class: MMLocalClass is abstract
276
277 redef meth to_s
278 do
279 if stype == null then
280 return local_class.to_s
281 else
282 return stype.to_s
283 end
284 end
285 end
286
287 # A static type
288 # Note that static type a related to a specific module
289 abstract class MMType
290 # The module where self makes sence
291 meth module: MMModule is abstract
292
293 # The local class that self direclty or indirectly refers to
294 meth local_class: MMLocalClass is abstract
295
296 # Is self a valid subtype of t
297 meth <(t : MMType): Bool is abstract
298
299 # Is self a valid supertype of t
300 # This method must be only called within definition of < if
301 # a double dispatch is needed
302 meth is_supertype(t: MMType): Bool is abstract
303
304 # Adapt self to another module
305 meth for_module(mod: MMModule): MMType is abstract
306
307 # Get the type adapted to another receiver type
308 # Useful for formal types
309 meth adapt_to(recv: MMType): MMType is abstract
310
311 # Adapt self to another local class context
312 # Useful for genericity
313 meth upcast_for(c: MMLocalClass): MMType is abstract
314
315 # Return a type approximation if the reveiver is not self
316 # Useful for virtual types
317 meth not_for_self: MMType do return self
318 end
319
320 class MMTypeClass
321 special MMType
322 redef readable attr _local_class: MMLocalClass
323 redef meth module do return _local_class.module end
324 redef meth <(t) do return t != null and t.is_supertype(self)
325
326 redef meth to_s
327 do
328 return _local_class.to_s
329 end
330
331 redef meth upcast_for(c)
332 do
333 assert _local_class != null
334 assert c != null
335
336 var t: MMType = self
337 if _local_class != c then
338 t = _local_class.ancestor(c)
339 end
340 assert t != null
341 return t
342 end
343
344 init(c : MMLocalClass)
345 do
346 _local_class = c
347 end
348 end
349
350 class MMTypeSimpleClass
351 special MMTypeClass
352 redef meth is_supertype(t)
353 do
354 return t.local_class.cshe <= _local_class
355 end
356
357 redef meth for_module(mod)
358 do
359 var t: MMType = self
360 if module != mod then
361 t = _local_class.for_module(mod).get_type
362 end
363 assert t != null
364 return t
365 end
366
367 redef meth adapt_to(recv) do return self
368
369 init(c: MMLocalClass)
370 do
371 super(c)
372 end
373 end
374
375 # The type of null
376 class MMTypeNone
377 special MMType
378 redef readable attr _module: MMModule
379 redef meth <(t) do return true
380 redef meth is_supertype(t) do return false
381 redef meth local_class do abort
382 redef meth upcast_for(c) do return self
383
384 private init(m: MMModule) do _module = m
385 end
386
387 redef class MMModule
388 # The type of null
389 readable attr _type_none: MMTypeNone = new MMTypeNone(self)
390 end