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