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