| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1 | //===--- ParseExpr.cpp - Expression Parsing -------------------------------===// | 
|  | 2 | // | 
|  | 3 | //                     The LLVM Compiler Infrastructure | 
|  | 4 | // | 
| Chris Lattner | 959e5be | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source | 
|  | 6 | // License. See LICENSE.TXT for details. | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 7 | // | 
|  | 8 | //===----------------------------------------------------------------------===// | 
|  | 9 | // | 
|  | 10 | // This file implements the Expression parsing implementation.  Expressions in | 
|  | 11 | // C99 basically consist of a bunch of binary operators with unary operators and | 
|  | 12 | // other random stuff at the leaves. | 
|  | 13 | // | 
|  | 14 | // In the C99 grammar, these unary operators bind tightest and are represented | 
|  | 15 | // as the 'cast-expression' production.  Everything else is either a binary | 
|  | 16 | // operator (e.g. '/') or a ternary operator ("?:").  The unary leaves are | 
|  | 17 | // handled by ParseCastExpression, the higher level pieces are handled by | 
|  | 18 | // ParseBinaryExpression. | 
|  | 19 | // | 
|  | 20 | //===----------------------------------------------------------------------===// | 
|  | 21 |  | 
|  | 22 | #include "clang/Parse/Parser.h" | 
| Argiris Kirtzidis | 7a1e741 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 23 | #include "clang/Parse/DeclSpec.h" | 
| Steve Naroff | fd5b19d | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 24 | #include "clang/Parse/Scope.h" | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 25 | #include "clang/Basic/Diagnostic.h" | 
|  | 26 | #include "llvm/ADT/SmallVector.h" | 
|  | 27 | #include "llvm/ADT/SmallString.h" | 
|  | 28 | using namespace clang; | 
|  | 29 |  | 
|  | 30 | /// PrecedenceLevels - These are precedences for the binary/ternary operators in | 
|  | 31 | /// the C99 grammar.  These have been named to relate with the C99 grammar | 
|  | 32 | /// productions.  Low precedences numbers bind more weakly than high numbers. | 
|  | 33 | namespace prec { | 
|  | 34 | enum Level { | 
|  | 35 | Unknown        = 0,    // Not binary operator. | 
|  | 36 | Comma          = 1,    // , | 
|  | 37 | Assignment     = 2,    // =, *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |= | 
|  | 38 | Conditional    = 3,    // ? | 
|  | 39 | LogicalOr      = 4,    // || | 
|  | 40 | LogicalAnd     = 5,    // && | 
|  | 41 | InclusiveOr    = 6,    // | | 
|  | 42 | ExclusiveOr    = 7,    // ^ | 
|  | 43 | And            = 8,    // & | 
|  | 44 | Equality       = 9,    // ==, != | 
|  | 45 | Relational     = 10,   //  >=, <=, >, < | 
|  | 46 | Shift          = 11,   // <<, >> | 
|  | 47 | Additive       = 12,   // -, + | 
|  | 48 | Multiplicative = 13    // *, /, % | 
|  | 49 | }; | 
|  | 50 | } | 
|  | 51 |  | 
|  | 52 |  | 
|  | 53 | /// getBinOpPrecedence - Return the precedence of the specified binary operator | 
|  | 54 | /// token.  This returns: | 
|  | 55 | /// | 
|  | 56 | static prec::Level getBinOpPrecedence(tok::TokenKind Kind) { | 
|  | 57 | switch (Kind) { | 
|  | 58 | default:                        return prec::Unknown; | 
|  | 59 | case tok::comma:                return prec::Comma; | 
|  | 60 | case tok::equal: | 
|  | 61 | case tok::starequal: | 
|  | 62 | case tok::slashequal: | 
|  | 63 | case tok::percentequal: | 
|  | 64 | case tok::plusequal: | 
|  | 65 | case tok::minusequal: | 
|  | 66 | case tok::lesslessequal: | 
|  | 67 | case tok::greatergreaterequal: | 
|  | 68 | case tok::ampequal: | 
|  | 69 | case tok::caretequal: | 
|  | 70 | case tok::pipeequal:            return prec::Assignment; | 
|  | 71 | case tok::question:             return prec::Conditional; | 
|  | 72 | case tok::pipepipe:             return prec::LogicalOr; | 
|  | 73 | case tok::ampamp:               return prec::LogicalAnd; | 
|  | 74 | case tok::pipe:                 return prec::InclusiveOr; | 
|  | 75 | case tok::caret:                return prec::ExclusiveOr; | 
|  | 76 | case tok::amp:                  return prec::And; | 
|  | 77 | case tok::exclaimequal: | 
|  | 78 | case tok::equalequal:           return prec::Equality; | 
|  | 79 | case tok::lessequal: | 
|  | 80 | case tok::less: | 
|  | 81 | case tok::greaterequal: | 
|  | 82 | case tok::greater:              return prec::Relational; | 
|  | 83 | case tok::lessless: | 
|  | 84 | case tok::greatergreater:       return prec::Shift; | 
|  | 85 | case tok::plus: | 
|  | 86 | case tok::minus:                return prec::Additive; | 
|  | 87 | case tok::percent: | 
|  | 88 | case tok::slash: | 
|  | 89 | case tok::star:                 return prec::Multiplicative; | 
|  | 90 | } | 
|  | 91 | } | 
|  | 92 |  | 
|  | 93 |  | 
|  | 94 | /// ParseExpression - Simple precedence-based parser for binary/ternary | 
|  | 95 | /// operators. | 
|  | 96 | /// | 
|  | 97 | /// Note: we diverge from the C99 grammar when parsing the assignment-expression | 
|  | 98 | /// production.  C99 specifies that the LHS of an assignment operator should be | 
|  | 99 | /// parsed as a unary-expression, but consistency dictates that it be a | 
|  | 100 | /// conditional-expession.  In practice, the important thing here is that the | 
|  | 101 | /// LHS of an assignment has to be an l-value, which productions between | 
|  | 102 | /// unary-expression and conditional-expression don't produce.  Because we want | 
|  | 103 | /// consistency, we parse the LHS as a conditional-expression, then check for | 
|  | 104 | /// l-value-ness in semantic analysis stages. | 
|  | 105 | /// | 
|  | 106 | ///       multiplicative-expression: [C99 6.5.5] | 
|  | 107 | ///         cast-expression | 
|  | 108 | ///         multiplicative-expression '*' cast-expression | 
|  | 109 | ///         multiplicative-expression '/' cast-expression | 
|  | 110 | ///         multiplicative-expression '%' cast-expression | 
|  | 111 | /// | 
|  | 112 | ///       additive-expression: [C99 6.5.6] | 
|  | 113 | ///         multiplicative-expression | 
|  | 114 | ///         additive-expression '+' multiplicative-expression | 
|  | 115 | ///         additive-expression '-' multiplicative-expression | 
|  | 116 | /// | 
|  | 117 | ///       shift-expression: [C99 6.5.7] | 
|  | 118 | ///         additive-expression | 
|  | 119 | ///         shift-expression '<<' additive-expression | 
|  | 120 | ///         shift-expression '>>' additive-expression | 
|  | 121 | /// | 
|  | 122 | ///       relational-expression: [C99 6.5.8] | 
|  | 123 | ///         shift-expression | 
|  | 124 | ///         relational-expression '<' shift-expression | 
|  | 125 | ///         relational-expression '>' shift-expression | 
|  | 126 | ///         relational-expression '<=' shift-expression | 
|  | 127 | ///         relational-expression '>=' shift-expression | 
|  | 128 | /// | 
|  | 129 | ///       equality-expression: [C99 6.5.9] | 
|  | 130 | ///         relational-expression | 
|  | 131 | ///         equality-expression '==' relational-expression | 
|  | 132 | ///         equality-expression '!=' relational-expression | 
|  | 133 | /// | 
|  | 134 | ///       AND-expression: [C99 6.5.10] | 
|  | 135 | ///         equality-expression | 
|  | 136 | ///         AND-expression '&' equality-expression | 
|  | 137 | /// | 
|  | 138 | ///       exclusive-OR-expression: [C99 6.5.11] | 
|  | 139 | ///         AND-expression | 
|  | 140 | ///         exclusive-OR-expression '^' AND-expression | 
|  | 141 | /// | 
|  | 142 | ///       inclusive-OR-expression: [C99 6.5.12] | 
|  | 143 | ///         exclusive-OR-expression | 
|  | 144 | ///         inclusive-OR-expression '|' exclusive-OR-expression | 
|  | 145 | /// | 
|  | 146 | ///       logical-AND-expression: [C99 6.5.13] | 
|  | 147 | ///         inclusive-OR-expression | 
|  | 148 | ///         logical-AND-expression '&&' inclusive-OR-expression | 
|  | 149 | /// | 
|  | 150 | ///       logical-OR-expression: [C99 6.5.14] | 
|  | 151 | ///         logical-AND-expression | 
|  | 152 | ///         logical-OR-expression '||' logical-AND-expression | 
|  | 153 | /// | 
|  | 154 | ///       conditional-expression: [C99 6.5.15] | 
|  | 155 | ///         logical-OR-expression | 
|  | 156 | ///         logical-OR-expression '?' expression ':' conditional-expression | 
|  | 157 | /// [GNU]   logical-OR-expression '?' ':' conditional-expression | 
|  | 158 | /// | 
|  | 159 | ///       assignment-expression: [C99 6.5.16] | 
|  | 160 | ///         conditional-expression | 
|  | 161 | ///         unary-expression assignment-operator assignment-expression | 
| Chris Lattner | a7447ba | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 162 | /// [C++]   throw-expression [C++ 15] | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 163 | /// | 
|  | 164 | ///       assignment-operator: one of | 
|  | 165 | ///         = *= /= %= += -= <<= >>= &= ^= |= | 
|  | 166 | /// | 
|  | 167 | ///       expression: [C99 6.5.17] | 
|  | 168 | ///         assignment-expression | 
|  | 169 | ///         expression ',' assignment-expression | 
|  | 170 | /// | 
|  | 171 | Parser::ExprResult Parser::ParseExpression() { | 
| Chris Lattner | a7447ba | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 172 | if (Tok.is(tok::kw_throw)) | 
|  | 173 | return ParseThrowExpression(); | 
|  | 174 |  | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 175 | ExprResult LHS = ParseCastExpression(false); | 
|  | 176 | if (LHS.isInvalid) return LHS; | 
|  | 177 |  | 
|  | 178 | return ParseRHSOfBinaryExpression(LHS, prec::Comma); | 
|  | 179 | } | 
|  | 180 |  | 
| Fariborz Jahanian | 64b864e | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 181 | /// This routine is called when the '@' is seen and consumed. | 
|  | 182 | /// Current token is an Identifier and is not a 'try'. This | 
| Chris Lattner | b82d6ef | 2007-10-03 22:03:06 +0000 | [diff] [blame] | 183 | /// routine is necessary to disambiguate @try-statement from, | 
|  | 184 | /// for example, @encode-expression. | 
| Fariborz Jahanian | 64b864e | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 185 | /// | 
| Fariborz Jahanian | 37c9c61 | 2007-10-04 20:19:06 +0000 | [diff] [blame] | 186 | Parser::ExprResult Parser::ParseExpressionWithLeadingAt(SourceLocation AtLoc) { | 
| Steve Naroff | fb9dd75 | 2007-10-15 20:55:58 +0000 | [diff] [blame] | 187 | ExprResult LHS = ParseObjCAtExpression(AtLoc); | 
| Fariborz Jahanian | 64b864e | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 188 | if (LHS.isInvalid) return LHS; | 
|  | 189 |  | 
|  | 190 | return ParseRHSOfBinaryExpression(LHS, prec::Comma); | 
|  | 191 | } | 
|  | 192 |  | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 193 | /// ParseAssignmentExpression - Parse an expr that doesn't include commas. | 
|  | 194 | /// | 
|  | 195 | Parser::ExprResult Parser::ParseAssignmentExpression() { | 
| Chris Lattner | a7447ba | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 196 | if (Tok.is(tok::kw_throw)) | 
|  | 197 | return ParseThrowExpression(); | 
|  | 198 |  | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 199 | ExprResult LHS = ParseCastExpression(false); | 
|  | 200 | if (LHS.isInvalid) return LHS; | 
|  | 201 |  | 
|  | 202 | return ParseRHSOfBinaryExpression(LHS, prec::Assignment); | 
|  | 203 | } | 
|  | 204 |  | 
| Chris Lattner | bfcf477 | 2008-06-02 21:31:07 +0000 | [diff] [blame] | 205 | /// ParseAssignmentExprWithObjCMessageExprStart - Parse an assignment expression | 
|  | 206 | /// where part of an objc message send has already been parsed.  In this case | 
|  | 207 | /// LBracLoc indicates the location of the '[' of the message send, and either | 
|  | 208 | /// ReceiverName or ReceiverExpr is non-null indicating the receiver of the | 
|  | 209 | /// message. | 
|  | 210 | /// | 
|  | 211 | /// Since this handles full assignment-expression's, it handles postfix | 
|  | 212 | /// expressions and other binary operators for these expressions as well. | 
|  | 213 | Parser::ExprResult | 
|  | 214 | Parser::ParseAssignmentExprWithObjCMessageExprStart(SourceLocation LBracLoc, | 
|  | 215 | IdentifierInfo *ReceiverName, | 
|  | 216 | ExprTy *ReceiverExpr) { | 
|  | 217 | ExprResult R = ParseObjCMessageExpressionBody(LBracLoc, ReceiverName, | 
|  | 218 | ReceiverExpr); | 
|  | 219 | if (R.isInvalid) return R; | 
|  | 220 | R = ParsePostfixExpressionSuffix(R); | 
|  | 221 | if (R.isInvalid) return R; | 
|  | 222 | return ParseRHSOfBinaryExpression(R, 2); | 
|  | 223 | } | 
|  | 224 |  | 
|  | 225 |  | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 226 | Parser::ExprResult Parser::ParseConstantExpression() { | 
|  | 227 | ExprResult LHS = ParseCastExpression(false); | 
|  | 228 | if (LHS.isInvalid) return LHS; | 
|  | 229 |  | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 230 | return ParseRHSOfBinaryExpression(LHS, prec::Conditional); | 
|  | 231 | } | 
|  | 232 |  | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 233 | /// ParseRHSOfBinaryExpression - Parse a binary expression that starts with | 
|  | 234 | /// LHS and has a precedence of at least MinPrec. | 
|  | 235 | Parser::ExprResult | 
|  | 236 | Parser::ParseRHSOfBinaryExpression(ExprResult LHS, unsigned MinPrec) { | 
|  | 237 | unsigned NextTokPrec = getBinOpPrecedence(Tok.getKind()); | 
|  | 238 | SourceLocation ColonLoc; | 
|  | 239 |  | 
|  | 240 | while (1) { | 
|  | 241 | // If this token has a lower precedence than we are allowed to parse (e.g. | 
|  | 242 | // because we are called recursively, or because the token is not a binop), | 
|  | 243 | // then we are done! | 
|  | 244 | if (NextTokPrec < MinPrec) | 
|  | 245 | return LHS; | 
|  | 246 |  | 
|  | 247 | // Consume the operator, saving the operator token for error reporting. | 
|  | 248 | Token OpToken = Tok; | 
|  | 249 | ConsumeToken(); | 
|  | 250 |  | 
|  | 251 | // Special case handling for the ternary operator. | 
|  | 252 | ExprResult TernaryMiddle(true); | 
|  | 253 | if (NextTokPrec == prec::Conditional) { | 
| Chris Lattner | 4d7d234 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 254 | if (Tok.isNot(tok::colon)) { | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 255 | // Handle this production specially: | 
|  | 256 | //   logical-OR-expression '?' expression ':' conditional-expression | 
|  | 257 | // In particular, the RHS of the '?' is 'expression', not | 
|  | 258 | // 'logical-OR-expression' as we might expect. | 
|  | 259 | TernaryMiddle = ParseExpression(); | 
| Chris Lattner | 214cbaf | 2007-08-31 04:58:34 +0000 | [diff] [blame] | 260 | if (TernaryMiddle.isInvalid) { | 
|  | 261 | Actions.DeleteExpr(LHS.Val); | 
|  | 262 | return TernaryMiddle; | 
|  | 263 | } | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 264 | } else { | 
|  | 265 | // Special case handling of "X ? Y : Z" where Y is empty: | 
|  | 266 | //   logical-OR-expression '?' ':' conditional-expression   [GNU] | 
|  | 267 | TernaryMiddle = ExprResult(false); | 
|  | 268 | Diag(Tok, diag::ext_gnu_conditional_expr); | 
|  | 269 | } | 
|  | 270 |  | 
| Chris Lattner | 4d7d234 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 271 | if (Tok.isNot(tok::colon)) { | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 272 | Diag(Tok, diag::err_expected_colon); | 
|  | 273 | Diag(OpToken, diag::err_matching, "?"); | 
| Chris Lattner | 214cbaf | 2007-08-31 04:58:34 +0000 | [diff] [blame] | 274 | Actions.DeleteExpr(LHS.Val); | 
|  | 275 | Actions.DeleteExpr(TernaryMiddle.Val); | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 276 | return ExprResult(true); | 
|  | 277 | } | 
|  | 278 |  | 
|  | 279 | // Eat the colon. | 
|  | 280 | ColonLoc = ConsumeToken(); | 
|  | 281 | } | 
|  | 282 |  | 
|  | 283 | // Parse another leaf here for the RHS of the operator. | 
|  | 284 | ExprResult RHS = ParseCastExpression(false); | 
| Chris Lattner | 214cbaf | 2007-08-31 04:58:34 +0000 | [diff] [blame] | 285 | if (RHS.isInvalid) { | 
|  | 286 | Actions.DeleteExpr(LHS.Val); | 
|  | 287 | Actions.DeleteExpr(TernaryMiddle.Val); | 
|  | 288 | return RHS; | 
|  | 289 | } | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 290 |  | 
|  | 291 | // Remember the precedence of this operator and get the precedence of the | 
|  | 292 | // operator immediately to the right of the RHS. | 
|  | 293 | unsigned ThisPrec = NextTokPrec; | 
|  | 294 | NextTokPrec = getBinOpPrecedence(Tok.getKind()); | 
|  | 295 |  | 
|  | 296 | // Assignment and conditional expressions are right-associative. | 
| Chris Lattner | 464c7f6 | 2007-12-18 06:06:23 +0000 | [diff] [blame] | 297 | bool isRightAssoc = ThisPrec == prec::Conditional || | 
|  | 298 | ThisPrec == prec::Assignment; | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 299 |  | 
|  | 300 | // Get the precedence of the operator to the right of the RHS.  If it binds | 
|  | 301 | // more tightly with RHS than we do, evaluate it completely first. | 
|  | 302 | if (ThisPrec < NextTokPrec || | 
|  | 303 | (ThisPrec == NextTokPrec && isRightAssoc)) { | 
|  | 304 | // If this is left-associative, only parse things on the RHS that bind | 
|  | 305 | // more tightly than the current operator.  If it is left-associative, it | 
|  | 306 | // is okay, to bind exactly as tightly.  For example, compile A=B=C=D as | 
|  | 307 | // A=(B=(C=D)), where each paren is a level of recursion here. | 
|  | 308 | RHS = ParseRHSOfBinaryExpression(RHS, ThisPrec + !isRightAssoc); | 
| Chris Lattner | 214cbaf | 2007-08-31 04:58:34 +0000 | [diff] [blame] | 309 | if (RHS.isInvalid) { | 
|  | 310 | Actions.DeleteExpr(LHS.Val); | 
|  | 311 | Actions.DeleteExpr(TernaryMiddle.Val); | 
|  | 312 | return RHS; | 
|  | 313 | } | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 314 |  | 
|  | 315 | NextTokPrec = getBinOpPrecedence(Tok.getKind()); | 
|  | 316 | } | 
|  | 317 | assert(NextTokPrec <= ThisPrec && "Recursion didn't work!"); | 
|  | 318 |  | 
| Chris Lattner | 4a149b6 | 2007-08-31 05:01:50 +0000 | [diff] [blame] | 319 | if (!LHS.isInvalid) { | 
|  | 320 | // Combine the LHS and RHS into the LHS (e.g. build AST). | 
|  | 321 | if (TernaryMiddle.isInvalid) | 
| Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 322 | LHS = Actions.ActOnBinOp(OpToken.getLocation(), OpToken.getKind(), | 
| Chris Lattner | 4a149b6 | 2007-08-31 05:01:50 +0000 | [diff] [blame] | 323 | LHS.Val, RHS.Val); | 
|  | 324 | else | 
| Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 325 | LHS = Actions.ActOnConditionalOp(OpToken.getLocation(), ColonLoc, | 
| Chris Lattner | 4a149b6 | 2007-08-31 05:01:50 +0000 | [diff] [blame] | 326 | LHS.Val, TernaryMiddle.Val, RHS.Val); | 
|  | 327 | } else { | 
|  | 328 | // We had a semantic error on the LHS.  Just free the RHS and continue. | 
|  | 329 | Actions.DeleteExpr(TernaryMiddle.Val); | 
|  | 330 | Actions.DeleteExpr(RHS.Val); | 
|  | 331 | } | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 332 | } | 
|  | 333 | } | 
|  | 334 |  | 
|  | 335 | /// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is | 
|  | 336 | /// true, parse a unary-expression. | 
|  | 337 | /// | 
|  | 338 | ///       cast-expression: [C99 6.5.4] | 
|  | 339 | ///         unary-expression | 
|  | 340 | ///         '(' type-name ')' cast-expression | 
|  | 341 | /// | 
|  | 342 | ///       unary-expression:  [C99 6.5.3] | 
|  | 343 | ///         postfix-expression | 
|  | 344 | ///         '++' unary-expression | 
|  | 345 | ///         '--' unary-expression | 
|  | 346 | ///         unary-operator cast-expression | 
|  | 347 | ///         'sizeof' unary-expression | 
|  | 348 | ///         'sizeof' '(' type-name ')' | 
|  | 349 | /// [GNU]   '__alignof' unary-expression | 
|  | 350 | /// [GNU]   '__alignof' '(' type-name ')' | 
|  | 351 | /// [GNU]   '&&' identifier | 
|  | 352 | /// | 
|  | 353 | ///       unary-operator: one of | 
|  | 354 | ///         '&'  '*'  '+'  '-'  '~'  '!' | 
|  | 355 | /// [GNU]   '__extension__'  '__real'  '__imag' | 
|  | 356 | /// | 
|  | 357 | ///       primary-expression: [C99 6.5.1] | 
|  | 358 | ///         identifier | 
|  | 359 | ///         constant | 
|  | 360 | ///         string-literal | 
|  | 361 | /// [C++]   boolean-literal  [C++ 2.13.5] | 
|  | 362 | ///         '(' expression ')' | 
|  | 363 | ///         '__func__'        [C99 6.4.2.2] | 
|  | 364 | /// [GNU]   '__FUNCTION__' | 
|  | 365 | /// [GNU]   '__PRETTY_FUNCTION__' | 
|  | 366 | /// [GNU]   '(' compound-statement ')' | 
|  | 367 | /// [GNU]   '__builtin_va_arg' '(' assignment-expression ',' type-name ')' | 
|  | 368 | /// [GNU]   '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')' | 
|  | 369 | /// [GNU]   '__builtin_choose_expr' '(' assign-expr ',' assign-expr ',' | 
|  | 370 | ///                                     assign-expr ')' | 
|  | 371 | /// [GNU]   '__builtin_types_compatible_p' '(' type-name ',' type-name ')' | 
| Fariborz Jahanian | 628abf1 | 2007-09-26 17:03:44 +0000 | [diff] [blame] | 372 | /// [OBJC]  '[' objc-message-expr ']' | 
| Chris Lattner | 8d050af | 2008-01-25 18:58:06 +0000 | [diff] [blame] | 373 | /// [OBJC]  '@selector' '(' objc-selector-arg ')' | 
| Fariborz Jahanian | 628abf1 | 2007-09-26 17:03:44 +0000 | [diff] [blame] | 374 | /// [OBJC]  '@protocol' '(' identifier ')' | 
|  | 375 | /// [OBJC]  '@encode' '(' type-name ')' | 
| Fariborz Jahanian | 1e534dc | 2007-09-05 19:52:07 +0000 | [diff] [blame] | 376 | /// [OBJC]  objc-string-literal | 
| Argiris Kirtzidis | 7a1e741 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 377 | /// [C++]   simple-type-specifier '(' expression-list[opt] ')'      [C++ 5.2.3] | 
|  | 378 | /// [C++]   typename-specifier '(' expression-list[opt] ')'         [TODO] | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 379 | /// [C++]   'const_cast' '<' type-name '>' '(' expression ')'       [C++ 5.2p1] | 
|  | 380 | /// [C++]   'dynamic_cast' '<' type-name '>' '(' expression ')'     [C++ 5.2p1] | 
|  | 381 | /// [C++]   'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1] | 
|  | 382 | /// [C++]   'static_cast' '<' type-name '>' '(' expression ')'      [C++ 5.2p1] | 
| Argiris Kirtzidis | fdde276 | 2008-07-16 07:23:27 +0000 | [diff] [blame] | 383 | /// [C++]   'this'          [C++ 9.3.2] | 
| Steve Naroff | fd5b19d | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 384 | /// [clang] '^' block-literal | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 385 | /// | 
|  | 386 | ///       constant: [C99 6.4.4] | 
|  | 387 | ///         integer-constant | 
|  | 388 | ///         floating-constant | 
|  | 389 | ///         enumeration-constant -> identifier | 
|  | 390 | ///         character-constant | 
|  | 391 | /// | 
|  | 392 | Parser::ExprResult Parser::ParseCastExpression(bool isUnaryExpression) { | 
|  | 393 | ExprResult Res; | 
|  | 394 | tok::TokenKind SavedKind = Tok.getKind(); | 
|  | 395 |  | 
|  | 396 | // This handles all of cast-expression, unary-expression, postfix-expression, | 
|  | 397 | // and primary-expression.  We handle them together like this for efficiency | 
|  | 398 | // and to simplify handling of an expression starting with a '(' token: which | 
|  | 399 | // may be one of a parenthesized expression, cast-expression, compound literal | 
|  | 400 | // expression, or statement expression. | 
|  | 401 | // | 
|  | 402 | // If the parsed tokens consist of a primary-expression, the cases below | 
|  | 403 | // call ParsePostfixExpressionSuffix to handle the postfix expression | 
|  | 404 | // suffixes.  Cases that cannot be followed by postfix exprs should | 
|  | 405 | // return without invoking ParsePostfixExpressionSuffix. | 
|  | 406 | switch (SavedKind) { | 
|  | 407 | case tok::l_paren: { | 
|  | 408 | // If this expression is limited to being a unary-expression, the parent can | 
|  | 409 | // not start a cast expression. | 
|  | 410 | ParenParseOption ParenExprType = | 
|  | 411 | isUnaryExpression ? CompoundLiteral : CastExpr; | 
|  | 412 | TypeTy *CastTy; | 
|  | 413 | SourceLocation LParenLoc = Tok.getLocation(); | 
|  | 414 | SourceLocation RParenLoc; | 
|  | 415 | Res = ParseParenExpression(ParenExprType, CastTy, RParenLoc); | 
|  | 416 | if (Res.isInvalid) return Res; | 
|  | 417 |  | 
|  | 418 | switch (ParenExprType) { | 
|  | 419 | case SimpleExpr:   break;    // Nothing else to do. | 
|  | 420 | case CompoundStmt: break;  // Nothing else to do. | 
|  | 421 | case CompoundLiteral: | 
|  | 422 | // We parsed '(' type-name ')' '{' ... '}'.  If any suffixes of | 
|  | 423 | // postfix-expression exist, parse them now. | 
|  | 424 | break; | 
|  | 425 | case CastExpr: | 
|  | 426 | // We parsed '(' type-name ')' and the thing after it wasn't a '{'.  Parse | 
|  | 427 | // the cast-expression that follows it next. | 
|  | 428 | // TODO: For cast expression with CastTy. | 
|  | 429 | Res = ParseCastExpression(false); | 
|  | 430 | if (!Res.isInvalid) | 
| Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 431 | Res = Actions.ActOnCastExpr(LParenLoc, CastTy, RParenLoc, Res.Val); | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 432 | return Res; | 
|  | 433 | } | 
|  | 434 |  | 
|  | 435 | // These can be followed by postfix-expr pieces. | 
|  | 436 | return ParsePostfixExpressionSuffix(Res); | 
|  | 437 | } | 
|  | 438 |  | 
|  | 439 | // primary-expression | 
|  | 440 | case tok::numeric_constant: | 
|  | 441 | // constant: integer-constant | 
|  | 442 | // constant: floating-constant | 
|  | 443 |  | 
| Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 444 | Res = Actions.ActOnNumericConstant(Tok); | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 445 | ConsumeToken(); | 
|  | 446 |  | 
|  | 447 | // These can be followed by postfix-expr pieces. | 
|  | 448 | return ParsePostfixExpressionSuffix(Res); | 
|  | 449 |  | 
|  | 450 | case tok::kw_true: | 
|  | 451 | case tok::kw_false: | 
|  | 452 | return ParseCXXBoolLiteral(); | 
|  | 453 |  | 
| Argiris Kirtzidis | 7a1e741 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 454 | case tok::identifier: { | 
|  | 455 | if (getLang().CPlusPlus && | 
|  | 456 | Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) { | 
|  | 457 | // Handle C++ function-style cast, e.g. "T(4.5)" where T is a typedef for | 
|  | 458 | // double. | 
|  | 459 | goto HandleType; | 
|  | 460 | } | 
|  | 461 |  | 
|  | 462 | // primary-expression: identifier | 
|  | 463 | // constant: enumeration-constant | 
|  | 464 |  | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 465 | // Consume the identifier so that we can see if it is followed by a '('. | 
|  | 466 | // Function designators are allowed to be undeclared (C99 6.5.1p2), so we | 
|  | 467 | // need to know whether or not this identifier is a function designator or | 
|  | 468 | // not. | 
|  | 469 | IdentifierInfo &II = *Tok.getIdentifierInfo(); | 
|  | 470 | SourceLocation L = ConsumeToken(); | 
| Chris Lattner | 4d7d234 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 471 | Res = Actions.ActOnIdentifierExpr(CurScope, L, II, Tok.is(tok::l_paren)); | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 472 | // These can be followed by postfix-expr pieces. | 
|  | 473 | return ParsePostfixExpressionSuffix(Res); | 
|  | 474 | } | 
|  | 475 | case tok::char_constant:     // constant: character-constant | 
| Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 476 | Res = Actions.ActOnCharacterConstant(Tok); | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 477 | ConsumeToken(); | 
|  | 478 | // These can be followed by postfix-expr pieces. | 
|  | 479 | return ParsePostfixExpressionSuffix(Res); | 
|  | 480 | case tok::kw___func__:       // primary-expression: __func__ [C99 6.4.2.2] | 
|  | 481 | case tok::kw___FUNCTION__:   // primary-expression: __FUNCTION__ [GNU] | 
|  | 482 | case tok::kw___PRETTY_FUNCTION__:  // primary-expression: __P..Y_F..N__ [GNU] | 
| Chris Lattner | 6990929 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 483 | Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind); | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 484 | ConsumeToken(); | 
|  | 485 | // These can be followed by postfix-expr pieces. | 
|  | 486 | return ParsePostfixExpressionSuffix(Res); | 
|  | 487 | case tok::string_literal:    // primary-expression: string-literal | 
|  | 488 | case tok::wide_string_literal: | 
|  | 489 | Res = ParseStringLiteralExpression(); | 
|  | 490 | if (Res.isInvalid) return Res; | 
|  | 491 | // This can be followed by postfix-expr pieces (e.g. "foo"[1]). | 
|  | 492 | return ParsePostfixExpressionSuffix(Res); | 
|  | 493 | case tok::kw___builtin_va_arg: | 
|  | 494 | case tok::kw___builtin_offsetof: | 
|  | 495 | case tok::kw___builtin_choose_expr: | 
| Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 496 | case tok::kw___builtin_overload: | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 497 | case tok::kw___builtin_types_compatible_p: | 
|  | 498 | return ParseBuiltinPrimaryExpression(); | 
|  | 499 | case tok::plusplus:      // unary-expression: '++' unary-expression | 
|  | 500 | case tok::minusminus: {  // unary-expression: '--' unary-expression | 
|  | 501 | SourceLocation SavedLoc = ConsumeToken(); | 
|  | 502 | Res = ParseCastExpression(true); | 
|  | 503 | if (!Res.isInvalid) | 
| Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 504 | Res = Actions.ActOnUnaryOp(SavedLoc, SavedKind, Res.Val); | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 505 | return Res; | 
|  | 506 | } | 
|  | 507 | case tok::amp:           // unary-expression: '&' cast-expression | 
|  | 508 | case tok::star:          // unary-expression: '*' cast-expression | 
|  | 509 | case tok::plus:          // unary-expression: '+' cast-expression | 
|  | 510 | case tok::minus:         // unary-expression: '-' cast-expression | 
|  | 511 | case tok::tilde:         // unary-expression: '~' cast-expression | 
|  | 512 | case tok::exclaim:       // unary-expression: '!' cast-expression | 
|  | 513 | case tok::kw___real:     // unary-expression: '__real' cast-expression [GNU] | 
| Chris Lattner | 6cf9294 | 2008-02-02 20:20:10 +0000 | [diff] [blame] | 514 | case tok::kw___imag: {   // unary-expression: '__imag' cast-expression [GNU] | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 515 | SourceLocation SavedLoc = ConsumeToken(); | 
|  | 516 | Res = ParseCastExpression(false); | 
|  | 517 | if (!Res.isInvalid) | 
| Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 518 | Res = Actions.ActOnUnaryOp(SavedLoc, SavedKind, Res.Val); | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 519 | return Res; | 
| Chris Lattner | 6cf9294 | 2008-02-02 20:20:10 +0000 | [diff] [blame] | 520 | } | 
|  | 521 |  | 
|  | 522 | case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU] | 
|  | 523 | // __extension__ silences extension warnings in the subexpression. | 
|  | 524 | bool SavedExtWarn = Diags.getWarnOnExtensions(); | 
|  | 525 | Diags.setWarnOnExtensions(false); | 
|  | 526 | SourceLocation SavedLoc = ConsumeToken(); | 
|  | 527 | Res = ParseCastExpression(false); | 
|  | 528 | if (!Res.isInvalid) | 
|  | 529 | Res = Actions.ActOnUnaryOp(SavedLoc, SavedKind, Res.Val); | 
|  | 530 | Diags.setWarnOnExtensions(SavedExtWarn); | 
|  | 531 | return Res; | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 532 | } | 
|  | 533 | case tok::kw_sizeof:     // unary-expression: 'sizeof' unary-expression | 
|  | 534 | // unary-expression: 'sizeof' '(' type-name ')' | 
|  | 535 | case tok::kw___alignof:  // unary-expression: '__alignof' unary-expression | 
|  | 536 | // unary-expression: '__alignof' '(' type-name ')' | 
|  | 537 | return ParseSizeofAlignofExpression(); | 
|  | 538 | case tok::ampamp: {      // unary-expression: '&&' identifier | 
|  | 539 | SourceLocation AmpAmpLoc = ConsumeToken(); | 
| Chris Lattner | 4d7d234 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 540 | if (Tok.isNot(tok::identifier)) { | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 541 | Diag(Tok, diag::err_expected_ident); | 
|  | 542 | return ExprResult(true); | 
|  | 543 | } | 
|  | 544 |  | 
|  | 545 | Diag(AmpAmpLoc, diag::ext_gnu_address_of_label); | 
| Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 546 | Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(), | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 547 | Tok.getIdentifierInfo()); | 
|  | 548 | ConsumeToken(); | 
|  | 549 | return Res; | 
|  | 550 | } | 
|  | 551 | case tok::kw_const_cast: | 
|  | 552 | case tok::kw_dynamic_cast: | 
|  | 553 | case tok::kw_reinterpret_cast: | 
|  | 554 | case tok::kw_static_cast: | 
| Argiris Kirtzidis | 4963ee4 | 2008-08-16 19:45:32 +0000 | [diff] [blame] | 555 | Res = ParseCXXCasts(); | 
|  | 556 | // These can be followed by postfix-expr pieces. | 
|  | 557 | return ParsePostfixExpressionSuffix(Res); | 
| Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 558 | case tok::kw_this: | 
| Argiris Kirtzidis | 8f8a52a | 2008-08-16 19:34:46 +0000 | [diff] [blame] | 559 | Res = ParseCXXThis(); | 
|  | 560 | // This can be followed by postfix-expr pieces. | 
|  | 561 | return ParsePostfixExpressionSuffix(Res); | 
| Argiris Kirtzidis | 7a1e741 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 562 |  | 
|  | 563 | case tok::kw_char: | 
|  | 564 | case tok::kw_wchar_t: | 
|  | 565 | case tok::kw_bool: | 
|  | 566 | case tok::kw_short: | 
|  | 567 | case tok::kw_int: | 
|  | 568 | case tok::kw_long: | 
|  | 569 | case tok::kw_signed: | 
|  | 570 | case tok::kw_unsigned: | 
|  | 571 | case tok::kw_float: | 
|  | 572 | case tok::kw_double: | 
|  | 573 | case tok::kw_void: | 
|  | 574 | case tok::kw_typeof: { | 
|  | 575 | if (!getLang().CPlusPlus) | 
|  | 576 | goto UnhandledToken; | 
|  | 577 | HandleType: | 
|  | 578 | // postfix-expression: simple-type-specifier '(' expression-list[opt] ')' | 
|  | 579 | // | 
|  | 580 | DeclSpec DS; | 
|  | 581 | ParseCXXSimpleTypeSpecifier(DS); | 
|  | 582 | if (Tok.isNot(tok::l_paren)) | 
|  | 583 | return Diag(Tok.getLocation(), diag::err_expected_lparen_after_type, | 
|  | 584 | DS.getSourceRange()); | 
|  | 585 |  | 
|  | 586 | Res = ParseCXXTypeConstructExpression(DS); | 
|  | 587 | // This can be followed by postfix-expr pieces. | 
|  | 588 | return ParsePostfixExpressionSuffix(Res); | 
|  | 589 | } | 
|  | 590 |  | 
| Chris Lattner | b82d6ef | 2007-10-03 22:03:06 +0000 | [diff] [blame] | 591 | case tok::at: { | 
|  | 592 | SourceLocation AtLoc = ConsumeToken(); | 
| Steve Naroff | fb9dd75 | 2007-10-15 20:55:58 +0000 | [diff] [blame] | 593 | return ParseObjCAtExpression(AtLoc); | 
| Chris Lattner | b82d6ef | 2007-10-03 22:03:06 +0000 | [diff] [blame] | 594 | } | 
| Fariborz Jahanian | 1e534dc | 2007-09-05 19:52:07 +0000 | [diff] [blame] | 595 | case tok::l_square: | 
| Steve Naroff | fb9dd75 | 2007-10-15 20:55:58 +0000 | [diff] [blame] | 596 | // These can be followed by postfix-expr pieces. | 
| Chris Lattner | 02d3c73 | 2008-05-09 05:28:21 +0000 | [diff] [blame] | 597 | if (getLang().ObjC1) | 
|  | 598 | return ParsePostfixExpressionSuffix(ParseObjCMessageExpression()); | 
|  | 599 | // FALL THROUGH. | 
| Steve Naroff | fd5b19d | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 600 | case tok::caret: | 
|  | 601 | if (getLang().Blocks) | 
|  | 602 | return ParsePostfixExpressionSuffix(ParseBlockLiteralExpression()); | 
|  | 603 | Diag(Tok, diag::err_expected_expression); | 
|  | 604 | return ExprResult(true); | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 605 | default: | 
| Argiris Kirtzidis | 7a1e741 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 606 | UnhandledToken: | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 607 | Diag(Tok, diag::err_expected_expression); | 
|  | 608 | return ExprResult(true); | 
|  | 609 | } | 
|  | 610 |  | 
|  | 611 | // unreachable. | 
|  | 612 | abort(); | 
|  | 613 | } | 
|  | 614 |  | 
|  | 615 | /// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression | 
|  | 616 | /// is parsed, this method parses any suffixes that apply. | 
|  | 617 | /// | 
|  | 618 | ///       postfix-expression: [C99 6.5.2] | 
|  | 619 | ///         primary-expression | 
|  | 620 | ///         postfix-expression '[' expression ']' | 
|  | 621 | ///         postfix-expression '(' argument-expression-list[opt] ')' | 
|  | 622 | ///         postfix-expression '.' identifier | 
|  | 623 | ///         postfix-expression '->' identifier | 
|  | 624 | ///         postfix-expression '++' | 
|  | 625 | ///         postfix-expression '--' | 
|  | 626 | ///         '(' type-name ')' '{' initializer-list '}' | 
|  | 627 | ///         '(' type-name ')' '{' initializer-list ',' '}' | 
|  | 628 | /// | 
|  | 629 | ///       argument-expression-list: [C99 6.5.2] | 
|  | 630 | ///         argument-expression | 
|  | 631 | ///         argument-expression-list ',' assignment-expression | 
|  | 632 | /// | 
|  | 633 | Parser::ExprResult Parser::ParsePostfixExpressionSuffix(ExprResult LHS) { | 
|  | 634 |  | 
|  | 635 | // Now that the primary-expression piece of the postfix-expression has been | 
|  | 636 | // parsed, see if there are any postfix-expression pieces here. | 
|  | 637 | SourceLocation Loc; | 
|  | 638 | while (1) { | 
|  | 639 | switch (Tok.getKind()) { | 
|  | 640 | default:  // Not a postfix-expression suffix. | 
|  | 641 | return LHS; | 
|  | 642 | case tok::l_square: {  // postfix-expression: p-e '[' expression ']' | 
|  | 643 | Loc = ConsumeBracket(); | 
|  | 644 | ExprResult Idx = ParseExpression(); | 
|  | 645 |  | 
|  | 646 | SourceLocation RLoc = Tok.getLocation(); | 
|  | 647 |  | 
| Chris Lattner | 4d7d234 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 648 | if (!LHS.isInvalid && !Idx.isInvalid && Tok.is(tok::r_square)) | 
| Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 649 | LHS = Actions.ActOnArraySubscriptExpr(LHS.Val, Loc, Idx.Val, RLoc); | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 650 | else | 
|  | 651 | LHS = ExprResult(true); | 
|  | 652 |  | 
|  | 653 | // Match the ']'. | 
|  | 654 | MatchRHSPunctuation(tok::r_square, Loc); | 
|  | 655 | break; | 
|  | 656 | } | 
|  | 657 |  | 
|  | 658 | case tok::l_paren: {   // p-e: p-e '(' argument-expression-list[opt] ')' | 
| Argiris Kirtzidis | 83dce11 | 2008-08-16 20:03:01 +0000 | [diff] [blame] | 659 | ExprListTy ArgExprs; | 
|  | 660 | CommaLocsTy CommaLocs; | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 661 |  | 
|  | 662 | Loc = ConsumeParen(); | 
|  | 663 |  | 
| Chris Lattner | 4d7d234 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 664 | if (Tok.isNot(tok::r_paren)) { | 
| Argiris Kirtzidis | 83dce11 | 2008-08-16 20:03:01 +0000 | [diff] [blame] | 665 | if (ParseExpressionList(ArgExprs, CommaLocs)) { | 
|  | 666 | SkipUntil(tok::r_paren); | 
|  | 667 | return ExprResult(true); | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 668 | } | 
|  | 669 | } | 
|  | 670 |  | 
|  | 671 | // Match the ')'. | 
| Chris Lattner | 4d7d234 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 672 | if (!LHS.isInvalid && Tok.is(tok::r_paren)) { | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 673 | assert((ArgExprs.size() == 0 || ArgExprs.size()-1 == CommaLocs.size())&& | 
|  | 674 | "Unexpected number of commas!"); | 
| Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 675 | LHS = Actions.ActOnCallExpr(LHS.Val, Loc, &ArgExprs[0], ArgExprs.size(), | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 676 | &CommaLocs[0], Tok.getLocation()); | 
|  | 677 | } | 
|  | 678 |  | 
|  | 679 | MatchRHSPunctuation(tok::r_paren, Loc); | 
|  | 680 | break; | 
|  | 681 | } | 
|  | 682 | case tok::arrow:       // postfix-expression: p-e '->' identifier | 
|  | 683 | case tok::period: {    // postfix-expression: p-e '.' identifier | 
|  | 684 | tok::TokenKind OpKind = Tok.getKind(); | 
|  | 685 | SourceLocation OpLoc = ConsumeToken();  // Eat the "." or "->" token. | 
|  | 686 |  | 
| Chris Lattner | 4d7d234 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 687 | if (Tok.isNot(tok::identifier)) { | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 688 | Diag(Tok, diag::err_expected_ident); | 
|  | 689 | return ExprResult(true); | 
|  | 690 | } | 
|  | 691 |  | 
|  | 692 | if (!LHS.isInvalid) | 
| Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 693 | LHS = Actions.ActOnMemberReferenceExpr(LHS.Val, OpLoc, OpKind, | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 694 | Tok.getLocation(), | 
|  | 695 | *Tok.getIdentifierInfo()); | 
|  | 696 | ConsumeToken(); | 
|  | 697 | break; | 
|  | 698 | } | 
|  | 699 | case tok::plusplus:    // postfix-expression: postfix-expression '++' | 
|  | 700 | case tok::minusminus:  // postfix-expression: postfix-expression '--' | 
|  | 701 | if (!LHS.isInvalid) | 
| Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 702 | LHS = Actions.ActOnPostfixUnaryOp(Tok.getLocation(), Tok.getKind(), | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 703 | LHS.Val); | 
|  | 704 | ConsumeToken(); | 
|  | 705 | break; | 
|  | 706 | } | 
|  | 707 | } | 
|  | 708 | } | 
|  | 709 |  | 
|  | 710 |  | 
|  | 711 | /// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression. | 
|  | 712 | ///       unary-expression:  [C99 6.5.3] | 
|  | 713 | ///         'sizeof' unary-expression | 
|  | 714 | ///         'sizeof' '(' type-name ')' | 
|  | 715 | /// [GNU]   '__alignof' unary-expression | 
|  | 716 | /// [GNU]   '__alignof' '(' type-name ')' | 
|  | 717 | Parser::ExprResult Parser::ParseSizeofAlignofExpression() { | 
| Chris Lattner | 4d7d234 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 718 | assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof)) && | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 719 | "Not a sizeof/alignof expression!"); | 
|  | 720 | Token OpTok = Tok; | 
|  | 721 | ConsumeToken(); | 
|  | 722 |  | 
|  | 723 | // If the operand doesn't start with an '(', it must be an expression. | 
|  | 724 | ExprResult Operand; | 
| Chris Lattner | 4d7d234 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 725 | if (Tok.isNot(tok::l_paren)) { | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 726 | Operand = ParseCastExpression(true); | 
|  | 727 | } else { | 
|  | 728 | // If it starts with a '(', we know that it is either a parenthesized | 
|  | 729 | // type-name, or it is a unary-expression that starts with a compound | 
|  | 730 | // literal, or starts with a primary-expression that is a parenthesized | 
|  | 731 | // expression. | 
|  | 732 | ParenParseOption ExprType = CastExpr; | 
|  | 733 | TypeTy *CastTy; | 
|  | 734 | SourceLocation LParenLoc = Tok.getLocation(), RParenLoc; | 
|  | 735 | Operand = ParseParenExpression(ExprType, CastTy, RParenLoc); | 
|  | 736 |  | 
|  | 737 | // If ParseParenExpression parsed a '(typename)' sequence only, the this is | 
|  | 738 | // sizeof/alignof a type.  Otherwise, it is sizeof/alignof an expression. | 
| Chris Lattner | 4855356 | 2007-11-13 20:50:37 +0000 | [diff] [blame] | 739 | if (ExprType == CastExpr) | 
| Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 740 | return Actions.ActOnSizeOfAlignOfTypeExpr(OpTok.getLocation(), | 
| Chris Lattner | 4d7d234 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 741 | OpTok.is(tok::kw_sizeof), | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 742 | LParenLoc, CastTy, RParenLoc); | 
| Chris Lattner | 4855356 | 2007-11-13 20:50:37 +0000 | [diff] [blame] | 743 |  | 
|  | 744 | // If this is a parenthesized expression, it is the start of a | 
|  | 745 | // unary-expression, but doesn't include any postfix pieces.  Parse these | 
|  | 746 | // now if present. | 
|  | 747 | Operand = ParsePostfixExpressionSuffix(Operand); | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 748 | } | 
|  | 749 |  | 
|  | 750 | // If we get here, the operand to the sizeof/alignof was an expresion. | 
|  | 751 | if (!Operand.isInvalid) | 
| Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 752 | Operand = Actions.ActOnUnaryOp(OpTok.getLocation(), OpTok.getKind(), | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 753 | Operand.Val); | 
|  | 754 | return Operand; | 
|  | 755 | } | 
|  | 756 |  | 
|  | 757 | /// ParseBuiltinPrimaryExpression | 
|  | 758 | /// | 
|  | 759 | ///       primary-expression: [C99 6.5.1] | 
|  | 760 | /// [GNU]   '__builtin_va_arg' '(' assignment-expression ',' type-name ')' | 
|  | 761 | /// [GNU]   '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')' | 
|  | 762 | /// [GNU]   '__builtin_choose_expr' '(' assign-expr ',' assign-expr ',' | 
|  | 763 | ///                                     assign-expr ')' | 
|  | 764 | /// [GNU]   '__builtin_types_compatible_p' '(' type-name ',' type-name ')' | 
| Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 765 | /// [CLANG] '__builtin_overload' '(' expr (',' expr)* ')' | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 766 | /// | 
|  | 767 | /// [GNU] offsetof-member-designator: | 
|  | 768 | /// [GNU]   identifier | 
|  | 769 | /// [GNU]   offsetof-member-designator '.' identifier | 
|  | 770 | /// [GNU]   offsetof-member-designator '[' expression ']' | 
|  | 771 | /// | 
|  | 772 | Parser::ExprResult Parser::ParseBuiltinPrimaryExpression() { | 
|  | 773 | ExprResult Res(false); | 
|  | 774 | const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo(); | 
|  | 775 |  | 
|  | 776 | tok::TokenKind T = Tok.getKind(); | 
|  | 777 | SourceLocation StartLoc = ConsumeToken();   // Eat the builtin identifier. | 
|  | 778 |  | 
|  | 779 | // All of these start with an open paren. | 
| Chris Lattner | 4d7d234 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 780 | if (Tok.isNot(tok::l_paren)) { | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 781 | Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName()); | 
|  | 782 | return ExprResult(true); | 
|  | 783 | } | 
|  | 784 |  | 
|  | 785 | SourceLocation LParenLoc = ConsumeParen(); | 
|  | 786 | // TODO: Build AST. | 
|  | 787 |  | 
|  | 788 | switch (T) { | 
|  | 789 | default: assert(0 && "Not a builtin primary expression!"); | 
| Anders Carlsson | 3676033 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 790 | case tok::kw___builtin_va_arg: { | 
|  | 791 | ExprResult Expr = ParseAssignmentExpression(); | 
|  | 792 | if (Expr.isInvalid) { | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 793 | SkipUntil(tok::r_paren); | 
| Eli Friedman | a1b6d80 | 2008-08-20 22:07:34 +0000 | [diff] [blame] | 794 | return ExprResult(true); | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 795 | } | 
|  | 796 |  | 
|  | 797 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) | 
|  | 798 | return ExprResult(true); | 
|  | 799 |  | 
| Anders Carlsson | 3676033 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 800 | TypeTy *Ty = ParseTypeName(); | 
| Chris Lattner | cb8943a | 2007-08-30 15:52:49 +0000 | [diff] [blame] | 801 |  | 
| Anders Carlsson | 3676033 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 802 | if (Tok.isNot(tok::r_paren)) { | 
|  | 803 | Diag(Tok, diag::err_expected_rparen); | 
|  | 804 | return ExprResult(true); | 
|  | 805 | } | 
|  | 806 | Res = Actions.ActOnVAArg(StartLoc, Expr.Val, Ty, ConsumeParen()); | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 807 | break; | 
| Anders Carlsson | 3676033 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 808 | } | 
| Chris Lattner | 69638b1 | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 809 | case tok::kw___builtin_offsetof: { | 
| Chris Lattner | 1b6b5be | 2007-08-30 17:08:45 +0000 | [diff] [blame] | 810 | SourceLocation TypeLoc = Tok.getLocation(); | 
| Chris Lattner | 69638b1 | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 811 | TypeTy *Ty = ParseTypeName(); | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 812 |  | 
|  | 813 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) | 
|  | 814 | return ExprResult(true); | 
|  | 815 |  | 
|  | 816 | // We must have at least one identifier here. | 
| Chris Lattner | 4d7d234 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 817 | if (Tok.isNot(tok::identifier)) { | 
| Chris Lattner | 69638b1 | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 818 | Diag(Tok, diag::err_expected_ident); | 
|  | 819 | SkipUntil(tok::r_paren); | 
|  | 820 | return true; | 
|  | 821 | } | 
|  | 822 |  | 
|  | 823 | // Keep track of the various subcomponents we see. | 
|  | 824 | llvm::SmallVector<Action::OffsetOfComponent, 4> Comps; | 
|  | 825 |  | 
|  | 826 | Comps.push_back(Action::OffsetOfComponent()); | 
|  | 827 | Comps.back().isBrackets = false; | 
|  | 828 | Comps.back().U.IdentInfo = Tok.getIdentifierInfo(); | 
|  | 829 | Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken(); | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 830 |  | 
|  | 831 | while (1) { | 
| Chris Lattner | 4d7d234 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 832 | if (Tok.is(tok::period)) { | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 833 | // offsetof-member-designator: offsetof-member-designator '.' identifier | 
| Chris Lattner | 69638b1 | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 834 | Comps.push_back(Action::OffsetOfComponent()); | 
|  | 835 | Comps.back().isBrackets = false; | 
|  | 836 | Comps.back().LocStart = ConsumeToken(); | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 837 |  | 
| Chris Lattner | 4d7d234 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 838 | if (Tok.isNot(tok::identifier)) { | 
| Chris Lattner | 69638b1 | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 839 | Diag(Tok, diag::err_expected_ident); | 
|  | 840 | SkipUntil(tok::r_paren); | 
|  | 841 | return true; | 
|  | 842 | } | 
|  | 843 | Comps.back().U.IdentInfo = Tok.getIdentifierInfo(); | 
|  | 844 | Comps.back().LocEnd = ConsumeToken(); | 
|  | 845 |  | 
| Chris Lattner | 4d7d234 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 846 | } else if (Tok.is(tok::l_square)) { | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 847 | // offsetof-member-designator: offsetof-member-design '[' expression ']' | 
| Chris Lattner | 69638b1 | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 848 | Comps.push_back(Action::OffsetOfComponent()); | 
|  | 849 | Comps.back().isBrackets = true; | 
|  | 850 | Comps.back().LocStart = ConsumeBracket(); | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 851 | Res = ParseExpression(); | 
|  | 852 | if (Res.isInvalid) { | 
|  | 853 | SkipUntil(tok::r_paren); | 
|  | 854 | return Res; | 
|  | 855 | } | 
| Chris Lattner | 69638b1 | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 856 | Comps.back().U.E = Res.Val; | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 857 |  | 
| Chris Lattner | 69638b1 | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 858 | Comps.back().LocEnd = | 
|  | 859 | MatchRHSPunctuation(tok::r_square, Comps.back().LocStart); | 
| Chris Lattner | 4d7d234 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 860 | } else if (Tok.is(tok::r_paren)) { | 
| Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 861 | Res = Actions.ActOnBuiltinOffsetOf(StartLoc, TypeLoc, Ty, &Comps[0], | 
| Chris Lattner | cb8943a | 2007-08-30 15:52:49 +0000 | [diff] [blame] | 862 | Comps.size(), ConsumeParen()); | 
|  | 863 | break; | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 864 | } else { | 
| Chris Lattner | 69638b1 | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 865 | // Error occurred. | 
|  | 866 | return ExprResult(true); | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 867 | } | 
|  | 868 | } | 
|  | 869 | break; | 
| Chris Lattner | 69638b1 | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 870 | } | 
| Steve Naroff | 93c5301 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 871 | case tok::kw___builtin_choose_expr: { | 
|  | 872 | ExprResult Cond = ParseAssignmentExpression(); | 
|  | 873 | if (Cond.isInvalid) { | 
|  | 874 | SkipUntil(tok::r_paren); | 
|  | 875 | return Cond; | 
|  | 876 | } | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 877 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) | 
|  | 878 | return ExprResult(true); | 
|  | 879 |  | 
| Steve Naroff | 93c5301 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 880 | ExprResult Expr1 = ParseAssignmentExpression(); | 
|  | 881 | if (Expr1.isInvalid) { | 
|  | 882 | SkipUntil(tok::r_paren); | 
|  | 883 | return Expr1; | 
|  | 884 | } | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 885 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) | 
|  | 886 | return ExprResult(true); | 
|  | 887 |  | 
| Steve Naroff | 93c5301 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 888 | ExprResult Expr2 = ParseAssignmentExpression(); | 
|  | 889 | if (Expr2.isInvalid) { | 
|  | 890 | SkipUntil(tok::r_paren); | 
|  | 891 | return Expr2; | 
|  | 892 | } | 
| Chris Lattner | 4d7d234 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 893 | if (Tok.isNot(tok::r_paren)) { | 
| Steve Naroff | 93c5301 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 894 | Diag(Tok, diag::err_expected_rparen); | 
|  | 895 | return ExprResult(true); | 
|  | 896 | } | 
| Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 897 | Res = Actions.ActOnChooseExpr(StartLoc, Cond.Val, Expr1.Val, Expr2.Val, | 
| Chris Lattner | cb8943a | 2007-08-30 15:52:49 +0000 | [diff] [blame] | 898 | ConsumeParen()); | 
|  | 899 | break; | 
| Steve Naroff | 93c5301 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 900 | } | 
| Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 901 | case tok::kw___builtin_overload: { | 
|  | 902 | llvm::SmallVector<ExprTy*, 8> ArgExprs; | 
|  | 903 | llvm::SmallVector<SourceLocation, 8> CommaLocs; | 
|  | 904 |  | 
|  | 905 | // For each iteration through the loop look for assign-expr followed by a | 
|  | 906 | // comma.  If there is no comma, break and attempt to match r-paren. | 
|  | 907 | if (Tok.isNot(tok::r_paren)) { | 
|  | 908 | while (1) { | 
|  | 909 | ExprResult ArgExpr = ParseAssignmentExpression(); | 
|  | 910 | if (ArgExpr.isInvalid) { | 
|  | 911 | SkipUntil(tok::r_paren); | 
|  | 912 | return ExprResult(true); | 
|  | 913 | } else | 
|  | 914 | ArgExprs.push_back(ArgExpr.Val); | 
|  | 915 |  | 
|  | 916 | if (Tok.isNot(tok::comma)) | 
|  | 917 | break; | 
|  | 918 | // Move to the next argument, remember where the comma was. | 
|  | 919 | CommaLocs.push_back(ConsumeToken()); | 
|  | 920 | } | 
|  | 921 | } | 
|  | 922 |  | 
|  | 923 | // Attempt to consume the r-paren | 
|  | 924 | if (Tok.isNot(tok::r_paren)) { | 
|  | 925 | Diag(Tok, diag::err_expected_rparen); | 
|  | 926 | SkipUntil(tok::r_paren); | 
|  | 927 | return ExprResult(true); | 
|  | 928 | } | 
| Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 929 | Res = Actions.ActOnOverloadExpr(&ArgExprs[0], ArgExprs.size(), | 
|  | 930 | &CommaLocs[0], StartLoc, ConsumeParen()); | 
|  | 931 | break; | 
|  | 932 | } | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 933 | case tok::kw___builtin_types_compatible_p: | 
| Steve Naroff | 5b52892 | 2007-08-01 23:45:51 +0000 | [diff] [blame] | 934 | TypeTy *Ty1 = ParseTypeName(); | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 935 |  | 
|  | 936 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) | 
|  | 937 | return ExprResult(true); | 
|  | 938 |  | 
| Steve Naroff | 5b52892 | 2007-08-01 23:45:51 +0000 | [diff] [blame] | 939 | TypeTy *Ty2 = ParseTypeName(); | 
|  | 940 |  | 
| Chris Lattner | 4d7d234 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 941 | if (Tok.isNot(tok::r_paren)) { | 
| Steve Naroff | 5b52892 | 2007-08-01 23:45:51 +0000 | [diff] [blame] | 942 | Diag(Tok, diag::err_expected_rparen); | 
|  | 943 | return ExprResult(true); | 
|  | 944 | } | 
| Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 945 | Res = Actions.ActOnTypesCompatibleExpr(StartLoc, Ty1, Ty2, ConsumeParen()); | 
| Chris Lattner | cb8943a | 2007-08-30 15:52:49 +0000 | [diff] [blame] | 946 | break; | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 947 | } | 
|  | 948 |  | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 949 | // These can be followed by postfix-expr pieces because they are | 
|  | 950 | // primary-expressions. | 
|  | 951 | return ParsePostfixExpressionSuffix(Res); | 
|  | 952 | } | 
|  | 953 |  | 
|  | 954 | /// ParseParenExpression - This parses the unit that starts with a '(' token, | 
|  | 955 | /// based on what is allowed by ExprType.  The actual thing parsed is returned | 
|  | 956 | /// in ExprType. | 
|  | 957 | /// | 
|  | 958 | ///       primary-expression: [C99 6.5.1] | 
|  | 959 | ///         '(' expression ')' | 
|  | 960 | /// [GNU]   '(' compound-statement ')'      (if !ParenExprOnly) | 
|  | 961 | ///       postfix-expression: [C99 6.5.2] | 
|  | 962 | ///         '(' type-name ')' '{' initializer-list '}' | 
|  | 963 | ///         '(' type-name ')' '{' initializer-list ',' '}' | 
|  | 964 | ///       cast-expression: [C99 6.5.4] | 
|  | 965 | ///         '(' type-name ')' cast-expression | 
|  | 966 | /// | 
|  | 967 | Parser::ExprResult Parser::ParseParenExpression(ParenParseOption &ExprType, | 
|  | 968 | TypeTy *&CastTy, | 
|  | 969 | SourceLocation &RParenLoc) { | 
| Chris Lattner | 4d7d234 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 970 | assert(Tok.is(tok::l_paren) && "Not a paren expr!"); | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 971 | SourceLocation OpenLoc = ConsumeParen(); | 
|  | 972 | ExprResult Result(true); | 
|  | 973 | CastTy = 0; | 
|  | 974 |  | 
| Chris Lattner | 4d7d234 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 975 | if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) { | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 976 | Diag(Tok, diag::ext_gnu_statement_expr); | 
| Chris Lattner | f2b0757 | 2007-08-31 21:49:55 +0000 | [diff] [blame] | 977 | Parser::StmtResult Stmt = ParseCompoundStatement(true); | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 978 | ExprType = CompoundStmt; | 
|  | 979 |  | 
|  | 980 | // If the substmt parsed correctly, build the AST node. | 
| Chris Lattner | 4d7d234 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 981 | if (!Stmt.isInvalid && Tok.is(tok::r_paren)) | 
| Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 982 | Result = Actions.ActOnStmtExpr(OpenLoc, Stmt.Val, Tok.getLocation()); | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 983 |  | 
|  | 984 | } else if (ExprType >= CompoundLiteral && isTypeSpecifierQualifier()) { | 
|  | 985 | // Otherwise, this is a compound literal expression or cast expression. | 
|  | 986 | TypeTy *Ty = ParseTypeName(); | 
|  | 987 |  | 
|  | 988 | // Match the ')'. | 
| Chris Lattner | 4d7d234 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 989 | if (Tok.is(tok::r_paren)) | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 990 | RParenLoc = ConsumeParen(); | 
|  | 991 | else | 
|  | 992 | MatchRHSPunctuation(tok::r_paren, OpenLoc); | 
|  | 993 |  | 
| Chris Lattner | 4d7d234 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 994 | if (Tok.is(tok::l_brace)) { | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 995 | if (!getLang().C99)   // Compound literals don't exist in C90. | 
|  | 996 | Diag(OpenLoc, diag::ext_c99_compound_literal); | 
|  | 997 | Result = ParseInitializer(); | 
|  | 998 | ExprType = CompoundLiteral; | 
|  | 999 | if (!Result.isInvalid) | 
| Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 1000 | return Actions.ActOnCompoundLiteral(OpenLoc, Ty, RParenLoc, Result.Val); | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1001 | } else if (ExprType == CastExpr) { | 
|  | 1002 | // Note that this doesn't parse the subsequence cast-expression, it just | 
|  | 1003 | // returns the parsed type to the callee. | 
|  | 1004 | ExprType = CastExpr; | 
|  | 1005 | CastTy = Ty; | 
|  | 1006 | return ExprResult(false); | 
|  | 1007 | } else { | 
|  | 1008 | Diag(Tok, diag::err_expected_lbrace_in_compound_literal); | 
|  | 1009 | return ExprResult(true); | 
|  | 1010 | } | 
|  | 1011 | return Result; | 
|  | 1012 | } else { | 
|  | 1013 | Result = ParseExpression(); | 
|  | 1014 | ExprType = SimpleExpr; | 
| Chris Lattner | 4d7d234 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1015 | if (!Result.isInvalid && Tok.is(tok::r_paren)) | 
| Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 1016 | Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), Result.Val); | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1017 | } | 
|  | 1018 |  | 
|  | 1019 | // Match the ')'. | 
|  | 1020 | if (Result.isInvalid) | 
|  | 1021 | SkipUntil(tok::r_paren); | 
|  | 1022 | else { | 
| Chris Lattner | 4d7d234 | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1023 | if (Tok.is(tok::r_paren)) | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1024 | RParenLoc = ConsumeParen(); | 
|  | 1025 | else | 
|  | 1026 | MatchRHSPunctuation(tok::r_paren, OpenLoc); | 
|  | 1027 | } | 
|  | 1028 |  | 
|  | 1029 | return Result; | 
|  | 1030 | } | 
|  | 1031 |  | 
|  | 1032 | /// ParseStringLiteralExpression - This handles the various token types that | 
|  | 1033 | /// form string literals, and also handles string concatenation [C99 5.1.1.2, | 
|  | 1034 | /// translation phase #6]. | 
|  | 1035 | /// | 
|  | 1036 | ///       primary-expression: [C99 6.5.1] | 
|  | 1037 | ///         string-literal | 
|  | 1038 | Parser::ExprResult Parser::ParseStringLiteralExpression() { | 
|  | 1039 | assert(isTokenStringLiteral() && "Not a string literal!"); | 
|  | 1040 |  | 
|  | 1041 | // String concat.  Note that keywords like __func__ and __FUNCTION__ are not | 
|  | 1042 | // considered to be strings for concatenation purposes. | 
|  | 1043 | llvm::SmallVector<Token, 4> StringToks; | 
|  | 1044 |  | 
|  | 1045 | do { | 
|  | 1046 | StringToks.push_back(Tok); | 
|  | 1047 | ConsumeStringToken(); | 
|  | 1048 | } while (isTokenStringLiteral()); | 
|  | 1049 |  | 
|  | 1050 | // Pass the set of string tokens, ready for concatenation, to the actions. | 
| Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 1051 | return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size()); | 
| Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1052 | } | 
| Argiris Kirtzidis | 83dce11 | 2008-08-16 20:03:01 +0000 | [diff] [blame] | 1053 |  | 
|  | 1054 | /// ParseExpressionList - Used for C/C++ (argument-)expression-list. | 
|  | 1055 | /// | 
|  | 1056 | ///       argument-expression-list: | 
|  | 1057 | ///         assignment-expression | 
|  | 1058 | ///         argument-expression-list , assignment-expression | 
|  | 1059 | /// | 
|  | 1060 | /// [C++] expression-list: | 
|  | 1061 | /// [C++]   assignment-expression | 
|  | 1062 | /// [C++]   expression-list , assignment-expression | 
|  | 1063 | /// | 
|  | 1064 | bool Parser::ParseExpressionList(ExprListTy &Exprs, CommaLocsTy &CommaLocs) { | 
|  | 1065 | while (1) { | 
|  | 1066 | ExprResult Expr = ParseAssignmentExpression(); | 
|  | 1067 | if (Expr.isInvalid) | 
|  | 1068 | return true; | 
| Argiris Kirtzidis | d37a418 | 2008-08-18 22:49:40 +0000 | [diff] [blame] | 1069 |  | 
|  | 1070 | Exprs.push_back(Expr.Val); | 
| Argiris Kirtzidis | 83dce11 | 2008-08-16 20:03:01 +0000 | [diff] [blame] | 1071 |  | 
|  | 1072 | if (Tok.isNot(tok::comma)) | 
|  | 1073 | return false; | 
|  | 1074 | // Move to the next argument, remember where the comma was. | 
|  | 1075 | CommaLocs.push_back(ConsumeToken()); | 
|  | 1076 | } | 
|  | 1077 | } | 
| Steve Naroff | fd5b19d | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1078 |  | 
|  | 1079 | /// ParseBlockLiteralExpression - Parse a block literal, which roughly looks | 
| Steve Naroff | a095a75 | 2008-09-16 23:11:46 +0000 | [diff] [blame^] | 1080 | /// like ^(int x){ return x+1; } | 
| Steve Naroff | fd5b19d | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1081 | /// | 
|  | 1082 | ///         block-literal: | 
|  | 1083 | /// [clang]   '^' block-args[opt] compound-statement | 
| Steve Naroff | fd5b19d | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1084 | /// [clang] block-args: | 
|  | 1085 | /// [clang]   '(' parameter-list ')' | 
|  | 1086 | /// | 
|  | 1087 | Parser::ExprResult Parser::ParseBlockLiteralExpression() { | 
|  | 1088 | assert(Tok.is(tok::caret) && "block literal starts with ^"); | 
|  | 1089 | SourceLocation CaretLoc = ConsumeToken(); | 
|  | 1090 |  | 
|  | 1091 | // Enter a scope to hold everything within the block.  This includes the | 
|  | 1092 | // argument decls, decls within the compound expression, etc.  This also | 
|  | 1093 | // allows determining whether a variable reference inside the block is | 
|  | 1094 | // within or outside of the block. | 
|  | 1095 | EnterScope(Scope::BlockScope|Scope::FnScope|Scope::BreakScope| | 
|  | 1096 | Scope::ContinueScope|Scope::DeclScope); | 
|  | 1097 |  | 
|  | 1098 | // Parse the return type if present. | 
|  | 1099 | DeclSpec DS; | 
|  | 1100 | Declarator ParamInfo(DS, Declarator::PrototypeContext); | 
|  | 1101 |  | 
|  | 1102 | // If this block has arguments, parse them.  There is no ambiguity here with | 
|  | 1103 | // the expression case, because the expression case requires a parameter list. | 
|  | 1104 | if (Tok.is(tok::l_paren)) { | 
|  | 1105 | ParseParenDeclarator(ParamInfo); | 
|  | 1106 | // Parse the pieces after the identifier as if we had "int(...)". | 
|  | 1107 | ParamInfo.SetIdentifier(0, CaretLoc); | 
|  | 1108 | if (ParamInfo.getInvalidType()) { | 
|  | 1109 | // If there was an error parsing the arguments, they may have tried to use | 
|  | 1110 | // ^(x+y) which requires an argument list.  Just skip the whole block | 
|  | 1111 | // literal. | 
|  | 1112 | ExitScope(); | 
|  | 1113 | return true; | 
|  | 1114 | } | 
|  | 1115 | } else { | 
|  | 1116 | // Otherwise, pretend we saw (void). | 
|  | 1117 | ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(true, false, | 
|  | 1118 | 0, 0, CaretLoc)); | 
|  | 1119 | } | 
|  | 1120 |  | 
|  | 1121 | // Inform sema that we are starting a block. | 
|  | 1122 | Actions.ActOnBlockStart(CaretLoc, CurScope, ParamInfo); | 
|  | 1123 |  | 
| Steve Naroff | a095a75 | 2008-09-16 23:11:46 +0000 | [diff] [blame^] | 1124 | ExprResult Result = true; | 
| Steve Naroff | fd5b19d | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1125 | if (Tok.is(tok::l_brace)) { | 
|  | 1126 | StmtResult Stmt = ParseCompoundStatementBody(); | 
|  | 1127 | if (!Stmt.isInvalid) { | 
|  | 1128 | Result = Actions.ActOnBlockStmtExpr(CaretLoc, Stmt.Val, CurScope); | 
|  | 1129 | } else { | 
|  | 1130 | Actions.ActOnBlockError(CaretLoc, CurScope); | 
| Steve Naroff | fd5b19d | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1131 | } | 
|  | 1132 | } | 
| Steve Naroff | fd5b19d | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1133 | ExitScope(); | 
|  | 1134 | return Result; | 
|  | 1135 | } | 
|  | 1136 |  |