Merge: add attribute annotation `noinit` to skip the attribute in inits
authorJean Privat <jean@pryen.org>
Tue, 22 Jul 2014 03:16:56 +0000 (23:16 -0400)
committerJean Privat <jean@pryen.org>
Tue, 22 Jul 2014 03:16:56 +0000 (23:16 -0400)
~~~.rb
class Toto
   var tata: Int is noinit
   var tutu: String
end
var t = new Toto("hello") # only tutu in the constructor
~~~

Pull-Request: #600
Reviewed-by: Lucas Bajolet <r4pass@hotmail.com>
Reviewed-by: Alexandre Terrasa <alexandre@moz-code.org>

src/abstract_compiler.nit
src/annotation.nit
src/modelize_property.nit
src/naive_interpreter.nit
tests/base_init_noinit.nit [new file with mode: 0644]
tests/sav/base_init_noinit.res [new file with mode: 0644]
tests/sav/base_init_noinit_alt1.res [new file with mode: 0644]
tests/sav/base_init_noinit_alt2.res [new file with mode: 0644]
tests/sav/base_init_noinit_alt3.res [new file with mode: 0644]
tests/sav/base_init_noinit_alt4.res [new file with mode: 0644]
tests/sav/base_init_noinit_alt5.res [new file with mode: 0644]

index a2c08de..8015a5a 100644 (file)
@@ -2178,7 +2178,7 @@ redef class AClassdef
                        var i = 1
                        # Collect undefined attributes
                        for npropdef in self.n_propdefs do
-                               if npropdef isa AAttrPropdef and npropdef.n_expr == null then
+                               if npropdef isa AAttrPropdef and npropdef.n_expr == null and not npropdef.noinit then
                                        v.write_attribute(npropdef.mpropdef.mproperty, recv, arguments[i])
                                        i += 1
                                end
index 4daafef..d4a13ac 100644 (file)
@@ -19,6 +19,37 @@ import parser
 import modelbuilder
 import literal
 
+redef class Prod
+       super ANode
+
+       # Try to get its single annotation with a given name
+       # If there is no such an annotation, null is returned.
+       # If there is more than one annotation, a error message is printed and the first annotation is returned
+       fun get_single_annotation(name: String, modelbuilder: ModelBuilder): nullable AAnnotation
+       do
+               var res = get_annotations(name)
+               if res.is_empty then return null
+               if res.length > 1 then
+                       modelbuilder.error(res[1], "Error: multiple annotation `{name}`. A previous one is defined line {res[0].location.line_start}")
+               end
+               return res.first
+       end
+
+       # Return all its annotations of a given name in the order of their declaration
+       # Retun an empty array if no such an annotation.
+       fun get_annotations(name: String): Array[AAnnotation]
+       do
+               var res = new Array[AAnnotation]
+               var nas = n_annotations
+               if nas == null then return res
+               for na in nas.n_items do
+                       if na.name != name then continue
+                       res.add(na)
+               end
+               return res
+       end
+end
+
 redef class AAnnotation
        # The name of the annotation
        fun name: String
index 938a3a0..04e022f 100644 (file)
@@ -18,6 +18,7 @@
 module modelize_property
 
 import modelize_class
+import annotation
 
 redef class ToolContext
        var modelize_property_phase: Phase = new ModelizePropertyPhase(self, [modelize_class_phase])
@@ -118,8 +119,17 @@ redef class ModelBuilder
                var mparameters = new Array[MParameter]
                var anode: nullable ANode = null
                for npropdef in nclassdef.n_propdefs do
-                       if npropdef isa AAttrPropdef and npropdef.n_expr == null then
+                       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.n_expr != null then
+                                               self.error(at, "Error: `noinit` attributes cannot have an initial value")
+                                       end
+                                       continue # Skip noinit attributes
+                               end
+                               if npropdef.n_expr != null then continue
                                var paramname = npropdef.mpropdef.mproperty.name.substring_from(1)
                                var ret_type = npropdef.mpropdef.static_mtype
                                if ret_type == null then return
@@ -632,6 +642,9 @@ end
 redef class AAttrPropdef
        redef type MPROPDEF: MAttributeDef
 
+       # Is the node tagged `noinit`?
+       var noinit = false
+
        # The associated getter (read accessor) if any
        var mreadpropdef: nullable MMethodDef writable
        # The associated setter (write accessor) if any
index fc6cbfc..ba2fb39 100644 (file)
@@ -1031,7 +1031,7 @@ redef class AClassdef
                var i = 1
                # Collect undefined attributes
                for npropdef in self.n_propdefs do
-                       if npropdef isa AAttrPropdef and npropdef.n_expr == null then
+                       if npropdef isa AAttrPropdef and not npropdef.noinit and npropdef.n_expr == null then
                                v.write_attribute(npropdef.mpropdef.mproperty, recv, args[i])
                                i += 1
                        end
diff --git a/tests/base_init_noinit.nit b/tests/base_init_noinit.nit
new file mode 100644 (file)
index 0000000..87de46e
--- /dev/null
@@ -0,0 +1,31 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import kernel
+
+class A
+       var x: Object is noinit #alt1,3# var x: Object
+       var y: Object is noinit #alt2,3# var y: Object
+       fun work
+       do
+               if isset _x then x.output
+               if isset _y then y.output
+               0.output
+       end
+       #alt5#var z: Object = 5 is noinit
+end
+
+var a: A
+a = new A#alt1#a = new A(2)#alt2#a = new A(30)#alt3#a = new A(4, 40)#alt4#a=new A(5)
+a.work
diff --git a/tests/sav/base_init_noinit.res b/tests/sav/base_init_noinit.res
new file mode 100644 (file)
index 0000000..573541a
--- /dev/null
@@ -0,0 +1 @@
+0
diff --git a/tests/sav/base_init_noinit_alt1.res b/tests/sav/base_init_noinit_alt1.res
new file mode 100644 (file)
index 0000000..389e262
--- /dev/null
@@ -0,0 +1,2 @@
+2
+0
diff --git a/tests/sav/base_init_noinit_alt2.res b/tests/sav/base_init_noinit_alt2.res
new file mode 100644 (file)
index 0000000..f4b9eda
--- /dev/null
@@ -0,0 +1,2 @@
+30
+0
diff --git a/tests/sav/base_init_noinit_alt3.res b/tests/sav/base_init_noinit_alt3.res
new file mode 100644 (file)
index 0000000..84cd911
--- /dev/null
@@ -0,0 +1,3 @@
+4
+40
+0
diff --git a/tests/sav/base_init_noinit_alt4.res b/tests/sav/base_init_noinit_alt4.res
new file mode 100644 (file)
index 0000000..0e9d1ef
--- /dev/null
@@ -0,0 +1 @@
+alt/base_init_noinit_alt4.nit:30,3--10: Error: Incorrect number of parameters. Got 1, expected 0. Signature is 
diff --git a/tests/sav/base_init_noinit_alt5.res b/tests/sav/base_init_noinit_alt5.res
new file mode 100644 (file)
index 0000000..b79ac75
--- /dev/null
@@ -0,0 +1 @@
+alt/base_init_noinit_alt5.nit:26,23--28: Error: `noinit` attributes cannot have an initial value