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 | 6b91f00 | 2009-03-05 07:32:12 +0000 | [diff] [blame] | 25 | #include "clang/Basic/PrettyStackTrace.h" |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 26 | #include "ExtensionRAIIObject.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 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 58 | static prec::Level getBinOpPrecedence(tok::TokenKind Kind, |
Douglas Gregor | 3965b7b | 2009-02-25 23:02:36 +0000 | [diff] [blame] | 59 | bool GreaterThanIsOperator, |
| 60 | bool CPlusPlus0x) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 61 | switch (Kind) { |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 62 | case tok::greater: |
Douglas Gregor | 3965b7b | 2009-02-25 23:02:36 +0000 | [diff] [blame] | 63 | // C++ [temp.names]p3: |
| 64 | // [...] When parsing a template-argument-list, the first |
| 65 | // non-nested > is taken as the ending delimiter rather than a |
| 66 | // greater-than operator. [...] |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 67 | if (GreaterThanIsOperator) |
| 68 | return prec::Relational; |
| 69 | return prec::Unknown; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 70 | |
Douglas Gregor | 3965b7b | 2009-02-25 23:02:36 +0000 | [diff] [blame] | 71 | case tok::greatergreater: |
| 72 | // C++0x [temp.names]p3: |
| 73 | // |
| 74 | // [...] Similarly, the first non-nested >> is treated as two |
| 75 | // consecutive but distinct > tokens, the first of which is |
| 76 | // taken as the end of the template-argument-list and completes |
| 77 | // the template-id. [...] |
| 78 | if (GreaterThanIsOperator || !CPlusPlus0x) |
| 79 | return prec::Shift; |
| 80 | return prec::Unknown; |
| 81 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 82 | default: return prec::Unknown; |
| 83 | case tok::comma: return prec::Comma; |
| 84 | case tok::equal: |
| 85 | case tok::starequal: |
| 86 | case tok::slashequal: |
| 87 | case tok::percentequal: |
| 88 | case tok::plusequal: |
| 89 | case tok::minusequal: |
| 90 | case tok::lesslessequal: |
| 91 | case tok::greatergreaterequal: |
| 92 | case tok::ampequal: |
| 93 | case tok::caretequal: |
| 94 | case tok::pipeequal: return prec::Assignment; |
| 95 | case tok::question: return prec::Conditional; |
| 96 | case tok::pipepipe: return prec::LogicalOr; |
| 97 | case tok::ampamp: return prec::LogicalAnd; |
| 98 | case tok::pipe: return prec::InclusiveOr; |
| 99 | case tok::caret: return prec::ExclusiveOr; |
| 100 | case tok::amp: return prec::And; |
| 101 | case tok::exclaimequal: |
| 102 | case tok::equalequal: return prec::Equality; |
| 103 | case tok::lessequal: |
| 104 | case tok::less: |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 105 | case tok::greaterequal: return prec::Relational; |
Douglas Gregor | 3965b7b | 2009-02-25 23:02:36 +0000 | [diff] [blame] | 106 | case tok::lessless: return prec::Shift; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 107 | case tok::plus: |
| 108 | case tok::minus: return prec::Additive; |
| 109 | case tok::percent: |
| 110 | case tok::slash: |
| 111 | case tok::star: return prec::Multiplicative; |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 112 | case tok::periodstar: |
| 113 | case tok::arrowstar: return prec::PointerToMember; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
| 117 | |
| 118 | /// ParseExpression - Simple precedence-based parser for binary/ternary |
| 119 | /// operators. |
| 120 | /// |
| 121 | /// Note: we diverge from the C99 grammar when parsing the assignment-expression |
| 122 | /// production. C99 specifies that the LHS of an assignment operator should be |
| 123 | /// parsed as a unary-expression, but consistency dictates that it be a |
| 124 | /// conditional-expession. In practice, the important thing here is that the |
| 125 | /// LHS of an assignment has to be an l-value, which productions between |
| 126 | /// unary-expression and conditional-expression don't produce. Because we want |
| 127 | /// consistency, we parse the LHS as a conditional-expression, then check for |
| 128 | /// l-value-ness in semantic analysis stages. |
| 129 | /// |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 130 | /// pm-expression: [C++ 5.5] |
| 131 | /// cast-expression |
| 132 | /// pm-expression '.*' cast-expression |
| 133 | /// pm-expression '->*' cast-expression |
| 134 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 135 | /// multiplicative-expression: [C99 6.5.5] |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 136 | /// Note: in C++, apply pm-expression instead of cast-expression |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 137 | /// cast-expression |
| 138 | /// multiplicative-expression '*' cast-expression |
| 139 | /// multiplicative-expression '/' cast-expression |
| 140 | /// multiplicative-expression '%' cast-expression |
| 141 | /// |
| 142 | /// additive-expression: [C99 6.5.6] |
| 143 | /// multiplicative-expression |
| 144 | /// additive-expression '+' multiplicative-expression |
| 145 | /// additive-expression '-' multiplicative-expression |
| 146 | /// |
| 147 | /// shift-expression: [C99 6.5.7] |
| 148 | /// additive-expression |
| 149 | /// shift-expression '<<' additive-expression |
| 150 | /// shift-expression '>>' additive-expression |
| 151 | /// |
| 152 | /// relational-expression: [C99 6.5.8] |
| 153 | /// shift-expression |
| 154 | /// relational-expression '<' shift-expression |
| 155 | /// relational-expression '>' shift-expression |
| 156 | /// relational-expression '<=' shift-expression |
| 157 | /// relational-expression '>=' shift-expression |
| 158 | /// |
| 159 | /// equality-expression: [C99 6.5.9] |
| 160 | /// relational-expression |
| 161 | /// equality-expression '==' relational-expression |
| 162 | /// equality-expression '!=' relational-expression |
| 163 | /// |
| 164 | /// AND-expression: [C99 6.5.10] |
| 165 | /// equality-expression |
| 166 | /// AND-expression '&' equality-expression |
| 167 | /// |
| 168 | /// exclusive-OR-expression: [C99 6.5.11] |
| 169 | /// AND-expression |
| 170 | /// exclusive-OR-expression '^' AND-expression |
| 171 | /// |
| 172 | /// inclusive-OR-expression: [C99 6.5.12] |
| 173 | /// exclusive-OR-expression |
| 174 | /// inclusive-OR-expression '|' exclusive-OR-expression |
| 175 | /// |
| 176 | /// logical-AND-expression: [C99 6.5.13] |
| 177 | /// inclusive-OR-expression |
| 178 | /// logical-AND-expression '&&' inclusive-OR-expression |
| 179 | /// |
| 180 | /// logical-OR-expression: [C99 6.5.14] |
| 181 | /// logical-AND-expression |
| 182 | /// logical-OR-expression '||' logical-AND-expression |
| 183 | /// |
| 184 | /// conditional-expression: [C99 6.5.15] |
| 185 | /// logical-OR-expression |
| 186 | /// logical-OR-expression '?' expression ':' conditional-expression |
| 187 | /// [GNU] logical-OR-expression '?' ':' conditional-expression |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 188 | /// [C++] the third operand is an assignment-expression |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 189 | /// |
| 190 | /// assignment-expression: [C99 6.5.16] |
| 191 | /// conditional-expression |
| 192 | /// unary-expression assignment-operator assignment-expression |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 193 | /// [C++] throw-expression [C++ 15] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 194 | /// |
| 195 | /// assignment-operator: one of |
| 196 | /// = *= /= %= += -= <<= >>= &= ^= |= |
| 197 | /// |
| 198 | /// expression: [C99 6.5.17] |
| 199 | /// assignment-expression |
| 200 | /// expression ',' assignment-expression |
| 201 | /// |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 202 | Parser::OwningExprResult Parser::ParseExpression() { |
Mike Stump | 6ce0c39 | 2009-05-15 21:47:08 +0000 | [diff] [blame] | 203 | OwningExprResult LHS(ParseAssignmentExpression()); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 204 | if (LHS.isInvalid()) return move(LHS); |
| 205 | |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 206 | return ParseRHSOfBinaryExpression(move(LHS), prec::Comma); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 209 | /// This routine is called when the '@' is seen and consumed. |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 210 | /// Current token is an Identifier and is not a 'try'. This |
Chris Lattner | c97c204 | 2007-10-03 22:03:06 +0000 | [diff] [blame] | 211 | /// routine is necessary to disambiguate @try-statement from, |
| 212 | /// for example, @encode-expression. |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 213 | /// |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 214 | Parser::OwningExprResult |
| 215 | Parser::ParseExpressionWithLeadingAt(SourceLocation AtLoc) { |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 216 | OwningExprResult LHS(ParseObjCAtExpression(AtLoc)); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 217 | if (LHS.isInvalid()) return move(LHS); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 218 | |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 219 | return ParseRHSOfBinaryExpression(move(LHS), prec::Comma); |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Eli Friedman | adf077f | 2009-01-27 08:43:38 +0000 | [diff] [blame] | 222 | /// This routine is called when a leading '__extension__' is seen and |
| 223 | /// consumed. This is necessary because the token gets consumed in the |
| 224 | /// process of disambiguating between an expression and a declaration. |
| 225 | Parser::OwningExprResult |
| 226 | Parser::ParseExpressionWithLeadingExtension(SourceLocation ExtLoc) { |
Eli Friedman | bc6c848 | 2009-05-16 23:40:44 +0000 | [diff] [blame] | 227 | OwningExprResult LHS(Actions, true); |
| 228 | { |
| 229 | // Silence extension warnings in the sub-expression |
| 230 | ExtensionRAIIObject O(Diags); |
| 231 | |
| 232 | LHS = ParseCastExpression(false); |
| 233 | if (LHS.isInvalid()) return move(LHS); |
| 234 | } |
Eli Friedman | adf077f | 2009-01-27 08:43:38 +0000 | [diff] [blame] | 235 | |
| 236 | LHS = Actions.ActOnUnaryOp(CurScope, ExtLoc, tok::kw___extension__, |
Sebastian Redl | 76ad2e8 | 2009-02-05 15:02:23 +0000 | [diff] [blame] | 237 | move(LHS)); |
Eli Friedman | adf077f | 2009-01-27 08:43:38 +0000 | [diff] [blame] | 238 | if (LHS.isInvalid()) return move(LHS); |
| 239 | |
| 240 | return ParseRHSOfBinaryExpression(move(LHS), prec::Comma); |
| 241 | } |
| 242 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 243 | /// ParseAssignmentExpression - Parse an expr that doesn't include commas. |
| 244 | /// |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 245 | Parser::OwningExprResult Parser::ParseAssignmentExpression() { |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 246 | if (Tok.is(tok::kw_throw)) |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 247 | return ParseThrowExpression(); |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 248 | |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 249 | OwningExprResult LHS(ParseCastExpression(false)); |
| 250 | if (LHS.isInvalid()) return move(LHS); |
| 251 | |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 252 | return ParseRHSOfBinaryExpression(move(LHS), prec::Assignment); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Chris Lattner | b93fb49 | 2008-06-02 21:31:07 +0000 | [diff] [blame] | 255 | /// ParseAssignmentExprWithObjCMessageExprStart - Parse an assignment expression |
| 256 | /// where part of an objc message send has already been parsed. In this case |
| 257 | /// LBracLoc indicates the location of the '[' of the message send, and either |
| 258 | /// ReceiverName or ReceiverExpr is non-null indicating the receiver of the |
| 259 | /// message. |
| 260 | /// |
| 261 | /// Since this handles full assignment-expression's, it handles postfix |
| 262 | /// expressions and other binary operators for these expressions as well. |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 263 | Parser::OwningExprResult |
Chris Lattner | b93fb49 | 2008-06-02 21:31:07 +0000 | [diff] [blame] | 264 | Parser::ParseAssignmentExprWithObjCMessageExprStart(SourceLocation LBracLoc, |
Steve Naroff | 5cb93b8 | 2008-11-19 15:54:23 +0000 | [diff] [blame] | 265 | SourceLocation NameLoc, |
Chris Lattner | b93fb49 | 2008-06-02 21:31:07 +0000 | [diff] [blame] | 266 | IdentifierInfo *ReceiverName, |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 267 | ExprArg ReceiverExpr) { |
| 268 | OwningExprResult R(ParseObjCMessageExpressionBody(LBracLoc, NameLoc, |
| 269 | ReceiverName, |
| 270 | move(ReceiverExpr))); |
| 271 | if (R.isInvalid()) return move(R); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 272 | R = ParsePostfixExpressionSuffix(move(R)); |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 273 | if (R.isInvalid()) return move(R); |
| 274 | return ParseRHSOfBinaryExpression(move(R), prec::Assignment); |
Chris Lattner | b93fb49 | 2008-06-02 21:31:07 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 278 | Parser::OwningExprResult Parser::ParseConstantExpression() { |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 279 | // C++ [basic.def.odr]p2: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 280 | // An expression is potentially evaluated unless it appears where an |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 281 | // integral constant expression is required (see 5.19) [...]. |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 282 | EnterExpressionEvaluationContext Unevaluated(Actions, |
| 283 | Action::Unevaluated); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 284 | |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 285 | OwningExprResult LHS(ParseCastExpression(false)); |
| 286 | if (LHS.isInvalid()) return move(LHS); |
| 287 | |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 288 | return ParseRHSOfBinaryExpression(move(LHS), prec::Conditional); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 291 | /// ParseRHSOfBinaryExpression - Parse a binary expression that starts with |
| 292 | /// LHS and has a precedence of at least MinPrec. |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 293 | Parser::OwningExprResult |
| 294 | Parser::ParseRHSOfBinaryExpression(OwningExprResult LHS, unsigned MinPrec) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 295 | unsigned NextTokPrec = getBinOpPrecedence(Tok.getKind(), |
Douglas Gregor | 3965b7b | 2009-02-25 23:02:36 +0000 | [diff] [blame] | 296 | GreaterThanIsOperator, |
| 297 | getLang().CPlusPlus0x); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 298 | SourceLocation ColonLoc; |
| 299 | |
| 300 | while (1) { |
| 301 | // If this token has a lower precedence than we are allowed to parse (e.g. |
| 302 | // because we are called recursively, or because the token is not a binop), |
| 303 | // then we are done! |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 304 | if (NextTokPrec < MinPrec) |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 305 | return move(LHS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 306 | |
| 307 | // Consume the operator, saving the operator token for error reporting. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 308 | Token OpToken = Tok; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 309 | ConsumeToken(); |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 310 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 311 | // Special case handling for the ternary operator. |
Sebastian Redl | 15faa7f | 2008-12-09 20:22:58 +0000 | [diff] [blame] | 312 | OwningExprResult TernaryMiddle(Actions, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 313 | if (NextTokPrec == prec::Conditional) { |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 314 | if (Tok.isNot(tok::colon)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 315 | // Handle this production specially: |
| 316 | // logical-OR-expression '?' expression ':' conditional-expression |
| 317 | // In particular, the RHS of the '?' is 'expression', not |
| 318 | // 'logical-OR-expression' as we might expect. |
| 319 | TernaryMiddle = ParseExpression(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 320 | if (TernaryMiddle.isInvalid()) |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 321 | return move(TernaryMiddle); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 322 | } else { |
| 323 | // Special case handling of "X ? Y : Z" where Y is empty: |
| 324 | // logical-OR-expression '?' ':' conditional-expression [GNU] |
Sebastian Redl | 15faa7f | 2008-12-09 20:22:58 +0000 | [diff] [blame] | 325 | TernaryMiddle = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 326 | Diag(Tok, diag::ext_gnu_conditional_expr); |
| 327 | } |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 328 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 329 | if (Tok.isNot(tok::colon)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 330 | Diag(Tok, diag::err_expected_colon); |
Chris Lattner | 28eb7e9 | 2008-11-23 23:17:07 +0000 | [diff] [blame] | 331 | Diag(OpToken, diag::note_matching) << "?"; |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 332 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 333 | } |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 334 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 335 | // Eat the colon. |
| 336 | ColonLoc = ConsumeToken(); |
| 337 | } |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 338 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 339 | // Parse another leaf here for the RHS of the operator. |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 340 | // ParseCastExpression works here because all RHS expressions in C have it |
| 341 | // as a prefix, at least. However, in C++, an assignment-expression could |
| 342 | // be a throw-expression, which is not a valid cast-expression. |
| 343 | // Therefore we need some special-casing here. |
| 344 | // Also note that the third operand of the conditional operator is |
| 345 | // an assignment-expression in C++. |
| 346 | OwningExprResult RHS(Actions); |
| 347 | if (getLang().CPlusPlus && NextTokPrec <= prec::Conditional) |
| 348 | RHS = ParseAssignmentExpression(); |
| 349 | else |
| 350 | RHS = ParseCastExpression(false); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 351 | if (RHS.isInvalid()) |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 352 | return move(RHS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 353 | |
| 354 | // Remember the precedence of this operator and get the precedence of the |
| 355 | // operator immediately to the right of the RHS. |
| 356 | unsigned ThisPrec = NextTokPrec; |
Douglas Gregor | 3965b7b | 2009-02-25 23:02:36 +0000 | [diff] [blame] | 357 | NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator, |
| 358 | getLang().CPlusPlus0x); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 359 | |
| 360 | // Assignment and conditional expressions are right-associative. |
Chris Lattner | d7d860d | 2007-12-18 06:06:23 +0000 | [diff] [blame] | 361 | bool isRightAssoc = ThisPrec == prec::Conditional || |
| 362 | ThisPrec == prec::Assignment; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 363 | |
| 364 | // Get the precedence of the operator to the right of the RHS. If it binds |
| 365 | // more tightly with RHS than we do, evaluate it completely first. |
| 366 | if (ThisPrec < NextTokPrec || |
| 367 | (ThisPrec == NextTokPrec && isRightAssoc)) { |
| 368 | // If this is left-associative, only parse things on the RHS that bind |
| 369 | // more tightly than the current operator. If it is left-associative, it |
| 370 | // is okay, to bind exactly as tightly. For example, compile A=B=C=D as |
| 371 | // 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] | 372 | // The function takes ownership of the RHS. |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 373 | RHS = ParseRHSOfBinaryExpression(move(RHS), ThisPrec + !isRightAssoc); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 374 | if (RHS.isInvalid()) |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 375 | return move(RHS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 376 | |
Douglas Gregor | 3965b7b | 2009-02-25 23:02:36 +0000 | [diff] [blame] | 377 | NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator, |
| 378 | getLang().CPlusPlus0x); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 379 | } |
| 380 | assert(NextTokPrec <= ThisPrec && "Recursion didn't work!"); |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 381 | |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 382 | if (!LHS.isInvalid()) { |
Chris Lattner | d56d6b6 | 2007-08-31 05:01:50 +0000 | [diff] [blame] | 383 | // Combine the LHS and RHS into the LHS (e.g. build AST). |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 384 | if (TernaryMiddle.isInvalid()) { |
| 385 | // If we're using '>>' as an operator within a template |
| 386 | // argument list (in C++98), suggest the addition of |
| 387 | // parentheses so that the code remains well-formed in C++0x. |
| 388 | if (!GreaterThanIsOperator && OpToken.is(tok::greatergreater)) |
| 389 | SuggestParentheses(OpToken.getLocation(), |
| 390 | diag::warn_cxx0x_right_shift_in_template_arg, |
| 391 | SourceRange(Actions.getExprRange(LHS.get()).getBegin(), |
| 392 | Actions.getExprRange(RHS.get()).getEnd())); |
| 393 | |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 394 | LHS = Actions.ActOnBinOp(CurScope, OpToken.getLocation(), |
Sebastian Redl | 76ad2e8 | 2009-02-05 15:02:23 +0000 | [diff] [blame] | 395 | OpToken.getKind(), move(LHS), move(RHS)); |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 396 | } else |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 397 | LHS = Actions.ActOnConditionalOp(OpToken.getLocation(), ColonLoc, |
Sebastian Redl | 76ad2e8 | 2009-02-05 15:02:23 +0000 | [diff] [blame] | 398 | move(LHS), move(TernaryMiddle), |
| 399 | move(RHS)); |
Chris Lattner | d56d6b6 | 2007-08-31 05:01:50 +0000 | [diff] [blame] | 400 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | |
| 404 | /// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 405 | /// true, parse a unary-expression. isAddressOfOperand exists because an |
| 406 | /// id-expression that is the operand of address-of gets special treatment |
| 407 | /// due to member pointers. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 408 | /// |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 409 | Parser::OwningExprResult Parser::ParseCastExpression(bool isUnaryExpression, |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 410 | bool isAddressOfOperand, |
| 411 | bool parseParenAsExprList){ |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 412 | bool NotCastExpr; |
| 413 | OwningExprResult Res = ParseCastExpression(isUnaryExpression, |
| 414 | isAddressOfOperand, |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 415 | NotCastExpr, |
| 416 | parseParenAsExprList); |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 417 | if (NotCastExpr) |
| 418 | Diag(Tok, diag::err_expected_expression); |
| 419 | return move(Res); |
| 420 | } |
| 421 | |
| 422 | /// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is |
| 423 | /// true, parse a unary-expression. isAddressOfOperand exists because an |
| 424 | /// id-expression that is the operand of address-of gets special treatment |
| 425 | /// due to member pointers. NotCastExpr is set to true if the token is not the |
| 426 | /// start of a cast-expression, and no diagnostic is emitted in this case. |
| 427 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 428 | /// cast-expression: [C99 6.5.4] |
| 429 | /// unary-expression |
| 430 | /// '(' type-name ')' cast-expression |
| 431 | /// |
| 432 | /// unary-expression: [C99 6.5.3] |
| 433 | /// postfix-expression |
| 434 | /// '++' unary-expression |
| 435 | /// '--' unary-expression |
| 436 | /// unary-operator cast-expression |
| 437 | /// 'sizeof' unary-expression |
| 438 | /// 'sizeof' '(' type-name ')' |
| 439 | /// [GNU] '__alignof' unary-expression |
| 440 | /// [GNU] '__alignof' '(' type-name ')' |
Douglas Gregor | 85bb3da | 2008-11-06 15:17:27 +0000 | [diff] [blame] | 441 | /// [C++0x] 'alignof' '(' type-id ')' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 442 | /// [GNU] '&&' identifier |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 443 | /// [C++] new-expression |
| 444 | /// [C++] delete-expression |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 445 | /// |
| 446 | /// unary-operator: one of |
| 447 | /// '&' '*' '+' '-' '~' '!' |
| 448 | /// [GNU] '__extension__' '__real' '__imag' |
| 449 | /// |
| 450 | /// primary-expression: [C99 6.5.1] |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 451 | /// [C99] identifier |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 452 | /// [C++] id-expression |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 453 | /// constant |
| 454 | /// string-literal |
| 455 | /// [C++] boolean-literal [C++ 2.13.5] |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 456 | /// [C++0x] 'nullptr' [C++0x 2.14.7] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 457 | /// '(' expression ')' |
| 458 | /// '__func__' [C99 6.4.2.2] |
| 459 | /// [GNU] '__FUNCTION__' |
| 460 | /// [GNU] '__PRETTY_FUNCTION__' |
| 461 | /// [GNU] '(' compound-statement ')' |
| 462 | /// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')' |
| 463 | /// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')' |
| 464 | /// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ',' |
| 465 | /// assign-expr ')' |
| 466 | /// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')' |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 467 | /// [GNU] '__null' |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 468 | /// [OBJC] '[' objc-message-expr ']' |
Chris Lattner | 5ac87ed | 2008-01-25 18:58:06 +0000 | [diff] [blame] | 469 | /// [OBJC] '@selector' '(' objc-selector-arg ')' |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 470 | /// [OBJC] '@protocol' '(' identifier ')' |
| 471 | /// [OBJC] '@encode' '(' type-name ')' |
Fariborz Jahanian | 0ccb27d | 2007-09-05 19:52:07 +0000 | [diff] [blame] | 472 | /// [OBJC] objc-string-literal |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 473 | /// [C++] simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3] |
| 474 | /// [C++] typename-specifier '(' expression-list[opt] ')' [TODO] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 475 | /// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1] |
| 476 | /// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1] |
| 477 | /// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1] |
| 478 | /// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1] |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 479 | /// [C++] 'typeid' '(' expression ')' [C++ 5.2p1] |
| 480 | /// [C++] 'typeid' '(' type-id ')' [C++ 5.2p1] |
Argyrios Kyrtzidis | d7464be | 2008-07-16 07:23:27 +0000 | [diff] [blame] | 481 | /// [C++] 'this' [C++ 9.3.2] |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 482 | /// [G++] unary-type-trait '(' type-id ')' |
| 483 | /// [G++] binary-type-trait '(' type-id ',' type-id ')' [TODO] |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 484 | /// [clang] '^' block-literal |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 485 | /// |
| 486 | /// constant: [C99 6.4.4] |
| 487 | /// integer-constant |
| 488 | /// floating-constant |
| 489 | /// enumeration-constant -> identifier |
| 490 | /// character-constant |
| 491 | /// |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 492 | /// id-expression: [C++ 5.1] |
| 493 | /// unqualified-id |
| 494 | /// qualified-id [TODO] |
| 495 | /// |
| 496 | /// unqualified-id: [C++ 5.1] |
| 497 | /// identifier |
| 498 | /// operator-function-id |
| 499 | /// conversion-function-id [TODO] |
| 500 | /// '~' class-name [TODO] |
| 501 | /// template-id [TODO] |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 502 | /// |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 503 | /// new-expression: [C++ 5.3.4] |
| 504 | /// '::'[opt] 'new' new-placement[opt] new-type-id |
| 505 | /// new-initializer[opt] |
| 506 | /// '::'[opt] 'new' new-placement[opt] '(' type-id ')' |
| 507 | /// new-initializer[opt] |
| 508 | /// |
| 509 | /// delete-expression: [C++ 5.3.5] |
| 510 | /// '::'[opt] 'delete' cast-expression |
| 511 | /// '::'[opt] 'delete' '[' ']' cast-expression |
| 512 | /// |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 513 | /// [GNU] unary-type-trait: |
| 514 | /// '__has_nothrow_assign' [TODO] |
| 515 | /// '__has_nothrow_copy' [TODO] |
| 516 | /// '__has_nothrow_constructor' [TODO] |
| 517 | /// '__has_trivial_assign' [TODO] |
| 518 | /// '__has_trivial_copy' [TODO] |
Anders Carlsson | 347ba89 | 2009-04-16 00:08:20 +0000 | [diff] [blame] | 519 | /// '__has_trivial_constructor' |
Anders Carlsson | 072abef | 2009-04-17 02:34:54 +0000 | [diff] [blame] | 520 | /// '__has_trivial_destructor' |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 521 | /// '__has_virtual_destructor' [TODO] |
| 522 | /// '__is_abstract' [TODO] |
| 523 | /// '__is_class' |
| 524 | /// '__is_empty' [TODO] |
| 525 | /// '__is_enum' |
| 526 | /// '__is_pod' |
| 527 | /// '__is_polymorphic' |
| 528 | /// '__is_union' |
| 529 | /// |
| 530 | /// [GNU] binary-type-trait: |
| 531 | /// '__is_base_of' [TODO] |
| 532 | /// |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 533 | Parser::OwningExprResult Parser::ParseCastExpression(bool isUnaryExpression, |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 534 | bool isAddressOfOperand, |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 535 | bool &NotCastExpr, |
| 536 | bool parseParenAsExprList){ |
Sebastian Redl | 15faa7f | 2008-12-09 20:22:58 +0000 | [diff] [blame] | 537 | OwningExprResult Res(Actions); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 538 | tok::TokenKind SavedKind = Tok.getKind(); |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 539 | NotCastExpr = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 540 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 541 | // This handles all of cast-expression, unary-expression, postfix-expression, |
| 542 | // and primary-expression. We handle them together like this for efficiency |
| 543 | // and to simplify handling of an expression starting with a '(' token: which |
| 544 | // may be one of a parenthesized expression, cast-expression, compound literal |
| 545 | // expression, or statement expression. |
| 546 | // |
| 547 | // If the parsed tokens consist of a primary-expression, the cases below |
| 548 | // call ParsePostfixExpressionSuffix to handle the postfix expression |
| 549 | // suffixes. Cases that cannot be followed by postfix exprs should |
| 550 | // return without invoking ParsePostfixExpressionSuffix. |
| 551 | switch (SavedKind) { |
| 552 | case tok::l_paren: { |
| 553 | // If this expression is limited to being a unary-expression, the parent can |
| 554 | // not start a cast expression. |
| 555 | ParenParseOption ParenExprType = |
| 556 | isUnaryExpression ? CompoundLiteral : CastExpr; |
| 557 | TypeTy *CastTy; |
| 558 | SourceLocation LParenLoc = Tok.getLocation(); |
| 559 | SourceLocation RParenLoc; |
Argyrios Kyrtzidis | 0350ca5 | 2009-05-22 10:23:40 +0000 | [diff] [blame] | 560 | Res = ParseParenExpression(ParenExprType, false/*stopIfCastExr*/, |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 561 | parseParenAsExprList, CastTy, RParenLoc); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 562 | if (Res.isInvalid()) return move(Res); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 563 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 564 | switch (ParenExprType) { |
| 565 | case SimpleExpr: break; // Nothing else to do. |
| 566 | case CompoundStmt: break; // Nothing else to do. |
| 567 | case CompoundLiteral: |
| 568 | // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of |
| 569 | // postfix-expression exist, parse them now. |
| 570 | break; |
| 571 | case CastExpr: |
Argyrios Kyrtzidis | 0350ca5 | 2009-05-22 10:23:40 +0000 | [diff] [blame] | 572 | // We have parsed the cast-expression and no postfix-expr pieces are |
| 573 | // following. |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 574 | return move(Res); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 575 | } |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 576 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 577 | // These can be followed by postfix-expr pieces. |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 578 | return ParsePostfixExpressionSuffix(move(Res)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 579 | } |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 580 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 581 | // primary-expression |
| 582 | case tok::numeric_constant: |
| 583 | // constant: integer-constant |
| 584 | // constant: floating-constant |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 585 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 586 | Res = Actions.ActOnNumericConstant(Tok); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 587 | ConsumeToken(); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 588 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 589 | // These can be followed by postfix-expr pieces. |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 590 | return ParsePostfixExpressionSuffix(move(Res)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 591 | |
| 592 | case tok::kw_true: |
| 593 | case tok::kw_false: |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 594 | return ParseCXXBoolLiteral(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 595 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 596 | case tok::kw_nullptr: |
| 597 | return Actions.ActOnCXXNullPtrLiteral(ConsumeToken()); |
| 598 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 599 | case tok::identifier: { // primary-expression: identifier |
| 600 | // unqualified-id: identifier |
| 601 | // constant: enumeration-constant |
Chris Lattner | b31757b | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 602 | // Turn a potentially qualified name into a annot_typename or |
Chris Lattner | 74ba410 | 2009-01-04 22:52:14 +0000 | [diff] [blame] | 603 | // 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] | 604 | if (getLang().CPlusPlus) { |
Chris Lattner | e26ff02 | 2009-01-04 23:46:59 +0000 | [diff] [blame] | 605 | // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse. |
| 606 | if (TryAnnotateTypeOrScopeToken()) |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 607 | return ParseCastExpression(isUnaryExpression, isAddressOfOperand); |
Chris Lattner | a7bc7c8 | 2009-01-04 23:23:14 +0000 | [diff] [blame] | 608 | } |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 609 | |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 610 | // Support 'Class.property' notation. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 611 | // We don't use isTokObjCMessageIdentifierReceiver(), since it allows |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 612 | // 'super' (which is inappropriate here). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 613 | if (getLang().ObjC1 && |
| 614 | Actions.getTypeName(*Tok.getIdentifierInfo(), |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 615 | Tok.getLocation(), CurScope) && |
| 616 | NextToken().is(tok::period)) { |
| 617 | IdentifierInfo &ReceiverName = *Tok.getIdentifierInfo(); |
| 618 | SourceLocation IdentLoc = ConsumeToken(); |
| 619 | SourceLocation DotLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 620 | |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 621 | if (Tok.isNot(tok::identifier)) { |
| 622 | Diag(Tok, diag::err_expected_ident); |
| 623 | return ExprError(); |
| 624 | } |
| 625 | IdentifierInfo &PropertyName = *Tok.getIdentifierInfo(); |
| 626 | SourceLocation PropertyLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 627 | |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 628 | Res = Actions.ActOnClassPropertyRefExpr(ReceiverName, PropertyName, |
| 629 | IdentLoc, PropertyLoc); |
Steve Naroff | ed91f90 | 2009-04-02 18:37:59 +0000 | [diff] [blame] | 630 | // These can be followed by postfix-expr pieces. |
| 631 | return ParsePostfixExpressionSuffix(move(Res)); |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 632 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 633 | // Consume the identifier so that we can see if it is followed by a '('. |
| 634 | // Function designators are allowed to be undeclared (C99 6.5.1p2), so we |
| 635 | // need to know whether or not this identifier is a function designator or |
| 636 | // not. |
| 637 | IdentifierInfo &II = *Tok.getIdentifierInfo(); |
| 638 | SourceLocation L = ConsumeToken(); |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 639 | Res = Actions.ActOnIdentifierExpr(CurScope, L, II, Tok.is(tok::l_paren)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 640 | // These can be followed by postfix-expr pieces. |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 641 | return ParsePostfixExpressionSuffix(move(Res)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 642 | } |
| 643 | case tok::char_constant: // constant: character-constant |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 644 | Res = Actions.ActOnCharacterConstant(Tok); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 645 | ConsumeToken(); |
| 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)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 648 | case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2] |
| 649 | case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU] |
| 650 | case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU] |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 651 | Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 652 | ConsumeToken(); |
| 653 | // These can be followed by postfix-expr pieces. |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 654 | return ParsePostfixExpressionSuffix(move(Res)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 655 | case tok::string_literal: // primary-expression: string-literal |
| 656 | case tok::wide_string_literal: |
| 657 | Res = ParseStringLiteralExpression(); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 658 | if (Res.isInvalid()) return move(Res); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 659 | // This can be followed by postfix-expr pieces (e.g. "foo"[1]). |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 660 | return ParsePostfixExpressionSuffix(move(Res)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 661 | case tok::kw___builtin_va_arg: |
| 662 | case tok::kw___builtin_offsetof: |
| 663 | case tok::kw___builtin_choose_expr: |
| 664 | case tok::kw___builtin_types_compatible_p: |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 665 | return ParseBuiltinPrimaryExpression(); |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 666 | case tok::kw___null: |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 667 | return Actions.ActOnGNUNullExpr(ConsumeToken()); |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 668 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 669 | case tok::plusplus: // unary-expression: '++' unary-expression |
| 670 | case tok::minusminus: { // unary-expression: '--' unary-expression |
| 671 | SourceLocation SavedLoc = ConsumeToken(); |
| 672 | Res = ParseCastExpression(true); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 673 | if (!Res.isInvalid()) |
Sebastian Redl | 76ad2e8 | 2009-02-05 15:02:23 +0000 | [diff] [blame] | 674 | Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res)); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 675 | return move(Res); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 676 | } |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 677 | case tok::amp: { // unary-expression: '&' cast-expression |
| 678 | // Special treatment because of member pointers |
| 679 | SourceLocation SavedLoc = ConsumeToken(); |
| 680 | Res = ParseCastExpression(false, true); |
| 681 | if (!Res.isInvalid()) |
Sebastian Redl | 76ad2e8 | 2009-02-05 15:02:23 +0000 | [diff] [blame] | 682 | Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res)); |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 683 | return move(Res); |
| 684 | } |
| 685 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 686 | case tok::star: // unary-expression: '*' cast-expression |
| 687 | case tok::plus: // unary-expression: '+' cast-expression |
| 688 | case tok::minus: // unary-expression: '-' cast-expression |
| 689 | case tok::tilde: // unary-expression: '~' cast-expression |
| 690 | case tok::exclaim: // unary-expression: '!' cast-expression |
| 691 | case tok::kw___real: // unary-expression: '__real' cast-expression [GNU] |
Chris Lattner | 3508084 | 2008-02-02 20:20:10 +0000 | [diff] [blame] | 692 | case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 693 | SourceLocation SavedLoc = ConsumeToken(); |
| 694 | Res = ParseCastExpression(false); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 695 | if (!Res.isInvalid()) |
Sebastian Redl | 76ad2e8 | 2009-02-05 15:02:23 +0000 | [diff] [blame] | 696 | Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res)); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 697 | return move(Res); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 698 | } |
| 699 | |
Chris Lattner | 3508084 | 2008-02-02 20:20:10 +0000 | [diff] [blame] | 700 | case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU] |
| 701 | // __extension__ silences extension warnings in the subexpression. |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 702 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
Chris Lattner | 3508084 | 2008-02-02 20:20:10 +0000 | [diff] [blame] | 703 | SourceLocation SavedLoc = ConsumeToken(); |
| 704 | Res = ParseCastExpression(false); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 705 | if (!Res.isInvalid()) |
Sebastian Redl | 76ad2e8 | 2009-02-05 15:02:23 +0000 | [diff] [blame] | 706 | Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res)); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 707 | return move(Res); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 708 | } |
| 709 | case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression |
| 710 | // unary-expression: 'sizeof' '(' type-name ')' |
Douglas Gregor | 85bb3da | 2008-11-06 15:17:27 +0000 | [diff] [blame] | 711 | case tok::kw_alignof: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 712 | case tok::kw___alignof: // unary-expression: '__alignof' unary-expression |
| 713 | // unary-expression: '__alignof' '(' type-name ')' |
Douglas Gregor | 85bb3da | 2008-11-06 15:17:27 +0000 | [diff] [blame] | 714 | // unary-expression: 'alignof' '(' type-id ')' |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 715 | return ParseSizeofAlignofExpression(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 716 | case tok::ampamp: { // unary-expression: '&&' identifier |
| 717 | SourceLocation AmpAmpLoc = ConsumeToken(); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 718 | if (Tok.isNot(tok::identifier)) |
| 719 | return ExprError(Diag(Tok, diag::err_expected_ident)); |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 720 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 721 | Diag(AmpAmpLoc, diag::ext_gnu_address_of_label); |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 722 | Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 723 | Tok.getIdentifierInfo()); |
| 724 | ConsumeToken(); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 725 | return move(Res); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 726 | } |
| 727 | case tok::kw_const_cast: |
| 728 | case tok::kw_dynamic_cast: |
| 729 | case tok::kw_reinterpret_cast: |
| 730 | case tok::kw_static_cast: |
Argyrios Kyrtzidis | b348b81 | 2008-08-16 19:45:32 +0000 | [diff] [blame] | 731 | Res = ParseCXXCasts(); |
| 732 | // These can be followed by postfix-expr pieces. |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 733 | return ParsePostfixExpressionSuffix(move(Res)); |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 734 | case tok::kw_typeid: |
| 735 | Res = ParseCXXTypeid(); |
| 736 | // This can be followed by postfix-expr pieces. |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 737 | return ParsePostfixExpressionSuffix(move(Res)); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 738 | case tok::kw_this: |
Argyrios Kyrtzidis | 289d773 | 2008-08-16 19:34:46 +0000 | [diff] [blame] | 739 | Res = ParseCXXThis(); |
| 740 | // This can be followed by postfix-expr pieces. |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 741 | return ParsePostfixExpressionSuffix(move(Res)); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 742 | |
| 743 | case tok::kw_char: |
| 744 | case tok::kw_wchar_t: |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 745 | case tok::kw_char16_t: |
| 746 | case tok::kw_char32_t: |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 747 | case tok::kw_bool: |
| 748 | case tok::kw_short: |
| 749 | case tok::kw_int: |
| 750 | case tok::kw_long: |
| 751 | case tok::kw_signed: |
| 752 | case tok::kw_unsigned: |
| 753 | case tok::kw_float: |
| 754 | case tok::kw_double: |
| 755 | case tok::kw_void: |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 756 | case tok::kw_typename: |
Chris Lattner | 2dcaab3 | 2009-01-04 22:28:21 +0000 | [diff] [blame] | 757 | case tok::kw_typeof: |
Chris Lattner | b31757b | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 758 | case tok::annot_typename: { |
Chris Lattner | 2dcaab3 | 2009-01-04 22:28:21 +0000 | [diff] [blame] | 759 | if (!getLang().CPlusPlus) { |
| 760 | Diag(Tok, diag::err_expected_expression); |
| 761 | return ExprError(); |
| 762 | } |
Eli Friedman | 2e0cdb4 | 2009-06-11 00:33:41 +0000 | [diff] [blame] | 763 | |
| 764 | if (SavedKind == tok::kw_typename) { |
| 765 | // postfix-expression: typename-specifier '(' expression-list[opt] ')' |
| 766 | if (!TryAnnotateTypeOrScopeToken()) |
| 767 | return ExprError(); |
| 768 | } |
| 769 | |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 770 | // postfix-expression: simple-type-specifier '(' expression-list[opt] ')' |
| 771 | // |
| 772 | DeclSpec DS; |
| 773 | ParseCXXSimpleTypeSpecifier(DS); |
| 774 | if (Tok.isNot(tok::l_paren)) |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 775 | return ExprError(Diag(Tok, diag::err_expected_lparen_after_type) |
| 776 | << DS.getSourceRange()); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 777 | |
| 778 | Res = ParseCXXTypeConstructExpression(DS); |
| 779 | // This can be followed by postfix-expr pieces. |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 780 | return ParsePostfixExpressionSuffix(move(Res)); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 781 | } |
| 782 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 783 | case tok::annot_cxxscope: // [C++] id-expression: qualified-id |
| 784 | case tok::kw_operator: // [C++] id-expression: operator/conversion-function-id |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 785 | case tok::annot_template_id: // [C++] template-id |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 786 | Res = ParseCXXIdExpression(isAddressOfOperand); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 787 | return ParsePostfixExpressionSuffix(move(Res)); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 788 | |
Chris Lattner | 74ba410 | 2009-01-04 22:52:14 +0000 | [diff] [blame] | 789 | case tok::coloncolon: { |
Chris Lattner | 5b45473 | 2009-01-05 03:55:46 +0000 | [diff] [blame] | 790 | // ::foo::bar -> global qualified name etc. If TryAnnotateTypeOrScopeToken |
| 791 | // annotates the token, tail recurse. |
| 792 | if (TryAnnotateTypeOrScopeToken()) |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 793 | return ParseCastExpression(isUnaryExpression, isAddressOfOperand); |
| 794 | |
Chris Lattner | 74ba410 | 2009-01-04 22:52:14 +0000 | [diff] [blame] | 795 | // ::new -> [C++] new-expression |
| 796 | // ::delete -> [C++] delete-expression |
Chris Lattner | 5b45473 | 2009-01-05 03:55:46 +0000 | [diff] [blame] | 797 | SourceLocation CCLoc = ConsumeToken(); |
Chris Lattner | 59232d3 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 798 | if (Tok.is(tok::kw_new)) |
Chris Lattner | 5b45473 | 2009-01-05 03:55:46 +0000 | [diff] [blame] | 799 | return ParseCXXNewExpression(true, CCLoc); |
Chris Lattner | 74ba410 | 2009-01-04 22:52:14 +0000 | [diff] [blame] | 800 | if (Tok.is(tok::kw_delete)) |
Chris Lattner | 5b45473 | 2009-01-05 03:55:46 +0000 | [diff] [blame] | 801 | return ParseCXXDeleteExpression(true, CCLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 802 | |
Chris Lattner | a7bc7c8 | 2009-01-04 23:23:14 +0000 | [diff] [blame] | 803 | // 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] | 804 | Diag(CCLoc, diag::err_expected_expression); |
Chris Lattner | a7bc7c8 | 2009-01-04 23:23:14 +0000 | [diff] [blame] | 805 | return ExprError(); |
Chris Lattner | 59232d3 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 806 | } |
Sebastian Redl | fb4ccd7 | 2008-12-02 16:35:44 +0000 | [diff] [blame] | 807 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 808 | case tok::kw_new: // [C++] new-expression |
Chris Lattner | 59232d3 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 809 | return ParseCXXNewExpression(false, Tok.getLocation()); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 810 | |
| 811 | case tok::kw_delete: // [C++] delete-expression |
Chris Lattner | 59232d3 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 812 | return ParseCXXDeleteExpression(false, Tok.getLocation()); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 813 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 814 | case tok::kw___is_pod: // [GNU] unary-type-trait |
| 815 | case tok::kw___is_class: |
| 816 | case tok::kw___is_enum: |
| 817 | case tok::kw___is_union: |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 818 | case tok::kw___is_empty: |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 819 | case tok::kw___is_polymorphic: |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 820 | case tok::kw___is_abstract: |
Anders Carlsson | 347ba89 | 2009-04-16 00:08:20 +0000 | [diff] [blame] | 821 | case tok::kw___has_trivial_constructor: |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 822 | case tok::kw___has_trivial_copy: |
| 823 | case tok::kw___has_trivial_assign: |
Anders Carlsson | 072abef | 2009-04-17 02:34:54 +0000 | [diff] [blame] | 824 | case tok::kw___has_trivial_destructor: |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 825 | return ParseUnaryTypeTrait(); |
| 826 | |
Chris Lattner | c97c204 | 2007-10-03 22:03:06 +0000 | [diff] [blame] | 827 | case tok::at: { |
| 828 | SourceLocation AtLoc = ConsumeToken(); |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 829 | return ParseObjCAtExpression(AtLoc); |
Chris Lattner | c97c204 | 2007-10-03 22:03:06 +0000 | [diff] [blame] | 830 | } |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 831 | case tok::caret: |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 832 | return ParsePostfixExpressionSuffix(ParseBlockLiteralExpression()); |
Chris Lattner | fdb548e | 2008-12-12 19:20:14 +0000 | [diff] [blame] | 833 | case tok::l_square: |
| 834 | // These can be followed by postfix-expr pieces. |
| 835 | if (getLang().ObjC1) |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 836 | return ParsePostfixExpressionSuffix(ParseObjCMessageExpression()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 837 | // FALL THROUGH. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 838 | default: |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 839 | NotCastExpr = true; |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 840 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 841 | } |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 842 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 843 | // unreachable. |
| 844 | abort(); |
| 845 | } |
| 846 | |
| 847 | /// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression |
| 848 | /// is parsed, this method parses any suffixes that apply. |
| 849 | /// |
| 850 | /// postfix-expression: [C99 6.5.2] |
| 851 | /// primary-expression |
| 852 | /// postfix-expression '[' expression ']' |
| 853 | /// postfix-expression '(' argument-expression-list[opt] ')' |
| 854 | /// postfix-expression '.' identifier |
| 855 | /// postfix-expression '->' identifier |
| 856 | /// postfix-expression '++' |
| 857 | /// postfix-expression '--' |
| 858 | /// '(' type-name ')' '{' initializer-list '}' |
| 859 | /// '(' type-name ')' '{' initializer-list ',' '}' |
| 860 | /// |
| 861 | /// argument-expression-list: [C99 6.5.2] |
| 862 | /// argument-expression |
| 863 | /// argument-expression-list ',' assignment-expression |
| 864 | /// |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 865 | Parser::OwningExprResult |
| 866 | Parser::ParsePostfixExpressionSuffix(OwningExprResult LHS) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 867 | // Now that the primary-expression piece of the postfix-expression has been |
| 868 | // parsed, see if there are any postfix-expression pieces here. |
| 869 | SourceLocation Loc; |
| 870 | while (1) { |
| 871 | switch (Tok.getKind()) { |
| 872 | default: // Not a postfix-expression suffix. |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 873 | return move(LHS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 874 | case tok::l_square: { // postfix-expression: p-e '[' expression ']' |
| 875 | Loc = ConsumeBracket(); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 876 | OwningExprResult Idx(ParseExpression()); |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 877 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 878 | SourceLocation RLoc = Tok.getLocation(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 879 | |
| 880 | if (!LHS.isInvalid() && !Idx.isInvalid() && Tok.is(tok::r_square)) { |
Sebastian Redl | 76ad2e8 | 2009-02-05 15:02:23 +0000 | [diff] [blame] | 881 | LHS = Actions.ActOnArraySubscriptExpr(CurScope, move(LHS), Loc, |
| 882 | move(Idx), RLoc); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 883 | } else |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 884 | LHS = ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 885 | |
| 886 | // Match the ']'. |
| 887 | MatchRHSPunctuation(tok::r_square, Loc); |
| 888 | break; |
| 889 | } |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 890 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 891 | case tok::l_paren: { // p-e: p-e '(' argument-expression-list[opt] ')' |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 892 | ExprVector ArgExprs(Actions); |
Argyrios Kyrtzidis | 0cd5b42 | 2008-08-16 20:03:01 +0000 | [diff] [blame] | 893 | CommaLocsTy CommaLocs; |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 894 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 895 | Loc = ConsumeParen(); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 896 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 897 | if (Tok.isNot(tok::r_paren)) { |
Argyrios Kyrtzidis | 0cd5b42 | 2008-08-16 20:03:01 +0000 | [diff] [blame] | 898 | if (ParseExpressionList(ArgExprs, CommaLocs)) { |
| 899 | SkipUntil(tok::r_paren); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 900 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 901 | } |
| 902 | } |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 903 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 904 | // Match the ')'. |
Chris Lattner | 1721a2d | 2009-04-13 00:10:38 +0000 | [diff] [blame] | 905 | if (Tok.isNot(tok::r_paren)) { |
| 906 | MatchRHSPunctuation(tok::r_paren, Loc); |
| 907 | return ExprError(); |
| 908 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 909 | |
Chris Lattner | 1721a2d | 2009-04-13 00:10:38 +0000 | [diff] [blame] | 910 | if (!LHS.isInvalid()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 911 | assert((ArgExprs.size() == 0 || ArgExprs.size()-1 == CommaLocs.size())&& |
| 912 | "Unexpected number of commas!"); |
Sebastian Redl | 76ad2e8 | 2009-02-05 15:02:23 +0000 | [diff] [blame] | 913 | LHS = Actions.ActOnCallExpr(CurScope, move(LHS), Loc, |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 914 | move_arg(ArgExprs), CommaLocs.data(), |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 915 | Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 916 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 917 | |
Chris Lattner | 1721a2d | 2009-04-13 00:10:38 +0000 | [diff] [blame] | 918 | ConsumeParen(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 919 | break; |
| 920 | } |
Douglas Gregor | 3b6afbb | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 921 | case tok::arrow: |
| 922 | case tok::period: { |
| 923 | // postfix-expression: p-e '->' template[opt] id-expression |
| 924 | // postfix-expression: p-e '.' template[opt] id-expression |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 925 | tok::TokenKind OpKind = Tok.getKind(); |
| 926 | SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token. |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 927 | |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 928 | CXXScopeSpec SS; |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 929 | Action::TypeTy *ObjectType = 0; |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 930 | if (getLang().CPlusPlus && !LHS.isInvalid()) { |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 931 | LHS = Actions.ActOnStartCXXMemberReference(CurScope, move(LHS), |
| 932 | OpLoc, OpKind, ObjectType); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 933 | if (LHS.isInvalid()) |
| 934 | break; |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 935 | ParseOptionalCXXScopeSpecifier(SS, ObjectType, false); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 936 | } |
| 937 | |
Anders Carlsson | ec77387 | 2009-08-25 23:46:41 +0000 | [diff] [blame] | 938 | if (Tok.is(tok::identifier)) { |
| 939 | if (!LHS.isInvalid()) |
| 940 | LHS = Actions.ActOnMemberReferenceExpr(CurScope, move(LHS), OpLoc, |
| 941 | OpKind, Tok.getLocation(), |
| 942 | *Tok.getIdentifierInfo(), |
| 943 | ObjCImpDecl, &SS); |
Douglas Gregor | a6f0f9d | 2009-08-31 19:52:13 +0000 | [diff] [blame] | 944 | ConsumeToken(); |
Anders Carlsson | ec77387 | 2009-08-25 23:46:41 +0000 | [diff] [blame] | 945 | } else if (getLang().CPlusPlus && Tok.is(tok::tilde)) { |
Douglas Gregor | f328a28 | 2009-08-31 21:16:32 +0000 | [diff] [blame] | 946 | // We have a C++ pseudo-destructor or a destructor call, e.g., t.~T() |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 947 | |
Anders Carlsson | ec77387 | 2009-08-25 23:46:41 +0000 | [diff] [blame] | 948 | // Consume the tilde. |
| 949 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 950 | |
Anders Carlsson | ec77387 | 2009-08-25 23:46:41 +0000 | [diff] [blame] | 951 | if (!Tok.is(tok::identifier)) { |
| 952 | Diag(Tok, diag::err_expected_ident); |
| 953 | return ExprError(); |
| 954 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 955 | |
Anders Carlsson | ec77387 | 2009-08-25 23:46:41 +0000 | [diff] [blame] | 956 | if (!LHS.isInvalid()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 957 | LHS = Actions.ActOnDestructorReferenceExpr(CurScope, move(LHS), |
Anders Carlsson | ec77387 | 2009-08-25 23:46:41 +0000 | [diff] [blame] | 958 | OpLoc, OpKind, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 959 | Tok.getLocation(), |
Anders Carlsson | ec77387 | 2009-08-25 23:46:41 +0000 | [diff] [blame] | 960 | Tok.getIdentifierInfo(), |
Douglas Gregor | a78c5c3 | 2009-09-04 18:29:40 +0000 | [diff] [blame] | 961 | SS, |
| 962 | NextToken().is(tok::l_paren)); |
Douglas Gregor | a6f0f9d | 2009-08-31 19:52:13 +0000 | [diff] [blame] | 963 | ConsumeToken(); |
| 964 | } else if (getLang().CPlusPlus && Tok.is(tok::kw_operator)) { |
Douglas Gregor | f328a28 | 2009-08-31 21:16:32 +0000 | [diff] [blame] | 965 | // We have a reference to a member operator, e.g., t.operator int or |
| 966 | // t.operator+. |
Douglas Gregor | a6f0f9d | 2009-08-31 19:52:13 +0000 | [diff] [blame] | 967 | if (OverloadedOperatorKind Op = TryParseOperatorFunctionId()) { |
| 968 | if (!LHS.isInvalid()) |
| 969 | LHS = Actions.ActOnOverloadedOperatorReferenceExpr(CurScope, |
| 970 | move(LHS), OpLoc, |
| 971 | OpKind, |
| 972 | Tok.getLocation(), |
| 973 | Op, &SS); |
| 974 | // TryParseOperatorFunctionId already consumed our token, so |
| 975 | // don't bother |
| 976 | } else if (TypeTy *ConvType = ParseConversionFunctionId()) { |
| 977 | if (!LHS.isInvalid()) |
| 978 | LHS = Actions.ActOnConversionOperatorReferenceExpr(CurScope, |
| 979 | move(LHS), OpLoc, |
| 980 | OpKind, |
| 981 | Tok.getLocation(), |
| 982 | ConvType, &SS); |
| 983 | } else { |
Douglas Gregor | a6f0f9d | 2009-08-31 19:52:13 +0000 | [diff] [blame] | 984 | // Don't emit a diagnostic; ParseConversionFunctionId does it for us |
| 985 | return ExprError(); |
| 986 | } |
Douglas Gregor | f328a28 | 2009-08-31 21:16:32 +0000 | [diff] [blame] | 987 | } else if (getLang().CPlusPlus && Tok.is(tok::annot_template_id)) { |
| 988 | // We have a reference to a member template along with explicitly- |
| 989 | // specified template arguments, e.g., t.f<int>. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 990 | TemplateIdAnnotation *TemplateId |
Douglas Gregor | f328a28 | 2009-08-31 21:16:32 +0000 | [diff] [blame] | 991 | = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); |
| 992 | if (!LHS.isInvalid()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 993 | ASTTemplateArgsPtr TemplateArgsPtr(Actions, |
Douglas Gregor | f328a28 | 2009-08-31 21:16:32 +0000 | [diff] [blame] | 994 | TemplateId->getTemplateArgs(), |
| 995 | TemplateId->getTemplateArgIsType(), |
| 996 | TemplateId->NumArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 997 | |
Douglas Gregor | f328a28 | 2009-08-31 21:16:32 +0000 | [diff] [blame] | 998 | LHS = Actions.ActOnMemberTemplateIdReferenceExpr(CurScope, move(LHS), |
| 999 | OpLoc, OpKind, SS, |
| 1000 | TemplateTy::make(TemplateId->Template), |
| 1001 | TemplateId->TemplateNameLoc, |
| 1002 | TemplateId->LAngleLoc, |
| 1003 | TemplateArgsPtr, |
| 1004 | TemplateId->getTemplateArgLocations(), |
| 1005 | TemplateId->RAngleLoc); |
| 1006 | } |
| 1007 | ConsumeToken(); |
Anders Carlsson | ec77387 | 2009-08-25 23:46:41 +0000 | [diff] [blame] | 1008 | } else { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1009 | Diag(Tok, diag::err_expected_ident); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 1010 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1011 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1012 | break; |
| 1013 | } |
| 1014 | case tok::plusplus: // postfix-expression: postfix-expression '++' |
| 1015 | case tok::minusminus: // postfix-expression: postfix-expression '--' |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1016 | if (!LHS.isInvalid()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1017 | LHS = Actions.ActOnPostfixUnaryOp(CurScope, Tok.getLocation(), |
Sebastian Redl | 76ad2e8 | 2009-02-05 15:02:23 +0000 | [diff] [blame] | 1018 | Tok.getKind(), move(LHS)); |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 1019 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1020 | ConsumeToken(); |
| 1021 | break; |
| 1022 | } |
| 1023 | } |
| 1024 | } |
| 1025 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 1026 | /// ParseExprAfterTypeofSizeofAlignof - We parsed a typeof/sizeof/alignof and |
| 1027 | /// we are at the start of an expression or a parenthesized type-id. |
| 1028 | /// OpTok is the operand token (typeof/sizeof/alignof). Returns the expression |
| 1029 | /// (isCastExpr == false) or the type (isCastExpr == true). |
| 1030 | /// |
| 1031 | /// unary-expression: [C99 6.5.3] |
| 1032 | /// 'sizeof' unary-expression |
| 1033 | /// 'sizeof' '(' type-name ')' |
| 1034 | /// [GNU] '__alignof' unary-expression |
| 1035 | /// [GNU] '__alignof' '(' type-name ')' |
| 1036 | /// [C++0x] 'alignof' '(' type-id ')' |
| 1037 | /// |
| 1038 | /// [GNU] typeof-specifier: |
| 1039 | /// typeof ( expressions ) |
| 1040 | /// typeof ( type-name ) |
| 1041 | /// [GNU/C++] typeof unary-expression |
| 1042 | /// |
| 1043 | Parser::OwningExprResult |
| 1044 | Parser::ParseExprAfterTypeofSizeofAlignof(const Token &OpTok, |
| 1045 | bool &isCastExpr, |
| 1046 | TypeTy *&CastTy, |
| 1047 | SourceRange &CastRange) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1048 | |
| 1049 | assert((OpTok.is(tok::kw_typeof) || OpTok.is(tok::kw_sizeof) || |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 1050 | OpTok.is(tok::kw___alignof) || OpTok.is(tok::kw_alignof)) && |
| 1051 | "Not a typeof/sizeof/alignof expression!"); |
| 1052 | |
| 1053 | OwningExprResult Operand(Actions); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1054 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 1055 | // If the operand doesn't start with an '(', it must be an expression. |
| 1056 | if (Tok.isNot(tok::l_paren)) { |
| 1057 | isCastExpr = false; |
| 1058 | if (OpTok.is(tok::kw_typeof) && !getLang().CPlusPlus) { |
| 1059 | Diag(Tok,diag::err_expected_lparen_after_id) << OpTok.getIdentifierInfo(); |
| 1060 | return ExprError(); |
| 1061 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1062 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1063 | // C++0x [expr.sizeof]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1064 | // [...] The operand is either an expression, which is an unevaluated |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1065 | // operand (Clause 5) [...] |
| 1066 | // |
| 1067 | // The GNU typeof and alignof extensions also behave as unevaluated |
| 1068 | // operands. |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 1069 | EnterExpressionEvaluationContext Unevaluated(Actions, |
| 1070 | Action::Unevaluated); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 1071 | Operand = ParseCastExpression(true/*isUnaryExpression*/); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 1072 | } else { |
| 1073 | // If it starts with a '(', we know that it is either a parenthesized |
| 1074 | // type-name, or it is a unary-expression that starts with a compound |
| 1075 | // literal, or starts with a primary-expression that is a parenthesized |
| 1076 | // expression. |
| 1077 | ParenParseOption ExprType = CastExpr; |
| 1078 | SourceLocation LParenLoc = Tok.getLocation(), RParenLoc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1079 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1080 | // C++0x [expr.sizeof]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1081 | // [...] The operand is either an expression, which is an unevaluated |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1082 | // operand (Clause 5) [...] |
| 1083 | // |
| 1084 | // The GNU typeof and alignof extensions also behave as unevaluated |
| 1085 | // operands. |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 1086 | EnterExpressionEvaluationContext Unevaluated(Actions, |
| 1087 | Action::Unevaluated); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1088 | Operand = ParseParenExpression(ExprType, true/*stopIfCastExpr*/, false, |
Argyrios Kyrtzidis | 0350ca5 | 2009-05-22 10:23:40 +0000 | [diff] [blame] | 1089 | CastTy, RParenLoc); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 1090 | CastRange = SourceRange(LParenLoc, RParenLoc); |
| 1091 | |
| 1092 | // If ParseParenExpression parsed a '(typename)' sequence only, then this is |
| 1093 | // a type. |
| 1094 | if (ExprType == CastExpr) { |
| 1095 | isCastExpr = true; |
| 1096 | return ExprEmpty(); |
| 1097 | } |
| 1098 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1099 | // If this is a parenthesized expression, it is the start of a |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 1100 | // unary-expression, but doesn't include any postfix pieces. Parse these |
| 1101 | // now if present. |
| 1102 | Operand = ParsePostfixExpressionSuffix(move(Operand)); |
| 1103 | } |
| 1104 | |
| 1105 | // If we get here, the operand to the typeof/sizeof/alignof was an expresion. |
| 1106 | isCastExpr = false; |
| 1107 | return move(Operand); |
| 1108 | } |
| 1109 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1110 | |
| 1111 | /// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression. |
| 1112 | /// unary-expression: [C99 6.5.3] |
| 1113 | /// 'sizeof' unary-expression |
| 1114 | /// 'sizeof' '(' type-name ')' |
| 1115 | /// [GNU] '__alignof' unary-expression |
| 1116 | /// [GNU] '__alignof' '(' type-name ')' |
Douglas Gregor | 85bb3da | 2008-11-06 15:17:27 +0000 | [diff] [blame] | 1117 | /// [C++0x] 'alignof' '(' type-id ')' |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1118 | Parser::OwningExprResult Parser::ParseSizeofAlignofExpression() { |
Douglas Gregor | 85bb3da | 2008-11-06 15:17:27 +0000 | [diff] [blame] | 1119 | assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof) |
| 1120 | || Tok.is(tok::kw_alignof)) && |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1121 | "Not a sizeof/alignof expression!"); |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1122 | Token OpTok = Tok; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1123 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1124 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 1125 | bool isCastExpr; |
| 1126 | TypeTy *CastTy; |
| 1127 | SourceRange CastRange; |
| 1128 | OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok, |
| 1129 | isCastExpr, |
| 1130 | CastTy, |
| 1131 | CastRange); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1132 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 1133 | if (isCastExpr) |
| 1134 | return Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(), |
| 1135 | OpTok.is(tok::kw_sizeof), |
| 1136 | /*isType=*/true, CastTy, |
| 1137 | CastRange); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1138 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1139 | // 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] | 1140 | if (!Operand.isInvalid()) |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1141 | Operand = Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(), |
| 1142 | OpTok.is(tok::kw_sizeof), |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 1143 | /*isType=*/false, |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 1144 | Operand.release(), CastRange); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1145 | return move(Operand); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
| 1148 | /// ParseBuiltinPrimaryExpression |
| 1149 | /// |
| 1150 | /// primary-expression: [C99 6.5.1] |
| 1151 | /// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')' |
| 1152 | /// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')' |
| 1153 | /// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ',' |
| 1154 | /// assign-expr ')' |
| 1155 | /// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')' |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1156 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1157 | /// [GNU] offsetof-member-designator: |
| 1158 | /// [GNU] identifier |
| 1159 | /// [GNU] offsetof-member-designator '.' identifier |
| 1160 | /// [GNU] offsetof-member-designator '[' expression ']' |
| 1161 | /// |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1162 | Parser::OwningExprResult Parser::ParseBuiltinPrimaryExpression() { |
Sebastian Redl | 15faa7f | 2008-12-09 20:22:58 +0000 | [diff] [blame] | 1163 | OwningExprResult Res(Actions); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1164 | const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo(); |
| 1165 | |
| 1166 | tok::TokenKind T = Tok.getKind(); |
| 1167 | SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier. |
| 1168 | |
| 1169 | // All of these start with an open paren. |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1170 | if (Tok.isNot(tok::l_paren)) |
| 1171 | return ExprError(Diag(Tok, diag::err_expected_lparen_after_id) |
| 1172 | << BuiltinII); |
| 1173 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1174 | SourceLocation LParenLoc = ConsumeParen(); |
| 1175 | // TODO: Build AST. |
| 1176 | |
| 1177 | switch (T) { |
| 1178 | default: assert(0 && "Not a builtin primary expression!"); |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 1179 | case tok::kw___builtin_va_arg: { |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 1180 | OwningExprResult Expr(ParseAssignmentExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1181 | if (Expr.isInvalid()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1182 | SkipUntil(tok::r_paren); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1183 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1184 | } |
| 1185 | |
| 1186 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1187 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1188 | |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 1189 | TypeResult Ty = ParseTypeName(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1190 | |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 1191 | if (Tok.isNot(tok::r_paren)) { |
| 1192 | Diag(Tok, diag::err_expected_rparen); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1193 | return ExprError(); |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 1194 | } |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 1195 | if (Ty.isInvalid()) |
| 1196 | Res = ExprError(); |
| 1197 | else |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1198 | Res = Actions.ActOnVAArg(StartLoc, move(Expr), Ty.get(), ConsumeParen()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1199 | break; |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 1200 | } |
Chris Lattner | f9aa3cb | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 1201 | case tok::kw___builtin_offsetof: { |
Chris Lattner | 9fddf0a | 2007-08-30 17:08:45 +0000 | [diff] [blame] | 1202 | SourceLocation TypeLoc = Tok.getLocation(); |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 1203 | TypeResult Ty = ParseTypeName(); |
Chris Lattner | ca7102c | 2009-03-24 17:21:43 +0000 | [diff] [blame] | 1204 | if (Ty.isInvalid()) { |
| 1205 | SkipUntil(tok::r_paren); |
| 1206 | return ExprError(); |
| 1207 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1208 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1209 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1210 | return ExprError(); |
| 1211 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1212 | // We must have at least one identifier here. |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1213 | if (Tok.isNot(tok::identifier)) { |
Chris Lattner | f9aa3cb | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 1214 | Diag(Tok, diag::err_expected_ident); |
| 1215 | SkipUntil(tok::r_paren); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1216 | return ExprError(); |
Chris Lattner | f9aa3cb | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 1217 | } |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1218 | |
Chris Lattner | f9aa3cb | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 1219 | // Keep track of the various subcomponents we see. |
| 1220 | llvm::SmallVector<Action::OffsetOfComponent, 4> Comps; |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1221 | |
Chris Lattner | f9aa3cb | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 1222 | Comps.push_back(Action::OffsetOfComponent()); |
| 1223 | Comps.back().isBrackets = false; |
| 1224 | Comps.back().U.IdentInfo = Tok.getIdentifierInfo(); |
| 1225 | Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1226 | |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 1227 | // FIXME: This loop leaks the index expressions on error. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1228 | while (1) { |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1229 | if (Tok.is(tok::period)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1230 | // offsetof-member-designator: offsetof-member-designator '.' identifier |
Chris Lattner | f9aa3cb | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 1231 | Comps.push_back(Action::OffsetOfComponent()); |
| 1232 | Comps.back().isBrackets = false; |
| 1233 | Comps.back().LocStart = ConsumeToken(); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1234 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1235 | if (Tok.isNot(tok::identifier)) { |
Chris Lattner | f9aa3cb | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 1236 | Diag(Tok, diag::err_expected_ident); |
| 1237 | SkipUntil(tok::r_paren); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1238 | return ExprError(); |
Chris Lattner | f9aa3cb | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 1239 | } |
| 1240 | Comps.back().U.IdentInfo = Tok.getIdentifierInfo(); |
| 1241 | Comps.back().LocEnd = ConsumeToken(); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1242 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1243 | } else if (Tok.is(tok::l_square)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1244 | // offsetof-member-designator: offsetof-member-design '[' expression ']' |
Chris Lattner | f9aa3cb | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 1245 | Comps.push_back(Action::OffsetOfComponent()); |
| 1246 | Comps.back().isBrackets = true; |
| 1247 | Comps.back().LocStart = ConsumeBracket(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1248 | Res = ParseExpression(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1249 | if (Res.isInvalid()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1250 | SkipUntil(tok::r_paren); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1251 | return move(Res); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1252 | } |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 1253 | Comps.back().U.E = Res.release(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1254 | |
Chris Lattner | f9aa3cb | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 1255 | Comps.back().LocEnd = |
| 1256 | MatchRHSPunctuation(tok::r_square, Comps.back().LocStart); |
Eli Friedman | 309fe0d | 2009-06-27 20:38:33 +0000 | [diff] [blame] | 1257 | } else { |
| 1258 | if (Tok.isNot(tok::r_paren)) { |
| 1259 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 1260 | Res = ExprError(); |
Eli Friedman | 309fe0d | 2009-06-27 20:38:33 +0000 | [diff] [blame] | 1261 | } else if (Ty.isInvalid()) { |
| 1262 | Res = ExprError(); |
| 1263 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1264 | Res = Actions.ActOnBuiltinOffsetOf(CurScope, StartLoc, TypeLoc, |
| 1265 | Ty.get(), &Comps[0], |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 1266 | Comps.size(), ConsumeParen()); |
Eli Friedman | 309fe0d | 2009-06-27 20:38:33 +0000 | [diff] [blame] | 1267 | } |
Chris Lattner | 6eb2109 | 2007-08-30 15:52:49 +0000 | [diff] [blame] | 1268 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1269 | } |
| 1270 | } |
| 1271 | break; |
Chris Lattner | f9aa3cb | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 1272 | } |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 1273 | case tok::kw___builtin_choose_expr: { |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 1274 | OwningExprResult Cond(ParseAssignmentExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1275 | if (Cond.isInvalid()) { |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 1276 | SkipUntil(tok::r_paren); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1277 | return move(Cond); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 1278 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1279 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1280 | return ExprError(); |
| 1281 | |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 1282 | OwningExprResult Expr1(ParseAssignmentExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1283 | if (Expr1.isInvalid()) { |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 1284 | SkipUntil(tok::r_paren); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1285 | return move(Expr1); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1286 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1287 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1288 | return ExprError(); |
| 1289 | |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 1290 | OwningExprResult Expr2(ParseAssignmentExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1291 | if (Expr2.isInvalid()) { |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 1292 | SkipUntil(tok::r_paren); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1293 | return move(Expr2); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1294 | } |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1295 | if (Tok.isNot(tok::r_paren)) { |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 1296 | Diag(Tok, diag::err_expected_rparen); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1297 | return ExprError(); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 1298 | } |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1299 | Res = Actions.ActOnChooseExpr(StartLoc, move(Cond), move(Expr1), |
| 1300 | move(Expr2), ConsumeParen()); |
Chris Lattner | 6eb2109 | 2007-08-30 15:52:49 +0000 | [diff] [blame] | 1301 | break; |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 1302 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1303 | case tok::kw___builtin_types_compatible_p: |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 1304 | TypeResult Ty1 = ParseTypeName(); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1305 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1306 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1307 | return ExprError(); |
| 1308 | |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 1309 | TypeResult Ty2 = ParseTypeName(); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1310 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1311 | if (Tok.isNot(tok::r_paren)) { |
Steve Naroff | 363bcff | 2007-08-01 23:45:51 +0000 | [diff] [blame] | 1312 | Diag(Tok, diag::err_expected_rparen); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1313 | return ExprError(); |
Steve Naroff | 363bcff | 2007-08-01 23:45:51 +0000 | [diff] [blame] | 1314 | } |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 1315 | |
| 1316 | if (Ty1.isInvalid() || Ty2.isInvalid()) |
| 1317 | Res = ExprError(); |
| 1318 | else |
| 1319 | Res = Actions.ActOnTypesCompatibleExpr(StartLoc, Ty1.get(), Ty2.get(), |
| 1320 | ConsumeParen()); |
Chris Lattner | 6eb2109 | 2007-08-30 15:52:49 +0000 | [diff] [blame] | 1321 | break; |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 1322 | } |
| 1323 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1324 | // These can be followed by postfix-expr pieces because they are |
| 1325 | // primary-expressions. |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1326 | return ParsePostfixExpressionSuffix(move(Res)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1327 | } |
| 1328 | |
| 1329 | /// ParseParenExpression - This parses the unit that starts with a '(' token, |
| 1330 | /// based on what is allowed by ExprType. The actual thing parsed is returned |
Argyrios Kyrtzidis | 0350ca5 | 2009-05-22 10:23:40 +0000 | [diff] [blame] | 1331 | /// in ExprType. If stopIfCastExpr is true, it will only return the parsed type, |
| 1332 | /// not the parsed cast-expression. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1333 | /// |
| 1334 | /// primary-expression: [C99 6.5.1] |
| 1335 | /// '(' expression ')' |
| 1336 | /// [GNU] '(' compound-statement ')' (if !ParenExprOnly) |
| 1337 | /// postfix-expression: [C99 6.5.2] |
| 1338 | /// '(' type-name ')' '{' initializer-list '}' |
| 1339 | /// '(' type-name ')' '{' initializer-list ',' '}' |
| 1340 | /// cast-expression: [C99 6.5.4] |
| 1341 | /// '(' type-name ')' cast-expression |
| 1342 | /// |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1343 | Parser::OwningExprResult |
Argyrios Kyrtzidis | 0350ca5 | 2009-05-22 10:23:40 +0000 | [diff] [blame] | 1344 | Parser::ParseParenExpression(ParenParseOption &ExprType, bool stopIfCastExpr, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1345 | bool parseAsExprList, TypeTy *&CastTy, |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1346 | SourceLocation &RParenLoc) { |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1347 | assert(Tok.is(tok::l_paren) && "Not a paren expr!"); |
Douglas Gregor | f02da89 | 2009-02-09 21:04:56 +0000 | [diff] [blame] | 1348 | GreaterThanIsOperatorScope G(GreaterThanIsOperator, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1349 | SourceLocation OpenLoc = ConsumeParen(); |
Sebastian Redl | 15faa7f | 2008-12-09 20:22:58 +0000 | [diff] [blame] | 1350 | OwningExprResult Result(Actions, true); |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1351 | bool isAmbiguousTypeId; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1352 | CastTy = 0; |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1353 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1354 | if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1355 | Diag(Tok, diag::ext_gnu_statement_expr); |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 1356 | OwningStmtResult Stmt(ParseCompoundStatement(true)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1357 | ExprType = CompoundStmt; |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1358 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 1359 | // If the substmt parsed correctly, build the AST node. |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1360 | if (!Stmt.isInvalid() && Tok.is(tok::r_paren)) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1361 | Result = Actions.ActOnStmtExpr(OpenLoc, move(Stmt), Tok.getLocation()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1362 | |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1363 | } else if (ExprType >= CompoundLiteral && |
| 1364 | isTypeIdInParens(isAmbiguousTypeId)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1365 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1366 | // Otherwise, this is a compound literal expression or cast expression. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1367 | |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1368 | // In C++, if the type-id is ambiguous we disambiguate based on context. |
| 1369 | // If stopIfCastExpr is true the context is a typeof/sizeof/alignof |
| 1370 | // in which case we should treat it as type-id. |
| 1371 | // if stopIfCastExpr is false, we need to determine the context past the |
| 1372 | // parens, so we defer to ParseCXXAmbiguousParenExpression for that. |
| 1373 | if (isAmbiguousTypeId && !stopIfCastExpr) |
| 1374 | return ParseCXXAmbiguousParenExpression(ExprType, CastTy, |
| 1375 | OpenLoc, RParenLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1376 | |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 1377 | TypeResult Ty = ParseTypeName(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1378 | |
| 1379 | // Match the ')'. |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1380 | if (Tok.is(tok::r_paren)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1381 | RParenLoc = ConsumeParen(); |
| 1382 | else |
| 1383 | MatchRHSPunctuation(tok::r_paren, OpenLoc); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1384 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1385 | if (Tok.is(tok::l_brace)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1386 | ExprType = CompoundLiteral; |
Argyrios Kyrtzidis | d974a7b | 2009-05-22 10:24:05 +0000 | [diff] [blame] | 1387 | return ParseCompoundLiteralExpression(Ty.get(), OpenLoc, RParenLoc); |
Chris Lattner | 42ece64 | 2008-12-12 06:00:12 +0000 | [diff] [blame] | 1388 | } |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 1389 | |
Chris Lattner | 42ece64 | 2008-12-12 06:00:12 +0000 | [diff] [blame] | 1390 | if (ExprType == CastExpr) { |
Argyrios Kyrtzidis | 0350ca5 | 2009-05-22 10:23:40 +0000 | [diff] [blame] | 1391 | // We parsed '(' type-name ')' and the thing after it wasn't a '{'. |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 1392 | |
| 1393 | if (Ty.isInvalid()) |
| 1394 | return ExprError(); |
| 1395 | |
| 1396 | CastTy = Ty.get(); |
Argyrios Kyrtzidis | 0350ca5 | 2009-05-22 10:23:40 +0000 | [diff] [blame] | 1397 | |
| 1398 | if (stopIfCastExpr) { |
| 1399 | // Note that this doesn't parse the subsequent cast-expression, it just |
| 1400 | // returns the parsed type to the callee. |
| 1401 | return OwningExprResult(Actions); |
| 1402 | } |
| 1403 | |
| 1404 | // Parse the cast-expression that follows it next. |
| 1405 | // TODO: For cast expression with CastTy. |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1406 | Result = ParseCastExpression(false, false, true); |
Argyrios Kyrtzidis | 0350ca5 | 2009-05-22 10:23:40 +0000 | [diff] [blame] | 1407 | if (!Result.isInvalid()) |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1408 | Result = Actions.ActOnCastExpr(CurScope, OpenLoc, CastTy, RParenLoc, |
| 1409 | move(Result)); |
Argyrios Kyrtzidis | 0350ca5 | 2009-05-22 10:23:40 +0000 | [diff] [blame] | 1410 | return move(Result); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1411 | } |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 1412 | |
Chris Lattner | 42ece64 | 2008-12-12 06:00:12 +0000 | [diff] [blame] | 1413 | Diag(Tok, diag::err_expected_lbrace_in_compound_literal); |
| 1414 | return ExprError(); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1415 | } else if (parseAsExprList) { |
| 1416 | // Parse the expression-list. |
| 1417 | ExprVector ArgExprs(Actions); |
| 1418 | CommaLocsTy CommaLocs; |
| 1419 | |
| 1420 | if (!ParseExpressionList(ArgExprs, CommaLocs)) { |
| 1421 | ExprType = SimpleExpr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1422 | Result = Actions.ActOnParenListExpr(OpenLoc, Tok.getLocation(), |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1423 | move_arg(ArgExprs)); |
| 1424 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1425 | } else { |
| 1426 | Result = ParseExpression(); |
| 1427 | ExprType = SimpleExpr; |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1428 | if (!Result.isInvalid() && Tok.is(tok::r_paren)) |
Sebastian Redl | 76ad2e8 | 2009-02-05 15:02:23 +0000 | [diff] [blame] | 1429 | Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), move(Result)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1430 | } |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1431 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1432 | // Match the ')'. |
Chris Lattner | 42ece64 | 2008-12-12 06:00:12 +0000 | [diff] [blame] | 1433 | if (Result.isInvalid()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1434 | SkipUntil(tok::r_paren); |
Chris Lattner | 42ece64 | 2008-12-12 06:00:12 +0000 | [diff] [blame] | 1435 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1436 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1437 | |
Chris Lattner | 42ece64 | 2008-12-12 06:00:12 +0000 | [diff] [blame] | 1438 | if (Tok.is(tok::r_paren)) |
| 1439 | RParenLoc = ConsumeParen(); |
| 1440 | else |
| 1441 | MatchRHSPunctuation(tok::r_paren, OpenLoc); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1442 | |
| 1443 | return move(Result); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1444 | } |
| 1445 | |
Argyrios Kyrtzidis | d974a7b | 2009-05-22 10:24:05 +0000 | [diff] [blame] | 1446 | /// ParseCompoundLiteralExpression - We have parsed the parenthesized type-name |
| 1447 | /// and we are at the left brace. |
| 1448 | /// |
| 1449 | /// postfix-expression: [C99 6.5.2] |
| 1450 | /// '(' type-name ')' '{' initializer-list '}' |
| 1451 | /// '(' type-name ')' '{' initializer-list ',' '}' |
| 1452 | /// |
| 1453 | Parser::OwningExprResult |
| 1454 | Parser::ParseCompoundLiteralExpression(TypeTy *Ty, |
| 1455 | SourceLocation LParenLoc, |
| 1456 | SourceLocation RParenLoc) { |
| 1457 | assert(Tok.is(tok::l_brace) && "Not a compound literal!"); |
| 1458 | if (!getLang().C99) // Compound literals don't exist in C90. |
| 1459 | Diag(LParenLoc, diag::ext_c99_compound_literal); |
| 1460 | OwningExprResult Result = ParseInitializer(); |
| 1461 | if (!Result.isInvalid() && Ty) |
| 1462 | return Actions.ActOnCompoundLiteral(LParenLoc, Ty, RParenLoc, move(Result)); |
| 1463 | return move(Result); |
| 1464 | } |
| 1465 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1466 | /// ParseStringLiteralExpression - This handles the various token types that |
| 1467 | /// form string literals, and also handles string concatenation [C99 5.1.1.2, |
| 1468 | /// translation phase #6]. |
| 1469 | /// |
| 1470 | /// primary-expression: [C99 6.5.1] |
| 1471 | /// string-literal |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 1472 | Parser::OwningExprResult Parser::ParseStringLiteralExpression() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1473 | assert(isTokenStringLiteral() && "Not a string literal!"); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 1474 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1475 | // String concat. Note that keywords like __func__ and __FUNCTION__ are not |
| 1476 | // considered to be strings for concatenation purposes. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1477 | llvm::SmallVector<Token, 4> StringToks; |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 1478 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1479 | do { |
| 1480 | StringToks.push_back(Tok); |
| 1481 | ConsumeStringToken(); |
| 1482 | } while (isTokenStringLiteral()); |
| 1483 | |
| 1484 | // Pass the set of string tokens, ready for concatenation, to the actions. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1485 | return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1486 | } |
Argyrios Kyrtzidis | 0cd5b42 | 2008-08-16 20:03:01 +0000 | [diff] [blame] | 1487 | |
| 1488 | /// ParseExpressionList - Used for C/C++ (argument-)expression-list. |
| 1489 | /// |
| 1490 | /// argument-expression-list: |
| 1491 | /// assignment-expression |
| 1492 | /// argument-expression-list , assignment-expression |
| 1493 | /// |
| 1494 | /// [C++] expression-list: |
| 1495 | /// [C++] assignment-expression |
| 1496 | /// [C++] expression-list , assignment-expression |
| 1497 | /// |
| 1498 | bool Parser::ParseExpressionList(ExprListTy &Exprs, CommaLocsTy &CommaLocs) { |
| 1499 | while (1) { |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 1500 | OwningExprResult Expr(ParseAssignmentExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1501 | if (Expr.isInvalid()) |
Argyrios Kyrtzidis | 0cd5b42 | 2008-08-16 20:03:01 +0000 | [diff] [blame] | 1502 | return true; |
Argyrios Kyrtzidis | 4fdc1ca | 2008-08-18 22:49:40 +0000 | [diff] [blame] | 1503 | |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 1504 | Exprs.push_back(Expr.release()); |
Argyrios Kyrtzidis | 0cd5b42 | 2008-08-16 20:03:01 +0000 | [diff] [blame] | 1505 | |
| 1506 | if (Tok.isNot(tok::comma)) |
| 1507 | return false; |
| 1508 | // Move to the next argument, remember where the comma was. |
| 1509 | CommaLocs.push_back(ConsumeToken()); |
| 1510 | } |
| 1511 | } |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1512 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 1513 | /// ParseBlockId - Parse a block-id, which roughly looks like int (int x). |
| 1514 | /// |
| 1515 | /// [clang] block-id: |
| 1516 | /// [clang] specifier-qualifier-list block-declarator |
| 1517 | /// |
| 1518 | void Parser::ParseBlockId() { |
| 1519 | // Parse the specifier-qualifier-list piece. |
| 1520 | DeclSpec DS; |
| 1521 | ParseSpecifierQualifierList(DS); |
| 1522 | |
| 1523 | // Parse the block-declarator. |
| 1524 | Declarator DeclaratorInfo(DS, Declarator::BlockLiteralContext); |
| 1525 | ParseDeclarator(DeclaratorInfo); |
Mike Stump | 19c30c0 | 2009-04-29 19:03:13 +0000 | [diff] [blame] | 1526 | |
Mike Stump | 6c92fa7 | 2009-04-29 21:40:37 +0000 | [diff] [blame] | 1527 | // We do this for: ^ __attribute__((noreturn)) {, as DS has the attributes. |
| 1528 | DeclaratorInfo.AddAttributes(DS.TakeAttributes(), |
| 1529 | SourceLocation()); |
| 1530 | |
Mike Stump | 19c30c0 | 2009-04-29 19:03:13 +0000 | [diff] [blame] | 1531 | if (Tok.is(tok::kw___attribute)) { |
| 1532 | SourceLocation Loc; |
| 1533 | AttributeList *AttrList = ParseAttributes(&Loc); |
| 1534 | DeclaratorInfo.AddAttributes(AttrList, Loc); |
| 1535 | } |
| 1536 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 1537 | // Inform sema that we are starting a block. |
| 1538 | Actions.ActOnBlockArguments(DeclaratorInfo, CurScope); |
| 1539 | } |
| 1540 | |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1541 | /// ParseBlockLiteralExpression - Parse a block literal, which roughly looks |
Steve Naroff | 17dab4f | 2008-09-16 23:11:46 +0000 | [diff] [blame] | 1542 | /// like ^(int x){ return x+1; } |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1543 | /// |
| 1544 | /// block-literal: |
| 1545 | /// [clang] '^' block-args[opt] compound-statement |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 1546 | /// [clang] '^' block-id compound-statement |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1547 | /// [clang] block-args: |
| 1548 | /// [clang] '(' parameter-list ')' |
| 1549 | /// |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1550 | Parser::OwningExprResult Parser::ParseBlockLiteralExpression() { |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1551 | assert(Tok.is(tok::caret) && "block literal starts with ^"); |
| 1552 | SourceLocation CaretLoc = ConsumeToken(); |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1553 | |
Chris Lattner | 6b91f00 | 2009-03-05 07:32:12 +0000 | [diff] [blame] | 1554 | PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), CaretLoc, |
| 1555 | "block literal parsing"); |
| 1556 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1557 | // Enter a scope to hold everything within the block. This includes the |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1558 | // argument decls, decls within the compound expression, etc. This also |
| 1559 | // allows determining whether a variable reference inside the block is |
| 1560 | // within or outside of the block. |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1561 | ParseScope BlockScope(this, Scope::BlockScope | Scope::FnScope | |
| 1562 | Scope::BreakScope | Scope::ContinueScope | |
| 1563 | Scope::DeclScope); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 1564 | |
| 1565 | // Inform sema that we are starting a block. |
| 1566 | Actions.ActOnBlockStart(CaretLoc, CurScope); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1567 | |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1568 | // Parse the return type if present. |
| 1569 | DeclSpec DS; |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 1570 | Declarator ParamInfo(DS, Declarator::BlockLiteralContext); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1571 | // FIXME: Since the return type isn't actually parsed, it can't be used to |
| 1572 | // fill ParamInfo with an initial valid range, so do it manually. |
| 1573 | ParamInfo.SetSourceRange(SourceRange(Tok.getLocation(), Tok.getLocation())); |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1574 | |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1575 | // If this block has arguments, parse them. There is no ambiguity here with |
| 1576 | // the expression case, because the expression case requires a parameter list. |
| 1577 | if (Tok.is(tok::l_paren)) { |
| 1578 | ParseParenDeclarator(ParamInfo); |
| 1579 | // Parse the pieces after the identifier as if we had "int(...)". |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1580 | // SetIdentifier sets the source range end, but in this case we're past |
| 1581 | // that location. |
| 1582 | SourceLocation Tmp = ParamInfo.getSourceRange().getEnd(); |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1583 | ParamInfo.SetIdentifier(0, CaretLoc); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1584 | ParamInfo.SetRangeEnd(Tmp); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 1585 | if (ParamInfo.isInvalidType()) { |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 1586 | // If there was an error parsing the arguments, they may have |
| 1587 | // tried to use ^(x+y) which requires an argument list. Just |
| 1588 | // skip the whole block literal. |
Chris Lattner | 4f2aac3 | 2009-04-18 20:05:34 +0000 | [diff] [blame] | 1589 | Actions.ActOnBlockError(CaretLoc, CurScope); |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1590 | return ExprError(); |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1591 | } |
Mike Stump | 19c30c0 | 2009-04-29 19:03:13 +0000 | [diff] [blame] | 1592 | |
| 1593 | if (Tok.is(tok::kw___attribute)) { |
| 1594 | SourceLocation Loc; |
| 1595 | AttributeList *AttrList = ParseAttributes(&Loc); |
| 1596 | ParamInfo.AddAttributes(AttrList, Loc); |
| 1597 | } |
| 1598 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 1599 | // Inform sema that we are starting a block. |
| 1600 | Actions.ActOnBlockArguments(ParamInfo, CurScope); |
Mike Stump | aa771a8 | 2009-04-14 18:24:37 +0000 | [diff] [blame] | 1601 | } else if (!Tok.is(tok::l_brace)) { |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 1602 | ParseBlockId(); |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1603 | } else { |
| 1604 | // Otherwise, pretend we saw (void). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1605 | ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(true, false, |
Douglas Gregor | 965acbb | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 1606 | SourceLocation(), |
Sebastian Redl | 7dc8134 | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 1607 | 0, 0, 0, |
Sebastian Redl | 3cc9726 | 2009-05-31 11:47:27 +0000 | [diff] [blame] | 1608 | false, SourceLocation(), |
| 1609 | false, 0, 0, 0, |
Argyrios Kyrtzidis | 82bf010 | 2009-08-19 23:14:54 +0000 | [diff] [blame] | 1610 | CaretLoc, CaretLoc, |
| 1611 | ParamInfo), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1612 | CaretLoc); |
Mike Stump | 19c30c0 | 2009-04-29 19:03:13 +0000 | [diff] [blame] | 1613 | |
| 1614 | if (Tok.is(tok::kw___attribute)) { |
| 1615 | SourceLocation Loc; |
| 1616 | AttributeList *AttrList = ParseAttributes(&Loc); |
| 1617 | ParamInfo.AddAttributes(AttrList, Loc); |
| 1618 | } |
| 1619 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 1620 | // Inform sema that we are starting a block. |
| 1621 | Actions.ActOnBlockArguments(ParamInfo, CurScope); |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1622 | } |
| 1623 | |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1624 | |
Sebastian Redl | 15faa7f | 2008-12-09 20:22:58 +0000 | [diff] [blame] | 1625 | OwningExprResult Result(Actions, true); |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 1626 | if (!Tok.is(tok::l_brace)) { |
Fariborz Jahanian | ff03fbb | 2009-01-14 19:39:53 +0000 | [diff] [blame] | 1627 | // Saw something like: ^expr |
| 1628 | Diag(Tok, diag::err_expected_expression); |
Chris Lattner | 4f2aac3 | 2009-04-18 20:05:34 +0000 | [diff] [blame] | 1629 | Actions.ActOnBlockError(CaretLoc, CurScope); |
Fariborz Jahanian | ff03fbb | 2009-01-14 19:39:53 +0000 | [diff] [blame] | 1630 | return ExprError(); |
| 1631 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1632 | |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 1633 | OwningStmtResult Stmt(ParseCompoundStatementBody()); |
| 1634 | if (!Stmt.isInvalid()) |
| 1635 | Result = Actions.ActOnBlockStmtExpr(CaretLoc, move(Stmt), CurScope); |
| 1636 | else |
| 1637 | Actions.ActOnBlockError(CaretLoc, CurScope); |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1638 | return move(Result); |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1639 | } |