From f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6 Mon Sep 17 00:00:00 2001 From: Anders Carlsson Date: Wed, 22 Aug 2007 15:14:15 +0000 Subject: [PATCH] Parse @encode expressions. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@41273 91177308-0d34-0410-b5e6-96231b3b80d8 --- AST/StmtDumper.cpp | 8 ++++++++ AST/StmtPrinter.cpp | 5 +++++ Parse/ParseObjc.cpp | 29 +++++++++++++++++++++++++++++ Sema/Sema.h | 5 +++++ Sema/SemaExpr.cpp | 13 +++++++++++-- include/clang/AST/Expr.h | 19 +++++++++++++++++++ include/clang/AST/StmtNodes.def | 1 + include/clang/Parse/Action.h | 7 +++++++ include/clang/Parse/Parser.h | 3 ++- 9 files changed, 87 insertions(+), 3 deletions(-) diff --git a/AST/StmtDumper.cpp b/AST/StmtDumper.cpp index 22af23cc0e..cf1eab172e 100644 --- a/AST/StmtDumper.cpp +++ b/AST/StmtDumper.cpp @@ -503,6 +503,14 @@ void StmtDumper::VisitObjCStringLiteral(ObjCStringLiteral *Node) { 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 6b2fce874a..2c77f131d8 100644 --- a/AST/StmtPrinter.cpp +++ b/AST/StmtPrinter.cpp @@ -518,6 +518,11 @@ void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) { 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 94a5c8d42c..98395d1922 100644 --- a/Parse/ParseObjc.cpp +++ b/Parse/ParseObjc.cpp @@ -369,6 +369,9 @@ Parser::ExprResult Parser::ParseObjCExpression() { 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 @@ Parser::ExprResult Parser::ParseObjCStringLiteral() { 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 42beb84f38..505633529c 100644 --- a/Sema/Sema.h +++ b/Sema/Sema.h @@ -326,6 +326,11 @@ public: // 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 da3bc3d94f..7fc6422ab7 100644 --- a/Sema/SemaExpr.cpp +++ b/Sema/SemaExpr.cpp @@ -1658,8 +1658,7 @@ Sema::ExprResult Sema::ParseChooseExpr(SourceLocation BuiltinLoc, ExprTy *cond, } // TODO: Move this to SemaObjC.cpp -Sema::ExprResult Sema::ParseObjCStringLiteral(ExprTy *string) -{ +Sema::ExprResult Sema::ParseObjCStringLiteral(ExprTy *string) { StringLiteral* S = static_cast(string); if (CheckBuiltinCFStringArgument(S)) @@ -1671,3 +1670,13 @@ Sema::ExprResult Sema::ParseObjCStringLiteral(ExprTy *string) 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 94cf512e30..4136a9eb1d 100644 --- a/include/clang/AST/Expr.h +++ b/include/clang/AST/Expr.h @@ -834,6 +834,25 @@ public: 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 9fa780c314..6a67e76cda 100644 --- a/include/clang/AST/StmtNodes.def +++ b/include/clang/AST/StmtNodes.def @@ -76,6 +76,7 @@ STMT(55, CXXBoolLiteralExpr , Expr) // 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 668bfb6d01..1ddbe85fb4 100644 --- a/include/clang/Parse/Action.h +++ b/include/clang/Parse/Action.h @@ -409,6 +409,13 @@ public: 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 02c2ee70f3..f0063f3748 100644 --- a/include/clang/Parse/Parser.h +++ b/include/clang/Parse/Parser.h @@ -327,7 +327,8 @@ private: // Objective-C Expressions ExprResult ParseObjCExpression(); ExprResult ParseObjCStringLiteral(); - + ExprResult ParseObjCEncodeExpression(); + //===--------------------------------------------------------------------===// // C99 6.8: Statements and Blocks. -- 2.40.0