start removing implicit properties
[nit.git] / src / metamodel / virtualtype.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2006-2008 Floréal Morandat <morandat@lirmm.fr>
4 # Copyright 2008 Jean Privat <jean@pryen.org>
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 # Virtual type (property of type type)
19 package virtualtype
20
21 import type_formal
22
23 redef class MMGlobalProperty
24 # Is self a virtual type
25 meth is_virtual_type: Bool do return intro isa MMTypeProperty
26 end
27
28 # Virtual type properties
29 class MMTypeProperty
30 special MMLocalProperty
31 # The virtual static type associated
32 meth stype_for(recv: MMType): MMVirtualType
33 do
34 var prop = recv.select_property(global)
35 assert prop isa MMTypeProperty
36 return prop.real_stype_for(recv)
37 end
38
39 # Cached results of stype
40 attr _stypes_cache: HashMap[MMType, MMVirtualType] = new HashMap[MMType, MMVirtualType]
41
42 private meth real_stype_for(recv: MMType): MMVirtualType
43 do
44 # If the signature is not build: Circular definition
45 if signature == null then return null
46
47 if _stypes_cache.has_key(recv) then return _stypes_cache[recv]
48
49 var res = new MMVirtualType(self, recv)
50 _stypes_cache[recv] = res
51
52 return res
53 end
54 end
55
56 redef class MMType
57 # Select a virtual type property by its name
58 meth select_virtual_type(name: Symbol): MMTypeProperty
59 do
60 assert local_class != null
61 assert name != null
62 var res = select_property(local_class.virtual_type(name))
63 assert res isa MMTypeProperty
64 return res
65 end
66 end
67
68 class MMVirtualType
69 special MMTypeFormal
70 # The property associed
71 readable attr _property: MMTypeProperty
72
73 # The receiver type
74 readable attr _recv: MMType
75
76 protected init(p: MMTypeProperty, recv: MMType)
77 do
78 super(p.name, p.signature_for(recv).return_type)
79 _property = p
80 _recv = recv
81 end
82
83 redef meth module do return _recv.module
84
85 redef meth for_module(mod)
86 do
87 if mod == module then return self
88 return adapt_to(recv.for_module(mod))
89 end
90
91 redef meth not_for_self
92 do
93 return bound.not_for_self
94 end
95
96 redef meth adapt_to(recv)
97 do
98 return property.stype_for(recv)
99 end
100 end
101
102 redef class MMLocalClass
103 meth virtual_type(s: Symbol): MMGlobalProperty
104 do
105 var prop = get_property_by_name(s)
106 if prop.is_virtual_type then
107 return prop
108 end
109 return null
110 end
111 end