[cfe-commits] r42883 - in /cfe/trunk: AST/Decl.cpp Driver/ASTConsumers.cpp Parse/ParseObjc.cpp Sema/Sema.h Sema/SemaDecl.cpp include/clang/AST/Decl.h include/clang/AST/DeclObjC.h include/clang/Basic/DiagnosticKinds.def include/clang/Parse/Action.h test/Parser/objc-alias-printing.m test/Sema/alias-test-1.m

Fariborz Jahanian fjahanian at apple.com
Thu Oct 11 16:42:27 PDT 2007


Author: fjahanian
Date: Thu Oct 11 18:42:27 2007
New Revision: 42883

URL: http://llvm.org/viewvc/llvm-project?rev=42883&view=rev
Log:
This patch implementa objective-c's @compatibilty-alias declaration.

Added:
    cfe/trunk/test/Parser/objc-alias-printing.m
    cfe/trunk/test/Sema/alias-test-1.m
Modified:
    cfe/trunk/AST/Decl.cpp
    cfe/trunk/Driver/ASTConsumers.cpp
    cfe/trunk/Parse/ParseObjc.cpp
    cfe/trunk/Sema/Sema.h
    cfe/trunk/Sema/SemaDecl.cpp
    cfe/trunk/include/clang/AST/Decl.h
    cfe/trunk/include/clang/AST/DeclObjC.h
    cfe/trunk/include/clang/Basic/DiagnosticKinds.def
    cfe/trunk/include/clang/Parse/Action.h

Modified: cfe/trunk/AST/Decl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/AST/Decl.cpp?rev=42883&r1=42882&r2=42883&view=diff

==============================================================================
--- cfe/trunk/AST/Decl.cpp (original)
+++ cfe/trunk/AST/Decl.cpp Thu Oct 11 18:42:27 2007
@@ -35,6 +35,7 @@
 static unsigned nIvarDecls = 0;
 static unsigned nObjcImplementationDecls = 0;
 static unsigned nObjcCategoryImpl = 0;
+static unsigned nObjcCompatibleAlias = 0;
 
 static bool StatSwitch = false;
 
@@ -141,6 +142,10 @@
 	  nObjcCategoryImpl, (int)sizeof(ObjcCategoryImplDecl),
 	  int(nObjcCategoryImpl*sizeof(ObjcCategoryImplDecl)));
 
+  fprintf(stderr, "    %d compatibility alias decls, %d each (%d bytes)\n", 
+	  nObjcCompatibleAlias, (int)sizeof(ObjcCompatibleAliasDecl),
+	  int(nObjcCompatibleAlias*sizeof(ObjcCompatibleAliasDecl)));
+  
   fprintf(stderr, "Total bytes = %d\n", 
 	  int(nFuncs*sizeof(FunctionDecl)+nBlockVars*sizeof(BlockVarDecl)+
 	      nFileVars*sizeof(FileVarDecl)+nParmVars*sizeof(ParmVarDecl)+
@@ -207,6 +212,9 @@
     case ObjcCategoryImpl:
       nObjcCategoryImpl++;
       break;
+    case CompatibleAlias:
+      nObjcCompatibleAlias++;
+      break;
   }
 }
 

Modified: cfe/trunk/Driver/ASTConsumers.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Driver/ASTConsumers.cpp?rev=42883&r1=42882&r2=42883&view=diff

==============================================================================
--- cfe/trunk/Driver/ASTConsumers.cpp (original)
+++ cfe/trunk/Driver/ASTConsumers.cpp Thu Oct 11 18:42:27 2007
@@ -117,6 +117,12 @@
   // FIXME: implement the rest...
 }
 
+static void PrintObjcCompatibleAliasDecl(ObjcCompatibleAliasDecl *AID) {
+  std::string A = AID->getName();
+  std::string I = AID->getClassInterface()->getName();
+  fprintf(stderr, "@compatibility_alias %s %s;\n", A.c_str(), I.c_str());
+}
+
 namespace {
   class ASTPrinter : public ASTConsumer {
     virtual void HandleTopLevelDecl(Decl *D) {
@@ -153,6 +159,9 @@
       } else if (ObjcCategoryDecl *OID = 
                  dyn_cast<ObjcCategoryDecl>(D)) {
         PrintObjcCategoryDecl(OID);
+      } else if (ObjcCompatibleAliasDecl *OID = 
+                 dyn_cast<ObjcCompatibleAliasDecl>(D)) {
+        PrintObjcCompatibleAliasDecl(OID);
       } else if (isa<ObjcClassDecl>(D)) {
         fprintf(stderr, "@class [printing todo]\n");
       } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {

Modified: cfe/trunk/Parse/ParseObjc.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Parse/ParseObjc.cpp?rev=42883&r1=42882&r2=42883&view=diff

==============================================================================
--- cfe/trunk/Parse/ParseObjc.cpp (original)
+++ cfe/trunk/Parse/ParseObjc.cpp Thu Oct 11 18:42:27 2007
@@ -923,15 +923,22 @@
     Diag(Tok, diag::err_expected_ident);
     return 0;
   }
-  ConsumeToken(); // consume alias-name
+  IdentifierInfo *aliasId = Tok.getIdentifierInfo();
+  SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
   if (Tok.isNot(tok::identifier)) {
     Diag(Tok, diag::err_expected_ident);
     return 0;
   }
-  ConsumeToken(); // consume class-name;
-  if (Tok.isNot(tok::semi))
+  IdentifierInfo *classId = Tok.getIdentifierInfo();
+  SourceLocation classLoc = ConsumeToken(); // consume class-name;
+  if (Tok.isNot(tok::semi)) {
     Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias");
-  return 0;
+    return 0;
+  }
+  DeclTy *ClsType = Actions.ActOnCompatiblityAlias(atLoc, 
+                                                   aliasId, aliasLoc,
+                                                   classId, classLoc);
+  return ClsType;
 }
 
 ///   property-synthesis:

Modified: cfe/trunk/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Sema/Sema.h?rev=42883&r1=42882&r2=42883&view=diff

==============================================================================
--- cfe/trunk/Sema/Sema.h (original)
+++ cfe/trunk/Sema/Sema.h Thu Oct 11 18:42:27 2007
@@ -416,6 +416,11 @@
                     IdentifierInfo *SuperName, SourceLocation SuperLoc,
                     IdentifierInfo **ProtocolNames, unsigned NumProtocols,
                     AttributeList *AttrList);
+  
+  virtual DeclTy *ActOnCompatiblityAlias(
+                    SourceLocation AtCompatibilityAliasLoc,
+                    IdentifierInfo *AliasName,  SourceLocation AliasLocation,
+                    IdentifierInfo *ClassName, SourceLocation ClassLocation);
                     
   virtual DeclTy *ActOnStartProtocolInterface(
 		    SourceLocation AtProtoInterfaceLoc,

Modified: cfe/trunk/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Sema/SemaDecl.cpp?rev=42883&r1=42882&r2=42883&view=diff

==============================================================================
--- cfe/trunk/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/Sema/SemaDecl.cpp Thu Oct 11 18:42:27 2007
@@ -31,6 +31,9 @@
   if (Decl *IIDecl = II.getFETokenInfo<Decl>())
     if (isa<TypedefDecl>(IIDecl) || isa<ObjcInterfaceDecl>(IIDecl))
       return IIDecl;
+    else if (ObjcCompatibleAliasDecl *ADecl = 
+               dyn_cast<ObjcCompatibleAliasDecl>(IIDecl))
+      return ADecl->getClassInterface();
   return 0;
 }
 
@@ -961,6 +964,49 @@
   return IDecl;
 }
 
+/// ActOnCompatiblityAlias - this action is called after complete parsing of
+/// @compaatibility_alias declaration. It sets up the alias relationships.
+Sema::DeclTy *Sema::ActOnCompatiblityAlias(
+                      SourceLocation AtCompatibilityAliasLoc,
+                      IdentifierInfo *AliasName,  SourceLocation AliasLocation,
+                      IdentifierInfo *ClassName, SourceLocation ClassLocation) {
+  // Look for previous declaration of alias name
+  ScopedDecl *ADecl = LookupScopedDecl(AliasName, Decl::IDNS_Ordinary,
+                                       AliasLocation, TUScope);
+  if (ADecl) {
+    if (isa<ObjcCompatibleAliasDecl>(ADecl)) {
+      Diag(AliasLocation, diag::warn_previous_alias_decl);
+      Diag(ADecl->getLocation(), diag::warn_previous_declaration);
+    }
+    else {
+      Diag(AliasLocation, diag::err_conflicting_aliasing_type,
+           AliasName->getName());
+      Diag(ADecl->getLocation(), diag::err_previous_declaration);
+    }
+    return 0;
+  }
+  // Check for class declaration
+  ScopedDecl *CDecl = LookupScopedDecl(ClassName, Decl::IDNS_Ordinary,
+                                       ClassLocation, TUScope);
+  if (!CDecl || !isa<ObjcInterfaceDecl>(CDecl)) {
+    Diag(ClassLocation, diag::warn_undef_interface,
+         ClassName->getName());
+    if (CDecl)
+      Diag(CDecl->getLocation(), diag::warn_previous_declaration);
+    return 0;
+  }
+  // Everything checked out, instantiate a new alias declaration ast
+  ObjcCompatibleAliasDecl *AliasDecl = 
+    new ObjcCompatibleAliasDecl(AtCompatibilityAliasLoc, 
+                                AliasName,
+                                dyn_cast<ObjcInterfaceDecl>(CDecl));
+    
+  // Chain & install the interface decl into the identifier.
+  AliasDecl->setNext(AliasName->getFETokenInfo<ScopedDecl>());
+  AliasName->setFETokenInfo(AliasDecl);
+  return AliasDecl;
+}
+
 Sema::DeclTy *Sema::ActOnStartProtocolInterface(
                 SourceLocation AtProtoInterfaceLoc,
                 IdentifierInfo *ProtocolName, SourceLocation ProtocolLoc,

Modified: cfe/trunk/include/clang/AST/Decl.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Decl.h?rev=42883&r1=42882&r2=42883&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/Decl.h (original)
+++ cfe/trunk/include/clang/AST/Decl.h Thu Oct 11 18:42:27 2007
@@ -44,6 +44,7 @@
            ObjcImplementation,
            ObjcProtocol,
     //     ScopedDecl
+             CompatibleAlias,
     //       TypeDecl
                ObjcInterface,
                Typedef,
@@ -68,7 +69,7 @@
     // of the class, to allow efficient classof.
     NamedFirst  = Field,         NamedLast  = ParmVar,
     FieldFirst  = Field,         FieldLast  = ObjcIvar,
-    ScopedFirst = ObjcInterface, ScopedLast = ParmVar,
+    ScopedFirst = CompatibleAlias, ScopedLast = ParmVar,
     TypeFirst   = ObjcInterface, TypeLast   = Class,
     TagFirst    = Enum         , TagLast    = Class,
     RecordFirst = Struct       , RecordLast = Class,
@@ -124,6 +125,7 @@
     case ParmVar:
     case EnumConstant:
     case ObjcInterface:
+    case CompatibleAlias:
       return IDNS_Ordinary;
     case Struct:
     case Union:

Modified: cfe/trunk/include/clang/AST/DeclObjC.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/DeclObjC.h?rev=42883&r1=42882&r2=42883&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/DeclObjC.h (original)
+++ cfe/trunk/include/clang/AST/DeclObjC.h Thu Oct 11 18:42:27 2007
@@ -591,7 +591,27 @@
   }
   static bool classof(const ObjcImplementationDecl *D) { return true; }
 };
+
+/// ObjcCompatibleAliasDecl - Represents alias of a class. This alias is 
+/// declared as @compatibility_alias alias class.
+class ObjcCompatibleAliasDecl : public ScopedDecl {
+  /// Class that this is an alias of.
+  ObjcInterfaceDecl *AliasedClass;
+  
+public:
+  ObjcCompatibleAliasDecl(SourceLocation L, IdentifierInfo *Id,
+                         ObjcInterfaceDecl* aliasedClass)
+  : ScopedDecl(CompatibleAlias, L, Id, 0),
+  AliasedClass(aliasedClass) {}
+  
+  ObjcInterfaceDecl *getClassInterface() const { return AliasedClass; }
   
+  static bool classof(const Decl *D) {
+    return D->getKind() == CompatibleAlias;
+  }
+  static bool classof(const ObjcCompatibleAliasDecl *D) { return true; }
+  
+};
 
 }  // end namespace clang
 #endif

Modified: cfe/trunk/include/clang/Basic/DiagnosticKinds.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticKinds.def?rev=42883&r1=42882&r2=42883&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticKinds.def (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticKinds.def Thu Oct 11 18:42:27 2007
@@ -446,7 +446,12 @@
      "cannot find protocol declaration for '%0'")
 DIAG(err_missing_id_definition, ERROR,
      "cannot find definition of 'id'")
-
+DIAG(warn_previous_alias_decl, WARNING,
+     "previously declared alias is ignored")
+DIAG(warn_previous_declaration, WARNING,
+     "previous declaration is here")
+DIAG(err_conflicting_aliasing_type, ERROR,
+     "conflicting types for alias %0'")
 
 //===----------------------------------------------------------------------===//
 // Semantic Analysis

Modified: cfe/trunk/include/clang/Parse/Action.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Parse/Action.h?rev=42883&r1=42882&r2=42883&view=diff

==============================================================================
--- cfe/trunk/include/clang/Parse/Action.h (original)
+++ cfe/trunk/include/clang/Parse/Action.h Thu Oct 11 18:42:27 2007
@@ -455,6 +455,16 @@
     AttributeList *AttrList) {
     return 0;
   }
+  
+  /// ActOnCompatiblityAlias - this action is called after complete parsing of
+  /// @compaatibility_alias declaration. It sets up the alias relationships.
+  virtual DeclTy *ActOnCompatiblityAlias(
+    SourceLocation AtCompatibilityAliasLoc,
+    IdentifierInfo *AliasName,  SourceLocation AliasLocation,
+    IdentifierInfo *ClassName, SourceLocation ClassLocation) {
+    return 0;
+  }
+  
   // ActOnStartProtocolInterface - this action is called immdiately after
   // parsing the prologue for a protocol interface.
   virtual DeclTy *ActOnStartProtocolInterface(

Added: cfe/trunk/test/Parser/objc-alias-printing.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Parser/objc-alias-printing.m?rev=42883&view=auto

==============================================================================
--- cfe/trunk/test/Parser/objc-alias-printing.m (added)
+++ cfe/trunk/test/Parser/objc-alias-printing.m Thu Oct 11 18:42:27 2007
@@ -0,0 +1,18 @@
+// RUN: clang -ast-print %s
+
+ at protocol P1 @end
+ at protocol P2 @end
+
+ at interface INTF @end
+
+ at compatibility_alias alias INTF;
+
+
+int foo ()
+{
+	INTF *pi;
+	INTF<P2,P1> *pi2;
+	alias *p;
+	alias<P1,P2> *p2;
+	return pi2 == p2;
+}

Added: cfe/trunk/test/Sema/alias-test-1.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/alias-test-1.m?rev=42883&view=auto

==============================================================================
--- cfe/trunk/test/Sema/alias-test-1.m (added)
+++ cfe/trunk/test/Sema/alias-test-1.m Thu Oct 11 18:42:27 2007
@@ -0,0 +1,31 @@
+// RUN: clang -fsyntax-only -verify %s
+
+ at compatibility_alias alias4 foo; // expected-warning {{cannot find interface declaration for 'foo'}}
+
+ at class class2;
+ at class class3;
+
+typedef int I;  // expected-warning {{previous declaration is here}}
+
+ at compatibility_alias alias1 I;  // expected-warning {{cannot find interface declaration for 'I'}}
+
+ at compatibility_alias alias class2;    // expected-warning {{previous declaration is here}} 
+ at compatibility_alias alias class3;   // expected-warning {{previously declared alias is ignored}}
+
+
+typedef int alias2;	// expected-error {{previous declaration is here}}
+ at compatibility_alias alias2 class3;  // expected-error {{conflicting types for alias alias2'}}
+
+alias *p;
+class2 *p2;
+
+int foo ()
+{
+
+	if (p == p2) {
+	  int alias = 1;
+	}
+
+	alias *p3;
+	return p3 == p2;
+}





More information about the cfe-commits mailing list