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" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 23 | #include "clang/Sema/DeclSpec.h" |
| 24 | #include "clang/Sema/Scope.h" |
| 25 | #include "clang/Sema/ParsedTemplate.h" |
Chris Lattner | 6b91f00 | 2009-03-05 07:32:12 +0000 | [diff] [blame] | 26 | #include "clang/Basic/PrettyStackTrace.h" |
Chris Lattner | d167ca0 | 2009-12-10 00:21:05 +0000 | [diff] [blame] | 27 | #include "RAIIObjectsForParser.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/SmallVector.h" |
| 29 | #include "llvm/ADT/SmallString.h" |
| 30 | using namespace clang; |
| 31 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 32 | /// getBinOpPrecedence - Return the precedence of the specified binary operator |
Chris Lattner | dfe503e | 2010-07-19 05:07:24 +0000 | [diff] [blame] | 33 | /// token. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 34 | static prec::Level getBinOpPrecedence(tok::TokenKind Kind, |
Douglas Gregor | 3965b7b | 2009-02-25 23:02:36 +0000 | [diff] [blame] | 35 | bool GreaterThanIsOperator, |
| 36 | bool CPlusPlus0x) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 37 | switch (Kind) { |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 38 | case tok::greater: |
Douglas Gregor | 3965b7b | 2009-02-25 23:02:36 +0000 | [diff] [blame] | 39 | // C++ [temp.names]p3: |
| 40 | // [...] When parsing a template-argument-list, the first |
| 41 | // non-nested > is taken as the ending delimiter rather than a |
| 42 | // greater-than operator. [...] |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 43 | if (GreaterThanIsOperator) |
| 44 | return prec::Relational; |
| 45 | return prec::Unknown; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 46 | |
Douglas Gregor | 3965b7b | 2009-02-25 23:02:36 +0000 | [diff] [blame] | 47 | case tok::greatergreater: |
| 48 | // C++0x [temp.names]p3: |
| 49 | // |
| 50 | // [...] Similarly, the first non-nested >> is treated as two |
| 51 | // consecutive but distinct > tokens, the first of which is |
| 52 | // taken as the end of the template-argument-list and completes |
| 53 | // the template-id. [...] |
| 54 | if (GreaterThanIsOperator || !CPlusPlus0x) |
| 55 | return prec::Shift; |
| 56 | return prec::Unknown; |
| 57 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 58 | default: return prec::Unknown; |
| 59 | case tok::comma: return prec::Comma; |
| 60 | case tok::equal: |
| 61 | case tok::starequal: |
| 62 | case tok::slashequal: |
| 63 | case tok::percentequal: |
| 64 | case tok::plusequal: |
| 65 | case tok::minusequal: |
| 66 | case tok::lesslessequal: |
| 67 | case tok::greatergreaterequal: |
| 68 | case tok::ampequal: |
| 69 | case tok::caretequal: |
| 70 | case tok::pipeequal: return prec::Assignment; |
| 71 | case tok::question: return prec::Conditional; |
| 72 | case tok::pipepipe: return prec::LogicalOr; |
| 73 | case tok::ampamp: return prec::LogicalAnd; |
| 74 | case tok::pipe: return prec::InclusiveOr; |
| 75 | case tok::caret: return prec::ExclusiveOr; |
| 76 | case tok::amp: return prec::And; |
| 77 | case tok::exclaimequal: |
| 78 | case tok::equalequal: return prec::Equality; |
| 79 | case tok::lessequal: |
| 80 | case tok::less: |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 81 | case tok::greaterequal: return prec::Relational; |
Douglas Gregor | 3965b7b | 2009-02-25 23:02:36 +0000 | [diff] [blame] | 82 | case tok::lessless: return prec::Shift; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 83 | case tok::plus: |
| 84 | case tok::minus: return prec::Additive; |
| 85 | case tok::percent: |
| 86 | case tok::slash: |
| 87 | case tok::star: return prec::Multiplicative; |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 88 | case tok::periodstar: |
| 89 | case tok::arrowstar: return prec::PointerToMember; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
| 93 | |
| 94 | /// ParseExpression - Simple precedence-based parser for binary/ternary |
| 95 | /// operators. |
| 96 | /// |
| 97 | /// Note: we diverge from the C99 grammar when parsing the assignment-expression |
| 98 | /// production. C99 specifies that the LHS of an assignment operator should be |
| 99 | /// parsed as a unary-expression, but consistency dictates that it be a |
| 100 | /// conditional-expession. In practice, the important thing here is that the |
| 101 | /// LHS of an assignment has to be an l-value, which productions between |
| 102 | /// unary-expression and conditional-expression don't produce. Because we want |
| 103 | /// consistency, we parse the LHS as a conditional-expression, then check for |
| 104 | /// l-value-ness in semantic analysis stages. |
| 105 | /// |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 106 | /// pm-expression: [C++ 5.5] |
| 107 | /// cast-expression |
| 108 | /// pm-expression '.*' cast-expression |
| 109 | /// pm-expression '->*' cast-expression |
| 110 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 111 | /// multiplicative-expression: [C99 6.5.5] |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 112 | /// Note: in C++, apply pm-expression instead of cast-expression |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 113 | /// cast-expression |
| 114 | /// multiplicative-expression '*' cast-expression |
| 115 | /// multiplicative-expression '/' cast-expression |
| 116 | /// multiplicative-expression '%' cast-expression |
| 117 | /// |
| 118 | /// additive-expression: [C99 6.5.6] |
| 119 | /// multiplicative-expression |
| 120 | /// additive-expression '+' multiplicative-expression |
| 121 | /// additive-expression '-' multiplicative-expression |
| 122 | /// |
| 123 | /// shift-expression: [C99 6.5.7] |
| 124 | /// additive-expression |
| 125 | /// shift-expression '<<' additive-expression |
| 126 | /// shift-expression '>>' additive-expression |
| 127 | /// |
| 128 | /// relational-expression: [C99 6.5.8] |
| 129 | /// shift-expression |
| 130 | /// relational-expression '<' shift-expression |
| 131 | /// relational-expression '>' shift-expression |
| 132 | /// relational-expression '<=' shift-expression |
| 133 | /// relational-expression '>=' shift-expression |
| 134 | /// |
| 135 | /// equality-expression: [C99 6.5.9] |
| 136 | /// relational-expression |
| 137 | /// equality-expression '==' relational-expression |
| 138 | /// equality-expression '!=' relational-expression |
| 139 | /// |
| 140 | /// AND-expression: [C99 6.5.10] |
| 141 | /// equality-expression |
| 142 | /// AND-expression '&' equality-expression |
| 143 | /// |
| 144 | /// exclusive-OR-expression: [C99 6.5.11] |
| 145 | /// AND-expression |
| 146 | /// exclusive-OR-expression '^' AND-expression |
| 147 | /// |
| 148 | /// inclusive-OR-expression: [C99 6.5.12] |
| 149 | /// exclusive-OR-expression |
| 150 | /// inclusive-OR-expression '|' exclusive-OR-expression |
| 151 | /// |
| 152 | /// logical-AND-expression: [C99 6.5.13] |
| 153 | /// inclusive-OR-expression |
| 154 | /// logical-AND-expression '&&' inclusive-OR-expression |
| 155 | /// |
| 156 | /// logical-OR-expression: [C99 6.5.14] |
| 157 | /// logical-AND-expression |
| 158 | /// logical-OR-expression '||' logical-AND-expression |
| 159 | /// |
| 160 | /// conditional-expression: [C99 6.5.15] |
| 161 | /// logical-OR-expression |
| 162 | /// logical-OR-expression '?' expression ':' conditional-expression |
| 163 | /// [GNU] logical-OR-expression '?' ':' conditional-expression |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 164 | /// [C++] the third operand is an assignment-expression |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 165 | /// |
| 166 | /// assignment-expression: [C99 6.5.16] |
| 167 | /// conditional-expression |
| 168 | /// unary-expression assignment-operator assignment-expression |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 169 | /// [C++] throw-expression [C++ 15] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 170 | /// |
| 171 | /// assignment-operator: one of |
| 172 | /// = *= /= %= += -= <<= >>= &= ^= |= |
| 173 | /// |
| 174 | /// expression: [C99 6.5.17] |
Douglas Gregor | dcaa1ca | 2011-01-03 19:31:53 +0000 | [diff] [blame] | 175 | /// assignment-expression ...[opt] |
| 176 | /// expression ',' assignment-expression ...[opt] |
Douglas Gregor | 5ecdd78 | 2011-04-27 06:18:01 +0000 | [diff] [blame] | 177 | ExprResult Parser::ParseExpression() { |
| 178 | ExprResult LHS(ParseAssignmentExpression()); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 179 | return ParseRHSOfBinaryExpression(move(LHS), prec::Comma); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 182 | /// This routine is called when the '@' is seen and consumed. |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 183 | /// Current token is an Identifier and is not a 'try'. This |
Chris Lattner | c97c204 | 2007-10-03 22:03:06 +0000 | [diff] [blame] | 184 | /// routine is necessary to disambiguate @try-statement from, |
| 185 | /// for example, @encode-expression. |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 186 | /// |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 187 | ExprResult |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 188 | Parser::ParseExpressionWithLeadingAt(SourceLocation AtLoc) { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 189 | ExprResult LHS(ParseObjCAtExpression(AtLoc)); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 190 | return ParseRHSOfBinaryExpression(move(LHS), prec::Comma); |
Fariborz Jahanian | 397fcc1 | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Eli Friedman | adf077f | 2009-01-27 08:43:38 +0000 | [diff] [blame] | 193 | /// This routine is called when a leading '__extension__' is seen and |
| 194 | /// consumed. This is necessary because the token gets consumed in the |
| 195 | /// process of disambiguating between an expression and a declaration. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 196 | ExprResult |
Eli Friedman | adf077f | 2009-01-27 08:43:38 +0000 | [diff] [blame] | 197 | Parser::ParseExpressionWithLeadingExtension(SourceLocation ExtLoc) { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 198 | ExprResult LHS(true); |
Eli Friedman | bc6c848 | 2009-05-16 23:40:44 +0000 | [diff] [blame] | 199 | { |
| 200 | // Silence extension warnings in the sub-expression |
| 201 | ExtensionRAIIObject O(Diags); |
| 202 | |
| 203 | LHS = ParseCastExpression(false); |
Eli Friedman | bc6c848 | 2009-05-16 23:40:44 +0000 | [diff] [blame] | 204 | } |
Eli Friedman | adf077f | 2009-01-27 08:43:38 +0000 | [diff] [blame] | 205 | |
Douglas Gregor | 200b292 | 2010-09-17 22:25:06 +0000 | [diff] [blame] | 206 | if (!LHS.isInvalid()) |
| 207 | LHS = Actions.ActOnUnaryOp(getCurScope(), ExtLoc, tok::kw___extension__, |
| 208 | LHS.take()); |
Eli Friedman | adf077f | 2009-01-27 08:43:38 +0000 | [diff] [blame] | 209 | |
Douglas Gregor | 200b292 | 2010-09-17 22:25:06 +0000 | [diff] [blame] | 210 | return ParseRHSOfBinaryExpression(move(LHS), prec::Comma); |
Eli Friedman | adf077f | 2009-01-27 08:43:38 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 213 | /// ParseAssignmentExpression - Parse an expr that doesn't include commas. |
Douglas Gregor | 5ecdd78 | 2011-04-27 06:18:01 +0000 | [diff] [blame] | 214 | ExprResult Parser::ParseAssignmentExpression() { |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 215 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 5ecdd78 | 2011-04-27 06:18:01 +0000 | [diff] [blame] | 216 | Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 217 | cutOffParsing(); |
| 218 | return ExprError(); |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Douglas Gregor | 5ecdd78 | 2011-04-27 06:18:01 +0000 | [diff] [blame] | 221 | if (Tok.is(tok::kw_throw)) |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 222 | return ParseThrowExpression(); |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 223 | |
Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 224 | ExprResult LHS = ParseCastExpression(/*isUnaryExpression=*/false); |
Douglas Gregor | 200b292 | 2010-09-17 22:25:06 +0000 | [diff] [blame] | 225 | return ParseRHSOfBinaryExpression(move(LHS), prec::Assignment); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Chris Lattner | b93fb49 | 2008-06-02 21:31:07 +0000 | [diff] [blame] | 228 | /// ParseAssignmentExprWithObjCMessageExprStart - Parse an assignment expression |
| 229 | /// where part of an objc message send has already been parsed. In this case |
| 230 | /// LBracLoc indicates the location of the '[' of the message send, and either |
| 231 | /// ReceiverName or ReceiverExpr is non-null indicating the receiver of the |
| 232 | /// message. |
| 233 | /// |
| 234 | /// Since this handles full assignment-expression's, it handles postfix |
| 235 | /// expressions and other binary operators for these expressions as well. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 236 | ExprResult |
Chris Lattner | b93fb49 | 2008-06-02 21:31:07 +0000 | [diff] [blame] | 237 | Parser::ParseAssignmentExprWithObjCMessageExprStart(SourceLocation LBracLoc, |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 238 | SourceLocation SuperLoc, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 239 | ParsedType ReceiverType, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 240 | Expr *ReceiverExpr) { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 241 | ExprResult R |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 242 | = ParseObjCMessageExpressionBody(LBracLoc, SuperLoc, |
| 243 | ReceiverType, ReceiverExpr); |
Douglas Gregor | ac5fd84 | 2010-09-18 01:28:11 +0000 | [diff] [blame] | 244 | R = ParsePostfixExpressionSuffix(R); |
Douglas Gregor | 200b292 | 2010-09-17 22:25:06 +0000 | [diff] [blame] | 245 | return ParseRHSOfBinaryExpression(R, prec::Assignment); |
Chris Lattner | b93fb49 | 2008-06-02 21:31:07 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 249 | ExprResult Parser::ParseConstantExpression() { |
Richard Smith | f6702a3 | 2011-12-20 02:08:33 +0000 | [diff] [blame] | 250 | // C++03 [basic.def.odr]p2: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 251 | // An expression is potentially evaluated unless it appears where an |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 252 | // integral constant expression is required (see 5.19) [...]. |
Richard Smith | f6702a3 | 2011-12-20 02:08:33 +0000 | [diff] [blame] | 253 | // C++98 and C++11 have no such rule, but this is only a defect in C++98. |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 254 | EnterExpressionEvaluationContext Unevaluated(Actions, |
Richard Smith | f6702a3 | 2011-12-20 02:08:33 +0000 | [diff] [blame] | 255 | Sema::ConstantEvaluated); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 256 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 257 | ExprResult LHS(ParseCastExpression(false)); |
Douglas Gregor | 200b292 | 2010-09-17 22:25:06 +0000 | [diff] [blame] | 258 | return ParseRHSOfBinaryExpression(LHS, prec::Conditional); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 261 | /// ParseRHSOfBinaryExpression - Parse a binary expression that starts with |
| 262 | /// LHS and has a precedence of at least MinPrec. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 263 | ExprResult |
| 264 | Parser::ParseRHSOfBinaryExpression(ExprResult LHS, prec::Level MinPrec) { |
Douglas Gregor | 6aa14d8 | 2010-04-21 22:36:40 +0000 | [diff] [blame] | 265 | prec::Level NextTokPrec = getBinOpPrecedence(Tok.getKind(), |
| 266 | GreaterThanIsOperator, |
| 267 | getLang().CPlusPlus0x); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 268 | SourceLocation ColonLoc; |
| 269 | |
| 270 | while (1) { |
| 271 | // If this token has a lower precedence than we are allowed to parse (e.g. |
| 272 | // because we are called recursively, or because the token is not a binop), |
| 273 | // then we are done! |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 274 | if (NextTokPrec < MinPrec) |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 275 | return move(LHS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 276 | |
| 277 | // Consume the operator, saving the operator token for error reporting. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 278 | Token OpToken = Tok; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 279 | ConsumeToken(); |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 280 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 281 | // Special case handling for the ternary operator. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 282 | ExprResult TernaryMiddle(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 283 | if (NextTokPrec == prec::Conditional) { |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 284 | if (Tok.isNot(tok::colon)) { |
Chris Lattner | a69d0ed | 2009-12-10 02:02:58 +0000 | [diff] [blame] | 285 | // Don't parse FOO:BAR as if it were a typo for FOO::BAR. |
| 286 | ColonProtectionRAIIObject X(*this); |
| 287 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 288 | // Handle this production specially: |
| 289 | // logical-OR-expression '?' expression ':' conditional-expression |
| 290 | // In particular, the RHS of the '?' is 'expression', not |
| 291 | // 'logical-OR-expression' as we might expect. |
| 292 | TernaryMiddle = ParseExpression(); |
Douglas Gregor | 9485989 | 2010-09-17 22:41:34 +0000 | [diff] [blame] | 293 | if (TernaryMiddle.isInvalid()) { |
| 294 | LHS = ExprError(); |
| 295 | TernaryMiddle = 0; |
| 296 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 297 | } else { |
| 298 | // Special case handling of "X ? Y : Z" where Y is empty: |
| 299 | // logical-OR-expression '?' ':' conditional-expression [GNU] |
Sebastian Redl | 15faa7f | 2008-12-09 20:22:58 +0000 | [diff] [blame] | 300 | TernaryMiddle = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 301 | Diag(Tok, diag::ext_gnu_conditional_expr); |
| 302 | } |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 303 | |
Chris Lattner | e5deae9 | 2010-04-20 21:33:39 +0000 | [diff] [blame] | 304 | if (Tok.is(tok::colon)) { |
| 305 | // Eat the colon. |
| 306 | ColonLoc = ConsumeToken(); |
| 307 | } else { |
Chandler Carruth | b00d37e | 2011-07-26 05:19:46 +0000 | [diff] [blame] | 308 | // Otherwise, we're missing a ':'. Assume that this was a typo that |
| 309 | // the user forgot. If we're not in a macro expansion, we can suggest |
| 310 | // a fixit hint. If there were two spaces before the current token, |
Chris Lattner | 2472882 | 2010-05-24 22:31:37 +0000 | [diff] [blame] | 311 | // suggest inserting the colon in between them, otherwise insert ": ". |
| 312 | SourceLocation FILoc = Tok.getLocation(); |
| 313 | const char *FIText = ": "; |
Argyrios Kyrtzidis | b5303aa | 2011-06-24 17:28:29 +0000 | [diff] [blame] | 314 | const SourceManager &SM = PP.getSourceManager(); |
Chandler Carruth | 433db06 | 2011-07-14 08:20:40 +0000 | [diff] [blame] | 315 | if (FILoc.isFileID() || PP.isAtStartOfMacroExpansion(FILoc)) { |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 316 | FILoc = SM.getExpansionLoc(FILoc); |
Chris Lattner | 2472882 | 2010-05-24 22:31:37 +0000 | [diff] [blame] | 317 | bool IsInvalid = false; |
| 318 | const char *SourcePtr = |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 319 | SM.getCharacterData(FILoc.getLocWithOffset(-1), &IsInvalid); |
Chris Lattner | 2472882 | 2010-05-24 22:31:37 +0000 | [diff] [blame] | 320 | if (!IsInvalid && *SourcePtr == ' ') { |
| 321 | SourcePtr = |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 322 | SM.getCharacterData(FILoc.getLocWithOffset(-2), &IsInvalid); |
Chris Lattner | 2472882 | 2010-05-24 22:31:37 +0000 | [diff] [blame] | 323 | if (!IsInvalid && *SourcePtr == ' ') { |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 324 | FILoc = FILoc.getLocWithOffset(-1); |
Chris Lattner | 2472882 | 2010-05-24 22:31:37 +0000 | [diff] [blame] | 325 | FIText = ":"; |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | |
Ted Kremenek | 987aa87 | 2010-04-12 22:10:35 +0000 | [diff] [blame] | 330 | Diag(Tok, diag::err_expected_colon) |
Chris Lattner | 2472882 | 2010-05-24 22:31:37 +0000 | [diff] [blame] | 331 | << FixItHint::CreateInsertion(FILoc, FIText); |
Chris Lattner | 28eb7e9 | 2008-11-23 23:17:07 +0000 | [diff] [blame] | 332 | Diag(OpToken, diag::note_matching) << "?"; |
Chris Lattner | e5deae9 | 2010-04-20 21:33:39 +0000 | [diff] [blame] | 333 | ColonLoc = Tok.getLocation(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 334 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 335 | } |
Fariborz Jahanian | f071e9b | 2009-10-23 21:01:39 +0000 | [diff] [blame] | 336 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 337 | // Code completion for the right-hand side of an assignment expression |
| 338 | // goes through a special hook that takes the left-hand side into account. |
| 339 | if (Tok.is(tok::code_completion) && NextTokPrec == prec::Assignment) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 340 | Actions.CodeCompleteAssignmentRHS(getCurScope(), LHS.get()); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 341 | cutOffParsing(); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 342 | return ExprError(); |
| 343 | } |
| 344 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 345 | // Parse another leaf here for the RHS of the operator. |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 346 | // ParseCastExpression works here because all RHS expressions in C have it |
| 347 | // as a prefix, at least. However, in C++, an assignment-expression could |
| 348 | // be a throw-expression, which is not a valid cast-expression. |
| 349 | // Therefore we need some special-casing here. |
| 350 | // Also note that the third operand of the conditional operator is |
| 351 | // an assignment-expression in C++. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 352 | ExprResult RHS; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 353 | if (getLang().CPlusPlus && NextTokPrec <= prec::Conditional) |
| 354 | RHS = ParseAssignmentExpression(); |
| 355 | else |
| 356 | RHS = ParseCastExpression(false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 357 | |
Douglas Gregor | 200b292 | 2010-09-17 22:25:06 +0000 | [diff] [blame] | 358 | if (RHS.isInvalid()) |
| 359 | LHS = ExprError(); |
| 360 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 361 | // Remember the precedence of this operator and get the precedence of the |
| 362 | // operator immediately to the right of the RHS. |
Douglas Gregor | 6aa14d8 | 2010-04-21 22:36:40 +0000 | [diff] [blame] | 363 | prec::Level ThisPrec = NextTokPrec; |
Douglas Gregor | 3965b7b | 2009-02-25 23:02:36 +0000 | [diff] [blame] | 364 | NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator, |
| 365 | getLang().CPlusPlus0x); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 366 | |
| 367 | // Assignment and conditional expressions are right-associative. |
Chris Lattner | d7d860d | 2007-12-18 06:06:23 +0000 | [diff] [blame] | 368 | bool isRightAssoc = ThisPrec == prec::Conditional || |
| 369 | ThisPrec == prec::Assignment; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 370 | |
| 371 | // Get the precedence of the operator to the right of the RHS. If it binds |
| 372 | // more tightly with RHS than we do, evaluate it completely first. |
| 373 | if (ThisPrec < NextTokPrec || |
| 374 | (ThisPrec == NextTokPrec && isRightAssoc)) { |
| 375 | // If this is left-associative, only parse things on the RHS that bind |
| 376 | // more tightly than the current operator. If it is left-associative, it |
| 377 | // is okay, to bind exactly as tightly. For example, compile A=B=C=D as |
| 378 | // 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] | 379 | // The function takes ownership of the RHS. |
Douglas Gregor | 200b292 | 2010-09-17 22:25:06 +0000 | [diff] [blame] | 380 | RHS = ParseRHSOfBinaryExpression(RHS, |
Douglas Gregor | 6aa14d8 | 2010-04-21 22:36:40 +0000 | [diff] [blame] | 381 | static_cast<prec::Level>(ThisPrec + !isRightAssoc)); |
Douglas Gregor | 200b292 | 2010-09-17 22:25:06 +0000 | [diff] [blame] | 382 | |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 383 | if (RHS.isInvalid()) |
Douglas Gregor | 200b292 | 2010-09-17 22:25:06 +0000 | [diff] [blame] | 384 | LHS = ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 385 | |
Douglas Gregor | 3965b7b | 2009-02-25 23:02:36 +0000 | [diff] [blame] | 386 | NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator, |
| 387 | getLang().CPlusPlus0x); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 388 | } |
| 389 | assert(NextTokPrec <= ThisPrec && "Recursion didn't work!"); |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 390 | |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 391 | if (!LHS.isInvalid()) { |
Chris Lattner | d56d6b6 | 2007-08-31 05:01:50 +0000 | [diff] [blame] | 392 | // Combine the LHS and RHS into the LHS (e.g. build AST). |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 393 | if (TernaryMiddle.isInvalid()) { |
| 394 | // If we're using '>>' as an operator within a template |
| 395 | // argument list (in C++98), suggest the addition of |
| 396 | // parentheses so that the code remains well-formed in C++0x. |
| 397 | if (!GreaterThanIsOperator && OpToken.is(tok::greatergreater)) |
| 398 | SuggestParentheses(OpToken.getLocation(), |
| 399 | diag::warn_cxx0x_right_shift_in_template_arg, |
| 400 | SourceRange(Actions.getExprRange(LHS.get()).getBegin(), |
| 401 | Actions.getExprRange(RHS.get()).getEnd())); |
| 402 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 403 | LHS = Actions.ActOnBinOp(getCurScope(), OpToken.getLocation(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 404 | OpToken.getKind(), LHS.take(), RHS.take()); |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 405 | } else |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 406 | LHS = Actions.ActOnConditionalOp(OpToken.getLocation(), ColonLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 407 | LHS.take(), TernaryMiddle.take(), |
| 408 | RHS.take()); |
Chris Lattner | d56d6b6 | 2007-08-31 05:01:50 +0000 | [diff] [blame] | 409 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | |
| 413 | /// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 414 | /// true, parse a unary-expression. isAddressOfOperand exists because an |
| 415 | /// id-expression that is the operand of address-of gets special treatment |
| 416 | /// due to member pointers. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 417 | /// |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 418 | ExprResult Parser::ParseCastExpression(bool isUnaryExpression, |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 419 | bool isAddressOfOperand, |
Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 420 | bool isTypeCast) { |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 421 | bool NotCastExpr; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 422 | ExprResult Res = ParseCastExpression(isUnaryExpression, |
Douglas Gregor | 200b292 | 2010-09-17 22:25:06 +0000 | [diff] [blame] | 423 | isAddressOfOperand, |
| 424 | NotCastExpr, |
Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 425 | isTypeCast); |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 426 | if (NotCastExpr) |
| 427 | Diag(Tok, diag::err_expected_expression); |
| 428 | return move(Res); |
| 429 | } |
| 430 | |
| 431 | /// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is |
| 432 | /// true, parse a unary-expression. isAddressOfOperand exists because an |
| 433 | /// id-expression that is the operand of address-of gets special treatment |
| 434 | /// due to member pointers. NotCastExpr is set to true if the token is not the |
| 435 | /// start of a cast-expression, and no diagnostic is emitted in this case. |
| 436 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 437 | /// cast-expression: [C99 6.5.4] |
| 438 | /// unary-expression |
| 439 | /// '(' type-name ')' cast-expression |
| 440 | /// |
| 441 | /// unary-expression: [C99 6.5.3] |
| 442 | /// postfix-expression |
| 443 | /// '++' unary-expression |
| 444 | /// '--' unary-expression |
| 445 | /// unary-operator cast-expression |
| 446 | /// 'sizeof' unary-expression |
| 447 | /// 'sizeof' '(' type-name ')' |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 448 | /// [C++0x] 'sizeof' '...' '(' identifier ')' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 449 | /// [GNU] '__alignof' unary-expression |
| 450 | /// [GNU] '__alignof' '(' type-name ')' |
Douglas Gregor | 85bb3da | 2008-11-06 15:17:27 +0000 | [diff] [blame] | 451 | /// [C++0x] 'alignof' '(' type-id ')' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 452 | /// [GNU] '&&' identifier |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 453 | /// [C++] new-expression |
| 454 | /// [C++] delete-expression |
Sebastian Redl | 02bc21a | 2010-09-10 20:55:37 +0000 | [diff] [blame] | 455 | /// [C++0x] 'noexcept' '(' expression ')' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 456 | /// |
| 457 | /// unary-operator: one of |
| 458 | /// '&' '*' '+' '-' '~' '!' |
| 459 | /// [GNU] '__extension__' '__real' '__imag' |
| 460 | /// |
| 461 | /// primary-expression: [C99 6.5.1] |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 462 | /// [C99] identifier |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 463 | /// [C++] id-expression |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 464 | /// constant |
| 465 | /// string-literal |
| 466 | /// [C++] boolean-literal [C++ 2.13.5] |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 467 | /// [C++0x] 'nullptr' [C++0x 2.14.7] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 468 | /// '(' expression ')' |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 469 | /// [C11] generic-selection |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 470 | /// '__func__' [C99 6.4.2.2] |
| 471 | /// [GNU] '__FUNCTION__' |
| 472 | /// [GNU] '__PRETTY_FUNCTION__' |
| 473 | /// [GNU] '(' compound-statement ')' |
| 474 | /// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')' |
| 475 | /// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')' |
| 476 | /// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ',' |
| 477 | /// assign-expr ')' |
| 478 | /// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')' |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 479 | /// [GNU] '__null' |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 480 | /// [OBJC] '[' objc-message-expr ']' |
Chris Lattner | 5ac87ed | 2008-01-25 18:58:06 +0000 | [diff] [blame] | 481 | /// [OBJC] '@selector' '(' objc-selector-arg ')' |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 482 | /// [OBJC] '@protocol' '(' identifier ')' |
| 483 | /// [OBJC] '@encode' '(' type-name ')' |
Fariborz Jahanian | 0ccb27d | 2007-09-05 19:52:07 +0000 | [diff] [blame] | 484 | /// [OBJC] objc-string-literal |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 485 | /// [C++] simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3] |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 486 | /// [C++0x] simple-type-specifier braced-init-list [C++ 5.2.3] |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 487 | /// [C++] typename-specifier '(' expression-list[opt] ')' [C++ 5.2.3] |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 488 | /// [C++0x] typename-specifier braced-init-list [C++ 5.2.3] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 489 | /// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1] |
| 490 | /// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1] |
| 491 | /// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1] |
| 492 | /// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1] |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 493 | /// [C++] 'typeid' '(' expression ')' [C++ 5.2p1] |
| 494 | /// [C++] 'typeid' '(' type-id ')' [C++ 5.2p1] |
Argyrios Kyrtzidis | d7464be | 2008-07-16 07:23:27 +0000 | [diff] [blame] | 495 | /// [C++] 'this' [C++ 9.3.2] |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 496 | /// [G++] unary-type-trait '(' type-id ')' |
| 497 | /// [G++] binary-type-trait '(' type-id ',' type-id ')' [TODO] |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 498 | /// [EMBT] array-type-trait '(' type-id ',' integer ')' |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 499 | /// [clang] '^' block-literal |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 500 | /// |
| 501 | /// constant: [C99 6.4.4] |
| 502 | /// integer-constant |
| 503 | /// floating-constant |
| 504 | /// enumeration-constant -> identifier |
| 505 | /// character-constant |
| 506 | /// |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 507 | /// id-expression: [C++ 5.1] |
| 508 | /// unqualified-id |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 509 | /// qualified-id |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 510 | /// |
| 511 | /// unqualified-id: [C++ 5.1] |
| 512 | /// identifier |
| 513 | /// operator-function-id |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 514 | /// conversion-function-id |
| 515 | /// '~' class-name |
| 516 | /// template-id |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 517 | /// |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 518 | /// new-expression: [C++ 5.3.4] |
| 519 | /// '::'[opt] 'new' new-placement[opt] new-type-id |
| 520 | /// new-initializer[opt] |
| 521 | /// '::'[opt] 'new' new-placement[opt] '(' type-id ')' |
| 522 | /// new-initializer[opt] |
| 523 | /// |
| 524 | /// delete-expression: [C++ 5.3.5] |
| 525 | /// '::'[opt] 'delete' cast-expression |
| 526 | /// '::'[opt] 'delete' '[' ']' cast-expression |
| 527 | /// |
John Wiegley | 20c0da7 | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 528 | /// [GNU/Embarcadero] unary-type-trait: |
| 529 | /// '__is_arithmetic' |
| 530 | /// '__is_floating_point' |
| 531 | /// '__is_integral' |
| 532 | /// '__is_lvalue_expr' |
| 533 | /// '__is_rvalue_expr' |
| 534 | /// '__is_complete_type' |
| 535 | /// '__is_void' |
| 536 | /// '__is_array' |
| 537 | /// '__is_function' |
| 538 | /// '__is_reference' |
| 539 | /// '__is_lvalue_reference' |
| 540 | /// '__is_rvalue_reference' |
| 541 | /// '__is_fundamental' |
| 542 | /// '__is_object' |
| 543 | /// '__is_scalar' |
| 544 | /// '__is_compound' |
| 545 | /// '__is_pointer' |
| 546 | /// '__is_member_object_pointer' |
| 547 | /// '__is_member_function_pointer' |
| 548 | /// '__is_member_pointer' |
| 549 | /// '__is_const' |
| 550 | /// '__is_volatile' |
| 551 | /// '__is_trivial' |
| 552 | /// '__is_standard_layout' |
| 553 | /// '__is_signed' |
| 554 | /// '__is_unsigned' |
| 555 | /// |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 556 | /// [GNU] unary-type-trait: |
Sebastian Redl | c238f09 | 2010-08-31 04:59:00 +0000 | [diff] [blame] | 557 | /// '__has_nothrow_assign' |
| 558 | /// '__has_nothrow_copy' |
| 559 | /// '__has_nothrow_constructor' |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 560 | /// '__has_trivial_assign' [TODO] |
| 561 | /// '__has_trivial_copy' [TODO] |
Anders Carlsson | 347ba89 | 2009-04-16 00:08:20 +0000 | [diff] [blame] | 562 | /// '__has_trivial_constructor' |
Anders Carlsson | 072abef | 2009-04-17 02:34:54 +0000 | [diff] [blame] | 563 | /// '__has_trivial_destructor' |
Sebastian Redl | d4b25cb | 2010-09-02 23:19:42 +0000 | [diff] [blame] | 564 | /// '__has_virtual_destructor' |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 565 | /// '__is_abstract' [TODO] |
| 566 | /// '__is_class' |
| 567 | /// '__is_empty' [TODO] |
| 568 | /// '__is_enum' |
Douglas Gregor | 5e9392b | 2011-12-03 18:14:24 +0000 | [diff] [blame] | 569 | /// '__is_final' |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 570 | /// '__is_pod' |
| 571 | /// '__is_polymorphic' |
Chandler Carruth | b7e9589 | 2011-04-23 10:47:28 +0000 | [diff] [blame] | 572 | /// '__is_trivial' |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 573 | /// '__is_union' |
| 574 | /// |
Sean Hunt | feb375d | 2011-05-13 00:31:07 +0000 | [diff] [blame] | 575 | /// [Clang] unary-type-trait: |
| 576 | /// '__trivially_copyable' |
| 577 | /// |
Douglas Gregor | 9f36113 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 578 | /// binary-type-trait: |
| 579 | /// [GNU] '__is_base_of' |
| 580 | /// [MS] '__is_convertible_to' |
John Wiegley | 20c0da7 | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 581 | /// '__is_convertible' |
| 582 | /// '__is_same' |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 583 | /// |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 584 | /// [Embarcadero] array-type-trait: |
| 585 | /// '__array_rank' |
| 586 | /// '__array_extent' |
| 587 | /// |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 588 | /// [Embarcadero] expression-trait: |
| 589 | /// '__is_lvalue_expr' |
| 590 | /// '__is_rvalue_expr' |
| 591 | /// |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 592 | ExprResult Parser::ParseCastExpression(bool isUnaryExpression, |
Sebastian Redl | 02bc21a | 2010-09-10 20:55:37 +0000 | [diff] [blame] | 593 | bool isAddressOfOperand, |
| 594 | bool &NotCastExpr, |
Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 595 | bool isTypeCast) { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 596 | ExprResult Res; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 597 | tok::TokenKind SavedKind = Tok.getKind(); |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 598 | NotCastExpr = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 599 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 600 | // This handles all of cast-expression, unary-expression, postfix-expression, |
| 601 | // and primary-expression. We handle them together like this for efficiency |
| 602 | // and to simplify handling of an expression starting with a '(' token: which |
| 603 | // may be one of a parenthesized expression, cast-expression, compound literal |
| 604 | // expression, or statement expression. |
| 605 | // |
| 606 | // If the parsed tokens consist of a primary-expression, the cases below |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 607 | // break out of the switch; at the end we call ParsePostfixExpressionSuffix |
| 608 | // to handle the postfix expression suffixes. Cases that cannot be followed |
| 609 | // by postfix exprs should return without invoking |
| 610 | // ParsePostfixExpressionSuffix. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 611 | switch (SavedKind) { |
| 612 | case tok::l_paren: { |
| 613 | // If this expression is limited to being a unary-expression, the parent can |
| 614 | // not start a cast expression. |
| 615 | ParenParseOption ParenExprType = |
Douglas Gregor | d420663 | 2010-08-06 14:50:36 +0000 | [diff] [blame] | 616 | (isUnaryExpression && !getLang().CPlusPlus)? CompoundLiteral : CastExpr; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 617 | ParsedType CastTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 618 | SourceLocation RParenLoc; |
Chris Lattner | 932dff7 | 2009-12-10 02:08:07 +0000 | [diff] [blame] | 619 | |
| 620 | { |
Douglas Gregor | 0fbda68 | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 621 | // The inside of the parens don't need to be a colon protected scope, and |
| 622 | // isn't immediately a message send. |
Chris Lattner | 932dff7 | 2009-12-10 02:08:07 +0000 | [diff] [blame] | 623 | ColonProtectionRAIIObject X(*this, false); |
Douglas Gregor | 0fbda68 | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 624 | |
Chris Lattner | 932dff7 | 2009-12-10 02:08:07 +0000 | [diff] [blame] | 625 | Res = ParseParenExpression(ParenExprType, false/*stopIfCastExr*/, |
Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 626 | isTypeCast, CastTy, RParenLoc); |
Chris Lattner | 932dff7 | 2009-12-10 02:08:07 +0000 | [diff] [blame] | 627 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 628 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 629 | switch (ParenExprType) { |
| 630 | case SimpleExpr: break; // Nothing else to do. |
| 631 | case CompoundStmt: break; // Nothing else to do. |
| 632 | case CompoundLiteral: |
| 633 | // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of |
| 634 | // postfix-expression exist, parse them now. |
| 635 | break; |
| 636 | case CastExpr: |
Argyrios Kyrtzidis | 0350ca5 | 2009-05-22 10:23:40 +0000 | [diff] [blame] | 637 | // We have parsed the cast-expression and no postfix-expr pieces are |
| 638 | // following. |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 639 | return move(Res); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 640 | } |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 641 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 642 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 643 | } |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 644 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 645 | // primary-expression |
| 646 | case tok::numeric_constant: |
| 647 | // constant: integer-constant |
| 648 | // constant: floating-constant |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 649 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 650 | Res = Actions.ActOnNumericConstant(Tok); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 651 | ConsumeToken(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 652 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 653 | |
| 654 | case tok::kw_true: |
| 655 | case tok::kw_false: |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 656 | return ParseCXXBoolLiteral(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 657 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 658 | case tok::kw_nullptr: |
Richard Smith | 841804b | 2011-10-17 23:06:20 +0000 | [diff] [blame] | 659 | Diag(Tok, diag::warn_cxx98_compat_nullptr); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 660 | return Actions.ActOnCXXNullPtrLiteral(ConsumeToken()); |
| 661 | |
Douglas Gregor | 5ecdd78 | 2011-04-27 06:18:01 +0000 | [diff] [blame] | 662 | case tok::annot_primary_expr: |
| 663 | assert(Res.get() == 0 && "Stray primary-expression annotation?"); |
| 664 | Res = getExprAnnotation(Tok); |
| 665 | ConsumeToken(); |
| 666 | break; |
| 667 | |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 668 | case tok::kw_decltype: |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 669 | case tok::identifier: { // primary-expression: identifier |
| 670 | // unqualified-id: identifier |
| 671 | // constant: enumeration-constant |
Chris Lattner | b31757b | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 672 | // Turn a potentially qualified name into a annot_typename or |
Chris Lattner | 74ba410 | 2009-01-04 22:52:14 +0000 | [diff] [blame] | 673 | // 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] | 674 | if (getLang().CPlusPlus) { |
John McCall | b672707 | 2010-01-07 19:29:58 +0000 | [diff] [blame] | 675 | // Avoid the unnecessary parse-time lookup in the common case |
| 676 | // where the syntax forbids a type. |
| 677 | const Token &Next = NextToken(); |
| 678 | if (Next.is(tok::coloncolon) || |
| 679 | (!ColonIsSacred && Next.is(tok::colon)) || |
| 680 | Next.is(tok::less) || |
Sebastian Redl | 62f13c9 | 2011-12-22 18:58:29 +0000 | [diff] [blame] | 681 | Next.is(tok::l_paren) || |
| 682 | Next.is(tok::l_brace)) { |
John McCall | b672707 | 2010-01-07 19:29:58 +0000 | [diff] [blame] | 683 | // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse. |
| 684 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 685 | return ExprError(); |
| 686 | if (!Tok.is(tok::identifier)) |
John McCall | b672707 | 2010-01-07 19:29:58 +0000 | [diff] [blame] | 687 | return ParseCastExpression(isUnaryExpression, isAddressOfOperand); |
| 688 | } |
Chris Lattner | a7bc7c8 | 2009-01-04 23:23:14 +0000 | [diff] [blame] | 689 | } |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 690 | |
Chris Lattner | b7c3fd7 | 2009-10-25 17:04:48 +0000 | [diff] [blame] | 691 | // Consume the identifier so that we can see if it is followed by a '(' or |
| 692 | // '.'. |
| 693 | IdentifierInfo &II = *Tok.getIdentifierInfo(); |
| 694 | SourceLocation ILoc = ConsumeToken(); |
| 695 | |
Chris Lattner | eb483eb | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 696 | // Support 'Class.property' and 'super.property' notation. |
Chris Lattner | b7c3fd7 | 2009-10-25 17:04:48 +0000 | [diff] [blame] | 697 | if (getLang().ObjC1 && Tok.is(tok::period) && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 698 | (Actions.getTypeName(II, ILoc, getCurScope()) || |
Chris Lattner | 236beab | 2010-04-12 06:20:33 +0000 | [diff] [blame] | 699 | // Allow the base to be 'super' if in an objc-method. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 700 | (&II == Ident_super && getCurScope()->isInObjcMethodScope()))) { |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 701 | ConsumeToken(); |
Chris Lattner | b7c3fd7 | 2009-10-25 17:04:48 +0000 | [diff] [blame] | 702 | |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 703 | if (Tok.isNot(tok::identifier)) { |
Chris Lattner | b7c3fd7 | 2009-10-25 17:04:48 +0000 | [diff] [blame] | 704 | Diag(Tok, diag::err_expected_property_name); |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 705 | return ExprError(); |
| 706 | } |
| 707 | IdentifierInfo &PropertyName = *Tok.getIdentifierInfo(); |
| 708 | SourceLocation PropertyLoc = ConsumeToken(); |
Chris Lattner | b7c3fd7 | 2009-10-25 17:04:48 +0000 | [diff] [blame] | 709 | |
| 710 | Res = Actions.ActOnClassPropertyRefExpr(II, PropertyName, |
| 711 | ILoc, PropertyLoc); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 712 | break; |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 713 | } |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 714 | |
Douglas Gregor | fa885c1 | 2010-09-15 15:09:43 +0000 | [diff] [blame] | 715 | // In an Objective-C method, if we have "super" followed by an identifier, |
Douglas Gregor | 78edf51 | 2010-09-15 16:23:04 +0000 | [diff] [blame] | 716 | // the token sequence is ill-formed. However, if there's a ':' or ']' after |
Douglas Gregor | fa885c1 | 2010-09-15 15:09:43 +0000 | [diff] [blame] | 717 | // that identifier, this is probably a message send with a missing open |
Douglas Gregor | 78edf51 | 2010-09-15 16:23:04 +0000 | [diff] [blame] | 718 | // bracket. Treat it as such. |
| 719 | if (getLang().ObjC1 && &II == Ident_super && !InMessageExpression && |
Douglas Gregor | fa885c1 | 2010-09-15 15:09:43 +0000 | [diff] [blame] | 720 | getCurScope()->isInObjcMethodScope() && |
Douglas Gregor | 78edf51 | 2010-09-15 16:23:04 +0000 | [diff] [blame] | 721 | ((Tok.is(tok::identifier) && |
| 722 | (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) || |
| 723 | Tok.is(tok::code_completion))) { |
Douglas Gregor | fa885c1 | 2010-09-15 15:09:43 +0000 | [diff] [blame] | 724 | Res = ParseObjCMessageExpressionBody(SourceLocation(), ILoc, ParsedType(), |
| 725 | 0); |
| 726 | break; |
| 727 | } |
| 728 | |
Douglas Gregor | 97d7ff0 | 2011-02-15 19:17:31 +0000 | [diff] [blame] | 729 | // If we have an Objective-C class name followed by an identifier |
| 730 | // and either ':' or ']', this is an Objective-C class message |
| 731 | // send that's missing the opening '['. Recovery |
| 732 | // appropriately. Also take this path if we're performing code |
| 733 | // completion after an Objective-C class name. |
| 734 | if (getLang().ObjC1 && |
| 735 | ((Tok.is(tok::identifier) && !InMessageExpression) || |
| 736 | Tok.is(tok::code_completion))) { |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 737 | const Token& Next = NextToken(); |
Douglas Gregor | 97d7ff0 | 2011-02-15 19:17:31 +0000 | [diff] [blame] | 738 | if (Tok.is(tok::code_completion) || |
| 739 | Next.is(tok::colon) || Next.is(tok::r_square)) |
Gabor Greif | 9fe871a | 2010-09-17 10:21:45 +0000 | [diff] [blame] | 740 | if (ParsedType Typ = Actions.getTypeName(II, ILoc, getCurScope())) |
| 741 | if (Typ.get()->isObjCObjectOrInterfaceType()) { |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 742 | // Fake up a Declarator to use with ActOnTypeName. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 743 | DeclSpec DS(AttrFactory); |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 744 | DS.SetRangeStart(ILoc); |
| 745 | DS.SetRangeEnd(ILoc); |
| 746 | const char *PrevSpec = 0; |
| 747 | unsigned DiagID; |
Gabor Greif | 9fe871a | 2010-09-17 10:21:45 +0000 | [diff] [blame] | 748 | DS.SetTypeSpecType(TST_typename, ILoc, PrevSpec, DiagID, Typ); |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 749 | |
| 750 | Declarator DeclaratorInfo(DS, Declarator::TypeNameContext); |
| 751 | TypeResult Ty = Actions.ActOnTypeName(getCurScope(), |
| 752 | DeclaratorInfo); |
| 753 | if (Ty.isInvalid()) |
| 754 | break; |
| 755 | |
| 756 | Res = ParseObjCMessageExpressionBody(SourceLocation(), |
| 757 | SourceLocation(), |
| 758 | Ty.get(), 0); |
| 759 | break; |
| 760 | } |
| 761 | } |
| 762 | |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 763 | // Make sure to pass down the right value for isAddressOfOperand. |
| 764 | if (isAddressOfOperand && isPostfixExpressionSuffixStart()) |
| 765 | isAddressOfOperand = false; |
Chris Lattner | b7c3fd7 | 2009-10-25 17:04:48 +0000 | [diff] [blame] | 766 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 767 | // Function designators are allowed to be undeclared (C99 6.5.1p2), so we |
| 768 | // need to know whether or not this identifier is a function designator or |
| 769 | // not. |
Douglas Gregor | 02a24ee | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 770 | UnqualifiedId Name; |
| 771 | CXXScopeSpec ScopeSpec; |
| 772 | Name.setIdentifier(&II, ILoc); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 773 | Res = Actions.ActOnIdExpression(getCurScope(), ScopeSpec, Name, |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 774 | Tok.is(tok::l_paren), isAddressOfOperand); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 775 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 776 | } |
| 777 | case tok::char_constant: // constant: character-constant |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 778 | case tok::wide_char_constant: |
| 779 | case tok::utf16_char_constant: |
| 780 | case tok::utf32_char_constant: |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 781 | Res = Actions.ActOnCharacterConstant(Tok); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 782 | ConsumeToken(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 783 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 784 | case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2] |
| 785 | case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU] |
| 786 | case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU] |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 787 | Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 788 | ConsumeToken(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 789 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 790 | case tok::string_literal: // primary-expression: string-literal |
| 791 | case tok::wide_string_literal: |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 792 | case tok::utf8_string_literal: |
| 793 | case tok::utf16_string_literal: |
| 794 | case tok::utf32_string_literal: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 795 | Res = ParseStringLiteralExpression(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 796 | break; |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 797 | case tok::kw__Generic: // primary-expression: generic-selection [C11 6.5.1] |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 798 | Res = ParseGenericSelectionExpression(); |
| 799 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 800 | case tok::kw___builtin_va_arg: |
| 801 | case tok::kw___builtin_offsetof: |
| 802 | case tok::kw___builtin_choose_expr: |
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 803 | case tok::kw___builtin_astype: // primary-expression: [OCL] as_type() |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 804 | return ParseBuiltinPrimaryExpression(); |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 805 | case tok::kw___null: |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 806 | return Actions.ActOnGNUNullExpr(ConsumeToken()); |
Chandler Carruth | 3c7fddd | 2011-07-08 04:59:44 +0000 | [diff] [blame] | 807 | |
Douglas Gregor | d420663 | 2010-08-06 14:50:36 +0000 | [diff] [blame] | 808 | case tok::plusplus: // unary-expression: '++' unary-expression [C99] |
| 809 | case tok::minusminus: { // unary-expression: '--' unary-expression [C99] |
| 810 | // C++ [expr.unary] has: |
| 811 | // unary-expression: |
| 812 | // ++ cast-expression |
| 813 | // -- cast-expression |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 814 | SourceLocation SavedLoc = ConsumeToken(); |
Douglas Gregor | d420663 | 2010-08-06 14:50:36 +0000 | [diff] [blame] | 815 | Res = ParseCastExpression(!getLang().CPlusPlus); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 816 | if (!Res.isInvalid()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 817 | Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get()); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 818 | return move(Res); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 819 | } |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 820 | case tok::amp: { // unary-expression: '&' cast-expression |
| 821 | // Special treatment because of member pointers |
| 822 | SourceLocation SavedLoc = ConsumeToken(); |
| 823 | Res = ParseCastExpression(false, true); |
| 824 | if (!Res.isInvalid()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 825 | Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get()); |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 826 | return move(Res); |
| 827 | } |
| 828 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 829 | case tok::star: // unary-expression: '*' cast-expression |
| 830 | case tok::plus: // unary-expression: '+' cast-expression |
| 831 | case tok::minus: // unary-expression: '-' cast-expression |
| 832 | case tok::tilde: // unary-expression: '~' cast-expression |
| 833 | case tok::exclaim: // unary-expression: '!' cast-expression |
| 834 | case tok::kw___real: // unary-expression: '__real' cast-expression [GNU] |
Chris Lattner | 3508084 | 2008-02-02 20:20:10 +0000 | [diff] [blame] | 835 | case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 836 | SourceLocation SavedLoc = ConsumeToken(); |
| 837 | Res = ParseCastExpression(false); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 838 | if (!Res.isInvalid()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 839 | Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get()); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 840 | return move(Res); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 841 | } |
| 842 | |
Chris Lattner | 3508084 | 2008-02-02 20:20:10 +0000 | [diff] [blame] | 843 | case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU] |
| 844 | // __extension__ silences extension warnings in the subexpression. |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 845 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
Chris Lattner | 3508084 | 2008-02-02 20:20:10 +0000 | [diff] [blame] | 846 | SourceLocation SavedLoc = ConsumeToken(); |
| 847 | Res = ParseCastExpression(false); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 848 | if (!Res.isInvalid()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 849 | Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get()); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 850 | return move(Res); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 851 | } |
| 852 | case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression |
| 853 | // unary-expression: 'sizeof' '(' type-name ')' |
Douglas Gregor | 85bb3da | 2008-11-06 15:17:27 +0000 | [diff] [blame] | 854 | case tok::kw_alignof: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 855 | case tok::kw___alignof: // unary-expression: '__alignof' unary-expression |
| 856 | // unary-expression: '__alignof' '(' type-name ')' |
Douglas Gregor | 85bb3da | 2008-11-06 15:17:27 +0000 | [diff] [blame] | 857 | // unary-expression: 'alignof' '(' type-id ')' |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 858 | case tok::kw_vec_step: // unary-expression: OpenCL 'vec_step' expression |
| 859 | return ParseUnaryExprOrTypeTraitExpression(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 860 | case tok::ampamp: { // unary-expression: '&&' identifier |
| 861 | SourceLocation AmpAmpLoc = ConsumeToken(); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 862 | if (Tok.isNot(tok::identifier)) |
| 863 | return ExprError(Diag(Tok, diag::err_expected_ident)); |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 864 | |
Chris Lattner | febb5b8 | 2011-02-18 21:16:39 +0000 | [diff] [blame] | 865 | if (getCurScope()->getFnParent() == 0) |
| 866 | return ExprError(Diag(Tok, diag::err_address_of_label_outside_fn)); |
| 867 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 868 | Diag(AmpAmpLoc, diag::ext_gnu_address_of_label); |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 869 | LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(), |
| 870 | Tok.getLocation()); |
| 871 | Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(), LD); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 872 | ConsumeToken(); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 873 | return move(Res); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 874 | } |
| 875 | case tok::kw_const_cast: |
| 876 | case tok::kw_dynamic_cast: |
| 877 | case tok::kw_reinterpret_cast: |
| 878 | case tok::kw_static_cast: |
Argyrios Kyrtzidis | b348b81 | 2008-08-16 19:45:32 +0000 | [diff] [blame] | 879 | Res = ParseCXXCasts(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 880 | break; |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 881 | case tok::kw_typeid: |
| 882 | Res = ParseCXXTypeid(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 883 | break; |
Francois Pichet | 01b7c30 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 884 | case tok::kw___uuidof: |
| 885 | Res = ParseCXXUuidof(); |
| 886 | break; |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 887 | case tok::kw_this: |
Argyrios Kyrtzidis | 289d773 | 2008-08-16 19:34:46 +0000 | [diff] [blame] | 888 | Res = ParseCXXThis(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 889 | break; |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 890 | |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 891 | case tok::annot_typename: |
| 892 | if (isStartOfObjCClassMessageMissingOpenBracket()) { |
| 893 | ParsedType Type = getTypeAnnotation(Tok); |
| 894 | |
| 895 | // Fake up a Declarator to use with ActOnTypeName. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 896 | DeclSpec DS(AttrFactory); |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 897 | DS.SetRangeStart(Tok.getLocation()); |
| 898 | DS.SetRangeEnd(Tok.getLastLoc()); |
| 899 | |
| 900 | const char *PrevSpec = 0; |
| 901 | unsigned DiagID; |
Nico Weber | 253e80b | 2010-11-22 10:30:56 +0000 | [diff] [blame] | 902 | DS.SetTypeSpecType(TST_typename, Tok.getAnnotationEndLoc(), |
| 903 | PrevSpec, DiagID, Type); |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 904 | |
| 905 | Declarator DeclaratorInfo(DS, Declarator::TypeNameContext); |
| 906 | TypeResult Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo); |
| 907 | if (Ty.isInvalid()) |
| 908 | break; |
| 909 | |
| 910 | ConsumeToken(); |
| 911 | Res = ParseObjCMessageExpressionBody(SourceLocation(), SourceLocation(), |
| 912 | Ty.get(), 0); |
| 913 | break; |
| 914 | } |
| 915 | // Fall through |
| 916 | |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 917 | case tok::kw_char: |
| 918 | case tok::kw_wchar_t: |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 919 | case tok::kw_char16_t: |
| 920 | case tok::kw_char32_t: |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 921 | case tok::kw_bool: |
| 922 | case tok::kw_short: |
| 923 | case tok::kw_int: |
| 924 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 925 | case tok::kw___int64: |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 926 | case tok::kw_signed: |
| 927 | case tok::kw_unsigned: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 928 | case tok::kw_half: |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 929 | case tok::kw_float: |
| 930 | case tok::kw_double: |
| 931 | case tok::kw_void: |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 932 | case tok::kw_typename: |
Chris Lattner | 2dcaab3 | 2009-01-04 22:28:21 +0000 | [diff] [blame] | 933 | case tok::kw_typeof: |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 934 | case tok::kw___vector: { |
Chris Lattner | 2dcaab3 | 2009-01-04 22:28:21 +0000 | [diff] [blame] | 935 | if (!getLang().CPlusPlus) { |
| 936 | Diag(Tok, diag::err_expected_expression); |
| 937 | return ExprError(); |
| 938 | } |
Eli Friedman | 2e0cdb4 | 2009-06-11 00:33:41 +0000 | [diff] [blame] | 939 | |
| 940 | if (SavedKind == tok::kw_typename) { |
| 941 | // postfix-expression: typename-specifier '(' expression-list[opt] ')' |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 942 | // typename-specifier braced-init-list |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 943 | if (TryAnnotateTypeOrScopeToken()) |
Eli Friedman | 2e0cdb4 | 2009-06-11 00:33:41 +0000 | [diff] [blame] | 944 | return ExprError(); |
| 945 | } |
| 946 | |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 947 | // postfix-expression: simple-type-specifier '(' expression-list[opt] ')' |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 948 | // simple-type-specifier braced-init-list |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 949 | // |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 950 | DeclSpec DS(AttrFactory); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 951 | ParseCXXSimpleTypeSpecifier(DS); |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 952 | if (Tok.isNot(tok::l_paren) && |
| 953 | (!getLang().CPlusPlus0x || Tok.isNot(tok::l_brace))) |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 954 | return ExprError(Diag(Tok, diag::err_expected_lparen_after_type) |
| 955 | << DS.getSourceRange()); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 956 | |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 957 | if (Tok.is(tok::l_brace)) |
| 958 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
| 959 | |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 960 | Res = ParseCXXTypeConstructExpression(DS); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 961 | break; |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 962 | } |
| 963 | |
Douglas Gregor | ae4c77d | 2010-02-05 19:11:37 +0000 | [diff] [blame] | 964 | case tok::annot_cxxscope: { // [C++] id-expression: qualified-id |
Douglas Gregor | 4074eef | 2010-04-23 02:08:13 +0000 | [diff] [blame] | 965 | // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse. |
| 966 | // (We can end up in this situation after tentative parsing.) |
| 967 | if (TryAnnotateTypeOrScopeToken()) |
| 968 | return ExprError(); |
| 969 | if (!Tok.is(tok::annot_cxxscope)) |
| 970 | return ParseCastExpression(isUnaryExpression, isAddressOfOperand, |
Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 971 | NotCastExpr, isTypeCast); |
Douglas Gregor | 4074eef | 2010-04-23 02:08:13 +0000 | [diff] [blame] | 972 | |
Douglas Gregor | ae4c77d | 2010-02-05 19:11:37 +0000 | [diff] [blame] | 973 | Token Next = NextToken(); |
| 974 | if (Next.is(tok::annot_template_id)) { |
Argyrios Kyrtzidis | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 975 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next); |
Douglas Gregor | ae4c77d | 2010-02-05 19:11:37 +0000 | [diff] [blame] | 976 | if (TemplateId->Kind == TNK_Type_template) { |
| 977 | // We have a qualified template-id that we know refers to a |
| 978 | // type, translate it into a type and continue parsing as a |
| 979 | // cast expression. |
| 980 | CXXScopeSpec SS; |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 981 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), |
| 982 | /*EnteringContext=*/false); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 983 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | ae4c77d | 2010-02-05 19:11:37 +0000 | [diff] [blame] | 984 | return ParseCastExpression(isUnaryExpression, isAddressOfOperand, |
Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 985 | NotCastExpr, isTypeCast); |
Douglas Gregor | ae4c77d | 2010-02-05 19:11:37 +0000 | [diff] [blame] | 986 | } |
| 987 | } |
| 988 | |
| 989 | // Parse as an id-expression. |
| 990 | Res = ParseCXXIdExpression(isAddressOfOperand); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 991 | break; |
Douglas Gregor | ae4c77d | 2010-02-05 19:11:37 +0000 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | case tok::annot_template_id: { // [C++] template-id |
Argyrios Kyrtzidis | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 995 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); |
Douglas Gregor | ae4c77d | 2010-02-05 19:11:37 +0000 | [diff] [blame] | 996 | if (TemplateId->Kind == TNK_Type_template) { |
| 997 | // We have a template-id that we know refers to a type, |
| 998 | // translate it into a type and continue parsing as a cast |
| 999 | // expression. |
| 1000 | AnnotateTemplateIdTokenAsType(); |
| 1001 | return ParseCastExpression(isUnaryExpression, isAddressOfOperand, |
Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 1002 | NotCastExpr, isTypeCast); |
Douglas Gregor | ae4c77d | 2010-02-05 19:11:37 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | // Fall through to treat the template-id as an id-expression. |
| 1006 | } |
| 1007 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1008 | case tok::kw_operator: // [C++] id-expression: operator/conversion-function-id |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 1009 | Res = ParseCXXIdExpression(isAddressOfOperand); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1010 | break; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 1011 | |
Chris Lattner | 74ba410 | 2009-01-04 22:52:14 +0000 | [diff] [blame] | 1012 | case tok::coloncolon: { |
Chris Lattner | 5b45473 | 2009-01-05 03:55:46 +0000 | [diff] [blame] | 1013 | // ::foo::bar -> global qualified name etc. If TryAnnotateTypeOrScopeToken |
| 1014 | // annotates the token, tail recurse. |
| 1015 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 1016 | return ExprError(); |
| 1017 | if (!Tok.is(tok::coloncolon)) |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 1018 | return ParseCastExpression(isUnaryExpression, isAddressOfOperand); |
| 1019 | |
Chris Lattner | 74ba410 | 2009-01-04 22:52:14 +0000 | [diff] [blame] | 1020 | // ::new -> [C++] new-expression |
| 1021 | // ::delete -> [C++] delete-expression |
Chris Lattner | 5b45473 | 2009-01-05 03:55:46 +0000 | [diff] [blame] | 1022 | SourceLocation CCLoc = ConsumeToken(); |
Chris Lattner | 59232d3 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 1023 | if (Tok.is(tok::kw_new)) |
Chris Lattner | 5b45473 | 2009-01-05 03:55:46 +0000 | [diff] [blame] | 1024 | return ParseCXXNewExpression(true, CCLoc); |
Chris Lattner | 74ba410 | 2009-01-04 22:52:14 +0000 | [diff] [blame] | 1025 | if (Tok.is(tok::kw_delete)) |
Chris Lattner | 5b45473 | 2009-01-05 03:55:46 +0000 | [diff] [blame] | 1026 | return ParseCXXDeleteExpression(true, CCLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1027 | |
Chris Lattner | a7bc7c8 | 2009-01-04 23:23:14 +0000 | [diff] [blame] | 1028 | // 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] | 1029 | Diag(CCLoc, diag::err_expected_expression); |
Chris Lattner | a7bc7c8 | 2009-01-04 23:23:14 +0000 | [diff] [blame] | 1030 | return ExprError(); |
Chris Lattner | 59232d3 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 1031 | } |
Sebastian Redl | fb4ccd7 | 2008-12-02 16:35:44 +0000 | [diff] [blame] | 1032 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1033 | case tok::kw_new: // [C++] new-expression |
Chris Lattner | 59232d3 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 1034 | return ParseCXXNewExpression(false, Tok.getLocation()); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1035 | |
| 1036 | case tok::kw_delete: // [C++] delete-expression |
Chris Lattner | 59232d3 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 1037 | return ParseCXXDeleteExpression(false, Tok.getLocation()); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1038 | |
Sebastian Redl | 02bc21a | 2010-09-10 20:55:37 +0000 | [diff] [blame] | 1039 | case tok::kw_noexcept: { // [C++0x] 'noexcept' '(' expression ')' |
Richard Smith | 841804b | 2011-10-17 23:06:20 +0000 | [diff] [blame] | 1040 | Diag(Tok, diag::warn_cxx98_compat_noexcept_expr); |
Sebastian Redl | 02bc21a | 2010-09-10 20:55:37 +0000 | [diff] [blame] | 1041 | SourceLocation KeyLoc = ConsumeToken(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1042 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1043 | |
| 1044 | if (T.expectAndConsume(diag::err_expected_lparen_after, "noexcept")) |
Sebastian Redl | 02bc21a | 2010-09-10 20:55:37 +0000 | [diff] [blame] | 1045 | return ExprError(); |
Richard Smith | f6702a3 | 2011-12-20 02:08:33 +0000 | [diff] [blame] | 1046 | // C++11 [expr.unary.noexcept]p1: |
Sebastian Redl | bd7c849 | 2010-09-10 21:57:27 +0000 | [diff] [blame] | 1047 | // The noexcept operator determines whether the evaluation of its operand, |
| 1048 | // which is an unevaluated operand, can throw an exception. |
| 1049 | EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated); |
Sebastian Redl | 02bc21a | 2010-09-10 20:55:37 +0000 | [diff] [blame] | 1050 | ExprResult Result = ParseExpression(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1051 | |
| 1052 | T.consumeClose(); |
| 1053 | |
Sebastian Redl | 02bc21a | 2010-09-10 20:55:37 +0000 | [diff] [blame] | 1054 | if (!Result.isInvalid()) |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1055 | Result = Actions.ActOnNoexceptExpr(KeyLoc, T.getOpenLocation(), |
| 1056 | Result.take(), T.getCloseLocation()); |
Sebastian Redl | 02bc21a | 2010-09-10 20:55:37 +0000 | [diff] [blame] | 1057 | return move(Result); |
| 1058 | } |
| 1059 | |
Chandler Carruth | 4e61ddd | 2011-04-23 10:47:20 +0000 | [diff] [blame] | 1060 | case tok::kw___is_abstract: // [GNU] unary-type-trait |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1061 | case tok::kw___is_class: |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 1062 | case tok::kw___is_empty: |
Chandler Carruth | 4e61ddd | 2011-04-23 10:47:20 +0000 | [diff] [blame] | 1063 | case tok::kw___is_enum: |
Sebastian Redl | ccf4350 | 2009-12-03 00:13:20 +0000 | [diff] [blame] | 1064 | case tok::kw___is_literal: |
John Wiegley | 20c0da7 | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 1065 | case tok::kw___is_arithmetic: |
| 1066 | case tok::kw___is_integral: |
| 1067 | case tok::kw___is_floating_point: |
| 1068 | case tok::kw___is_complete_type: |
| 1069 | case tok::kw___is_void: |
| 1070 | case tok::kw___is_array: |
| 1071 | case tok::kw___is_function: |
| 1072 | case tok::kw___is_reference: |
| 1073 | case tok::kw___is_lvalue_reference: |
| 1074 | case tok::kw___is_rvalue_reference: |
| 1075 | case tok::kw___is_fundamental: |
| 1076 | case tok::kw___is_object: |
| 1077 | case tok::kw___is_scalar: |
| 1078 | case tok::kw___is_compound: |
| 1079 | case tok::kw___is_pointer: |
| 1080 | case tok::kw___is_member_object_pointer: |
| 1081 | case tok::kw___is_member_function_pointer: |
| 1082 | case tok::kw___is_member_pointer: |
| 1083 | case tok::kw___is_const: |
| 1084 | case tok::kw___is_volatile: |
| 1085 | case tok::kw___is_standard_layout: |
| 1086 | case tok::kw___is_signed: |
| 1087 | case tok::kw___is_unsigned: |
Chandler Carruth | 3840281 | 2011-04-24 02:49:28 +0000 | [diff] [blame] | 1088 | case tok::kw___is_literal_type: |
Chandler Carruth | 4e61ddd | 2011-04-23 10:47:20 +0000 | [diff] [blame] | 1089 | case tok::kw___is_pod: |
| 1090 | case tok::kw___is_polymorphic: |
Chandler Carruth | b7e9589 | 2011-04-23 10:47:28 +0000 | [diff] [blame] | 1091 | case tok::kw___is_trivial: |
Sean Hunt | feb375d | 2011-05-13 00:31:07 +0000 | [diff] [blame] | 1092 | case tok::kw___is_trivially_copyable: |
Chandler Carruth | 4e61ddd | 2011-04-23 10:47:20 +0000 | [diff] [blame] | 1093 | case tok::kw___is_union: |
Douglas Gregor | 5e9392b | 2011-12-03 18:14:24 +0000 | [diff] [blame] | 1094 | case tok::kw___is_final: |
Anders Carlsson | 347ba89 | 2009-04-16 00:08:20 +0000 | [diff] [blame] | 1095 | case tok::kw___has_trivial_constructor: |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 1096 | case tok::kw___has_trivial_copy: |
| 1097 | case tok::kw___has_trivial_assign: |
Anders Carlsson | 072abef | 2009-04-17 02:34:54 +0000 | [diff] [blame] | 1098 | case tok::kw___has_trivial_destructor: |
Sebastian Redl | c238f09 | 2010-08-31 04:59:00 +0000 | [diff] [blame] | 1099 | case tok::kw___has_nothrow_assign: |
| 1100 | case tok::kw___has_nothrow_copy: |
| 1101 | case tok::kw___has_nothrow_constructor: |
Sebastian Redl | d4b25cb | 2010-09-02 23:19:42 +0000 | [diff] [blame] | 1102 | case tok::kw___has_virtual_destructor: |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1103 | return ParseUnaryTypeTrait(); |
| 1104 | |
Francois Pichet | f187237 | 2010-12-08 22:35:30 +0000 | [diff] [blame] | 1105 | case tok::kw___builtin_types_compatible_p: |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 1106 | case tok::kw___is_base_of: |
John Wiegley | 20c0da7 | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 1107 | case tok::kw___is_same: |
| 1108 | case tok::kw___is_convertible: |
Douglas Gregor | 9f36113 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 1109 | case tok::kw___is_convertible_to: |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 1110 | return ParseBinaryTypeTrait(); |
| 1111 | |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 1112 | case tok::kw___array_rank: |
| 1113 | case tok::kw___array_extent: |
| 1114 | return ParseArrayTypeTrait(); |
| 1115 | |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 1116 | case tok::kw___is_lvalue_expr: |
| 1117 | case tok::kw___is_rvalue_expr: |
| 1118 | return ParseExpressionTrait(); |
| 1119 | |
Chris Lattner | c97c204 | 2007-10-03 22:03:06 +0000 | [diff] [blame] | 1120 | case tok::at: { |
| 1121 | SourceLocation AtLoc = ConsumeToken(); |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 1122 | return ParseObjCAtExpression(AtLoc); |
Chris Lattner | c97c204 | 2007-10-03 22:03:06 +0000 | [diff] [blame] | 1123 | } |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1124 | case tok::caret: |
Chandler Carruth | bb39902 | 2011-07-08 04:28:55 +0000 | [diff] [blame] | 1125 | Res = ParseBlockLiteralExpression(); |
| 1126 | break; |
| 1127 | case tok::code_completion: { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1128 | Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1129 | cutOffParsing(); |
| 1130 | return ExprError(); |
Chandler Carruth | bb39902 | 2011-07-08 04:28:55 +0000 | [diff] [blame] | 1131 | } |
Chris Lattner | fdb548e | 2008-12-12 19:20:14 +0000 | [diff] [blame] | 1132 | case tok::l_square: |
Douglas Gregor | ae7902c | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 1133 | if (getLang().CPlusPlus0x) { |
| 1134 | if (getLang().ObjC1) { |
Eli Friedman | dc3b723 | 2012-01-04 02:40:39 +0000 | [diff] [blame^] | 1135 | // C++11 lambda expressions and Objective-C message sends both start with a |
| 1136 | // square bracket. There are three possibilities here: |
| 1137 | // we have a valid lambda expression, we have an invalid lambda |
| 1138 | // expression, or we have something that doesn't appear to be a lambda. |
| 1139 | // If we're in the last case, we fall back to ParseObjCMessageExpression. |
Douglas Gregor | ae7902c | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 1140 | Res = TryParseLambdaExpression(); |
Eli Friedman | dc3b723 | 2012-01-04 02:40:39 +0000 | [diff] [blame^] | 1141 | if (!Res.isInvalid() && !Res.get()) |
Douglas Gregor | ae7902c | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 1142 | Res = ParseObjCMessageExpression(); |
| 1143 | break; |
| 1144 | } |
| 1145 | Res = ParseLambdaExpression(); |
| 1146 | break; |
| 1147 | } |
Chandler Carruth | bb39902 | 2011-07-08 04:28:55 +0000 | [diff] [blame] | 1148 | if (getLang().ObjC1) { |
| 1149 | Res = ParseObjCMessageExpression(); |
| 1150 | break; |
| 1151 | } |
| 1152 | // FALL THROUGH. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1153 | default: |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1154 | NotCastExpr = true; |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 1155 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1156 | } |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 1157 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1158 | // These can be followed by postfix-expr pieces. |
Douglas Gregor | ac5fd84 | 2010-09-18 01:28:11 +0000 | [diff] [blame] | 1159 | return ParsePostfixExpressionSuffix(Res); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
| 1162 | /// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression |
| 1163 | /// is parsed, this method parses any suffixes that apply. |
| 1164 | /// |
| 1165 | /// postfix-expression: [C99 6.5.2] |
| 1166 | /// primary-expression |
| 1167 | /// postfix-expression '[' expression ']' |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1168 | /// postfix-expression '[' braced-init-list ']' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1169 | /// postfix-expression '(' argument-expression-list[opt] ')' |
| 1170 | /// postfix-expression '.' identifier |
| 1171 | /// postfix-expression '->' identifier |
| 1172 | /// postfix-expression '++' |
| 1173 | /// postfix-expression '--' |
| 1174 | /// '(' type-name ')' '{' initializer-list '}' |
| 1175 | /// '(' type-name ')' '{' initializer-list ',' '}' |
| 1176 | /// |
| 1177 | /// argument-expression-list: [C99 6.5.2] |
Douglas Gregor | dcaa1ca | 2011-01-03 19:31:53 +0000 | [diff] [blame] | 1178 | /// argument-expression ...[opt] |
| 1179 | /// argument-expression-list ',' assignment-expression ...[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1180 | /// |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1181 | ExprResult |
| 1182 | Parser::ParsePostfixExpressionSuffix(ExprResult LHS) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1183 | // Now that the primary-expression piece of the postfix-expression has been |
| 1184 | // parsed, see if there are any postfix-expression pieces here. |
| 1185 | SourceLocation Loc; |
| 1186 | while (1) { |
| 1187 | switch (Tok.getKind()) { |
Douglas Gregor | 78edf51 | 2010-09-15 16:23:04 +0000 | [diff] [blame] | 1188 | case tok::code_completion: |
| 1189 | if (InMessageExpression) |
| 1190 | return move(LHS); |
| 1191 | |
Douglas Gregor | ac5fd84 | 2010-09-18 01:28:11 +0000 | [diff] [blame] | 1192 | Actions.CodeCompletePostfixExpression(getCurScope(), LHS); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1193 | cutOffParsing(); |
| 1194 | return ExprError(); |
Douglas Gregor | 78edf51 | 2010-09-15 16:23:04 +0000 | [diff] [blame] | 1195 | |
Douglas Gregor | 0fbda68 | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 1196 | case tok::identifier: |
| 1197 | // If we see identifier: after an expression, and we're not already in a |
| 1198 | // message send, then this is probably a message send with a missing |
| 1199 | // opening bracket '['. |
| 1200 | if (getLang().ObjC1 && !InMessageExpression && |
Douglas Gregor | b65042d | 2010-09-15 14:54:45 +0000 | [diff] [blame] | 1201 | (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) { |
Douglas Gregor | 0fbda68 | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 1202 | LHS = ParseObjCMessageExpressionBody(SourceLocation(), SourceLocation(), |
| 1203 | ParsedType(), LHS.get()); |
| 1204 | break; |
| 1205 | } |
| 1206 | |
| 1207 | // Fall through; this isn't a message send. |
| 1208 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1209 | default: // Not a postfix-expression suffix. |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 1210 | return move(LHS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1211 | case tok::l_square: { // postfix-expression: p-e '[' expression ']' |
Chris Lattner | c59cb38 | 2010-05-31 18:18:22 +0000 | [diff] [blame] | 1212 | // If we have a array postfix expression that starts on a new line and |
| 1213 | // Objective-C is enabled, it is highly likely that the user forgot a |
| 1214 | // semicolon after the base expression and that the array postfix-expr is |
| 1215 | // actually another message send. In this case, do some look-ahead to see |
| 1216 | // if the contents of the square brackets are obviously not a valid |
| 1217 | // expression and recover by pretending there is no suffix. |
| 1218 | if (getLang().ObjC1 && Tok.isAtStartOfLine() && |
| 1219 | isSimpleObjCMessageExpression()) |
Douglas Gregor | 1b730e8 | 2010-05-31 14:40:22 +0000 | [diff] [blame] | 1220 | return move(LHS); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1221 | |
| 1222 | BalancedDelimiterTracker T(*this, tok::l_square); |
| 1223 | T.consumeOpen(); |
| 1224 | Loc = T.getOpenLocation(); |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1225 | ExprResult Idx; |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 1226 | if (getLang().CPlusPlus0x && Tok.is(tok::l_brace)) { |
| 1227 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1228 | Idx = ParseBraceInitializer(); |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 1229 | } else |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1230 | Idx = ParseExpression(); |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 1231 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1232 | SourceLocation RLoc = Tok.getLocation(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1233 | |
| 1234 | if (!LHS.isInvalid() && !Idx.isInvalid() && Tok.is(tok::r_square)) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1235 | LHS = Actions.ActOnArraySubscriptExpr(getCurScope(), LHS.take(), Loc, |
| 1236 | Idx.take(), RLoc); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1237 | } else |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 1238 | LHS = ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1239 | |
| 1240 | // Match the ']'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1241 | T.consumeClose(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1242 | break; |
| 1243 | } |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1244 | |
Peter Collingbourne | bf36e25 | 2011-02-09 21:12:02 +0000 | [diff] [blame] | 1245 | case tok::l_paren: // p-e: p-e '(' argument-expression-list[opt] ')' |
| 1246 | case tok::lesslessless: { // p-e: p-e '<<<' argument-expression-list '>>>' |
| 1247 | // '(' argument-expression-list[opt] ')' |
| 1248 | tok::TokenKind OpKind = Tok.getKind(); |
Douglas Gregor | 0fbda68 | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 1249 | InMessageExpressionRAIIObject InMessage(*this, false); |
| 1250 | |
Peter Collingbourne | bf36e25 | 2011-02-09 21:12:02 +0000 | [diff] [blame] | 1251 | Expr *ExecConfig = 0; |
| 1252 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1253 | BalancedDelimiterTracker LLLT(*this, tok::lesslessless); |
| 1254 | BalancedDelimiterTracker PT(*this, tok::l_paren); |
| 1255 | |
Peter Collingbourne | bf36e25 | 2011-02-09 21:12:02 +0000 | [diff] [blame] | 1256 | if (OpKind == tok::lesslessless) { |
| 1257 | ExprVector ExecConfigExprs(Actions); |
| 1258 | CommaLocsTy ExecConfigCommaLocs; |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1259 | LLLT.consumeOpen(); |
Peter Collingbourne | bf36e25 | 2011-02-09 21:12:02 +0000 | [diff] [blame] | 1260 | |
| 1261 | if (ParseExpressionList(ExecConfigExprs, ExecConfigCommaLocs)) { |
| 1262 | LHS = ExprError(); |
| 1263 | } |
| 1264 | |
| 1265 | if (LHS.isInvalid()) { |
| 1266 | SkipUntil(tok::greatergreatergreater); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1267 | } else if (LLLT.consumeClose()) { |
| 1268 | // There was an error closing the brackets |
Peter Collingbourne | bf36e25 | 2011-02-09 21:12:02 +0000 | [diff] [blame] | 1269 | LHS = ExprError(); |
Peter Collingbourne | bf36e25 | 2011-02-09 21:12:02 +0000 | [diff] [blame] | 1270 | } |
| 1271 | |
| 1272 | if (!LHS.isInvalid()) { |
| 1273 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen, "")) |
| 1274 | LHS = ExprError(); |
| 1275 | else |
| 1276 | Loc = PrevTokLocation; |
| 1277 | } |
| 1278 | |
| 1279 | if (!LHS.isInvalid()) { |
| 1280 | ExprResult ECResult = Actions.ActOnCUDAExecConfigExpr(getCurScope(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1281 | LLLT.getOpenLocation(), |
| 1282 | move_arg(ExecConfigExprs), |
| 1283 | LLLT.getCloseLocation()); |
Peter Collingbourne | bf36e25 | 2011-02-09 21:12:02 +0000 | [diff] [blame] | 1284 | if (ECResult.isInvalid()) |
| 1285 | LHS = ExprError(); |
| 1286 | else |
| 1287 | ExecConfig = ECResult.get(); |
| 1288 | } |
| 1289 | } else { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1290 | PT.consumeOpen(); |
| 1291 | Loc = PT.getOpenLocation(); |
Peter Collingbourne | bf36e25 | 2011-02-09 21:12:02 +0000 | [diff] [blame] | 1292 | } |
| 1293 | |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 1294 | ExprVector ArgExprs(Actions); |
Argyrios Kyrtzidis | 0cd5b42 | 2008-08-16 20:03:01 +0000 | [diff] [blame] | 1295 | CommaLocsTy CommaLocs; |
Douglas Gregor | ac5fd84 | 2010-09-18 01:28:11 +0000 | [diff] [blame] | 1296 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 1297 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1298 | Actions.CodeCompleteCall(getCurScope(), LHS.get(), 0, 0); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1299 | cutOffParsing(); |
| 1300 | return ExprError(); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 1301 | } |
Peter Collingbourne | bf36e25 | 2011-02-09 21:12:02 +0000 | [diff] [blame] | 1302 | |
| 1303 | if (OpKind == tok::l_paren || !LHS.isInvalid()) { |
| 1304 | if (Tok.isNot(tok::r_paren)) { |
| 1305 | if (ParseExpressionList(ArgExprs, CommaLocs, &Sema::CodeCompleteCall, |
| 1306 | LHS.get())) { |
Peter Collingbourne | bf36e25 | 2011-02-09 21:12:02 +0000 | [diff] [blame] | 1307 | LHS = ExprError(); |
| 1308 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1309 | } |
| 1310 | } |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 1311 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1312 | // Match the ')'. |
Douglas Gregor | ac5fd84 | 2010-09-18 01:28:11 +0000 | [diff] [blame] | 1313 | if (LHS.isInvalid()) { |
| 1314 | SkipUntil(tok::r_paren); |
| 1315 | } else if (Tok.isNot(tok::r_paren)) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1316 | PT.consumeClose(); |
Douglas Gregor | ac5fd84 | 2010-09-18 01:28:11 +0000 | [diff] [blame] | 1317 | LHS = ExprError(); |
| 1318 | } else { |
| 1319 | assert((ArgExprs.size() == 0 || |
| 1320 | ArgExprs.size()-1 == CommaLocs.size())&& |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1321 | "Unexpected number of commas!"); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1322 | LHS = Actions.ActOnCallExpr(getCurScope(), LHS.take(), Loc, |
Peter Collingbourne | bf36e25 | 2011-02-09 21:12:02 +0000 | [diff] [blame] | 1323 | move_arg(ArgExprs), Tok.getLocation(), |
| 1324 | ExecConfig); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1325 | PT.consumeClose(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1326 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1327 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1328 | break; |
| 1329 | } |
Douglas Gregor | 3b6afbb | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 1330 | case tok::arrow: |
| 1331 | case tok::period: { |
| 1332 | // postfix-expression: p-e '->' template[opt] id-expression |
| 1333 | // postfix-expression: p-e '.' template[opt] id-expression |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1334 | tok::TokenKind OpKind = Tok.getKind(); |
| 1335 | SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token. |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 1336 | |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 1337 | CXXScopeSpec SS; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1338 | ParsedType ObjectType; |
Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 1339 | bool MayBePseudoDestructor = false; |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 1340 | if (getLang().CPlusPlus && !LHS.isInvalid()) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1341 | LHS = Actions.ActOnStartCXXMemberReference(getCurScope(), LHS.take(), |
Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 1342 | OpLoc, OpKind, ObjectType, |
| 1343 | MayBePseudoDestructor); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 1344 | if (LHS.isInvalid()) |
| 1345 | break; |
Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 1346 | |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 1347 | ParseOptionalCXXScopeSpecifier(SS, ObjectType, |
| 1348 | /*EnteringContext=*/false, |
Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 1349 | &MayBePseudoDestructor); |
Douglas Gregor | 9f716e4 | 2010-05-27 15:25:59 +0000 | [diff] [blame] | 1350 | if (SS.isNotEmpty()) |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1351 | ObjectType = ParsedType(); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 1352 | } |
| 1353 | |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 1354 | if (Tok.is(tok::code_completion)) { |
| 1355 | // Code completion for a member access expression. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1356 | Actions.CodeCompleteMemberReferenceExpr(getCurScope(), LHS.get(), |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 1357 | OpLoc, OpKind == tok::arrow); |
| 1358 | |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1359 | cutOffParsing(); |
| 1360 | return ExprError(); |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 1361 | } |
| 1362 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1363 | if (MayBePseudoDestructor && !LHS.isInvalid()) { |
| 1364 | LHS = ParseCXXPseudoDestructor(LHS.take(), OpLoc, OpKind, SS, |
Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 1365 | ObjectType); |
| 1366 | break; |
| 1367 | } |
| 1368 | |
| 1369 | // Either the action has told is that this cannot be a |
| 1370 | // pseudo-destructor expression (based on the type of base |
| 1371 | // expression), or we didn't see a '~' in the right place. We |
| 1372 | // can still parse a destructor name here, but in that case it |
| 1373 | // names a real destructor. |
Francois Pichet | dbee341 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 1374 | // Allow explicit constructor calls in Microsoft mode. |
| 1375 | // FIXME: Add support for explicit call of template constructor. |
Douglas Gregor | 2d1c214 | 2009-11-03 19:44:04 +0000 | [diff] [blame] | 1376 | UnqualifiedId Name; |
| 1377 | if (ParseUnqualifiedId(SS, |
| 1378 | /*EnteringContext=*/false, |
| 1379 | /*AllowDestructorName=*/true, |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 1380 | /*AllowConstructorName=*/ getLang().MicrosoftExt, |
Douglas Gregor | 2d1c214 | 2009-11-03 19:44:04 +0000 | [diff] [blame] | 1381 | ObjectType, |
| 1382 | Name)) |
Douglas Gregor | ac5fd84 | 2010-09-18 01:28:11 +0000 | [diff] [blame] | 1383 | LHS = ExprError(); |
Douglas Gregor | 2d1c214 | 2009-11-03 19:44:04 +0000 | [diff] [blame] | 1384 | |
| 1385 | if (!LHS.isInvalid()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1386 | LHS = Actions.ActOnMemberAccessExpr(getCurScope(), LHS.take(), OpLoc, |
Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 1387 | OpKind, SS, Name, ObjCImpDecl, |
Douglas Gregor | 2d1c214 | 2009-11-03 19:44:04 +0000 | [diff] [blame] | 1388 | Tok.is(tok::l_paren)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1389 | break; |
| 1390 | } |
| 1391 | case tok::plusplus: // postfix-expression: postfix-expression '++' |
| 1392 | case tok::minusminus: // postfix-expression: postfix-expression '--' |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1393 | if (!LHS.isInvalid()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1394 | LHS = Actions.ActOnPostfixUnaryOp(getCurScope(), Tok.getLocation(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1395 | Tok.getKind(), LHS.take()); |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 1396 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1397 | ConsumeToken(); |
| 1398 | break; |
| 1399 | } |
| 1400 | } |
| 1401 | } |
| 1402 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 1403 | /// ParseExprAfterUnaryExprOrTypeTrait - We parsed a typeof/sizeof/alignof/ |
| 1404 | /// vec_step and we are at the start of an expression or a parenthesized |
| 1405 | /// type-id. OpTok is the operand token (typeof/sizeof/alignof). Returns the |
| 1406 | /// expression (isCastExpr == false) or the type (isCastExpr == true). |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 1407 | /// |
| 1408 | /// unary-expression: [C99 6.5.3] |
| 1409 | /// 'sizeof' unary-expression |
| 1410 | /// 'sizeof' '(' type-name ')' |
| 1411 | /// [GNU] '__alignof' unary-expression |
| 1412 | /// [GNU] '__alignof' '(' type-name ')' |
| 1413 | /// [C++0x] 'alignof' '(' type-id ')' |
| 1414 | /// |
| 1415 | /// [GNU] typeof-specifier: |
| 1416 | /// typeof ( expressions ) |
| 1417 | /// typeof ( type-name ) |
| 1418 | /// [GNU/C++] typeof unary-expression |
| 1419 | /// |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 1420 | /// [OpenCL 1.1 6.11.12] vec_step built-in function: |
| 1421 | /// vec_step ( expressions ) |
| 1422 | /// vec_step ( type-name ) |
| 1423 | /// |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1424 | ExprResult |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 1425 | Parser::ParseExprAfterUnaryExprOrTypeTrait(const Token &OpTok, |
| 1426 | bool &isCastExpr, |
| 1427 | ParsedType &CastTy, |
| 1428 | SourceRange &CastRange) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1429 | |
| 1430 | assert((OpTok.is(tok::kw_typeof) || OpTok.is(tok::kw_sizeof) || |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 1431 | OpTok.is(tok::kw___alignof) || OpTok.is(tok::kw_alignof) || |
| 1432 | OpTok.is(tok::kw_vec_step)) && |
| 1433 | "Not a typeof/sizeof/alignof/vec_step expression!"); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 1434 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1435 | ExprResult Operand; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1436 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 1437 | // If the operand doesn't start with an '(', it must be an expression. |
| 1438 | if (Tok.isNot(tok::l_paren)) { |
| 1439 | isCastExpr = false; |
| 1440 | if (OpTok.is(tok::kw_typeof) && !getLang().CPlusPlus) { |
| 1441 | Diag(Tok,diag::err_expected_lparen_after_id) << OpTok.getIdentifierInfo(); |
| 1442 | return ExprError(); |
| 1443 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1444 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1445 | // C++0x [expr.sizeof]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1446 | // [...] The operand is either an expression, which is an unevaluated |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1447 | // operand (Clause 5) [...] |
| 1448 | // |
Richard Smith | f6702a3 | 2011-12-20 02:08:33 +0000 | [diff] [blame] | 1449 | // The GNU typeof and GNU/C++11 alignof extensions also behave as |
| 1450 | // unevaluated operands. |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 1451 | EnterExpressionEvaluationContext Unevaluated(Actions, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1452 | Sema::Unevaluated); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 1453 | Operand = ParseCastExpression(true/*isUnaryExpression*/); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 1454 | } else { |
| 1455 | // If it starts with a '(', we know that it is either a parenthesized |
| 1456 | // type-name, or it is a unary-expression that starts with a compound |
| 1457 | // literal, or starts with a primary-expression that is a parenthesized |
| 1458 | // expression. |
| 1459 | ParenParseOption ExprType = CastExpr; |
| 1460 | SourceLocation LParenLoc = Tok.getLocation(), RParenLoc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1461 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1462 | // C++0x [expr.sizeof]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1463 | // [...] The operand is either an expression, which is an unevaluated |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1464 | // operand (Clause 5) [...] |
| 1465 | // |
Richard Smith | f6702a3 | 2011-12-20 02:08:33 +0000 | [diff] [blame] | 1466 | // The GNU typeof and GNU/C++11 alignof extensions also behave as |
| 1467 | // unevaluated operands. |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 1468 | EnterExpressionEvaluationContext Unevaluated(Actions, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1469 | Sema::Unevaluated); |
Fariborz Jahanian | f88f7ab | 2009-11-25 01:26:41 +0000 | [diff] [blame] | 1470 | Operand = ParseParenExpression(ExprType, true/*stopIfCastExpr*/, |
Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 1471 | false, CastTy, RParenLoc); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 1472 | CastRange = SourceRange(LParenLoc, RParenLoc); |
| 1473 | |
| 1474 | // If ParseParenExpression parsed a '(typename)' sequence only, then this is |
| 1475 | // a type. |
| 1476 | if (ExprType == CastExpr) { |
| 1477 | isCastExpr = true; |
| 1478 | return ExprEmpty(); |
| 1479 | } |
| 1480 | |
Douglas Gregor | 2a3a1bd | 2010-07-28 18:22:12 +0000 | [diff] [blame] | 1481 | if (getLang().CPlusPlus || OpTok.isNot(tok::kw_typeof)) { |
| 1482 | // GNU typeof in C requires the expression to be parenthesized. Not so for |
| 1483 | // sizeof/alignof or in C++. Therefore, the parenthesized expression is |
| 1484 | // the start of a unary-expression, but doesn't include any postfix |
| 1485 | // pieces. Parse these now if present. |
John McCall | 124300e | 2010-08-24 23:41:43 +0000 | [diff] [blame] | 1486 | if (!Operand.isInvalid()) |
| 1487 | Operand = ParsePostfixExpressionSuffix(Operand.get()); |
Douglas Gregor | 2a3a1bd | 2010-07-28 18:22:12 +0000 | [diff] [blame] | 1488 | } |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 1489 | } |
| 1490 | |
| 1491 | // If we get here, the operand to the typeof/sizeof/alignof was an expresion. |
| 1492 | isCastExpr = false; |
| 1493 | return move(Operand); |
| 1494 | } |
| 1495 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1496 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 1497 | /// ParseUnaryExprOrTypeTraitExpression - Parse a sizeof or alignof expression. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1498 | /// unary-expression: [C99 6.5.3] |
| 1499 | /// 'sizeof' unary-expression |
| 1500 | /// 'sizeof' '(' type-name ')' |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 1501 | /// [C++0x] 'sizeof' '...' '(' identifier ')' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1502 | /// [GNU] '__alignof' unary-expression |
| 1503 | /// [GNU] '__alignof' '(' type-name ')' |
Douglas Gregor | 85bb3da | 2008-11-06 15:17:27 +0000 | [diff] [blame] | 1504 | /// [C++0x] 'alignof' '(' type-id ')' |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 1505 | ExprResult Parser::ParseUnaryExprOrTypeTraitExpression() { |
Douglas Gregor | 85bb3da | 2008-11-06 15:17:27 +0000 | [diff] [blame] | 1506 | assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof) |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 1507 | || Tok.is(tok::kw_alignof) || Tok.is(tok::kw_vec_step)) && |
| 1508 | "Not a sizeof/alignof/vec_step expression!"); |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1509 | Token OpTok = Tok; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1510 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1511 | |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 1512 | // [C++0x] 'sizeof' '...' '(' identifier ')' |
| 1513 | if (Tok.is(tok::ellipsis) && OpTok.is(tok::kw_sizeof)) { |
| 1514 | SourceLocation EllipsisLoc = ConsumeToken(); |
| 1515 | SourceLocation LParenLoc, RParenLoc; |
| 1516 | IdentifierInfo *Name = 0; |
| 1517 | SourceLocation NameLoc; |
| 1518 | if (Tok.is(tok::l_paren)) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1519 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1520 | T.consumeOpen(); |
| 1521 | LParenLoc = T.getOpenLocation(); |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 1522 | if (Tok.is(tok::identifier)) { |
| 1523 | Name = Tok.getIdentifierInfo(); |
| 1524 | NameLoc = ConsumeToken(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1525 | T.consumeClose(); |
| 1526 | RParenLoc = T.getCloseLocation(); |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 1527 | if (RParenLoc.isInvalid()) |
| 1528 | RParenLoc = PP.getLocForEndOfToken(NameLoc); |
| 1529 | } else { |
| 1530 | Diag(Tok, diag::err_expected_parameter_pack); |
| 1531 | SkipUntil(tok::r_paren); |
| 1532 | } |
| 1533 | } else if (Tok.is(tok::identifier)) { |
| 1534 | Name = Tok.getIdentifierInfo(); |
| 1535 | NameLoc = ConsumeToken(); |
| 1536 | LParenLoc = PP.getLocForEndOfToken(EllipsisLoc); |
| 1537 | RParenLoc = PP.getLocForEndOfToken(NameLoc); |
| 1538 | Diag(LParenLoc, diag::err_paren_sizeof_parameter_pack) |
| 1539 | << Name |
| 1540 | << FixItHint::CreateInsertion(LParenLoc, "(") |
| 1541 | << FixItHint::CreateInsertion(RParenLoc, ")"); |
| 1542 | } else { |
| 1543 | Diag(Tok, diag::err_sizeof_parameter_pack); |
| 1544 | } |
| 1545 | |
| 1546 | if (!Name) |
| 1547 | return ExprError(); |
| 1548 | |
| 1549 | return Actions.ActOnSizeofParameterPackExpr(getCurScope(), |
| 1550 | OpTok.getLocation(), |
| 1551 | *Name, NameLoc, |
| 1552 | RParenLoc); |
| 1553 | } |
Richard Smith | 841804b | 2011-10-17 23:06:20 +0000 | [diff] [blame] | 1554 | |
| 1555 | if (OpTok.is(tok::kw_alignof)) |
| 1556 | Diag(OpTok, diag::warn_cxx98_compat_alignof); |
| 1557 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 1558 | bool isCastExpr; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1559 | ParsedType CastTy; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 1560 | SourceRange CastRange; |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 1561 | ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, |
| 1562 | isCastExpr, |
| 1563 | CastTy, |
| 1564 | CastRange); |
| 1565 | |
| 1566 | UnaryExprOrTypeTrait ExprKind = UETT_SizeOf; |
| 1567 | if (OpTok.is(tok::kw_alignof) || OpTok.is(tok::kw___alignof)) |
| 1568 | ExprKind = UETT_AlignOf; |
| 1569 | else if (OpTok.is(tok::kw_vec_step)) |
| 1570 | ExprKind = UETT_VecStep; |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1571 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 1572 | if (isCastExpr) |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 1573 | return Actions.ActOnUnaryExprOrTypeTraitExpr(OpTok.getLocation(), |
| 1574 | ExprKind, |
| 1575 | /*isType=*/true, |
| 1576 | CastTy.getAsOpaquePtr(), |
| 1577 | CastRange); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1578 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1579 | // 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] | 1580 | if (!Operand.isInvalid()) |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 1581 | Operand = Actions.ActOnUnaryExprOrTypeTraitExpr(OpTok.getLocation(), |
| 1582 | ExprKind, |
| 1583 | /*isType=*/false, |
| 1584 | Operand.release(), |
| 1585 | CastRange); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1586 | return move(Operand); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1587 | } |
| 1588 | |
| 1589 | /// ParseBuiltinPrimaryExpression |
| 1590 | /// |
| 1591 | /// primary-expression: [C99 6.5.1] |
| 1592 | /// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')' |
| 1593 | /// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')' |
| 1594 | /// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ',' |
| 1595 | /// assign-expr ')' |
| 1596 | /// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')' |
Peter Collingbourne | a160671 | 2011-11-05 03:47:48 +0000 | [diff] [blame] | 1597 | /// [OCL] '__builtin_astype' '(' assignment-expression ',' type-name ')' |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1598 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1599 | /// [GNU] offsetof-member-designator: |
| 1600 | /// [GNU] identifier |
| 1601 | /// [GNU] offsetof-member-designator '.' identifier |
| 1602 | /// [GNU] offsetof-member-designator '[' expression ']' |
| 1603 | /// |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1604 | ExprResult Parser::ParseBuiltinPrimaryExpression() { |
| 1605 | ExprResult Res; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1606 | const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo(); |
| 1607 | |
| 1608 | tok::TokenKind T = Tok.getKind(); |
| 1609 | SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier. |
| 1610 | |
| 1611 | // All of these start with an open paren. |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1612 | if (Tok.isNot(tok::l_paren)) |
| 1613 | return ExprError(Diag(Tok, diag::err_expected_lparen_after_id) |
| 1614 | << BuiltinII); |
| 1615 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1616 | BalancedDelimiterTracker PT(*this, tok::l_paren); |
| 1617 | PT.consumeOpen(); |
| 1618 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1619 | // TODO: Build AST. |
| 1620 | |
| 1621 | switch (T) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1622 | default: llvm_unreachable("Not a builtin primary expression!"); |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 1623 | case tok::kw___builtin_va_arg: { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1624 | ExprResult Expr(ParseAssignmentExpression()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1625 | |
| 1626 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) |
Douglas Gregor | ac5fd84 | 2010-09-18 01:28:11 +0000 | [diff] [blame] | 1627 | Expr = ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1628 | |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 1629 | TypeResult Ty = ParseTypeName(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1630 | |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 1631 | if (Tok.isNot(tok::r_paren)) { |
| 1632 | Diag(Tok, diag::err_expected_rparen); |
Douglas Gregor | ac5fd84 | 2010-09-18 01:28:11 +0000 | [diff] [blame] | 1633 | Expr = ExprError(); |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 1634 | } |
Douglas Gregor | ac5fd84 | 2010-09-18 01:28:11 +0000 | [diff] [blame] | 1635 | |
| 1636 | if (Expr.isInvalid() || Ty.isInvalid()) |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 1637 | Res = ExprError(); |
| 1638 | else |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1639 | Res = Actions.ActOnVAArg(StartLoc, Expr.take(), Ty.get(), ConsumeParen()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1640 | break; |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 1641 | } |
Chris Lattner | f9aa3cb | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 1642 | case tok::kw___builtin_offsetof: { |
Chris Lattner | 9fddf0a | 2007-08-30 17:08:45 +0000 | [diff] [blame] | 1643 | SourceLocation TypeLoc = Tok.getLocation(); |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 1644 | TypeResult Ty = ParseTypeName(); |
Chris Lattner | ca7102c | 2009-03-24 17:21:43 +0000 | [diff] [blame] | 1645 | if (Ty.isInvalid()) { |
| 1646 | SkipUntil(tok::r_paren); |
| 1647 | return ExprError(); |
| 1648 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1649 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1650 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1651 | return ExprError(); |
| 1652 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1653 | // We must have at least one identifier here. |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1654 | if (Tok.isNot(tok::identifier)) { |
Chris Lattner | f9aa3cb | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 1655 | Diag(Tok, diag::err_expected_ident); |
| 1656 | SkipUntil(tok::r_paren); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1657 | return ExprError(); |
Chris Lattner | f9aa3cb | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 1658 | } |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1659 | |
Chris Lattner | f9aa3cb | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 1660 | // Keep track of the various subcomponents we see. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1661 | SmallVector<Sema::OffsetOfComponent, 4> Comps; |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1662 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1663 | Comps.push_back(Sema::OffsetOfComponent()); |
Chris Lattner | f9aa3cb | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 1664 | Comps.back().isBrackets = false; |
| 1665 | Comps.back().U.IdentInfo = Tok.getIdentifierInfo(); |
| 1666 | Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1667 | |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 1668 | // FIXME: This loop leaks the index expressions on error. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1669 | while (1) { |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1670 | if (Tok.is(tok::period)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1671 | // offsetof-member-designator: offsetof-member-designator '.' identifier |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1672 | Comps.push_back(Sema::OffsetOfComponent()); |
Chris Lattner | f9aa3cb | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 1673 | Comps.back().isBrackets = false; |
| 1674 | Comps.back().LocStart = ConsumeToken(); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1675 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1676 | if (Tok.isNot(tok::identifier)) { |
Chris Lattner | f9aa3cb | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 1677 | Diag(Tok, diag::err_expected_ident); |
| 1678 | SkipUntil(tok::r_paren); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1679 | return ExprError(); |
Chris Lattner | f9aa3cb | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 1680 | } |
| 1681 | Comps.back().U.IdentInfo = Tok.getIdentifierInfo(); |
| 1682 | Comps.back().LocEnd = ConsumeToken(); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1683 | |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1684 | } else if (Tok.is(tok::l_square)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1685 | // offsetof-member-designator: offsetof-member-design '[' expression ']' |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1686 | Comps.push_back(Sema::OffsetOfComponent()); |
Chris Lattner | f9aa3cb | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 1687 | Comps.back().isBrackets = true; |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1688 | BalancedDelimiterTracker ST(*this, tok::l_square); |
| 1689 | ST.consumeOpen(); |
| 1690 | Comps.back().LocStart = ST.getOpenLocation(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1691 | Res = ParseExpression(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1692 | if (Res.isInvalid()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1693 | SkipUntil(tok::r_paren); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1694 | return move(Res); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1695 | } |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 1696 | Comps.back().U.E = Res.release(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1697 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1698 | ST.consumeClose(); |
| 1699 | Comps.back().LocEnd = ST.getCloseLocation(); |
Eli Friedman | 309fe0d | 2009-06-27 20:38:33 +0000 | [diff] [blame] | 1700 | } else { |
| 1701 | if (Tok.isNot(tok::r_paren)) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1702 | PT.consumeClose(); |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 1703 | Res = ExprError(); |
Eli Friedman | 309fe0d | 2009-06-27 20:38:33 +0000 | [diff] [blame] | 1704 | } else if (Ty.isInvalid()) { |
| 1705 | Res = ExprError(); |
| 1706 | } else { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1707 | PT.consumeClose(); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1708 | Res = Actions.ActOnBuiltinOffsetOf(getCurScope(), StartLoc, TypeLoc, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1709 | Ty.get(), &Comps[0], Comps.size(), |
| 1710 | PT.getCloseLocation()); |
Eli Friedman | 309fe0d | 2009-06-27 20:38:33 +0000 | [diff] [blame] | 1711 | } |
Chris Lattner | 6eb2109 | 2007-08-30 15:52:49 +0000 | [diff] [blame] | 1712 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1713 | } |
| 1714 | } |
| 1715 | break; |
Chris Lattner | f9aa3cb | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 1716 | } |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 1717 | case tok::kw___builtin_choose_expr: { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1718 | ExprResult Cond(ParseAssignmentExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1719 | if (Cond.isInvalid()) { |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 1720 | SkipUntil(tok::r_paren); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1721 | return move(Cond); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 1722 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1723 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1724 | return ExprError(); |
| 1725 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1726 | ExprResult Expr1(ParseAssignmentExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1727 | if (Expr1.isInvalid()) { |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 1728 | SkipUntil(tok::r_paren); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1729 | return move(Expr1); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1730 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1731 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1732 | return ExprError(); |
| 1733 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1734 | ExprResult Expr2(ParseAssignmentExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1735 | if (Expr2.isInvalid()) { |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 1736 | SkipUntil(tok::r_paren); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1737 | return move(Expr2); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1738 | } |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1739 | if (Tok.isNot(tok::r_paren)) { |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 1740 | Diag(Tok, diag::err_expected_rparen); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1741 | return ExprError(); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 1742 | } |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1743 | Res = Actions.ActOnChooseExpr(StartLoc, Cond.take(), Expr1.take(), |
| 1744 | Expr2.take(), ConsumeParen()); |
Chris Lattner | 6eb2109 | 2007-08-30 15:52:49 +0000 | [diff] [blame] | 1745 | break; |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 1746 | } |
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 1747 | case tok::kw___builtin_astype: { |
| 1748 | // The first argument is an expression to be converted, followed by a comma. |
| 1749 | ExprResult Expr(ParseAssignmentExpression()); |
| 1750 | if (Expr.isInvalid()) { |
| 1751 | SkipUntil(tok::r_paren); |
| 1752 | return ExprError(); |
| 1753 | } |
| 1754 | |
| 1755 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", |
| 1756 | tok::r_paren)) |
| 1757 | return ExprError(); |
| 1758 | |
| 1759 | // Second argument is the type to bitcast to. |
| 1760 | TypeResult DestTy = ParseTypeName(); |
| 1761 | if (DestTy.isInvalid()) |
| 1762 | return ExprError(); |
| 1763 | |
| 1764 | // Attempt to consume the r-paren. |
| 1765 | if (Tok.isNot(tok::r_paren)) { |
| 1766 | Diag(Tok, diag::err_expected_rparen); |
| 1767 | SkipUntil(tok::r_paren); |
| 1768 | return ExprError(); |
| 1769 | } |
| 1770 | |
| 1771 | Res = Actions.ActOnAsTypeExpr(Expr.take(), DestTy.get(), StartLoc, |
| 1772 | ConsumeParen()); |
| 1773 | break; |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 1774 | } |
Chandler Carruth | 3c7fddd | 2011-07-08 04:59:44 +0000 | [diff] [blame] | 1775 | } |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 1776 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1777 | if (Res.isInvalid()) |
| 1778 | return ExprError(); |
| 1779 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1780 | // These can be followed by postfix-expr pieces because they are |
| 1781 | // primary-expressions. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1782 | return ParsePostfixExpressionSuffix(Res.take()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1783 | } |
| 1784 | |
| 1785 | /// ParseParenExpression - This parses the unit that starts with a '(' token, |
| 1786 | /// 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] | 1787 | /// in ExprType. If stopIfCastExpr is true, it will only return the parsed type, |
| 1788 | /// not the parsed cast-expression. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1789 | /// |
| 1790 | /// primary-expression: [C99 6.5.1] |
| 1791 | /// '(' expression ')' |
| 1792 | /// [GNU] '(' compound-statement ')' (if !ParenExprOnly) |
| 1793 | /// postfix-expression: [C99 6.5.2] |
| 1794 | /// '(' type-name ')' '{' initializer-list '}' |
| 1795 | /// '(' type-name ')' '{' initializer-list ',' '}' |
| 1796 | /// cast-expression: [C99 6.5.4] |
| 1797 | /// '(' type-name ')' cast-expression |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1798 | /// [ARC] bridged-cast-expression |
| 1799 | /// |
| 1800 | /// [ARC] bridged-cast-expression: |
| 1801 | /// (__bridge type-name) cast-expression |
| 1802 | /// (__bridge_transfer type-name) cast-expression |
| 1803 | /// (__bridge_retained type-name) cast-expression |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1804 | ExprResult |
Argyrios Kyrtzidis | 0350ca5 | 2009-05-22 10:23:40 +0000 | [diff] [blame] | 1805 | Parser::ParseParenExpression(ParenParseOption &ExprType, bool stopIfCastExpr, |
Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 1806 | bool isTypeCast, ParsedType &CastTy, |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1807 | SourceLocation &RParenLoc) { |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1808 | assert(Tok.is(tok::l_paren) && "Not a paren expr!"); |
Douglas Gregor | f02da89 | 2009-02-09 21:04:56 +0000 | [diff] [blame] | 1809 | GreaterThanIsOperatorScope G(GreaterThanIsOperator, true); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1810 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1811 | if (T.consumeOpen()) |
| 1812 | return ExprError(); |
| 1813 | SourceLocation OpenLoc = T.getOpenLocation(); |
| 1814 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1815 | ExprResult Result(true); |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1816 | bool isAmbiguousTypeId; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1817 | CastTy = ParsedType(); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 1818 | |
Douglas Gregor | 0268810 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1819 | if (Tok.is(tok::code_completion)) { |
| 1820 | Actions.CodeCompleteOrdinaryName(getCurScope(), |
| 1821 | ExprType >= CompoundLiteral? Sema::PCC_ParenthesizedExpression |
| 1822 | : Sema::PCC_Expression); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1823 | cutOffParsing(); |
Douglas Gregor | 0268810 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1824 | return ExprError(); |
| 1825 | } |
John McCall | b3c4906 | 2011-04-06 02:35:25 +0000 | [diff] [blame] | 1826 | |
Fariborz Jahanian | 00852e4 | 2011-12-19 21:06:15 +0000 | [diff] [blame] | 1827 | // Diagnose use of bridge casts in non-arc mode. |
| 1828 | bool BridgeCast = (getLang().ObjC2 && |
| 1829 | (Tok.is(tok::kw___bridge) || |
| 1830 | Tok.is(tok::kw___bridge_transfer) || |
| 1831 | Tok.is(tok::kw___bridge_retained) || |
| 1832 | Tok.is(tok::kw___bridge_retain))); |
| 1833 | if (BridgeCast && !getLang().ObjCAutoRefCount) { |
Ted Kremenek | d9d12e0 | 2011-12-20 01:03:40 +0000 | [diff] [blame] | 1834 | StringRef BridgeCastName = Tok.getName(); |
Fariborz Jahanian | 00852e4 | 2011-12-19 21:06:15 +0000 | [diff] [blame] | 1835 | SourceLocation BridgeKeywordLoc = ConsumeToken(); |
| 1836 | if (!PP.getSourceManager().isInSystemHeader(BridgeKeywordLoc)) |
Ted Kremenek | d9d12e0 | 2011-12-20 01:03:40 +0000 | [diff] [blame] | 1837 | Diag(BridgeKeywordLoc, diag::err_arc_bridge_cast_nonarc) |
| 1838 | << BridgeCastName |
| 1839 | << FixItHint::CreateReplacement(BridgeKeywordLoc, ""); |
Fariborz Jahanian | 00852e4 | 2011-12-19 21:06:15 +0000 | [diff] [blame] | 1840 | BridgeCast = false; |
| 1841 | } |
| 1842 | |
John McCall | b3c4906 | 2011-04-06 02:35:25 +0000 | [diff] [blame] | 1843 | // None of these cases should fall through with an invalid Result |
| 1844 | // unless they've already reported an error. |
Chris Lattner | 4e1d99a | 2007-10-09 17:41:39 +0000 | [diff] [blame] | 1845 | if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1846 | Diag(Tok, diag::ext_gnu_statement_expr); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 1847 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1848 | StmtResult Stmt(ParseCompoundStatement(attrs, true)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1849 | ExprType = CompoundStmt; |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1850 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 1851 | // If the substmt parsed correctly, build the AST node. |
John McCall | b3c4906 | 2011-04-06 02:35:25 +0000 | [diff] [blame] | 1852 | if (!Stmt.isInvalid()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1853 | Result = Actions.ActOnStmtExpr(OpenLoc, Stmt.take(), Tok.getLocation()); |
Fariborz Jahanian | 00852e4 | 2011-12-19 21:06:15 +0000 | [diff] [blame] | 1854 | } else if (ExprType >= CompoundLiteral && BridgeCast) { |
John McCall | b64915a | 2011-06-17 21:56:12 +0000 | [diff] [blame] | 1855 | tok::TokenKind tokenKind = Tok.getKind(); |
| 1856 | SourceLocation BridgeKeywordLoc = ConsumeToken(); |
| 1857 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1858 | // Parse an Objective-C ARC ownership cast expression. |
| 1859 | ObjCBridgeCastKind Kind; |
John McCall | b64915a | 2011-06-17 21:56:12 +0000 | [diff] [blame] | 1860 | if (tokenKind == tok::kw___bridge) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1861 | Kind = OBC_Bridge; |
John McCall | b64915a | 2011-06-17 21:56:12 +0000 | [diff] [blame] | 1862 | else if (tokenKind == tok::kw___bridge_transfer) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1863 | Kind = OBC_BridgeTransfer; |
John McCall | b64915a | 2011-06-17 21:56:12 +0000 | [diff] [blame] | 1864 | else if (tokenKind == tok::kw___bridge_retained) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1865 | Kind = OBC_BridgeRetained; |
John McCall | b64915a | 2011-06-17 21:56:12 +0000 | [diff] [blame] | 1866 | else { |
| 1867 | // As a hopefully temporary workaround, allow __bridge_retain as |
| 1868 | // a synonym for __bridge_retained, but only in system headers. |
| 1869 | assert(tokenKind == tok::kw___bridge_retain); |
| 1870 | Kind = OBC_BridgeRetained; |
| 1871 | if (!PP.getSourceManager().isInSystemHeader(BridgeKeywordLoc)) |
| 1872 | Diag(BridgeKeywordLoc, diag::err_arc_bridge_retain) |
| 1873 | << FixItHint::CreateReplacement(BridgeKeywordLoc, |
| 1874 | "__bridge_retained"); |
| 1875 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1876 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1877 | TypeResult Ty = ParseTypeName(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1878 | T.consumeClose(); |
| 1879 | RParenLoc = T.getCloseLocation(); |
Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 1880 | ExprResult SubExpr = ParseCastExpression(/*isUnaryExpression=*/false); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1881 | |
| 1882 | if (Ty.isInvalid() || SubExpr.isInvalid()) |
| 1883 | return ExprError(); |
| 1884 | |
| 1885 | return Actions.ActOnObjCBridgedCast(getCurScope(), OpenLoc, Kind, |
| 1886 | BridgeKeywordLoc, Ty.get(), |
| 1887 | RParenLoc, SubExpr.get()); |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1888 | } else if (ExprType >= CompoundLiteral && |
| 1889 | isTypeIdInParens(isAmbiguousTypeId)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1890 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1891 | // Otherwise, this is a compound literal expression or cast expression. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1892 | |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1893 | // In C++, if the type-id is ambiguous we disambiguate based on context. |
| 1894 | // If stopIfCastExpr is true the context is a typeof/sizeof/alignof |
| 1895 | // in which case we should treat it as type-id. |
| 1896 | // if stopIfCastExpr is false, we need to determine the context past the |
| 1897 | // parens, so we defer to ParseCXXAmbiguousParenExpression for that. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1898 | if (isAmbiguousTypeId && !stopIfCastExpr) { |
| 1899 | ExprResult res = ParseCXXAmbiguousParenExpression(ExprType, CastTy, T); |
| 1900 | RParenLoc = T.getCloseLocation(); |
| 1901 | return res; |
| 1902 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1903 | |
Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 1904 | // Parse the type declarator. |
| 1905 | DeclSpec DS(AttrFactory); |
| 1906 | ParseSpecifierQualifierList(DS); |
| 1907 | Declarator DeclaratorInfo(DS, Declarator::TypeNameContext); |
| 1908 | ParseDeclarator(DeclaratorInfo); |
Douglas Gregor | 0fbda68 | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 1909 | |
Douglas Gregor | 77328d1 | 2010-09-15 23:19:31 +0000 | [diff] [blame] | 1910 | // If our type is followed by an identifier and either ':' or ']', then |
| 1911 | // this is probably an Objective-C message send where the leading '[' is |
| 1912 | // missing. Recover as if that were the case. |
Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 1913 | if (!DeclaratorInfo.isInvalidType() && Tok.is(tok::identifier) && |
| 1914 | !InMessageExpression && getLang().ObjC1 && |
| 1915 | (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) { |
| 1916 | TypeResult Ty; |
| 1917 | { |
| 1918 | InMessageExpressionRAIIObject InMessage(*this, false); |
| 1919 | Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo); |
| 1920 | } |
Douglas Gregor | 77328d1 | 2010-09-15 23:19:31 +0000 | [diff] [blame] | 1921 | Result = ParseObjCMessageExpressionBody(SourceLocation(), |
| 1922 | SourceLocation(), |
| 1923 | Ty.get(), 0); |
| 1924 | } else { |
| 1925 | // Match the ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1926 | T.consumeClose(); |
| 1927 | RParenLoc = T.getCloseLocation(); |
Douglas Gregor | 77328d1 | 2010-09-15 23:19:31 +0000 | [diff] [blame] | 1928 | if (Tok.is(tok::l_brace)) { |
| 1929 | ExprType = CompoundLiteral; |
Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 1930 | TypeResult Ty; |
| 1931 | { |
| 1932 | InMessageExpressionRAIIObject InMessage(*this, false); |
| 1933 | Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo); |
| 1934 | } |
Douglas Gregor | 77328d1 | 2010-09-15 23:19:31 +0000 | [diff] [blame] | 1935 | return ParseCompoundLiteralExpression(Ty.get(), OpenLoc, RParenLoc); |
Chris Lattner | eb483eb | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 1936 | } |
Argyrios Kyrtzidis | 0350ca5 | 2009-05-22 10:23:40 +0000 | [diff] [blame] | 1937 | |
Douglas Gregor | 77328d1 | 2010-09-15 23:19:31 +0000 | [diff] [blame] | 1938 | if (ExprType == CastExpr) { |
| 1939 | // We parsed '(' type-name ')' and the thing after it wasn't a '{'. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 1940 | |
Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 1941 | if (DeclaratorInfo.isInvalidType()) |
Douglas Gregor | 77328d1 | 2010-09-15 23:19:31 +0000 | [diff] [blame] | 1942 | return ExprError(); |
| 1943 | |
Douglas Gregor | 77328d1 | 2010-09-15 23:19:31 +0000 | [diff] [blame] | 1944 | // Note that this doesn't parse the subsequent cast-expression, it just |
| 1945 | // returns the parsed type to the callee. |
Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 1946 | if (stopIfCastExpr) { |
| 1947 | TypeResult Ty; |
| 1948 | { |
| 1949 | InMessageExpressionRAIIObject InMessage(*this, false); |
| 1950 | Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo); |
| 1951 | } |
| 1952 | CastTy = Ty.get(); |
Douglas Gregor | 77328d1 | 2010-09-15 23:19:31 +0000 | [diff] [blame] | 1953 | return ExprResult(); |
Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 1954 | } |
Douglas Gregor | 77328d1 | 2010-09-15 23:19:31 +0000 | [diff] [blame] | 1955 | |
| 1956 | // Reject the cast of super idiom in ObjC. |
| 1957 | if (Tok.is(tok::identifier) && getLang().ObjC1 && |
| 1958 | Tok.getIdentifierInfo() == Ident_super && |
| 1959 | getCurScope()->isInObjcMethodScope() && |
| 1960 | GetLookAheadToken(1).isNot(tok::period)) { |
| 1961 | Diag(Tok.getLocation(), diag::err_illegal_super_cast) |
| 1962 | << SourceRange(OpenLoc, RParenLoc); |
| 1963 | return ExprError(); |
| 1964 | } |
| 1965 | |
| 1966 | // Parse the cast-expression that follows it next. |
| 1967 | // TODO: For cast expression with CastTy. |
Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 1968 | Result = ParseCastExpression(/*isUnaryExpression=*/false, |
| 1969 | /*isAddressOfOperand=*/false, |
| 1970 | /*isTypeCast=*/true); |
| 1971 | if (!Result.isInvalid()) { |
| 1972 | Result = Actions.ActOnCastExpr(getCurScope(), OpenLoc, |
| 1973 | DeclaratorInfo, CastTy, |
Douglas Gregor | 77328d1 | 2010-09-15 23:19:31 +0000 | [diff] [blame] | 1974 | RParenLoc, Result.take()); |
Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 1975 | } |
Douglas Gregor | 77328d1 | 2010-09-15 23:19:31 +0000 | [diff] [blame] | 1976 | return move(Result); |
| 1977 | } |
| 1978 | |
| 1979 | Diag(Tok, diag::err_expected_lbrace_in_compound_literal); |
| 1980 | return ExprError(); |
| 1981 | } |
Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 1982 | } else if (isTypeCast) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1983 | // Parse the expression-list. |
Douglas Gregor | 0fbda68 | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 1984 | InMessageExpressionRAIIObject InMessage(*this, false); |
| 1985 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1986 | ExprVector ArgExprs(Actions); |
| 1987 | CommaLocsTy CommaLocs; |
| 1988 | |
| 1989 | if (!ParseExpressionList(ArgExprs, CommaLocs)) { |
| 1990 | ExprType = SimpleExpr; |
Fariborz Jahanian | f88f7ab | 2009-11-25 01:26:41 +0000 | [diff] [blame] | 1991 | Result = Actions.ActOnParenOrParenListExpr(OpenLoc, Tok.getLocation(), |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 1992 | move_arg(ArgExprs)); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1993 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1994 | } else { |
Douglas Gregor | 0fbda68 | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 1995 | InMessageExpressionRAIIObject InMessage(*this, false); |
| 1996 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1997 | Result = ParseExpression(); |
| 1998 | ExprType = SimpleExpr; |
John McCall | b3c4906 | 2011-04-06 02:35:25 +0000 | [diff] [blame] | 1999 | |
| 2000 | // Don't build a paren expression unless we actually match a ')'. |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 2001 | if (!Result.isInvalid() && Tok.is(tok::r_paren)) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2002 | Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), Result.take()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2003 | } |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 2004 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2005 | // Match the ')'. |
Chris Lattner | 42ece64 | 2008-12-12 06:00:12 +0000 | [diff] [blame] | 2006 | if (Result.isInvalid()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2007 | SkipUntil(tok::r_paren); |
Chris Lattner | 42ece64 | 2008-12-12 06:00:12 +0000 | [diff] [blame] | 2008 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2009 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2010 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2011 | T.consumeClose(); |
| 2012 | RParenLoc = T.getCloseLocation(); |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 2013 | return move(Result); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2014 | } |
| 2015 | |
Argyrios Kyrtzidis | d974a7b | 2009-05-22 10:24:05 +0000 | [diff] [blame] | 2016 | /// ParseCompoundLiteralExpression - We have parsed the parenthesized type-name |
| 2017 | /// and we are at the left brace. |
| 2018 | /// |
| 2019 | /// postfix-expression: [C99 6.5.2] |
| 2020 | /// '(' type-name ')' '{' initializer-list '}' |
| 2021 | /// '(' type-name ')' '{' initializer-list ',' '}' |
| 2022 | /// |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2023 | ExprResult |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2024 | Parser::ParseCompoundLiteralExpression(ParsedType Ty, |
Argyrios Kyrtzidis | d974a7b | 2009-05-22 10:24:05 +0000 | [diff] [blame] | 2025 | SourceLocation LParenLoc, |
| 2026 | SourceLocation RParenLoc) { |
| 2027 | assert(Tok.is(tok::l_brace) && "Not a compound literal!"); |
| 2028 | if (!getLang().C99) // Compound literals don't exist in C90. |
| 2029 | Diag(LParenLoc, diag::ext_c99_compound_literal); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2030 | ExprResult Result = ParseInitializer(); |
Argyrios Kyrtzidis | d974a7b | 2009-05-22 10:24:05 +0000 | [diff] [blame] | 2031 | if (!Result.isInvalid() && Ty) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2032 | return Actions.ActOnCompoundLiteral(LParenLoc, Ty, RParenLoc, Result.take()); |
Argyrios Kyrtzidis | d974a7b | 2009-05-22 10:24:05 +0000 | [diff] [blame] | 2033 | return move(Result); |
| 2034 | } |
| 2035 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2036 | /// ParseStringLiteralExpression - This handles the various token types that |
| 2037 | /// form string literals, and also handles string concatenation [C99 5.1.1.2, |
| 2038 | /// translation phase #6]. |
| 2039 | /// |
| 2040 | /// primary-expression: [C99 6.5.1] |
| 2041 | /// string-literal |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2042 | ExprResult Parser::ParseStringLiteralExpression() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2043 | assert(isTokenStringLiteral() && "Not a string literal!"); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 2044 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2045 | // String concat. Note that keywords like __func__ and __FUNCTION__ are not |
| 2046 | // considered to be strings for concatenation purposes. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2047 | SmallVector<Token, 4> StringToks; |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 2048 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2049 | do { |
| 2050 | StringToks.push_back(Tok); |
| 2051 | ConsumeStringToken(); |
| 2052 | } while (isTokenStringLiteral()); |
| 2053 | |
| 2054 | // Pass the set of string tokens, ready for concatenation, to the actions. |
Sean Hunt | 6cf7502 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 2055 | return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2056 | } |
Argyrios Kyrtzidis | 0cd5b42 | 2008-08-16 20:03:01 +0000 | [diff] [blame] | 2057 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2058 | /// ParseGenericSelectionExpression - Parse a C11 generic-selection |
| 2059 | /// [C11 6.5.1.1]. |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 2060 | /// |
| 2061 | /// generic-selection: |
| 2062 | /// _Generic ( assignment-expression , generic-assoc-list ) |
| 2063 | /// generic-assoc-list: |
| 2064 | /// generic-association |
| 2065 | /// generic-assoc-list , generic-association |
| 2066 | /// generic-association: |
| 2067 | /// type-name : assignment-expression |
| 2068 | /// default : assignment-expression |
| 2069 | ExprResult Parser::ParseGenericSelectionExpression() { |
| 2070 | assert(Tok.is(tok::kw__Generic) && "_Generic keyword expected"); |
| 2071 | SourceLocation KeyLoc = ConsumeToken(); |
| 2072 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2073 | if (!getLang().C11) |
| 2074 | Diag(KeyLoc, diag::ext_c11_generic_selection); |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 2075 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2076 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 2077 | if (T.expectAndConsume(diag::err_expected_lparen)) |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 2078 | return ExprError(); |
| 2079 | |
| 2080 | ExprResult ControllingExpr; |
| 2081 | { |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2082 | // C11 6.5.1.1p3 "The controlling expression of a generic selection is |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 2083 | // not evaluated." |
| 2084 | EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated); |
| 2085 | ControllingExpr = ParseAssignmentExpression(); |
| 2086 | if (ControllingExpr.isInvalid()) { |
| 2087 | SkipUntil(tok::r_paren); |
| 2088 | return ExprError(); |
| 2089 | } |
| 2090 | } |
| 2091 | |
| 2092 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "")) { |
| 2093 | SkipUntil(tok::r_paren); |
| 2094 | return ExprError(); |
| 2095 | } |
| 2096 | |
| 2097 | SourceLocation DefaultLoc; |
| 2098 | TypeVector Types(Actions); |
| 2099 | ExprVector Exprs(Actions); |
| 2100 | while (1) { |
| 2101 | ParsedType Ty; |
| 2102 | if (Tok.is(tok::kw_default)) { |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2103 | // C11 6.5.1.1p2 "A generic selection shall have no more than one default |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 2104 | // generic association." |
| 2105 | if (!DefaultLoc.isInvalid()) { |
| 2106 | Diag(Tok, diag::err_duplicate_default_assoc); |
| 2107 | Diag(DefaultLoc, diag::note_previous_default_assoc); |
| 2108 | SkipUntil(tok::r_paren); |
| 2109 | return ExprError(); |
| 2110 | } |
| 2111 | DefaultLoc = ConsumeToken(); |
| 2112 | Ty = ParsedType(); |
| 2113 | } else { |
| 2114 | ColonProtectionRAIIObject X(*this); |
| 2115 | TypeResult TR = ParseTypeName(); |
| 2116 | if (TR.isInvalid()) { |
| 2117 | SkipUntil(tok::r_paren); |
| 2118 | return ExprError(); |
| 2119 | } |
| 2120 | Ty = TR.release(); |
| 2121 | } |
| 2122 | Types.push_back(Ty); |
| 2123 | |
| 2124 | if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "")) { |
| 2125 | SkipUntil(tok::r_paren); |
| 2126 | return ExprError(); |
| 2127 | } |
| 2128 | |
| 2129 | // FIXME: These expressions should be parsed in a potentially potentially |
| 2130 | // evaluated context. |
| 2131 | ExprResult ER(ParseAssignmentExpression()); |
| 2132 | if (ER.isInvalid()) { |
| 2133 | SkipUntil(tok::r_paren); |
| 2134 | return ExprError(); |
| 2135 | } |
| 2136 | Exprs.push_back(ER.release()); |
| 2137 | |
| 2138 | if (Tok.isNot(tok::comma)) |
| 2139 | break; |
| 2140 | ConsumeToken(); |
| 2141 | } |
| 2142 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2143 | T.consumeClose(); |
| 2144 | if (T.getCloseLocation().isInvalid()) |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 2145 | return ExprError(); |
| 2146 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2147 | return Actions.ActOnGenericSelectionExpr(KeyLoc, DefaultLoc, |
| 2148 | T.getCloseLocation(), |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 2149 | ControllingExpr.release(), |
| 2150 | move_arg(Types), move_arg(Exprs)); |
| 2151 | } |
| 2152 | |
Argyrios Kyrtzidis | 0cd5b42 | 2008-08-16 20:03:01 +0000 | [diff] [blame] | 2153 | /// ParseExpressionList - Used for C/C++ (argument-)expression-list. |
| 2154 | /// |
| 2155 | /// argument-expression-list: |
| 2156 | /// assignment-expression |
| 2157 | /// argument-expression-list , assignment-expression |
| 2158 | /// |
| 2159 | /// [C++] expression-list: |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 2160 | /// [C++] assignment-expression |
| 2161 | /// [C++] expression-list , assignment-expression |
| 2162 | /// |
| 2163 | /// [C++0x] expression-list: |
| 2164 | /// [C++0x] initializer-list |
| 2165 | /// |
| 2166 | /// [C++0x] initializer-list |
| 2167 | /// [C++0x] initializer-clause ...[opt] |
| 2168 | /// [C++0x] initializer-list , initializer-clause ...[opt] |
| 2169 | /// |
| 2170 | /// [C++0x] initializer-clause: |
| 2171 | /// [C++0x] assignment-expression |
| 2172 | /// [C++0x] braced-init-list |
Argyrios Kyrtzidis | 0cd5b42 | 2008-08-16 20:03:01 +0000 | [diff] [blame] | 2173 | /// |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2174 | bool Parser::ParseExpressionList(SmallVectorImpl<Expr*> &Exprs, |
| 2175 | SmallVectorImpl<SourceLocation> &CommaLocs, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2176 | void (Sema::*Completer)(Scope *S, |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 2177 | Expr *Data, |
| 2178 | Expr **Args, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2179 | unsigned NumArgs), |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 2180 | Expr *Data) { |
Argyrios Kyrtzidis | 0cd5b42 | 2008-08-16 20:03:01 +0000 | [diff] [blame] | 2181 | while (1) { |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2182 | if (Tok.is(tok::code_completion)) { |
| 2183 | if (Completer) |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2184 | (Actions.*Completer)(getCurScope(), Data, Exprs.data(), Exprs.size()); |
Douglas Gregor | 4706e87 | 2011-02-17 03:09:23 +0000 | [diff] [blame] | 2185 | else |
| 2186 | Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2187 | cutOffParsing(); |
| 2188 | return true; |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2189 | } |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 2190 | |
| 2191 | ExprResult Expr; |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 2192 | if (getLang().CPlusPlus0x && Tok.is(tok::l_brace)) { |
| 2193 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 2194 | Expr = ParseBraceInitializer(); |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 2195 | } else |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 2196 | Expr = ParseAssignmentExpression(); |
| 2197 | |
Douglas Gregor | dcaa1ca | 2011-01-03 19:31:53 +0000 | [diff] [blame] | 2198 | if (Tok.is(tok::ellipsis)) |
| 2199 | Expr = Actions.ActOnPackExpansion(Expr.get(), ConsumeToken()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 2200 | if (Expr.isInvalid()) |
Argyrios Kyrtzidis | 0cd5b42 | 2008-08-16 20:03:01 +0000 | [diff] [blame] | 2201 | return true; |
Argyrios Kyrtzidis | 4fdc1ca | 2008-08-18 22:49:40 +0000 | [diff] [blame] | 2202 | |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 2203 | Exprs.push_back(Expr.release()); |
Argyrios Kyrtzidis | 0cd5b42 | 2008-08-16 20:03:01 +0000 | [diff] [blame] | 2204 | |
| 2205 | if (Tok.isNot(tok::comma)) |
| 2206 | return false; |
| 2207 | // Move to the next argument, remember where the comma was. |
| 2208 | CommaLocs.push_back(ConsumeToken()); |
| 2209 | } |
| 2210 | } |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 2211 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 2212 | /// ParseBlockId - Parse a block-id, which roughly looks like int (int x). |
| 2213 | /// |
| 2214 | /// [clang] block-id: |
| 2215 | /// [clang] specifier-qualifier-list block-declarator |
| 2216 | /// |
| 2217 | void Parser::ParseBlockId() { |
Douglas Gregor | 75ab414 | 2010-10-18 21:34:55 +0000 | [diff] [blame] | 2218 | if (Tok.is(tok::code_completion)) { |
| 2219 | Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Type); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2220 | return cutOffParsing(); |
Douglas Gregor | 75ab414 | 2010-10-18 21:34:55 +0000 | [diff] [blame] | 2221 | } |
| 2222 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 2223 | // Parse the specifier-qualifier-list piece. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2224 | DeclSpec DS(AttrFactory); |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 2225 | ParseSpecifierQualifierList(DS); |
| 2226 | |
| 2227 | // Parse the block-declarator. |
| 2228 | Declarator DeclaratorInfo(DS, Declarator::BlockLiteralContext); |
| 2229 | ParseDeclarator(DeclaratorInfo); |
Mike Stump | 19c30c0 | 2009-04-29 19:03:13 +0000 | [diff] [blame] | 2230 | |
Mike Stump | 6c92fa7 | 2009-04-29 21:40:37 +0000 | [diff] [blame] | 2231 | // We do this for: ^ __attribute__((noreturn)) {, as DS has the attributes. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2232 | DeclaratorInfo.takeAttributes(DS.getAttributes(), SourceLocation()); |
Mike Stump | 6c92fa7 | 2009-04-29 21:40:37 +0000 | [diff] [blame] | 2233 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2234 | MaybeParseGNUAttributes(DeclaratorInfo); |
Mike Stump | 19c30c0 | 2009-04-29 19:03:13 +0000 | [diff] [blame] | 2235 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 2236 | // Inform sema that we are starting a block. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2237 | Actions.ActOnBlockArguments(DeclaratorInfo, getCurScope()); |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 2238 | } |
| 2239 | |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 2240 | /// ParseBlockLiteralExpression - Parse a block literal, which roughly looks |
Steve Naroff | 17dab4f | 2008-09-16 23:11:46 +0000 | [diff] [blame] | 2241 | /// like ^(int x){ return x+1; } |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 2242 | /// |
| 2243 | /// block-literal: |
| 2244 | /// [clang] '^' block-args[opt] compound-statement |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 2245 | /// [clang] '^' block-id compound-statement |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 2246 | /// [clang] block-args: |
| 2247 | /// [clang] '(' parameter-list ')' |
| 2248 | /// |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2249 | ExprResult Parser::ParseBlockLiteralExpression() { |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 2250 | assert(Tok.is(tok::caret) && "block literal starts with ^"); |
| 2251 | SourceLocation CaretLoc = ConsumeToken(); |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 2252 | |
Chris Lattner | 6b91f00 | 2009-03-05 07:32:12 +0000 | [diff] [blame] | 2253 | PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), CaretLoc, |
| 2254 | "block literal parsing"); |
| 2255 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2256 | // Enter a scope to hold everything within the block. This includes the |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 2257 | // argument decls, decls within the compound expression, etc. This also |
| 2258 | // allows determining whether a variable reference inside the block is |
| 2259 | // within or outside of the block. |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2260 | ParseScope BlockScope(this, Scope::BlockScope | Scope::FnScope | |
| 2261 | Scope::BreakScope | Scope::ContinueScope | |
| 2262 | Scope::DeclScope); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 2263 | |
| 2264 | // Inform sema that we are starting a block. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2265 | Actions.ActOnBlockStart(CaretLoc, getCurScope()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2266 | |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 2267 | // Parse the return type if present. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2268 | DeclSpec DS(AttrFactory); |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 2269 | Declarator ParamInfo(DS, Declarator::BlockLiteralContext); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2270 | // FIXME: Since the return type isn't actually parsed, it can't be used to |
| 2271 | // fill ParamInfo with an initial valid range, so do it manually. |
| 2272 | ParamInfo.SetSourceRange(SourceRange(Tok.getLocation(), Tok.getLocation())); |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 2273 | |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 2274 | // If this block has arguments, parse them. There is no ambiguity here with |
| 2275 | // the expression case, because the expression case requires a parameter list. |
| 2276 | if (Tok.is(tok::l_paren)) { |
| 2277 | ParseParenDeclarator(ParamInfo); |
| 2278 | // Parse the pieces after the identifier as if we had "int(...)". |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2279 | // SetIdentifier sets the source range end, but in this case we're past |
| 2280 | // that location. |
| 2281 | SourceLocation Tmp = ParamInfo.getSourceRange().getEnd(); |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 2282 | ParamInfo.SetIdentifier(0, CaretLoc); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2283 | ParamInfo.SetRangeEnd(Tmp); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 2284 | if (ParamInfo.isInvalidType()) { |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 2285 | // If there was an error parsing the arguments, they may have |
| 2286 | // tried to use ^(x+y) which requires an argument list. Just |
| 2287 | // skip the whole block literal. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2288 | Actions.ActOnBlockError(CaretLoc, getCurScope()); |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 2289 | return ExprError(); |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 2290 | } |
Mike Stump | 19c30c0 | 2009-04-29 19:03:13 +0000 | [diff] [blame] | 2291 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2292 | MaybeParseGNUAttributes(ParamInfo); |
Mike Stump | 19c30c0 | 2009-04-29 19:03:13 +0000 | [diff] [blame] | 2293 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 2294 | // Inform sema that we are starting a block. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2295 | Actions.ActOnBlockArguments(ParamInfo, getCurScope()); |
Mike Stump | aa771a8 | 2009-04-14 18:24:37 +0000 | [diff] [blame] | 2296 | } else if (!Tok.is(tok::l_brace)) { |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 2297 | ParseBlockId(); |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 2298 | } else { |
| 2299 | // Otherwise, pretend we saw (void). |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2300 | ParsedAttributes attrs(AttrFactory); |
| 2301 | ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(true, false, |
Douglas Gregor | 965acbb | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 2302 | SourceLocation(), |
Sebastian Redl | 7dc8134 | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 2303 | 0, 0, 0, |
Douglas Gregor | 83f5172 | 2011-01-26 03:43:54 +0000 | [diff] [blame] | 2304 | true, SourceLocation(), |
Douglas Gregor | 90ebed0 | 2011-07-13 21:47:47 +0000 | [diff] [blame] | 2305 | SourceLocation(), |
Douglas Gregor | 43f5103 | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 2306 | SourceLocation(), |
| 2307 | SourceLocation(), |
Sebastian Redl | 6e5d319 | 2011-03-05 22:42:13 +0000 | [diff] [blame] | 2308 | EST_None, |
| 2309 | SourceLocation(), |
| 2310 | 0, 0, 0, 0, |
Argyrios Kyrtzidis | 82bf010 | 2009-08-19 23:14:54 +0000 | [diff] [blame] | 2311 | CaretLoc, CaretLoc, |
| 2312 | ParamInfo), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2313 | attrs, CaretLoc); |
Mike Stump | 19c30c0 | 2009-04-29 19:03:13 +0000 | [diff] [blame] | 2314 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2315 | MaybeParseGNUAttributes(ParamInfo); |
Mike Stump | 19c30c0 | 2009-04-29 19:03:13 +0000 | [diff] [blame] | 2316 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 2317 | // Inform sema that we are starting a block. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2318 | Actions.ActOnBlockArguments(ParamInfo, getCurScope()); |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 2319 | } |
| 2320 | |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 2321 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2322 | ExprResult Result(true); |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 2323 | if (!Tok.is(tok::l_brace)) { |
Fariborz Jahanian | ff03fbb | 2009-01-14 19:39:53 +0000 | [diff] [blame] | 2324 | // Saw something like: ^expr |
| 2325 | Diag(Tok, diag::err_expected_expression); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2326 | Actions.ActOnBlockError(CaretLoc, getCurScope()); |
Fariborz Jahanian | ff03fbb | 2009-01-14 19:39:53 +0000 | [diff] [blame] | 2327 | return ExprError(); |
| 2328 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2329 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2330 | StmtResult Stmt(ParseCompoundStatementBody()); |
Douglas Gregor | c9977d0 | 2011-03-16 17:05:57 +0000 | [diff] [blame] | 2331 | BlockScope.Exit(); |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 2332 | if (!Stmt.isInvalid()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2333 | Result = Actions.ActOnBlockStmtExpr(CaretLoc, Stmt.take(), getCurScope()); |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 2334 | else |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2335 | Actions.ActOnBlockError(CaretLoc, getCurScope()); |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 2336 | return move(Result); |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 2337 | } |