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