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 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 572 | // Consume the identifier so that we can see if it is followed by a '('. |
| 573 | // Function designators are allowed to be undeclared (C99 6.5.1p2), so we |
| 574 | // need to know whether or not this identifier is a function designator or |
| 575 | // not. |
| 576 | IdentifierInfo &II = *Tok.getIdentifierInfo(); |
| 577 | SourceLocation L = ConsumeToken(); |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 578 | Res = Actions.ActOnIdentifierExpr(CurScope, L, II, Tok.is(tok::l_paren)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 579 | // These can be followed by postfix-expr pieces. |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 580 | return ParsePostfixExpressionSuffix(move(Res)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 581 | } |
| 582 | case tok::char_constant: // constant: character-constant |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 583 | Res = Actions.ActOnCharacterConstant(Tok); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 584 | ConsumeToken(); |
| 585 | // These can be followed by postfix-expr pieces. |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 586 | return ParsePostfixExpressionSuffix(move(Res)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 587 | case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2] |
| 588 | case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU] |
| 589 | case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU] |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 590 | Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 591 | ConsumeToken(); |
| 592 | // These can be followed by postfix-expr pieces. |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 593 | return ParsePostfixExpressionSuffix(move(Res)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 594 | case tok::string_literal: // primary-expression: string-literal |
| 595 | case tok::wide_string_literal: |
| 596 | Res = ParseStringLiteralExpression(); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 597 | if (Res.isInvalid()) return move(Res); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 598 | // This can be followed by postfix-expr pieces (e.g. "foo"[1]). |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 599 | return ParsePostfixExpressionSuffix(move(Res)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 600 | case tok::kw___builtin_va_arg: |
| 601 | case tok::kw___builtin_offsetof: |
| 602 | case tok::kw___builtin_choose_expr: |
| 603 | case tok::kw___builtin_types_compatible_p: |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 604 | return ParseBuiltinPrimaryExpression(); |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 605 | case tok::kw___null: |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 606 | return Owned(Actions.ActOnGNUNullExpr(ConsumeToken())); |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 607 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 608 | case tok::plusplus: // unary-expression: '++' unary-expression |
| 609 | case tok::minusminus: { // unary-expression: '--' unary-expression |
| 610 | SourceLocation SavedLoc = ConsumeToken(); |
| 611 | Res = ParseCastExpression(true); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 612 | if (!Res.isInvalid()) |
Sebastian Redl | 76ad2e8 | 2009-02-05 15:02:23 +0000 | [diff] [blame] | 613 | Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res)); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 614 | return move(Res); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 615 | } |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 616 | case tok::amp: { // unary-expression: '&' cast-expression |
| 617 | // Special treatment because of member pointers |
| 618 | SourceLocation SavedLoc = ConsumeToken(); |
| 619 | Res = ParseCastExpression(false, true); |
| 620 | if (!Res.isInvalid()) |
Sebastian Redl | 76ad2e8 | 2009-02-05 15:02:23 +0000 | [diff] [blame] | 621 | Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res)); |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 622 | return move(Res); |
| 623 | } |
| 624 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 625 | case tok::star: // unary-expression: '*' cast-expression |
| 626 | case tok::plus: // unary-expression: '+' cast-expression |
| 627 | case tok::minus: // unary-expression: '-' cast-expression |
| 628 | case tok::tilde: // unary-expression: '~' cast-expression |
| 629 | case tok::exclaim: // unary-expression: '!' cast-expression |
| 630 | case tok::kw___real: // unary-expression: '__real' cast-expression [GNU] |
Chris Lattner | 3508084 | 2008-02-02 20:20:10 +0000 | [diff] [blame] | 631 | case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 632 | SourceLocation SavedLoc = ConsumeToken(); |
| 633 | Res = ParseCastExpression(false); |
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); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 637 | } |
| 638 | |
Chris Lattner | 3508084 | 2008-02-02 20:20:10 +0000 | [diff] [blame] | 639 | case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU] |
| 640 | // __extension__ silences extension warnings in the subexpression. |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 641 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
Chris Lattner | 3508084 | 2008-02-02 20:20:10 +0000 | [diff] [blame] | 642 | SourceLocation SavedLoc = ConsumeToken(); |
| 643 | Res = ParseCastExpression(false); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 644 | if (!Res.isInvalid()) |
Sebastian Redl | 76ad2e8 | 2009-02-05 15:02:23 +0000 | [diff] [blame] | 645 | Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res)); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 646 | return move(Res); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 647 | } |
| 648 | case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression |
| 649 | // unary-expression: 'sizeof' '(' type-name ')' |
Douglas Gregor | 85bb3da | 2008-11-06 15:17:27 +0000 | [diff] [blame] | 650 | case tok::kw_alignof: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 651 | case tok::kw___alignof: // unary-expression: '__alignof' unary-expression |
| 652 | // unary-expression: '__alignof' '(' type-name ')' |
Douglas Gregor | 85bb3da | 2008-11-06 15:17:27 +0000 | [diff] [blame] | 653 | // unary-expression: 'alignof' '(' type-id ')' |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 654 | return ParseSizeofAlignofExpression(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 655 | case tok::ampamp: { // unary-expression: '&&' identifier |
| 656 | SourceLocation AmpAmpLoc = ConsumeToken(); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 657 | if (Tok.isNot(tok::identifier)) |
| 658 | return ExprError(Diag(Tok, diag::err_expected_ident)); |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 659 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 660 | Diag(AmpAmpLoc, diag::ext_gnu_address_of_label); |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 661 | Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 662 | Tok.getIdentifierInfo()); |
| 663 | ConsumeToken(); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 664 | return move(Res); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 665 | } |
| 666 | case tok::kw_const_cast: |
| 667 | case tok::kw_dynamic_cast: |
| 668 | case tok::kw_reinterpret_cast: |
| 669 | case tok::kw_static_cast: |
Argyrios Kyrtzidis | b348b81 | 2008-08-16 19:45:32 +0000 | [diff] [blame] | 670 | Res = ParseCXXCasts(); |
| 671 | // These can be followed by postfix-expr pieces. |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 672 | return ParsePostfixExpressionSuffix(move(Res)); |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 673 | case tok::kw_typeid: |
| 674 | Res = ParseCXXTypeid(); |
| 675 | // This can be followed by postfix-expr pieces. |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 676 | return ParsePostfixExpressionSuffix(move(Res)); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 677 | case tok::kw_this: |
Argyrios Kyrtzidis | 289d773 | 2008-08-16 19:34:46 +0000 | [diff] [blame] | 678 | Res = ParseCXXThis(); |
| 679 | // This can be followed by postfix-expr pieces. |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 680 | return ParsePostfixExpressionSuffix(move(Res)); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 681 | |
| 682 | case tok::kw_char: |
| 683 | case tok::kw_wchar_t: |
| 684 | case tok::kw_bool: |
| 685 | case tok::kw_short: |
| 686 | case tok::kw_int: |
| 687 | case tok::kw_long: |
| 688 | case tok::kw_signed: |
| 689 | case tok::kw_unsigned: |
| 690 | case tok::kw_float: |
| 691 | case tok::kw_double: |
| 692 | case tok::kw_void: |
Chris Lattner | 2dcaab3 | 2009-01-04 22:28:21 +0000 | [diff] [blame] | 693 | case tok::kw_typeof: |
Chris Lattner | b31757b | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 694 | case tok::annot_typename: { |
Chris Lattner | 2dcaab3 | 2009-01-04 22:28:21 +0000 | [diff] [blame] | 695 | if (!getLang().CPlusPlus) { |
| 696 | Diag(Tok, diag::err_expected_expression); |
| 697 | return ExprError(); |
| 698 | } |
| 699 | |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 700 | // postfix-expression: simple-type-specifier '(' expression-list[opt] ')' |
| 701 | // |
| 702 | DeclSpec DS; |
| 703 | ParseCXXSimpleTypeSpecifier(DS); |
| 704 | if (Tok.isNot(tok::l_paren)) |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 705 | return ExprError(Diag(Tok, diag::err_expected_lparen_after_type) |
| 706 | << DS.getSourceRange()); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 707 | |
| 708 | Res = ParseCXXTypeConstructExpression(DS); |
| 709 | // This can be followed by postfix-expr pieces. |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 710 | return ParsePostfixExpressionSuffix(move(Res)); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 713 | case tok::annot_cxxscope: // [C++] id-expression: qualified-id |
| 714 | case tok::kw_operator: // [C++] id-expression: operator/conversion-function-id |
| 715 | // template-id |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 716 | Res = ParseCXXIdExpression(isAddressOfOperand); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 717 | return ParsePostfixExpressionSuffix(move(Res)); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 718 | |
Chris Lattner | 74ba410 | 2009-01-04 22:52:14 +0000 | [diff] [blame] | 719 | case tok::coloncolon: { |
Chris Lattner | 5b45473 | 2009-01-05 03:55:46 +0000 | [diff] [blame] | 720 | // ::foo::bar -> global qualified name etc. If TryAnnotateTypeOrScopeToken |
| 721 | // annotates the token, tail recurse. |
| 722 | if (TryAnnotateTypeOrScopeToken()) |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 723 | return ParseCastExpression(isUnaryExpression, isAddressOfOperand); |
| 724 | |
Chris Lattner | 74ba410 | 2009-01-04 22:52:14 +0000 | [diff] [blame] | 725 | // ::new -> [C++] new-expression |
| 726 | // ::delete -> [C++] delete-expression |
Chris Lattner | 5b45473 | 2009-01-05 03:55:46 +0000 | [diff] [blame] | 727 | SourceLocation CCLoc = ConsumeToken(); |
Chris Lattner | 59232d3 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 728 | if (Tok.is(tok::kw_new)) |
Chris Lattner | 5b45473 | 2009-01-05 03:55:46 +0000 | [diff] [blame] | 729 | return ParseCXXNewExpression(true, CCLoc); |
Chris Lattner | 74ba410 | 2009-01-04 22:52:14 +0000 | [diff] [blame] | 730 | if (Tok.is(tok::kw_delete)) |
Chris Lattner | 5b45473 | 2009-01-05 03:55:46 +0000 | [diff] [blame] | 731 | return ParseCXXDeleteExpression(true, CCLoc); |
| 732 | |
Chris Lattner | a7bc7c8 | 2009-01-04 23:23:14 +0000 | [diff] [blame] | 733 | // 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] | 734 | Diag(CCLoc, diag::err_expected_expression); |
Chris Lattner | a7bc7c8 | 2009-01-04 23:23:14 +0000 | [diff] [blame] | 735 | return ExprError(); |
Chris Lattner | 59232d3 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 736 | } |
Sebastian Redl | fb4ccd7 | 2008-12-02 16:35:44 +0000 | [diff] [blame] | 737 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 738 | case tok::kw_new: // [C++] new-expression |
Chris Lattner | 59232d3 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 739 | return ParseCXXNewExpression(false, Tok.getLocation()); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 740 | |
| 741 | case tok::kw_delete: // [C++] delete-expression |
Chris Lattner | 59232d3 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 742 | return ParseCXXDeleteExpression(false, Tok.getLocation()); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 743 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 744 | case tok::kw___is_pod: // [GNU] unary-type-trait |
| 745 | case tok::kw___is_class: |
| 746 | case tok::kw___is_enum: |
| 747 | case tok::kw___is_union: |
| 748 | case tok::kw___is_polymorphic: |
| 749 | return ParseUnaryTypeTrait(); |
| 750 | |
Chris Lattner | c97c204 | 2007-10-03 22:03:06 +0000 | [diff] [blame] | 751 | case tok::at: { |
| 752 | SourceLocation AtLoc = ConsumeToken(); |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 753 | return ParseObjCAtExpression(AtLoc); |
Chris Lattner | c97c204 | 2007-10-03 22:03:06 +0000 | [diff] [blame] | 754 | } |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 755 | case tok::caret: |
| 756 | if (getLang().Blocks) |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 757 | return ParsePostfixExpressionSuffix(ParseBlockLiteralExpression()); |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 758 | Diag(Tok, diag::err_expected_expression); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 759 | return ExprError(); |
Chris Lattner | fdb548e | 2008-12-12 19:20:14 +0000 | [diff] [blame] | 760 | case tok::l_square: |
| 761 | // These can be followed by postfix-expr pieces. |
| 762 | if (getLang().ObjC1) |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 763 | return ParsePostfixExpressionSuffix(ParseObjCMessageExpression()); |
Chris Lattner | 2dcaab3 | 2009-01-04 22:28:21 +0000 | [diff] [blame] | 764 | // FALL THROUGH. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 765 | default: |
| 766 | Diag(Tok, diag::err_expected_expression); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 767 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 768 | } |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 769 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 770 | // unreachable. |
| 771 | abort(); |
| 772 | } |
| 773 | |
| 774 | /// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression |
| 775 | /// is parsed, this method parses any suffixes that apply. |
| 776 | /// |
| 777 | /// postfix-expression: [C99 6.5.2] |
| 778 | /// primary-expression |
| 779 | /// postfix-expression '[' expression ']' |
| 780 | /// postfix-expression '(' argument-expression-list[opt] ')' |
| 781 | /// postfix-expression '.' identifier |
| 782 | /// postfix-expression '->' identifier |
| 783 | /// postfix-expression '++' |
| 784 | /// postfix-expression '--' |
| 785 | /// '(' type-name ')' '{' initializer-list '}' |
| 786 | /// '(' type-name ')' '{' initializer-list ',' '}' |
| 787 | /// |
| 788 | /// argument-expression-list: [C99 6.5.2] |
| 789 | /// argument-expression |
| 790 | /// argument-expression-list ',' assignment-expression |
| 791 | /// |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 792 | Parser::OwningExprResult |
| 793 | Parser::ParsePostfixExpressionSuffix(OwningExprResult LHS) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 794 | // Now that the primary-expression piece of the postfix-expression has been |
| 795 | // parsed, see if there are any postfix-expression pieces here. |
| 796 | SourceLocation Loc; |
| 797 | while (1) { |
| 798 | switch (Tok.getKind()) { |
| 799 | default: // Not a postfix-expression suffix. |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 800 | return move(LHS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 801 | case tok::l_square: { // postfix-expression: p-e '[' expression ']' |
| 802 | Loc = ConsumeBracket(); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 803 | OwningExprResult Idx(ParseExpression()); |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 804 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 805 | SourceLocation RLoc = Tok.getLocation(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 806 | |
| 807 | if (!LHS.isInvalid() && !Idx.isInvalid() && Tok.is(tok::r_square)) { |
Sebastian Redl | 76ad2e8 | 2009-02-05 15:02:23 +0000 | [diff] [blame] | 808 | LHS = Actions.ActOnArraySubscriptExpr(CurScope, move(LHS), Loc, |
| 809 | move(Idx), RLoc); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 810 | } else |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 811 | LHS = ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 812 | |
| 813 | // Match the ']'. |
| 814 | MatchRHSPunctuation(tok::r_square, Loc); |
| 815 | break; |
| 816 | } |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 817 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 818 | case tok::l_paren: { // p-e: p-e '(' argument-expression-list[opt] ')' |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 819 | ExprVector ArgExprs(Actions); |
Argyrios Kyrtzidis | 0cd5b42 | 2008-08-16 20:03:01 +0000 | [diff] [blame] | 820 | CommaLocsTy CommaLocs; |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 821 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 822 | Loc = ConsumeParen(); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 823 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 824 | if (Tok.isNot(tok::r_paren)) { |
Argyrios Kyrtzidis | 0cd5b42 | 2008-08-16 20:03:01 +0000 | [diff] [blame] | 825 | if (ParseExpressionList(ArgExprs, CommaLocs)) { |
| 826 | SkipUntil(tok::r_paren); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 827 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 828 | } |
| 829 | } |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 830 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 831 | // Match the ')'. |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 832 | if (!LHS.isInvalid() && Tok.is(tok::r_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 833 | assert((ArgExprs.size() == 0 || ArgExprs.size()-1 == CommaLocs.size())&& |
| 834 | "Unexpected number of commas!"); |
Sebastian Redl | 76ad2e8 | 2009-02-05 15:02:23 +0000 | [diff] [blame] | 835 | LHS = Actions.ActOnCallExpr(CurScope, move(LHS), Loc, |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 836 | move_arg(ArgExprs), &CommaLocs[0], |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 837 | Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 838 | } |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 839 | |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 840 | MatchRHSPunctuation(tok::r_paren, Loc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 841 | break; |
| 842 | } |
| 843 | case tok::arrow: // postfix-expression: p-e '->' identifier |
| 844 | case tok::period: { // postfix-expression: p-e '.' identifier |
| 845 | tok::TokenKind OpKind = Tok.getKind(); |
| 846 | SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token. |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 847 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 848 | if (Tok.isNot(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 849 | Diag(Tok, diag::err_expected_ident); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 850 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 851 | } |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 852 | |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 853 | if (!LHS.isInvalid()) { |
Sebastian Redl | 76ad2e8 | 2009-02-05 15:02:23 +0000 | [diff] [blame] | 854 | LHS = Actions.ActOnMemberReferenceExpr(CurScope, move(LHS), OpLoc, |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 855 | OpKind, Tok.getLocation(), |
Fariborz Jahanian | a6e3ac5 | 2009-03-04 22:30:12 +0000 | [diff] [blame] | 856 | *Tok.getIdentifierInfo(), |
| 857 | ObjCImpDecl); |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 858 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 859 | ConsumeToken(); |
| 860 | break; |
| 861 | } |
| 862 | case tok::plusplus: // postfix-expression: postfix-expression '++' |
| 863 | case tok::minusminus: // postfix-expression: postfix-expression '--' |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 864 | if (!LHS.isInvalid()) { |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 865 | LHS = Actions.ActOnPostfixUnaryOp(CurScope, Tok.getLocation(), |
Sebastian Redl | 76ad2e8 | 2009-02-05 15:02:23 +0000 | [diff] [blame] | 866 | Tok.getKind(), move(LHS)); |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 867 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 868 | ConsumeToken(); |
| 869 | break; |
| 870 | } |
| 871 | } |
| 872 | } |
| 873 | |
| 874 | |
| 875 | /// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression. |
| 876 | /// unary-expression: [C99 6.5.3] |
| 877 | /// 'sizeof' unary-expression |
| 878 | /// 'sizeof' '(' type-name ')' |
| 879 | /// [GNU] '__alignof' unary-expression |
| 880 | /// [GNU] '__alignof' '(' type-name ')' |
Douglas Gregor | 85bb3da | 2008-11-06 15:17:27 +0000 | [diff] [blame] | 881 | /// [C++0x] 'alignof' '(' type-id ')' |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 882 | Parser::OwningExprResult Parser::ParseSizeofAlignofExpression() { |
Douglas Gregor | 85bb3da | 2008-11-06 15:17:27 +0000 | [diff] [blame] | 883 | assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof) |
| 884 | || Tok.is(tok::kw_alignof)) && |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 885 | "Not a sizeof/alignof expression!"); |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 886 | Token OpTok = Tok; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 887 | ConsumeToken(); |
| 888 | |
| 889 | // 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] | 890 | OwningExprResult Operand(Actions); |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 891 | if (Tok.isNot(tok::l_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 892 | Operand = ParseCastExpression(true); |
| 893 | } else { |
| 894 | // If it starts with a '(', we know that it is either a parenthesized |
| 895 | // type-name, or it is a unary-expression that starts with a compound |
| 896 | // literal, or starts with a primary-expression that is a parenthesized |
| 897 | // expression. |
| 898 | ParenParseOption ExprType = CastExpr; |
| 899 | TypeTy *CastTy; |
| 900 | SourceLocation LParenLoc = Tok.getLocation(), RParenLoc; |
| 901 | Operand = ParseParenExpression(ExprType, CastTy, RParenLoc); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 902 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 903 | // If ParseParenExpression parsed a '(typename)' sequence only, the this is |
| 904 | // sizeof/alignof a type. Otherwise, it is sizeof/alignof an expression. |
Chris Lattner | 4c1a2a9 | 2007-11-13 20:50:37 +0000 | [diff] [blame] | 905 | if (ExprType == CastExpr) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 906 | return Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(), |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 907 | OpTok.is(tok::kw_sizeof), |
| 908 | /*isType=*/true, CastTy, |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 909 | SourceRange(LParenLoc, RParenLoc)); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 910 | |
Chris Lattner | 4c1a2a9 | 2007-11-13 20:50:37 +0000 | [diff] [blame] | 911 | // If this is a parenthesized expression, it is the start of a |
| 912 | // unary-expression, but doesn't include any postfix pieces. Parse these |
| 913 | // now if present. |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 914 | Operand = ParsePostfixExpressionSuffix(move(Operand)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 915 | } |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 916 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 917 | // 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] | 918 | if (!Operand.isInvalid()) |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 919 | Operand = Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(), |
| 920 | OpTok.is(tok::kw_sizeof), |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 921 | /*isType=*/false, |
| 922 | Operand.release(), SourceRange()); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 923 | return move(Operand); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | /// ParseBuiltinPrimaryExpression |
| 927 | /// |
| 928 | /// primary-expression: [C99 6.5.1] |
| 929 | /// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')' |
| 930 | /// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')' |
| 931 | /// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ',' |
| 932 | /// assign-expr ')' |
| 933 | /// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')' |
| 934 | /// |
| 935 | /// [GNU] offsetof-member-designator: |
| 936 | /// [GNU] identifier |
| 937 | /// [GNU] offsetof-member-designator '.' identifier |
| 938 | /// [GNU] offsetof-member-designator '[' expression ']' |
| 939 | /// |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 940 | Parser::OwningExprResult Parser::ParseBuiltinPrimaryExpression() { |
Sebastian Redl | 15faa7f | 2008-12-09 20:22:58 +0000 | [diff] [blame] | 941 | OwningExprResult Res(Actions); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 942 | const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo(); |
| 943 | |
| 944 | tok::TokenKind T = Tok.getKind(); |
| 945 | SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier. |
| 946 | |
| 947 | // All of these start with an open paren. |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 948 | if (Tok.isNot(tok::l_paren)) |
| 949 | return ExprError(Diag(Tok, diag::err_expected_lparen_after_id) |
| 950 | << BuiltinII); |
| 951 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 952 | SourceLocation LParenLoc = ConsumeParen(); |
| 953 | // TODO: Build AST. |
| 954 | |
| 955 | switch (T) { |
| 956 | default: assert(0 && "Not a builtin primary expression!"); |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 957 | case tok::kw___builtin_va_arg: { |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 958 | OwningExprResult Expr(ParseAssignmentExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 959 | if (Expr.isInvalid()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 960 | SkipUntil(tok::r_paren); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 961 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 965 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 966 | |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 967 | TypeResult Ty = ParseTypeName(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 968 | |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 969 | if (Tok.isNot(tok::r_paren)) { |
| 970 | Diag(Tok, diag::err_expected_rparen); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 971 | return ExprError(); |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 972 | } |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 973 | if (Ty.isInvalid()) |
| 974 | Res = ExprError(); |
| 975 | else |
| 976 | Res = Actions.ActOnVAArg(StartLoc, Expr.release(), Ty.get(), |
| 977 | ConsumeParen()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 978 | break; |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 979 | } |
Chris Lattner | f9aa3cb | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 980 | case tok::kw___builtin_offsetof: { |
Chris Lattner | 9fddf0a | 2007-08-30 17:08:45 +0000 | [diff] [blame] | 981 | SourceLocation TypeLoc = Tok.getLocation(); |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 982 | TypeResult Ty = ParseTypeName(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 983 | |
| 984 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 985 | return ExprError(); |
| 986 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 987 | // We must have at least one identifier here. |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 988 | if (Tok.isNot(tok::identifier)) { |
Chris Lattner | f9aa3cb | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 989 | Diag(Tok, diag::err_expected_ident); |
| 990 | SkipUntil(tok::r_paren); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 991 | return ExprError(); |
Chris Lattner | f9aa3cb | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 992 | } |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 993 | |
Chris Lattner | f9aa3cb | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 994 | // Keep track of the various subcomponents we see. |
| 995 | llvm::SmallVector<Action::OffsetOfComponent, 4> Comps; |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 996 | |
Chris Lattner | f9aa3cb | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 997 | Comps.push_back(Action::OffsetOfComponent()); |
| 998 | Comps.back().isBrackets = false; |
| 999 | Comps.back().U.IdentInfo = Tok.getIdentifierInfo(); |
| 1000 | Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1001 | |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 1002 | // FIXME: This loop leaks the index expressions on error. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1003 | while (1) { |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1004 | if (Tok.is(tok::period)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1005 | // offsetof-member-designator: offsetof-member-designator '.' identifier |
Chris Lattner | f9aa3cb | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 1006 | Comps.push_back(Action::OffsetOfComponent()); |
| 1007 | Comps.back().isBrackets = false; |
| 1008 | Comps.back().LocStart = ConsumeToken(); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1009 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1010 | if (Tok.isNot(tok::identifier)) { |
Chris Lattner | f9aa3cb | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 1011 | Diag(Tok, diag::err_expected_ident); |
| 1012 | SkipUntil(tok::r_paren); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1013 | return ExprError(); |
Chris Lattner | f9aa3cb | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 1014 | } |
| 1015 | Comps.back().U.IdentInfo = Tok.getIdentifierInfo(); |
| 1016 | Comps.back().LocEnd = ConsumeToken(); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1017 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1018 | } else if (Tok.is(tok::l_square)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1019 | // offsetof-member-designator: offsetof-member-design '[' expression ']' |
Chris Lattner | f9aa3cb | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 1020 | Comps.push_back(Action::OffsetOfComponent()); |
| 1021 | Comps.back().isBrackets = true; |
| 1022 | Comps.back().LocStart = ConsumeBracket(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1023 | Res = ParseExpression(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1024 | if (Res.isInvalid()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1025 | SkipUntil(tok::r_paren); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1026 | return move(Res); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1027 | } |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 1028 | Comps.back().U.E = Res.release(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1029 | |
Chris Lattner | f9aa3cb | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 1030 | Comps.back().LocEnd = |
| 1031 | MatchRHSPunctuation(tok::r_square, Comps.back().LocStart); |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1032 | } else if (Tok.is(tok::r_paren)) { |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 1033 | if (Ty.isInvalid()) |
| 1034 | Res = ExprError(); |
| 1035 | else |
| 1036 | Res = Actions.ActOnBuiltinOffsetOf(CurScope, StartLoc, TypeLoc, |
| 1037 | Ty.get(), &Comps[0], |
| 1038 | Comps.size(), ConsumeParen()); |
Chris Lattner | 6eb2109 | 2007-08-30 15:52:49 +0000 | [diff] [blame] | 1039 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1040 | } else { |
Chris Lattner | f9aa3cb | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 1041 | // Error occurred. |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1042 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1043 | } |
| 1044 | } |
| 1045 | break; |
Chris Lattner | f9aa3cb | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 1046 | } |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 1047 | case tok::kw___builtin_choose_expr: { |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 1048 | OwningExprResult Cond(ParseAssignmentExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1049 | if (Cond.isInvalid()) { |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 1050 | SkipUntil(tok::r_paren); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1051 | return move(Cond); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 1052 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1053 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1054 | return ExprError(); |
| 1055 | |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 1056 | OwningExprResult Expr1(ParseAssignmentExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1057 | if (Expr1.isInvalid()) { |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 1058 | SkipUntil(tok::r_paren); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1059 | return move(Expr1); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1060 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1061 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1062 | return ExprError(); |
| 1063 | |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 1064 | OwningExprResult Expr2(ParseAssignmentExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1065 | if (Expr2.isInvalid()) { |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 1066 | SkipUntil(tok::r_paren); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1067 | return move(Expr2); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1068 | } |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1069 | if (Tok.isNot(tok::r_paren)) { |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 1070 | Diag(Tok, diag::err_expected_rparen); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1071 | return ExprError(); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 1072 | } |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 1073 | Res = Actions.ActOnChooseExpr(StartLoc, Cond.release(), Expr1.release(), |
| 1074 | Expr2.release(), ConsumeParen()); |
Chris Lattner | 6eb2109 | 2007-08-30 15:52:49 +0000 | [diff] [blame] | 1075 | break; |
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 | case tok::kw___builtin_types_compatible_p: |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 1078 | TypeResult Ty1 = ParseTypeName(); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1079 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1080 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1081 | return ExprError(); |
| 1082 | |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 1083 | TypeResult Ty2 = ParseTypeName(); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1084 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1085 | if (Tok.isNot(tok::r_paren)) { |
Steve Naroff | 363bcff | 2007-08-01 23:45:51 +0000 | [diff] [blame] | 1086 | Diag(Tok, diag::err_expected_rparen); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1087 | return ExprError(); |
Steve Naroff | 363bcff | 2007-08-01 23:45:51 +0000 | [diff] [blame] | 1088 | } |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 1089 | |
| 1090 | if (Ty1.isInvalid() || Ty2.isInvalid()) |
| 1091 | Res = ExprError(); |
| 1092 | else |
| 1093 | Res = Actions.ActOnTypesCompatibleExpr(StartLoc, Ty1.get(), Ty2.get(), |
| 1094 | ConsumeParen()); |
Chris Lattner | 6eb2109 | 2007-08-30 15:52:49 +0000 | [diff] [blame] | 1095 | break; |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 1096 | } |
| 1097 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1098 | // These can be followed by postfix-expr pieces because they are |
| 1099 | // primary-expressions. |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1100 | return ParsePostfixExpressionSuffix(move(Res)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | /// ParseParenExpression - This parses the unit that starts with a '(' token, |
| 1104 | /// based on what is allowed by ExprType. The actual thing parsed is returned |
| 1105 | /// in ExprType. |
| 1106 | /// |
| 1107 | /// primary-expression: [C99 6.5.1] |
| 1108 | /// '(' expression ')' |
| 1109 | /// [GNU] '(' compound-statement ')' (if !ParenExprOnly) |
| 1110 | /// postfix-expression: [C99 6.5.2] |
| 1111 | /// '(' type-name ')' '{' initializer-list '}' |
| 1112 | /// '(' type-name ')' '{' initializer-list ',' '}' |
| 1113 | /// cast-expression: [C99 6.5.4] |
| 1114 | /// '(' type-name ')' cast-expression |
| 1115 | /// |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1116 | Parser::OwningExprResult |
| 1117 | Parser::ParseParenExpression(ParenParseOption &ExprType, |
| 1118 | TypeTy *&CastTy, SourceLocation &RParenLoc) { |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1119 | assert(Tok.is(tok::l_paren) && "Not a paren expr!"); |
Douglas Gregor | f02da89 | 2009-02-09 21:04:56 +0000 | [diff] [blame] | 1120 | GreaterThanIsOperatorScope G(GreaterThanIsOperator, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1121 | SourceLocation OpenLoc = ConsumeParen(); |
Sebastian Redl | 15faa7f | 2008-12-09 20:22:58 +0000 | [diff] [blame] | 1122 | OwningExprResult Result(Actions, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1123 | CastTy = 0; |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1124 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1125 | if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1126 | Diag(Tok, diag::ext_gnu_statement_expr); |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 1127 | OwningStmtResult Stmt(ParseCompoundStatement(true)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1128 | ExprType = CompoundStmt; |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1129 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 1130 | // If the substmt parsed correctly, build the AST node. |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1131 | if (!Stmt.isInvalid() && Tok.is(tok::r_paren)) |
| 1132 | Result = Actions.ActOnStmtExpr( |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 1133 | OpenLoc, Stmt.release(), Tok.getLocation()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1134 | |
Argyrios Kyrtzidis | 78c8d80 | 2008-10-05 19:56:22 +0000 | [diff] [blame] | 1135 | } else if (ExprType >= CompoundLiteral && isTypeIdInParens()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1136 | // Otherwise, this is a compound literal expression or cast expression. |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 1137 | TypeResult Ty = ParseTypeName(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1138 | |
| 1139 | // Match the ')'. |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1140 | if (Tok.is(tok::r_paren)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1141 | RParenLoc = ConsumeParen(); |
| 1142 | else |
| 1143 | MatchRHSPunctuation(tok::r_paren, OpenLoc); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1144 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1145 | if (Tok.is(tok::l_brace)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1146 | if (!getLang().C99) // Compound literals don't exist in C90. |
| 1147 | Diag(OpenLoc, diag::ext_c99_compound_literal); |
| 1148 | Result = ParseInitializer(); |
| 1149 | ExprType = CompoundLiteral; |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 1150 | if (!Result.isInvalid() && !Ty.isInvalid()) |
| 1151 | return Actions.ActOnCompoundLiteral(OpenLoc, Ty.get(), RParenLoc, |
Sebastian Redl | 76ad2e8 | 2009-02-05 15:02:23 +0000 | [diff] [blame] | 1152 | move(Result)); |
Chris Lattner | 42ece64 | 2008-12-12 06:00:12 +0000 | [diff] [blame] | 1153 | return move(Result); |
| 1154 | } |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 1155 | |
Chris Lattner | 42ece64 | 2008-12-12 06:00:12 +0000 | [diff] [blame] | 1156 | if (ExprType == CastExpr) { |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 1157 | // Note that this doesn't parse the subsequent cast-expression, it just |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1158 | // returns the parsed type to the callee. |
| 1159 | ExprType = CastExpr; |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 1160 | |
| 1161 | if (Ty.isInvalid()) |
| 1162 | return ExprError(); |
| 1163 | |
| 1164 | CastTy = Ty.get(); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1165 | return OwningExprResult(Actions); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1166 | } |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 1167 | |
Chris Lattner | 42ece64 | 2008-12-12 06:00:12 +0000 | [diff] [blame] | 1168 | Diag(Tok, diag::err_expected_lbrace_in_compound_literal); |
| 1169 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1170 | } else { |
| 1171 | Result = ParseExpression(); |
| 1172 | ExprType = SimpleExpr; |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1173 | if (!Result.isInvalid() && Tok.is(tok::r_paren)) |
Sebastian Redl | 76ad2e8 | 2009-02-05 15:02:23 +0000 | [diff] [blame] | 1174 | Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), move(Result)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1175 | } |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1176 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1177 | // Match the ')'. |
Chris Lattner | 42ece64 | 2008-12-12 06:00:12 +0000 | [diff] [blame] | 1178 | if (Result.isInvalid()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1179 | SkipUntil(tok::r_paren); |
Chris Lattner | 42ece64 | 2008-12-12 06:00:12 +0000 | [diff] [blame] | 1180 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1181 | } |
Chris Lattner | 42ece64 | 2008-12-12 06:00:12 +0000 | [diff] [blame] | 1182 | |
| 1183 | if (Tok.is(tok::r_paren)) |
| 1184 | RParenLoc = ConsumeParen(); |
| 1185 | else |
| 1186 | MatchRHSPunctuation(tok::r_paren, OpenLoc); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1187 | |
| 1188 | return move(Result); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1189 | } |
| 1190 | |
| 1191 | /// ParseStringLiteralExpression - This handles the various token types that |
| 1192 | /// form string literals, and also handles string concatenation [C99 5.1.1.2, |
| 1193 | /// translation phase #6]. |
| 1194 | /// |
| 1195 | /// primary-expression: [C99 6.5.1] |
| 1196 | /// string-literal |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 1197 | Parser::OwningExprResult Parser::ParseStringLiteralExpression() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1198 | assert(isTokenStringLiteral() && "Not a string literal!"); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 1199 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1200 | // String concat. Note that keywords like __func__ and __FUNCTION__ are not |
| 1201 | // considered to be strings for concatenation purposes. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1202 | llvm::SmallVector<Token, 4> StringToks; |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 1203 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1204 | do { |
| 1205 | StringToks.push_back(Tok); |
| 1206 | ConsumeStringToken(); |
| 1207 | } while (isTokenStringLiteral()); |
| 1208 | |
| 1209 | // Pass the set of string tokens, ready for concatenation, to the actions. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1210 | return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1211 | } |
Argyrios Kyrtzidis | 0cd5b42 | 2008-08-16 20:03:01 +0000 | [diff] [blame] | 1212 | |
| 1213 | /// ParseExpressionList - Used for C/C++ (argument-)expression-list. |
| 1214 | /// |
| 1215 | /// argument-expression-list: |
| 1216 | /// assignment-expression |
| 1217 | /// argument-expression-list , assignment-expression |
| 1218 | /// |
| 1219 | /// [C++] expression-list: |
| 1220 | /// [C++] assignment-expression |
| 1221 | /// [C++] expression-list , assignment-expression |
| 1222 | /// |
| 1223 | bool Parser::ParseExpressionList(ExprListTy &Exprs, CommaLocsTy &CommaLocs) { |
| 1224 | while (1) { |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 1225 | OwningExprResult Expr(ParseAssignmentExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1226 | if (Expr.isInvalid()) |
Argyrios Kyrtzidis | 0cd5b42 | 2008-08-16 20:03:01 +0000 | [diff] [blame] | 1227 | return true; |
Argyrios Kyrtzidis | 4fdc1ca | 2008-08-18 22:49:40 +0000 | [diff] [blame] | 1228 | |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 1229 | Exprs.push_back(Expr.release()); |
Argyrios Kyrtzidis | 0cd5b42 | 2008-08-16 20:03:01 +0000 | [diff] [blame] | 1230 | |
| 1231 | if (Tok.isNot(tok::comma)) |
| 1232 | return false; |
| 1233 | // Move to the next argument, remember where the comma was. |
| 1234 | CommaLocs.push_back(ConsumeToken()); |
| 1235 | } |
| 1236 | } |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1237 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 1238 | /// ParseBlockId - Parse a block-id, which roughly looks like int (int x). |
| 1239 | /// |
| 1240 | /// [clang] block-id: |
| 1241 | /// [clang] specifier-qualifier-list block-declarator |
| 1242 | /// |
| 1243 | void Parser::ParseBlockId() { |
| 1244 | // Parse the specifier-qualifier-list piece. |
| 1245 | DeclSpec DS; |
| 1246 | ParseSpecifierQualifierList(DS); |
| 1247 | |
| 1248 | // Parse the block-declarator. |
| 1249 | Declarator DeclaratorInfo(DS, Declarator::BlockLiteralContext); |
| 1250 | ParseDeclarator(DeclaratorInfo); |
| 1251 | // Inform sema that we are starting a block. |
| 1252 | Actions.ActOnBlockArguments(DeclaratorInfo, CurScope); |
| 1253 | } |
| 1254 | |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1255 | /// ParseBlockLiteralExpression - Parse a block literal, which roughly looks |
Steve Naroff | 17dab4f | 2008-09-16 23:11:46 +0000 | [diff] [blame] | 1256 | /// like ^(int x){ return x+1; } |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1257 | /// |
| 1258 | /// block-literal: |
| 1259 | /// [clang] '^' block-args[opt] compound-statement |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 1260 | /// [clang] '^' block-id compound-statement |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1261 | /// [clang] block-args: |
| 1262 | /// [clang] '(' parameter-list ')' |
| 1263 | /// |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1264 | Parser::OwningExprResult Parser::ParseBlockLiteralExpression() { |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1265 | assert(Tok.is(tok::caret) && "block literal starts with ^"); |
| 1266 | SourceLocation CaretLoc = ConsumeToken(); |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1267 | |
Chris Lattner | 6b91f00 | 2009-03-05 07:32:12 +0000 | [diff] [blame^] | 1268 | PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), CaretLoc, |
| 1269 | "block literal parsing"); |
| 1270 | |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1271 | // Enter a scope to hold everything within the block. This includes the |
| 1272 | // argument decls, decls within the compound expression, etc. This also |
| 1273 | // allows determining whether a variable reference inside the block is |
| 1274 | // within or outside of the block. |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1275 | ParseScope BlockScope(this, Scope::BlockScope | Scope::FnScope | |
| 1276 | Scope::BreakScope | Scope::ContinueScope | |
| 1277 | Scope::DeclScope); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 1278 | |
| 1279 | // Inform sema that we are starting a block. |
| 1280 | Actions.ActOnBlockStart(CaretLoc, CurScope); |
Chris Lattner | 6b91f00 | 2009-03-05 07:32:12 +0000 | [diff] [blame^] | 1281 | |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1282 | // Parse the return type if present. |
| 1283 | DeclSpec DS; |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 1284 | Declarator ParamInfo(DS, Declarator::BlockLiteralContext); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1285 | // FIXME: Since the return type isn't actually parsed, it can't be used to |
| 1286 | // fill ParamInfo with an initial valid range, so do it manually. |
| 1287 | ParamInfo.SetSourceRange(SourceRange(Tok.getLocation(), Tok.getLocation())); |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1288 | |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1289 | // If this block has arguments, parse them. There is no ambiguity here with |
| 1290 | // the expression case, because the expression case requires a parameter list. |
| 1291 | if (Tok.is(tok::l_paren)) { |
| 1292 | ParseParenDeclarator(ParamInfo); |
| 1293 | // Parse the pieces after the identifier as if we had "int(...)". |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1294 | // SetIdentifier sets the source range end, but in this case we're past |
| 1295 | // that location. |
| 1296 | SourceLocation Tmp = ParamInfo.getSourceRange().getEnd(); |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1297 | ParamInfo.SetIdentifier(0, CaretLoc); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1298 | ParamInfo.SetRangeEnd(Tmp); |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1299 | if (ParamInfo.getInvalidType()) { |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 1300 | // If there was an error parsing the arguments, they may have |
| 1301 | // tried to use ^(x+y) which requires an argument list. Just |
| 1302 | // skip the whole block literal. |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1303 | return ExprError(); |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1304 | } |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 1305 | // Inform sema that we are starting a block. |
| 1306 | Actions.ActOnBlockArguments(ParamInfo, CurScope); |
| 1307 | } else if (! Tok.is(tok::l_brace)) { |
| 1308 | ParseBlockId(); |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1309 | } else { |
| 1310 | // Otherwise, pretend we saw (void). |
Douglas Gregor | 965acbb | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 1311 | ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(true, false, |
| 1312 | SourceLocation(), |
Chris Lattner | 5af2f35 | 2009-01-20 19:11:22 +0000 | [diff] [blame] | 1313 | 0, 0, 0, CaretLoc, |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1314 | ParamInfo), |
| 1315 | CaretLoc); |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 1316 | // Inform sema that we are starting a block. |
| 1317 | Actions.ActOnBlockArguments(ParamInfo, CurScope); |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1318 | } |
| 1319 | |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1320 | |
Sebastian Redl | 15faa7f | 2008-12-09 20:22:58 +0000 | [diff] [blame] | 1321 | OwningExprResult Result(Actions, true); |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1322 | if (Tok.is(tok::l_brace)) { |
Sebastian Redl | 61364dd | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 1323 | OwningStmtResult Stmt(ParseCompoundStatementBody()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1324 | if (!Stmt.isInvalid()) { |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 1325 | Result = Actions.ActOnBlockStmtExpr(CaretLoc, Stmt.release(), CurScope); |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1326 | } else { |
| 1327 | Actions.ActOnBlockError(CaretLoc, CurScope); |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1328 | } |
Mike Stump | 281481d | 2009-02-02 23:46:21 +0000 | [diff] [blame] | 1329 | } else { |
Fariborz Jahanian | ff03fbb | 2009-01-14 19:39:53 +0000 | [diff] [blame] | 1330 | // Saw something like: ^expr |
| 1331 | Diag(Tok, diag::err_expected_expression); |
| 1332 | return ExprError(); |
| 1333 | } |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1334 | return move(Result); |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1335 | } |
| 1336 | |