Remove class MMSrcLocalProperty. Use MMConcreteProperty instead
[nit.git] / src / syntax / syntax_base.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2008 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 # Common syntax structures for syntax analysis of NIT AST.
18 package syntax_base
19
20 import parser
21 import mmloader
22
23 # Concrete NIT source module
24 class MMSrcModule
25 special MMModule
26 # The related AST node
27 readable attr _node: AModule
28
29 # Concrete NIT source local classs by name
30 readable attr _src_local_classes: Map[Symbol, MMSrcLocalClass]
31
32 init(c: MMContext, source: AModule, dir: MMDirectory, name: Symbol)
33 do
34 super(name, dir, c)
35 _node = source
36 _src_local_classes = new HashMap[Symbol, MMSrcLocalClass]
37 end
38 end
39
40 redef class MMGlobalClass
41 # Check that a module can access a class
42 meth check_visibility(v: AbsSyntaxVisitor, n: PNode, cm: MMSrcModule): Bool do
43 var pm = intro.module
44 assert pm isa MMSrcModule
45 var vpm = cm.visibility_for(pm)
46 if vpm == 3 then
47 return true
48 else if vpm == 0 then
49 v.error(n, "Visibility error: Class {self} comes from the hidden module {cm}.") # TODO: should not occur
50 return false
51 else if visibility_level >= 3 then
52 v.error(n, "Visibility error: Class {self} is private.")
53 return false
54 end
55 return true
56 end
57 end
58
59 # Concrete NIT source local classes
60 class MMSrcLocalClass
61 special MMConcreteClass
62 # The related AST nodes
63 readable attr _nodes: Array[PClassdef]
64
65 # Concrete NIT source generic formal parameter by name
66 readable writable attr _formal_dict: Map[Symbol, MMTypeFormalParameter]
67
68 # Concrete NIT source properties by name
69 readable attr _src_local_properties: Map[Symbol, MMConcreteProperty]
70
71 init(n: Symbol, cla: PClassdef, a: Int)
72 do
73 super(n, a)
74 _nodes = [cla]
75 _src_local_properties = new HashMap[Symbol, MMConcreteProperty]
76 end
77 end
78
79 redef class MMGlobalProperty
80 # Check that a module can access a property
81 meth check_visibility(v: AbsSyntaxVisitor, n: PNode, cm: MMSrcModule, allows_protected: Bool): Bool do
82 var pm = local_class.module
83 assert pm isa MMSrcModule
84 var vpm = cm.visibility_for(pm)
85 if vpm == 3 then
86 return true
87 else if vpm == 0 then
88 # TODO: should not occurs
89 v.error(n, "Visibility error: Property {self} comes from the hidden module {cm}.")
90 return false
91 else if visibility_level >= 3 then
92 v.error(n, "Visibility error: Property {self} is private.")
93 return false
94 else if visibility_level >= 2 and not allows_protected then
95 v.error(n, "Visibility error: Property {self} is protected and can only acceded by self.")
96 return false
97 end
98 return true
99 end
100 end
101
102 redef class MMConcreteProperty
103 # The attached node (if any)
104 meth node: PNode do return null
105 end
106
107 # Concrete NIT source attribute
108 class MMSrcAttribute
109 special MMConcreteProperty
110 special MMAttribute
111 redef readable attr _node: AAttrPropdef
112 init(name: Symbol, cla: MMLocalClass, n: AAttrPropdef)
113 do
114 super(name, cla, self)
115 _node = n
116 end
117 end
118
119 # Concrete NIT source method
120 class MMSrcMethod
121 special MMConcreteProperty
122 special MMMethod
123 end
124
125 # Concrete NIT source method for an automatic accesor
126 class MMAttrImplementationMethod
127 special MMSrcMethod
128 redef readable attr _node: AAttrPropdef
129 end
130
131 # Concrete NIT source method for an automatic read accesor
132 class MMReadImplementationMethod
133 special MMAttrImplementationMethod
134
135 init(name: Symbol, cla: MMLocalClass, n: AAttrPropdef)
136 do
137 super(name, cla, self)
138 _node = n
139 end
140 end
141
142 # Concrete NIT source method for an automatic write accesor
143 class MMWriteImplementationMethod
144 special MMAttrImplementationMethod
145
146 init(name: Symbol, cla: MMLocalClass, n: AAttrPropdef)
147 do
148 super(name, cla, self)
149 _node = n
150 end
151 end
152
153 # Concrete NIT source method for an explicit method
154 class MMMethSrcMethod
155 special MMSrcMethod
156 redef readable attr _node: AMethPropdef
157 init(name: Symbol, cla: MMLocalClass, n: AMethPropdef)
158 do
159 super(name, cla, self)
160 _node = n
161 end
162 end
163
164 # Concrete NIT source virtual type
165 class MMSrcTypeProperty
166 special MMConcreteProperty
167 special MMTypeProperty
168 redef readable attr _node: ATypePropdef
169 init(name: Symbol, cla: MMLocalClass, n: ATypePropdef)
170 do
171 super(name, cla, self)
172 _node = n
173 end
174 end
175
176
177 # Local variable and method parameter
178 class Variable
179 # Name of the variable
180 readable attr _name: Symbol
181
182 # Declaration AST node
183 readable attr _decl: PNode
184
185 # Static type
186 readable writable attr _stype: MMType
187
188 init(n: Symbol, d: PNode)
189 do
190 assert n != null
191 assert d != null
192 _name = n
193 _decl = d
194 end
195 end
196
197 ###############################################################################
198
199 # Visitor used during the syntax analysis
200 class AbsSyntaxVisitor
201 special Visitor
202 # The primitive type Bool
203 meth type_bool: MMType
204 do
205 return _module.class_by_name(once ("Bool".to_symbol)).get_type
206 end
207
208 # The primitive type Int
209 meth type_int: MMType
210 do
211 return _module.class_by_name(once ("Int".to_symbol)).get_type
212 end
213
214 # The primitive type Float
215 meth type_float: MMType
216 do
217 return _module.class_by_name(once ("Float".to_symbol)).get_type
218 end
219
220 # The primitive type Char
221 meth type_char: MMType
222 do
223 return _module.class_by_name(once ("Char".to_symbol)).get_type
224 end
225
226 # The primitive type String
227 meth type_string: MMType
228 do
229 return _module.class_by_name(once ("String".to_symbol)).get_type
230 end
231
232 # The primitive type Collection[Object]
233 meth type_collection: MMType
234 do
235 return _module.class_by_name(once ("Collection".to_symbol)).get_type
236 end
237
238 # The primitive type Array[?]
239 meth type_array(stype: MMType): MMType
240 do
241 return _module.class_by_name(once ("Array".to_symbol)).get_instantiate_type([stype])
242 end
243
244 # The primitive type Discrete
245 meth type_discrete: MMType
246 do
247 return _module.class_by_name(once ("Discrete".to_symbol)).get_type
248 end
249
250 # The primitive type Range[?]
251 meth type_range(stype: MMType): MMType
252 do
253 return _module.class_by_name(once ("Range".to_symbol)).get_instantiate_type([stype])
254 end
255
256 # The primitive type of null
257 meth type_none: MMType
258 do
259 return _module.type_none
260 end
261
262 # The current module
263 readable writable attr _module: MMSrcModule
264
265 # The current class
266 readable writable attr _local_class: MMSrcLocalClass
267
268 # The current property
269 readable writable attr _local_property: MMConcreteProperty
270
271 # The current tool configuration/status
272 readable attr _tc: ToolContext
273
274 # Display an error for a given syntax node
275 meth error(n: PNode, s: String)
276 do
277 _tc.error("{n.locate}: {s}")
278 end
279
280 # Display a warning for a given syntax node
281 meth warning(n: PNode, s: String)
282 do
283 _tc.warning("{n.locate}: {s}")
284 end
285
286 # Check conformity and display error
287 meth check_conform(n: PNode, subtype: MMType, stype: MMType): Bool
288 do
289 if stype == null or subtype == null then
290 return false
291 end
292 if subtype < stype then
293 return true
294 end
295 error(n, "Type error: expected {stype}, got {subtype}")
296 return false
297 end
298
299
300 protected init(tc: ToolContext, module: MMSrcModule)
301 do
302 _tc = tc
303 _module = module
304 end
305 end
306
307 ###############################################################################
308
309 redef class PNode
310 protected meth accept_abs_syntax_visitor(v: AbsSyntaxVisitor) do visit_all(v)
311 end
312
313 redef class Token
314 attr _symbol: Symbol
315
316 # Symbol associated with the text
317 # Lazily computed
318 meth to_symbol: Symbol
319 do
320 var s = _symbol
321 if s == null then
322 s = text.to_symbol
323 _symbol = s
324 end
325 return s
326 end
327 end
328
329 redef class PClassdef
330 # Associated class (MM entity)
331 meth local_class: MMSrcLocalClass is abstract
332 end
333
334 redef class AAttrPropdef
335 # Associated attribute (MM entity)
336 meth prop: MMSrcAttribute is abstract
337
338 # Associated read accessor (MM entity)
339 meth readmethod: MMSrcMethod is abstract
340
341 # Associated write accessor (MM entity)
342 meth writemethod: MMSrcMethod is abstract
343 end
344
345 redef class AMethPropdef
346 # Associated method (MM entity)
347 meth method: MMMethSrcMethod is abstract
348 end
349
350 redef class ATypePropdef
351 # Associated formal type (MM entity)
352 meth prop: MMSrcTypeProperty is abstract
353 end
354
355 redef class PParam
356 # Position in the signature
357 meth position: Int is abstract
358
359 # Associated local variable
360 meth variable: Variable is abstract
361 end
362
363 redef class PType
364 # Retrieve the local class corresponding to the type.
365 # Display an error and return null if there is no class
366 # Display an error and return null if the type is not class based (formal one)
367 meth get_local_class(v: AbsSyntaxVisitor): MMLocalClass is abstract
368
369 # Retrieve corresponding static type.
370 # Display an error and return null if there is a problem
371 meth get_stype(v: AbsSyntaxVisitor): MMType is abstract
372
373 # Retrieve corresponding static type.
374 # Display an error and return null if there is a problem
375 # But do not performs any subtype check.
376 # get_unchecked_stype should be called to check that the static type is fully valid
377 meth get_unchecked_stype(v: AbsSyntaxVisitor): MMType is abstract
378
379 # Check that a static definition type is conform with regard to formal types
380 # Useful with get_unchecked_stype
381 # Remember that conformance check need that ancestors are totaly computed
382 meth check_conform(v: AbsSyntaxVisitor) is abstract
383 end
384
385 redef class AType
386 attr _stype_cache: MMType
387 attr _stype_cached: Bool
388
389 redef meth get_local_class(v)
390 do
391 var name = n_id.to_symbol
392 var mod = v.module
393 var cla = v.local_class
394
395 if (cla.formal_dict != null and cla.formal_dict.has_key(name)) or (cla.global_properties != null and cla.has_global_property_by_name(name)) then
396 v.error(n_id, "Type error: {name} is a formal type")
397 _stype_cached = true
398 return null
399 end
400
401 if not mod.has_global_class_named(name) then
402 v.error(n_id, "Type error: class {name} not found in module {mod}.")
403 _stype_cached = true
404 return null
405 end
406
407 var local_class = mod.class_by_name(name)
408 local_class.global.check_visibility(v, self, mod)
409 return local_class
410 end
411
412 redef meth get_unchecked_stype(v)
413 do
414 if _stype_cached then return _stype_cache
415 _stype_cached = true
416
417 var name = n_id.to_symbol
418 var mod = v.module
419 var cla = v.local_class
420
421 if cla.formal_dict.has_key(name) then
422 if n_types.length > 0 then
423 v.error(self, "Type error: formal type {name} cannot have formal parameters.")
424 return null
425 end
426 var formal = cla.formal_dict[name]
427 _stype_cache = formal
428 return formal
429 end
430
431 if cla.global_properties != null and cla.has_global_property_by_name(name) then
432 if n_types.length > 0 then
433 v.error(self, "Type error: formal type {name} cannot have formal parameters.")
434 return null
435 end
436 var t = cla.get_type.select_virtual_type(name).stype
437 if t == null then
438 v.error(self, "Type error: circular definition in formal type {name}.")
439 return null
440 end
441 _stype_cache = t
442 return t
443 end
444
445 var local_class = get_local_class(v)
446 if local_class == null then return null
447
448 var arity = n_types.length
449 if local_class.arity != arity then
450 v.error(self, "Type error: '{local_class}' has {local_class.arity} parameters which differs from the {arity} params.")
451 return null
452 end
453
454 if arity > 0 then
455 var tab = new Array[MMType]
456 for p in n_types do
457 tab.add(p.get_unchecked_stype(v))
458 end
459 var t = local_class.get_instantiate_type(tab)
460 _stype_cache = t
461 return t
462 else
463 var t = local_class.get_type
464 _stype_cache = t
465 return t
466 end
467 end
468
469 redef meth get_stype(v)
470 do
471 var t = get_unchecked_stype(v)
472 if t != null then check_conform(v)
473 return t
474 end
475
476 redef meth check_conform(v)
477 do
478 var st = get_unchecked_stype(v)
479 if st == null then return
480 var local_class = st.local_class
481 var arity = n_types.length
482 if arity > 0 then
483 for i in [0..arity[ do
484 var p = n_types[i]
485 var pt = p.get_stype(v)
486 var bt = local_class.get_formal(i).bound
487 if bt == null then return
488 bt = bt.adapt_to(st) # We need to abapt because of F-genericity
489 v.check_conform(p, pt, bt)
490 end
491 end
492 end
493 end
494
495 redef class PExpr
496 # Static type
497 # Is null for statement and for erronus expression
498 meth stype: MMType is abstract
499 end