clang API Documentation
00001 //===--- ParseExpr.cpp - Expression Parsing -------------------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the Expression parsing implementation. Expressions in 00011 // C99 basically consist of a bunch of binary operators with unary operators and 00012 // other random stuff at the leaves. 00013 // 00014 // In the C99 grammar, these unary operators bind tightest and are represented 00015 // as the 'cast-expression' production. Everything else is either a binary 00016 // operator (e.g. '/') or a ternary operator ("?:"). The unary leaves are 00017 // handled by ParseCastExpression, the higher level pieces are handled by 00018 // ParseBinaryExpression. 00019 // 00020 //===----------------------------------------------------------------------===// 00021 00022 #include "clang/Parse/Parser.h" 00023 #include "clang/Parse/DeclSpec.h" 00024 #include "clang/Parse/Scope.h" 00025 #include "clang/Parse/Template.h" 00026 #include "clang/Basic/PrettyStackTrace.h" 00027 #include "RAIIObjectsForParser.h" 00028 #include "llvm/ADT/SmallVector.h" 00029 #include "llvm/ADT/SmallString.h" 00030 using namespace clang; 00031 00032 /// getBinOpPrecedence - Return the precedence of the specified binary operator 00033 /// token. This returns: 00034 /// 00035 static prec::Level getBinOpPrecedence(tok::TokenKind Kind, 00036 bool GreaterThanIsOperator, 00037 bool CPlusPlus0x) { 00038 switch (Kind) { 00039 case tok::greater: 00040 // C++ [temp.names]p3: 00041 // [...] When parsing a template-argument-list, the first 00042 // non-nested > is taken as the ending delimiter rather than a 00043 // greater-than operator. [...] 00044 if (GreaterThanIsOperator) 00045 return prec::Relational; 00046 return prec::Unknown; 00047 00048 case tok::greatergreater: 00049 // C++0x [temp.names]p3: 00050 // 00051 // [...] Similarly, the first non-nested >> is treated as two 00052 // consecutive but distinct > tokens, the first of which is 00053 // taken as the end of the template-argument-list and completes 00054 // the template-id. [...] 00055 if (GreaterThanIsOperator || !CPlusPlus0x) 00056 return prec::Shift; 00057 return prec::Unknown; 00058 00059 default: return prec::Unknown; 00060 case tok::comma: return prec::Comma; 00061 case tok::equal: 00062 case tok::starequal: 00063 case tok::slashequal: 00064 case tok::percentequal: 00065 case tok::plusequal: 00066 case tok::minusequal: 00067 case tok::lesslessequal: 00068 case tok::greatergreaterequal: 00069 case tok::ampequal: 00070 case tok::caretequal: 00071 case tok::pipeequal: return prec::Assignment; 00072 case tok::question: return prec::Conditional; 00073 case tok::pipepipe: return prec::LogicalOr; 00074 case tok::ampamp: return prec::LogicalAnd; 00075 case tok::pipe: return prec::InclusiveOr; 00076 case tok::caret: return prec::ExclusiveOr; 00077 case tok::amp: return prec::And; 00078 case tok::exclaimequal: 00079 case tok::equalequal: return prec::Equality; 00080 case tok::lessequal: 00081 case tok::less: 00082 case tok::greaterequal: return prec::Relational; 00083 case tok::lessless: return prec::Shift; 00084 case tok::plus: 00085 case tok::minus: return prec::Additive; 00086 case tok::percent: 00087 case tok::slash: 00088 case tok::star: return prec::Multiplicative; 00089 case tok::periodstar: 00090 case tok::arrowstar: return prec::PointerToMember; 00091 } 00092 } 00093 00094 00095 /// ParseExpression - Simple precedence-based parser for binary/ternary 00096 /// operators. 00097 /// 00098 /// Note: we diverge from the C99 grammar when parsing the assignment-expression 00099 /// production. C99 specifies that the LHS of an assignment operator should be 00100 /// parsed as a unary-expression, but consistency dictates that it be a 00101 /// conditional-expession. In practice, the important thing here is that the 00102 /// LHS of an assignment has to be an l-value, which productions between 00103 /// unary-expression and conditional-expression don't produce. Because we want 00104 /// consistency, we parse the LHS as a conditional-expression, then check for 00105 /// l-value-ness in semantic analysis stages. 00106 /// 00107 /// pm-expression: [C++ 5.5] 00108 /// cast-expression 00109 /// pm-expression '.*' cast-expression 00110 /// pm-expression '->*' cast-expression 00111 /// 00112 /// multiplicative-expression: [C99 6.5.5] 00113 /// Note: in C++, apply pm-expression instead of cast-expression 00114 /// cast-expression 00115 /// multiplicative-expression '*' cast-expression 00116 /// multiplicative-expression '/' cast-expression 00117 /// multiplicative-expression '%' cast-expression 00118 /// 00119 /// additive-expression: [C99 6.5.6] 00120 /// multiplicative-expression 00121 /// additive-expression '+' multiplicative-expression 00122 /// additive-expression '-' multiplicative-expression 00123 /// 00124 /// shift-expression: [C99 6.5.7] 00125 /// additive-expression 00126 /// shift-expression '<<' additive-expression 00127 /// shift-expression '>>' additive-expression 00128 /// 00129 /// relational-expression: [C99 6.5.8] 00130 /// shift-expression 00131 /// relational-expression '<' shift-expression 00132 /// relational-expression '>' shift-expression 00133 /// relational-expression '<=' shift-expression 00134 /// relational-expression '>=' shift-expression 00135 /// 00136 /// equality-expression: [C99 6.5.9] 00137 /// relational-expression 00138 /// equality-expression '==' relational-expression 00139 /// equality-expression '!=' relational-expression 00140 /// 00141 /// AND-expression: [C99 6.5.10] 00142 /// equality-expression 00143 /// AND-expression '&' equality-expression 00144 /// 00145 /// exclusive-OR-expression: [C99 6.5.11] 00146 /// AND-expression 00147 /// exclusive-OR-expression '^' AND-expression 00148 /// 00149 /// inclusive-OR-expression: [C99 6.5.12] 00150 /// exclusive-OR-expression 00151 /// inclusive-OR-expression '|' exclusive-OR-expression 00152 /// 00153 /// logical-AND-expression: [C99 6.5.13] 00154 /// inclusive-OR-expression 00155 /// logical-AND-expression '&&' inclusive-OR-expression 00156 /// 00157 /// logical-OR-expression: [C99 6.5.14] 00158 /// logical-AND-expression 00159 /// logical-OR-expression '||' logical-AND-expression 00160 /// 00161 /// conditional-expression: [C99 6.5.15] 00162 /// logical-OR-expression 00163 /// logical-OR-expression '?' expression ':' conditional-expression 00164 /// [GNU] logical-OR-expression '?' ':' conditional-expression 00165 /// [C++] the third operand is an assignment-expression 00166 /// 00167 /// assignment-expression: [C99 6.5.16] 00168 /// conditional-expression 00169 /// unary-expression assignment-operator assignment-expression 00170 /// [C++] throw-expression [C++ 15] 00171 /// 00172 /// assignment-operator: one of 00173 /// = *= /= %= += -= <<= >>= &= ^= |= 00174 /// 00175 /// expression: [C99 6.5.17] 00176 /// assignment-expression 00177 /// expression ',' assignment-expression 00178 /// 00179 Parser::OwningExprResult Parser::ParseExpression() { 00180 OwningExprResult LHS(ParseAssignmentExpression()); 00181 if (LHS.isInvalid()) return move(LHS); 00182 00183 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma); 00184 } 00185 00186 /// This routine is called when the '@' is seen and consumed. 00187 /// Current token is an Identifier and is not a 'try'. This 00188 /// routine is necessary to disambiguate @try-statement from, 00189 /// for example, @encode-expression. 00190 /// 00191 Parser::OwningExprResult 00192 Parser::ParseExpressionWithLeadingAt(SourceLocation AtLoc) { 00193 OwningExprResult LHS(ParseObjCAtExpression(AtLoc)); 00194 if (LHS.isInvalid()) return move(LHS); 00195 00196 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma); 00197 } 00198 00199 /// This routine is called when a leading '__extension__' is seen and 00200 /// consumed. This is necessary because the token gets consumed in the 00201 /// process of disambiguating between an expression and a declaration. 00202 Parser::OwningExprResult 00203 Parser::ParseExpressionWithLeadingExtension(SourceLocation ExtLoc) { 00204 OwningExprResult LHS(Actions, true); 00205 { 00206 // Silence extension warnings in the sub-expression 00207 ExtensionRAIIObject O(Diags); 00208 00209 LHS = ParseCastExpression(false); 00210 if (LHS.isInvalid()) return move(LHS); 00211 } 00212 00213 LHS = Actions.ActOnUnaryOp(CurScope, ExtLoc, tok::kw___extension__, 00214 move(LHS)); 00215 if (LHS.isInvalid()) return move(LHS); 00216 00217 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma); 00218 } 00219 00220 /// ParseAssignmentExpression - Parse an expr that doesn't include commas. 00221 /// 00222 Parser::OwningExprResult Parser::ParseAssignmentExpression() { 00223 if (Tok.is(tok::code_completion)) { 00224 Actions.CodeCompleteOrdinaryName(CurScope, Action::CCC_Expression); 00225 ConsumeToken(); 00226 } 00227 00228 if (Tok.is(tok::kw_throw)) 00229 return ParseThrowExpression(); 00230 00231 OwningExprResult LHS(ParseCastExpression(false)); 00232 if (LHS.isInvalid()) return move(LHS); 00233 00234 return ParseRHSOfBinaryExpression(move(LHS), prec::Assignment); 00235 } 00236 00237 /// ParseAssignmentExprWithObjCMessageExprStart - Parse an assignment expression 00238 /// where part of an objc message send has already been parsed. In this case 00239 /// LBracLoc indicates the location of the '[' of the message send, and either 00240 /// ReceiverName or ReceiverExpr is non-null indicating the receiver of the 00241 /// message. 00242 /// 00243 /// Since this handles full assignment-expression's, it handles postfix 00244 /// expressions and other binary operators for these expressions as well. 00245 Parser::OwningExprResult 00246 Parser::ParseAssignmentExprWithObjCMessageExprStart(SourceLocation LBracLoc, 00247 SourceLocation SuperLoc, 00248 TypeTy *ReceiverType, 00249 ExprArg ReceiverExpr) { 00250 OwningExprResult R(ParseObjCMessageExpressionBody(LBracLoc, SuperLoc, 00251 ReceiverType, 00252 move(ReceiverExpr))); 00253 if (R.isInvalid()) return move(R); 00254 R = ParsePostfixExpressionSuffix(move(R)); 00255 if (R.isInvalid()) return move(R); 00256 return ParseRHSOfBinaryExpression(move(R), prec::Assignment); 00257 } 00258 00259 00260 Parser::OwningExprResult Parser::ParseConstantExpression() { 00261 // C++ [basic.def.odr]p2: 00262 // An expression is potentially evaluated unless it appears where an 00263 // integral constant expression is required (see 5.19) [...]. 00264 EnterExpressionEvaluationContext Unevaluated(Actions, 00265 Action::Unevaluated); 00266 00267 OwningExprResult LHS(ParseCastExpression(false)); 00268 if (LHS.isInvalid()) return move(LHS); 00269 00270 return ParseRHSOfBinaryExpression(move(LHS), prec::Conditional); 00271 } 00272 00273 /// ParseRHSOfBinaryExpression - Parse a binary expression that starts with 00274 /// LHS and has a precedence of at least MinPrec. 00275 Parser::OwningExprResult 00276 Parser::ParseRHSOfBinaryExpression(OwningExprResult LHS, prec::Level MinPrec) { 00277 prec::Level NextTokPrec = getBinOpPrecedence(Tok.getKind(), 00278 GreaterThanIsOperator, 00279 getLang().CPlusPlus0x); 00280 SourceLocation ColonLoc; 00281 00282 while (1) { 00283 // If this token has a lower precedence than we are allowed to parse (e.g. 00284 // because we are called recursively, or because the token is not a binop), 00285 // then we are done! 00286 if (NextTokPrec < MinPrec) 00287 return move(LHS); 00288 00289 // Consume the operator, saving the operator token for error reporting. 00290 Token OpToken = Tok; 00291 ConsumeToken(); 00292 00293 // Special case handling for the ternary operator. 00294 OwningExprResult TernaryMiddle(Actions, true); 00295 if (NextTokPrec == prec::Conditional) { 00296 if (Tok.isNot(tok::colon)) { 00297 // Don't parse FOO:BAR as if it were a typo for FOO::BAR. 00298 ColonProtectionRAIIObject X(*this); 00299 00300 // Handle this production specially: 00301 // logical-OR-expression '?' expression ':' conditional-expression 00302 // In particular, the RHS of the '?' is 'expression', not 00303 // 'logical-OR-expression' as we might expect. 00304 TernaryMiddle = ParseExpression(); 00305 if (TernaryMiddle.isInvalid()) 00306 return move(TernaryMiddle); 00307 } else { 00308 // Special case handling of "X ? Y : Z" where Y is empty: 00309 // logical-OR-expression '?' ':' conditional-expression [GNU] 00310 TernaryMiddle = 0; 00311 Diag(Tok, diag::ext_gnu_conditional_expr); 00312 } 00313 00314 if (Tok.is(tok::colon)) { 00315 // Eat the colon. 00316 ColonLoc = ConsumeToken(); 00317 } else { 00318 Diag(Tok, diag::err_expected_colon) 00319 << FixItHint::CreateInsertion(Tok.getLocation(), ": "); 00320 Diag(OpToken, diag::note_matching) << "?"; 00321 ColonLoc = Tok.getLocation(); 00322 } 00323 } 00324 00325 // Parse another leaf here for the RHS of the operator. 00326 // ParseCastExpression works here because all RHS expressions in C have it 00327 // as a prefix, at least. However, in C++, an assignment-expression could 00328 // be a throw-expression, which is not a valid cast-expression. 00329 // Therefore we need some special-casing here. 00330 // Also note that the third operand of the conditional operator is 00331 // an assignment-expression in C++. 00332 OwningExprResult RHS(Actions); 00333 if (getLang().CPlusPlus && NextTokPrec <= prec::Conditional) 00334 RHS = ParseAssignmentExpression(); 00335 else 00336 RHS = ParseCastExpression(false); 00337 if (RHS.isInvalid()) 00338 return move(RHS); 00339 00340 // Remember the precedence of this operator and get the precedence of the 00341 // operator immediately to the right of the RHS. 00342 prec::Level ThisPrec = NextTokPrec; 00343 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator, 00344 getLang().CPlusPlus0x); 00345 00346 // Assignment and conditional expressions are right-associative. 00347 bool isRightAssoc = ThisPrec == prec::Conditional || 00348 ThisPrec == prec::Assignment; 00349 00350 // Get the precedence of the operator to the right of the RHS. If it binds 00351 // more tightly with RHS than we do, evaluate it completely first. 00352 if (ThisPrec < NextTokPrec || 00353 (ThisPrec == NextTokPrec && isRightAssoc)) { 00354 // If this is left-associative, only parse things on the RHS that bind 00355 // more tightly than the current operator. If it is left-associative, it 00356 // is okay, to bind exactly as tightly. For example, compile A=B=C=D as 00357 // A=(B=(C=D)), where each paren is a level of recursion here. 00358 // The function takes ownership of the RHS. 00359 RHS = ParseRHSOfBinaryExpression(move(RHS), 00360 static_cast<prec::Level>(ThisPrec + !isRightAssoc)); 00361 if (RHS.isInvalid()) 00362 return move(RHS); 00363 00364 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator, 00365 getLang().CPlusPlus0x); 00366 } 00367 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!"); 00368 00369 if (!LHS.isInvalid()) { 00370 // Combine the LHS and RHS into the LHS (e.g. build AST). 00371 if (TernaryMiddle.isInvalid()) { 00372 // If we're using '>>' as an operator within a template 00373 // argument list (in C++98), suggest the addition of 00374 // parentheses so that the code remains well-formed in C++0x. 00375 if (!GreaterThanIsOperator && OpToken.is(tok::greatergreater)) 00376 SuggestParentheses(OpToken.getLocation(), 00377 diag::warn_cxx0x_right_shift_in_template_arg, 00378 SourceRange(Actions.getExprRange(LHS.get()).getBegin(), 00379 Actions.getExprRange(RHS.get()).getEnd())); 00380 00381 LHS = Actions.ActOnBinOp(CurScope, OpToken.getLocation(), 00382 OpToken.getKind(), move(LHS), move(RHS)); 00383 } else 00384 LHS = Actions.ActOnConditionalOp(OpToken.getLocation(), ColonLoc, 00385 move(LHS), move(TernaryMiddle), 00386 move(RHS)); 00387 } 00388 } 00389 } 00390 00391 /// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is 00392 /// true, parse a unary-expression. isAddressOfOperand exists because an 00393 /// id-expression that is the operand of address-of gets special treatment 00394 /// due to member pointers. 00395 /// 00396 Parser::OwningExprResult Parser::ParseCastExpression(bool isUnaryExpression, 00397 bool isAddressOfOperand, 00398 TypeTy *TypeOfCast) { 00399 bool NotCastExpr; 00400 OwningExprResult Res = ParseCastExpression(isUnaryExpression, 00401 isAddressOfOperand, 00402 NotCastExpr, 00403 TypeOfCast); 00404 if (NotCastExpr) 00405 Diag(Tok, diag::err_expected_expression); 00406 return move(Res); 00407 } 00408 00409 /// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is 00410 /// true, parse a unary-expression. isAddressOfOperand exists because an 00411 /// id-expression that is the operand of address-of gets special treatment 00412 /// due to member pointers. NotCastExpr is set to true if the token is not the 00413 /// start of a cast-expression, and no diagnostic is emitted in this case. 00414 /// 00415 /// cast-expression: [C99 6.5.4] 00416 /// unary-expression 00417 /// '(' type-name ')' cast-expression 00418 /// 00419 /// unary-expression: [C99 6.5.3] 00420 /// postfix-expression 00421 /// '++' unary-expression 00422 /// '--' unary-expression 00423 /// unary-operator cast-expression 00424 /// 'sizeof' unary-expression 00425 /// 'sizeof' '(' type-name ')' 00426 /// [GNU] '__alignof' unary-expression 00427 /// [GNU] '__alignof' '(' type-name ')' 00428 /// [C++0x] 'alignof' '(' type-id ')' 00429 /// [GNU] '&&' identifier 00430 /// [C++] new-expression 00431 /// [C++] delete-expression 00432 /// 00433 /// unary-operator: one of 00434 /// '&' '*' '+' '-' '~' '!' 00435 /// [GNU] '__extension__' '__real' '__imag' 00436 /// 00437 /// primary-expression: [C99 6.5.1] 00438 /// [C99] identifier 00439 /// [C++] id-expression 00440 /// constant 00441 /// string-literal 00442 /// [C++] boolean-literal [C++ 2.13.5] 00443 /// [C++0x] 'nullptr' [C++0x 2.14.7] 00444 /// '(' expression ')' 00445 /// '__func__' [C99 6.4.2.2] 00446 /// [GNU] '__FUNCTION__' 00447 /// [GNU] '__PRETTY_FUNCTION__' 00448 /// [GNU] '(' compound-statement ')' 00449 /// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')' 00450 /// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')' 00451 /// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ',' 00452 /// assign-expr ')' 00453 /// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')' 00454 /// [GNU] '__null' 00455 /// [OBJC] '[' objc-message-expr ']' 00456 /// [OBJC] '@selector' '(' objc-selector-arg ')' 00457 /// [OBJC] '@protocol' '(' identifier ')' 00458 /// [OBJC] '@encode' '(' type-name ')' 00459 /// [OBJC] objc-string-literal 00460 /// [C++] simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3] 00461 /// [C++] typename-specifier '(' expression-list[opt] ')' [C++ 5.2.3] 00462 /// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1] 00463 /// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1] 00464 /// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1] 00465 /// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1] 00466 /// [C++] 'typeid' '(' expression ')' [C++ 5.2p1] 00467 /// [C++] 'typeid' '(' type-id ')' [C++ 5.2p1] 00468 /// [C++] 'this' [C++ 9.3.2] 00469 /// [G++] unary-type-trait '(' type-id ')' 00470 /// [G++] binary-type-trait '(' type-id ',' type-id ')' [TODO] 00471 /// [clang] '^' block-literal 00472 /// 00473 /// constant: [C99 6.4.4] 00474 /// integer-constant 00475 /// floating-constant 00476 /// enumeration-constant -> identifier 00477 /// character-constant 00478 /// 00479 /// id-expression: [C++ 5.1] 00480 /// unqualified-id 00481 /// qualified-id 00482 /// 00483 /// unqualified-id: [C++ 5.1] 00484 /// identifier 00485 /// operator-function-id 00486 /// conversion-function-id 00487 /// '~' class-name 00488 /// template-id 00489 /// 00490 /// new-expression: [C++ 5.3.4] 00491 /// '::'[opt] 'new' new-placement[opt] new-type-id 00492 /// new-initializer[opt] 00493 /// '::'[opt] 'new' new-placement[opt] '(' type-id ')' 00494 /// new-initializer[opt] 00495 /// 00496 /// delete-expression: [C++ 5.3.5] 00497 /// '::'[opt] 'delete' cast-expression 00498 /// '::'[opt] 'delete' '[' ']' cast-expression 00499 /// 00500 /// [GNU] unary-type-trait: 00501 /// '__has_nothrow_assign' [TODO] 00502 /// '__has_nothrow_copy' [TODO] 00503 /// '__has_nothrow_constructor' [TODO] 00504 /// '__has_trivial_assign' [TODO] 00505 /// '__has_trivial_copy' [TODO] 00506 /// '__has_trivial_constructor' 00507 /// '__has_trivial_destructor' 00508 /// '__has_virtual_destructor' [TODO] 00509 /// '__is_abstract' [TODO] 00510 /// '__is_class' 00511 /// '__is_empty' [TODO] 00512 /// '__is_enum' 00513 /// '__is_pod' 00514 /// '__is_polymorphic' 00515 /// '__is_union' 00516 /// 00517 /// [GNU] binary-type-trait: 00518 /// '__is_base_of' [TODO] 00519 /// 00520 Parser::OwningExprResult Parser::ParseCastExpression(bool isUnaryExpression, 00521 bool isAddressOfOperand, 00522 bool &NotCastExpr, 00523 TypeTy *TypeOfCast) { 00524 OwningExprResult Res(Actions); 00525 tok::TokenKind SavedKind = Tok.getKind(); 00526 NotCastExpr = false; 00527 00528 // This handles all of cast-expression, unary-expression, postfix-expression, 00529 // and primary-expression. We handle them together like this for efficiency 00530 // and to simplify handling of an expression starting with a '(' token: which 00531 // may be one of a parenthesized expression, cast-expression, compound literal 00532 // expression, or statement expression. 00533 // 00534 // If the parsed tokens consist of a primary-expression, the cases below 00535 // call ParsePostfixExpressionSuffix to handle the postfix expression 00536 // suffixes. Cases that cannot be followed by postfix exprs should 00537 // return without invoking ParsePostfixExpressionSuffix. 00538 switch (SavedKind) { 00539 case tok::l_paren: { 00540 // If this expression is limited to being a unary-expression, the parent can 00541 // not start a cast expression. 00542 ParenParseOption ParenExprType = 00543 isUnaryExpression ? CompoundLiteral : CastExpr; 00544 TypeTy *CastTy; 00545 SourceLocation LParenLoc = Tok.getLocation(); 00546 SourceLocation RParenLoc; 00547 00548 { 00549 // The inside of the parens don't need to be a colon protected scope. 00550 ColonProtectionRAIIObject X(*this, false); 00551 00552 Res = ParseParenExpression(ParenExprType, false/*stopIfCastExr*/, 00553 TypeOfCast, CastTy, RParenLoc); 00554 if (Res.isInvalid()) return move(Res); 00555 } 00556 00557 switch (ParenExprType) { 00558 case SimpleExpr: break; // Nothing else to do. 00559 case CompoundStmt: break; // Nothing else to do. 00560 case CompoundLiteral: 00561 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of 00562 // postfix-expression exist, parse them now. 00563 break; 00564 case CastExpr: 00565 // We have parsed the cast-expression and no postfix-expr pieces are 00566 // following. 00567 return move(Res); 00568 } 00569 00570 // These can be followed by postfix-expr pieces. 00571 return ParsePostfixExpressionSuffix(move(Res)); 00572 } 00573 00574 // primary-expression 00575 case tok::numeric_constant: 00576 // constant: integer-constant 00577 // constant: floating-constant 00578 00579 Res = Actions.ActOnNumericConstant(Tok); 00580 ConsumeToken(); 00581 00582 // These can be followed by postfix-expr pieces. 00583 return ParsePostfixExpressionSuffix(move(Res)); 00584 00585 case tok::kw_true: 00586 case tok::kw_false: 00587 return ParseCXXBoolLiteral(); 00588 00589 case tok::kw_nullptr: 00590 return Actions.ActOnCXXNullPtrLiteral(ConsumeToken()); 00591 00592 case tok::identifier: { // primary-expression: identifier 00593 // unqualified-id: identifier 00594 // constant: enumeration-constant 00595 // Turn a potentially qualified name into a annot_typename or 00596 // annot_cxxscope if it would be valid. This handles things like x::y, etc. 00597 if (getLang().CPlusPlus) { 00598 // Avoid the unnecessary parse-time lookup in the common case 00599 // where the syntax forbids a type. 00600 const Token &Next = NextToken(); 00601 if (Next.is(tok::coloncolon) || 00602 (!ColonIsSacred && Next.is(tok::colon)) || 00603 Next.is(tok::less) || 00604 Next.is(tok::l_paren)) { 00605 // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse. 00606 if (TryAnnotateTypeOrScopeToken()) 00607 return ExprError(); 00608 if (!Tok.is(tok::identifier)) 00609 return ParseCastExpression(isUnaryExpression, isAddressOfOperand); 00610 } 00611 } 00612 00613 // Consume the identifier so that we can see if it is followed by a '(' or 00614 // '.'. 00615 IdentifierInfo &II = *Tok.getIdentifierInfo(); 00616 SourceLocation ILoc = ConsumeToken(); 00617 00618 // Support 'Class.property' and 'super.property' notation. 00619 if (getLang().ObjC1 && Tok.is(tok::period) && 00620 (Actions.getTypeName(II, ILoc, CurScope) || 00621 // Allow the base to be 'super' if in an objc-method. 00622 (&II == Ident_super && CurScope->isInObjcMethodScope()))) { 00623 SourceLocation DotLoc = ConsumeToken(); 00624 00625 if (Tok.isNot(tok::identifier)) { 00626 Diag(Tok, diag::err_expected_property_name); 00627 return ExprError(); 00628 } 00629 IdentifierInfo &PropertyName = *Tok.getIdentifierInfo(); 00630 SourceLocation PropertyLoc = ConsumeToken(); 00631 00632 Res = Actions.ActOnClassPropertyRefExpr(II, PropertyName, 00633 ILoc, PropertyLoc); 00634 // These can be followed by postfix-expr pieces. 00635 return ParsePostfixExpressionSuffix(move(Res)); 00636 } 00637 00638 // Function designators are allowed to be undeclared (C99 6.5.1p2), so we 00639 // need to know whether or not this identifier is a function designator or 00640 // not. 00641 UnqualifiedId Name; 00642 CXXScopeSpec ScopeSpec; 00643 Name.setIdentifier(&II, ILoc); 00644 Res = Actions.ActOnIdExpression(CurScope, ScopeSpec, Name, 00645 Tok.is(tok::l_paren), false); 00646 // These can be followed by postfix-expr pieces. 00647 return ParsePostfixExpressionSuffix(move(Res)); 00648 } 00649 case tok::char_constant: // constant: character-constant 00650 Res = Actions.ActOnCharacterConstant(Tok); 00651 ConsumeToken(); 00652 // These can be followed by postfix-expr pieces. 00653 return ParsePostfixExpressionSuffix(move(Res)); 00654 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2] 00655 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU] 00656 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU] 00657 Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind); 00658 ConsumeToken(); 00659 // These can be followed by postfix-expr pieces. 00660 return ParsePostfixExpressionSuffix(move(Res)); 00661 case tok::string_literal: // primary-expression: string-literal 00662 case tok::wide_string_literal: 00663 Res = ParseStringLiteralExpression(); 00664 if (Res.isInvalid()) return move(Res); 00665 // This can be followed by postfix-expr pieces (e.g. "foo"[1]). 00666 return ParsePostfixExpressionSuffix(move(Res)); 00667 case tok::kw___builtin_va_arg: 00668 case tok::kw___builtin_offsetof: 00669 case tok::kw___builtin_choose_expr: 00670 case tok::kw___builtin_types_compatible_p: 00671 return ParseBuiltinPrimaryExpression(); 00672 case tok::kw___null: 00673 return Actions.ActOnGNUNullExpr(ConsumeToken()); 00674 break; 00675 case tok::plusplus: // unary-expression: '++' unary-expression 00676 case tok::minusminus: { // unary-expression: '--' unary-expression 00677 SourceLocation SavedLoc = ConsumeToken(); 00678 Res = ParseCastExpression(true); 00679 if (!Res.isInvalid()) 00680 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res)); 00681 return move(Res); 00682 } 00683 case tok::amp: { // unary-expression: '&' cast-expression 00684 // Special treatment because of member pointers 00685 SourceLocation SavedLoc = ConsumeToken(); 00686 Res = ParseCastExpression(false, true); 00687 if (!Res.isInvalid()) 00688 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res)); 00689 return move(Res); 00690 } 00691 00692 case tok::star: // unary-expression: '*' cast-expression 00693 case tok::plus: // unary-expression: '+' cast-expression 00694 case tok::minus: // unary-expression: '-' cast-expression 00695 case tok::tilde: // unary-expression: '~' cast-expression 00696 case tok::exclaim: // unary-expression: '!' cast-expression 00697 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU] 00698 case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU] 00699 SourceLocation SavedLoc = ConsumeToken(); 00700 Res = ParseCastExpression(false); 00701 if (!Res.isInvalid()) 00702 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res)); 00703 return move(Res); 00704 } 00705 00706 case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU] 00707 // __extension__ silences extension warnings in the subexpression. 00708 ExtensionRAIIObject O(Diags); // Use RAII to do this. 00709 SourceLocation SavedLoc = ConsumeToken(); 00710 Res = ParseCastExpression(false); 00711 if (!Res.isInvalid()) 00712 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res)); 00713 return move(Res); 00714 } 00715 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression 00716 // unary-expression: 'sizeof' '(' type-name ')' 00717 case tok::kw_alignof: 00718 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression 00719 // unary-expression: '__alignof' '(' type-name ')' 00720 // unary-expression: 'alignof' '(' type-id ')' 00721 return ParseSizeofAlignofExpression(); 00722 case tok::ampamp: { // unary-expression: '&&' identifier 00723 SourceLocation AmpAmpLoc = ConsumeToken(); 00724 if (Tok.isNot(tok::identifier)) 00725 return ExprError(Diag(Tok, diag::err_expected_ident)); 00726 00727 Diag(AmpAmpLoc, diag::ext_gnu_address_of_label); 00728 Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(), 00729 Tok.getIdentifierInfo()); 00730 ConsumeToken(); 00731 return move(Res); 00732 } 00733 case tok::kw_const_cast: 00734 case tok::kw_dynamic_cast: 00735 case tok::kw_reinterpret_cast: 00736 case tok::kw_static_cast: 00737 Res = ParseCXXCasts(); 00738 // These can be followed by postfix-expr pieces. 00739 return ParsePostfixExpressionSuffix(move(Res)); 00740 case tok::kw_typeid: 00741 Res = ParseCXXTypeid(); 00742 // This can be followed by postfix-expr pieces. 00743 return ParsePostfixExpressionSuffix(move(Res)); 00744 case tok::kw_this: 00745 Res = ParseCXXThis(); 00746 // This can be followed by postfix-expr pieces. 00747 return ParsePostfixExpressionSuffix(move(Res)); 00748 00749 case tok::kw_char: 00750 case tok::kw_wchar_t: 00751 case tok::kw_char16_t: 00752 case tok::kw_char32_t: 00753 case tok::kw_bool: 00754 case tok::kw_short: 00755 case tok::kw_int: 00756 case tok::kw_long: 00757 case tok::kw_signed: 00758 case tok::kw_unsigned: 00759 case tok::kw_float: 00760 case tok::kw_double: 00761 case tok::kw_void: 00762 case tok::kw_typename: 00763 case tok::kw_typeof: 00764 case tok::kw___vector: 00765 case tok::annot_typename: { 00766 if (!getLang().CPlusPlus) { 00767 Diag(Tok, diag::err_expected_expression); 00768 return ExprError(); 00769 } 00770 00771 if (SavedKind == tok::kw_typename) { 00772 // postfix-expression: typename-specifier '(' expression-list[opt] ')' 00773 if (TryAnnotateTypeOrScopeToken()) 00774 return ExprError(); 00775 } 00776 00777 // postfix-expression: simple-type-specifier '(' expression-list[opt] ')' 00778 // 00779 DeclSpec DS; 00780 ParseCXXSimpleTypeSpecifier(DS); 00781 if (Tok.isNot(tok::l_paren)) 00782 return ExprError(Diag(Tok, diag::err_expected_lparen_after_type) 00783 << DS.getSourceRange()); 00784 00785 Res = ParseCXXTypeConstructExpression(DS); 00786 // This can be followed by postfix-expr pieces. 00787 return ParsePostfixExpressionSuffix(move(Res)); 00788 } 00789 00790 case tok::annot_cxxscope: { // [C++] id-expression: qualified-id 00791 // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse. 00792 // (We can end up in this situation after tentative parsing.) 00793 if (TryAnnotateTypeOrScopeToken()) 00794 return ExprError(); 00795 if (!Tok.is(tok::annot_cxxscope)) 00796 return ParseCastExpression(isUnaryExpression, isAddressOfOperand, 00797 NotCastExpr, TypeOfCast); 00798 00799 Token Next = NextToken(); 00800 if (Next.is(tok::annot_template_id)) { 00801 TemplateIdAnnotation *TemplateId 00802 = static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue()); 00803 if (TemplateId->Kind == TNK_Type_template) { 00804 // We have a qualified template-id that we know refers to a 00805 // type, translate it into a type and continue parsing as a 00806 // cast expression. 00807 CXXScopeSpec SS; 00808 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false); 00809 AnnotateTemplateIdTokenAsType(&SS); 00810 return ParseCastExpression(isUnaryExpression, isAddressOfOperand, 00811 NotCastExpr, TypeOfCast); 00812 } 00813 } 00814 00815 // Parse as an id-expression. 00816 Res = ParseCXXIdExpression(isAddressOfOperand); 00817 return ParsePostfixExpressionSuffix(move(Res)); 00818 } 00819 00820 case tok::annot_template_id: { // [C++] template-id 00821 TemplateIdAnnotation *TemplateId 00822 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); 00823 if (TemplateId->Kind == TNK_Type_template) { 00824 // We have a template-id that we know refers to a type, 00825 // translate it into a type and continue parsing as a cast 00826 // expression. 00827 AnnotateTemplateIdTokenAsType(); 00828 return ParseCastExpression(isUnaryExpression, isAddressOfOperand, 00829 NotCastExpr, TypeOfCast); 00830 } 00831 00832 // Fall through to treat the template-id as an id-expression. 00833 } 00834 00835 case tok::kw_operator: // [C++] id-expression: operator/conversion-function-id 00836 Res = ParseCXXIdExpression(isAddressOfOperand); 00837 return ParsePostfixExpressionSuffix(move(Res)); 00838 00839 case tok::coloncolon: { 00840 // ::foo::bar -> global qualified name etc. If TryAnnotateTypeOrScopeToken 00841 // annotates the token, tail recurse. 00842 if (TryAnnotateTypeOrScopeToken()) 00843 return ExprError(); 00844 if (!Tok.is(tok::coloncolon)) 00845 return ParseCastExpression(isUnaryExpression, isAddressOfOperand); 00846 00847 // ::new -> [C++] new-expression 00848 // ::delete -> [C++] delete-expression 00849 SourceLocation CCLoc = ConsumeToken(); 00850 if (Tok.is(tok::kw_new)) 00851 return ParseCXXNewExpression(true, CCLoc); 00852 if (Tok.is(tok::kw_delete)) 00853 return ParseCXXDeleteExpression(true, CCLoc); 00854 00855 // This is not a type name or scope specifier, it is an invalid expression. 00856 Diag(CCLoc, diag::err_expected_expression); 00857 return ExprError(); 00858 } 00859 00860 case tok::kw_new: // [C++] new-expression 00861 return ParseCXXNewExpression(false, Tok.getLocation()); 00862 00863 case tok::kw_delete: // [C++] delete-expression 00864 return ParseCXXDeleteExpression(false, Tok.getLocation()); 00865 00866 case tok::kw___is_pod: // [GNU] unary-type-trait 00867 case tok::kw___is_class: 00868 case tok::kw___is_enum: 00869 case tok::kw___is_union: 00870 case tok::kw___is_empty: 00871 case tok::kw___is_polymorphic: 00872 case tok::kw___is_abstract: 00873 case tok::kw___is_literal: 00874 case tok::kw___has_trivial_constructor: 00875 case tok::kw___has_trivial_copy: 00876 case tok::kw___has_trivial_assign: 00877 case tok::kw___has_trivial_destructor: 00878 return ParseUnaryTypeTrait(); 00879 00880 case tok::at: { 00881 SourceLocation AtLoc = ConsumeToken(); 00882 return ParseObjCAtExpression(AtLoc); 00883 } 00884 case tok::caret: 00885 return ParsePostfixExpressionSuffix(ParseBlockLiteralExpression()); 00886 case tok::code_completion: 00887 Actions.CodeCompleteOrdinaryName(CurScope, Action::CCC_Expression); 00888 ConsumeToken(); 00889 return ParseCastExpression(isUnaryExpression, isAddressOfOperand, 00890 NotCastExpr, TypeOfCast); 00891 case tok::l_square: 00892 // These can be followed by postfix-expr pieces. 00893 if (getLang().ObjC1) 00894 return ParsePostfixExpressionSuffix(ParseObjCMessageExpression()); 00895 // FALL THROUGH. 00896 default: 00897 NotCastExpr = true; 00898 return ExprError(); 00899 } 00900 00901 // unreachable. 00902 abort(); 00903 } 00904 00905 /// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression 00906 /// is parsed, this method parses any suffixes that apply. 00907 /// 00908 /// postfix-expression: [C99 6.5.2] 00909 /// primary-expression 00910 /// postfix-expression '[' expression ']' 00911 /// postfix-expression '(' argument-expression-list[opt] ')' 00912 /// postfix-expression '.' identifier 00913 /// postfix-expression '->' identifier 00914 /// postfix-expression '++' 00915 /// postfix-expression '--' 00916 /// '(' type-name ')' '{' initializer-list '}' 00917 /// '(' type-name ')' '{' initializer-list ',' '}' 00918 /// 00919 /// argument-expression-list: [C99 6.5.2] 00920 /// argument-expression 00921 /// argument-expression-list ',' assignment-expression 00922 /// 00923 Parser::OwningExprResult 00924 Parser::ParsePostfixExpressionSuffix(OwningExprResult LHS) { 00925 // Now that the primary-expression piece of the postfix-expression has been 00926 // parsed, see if there are any postfix-expression pieces here. 00927 SourceLocation Loc; 00928 while (1) { 00929 switch (Tok.getKind()) { 00930 default: // Not a postfix-expression suffix. 00931 return move(LHS); 00932 case tok::l_square: { // postfix-expression: p-e '[' expression ']' 00933 Loc = ConsumeBracket(); 00934 OwningExprResult Idx(ParseExpression()); 00935 00936 SourceLocation RLoc = Tok.getLocation(); 00937 00938 if (!LHS.isInvalid() && !Idx.isInvalid() && Tok.is(tok::r_square)) { 00939 LHS = Actions.ActOnArraySubscriptExpr(CurScope, move(LHS), Loc, 00940 move(Idx), RLoc); 00941 } else 00942 LHS = ExprError(); 00943 00944 // Match the ']'. 00945 MatchRHSPunctuation(tok::r_square, Loc); 00946 break; 00947 } 00948 00949 case tok::l_paren: { // p-e: p-e '(' argument-expression-list[opt] ')' 00950 ExprVector ArgExprs(Actions); 00951 CommaLocsTy CommaLocs; 00952 00953 Loc = ConsumeParen(); 00954 00955 if (Tok.is(tok::code_completion)) { 00956 Actions.CodeCompleteCall(CurScope, LHS.get(), 0, 0); 00957 ConsumeToken(); 00958 } 00959 00960 if (Tok.isNot(tok::r_paren)) { 00961 if (ParseExpressionList(ArgExprs, CommaLocs, &Action::CodeCompleteCall, 00962 LHS.get())) { 00963 SkipUntil(tok::r_paren); 00964 return ExprError(); 00965 } 00966 } 00967 00968 // Match the ')'. 00969 if (Tok.isNot(tok::r_paren)) { 00970 MatchRHSPunctuation(tok::r_paren, Loc); 00971 return ExprError(); 00972 } 00973 00974 if (!LHS.isInvalid()) { 00975 assert((ArgExprs.size() == 0 || ArgExprs.size()-1 == CommaLocs.size())&& 00976 "Unexpected number of commas!"); 00977 LHS = Actions.ActOnCallExpr(CurScope, move(LHS), Loc, 00978 move_arg(ArgExprs), CommaLocs.data(), 00979 Tok.getLocation()); 00980 } 00981 00982 ConsumeParen(); 00983 break; 00984 } 00985 case tok::arrow: 00986 case tok::period: { 00987 // postfix-expression: p-e '->' template[opt] id-expression 00988 // postfix-expression: p-e '.' template[opt] id-expression 00989 tok::TokenKind OpKind = Tok.getKind(); 00990 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token. 00991 00992 CXXScopeSpec SS; 00993 Action::TypeTy *ObjectType = 0; 00994 bool MayBePseudoDestructor = false; 00995 if (getLang().CPlusPlus && !LHS.isInvalid()) { 00996 LHS = Actions.ActOnStartCXXMemberReference(CurScope, move(LHS), 00997 OpLoc, OpKind, ObjectType, 00998 MayBePseudoDestructor); 00999 if (LHS.isInvalid()) 01000 break; 01001 01002 ParseOptionalCXXScopeSpecifier(SS, ObjectType, false, 01003 &MayBePseudoDestructor); 01004 } 01005 01006 if (Tok.is(tok::code_completion)) { 01007 // Code completion for a member access expression. 01008 Actions.CodeCompleteMemberReferenceExpr(CurScope, LHS.get(), 01009 OpLoc, OpKind == tok::arrow); 01010 01011 ConsumeToken(); 01012 } 01013 01014 if (MayBePseudoDestructor) { 01015 LHS = ParseCXXPseudoDestructor(move(LHS), OpLoc, OpKind, SS, 01016 ObjectType); 01017 break; 01018 } 01019 01020 // Either the action has told is that this cannot be a 01021 // pseudo-destructor expression (based on the type of base 01022 // expression), or we didn't see a '~' in the right place. We 01023 // can still parse a destructor name here, but in that case it 01024 // names a real destructor. 01025 UnqualifiedId Name; 01026 if (ParseUnqualifiedId(SS, 01027 /*EnteringContext=*/false, 01028 /*AllowDestructorName=*/true, 01029 /*AllowConstructorName=*/false, 01030 ObjectType, 01031 Name)) 01032 return ExprError(); 01033 01034 if (!LHS.isInvalid()) 01035 LHS = Actions.ActOnMemberAccessExpr(CurScope, move(LHS), OpLoc, 01036 OpKind, SS, Name, ObjCImpDecl, 01037 Tok.is(tok::l_paren)); 01038 break; 01039 } 01040 case tok::plusplus: // postfix-expression: postfix-expression '++' 01041 case tok::minusminus: // postfix-expression: postfix-expression '--' 01042 if (!LHS.isInvalid()) { 01043 LHS = Actions.ActOnPostfixUnaryOp(CurScope, Tok.getLocation(), 01044 Tok.getKind(), move(LHS)); 01045 } 01046 ConsumeToken(); 01047 break; 01048 } 01049 } 01050 } 01051 01052 /// ParseExprAfterTypeofSizeofAlignof - We parsed a typeof/sizeof/alignof and 01053 /// we are at the start of an expression or a parenthesized type-id. 01054 /// OpTok is the operand token (typeof/sizeof/alignof). Returns the expression 01055 /// (isCastExpr == false) or the type (isCastExpr == true). 01056 /// 01057 /// unary-expression: [C99 6.5.3] 01058 /// 'sizeof' unary-expression 01059 /// 'sizeof' '(' type-name ')' 01060 /// [GNU] '__alignof' unary-expression 01061 /// [GNU] '__alignof' '(' type-name ')' 01062 /// [C++0x] 'alignof' '(' type-id ')' 01063 /// 01064 /// [GNU] typeof-specifier: 01065 /// typeof ( expressions ) 01066 /// typeof ( type-name ) 01067 /// [GNU/C++] typeof unary-expression 01068 /// 01069 Parser::OwningExprResult 01070 Parser::ParseExprAfterTypeofSizeofAlignof(const Token &OpTok, 01071 bool &isCastExpr, 01072 TypeTy *&CastTy, 01073 SourceRange &CastRange) { 01074 01075 assert((OpTok.is(tok::kw_typeof) || OpTok.is(tok::kw_sizeof) || 01076 OpTok.is(tok::kw___alignof) || OpTok.is(tok::kw_alignof)) && 01077 "Not a typeof/sizeof/alignof expression!"); 01078 01079 OwningExprResult Operand(Actions); 01080 01081 // If the operand doesn't start with an '(', it must be an expression. 01082 if (Tok.isNot(tok::l_paren)) { 01083 isCastExpr = false; 01084 if (OpTok.is(tok::kw_typeof) && !getLang().CPlusPlus) { 01085 Diag(Tok,diag::err_expected_lparen_after_id) << OpTok.getIdentifierInfo(); 01086 return ExprError(); 01087 } 01088 01089 // C++0x [expr.sizeof]p1: 01090 // [...] The operand is either an expression, which is an unevaluated 01091 // operand (Clause 5) [...] 01092 // 01093 // The GNU typeof and alignof extensions also behave as unevaluated 01094 // operands. 01095 EnterExpressionEvaluationContext Unevaluated(Actions, 01096 Action::Unevaluated); 01097 Operand = ParseCastExpression(true/*isUnaryExpression*/); 01098 } else { 01099 // If it starts with a '(', we know that it is either a parenthesized 01100 // type-name, or it is a unary-expression that starts with a compound 01101 // literal, or starts with a primary-expression that is a parenthesized 01102 // expression. 01103 ParenParseOption ExprType = CastExpr; 01104 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc; 01105 01106 // C++0x [expr.sizeof]p1: 01107 // [...] The operand is either an expression, which is an unevaluated 01108 // operand (Clause 5) [...] 01109 // 01110 // The GNU typeof and alignof extensions also behave as unevaluated 01111 // operands. 01112 EnterExpressionEvaluationContext Unevaluated(Actions, 01113 Action::Unevaluated); 01114 Operand = ParseParenExpression(ExprType, true/*stopIfCastExpr*/, 01115 0/*TypeOfCast*/, 01116 CastTy, RParenLoc); 01117 CastRange = SourceRange(LParenLoc, RParenLoc); 01118 01119 // If ParseParenExpression parsed a '(typename)' sequence only, then this is 01120 // a type. 01121 if (ExprType == CastExpr) { 01122 isCastExpr = true; 01123 return ExprEmpty(); 01124 } 01125 01126 // If this is a parenthesized expression, it is the start of a 01127 // unary-expression, but doesn't include any postfix pieces. Parse these 01128 // now if present. 01129 Operand = ParsePostfixExpressionSuffix(move(Operand)); 01130 } 01131 01132 // If we get here, the operand to the typeof/sizeof/alignof was an expresion. 01133 isCastExpr = false; 01134 return move(Operand); 01135 } 01136 01137 01138 /// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression. 01139 /// unary-expression: [C99 6.5.3] 01140 /// 'sizeof' unary-expression 01141 /// 'sizeof' '(' type-name ')' 01142 /// [GNU] '__alignof' unary-expression 01143 /// [GNU] '__alignof' '(' type-name ')' 01144 /// [C++0x] 'alignof' '(' type-id ')' 01145 Parser::OwningExprResult Parser::ParseSizeofAlignofExpression() { 01146 assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof) 01147 || Tok.is(tok::kw_alignof)) && 01148 "Not a sizeof/alignof expression!"); 01149 Token OpTok = Tok; 01150 ConsumeToken(); 01151 01152 bool isCastExpr; 01153 TypeTy *CastTy; 01154 SourceRange CastRange; 01155 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok, 01156 isCastExpr, 01157 CastTy, 01158 CastRange); 01159 01160 if (isCastExpr) 01161 return Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(), 01162 OpTok.is(tok::kw_sizeof), 01163 /*isType=*/true, CastTy, 01164 CastRange); 01165 01166 // If we get here, the operand to the sizeof/alignof was an expresion. 01167 if (!Operand.isInvalid()) 01168 Operand = Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(), 01169 OpTok.is(tok::kw_sizeof), 01170 /*isType=*/false, 01171 Operand.release(), CastRange); 01172 return move(Operand); 01173 } 01174 01175 /// ParseBuiltinPrimaryExpression 01176 /// 01177 /// primary-expression: [C99 6.5.1] 01178 /// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')' 01179 /// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')' 01180 /// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ',' 01181 /// assign-expr ')' 01182 /// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')' 01183 /// 01184 /// [GNU] offsetof-member-designator: 01185 /// [GNU] identifier 01186 /// [GNU] offsetof-member-designator '.' identifier 01187 /// [GNU] offsetof-member-designator '[' expression ']' 01188 /// 01189 Parser::OwningExprResult Parser::ParseBuiltinPrimaryExpression() { 01190 OwningExprResult Res(Actions); 01191 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo(); 01192 01193 tok::TokenKind T = Tok.getKind(); 01194 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier. 01195 01196 // All of these start with an open paren. 01197 if (Tok.isNot(tok::l_paren)) 01198 return ExprError(Diag(Tok, diag::err_expected_lparen_after_id) 01199 << BuiltinII); 01200 01201 SourceLocation LParenLoc = ConsumeParen(); 01202 // TODO: Build AST. 01203 01204 switch (T) { 01205 default: assert(0 && "Not a builtin primary expression!"); 01206 case tok::kw___builtin_va_arg: { 01207 OwningExprResult Expr(ParseAssignmentExpression()); 01208 if (Expr.isInvalid()) { 01209 SkipUntil(tok::r_paren); 01210 return ExprError(); 01211 } 01212 01213 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) 01214 return ExprError(); 01215 01216 TypeResult Ty = ParseTypeName(); 01217 01218 if (Tok.isNot(tok::r_paren)) { 01219 Diag(Tok, diag::err_expected_rparen); 01220 return ExprError(); 01221 } 01222 if (Ty.isInvalid()) 01223 Res = ExprError(); 01224 else 01225 Res = Actions.ActOnVAArg(StartLoc, move(Expr), Ty.get(), ConsumeParen()); 01226 break; 01227 } 01228 case tok::kw___builtin_offsetof: { 01229 SourceLocation TypeLoc = Tok.getLocation(); 01230 TypeResult Ty = ParseTypeName(); 01231 if (Ty.isInvalid()) { 01232 SkipUntil(tok::r_paren); 01233 return ExprError(); 01234 } 01235 01236 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) 01237 return ExprError(); 01238 01239 // We must have at least one identifier here. 01240 if (Tok.isNot(tok::identifier)) { 01241 Diag(Tok, diag::err_expected_ident); 01242 SkipUntil(tok::r_paren); 01243 return ExprError(); 01244 } 01245 01246 // Keep track of the various subcomponents we see. 01247 llvm::SmallVector<Action::OffsetOfComponent, 4> Comps; 01248 01249 Comps.push_back(Action::OffsetOfComponent()); 01250 Comps.back().isBrackets = false; 01251 Comps.back().U.IdentInfo = Tok.getIdentifierInfo(); 01252 Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken(); 01253 01254 // FIXME: This loop leaks the index expressions on error. 01255 while (1) { 01256 if (Tok.is(tok::period)) { 01257 // offsetof-member-designator: offsetof-member-designator '.' identifier 01258 Comps.push_back(Action::OffsetOfComponent()); 01259 Comps.back().isBrackets = false; 01260 Comps.back().LocStart = ConsumeToken(); 01261 01262 if (Tok.isNot(tok::identifier)) { 01263 Diag(Tok, diag::err_expected_ident); 01264 SkipUntil(tok::r_paren); 01265 return ExprError(); 01266 } 01267 Comps.back().U.IdentInfo = Tok.getIdentifierInfo(); 01268 Comps.back().LocEnd = ConsumeToken(); 01269 01270 } else if (Tok.is(tok::l_square)) { 01271 // offsetof-member-designator: offsetof-member-design '[' expression ']' 01272 Comps.push_back(Action::OffsetOfComponent()); 01273 Comps.back().isBrackets = true; 01274 Comps.back().LocStart = ConsumeBracket(); 01275 Res = ParseExpression(); 01276 if (Res.isInvalid()) { 01277 SkipUntil(tok::r_paren); 01278 return move(Res); 01279 } 01280 Comps.back().U.E = Res.release(); 01281 01282 Comps.back().LocEnd = 01283 MatchRHSPunctuation(tok::r_square, Comps.back().LocStart); 01284 } else { 01285 if (Tok.isNot(tok::r_paren)) { 01286 MatchRHSPunctuation(tok::r_paren, LParenLoc); 01287 Res = ExprError(); 01288 } else if (Ty.isInvalid()) { 01289 Res = ExprError(); 01290 } else { 01291 Res = Actions.ActOnBuiltinOffsetOf(CurScope, StartLoc, TypeLoc, 01292 Ty.get(), &Comps[0], 01293 Comps.size(), ConsumeParen()); 01294 } 01295 break; 01296 } 01297 } 01298 break; 01299 } 01300 case tok::kw___builtin_choose_expr: { 01301 OwningExprResult Cond(ParseAssignmentExpression()); 01302 if (Cond.isInvalid()) { 01303 SkipUntil(tok::r_paren); 01304 return move(Cond); 01305 } 01306 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) 01307 return ExprError(); 01308 01309 OwningExprResult Expr1(ParseAssignmentExpression()); 01310 if (Expr1.isInvalid()) { 01311 SkipUntil(tok::r_paren); 01312 return move(Expr1); 01313 } 01314 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) 01315 return ExprError(); 01316 01317 OwningExprResult Expr2(ParseAssignmentExpression()); 01318 if (Expr2.isInvalid()) { 01319 SkipUntil(tok::r_paren); 01320 return move(Expr2); 01321 } 01322 if (Tok.isNot(tok::r_paren)) { 01323 Diag(Tok, diag::err_expected_rparen); 01324 return ExprError(); 01325 } 01326 Res = Actions.ActOnChooseExpr(StartLoc, move(Cond), move(Expr1), 01327 move(Expr2), ConsumeParen()); 01328 break; 01329 } 01330 case tok::kw___builtin_types_compatible_p: 01331 TypeResult Ty1 = ParseTypeName(); 01332 01333 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) 01334 return ExprError(); 01335 01336 TypeResult Ty2 = ParseTypeName(); 01337 01338 if (Tok.isNot(tok::r_paren)) { 01339 Diag(Tok, diag::err_expected_rparen); 01340 return ExprError(); 01341 } 01342 01343 if (Ty1.isInvalid() || Ty2.isInvalid()) 01344 Res = ExprError(); 01345 else 01346 Res = Actions.ActOnTypesCompatibleExpr(StartLoc, Ty1.get(), Ty2.get(), 01347 ConsumeParen()); 01348 break; 01349 } 01350 01351 // These can be followed by postfix-expr pieces because they are 01352 // primary-expressions. 01353 return ParsePostfixExpressionSuffix(move(Res)); 01354 } 01355 01356 /// ParseParenExpression - This parses the unit that starts with a '(' token, 01357 /// based on what is allowed by ExprType. The actual thing parsed is returned 01358 /// in ExprType. If stopIfCastExpr is true, it will only return the parsed type, 01359 /// not the parsed cast-expression. 01360 /// 01361 /// primary-expression: [C99 6.5.1] 01362 /// '(' expression ')' 01363 /// [GNU] '(' compound-statement ')' (if !ParenExprOnly) 01364 /// postfix-expression: [C99 6.5.2] 01365 /// '(' type-name ')' '{' initializer-list '}' 01366 /// '(' type-name ')' '{' initializer-list ',' '}' 01367 /// cast-expression: [C99 6.5.4] 01368 /// '(' type-name ')' cast-expression 01369 /// 01370 Parser::OwningExprResult 01371 Parser::ParseParenExpression(ParenParseOption &ExprType, bool stopIfCastExpr, 01372 TypeTy *TypeOfCast, TypeTy *&CastTy, 01373 SourceLocation &RParenLoc) { 01374 assert(Tok.is(tok::l_paren) && "Not a paren expr!"); 01375 GreaterThanIsOperatorScope G(GreaterThanIsOperator, true); 01376 SourceLocation OpenLoc = ConsumeParen(); 01377 OwningExprResult Result(Actions, true); 01378 bool isAmbiguousTypeId; 01379 CastTy = 0; 01380 01381 if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) { 01382 Diag(Tok, diag::ext_gnu_statement_expr); 01383 OwningStmtResult Stmt(ParseCompoundStatement(0, true)); 01384 ExprType = CompoundStmt; 01385 01386 // If the substmt parsed correctly, build the AST node. 01387 if (!Stmt.isInvalid() && Tok.is(tok::r_paren)) 01388 Result = Actions.ActOnStmtExpr(OpenLoc, move(Stmt), Tok.getLocation()); 01389 01390 } else if (ExprType >= CompoundLiteral && 01391 isTypeIdInParens(isAmbiguousTypeId)) { 01392 01393 // Otherwise, this is a compound literal expression or cast expression. 01394 01395 // In C++, if the type-id is ambiguous we disambiguate based on context. 01396 // If stopIfCastExpr is true the context is a typeof/sizeof/alignof 01397 // in which case we should treat it as type-id. 01398 // if stopIfCastExpr is false, we need to determine the context past the 01399 // parens, so we defer to ParseCXXAmbiguousParenExpression for that. 01400 if (isAmbiguousTypeId && !stopIfCastExpr) 01401 return ParseCXXAmbiguousParenExpression(ExprType, CastTy, 01402 OpenLoc, RParenLoc); 01403 01404 TypeResult Ty = ParseTypeName(); 01405 01406 // Match the ')'. 01407 if (Tok.is(tok::r_paren)) 01408 RParenLoc = ConsumeParen(); 01409 else 01410 MatchRHSPunctuation(tok::r_paren, OpenLoc); 01411 01412 if (Tok.is(tok::l_brace)) { 01413 ExprType = CompoundLiteral; 01414 return ParseCompoundLiteralExpression(Ty.get(), OpenLoc, RParenLoc); 01415 } 01416 01417 if (ExprType == CastExpr) { 01418 // We parsed '(' type-name ')' and the thing after it wasn't a '{'. 01419 01420 if (Ty.isInvalid()) 01421 return ExprError(); 01422 01423 CastTy = Ty.get(); 01424 01425 // Note that this doesn't parse the subsequent cast-expression, it just 01426 // returns the parsed type to the callee. 01427 if (stopIfCastExpr) 01428 return OwningExprResult(Actions); 01429 01430 // Reject the cast of super idiom in ObjC. 01431 if (Tok.is(tok::identifier) && getLang().ObjC1 && 01432 Tok.getIdentifierInfo() == Ident_super && 01433 CurScope->isInObjcMethodScope() && 01434 GetLookAheadToken(1).isNot(tok::period)) { 01435 Diag(Tok.getLocation(), diag::err_illegal_super_cast) 01436 << SourceRange(OpenLoc, RParenLoc); 01437 return ExprError(); 01438 } 01439 01440 // Parse the cast-expression that follows it next. 01441 // TODO: For cast expression with CastTy. 01442 Result = ParseCastExpression(false, false, CastTy); 01443 if (!Result.isInvalid()) 01444 Result = Actions.ActOnCastExpr(CurScope, OpenLoc, CastTy, RParenLoc, 01445 move(Result)); 01446 return move(Result); 01447 } 01448 01449 Diag(Tok, diag::err_expected_lbrace_in_compound_literal); 01450 return ExprError(); 01451 } else if (TypeOfCast) { 01452 // Parse the expression-list. 01453 ExprVector ArgExprs(Actions); 01454 CommaLocsTy CommaLocs; 01455 01456 if (!ParseExpressionList(ArgExprs, CommaLocs)) { 01457 ExprType = SimpleExpr; 01458 Result = Actions.ActOnParenOrParenListExpr(OpenLoc, Tok.getLocation(), 01459 move_arg(ArgExprs), TypeOfCast); 01460 } 01461 } else { 01462 Result = ParseExpression(); 01463 ExprType = SimpleExpr; 01464 if (!Result.isInvalid() && Tok.is(tok::r_paren)) 01465 Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), move(Result)); 01466 } 01467 01468 // Match the ')'. 01469 if (Result.isInvalid()) { 01470 SkipUntil(tok::r_paren); 01471 return ExprError(); 01472 } 01473 01474 if (Tok.is(tok::r_paren)) 01475 RParenLoc = ConsumeParen(); 01476 else 01477 MatchRHSPunctuation(tok::r_paren, OpenLoc); 01478 01479 return move(Result); 01480 } 01481 01482 /// ParseCompoundLiteralExpression - We have parsed the parenthesized type-name 01483 /// and we are at the left brace. 01484 /// 01485 /// postfix-expression: [C99 6.5.2] 01486 /// '(' type-name ')' '{' initializer-list '}' 01487 /// '(' type-name ')' '{' initializer-list ',' '}' 01488 /// 01489 Parser::OwningExprResult 01490 Parser::ParseCompoundLiteralExpression(TypeTy *Ty, 01491 SourceLocation LParenLoc, 01492 SourceLocation RParenLoc) { 01493 assert(Tok.is(tok::l_brace) && "Not a compound literal!"); 01494 if (!getLang().C99) // Compound literals don't exist in C90. 01495 Diag(LParenLoc, diag::ext_c99_compound_literal); 01496 OwningExprResult Result = ParseInitializer(); 01497 if (!Result.isInvalid() && Ty) 01498 return Actions.ActOnCompoundLiteral(LParenLoc, Ty, RParenLoc, move(Result)); 01499 return move(Result); 01500 } 01501 01502 /// ParseStringLiteralExpression - This handles the various token types that 01503 /// form string literals, and also handles string concatenation [C99 5.1.1.2, 01504 /// translation phase #6]. 01505 /// 01506 /// primary-expression: [C99 6.5.1] 01507 /// string-literal 01508 Parser::OwningExprResult Parser::ParseStringLiteralExpression() { 01509 assert(isTokenStringLiteral() && "Not a string literal!"); 01510 01511 // String concat. Note that keywords like __func__ and __FUNCTION__ are not 01512 // considered to be strings for concatenation purposes. 01513 llvm::SmallVector<Token, 4> StringToks; 01514 01515 do { 01516 StringToks.push_back(Tok); 01517 ConsumeStringToken(); 01518 } while (isTokenStringLiteral()); 01519 01520 // Pass the set of string tokens, ready for concatenation, to the actions. 01521 return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size()); 01522 } 01523 01524 /// ParseExpressionList - Used for C/C++ (argument-)expression-list. 01525 /// 01526 /// argument-expression-list: 01527 /// assignment-expression 01528 /// argument-expression-list , assignment-expression 01529 /// 01530 /// [C++] expression-list: 01531 /// [C++] assignment-expression 01532 /// [C++] expression-list , assignment-expression 01533 /// 01534 bool Parser::ParseExpressionList(ExprListTy &Exprs, CommaLocsTy &CommaLocs, 01535 void (Action::*Completer)(Scope *S, 01536 void *Data, 01537 ExprTy **Args, 01538 unsigned NumArgs), 01539 void *Data) { 01540 while (1) { 01541 if (Tok.is(tok::code_completion)) { 01542 if (Completer) 01543 (Actions.*Completer)(CurScope, Data, Exprs.data(), Exprs.size()); 01544 ConsumeToken(); 01545 } 01546 01547 OwningExprResult Expr(ParseAssignmentExpression()); 01548 if (Expr.isInvalid()) 01549 return true; 01550 01551 Exprs.push_back(Expr.release()); 01552 01553 if (Tok.isNot(tok::comma)) 01554 return false; 01555 // Move to the next argument, remember where the comma was. 01556 CommaLocs.push_back(ConsumeToken()); 01557 } 01558 } 01559 01560 /// ParseBlockId - Parse a block-id, which roughly looks like int (int x). 01561 /// 01562 /// [clang] block-id: 01563 /// [clang] specifier-qualifier-list block-declarator 01564 /// 01565 void Parser::ParseBlockId() { 01566 // Parse the specifier-qualifier-list piece. 01567 DeclSpec DS; 01568 ParseSpecifierQualifierList(DS); 01569 01570 // Parse the block-declarator. 01571 Declarator DeclaratorInfo(DS, Declarator::BlockLiteralContext); 01572 ParseDeclarator(DeclaratorInfo); 01573 01574 // We do this for: ^ __attribute__((noreturn)) {, as DS has the attributes. 01575 DeclaratorInfo.AddAttributes(DS.TakeAttributes(), 01576 SourceLocation()); 01577 01578 if (Tok.is(tok::kw___attribute)) { 01579 SourceLocation Loc; 01580 AttributeList *AttrList = ParseGNUAttributes(&Loc); 01581 DeclaratorInfo.AddAttributes(AttrList, Loc); 01582 } 01583 01584 // Inform sema that we are starting a block. 01585 Actions.ActOnBlockArguments(DeclaratorInfo, CurScope); 01586 } 01587 01588 /// ParseBlockLiteralExpression - Parse a block literal, which roughly looks 01589 /// like ^(int x){ return x+1; } 01590 /// 01591 /// block-literal: 01592 /// [clang] '^' block-args[opt] compound-statement 01593 /// [clang] '^' block-id compound-statement 01594 /// [clang] block-args: 01595 /// [clang] '(' parameter-list ')' 01596 /// 01597 Parser::OwningExprResult Parser::ParseBlockLiteralExpression() { 01598 assert(Tok.is(tok::caret) && "block literal starts with ^"); 01599 SourceLocation CaretLoc = ConsumeToken(); 01600 01601 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), CaretLoc, 01602 "block literal parsing"); 01603 01604 // Enter a scope to hold everything within the block. This includes the 01605 // argument decls, decls within the compound expression, etc. This also 01606 // allows determining whether a variable reference inside the block is 01607 // within or outside of the block. 01608 ParseScope BlockScope(this, Scope::BlockScope | Scope::FnScope | 01609 Scope::BreakScope | Scope::ContinueScope | 01610 Scope::DeclScope); 01611 01612 // Inform sema that we are starting a block. 01613 Actions.ActOnBlockStart(CaretLoc, CurScope); 01614 01615 // Parse the return type if present. 01616 DeclSpec DS; 01617 Declarator ParamInfo(DS, Declarator::BlockLiteralContext); 01618 // FIXME: Since the return type isn't actually parsed, it can't be used to 01619 // fill ParamInfo with an initial valid range, so do it manually. 01620 ParamInfo.SetSourceRange(SourceRange(Tok.getLocation(), Tok.getLocation())); 01621 01622 // If this block has arguments, parse them. There is no ambiguity here with 01623 // the expression case, because the expression case requires a parameter list. 01624 if (Tok.is(tok::l_paren)) { 01625 ParseParenDeclarator(ParamInfo); 01626 // Parse the pieces after the identifier as if we had "int(...)". 01627 // SetIdentifier sets the source range end, but in this case we're past 01628 // that location. 01629 SourceLocation Tmp = ParamInfo.getSourceRange().getEnd(); 01630 ParamInfo.SetIdentifier(0, CaretLoc); 01631 ParamInfo.SetRangeEnd(Tmp); 01632 if (ParamInfo.isInvalidType()) { 01633 // If there was an error parsing the arguments, they may have 01634 // tried to use ^(x+y) which requires an argument list. Just 01635 // skip the whole block literal. 01636 Actions.ActOnBlockError(CaretLoc, CurScope); 01637 return ExprError(); 01638 } 01639 01640 if (Tok.is(tok::kw___attribute)) { 01641 SourceLocation Loc; 01642 AttributeList *AttrList = ParseGNUAttributes(&Loc); 01643 ParamInfo.AddAttributes(AttrList, Loc); 01644 } 01645 01646 // Inform sema that we are starting a block. 01647 Actions.ActOnBlockArguments(ParamInfo, CurScope); 01648 } else if (!Tok.is(tok::l_brace)) { 01649 ParseBlockId(); 01650 } else { 01651 // Otherwise, pretend we saw (void). 01652 ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(true, false, 01653 SourceLocation(), 01654 0, 0, 0, 01655 false, SourceLocation(), 01656 false, 0, 0, 0, 01657 CaretLoc, CaretLoc, 01658 ParamInfo), 01659 CaretLoc); 01660 01661 if (Tok.is(tok::kw___attribute)) { 01662 SourceLocation Loc; 01663 AttributeList *AttrList = ParseGNUAttributes(&Loc); 01664 ParamInfo.AddAttributes(AttrList, Loc); 01665 } 01666 01667 // Inform sema that we are starting a block. 01668 Actions.ActOnBlockArguments(ParamInfo, CurScope); 01669 } 01670 01671 01672 OwningExprResult Result(Actions, true); 01673 if (!Tok.is(tok::l_brace)) { 01674 // Saw something like: ^expr 01675 Diag(Tok, diag::err_expected_expression); 01676 Actions.ActOnBlockError(CaretLoc, CurScope); 01677 return ExprError(); 01678 } 01679 01680 OwningStmtResult Stmt(ParseCompoundStatementBody()); 01681 if (!Stmt.isInvalid()) 01682 Result = Actions.ActOnBlockStmtExpr(CaretLoc, move(Stmt), CurScope); 01683 else 01684 Actions.ActOnBlockError(CaretLoc, CurScope); 01685 return move(Result); 01686 }