First NIT release and new clean mercurial repository
[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 redef meth inherit_to(t)
32 do
33 return new MMImplicitType(self, t)
34 end
35
36 # Cached result of stype
37 attr _stype_cache: MMVirtualType
38
39 # The virtual static type associated
40 meth stype: MMVirtualType
41 do
42 # If the signature is not build: Circular definition
43 if signature == null then return null
44
45 var r = _stype_cache
46 if r == null then
47 r = new MMVirtualType(self)
48 _stype_cache = r
49 end
50 return r
51 end
52 end
53
54 redef class MMType
55 # Select a virtual type property by its name
56 meth select_virtual_type(name: Symbol): MMTypeProperty
57 do
58 assert local_class != null
59 assert name != null
60 var res = select_property(local_class.virtual_type(name))
61 assert res isa MMTypeProperty
62 return res
63 end
64 end
65
66 class MMVirtualType
67 special MMTypeFormal
68 # The property associed
69 readable attr _property: MMTypeProperty
70
71 protected init(p: MMTypeProperty)
72 do
73 super(p.name, p.signature.return_type)
74 _property = p
75 end
76
77 redef meth for_module(mod)
78 do
79 var recv = _property.signature.recv.for_module(mod)
80 return adapt_to(recv)
81 end
82
83 redef meth not_for_self
84 do
85 return bound.not_for_self
86 end
87
88 redef meth adapt_to(recv)
89 do
90 # print "adapt {self} from {_property.signature.recv.module}::{_property.signature.recv} to {recv.module}::{recv}"
91 var prop = recv.select_property(_property.global)
92 assert prop isa MMTypeProperty
93 return prop.stype
94 end
95 end
96
97 redef class MMLocalClass
98 meth virtual_type(s: Symbol): MMGlobalProperty
99 do
100 var prop = get_property_by_name(s)
101 if prop.is_virtual_type then
102 return prop
103 end
104 return null
105 end
106 end
107
108 class MMImplicitType
109 special MMTypeProperty
110 special MMImplicitProperty
111 init(p, t) do super
112 end