[cfe-commits] r43075 - in /cfe/trunk: AST/ASTContext.cpp AST/Expr.cpp AST/StmtDumper.cpp AST/StmtPrinter.cpp Parse/ParseObjc.cpp Sema/Sema.cpp Sema/Sema.h Sema/SemaExpr.cpp include/clang/AST/ASTContext.h include/clang/AST/Expr.h include/clang/AST/StmtNodes.def include/clang/Basic/DiagnosticKinds.def include/clang/Parse/Action.h test/Sema/protocol-expr-1.m test/Sema/protocol-expr-neg-1.m

Fariborz Jahanian fjahanian at apple.com
Wed Oct 17 09:58:11 PDT 2007


Author: fjahanian
Date: Wed Oct 17 11:58:11 2007
New Revision: 43075

URL: http://llvm.org/viewvc/llvm-project?rev=43075&view=rev
Log:
Implementation of AST for @protocol expression.

Added:
    cfe/trunk/test/Sema/protocol-expr-1.m
    cfe/trunk/test/Sema/protocol-expr-neg-1.m
Modified:
    cfe/trunk/AST/ASTContext.cpp
    cfe/trunk/AST/Expr.cpp
    cfe/trunk/AST/StmtDumper.cpp
    cfe/trunk/AST/StmtPrinter.cpp
    cfe/trunk/Parse/ParseObjc.cpp
    cfe/trunk/Sema/Sema.cpp
    cfe/trunk/Sema/Sema.h
    cfe/trunk/Sema/SemaExpr.cpp
    cfe/trunk/include/clang/AST/ASTContext.h
    cfe/trunk/include/clang/AST/Expr.h
    cfe/trunk/include/clang/AST/StmtNodes.def
    cfe/trunk/include/clang/Basic/DiagnosticKinds.def
    cfe/trunk/include/clang/Parse/Action.h

Modified: cfe/trunk/AST/ASTContext.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/AST/ASTContext.cpp?rev=43075&r1=43074&r2=43075&view=diff

==============================================================================
--- cfe/trunk/AST/ASTContext.cpp (original)
+++ cfe/trunk/AST/ASTContext.cpp Wed Oct 17 11:58:11 2007
@@ -870,6 +870,17 @@
   SelStructType = rec;
 }
 
+void ASTContext::setObjcProtoType(TypedefDecl *TD)
+{
+  assert(ObjcProtoType.isNull() && "'Protocol' type already set!");
+  
+  // typedef struct Protocol Protocol;
+  ObjcProtoType = TD->getUnderlyingType();
+  // Protocol * type
+  ObjcProtoType = getPointerType(ObjcProtoType);  
+  ProtoStructType = TD->getUnderlyingType()->getAsStructureType();
+}
+
 void ASTContext::setObjcConstantStringInterface(ObjcInterfaceDecl *Decl) {
   assert(ObjcConstantStringType.isNull() && 
          "'NSConstantString' type already set!");

Modified: cfe/trunk/AST/Expr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/AST/Expr.cpp?rev=43075&r1=43074&r2=43075&view=diff

==============================================================================
--- cfe/trunk/AST/Expr.cpp (original)
+++ cfe/trunk/AST/Expr.cpp Wed Oct 17 11:58:11 2007
@@ -1084,6 +1084,10 @@
 Stmt::child_iterator ObjCSelectorExpr::child_begin() { return NULL; }
 Stmt::child_iterator ObjCSelectorExpr::child_end() { return NULL; }
 
+// ObjCProtocolExpr
+Stmt::child_iterator ObjCProtocolExpr::child_begin() { return NULL; }
+Stmt::child_iterator ObjCProtocolExpr::child_end() { return NULL; }
+
 // ObjCMessageExpr
 Stmt::child_iterator ObjCMessageExpr::child_begin() {
   return reinterpret_cast<Stmt**>(&SubExprs[0]);

Modified: cfe/trunk/AST/StmtDumper.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/AST/StmtDumper.cpp?rev=43075&r1=43074&r2=43075&view=diff

==============================================================================
--- cfe/trunk/AST/StmtDumper.cpp (original)
+++ cfe/trunk/AST/StmtDumper.cpp Wed Oct 17 11:58:11 2007
@@ -14,6 +14,7 @@
 
 #include "clang/AST/StmtVisitor.h"
 #include "clang/AST/Decl.h"
+#include "clang/AST/DeclObjc.h"
 #include "clang/AST/ExprCXX.h"
 #include "clang/Basic/IdentifierTable.h"
 #include "clang/Basic/SourceManager.h"
@@ -129,6 +130,7 @@
     // ObjC
     void VisitObjCEncodeExpr(ObjCEncodeExpr *Node);
     void VisitObjCSelectorExpr(ObjCSelectorExpr *Node);
+    void VisitObjCProtocolExpr(ObjCProtocolExpr *Node);
   };
 }
 
@@ -415,6 +417,12 @@
   fprintf(F, "%s", selector.getName().c_str());
 }
 
+void StmtDumper::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
+  DumpExpr(Node);
+  
+  fprintf(F, " ");
+  fprintf(F, "%s", Node->getProtocol()->getName());
+}
 //===----------------------------------------------------------------------===//
 // Stmt method implementations
 //===----------------------------------------------------------------------===//

Modified: cfe/trunk/AST/StmtPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/AST/StmtPrinter.cpp?rev=43075&r1=43074&r2=43075&view=diff

==============================================================================
--- cfe/trunk/AST/StmtPrinter.cpp (original)
+++ cfe/trunk/AST/StmtPrinter.cpp Wed Oct 17 11:58:11 2007
@@ -14,6 +14,7 @@
 
 #include "clang/AST/StmtVisitor.h"
 #include "clang/AST/Decl.h"
+#include "clang/AST/DeclObjc.h"
 #include "clang/AST/ExprCXX.h"
 #include "clang/AST/PrettyPrinter.h"
 #include "clang/Basic/IdentifierTable.h"
@@ -627,6 +628,12 @@
   OS << ")";
 }
 
+void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
+  OS << "@protocol(";
+  OS << Node->getProtocol()->getName();
+  OS << ")";
+}
+
 void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
   OS << "[";
   Expr *receiver = Mess->getReceiver();

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

==============================================================================
--- cfe/trunk/Parse/ParseObjc.cpp (original)
+++ cfe/trunk/Parse/ParseObjc.cpp Wed Oct 17 11:58:11 2007
@@ -1297,14 +1297,13 @@
     Diag(Tok, diag::err_expected_ident);
     return true;
   }
-
-  // FIXME: Do something with the protocol name
+  IdentifierInfo *protocolId = Tok.getIdentifierInfo();
   ConsumeToken();
   
   SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
 
-  // FIXME 
-  return 0;
+  return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc, 
+                                             LParenLoc, RParenLoc);
 }
 
 ///     objc-selector-expression

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

==============================================================================
--- cfe/trunk/Sema/Sema.cpp (original)
+++ cfe/trunk/Sema/Sema.cpp Wed Oct 17 11:58:11 2007
@@ -63,6 +63,24 @@
   return Context.getObjcSelType();
 }
 
+/// 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, std::vector<Decl*> &prevInGroup)
   : PP(pp), Context(ctxt), CurFunctionDecl(0), LastInGroupList(prevInGroup) {
   

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

==============================================================================
--- cfe/trunk/Sema/Sema.h (original)
+++ cfe/trunk/Sema/Sema.h Wed Oct 17 11:58:11 2007
@@ -280,6 +280,9 @@
   /// GetObjcSelType - Getter for the build-in "SEL" type.
   QualType GetObjcSelType(SourceLocation Loc = SourceLocation());
   
+  /// GetObjcSelType - Getter for the build-in "Protocol *" type.
+  QualType GetObjcProtoType(SourceLocation Loc = SourceLocation());
+  
   /// AddInstanceMethodToGlobalPool - All instance methods in a translation
   /// unit are added to a global pool. This allows us to efficiently associate
   /// a selector with a method declaraation for purposes of typechecking
@@ -452,6 +455,13 @@
                                                  SourceLocation LParenLoc,
                                                  SourceLocation RParenLoc);
   
+  // ParseObjCProtocolExpression - Build protocol expression for @protocol
+  virtual ExprResult ParseObjCProtocolExpression(IdentifierInfo * ProtocolName,
+                                                 SourceLocation AtLoc,
+                                                 SourceLocation ProtoLoc,
+                                                 SourceLocation LParenLoc,
+                                                 SourceLocation RParenLoc);
+  
   // Objective-C declarations.
   virtual DeclTy *ActOnStartClassInterface(
 		    SourceLocation AtInterafceLoc,

Modified: cfe/trunk/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Sema/SemaExpr.cpp?rev=43075&r1=43074&r2=43075&view=diff

==============================================================================
--- cfe/trunk/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/Sema/SemaExpr.cpp Wed Oct 17 11:58:11 2007
@@ -1929,6 +1929,20 @@
   return new ObjCSelectorExpr(t, Sel, AtLoc, RParenLoc);
 }
 
+Sema::ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId,
+                                                   SourceLocation AtLoc,
+                                                   SourceLocation ProtoLoc,
+                                                   SourceLocation LParenLoc,
+                                                   SourceLocation RParenLoc) {
+  ObjcProtocolDecl* PDecl = ObjcProtocols[ProtocolId];
+  if (!PDecl) {
+    Diag(ProtoLoc, diag::err_undeclared_protocol, ProtocolId->getName());
+    return true;
+  }
+  
+  QualType t = GetObjcProtoType(AtLoc);
+  return new ObjCProtocolExpr(t, PDecl, AtLoc, RParenLoc);
+}
 
 bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs,
                                      ObjcMethodDecl *Method) {

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

==============================================================================
--- cfe/trunk/include/clang/AST/ASTContext.h (original)
+++ cfe/trunk/include/clang/AST/ASTContext.h Wed Oct 17 11:58:11 2007
@@ -54,6 +54,10 @@
   QualType ObjcSelType;
   const RecordType *SelStructType;
   
+  /// ObjcProtoType - another psuedo built-in typedef type (set by Sema).
+  QualType ObjcProtoType;
+  const RecordType *ProtoStructType;
+  
   QualType ObjcConstantStringType;
   RecordDecl *CFConstantStringTypeDecl;
 public:
@@ -175,6 +179,9 @@
   void setObjcSelType(TypedefDecl *Decl);
   QualType getObjcSelType() const { return ObjcSelType; }
   
+  void setObjcProtoType(TypedefDecl *Decl);
+  QualType getObjcProtoType() const { return ObjcProtoType; }
+  
   void setBuiltinVaListType(QualType T);
   QualType getBuiltinVaListType() const { return BuiltinVaListType; }
     

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

==============================================================================
--- cfe/trunk/include/clang/AST/Expr.h (original)
+++ cfe/trunk/include/clang/AST/Expr.h Wed Oct 17 11:58:11 2007
@@ -1119,7 +1119,7 @@
   static bool classof(const Stmt *T) {
     return T->getStmtClass() == ObjCSelectorExprClass;
   }
-  static bool classof(const ObjCEncodeExpr *) { return true; }
+  static bool classof(const ObjCSelectorExpr *) { return true; }
   
   // Iterators
   virtual child_iterator child_begin();
@@ -1127,6 +1127,35 @@
   
 };
   
+/// ObjCProtocolExpr used for protocol in Objective-C.
+class ObjCProtocolExpr : public Expr {
+    
+  ObjcProtocolDecl *Protocol;
+    
+  SourceLocation AtLoc, RParenLoc;
+  public:
+  ObjCProtocolExpr(QualType T, ObjcProtocolDecl *protocol,
+                   SourceLocation at, SourceLocation rp)
+  : Expr(ObjCProtocolExprClass, T), Protocol(protocol), 
+  AtLoc(at), RParenLoc(rp) {}
+    
+  ObjcProtocolDecl *getProtocol() const { return Protocol; }
+    
+  SourceLocation getAtLoc() const { return AtLoc; }
+  SourceLocation getRParenLoc() const { return RParenLoc; }
+  SourceRange getSourceRange() const { return SourceRange(AtLoc, RParenLoc); }
+        
+  static bool classof(const Stmt *T) {
+    return T->getStmtClass() == ObjCProtocolExprClass;
+  }
+  static bool classof(const ObjCProtocolExpr *) { return true; }
+    
+  // Iterators
+  virtual child_iterator child_begin();
+  virtual child_iterator child_end();
+    
+};
+  
 class ObjCMessageExpr : public Expr {
   enum { RECEIVER=0, ARGS_START=1 };
 

Modified: cfe/trunk/include/clang/AST/StmtNodes.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/StmtNodes.def?rev=43075&r1=43074&r2=43075&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/StmtNodes.def (original)
+++ cfe/trunk/include/clang/AST/StmtNodes.def Wed Oct 17 11:58:11 2007
@@ -83,8 +83,9 @@
 STMT(71, ObjCEncodeExpr       , Expr)
 STMT(72, ObjCMessageExpr      , Expr)
 STMT(73, ObjCSelectorExpr     , Expr)
+STMT(74, ObjCProtocolExpr     , Expr)
 
-LAST_EXPR(73)
+LAST_EXPR(74)
 
 #undef STMT
 #undef FIRST_STMT

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

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticKinds.def (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticKinds.def Wed Oct 17 11:58:11 2007
@@ -448,6 +448,8 @@
      "cannot find definition of 'SEL'")
 DIAG(err_missing_id_definition, ERROR,
      "cannot find definition of 'id'")
+DIAG(err_missing_proto_definition, ERROR,
+     "cannot find definition of 'Protocol'")
 DIAG(warn_previous_alias_decl, WARNING,
      "previously declared alias is ignored")
 DIAG(warn_previous_declaration, WARNING,

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

==============================================================================
--- cfe/trunk/include/clang/Parse/Action.h (original)
+++ cfe/trunk/include/clang/Parse/Action.h Wed Oct 17 11:58:11 2007
@@ -603,6 +603,13 @@
     return 0;
   }
   
+  virtual ExprResult ParseObjCProtocolExpression(IdentifierInfo *ProtocolId,
+                                                 SourceLocation AtLoc,
+                                                 SourceLocation ProtoLoc,
+                                                 SourceLocation LParenLoc,
+                                                 SourceLocation RParenLoc) {
+    return 0;
+  } 
 };
 
 /// MinimalAction - Minimal actions are used by light-weight clients of the

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

==============================================================================
--- cfe/trunk/test/Sema/protocol-expr-1.m (added)
+++ cfe/trunk/test/Sema/protocol-expr-1.m Wed Oct 17 11:58:11 2007
@@ -0,0 +1,17 @@
+// RUN: clang -fsyntax-only -verify %s
+
+typedef struct Protocol Protocol;
+
+ at protocol fproto;
+
+ at protocol p1 
+ at end
+
+ at class cl;
+
+int main()
+{
+	Protocol *proto = @protocol(p1);
+        Protocol *fproto = @protocol(fproto);
+}
+

Added: cfe/trunk/test/Sema/protocol-expr-neg-1.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/protocol-expr-neg-1.m?rev=43075&view=auto

==============================================================================
--- cfe/trunk/test/Sema/protocol-expr-neg-1.m (added)
+++ cfe/trunk/test/Sema/protocol-expr-neg-1.m Wed Oct 17 11:58:11 2007
@@ -0,0 +1,19 @@
+// RUN: clang -fsyntax-only -verify %s
+
+typedef struct Protocol Protocol;
+
+ at protocol fproto;
+
+ at protocol p1 
+ at end
+
+ at class cl;
+
+int main()
+{
+	Protocol *proto = @protocol(p1);
+        Protocol *fproto = @protocol(fproto);
+	Protocol *pp = @protocol(i); // expected-error {{cannot find protocol declaration for 'i'}}
+	Protocol *p1p = @protocol(cl); // expected-error {{cannot find protocol declaration for 'cl'}}
+}
+





More information about the cfe-commits mailing list