src: Update init
[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 or mclassdef.is_broken then return # skip error
61 var mpropdef = self.mpropdef
62 if mpropdef == null or mpropdef.is_broken 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 allowed in `init`.")
73 return
74 end
75
76 # Now we search for the absence of any explicit super-init invocation
77 # * via a "super"
78 # * via a call of an other init
79 var nblock = self.n_block
80 if nblock != null then
81 var v = new AutoSuperInitVisitor
82 v.enter_visit(nblock)
83 var anode = v.has_explicit_super_init
84 if anode != null then
85 if nosuper != null then modelbuilder.error(anode, "Error: method is annotated `nosuper` but a super-constructor call is present.")
86 return
87 end
88 if v.is_broken then return # skip
89 end
90
91 if nosuper != null then return
92
93 # Still here? So it means that we must add an implicit super-call on redefinitions.
94 if not mpropdef.is_intro then
95 auto_super_call = true
96 mpropdef.has_supercall = true
97 modelbuilder.toolcontext.info("Auto-super call for {mpropdef}", 4)
98 return
99 end
100
101 # Still here? So it means that we must determine what super inits need to be automatically invoked
102 # The code that follow is required to deal with complex cases with old-style and new-style inits
103
104 var auto_super_inits = new Array[CallSite]
105
106 # The look for new-style super constructors (called from a old style constructor)
107 var candidatedefs = get_super_candidatedefs(modelbuilder)
108
109 if not candidatedefs.is_empty and auto_super_inits.is_empty then
110
111 var candidatedef = candidatedefs.first
112 if candidatedefs.length > 1 then
113 var cd2 = candidatedefs[1]
114 modelbuilder.error(self, "Error: cannot do an implicit constructor call to conflicting inherited inits `{cd2}({cd2.initializers.join(", ")}`) and `{candidatedef}({candidatedef.initializers.join(", ")}`). NOTE: Do not mix old-style and new-style init!")
115 is_broken = true
116 return
117 end
118
119 var msignature = candidatedef.msignature
120 msignature = msignature.resolve_for(recvtype, anchor, mmodule, true)
121
122 if msignature.arity > mpropdef.msignature.arity then
123 modelbuilder.error(self, "Error: cannot do an implicit constructor call to `{candidatedef}{msignature}`. Expected at least `{msignature.arity}` arguments.")
124 is_broken = true
125 return
126 end
127
128 if candidatedef.mproperty != mpropdef.mproperty then
129 var i = 0
130 for candidat_mparameter in msignature.mparameters do
131 var actual_mparameter = mpropdef.msignature.mparameters[i]
132 if not candidat_mparameter.mtype.is_subtype(mmodule, anchor, actual_mparameter.mtype) then
133 modelbuilder.error(self, "Type Error: expected argument #{i} of type `{candidat_mparameter.mtype}`, got implicit argument `{candidat_mparameter.name}` of type `{actual_mparameter.mtype}`. Signature is {msignature}")
134 return
135 end
136 i += 1
137 end
138 end
139
140 var callsite = new CallSite(hot_location, recvtype, mmodule, anchor, true, candidatedef.mproperty, candidatedef, msignature, false)
141 auto_super_inits.add(callsite)
142 modelbuilder.toolcontext.info("Auto-super init for {mpropdef} to {candidatedef.full_name}", 4)
143 else if candidatedefs.is_empty then
144 # skip broken
145 is_broken = true
146 return
147 end
148 if auto_super_inits.is_empty then
149 modelbuilder.error(self, "Error: no constructors to call implicitly in `{mpropdef}`. Call one explicitly.")
150 return
151 end
152
153 self.auto_super_inits = auto_super_inits
154 end
155
156 # This method returns the list of possible candidates for the current definition.
157 #
158 # Warning this method supports super call from old_style_init to default_inits without signature verification!!!
159 private fun get_super_candidatedefs(modelbuilder: ModelBuilder): Array[MMethodDef]
160 do
161 var candidatedefs = new Array[MMethodDef]
162
163 var mclassdef = self.parent.as(AClassdef).mclassdef
164 if mclassdef == null or mclassdef.is_broken then return candidatedefs # skip error
165 var mpropdef = self.mpropdef
166 if mpropdef == null or mpropdef.is_broken then return candidatedefs # skip error
167 var mmodule = mpropdef.mclassdef.mmodule
168 var anchor = mclassdef.bound_mtype
169 var mproperty = mpropdef.mproperty
170
171 # The look for new-style super constructors (called from a old style constructor)
172 var the_root_init_mmethod = modelbuilder.the_root_init_mmethod
173
174 if mpropdef.is_old_style_init then
175 var superprop: nullable MMethodDef = null
176 for mclass in mclassdef.mclass.in_hierarchy(mmodule).direct_greaters do
177 candidatedefs.add(mclass.intro.default_init.as(not null))
178 end
179 else
180 candidatedefs = the_root_init_mmethod.lookup_definitions(mmodule, anchor)
181 end
182 return candidatedefs
183 end
184 end
185
186 redef class ANode
187 private fun accept_auto_super_init(v: AutoSuperInitVisitor) do end
188 end
189
190
191 redef class ASendExpr
192 redef fun accept_auto_super_init(v)
193 do
194 var callsite = self.callsite
195 if callsite == null then
196 v.is_broken = true
197 return
198 end
199 if callsite.mproperty.is_init then
200 v.has_explicit_super_init = self
201 end
202 end
203 end
204
205 redef class ASuperExpr
206 redef fun accept_auto_super_init(v)
207 do
208 # If the super is a standard call-next-method then there it is considered am explicit super init call
209 # The the super is a "super int" then it is also an explicit super init call
210 v.has_explicit_super_init = self
211 end
212 end