Parse @encode expressions.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@41273 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/AST/StmtDumper.cpp b/AST/StmtDumper.cpp
index 22af23c..cf1eab1 100644
--- a/AST/StmtDumper.cpp
+++ b/AST/StmtDumper.cpp
@@ -503,6 +503,14 @@
   fprintf(F, ")");
 }
 
+void StmtDumper::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
+  DumpExpr(Node);
+ 
+  fprintf(F, " ");
+  DumpType(Node->getEncodedType());
+  fprintf(F, ")");  
+}
+
 //===----------------------------------------------------------------------===//
 // Stmt method implementations
 //===----------------------------------------------------------------------===//
diff --git a/AST/StmtPrinter.cpp b/AST/StmtPrinter.cpp
index 6b2fce8..2c77f13 100644
--- a/AST/StmtPrinter.cpp
+++ b/AST/StmtPrinter.cpp
@@ -518,6 +518,11 @@
   VisitStringLiteral(Node->getString());
 }
 
+void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
+  OS << "@encode(";
+  OS << Node->getEncodedType().getAsString() << ")";
+}
+
 //===----------------------------------------------------------------------===//
 // Stmt method implementations
 //===----------------------------------------------------------------------===//
diff --git a/Parse/ParseObjc.cpp b/Parse/ParseObjc.cpp
index 94a5c8d..98395d1 100644
--- a/Parse/ParseObjc.cpp
+++ b/Parse/ParseObjc.cpp
@@ -369,6 +369,9 @@
     case tok::string_literal:    // primary-expression: string-literal
     case tok::wide_string_literal:
       return ParseObjCStringLiteral();
+    case tok::objc_encode:
+      return ParseObjCEncodeExpression();
+      break;
     default:
       Diag(AtLoc, diag::err_unexpected_at);
       SkipUntil(tok::semi);
@@ -385,3 +388,29 @@
 
   return Actions.ParseObjCStringLiteral(Res.Val);
 }
+
+///    objc-encode-expression:
+///      @encode ( type-name )
+Parser::ExprResult Parser::ParseObjCEncodeExpression() {
+  assert(Tok.getIdentifierInfo()->getObjCKeywordID() == tok::objc_encode && 
+         "Not an @encode expression!");
+  
+  SourceLocation EncLoc = ConsumeToken();
+  
+  if (Tok.getKind() != tok::l_paren) {
+    Diag(Tok, diag::err_expected_lparen_after, "@encode");
+    return true;
+  }
+   
+  SourceLocation LParenLoc = ConsumeParen();
+  
+  TypeTy *Ty = ParseTypeName();
+  
+  if (Tok.getKind() != tok::r_paren) {
+    Diag(Tok, diag::err_expected_rparen);
+    return true;
+  }
+   
+  return Actions.ParseObjCEncodeExpression(EncLoc, LParenLoc, Ty, 
+                                           ConsumeParen());
+}
diff --git a/Sema/Sema.h b/Sema/Sema.h
index 42beb84..5056335 100644
--- a/Sema/Sema.h
+++ b/Sema/Sema.h
@@ -326,6 +326,11 @@
   
   // ParseObjCStringLiteral - Parse Objective-C string literals.
   virtual ExprResult ParseObjCStringLiteral(ExprTy *string);
+  virtual ExprResult ParseObjCEncodeExpression(SourceLocation AtLoc,
+                                               SourceLocation LParenLoc,
+                                               TypeTy *Ty,
+                                               SourceLocation RParenLoc);
+  
 private:
   // UsualUnaryConversions - promotes integers (C99 6.3.1.1p2) and converts
   // functions and arrays to their respective pointers (C99 6.3.2.1). 
diff --git a/Sema/SemaExpr.cpp b/Sema/SemaExpr.cpp
index da3bc3d..7fc6422 100644
--- a/Sema/SemaExpr.cpp
+++ b/Sema/SemaExpr.cpp
@@ -1658,8 +1658,7 @@
 }
 
 // TODO: Move this to SemaObjC.cpp
-Sema::ExprResult Sema::ParseObjCStringLiteral(ExprTy *string)
-{
+Sema::ExprResult Sema::ParseObjCStringLiteral(ExprTy *string) {
   StringLiteral* S = static_cast<StringLiteral *>(string);
   
   if (CheckBuiltinCFStringArgument(S))
@@ -1671,3 +1670,13 @@
 
   return new ObjCStringLiteral(S, t);
 }
+
+Sema::ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc,
+                                                 SourceLocation LParenLoc,
+                                                 TypeTy *Ty,
+                                                 SourceLocation RParenLoc) {
+  QualType EncodedType = QualType::getFromOpaquePtr(Ty);
+
+  QualType t = Context.getPointerType(Context.CharTy);
+  return new ObjCEncodeExpr(t, EncodedType, AtLoc, RParenLoc);
+}
diff --git a/include/clang/AST/Expr.h b/include/clang/AST/Expr.h
index 94cf512..4136a9e 100644
--- a/include/clang/AST/Expr.h
+++ b/include/clang/AST/Expr.h
@@ -834,6 +834,25 @@
   static bool classof(const ObjCStringLiteral *) { return true; }  
 };
   
+/// ObjCEncodeExpr, used for @encode in Objective-C.
+class ObjCEncodeExpr : public Expr {
+  QualType EncType;
+  SourceLocation EncLoc, RParenLoc;
+public:
+  ObjCEncodeExpr(QualType T, QualType ET, 
+                 SourceLocation enc, SourceLocation rp)
+    : Expr(ObjCEncodeExprClass, T), EncType(ET), EncLoc(enc), RParenLoc(rp) {}
+  
+  SourceRange getSourceRange() const { return SourceRange(EncLoc, RParenLoc); }
+
+  QualType getEncodedType() const { return EncType; }
+
+  static bool classof(const Stmt *T) {
+    return T->getStmtClass() == ObjCEncodeExprClass;
+  }
+  static bool classof(const ObjCEncodeExpr *) { return true; }
+};
+
 }  // end namespace clang
 
 #endif
diff --git a/include/clang/AST/StmtNodes.def b/include/clang/AST/StmtNodes.def
index 9fa780c..6a67e76 100644
--- a/include/clang/AST/StmtNodes.def
+++ b/include/clang/AST/StmtNodes.def
@@ -76,6 +76,7 @@
 
 // Obj-C Expressions.
 STMT(56, ObjCStringLiteral    , Expr)
+STMT(57, ObjCEncodeExpr       , Expr)
 
 LAST_EXPR(56)
 
diff --git a/include/clang/Parse/Action.h b/include/clang/Parse/Action.h
index 668bfb6..1ddbe85 100644
--- a/include/clang/Parse/Action.h
+++ b/include/clang/Parse/Action.h
@@ -409,6 +409,13 @@
   virtual ExprResult ParseObjCStringLiteral(ExprTy *string) {
     return 0;
   }
+
+  virtual ExprResult ParseObjCEncodeExpression(SourceLocation EncLoc,
+                                               SourceLocation LParenLoc,
+                                               TypeTy *Ty,
+                                               SourceLocation RParenLoc) {
+    return 0;
+  }
   
 };
 
diff --git a/include/clang/Parse/Parser.h b/include/clang/Parse/Parser.h
index 02c2ee7..f0063f3 100644
--- a/include/clang/Parse/Parser.h
+++ b/include/clang/Parse/Parser.h
@@ -327,7 +327,8 @@
   // Objective-C Expressions
   ExprResult ParseObjCExpression();
   ExprResult ParseObjCStringLiteral();
-  
+  ExprResult ParseObjCEncodeExpression();
+
   //===--------------------------------------------------------------------===//
   // C99 6.8: Statements and Blocks.