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