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