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