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