report error when using `this' or `base' access outside of instance
authorJuerg Billeter <j@bitron.ch>
Thu, 17 Apr 2008 20:01:36 +0000 (20:01 +0000)
committerJürg Billeter <juergbi@src.gnome.org>
Thu, 17 Apr 2008 20:01:36 +0000 (20:01 +0000)
2008-04-17  Juerg Billeter  <j@bitron.ch>

* vala/valasemanticanalyzer.vala: report error when using `this' or
  `base' access outside of instance methods

svn path=/trunk/; revision=1252

ChangeLog
vala/valasemanticanalyzer.vala

index bba03bd..4d94003 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2008-04-17  Jürg Billeter  <j@bitron.ch>
+
+       * vala/valasemanticanalyzer.vala: report error when using `this' or
+         `base' access outside of instance methods
+
 2008-04-16  Marc-Andre Lureau  <marcandre.lureau@gmail.com>
 
        * vapi/glib-2.0.vapi (EnumClass, FlagsClass, FlagsValue): complete
index c5a199d..e75effc 100644 (file)
@@ -1482,6 +1482,14 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
                expr.symbol_reference = null;
 
                if (expr.inner == null) {
+                       if (expr.member_name == "this") {
+                               if (!is_in_instance_method ()) {
+                                       expr.error = true;
+                                       Report.error (expr.source_reference, "This access invalid outside of instance methods");
+                                       return;
+                               }
+                       }
+
                        base_symbol = current_symbol;
 
                        var sym = current_symbol;
@@ -2138,7 +2146,33 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
                }
        }
 
+       private bool is_in_instance_method () {
+               var sym = current_symbol;
+               while (sym != null) {
+                       if (sym is CreationMethod) {
+                               return true;
+                       } else if (sym is Method) {
+                               var m = (Method) sym;
+                               return m.instance;
+                       } else if (sym is Constructor) {
+                               var c = (Constructor) sym;
+                               return c.instance;
+                       } else if (sym is Property) {
+                               return true;
+                       }
+                       sym = sym.parent_symbol;
+               }
+
+               return false;
+       }
+
        public override void visit_base_access (BaseAccess expr) {
+               if (!is_in_instance_method ()) {
+                       expr.error = true;
+                       Report.error (expr.source_reference, "Base access invalid outside of instance methods");
+                       return;
+               }
+
                if (current_class == null) {
                        if (current_struct == null) {
                                expr.error = true;
@@ -2152,6 +2186,10 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
                        Iterator<DataType> base_type_it = current_struct.get_base_types ().iterator ();
                        base_type_it.next ();
                        expr.static_type = base_type_it.get ();
+               } else if (current_class.base_class == null) {
+                       expr.error = true;
+                       Report.error (expr.source_reference, "Base access invalid without base class");
+                       return;
                } else {
                        expr.static_type = new ClassType (current_class.base_class);
                }