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