various phases: more robust for keep-going
[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
60 if mclassdef == null then return # skip error
61 var mpropdef = self.mpropdef
62 if mpropdef == null then return # skip error
63 var mmodule = mpropdef.mclassdef.mmodule
64 var anchor = mclassdef.bound_mtype
65 var recvtype = mclassdef.mclass.mclass_type
66
67 # Get the annotation, but check its pertinence before returning
68 var nosuper = get_single_annotation("nosuper", modelbuilder)
69
70 # Collect only for constructors
71 if not mpropdef.mproperty.is_init or mpropdef.mproperty.is_new then
72 if nosuper != null then modelbuilder.error(nosuper, "Error: nosuper only in `init`")
73 return
74 end
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 if v.is_broken then return # skip
96 end
97
98 if nosuper != null then return
99
100 # Still here? So it means that we must add an implicit super-call on redefinitions.
101 if not mpropdef.is_intro then
102 auto_super_call = true
103 mpropdef.has_supercall = true
104 modelbuilder.toolcontext.info("Auto-super call for {mpropdef}", 4)
105 return
106 end
107
108 # Still here? So it means that we must determine what super inits need to be automatically invoked
109 # The code that follow is required to deal with complex cases with old-style and new-style inits
110
111 # Look for old-style super constructors
112 var auto_super_inits = new Array[CallSite]
113 for msupertype in mclassdef.supertypes do
114 # FIXME: the order is quite arbitrary
115 if not msupertype.mclass.kind.need_init then continue
116 msupertype = msupertype.anchor_to(mmodule, mclassdef.bound_mtype)
117 var candidate = modelbuilder.try_get_mproperty_by_name2(self, mmodule, msupertype, mpropdef.mproperty.name)
118 if candidate == null then
119 candidate = modelbuilder.try_get_mproperty_by_name2(self, mmodule, msupertype, "init")
120 end
121 if candidate == null then
122 modelbuilder.error(self, "Error: Cannot do an implicit constructor call in {mpropdef}; there is no constructor named {mpropdef.mproperty.name} in {msupertype}.")
123 return
124 end
125 assert candidate isa MMethod
126
127 # Skip new-style init
128 if candidate.is_root_init then continue
129
130 var candidatedefs = candidate.lookup_definitions(mmodule, anchor)
131 var candidatedef = candidatedefs.first
132 # TODO, we drop the others propdefs in the callsite, that is not great :(
133
134 var msignature = candidatedef.new_msignature or else candidatedef.msignature
135 msignature = msignature.resolve_for(recvtype, anchor, mmodule, true)
136
137 var callsite = new CallSite(self, recvtype, mmodule, anchor, true, candidate, candidatedef, msignature, false)
138 auto_super_inits.add(callsite)
139 modelbuilder.toolcontext.info("Old-style auto-super init for {mpropdef} to {candidate.full_name}", 4)
140 end
141
142 # No old style? The look for new-style super constructors (called from a old style constructor)
143 var the_root_init_mmethod = modelbuilder.the_root_init_mmethod
144 if the_root_init_mmethod != null and auto_super_inits.is_empty then
145 var candidatedefs = the_root_init_mmethod.lookup_definitions(mmodule, anchor)
146
147 # Search the longest-one and checks for conflict
148 var candidatedef = candidatedefs.first
149 if candidatedefs.length > 1 then
150 # Check for conflict in the order of initializers
151 # Each initializer list must me a prefix of the longest list
152 # part 1. find the longest list
153 for spd in candidatedefs do
154 if spd.initializers.length > candidatedef.initializers.length then candidatedef = spd
155 end
156 # compare
157 for spd in candidatedefs do
158 var i = 0
159 for p in spd.initializers do
160 if p != candidatedef.initializers[i] then
161 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!")
162 return
163 end
164 i += 1
165 end
166 end
167 end
168
169 var msignature = candidatedef.new_msignature or else candidatedef.msignature
170 msignature = msignature.resolve_for(recvtype, anchor, mmodule, true)
171
172 var callsite = new CallSite(self, recvtype, mmodule, anchor, true, the_root_init_mmethod, candidatedef, msignature, false)
173 auto_super_inits.add(callsite)
174 modelbuilder.toolcontext.info("Auto-super init for {mpropdef} to {the_root_init_mmethod.full_name}", 4)
175 end
176 if auto_super_inits.is_empty then
177 modelbuilder.error(self, "Error: No constructors to call implicitely in {mpropdef}. Call one explicitely.")
178 return
179 end
180
181 # Can the super-constructors be called?
182 for auto_super_init in auto_super_inits do
183 var auto_super_init_def = auto_super_init.mpropdef
184 var msig = mpropdef.msignature.as(not null)
185 var supermsig = auto_super_init.msignature
186 if supermsig.arity > msig.arity then
187 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}.")
188 continue
189 end
190 var i = 0
191 for sp in supermsig.mparameters do
192 var p = msig.mparameters[i]
193 var sub = p.mtype
194 var sup = sp.mtype
195 if not sub.is_subtype(mmodule, anchor, sup) then
196 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}.")
197 break
198 end
199 i += 1
200 end
201 end
202 self.auto_super_inits = auto_super_inits
203 end
204
205 end
206
207 redef class ANode
208 private fun accept_auto_super_init(v: AutoSuperInitVisitor) do end
209 end
210
211
212 redef class ASendExpr
213 redef fun accept_auto_super_init(v)
214 do
215 var callsite = self.callsite
216 if callsite == null then
217 v.is_broken = true
218 return
219 end
220 if callsite.mproperty.is_init then
221 v.has_explicit_super_init = self
222 end
223 end
224 end
225
226 redef class ASuperExpr
227 redef fun accept_auto_super_init(v)
228 do
229 # If the super is a standard call-next-method then there it is considered am explicit super init call
230 # The the super is a "super int" then it is also an explicit super init call
231 v.has_explicit_super_init = self
232 end
233 end