From dfaf91b7d9de7d1b51d9f9b74eeebf0f8b462c55 Mon Sep 17 00:00:00 2001 From: Jean Privat Date: Thu, 26 Feb 2015 20:01:11 +0700 Subject: [PATCH] modelize: new annotation `abstract` on attribute that just generate abstract accessors Signed-off-by: Jean Privat --- src/modelize/modelize_property.nit | 51 ++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 19 deletions(-) diff --git a/src/modelize/modelize_property.nit b/src/modelize/modelize_property.nit index 29ab5cd..7c6ea5d 100644 --- a/src/modelize/modelize_property.nit +++ b/src/modelize/modelize_property.nit @@ -951,25 +951,25 @@ redef class AAttrPropdef redef fun build_property(modelbuilder, mclassdef) do var mclass = mclassdef.mclass + var nid2 = n_id2 + var name = nid2.text + + var atabstract = self.get_single_annotation("abstract", modelbuilder) + if atabstract == null then + if mclass.kind == interface_kind then + modelbuilder.error(self, "Error: Attempt to define attribute {name} in the interface {mclass}.") + else if mclass.kind == enum_kind then + modelbuilder.error(self, "Error: Attempt to define attribute {name} in the enum class {mclass}.") + else if mclass.kind == extern_kind then + modelbuilder.error(self, "Error: Attempt to define attribute {name} in the extern class {mclass}.") + end - var name: String - name = self.n_id2.text - - if mclass.kind == interface_kind or mclassdef.mclass.kind == enum_kind then - modelbuilder.error(self, "Error: Attempt to define attribute {name} in the interface {mclass}.") - else if mclass.kind == enum_kind then - modelbuilder.error(self, "Error: Attempt to define attribute {name} in the enum class {mclass}.") - else if mclass.kind == extern_kind then - modelbuilder.error(self, "Error: Attempt to define attribute {name} in the extern class {mclass}.") + var mprop = new MAttribute(mclassdef, "_" + name, private_visibility) + var mpropdef = new MAttributeDef(mclassdef, mprop, self.location) + self.mpropdef = mpropdef + modelbuilder.mpropdef2npropdef[mpropdef] = self end - # New attribute style - var nid2 = self.n_id2 - var mprop = new MAttribute(mclassdef, "_" + name, private_visibility) - var mpropdef = new MAttributeDef(mclassdef, mprop, self.location) - self.mpropdef = mpropdef - modelbuilder.mpropdef2npropdef[mpropdef] = self - var readname = name var mreadprop = modelbuilder.try_get_mproperty_by_name(nid2, mclassdef, readname).as(nullable MMethod) if mreadprop == null then @@ -986,10 +986,16 @@ redef class AAttrPropdef self.mreadpropdef = mreadpropdef modelbuilder.mpropdef2npropdef[mreadpropdef] = self set_doc(mreadpropdef, modelbuilder) - mpropdef.mdoc = mreadpropdef.mdoc + if mpropdef != null then mpropdef.mdoc = mreadpropdef.mdoc + if atabstract != null then mreadpropdef.is_abstract = true has_value = n_expr != null or n_block != null + if atabstract != null and has_value then + modelbuilder.error(atabstract, "Error: `abstract` attributes cannot have an initial value") + return + end + var atnoinit = self.get_single_annotation("noinit", modelbuilder) if atnoinit == null then atnoinit = self.get_single_annotation("noautoinit", modelbuilder) if atnoinit != null then @@ -998,6 +1004,10 @@ redef class AAttrPropdef modelbuilder.error(atnoinit, "Error: `noautoinit` attributes cannot have an initial value") return end + if atabstract != null then + modelbuilder.error(atnoinit, "Error: `noautoinit` attributes cannot be abstract") + return + end end var atlazy = self.get_single_annotation("lazy", modelbuilder) @@ -1061,7 +1071,8 @@ redef class AAttrPropdef var mwritepropdef = new MMethodDef(mclassdef, mwriteprop, self.location) self.mwritepropdef = mwritepropdef modelbuilder.mpropdef2npropdef[mwritepropdef] = self - mwritepropdef.mdoc = mpropdef.mdoc + mwritepropdef.mdoc = mreadpropdef.mdoc + if atabstract != null then mwritepropdef.is_abstract = true end redef fun build_signature(modelbuilder) @@ -1136,7 +1147,9 @@ redef class AAttrPropdef return end - mpropdef.static_mtype = mtype + if mpropdef != null then + mpropdef.static_mtype = mtype + end do var msignature = new MSignature(new Array[MParameter], mtype) -- 1.7.9.5