(build_ivar_reference): Warn when a class method
authorTom Wood <wood@gnu.org>
Sat, 1 May 1993 10:48:31 +0000 (10:48 +0000)
committerTom Wood <wood@gnu.org>
Sat, 1 May 1993 10:48:31 +0000 (10:48 +0000)
refers to an instance variable.

From-SVN: r4294

gcc/objc/objc-act.c

index 6a973b6278a91c36c4a69ff419ea3053ae2082e8..0968f1e3bbdd007cfa5c3f664ef5b37b277ae234 100644 (file)
@@ -4739,7 +4739,20 @@ build_ivar_reference (id)
      tree id;
 {
   if (TREE_CODE (method_context) == CLASS_METHOD_DECL)
-    TREE_TYPE (self_decl) = instance_type; /* cast */
+    {
+      /* Historically, a class method that produced objects (factory
+        method) would assign `self' to the instance that it
+        allocated.  This would effectively turn the class method into
+        an instance method.  Following this assignment, the instance
+        variables could be accessed.  That practice, while safe,
+        violates the simple rule that a class method should not refer
+        to an instance variable.  It's better to catch the cases
+        where this is done unknowingly than to support the above
+        paradigm.  */
+      warning ("instance variable `%s' accessed in class method",
+              IDENTIFIER_POINTER (id));
+      TREE_TYPE (self_decl) = instance_type; /* cast */
+    }
 
   return build_component_ref (build_indirect_ref (self_decl, "->"), id);
 }