syntax: move varctx initialization to the PClassdef
authorJean Privat <jean@pryen.org>
Fri, 3 Jul 2009 21:34:11 +0000 (17:34 -0400)
committerJean Privat <jean@pryen.org>
Sat, 4 Jul 2009 00:56:19 +0000 (20:56 -0400)
variable 'self' can now be accessed (even implicitly) in default
attribute definition.

A test base_attr_init_val3 is added.

Signed-off-by: Jean Privat <jean@pryen.org>

src/syntax/typing.nit
tests/base_attr_init_val3.nit [new file with mode: 0644]
tests/sav/base_attr_init_val3.sav [new file with mode: 0644]

index 7730152..738fa7a 100644 (file)
@@ -151,6 +151,8 @@ end
 redef class PClassdef
        redef fun accept_typing(v)
        do
+               v.variable_ctx = new RootVariableContext(v, self)
+               v.base_variable_ctx = v.variable_ctx
                v.self_var = new ParamVariable("self".to_symbol, self)
                v.self_var.stype = local_class.get_type
                super
@@ -160,10 +162,13 @@ end
 redef class AAttrPropdef
        redef fun accept_typing(v)
        do
+               var old_var_ctx = v.variable_ctx
+               v.variable_ctx = old_var_ctx.sub(self)
                super
                if n_expr != null then
                        v.check_conform_expr(n_expr.as(not null), prop.signature.return_type.as(not null))
                end
+               v.variable_ctx = old_var_ctx
        end
 end
 
@@ -172,15 +177,16 @@ redef class AMethPropdef
        var _self_var: nullable ParamVariable
        redef fun accept_typing(v)
        do
-               v.variable_ctx = new RootVariableContext(v, self)
-               v.base_variable_ctx = v.variable_ctx
+               var old_var_ctx = v.variable_ctx
+               v.variable_ctx = old_var_ctx.sub(self)
                _self_var = v.self_var
                super
+               v.variable_ctx = old_var_ctx
        end
 end
 
 redef class AConcreteMethPropdef
-       redef fun accept_typing(v)
+       redef fun after_typing(v)
        do
                super
                if v.variable_ctx.unreash == false and method.signature.return_type != null then
diff --git a/tests/base_attr_init_val3.nit b/tests/base_attr_init_val3.nit
new file mode 100644 (file)
index 0000000..a01ae58
--- /dev/null
@@ -0,0 +1,32 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2009 Jean Privat <jean@pryen.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
+       readable var _i: Int = foo
+       fun foo: Int do return 1
+       init do end
+end
+
+class B
+       readable var _a: A = bar
+       fun bar: A do return new A
+       init do end
+end
+
+(new A).i.output
+(new B).a.i.output
diff --git a/tests/sav/base_attr_init_val3.sav b/tests/sav/base_attr_init_val3.sav
new file mode 100644 (file)
index 0000000..6ed281c
--- /dev/null
@@ -0,0 +1,2 @@
+1
+1