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