Parse @encode expressions.

llvm-svn: 41273
This commit is contained in:
Anders Carlsson 2007-08-22 15:14:15 +00:00
parent 9bbecaeb55
commit c5a81ebb1f
9 changed files with 87 additions and 3 deletions

View File

@ -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
//===----------------------------------------------------------------------===//

View File

@ -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
//===----------------------------------------------------------------------===//

View File

@ -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());
}

View File

@ -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).

View File

@ -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<StringLiteral *>(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);
}

View File

@ -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

View File

@ -76,6 +76,7 @@ STMT(55, CXXBoolLiteralExpr , Expr)
// Obj-C Expressions.
STMT(56, ObjCStringLiteral , Expr)
STMT(57, ObjCEncodeExpr , Expr)
LAST_EXPR(56)

View File

@ -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;
}
};

View File

@ -327,7 +327,8 @@ private:
// Objective-C Expressions
ExprResult ParseObjCExpression();
ExprResult ParseObjCStringLiteral();
ExprResult ParseObjCEncodeExpression();
//===--------------------------------------------------------------------===//
// C99 6.8: Statements and Blocks.