src: create groups for related things
[nit.git] / src / semantize / auto_super_init.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2012 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 # Computing of super-constructors that must be implicitely called at the begin of constructors.
18 # The current rules are a bit crazy but whatever.
19 module auto_super_init
20
21 import typing
22 private import annotation
23
24 redef class ToolContext
25 var auto_super_init_phase: Phase = new AutoSuperInitPhase(self, [typing_phase])
26 end
27
28 private class AutoSuperInitPhase
29 super Phase
30 redef fun process_npropdef(npropdef) do if npropdef isa AMethPropdef then npropdef.do_auto_super_init(toolcontext.modelbuilder)
31 end
32
33 private class AutoSuperInitVisitor
34 super Visitor
35 init
36 do
37 end
38
39 redef fun visit(n)
40 do
41 n.accept_auto_super_init(self)
42 n.visit_all(self)
43 end
44
45 var has_explicit_super_init: nullable ANode = null
46 end
47
48
49 redef class AMethPropdef
50 # In case of introduced constructor, the list of implicit auto super init constructors invoked (if needed)
51 var auto_super_inits: nullable Array[CallSite] = null
52
53 # In case of redefined constructors, is an implicit call-to-super required?
54 var auto_super_call = false
55
56 fun do_auto_super_init(modelbuilder: ModelBuilder)
57 do
58 var mclassdef = self.parent.as(AClassdef).mclassdef.as(not null)
59 var mpropdef = self.mpropdef.as(not null)
60 var mmodule = mpropdef.mclassdef.mmodule
61 var anchor = mclassdef.bound_mtype
62 var recvtype = mclassdef.mclass.mclass_type
63
64 # Get the annotation, but check its pertinence before returning
65 var nosuper = get_single_annotation("nosuper", modelbuilder)
66
67 # Collect only for constructors
68 if not mpropdef.mproperty.is_init then
69 if nosuper != null then modelbuilder.error(nosuper, "Error: nosuper only in `init`")
70 return
71 end
72
73 # FIXME: THIS IS STUPID (be here to keep the old code working)
74 if not mpropdef.mclassdef.is_intro then return
75
76 # Do we inherit for a constructor?
77 var skip = true
78 for cd in mclassdef.in_hierarchy.direct_greaters do
79 if cd.mclass.kind.need_init then skip = false
80 end
81 if skip then return
82
83 # Now we search for the absence of any explicit super-init invocation
84 # * via a "super"
85 # * via a call of an other init
86 var nblock = self.n_block
87 if nblock != null then
88 var v = new AutoSuperInitVisitor
89 v.enter_visit(nblock)
90 var anode = v.has_explicit_super_init
91 if anode != null then
92 if nosuper != null then modelbuilder.error(anode, "Error: method is annotated nosuper but a constructor call is present")
93 return
94 end
95 end
96
97 if nosuper != null then return
98
99 # Still here? So it means that we must add an implicit super-call on redefinitions.
100 if not mpropdef.is_intro then
101 auto_super_call = true
102 mpropdef.has_supercall = true
103 return
104 end
105
106 # Still here? So it means that we must determine what super inits need to be automatically invoked
107 # The code that follow is required to deal with complex cases with old-style and new-style inits
108
109 # Look for old-style super constructors
110 var auto_super_inits = new Array[CallSite]
111 for msupertype in mclassdef.supertypes do
112 # FIXME: the order is quite arbitrary
113 if not msupertype.mclass.kind.need_init then continue
114 msupertype = msupertype.anchor_to(mmodule, mclassdef.bound_mtype)
115 var candidate = modelbuilder.try_get_mproperty_by_name2(self, mmodule, msupertype, mpropdef.mproperty.name)
116 if candidate == null then
117 candidate = modelbuilder.try_get_mproperty_by_name2(self, mmodule, msupertype, "init")
118 end
119 if candidate == null then
120 modelbuilder.error(self, "Error: Cannot do an implicit constructor call in {mpropdef}; there is no constructor named {mpropdef.mproperty.name} in {msupertype}.")
121 return
122 end
123 assert candidate isa MMethod
124
125 # Skip new-style init
126 if candidate.is_root_init then continue
127
128 var candidatedefs = candidate.lookup_definitions(mmodule, anchor)
129 var candidatedef = candidatedefs.first
130 # TODO, we drop the others propdefs in the callsite, that is not great :(
131
132 var msignature = candidatedef.new_msignature or else candidatedef.msignature
133 msignature = msignature.resolve_for(recvtype, anchor, mmodule, true)
134
135 var callsite = new CallSite(self, recvtype, mmodule, anchor, true, candidate, candidatedef, msignature, false)
136 auto_super_inits.add(callsite)
137 end
138
139 # No old style? The look for new-style super constructors (called from a old style constructor)
140 var the_root_init_mmethod = modelbuilder.the_root_init_mmethod
141 if the_root_init_mmethod != null and auto_super_inits.is_empty then
142 var candidatedefs = the_root_init_mmethod.lookup_definitions(mmodule, anchor)
143
144 # Search the longest-one and checks for conflict
145 var candidatedef = candidatedefs.first
146 if candidatedefs.length > 1 then
147 # Check for conflict in the order of initializers
148 # Each initializer list must me a prefix of the longest list
149 # part 1. find the longest list
150 for spd in candidatedefs do
151 if spd.initializers.length > candidatedef.initializers.length then candidatedef = spd
152 end
153 # compare
154 for spd in candidatedefs do
155 var i = 0
156 for p in spd.initializers do
157 if p != candidatedef.initializers[i] then
158 modelbuilder.error(self, "Error: Cannot do an implicit constructor call to comflicting for inherited inits {spd}({spd.initializers.join(", ")}) and {candidatedef}({candidatedef.initializers.join(", ")}). NOTE: Do not mix old-style and new-style init!")
159 return
160 end
161 i += 1
162 end
163 end
164 end
165
166 var msignature = candidatedef.new_msignature or else candidatedef.msignature
167 msignature = msignature.resolve_for(recvtype, anchor, mmodule, true)
168
169 var callsite = new CallSite(self, recvtype, mmodule, anchor, true, the_root_init_mmethod, candidatedef, msignature, false)
170 auto_super_inits.add(callsite)
171 end
172 if auto_super_inits.is_empty then
173 modelbuilder.error(self, "Error: No constructors to call implicitely in {mpropdef}. Call one explicitely.")
174 return
175 end
176
177 # Can the super-constructors be called?
178 for auto_super_init in auto_super_inits do
179 var auto_super_init_def = auto_super_init.mpropdef
180 var msig = mpropdef.msignature.as(not null)
181 var supermsig = auto_super_init.msignature
182 if supermsig.arity > msig.arity then
183 modelbuilder.error(self, "Error: Cannot do an implicit constructor call to {auto_super_init_def}{supermsig}. Expected at least {supermsig.arity} arguments, got {msig.arity}.")
184 continue
185 end
186 var i = 0
187 for sp in supermsig.mparameters do
188 var p = msig.mparameters[i]
189 var sub = p.mtype
190 var sup = sp.mtype
191 if not sub.is_subtype(mmodule, anchor, sup) then
192 modelbuilder.error(self, "Error: Cannot do an implicit constructor call to {auto_super_init_def}{supermsig}. Expected argument #{i} of type {sp.mtype}, got implicit argument {p.name} of type {p.mtype}.")
193 break
194 end
195 i += 1
196 end
197 end
198 self.auto_super_inits = auto_super_inits
199 end
200
201 end
202
203 redef class ANode
204 private fun accept_auto_super_init(v: AutoSuperInitVisitor) do end
205 end
206
207
208 redef class ASendExpr
209 redef fun accept_auto_super_init(v)
210 do
211 var mproperty = self.callsite.mproperty
212 if mproperty == null then return
213 if mproperty.is_init then
214 v.has_explicit_super_init = self
215 end
216 end
217 end
218
219 redef class ASuperExpr
220 redef fun accept_auto_super_init(v)
221 do
222 # If the super is a standard call-next-method then there it is considered am explicit super init call
223 # The the super is a "super int" then it is also an explicit super init call
224 v.has_explicit_super_init = self
225 end
226 end