end
if npropdef isa AAttrPropdef then
if npropdef.mpropdef == null then return # Skip broken attribute
- var at = npropdef.get_single_annotation("noinit", self)
- if at != null then
- npropdef.noinit = true
- if npropdef.has_value then
- self.error(at, "Error: `noinit` attributes cannot have an initial value")
- end
- continue # Skip noinit attributes
+ if npropdef.noinit then continue # Skip noinit attribute
+ var atautoinit = npropdef.get_single_annotation("autoinit", self)
+ if atautoinit != null then
+ # For autoinit attributes, call the reader to force
+ # the lazy initialization of the attribute.
+ initializers.add(npropdef.mreadpropdef.mproperty)
+ continue
end
if npropdef.has_value then continue
var paramname = npropdef.mpropdef.mproperty.name.substring_from(1)
# Can self be used as a root init?
- private fun look_like_a_root_init(modelbuilder: ModelBuilder): Bool
+ private fun look_like_a_root_init(modelbuilder: ModelBuilder, mclassdef: MClassDef): Bool
do
# Need the `init` keyword
if n_kwinit == null then return false
# Need to by anonymous
if self.n_methid != null then return false
- # No parameters
- if self.n_signature.n_params.length > 0 then return false
- # Cannot be private or something
- if not self.n_visibility isa APublicVisibility then return false
# No annotation on itself
if get_single_annotation("old_style_init", modelbuilder) != null then return false
# Nor on its module
var old = amoddecl.get_single_annotation("old_style_init", modelbuilder)
if old != null then return false
end
+ # No parameters
+ if self.n_signature.n_params.length > 0 then
+ modelbuilder.advice(self, "old-init", "Warning: init with signature in {mclassdef}")
+ return false
+ end
+ # Cannot be private or something
+ if not self.n_visibility isa APublicVisibility then
+ modelbuilder.advice(self, "old-init", "Warning: non-public init in {mclassdef}")
+ return false
+ end
return true
end
end
end
+ var look_like_a_root_init = look_like_a_root_init(modelbuilder, mclassdef)
var mprop: nullable MMethod = null
if not is_init or n_kwredef != null then mprop = modelbuilder.try_get_mproperty_by_name(name_node, mclassdef, name).as(nullable MMethod)
- if mprop == null and look_like_a_root_init(modelbuilder) then
+ if mprop == null and look_like_a_root_init then
mprop = modelbuilder.the_root_init_mmethod
var nb = n_block
if nb isa ABlockExpr and nb.n_expr.is_empty and n_doc == null then
if mprop == null then
var mvisibility = new_property_visibility(modelbuilder, mclassdef, self.n_visibility)
mprop = new MMethod(mclassdef, name, mvisibility)
- if look_like_a_root_init(modelbuilder) and modelbuilder.the_root_init_mmethod == null then
+ if look_like_a_root_init and modelbuilder.the_root_init_mmethod == null then
modelbuilder.the_root_init_mmethod = mprop
mprop.is_root_init = true
end
self.mpropdef = mpropdef
modelbuilder.mpropdef2npropdef[mpropdef] = self
if mpropdef.is_intro then
- modelbuilder.toolcontext.info("{mpropdef} introduces new method {mprop.full_name}", 3)
+ modelbuilder.toolcontext.info("{mpropdef} introduces new method {mprop.full_name}", 4)
else
- modelbuilder.toolcontext.info("{mpropdef} redefines method {mprop.full_name}", 3)
+ modelbuilder.toolcontext.info("{mpropdef} redefines method {mprop.full_name}", 4)
end
end
has_value = n_expr != null or n_block != null
+ var atnoinit = self.get_single_annotation("noinit", modelbuilder)
+ if atnoinit != null then
+ noinit = true
+ if has_value then
+ modelbuilder.error(atnoinit, "Error: `noinit` attributes cannot have an initial value")
+ return
+ end
+ end
+
var atlazy = self.get_single_annotation("lazy", modelbuilder)
- if atlazy != null then
+ var atautoinit = self.get_single_annotation("autoinit", modelbuilder)
+ if atlazy != null or atautoinit != null then
+ if atlazy != null and atautoinit != null then
+ modelbuilder.error(atlazy, "Error: lazy incompatible with autoinit")
+ return
+ end
if not has_value then
- modelbuilder.error(atlazy, "Error: a lazy attribute needs a value")
+ if atlazy != null then
+ modelbuilder.error(atlazy, "Error: a lazy attribute needs a value")
+ else if atautoinit != null then
+ modelbuilder.error(atautoinit, "Error: a autoinit attribute needs a value")
+ end
+ return
end
is_lazy = true
var mlazyprop = new MAttribute(mclassdef, "lazy _" + name, none_visibility)