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