#include "clang/Parse/Parser.h"
#include "clang/Sema/DeclSpec.h"
#include "clang/Sema/Scope.h"
#include "clang/Sema/ParsedTemplate.h"
#include "clang/Sema/TypoCorrection.h"
#include "clang/Basic/PrettyStackTrace.h"
#include "RAIIObjectsForParser.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/SmallString.h"
using namespace clang;
static prec::Level getBinOpPrecedence(tok::TokenKind Kind,
bool GreaterThanIsOperator,
bool CPlusPlus0x) {
switch (Kind) {
case tok::greater:
if (GreaterThanIsOperator)
return prec::Relational;
return prec::Unknown;
case tok::greatergreater:
if (GreaterThanIsOperator || !CPlusPlus0x)
return prec::Shift;
return prec::Unknown;
default: return prec::Unknown;
case tok::comma: return prec::Comma;
case tok::equal:
case tok::starequal:
case tok::slashequal:
case tok::percentequal:
case tok::plusequal:
case tok::minusequal:
case tok::lesslessequal:
case tok::greatergreaterequal:
case tok::ampequal:
case tok::caretequal:
case tok::pipeequal: return prec::Assignment;
case tok::question: return prec::Conditional;
case tok::pipepipe: return prec::LogicalOr;
case tok::ampamp: return prec::LogicalAnd;
case tok::pipe: return prec::InclusiveOr;
case tok::caret: return prec::ExclusiveOr;
case tok::amp: return prec::And;
case tok::exclaimequal:
case tok::equalequal: return prec::Equality;
case tok::lessequal:
case tok::less:
case tok::greaterequal: return prec::Relational;
case tok::lessless: return prec::Shift;
case tok::plus:
case tok::minus: return prec::Additive;
case tok::percent:
case tok::slash:
case tok::star: return prec::Multiplicative;
case tok::periodstar:
case tok::arrowstar: return prec::PointerToMember;
}
}
ExprResult Parser::ParseExpression(TypeCastState isTypeCast) {
ExprResult LHS(ParseAssignmentExpression(isTypeCast));
return ParseRHSOfBinaryExpression(LHS, prec::Comma);
}
ExprResult
Parser::ParseExpressionWithLeadingAt(SourceLocation AtLoc) {
ExprResult LHS(ParseObjCAtExpression(AtLoc));
return ParseRHSOfBinaryExpression(LHS, prec::Comma);
}
ExprResult
Parser::ParseExpressionWithLeadingExtension(SourceLocation ExtLoc) {
ExprResult LHS(true);
{
ExtensionRAIIObject O(Diags);
LHS = ParseCastExpression(false);
}
if (!LHS.isInvalid())
LHS = Actions.ActOnUnaryOp(getCurScope(), ExtLoc, tok::kw___extension__,
LHS.take());
return ParseRHSOfBinaryExpression(LHS, prec::Comma);
}
ExprResult Parser::ParseAssignmentExpression(TypeCastState isTypeCast) {
if (Tok.is(tok::code_completion)) {
Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression);
cutOffParsing();
return ExprError();
}
if (Tok.is(tok::kw_throw))
return ParseThrowExpression();
ExprResult LHS = ParseCastExpression(false,
false,
isTypeCast);
return ParseRHSOfBinaryExpression(LHS, prec::Assignment);
}
ExprResult
Parser::ParseAssignmentExprWithObjCMessageExprStart(SourceLocation LBracLoc,
SourceLocation SuperLoc,
ParsedType ReceiverType,
Expr *ReceiverExpr) {
ExprResult R
= ParseObjCMessageExpressionBody(LBracLoc, SuperLoc,
ReceiverType, ReceiverExpr);
R = ParsePostfixExpressionSuffix(R);
return ParseRHSOfBinaryExpression(R, prec::Assignment);
}
ExprResult Parser::ParseConstantExpression(TypeCastState isTypeCast) {
EnterExpressionEvaluationContext Unevaluated(Actions,
Sema::ConstantEvaluated);
ExprResult LHS(ParseCastExpression(false, false, isTypeCast));
ExprResult Res(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
return Actions.ActOnConstantExpression(Res);
}
bool Parser::isNotExpressionStart() {
tok::TokenKind K = Tok.getKind();
if (K == tok::l_brace || K == tok::r_brace ||
K == tok::kw_for || K == tok::kw_while ||
K == tok::kw_if || K == tok::kw_else ||
K == tok::kw_goto || K == tok::kw_try)
return true;
return isKnownToBeDeclarationSpecifier();
}
ExprResult
Parser::ParseRHSOfBinaryExpression(ExprResult LHS, prec::Level MinPrec) {
prec::Level NextTokPrec = getBinOpPrecedence(Tok.getKind(),
GreaterThanIsOperator,
getLangOpts().CPlusPlus0x);
SourceLocation ColonLoc;
while (1) {
if (NextTokPrec < MinPrec)
return LHS;
Token OpToken = Tok;
ConsumeToken();
if (OpToken.is(tok::comma) && isNotExpressionStart()) {
PP.EnterToken(Tok);
Tok = OpToken;
return LHS;
}
ExprResult TernaryMiddle(true);
if (NextTokPrec == prec::Conditional) {
if (Tok.isNot(tok::colon)) {
ColonProtectionRAIIObject X(*this);
TernaryMiddle = ParseExpression();
if (TernaryMiddle.isInvalid()) {
LHS = ExprError();
TernaryMiddle = 0;
}
} else {
TernaryMiddle = 0;
Diag(Tok, diag::ext_gnu_conditional_expr);
}
if (Tok.is(tok::colon)) {
ColonLoc = ConsumeToken();
} else {
SourceLocation FILoc = Tok.getLocation();
const char *FIText = ": ";
const SourceManager &SM = PP.getSourceManager();
if (FILoc.isFileID() || PP.isAtStartOfMacroExpansion(FILoc, &FILoc)) {
assert(FILoc.isFileID());
bool IsInvalid = false;
const char *SourcePtr =
SM.getCharacterData(FILoc.getLocWithOffset(-1), &IsInvalid);
if (!IsInvalid && *SourcePtr == ' ') {
SourcePtr =
SM.getCharacterData(FILoc.getLocWithOffset(-2), &IsInvalid);
if (!IsInvalid && *SourcePtr == ' ') {
FILoc = FILoc.getLocWithOffset(-1);
FIText = ":";
}
}
}
Diag(Tok, diag::err_expected_colon)
<< FixItHint::CreateInsertion(FILoc, FIText);
Diag(OpToken, diag::note_matching) << "?";
ColonLoc = Tok.getLocation();
}
}
if (Tok.is(tok::code_completion) && NextTokPrec == prec::Assignment) {
Actions.CodeCompleteAssignmentRHS(getCurScope(), LHS.get());
cutOffParsing();
return ExprError();
}
ExprResult RHS;
bool RHSIsInitList = false;
if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
RHS = ParseBraceInitializer();
RHSIsInitList = true;
} else if (getLangOpts().CPlusPlus && NextTokPrec <= prec::Conditional)
RHS = ParseAssignmentExpression();
else
RHS = ParseCastExpression(false);
if (RHS.isInvalid())
LHS = ExprError();
prec::Level ThisPrec = NextTokPrec;
NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
getLangOpts().CPlusPlus0x);
bool isRightAssoc = ThisPrec == prec::Conditional ||
ThisPrec == prec::Assignment;
if (ThisPrec < NextTokPrec ||
(ThisPrec == NextTokPrec && isRightAssoc)) {
if (!RHS.isInvalid() && RHSIsInitList) {
Diag(Tok, diag::err_init_list_bin_op)
<< 0 << PP.getSpelling(Tok) << Actions.getExprRange(RHS.get());
RHS = ExprError();
}
RHS = ParseRHSOfBinaryExpression(RHS,
static_cast<prec::Level>(ThisPrec + !isRightAssoc));
RHSIsInitList = false;
if (RHS.isInvalid())
LHS = ExprError();
NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
getLangOpts().CPlusPlus0x);
}
assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
if (!RHS.isInvalid() && RHSIsInitList) {
if (ThisPrec == prec::Assignment) {
Diag(OpToken, diag::warn_cxx98_compat_generalized_initializer_lists)
<< Actions.getExprRange(RHS.get());
} else {
Diag(OpToken, diag::err_init_list_bin_op)
<< 1 << PP.getSpelling(OpToken)
<< Actions.getExprRange(RHS.get());
LHS = ExprError();
}
}
if (!LHS.isInvalid()) {
if (TernaryMiddle.isInvalid()) {
if (!GreaterThanIsOperator && OpToken.is(tok::greatergreater))
SuggestParentheses(OpToken.getLocation(),
diag::warn_cxx0x_right_shift_in_template_arg,
SourceRange(Actions.getExprRange(LHS.get()).getBegin(),
Actions.getExprRange(RHS.get()).getEnd()));
LHS = Actions.ActOnBinOp(getCurScope(), OpToken.getLocation(),
OpToken.getKind(), LHS.take(), RHS.take());
} else
LHS = Actions.ActOnConditionalOp(OpToken.getLocation(), ColonLoc,
LHS.take(), TernaryMiddle.take(),
RHS.take());
}
}
}
ExprResult Parser::ParseCastExpression(bool isUnaryExpression,
bool isAddressOfOperand,
TypeCastState isTypeCast) {
bool NotCastExpr;
ExprResult Res = ParseCastExpression(isUnaryExpression,
isAddressOfOperand,
NotCastExpr,
isTypeCast);
if (NotCastExpr)
Diag(Tok, diag::err_expected_expression);
return Res;
}
namespace {
class CastExpressionIdValidator : public CorrectionCandidateCallback {
public:
CastExpressionIdValidator(bool AllowTypes, bool AllowNonTypes)
: AllowNonTypes(AllowNonTypes) {
WantTypeSpecifiers = AllowTypes;
}
virtual bool ValidateCandidate(const TypoCorrection &candidate) {
NamedDecl *ND = candidate.getCorrectionDecl();
if (!ND)
return candidate.isKeyword();
if (isa<TypeDecl>(ND))
return WantTypeSpecifiers;
return AllowNonTypes;
}
private:
bool AllowNonTypes;
};
}
ExprResult Parser::ParseCastExpression(bool isUnaryExpression,
bool isAddressOfOperand,
bool &NotCastExpr,
TypeCastState isTypeCast) {
ExprResult Res;
tok::TokenKind SavedKind = Tok.getKind();
NotCastExpr = false;
switch (SavedKind) {
case tok::l_paren: {
ParenParseOption ParenExprType =
(isUnaryExpression && !getLangOpts().CPlusPlus)? CompoundLiteral : CastExpr;
ParsedType CastTy;
SourceLocation RParenLoc;
{
ColonProtectionRAIIObject X(*this, false);
Res = ParseParenExpression(ParenExprType, false,
isTypeCast == IsTypeCast, CastTy, RParenLoc);
}
switch (ParenExprType) {
case SimpleExpr: break; case CompoundStmt: break; case CompoundLiteral:
break;
case CastExpr:
return Res;
}
break;
}
case tok::numeric_constant:
Res = Actions.ActOnNumericConstant(Tok, getCurScope());
ConsumeToken();
break;
case tok::kw_true:
case tok::kw_false:
return ParseCXXBoolLiteral();
case tok::kw___objc_yes:
case tok::kw___objc_no:
return ParseObjCBoolLiteral();
case tok::kw_nullptr:
Diag(Tok, diag::warn_cxx98_compat_nullptr);
return Actions.ActOnCXXNullPtrLiteral(ConsumeToken());
case tok::annot_primary_expr:
assert(Res.get() == 0 && "Stray primary-expression annotation?");
Res = getExprAnnotation(Tok);
ConsumeToken();
break;
case tok::kw_decltype:
case tok::identifier: { if (getLangOpts().CPlusPlus) {
const Token &Next = NextToken();
if (Next.is(tok::l_paren) &&
Tok.is(tok::identifier) &&
Tok.getIdentifierInfo()->hasRevertedTokenIDToIdentifier()) {
IdentifierInfo *II = Tok.getIdentifierInfo();
if (RevertableTypeTraits.empty()) {
#define RTT_JOIN(X,Y) X##Y
#define REVERTABLE_TYPE_TRAIT(Name) \
RevertableTypeTraits[PP.getIdentifierInfo(#Name)] \
= RTT_JOIN(tok::kw_,Name)
REVERTABLE_TYPE_TRAIT(__is_arithmetic);
REVERTABLE_TYPE_TRAIT(__is_convertible);
REVERTABLE_TYPE_TRAIT(__is_empty);
REVERTABLE_TYPE_TRAIT(__is_floating_point);
REVERTABLE_TYPE_TRAIT(__is_function);
REVERTABLE_TYPE_TRAIT(__is_fundamental);
REVERTABLE_TYPE_TRAIT(__is_integral);
REVERTABLE_TYPE_TRAIT(__is_member_function_pointer);
REVERTABLE_TYPE_TRAIT(__is_member_pointer);
REVERTABLE_TYPE_TRAIT(__is_pod);
REVERTABLE_TYPE_TRAIT(__is_pointer);
REVERTABLE_TYPE_TRAIT(__is_same);
REVERTABLE_TYPE_TRAIT(__is_scalar);
REVERTABLE_TYPE_TRAIT(__is_signed);
REVERTABLE_TYPE_TRAIT(__is_unsigned);
REVERTABLE_TYPE_TRAIT(__is_void);
#undef REVERTABLE_TYPE_TRAIT
#undef RTT_JOIN
}
llvm::SmallDenseMap<IdentifierInfo *, tok::TokenKind>::iterator Known
= RevertableTypeTraits.find(II);
if (Known != RevertableTypeTraits.end()) {
Tok.setKind(Known->second);
return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
NotCastExpr, isTypeCast);
}
}
if (Next.is(tok::coloncolon) ||
(!ColonIsSacred && Next.is(tok::colon)) ||
Next.is(tok::less) ||
Next.is(tok::l_paren) ||
Next.is(tok::l_brace)) {
if (TryAnnotateTypeOrScopeToken())
return ExprError();
if (!Tok.is(tok::identifier))
return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
}
}
IdentifierInfo &II = *Tok.getIdentifierInfo();
SourceLocation ILoc = ConsumeToken();
if (getLangOpts().ObjC1 && Tok.is(tok::period) &&
(Actions.getTypeName(II, ILoc, getCurScope()) ||
(&II == Ident_super && getCurScope()->isInObjcMethodScope()))) {
ConsumeToken();
if (Tok.isNot(tok::identifier) &&
!(getLangOpts().CPlusPlus && Tok.is(tok::kw_class))) {
Diag(Tok, diag::err_expected_property_name);
return ExprError();
}
IdentifierInfo &PropertyName = *Tok.getIdentifierInfo();
SourceLocation PropertyLoc = ConsumeToken();
Res = Actions.ActOnClassPropertyRefExpr(II, PropertyName,
ILoc, PropertyLoc);
break;
}
if (getLangOpts().ObjC1 && &II == Ident_super && !InMessageExpression &&
getCurScope()->isInObjcMethodScope() &&
((Tok.is(tok::identifier) &&
(NextToken().is(tok::colon) || NextToken().is(tok::r_square))) ||
Tok.is(tok::code_completion))) {
Res = ParseObjCMessageExpressionBody(SourceLocation(), ILoc, ParsedType(),
0);
break;
}
if (getLangOpts().ObjC1 &&
((Tok.is(tok::identifier) && !InMessageExpression) ||
Tok.is(tok::code_completion))) {
const Token& Next = NextToken();
if (Tok.is(tok::code_completion) ||
Next.is(tok::colon) || Next.is(tok::r_square))
if (ParsedType Typ = Actions.getTypeName(II, ILoc, getCurScope()))
if (Typ.get()->isObjCObjectOrInterfaceType()) {
DeclSpec DS(AttrFactory);
DS.SetRangeStart(ILoc);
DS.SetRangeEnd(ILoc);
const char *PrevSpec = 0;
unsigned DiagID;
DS.SetTypeSpecType(TST_typename, ILoc, PrevSpec, DiagID, Typ);
Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
TypeResult Ty = Actions.ActOnTypeName(getCurScope(),
DeclaratorInfo);
if (Ty.isInvalid())
break;
Res = ParseObjCMessageExpressionBody(SourceLocation(),
SourceLocation(),
Ty.get(), 0);
break;
}
}
if (isAddressOfOperand && isPostfixExpressionSuffixStart())
isAddressOfOperand = false;
UnqualifiedId Name;
CXXScopeSpec ScopeSpec;
SourceLocation TemplateKWLoc;
CastExpressionIdValidator Validator(isTypeCast != NotTypeCast,
isTypeCast != IsTypeCast);
Name.setIdentifier(&II, ILoc);
Res = Actions.ActOnIdExpression(getCurScope(), ScopeSpec, TemplateKWLoc,
Name, Tok.is(tok::l_paren),
isAddressOfOperand, &Validator);
break;
}
case tok::char_constant: case tok::wide_char_constant:
case tok::utf16_char_constant:
case tok::utf32_char_constant:
Res = Actions.ActOnCharacterConstant(Tok, getCurScope());
ConsumeToken();
break;
case tok::kw___func__: case tok::kw___FUNCTION__: case tok::kw_L__FUNCTION__: case tok::kw___PRETTY_FUNCTION__: Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind);
ConsumeToken();
break;
case tok::string_literal: case tok::wide_string_literal:
case tok::utf8_string_literal:
case tok::utf16_string_literal:
case tok::utf32_string_literal:
Res = ParseStringLiteralExpression(true);
break;
case tok::kw__Generic: Res = ParseGenericSelectionExpression();
break;
case tok::kw___builtin_va_arg:
case tok::kw___builtin_offsetof:
case tok::kw___builtin_choose_expr:
case tok::kw___builtin_astype: return ParseBuiltinPrimaryExpression();
case tok::kw___null:
return Actions.ActOnGNUNullExpr(ConsumeToken());
case tok::plusplus: case tok::minusminus: { SourceLocation SavedLoc = ConsumeToken();
Res = ParseCastExpression(!getLangOpts().CPlusPlus);
if (!Res.isInvalid())
Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
return Res;
}
case tok::amp: { SourceLocation SavedLoc = ConsumeToken();
Res = ParseCastExpression(false, true);
if (!Res.isInvalid())
Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
return Res;
}
case tok::star: case tok::plus: case tok::minus: case tok::tilde: case tok::exclaim: case tok::kw___real: case tok::kw___imag: { SourceLocation SavedLoc = ConsumeToken();
Res = ParseCastExpression(false);
if (!Res.isInvalid())
Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
return Res;
}
case tok::kw___extension__:{ ExtensionRAIIObject O(Diags); SourceLocation SavedLoc = ConsumeToken();
Res = ParseCastExpression(false);
if (!Res.isInvalid())
Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
return Res;
}
case tok::kw__Alignof: if (!getLangOpts().C11)
Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
case tok::kw_alignof: case tok::kw___alignof: case tok::kw_sizeof: case tok::kw_vec_step: return ParseUnaryExprOrTypeTraitExpression();
case tok::ampamp: { SourceLocation AmpAmpLoc = ConsumeToken();
if (Tok.isNot(tok::identifier))
return ExprError(Diag(Tok, diag::err_expected_ident));
if (getCurScope()->getFnParent() == 0)
return ExprError(Diag(Tok, diag::err_address_of_label_outside_fn));
Diag(AmpAmpLoc, diag::ext_gnu_address_of_label);
LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
Tok.getLocation());
Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(), LD);
ConsumeToken();
return Res;
}
case tok::kw_const_cast:
case tok::kw_dynamic_cast:
case tok::kw_reinterpret_cast:
case tok::kw_static_cast:
Res = ParseCXXCasts();
break;
case tok::kw_typeid:
Res = ParseCXXTypeid();
break;
case tok::kw___uuidof:
Res = ParseCXXUuidof();
break;
case tok::kw_this:
Res = ParseCXXThis();
break;
case tok::annot_typename:
if (isStartOfObjCClassMessageMissingOpenBracket()) {
ParsedType Type = getTypeAnnotation(Tok);
DeclSpec DS(AttrFactory);
DS.SetRangeStart(Tok.getLocation());
DS.SetRangeEnd(Tok.getLastLoc());
const char *PrevSpec = 0;
unsigned DiagID;
DS.SetTypeSpecType(TST_typename, Tok.getAnnotationEndLoc(),
PrevSpec, DiagID, Type);
Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
TypeResult Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
if (Ty.isInvalid())
break;
ConsumeToken();
Res = ParseObjCMessageExpressionBody(SourceLocation(), SourceLocation(),
Ty.get(), 0);
break;
}
case tok::annot_decltype:
case tok::kw_char:
case tok::kw_wchar_t:
case tok::kw_char16_t:
case tok::kw_char32_t:
case tok::kw_bool:
case tok::kw_short:
case tok::kw_int:
case tok::kw_long:
case tok::kw___int64:
case tok::kw___int128:
case tok::kw_signed:
case tok::kw_unsigned:
case tok::kw_half:
case tok::kw_float:
case tok::kw_double:
case tok::kw_void:
case tok::kw_typename:
case tok::kw_typeof:
case tok::kw___vector: {
if (!getLangOpts().CPlusPlus) {
Diag(Tok, diag::err_expected_expression);
return ExprError();
}
if (SavedKind == tok::kw_typename) {
if (TryAnnotateTypeOrScopeToken())
return ExprError();
}
DeclSpec DS(AttrFactory);
ParseCXXSimpleTypeSpecifier(DS);
if (Tok.isNot(tok::l_paren) &&
(!getLangOpts().CPlusPlus0x || Tok.isNot(tok::l_brace)))
return ExprError(Diag(Tok, diag::err_expected_lparen_after_type)
<< DS.getSourceRange());
if (Tok.is(tok::l_brace))
Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Res = ParseCXXTypeConstructExpression(DS);
break;
}
case tok::annot_cxxscope: { if (TryAnnotateTypeOrScopeToken())
return ExprError();
if (!Tok.is(tok::annot_cxxscope))
return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
NotCastExpr, isTypeCast);
Token Next = NextToken();
if (Next.is(tok::annot_template_id)) {
TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
if (TemplateId->Kind == TNK_Type_template) {
CXXScopeSpec SS;
ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
false);
AnnotateTemplateIdTokenAsType();
return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
NotCastExpr, isTypeCast);
}
}
Res = ParseCXXIdExpression(isAddressOfOperand);
break;
}
case tok::annot_template_id: { TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
if (TemplateId->Kind == TNK_Type_template) {
AnnotateTemplateIdTokenAsType();
return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
NotCastExpr, isTypeCast);
}
}
case tok::kw_operator: Res = ParseCXXIdExpression(isAddressOfOperand);
break;
case tok::coloncolon: {
if (TryAnnotateTypeOrScopeToken())
return ExprError();
if (!Tok.is(tok::coloncolon))
return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
SourceLocation CCLoc = ConsumeToken();
if (Tok.is(tok::kw_new))
return ParseCXXNewExpression(true, CCLoc);
if (Tok.is(tok::kw_delete))
return ParseCXXDeleteExpression(true, CCLoc);
Diag(CCLoc, diag::err_expected_expression);
return ExprError();
}
case tok::kw_new: return ParseCXXNewExpression(false, Tok.getLocation());
case tok::kw_delete: return ParseCXXDeleteExpression(false, Tok.getLocation());
case tok::kw_noexcept: { Diag(Tok, diag::warn_cxx98_compat_noexcept_expr);
SourceLocation KeyLoc = ConsumeToken();
BalancedDelimiterTracker T(*this, tok::l_paren);
if (T.expectAndConsume(diag::err_expected_lparen_after, "noexcept"))
return ExprError();
EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
ExprResult Result = ParseExpression();
T.consumeClose();
if (!Result.isInvalid())
Result = Actions.ActOnNoexceptExpr(KeyLoc, T.getOpenLocation(),
Result.take(), T.getCloseLocation());
return Result;
}
case tok::kw___is_abstract: case tok::kw___is_class:
case tok::kw___is_empty:
case tok::kw___is_enum:
case tok::kw___is_interface_class:
case tok::kw___is_literal:
case tok::kw___is_arithmetic:
case tok::kw___is_integral:
case tok::kw___is_floating_point:
case tok::kw___is_complete_type:
case tok::kw___is_void:
case tok::kw___is_array:
case tok::kw___is_function:
case tok::kw___is_reference:
case tok::kw___is_lvalue_reference:
case tok::kw___is_rvalue_reference:
case tok::kw___is_fundamental:
case tok::kw___is_object:
case tok::kw___is_scalar:
case tok::kw___is_compound:
case tok::kw___is_pointer:
case tok::kw___is_member_object_pointer:
case tok::kw___is_member_function_pointer:
case tok::kw___is_member_pointer:
case tok::kw___is_const:
case tok::kw___is_volatile:
case tok::kw___is_standard_layout:
case tok::kw___is_signed:
case tok::kw___is_unsigned:
case tok::kw___is_literal_type:
case tok::kw___is_pod:
case tok::kw___is_polymorphic:
case tok::kw___is_trivial:
case tok::kw___is_trivially_copyable:
case tok::kw___is_union:
case tok::kw___is_final:
case tok::kw___has_trivial_constructor:
case tok::kw___has_trivial_copy:
case tok::kw___has_trivial_assign:
case tok::kw___has_trivial_destructor:
case tok::kw___has_nothrow_assign:
case tok::kw___has_nothrow_copy:
case tok::kw___has_nothrow_constructor:
case tok::kw___has_virtual_destructor:
return ParseUnaryTypeTrait();
case tok::kw___builtin_types_compatible_p:
case tok::kw___is_base_of:
case tok::kw___is_same:
case tok::kw___is_convertible:
case tok::kw___is_convertible_to:
case tok::kw___is_trivially_assignable:
return ParseBinaryTypeTrait();
case tok::kw___is_trivially_constructible:
return ParseTypeTrait();
case tok::kw___array_rank:
case tok::kw___array_extent:
return ParseArrayTypeTrait();
case tok::kw___is_lvalue_expr:
case tok::kw___is_rvalue_expr:
return ParseExpressionTrait();
case tok::at: {
SourceLocation AtLoc = ConsumeToken();
return ParseObjCAtExpression(AtLoc);
}
case tok::caret:
Res = ParseBlockLiteralExpression();
break;
case tok::code_completion: {
Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression);
cutOffParsing();
return ExprError();
}
case tok::l_square:
if (getLangOpts().CPlusPlus0x) {
if (getLangOpts().ObjC1) {
Res = TryParseLambdaExpression();
if (!Res.isInvalid() && !Res.get())
Res = ParseObjCMessageExpression();
break;
}
Res = ParseLambdaExpression();
break;
}
if (getLangOpts().ObjC1) {
Res = ParseObjCMessageExpression();
break;
}
default:
NotCastExpr = true;
return ExprError();
}
return ParsePostfixExpressionSuffix(Res);
}
ExprResult
Parser::ParsePostfixExpressionSuffix(ExprResult LHS) {
SourceLocation Loc;
while (1) {
switch (Tok.getKind()) {
case tok::code_completion:
if (InMessageExpression)
return LHS;
Actions.CodeCompletePostfixExpression(getCurScope(), LHS);
cutOffParsing();
return ExprError();
case tok::identifier:
if (getLangOpts().ObjC1 && !InMessageExpression &&
(NextToken().is(tok::colon) || NextToken().is(tok::r_square))) {
LHS = ParseObjCMessageExpressionBody(SourceLocation(), SourceLocation(),
ParsedType(), LHS.get());
break;
}
default: return LHS;
case tok::l_square: { if (getLangOpts().ObjC1 && Tok.isAtStartOfLine() &&
isSimpleObjCMessageExpression())
return LHS;
if (CheckProhibitedCXX11Attribute())
return ExprError();
BalancedDelimiterTracker T(*this, tok::l_square);
T.consumeOpen();
Loc = T.getOpenLocation();
ExprResult Idx;
if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Idx = ParseBraceInitializer();
} else
Idx = ParseExpression();
SourceLocation RLoc = Tok.getLocation();
if (!LHS.isInvalid() && !Idx.isInvalid() && Tok.is(tok::r_square)) {
LHS = Actions.ActOnArraySubscriptExpr(getCurScope(), LHS.take(), Loc,
Idx.take(), RLoc);
} else
LHS = ExprError();
T.consumeClose();
break;
}
case tok::l_paren: case tok::lesslessless: { tok::TokenKind OpKind = Tok.getKind();
InMessageExpressionRAIIObject InMessage(*this, false);
Expr *ExecConfig = 0;
BalancedDelimiterTracker PT(*this, tok::l_paren);
if (OpKind == tok::lesslessless) {
ExprVector ExecConfigExprs;
CommaLocsTy ExecConfigCommaLocs;
SourceLocation OpenLoc = ConsumeToken();
if (ParseExpressionList(ExecConfigExprs, ExecConfigCommaLocs)) {
LHS = ExprError();
}
SourceLocation CloseLoc = Tok.getLocation();
if (Tok.is(tok::greatergreatergreater)) {
ConsumeToken();
} else if (LHS.isInvalid()) {
SkipUntil(tok::greatergreatergreater);
} else {
Diag(Tok, diag::err_expected_ggg);
Diag(OpenLoc, diag::note_matching) << "<<<";
SkipUntil(tok::greatergreatergreater);
LHS = ExprError();
}
if (!LHS.isInvalid()) {
if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen, ""))
LHS = ExprError();
else
Loc = PrevTokLocation;
}
if (!LHS.isInvalid()) {
ExprResult ECResult = Actions.ActOnCUDAExecConfigExpr(getCurScope(),
OpenLoc,
ExecConfigExprs,
CloseLoc);
if (ECResult.isInvalid())
LHS = ExprError();
else
ExecConfig = ECResult.get();
}
} else {
PT.consumeOpen();
Loc = PT.getOpenLocation();
}
ExprVector ArgExprs;
CommaLocsTy CommaLocs;
if (Tok.is(tok::code_completion)) {
Actions.CodeCompleteCall(getCurScope(), LHS.get(),
llvm::ArrayRef<Expr *>());
cutOffParsing();
return ExprError();
}
if (OpKind == tok::l_paren || !LHS.isInvalid()) {
if (Tok.isNot(tok::r_paren)) {
if (ParseExpressionList(ArgExprs, CommaLocs, &Sema::CodeCompleteCall,
LHS.get())) {
LHS = ExprError();
}
}
}
if (LHS.isInvalid()) {
SkipUntil(tok::r_paren);
} else if (Tok.isNot(tok::r_paren)) {
PT.consumeClose();
LHS = ExprError();
} else {
assert((ArgExprs.size() == 0 ||
ArgExprs.size()-1 == CommaLocs.size())&&
"Unexpected number of commas!");
LHS = Actions.ActOnCallExpr(getCurScope(), LHS.take(), Loc,
ArgExprs, Tok.getLocation(),
ExecConfig);
PT.consumeClose();
}
break;
}
case tok::arrow:
case tok::period: {
tok::TokenKind OpKind = Tok.getKind();
SourceLocation OpLoc = ConsumeToken();
CXXScopeSpec SS;
ParsedType ObjectType;
bool MayBePseudoDestructor = false;
if (getLangOpts().CPlusPlus && !LHS.isInvalid()) {
LHS = Actions.ActOnStartCXXMemberReference(getCurScope(), LHS.take(),
OpLoc, OpKind, ObjectType,
MayBePseudoDestructor);
if (LHS.isInvalid())
break;
ParseOptionalCXXScopeSpecifier(SS, ObjectType,
false,
&MayBePseudoDestructor);
if (SS.isNotEmpty())
ObjectType = ParsedType();
}
if (Tok.is(tok::code_completion)) {
Actions.CodeCompleteMemberReferenceExpr(getCurScope(), LHS.get(),
OpLoc, OpKind == tok::arrow);
cutOffParsing();
return ExprError();
}
if (MayBePseudoDestructor && !LHS.isInvalid()) {
LHS = ParseCXXPseudoDestructor(LHS.take(), OpLoc, OpKind, SS,
ObjectType);
break;
}
SourceLocation TemplateKWLoc;
UnqualifiedId Name;
if (getLangOpts().ObjC2 && OpKind == tok::period && Tok.is(tok::kw_class)) {
IdentifierInfo *Id = Tok.getIdentifierInfo();
SourceLocation Loc = ConsumeToken();
Name.setIdentifier(Id, Loc);
} else if (ParseUnqualifiedId(SS,
false,
true,
getLangOpts().MicrosoftExt,
ObjectType, TemplateKWLoc, Name))
LHS = ExprError();
if (!LHS.isInvalid())
LHS = Actions.ActOnMemberAccessExpr(getCurScope(), LHS.take(), OpLoc,
OpKind, SS, TemplateKWLoc, Name,
CurParsedObjCImpl ? CurParsedObjCImpl->Dcl : 0,
Tok.is(tok::l_paren));
break;
}
case tok::plusplus: case tok::minusminus: if (!LHS.isInvalid()) {
LHS = Actions.ActOnPostfixUnaryOp(getCurScope(), Tok.getLocation(),
Tok.getKind(), LHS.take());
}
ConsumeToken();
break;
}
}
}
ExprResult
Parser::ParseExprAfterUnaryExprOrTypeTrait(const Token &OpTok,
bool &isCastExpr,
ParsedType &CastTy,
SourceRange &CastRange) {
assert((OpTok.is(tok::kw_typeof) || OpTok.is(tok::kw_sizeof) ||
OpTok.is(tok::kw___alignof) || OpTok.is(tok::kw_alignof) ||
OpTok.is(tok::kw__Alignof) || OpTok.is(tok::kw_vec_step)) &&
"Not a typeof/sizeof/alignof/vec_step expression!");
ExprResult Operand;
if (Tok.isNot(tok::l_paren)) {
isCastExpr = false;
if (OpTok.is(tok::kw_typeof) && !getLangOpts().CPlusPlus) {
Diag(Tok,diag::err_expected_lparen_after_id) << OpTok.getIdentifierInfo();
return ExprError();
}
Operand = ParseCastExpression(true);
} else {
ParenParseOption ExprType = CastExpr;
SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
Operand = ParseParenExpression(ExprType, true,
false, CastTy, RParenLoc);
CastRange = SourceRange(LParenLoc, RParenLoc);
if (ExprType == CastExpr) {
isCastExpr = true;
return ExprEmpty();
}
if (getLangOpts().CPlusPlus || OpTok.isNot(tok::kw_typeof)) {
if (!Operand.isInvalid())
Operand = ParsePostfixExpressionSuffix(Operand.get());
}
}
isCastExpr = false;
return Operand;
}
ExprResult Parser::ParseUnaryExprOrTypeTraitExpression() {
assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof) ||
Tok.is(tok::kw_alignof) || Tok.is(tok::kw__Alignof) ||
Tok.is(tok::kw_vec_step)) &&
"Not a sizeof/alignof/vec_step expression!");
Token OpTok = Tok;
ConsumeToken();
if (Tok.is(tok::ellipsis) && OpTok.is(tok::kw_sizeof)) {
SourceLocation EllipsisLoc = ConsumeToken();
SourceLocation LParenLoc, RParenLoc;
IdentifierInfo *Name = 0;
SourceLocation NameLoc;
if (Tok.is(tok::l_paren)) {
BalancedDelimiterTracker T(*this, tok::l_paren);
T.consumeOpen();
LParenLoc = T.getOpenLocation();
if (Tok.is(tok::identifier)) {
Name = Tok.getIdentifierInfo();
NameLoc = ConsumeToken();
T.consumeClose();
RParenLoc = T.getCloseLocation();
if (RParenLoc.isInvalid())
RParenLoc = PP.getLocForEndOfToken(NameLoc);
} else {
Diag(Tok, diag::err_expected_parameter_pack);
SkipUntil(tok::r_paren);
}
} else if (Tok.is(tok::identifier)) {
Name = Tok.getIdentifierInfo();
NameLoc = ConsumeToken();
LParenLoc = PP.getLocForEndOfToken(EllipsisLoc);
RParenLoc = PP.getLocForEndOfToken(NameLoc);
Diag(LParenLoc, diag::err_paren_sizeof_parameter_pack)
<< Name
<< FixItHint::CreateInsertion(LParenLoc, "(")
<< FixItHint::CreateInsertion(RParenLoc, ")");
} else {
Diag(Tok, diag::err_sizeof_parameter_pack);
}
if (!Name)
return ExprError();
return Actions.ActOnSizeofParameterPackExpr(getCurScope(),
OpTok.getLocation(),
*Name, NameLoc,
RParenLoc);
}
if (OpTok.is(tok::kw_alignof) || OpTok.is(tok::kw__Alignof))
Diag(OpTok, diag::warn_cxx98_compat_alignof);
EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
Sema::ReuseLambdaContextDecl);
bool isCastExpr;
ParsedType CastTy;
SourceRange CastRange;
ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok,
isCastExpr,
CastTy,
CastRange);
UnaryExprOrTypeTrait ExprKind = UETT_SizeOf;
if (OpTok.is(tok::kw_alignof) || OpTok.is(tok::kw___alignof) ||
OpTok.is(tok::kw__Alignof))
ExprKind = UETT_AlignOf;
else if (OpTok.is(tok::kw_vec_step))
ExprKind = UETT_VecStep;
if (isCastExpr)
return Actions.ActOnUnaryExprOrTypeTraitExpr(OpTok.getLocation(),
ExprKind,
true,
CastTy.getAsOpaquePtr(),
CastRange);
if (!Operand.isInvalid())
Operand = Actions.ActOnUnaryExprOrTypeTraitExpr(OpTok.getLocation(),
ExprKind,
false,
Operand.release(),
CastRange);
return Operand;
}
ExprResult Parser::ParseBuiltinPrimaryExpression() {
ExprResult Res;
const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
tok::TokenKind T = Tok.getKind();
SourceLocation StartLoc = ConsumeToken();
if (Tok.isNot(tok::l_paren))
return ExprError(Diag(Tok, diag::err_expected_lparen_after_id)
<< BuiltinII);
BalancedDelimiterTracker PT(*this, tok::l_paren);
PT.consumeOpen();
switch (T) {
default: llvm_unreachable("Not a builtin primary expression!");
case tok::kw___builtin_va_arg: {
ExprResult Expr(ParseAssignmentExpression());
if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Expr = ExprError();
TypeResult Ty = ParseTypeName();
if (Tok.isNot(tok::r_paren)) {
Diag(Tok, diag::err_expected_rparen);
Expr = ExprError();
}
if (Expr.isInvalid() || Ty.isInvalid())
Res = ExprError();
else
Res = Actions.ActOnVAArg(StartLoc, Expr.take(), Ty.get(), ConsumeParen());
break;
}
case tok::kw___builtin_offsetof: {
SourceLocation TypeLoc = Tok.getLocation();
TypeResult Ty = ParseTypeName();
if (Ty.isInvalid()) {
SkipUntil(tok::r_paren);
return ExprError();
}
if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
return ExprError();
if (Tok.isNot(tok::identifier)) {
Diag(Tok, diag::err_expected_ident);
SkipUntil(tok::r_paren);
return ExprError();
}
SmallVector<Sema::OffsetOfComponent, 4> Comps;
Comps.push_back(Sema::OffsetOfComponent());
Comps.back().isBrackets = false;
Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken();
while (1) {
if (Tok.is(tok::period)) {
Comps.push_back(Sema::OffsetOfComponent());
Comps.back().isBrackets = false;
Comps.back().LocStart = ConsumeToken();
if (Tok.isNot(tok::identifier)) {
Diag(Tok, diag::err_expected_ident);
SkipUntil(tok::r_paren);
return ExprError();
}
Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
Comps.back().LocEnd = ConsumeToken();
} else if (Tok.is(tok::l_square)) {
if (CheckProhibitedCXX11Attribute())
return ExprError();
Comps.push_back(Sema::OffsetOfComponent());
Comps.back().isBrackets = true;
BalancedDelimiterTracker ST(*this, tok::l_square);
ST.consumeOpen();
Comps.back().LocStart = ST.getOpenLocation();
Res = ParseExpression();
if (Res.isInvalid()) {
SkipUntil(tok::r_paren);
return Res;
}
Comps.back().U.E = Res.release();
ST.consumeClose();
Comps.back().LocEnd = ST.getCloseLocation();
} else {
if (Tok.isNot(tok::r_paren)) {
PT.consumeClose();
Res = ExprError();
} else if (Ty.isInvalid()) {
Res = ExprError();
} else {
PT.consumeClose();
Res = Actions.ActOnBuiltinOffsetOf(getCurScope(), StartLoc, TypeLoc,
Ty.get(), &Comps[0], Comps.size(),
PT.getCloseLocation());
}
break;
}
}
break;
}
case tok::kw___builtin_choose_expr: {
ExprResult Cond(ParseAssignmentExpression());
if (Cond.isInvalid()) {
SkipUntil(tok::r_paren);
return Cond;
}
if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
return ExprError();
ExprResult Expr1(ParseAssignmentExpression());
if (Expr1.isInvalid()) {
SkipUntil(tok::r_paren);
return Expr1;
}
if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
return ExprError();
ExprResult Expr2(ParseAssignmentExpression());
if (Expr2.isInvalid()) {
SkipUntil(tok::r_paren);
return Expr2;
}
if (Tok.isNot(tok::r_paren)) {
Diag(Tok, diag::err_expected_rparen);
return ExprError();
}
Res = Actions.ActOnChooseExpr(StartLoc, Cond.take(), Expr1.take(),
Expr2.take(), ConsumeParen());
break;
}
case tok::kw___builtin_astype: {
ExprResult Expr(ParseAssignmentExpression());
if (Expr.isInvalid()) {
SkipUntil(tok::r_paren);
return ExprError();
}
if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",
tok::r_paren))
return ExprError();
TypeResult DestTy = ParseTypeName();
if (DestTy.isInvalid())
return ExprError();
if (Tok.isNot(tok::r_paren)) {
Diag(Tok, diag::err_expected_rparen);
SkipUntil(tok::r_paren);
return ExprError();
}
Res = Actions.ActOnAsTypeExpr(Expr.take(), DestTy.get(), StartLoc,
ConsumeParen());
break;
}
}
if (Res.isInvalid())
return ExprError();
return ParsePostfixExpressionSuffix(Res.take());
}
ExprResult
Parser::ParseParenExpression(ParenParseOption &ExprType, bool stopIfCastExpr,
bool isTypeCast, ParsedType &CastTy,
SourceLocation &RParenLoc) {
assert(Tok.is(tok::l_paren) && "Not a paren expr!");
BalancedDelimiterTracker T(*this, tok::l_paren);
if (T.consumeOpen())
return ExprError();
SourceLocation OpenLoc = T.getOpenLocation();
ExprResult Result(true);
bool isAmbiguousTypeId;
CastTy = ParsedType();
if (Tok.is(tok::code_completion)) {
Actions.CodeCompleteOrdinaryName(getCurScope(),
ExprType >= CompoundLiteral? Sema::PCC_ParenthesizedExpression
: Sema::PCC_Expression);
cutOffParsing();
return ExprError();
}
bool BridgeCast = (getLangOpts().ObjC2 &&
(Tok.is(tok::kw___bridge) ||
Tok.is(tok::kw___bridge_transfer) ||
Tok.is(tok::kw___bridge_retained) ||
Tok.is(tok::kw___bridge_retain)));
if (BridgeCast && !getLangOpts().ObjCAutoRefCount) {
StringRef BridgeCastName = Tok.getName();
SourceLocation BridgeKeywordLoc = ConsumeToken();
if (!PP.getSourceManager().isInSystemHeader(BridgeKeywordLoc))
Diag(BridgeKeywordLoc, diag::warn_arc_bridge_cast_nonarc)
<< BridgeCastName
<< FixItHint::CreateReplacement(BridgeKeywordLoc, "");
BridgeCast = false;
}
if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
Diag(Tok, diag::ext_gnu_statement_expr);
Actions.ActOnStartStmtExpr();
StmtResult Stmt(ParseCompoundStatement(true));
ExprType = CompoundStmt;
if (!Stmt.isInvalid()) {
Result = Actions.ActOnStmtExpr(OpenLoc, Stmt.take(), Tok.getLocation());
} else {
Actions.ActOnStmtExprError();
}
} else if (ExprType >= CompoundLiteral && BridgeCast) {
tok::TokenKind tokenKind = Tok.getKind();
SourceLocation BridgeKeywordLoc = ConsumeToken();
ObjCBridgeCastKind Kind;
if (tokenKind == tok::kw___bridge)
Kind = OBC_Bridge;
else if (tokenKind == tok::kw___bridge_transfer)
Kind = OBC_BridgeTransfer;
else if (tokenKind == tok::kw___bridge_retained)
Kind = OBC_BridgeRetained;
else {
assert(tokenKind == tok::kw___bridge_retain);
Kind = OBC_BridgeRetained;
if (!PP.getSourceManager().isInSystemHeader(BridgeKeywordLoc))
Diag(BridgeKeywordLoc, diag::err_arc_bridge_retain)
<< FixItHint::CreateReplacement(BridgeKeywordLoc,
"__bridge_retained");
}
TypeResult Ty = ParseTypeName();
T.consumeClose();
RParenLoc = T.getCloseLocation();
ExprResult SubExpr = ParseCastExpression(false);
if (Ty.isInvalid() || SubExpr.isInvalid())
return ExprError();
return Actions.ActOnObjCBridgedCast(getCurScope(), OpenLoc, Kind,
BridgeKeywordLoc, Ty.get(),
RParenLoc, SubExpr.get());
} else if (ExprType >= CompoundLiteral &&
isTypeIdInParens(isAmbiguousTypeId)) {
if (isAmbiguousTypeId && !stopIfCastExpr) {
ExprResult res = ParseCXXAmbiguousParenExpression(ExprType, CastTy, T);
RParenLoc = T.getCloseLocation();
return res;
}
DeclSpec DS(AttrFactory);
ParseSpecifierQualifierList(DS);
Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
ParseDeclarator(DeclaratorInfo);
if (!DeclaratorInfo.isInvalidType() && Tok.is(tok::identifier) &&
!InMessageExpression && getLangOpts().ObjC1 &&
(NextToken().is(tok::colon) || NextToken().is(tok::r_square))) {
TypeResult Ty;
{
InMessageExpressionRAIIObject InMessage(*this, false);
Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
}
Result = ParseObjCMessageExpressionBody(SourceLocation(),
SourceLocation(),
Ty.get(), 0);
} else {
T.consumeClose();
RParenLoc = T.getCloseLocation();
if (Tok.is(tok::l_brace)) {
ExprType = CompoundLiteral;
TypeResult Ty;
{
InMessageExpressionRAIIObject InMessage(*this, false);
Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
}
return ParseCompoundLiteralExpression(Ty.get(), OpenLoc, RParenLoc);
}
if (ExprType == CastExpr) {
if (DeclaratorInfo.isInvalidType())
return ExprError();
if (stopIfCastExpr) {
TypeResult Ty;
{
InMessageExpressionRAIIObject InMessage(*this, false);
Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
}
CastTy = Ty.get();
return ExprResult();
}
if (Tok.is(tok::identifier) && getLangOpts().ObjC1 &&
Tok.getIdentifierInfo() == Ident_super &&
getCurScope()->isInObjcMethodScope() &&
GetLookAheadToken(1).isNot(tok::period)) {
Diag(Tok.getLocation(), diag::err_illegal_super_cast)
<< SourceRange(OpenLoc, RParenLoc);
return ExprError();
}
Result = ParseCastExpression(false,
false,
IsTypeCast);
if (!Result.isInvalid()) {
Result = Actions.ActOnCastExpr(getCurScope(), OpenLoc,
DeclaratorInfo, CastTy,
RParenLoc, Result.take());
}
return Result;
}
Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
return ExprError();
}
} else if (isTypeCast) {
InMessageExpressionRAIIObject InMessage(*this, false);
ExprVector ArgExprs;
CommaLocsTy CommaLocs;
if (!ParseExpressionList(ArgExprs, CommaLocs)) {
ExprType = SimpleExpr;
Result = Actions.ActOnParenListExpr(OpenLoc, Tok.getLocation(),
ArgExprs);
}
} else {
InMessageExpressionRAIIObject InMessage(*this, false);
Result = ParseExpression(MaybeTypeCast);
ExprType = SimpleExpr;
if (!Result.isInvalid() && Tok.is(tok::r_paren))
Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), Result.take());
}
if (Result.isInvalid()) {
SkipUntil(tok::r_paren);
return ExprError();
}
T.consumeClose();
RParenLoc = T.getCloseLocation();
return Result;
}
ExprResult
Parser::ParseCompoundLiteralExpression(ParsedType Ty,
SourceLocation LParenLoc,
SourceLocation RParenLoc) {
assert(Tok.is(tok::l_brace) && "Not a compound literal!");
if (!getLangOpts().C99) Diag(LParenLoc, diag::ext_c99_compound_literal);
ExprResult Result = ParseInitializer();
if (!Result.isInvalid() && Ty)
return Actions.ActOnCompoundLiteral(LParenLoc, Ty, RParenLoc, Result.take());
return Result;
}
ExprResult Parser::ParseStringLiteralExpression(bool AllowUserDefinedLiteral) {
assert(isTokenStringLiteral() && "Not a string literal!");
SmallVector<Token, 4> StringToks;
do {
StringToks.push_back(Tok);
ConsumeStringToken();
} while (isTokenStringLiteral());
return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size(),
AllowUserDefinedLiteral ? getCurScope() : 0);
}
ExprResult Parser::ParseGenericSelectionExpression() {
assert(Tok.is(tok::kw__Generic) && "_Generic keyword expected");
SourceLocation KeyLoc = ConsumeToken();
if (!getLangOpts().C11)
Diag(KeyLoc, diag::ext_c11_generic_selection);
BalancedDelimiterTracker T(*this, tok::l_paren);
if (T.expectAndConsume(diag::err_expected_lparen))
return ExprError();
ExprResult ControllingExpr;
{
EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
ControllingExpr = ParseAssignmentExpression();
if (ControllingExpr.isInvalid()) {
SkipUntil(tok::r_paren);
return ExprError();
}
}
if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "")) {
SkipUntil(tok::r_paren);
return ExprError();
}
SourceLocation DefaultLoc;
TypeVector Types;
ExprVector Exprs;
while (1) {
ParsedType Ty;
if (Tok.is(tok::kw_default)) {
if (!DefaultLoc.isInvalid()) {
Diag(Tok, diag::err_duplicate_default_assoc);
Diag(DefaultLoc, diag::note_previous_default_assoc);
SkipUntil(tok::r_paren);
return ExprError();
}
DefaultLoc = ConsumeToken();
Ty = ParsedType();
} else {
ColonProtectionRAIIObject X(*this);
TypeResult TR = ParseTypeName();
if (TR.isInvalid()) {
SkipUntil(tok::r_paren);
return ExprError();
}
Ty = TR.release();
}
Types.push_back(Ty);
if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "")) {
SkipUntil(tok::r_paren);
return ExprError();
}
ExprResult ER(ParseAssignmentExpression());
if (ER.isInvalid()) {
SkipUntil(tok::r_paren);
return ExprError();
}
Exprs.push_back(ER.release());
if (Tok.isNot(tok::comma))
break;
ConsumeToken();
}
T.consumeClose();
if (T.getCloseLocation().isInvalid())
return ExprError();
return Actions.ActOnGenericSelectionExpr(KeyLoc, DefaultLoc,
T.getCloseLocation(),
ControllingExpr.release(),
Types, Exprs);
}
bool Parser::ParseExpressionList(SmallVectorImpl<Expr*> &Exprs,
SmallVectorImpl<SourceLocation> &CommaLocs,
void (Sema::*Completer)(Scope *S,
Expr *Data,
llvm::ArrayRef<Expr *> Args),
Expr *Data) {
while (1) {
if (Tok.is(tok::code_completion)) {
if (Completer)
(Actions.*Completer)(getCurScope(), Data, Exprs);
else
Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression);
cutOffParsing();
return true;
}
ExprResult Expr;
if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Expr = ParseBraceInitializer();
} else
Expr = ParseAssignmentExpression();
if (Tok.is(tok::ellipsis))
Expr = Actions.ActOnPackExpansion(Expr.get(), ConsumeToken());
if (Expr.isInvalid())
return true;
Exprs.push_back(Expr.release());
if (Tok.isNot(tok::comma))
return false;
CommaLocs.push_back(ConsumeToken());
}
}
void Parser::ParseBlockId(SourceLocation CaretLoc) {
if (Tok.is(tok::code_completion)) {
Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Type);
return cutOffParsing();
}
DeclSpec DS(AttrFactory);
ParseSpecifierQualifierList(DS);
Declarator DeclaratorInfo(DS, Declarator::BlockLiteralContext);
ParseDeclarator(DeclaratorInfo);
DeclaratorInfo.takeAttributes(DS.getAttributes(), SourceLocation());
MaybeParseGNUAttributes(DeclaratorInfo);
Actions.ActOnBlockArguments(CaretLoc, DeclaratorInfo, getCurScope());
}
ExprResult Parser::ParseBlockLiteralExpression() {
assert(Tok.is(tok::caret) && "block literal starts with ^");
SourceLocation CaretLoc = ConsumeToken();
PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), CaretLoc,
"block literal parsing");
ParseScope BlockScope(this, Scope::BlockScope | Scope::FnScope |
Scope::DeclScope);
Actions.ActOnBlockStart(CaretLoc, getCurScope());
DeclSpec DS(AttrFactory);
Declarator ParamInfo(DS, Declarator::BlockLiteralContext);
ParamInfo.SetSourceRange(SourceRange(Tok.getLocation(), Tok.getLocation()));
if (Tok.is(tok::l_paren)) {
ParseParenDeclarator(ParamInfo);
SourceLocation Tmp = ParamInfo.getSourceRange().getEnd();
ParamInfo.SetIdentifier(0, CaretLoc);
ParamInfo.SetRangeEnd(Tmp);
if (ParamInfo.isInvalidType()) {
Actions.ActOnBlockError(CaretLoc, getCurScope());
return ExprError();
}
MaybeParseGNUAttributes(ParamInfo);
Actions.ActOnBlockArguments(CaretLoc, ParamInfo, getCurScope());
} else if (!Tok.is(tok::l_brace)) {
ParseBlockId(CaretLoc);
} else {
ParsedAttributes attrs(AttrFactory);
ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(true, false, false,
SourceLocation(),
0, 0, 0,
true, SourceLocation(),
SourceLocation(),
SourceLocation(),
SourceLocation(),
EST_None,
SourceLocation(),
0, 0, 0, 0,
CaretLoc, CaretLoc,
ParamInfo),
attrs, CaretLoc);
MaybeParseGNUAttributes(ParamInfo);
Actions.ActOnBlockArguments(CaretLoc, ParamInfo, getCurScope());
}
ExprResult Result(true);
if (!Tok.is(tok::l_brace)) {
Diag(Tok, diag::err_expected_expression);
Actions.ActOnBlockError(CaretLoc, getCurScope());
return ExprError();
}
StmtResult Stmt(ParseCompoundStatementBody());
BlockScope.Exit();
if (!Stmt.isInvalid())
Result = Actions.ActOnBlockStmtExpr(CaretLoc, Stmt.take(), getCurScope());
else
Actions.ActOnBlockError(CaretLoc, getCurScope());
return Result;
}
ExprResult Parser::ParseObjCBoolLiteral() {
tok::TokenKind Kind = Tok.getKind();
return Actions.ActOnObjCBoolLiteral(ConsumeToken(), Kind);
}