Do not abort when comparing signatures, just return false
[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 # Number of parameters
86 meth arity: Int
87 do
88 assert _params != null
89 return _params.length
90 end
91
92 # Is self a valid subtype of an other signature
93 meth <(s: MMSignature): Bool
94 do
95 assert s != null
96 if self == s then
97 return true
98 end
99 assert _recv.module == s.recv.module
100 if arity != s.arity or (_return_type == null) != (s.return_type == null) then return false
101 if _return_type != null and not _return_type < s.return_type then
102 return false
103 end
104
105 for i in [0..arity[ do
106 if not s[i] < self[i] then
107 return false
108 end
109 end
110 return true
111 end
112
113 # The type of the i-th parameter
114 meth [](i: Int): MMType
115 do
116 assert _params.length > i
117 return _params[i]
118 end
119
120 redef meth to_s
121 do
122 var s: String
123 if _params != null and _params.length > 0 then
124 var tmp: String
125 var a = new Array[String].with_capacity(_params.length)
126 for i in [0.._params.length[ do
127 #var pn = _params_name[i]
128 var p = _params[i]
129 #a.add("{pn}: {p}")
130 a.add(p.to_s)
131 end
132 s = "({a.join(",")})"
133 else
134 s = ""
135 end
136 if _return_type != null then
137 s.append(": {_return_type}")
138 end
139 return s
140 end
141
142 # Adapt the signature to a different receiver
143 meth adaptation_to(r: MMType): MMSignature
144 do
145 if _recv == r then
146 return self
147 end
148 var mod = r.module
149 var p = _params
150 if p != null then
151 p = new Array[MMType]
152 for i in _params do
153 p.add(i.for_module(mod).adapt_to(r))
154 end
155 end
156 var rv = _return_type
157 if rv != null then
158 rv = rv.for_module(mod).adapt_to(r)
159 end
160 return new MMSignature(p,rv,r)
161 end
162
163 init(params: Array[MMType], return_type: MMType, r: MMType)
164 do
165 assert params != null
166 _params = params
167 _return_type = return_type
168 _recv = r
169 end
170 end
171
172 # Inheritance relation between two types
173 abstract class MMAncestor
174 # The inherited type
175 readable writable attr _stype: MMType
176
177 # The inheriter (heir) type
178 readable writable attr _inheriter: MMType
179
180 meth is_reffinement: Bool do
181 return stype.module != stype.module
182 end
183
184 meth is_specialisation: Bool do
185 return stype.local_class.global != inheriter.local_class.global
186 end
187
188 # The inherited class
189 meth local_class: MMLocalClass is abstract
190
191 redef meth to_s
192 do
193 if stype == null then
194 return local_class.to_s
195 else
196 return stype.to_s
197 end
198 end
199 end
200
201 # A static type
202 # Note that static type a related to a specific module
203 abstract class MMType
204 # The module where self makes sence
205 meth module: MMModule is abstract
206
207 # The local class that self direclty or indirectly refers to
208 meth local_class: MMLocalClass is abstract
209
210 # Is self a valid subtype of t
211 meth <(t : MMType): Bool is abstract
212
213 # Is self a valid supertype of t
214 # This method must be only called within definition of < if
215 # a double dispatch is needed
216 meth is_supertype(t: MMType): Bool is abstract
217
218 # Adapt self to another module
219 meth for_module(mod: MMModule): MMType is abstract
220
221 # Get the type adapted to another receiver type
222 # Useful for formal types
223 meth adapt_to(recv: MMType): MMType is abstract
224
225 # Adapt self to another local class context
226 # Useful for genericity
227 meth upcast_for(c: MMLocalClass): MMType is abstract
228
229 # Return a type approximation if the reveiver is not self
230 # Useful for virtual types
231 meth not_for_self: MMType do return self
232 end
233
234 class MMTypeClass
235 special MMType
236 redef readable attr _local_class: MMLocalClass
237 redef meth module do return _local_class.module end
238 redef meth <(t) do return t != null and t.is_supertype(self)
239
240 redef meth to_s
241 do
242 return _local_class.to_s
243 end
244
245 redef meth upcast_for(c)
246 do
247 assert _local_class != null
248 assert c != null
249
250 var t: MMType = self
251 if _local_class != c then
252 t = _local_class.ancestor(c)
253 end
254 assert t != null
255 return t
256 end
257
258 init(c : MMLocalClass)
259 do
260 _local_class = c
261 end
262 end
263
264 class MMTypeSimpleClass
265 special MMTypeClass
266 redef meth is_supertype(t)
267 do
268 return t.local_class.cshe <= _local_class
269 end
270
271 redef meth for_module(mod)
272 do
273 var t: MMType = self
274 if module != mod then
275 t = _local_class.for_module(mod).get_type
276 end
277 assert t != null
278 return t
279 end
280
281 redef meth adapt_to(recv) do return self
282
283 redef init(c: MMLocalClass)
284 do
285 super(c)
286 end
287 end
288
289 # The type of null
290 class MMTypeNone
291 special MMType
292 redef readable attr _module: MMModule
293 redef meth <(t) do return true
294 redef meth is_supertype(t) do return false
295 redef meth local_class do abort
296 redef meth upcast_for(c) do return self
297
298 private init(m: MMModule) do _module = m
299 end
300
301 redef class MMModule
302 # The type of null
303 readable attr _type_none: MMTypeNone = new MMTypeNone(self)
304 end