d60e2a30c967e5eb19a59af408b228ab2f4d1de6
[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 # Do we inherit for a constructor?
75 var skip = true
76 for cd in mclassdef.in_hierarchy.direct_greaters do
77 if cd.mclass.kind.need_init then skip = false
78 end
79 if skip then return
80
81 # Now we search for the absence of any explicit super-init invocation
82 # * via a "super"
83 # * via a call of an other init
84 var nblock = self.n_block
85 if nblock != null then
86 var v = new AutoSuperInitVisitor
87 v.enter_visit(nblock)
88 var anode = v.has_explicit_super_init
89 if anode != null then
90 if nosuper != null then modelbuilder.error(anode, "Error: method is annotated nosuper but a constructor call is present")
91 return
92 end
93 if v.is_broken then return # skip
94 end
95
96 if nosuper != null then return
97
98 # Still here? So it means that we must add an implicit super-call on redefinitions.
99 if not mpropdef.is_intro then
100 auto_super_call = true
101 mpropdef.has_supercall = true
102 modelbuilder.toolcontext.info("Auto-super call for {mpropdef}", 4)
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 modelbuilder.toolcontext.info("Old-style auto-super init for {mpropdef} to {candidate.full_name}", 4)
138 end
139
140 # No old style? The look for new-style super constructors (called from a old style constructor)
141 var the_root_init_mmethod = modelbuilder.the_root_init_mmethod
142 if the_root_init_mmethod != null and auto_super_inits.is_empty then
143 var candidatedefs = the_root_init_mmethod.lookup_definitions(mmodule, anchor)
144
145 # Search the longest-one and checks for conflict
146 var candidatedef = candidatedefs.first
147 if candidatedefs.length > 1 then
148 # Check for conflict in the order of initializers
149 # Each initializer list must me a prefix of the longest list
150 # part 1. find the longest list
151 for spd in candidatedefs do
152 if spd.initializers.length > candidatedef.initializers.length then candidatedef = spd
153 end
154 # compare
155 for spd in candidatedefs do
156 var i = 0
157 for p in spd.initializers do
158 if p != candidatedef.initializers[i] then
159 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!")
160 return
161 end
162 i += 1
163 end
164 end
165 end
166
167 var msignature = candidatedef.new_msignature or else candidatedef.msignature
168 msignature = msignature.resolve_for(recvtype, anchor, mmodule, true)
169
170 var callsite = new CallSite(self, recvtype, mmodule, anchor, true, the_root_init_mmethod, candidatedef, msignature, false)
171 auto_super_inits.add(callsite)
172 modelbuilder.toolcontext.info("Auto-super init for {mpropdef} to {the_root_init_mmethod.full_name}", 4)
173 end
174 if auto_super_inits.is_empty then
175 modelbuilder.error(self, "Error: No constructors to call implicitely in {mpropdef}. Call one explicitely.")
176 return
177 end
178
179 # Can the super-constructors be called?
180 for auto_super_init in auto_super_inits do
181 var auto_super_init_def = auto_super_init.mpropdef
182 var msig = mpropdef.msignature.as(not null)
183 var supermsig = auto_super_init.msignature
184 if supermsig.arity > msig.arity then
185 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}.")
186 continue
187 end
188 var i = 0
189 for sp in supermsig.mparameters do
190 var p = msig.mparameters[i]
191 var sub = p.mtype
192 var sup = sp.mtype
193 if not sub.is_subtype(mmodule, anchor, sup) then
194 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}.")
195 break
196 end
197 i += 1
198 end
199 end
200 self.auto_super_inits = auto_super_inits
201 end
202
203 end
204
205 redef class ANode
206 private fun accept_auto_super_init(v: AutoSuperInitVisitor) do end
207 end
208
209
210 redef class ASendExpr
211 redef fun accept_auto_super_init(v)
212 do
213 var callsite = self.callsite
214 if callsite == null then
215 v.is_broken = true
216 return
217 end
218 if callsite.mproperty.is_init then
219 v.has_explicit_super_init = self
220 end
221 end
222 end
223
224 redef class ASuperExpr
225 redef fun accept_auto_super_init(v)
226 do
227 # If the super is a standard call-next-method then there it is considered am explicit super init call
228 # The the super is a "super int" then it is also an explicit super init call
229 v.has_explicit_super_init = self
230 end
231 end