Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- ParseExpr.cpp - Expression Parsing -------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 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. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +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" |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 23 | #include "clang/Parse/DeclSpec.h" |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 24 | #include "clang/Parse/Scope.h" |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 25 | #include "ExtensionRAIIObject.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 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 | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 162 | /// [C++] throw-expression [C++ 15] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +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 | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 172 | if (Tok.is(tok::kw_throw)) |
| 173 | return ParseThrowExpression(); |
| 174 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +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 | 397fcc1 | 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 | c97c204 | 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 | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 185 | /// |
Fariborz Jahanian | b384d32 | 2007-10-04 20:19:06 +0000 | [diff] [blame] | 186 | Parser::ExprResult Parser::ParseExpressionWithLeadingAt(SourceLocation AtLoc) { |
Steve Naroff | a642beb | 2007-10-15 20:55:58 +0000 | [diff] [blame] | 187 | ExprResult LHS = ParseObjCAtExpression(AtLoc); |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 188 | if (LHS.isInvalid) return LHS; |
| 189 | |
| 190 | return ParseRHSOfBinaryExpression(LHS, prec::Comma); |
| 191 | } |
| 192 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 193 | /// ParseAssignmentExpression - Parse an expr that doesn't include commas. |
| 194 | /// |
| 195 | Parser::ExprResult Parser::ParseAssignmentExpression() { |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 196 | if (Tok.is(tok::kw_throw)) |
| 197 | return ParseThrowExpression(); |
| 198 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +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 | b93fb49 | 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 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 226 | Parser::ExprResult Parser::ParseConstantExpression() { |
| 227 | ExprResult LHS = ParseCastExpression(false); |
| 228 | if (LHS.isInvalid) return LHS; |
| 229 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 230 | return ParseRHSOfBinaryExpression(LHS, prec::Conditional); |
| 231 | } |
| 232 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +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. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 248 | Token OpToken = Tok; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 249 | ConsumeToken(); |
| 250 | |
| 251 | // Special case handling for the ternary operator. |
| 252 | ExprResult TernaryMiddle(true); |
| 253 | if (NextTokPrec == prec::Conditional) { |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 254 | if (Tok.isNot(tok::colon)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +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 | dbd583c | 2007-08-31 04:58:34 +0000 | [diff] [blame] | 260 | if (TernaryMiddle.isInvalid) { |
| 261 | Actions.DeleteExpr(LHS.Val); |
| 262 | return TernaryMiddle; |
| 263 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +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 | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 271 | if (Tok.isNot(tok::colon)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 272 | Diag(Tok, diag::err_expected_colon); |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame^] | 273 | Diag(OpToken, diag::err_matching) << "?"; |
Chris Lattner | dbd583c | 2007-08-31 04:58:34 +0000 | [diff] [blame] | 274 | Actions.DeleteExpr(LHS.Val); |
| 275 | Actions.DeleteExpr(TernaryMiddle.Val); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +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 | dbd583c | 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 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +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 | d7d860d | 2007-12-18 06:06:23 +0000 | [diff] [blame] | 297 | bool isRightAssoc = ThisPrec == prec::Conditional || |
| 298 | ThisPrec == prec::Assignment; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +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 | dbd583c | 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 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 314 | |
| 315 | NextTokPrec = getBinOpPrecedence(Tok.getKind()); |
| 316 | } |
| 317 | assert(NextTokPrec <= ThisPrec && "Recursion didn't work!"); |
| 318 | |
Chris Lattner | d56d6b6 | 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) |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 322 | LHS = Actions.ActOnBinOp(CurScope, OpToken.getLocation(), |
| 323 | OpToken.getKind(), LHS.Val, RHS.Val); |
Chris Lattner | d56d6b6 | 2007-08-31 05:01:50 +0000 | [diff] [blame] | 324 | else |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 325 | LHS = Actions.ActOnConditionalOp(OpToken.getLocation(), ColonLoc, |
Chris Lattner | d56d6b6 | 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 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +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 ')' |
Douglas Gregor | 85bb3da | 2008-11-06 15:17:27 +0000 | [diff] [blame] | 351 | /// [C++0x] 'alignof' '(' type-id ')' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 352 | /// [GNU] '&&' identifier |
| 353 | /// |
| 354 | /// unary-operator: one of |
| 355 | /// '&' '*' '+' '-' '~' '!' |
| 356 | /// [GNU] '__extension__' '__real' '__imag' |
| 357 | /// |
| 358 | /// primary-expression: [C99 6.5.1] |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 359 | /// [C99] identifier |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 360 | /// [C++] id-expression |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 361 | /// constant |
| 362 | /// string-literal |
| 363 | /// [C++] boolean-literal [C++ 2.13.5] |
| 364 | /// '(' expression ')' |
| 365 | /// '__func__' [C99 6.4.2.2] |
| 366 | /// [GNU] '__FUNCTION__' |
| 367 | /// [GNU] '__PRETTY_FUNCTION__' |
| 368 | /// [GNU] '(' compound-statement ')' |
| 369 | /// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')' |
| 370 | /// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')' |
| 371 | /// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ',' |
| 372 | /// assign-expr ')' |
| 373 | /// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')' |
Fariborz Jahanian | 095ffca | 2007-09-26 17:03:44 +0000 | [diff] [blame] | 374 | /// [OBJC] '[' objc-message-expr ']' |
Chris Lattner | 5ac87ed | 2008-01-25 18:58:06 +0000 | [diff] [blame] | 375 | /// [OBJC] '@selector' '(' objc-selector-arg ')' |
Fariborz Jahanian | 095ffca | 2007-09-26 17:03:44 +0000 | [diff] [blame] | 376 | /// [OBJC] '@protocol' '(' identifier ')' |
| 377 | /// [OBJC] '@encode' '(' type-name ')' |
Fariborz Jahanian | 0ccb27d | 2007-09-05 19:52:07 +0000 | [diff] [blame] | 378 | /// [OBJC] objc-string-literal |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 379 | /// [C++] simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3] |
| 380 | /// [C++] typename-specifier '(' expression-list[opt] ')' [TODO] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 381 | /// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1] |
| 382 | /// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1] |
| 383 | /// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1] |
| 384 | /// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1] |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 385 | /// [C++] 'typeid' '(' expression ')' [C++ 5.2p1] |
| 386 | /// [C++] 'typeid' '(' type-id ')' [C++ 5.2p1] |
Argyrios Kyrtzidis | d7464be | 2008-07-16 07:23:27 +0000 | [diff] [blame] | 387 | /// [C++] 'this' [C++ 9.3.2] |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 388 | /// [clang] '^' block-literal |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 389 | /// |
| 390 | /// constant: [C99 6.4.4] |
| 391 | /// integer-constant |
| 392 | /// floating-constant |
| 393 | /// enumeration-constant -> identifier |
| 394 | /// character-constant |
| 395 | /// |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 396 | /// id-expression: [C++ 5.1] |
| 397 | /// unqualified-id |
| 398 | /// qualified-id [TODO] |
| 399 | /// |
| 400 | /// unqualified-id: [C++ 5.1] |
| 401 | /// identifier |
| 402 | /// operator-function-id |
| 403 | /// conversion-function-id [TODO] |
| 404 | /// '~' class-name [TODO] |
| 405 | /// template-id [TODO] |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 406 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 407 | Parser::ExprResult Parser::ParseCastExpression(bool isUnaryExpression) { |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 408 | if (getLang().CPlusPlus) { |
| 409 | // Annotate typenames and C++ scope specifiers. |
| 410 | // Used only in C++; in C let the typedef name be handled as an identifier. |
| 411 | TryAnnotateTypeOrScopeToken(); |
| 412 | } |
| 413 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 414 | ExprResult Res; |
| 415 | tok::TokenKind SavedKind = Tok.getKind(); |
| 416 | |
| 417 | // This handles all of cast-expression, unary-expression, postfix-expression, |
| 418 | // and primary-expression. We handle them together like this for efficiency |
| 419 | // and to simplify handling of an expression starting with a '(' token: which |
| 420 | // may be one of a parenthesized expression, cast-expression, compound literal |
| 421 | // expression, or statement expression. |
| 422 | // |
| 423 | // If the parsed tokens consist of a primary-expression, the cases below |
| 424 | // call ParsePostfixExpressionSuffix to handle the postfix expression |
| 425 | // suffixes. Cases that cannot be followed by postfix exprs should |
| 426 | // return without invoking ParsePostfixExpressionSuffix. |
| 427 | switch (SavedKind) { |
| 428 | case tok::l_paren: { |
| 429 | // If this expression is limited to being a unary-expression, the parent can |
| 430 | // not start a cast expression. |
| 431 | ParenParseOption ParenExprType = |
| 432 | isUnaryExpression ? CompoundLiteral : CastExpr; |
| 433 | TypeTy *CastTy; |
| 434 | SourceLocation LParenLoc = Tok.getLocation(); |
| 435 | SourceLocation RParenLoc; |
| 436 | Res = ParseParenExpression(ParenExprType, CastTy, RParenLoc); |
| 437 | if (Res.isInvalid) return Res; |
| 438 | |
| 439 | switch (ParenExprType) { |
| 440 | case SimpleExpr: break; // Nothing else to do. |
| 441 | case CompoundStmt: break; // Nothing else to do. |
| 442 | case CompoundLiteral: |
| 443 | // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of |
| 444 | // postfix-expression exist, parse them now. |
| 445 | break; |
| 446 | case CastExpr: |
| 447 | // We parsed '(' type-name ')' and the thing after it wasn't a '{'. Parse |
| 448 | // the cast-expression that follows it next. |
| 449 | // TODO: For cast expression with CastTy. |
| 450 | Res = ParseCastExpression(false); |
| 451 | if (!Res.isInvalid) |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 452 | Res = Actions.ActOnCastExpr(LParenLoc, CastTy, RParenLoc, Res.Val); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 453 | return Res; |
| 454 | } |
| 455 | |
| 456 | // These can be followed by postfix-expr pieces. |
| 457 | return ParsePostfixExpressionSuffix(Res); |
| 458 | } |
| 459 | |
| 460 | // primary-expression |
| 461 | case tok::numeric_constant: |
| 462 | // constant: integer-constant |
| 463 | // constant: floating-constant |
| 464 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 465 | Res = Actions.ActOnNumericConstant(Tok); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 466 | ConsumeToken(); |
| 467 | |
| 468 | // These can be followed by postfix-expr pieces. |
| 469 | return ParsePostfixExpressionSuffix(Res); |
| 470 | |
| 471 | case tok::kw_true: |
| 472 | case tok::kw_false: |
| 473 | return ParseCXXBoolLiteral(); |
| 474 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 475 | case tok::identifier: { // primary-expression: identifier |
| 476 | // unqualified-id: identifier |
| 477 | // constant: enumeration-constant |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 478 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 479 | // Consume the identifier so that we can see if it is followed by a '('. |
| 480 | // Function designators are allowed to be undeclared (C99 6.5.1p2), so we |
| 481 | // need to know whether or not this identifier is a function designator or |
| 482 | // not. |
| 483 | IdentifierInfo &II = *Tok.getIdentifierInfo(); |
| 484 | SourceLocation L = ConsumeToken(); |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 485 | Res = Actions.ActOnIdentifierExpr(CurScope, L, II, Tok.is(tok::l_paren)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 486 | // These can be followed by postfix-expr pieces. |
| 487 | return ParsePostfixExpressionSuffix(Res); |
| 488 | } |
| 489 | case tok::char_constant: // constant: character-constant |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 490 | Res = Actions.ActOnCharacterConstant(Tok); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 491 | ConsumeToken(); |
| 492 | // These can be followed by postfix-expr pieces. |
| 493 | return ParsePostfixExpressionSuffix(Res); |
| 494 | case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2] |
| 495 | case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU] |
| 496 | case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU] |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 497 | Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 498 | ConsumeToken(); |
| 499 | // These can be followed by postfix-expr pieces. |
| 500 | return ParsePostfixExpressionSuffix(Res); |
| 501 | case tok::string_literal: // primary-expression: string-literal |
| 502 | case tok::wide_string_literal: |
| 503 | Res = ParseStringLiteralExpression(); |
| 504 | if (Res.isInvalid) return Res; |
| 505 | // This can be followed by postfix-expr pieces (e.g. "foo"[1]). |
| 506 | return ParsePostfixExpressionSuffix(Res); |
| 507 | case tok::kw___builtin_va_arg: |
| 508 | case tok::kw___builtin_offsetof: |
| 509 | case tok::kw___builtin_choose_expr: |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 510 | case tok::kw___builtin_overload: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 511 | case tok::kw___builtin_types_compatible_p: |
| 512 | return ParseBuiltinPrimaryExpression(); |
| 513 | case tok::plusplus: // unary-expression: '++' unary-expression |
| 514 | case tok::minusminus: { // unary-expression: '--' unary-expression |
| 515 | SourceLocation SavedLoc = ConsumeToken(); |
| 516 | Res = ParseCastExpression(true); |
| 517 | if (!Res.isInvalid) |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 518 | Res = Actions.ActOnUnaryOp(SavedLoc, SavedKind, Res.Val); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 519 | return Res; |
| 520 | } |
| 521 | case tok::amp: // unary-expression: '&' cast-expression |
| 522 | case tok::star: // unary-expression: '*' cast-expression |
| 523 | case tok::plus: // unary-expression: '+' cast-expression |
| 524 | case tok::minus: // unary-expression: '-' cast-expression |
| 525 | case tok::tilde: // unary-expression: '~' cast-expression |
| 526 | case tok::exclaim: // unary-expression: '!' cast-expression |
| 527 | case tok::kw___real: // unary-expression: '__real' cast-expression [GNU] |
Chris Lattner | 3508084 | 2008-02-02 20:20:10 +0000 | [diff] [blame] | 528 | case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 529 | SourceLocation SavedLoc = ConsumeToken(); |
| 530 | Res = ParseCastExpression(false); |
| 531 | if (!Res.isInvalid) |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 532 | Res = Actions.ActOnUnaryOp(SavedLoc, SavedKind, Res.Val); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 533 | return Res; |
Chris Lattner | 3508084 | 2008-02-02 20:20:10 +0000 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU] |
| 537 | // __extension__ silences extension warnings in the subexpression. |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 538 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
Chris Lattner | 3508084 | 2008-02-02 20:20:10 +0000 | [diff] [blame] | 539 | SourceLocation SavedLoc = ConsumeToken(); |
| 540 | Res = ParseCastExpression(false); |
| 541 | if (!Res.isInvalid) |
| 542 | Res = Actions.ActOnUnaryOp(SavedLoc, SavedKind, Res.Val); |
Chris Lattner | 3508084 | 2008-02-02 20:20:10 +0000 | [diff] [blame] | 543 | return Res; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 544 | } |
| 545 | case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression |
| 546 | // unary-expression: 'sizeof' '(' type-name ')' |
Douglas Gregor | 85bb3da | 2008-11-06 15:17:27 +0000 | [diff] [blame] | 547 | case tok::kw_alignof: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 548 | case tok::kw___alignof: // unary-expression: '__alignof' unary-expression |
| 549 | // unary-expression: '__alignof' '(' type-name ')' |
Douglas Gregor | 85bb3da | 2008-11-06 15:17:27 +0000 | [diff] [blame] | 550 | // unary-expression: 'alignof' '(' type-id ')' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 551 | return ParseSizeofAlignofExpression(); |
| 552 | case tok::ampamp: { // unary-expression: '&&' identifier |
| 553 | SourceLocation AmpAmpLoc = ConsumeToken(); |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 554 | if (Tok.isNot(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 555 | Diag(Tok, diag::err_expected_ident); |
| 556 | return ExprResult(true); |
| 557 | } |
| 558 | |
| 559 | Diag(AmpAmpLoc, diag::ext_gnu_address_of_label); |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 560 | Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 561 | Tok.getIdentifierInfo()); |
| 562 | ConsumeToken(); |
| 563 | return Res; |
| 564 | } |
| 565 | case tok::kw_const_cast: |
| 566 | case tok::kw_dynamic_cast: |
| 567 | case tok::kw_reinterpret_cast: |
| 568 | case tok::kw_static_cast: |
Argyrios Kyrtzidis | b348b81 | 2008-08-16 19:45:32 +0000 | [diff] [blame] | 569 | Res = ParseCXXCasts(); |
| 570 | // These can be followed by postfix-expr pieces. |
| 571 | return ParsePostfixExpressionSuffix(Res); |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 572 | case tok::kw_typeid: |
| 573 | Res = ParseCXXTypeid(); |
| 574 | // This can be followed by postfix-expr pieces. |
| 575 | return ParsePostfixExpressionSuffix(Res); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 576 | case tok::kw_this: |
Argyrios Kyrtzidis | 289d773 | 2008-08-16 19:34:46 +0000 | [diff] [blame] | 577 | Res = ParseCXXThis(); |
| 578 | // This can be followed by postfix-expr pieces. |
| 579 | return ParsePostfixExpressionSuffix(Res); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 580 | |
| 581 | case tok::kw_char: |
| 582 | case tok::kw_wchar_t: |
| 583 | case tok::kw_bool: |
| 584 | case tok::kw_short: |
| 585 | case tok::kw_int: |
| 586 | case tok::kw_long: |
| 587 | case tok::kw_signed: |
| 588 | case tok::kw_unsigned: |
| 589 | case tok::kw_float: |
| 590 | case tok::kw_double: |
| 591 | case tok::kw_void: |
| 592 | case tok::kw_typeof: { |
| 593 | if (!getLang().CPlusPlus) |
| 594 | goto UnhandledToken; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 595 | case tok::annot_qualtypename: |
| 596 | assert(getLang().CPlusPlus && "Expected C++"); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 597 | // postfix-expression: simple-type-specifier '(' expression-list[opt] ')' |
| 598 | // |
| 599 | DeclSpec DS; |
| 600 | ParseCXXSimpleTypeSpecifier(DS); |
| 601 | if (Tok.isNot(tok::l_paren)) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame^] | 602 | return Diag(Tok, diag::err_expected_lparen_after_type) |
| 603 | << DS.getSourceRange(); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 604 | |
| 605 | Res = ParseCXXTypeConstructExpression(DS); |
| 606 | // This can be followed by postfix-expr pieces. |
| 607 | return ParsePostfixExpressionSuffix(Res); |
| 608 | } |
| 609 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 610 | case tok::annot_cxxscope: // [C++] id-expression: qualified-id |
| 611 | case tok::kw_operator: // [C++] id-expression: operator/conversion-function-id |
| 612 | // template-id |
| 613 | Res = ParseCXXIdExpression(); |
| 614 | return ParsePostfixExpressionSuffix(Res); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 615 | |
Chris Lattner | c97c204 | 2007-10-03 22:03:06 +0000 | [diff] [blame] | 616 | case tok::at: { |
| 617 | SourceLocation AtLoc = ConsumeToken(); |
Steve Naroff | a642beb | 2007-10-15 20:55:58 +0000 | [diff] [blame] | 618 | return ParseObjCAtExpression(AtLoc); |
Chris Lattner | c97c204 | 2007-10-03 22:03:06 +0000 | [diff] [blame] | 619 | } |
Fariborz Jahanian | 0ccb27d | 2007-09-05 19:52:07 +0000 | [diff] [blame] | 620 | case tok::l_square: |
Steve Naroff | a642beb | 2007-10-15 20:55:58 +0000 | [diff] [blame] | 621 | // These can be followed by postfix-expr pieces. |
Chris Lattner | 039a642 | 2008-05-09 05:28:21 +0000 | [diff] [blame] | 622 | if (getLang().ObjC1) |
| 623 | return ParsePostfixExpressionSuffix(ParseObjCMessageExpression()); |
| 624 | // FALL THROUGH. |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 625 | case tok::caret: |
| 626 | if (getLang().Blocks) |
| 627 | return ParsePostfixExpressionSuffix(ParseBlockLiteralExpression()); |
| 628 | Diag(Tok, diag::err_expected_expression); |
| 629 | return ExprResult(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 630 | default: |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 631 | UnhandledToken: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 632 | Diag(Tok, diag::err_expected_expression); |
| 633 | return ExprResult(true); |
| 634 | } |
| 635 | |
| 636 | // unreachable. |
| 637 | abort(); |
| 638 | } |
| 639 | |
| 640 | /// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression |
| 641 | /// is parsed, this method parses any suffixes that apply. |
| 642 | /// |
| 643 | /// postfix-expression: [C99 6.5.2] |
| 644 | /// primary-expression |
| 645 | /// postfix-expression '[' expression ']' |
| 646 | /// postfix-expression '(' argument-expression-list[opt] ')' |
| 647 | /// postfix-expression '.' identifier |
| 648 | /// postfix-expression '->' identifier |
| 649 | /// postfix-expression '++' |
| 650 | /// postfix-expression '--' |
| 651 | /// '(' type-name ')' '{' initializer-list '}' |
| 652 | /// '(' type-name ')' '{' initializer-list ',' '}' |
| 653 | /// |
| 654 | /// argument-expression-list: [C99 6.5.2] |
| 655 | /// argument-expression |
| 656 | /// argument-expression-list ',' assignment-expression |
| 657 | /// |
| 658 | Parser::ExprResult Parser::ParsePostfixExpressionSuffix(ExprResult LHS) { |
| 659 | |
| 660 | // Now that the primary-expression piece of the postfix-expression has been |
| 661 | // parsed, see if there are any postfix-expression pieces here. |
| 662 | SourceLocation Loc; |
| 663 | while (1) { |
| 664 | switch (Tok.getKind()) { |
| 665 | default: // Not a postfix-expression suffix. |
| 666 | return LHS; |
| 667 | case tok::l_square: { // postfix-expression: p-e '[' expression ']' |
| 668 | Loc = ConsumeBracket(); |
| 669 | ExprResult Idx = ParseExpression(); |
| 670 | |
| 671 | SourceLocation RLoc = Tok.getLocation(); |
| 672 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 673 | if (!LHS.isInvalid && !Idx.isInvalid && Tok.is(tok::r_square)) |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 674 | LHS = Actions.ActOnArraySubscriptExpr(LHS.Val, Loc, Idx.Val, RLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 675 | else |
| 676 | LHS = ExprResult(true); |
| 677 | |
| 678 | // Match the ']'. |
| 679 | MatchRHSPunctuation(tok::r_square, Loc); |
| 680 | break; |
| 681 | } |
| 682 | |
| 683 | case tok::l_paren: { // p-e: p-e '(' argument-expression-list[opt] ')' |
Argyrios Kyrtzidis | 0cd5b42 | 2008-08-16 20:03:01 +0000 | [diff] [blame] | 684 | ExprListTy ArgExprs; |
| 685 | CommaLocsTy CommaLocs; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 686 | |
| 687 | Loc = ConsumeParen(); |
| 688 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 689 | if (Tok.isNot(tok::r_paren)) { |
Argyrios Kyrtzidis | 0cd5b42 | 2008-08-16 20:03:01 +0000 | [diff] [blame] | 690 | if (ParseExpressionList(ArgExprs, CommaLocs)) { |
| 691 | SkipUntil(tok::r_paren); |
| 692 | return ExprResult(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 693 | } |
| 694 | } |
| 695 | |
| 696 | // Match the ')'. |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 697 | if (!LHS.isInvalid && Tok.is(tok::r_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 698 | assert((ArgExprs.size() == 0 || ArgExprs.size()-1 == CommaLocs.size())&& |
| 699 | "Unexpected number of commas!"); |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 700 | LHS = Actions.ActOnCallExpr(LHS.Val, Loc, &ArgExprs[0], ArgExprs.size(), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 701 | &CommaLocs[0], Tok.getLocation()); |
| 702 | } |
| 703 | |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 704 | MatchRHSPunctuation(tok::r_paren, Loc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 705 | break; |
| 706 | } |
| 707 | case tok::arrow: // postfix-expression: p-e '->' identifier |
| 708 | case tok::period: { // postfix-expression: p-e '.' identifier |
| 709 | tok::TokenKind OpKind = Tok.getKind(); |
| 710 | SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token. |
| 711 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 712 | if (Tok.isNot(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 713 | Diag(Tok, diag::err_expected_ident); |
| 714 | return ExprResult(true); |
| 715 | } |
| 716 | |
| 717 | if (!LHS.isInvalid) |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 718 | LHS = Actions.ActOnMemberReferenceExpr(LHS.Val, OpLoc, OpKind, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 719 | Tok.getLocation(), |
| 720 | *Tok.getIdentifierInfo()); |
| 721 | ConsumeToken(); |
| 722 | break; |
| 723 | } |
| 724 | case tok::plusplus: // postfix-expression: postfix-expression '++' |
| 725 | case tok::minusminus: // postfix-expression: postfix-expression '--' |
| 726 | if (!LHS.isInvalid) |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 727 | LHS = Actions.ActOnPostfixUnaryOp(Tok.getLocation(), Tok.getKind(), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 728 | LHS.Val); |
| 729 | ConsumeToken(); |
| 730 | break; |
| 731 | } |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | |
| 736 | /// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression. |
| 737 | /// unary-expression: [C99 6.5.3] |
| 738 | /// 'sizeof' unary-expression |
| 739 | /// 'sizeof' '(' type-name ')' |
| 740 | /// [GNU] '__alignof' unary-expression |
| 741 | /// [GNU] '__alignof' '(' type-name ')' |
Douglas Gregor | 85bb3da | 2008-11-06 15:17:27 +0000 | [diff] [blame] | 742 | /// [C++0x] 'alignof' '(' type-id ')' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 743 | Parser::ExprResult Parser::ParseSizeofAlignofExpression() { |
Douglas Gregor | 85bb3da | 2008-11-06 15:17:27 +0000 | [diff] [blame] | 744 | assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof) |
| 745 | || Tok.is(tok::kw_alignof)) && |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 746 | "Not a sizeof/alignof expression!"); |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 747 | Token OpTok = Tok; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 748 | ConsumeToken(); |
| 749 | |
| 750 | // If the operand doesn't start with an '(', it must be an expression. |
| 751 | ExprResult Operand; |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 752 | if (Tok.isNot(tok::l_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 753 | Operand = ParseCastExpression(true); |
| 754 | } else { |
| 755 | // If it starts with a '(', we know that it is either a parenthesized |
| 756 | // type-name, or it is a unary-expression that starts with a compound |
| 757 | // literal, or starts with a primary-expression that is a parenthesized |
| 758 | // expression. |
| 759 | ParenParseOption ExprType = CastExpr; |
| 760 | TypeTy *CastTy; |
| 761 | SourceLocation LParenLoc = Tok.getLocation(), RParenLoc; |
| 762 | Operand = ParseParenExpression(ExprType, CastTy, RParenLoc); |
| 763 | |
| 764 | // If ParseParenExpression parsed a '(typename)' sequence only, the this is |
| 765 | // sizeof/alignof a type. Otherwise, it is sizeof/alignof an expression. |
Chris Lattner | 4c1a2a9 | 2007-11-13 20:50:37 +0000 | [diff] [blame] | 766 | if (ExprType == CastExpr) |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 767 | return Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(), |
| 768 | OpTok.is(tok::kw_sizeof), |
| 769 | /*isType=*/true, CastTy, |
| 770 | SourceRange(LParenLoc, RParenLoc)); |
Chris Lattner | 4c1a2a9 | 2007-11-13 20:50:37 +0000 | [diff] [blame] | 771 | |
| 772 | // If this is a parenthesized expression, it is the start of a |
| 773 | // unary-expression, but doesn't include any postfix pieces. Parse these |
| 774 | // now if present. |
| 775 | Operand = ParsePostfixExpressionSuffix(Operand); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | // If we get here, the operand to the sizeof/alignof was an expresion. |
| 779 | if (!Operand.isInvalid) |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 780 | Operand = Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(), |
| 781 | OpTok.is(tok::kw_sizeof), |
| 782 | /*isType=*/false, Operand.Val, |
| 783 | SourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 784 | return Operand; |
| 785 | } |
| 786 | |
| 787 | /// ParseBuiltinPrimaryExpression |
| 788 | /// |
| 789 | /// primary-expression: [C99 6.5.1] |
| 790 | /// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')' |
| 791 | /// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')' |
| 792 | /// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ',' |
| 793 | /// assign-expr ')' |
| 794 | /// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')' |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 795 | /// [CLANG] '__builtin_overload' '(' expr (',' expr)* ')' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 796 | /// |
| 797 | /// [GNU] offsetof-member-designator: |
| 798 | /// [GNU] identifier |
| 799 | /// [GNU] offsetof-member-designator '.' identifier |
| 800 | /// [GNU] offsetof-member-designator '[' expression ']' |
| 801 | /// |
| 802 | Parser::ExprResult Parser::ParseBuiltinPrimaryExpression() { |
| 803 | ExprResult Res(false); |
| 804 | const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo(); |
| 805 | |
| 806 | tok::TokenKind T = Tok.getKind(); |
| 807 | SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier. |
| 808 | |
| 809 | // All of these start with an open paren. |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 810 | if (Tok.isNot(tok::l_paren)) { |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame^] | 811 | Diag(Tok, diag::err_expected_lparen_after) << BuiltinII->getName(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 812 | return ExprResult(true); |
| 813 | } |
| 814 | |
| 815 | SourceLocation LParenLoc = ConsumeParen(); |
| 816 | // TODO: Build AST. |
| 817 | |
| 818 | switch (T) { |
| 819 | default: assert(0 && "Not a builtin primary expression!"); |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 820 | case tok::kw___builtin_va_arg: { |
| 821 | ExprResult Expr = ParseAssignmentExpression(); |
| 822 | if (Expr.isInvalid) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 823 | SkipUntil(tok::r_paren); |
Eli Friedman | 0976278 | 2008-08-20 22:07:34 +0000 | [diff] [blame] | 824 | return ExprResult(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) |
| 828 | return ExprResult(true); |
| 829 | |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 830 | TypeTy *Ty = ParseTypeName(); |
Chris Lattner | 6eb2109 | 2007-08-30 15:52:49 +0000 | [diff] [blame] | 831 | |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 832 | if (Tok.isNot(tok::r_paren)) { |
| 833 | Diag(Tok, diag::err_expected_rparen); |
| 834 | return ExprResult(true); |
| 835 | } |
| 836 | Res = Actions.ActOnVAArg(StartLoc, Expr.Val, Ty, ConsumeParen()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 837 | break; |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 838 | } |
Chris Lattner | f9aa3cb | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 839 | case tok::kw___builtin_offsetof: { |
Chris Lattner | 9fddf0a | 2007-08-30 17:08:45 +0000 | [diff] [blame] | 840 | SourceLocation TypeLoc = Tok.getLocation(); |
Chris Lattner | f9aa3cb | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 841 | TypeTy *Ty = ParseTypeName(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 842 | |
| 843 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) |
| 844 | return ExprResult(true); |
| 845 | |
| 846 | // We must have at least one identifier here. |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 847 | if (Tok.isNot(tok::identifier)) { |
Chris Lattner | f9aa3cb | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 848 | Diag(Tok, diag::err_expected_ident); |
| 849 | SkipUntil(tok::r_paren); |
| 850 | return true; |
| 851 | } |
| 852 | |
| 853 | // Keep track of the various subcomponents we see. |
| 854 | llvm::SmallVector<Action::OffsetOfComponent, 4> Comps; |
| 855 | |
| 856 | Comps.push_back(Action::OffsetOfComponent()); |
| 857 | Comps.back().isBrackets = false; |
| 858 | Comps.back().U.IdentInfo = Tok.getIdentifierInfo(); |
| 859 | Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 860 | |
| 861 | while (1) { |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 862 | if (Tok.is(tok::period)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 863 | // offsetof-member-designator: offsetof-member-designator '.' identifier |
Chris Lattner | f9aa3cb | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 864 | Comps.push_back(Action::OffsetOfComponent()); |
| 865 | Comps.back().isBrackets = false; |
| 866 | Comps.back().LocStart = ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 867 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 868 | if (Tok.isNot(tok::identifier)) { |
Chris Lattner | f9aa3cb | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 869 | Diag(Tok, diag::err_expected_ident); |
| 870 | SkipUntil(tok::r_paren); |
| 871 | return true; |
| 872 | } |
| 873 | Comps.back().U.IdentInfo = Tok.getIdentifierInfo(); |
| 874 | Comps.back().LocEnd = ConsumeToken(); |
| 875 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 876 | } else if (Tok.is(tok::l_square)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 877 | // offsetof-member-designator: offsetof-member-design '[' expression ']' |
Chris Lattner | f9aa3cb | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 878 | Comps.push_back(Action::OffsetOfComponent()); |
| 879 | Comps.back().isBrackets = true; |
| 880 | Comps.back().LocStart = ConsumeBracket(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 881 | Res = ParseExpression(); |
| 882 | if (Res.isInvalid) { |
| 883 | SkipUntil(tok::r_paren); |
| 884 | return Res; |
| 885 | } |
Chris Lattner | f9aa3cb | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 886 | Comps.back().U.E = Res.Val; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 887 | |
Chris Lattner | f9aa3cb | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 888 | Comps.back().LocEnd = |
| 889 | MatchRHSPunctuation(tok::r_square, Comps.back().LocStart); |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 890 | } else if (Tok.is(tok::r_paren)) { |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 891 | Res = Actions.ActOnBuiltinOffsetOf(StartLoc, TypeLoc, Ty, &Comps[0], |
Chris Lattner | 6eb2109 | 2007-08-30 15:52:49 +0000 | [diff] [blame] | 892 | Comps.size(), ConsumeParen()); |
| 893 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 894 | } else { |
Chris Lattner | f9aa3cb | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 895 | // Error occurred. |
| 896 | return ExprResult(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 897 | } |
| 898 | } |
| 899 | break; |
Chris Lattner | f9aa3cb | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 900 | } |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 901 | case tok::kw___builtin_choose_expr: { |
| 902 | ExprResult Cond = ParseAssignmentExpression(); |
| 903 | if (Cond.isInvalid) { |
| 904 | SkipUntil(tok::r_paren); |
| 905 | return Cond; |
| 906 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 907 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) |
| 908 | return ExprResult(true); |
| 909 | |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 910 | ExprResult Expr1 = ParseAssignmentExpression(); |
| 911 | if (Expr1.isInvalid) { |
| 912 | SkipUntil(tok::r_paren); |
| 913 | return Expr1; |
| 914 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 915 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) |
| 916 | return ExprResult(true); |
| 917 | |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 918 | ExprResult Expr2 = ParseAssignmentExpression(); |
| 919 | if (Expr2.isInvalid) { |
| 920 | SkipUntil(tok::r_paren); |
| 921 | return Expr2; |
| 922 | } |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 923 | if (Tok.isNot(tok::r_paren)) { |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 924 | Diag(Tok, diag::err_expected_rparen); |
| 925 | return ExprResult(true); |
| 926 | } |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 927 | Res = Actions.ActOnChooseExpr(StartLoc, Cond.Val, Expr1.Val, Expr2.Val, |
Chris Lattner | 6eb2109 | 2007-08-30 15:52:49 +0000 | [diff] [blame] | 928 | ConsumeParen()); |
| 929 | break; |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 930 | } |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 931 | case tok::kw___builtin_overload: { |
| 932 | llvm::SmallVector<ExprTy*, 8> ArgExprs; |
| 933 | llvm::SmallVector<SourceLocation, 8> CommaLocs; |
| 934 | |
| 935 | // For each iteration through the loop look for assign-expr followed by a |
| 936 | // comma. If there is no comma, break and attempt to match r-paren. |
| 937 | if (Tok.isNot(tok::r_paren)) { |
| 938 | while (1) { |
| 939 | ExprResult ArgExpr = ParseAssignmentExpression(); |
| 940 | if (ArgExpr.isInvalid) { |
| 941 | SkipUntil(tok::r_paren); |
| 942 | return ExprResult(true); |
| 943 | } else |
| 944 | ArgExprs.push_back(ArgExpr.Val); |
| 945 | |
| 946 | if (Tok.isNot(tok::comma)) |
| 947 | break; |
| 948 | // Move to the next argument, remember where the comma was. |
| 949 | CommaLocs.push_back(ConsumeToken()); |
| 950 | } |
| 951 | } |
| 952 | |
| 953 | // Attempt to consume the r-paren |
| 954 | if (Tok.isNot(tok::r_paren)) { |
| 955 | Diag(Tok, diag::err_expected_rparen); |
| 956 | SkipUntil(tok::r_paren); |
| 957 | return ExprResult(true); |
| 958 | } |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 959 | Res = Actions.ActOnOverloadExpr(&ArgExprs[0], ArgExprs.size(), |
| 960 | &CommaLocs[0], StartLoc, ConsumeParen()); |
| 961 | break; |
| 962 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 963 | case tok::kw___builtin_types_compatible_p: |
Steve Naroff | 363bcff | 2007-08-01 23:45:51 +0000 | [diff] [blame] | 964 | TypeTy *Ty1 = ParseTypeName(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 965 | |
| 966 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) |
| 967 | return ExprResult(true); |
| 968 | |
Steve Naroff | 363bcff | 2007-08-01 23:45:51 +0000 | [diff] [blame] | 969 | TypeTy *Ty2 = ParseTypeName(); |
| 970 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 971 | if (Tok.isNot(tok::r_paren)) { |
Steve Naroff | 363bcff | 2007-08-01 23:45:51 +0000 | [diff] [blame] | 972 | Diag(Tok, diag::err_expected_rparen); |
| 973 | return ExprResult(true); |
| 974 | } |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 975 | Res = Actions.ActOnTypesCompatibleExpr(StartLoc, Ty1, Ty2, ConsumeParen()); |
Chris Lattner | 6eb2109 | 2007-08-30 15:52:49 +0000 | [diff] [blame] | 976 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 977 | } |
| 978 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 979 | // These can be followed by postfix-expr pieces because they are |
| 980 | // primary-expressions. |
| 981 | return ParsePostfixExpressionSuffix(Res); |
| 982 | } |
| 983 | |
| 984 | /// ParseParenExpression - This parses the unit that starts with a '(' token, |
| 985 | /// based on what is allowed by ExprType. The actual thing parsed is returned |
| 986 | /// in ExprType. |
| 987 | /// |
| 988 | /// primary-expression: [C99 6.5.1] |
| 989 | /// '(' expression ')' |
| 990 | /// [GNU] '(' compound-statement ')' (if !ParenExprOnly) |
| 991 | /// postfix-expression: [C99 6.5.2] |
| 992 | /// '(' type-name ')' '{' initializer-list '}' |
| 993 | /// '(' type-name ')' '{' initializer-list ',' '}' |
| 994 | /// cast-expression: [C99 6.5.4] |
| 995 | /// '(' type-name ')' cast-expression |
| 996 | /// |
| 997 | Parser::ExprResult Parser::ParseParenExpression(ParenParseOption &ExprType, |
| 998 | TypeTy *&CastTy, |
| 999 | SourceLocation &RParenLoc) { |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1000 | assert(Tok.is(tok::l_paren) && "Not a paren expr!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1001 | SourceLocation OpenLoc = ConsumeParen(); |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 1002 | ExprResult Result(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1003 | CastTy = 0; |
| 1004 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1005 | if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1006 | Diag(Tok, diag::ext_gnu_statement_expr); |
Chris Lattner | 98414c1 | 2007-08-31 21:49:55 +0000 | [diff] [blame] | 1007 | Parser::StmtResult Stmt = ParseCompoundStatement(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1008 | ExprType = CompoundStmt; |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 1009 | |
| 1010 | // If the substmt parsed correctly, build the AST node. |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1011 | if (!Stmt.isInvalid && Tok.is(tok::r_paren)) |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 1012 | Result = Actions.ActOnStmtExpr(OpenLoc, Stmt.Val, Tok.getLocation()); |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 1013 | |
Argyrios Kyrtzidis | 78c8d80 | 2008-10-05 19:56:22 +0000 | [diff] [blame] | 1014 | } else if (ExprType >= CompoundLiteral && isTypeIdInParens()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1015 | // Otherwise, this is a compound literal expression or cast expression. |
| 1016 | TypeTy *Ty = ParseTypeName(); |
| 1017 | |
| 1018 | // Match the ')'. |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1019 | if (Tok.is(tok::r_paren)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1020 | RParenLoc = ConsumeParen(); |
| 1021 | else |
| 1022 | MatchRHSPunctuation(tok::r_paren, OpenLoc); |
| 1023 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1024 | if (Tok.is(tok::l_brace)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1025 | if (!getLang().C99) // Compound literals don't exist in C90. |
| 1026 | Diag(OpenLoc, diag::ext_c99_compound_literal); |
| 1027 | Result = ParseInitializer(); |
| 1028 | ExprType = CompoundLiteral; |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 1029 | if (!Result.isInvalid) |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 1030 | return Actions.ActOnCompoundLiteral(OpenLoc, Ty, RParenLoc, Result.Val); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1031 | } else if (ExprType == CastExpr) { |
| 1032 | // Note that this doesn't parse the subsequence cast-expression, it just |
| 1033 | // returns the parsed type to the callee. |
| 1034 | ExprType = CastExpr; |
| 1035 | CastTy = Ty; |
| 1036 | return ExprResult(false); |
| 1037 | } else { |
| 1038 | Diag(Tok, diag::err_expected_lbrace_in_compound_literal); |
| 1039 | return ExprResult(true); |
| 1040 | } |
| 1041 | return Result; |
| 1042 | } else { |
| 1043 | Result = ParseExpression(); |
| 1044 | ExprType = SimpleExpr; |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1045 | if (!Result.isInvalid && Tok.is(tok::r_paren)) |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 1046 | Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), Result.Val); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1047 | } |
| 1048 | |
| 1049 | // Match the ')'. |
| 1050 | if (Result.isInvalid) |
| 1051 | SkipUntil(tok::r_paren); |
| 1052 | else { |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1053 | if (Tok.is(tok::r_paren)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1054 | RParenLoc = ConsumeParen(); |
| 1055 | else |
| 1056 | MatchRHSPunctuation(tok::r_paren, OpenLoc); |
| 1057 | } |
| 1058 | |
| 1059 | return Result; |
| 1060 | } |
| 1061 | |
| 1062 | /// ParseStringLiteralExpression - This handles the various token types that |
| 1063 | /// form string literals, and also handles string concatenation [C99 5.1.1.2, |
| 1064 | /// translation phase #6]. |
| 1065 | /// |
| 1066 | /// primary-expression: [C99 6.5.1] |
| 1067 | /// string-literal |
| 1068 | Parser::ExprResult Parser::ParseStringLiteralExpression() { |
| 1069 | assert(isTokenStringLiteral() && "Not a string literal!"); |
| 1070 | |
| 1071 | // String concat. Note that keywords like __func__ and __FUNCTION__ are not |
| 1072 | // considered to be strings for concatenation purposes. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1073 | llvm::SmallVector<Token, 4> StringToks; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1074 | |
| 1075 | do { |
| 1076 | StringToks.push_back(Tok); |
| 1077 | ConsumeStringToken(); |
| 1078 | } while (isTokenStringLiteral()); |
| 1079 | |
| 1080 | // Pass the set of string tokens, ready for concatenation, to the actions. |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 1081 | return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1082 | } |
Argyrios Kyrtzidis | 0cd5b42 | 2008-08-16 20:03:01 +0000 | [diff] [blame] | 1083 | |
| 1084 | /// ParseExpressionList - Used for C/C++ (argument-)expression-list. |
| 1085 | /// |
| 1086 | /// argument-expression-list: |
| 1087 | /// assignment-expression |
| 1088 | /// argument-expression-list , assignment-expression |
| 1089 | /// |
| 1090 | /// [C++] expression-list: |
| 1091 | /// [C++] assignment-expression |
| 1092 | /// [C++] expression-list , assignment-expression |
| 1093 | /// |
| 1094 | bool Parser::ParseExpressionList(ExprListTy &Exprs, CommaLocsTy &CommaLocs) { |
| 1095 | while (1) { |
| 1096 | ExprResult Expr = ParseAssignmentExpression(); |
| 1097 | if (Expr.isInvalid) |
| 1098 | return true; |
Argyrios Kyrtzidis | 4fdc1ca | 2008-08-18 22:49:40 +0000 | [diff] [blame] | 1099 | |
| 1100 | Exprs.push_back(Expr.Val); |
Argyrios Kyrtzidis | 0cd5b42 | 2008-08-16 20:03:01 +0000 | [diff] [blame] | 1101 | |
| 1102 | if (Tok.isNot(tok::comma)) |
| 1103 | return false; |
| 1104 | // Move to the next argument, remember where the comma was. |
| 1105 | CommaLocs.push_back(ConsumeToken()); |
| 1106 | } |
| 1107 | } |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1108 | |
| 1109 | /// ParseBlockLiteralExpression - Parse a block literal, which roughly looks |
Steve Naroff | 17dab4f | 2008-09-16 23:11:46 +0000 | [diff] [blame] | 1110 | /// like ^(int x){ return x+1; } |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1111 | /// |
| 1112 | /// block-literal: |
| 1113 | /// [clang] '^' block-args[opt] compound-statement |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1114 | /// [clang] block-args: |
| 1115 | /// [clang] '(' parameter-list ')' |
| 1116 | /// |
| 1117 | Parser::ExprResult Parser::ParseBlockLiteralExpression() { |
| 1118 | assert(Tok.is(tok::caret) && "block literal starts with ^"); |
| 1119 | SourceLocation CaretLoc = ConsumeToken(); |
| 1120 | |
| 1121 | // Enter a scope to hold everything within the block. This includes the |
| 1122 | // argument decls, decls within the compound expression, etc. This also |
| 1123 | // allows determining whether a variable reference inside the block is |
| 1124 | // within or outside of the block. |
| 1125 | EnterScope(Scope::BlockScope|Scope::FnScope|Scope::BreakScope| |
| 1126 | Scope::ContinueScope|Scope::DeclScope); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 1127 | |
| 1128 | // Inform sema that we are starting a block. |
| 1129 | Actions.ActOnBlockStart(CaretLoc, CurScope); |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1130 | |
| 1131 | // Parse the return type if present. |
| 1132 | DeclSpec DS; |
| 1133 | Declarator ParamInfo(DS, Declarator::PrototypeContext); |
| 1134 | |
| 1135 | // If this block has arguments, parse them. There is no ambiguity here with |
| 1136 | // the expression case, because the expression case requires a parameter list. |
| 1137 | if (Tok.is(tok::l_paren)) { |
| 1138 | ParseParenDeclarator(ParamInfo); |
| 1139 | // Parse the pieces after the identifier as if we had "int(...)". |
| 1140 | ParamInfo.SetIdentifier(0, CaretLoc); |
| 1141 | if (ParamInfo.getInvalidType()) { |
| 1142 | // If there was an error parsing the arguments, they may have tried to use |
| 1143 | // ^(x+y) which requires an argument list. Just skip the whole block |
| 1144 | // literal. |
| 1145 | ExitScope(); |
| 1146 | return true; |
| 1147 | } |
| 1148 | } else { |
| 1149 | // Otherwise, pretend we saw (void). |
| 1150 | ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(true, false, |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1151 | 0, 0, 0, CaretLoc)); |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
| 1154 | // Inform sema that we are starting a block. |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 1155 | Actions.ActOnBlockArguments(ParamInfo); |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1156 | |
Steve Naroff | 17dab4f | 2008-09-16 23:11:46 +0000 | [diff] [blame] | 1157 | ExprResult Result = true; |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1158 | if (Tok.is(tok::l_brace)) { |
| 1159 | StmtResult Stmt = ParseCompoundStatementBody(); |
| 1160 | if (!Stmt.isInvalid) { |
| 1161 | Result = Actions.ActOnBlockStmtExpr(CaretLoc, Stmt.Val, CurScope); |
| 1162 | } else { |
| 1163 | Actions.ActOnBlockError(CaretLoc, CurScope); |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1164 | } |
| 1165 | } |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1166 | ExitScope(); |
| 1167 | return Result; |
| 1168 | } |
| 1169 | |