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