#include "clang/Parse/Parser.h"
#include "clang/Parse/ParseDiagnostic.h"
#include "clang/Basic/OpenCL.h"
#include "clang/Sema/Lookup.h"
#include "clang/Sema/Scope.h"
#include "clang/Sema/ParsedTemplate.h"
#include "clang/Sema/PrettyDeclStackTrace.h"
#include "RAIIObjectsForParser.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringSwitch.h"
using namespace clang;
TypeResult Parser::ParseTypeName(SourceRange *Range,
Declarator::TheContext Context,
AccessSpecifier AS,
Decl **OwnedType) {
DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context);
if (DSC == DSC_normal)
DSC = DSC_type_specifier;
DeclSpec DS(AttrFactory);
ParseSpecifierQualifierList(DS, AS, DSC);
if (OwnedType)
*OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Declarator DeclaratorInfo(DS, Context);
ParseDeclarator(DeclaratorInfo);
if (Range)
*Range = DeclaratorInfo.getSourceRange();
if (DeclaratorInfo.isInvalidType())
return true;
return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
}
static bool isAttributeLateParsed(const IdentifierInfo &II) {
return llvm::StringSwitch<bool>(II.getName())
#include "clang/Parse/AttrLateParsed.inc"
.Default(false);
}
void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
SourceLocation *endLoc,
LateParsedAttrList *LateAttrs) {
assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
while (Tok.is(tok::kw___attribute)) {
ConsumeToken();
if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
"attribute")) {
SkipUntil(tok::r_paren, true); return;
}
if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
SkipUntil(tok::r_paren, true); return;
}
while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
Tok.is(tok::comma)) {
if (Tok.is(tok::comma)) {
ConsumeToken();
continue;
}
IdentifierInfo *AttrName = Tok.getIdentifierInfo();
SourceLocation AttrNameLoc = ConsumeToken();
if (Tok.is(tok::l_paren)) {
if (LateAttrs && isAttributeLateParsed(*AttrName)) {
LateParsedAttribute *LA =
new LateParsedAttribute(this, *AttrName, AttrNameLoc);
LateAttrs->push_back(LA);
if (!ClassStack.empty())
getCurrentClass().LateParsedDeclarations.push_back(LA);
ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
Token Eof;
Eof.startToken();
Eof.setLocation(Tok.getLocation());
LA->Toks.push_back(Eof);
} else {
ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc);
}
} else {
attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
0, SourceLocation(), 0, 0, AttributeList::AS_GNU);
}
}
if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
SkipUntil(tok::r_paren, false);
SourceLocation Loc = Tok.getLocation();
if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
SkipUntil(tok::r_paren, false);
}
if (endLoc)
*endLoc = Loc;
}
}
void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
SourceLocation AttrNameLoc,
ParsedAttributes &Attrs,
SourceLocation *EndLoc) {
assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
if (AttrName->isStr("availability")) {
ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
return;
}
if (IsThreadSafetyAttribute(AttrName->getName())) {
ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
return;
}
if (AttrName->isStr("type_tag_for_datatype")) {
ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
return;
}
ConsumeParen();
IdentifierInfo *ParmName = 0;
SourceLocation ParmLoc;
bool BuiltinType = false;
switch (Tok.getKind()) {
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_float:
case tok::kw_double:
case tok::kw_void:
case tok::kw_typeof:
ConsumeToken();
BuiltinType = true;
break;
case tok::identifier:
ParmName = Tok.getIdentifierInfo();
ParmLoc = ConsumeToken();
break;
default:
break;
}
ExprVector ArgExprs;
if (!BuiltinType &&
(ParmLoc.isValid() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) {
if (ParmLoc.isValid())
ConsumeToken();
while (1) {
ExprResult ArgExpr(ParseAssignmentExpression());
if (ArgExpr.isInvalid()) {
SkipUntil(tok::r_paren);
return;
}
ArgExprs.push_back(ArgExpr.release());
if (Tok.isNot(tok::comma))
break;
ConsumeToken(); }
}
else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) {
if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<",
tok::greater)) {
while (Tok.is(tok::identifier)) {
ConsumeToken();
if (Tok.is(tok::greater))
break;
if (Tok.is(tok::comma)) {
ConsumeToken();
continue;
}
}
if (Tok.isNot(tok::greater))
Diag(Tok, diag::err_iboutletcollection_with_protocol);
SkipUntil(tok::r_paren, false, true); }
}
SourceLocation RParen = Tok.getLocation();
if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
AttributeList *attr =
Attrs.addNew(AttrName, SourceRange(AttrNameLoc, RParen), 0, AttrNameLoc,
ParmName, ParmLoc, ArgExprs.data(), ArgExprs.size(),
AttributeList::AS_GNU);
if (BuiltinType && attr->getKind() == AttributeList::AT_IBOutletCollection)
Diag(Tok, diag::err_iboutletcollection_builtintype);
}
}
void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName,
SourceLocation AttrNameLoc,
ParsedAttributes &Attrs)
{
BalancedDelimiterTracker T(*this, tok::l_paren);
if (T.expectAndConsume(diag::err_expected_lparen_after,
AttrName->getNameStart(), tok::r_paren))
return;
ExprResult ArgExpr(ParseConstantExpression());
if (ArgExpr.isInvalid()) {
T.skipToEnd();
return;
}
Expr *ExprList = ArgExpr.take();
Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
&ExprList, 1, AttributeList::AS_Declspec);
T.consumeClose();
}
bool Parser::IsSimpleMicrosoftDeclSpec(IdentifierInfo *Ident) {
return llvm::StringSwitch<bool>(Ident->getName())
.Case("dllimport", true)
.Case("dllexport", true)
.Case("noreturn", true)
.Case("nothrow", true)
.Case("noinline", true)
.Case("naked", true)
.Case("appdomain", true)
.Case("process", true)
.Case("jitintrinsic", true)
.Case("noalias", true)
.Case("restrict", true)
.Case("novtable", true)
.Case("selectany", true)
.Case("thread", true)
.Default(false);
}
void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident,
SourceLocation Loc,
ParsedAttributes &Attrs) {
if (llvm::StringSwitch<bool>(Ident->getName())
.Case("uuid", true)
.Case("align", true)
.Case("allocate", true)
.Default(false)) {
ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
} else if (Ident->getName() == "deprecated") {
if (Tok.getKind() == tok::l_paren)
ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
else
Attrs.addNew(Ident, Loc, 0, Loc, 0, SourceLocation(), 0, 0,
AttributeList::AS_Declspec);
} else if (Ident->getName() == "property") {
BalancedDelimiterTracker T(*this, tok::l_paren);
if (T.expectAndConsume(diag::err_expected_lparen_after,
Ident->getNameStart(), tok::r_paren))
return;
T.skipToEnd();
} else {
Diag(Loc, diag::warn_ms_declspec_unknown) << Ident;
BalancedDelimiterTracker T(*this, tok::l_paren);
if (!T.consumeOpen())
T.skipToEnd();
}
}
void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) {
assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
ConsumeToken();
BalancedDelimiterTracker T(*this, tok::l_paren);
if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
tok::r_paren))
return;
while (Tok.getKind() != tok::r_paren) {
bool IsString = Tok.getKind() == tok::string_literal ? true : false;
if (!IsString && Tok.getKind() != tok::identifier &&
Tok.getKind() != tok::kw_restrict) {
Diag(Tok, diag::err_ms_declspec_type);
T.skipToEnd();
return;
}
IdentifierInfo *AttrName;
SourceLocation AttrNameLoc;
if (IsString) {
SmallString<8> StrBuffer;
bool Invalid = false;
StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid);
if (Invalid) {
T.skipToEnd();
return;
}
AttrName = PP.getIdentifierInfo(Str);
AttrNameLoc = ConsumeStringToken();
} else {
AttrName = Tok.getIdentifierInfo();
AttrNameLoc = ConsumeToken();
}
if (IsString || IsSimpleMicrosoftDeclSpec(AttrName))
Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
0, 0, AttributeList::AS_Declspec);
else
ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs);
}
T.consumeClose();
}
void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Tok.is(tok::kw___ptr32) ||
Tok.is(tok::kw___unaligned)) {
IdentifierInfo *AttrName = Tok.getIdentifierInfo();
SourceLocation AttrNameLoc = ConsumeToken();
attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
SourceLocation(), 0, 0, AttributeList::AS_MSTypespec);
}
}
void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
while (Tok.is(tok::kw___pascal)) {
IdentifierInfo *AttrName = Tok.getIdentifierInfo();
SourceLocation AttrNameLoc = ConsumeToken();
attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
SourceLocation(), 0, 0, AttributeList::AS_MSTypespec);
}
}
void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
while (Tok.is(tok::kw___kernel)) {
SourceLocation AttrNameLoc = ConsumeToken();
attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"),
AttrNameLoc, 0, AttrNameLoc, 0,
SourceLocation(), 0, 0, AttributeList::AS_GNU);
}
}
void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
SourceLocation Loc = Tok.getLocation();
switch(Tok.getKind()) {
case tok::kw___private:
case tok::kw_private:
DS.getAttributes().addNewInteger(
Actions.getASTContext(),
PP.getIdentifierInfo("address_space"), Loc, 0);
break;
case tok::kw___global:
DS.getAttributes().addNewInteger(
Actions.getASTContext(),
PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
break;
case tok::kw___local:
DS.getAttributes().addNewInteger(
Actions.getASTContext(),
PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
break;
case tok::kw___constant:
DS.getAttributes().addNewInteger(
Actions.getASTContext(),
PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
break;
case tok::kw___read_only:
DS.getAttributes().addNewInteger(
Actions.getASTContext(),
PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
break;
case tok::kw___write_only:
DS.getAttributes().addNewInteger(
Actions.getASTContext(),
PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
break;
case tok::kw___read_write:
DS.getAttributes().addNewInteger(
Actions.getASTContext(),
PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
break;
default: break;
}
}
VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
Range = Tok.getLocation();
if (!Tok.is(tok::numeric_constant)) {
Diag(Tok, diag::err_expected_version);
SkipUntil(tok::comma, tok::r_paren, true, true, true);
return VersionTuple();
}
SmallString<512> Buffer;
Buffer.resize(Tok.getLength()+1);
const char *ThisTokBegin = &Buffer[0];
bool Invalid = false;
unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
if (Invalid)
return VersionTuple();
unsigned AfterMajor = 0;
unsigned Major = 0;
while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) {
Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
++AfterMajor;
}
if (AfterMajor == 0) {
Diag(Tok, diag::err_expected_version);
SkipUntil(tok::comma, tok::r_paren, true, true, true);
return VersionTuple();
}
if (AfterMajor == ActualLength) {
ConsumeToken();
if (Major == 0) {
Diag(Tok, diag::err_zero_version);
return VersionTuple();
}
return VersionTuple(Major);
}
if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
Diag(Tok, diag::err_expected_version);
SkipUntil(tok::comma, tok::r_paren, true, true, true);
return VersionTuple();
}
unsigned AfterMinor = AfterMajor + 1;
unsigned Minor = 0;
while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) {
Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
++AfterMinor;
}
if (AfterMinor == ActualLength) {
ConsumeToken();
if (Major == 0 && Minor == 0) {
Diag(Tok, diag::err_zero_version);
return VersionTuple();
}
return VersionTuple(Major, Minor);
}
if (ThisTokBegin[AfterMinor] != '.') {
Diag(Tok, diag::err_expected_version);
SkipUntil(tok::comma, tok::r_paren, true, true, true);
return VersionTuple();
}
unsigned AfterSubminor = AfterMinor + 1;
unsigned Subminor = 0;
while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) {
Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
++AfterSubminor;
}
if (AfterSubminor != ActualLength) {
Diag(Tok, diag::err_expected_version);
SkipUntil(tok::comma, tok::r_paren, true, true, true);
return VersionTuple();
}
ConsumeToken();
return VersionTuple(Major, Minor, Subminor);
}
void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
SourceLocation AvailabilityLoc,
ParsedAttributes &attrs,
SourceLocation *endLoc) {
SourceLocation PlatformLoc;
IdentifierInfo *Platform = 0;
enum { Introduced, Deprecated, Obsoleted, Unknown };
AvailabilityChange Changes[Unknown];
ExprResult MessageExpr;
BalancedDelimiterTracker T(*this, tok::l_paren);
if (T.consumeOpen()) {
Diag(Tok, diag::err_expected_lparen);
return;
}
if (Tok.isNot(tok::identifier)) {
Diag(Tok, diag::err_availability_expected_platform);
SkipUntil(tok::r_paren);
return;
}
Platform = Tok.getIdentifierInfo();
PlatformLoc = ConsumeToken();
if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
return;
if (!Ident_introduced) {
Ident_introduced = PP.getIdentifierInfo("introduced");
Ident_deprecated = PP.getIdentifierInfo("deprecated");
Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Ident_unavailable = PP.getIdentifierInfo("unavailable");
Ident_message = PP.getIdentifierInfo("message");
}
SourceLocation UnavailableLoc;
do {
if (Tok.isNot(tok::identifier)) {
Diag(Tok, diag::err_availability_expected_change);
SkipUntil(tok::r_paren);
return;
}
IdentifierInfo *Keyword = Tok.getIdentifierInfo();
SourceLocation KeywordLoc = ConsumeToken();
if (Keyword == Ident_unavailable) {
if (UnavailableLoc.isValid()) {
Diag(KeywordLoc, diag::err_availability_redundant)
<< Keyword << SourceRange(UnavailableLoc);
}
UnavailableLoc = KeywordLoc;
if (Tok.isNot(tok::comma))
break;
ConsumeToken();
continue;
}
if (Tok.isNot(tok::equal)) {
Diag(Tok, diag::err_expected_equal_after)
<< Keyword;
SkipUntil(tok::r_paren);
return;
}
ConsumeToken();
if (Keyword == Ident_message) {
if (!isTokenStringLiteral()) {
Diag(Tok, diag::err_expected_string_literal);
SkipUntil(tok::r_paren);
return;
}
MessageExpr = ParseStringLiteralExpression();
break;
}
SourceRange VersionRange;
VersionTuple Version = ParseVersionTuple(VersionRange);
if (Version.empty()) {
SkipUntil(tok::r_paren);
return;
}
unsigned Index;
if (Keyword == Ident_introduced)
Index = Introduced;
else if (Keyword == Ident_deprecated)
Index = Deprecated;
else if (Keyword == Ident_obsoleted)
Index = Obsoleted;
else
Index = Unknown;
if (Index < Unknown) {
if (!Changes[Index].KeywordLoc.isInvalid()) {
Diag(KeywordLoc, diag::err_availability_redundant)
<< Keyword
<< SourceRange(Changes[Index].KeywordLoc,
Changes[Index].VersionRange.getEnd());
}
Changes[Index].KeywordLoc = KeywordLoc;
Changes[Index].Version = Version;
Changes[Index].VersionRange = VersionRange;
} else {
Diag(KeywordLoc, diag::err_availability_unknown_change)
<< Keyword << VersionRange;
}
if (Tok.isNot(tok::comma))
break;
ConsumeToken();
} while (true);
if (T.consumeClose())
return;
if (endLoc)
*endLoc = T.getCloseLocation();
if (UnavailableLoc.isValid()) {
bool Complained = false;
for (unsigned Index = Introduced; Index != Unknown; ++Index) {
if (Changes[Index].KeywordLoc.isValid()) {
if (!Complained) {
Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
<< SourceRange(Changes[Index].KeywordLoc,
Changes[Index].VersionRange.getEnd());
Complained = true;
}
Changes[Index] = AvailabilityChange();
}
}
}
attrs.addNew(&Availability,
SourceRange(AvailabilityLoc, T.getCloseLocation()),
0, AvailabilityLoc,
Platform, PlatformLoc,
Changes[Introduced],
Changes[Deprecated],
Changes[Obsoleted],
UnavailableLoc, MessageExpr.take(),
AttributeList::AS_GNU);
}
void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
void Parser::LateParsedClass::ParseLexedAttributes() {
Self->ParseLexedAttributes(*Class);
}
void Parser::LateParsedAttribute::ParseLexedAttributes() {
Self->ParseLexedAttribute(*this, true, false);
}
void Parser::ParseLexedAttributes(ParsingClass &Class) {
bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
HasTemplateScope);
if (HasTemplateScope)
Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
bool AlreadyHasClassScope = Class.TopLevelClass;
unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
if (!AlreadyHasClassScope)
Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
Class.TagOrTemplate);
if (!Class.LateParsedDeclarations.empty()) {
for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
Class.LateParsedDeclarations[i]->ParseLexedAttributes();
}
}
if (!AlreadyHasClassScope)
Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
Class.TagOrTemplate);
}
void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
bool EnterScope, bool OnDefinition) {
for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
if (D)
LAs[i]->addDecl(D);
ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
delete LAs[i];
}
LAs.clear();
}
void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
bool EnterScope, bool OnDefinition) {
SourceLocation OrigLoc = Tok.getLocation();
LA.Toks.push_back(Tok);
PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
ConsumeAnyToken();
if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
Diag(Tok, diag::warn_attribute_on_function_definition)
<< LA.AttrName.getName();
}
ParsedAttributes Attrs(AttrFactory);
SourceLocation endLoc;
if (LA.Decls.size() > 0) {
Decl *D = LA.Decls[0];
NamedDecl *ND = dyn_cast<NamedDecl>(D);
RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
Sema::CXXThisScopeRAII ThisScope(Actions, RD,
0,
ND && RD && ND->isCXXInstanceMember());
if (LA.Decls.size() == 1) {
bool HasTemplateScope = EnterScope && D->isTemplateDecl();
ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
if (HasTemplateScope)
Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate();
ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope);
if (HasFunScope)
Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
if (HasFunScope) {
Actions.ActOnExitFunctionContext();
FnScope.Exit(); }
if (HasTemplateScope) {
TempScope.Exit();
}
} else {
ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
}
} else {
Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
}
for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
}
if (Tok.getLocation() != OrigLoc) {
if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
OrigLoc))
while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
ConsumeAnyToken();
}
}
bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){
return llvm::StringSwitch<bool>(AttrName)
.Case("guarded_by", true)
.Case("guarded_var", true)
.Case("pt_guarded_by", true)
.Case("pt_guarded_var", true)
.Case("lockable", true)
.Case("scoped_lockable", true)
.Case("no_thread_safety_analysis", true)
.Case("acquired_after", true)
.Case("acquired_before", true)
.Case("exclusive_lock_function", true)
.Case("shared_lock_function", true)
.Case("exclusive_trylock_function", true)
.Case("shared_trylock_function", true)
.Case("unlock_function", true)
.Case("lock_returned", true)
.Case("locks_excluded", true)
.Case("exclusive_locks_required", true)
.Case("shared_locks_required", true)
.Default(false);
}
void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
SourceLocation AttrNameLoc,
ParsedAttributes &Attrs,
SourceLocation *EndLoc) {
assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
BalancedDelimiterTracker T(*this, tok::l_paren);
T.consumeOpen();
ExprVector ArgExprs;
bool ArgExprsOk = true;
while (Tok.isNot(tok::r_paren)) {
ExprResult ArgExpr(ParseAssignmentExpression());
if (ArgExpr.isInvalid()) {
ArgExprsOk = false;
T.consumeClose();
break;
} else {
ArgExprs.push_back(ArgExpr.release());
}
if (Tok.isNot(tok::comma))
break;
ConsumeToken(); }
if (ArgExprsOk && !T.consumeClose()) {
Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
ArgExprs.data(), ArgExprs.size(), AttributeList::AS_GNU);
}
if (EndLoc)
*EndLoc = T.getCloseLocation();
}
void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName,
SourceLocation AttrNameLoc,
ParsedAttributes &Attrs,
SourceLocation *EndLoc) {
assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
BalancedDelimiterTracker T(*this, tok::l_paren);
T.consumeOpen();
if (Tok.isNot(tok::identifier)) {
Diag(Tok, diag::err_expected_ident);
T.skipToEnd();
return;
}
IdentifierInfo *ArgumentKind = Tok.getIdentifierInfo();
SourceLocation ArgumentKindLoc = ConsumeToken();
if (Tok.isNot(tok::comma)) {
Diag(Tok, diag::err_expected_comma);
T.skipToEnd();
return;
}
ConsumeToken();
SourceRange MatchingCTypeRange;
TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
if (MatchingCType.isInvalid()) {
T.skipToEnd();
return;
}
bool LayoutCompatible = false;
bool MustBeNull = false;
while (Tok.is(tok::comma)) {
ConsumeToken();
if (Tok.isNot(tok::identifier)) {
Diag(Tok, diag::err_expected_ident);
T.skipToEnd();
return;
}
IdentifierInfo *Flag = Tok.getIdentifierInfo();
if (Flag->isStr("layout_compatible"))
LayoutCompatible = true;
else if (Flag->isStr("must_be_null"))
MustBeNull = true;
else {
Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
T.skipToEnd();
return;
}
ConsumeToken(); }
if (!T.consumeClose()) {
Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc,
ArgumentKind, ArgumentKindLoc,
MatchingCType.release(), LayoutCompatible,
MustBeNull, AttributeList::AS_GNU);
}
if (EndLoc)
*EndLoc = T.getCloseLocation();
}
bool Parser::DiagnoseProhibitedCXX11Attribute() {
assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
switch (isCXX11AttributeSpecifier(true)) {
case CAK_NotAttributeSpecifier:
return false;
case CAK_InvalidAttributeSpecifier:
Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
return false;
case CAK_AttributeSpecifier:
SourceLocation BeginLoc = ConsumeBracket();
ConsumeBracket();
SkipUntil(tok::r_square, false);
assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
SourceLocation EndLoc = ConsumeBracket();
Diag(BeginLoc, diag::err_attributes_not_allowed)
<< SourceRange(BeginLoc, EndLoc);
return true;
}
llvm_unreachable("All cases handled above.");
}
void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
<< attrs.Range;
}
Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
unsigned Context,
SourceLocation &DeclEnd,
ParsedAttributesWithRange &attrs) {
ParenBraceBracketBalancer BalancerRAIIObj(*this);
ObjCDeclContextSwitch ObjCDC(*this);
Decl *SingleDecl = 0;
Decl *OwnedType = 0;
switch (Tok.getKind()) {
case tok::kw_template:
case tok::kw_export:
ProhibitAttributes(attrs);
SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
break;
case tok::kw_inline:
if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
ProhibitAttributes(attrs);
SourceLocation InlineLoc = ConsumeToken();
SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
break;
}
return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
true);
case tok::kw_namespace:
ProhibitAttributes(attrs);
SingleDecl = ParseNamespace(Context, DeclEnd);
break;
case tok::kw_using:
SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
DeclEnd, attrs, &OwnedType);
break;
case tok::kw_static_assert:
case tok::kw__Static_assert:
ProhibitAttributes(attrs);
SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
break;
default:
return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
}
return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
}
Parser::DeclGroupPtrTy
Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
SourceLocation &DeclEnd,
ParsedAttributesWithRange &attrs,
bool RequireSemi, ForRangeInit *FRI) {
ParsingDeclSpec DS(*this);
DS.takeAttributesFrom(attrs);
ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
getDeclSpecContextFromDeclaratorContext(Context));
if (Tok.is(tok::semi)) {
DeclEnd = Tok.getLocation();
if (RequireSemi) ConsumeToken();
Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
DS);
DS.complete(TheDecl);
return Actions.ConvertDeclToDeclGroup(TheDecl);
}
return ParseDeclGroup(DS, Context, false, &DeclEnd, FRI);
}
bool Parser::MightBeDeclarator(unsigned Context) {
switch (Tok.getKind()) {
case tok::annot_cxxscope:
case tok::annot_template_id:
case tok::caret:
case tok::code_completion:
case tok::coloncolon:
case tok::ellipsis:
case tok::kw___attribute:
case tok::kw_operator:
case tok::l_paren:
case tok::star:
return true;
case tok::amp:
case tok::ampamp:
return getLangOpts().CPlusPlus;
case tok::l_square: return Context == Declarator::MemberContext && getLangOpts().CPlusPlus0x &&
NextToken().is(tok::l_square);
case tok::colon: return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
case tok::identifier:
switch (NextToken().getKind()) {
case tok::code_completion:
case tok::coloncolon:
case tok::comma:
case tok::equal:
case tok::equalequal: case tok::kw_alignas:
case tok::kw_asm:
case tok::kw___attribute:
case tok::l_brace:
case tok::l_paren:
case tok::l_square:
case tok::less:
case tok::r_brace:
case tok::r_paren:
case tok::r_square:
case tok::semi:
return true;
case tok::colon:
return Context == Declarator::MemberContext ||
(getLangOpts().CPlusPlus && Context == Declarator::FileContext);
case tok::identifier: return getLangOpts().CPlusPlus0x && isCXX0XVirtSpecifier(NextToken());
default:
return false;
}
default:
return false;
}
}
void Parser::SkipMalformedDecl() {
while (true) {
switch (Tok.getKind()) {
case tok::l_brace:
ConsumeBrace();
SkipUntil(tok::r_brace, false);
if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
continue;
}
if (Tok.is(tok::semi))
ConsumeToken();
return;
case tok::l_square:
ConsumeBracket();
SkipUntil(tok::r_square, false);
continue;
case tok::l_paren:
ConsumeParen();
SkipUntil(tok::r_paren, false);
continue;
case tok::r_brace:
return;
case tok::semi:
ConsumeToken();
return;
case tok::kw_inline:
if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
(!ParsingInObjCContainer || CurParsedObjCImpl))
return;
break;
case tok::kw_namespace:
if (Tok.isAtStartOfLine() &&
(!ParsingInObjCContainer || CurParsedObjCImpl))
return;
break;
case tok::at:
if (NextToken().isObjCAtKeyword(tok::objc_end) &&
ParsingInObjCContainer)
return;
break;
case tok::minus:
case tok::plus:
if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
return;
break;
case tok::eof:
return;
default:
break;
}
ConsumeAnyToken();
}
}
Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
unsigned Context,
bool AllowFunctionDefinitions,
SourceLocation *DeclEnd,
ForRangeInit *FRI) {
ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
ParseDeclarator(D);
if (!D.hasName() && !D.mayOmitIdentifier()) {
SkipMalformedDecl();
return DeclGroupPtrTy();
}
LateParsedAttrList LateParsedAttrs;
if (D.isFunctionDeclarator())
MaybeParseGNUAttributes(D, &LateParsedAttrs);
if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
!isDeclarationAfterDeclarator()) {
if (isStartOfFunctionDefinition(D)) {
if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
Diag(Tok, diag::err_function_declared_typedef);
DS.ClearStorageClassSpecs();
}
Decl *TheDecl =
ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
return Actions.ConvertDeclToDeclGroup(TheDecl);
}
if (isDeclarationSpecifier()) {
} else {
Diag(Tok, diag::err_expected_fn_body);
SkipUntil(tok::semi);
return DeclGroupPtrTy();
}
}
if (ParseAsmAttributesAfterDeclarator(D))
return DeclGroupPtrTy();
if (FRI && Tok.is(tok::colon)) {
FRI->ColonLoc = ConsumeToken();
if (Tok.is(tok::l_brace))
FRI->RangeExpr = ParseBraceInitializer();
else
FRI->RangeExpr = ParseExpression();
Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Actions.ActOnCXXForRangeDecl(ThisDecl);
Actions.FinalizeDeclaration(ThisDecl);
D.complete(ThisDecl);
return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
}
SmallVector<Decl *, 8> DeclsInGroup;
Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
if (LateParsedAttrs.size() > 0)
ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
D.complete(FirstDecl);
if (FirstDecl)
DeclsInGroup.push_back(FirstDecl);
bool ExpectSemi = Context != Declarator::ForContext;
while (Tok.is(tok::comma)) {
SourceLocation CommaLoc = ConsumeToken();
if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
Diag(CommaLoc, diag::err_expected_semi_declaration)
<< FixItHint::CreateReplacement(CommaLoc, ";");
ExpectSemi = false;
break;
}
D.clear();
D.setCommaLoc(CommaLoc);
MaybeParseGNUAttributes(D);
ParseDeclarator(D);
if (!D.isInvalidType()) {
Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
D.complete(ThisDecl);
if (ThisDecl)
DeclsInGroup.push_back(ThisDecl);
}
}
if (DeclEnd)
*DeclEnd = Tok.getLocation();
if (ExpectSemi &&
ExpectAndConsumeSemi(Context == Declarator::FileContext
? diag::err_invalid_token_after_toplevel_declarator
: diag::err_expected_semi_declaration)) {
if (!isDeclarationSpecifier()) {
SkipUntil(tok::r_brace, true, true);
if (Tok.is(tok::semi))
ConsumeToken();
}
}
return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
DeclsInGroup.data(),
DeclsInGroup.size());
}
bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
if (Tok.is(tok::kw_asm)) {
SourceLocation Loc;
ExprResult AsmLabel(ParseSimpleAsm(&Loc));
if (AsmLabel.isInvalid()) {
SkipUntil(tok::semi, true, true);
return true;
}
D.setAsmLabel(AsmLabel.release());
D.SetRangeEnd(Loc);
}
MaybeParseGNUAttributes(D);
return false;
}
Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
const ParsedTemplateInfo &TemplateInfo) {
if (ParseAsmAttributesAfterDeclarator(D))
return 0;
return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
}
Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
const ParsedTemplateInfo &TemplateInfo) {
Decl *ThisDecl = 0;
switch (TemplateInfo.Kind) {
case ParsedTemplateInfo::NonTemplate:
ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
break;
case ParsedTemplateInfo::Template:
case ParsedTemplateInfo::ExplicitSpecialization:
ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
*TemplateInfo.TemplateParams,
D);
break;
case ParsedTemplateInfo::ExplicitInstantiation: {
DeclResult ThisRes
= Actions.ActOnExplicitInstantiation(getCurScope(),
TemplateInfo.ExternLoc,
TemplateInfo.TemplateLoc,
D);
if (ThisRes.isInvalid()) {
SkipUntil(tok::semi, true, true);
return 0;
}
ThisDecl = ThisRes.get();
break;
}
}
bool TypeContainsAuto =
D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
if (isTokenEqualOrEqualTypo()) {
ConsumeToken();
if (Tok.is(tok::kw_delete)) {
if (D.isFunctionDeclarator())
Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
<< 1 ;
else
Diag(ConsumeToken(), diag::err_deleted_non_function);
} else if (Tok.is(tok::kw_default)) {
if (D.isFunctionDeclarator())
Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
<< 0 ;
else
Diag(ConsumeToken(), diag::err_default_special_members);
} else {
if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
EnterScope(0);
Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
}
if (Tok.is(tok::code_completion)) {
Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Actions.FinalizeDeclaration(ThisDecl);
cutOffParsing();
return 0;
}
ExprResult Init(ParseInitializer());
if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
ExitScope();
}
if (Init.isInvalid()) {
SkipUntil(tok::comma, true, true);
Actions.ActOnInitializerError(ThisDecl);
} else
Actions.AddInitializerToDecl(ThisDecl, Init.take(),
false, TypeContainsAuto);
}
} else if (Tok.is(tok::l_paren)) {
BalancedDelimiterTracker T(*this, tok::l_paren);
T.consumeOpen();
ExprVector Exprs;
CommaLocsTy CommaLocs;
if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
EnterScope(0);
Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
}
if (ParseExpressionList(Exprs, CommaLocs)) {
Actions.ActOnInitializerError(ThisDecl);
SkipUntil(tok::r_paren);
if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
ExitScope();
}
} else {
T.consumeClose();
assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
"Unexpected number of commas!");
if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
ExitScope();
}
ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
T.getCloseLocation(),
Exprs);
Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
true, TypeContainsAuto);
}
} else if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace) &&
(!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
if (D.getCXXScopeSpec().isSet()) {
EnterScope(0);
Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
}
ExprResult Init(ParseBraceInitializer());
if (D.getCXXScopeSpec().isSet()) {
Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
ExitScope();
}
if (Init.isInvalid()) {
Actions.ActOnInitializerError(ThisDecl);
} else
Actions.AddInitializerToDecl(ThisDecl, Init.take(),
true, TypeContainsAuto);
} else {
Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
}
Actions.FinalizeDeclaration(ThisDecl);
return ThisDecl;
}
void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
DeclSpecContext DSC) {
ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
unsigned Specs = DS.getParsedSpecifiers();
if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
!DS.hasTypeSpecifier()) {
Diag(Tok, diag::err_expected_type);
DS.SetTypeSpecError();
} else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
!DS.hasAttributes()) {
Diag(Tok, diag::err_typename_requires_specqual);
if (!DS.hasTypeSpecifier())
DS.SetTypeSpecError();
}
if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
if (DS.getStorageClassSpecLoc().isValid())
Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
else
Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
DS.ClearStorageClassSpecs();
}
if (Specs & DeclSpec::PQ_FunctionSpecifier) {
if (DS.isInlineSpecified())
Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
if (DS.isVirtualSpecified())
Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
if (DS.isExplicitSpecified())
Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
DS.ClearFunctionSpecs();
}
if (DS.isConstexprSpecified()) {
Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
DS.ClearConstexprSpec();
}
}
static bool isValidAfterIdentifierInDeclarator(const Token &T) {
return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
}
bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
const ParsedTemplateInfo &TemplateInfo,
AccessSpecifier AS, DeclSpecContext DSC) {
assert(Tok.is(tok::identifier) && "should have identifier");
SourceLocation Loc = Tok.getLocation();
assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
(!getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt) &&
isValidAfterIdentifierInDeclarator(NextToken())) {
return false;
}
if (getLangOpts().CPlusPlus &&
DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
return false;
}
if (SS == 0) {
const char *TagName = 0, *FixitTagName = 0;
tok::TokenKind TagKind = tok::unknown;
switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
default: break;
case DeclSpec::TST_enum:
TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
case DeclSpec::TST_union:
TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
case DeclSpec::TST_struct:
TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
case DeclSpec::TST_interface:
TagName="__interface"; FixitTagName = "__interface ";
TagKind=tok::kw___interface;break;
case DeclSpec::TST_class:
TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
}
if (TagName) {
IdentifierInfo *TokenName = Tok.getIdentifierInfo();
LookupResult R(Actions, TokenName, SourceLocation(),
Sema::LookupOrdinaryName);
Diag(Loc, diag::err_use_of_tag_name_without_tag)
<< TokenName << TagName << getLangOpts().CPlusPlus
<< FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
if (Actions.LookupParsedName(R, getCurScope(), SS)) {
for (LookupResult::iterator I = R.begin(), IEnd = R.end();
I != IEnd; ++I)
Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
<< TokenName << TagName;
}
if (TagKind == tok::kw_enum)
ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
else
ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
false, DSC_normal);
return true;
}
}
if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
(!SS || DSC == DSC_top_level || DSC == DSC_class)) {
switch (NextToken().getKind()) {
case tok::comma:
case tok::equal:
case tok::kw_asm:
case tok::l_brace:
case tok::l_square:
case tok::semi:
return false;
case tok::l_paren: {
TentativeParsingAction PA(*this);
ConsumeToken();
TPResult TPR = TryParseDeclarator(false);
PA.Revert();
if (TPR == TPResult::False())
return false;
break;
}
default:
break;
}
}
ParsedType T;
IdentifierInfo *II = Tok.getIdentifierInfo();
if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
if (T) {
const char *PrevSpec;
unsigned DiagID;
DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
DS.SetRangeEnd(Tok.getLocation());
ConsumeToken();
return true;
} else if (II != Tok.getIdentifierInfo()) {
Tok.setKind(II->getTokenID());
return true;
}
} else {
SourceRange R;
if (SS) R = SS->getRange();
Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
}
DS.SetTypeSpecError();
DS.SetRangeEnd(Tok.getLocation());
ConsumeToken();
return false;
}
Parser::DeclSpecContext
Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
if (Context == Declarator::MemberContext)
return DSC_class;
if (Context == Declarator::FileContext)
return DSC_top_level;
if (Context == Declarator::TrailingReturnContext)
return DSC_trailing;
return DSC_normal;
}
ExprResult Parser::ParseAlignArgument(SourceLocation Start,
SourceLocation &EllipsisLoc) {
ExprResult ER;
if (isTypeIdInParens()) {
SourceLocation TypeLoc = Tok.getLocation();
ParsedType Ty = ParseTypeName().get();
SourceRange TypeRange(Start, Tok.getLocation());
ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
Ty.getAsOpaquePtr(), TypeRange);
} else
ER = ParseConstantExpression();
if (getLangOpts().CPlusPlus0x && Tok.is(tok::ellipsis))
EllipsisLoc = ConsumeToken();
return ER;
}
void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
SourceLocation *endLoc) {
assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
"Not an alignment-specifier!");
SourceLocation KWLoc = Tok.getLocation();
ConsumeToken();
BalancedDelimiterTracker T(*this, tok::l_paren);
if (T.expectAndConsume(diag::err_expected_lparen))
return;
SourceLocation EllipsisLoc;
ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
if (ArgExpr.isInvalid()) {
SkipUntil(tok::r_paren);
return;
}
T.consumeClose();
if (endLoc)
*endLoc = T.getCloseLocation();
if (EllipsisLoc.isValid()) {
Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
return;
}
ExprVector ArgExprs;
ArgExprs.push_back(ArgExpr.release());
Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
0, T.getOpenLocation(), ArgExprs.data(), 1,
AttributeList::AS_GNU);
}
void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
const ParsedTemplateInfo &TemplateInfo,
AccessSpecifier AS,
DeclSpecContext DSContext,
LateParsedAttrList *LateAttrs) {
if (DS.getSourceRange().isInvalid()) {
DS.SetRangeStart(Tok.getLocation());
DS.SetRangeEnd(Tok.getLocation());
}
bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
bool AttrsLastTime = false;
ParsedAttributesWithRange attrs(AttrFactory);
while (1) {
bool isInvalid = false;
const char *PrevSpec = 0;
unsigned DiagID = 0;
SourceLocation Loc = Tok.getLocation();
switch (Tok.getKind()) {
default:
DoneWithDeclSpec:
if (!AttrsLastTime)
ProhibitAttributes(attrs);
else
DS.takeAttributesFrom(attrs);
DS.Finish(Diags, PP);
return;
case tok::l_square:
case tok::kw_alignas:
if (!isCXX11AttributeSpecifier())
goto DoneWithDeclSpec;
ProhibitAttributes(attrs);
attrs.clear();
attrs.Range = SourceRange();
ParseCXX11Attributes(attrs);
AttrsLastTime = true;
continue;
case tok::code_completion: {
Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
if (DS.hasTypeSpecifier()) {
bool AllowNonIdentifiers
= (getCurScope()->getFlags() & (Scope::ControlScope |
Scope::BlockScope |
Scope::TemplateParamScope |
Scope::FunctionPrototypeScope |
Scope::AtCatchScope)) == 0;
bool AllowNestedNameSpecifiers
= DSContext == DSC_top_level ||
(DSContext == DSC_class && DS.isFriendSpecified());
Actions.CodeCompleteDeclSpec(getCurScope(), DS,
AllowNonIdentifiers,
AllowNestedNameSpecifiers);
return cutOffParsing();
}
if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
CCC = Sema::PCC_LocalDeclarationSpecifiers;
else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
: Sema::PCC_Template;
else if (DSContext == DSC_class)
CCC = Sema::PCC_Class;
else if (CurParsedObjCImpl)
CCC = Sema::PCC_ObjCImplementation;
Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
return cutOffParsing();
}
case tok::coloncolon: if (TryAnnotateCXXScopeToken(true)) {
if (!DS.hasTypeSpecifier())
DS.SetTypeSpecError();
goto DoneWithDeclSpec;
}
if (Tok.is(tok::coloncolon)) goto DoneWithDeclSpec;
continue;
case tok::annot_cxxscope: {
if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
goto DoneWithDeclSpec;
CXXScopeSpec SS;
Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
Tok.getAnnotationRange(),
SS);
Token Next = NextToken();
if (Next.is(tok::annot_template_id) &&
static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
->Kind == TNK_Type_template) {
TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
if ((DSContext == DSC_top_level ||
(DSContext == DSC_class && DS.isFriendSpecified())) &&
TemplateId->Name &&
Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
if (isConstructorDeclarator()) {
goto DoneWithDeclSpec;
}
Diag(TemplateId->TemplateNameLoc,
diag::err_out_of_line_template_id_names_constructor)
<< TemplateId->Name;
}
DS.getTypeSpecScope() = SS;
ConsumeToken(); assert(Tok.is(tok::annot_template_id) &&
"ParseOptionalCXXScopeSpecifier not working");
AnnotateTemplateIdTokenAsType();
continue;
}
if (Next.is(tok::annot_typename)) {
DS.getTypeSpecScope() = SS;
ConsumeToken(); if (Tok.getAnnotationValue()) {
ParsedType T = getTypeAnnotation(Tok);
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Tok.getAnnotationEndLoc(),
PrevSpec, DiagID, T);
if (isInvalid)
break;
}
else
DS.SetTypeSpecError();
DS.SetRangeEnd(Tok.getAnnotationEndLoc());
ConsumeToken(); }
if (Next.isNot(tok::identifier))
goto DoneWithDeclSpec;
if ((DSContext == DSC_top_level ||
(DSContext == DSC_class && DS.isFriendSpecified())) &&
Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
&SS)) {
if (isConstructorDeclarator())
goto DoneWithDeclSpec;
Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
<< Next.getIdentifierInfo();
}
ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
Next.getLocation(),
getCurScope(), &SS,
false, false, ParsedType(),
false,
true);
if (TypeRep == 0) {
ConsumeToken(); if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext)) continue;
goto DoneWithDeclSpec;
}
DS.getTypeSpecScope() = SS;
ConsumeToken();
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
DiagID, TypeRep);
if (isInvalid)
break;
DS.SetRangeEnd(Tok.getLocation());
ConsumeToken();
continue;
}
case tok::annot_typename: {
if (Tok.getAnnotationValue()) {
ParsedType T = getTypeAnnotation(Tok);
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
DiagID, T);
} else
DS.SetTypeSpecError();
if (isInvalid)
break;
DS.SetRangeEnd(Tok.getAnnotationEndLoc());
ConsumeToken();
if (Tok.is(tok::less) && getLangOpts().ObjC1)
ParseObjCProtocolQualifiers(DS);
continue;
}
case tok::kw___is_signed:
if (DS.getTypeSpecType() == TST_bool &&
DS.getTypeQualifiers() == DeclSpec::TQ_const &&
DS.getStorageClassSpec() == DeclSpec::SCS_static) {
Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
Tok.setKind(tok::identifier);
}
goto DoneWithDeclSpec;
case tok::kw_decltype:
case tok::identifier: {
if (getLangOpts().CPlusPlus) {
if (TryAnnotateCXXScopeToken(true)) {
if (!DS.hasTypeSpecifier())
DS.SetTypeSpecError();
goto DoneWithDeclSpec;
}
if (!Tok.is(tok::identifier))
continue;
}
if (DS.hasTypeSpecifier())
goto DoneWithDeclSpec;
if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
break;
if (DS.isTypeAltiVecVector())
goto DoneWithDeclSpec;
ParsedType TypeRep =
Actions.getTypeName(*Tok.getIdentifierInfo(),
Tok.getLocation(), getCurScope());
if (!TypeRep) {
if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext)) continue;
goto DoneWithDeclSpec;
}
if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
isConstructorDeclarator())
goto DoneWithDeclSpec;
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
DiagID, TypeRep);
if (isInvalid)
break;
DS.SetRangeEnd(Tok.getLocation());
ConsumeToken();
if (Tok.is(tok::less) && getLangOpts().ObjC1)
ParseObjCProtocolQualifiers(DS);
continue;
}
case tok::annot_template_id: {
TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
if (TemplateId->Kind != TNK_Type_template) {
goto DoneWithDeclSpec;
}
if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
isConstructorDeclarator())
goto DoneWithDeclSpec;
AnnotateTemplateIdTokenAsType();
continue;
}
case tok::kw___attribute:
ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
continue;
case tok::kw___declspec:
ParseMicrosoftDeclSpec(DS.getAttributes());
continue;
case tok::kw___forceinline: {
isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
IdentifierInfo *AttrName = Tok.getIdentifierInfo();
SourceLocation AttrNameLoc = Tok.getLocation();
DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
SourceLocation(), 0, 0, AttributeList::AS_GNU);
break;
}
case tok::kw___ptr64:
case tok::kw___ptr32:
case tok::kw___w64:
case tok::kw___cdecl:
case tok::kw___stdcall:
case tok::kw___fastcall:
case tok::kw___thiscall:
case tok::kw___unaligned:
ParseMicrosoftTypeAttributes(DS.getAttributes());
continue;
case tok::kw___pascal:
ParseBorlandTypeAttributes(DS.getAttributes());
continue;
case tok::kw___kernel:
ParseOpenCLAttributes(DS.getAttributes());
continue;
case tok::kw_typedef:
isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
PrevSpec, DiagID);
break;
case tok::kw_extern:
if (DS.isThreadSpecified())
Diag(Tok, diag::ext_thread_before) << "extern";
isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
PrevSpec, DiagID);
break;
case tok::kw___private_extern__:
isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
Loc, PrevSpec, DiagID);
break;
case tok::kw_static:
if (DS.isThreadSpecified())
Diag(Tok, diag::ext_thread_before) << "static";
isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
PrevSpec, DiagID);
break;
case tok::kw_auto:
if (getLangOpts().CPlusPlus0x) {
if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
PrevSpec, DiagID);
if (!isInvalid)
Diag(Tok, diag::ext_auto_storage_class)
<< FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
} else
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
DiagID);
} else
isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
PrevSpec, DiagID);
break;
case tok::kw_register:
isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
PrevSpec, DiagID);
break;
case tok::kw_mutable:
isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
PrevSpec, DiagID);
break;
case tok::kw___thread:
isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
break;
case tok::kw_inline:
isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
break;
case tok::kw_virtual:
isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
break;
case tok::kw_explicit:
isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
break;
case tok::kw__Alignas:
if (!getLangOpts().C11)
Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
ParseAlignmentSpecifier(DS.getAttributes());
continue;
case tok::kw_friend:
if (DSContext == DSC_class)
isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
else {
PrevSpec = ""; DiagID = diag::err_friend_invalid_in_context;
isInvalid = true;
}
break;
case tok::kw___module_private__:
isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
break;
case tok::kw_constexpr:
isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
break;
case tok::kw_short:
isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
DiagID);
break;
case tok::kw_long:
if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
DiagID);
else
isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
DiagID);
break;
case tok::kw___int64:
isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
DiagID);
break;
case tok::kw_signed:
isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
DiagID);
break;
case tok::kw_unsigned:
isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
DiagID);
break;
case tok::kw__Complex:
isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
DiagID);
break;
case tok::kw__Imaginary:
isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
DiagID);
break;
case tok::kw_void:
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
DiagID);
break;
case tok::kw_char:
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
DiagID);
break;
case tok::kw_int:
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
DiagID);
break;
case tok::kw___int128:
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
DiagID);
break;
case tok::kw_half:
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
DiagID);
break;
case tok::kw_float:
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
DiagID);
break;
case tok::kw_double:
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
DiagID);
break;
case tok::kw_wchar_t:
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
DiagID);
break;
case tok::kw_char16_t:
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
DiagID);
break;
case tok::kw_char32_t:
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
DiagID);
break;
case tok::kw_bool:
case tok::kw__Bool:
if (Tok.is(tok::kw_bool) &&
DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
PrevSpec = ""; DiagID = diag::err_bool_redeclaration;
Tok.setKind(tok::identifier);
isInvalid = true;
} else {
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
DiagID);
}
break;
case tok::kw__Decimal32:
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
DiagID);
break;
case tok::kw__Decimal64:
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
DiagID);
break;
case tok::kw__Decimal128:
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
DiagID);
break;
case tok::kw___vector:
isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
break;
case tok::kw___pixel:
isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
break;
case tok::kw___unknown_anytype:
isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
PrevSpec, DiagID);
break;
case tok::kw_class:
case tok::kw_struct:
case tok::kw___interface:
case tok::kw_union: {
tok::TokenKind Kind = Tok.getKind();
ConsumeToken();
ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
EnteringContext, DSContext);
continue;
}
case tok::kw_enum:
ConsumeToken();
ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
continue;
case tok::kw_const:
isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
getLangOpts(), true);
break;
case tok::kw_volatile:
isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
getLangOpts(), true);
break;
case tok::kw_restrict:
isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
getLangOpts(), true);
break;
case tok::kw_typename:
if (TryAnnotateTypeOrScopeToken()) {
DS.SetTypeSpecError();
goto DoneWithDeclSpec;
}
if (!Tok.is(tok::kw_typename))
continue;
break;
case tok::kw_typeof:
ParseTypeofSpecifier(DS);
continue;
case tok::annot_decltype:
ParseDecltypeSpecifier(DS);
continue;
case tok::kw___underlying_type:
ParseUnderlyingTypeSpecifier(DS);
continue;
case tok::kw__Atomic:
ParseAtomicSpecifier(DS);
continue;
case tok::kw_private:
if (!getLangOpts().OpenCL)
goto DoneWithDeclSpec;
case tok::kw___private:
case tok::kw___global:
case tok::kw___local:
case tok::kw___constant:
case tok::kw___read_only:
case tok::kw___write_only:
case tok::kw___read_write:
ParseOpenCLQualifiers(DS);
break;
case tok::less:
if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
goto DoneWithDeclSpec;
if (!ParseObjCProtocolQualifiers(DS))
Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
<< FixItHint::CreateInsertion(Loc, "id")
<< SourceRange(Loc, DS.getSourceRange().getEnd());
continue;
}
if (isInvalid) {
assert(PrevSpec && "Method did not return previous specifier!");
assert(DiagID);
if (DiagID == diag::ext_duplicate_declspec)
Diag(Tok, DiagID)
<< PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
else
Diag(Tok, DiagID) << PrevSpec;
}
DS.SetRangeEnd(Tok.getLocation());
if (DiagID != diag::err_bool_redeclaration)
ConsumeToken();
AttrsLastTime = false;
}
}
void Parser::
ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
if (Tok.is(tok::kw___extension__)) {
ExtensionRAIIObject O(Diags); ConsumeToken();
return ParseStructDeclaration(DS, Fields);
}
ParseSpecifierQualifierList(DS);
if (Tok.is(tok::semi)) {
Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
DS);
DS.complete(TheDecl);
return;
}
bool FirstDeclarator = true;
SourceLocation CommaLoc;
while (1) {
ParsingFieldDeclarator DeclaratorInfo(*this, DS);
DeclaratorInfo.D.setCommaLoc(CommaLoc);
if (!FirstDeclarator)
MaybeParseGNUAttributes(DeclaratorInfo.D);
if (Tok.isNot(tok::colon)) {
ColonProtectionRAIIObject X(*this);
ParseDeclarator(DeclaratorInfo.D);
}
if (Tok.is(tok::colon)) {
ConsumeToken();
ExprResult Res(ParseConstantExpression());
if (Res.isInvalid())
SkipUntil(tok::semi, true, true);
else
DeclaratorInfo.BitfieldSize = Res.release();
}
MaybeParseGNUAttributes(DeclaratorInfo.D);
Fields.invoke(DeclaratorInfo);
if (Tok.isNot(tok::comma))
return;
CommaLoc = ConsumeToken();
FirstDeclarator = false;
}
}
void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
unsigned TagType, Decl *TagDecl) {
PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
"parsing struct/union body");
BalancedDelimiterTracker T(*this, tok::l_brace);
if (T.consumeOpen())
return;
ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) {
Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
}
SmallVector<Decl *, 32> FieldDecls;
while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
if (Tok.is(tok::semi)) {
ConsumeExtraSemi(InsideStruct, TagType);
continue;
}
if (!Tok.is(tok::at)) {
struct CFieldCallback : FieldCallback {
Parser &P;
Decl *TagDecl;
SmallVectorImpl<Decl *> &FieldDecls;
CFieldCallback(Parser &P, Decl *TagDecl,
SmallVectorImpl<Decl *> &FieldDecls) :
P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
void invoke(ParsingFieldDeclarator &FD) {
Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
FD.D.getDeclSpec().getSourceRange().getBegin(),
FD.D, FD.BitfieldSize);
FieldDecls.push_back(Field);
FD.complete(Field);
}
} Callback(*this, TagDecl, FieldDecls);
ParsingDeclSpec DS(*this);
ParseStructDeclaration(DS, Callback);
} else { ConsumeToken();
if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
Diag(Tok, diag::err_unexpected_at);
SkipUntil(tok::semi, true);
continue;
}
ConsumeToken();
ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
if (!Tok.is(tok::identifier)) {
Diag(Tok, diag::err_expected_ident);
SkipUntil(tok::semi, true);
continue;
}
SmallVector<Decl *, 16> Fields;
Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Tok.getIdentifierInfo(), Fields);
FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
ConsumeToken();
ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
}
if (Tok.is(tok::semi)) {
ConsumeToken();
} else if (Tok.is(tok::r_brace)) {
ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
break;
} else {
ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
SkipUntil(tok::r_brace, true, true);
if (Tok.is(tok::semi)) ConsumeToken();
}
}
T.consumeClose();
ParsedAttributes attrs(AttrFactory);
MaybeParseGNUAttributes(attrs);
Actions.ActOnFields(getCurScope(),
RecordLoc, TagDecl, FieldDecls,
T.getOpenLocation(), T.getCloseLocation(),
attrs.getList());
StructScope.Exit();
Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
T.getCloseLocation());
}
void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
const ParsedTemplateInfo &TemplateInfo,
AccessSpecifier AS, DeclSpecContext DSC) {
if (Tok.is(tok::code_completion)) {
Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
return cutOffParsing();
}
ParsedAttributesWithRange attrs(AttrFactory);
MaybeParseGNUAttributes(attrs);
MaybeParseCXX0XAttributes(attrs);
while (Tok.is(tok::kw___declspec))
ParseMicrosoftDeclSpec(attrs);
SourceLocation ScopedEnumKWLoc;
bool IsScopedUsingClassTag = false;
if (getLangOpts().CPlusPlus0x &&
(Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
IsScopedUsingClassTag = Tok.is(tok::kw_class);
ScopedEnumKWLoc = ConsumeToken();
ProhibitAttributes(attrs);
MaybeParseGNUAttributes(attrs);
MaybeParseCXX0XAttributes(attrs);
while (Tok.is(tok::kw___declspec))
ParseMicrosoftDeclSpec(attrs);
}
bool shouldDelayDiagsInTag =
(TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
bool AllowDeclaration = DSC != DSC_trailing;
bool AllowFixedUnderlyingType = AllowDeclaration &&
(getLangOpts().CPlusPlus0x || getLangOpts().MicrosoftExt ||
getLangOpts().ObjC2);
CXXScopeSpec &SS = DS.getTypeSpecScope();
if (getLangOpts().CPlusPlus) {
ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
false))
return;
if (SS.isSet() && Tok.isNot(tok::identifier)) {
Diag(Tok, diag::err_expected_ident);
if (Tok.isNot(tok::l_brace)) {
SkipUntil(tok::comma, true);
return;
}
}
}
if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
!(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Diag(Tok, diag::err_expected_ident_lbrace);
SkipUntil(tok::comma, true);
return;
}
IdentifierInfo *Name = 0;
SourceLocation NameLoc;
if (Tok.is(tok::identifier)) {
Name = Tok.getIdentifierInfo();
NameLoc = ConsumeToken();
}
if (!Name && ScopedEnumKWLoc.isValid()) {
Diag(Tok, diag::err_scoped_enum_missing_identifier);
ScopedEnumKWLoc = SourceLocation();
IsScopedUsingClassTag = false;
}
if (shouldDelayDiagsInTag)
diagsFromTag.done();
TypeResult BaseType;
bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
bool PossibleBitfield = false;
if (CanBeBitfield) {
EnterExpressionEvaluationContext Unevaluated(Actions,
Sema::ConstantEvaluated);
TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
if (TPR == TPResult::True())
PossibleBitfield = true;
else if (TPR == TPResult::False() &&
GetLookAheadToken(2).getKind() == tok::semi) {
ConsumeToken();
} else {
TentativeParsingAction TPA(*this);
ConsumeToken();
if ((getLangOpts().CPlusPlus &&
isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
(!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
PossibleBitfield = true;
TPA.Revert();
} else {
TPA.Commit();
}
}
} else {
ConsumeToken();
}
if (!PossibleBitfield) {
SourceRange Range;
BaseType = ParseTypeName(&Range);
if (!getLangOpts().CPlusPlus0x && !getLangOpts().ObjC2)
Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
<< Range;
if (getLangOpts().CPlusPlus0x)
Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
}
}
Sema::TagUseKind TUK;
if (!AllowDeclaration) {
TUK = Sema::TUK_Reference;
} else if (Tok.is(tok::l_brace)) {
if (DS.isFriendSpecified()) {
Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
<< SourceRange(DS.getFriendSpecLoc());
ConsumeBrace();
SkipUntil(tok::r_brace);
TUK = Sema::TUK_Friend;
} else {
TUK = Sema::TUK_Definition;
}
} else if (DSC != DSC_type_specifier &&
(Tok.is(tok::semi) ||
(Tok.isAtStartOfLine() &&
!isValidAfterTypeSpecifier(CanBeBitfield)))) {
TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
if (Tok.isNot(tok::semi)) {
ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
"enum");
PP.EnterToken(Tok);
Tok.setKind(tok::semi);
}
} else {
TUK = Sema::TUK_Reference;
}
if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
diagsFromTag.redelay();
}
MultiTemplateParamsArg TParams;
if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
TUK != Sema::TUK_Reference) {
if (!getLangOpts().CPlusPlus0x || !SS.isSet()) {
Diag(Tok, diag::err_enum_template);
SkipUntil(tok::comma, true);
return;
}
if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
DS.SetTypeSpecError();
Diag(StartLoc, diag::err_explicit_instantiation_enum);
return;
}
assert(TemplateInfo.TemplateParams && "no template parameters");
TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
TemplateInfo.TemplateParams->size());
}
if (TUK == Sema::TUK_Reference)
ProhibitAttributes(attrs);
if (!Name && TUK != Sema::TUK_Definition) {
Diag(Tok, diag::err_enumerator_unnamed_no_def);
SkipUntil(tok::comma, true);
return;
}
bool Owned = false;
bool IsDependent = false;
const char *PrevSpec = 0;
unsigned DiagID;
Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
StartLoc, SS, Name, NameLoc, attrs.getList(),
AS, DS.getModulePrivateSpecLoc(), TParams,
Owned, IsDependent, ScopedEnumKWLoc,
IsScopedUsingClassTag, BaseType);
if (IsDependent) {
if (!Name) {
DS.SetTypeSpecError();
Diag(Tok, diag::err_expected_type_name_after_typename);
return;
}
TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
TUK, SS, Name, StartLoc,
NameLoc);
if (Type.isInvalid()) {
DS.SetTypeSpecError();
return;
}
if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
NameLoc.isValid() ? NameLoc : StartLoc,
PrevSpec, DiagID, Type.get()))
Diag(StartLoc, DiagID) << PrevSpec;
return;
}
if (!TagDecl) {
if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
ConsumeBrace();
SkipUntil(tok::r_brace);
}
DS.SetTypeSpecError();
return;
}
if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
ParseEnumBody(StartLoc, TagDecl);
if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
NameLoc.isValid() ? NameLoc : StartLoc,
PrevSpec, DiagID, TagDecl, Owned))
Diag(StartLoc, DiagID) << PrevSpec;
}
void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
ParseScope EnumScope(this, Scope::DeclScope);
Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
BalancedDelimiterTracker T(*this, tok::l_brace);
T.consumeOpen();
if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Diag(Tok, diag::error_empty_enum);
SmallVector<Decl *, 32> EnumConstantDecls;
Decl *LastEnumConstDecl = 0;
while (Tok.is(tok::identifier)) {
IdentifierInfo *Ident = Tok.getIdentifierInfo();
SourceLocation IdentLoc = ConsumeToken();
ParsedAttributesWithRange attrs(AttrFactory);
MaybeParseGNUAttributes(attrs);
MaybeParseCXX0XAttributes(attrs);
ProhibitAttributes(attrs);
SourceLocation EqualLoc;
ExprResult AssignedVal;
ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
if (Tok.is(tok::equal)) {
EqualLoc = ConsumeToken();
AssignedVal = ParseConstantExpression();
if (AssignedVal.isInvalid())
SkipUntil(tok::comma, tok::r_brace, true, true);
}
Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
LastEnumConstDecl,
IdentLoc, Ident,
attrs.getList(), EqualLoc,
AssignedVal.release());
PD.complete(EnumConstDecl);
EnumConstantDecls.push_back(EnumConstDecl);
LastEnumConstDecl = EnumConstDecl;
if (Tok.is(tok::identifier)) {
SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Diag(Loc, diag::err_enumerator_list_missing_comma)
<< FixItHint::CreateInsertion(Loc, ", ");
continue;
}
if (Tok.isNot(tok::comma))
break;
SourceLocation CommaLoc = ConsumeToken();
if (Tok.isNot(tok::identifier)) {
if (!getLangOpts().C99 && !getLangOpts().CPlusPlus0x)
Diag(CommaLoc, getLangOpts().CPlusPlus ?
diag::ext_enumerator_list_comma_cxx :
diag::ext_enumerator_list_comma_c)
<< FixItHint::CreateRemoval(CommaLoc);
else if (getLangOpts().CPlusPlus0x)
Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
<< FixItHint::CreateRemoval(CommaLoc);
}
}
T.consumeClose();
ParsedAttributes attrs(AttrFactory);
MaybeParseGNUAttributes(attrs);
Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
EnumDecl, EnumConstantDecls.data(),
EnumConstantDecls.size(), getCurScope(),
attrs.getList());
EnumScope.Exit();
Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
T.getCloseLocation());
bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum");
PP.EnterToken(Tok);
Tok.setKind(tok::semi);
}
}
bool Parser::isTypeQualifier() const {
switch (Tok.getKind()) {
default: return false;
case tok::kw_private:
return getLangOpts().OpenCL;
case tok::kw_const:
case tok::kw_volatile:
case tok::kw_restrict:
case tok::kw___private:
case tok::kw___local:
case tok::kw___global:
case tok::kw___constant:
case tok::kw___read_only:
case tok::kw___read_write:
case tok::kw___write_only:
return true;
}
}
bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
switch (Tok.getKind()) {
default: return false;
case tok::kw_short:
case tok::kw_long:
case tok::kw___int64:
case tok::kw___int128:
case tok::kw_signed:
case tok::kw_unsigned:
case tok::kw__Complex:
case tok::kw__Imaginary:
case tok::kw_void:
case tok::kw_char:
case tok::kw_wchar_t:
case tok::kw_char16_t:
case tok::kw_char32_t:
case tok::kw_int:
case tok::kw_half:
case tok::kw_float:
case tok::kw_double:
case tok::kw_bool:
case tok::kw__Bool:
case tok::kw__Decimal32:
case tok::kw__Decimal64:
case tok::kw__Decimal128:
case tok::kw___vector:
case tok::kw_class:
case tok::kw_struct:
case tok::kw___interface:
case tok::kw_union:
case tok::kw_enum:
case tok::annot_typename:
return true;
}
}
bool Parser::isTypeSpecifierQualifier() {
switch (Tok.getKind()) {
default: return false;
case tok::identifier: if (TryAltiVecVectorToken())
return true;
case tok::kw_typename: if (TryAnnotateTypeOrScopeToken())
return true;
if (Tok.is(tok::identifier))
return false;
return isTypeSpecifierQualifier();
case tok::coloncolon: if (NextToken().is(tok::kw_new) || NextToken().is(tok::kw_delete)) return false;
if (TryAnnotateTypeOrScopeToken())
return true;
return isTypeSpecifierQualifier();
case tok::kw___attribute:
case tok::kw_typeof:
case tok::kw_short:
case tok::kw_long:
case tok::kw___int64:
case tok::kw___int128:
case tok::kw_signed:
case tok::kw_unsigned:
case tok::kw__Complex:
case tok::kw__Imaginary:
case tok::kw_void:
case tok::kw_char:
case tok::kw_wchar_t:
case tok::kw_char16_t:
case tok::kw_char32_t:
case tok::kw_int:
case tok::kw_half:
case tok::kw_float:
case tok::kw_double:
case tok::kw_bool:
case tok::kw__Bool:
case tok::kw__Decimal32:
case tok::kw__Decimal64:
case tok::kw__Decimal128:
case tok::kw___vector:
case tok::kw_class:
case tok::kw_struct:
case tok::kw___interface:
case tok::kw_union:
case tok::kw_enum:
case tok::kw_const:
case tok::kw_volatile:
case tok::kw_restrict:
case tok::kw___unknown_anytype:
case tok::annot_typename:
return true;
case tok::less:
return getLangOpts().ObjC1;
case tok::kw___cdecl:
case tok::kw___stdcall:
case tok::kw___fastcall:
case tok::kw___thiscall:
case tok::kw___w64:
case tok::kw___ptr64:
case tok::kw___ptr32:
case tok::kw___pascal:
case tok::kw___unaligned:
case tok::kw___private:
case tok::kw___local:
case tok::kw___global:
case tok::kw___constant:
case tok::kw___read_only:
case tok::kw___read_write:
case tok::kw___write_only:
return true;
case tok::kw_private:
return getLangOpts().OpenCL;
case tok::kw__Atomic:
return true;
}
}
bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
switch (Tok.getKind()) {
default: return false;
case tok::kw_private:
return getLangOpts().OpenCL;
case tok::identifier: if (getLangOpts().ObjC1 && NextToken().is(tok::period))
return false;
if (TryAltiVecVectorToken())
return true;
case tok::kw_decltype: case tok::kw_typename: if (TryAnnotateTypeOrScopeToken())
return true;
if (Tok.is(tok::identifier))
return false;
if (DisambiguatingWithExpression &&
isStartOfObjCClassMessageMissingOpenBracket())
return false;
return isDeclarationSpecifier();
case tok::coloncolon: if (NextToken().is(tok::kw_new) || NextToken().is(tok::kw_delete)) return false;
if (TryAnnotateTypeOrScopeToken())
return true;
return isDeclarationSpecifier();
case tok::kw_typedef:
case tok::kw_extern:
case tok::kw___private_extern__:
case tok::kw_static:
case tok::kw_auto:
case tok::kw_register:
case tok::kw___thread:
case tok::kw___module_private__:
case tok::kw___unknown_anytype:
case tok::kw_short:
case tok::kw_long:
case tok::kw___int64:
case tok::kw___int128:
case tok::kw_signed:
case tok::kw_unsigned:
case tok::kw__Complex:
case tok::kw__Imaginary:
case tok::kw_void:
case tok::kw_char:
case tok::kw_wchar_t:
case tok::kw_char16_t:
case tok::kw_char32_t:
case tok::kw_int:
case tok::kw_half:
case tok::kw_float:
case tok::kw_double:
case tok::kw_bool:
case tok::kw__Bool:
case tok::kw__Decimal32:
case tok::kw__Decimal64:
case tok::kw__Decimal128:
case tok::kw___vector:
case tok::kw_class:
case tok::kw_struct:
case tok::kw_union:
case tok::kw___interface:
case tok::kw_enum:
case tok::kw_const:
case tok::kw_volatile:
case tok::kw_restrict:
case tok::kw_inline:
case tok::kw_virtual:
case tok::kw_explicit:
case tok::kw__Static_assert:
case tok::kw_typeof:
case tok::kw___attribute:
return true;
case tok::annot_decltype:
return true;
case tok::kw__Atomic:
return true;
case tok::less:
return getLangOpts().ObjC1;
case tok::annot_typename:
return !DisambiguatingWithExpression ||
!isStartOfObjCClassMessageMissingOpenBracket();
case tok::kw___declspec:
case tok::kw___cdecl:
case tok::kw___stdcall:
case tok::kw___fastcall:
case tok::kw___thiscall:
case tok::kw___w64:
case tok::kw___ptr64:
case tok::kw___ptr32:
case tok::kw___forceinline:
case tok::kw___pascal:
case tok::kw___unaligned:
case tok::kw___private:
case tok::kw___local:
case tok::kw___global:
case tok::kw___constant:
case tok::kw___read_only:
case tok::kw___read_write:
case tok::kw___write_only:
return true;
}
}
bool Parser::isConstructorDeclarator() {
TentativeParsingAction TPA(*this);
CXXScopeSpec SS;
if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
true)) {
TPA.Revert();
return false;
}
if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
ConsumeToken();
} else {
TPA.Revert();
return false;
}
if (Tok.isNot(tok::l_paren)) {
TPA.Revert();
return false;
}
ConsumeParen();
if (Tok.is(tok::r_paren) ||
(Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
TPA.Revert();
return true;
}
DeclaratorScopeObj DeclScopeObj(*this, SS);
if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
DeclScopeObj.EnterDeclaratorScope();
ParsedAttributes Attrs(AttrFactory);
MaybeParseMicrosoftAttributes(Attrs);
bool IsConstructor = false;
if (isDeclarationSpecifier())
IsConstructor = true;
else if (Tok.is(tok::identifier) ||
(Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
if (Tok.is(tok::annot_cxxscope))
ConsumeToken();
ConsumeToken();
switch (Tok.getKind()) {
case tok::l_paren:
case tok::l_square:
case tok::coloncolon:
case tok::r_paren:
break;
default:
IsConstructor = true;
break;
}
}
TPA.Revert();
return IsConstructor;
}
void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
bool VendorAttributesAllowed,
bool CXX11AttributesAllowed) {
if (getLangOpts().CPlusPlus0x && CXX11AttributesAllowed &&
isCXX11AttributeSpecifier()) {
ParsedAttributesWithRange attrs(AttrFactory);
ParseCXX11Attributes(attrs);
DS.takeAttributesFrom(attrs);
}
SourceLocation EndLoc;
while (1) {
bool isInvalid = false;
const char *PrevSpec = 0;
unsigned DiagID = 0;
SourceLocation Loc = Tok.getLocation();
switch (Tok.getKind()) {
case tok::code_completion:
Actions.CodeCompleteTypeQualifiers(DS);
return cutOffParsing();
case tok::kw_const:
isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
getLangOpts(), false);
break;
case tok::kw_volatile:
isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
getLangOpts(), false);
break;
case tok::kw_restrict:
isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
getLangOpts(), false);
break;
case tok::kw_private:
if (!getLangOpts().OpenCL)
goto DoneWithTypeQuals;
case tok::kw___private:
case tok::kw___global:
case tok::kw___local:
case tok::kw___constant:
case tok::kw___read_only:
case tok::kw___write_only:
case tok::kw___read_write:
ParseOpenCLQualifiers(DS);
break;
case tok::kw___w64:
case tok::kw___ptr64:
case tok::kw___ptr32:
case tok::kw___cdecl:
case tok::kw___stdcall:
case tok::kw___fastcall:
case tok::kw___thiscall:
case tok::kw___unaligned:
if (VendorAttributesAllowed) {
ParseMicrosoftTypeAttributes(DS.getAttributes());
continue;
}
goto DoneWithTypeQuals;
case tok::kw___pascal:
if (VendorAttributesAllowed) {
ParseBorlandTypeAttributes(DS.getAttributes());
continue;
}
goto DoneWithTypeQuals;
case tok::kw___attribute:
if (VendorAttributesAllowed) {
ParseGNUAttributes(DS.getAttributes());
continue; }
default:
DoneWithTypeQuals:
DS.Finish(Diags, PP);
if (EndLoc.isValid())
DS.SetRangeEnd(EndLoc);
return;
}
if (isInvalid) {
assert(PrevSpec && "Method did not return previous specifier!");
Diag(Tok, DiagID) << PrevSpec;
}
EndLoc = ConsumeToken();
}
}
void Parser::ParseDeclarator(Declarator &D) {
ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
}
static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
if (Kind == tok::star || Kind == tok::caret)
return true;
if (!Lang.CPlusPlus)
return false;
return Kind == tok::amp || Kind == tok::ampamp;
}
void Parser::ParseDeclaratorInternal(Declarator &D,
DirectDeclParseFunction DirectDeclParser) {
if (Diags.hasAllExtensionsSilenced())
D.setExtension();
if (getLangOpts().CPlusPlus &&
(Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
Tok.is(tok::annot_cxxscope))) {
bool EnteringContext = D.getContext() == Declarator::FileContext ||
D.getContext() == Declarator::MemberContext;
CXXScopeSpec SS;
ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
if (SS.isNotEmpty()) {
if (Tok.isNot(tok::star)) {
D.getCXXScopeSpec() = SS;
if (DirectDeclParser)
(this->*DirectDeclParser)(D);
return;
}
SourceLocation Loc = ConsumeToken();
D.SetRangeEnd(Loc);
DeclSpec DS(AttrFactory);
ParseTypeQualifierListOpt(DS);
D.ExtendWithDeclSpec(DS);
ParseDeclaratorInternal(D, DirectDeclParser);
D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Loc),
DS.getAttributes(),
SourceLocation());
return;
}
}
tok::TokenKind Kind = Tok.getKind();
if (!isPtrOperatorToken(Kind, getLangOpts())) {
if (DirectDeclParser)
(this->*DirectDeclParser)(D);
return;
}
SourceLocation Loc = ConsumeToken(); D.SetRangeEnd(Loc);
if (Kind == tok::star || Kind == tok::caret) {
DeclSpec DS(AttrFactory);
ParseTypeQualifierListOpt(DS);
D.ExtendWithDeclSpec(DS);
ParseDeclaratorInternal(D, DirectDeclParser);
if (Kind == tok::star)
D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
DS.getConstSpecLoc(),
DS.getVolatileSpecLoc(),
DS.getRestrictSpecLoc()),
DS.getAttributes(),
SourceLocation());
else
D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Loc),
DS.getAttributes(),
SourceLocation());
} else {
DeclSpec DS(AttrFactory);
if (Kind == tok::ampamp)
Diag(Loc, getLangOpts().CPlusPlus0x ?
diag::warn_cxx98_compat_rvalue_reference :
diag::ext_rvalue_reference);
ParseTypeQualifierListOpt(DS);
D.ExtendWithDeclSpec(DS);
if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Diag(DS.getConstSpecLoc(),
diag::err_invalid_reference_qualifier_application) << "const";
if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
Diag(DS.getVolatileSpecLoc(),
diag::err_invalid_reference_qualifier_application) << "volatile";
}
ParseDeclaratorInternal(D, DirectDeclParser);
if (D.getNumTypeObjects() > 0) {
DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
if (InnerChunk.Kind == DeclaratorChunk::Reference) {
if (const IdentifierInfo *II = D.getIdentifier())
Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
<< II;
else
Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
<< "type name";
}
}
D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Kind == tok::amp),
DS.getAttributes(),
SourceLocation());
}
}
static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
SourceLocation EllipsisLoc) {
if (EllipsisLoc.isValid()) {
FixItHint Insertion;
if (!D.getEllipsisLoc().isValid()) {
Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
D.setEllipsisLoc(EllipsisLoc);
}
P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
<< FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
}
}
void Parser::ParseDirectDeclarator(Declarator &D) {
DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
if (D.getCXXScopeSpec().isEmpty()) {
bool EnteringContext = D.getContext() == Declarator::FileContext ||
D.getContext() == Declarator::MemberContext;
ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
EnteringContext);
}
if (D.getCXXScopeSpec().isValid()) {
if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
DeclScopeObj.EnterDeclaratorScope();
}
if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
!((D.getContext() == Declarator::PrototypeContext ||
D.getContext() == Declarator::BlockLiteralContext) &&
NextToken().is(tok::r_paren) &&
!Actions.containsUnexpandedParameterPacks(D))) {
SourceLocation EllipsisLoc = ConsumeToken();
if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
ParseDeclarator(D);
diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
return;
} else
D.setEllipsisLoc(EllipsisLoc);
}
if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
bool AllowConstructorName;
if (D.getDeclSpec().hasTypeSpecifier())
AllowConstructorName = false;
else if (D.getCXXScopeSpec().isSet())
AllowConstructorName =
(D.getContext() == Declarator::FileContext ||
(D.getContext() == Declarator::MemberContext &&
D.getDeclSpec().isFriendSpecified()));
else
AllowConstructorName = (D.getContext() == Declarator::MemberContext);
SourceLocation TemplateKWLoc;
if (ParseUnqualifiedId(D.getCXXScopeSpec(),
true,
true,
AllowConstructorName,
ParsedType(),
TemplateKWLoc,
D.getName()) ||
D.getCXXScopeSpec().isInvalid()) {
D.SetIdentifier(0, Tok.getLocation());
D.setInvalidType(true);
} else {
if (D.getSourceRange().getBegin().isInvalid())
D.SetRangeBegin(D.getName().getSourceRange().getBegin());
D.SetRangeEnd(D.getName().getSourceRange().getEnd());
}
goto PastIdentifier;
}
} else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
assert(!getLangOpts().CPlusPlus &&
"There's a C++-specific check for tok::identifier above");
assert(Tok.getIdentifierInfo() && "Not an identifier?");
D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
ConsumeToken();
goto PastIdentifier;
}
if (Tok.is(tok::l_paren)) {
ParseParenDeclarator(D);
if (D.getCXXScopeSpec().isSet()) {
if (!D.isInvalidType() &&
Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
DeclScopeObj.EnterDeclaratorScope();
}
} else if (D.mayOmitIdentifier()) {
D.SetIdentifier(0, Tok.getLocation());
} else {
if (Tok.getKind() == tok::annot_pragma_parser_crash)
LLVM_BUILTIN_TRAP;
if (D.getContext() == Declarator::MemberContext)
Diag(Tok, diag::err_expected_member_name_or_semi)
<< D.getDeclSpec().getSourceRange();
else if (getLangOpts().CPlusPlus)
Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
else
Diag(Tok, diag::err_expected_ident_lparen);
D.SetIdentifier(0, Tok.getLocation());
D.setInvalidType(true);
}
PastIdentifier:
assert(D.isPastIdentifier() &&
"Haven't past the location of the identifier yet?");
if (D.hasName() && !D.getNumTypeObjects())
MaybeParseCXX0XAttributes(D);
while (1) {
if (Tok.is(tok::l_paren)) {
ParseScope PrototypeScope(this,
Scope::FunctionPrototypeScope|Scope::DeclScope);
bool IsAmbiguous = false;
if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
TentativelyDeclaredIdentifiers.pop_back();
if (!IsFunctionDecl)
break;
}
ParsedAttributes attrs(AttrFactory);
BalancedDelimiterTracker T(*this, tok::l_paren);
T.consumeOpen();
ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
PrototypeScope.Exit();
} else if (Tok.is(tok::l_square)) {
ParseBracketDeclarator(D);
} else {
break;
}
}
}
void Parser::ParseParenDeclarator(Declarator &D) {
BalancedDelimiterTracker T(*this, tok::l_paren);
T.consumeOpen();
assert(!D.isPastIdentifier() && "Should be called before passing identifier");
ParsedAttributes attrs(AttrFactory);
bool RequiresArg = false;
if (Tok.is(tok::kw___attribute)) {
ParseGNUAttributes(attrs);
RequiresArg = true;
}
if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
ParseMicrosoftTypeAttributes(attrs);
}
if (Tok.is(tok::kw___pascal))
ParseBorlandTypeAttributes(attrs);
bool isGrouping;
if (!D.mayOmitIdentifier()) {
isGrouping = true;
} else if (Tok.is(tok::r_paren) || (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
NextToken().is(tok::r_paren)) || isDeclarationSpecifier() || isCXX11AttributeSpecifier()) { isGrouping = false;
} else {
isGrouping = true;
}
if (isGrouping) {
SourceLocation EllipsisLoc = D.getEllipsisLoc();
D.setEllipsisLoc(SourceLocation());
bool hadGroupingParens = D.hasGroupingParens();
D.setGroupingParens(true);
ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
T.consumeClose();
D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
T.getCloseLocation()),
attrs, T.getCloseLocation());
D.setGroupingParens(hadGroupingParens);
if (EllipsisLoc.isValid())
diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
return;
}
D.SetIdentifier(0, Tok.getLocation());
ParseScope PrototypeScope(this,
Scope::FunctionPrototypeScope|Scope::DeclScope);
ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
PrototypeScope.Exit();
}
void Parser::ParseFunctionDeclarator(Declarator &D,
ParsedAttributes &FirstArgAttrs,
BalancedDelimiterTracker &Tracker,
bool IsAmbiguous,
bool RequiresArg) {
assert(getCurScope()->isFunctionPrototypeScope() &&
"Should call from a Function scope");
assert(D.isPastIdentifier() && "Should not call before identifier!");
bool HasProto = false;
SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
SourceLocation EllipsisLoc;
DeclSpec DS(AttrFactory);
bool RefQualifierIsLValueRef = true;
SourceLocation RefQualifierLoc;
SourceLocation ConstQualifierLoc;
SourceLocation VolatileQualifierLoc;
ExceptionSpecificationType ESpecType = EST_None;
SourceRange ESpecRange;
SmallVector<ParsedType, 2> DynamicExceptions;
SmallVector<SourceRange, 2> DynamicExceptionRanges;
ExprResult NoexceptExpr;
ParsedAttributes FnAttrs(AttrFactory);
TypeResult TrailingReturnType;
Actions.ActOnStartFunctionDeclarator();
SourceLocation EndLoc;
if (isFunctionDeclaratorIdentifierList()) {
if (RequiresArg)
Diag(Tok, diag::err_argument_required_after_attribute);
ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
Tracker.consumeClose();
EndLoc = Tracker.getCloseLocation();
} else {
if (Tok.isNot(tok::r_paren))
ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
else if (RequiresArg)
Diag(Tok, diag::err_argument_required_after_attribute);
HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Tracker.consumeClose();
EndLoc = Tracker.getCloseLocation();
if (getLangOpts().CPlusPlus) {
ParseTypeQualifierListOpt(DS, false , false);
if (!DS.getSourceRange().getEnd().isInvalid()) {
EndLoc = DS.getSourceRange().getEnd();
ConstQualifierLoc = DS.getConstSpecLoc();
VolatileQualifierLoc = DS.getVolatileSpecLoc();
}
if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Diag(Tok, getLangOpts().CPlusPlus0x ?
diag::warn_cxx98_compat_ref_qualifier :
diag::ext_ref_qualifier);
RefQualifierIsLValueRef = Tok.is(tok::amp);
RefQualifierLoc = ConsumeToken();
EndLoc = RefQualifierLoc;
}
bool IsCXX11MemberFunction =
getLangOpts().CPlusPlus0x &&
(D.getContext() == Declarator::MemberContext ||
(D.getContext() == Declarator::FileContext &&
D.getCXXScopeSpec().isValid() &&
Actions.CurContext->isRecord()));
Sema::CXXThisScopeRAII ThisScope(Actions,
dyn_cast<CXXRecordDecl>(Actions.CurContext),
DS.getTypeQualifiers(),
IsCXX11MemberFunction);
ESpecType = tryParseExceptionSpecification(ESpecRange,
DynamicExceptions,
DynamicExceptionRanges,
NoexceptExpr);
if (ESpecType != EST_None)
EndLoc = ESpecRange.getEnd();
MaybeParseCXX0XAttributes(FnAttrs);
if (getLangOpts().CPlusPlus0x && Tok.is(tok::arrow)) {
Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
SourceRange Range;
TrailingReturnType = ParseTrailingReturnType(Range);
if (Range.getEnd().isValid())
EndLoc = Range.getEnd();
}
}
}
D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
EllipsisLoc.isValid(),
IsAmbiguous, EllipsisLoc,
ParamInfo.data(), ParamInfo.size(),
DS.getTypeQualifiers(),
RefQualifierIsLValueRef,
RefQualifierLoc, ConstQualifierLoc,
VolatileQualifierLoc,
SourceLocation(),
ESpecType, ESpecRange.getBegin(),
DynamicExceptions.data(),
DynamicExceptionRanges.data(),
DynamicExceptions.size(),
NoexceptExpr.isUsable() ?
NoexceptExpr.get() : 0,
Tracker.getOpenLocation(),
EndLoc, D,
TrailingReturnType),
FnAttrs, EndLoc);
Actions.ActOnEndFunctionDeclarator();
}
bool Parser::isFunctionDeclaratorIdentifierList() {
return !getLangOpts().CPlusPlus
&& Tok.is(tok::identifier)
&& !TryAltiVecVectorToken()
&& (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
&& (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
}
void Parser::ParseFunctionDeclaratorIdentifierList(
Declarator &D,
SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
if (!D.getIdentifier())
Diag(Tok, diag::ext_ident_list_in_param);
llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
while (1) {
if (Tok.isNot(tok::identifier)) {
Diag(Tok, diag::err_expected_ident);
SkipUntil(tok::r_paren, true, true);
ParamInfo.clear();
return;
}
IdentifierInfo *ParmII = Tok.getIdentifierInfo();
if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
if (!ParamsSoFar.insert(ParmII)) {
Diag(Tok, diag::err_param_redefinition) << ParmII;
} else {
ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Tok.getLocation(),
0));
}
ConsumeToken();
if (Tok.isNot(tok::comma))
break;
ConsumeToken();
}
}
void Parser::ParseParameterDeclarationClause(
Declarator &D,
ParsedAttributes &FirstArgAttrs,
SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
SourceLocation &EllipsisLoc) {
while (1) {
if (Tok.is(tok::ellipsis)) {
EllipsisLoc = ConsumeToken(); break;
}
DeclSpec DS(AttrFactory);
MaybeParseCXX0XAttributes(DS.getAttributes());
if (getLangOpts().MicrosoftExt && Tok.is(tok::l_square))
ParseMicrosoftAttributes(DS.getAttributes());
SourceLocation DSStart = Tok.getLocation();
DS.takeAttributesFrom(FirstArgAttrs);
ParseDeclarationSpecifiers(DS);
Declarator ParmDecl(DS, Declarator::PrototypeContext);
ParseDeclarator(ParmDecl);
MaybeParseGNUAttributes(ParmDecl);
IdentifierInfo *ParmII = ParmDecl.getIdentifier();
CachedTokens *DefArgToks = 0;
if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
ParmDecl.getNumTypeObjects() == 0) {
Diag(DSStart, diag::err_missing_param);
} else {
Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
if (Tok.is(tok::equal)) {
SourceLocation EqualLoc = Tok.getLocation();
if (D.getContext() == Declarator::MemberContext) {
DefArgToks = new CachedTokens;
if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
true,
false)) {
delete DefArgToks;
DefArgToks = 0;
Actions.ActOnParamDefaultArgumentError(Param);
} else {
Token DefArgEnd;
DefArgEnd.startToken();
DefArgEnd.setKind(tok::cxx_defaultarg_end);
DefArgEnd.setLocation(Tok.getLocation());
DefArgToks->push_back(DefArgEnd);
Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
(*DefArgToks)[1].getLocation());
}
} else {
ConsumeToken();
EnterExpressionEvaluationContext Eval(Actions,
Sema::PotentiallyEvaluatedIfUsed,
Param);
ExprResult DefArgResult;
if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
DefArgResult = ParseBraceInitializer();
} else
DefArgResult = ParseAssignmentExpression();
if (DefArgResult.isInvalid()) {
Actions.ActOnParamDefaultArgumentError(Param);
SkipUntil(tok::comma, tok::r_paren, true, true);
} else {
Actions.ActOnParamDefaultArgument(Param, EqualLoc,
DefArgResult.take());
}
}
}
ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
ParmDecl.getIdentifierLoc(), Param,
DefArgToks));
}
if (Tok.isNot(tok::comma)) {
if (Tok.is(tok::ellipsis)) {
EllipsisLoc = ConsumeToken();
if (!getLangOpts().CPlusPlus) {
Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
<< FixItHint::CreateInsertion(EllipsisLoc, ", ");
}
}
break;
}
ConsumeToken();
}
}
void Parser::ParseBracketDeclarator(Declarator &D) {
if (CheckProhibitedCXX11Attribute())
return;
BalancedDelimiterTracker T(*this, tok::l_square);
T.consumeOpen();
if (Tok.getKind() == tok::r_square) {
T.consumeClose();
ParsedAttributes attrs(AttrFactory);
MaybeParseCXX0XAttributes(attrs);
ExprResult NumElements;
D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
T.getOpenLocation(),
T.getCloseLocation()),
attrs, T.getCloseLocation());
return;
} else if (Tok.getKind() == tok::numeric_constant &&
GetLookAheadToken(1).is(tok::r_square)) {
ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
ConsumeToken();
T.consumeClose();
ParsedAttributes attrs(AttrFactory);
MaybeParseCXX0XAttributes(attrs);
D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
ExprRes.release(),
T.getOpenLocation(),
T.getCloseLocation()),
attrs, T.getCloseLocation());
return;
}
SourceLocation StaticLoc;
if (Tok.is(tok::kw_static))
StaticLoc = ConsumeToken();
DeclSpec DS(AttrFactory);
ParseTypeQualifierListOpt(DS, false );
if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
StaticLoc = ConsumeToken();
bool isStar = false;
ExprResult NumElements;
if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
ConsumeToken();
if (StaticLoc.isValid()) {
Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
StaticLoc = SourceLocation(); }
isStar = true;
} else if (Tok.isNot(tok::r_square)) {
if (getLangOpts().CPlusPlus) {
NumElements = ParseConstantExpression();
} else {
EnterExpressionEvaluationContext Unevaluated(Actions,
Sema::ConstantEvaluated);
NumElements = ParseAssignmentExpression();
}
}
if (NumElements.isInvalid()) {
D.setInvalidType(true);
SkipUntil(tok::r_square);
return;
}
T.consumeClose();
ParsedAttributes attrs(AttrFactory);
MaybeParseCXX0XAttributes(attrs);
D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
StaticLoc.isValid(), isStar,
NumElements.release(),
T.getOpenLocation(),
T.getCloseLocation()),
attrs, T.getCloseLocation());
}
void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Token OpTok = Tok;
SourceLocation StartLoc = ConsumeToken();
const bool hasParens = Tok.is(tok::l_paren);
EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
Sema::ReuseLambdaContextDecl);
bool isCastExpr;
ParsedType CastTy;
SourceRange CastRange;
ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
CastTy, CastRange);
if (hasParens)
DS.setTypeofParensRange(CastRange);
if (CastRange.getEnd().isInvalid())
DS.SetRangeEnd(Tok.getLocation());
else
DS.SetRangeEnd(CastRange.getEnd());
if (isCastExpr) {
if (!CastTy) {
DS.SetTypeSpecError();
return;
}
const char *PrevSpec = 0;
unsigned DiagID;
if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
DiagID, CastTy))
Diag(StartLoc, DiagID) << PrevSpec;
return;
}
if (Operand.isInvalid()) {
DS.SetTypeSpecError();
return;
}
Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
if (Operand.isInvalid()) {
DS.SetTypeSpecError();
return;
}
const char *PrevSpec = 0;
unsigned DiagID;
if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
DiagID, Operand.get()))
Diag(StartLoc, DiagID) << PrevSpec;
}
void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
SourceLocation StartLoc = ConsumeToken();
BalancedDelimiterTracker T(*this, tok::l_paren);
if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
SkipUntil(tok::r_paren);
return;
}
TypeResult Result = ParseTypeName();
if (Result.isInvalid()) {
SkipUntil(tok::r_paren);
return;
}
T.consumeClose();
if (T.getCloseLocation().isInvalid())
return;
DS.setTypeofParensRange(T.getRange());
DS.SetRangeEnd(T.getCloseLocation());
const char *PrevSpec = 0;
unsigned DiagID;
if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
DiagID, Result.release()))
Diag(StartLoc, DiagID) << PrevSpec;
}
bool Parser::TryAltiVecVectorTokenOutOfLine() {
Token Next = NextToken();
switch (Next.getKind()) {
default: return false;
case tok::kw_short:
case tok::kw_long:
case tok::kw_signed:
case tok::kw_unsigned:
case tok::kw_void:
case tok::kw_char:
case tok::kw_int:
case tok::kw_float:
case tok::kw_double:
case tok::kw_bool:
case tok::kw___pixel:
Tok.setKind(tok::kw___vector);
return true;
case tok::identifier:
if (Next.getIdentifierInfo() == Ident_pixel) {
Tok.setKind(tok::kw___vector);
return true;
}
return false;
}
}
bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
const char *&PrevSpec, unsigned &DiagID,
bool &isInvalid) {
if (Tok.getIdentifierInfo() == Ident_vector) {
Token Next = NextToken();
switch (Next.getKind()) {
case tok::kw_short:
case tok::kw_long:
case tok::kw_signed:
case tok::kw_unsigned:
case tok::kw_void:
case tok::kw_char:
case tok::kw_int:
case tok::kw_float:
case tok::kw_double:
case tok::kw_bool:
case tok::kw___pixel:
isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
return true;
case tok::identifier:
if (Next.getIdentifierInfo() == Ident_pixel) {
isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
return true;
}
break;
default:
break;
}
} else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
DS.isTypeAltiVecVector()) {
isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
return true;
}
return false;
}