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