#include "clang/Parse/ParseDiagnostic.h"
#include "clang/Parse/Parser.h"
#include "RAIIObjectsForParser.h"
#include "clang/Basic/PrettyStackTrace.h"
#include "clang/Lex/LiteralSupport.h"
#include "clang/Sema/DeclSpec.h"
#include "clang/Sema/Scope.h"
#include "clang/Sema/ParsedTemplate.h"
#include "llvm/Support/ErrorHandling.h"
using namespace clang;
static int SelectDigraphErrorMessage(tok::TokenKind Kind) {
switch (Kind) {
case tok::kw_template: return 0;
case tok::kw_const_cast: return 1;
case tok::kw_dynamic_cast: return 2;
case tok::kw_reinterpret_cast: return 3;
case tok::kw_static_cast: return 4;
default:
llvm_unreachable("Unknown type for digraph error message.");
}
}
bool Parser::areTokensAdjacent(const Token &First, const Token &Second) {
SourceManager &SM = PP.getSourceManager();
SourceLocation FirstLoc = SM.getSpellingLoc(First.getLocation());
SourceLocation FirstEnd = FirstLoc.getLocWithOffset(First.getLength());
return FirstEnd == SM.getSpellingLoc(Second.getLocation());
}
static void FixDigraph(Parser &P, Preprocessor &PP, Token &DigraphToken,
Token &ColonToken, tok::TokenKind Kind, bool AtDigraph) {
if (!AtDigraph)
PP.Lex(DigraphToken);
PP.Lex(ColonToken);
SourceRange Range;
Range.setBegin(DigraphToken.getLocation());
Range.setEnd(ColonToken.getLocation());
P.Diag(DigraphToken.getLocation(), diag::err_missing_whitespace_digraph)
<< SelectDigraphErrorMessage(Kind)
<< FixItHint::CreateReplacement(Range, "< ::");
ColonToken.setKind(tok::coloncolon);
ColonToken.setLocation(ColonToken.getLocation().getLocWithOffset(-1));
ColonToken.setLength(2);
DigraphToken.setKind(tok::less);
DigraphToken.setLength(1);
PP.EnterToken(ColonToken);
if (!AtDigraph)
PP.EnterToken(DigraphToken);
}
void Parser::CheckForTemplateAndDigraph(Token &Next, ParsedType ObjectType,
bool EnteringContext,
IdentifierInfo &II, CXXScopeSpec &SS) {
if (!Next.is(tok::l_square) || Next.getLength() != 2)
return;
Token SecondToken = GetLookAheadToken(2);
if (!SecondToken.is(tok::colon) || !areTokensAdjacent(Next, SecondToken))
return;
TemplateTy Template;
UnqualifiedId TemplateName;
TemplateName.setIdentifier(&II, Tok.getLocation());
bool MemberOfUnknownSpecialization;
if (!Actions.isTemplateName(getCurScope(), SS, false,
TemplateName, ObjectType, EnteringContext,
Template, MemberOfUnknownSpecialization))
return;
FixDigraph(*this, PP, Next, SecondToken, tok::kw_template,
false);
}
bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
ParsedType ObjectType,
bool EnteringContext,
bool *MayBePseudoDestructor,
bool IsTypename) {
assert(getLangOpts().CPlusPlus &&
"Call sites of this function should be guarded by checking for C++");
if (Tok.is(tok::annot_cxxscope)) {
Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
Tok.getAnnotationRange(),
SS);
ConsumeToken();
return false;
}
bool HasScopeSpecifier = false;
if (Tok.is(tok::coloncolon)) {
tok::TokenKind NextKind = NextToken().getKind();
if (NextKind == tok::kw_new || NextKind == tok::kw_delete)
return false;
if (Actions.ActOnCXXGlobalScopeSpecifier(getCurScope(), ConsumeToken(), SS))
return true;
HasScopeSpecifier = true;
}
bool CheckForDestructor = false;
if (MayBePseudoDestructor && *MayBePseudoDestructor) {
CheckForDestructor = true;
*MayBePseudoDestructor = false;
}
if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
DeclSpec DS(AttrFactory);
SourceLocation DeclLoc = Tok.getLocation();
SourceLocation EndLoc = ParseDecltypeSpecifier(DS);
if (Tok.isNot(tok::coloncolon)) {
AnnotateExistingDecltypeSpecifier(DS, DeclLoc, EndLoc);
return false;
}
SourceLocation CCLoc = ConsumeToken();
if (Actions.ActOnCXXNestedNameSpecifierDecltype(SS, DS, CCLoc))
SS.SetInvalid(SourceRange(DeclLoc, CCLoc));
HasScopeSpecifier = true;
}
while (true) {
if (HasScopeSpecifier) {
ObjectType = ParsedType();
if (Tok.is(tok::code_completion)) {
Actions.CodeCompleteQualifiedId(getCurScope(), SS, EnteringContext);
SS.setEndLoc(Tok.getLocation());
cutOffParsing();
return true;
}
}
if (Tok.is(tok::kw_template)) {
if (!HasScopeSpecifier && !ObjectType)
break;
TentativeParsingAction TPA(*this);
SourceLocation TemplateKWLoc = ConsumeToken();
UnqualifiedId TemplateName;
if (Tok.is(tok::identifier)) {
TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
ConsumeToken();
} else if (Tok.is(tok::kw_operator)) {
if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
TemplateName)) {
TPA.Commit();
break;
}
if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId &&
TemplateName.getKind() != UnqualifiedId::IK_LiteralOperatorId) {
Diag(TemplateName.getSourceRange().getBegin(),
diag::err_id_after_template_in_nested_name_spec)
<< TemplateName.getSourceRange();
TPA.Commit();
break;
}
} else {
TPA.Revert();
break;
}
if (Tok.isNot(tok::less)) {
TPA.Revert();
break;
}
TPA.Commit();
TemplateTy Template;
if (TemplateNameKind TNK
= Actions.ActOnDependentTemplateName(getCurScope(),
SS, TemplateKWLoc, TemplateName,
ObjectType, EnteringContext,
Template)) {
if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateKWLoc,
TemplateName, false))
return true;
} else
return true;
continue;
}
if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) {
TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde)) {
*MayBePseudoDestructor = true;
return false;
}
ConsumeToken();
assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!");
SourceLocation CCLoc = ConsumeToken();
HasScopeSpecifier = true;
ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
TemplateId->NumArgs);
if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(),
SS,
TemplateId->TemplateKWLoc,
TemplateId->Template,
TemplateId->TemplateNameLoc,
TemplateId->LAngleLoc,
TemplateArgsPtr,
TemplateId->RAngleLoc,
CCLoc,
EnteringContext)) {
SourceLocation StartLoc
= SS.getBeginLoc().isValid()? SS.getBeginLoc()
: TemplateId->TemplateNameLoc;
SS.SetInvalid(SourceRange(StartLoc, CCLoc));
}
continue;
}
if (Tok.isNot(tok::identifier))
break;
IdentifierInfo &II = *Tok.getIdentifierInfo();
Token Next = NextToken();
if (Next.is(tok::colon) && !ColonIsSacred) {
if (Actions.IsInvalidUnlessNestedName(getCurScope(), SS, II,
Tok.getLocation(),
Next.getLocation(), ObjectType,
EnteringContext) &&
PP.LookAhead(1).is(tok::identifier)) {
Diag(Next, diag::err_unexected_colon_in_nested_name_spec)
<< FixItHint::CreateReplacement(Next.getLocation(), "::");
Next.setKind(tok::coloncolon);
}
}
if (Next.is(tok::coloncolon)) {
if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) &&
!Actions.isNonTypeNestedNameSpecifier(getCurScope(), SS, Tok.getLocation(),
II, ObjectType)) {
*MayBePseudoDestructor = true;
return false;
}
SourceLocation IdLoc = ConsumeToken();
assert((Tok.is(tok::coloncolon) || Tok.is(tok::colon)) &&
"NextToken() not working properly!");
SourceLocation CCLoc = ConsumeToken();
HasScopeSpecifier = true;
if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(), II, IdLoc, CCLoc,
ObjectType, EnteringContext, SS))
SS.SetInvalid(SourceRange(IdLoc, CCLoc));
continue;
}
CheckForTemplateAndDigraph(Next, ObjectType, EnteringContext, II, SS);
if (Next.is(tok::less)) {
TemplateTy Template;
UnqualifiedId TemplateName;
TemplateName.setIdentifier(&II, Tok.getLocation());
bool MemberOfUnknownSpecialization;
if (TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS,
false,
TemplateName,
ObjectType,
EnteringContext,
Template,
MemberOfUnknownSpecialization)) {
ConsumeToken();
if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
TemplateName, false))
return true;
continue;
}
if (MemberOfUnknownSpecialization && (ObjectType || SS.isSet()) &&
(IsTypename || IsTemplateArgumentList(1))) {
unsigned DiagID = diag::err_missing_dependent_template_keyword;
if (getLangOpts().MicrosoftExt)
DiagID = diag::warn_missing_dependent_template_keyword;
Diag(Tok.getLocation(), DiagID)
<< II.getName()
<< FixItHint::CreateInsertion(Tok.getLocation(), "template ");
if (TemplateNameKind TNK
= Actions.ActOnDependentTemplateName(getCurScope(),
SS, SourceLocation(),
TemplateName, ObjectType,
EnteringContext, Template)) {
ConsumeToken();
if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
TemplateName, false))
return true;
}
else
return true;
continue;
}
}
break;
}
if (CheckForDestructor && Tok.is(tok::tilde))
*MayBePseudoDestructor = true;
return false;
}
ExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) {
CXXScopeSpec SS;
ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
SourceLocation TemplateKWLoc;
UnqualifiedId Name;
if (ParseUnqualifiedId(SS,
false,
false,
false,
ParsedType(),
TemplateKWLoc,
Name))
return ExprError();
if (isAddressOfOperand && isPostfixExpressionSuffixStart())
isAddressOfOperand = false;
return Actions.ActOnIdExpression(getCurScope(), SS, TemplateKWLoc, Name,
Tok.is(tok::l_paren), isAddressOfOperand);
}
ExprResult Parser::ParseLambdaExpression() {
LambdaIntroducer Intro;
llvm::Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro));
if (DiagID) {
Diag(Tok, DiagID.getValue());
SkipUntil(tok::r_square);
SkipUntil(tok::l_brace);
SkipUntil(tok::r_brace);
return ExprError();
}
return ParseLambdaExpressionAfterIntroducer(Intro);
}
ExprResult Parser::TryParseLambdaExpression() {
assert(getLangOpts().CPlusPlus0x
&& Tok.is(tok::l_square)
&& "Not at the start of a possible lambda expression.");
const Token Next = NextToken(), After = GetLookAheadToken(2);
if (Next.is(tok::r_square) || Next.is(tok::equal) || (Next.is(tok::amp) && (After.is(tok::r_square) ||
After.is(tok::comma))) ||
(Next.is(tok::identifier) && After.is(tok::r_square))) {
return ParseLambdaExpression();
}
if (Next.is(tok::identifier) && After.is(tok::identifier)) {
return ExprEmpty();
}
LambdaIntroducer Intro;
if (TryParseLambdaIntroducer(Intro))
return ExprEmpty();
return ParseLambdaExpressionAfterIntroducer(Intro);
}
llvm::Optional<unsigned> Parser::ParseLambdaIntroducer(LambdaIntroducer &Intro){
typedef llvm::Optional<unsigned> DiagResult;
assert(Tok.is(tok::l_square) && "Lambda expressions begin with '['.");
BalancedDelimiterTracker T(*this, tok::l_square);
T.consumeOpen();
Intro.Range.setBegin(T.getOpenLocation());
bool first = true;
if (Tok.is(tok::amp) &&
(NextToken().is(tok::comma) || NextToken().is(tok::r_square))) {
Intro.Default = LCD_ByRef;
Intro.DefaultLoc = ConsumeToken();
first = false;
} else if (Tok.is(tok::equal)) {
Intro.Default = LCD_ByCopy;
Intro.DefaultLoc = ConsumeToken();
first = false;
}
while (Tok.isNot(tok::r_square)) {
if (!first) {
if (Tok.isNot(tok::comma)) {
if (Tok.is(tok::code_completion) &&
!(getLangOpts().ObjC1 && Intro.Default == LCD_None &&
!Intro.Captures.empty())) {
Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
false);
ConsumeCodeCompletionToken();
break;
}
return DiagResult(diag::err_expected_comma_or_rsquare);
}
ConsumeToken();
}
if (Tok.is(tok::code_completion)) {
if (getLangOpts().ObjC1 && first)
Actions.CodeCompleteObjCMessageReceiver(getCurScope());
else
Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
false);
ConsumeCodeCompletionToken();
break;
}
first = false;
LambdaCaptureKind Kind = LCK_ByCopy;
SourceLocation Loc;
IdentifierInfo* Id = 0;
SourceLocation EllipsisLoc;
if (Tok.is(tok::kw_this)) {
Kind = LCK_This;
Loc = ConsumeToken();
} else {
if (Tok.is(tok::amp)) {
Kind = LCK_ByRef;
ConsumeToken();
if (Tok.is(tok::code_completion)) {
Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro,
true);
ConsumeCodeCompletionToken();
break;
}
}
if (Tok.is(tok::identifier)) {
Id = Tok.getIdentifierInfo();
Loc = ConsumeToken();
if (Tok.is(tok::ellipsis))
EllipsisLoc = ConsumeToken();
} else if (Tok.is(tok::kw_this)) {
return DiagResult(diag::err_this_captured_by_reference);
} else {
return DiagResult(diag::err_expected_capture);
}
}
Intro.addCapture(Kind, Loc, Id, EllipsisLoc);
}
T.consumeClose();
Intro.Range.setEnd(T.getCloseLocation());
return DiagResult();
}
bool Parser::TryParseLambdaIntroducer(LambdaIntroducer &Intro) {
TentativeParsingAction PA(*this);
llvm::Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro));
if (DiagID) {
PA.Revert();
return true;
}
PA.Commit();
return false;
}
ExprResult Parser::ParseLambdaExpressionAfterIntroducer(
LambdaIntroducer &Intro) {
SourceLocation LambdaBeginLoc = Intro.Range.getBegin();
Diag(LambdaBeginLoc, diag::warn_cxx98_compat_lambda);
PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), LambdaBeginLoc,
"lambda expression parsing");
DeclSpec DS(AttrFactory);
Declarator D(DS, Declarator::LambdaExprContext);
if (Tok.is(tok::l_paren)) {
ParseScope PrototypeScope(this,
Scope::FunctionPrototypeScope |
Scope::DeclScope);
SourceLocation DeclLoc, DeclEndLoc;
BalancedDelimiterTracker T(*this, tok::l_paren);
T.consumeOpen();
DeclLoc = T.getOpenLocation();
ParsedAttributes Attr(AttrFactory);
llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
SourceLocation EllipsisLoc;
if (Tok.isNot(tok::r_paren))
ParseParameterDeclarationClause(D, Attr, ParamInfo, EllipsisLoc);
T.consumeClose();
DeclEndLoc = T.getCloseLocation();
SourceLocation MutableLoc;
if (Tok.is(tok::kw_mutable)) {
MutableLoc = ConsumeToken();
DeclEndLoc = MutableLoc;
}
ExceptionSpecificationType ESpecType = EST_None;
SourceRange ESpecRange;
llvm::SmallVector<ParsedType, 2> DynamicExceptions;
llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges;
ExprResult NoexceptExpr;
ESpecType = tryParseExceptionSpecification(ESpecRange,
DynamicExceptions,
DynamicExceptionRanges,
NoexceptExpr);
if (ESpecType != EST_None)
DeclEndLoc = ESpecRange.getEnd();
MaybeParseCXX0XAttributes(Attr, &DeclEndLoc);
TypeResult TrailingReturnType;
if (Tok.is(tok::arrow)) {
SourceRange Range;
TrailingReturnType = ParseTrailingReturnType(Range);
if (Range.getEnd().isValid())
DeclEndLoc = Range.getEnd();
}
PrototypeScope.Exit();
D.AddTypeInfo(DeclaratorChunk::getFunction(true,
EllipsisLoc.isValid(),
false, EllipsisLoc,
ParamInfo.data(), ParamInfo.size(),
DS.getTypeQualifiers(),
true,
SourceLocation(),
SourceLocation(),
SourceLocation(),
MutableLoc,
ESpecType, ESpecRange.getBegin(),
DynamicExceptions.data(),
DynamicExceptionRanges.data(),
DynamicExceptions.size(),
NoexceptExpr.isUsable() ?
NoexceptExpr.get() : 0,
DeclLoc, DeclEndLoc, D,
TrailingReturnType),
Attr, DeclEndLoc);
} else if (Tok.is(tok::kw_mutable) || Tok.is(tok::arrow)) {
Diag(Tok, diag::err_lambda_missing_parens)
<< Tok.is(tok::arrow)
<< FixItHint::CreateInsertion(Tok.getLocation(), "() ");
SourceLocation DeclLoc = Tok.getLocation();
SourceLocation DeclEndLoc = DeclLoc;
SourceLocation MutableLoc;
if (Tok.is(tok::kw_mutable)) {
MutableLoc = ConsumeToken();
DeclEndLoc = MutableLoc;
}
TypeResult TrailingReturnType;
if (Tok.is(tok::arrow)) {
SourceRange Range;
TrailingReturnType = ParseTrailingReturnType(Range);
if (Range.getEnd().isValid())
DeclEndLoc = Range.getEnd();
}
ParsedAttributes Attr(AttrFactory);
D.AddTypeInfo(DeclaratorChunk::getFunction(true,
false,
false,
SourceLocation(),
0, 0,
0,
true,
SourceLocation(),
SourceLocation(),
SourceLocation(),
MutableLoc,
EST_None,
SourceLocation(),
0,
0,
0,
0,
DeclLoc, DeclEndLoc, D,
TrailingReturnType),
Attr, DeclEndLoc);
}
unsigned ScopeFlags = Scope::BlockScope | Scope::FnScope | Scope::DeclScope;
ParseScope BodyScope(this, ScopeFlags);
Actions.ActOnStartOfLambdaDefinition(Intro, D, getCurScope());
if (!Tok.is(tok::l_brace)) {
Diag(Tok, diag::err_expected_lambda_body);
Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
return ExprError();
}
StmtResult Stmt(ParseCompoundStatementBody());
BodyScope.Exit();
if (!Stmt.isInvalid())
return Actions.ActOnLambdaExpr(LambdaBeginLoc, Stmt.take(), getCurScope());
Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
return ExprError();
}
ExprResult Parser::ParseCXXCasts() {
tok::TokenKind Kind = Tok.getKind();
const char *CastName = 0;
switch (Kind) {
default: llvm_unreachable("Unknown C++ cast!");
case tok::kw_const_cast: CastName = "const_cast"; break;
case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break;
case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break;
case tok::kw_static_cast: CastName = "static_cast"; break;
}
SourceLocation OpLoc = ConsumeToken();
SourceLocation LAngleBracketLoc = Tok.getLocation();
if (Tok.is(tok::l_square) && Tok.getLength() == 2) {
Token Next = NextToken();
if (Next.is(tok::colon) && areTokensAdjacent(Tok, Next))
FixDigraph(*this, PP, Tok, Next, Kind, true);
}
if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName))
return ExprError();
DeclSpec DS(AttrFactory);
ParseSpecifierQualifierList(DS);
Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
ParseDeclarator(DeclaratorInfo);
SourceLocation RAngleBracketLoc = Tok.getLocation();
if (ExpectAndConsume(tok::greater, diag::err_expected_greater))
return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<");
SourceLocation LParenLoc, RParenLoc;
BalancedDelimiterTracker T(*this, tok::l_paren);
if (T.expectAndConsume(diag::err_expected_lparen_after, CastName))
return ExprError();
ExprResult Result = ParseExpression();
T.consumeClose();
if (!Result.isInvalid() && !DeclaratorInfo.isInvalidType())
Result = Actions.ActOnCXXNamedCast(OpLoc, Kind,
LAngleBracketLoc, DeclaratorInfo,
RAngleBracketLoc,
T.getOpenLocation(), Result.take(),
T.getCloseLocation());
return Result;
}
ExprResult Parser::ParseCXXTypeid() {
assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!");
SourceLocation OpLoc = ConsumeToken();
SourceLocation LParenLoc, RParenLoc;
BalancedDelimiterTracker T(*this, tok::l_paren);
if (T.expectAndConsume(diag::err_expected_lparen_after, "typeid"))
return ExprError();
LParenLoc = T.getOpenLocation();
ExprResult Result;
EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
Sema::ReuseLambdaContextDecl);
if (isTypeIdInParens()) {
TypeResult Ty = ParseTypeName();
T.consumeClose();
RParenLoc = T.getCloseLocation();
if (Ty.isInvalid() || RParenLoc.isInvalid())
return ExprError();
Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, true,
Ty.get().getAsOpaquePtr(), RParenLoc);
} else {
Result = ParseExpression();
if (Result.isInvalid())
SkipUntil(tok::r_paren);
else {
T.consumeClose();
RParenLoc = T.getCloseLocation();
if (RParenLoc.isInvalid())
return ExprError();
Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, false,
Result.release(), RParenLoc);
}
}
return Result;
}
ExprResult Parser::ParseCXXUuidof() {
assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!");
SourceLocation OpLoc = ConsumeToken();
BalancedDelimiterTracker T(*this, tok::l_paren);
if (T.expectAndConsume(diag::err_expected_lparen_after, "__uuidof"))
return ExprError();
ExprResult Result;
if (isTypeIdInParens()) {
TypeResult Ty = ParseTypeName();
T.consumeClose();
if (Ty.isInvalid())
return ExprError();
Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(), true,
Ty.get().getAsOpaquePtr(),
T.getCloseLocation());
} else {
EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Result = ParseExpression();
if (Result.isInvalid())
SkipUntil(tok::r_paren);
else {
T.consumeClose();
Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(),
false,
Result.release(), T.getCloseLocation());
}
}
return Result;
}
ExprResult
Parser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc,
tok::TokenKind OpKind,
CXXScopeSpec &SS,
ParsedType ObjectType) {
UnqualifiedId FirstTypeName;
SourceLocation CCLoc;
if (Tok.is(tok::identifier)) {
FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
ConsumeToken();
assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
CCLoc = ConsumeToken();
} else if (Tok.is(tok::annot_template_id)) {
FirstTypeName.setTemplateId(
(TemplateIdAnnotation *)Tok.getAnnotationValue());
ConsumeToken();
assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail");
CCLoc = ConsumeToken();
} else {
FirstTypeName.setIdentifier(0, SourceLocation());
}
assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail");
SourceLocation TildeLoc = ConsumeToken();
if (Tok.is(tok::kw_decltype) && !FirstTypeName.isValid() && SS.isEmpty()) {
DeclSpec DS(AttrFactory);
ParseDecltypeSpecifier(DS);
if (DS.getTypeSpecType() == TST_error)
return ExprError();
return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base, OpLoc,
OpKind, TildeLoc, DS,
Tok.is(tok::l_paren));
}
if (!Tok.is(tok::identifier)) {
Diag(Tok, diag::err_destructor_tilde_identifier);
return ExprError();
}
UnqualifiedId SecondTypeName;
IdentifierInfo *Name = Tok.getIdentifierInfo();
SourceLocation NameLoc = ConsumeToken();
SecondTypeName.setIdentifier(Name, NameLoc);
if (Tok.is(tok::less) &&
ParseUnqualifiedIdTemplateId(SS, SourceLocation(),
Name, NameLoc,
false, ObjectType, SecondTypeName,
true))
return ExprError();
return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base,
OpLoc, OpKind,
SS, FirstTypeName, CCLoc,
TildeLoc, SecondTypeName,
Tok.is(tok::l_paren));
}
ExprResult Parser::ParseCXXBoolLiteral() {
tok::TokenKind Kind = Tok.getKind();
return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind);
}
ExprResult Parser::ParseThrowExpression() {
assert(Tok.is(tok::kw_throw) && "Not throw!");
SourceLocation ThrowLoc = ConsumeToken();
switch (Tok.getKind()) { case tok::semi:
case tok::r_paren:
case tok::r_square:
case tok::r_brace:
case tok::colon:
case tok::comma:
return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, 0);
default:
ExprResult Expr(ParseAssignmentExpression());
if (Expr.isInvalid()) return Expr;
return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, Expr.take());
}
}
ExprResult Parser::ParseCXXThis() {
assert(Tok.is(tok::kw_this) && "Not 'this'!");
SourceLocation ThisLoc = ConsumeToken();
return Actions.ActOnCXXThis(ThisLoc);
}
ExprResult
Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get();
assert((Tok.is(tok::l_paren) ||
(getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)))
&& "Expected '(' or '{'!");
if (Tok.is(tok::l_brace)) {
ExprResult Init = ParseBraceInitializer();
if (Init.isInvalid())
return Init;
Expr *InitList = Init.take();
return Actions.ActOnCXXTypeConstructExpr(TypeRep, SourceLocation(),
MultiExprArg(&InitList, 1),
SourceLocation());
} else {
BalancedDelimiterTracker T(*this, tok::l_paren);
T.consumeOpen();
ExprVector Exprs;
CommaLocsTy CommaLocs;
if (Tok.isNot(tok::r_paren)) {
if (ParseExpressionList(Exprs, CommaLocs)) {
SkipUntil(tok::r_paren);
return ExprError();
}
}
T.consumeClose();
if (!TypeRep)
return ExprError();
assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
"Unexpected number of commas!");
return Actions.ActOnCXXTypeConstructExpr(TypeRep, T.getOpenLocation(),
Exprs,
T.getCloseLocation());
}
}
bool Parser::ParseCXXCondition(ExprResult &ExprOut,
Decl *&DeclOut,
SourceLocation Loc,
bool ConvertToBoolean) {
if (Tok.is(tok::code_completion)) {
Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition);
cutOffParsing();
return true;
}
ParsedAttributesWithRange attrs(AttrFactory);
MaybeParseCXX0XAttributes(attrs);
if (!isCXXConditionDeclaration()) {
ProhibitAttributes(attrs);
ExprOut = ParseExpression(); DeclOut = 0;
if (ExprOut.isInvalid())
return true;
if (ConvertToBoolean)
ExprOut
= Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprOut.get());
return ExprOut.isInvalid();
}
DeclSpec DS(AttrFactory);
ParseSpecifierQualifierList(DS);
Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
ParseDeclarator(DeclaratorInfo);
if (Tok.is(tok::kw_asm)) {
SourceLocation Loc;
ExprResult AsmLabel(ParseSimpleAsm(&Loc));
if (AsmLabel.isInvalid()) {
SkipUntil(tok::semi);
return true;
}
DeclaratorInfo.setAsmLabel(AsmLabel.release());
DeclaratorInfo.SetRangeEnd(Loc);
}
MaybeParseGNUAttributes(DeclaratorInfo);
DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(),
DeclaratorInfo);
DeclOut = Dcl.get();
ExprOut = ExprError();
bool CopyInitialization = isTokenEqualOrEqualTypo();
if (CopyInitialization)
ConsumeToken();
ExprResult InitExpr = ExprError();
if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
Diag(Tok.getLocation(),
diag::warn_cxx98_compat_generalized_initializer_lists);
InitExpr = ParseBraceInitializer();
} else if (CopyInitialization) {
InitExpr = ParseAssignmentExpression();
} else if (Tok.is(tok::l_paren)) {
SourceLocation LParen = ConsumeParen(), RParen = LParen;
if (SkipUntil(tok::r_paren, true, true))
RParen = ConsumeParen();
Diag(DeclOut ? DeclOut->getLocation() : LParen,
diag::err_expected_init_in_condition_lparen)
<< SourceRange(LParen, RParen);
} else {
Diag(DeclOut ? DeclOut->getLocation() : Tok.getLocation(),
diag::err_expected_init_in_condition);
}
if (!InitExpr.isInvalid())
Actions.AddInitializerToDecl(DeclOut, InitExpr.take(), !CopyInitialization,
DS.getTypeSpecType() == DeclSpec::TST_auto);
Actions.FinalizeDeclaration(DeclOut);
return false;
}
void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
DS.SetRangeStart(Tok.getLocation());
const char *PrevSpec;
unsigned DiagID;
SourceLocation Loc = Tok.getLocation();
switch (Tok.getKind()) {
case tok::identifier: case tok::coloncolon: llvm_unreachable("Annotation token should already be formed!");
default:
llvm_unreachable("Not a simple-type-specifier token!");
case tok::annot_typename: {
if (getTypeAnnotation(Tok))
DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID,
getTypeAnnotation(Tok));
else
DS.SetTypeSpecError();
DS.SetRangeEnd(Tok.getAnnotationEndLoc());
ConsumeToken();
if (Tok.is(tok::less) && getLangOpts().ObjC1)
ParseObjCProtocolQualifiers(DS);
DS.Finish(Diags, PP);
return;
}
case tok::kw_short:
DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
break;
case tok::kw_long:
DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID);
break;
case tok::kw___int64:
DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, DiagID);
break;
case tok::kw_signed:
DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
break;
case tok::kw_unsigned:
DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID);
break;
case tok::kw_void:
DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
break;
case tok::kw_char:
DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
break;
case tok::kw_int:
DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
break;
case tok::kw___int128:
DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec, DiagID);
break;
case tok::kw_half:
DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID);
break;
case tok::kw_float:
DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
break;
case tok::kw_double:
DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
break;
case tok::kw_wchar_t:
DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
break;
case tok::kw_char16_t:
DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
break;
case tok::kw_char32_t:
DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
break;
case tok::kw_bool:
DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
break;
case tok::annot_decltype:
case tok::kw_decltype:
DS.SetRangeEnd(ParseDecltypeSpecifier(DS));
return DS.Finish(Diags, PP);
case tok::kw_typeof:
ParseTypeofSpecifier(DS);
DS.Finish(Diags, PP);
return;
}
if (Tok.is(tok::annot_typename))
DS.SetRangeEnd(Tok.getAnnotationEndLoc());
else
DS.SetRangeEnd(Tok.getLocation());
ConsumeToken();
DS.Finish(Diags, PP);
}
bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) {
ParseSpecifierQualifierList(DS, AS_none, DSC_type_specifier);
DS.Finish(Diags, PP);
return false;
}
bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS,
SourceLocation TemplateKWLoc,
IdentifierInfo *Name,
SourceLocation NameLoc,
bool EnteringContext,
ParsedType ObjectType,
UnqualifiedId &Id,
bool AssumeTemplateId) {
assert((AssumeTemplateId || Tok.is(tok::less)) &&
"Expected '<' to finish parsing a template-id");
TemplateTy Template;
TemplateNameKind TNK = TNK_Non_template;
switch (Id.getKind()) {
case UnqualifiedId::IK_Identifier:
case UnqualifiedId::IK_OperatorFunctionId:
case UnqualifiedId::IK_LiteralOperatorId:
if (AssumeTemplateId) {
TNK = Actions.ActOnDependentTemplateName(getCurScope(), SS, TemplateKWLoc,
Id, ObjectType, EnteringContext,
Template);
if (TNK == TNK_Non_template)
return true;
} else {
bool MemberOfUnknownSpecialization;
TNK = Actions.isTemplateName(getCurScope(), SS,
TemplateKWLoc.isValid(), Id,
ObjectType, EnteringContext, Template,
MemberOfUnknownSpecialization);
if (TNK == TNK_Non_template && MemberOfUnknownSpecialization &&
ObjectType && IsTemplateArgumentList()) {
std::string Name;
if (Id.getKind() == UnqualifiedId::IK_Identifier)
Name = Id.Identifier->getName();
else {
Name = "operator ";
if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId)
Name += getOperatorSpelling(Id.OperatorFunctionId.Operator);
else
Name += Id.Identifier->getName();
}
Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword)
<< Name
<< FixItHint::CreateInsertion(Id.StartLocation, "template ");
TNK = Actions.ActOnDependentTemplateName(getCurScope(),
SS, TemplateKWLoc, Id,
ObjectType, EnteringContext,
Template);
if (TNK == TNK_Non_template)
return true;
}
}
break;
case UnqualifiedId::IK_ConstructorName: {
UnqualifiedId TemplateName;
bool MemberOfUnknownSpecialization;
TemplateName.setIdentifier(Name, NameLoc);
TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
TemplateName, ObjectType,
EnteringContext, Template,
MemberOfUnknownSpecialization);
break;
}
case UnqualifiedId::IK_DestructorName: {
UnqualifiedId TemplateName;
bool MemberOfUnknownSpecialization;
TemplateName.setIdentifier(Name, NameLoc);
if (ObjectType) {
TNK = Actions.ActOnDependentTemplateName(getCurScope(),
SS, TemplateKWLoc, TemplateName,
ObjectType, EnteringContext,
Template);
if (TNK == TNK_Non_template)
return true;
} else {
TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(),
TemplateName, ObjectType,
EnteringContext, Template,
MemberOfUnknownSpecialization);
if (TNK == TNK_Non_template && !Id.DestructorName.get()) {
Diag(NameLoc, diag::err_destructor_template_id)
<< Name << SS.getRange();
return true;
}
}
break;
}
default:
return false;
}
if (TNK == TNK_Non_template)
return false;
SourceLocation LAngleLoc, RAngleLoc;
TemplateArgList TemplateArgs;
if (Tok.is(tok::less) &&
ParseTemplateIdAfterTemplateName(Template, Id.StartLocation,
SS, true, LAngleLoc,
TemplateArgs,
RAngleLoc))
return true;
if (Id.getKind() == UnqualifiedId::IK_Identifier ||
Id.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) {
TemplateIdAnnotation *TemplateId
= TemplateIdAnnotation::Allocate(TemplateArgs.size(), TemplateIds);
if (Id.getKind() == UnqualifiedId::IK_Identifier) {
TemplateId->Name = Id.Identifier;
TemplateId->Operator = OO_None;
TemplateId->TemplateNameLoc = Id.StartLocation;
} else {
TemplateId->Name = 0;
TemplateId->Operator = Id.OperatorFunctionId.Operator;
TemplateId->TemplateNameLoc = Id.StartLocation;
}
TemplateId->SS = SS;
TemplateId->TemplateKWLoc = TemplateKWLoc;
TemplateId->Template = Template;
TemplateId->Kind = TNK;
TemplateId->LAngleLoc = LAngleLoc;
TemplateId->RAngleLoc = RAngleLoc;
ParsedTemplateArgument *Args = TemplateId->getTemplateArgs();
for (unsigned Arg = 0, ArgEnd = TemplateArgs.size();
Arg != ArgEnd; ++Arg)
Args[Arg] = TemplateArgs[Arg];
Id.setTemplateId(TemplateId);
return false;
}
ASTTemplateArgsPtr TemplateArgsPtr(TemplateArgs);
TypeResult Type
= Actions.ActOnTemplateIdType(SS, TemplateKWLoc,
Template, NameLoc,
LAngleLoc, TemplateArgsPtr, RAngleLoc,
true);
if (Type.isInvalid())
return true;
if (Id.getKind() == UnqualifiedId::IK_ConstructorName)
Id.setConstructorName(Type.get(), NameLoc, RAngleLoc);
else
Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc);
return false;
}
bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
ParsedType ObjectType,
UnqualifiedId &Result) {
assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword");
SourceLocation KeywordLoc = ConsumeToken();
unsigned SymbolIdx = 0;
SourceLocation SymbolLocations[3];
OverloadedOperatorKind Op = OO_None;
switch (Tok.getKind()) {
case tok::kw_new:
case tok::kw_delete: {
bool isNew = Tok.getKind() == tok::kw_new;
SymbolLocations[SymbolIdx++] = ConsumeToken();
if (Tok.is(tok::l_square) &&
(!getLangOpts().CPlusPlus0x || NextToken().isNot(tok::l_square))) {
BalancedDelimiterTracker T(*this, tok::l_square);
T.consumeOpen();
T.consumeClose();
if (T.getCloseLocation().isInvalid())
return true;
SymbolLocations[SymbolIdx++] = T.getOpenLocation();
SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Op = isNew? OO_Array_New : OO_Array_Delete;
} else {
Op = isNew? OO_New : OO_Delete;
}
break;
}
#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
case tok::Token: \
SymbolLocations[SymbolIdx++] = ConsumeToken(); \
Op = OO_##Name; \
break;
#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
#include "clang/Basic/OperatorKinds.def"
case tok::l_paren: {
BalancedDelimiterTracker T(*this, tok::l_paren);
T.consumeOpen();
T.consumeClose();
if (T.getCloseLocation().isInvalid())
return true;
SymbolLocations[SymbolIdx++] = T.getOpenLocation();
SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Op = OO_Call;
break;
}
case tok::l_square: {
BalancedDelimiterTracker T(*this, tok::l_square);
T.consumeOpen();
T.consumeClose();
if (T.getCloseLocation().isInvalid())
return true;
SymbolLocations[SymbolIdx++] = T.getOpenLocation();
SymbolLocations[SymbolIdx++] = T.getCloseLocation();
Op = OO_Subscript;
break;
}
case tok::code_completion: {
Actions.CodeCompleteOperatorName(getCurScope());
cutOffParsing();
return true;
}
default:
break;
}
if (Op != OO_None) {
Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations);
return false;
}
if (getLangOpts().CPlusPlus0x && isTokenStringLiteral()) {
Diag(Tok.getLocation(), diag::warn_cxx98_compat_literal_operator);
SourceLocation DiagLoc;
unsigned DiagId = 0;
llvm::SmallVector<Token, 4> Toks;
llvm::SmallVector<SourceLocation, 4> TokLocs;
while (isTokenStringLiteral()) {
if (!Tok.is(tok::string_literal) && !DiagId) {
DiagLoc = Tok.getLocation();
DiagId = diag::err_literal_operator_string_prefix;
}
Toks.push_back(Tok);
TokLocs.push_back(ConsumeStringToken());
}
StringLiteralParser Literal(Toks.data(), Toks.size(), PP);
if (Literal.hadError)
return true;
IdentifierInfo *II = 0;
SourceLocation SuffixLoc;
if (!Literal.getUDSuffix().empty()) {
II = &PP.getIdentifierTable().get(Literal.getUDSuffix());
SuffixLoc =
Lexer::AdvanceToTokenCharacter(TokLocs[Literal.getUDSuffixToken()],
Literal.getUDSuffixOffset(),
PP.getSourceManager(), getLangOpts());
DiagLoc = SuffixLoc;
DiagId = diag::err_literal_operator_missing_space;
} else if (Tok.is(tok::identifier)) {
II = Tok.getIdentifierInfo();
SuffixLoc = ConsumeToken();
TokLocs.push_back(SuffixLoc);
} else {
Diag(Tok.getLocation(), diag::err_expected_ident);
return true;
}
if (!Literal.GetString().empty() || Literal.Pascal) {
DiagLoc = TokLocs.front();
DiagId = diag::err_literal_operator_string_not_empty;
}
if (DiagId) {
llvm::SmallString<32> Str;
Str += "\"\" ";
Str += II->getName();
Diag(DiagLoc, DiagId) << FixItHint::CreateReplacement(
SourceRange(TokLocs.front(), TokLocs.back()), Str);
}
Result.setLiteralOperatorId(II, KeywordLoc, SuffixLoc);
return false;
}
DeclSpec DS(AttrFactory);
if (ParseCXXTypeSpecifierSeq(DS)) return true;
Declarator D(DS, Declarator::TypeNameContext);
ParseDeclaratorInternal(D, 0);
TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D);
if (Ty.isInvalid())
return true;
Result.setConversionFunctionId(KeywordLoc, Ty.get(),
D.getSourceRange().getEnd());
return false;
}
bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext,
bool AllowDestructorName,
bool AllowConstructorName,
ParsedType ObjectType,
SourceLocation& TemplateKWLoc,
UnqualifiedId &Result) {
bool TemplateSpecified = false;
if (getLangOpts().CPlusPlus && Tok.is(tok::kw_template) &&
(ObjectType || SS.isSet())) {
TemplateSpecified = true;
TemplateKWLoc = ConsumeToken();
}
if (Tok.is(tok::identifier)) {
IdentifierInfo *Id = Tok.getIdentifierInfo();
SourceLocation IdLoc = ConsumeToken();
if (!getLangOpts().CPlusPlus) {
Result.setIdentifier(Id, IdLoc);
return false;
}
if (AllowConstructorName &&
Actions.isCurrentClassName(*Id, getCurScope(), &SS)) {
ParsedType Ty = Actions.getTypeName(*Id, IdLoc, getCurScope(),
&SS, false, false,
ParsedType(),
true,
true);
Result.setConstructorName(Ty, IdLoc, IdLoc);
} else {
Result.setIdentifier(Id, IdLoc);
}
if (TemplateSpecified || Tok.is(tok::less))
return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc, Id, IdLoc,
EnteringContext, ObjectType,
Result, TemplateSpecified);
return false;
}
if (Tok.is(tok::annot_template_id)) {
TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
if (AllowConstructorName && TemplateId->Name &&
Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
if (SS.isSet()) {
Diag(TemplateId->TemplateNameLoc,
diag::err_out_of_line_constructor_template_id)
<< TemplateId->Name
<< FixItHint::CreateRemoval(
SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc));
ParsedType Ty = Actions.getTypeName(*TemplateId->Name,
TemplateId->TemplateNameLoc,
getCurScope(),
&SS, false, false,
ParsedType(),
true,
true);
Result.setConstructorName(Ty, TemplateId->TemplateNameLoc,
TemplateId->RAngleLoc);
ConsumeToken();
return false;
}
Result.setConstructorTemplateId(TemplateId);
ConsumeToken();
return false;
}
Result.setTemplateId(TemplateId);
TemplateKWLoc = TemplateId->TemplateKWLoc;
ConsumeToken();
return false;
}
if (Tok.is(tok::kw_operator)) {
if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result))
return true;
if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId ||
Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) &&
(TemplateSpecified || Tok.is(tok::less)))
return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc,
0, SourceLocation(),
EnteringContext, ObjectType,
Result, TemplateSpecified);
return false;
}
if (getLangOpts().CPlusPlus &&
(AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) {
SourceLocation TildeLoc = ConsumeToken();
if (SS.isEmpty() && Tok.is(tok::kw_decltype)) {
DeclSpec DS(AttrFactory);
SourceLocation EndLoc = ParseDecltypeSpecifier(DS);
if (ParsedType Type = Actions.getDestructorType(DS, ObjectType)) {
Result.setDestructorName(TildeLoc, Type, EndLoc);
return false;
}
return true;
}
if (Tok.isNot(tok::identifier)) {
Diag(Tok, diag::err_destructor_tilde_identifier);
return true;
}
IdentifierInfo *ClassName = Tok.getIdentifierInfo();
SourceLocation ClassNameLoc = ConsumeToken();
if (TemplateSpecified || Tok.is(tok::less)) {
Result.setDestructorName(TildeLoc, ParsedType(), ClassNameLoc);
return ParseUnqualifiedIdTemplateId(SS, TemplateKWLoc,
ClassName, ClassNameLoc,
EnteringContext, ObjectType,
Result, TemplateSpecified);
}
ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName,
ClassNameLoc, getCurScope(),
SS, ObjectType,
EnteringContext);
if (!Ty)
return true;
Result.setDestructorName(TildeLoc, Ty, ClassNameLoc);
return false;
}
Diag(Tok, diag::err_expected_unqualified_id)
<< getLangOpts().CPlusPlus;
return true;
}
ExprResult
Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
assert(Tok.is(tok::kw_new) && "expected 'new' token");
ConsumeToken();
ExprVector PlacementArgs;
SourceLocation PlacementLParen, PlacementRParen;
SourceRange TypeIdParens;
DeclSpec DS(AttrFactory);
Declarator DeclaratorInfo(DS, Declarator::CXXNewContext);
if (Tok.is(tok::l_paren)) {
BalancedDelimiterTracker T(*this, tok::l_paren);
T.consumeOpen();
PlacementLParen = T.getOpenLocation();
if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) {
SkipUntil(tok::semi, true, true);
return ExprError();
}
T.consumeClose();
PlacementRParen = T.getCloseLocation();
if (PlacementRParen.isInvalid()) {
SkipUntil(tok::semi, true, true);
return ExprError();
}
if (PlacementArgs.empty()) {
TypeIdParens = T.getRange();
PlacementLParen = PlacementRParen = SourceLocation();
} else {
if (Tok.is(tok::l_paren)) {
BalancedDelimiterTracker T(*this, tok::l_paren);
T.consumeOpen();
MaybeParseGNUAttributes(DeclaratorInfo);
ParseSpecifierQualifierList(DS);
DeclaratorInfo.SetSourceRange(DS.getSourceRange());
ParseDeclarator(DeclaratorInfo);
T.consumeClose();
TypeIdParens = T.getRange();
} else {
MaybeParseGNUAttributes(DeclaratorInfo);
if (ParseCXXTypeSpecifierSeq(DS))
DeclaratorInfo.setInvalidType(true);
else {
DeclaratorInfo.SetSourceRange(DS.getSourceRange());
ParseDeclaratorInternal(DeclaratorInfo,
&Parser::ParseDirectNewDeclarator);
}
}
}
} else {
MaybeParseGNUAttributes(DeclaratorInfo);
if (ParseCXXTypeSpecifierSeq(DS))
DeclaratorInfo.setInvalidType(true);
else {
DeclaratorInfo.SetSourceRange(DS.getSourceRange());
ParseDeclaratorInternal(DeclaratorInfo,
&Parser::ParseDirectNewDeclarator);
}
}
if (DeclaratorInfo.isInvalidType()) {
SkipUntil(tok::semi, true, true);
return ExprError();
}
ExprResult Initializer;
if (Tok.is(tok::l_paren)) {
SourceLocation ConstructorLParen, ConstructorRParen;
ExprVector ConstructorArgs;
BalancedDelimiterTracker T(*this, tok::l_paren);
T.consumeOpen();
ConstructorLParen = T.getOpenLocation();
if (Tok.isNot(tok::r_paren)) {
CommaLocsTy CommaLocs;
if (ParseExpressionList(ConstructorArgs, CommaLocs)) {
SkipUntil(tok::semi, true, true);
return ExprError();
}
}
T.consumeClose();
ConstructorRParen = T.getCloseLocation();
if (ConstructorRParen.isInvalid()) {
SkipUntil(tok::semi, true, true);
return ExprError();
}
Initializer = Actions.ActOnParenListExpr(ConstructorLParen,
ConstructorRParen,
ConstructorArgs);
} else if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus0x) {
Diag(Tok.getLocation(),
diag::warn_cxx98_compat_generalized_initializer_lists);
Initializer = ParseBraceInitializer();
}
if (Initializer.isInvalid())
return Initializer;
return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen,
PlacementArgs, PlacementRParen,
TypeIdParens, DeclaratorInfo, Initializer.take());
}
void Parser::ParseDirectNewDeclarator(Declarator &D) {
bool first = true;
while (Tok.is(tok::l_square)) {
if (CheckProhibitedCXX11Attribute())
continue;
BalancedDelimiterTracker T(*this, tok::l_square);
T.consumeOpen();
ExprResult Size(first ? ParseExpression()
: ParseConstantExpression());
if (Size.isInvalid()) {
SkipUntil(tok::r_square);
return;
}
first = false;
T.consumeClose();
ParsedAttributes Attrs(AttrFactory);
MaybeParseCXX0XAttributes(Attrs);
D.AddTypeInfo(DeclaratorChunk::getArray(0,
false, false,
Size.release(),
T.getOpenLocation(),
T.getCloseLocation()),
Attrs, T.getCloseLocation());
if (T.getCloseLocation().isInvalid())
return;
}
}
bool Parser::ParseExpressionListOrTypeId(
SmallVectorImpl<Expr*> &PlacementArgs,
Declarator &D) {
if (isTypeIdInParens()) {
ParseSpecifierQualifierList(D.getMutableDeclSpec());
D.SetSourceRange(D.getDeclSpec().getSourceRange());
ParseDeclarator(D);
return D.isInvalidType();
}
CommaLocsTy CommaLocs;
return ParseExpressionList(PlacementArgs, CommaLocs);
}
ExprResult
Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) {
assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword");
ConsumeToken();
bool ArrayDelete = false;
if (Tok.is(tok::l_square) && NextToken().is(tok::r_square)) {
ArrayDelete = true;
BalancedDelimiterTracker T(*this, tok::l_square);
T.consumeOpen();
T.consumeClose();
if (T.getCloseLocation().isInvalid())
return ExprError();
}
ExprResult Operand(ParseCastExpression(false));
if (Operand.isInvalid())
return Operand;
return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.take());
}
static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
switch(kind) {
default: llvm_unreachable("Not a known unary type trait.");
case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign;
case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor;
case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy;
case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign;
case tok::kw___has_trivial_constructor:
return UTT_HasTrivialDefaultConstructor;
case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy;
case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor;
case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor;
case tok::kw___is_abstract: return UTT_IsAbstract;
case tok::kw___is_arithmetic: return UTT_IsArithmetic;
case tok::kw___is_array: return UTT_IsArray;
case tok::kw___is_class: return UTT_IsClass;
case tok::kw___is_complete_type: return UTT_IsCompleteType;
case tok::kw___is_compound: return UTT_IsCompound;
case tok::kw___is_const: return UTT_IsConst;
case tok::kw___is_empty: return UTT_IsEmpty;
case tok::kw___is_enum: return UTT_IsEnum;
case tok::kw___is_final: return UTT_IsFinal;
case tok::kw___is_floating_point: return UTT_IsFloatingPoint;
case tok::kw___is_function: return UTT_IsFunction;
case tok::kw___is_fundamental: return UTT_IsFundamental;
case tok::kw___is_integral: return UTT_IsIntegral;
case tok::kw___is_interface_class: return UTT_IsInterfaceClass;
case tok::kw___is_lvalue_reference: return UTT_IsLvalueReference;
case tok::kw___is_member_function_pointer: return UTT_IsMemberFunctionPointer;
case tok::kw___is_member_object_pointer: return UTT_IsMemberObjectPointer;
case tok::kw___is_member_pointer: return UTT_IsMemberPointer;
case tok::kw___is_object: return UTT_IsObject;
case tok::kw___is_literal: return UTT_IsLiteral;
case tok::kw___is_literal_type: return UTT_IsLiteral;
case tok::kw___is_pod: return UTT_IsPOD;
case tok::kw___is_pointer: return UTT_IsPointer;
case tok::kw___is_polymorphic: return UTT_IsPolymorphic;
case tok::kw___is_reference: return UTT_IsReference;
case tok::kw___is_rvalue_reference: return UTT_IsRvalueReference;
case tok::kw___is_scalar: return UTT_IsScalar;
case tok::kw___is_signed: return UTT_IsSigned;
case tok::kw___is_standard_layout: return UTT_IsStandardLayout;
case tok::kw___is_trivial: return UTT_IsTrivial;
case tok::kw___is_trivially_copyable: return UTT_IsTriviallyCopyable;
case tok::kw___is_union: return UTT_IsUnion;
case tok::kw___is_unsigned: return UTT_IsUnsigned;
case tok::kw___is_void: return UTT_IsVoid;
case tok::kw___is_volatile: return UTT_IsVolatile;
}
}
static BinaryTypeTrait BinaryTypeTraitFromTokKind(tok::TokenKind kind) {
switch(kind) {
default: llvm_unreachable("Not a known binary type trait");
case tok::kw___is_base_of: return BTT_IsBaseOf;
case tok::kw___is_convertible: return BTT_IsConvertible;
case tok::kw___is_same: return BTT_IsSame;
case tok::kw___builtin_types_compatible_p: return BTT_TypeCompatible;
case tok::kw___is_convertible_to: return BTT_IsConvertibleTo;
case tok::kw___is_trivially_assignable: return BTT_IsTriviallyAssignable;
}
}
static TypeTrait TypeTraitFromTokKind(tok::TokenKind kind) {
switch (kind) {
default: llvm_unreachable("Not a known type trait");
case tok::kw___is_trivially_constructible:
return TT_IsTriviallyConstructible;
}
}
static ArrayTypeTrait ArrayTypeTraitFromTokKind(tok::TokenKind kind) {
switch(kind) {
default: llvm_unreachable("Not a known binary type trait");
case tok::kw___array_rank: return ATT_ArrayRank;
case tok::kw___array_extent: return ATT_ArrayExtent;
}
}
static ExpressionTrait ExpressionTraitFromTokKind(tok::TokenKind kind) {
switch(kind) {
default: llvm_unreachable("Not a known unary expression trait.");
case tok::kw___is_lvalue_expr: return ET_IsLValueExpr;
case tok::kw___is_rvalue_expr: return ET_IsRValueExpr;
}
}
ExprResult Parser::ParseUnaryTypeTrait() {
UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind());
SourceLocation Loc = ConsumeToken();
BalancedDelimiterTracker T(*this, tok::l_paren);
if (T.expectAndConsume(diag::err_expected_lparen))
return ExprError();
TypeResult Ty = ParseTypeName();
T.consumeClose();
if (Ty.isInvalid())
return ExprError();
return Actions.ActOnUnaryTypeTrait(UTT, Loc, Ty.get(), T.getCloseLocation());
}
ExprResult Parser::ParseBinaryTypeTrait() {
BinaryTypeTrait BTT = BinaryTypeTraitFromTokKind(Tok.getKind());
SourceLocation Loc = ConsumeToken();
BalancedDelimiterTracker T(*this, tok::l_paren);
if (T.expectAndConsume(diag::err_expected_lparen))
return ExprError();
TypeResult LhsTy = ParseTypeName();
if (LhsTy.isInvalid()) {
SkipUntil(tok::r_paren);
return ExprError();
}
if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
SkipUntil(tok::r_paren);
return ExprError();
}
TypeResult RhsTy = ParseTypeName();
if (RhsTy.isInvalid()) {
SkipUntil(tok::r_paren);
return ExprError();
}
T.consumeClose();
return Actions.ActOnBinaryTypeTrait(BTT, Loc, LhsTy.get(), RhsTy.get(),
T.getCloseLocation());
}
ExprResult Parser::ParseTypeTrait() {
TypeTrait Kind = TypeTraitFromTokKind(Tok.getKind());
SourceLocation Loc = ConsumeToken();
BalancedDelimiterTracker Parens(*this, tok::l_paren);
if (Parens.expectAndConsume(diag::err_expected_lparen))
return ExprError();
llvm::SmallVector<ParsedType, 2> Args;
do {
TypeResult Ty = ParseTypeName();
if (Ty.isInvalid()) {
Parens.skipToEnd();
return ExprError();
}
if (Tok.is(tok::ellipsis)) {
Ty = Actions.ActOnPackExpansion(Ty.get(), ConsumeToken());
if (Ty.isInvalid()) {
Parens.skipToEnd();
return ExprError();
}
}
Args.push_back(Ty.get());
if (Tok.is(tok::comma)) {
ConsumeToken();
continue;
}
break;
} while (true);
if (Parens.consumeClose())
return ExprError();
return Actions.ActOnTypeTrait(Kind, Loc, Args, Parens.getCloseLocation());
}
ExprResult Parser::ParseArrayTypeTrait() {
ArrayTypeTrait ATT = ArrayTypeTraitFromTokKind(Tok.getKind());
SourceLocation Loc = ConsumeToken();
BalancedDelimiterTracker T(*this, tok::l_paren);
if (T.expectAndConsume(diag::err_expected_lparen))
return ExprError();
TypeResult Ty = ParseTypeName();
if (Ty.isInvalid()) {
SkipUntil(tok::comma);
SkipUntil(tok::r_paren);
return ExprError();
}
switch (ATT) {
case ATT_ArrayRank: {
T.consumeClose();
return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), NULL,
T.getCloseLocation());
}
case ATT_ArrayExtent: {
if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) {
SkipUntil(tok::r_paren);
return ExprError();
}
ExprResult DimExpr = ParseExpression();
T.consumeClose();
return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), DimExpr.get(),
T.getCloseLocation());
}
}
llvm_unreachable("Invalid ArrayTypeTrait!");
}
ExprResult Parser::ParseExpressionTrait() {
ExpressionTrait ET = ExpressionTraitFromTokKind(Tok.getKind());
SourceLocation Loc = ConsumeToken();
BalancedDelimiterTracker T(*this, tok::l_paren);
if (T.expectAndConsume(diag::err_expected_lparen))
return ExprError();
ExprResult Expr = ParseExpression();
T.consumeClose();
return Actions.ActOnExpressionTrait(ET, Loc, Expr.get(),
T.getCloseLocation());
}
ExprResult
Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType,
ParsedType &CastTy,
BalancedDelimiterTracker &Tracker) {
assert(getLangOpts().CPlusPlus && "Should only be called for C++!");
assert(ExprType == CastExpr && "Compound literals are not ambiguous!");
assert(isTypeIdInParens() && "Not a type-id!");
ExprResult Result(true);
CastTy = ParsedType();
ParenParseOption ParseAs;
CachedTokens Toks;
if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) {
Tracker.consumeClose();
return ExprError();
}
if (Tok.is(tok::l_brace)) {
ParseAs = CompoundLiteral;
} else {
bool NotCastExpr;
if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) {
NotCastExpr = true;
} else {
Result = ParseCastExpression(false,
false,
NotCastExpr,
IsTypeCast);
}
ParseAs = NotCastExpr ? SimpleExpr : CastExpr;
}
Toks.push_back(Tok);
PP.EnterTokenStream(Toks.data(), Toks.size(),
true, false);
ConsumeAnyToken();
if (ParseAs >= CompoundLiteral) {
DeclSpec DS(AttrFactory);
ParseSpecifierQualifierList(DS);
Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
ParseDeclarator(DeclaratorInfo);
Tracker.consumeClose();
if (ParseAs == CompoundLiteral) {
ExprType = CompoundLiteral;
TypeResult Ty = ParseTypeName();
return ParseCompoundLiteralExpression(Ty.get(),
Tracker.getOpenLocation(),
Tracker.getCloseLocation());
}
assert(ParseAs == CastExpr);
if (DeclaratorInfo.isInvalidType())
return ExprError();
if (!Result.isInvalid())
Result = Actions.ActOnCastExpr(getCurScope(), Tracker.getOpenLocation(),
DeclaratorInfo, CastTy,
Tracker.getCloseLocation(), Result.take());
return Result;
}
assert(ParseAs == SimpleExpr);
ExprType = SimpleExpr;
Result = ParseExpression();
if (!Result.isInvalid() && Tok.is(tok::r_paren))
Result = Actions.ActOnParenExpr(Tracker.getOpenLocation(),
Tok.getLocation(), Result.take());
if (Result.isInvalid()) {
SkipUntil(tok::r_paren);
return ExprError();
}
Tracker.consumeClose();
return Result;
}