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