Merge: Abstract attribute
authorJean Privat <jean@pryen.org>
Tue, 10 Mar 2015 13:51:12 +0000 (20:51 +0700)
committerJean Privat <jean@pryen.org>
Tue, 10 Mar 2015 13:51:12 +0000 (20:51 +0700)
Add the annotation `abstract` on attributes.

It is just a syntactic sugar to define a couple of abstract getter-setters with a shared documentation without an associated slot in the instance so it can be used in interfaces.

~~~nit
interface Foo
var a: Object is abstract
end

class Bar
super Foo
         # A concrete attribute that redefine the abstract one
redef var a
end

class Baz
super Foo
var real_a: Object
         # A pair of concrete methods that redefine the abstract attribute
redef fun a do return real_a
redef fun a=(x) do real_a = x
end
~~~

The visibility rules are unchanged with regard to concrete attributes, so by default the writer is private.

Needed cleaning (and a bugfix) are included in the PR

Pull-Request: #1177
Reviewed-by: Lucas Bajolet <r4pass@hotmail.com>
Reviewed-by: Alexandre Terrasa <alexandre@moz-code.org>
Reviewed-by: Alexis Laferrière <alexis.laf@xymus.net>

1  2 
src/compiler/abstract_compiler.nit
src/modelize/modelize_property.nit
src/rapid_type_analysis.nit

@@@ -1443,7 -1443,7 +1443,7 @@@ abstract class AbstractCompilerVisito
                var name = self.get_name("varonce")
                self.add_decl("static {mtype.ctype} {name};")
                var res = self.new_var(mtype)
 -              self.add("if ({name}) \{")
 +              self.add("if (likely({name}!=NULL)) \{")
                self.add("{res} = {name};")
                self.add("\} else \{")
                var native_mtype = self.get_class("NativeString").mclass_type
@@@ -1834,6 -1834,15 +1834,15 @@@ redef class MMethodDe
                var modelbuilder = v.compiler.modelbuilder
                var val = constant_value
                var node = modelbuilder.mpropdef2node(self)
+               if is_abstract then
+                       var cn = v.class_name_string(arguments.first)
+                       v.current_node = node
+                       v.add("PRINT_ERROR(\"Runtime error: Abstract method `%s` called on `%s`\", \"{mproperty.name.escape_to_c}\", {cn});")
+                       v.add_raw_abort
+                       return null
+               end
                if node isa APropdef then
                        var oldnode = v.current_node
                        v.current_node = node
  redef class AMethPropdef
        redef fun compile_to_c(v, mpropdef, arguments)
        do
-               if mpropdef.is_abstract then
-                       var cn = v.class_name_string(arguments.first)
-                       v.add("PRINT_ERROR(\"Runtime error: Abstract method `%s` called on `%s`\", \"{mpropdef.mproperty.name.escape_to_c}\", {cn});")
-                       v.add_raw_abort
-                       return
-               end
                # Call the implicit super-init
                var auto_super_inits = self.auto_super_inits
                if auto_super_inits != null then
@@@ -2304,7 -2306,7 +2306,7 @@@ redef class AAttrPropde
  
        fun init_expr(v: AbstractCompilerVisitor, recv: RuntimeVariable)
        do
 -              if has_value and not is_lazy then evaluate_expr(v, recv)
 +              if has_value and not is_lazy and not n_expr isa ANullExpr then evaluate_expr(v, recv)
        end
  
        # Evaluate, store and return the default value of the attribute
@@@ -2823,7 -2825,7 +2825,7 @@@ redef class AOnceExp
                v.add_decl("static {mtype.ctype} {name};")
                v.add_decl("static int {guard};")
                var res = v.new_var(mtype)
 -              v.add("if ({guard}) \{")
 +              v.add("if (likely({guard})) \{")
                v.add("{res} = {name};")
                v.add("\} else \{")
                var i = v.expr(self.n_expr, mtype)
@@@ -863,10 -863,6 +863,10 @@@ redef class AMethPropde
                mpropdef.is_abstract = self.get_single_annotation("abstract", modelbuilder) != null
                mpropdef.is_intern = self.get_single_annotation("intern", modelbuilder) != null
                mpropdef.is_extern = self.n_extern_code_block != null or self.get_single_annotation("extern", modelbuilder) != null
 +
 +              # Check annotations
 +              var at = self.get_single_annotation("lazy", modelbuilder)
 +              if at != null then modelbuilder.error(at, "Syntax error: `lazy` must be used on attributes.")
        end
  
        redef fun check_signature(modelbuilder)
@@@ -955,32 -951,31 +955,31 @@@ redef class AAttrPropde
        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
                        var mvisibility = new_property_visibility(modelbuilder, mclassdef, self.n_visibility)
                        mreadprop = new MMethod(mclassdef, readname, mvisibility)
                        if not self.check_redef_keyword(modelbuilder, mclassdef, n_kwredef, false, mreadprop) then return
-                       mreadprop.deprecation = mprop.deprecation
                else
                        if not self.check_redef_keyword(modelbuilder, mclassdef, n_kwredef, true, mreadprop) then return
                        check_redef_property_visibility(modelbuilder, self.n_visibility, mreadprop)
                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
                                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)
                        end
                        mwriteprop = new MMethod(mclassdef, writename, mvisibility)
                        if not self.check_redef_keyword(modelbuilder, mclassdef, nwkwredef, false, mwriteprop) then return
-                       mwriteprop.deprecation = mprop.deprecation
+                       mwriteprop.deprecation = mreadprop.deprecation
                else
                        if not self.check_redef_keyword(modelbuilder, mclassdef, nwkwredef or else n_kwredef, true, mwriteprop) then return
                        if atwritable != null then
                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)
        do
+               var mreadpropdef = self.mreadpropdef
                var mpropdef = self.mpropdef
-               if mpropdef == null then return # Error thus skipped
-               var mclassdef = mpropdef.mclassdef
+               if mreadpropdef == null then return # Error thus skipped
+               var mclassdef = mreadpropdef.mclassdef
                var mmodule = mclassdef.mmodule
                var mtype: nullable MType = null
  
-               var mreadpropdef = self.mreadpropdef
  
                var ntype = self.n_type
                if ntype != null then
  
                var inherited_type: nullable MType = null
                # Inherit the type from the getter (usually an abstract getter)
-               if mreadpropdef != null and not mreadpropdef.is_intro then
+               if not mreadpropdef.is_intro then
                        var msignature = mreadpropdef.mproperty.intro.msignature
                        if msignature == null then return # Error, thus skipped
                        inherited_type = msignature.return_mtype
                                        var cla = modelbuilder.try_get_mclass_by_name(nexpr, mmodule, "String")
                                        if cla != null then mtype = cla.mclass_type
                                else
-                                       modelbuilder.error(self, "Error: Untyped attribute {mpropdef}. Implicit typing allowed only for literals and new.")
+                                       modelbuilder.error(self, "Error: Untyped attribute {mreadpropdef}. Implicit typing allowed only for literals and new.")
                                end
  
                                if mtype == null then return
                end
  
                if mtype == null then
-                       modelbuilder.error(self, "Error: Untyped attribute {mpropdef}")
+                       modelbuilder.error(self, "Error: Untyped attribute {mreadpropdef}")
                        return
                end
  
-               mpropdef.static_mtype = mtype
+               if mpropdef != null then
+                       mpropdef.static_mtype = mtype
+               end
  
-               if mreadpropdef != null then
+               do
                        var msignature = new MSignature(new Array[MParameter], mtype)
                        mreadpropdef.msignature = msignature
                end
@@@ -36,13 -36,6 +36,13 @@@ redef class ModelBuilde
        do
                var analysis = new RapidTypeAnalysis(self, mainmodule)
                analysis.run_analysis
 +
 +              if toolcontext.opt_log.value then
 +                      var basename = toolcontext.log_directory / mainmodule.name
 +                      analysis.live_methods_to_tree.write_to_file(basename + ".rta_methods.txt")
 +                      analysis.live_types_to_csv.write_to_file(basename + ".rta_types.csv")
 +              end
 +
                return analysis
        end
  end
@@@ -389,7 -382,6 +389,6 @@@ class RapidTypeAnalysi
                if mproperty.mpropdefs.length <= 1 then return
                # If all definitions of a method are live, we can remove the definition of the totry set
                for d in mproperty.mpropdefs do
-                       if d.is_abstract then continue
                        if not live_methoddefs.has(d) then return
                end
                #print "full property: {mpropdef.mproperty} for {mpropdef.mproperty.mpropdefs.length} definitions"