Patch to implement "Protocol" as a built-in type declared as
"@class Protocol;"


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@44670 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/AST/ASTContext.cpp b/AST/ASTContext.cpp
index 3f5a7fb..e8ed9a2 100644
--- a/AST/ASTContext.cpp
+++ b/AST/ASTContext.cpp
@@ -1121,15 +1121,10 @@
   SelStructType = rec;
 }
 
-void ASTContext::setObjcProtoType(TypedefDecl *TD)
+void ASTContext::setObjcProtoType(QualType QT)
 {
   assert(ObjcProtoType.isNull() && "'Protocol' type already set!");
-  
-  // typedef struct Protocol Protocol;
-  ObjcProtoType = TD->getUnderlyingType();
-  // Protocol * type
-  ObjcProtoType = getPointerType(ObjcProtoType);  
-  ProtoStructType = TD->getUnderlyingType()->getAsStructureType();
+  ObjcProtoType = QT;
 }
 
 void ASTContext::setObjcClassType(TypedefDecl *TD)
diff --git a/Parse/MinimalAction.cpp b/Parse/MinimalAction.cpp
index 92d997f..5426152 100644
--- a/Parse/MinimalAction.cpp
+++ b/Parse/MinimalAction.cpp
@@ -43,6 +43,9 @@
   II = &Idents.get("Class");
   TI = new TypeNameInfo(1, II->getFETokenInfo<TypeNameInfo>());
   II->setFETokenInfo(TI);
+  II = &Idents.get("Protocol");
+  TI = new TypeNameInfo(1, II->getFETokenInfo<TypeNameInfo>());
+  II->setFETokenInfo(TI);
 }
 
 /// isTypeName - This looks at the IdentifierInfo::FETokenInfo field to
diff --git a/Sema/Sema.cpp b/Sema/Sema.cpp
index 7bafd7d..7a657cd 100644
--- a/Sema/Sema.cpp
+++ b/Sema/Sema.cpp
@@ -23,7 +23,7 @@
 bool Sema::isBuiltinObjcType(TypedefDecl *TD) {
   const char *typeName = TD->getIdentifier()->getName();
   return strcmp(typeName, "id") == 0 || strcmp(typeName, "Class") == 0 ||
-         strcmp(typeName, "SEL") == 0;
+         strcmp(typeName, "SEL") == 0 || strcmp(typeName, "Protocol") == 0;
 }
 
 void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
@@ -38,31 +38,16 @@
     t = dyn_cast<TypedefType>(Context.getObjcClassType().getTypePtr());
     t->getDecl()->getIdentifier()->setFETokenInfo(t->getDecl());
     TUScope->AddDecl(t->getDecl());
+    ObjcInterfaceType *it = dyn_cast<ObjcInterfaceType>(Context.getObjcProtoType());
+    ObjcInterfaceDecl *IDecl = it->getDecl();
+    IDecl->getIdentifier()->setFETokenInfo(IDecl);
+    TUScope->AddDecl(IDecl);
     t = dyn_cast<TypedefType>(Context.getObjcSelType().getTypePtr());
     t->getDecl()->getIdentifier()->setFETokenInfo(t->getDecl());
     TUScope->AddDecl(t->getDecl());
   }
 }
 
-/// FIXME: remove this.
-/// GetObjcProtoType - See comments for Sema::GetObjcIdType above; replace "id"
-/// with "Protocol".
-QualType Sema::GetObjcProtoType(SourceLocation Loc) {
-  assert(TUScope && "GetObjcProtoType(): Top-level scope is null");
-  if (Context.getObjcProtoType().isNull()) {
-    IdentifierInfo *ProtoIdent = &Context.Idents.get("Protocol");
-    ScopedDecl *ProtoDecl = LookupScopedDecl(ProtoIdent, Decl::IDNS_Ordinary, 
-                                           SourceLocation(), TUScope);
-    TypedefDecl *ObjcProtoTypedef = dyn_cast_or_null<TypedefDecl>(ProtoDecl);
-    if (!ObjcProtoTypedef) {
-      Diag(Loc, diag::err_missing_proto_definition);
-      return QualType();
-    }
-    Context.setObjcProtoType(ObjcProtoTypedef);
-  }
-  return Context.getObjcProtoType();
-}
-
 Sema::Sema(Preprocessor &pp, ASTContext &ctxt)
   : PP(pp), Context(ctxt), CurFunctionDecl(0), CurMethodDecl(0) {
   
@@ -91,6 +76,11 @@
                                                 ClassT, 0);
     Context.setObjcClassType(ClassTypedef);
     
+    // Synthesize "@class Protocol;
+    ObjcInterfaceDecl *ProtocolDecl = new ObjcInterfaceDecl(SourceLocation(), 0, 
+                                        &Context.Idents.get("Protocol"), true);
+    Context.setObjcProtoType(Context.getObjcInterfaceType(ProtocolDecl));
+    
     // Synthesize "typedef struct objc_object { Class isa; } *id;"
     RecordDecl *ObjectTag = new RecordDecl(Decl::Struct, SourceLocation(), 
                                           &IT.get("objc_object"), 0);
@@ -111,6 +101,7 @@
                                               &Context.Idents.get("SEL"),
                                               SelT, 0);
     Context.setObjcSelType(SelTypedef);
+    
   }
   TUScope = 0;
 }
diff --git a/Sema/Sema.h b/Sema/Sema.h
index 00133f8..234fc157 100644
--- a/Sema/Sema.h
+++ b/Sema/Sema.h
@@ -277,10 +277,8 @@
   bool MatchTwoMethodDeclarations(const ObjcMethodDecl *Method, 
                                   const ObjcMethodDecl *PrevMethod); 
 
-  /// GetObjcSelType - Getter for the build-in "Protocol *" type.
-  QualType GetObjcProtoType(SourceLocation Loc = SourceLocation());
-  
-  /// isBuiltinObjcType - Returns true of the type is "id", "SEL", "Class".
+  /// isBuiltinObjcType - Returns true of the type is "id", "SEL", "Class"
+  /// or "Protocol".
   bool isBuiltinObjcType(TypedefDecl *TD);
 
   /// AddInstanceMethodToGlobalPool - All instance methods in a translation
diff --git a/Sema/SemaExpr.cpp b/Sema/SemaExpr.cpp
index 379a033..9d3e9eb 100644
--- a/Sema/SemaExpr.cpp
+++ b/Sema/SemaExpr.cpp
@@ -2061,9 +2061,10 @@
     return true;
   }
   
-  QualType t = GetObjcProtoType(AtLoc);
+  QualType t = Context.getObjcProtoType();
   if (t.isNull())
     return true;
+  t = Context.getPointerType(t);
   return new ObjCProtocolExpr(t, PDecl, AtLoc, RParenLoc);
 }
 
diff --git a/include/clang/AST/ASTContext.h b/include/clang/AST/ASTContext.h
index c4a7c85..c8a80b0 100644
--- a/include/clang/AST/ASTContext.h
+++ b/include/clang/AST/ASTContext.h
@@ -212,7 +212,7 @@
   void setObjcSelType(TypedefDecl *Decl);
   QualType getObjcSelType() const { return ObjcSelType; }
   
-  void setObjcProtoType(TypedefDecl *Decl);
+  void setObjcProtoType(QualType QT);
   QualType getObjcProtoType() const { return ObjcProtoType; }
   
   void setObjcClassType(TypedefDecl *Decl);
diff --git a/test/Sema/protocol-expr-1.m b/test/Sema/protocol-expr-1.m
index 23cb5dd..dff2866 100644
--- a/test/Sema/protocol-expr-1.m
+++ b/test/Sema/protocol-expr-1.m
@@ -1,7 +1,5 @@
 // RUN: clang -fsyntax-only -verify %s
 
-typedef struct Protocol Protocol;
-
 @protocol fproto;
 
 @protocol p1 
diff --git a/test/Sema/protocol-expr-neg-1.m b/test/Sema/protocol-expr-neg-1.m
index d59d83f..427e744 100644
--- a/test/Sema/protocol-expr-neg-1.m
+++ b/test/Sema/protocol-expr-neg-1.m
@@ -1,6 +1,6 @@
 // RUN: clang -fsyntax-only -verify %s
 
-typedef struct Protocol Protocol;
+@class Protocol;
 
 @protocol fproto;