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