From f88f7ab5adaa11d050270ffee6aa871e855f83b8 Mon Sep 17 00:00:00 2001 From: Fariborz Jahanian Date: Wed, 25 Nov 2009 01:26:41 +0000 Subject: [PATCH] Some fancy footwork to move the decision on how to build casted expression-list AST to Sema. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@89827 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Parse/Action.h | 5 +++-- include/clang/Parse/Parser.h | 6 +++--- lib/Parse/ParseExpr.cpp | 22 +++++++++++----------- lib/Sema/Sema.h | 5 +++-- lib/Sema/SemaExpr.cpp | 13 +++++++++---- lib/Sema/TreeTransform.h | 3 ++- 6 files changed, 31 insertions(+), 23 deletions(-) diff --git a/include/clang/Parse/Action.h b/include/clang/Parse/Action.h index 4b09cabfaa..c201b78df9 100644 --- a/include/clang/Parse/Action.h +++ b/include/clang/Parse/Action.h @@ -992,9 +992,10 @@ public: return move(Val); // Default impl returns operand. } - virtual OwningExprResult ActOnParenListExpr(SourceLocation L, + virtual OwningExprResult ActOnParenOrParenListExpr(SourceLocation L, SourceLocation R, - MultiExprArg Val) { + MultiExprArg Val, + TypeTy *TypeOfCast=0) { return ExprEmpty(); } diff --git a/include/clang/Parse/Parser.h b/include/clang/Parse/Parser.h index 30c6b90d1f..81a80ebcbc 100644 --- a/include/clang/Parse/Parser.h +++ b/include/clang/Parse/Parser.h @@ -833,10 +833,10 @@ private: OwningExprResult ParseCastExpression(bool isUnaryExpression, bool isAddressOfOperand, bool &NotCastExpr, - bool parseParenAsExprList); + TypeTy *TypeOfCast); OwningExprResult ParseCastExpression(bool isUnaryExpression, bool isAddressOfOperand = false, - bool parseParenAsExprList = false); + TypeTy *TypeOfCast = 0); OwningExprResult ParsePostfixExpressionSuffix(OwningExprResult LHS); OwningExprResult ParseSizeofAlignofExpression(); OwningExprResult ParseBuiltinPrimaryExpression(); @@ -866,7 +866,7 @@ private: }; OwningExprResult ParseParenExpression(ParenParseOption &ExprType, bool stopIfCastExpr, - bool parseAsExprList, + TypeTy *TypeOfCast, TypeTy *&CastTy, SourceLocation &RParenLoc); diff --git a/lib/Parse/ParseExpr.cpp b/lib/Parse/ParseExpr.cpp index 5eb19d07cd..f780cf1a60 100644 --- a/lib/Parse/ParseExpr.cpp +++ b/lib/Parse/ParseExpr.cpp @@ -413,12 +413,12 @@ Parser::ParseRHSOfBinaryExpression(OwningExprResult LHS, unsigned MinPrec) { /// Parser::OwningExprResult Parser::ParseCastExpression(bool isUnaryExpression, bool isAddressOfOperand, - bool parseParenAsExprList){ + TypeTy *TypeOfCast) { bool NotCastExpr; OwningExprResult Res = ParseCastExpression(isUnaryExpression, isAddressOfOperand, NotCastExpr, - parseParenAsExprList); + TypeOfCast); if (NotCastExpr) Diag(Tok, diag::err_expected_expression); return move(Res); @@ -538,7 +538,7 @@ Parser::OwningExprResult Parser::ParseCastExpression(bool isUnaryExpression, Parser::OwningExprResult Parser::ParseCastExpression(bool isUnaryExpression, bool isAddressOfOperand, bool &NotCastExpr, - bool parseParenAsExprList){ + TypeTy *TypeOfCast) { OwningExprResult Res(Actions); tok::TokenKind SavedKind = Tok.getKind(); NotCastExpr = false; @@ -563,7 +563,7 @@ Parser::OwningExprResult Parser::ParseCastExpression(bool isUnaryExpression, SourceLocation LParenLoc = Tok.getLocation(); SourceLocation RParenLoc; Res = ParseParenExpression(ParenExprType, false/*stopIfCastExr*/, - parseParenAsExprList, CastTy, RParenLoc); + TypeOfCast, CastTy, RParenLoc); if (Res.isInvalid()) return move(Res); switch (ParenExprType) { @@ -1047,7 +1047,8 @@ Parser::ParseExprAfterTypeofSizeofAlignof(const Token &OpTok, // operands. EnterExpressionEvaluationContext Unevaluated(Actions, Action::Unevaluated); - Operand = ParseParenExpression(ExprType, true/*stopIfCastExpr*/, false, + Operand = ParseParenExpression(ExprType, true/*stopIfCastExpr*/, + 0/*TypeOfCast*/, CastTy, RParenLoc); CastRange = SourceRange(LParenLoc, RParenLoc); @@ -1304,7 +1305,7 @@ Parser::OwningExprResult Parser::ParseBuiltinPrimaryExpression() { /// Parser::OwningExprResult Parser::ParseParenExpression(ParenParseOption &ExprType, bool stopIfCastExpr, - bool parseAsExprList, TypeTy *&CastTy, + TypeTy *TypeOfCast, TypeTy *&CastTy, SourceLocation &RParenLoc) { assert(Tok.is(tok::l_paren) && "Not a paren expr!"); GreaterThanIsOperatorScope G(GreaterThanIsOperator, true); @@ -1365,8 +1366,7 @@ Parser::ParseParenExpression(ParenParseOption &ExprType, bool stopIfCastExpr, // Parse the cast-expression that follows it next. // TODO: For cast expression with CastTy. - Result = ParseCastExpression(false, false, - Actions.TypeIsVectorType(CastTy)); + Result = ParseCastExpression(false, false, CastTy); if (!Result.isInvalid()) Result = Actions.ActOnCastExpr(CurScope, OpenLoc, CastTy, RParenLoc, move(Result)); @@ -1375,15 +1375,15 @@ Parser::ParseParenExpression(ParenParseOption &ExprType, bool stopIfCastExpr, Diag(Tok, diag::err_expected_lbrace_in_compound_literal); return ExprError(); - } else if (parseAsExprList) { + } else if (TypeOfCast) { // Parse the expression-list. ExprVector ArgExprs(Actions); CommaLocsTy CommaLocs; if (!ParseExpressionList(ArgExprs, CommaLocs)) { ExprType = SimpleExpr; - Result = Actions.ActOnParenListExpr(OpenLoc, Tok.getLocation(), - move_arg(ArgExprs)); + Result = Actions.ActOnParenOrParenListExpr(OpenLoc, Tok.getLocation(), + move_arg(ArgExprs), TypeOfCast); } } else { Result = ParseExpression(); diff --git a/lib/Sema/Sema.h b/lib/Sema/Sema.h index 19a255ce6a..92d809b9c2 100644 --- a/lib/Sema/Sema.h +++ b/lib/Sema/Sema.h @@ -1443,9 +1443,10 @@ public: virtual OwningExprResult ActOnCharacterConstant(const Token &); virtual OwningExprResult ActOnParenExpr(SourceLocation L, SourceLocation R, ExprArg Val); - virtual OwningExprResult ActOnParenListExpr(SourceLocation L, + virtual OwningExprResult ActOnParenOrParenListExpr(SourceLocation L, SourceLocation R, - MultiExprArg Val); + MultiExprArg Val, + TypeTy *TypeOfCast=0); /// ActOnStringLiteral - The specified tokens were lexed as pasted string /// fragments (e.g. "foo" "bar" L"baz"). diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index 45a82b5c03..c37b7707b2 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -3328,13 +3328,18 @@ Sema::ActOnCastOfParenListExpr(Scope *S, SourceLocation LParenLoc, } } -Action::OwningExprResult Sema::ActOnParenListExpr(SourceLocation L, +Action::OwningExprResult Sema::ActOnParenOrParenListExpr(SourceLocation L, SourceLocation R, - MultiExprArg Val) { + MultiExprArg Val, + TypeTy *TypeOfCast) { unsigned nexprs = Val.size(); Expr **exprs = reinterpret_cast(Val.release()); - assert((exprs != 0) && "ActOnParenListExpr() missing expr list"); - Expr *expr = new (Context) ParenListExpr(Context, L, exprs, nexprs, R); + assert((exprs != 0) && "ActOnParenOrParenListExpr() missing expr list"); + Expr *expr; + if (nexprs == 1 && TypeOfCast && !TypeIsVectorType(TypeOfCast)) + expr = new (Context) ParenExpr(L, R, exprs[0]); + else + expr = new (Context) ParenListExpr(Context, L, exprs, nexprs, R); return Owned(expr); } diff --git a/lib/Sema/TreeTransform.h b/lib/Sema/TreeTransform.h index 1071aebe17..4cfaf2b79b 100644 --- a/lib/Sema/TreeTransform.h +++ b/lib/Sema/TreeTransform.h @@ -1120,7 +1120,8 @@ public: OwningExprResult RebuildParenListExpr(SourceLocation LParenLoc, MultiExprArg SubExprs, SourceLocation RParenLoc) { - return getSema().ActOnParenListExpr(LParenLoc, RParenLoc, move(SubExprs)); + return getSema().ActOnParenOrParenListExpr(LParenLoc, RParenLoc, + move(SubExprs)); } /// \brief Build a new address-of-label expression. -- 2.40.0