1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2012 Jean Privat <jean@pryen.org>
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
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
25 redef class ToolContext
26 var auto_super_init_phase
: Phase = new AutoSuperInitPhase(self, [typing_phase
])
29 private class AutoSuperInitPhase
31 redef fun process_npropdef
(npropdef
) do if npropdef
isa AMethPropdef then npropdef
.do_auto_super_init
(toolcontext
.modelbuilder
)
34 private class AutoSuperInitVisitor
42 n
.accept_auto_super_init
(self)
46 var has_explicit_super_init
: nullable ANode = null
50 redef class AMethPropdef
51 # In case of introduced constructor, the list of implicit auto super init constructors invoked (if needed)
52 var auto_super_inits
: nullable Array[CallSite] = null
54 # In case of redefined constructors, is an implicit call-to-super required?
55 var auto_super_call
= false
57 fun do_auto_super_init
(modelbuilder
: ModelBuilder)
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
65 # Get the annotation, but check its pertinence before returning
66 var nosuper
= get_single_annotation
("nosuper", modelbuilder
)
68 # Collect only for constructors
69 if not mpropdef
.mproperty
.is_init
then
70 if nosuper
!= null then modelbuilder
.error
(nosuper
, "Error: nosuper only in `init`")
74 # FIXME: THIS IS STUPID (be here to keep the old code working)
75 if not mpropdef
.mclassdef
.is_intro
then return
77 # Do we inherit for a constructor?
79 for cd
in mclassdef
.in_hierarchy
.direct_greaters
do
80 if cd
.mclass
.kind
.need_init
then skip
= false
84 # Now we search for the absence of any explicit super-init invocation
86 # * via a call of an other init
87 var nblock
= self.n_block
88 if nblock
!= null then
89 var v
= new AutoSuperInitVisitor
91 var anode
= v
.has_explicit_super_init
93 if nosuper
!= null then modelbuilder
.error
(anode
, "Error: method is annotated nosuper but a constructor call is present")
98 if nosuper
!= null then return
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
107 # Still here? So it means that we must determine what super inits need to be automatically invoked
109 var auto_super_inits
= new Array[CallSite]
110 for msupertype
in mclassdef
.supertypes
do
111 # FIXME: the order is quite arbitrary
112 if not msupertype
.mclass
.kind
.need_init
then continue
113 msupertype
= msupertype
.anchor_to
(mmodule
, mclassdef
.bound_mtype
)
114 var candidate
= modelbuilder
.try_get_mproperty_by_name2
(self, mmodule
, msupertype
, mpropdef
.mproperty
.name
)
115 if candidate
== null then
116 candidate
= modelbuilder
.try_get_mproperty_by_name2
(self, mmodule
, msupertype
, "init")
118 if candidate
== null then
119 modelbuilder
.error
(self, "Error: Cannot do an implicit constructor call in {mpropdef}; there is no constructor named {mpropdef.mproperty.name} in {msupertype}.")
122 assert candidate
isa MMethod
124 var candidatedefs
= candidate
.lookup_definitions
(mmodule
, anchor
)
125 var candidatedef
= candidatedefs
.first
126 # TODO, we drop the others propdefs in the callsite, that is not great :(
128 var msignature
= candidatedef
.msignature
129 msignature
= msignature
.resolve_for
(recvtype
, anchor
, mmodule
, true)
131 var callsite
= new CallSite(self, recvtype
, mmodule
, anchor
, true, candidate
, candidatedef
, msignature
, false)
132 auto_super_inits
.add
(callsite
)
134 if auto_super_inits
.is_empty
then
135 modelbuilder
.error
(self, "Error: No constructors to call implicitely in {mpropdef}. Call one explicitely.")
138 for auto_super_init
in auto_super_inits
do
139 var auto_super_init_def
= auto_super_init
.mpropdef
140 var msig
= mpropdef
.msignature
.as(not null)
141 var supermsig
= auto_super_init
.msignature
142 if supermsig
.arity
> msig
.arity
then
143 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}.")
147 for sp
in supermsig
.mparameters
do
148 var p
= msig
.mparameters
[i
]
151 if not sub
.is_subtype
(mmodule
, anchor
, sup
) then
152 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}.")
158 self.auto_super_inits
= auto_super_inits
164 private fun accept_auto_super_init
(v
: AutoSuperInitVisitor) do end
168 redef class ASendExpr
169 redef fun accept_auto_super_init
(v
)
171 var mproperty
= self.callsite
.mproperty
172 if mproperty
== null then return
173 if mproperty
.is_init
then
174 v
.has_explicit_super_init
= self
179 redef class ASuperExpr
180 redef fun accept_auto_super_init
(v
)
182 # If the super is a standard call-next-method then there it is considered am explicit super init call
183 # The the super is a "super int" then it is also an explicit super init call
184 v
.has_explicit_super_init
= self