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