#include "clang/Parse/Parser.h"
#include "clang/Parse/ParseDiagnostic.h"
#include "clang/Parse/Scope.h"
#include "ExtensionRAIIObject.h"
#include "AstGuard.h"
#include "llvm/ADT/SmallSet.h"
using namespace clang;
Action::TypeResult Parser::ParseTypeName() {
DeclSpec DS;
ParseSpecifierQualifierList(DS);
Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
ParseDeclarator(DeclaratorInfo);
if (DeclaratorInfo.isInvalidType())
return true;
return Actions.ActOnTypeName(CurScope, DeclaratorInfo);
}
AttributeList *Parser::ParseAttributes(SourceLocation *EndLoc) {
assert(Tok.is(tok::kw___attribute) && "Not an attribute list!");
AttributeList *CurrAttr = 0;
while (Tok.is(tok::kw___attribute)) {
ConsumeToken();
if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
"attribute")) {
SkipUntil(tok::r_paren, true); return CurrAttr;
}
if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
SkipUntil(tok::r_paren, true); return CurrAttr;
}
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)) {
ConsumeParen();
if (Tok.is(tok::identifier)) {
IdentifierInfo *ParmName = Tok.getIdentifierInfo();
SourceLocation ParmLoc = ConsumeToken();
if (Tok.is(tok::r_paren)) {
ConsumeParen(); CurrAttr = new AttributeList(AttrName, AttrNameLoc,
ParmName, ParmLoc, 0, 0, CurrAttr);
} else if (Tok.is(tok::comma)) {
ConsumeToken();
ExprVector ArgExprs(Actions);
bool ArgExprsOk = true;
while (1) {
OwningExprResult ArgExpr(ParseAssignmentExpression());
if (ArgExpr.isInvalid()) {
ArgExprsOk = false;
SkipUntil(tok::r_paren);
break;
} else {
ArgExprs.push_back(ArgExpr.release());
}
if (Tok.isNot(tok::comma))
break;
ConsumeToken(); }
if (ArgExprsOk && Tok.is(tok::r_paren)) {
ConsumeParen(); CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName,
ParmLoc, ArgExprs.take(), ArgExprs.size(), CurrAttr);
}
}
} else { if (Tok.is(tok::r_paren)) {
ConsumeParen(); CurrAttr = new AttributeList(AttrName, AttrNameLoc,
0, SourceLocation(), 0, 0, CurrAttr);
} else {
ExprVector ArgExprs(Actions);
bool ArgExprsOk = true;
while (1) {
OwningExprResult ArgExpr(ParseAssignmentExpression());
if (ArgExpr.isInvalid()) {
ArgExprsOk = false;
SkipUntil(tok::r_paren);
break;
} else {
ArgExprs.push_back(ArgExpr.release());
}
if (Tok.isNot(tok::comma))
break;
ConsumeToken(); }
if (ArgExprsOk && Tok.is(tok::r_paren)) {
ConsumeParen(); CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
SourceLocation(), ArgExprs.take(), ArgExprs.size(),
CurrAttr);
}
}
}
} else {
CurrAttr = new AttributeList(AttrName, AttrNameLoc,
0, SourceLocation(), 0, 0, CurrAttr);
}
}
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;
}
return CurrAttr;
}
void Parser::FuzzyParseMicrosoftDeclSpec() {
assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
ConsumeToken();
if (Tok.is(tok::l_paren)) {
unsigned short savedParenCount = ParenCount;
do {
ConsumeAnyToken();
} while (ParenCount > savedParenCount && Tok.isNot(tok::eof));
}
return;
}
Parser::DeclGroupPtrTy Parser::ParseDeclaration(unsigned Context,
SourceLocation &DeclEnd) {
DeclPtrTy SingleDecl;
switch (Tok.getKind()) {
case tok::kw_export:
case tok::kw_template:
SingleDecl = ParseTemplateDeclarationOrSpecialization(Context, DeclEnd);
break;
case tok::kw_namespace:
SingleDecl = ParseNamespace(Context, DeclEnd);
break;
case tok::kw_using:
SingleDecl = ParseUsingDirectiveOrDeclaration(Context, DeclEnd);
break;
case tok::kw_static_assert:
SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
break;
default:
return ParseSimpleDeclaration(Context, DeclEnd);
}
return Actions.ConvertDeclToDeclGroup(SingleDecl);
}
Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(unsigned Context,
SourceLocation &DeclEnd,
bool RequireSemi) {
DeclSpec DS;
ParseDeclarationSpecifiers(DS);
if (Tok.is(tok::semi)) {
ConsumeToken();
DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
return Actions.ConvertDeclToDeclGroup(TheDecl);
}
Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
ParseDeclarator(DeclaratorInfo);
DeclGroupPtrTy DG =
ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
DeclEnd = Tok.getLocation();
if (!RequireSemi) return DG;
if (Tok.is(tok::semi)) {
ConsumeToken();
return DG;
}
Diag(Tok, diag::err_expected_semi_declation);
SkipUntil(tok::r_brace, true, true);
if (Tok.is(tok::semi))
ConsumeToken();
return DG;
}
Parser::DeclGroupPtrTy Parser::
ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
while (1) {
if (Tok.is(tok::kw_asm)) {
SourceLocation Loc;
OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
if (AsmLabel.isInvalid()) {
SkipUntil(tok::semi, true, true);
return DeclGroupPtrTy();
}
D.setAsmLabel(AsmLabel.release());
D.SetRangeEnd(Loc);
}
if (Tok.is(tok::kw___attribute)) {
SourceLocation Loc;
AttributeList *AttrList = ParseAttributes(&Loc);
D.AddAttributes(AttrList, Loc);
}
DeclPtrTy ThisDecl = Actions.ActOnDeclarator(CurScope, D);
DeclsInGroup.push_back(ThisDecl);
if (Tok.is(tok::equal)) {
ConsumeToken();
if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
SourceLocation DelLoc = ConsumeToken();
Actions.SetDeclDeleted(ThisDecl, DelLoc);
} else {
OwningExprResult Init(ParseInitializer());
if (Init.isInvalid()) {
SkipUntil(tok::semi, true, true);
return DeclGroupPtrTy();
}
Actions.AddInitializerToDecl(ThisDecl, move(Init));
}
} else if (Tok.is(tok::l_paren)) {
SourceLocation LParenLoc = ConsumeParen();
ExprVector Exprs(Actions);
CommaLocsTy CommaLocs;
if (ParseExpressionList(Exprs, CommaLocs)) {
SkipUntil(tok::r_paren);
} else {
SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
"Unexpected number of commas!");
Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
move_arg(Exprs),
&CommaLocs[0], RParenLoc);
}
} else {
Actions.ActOnUninitializedDecl(ThisDecl);
}
if (Tok.isNot(tok::comma))
break;
ConsumeToken();
D.clear();
if (Tok.is(tok::kw___attribute)) {
SourceLocation Loc;
AttributeList *AttrList = ParseAttributes(&Loc);
D.AddAttributes(AttrList, Loc);
}
ParseDeclarator(D);
}
return Actions.FinalizeDeclaratorGroup(CurScope, &DeclsInGroup[0],
DeclsInGroup.size());
}
void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
ParseDeclarationSpecifiers(DS);
unsigned Specs = DS.getParsedSpecifiers();
if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
!DS.getAttributes())
Diag(Tok, diag::err_typename_requires_specqual);
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();
}
}
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,
TemplateParameterLists *TemplateParams,
AccessSpecifier AS) {
assert(Tok.is(tok::identifier) && "should have identifier");
SourceLocation Loc = Tok.getLocation();
assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
if (isValidAfterIdentifierInDeclarator(NextToken())) {
return false;
}
if (SS == 0) {
const char *TagName = 0;
tok::TokenKind TagKind = tok::unknown;
switch (Actions.isTagName(*Tok.getIdentifierInfo(), CurScope)) {
default: break;
case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
}
if (TagName) {
Diag(Loc, diag::err_use_of_tag_name_without_tag)
<< Tok.getIdentifierInfo() << TagName
<< CodeModificationHint::CreateInsertion(Tok.getLocation(),TagName);
if (TagKind == tok::kw_enum)
ParseEnumSpecifier(Loc, DS, AS);
else
ParseClassSpecifier(TagKind, Loc, DS, TemplateParams, AS);
return true;
}
}
SourceRange R;
if (SS) R = SS->getRange();
Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
const char *PrevSpec;
DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec);
DS.SetRangeEnd(Tok.getLocation());
ConsumeToken();
return false;
}
void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
TemplateParameterLists *TemplateParams,
AccessSpecifier AS) {
DS.SetRangeStart(Tok.getLocation());
while (1) {
int isInvalid = false;
const char *PrevSpec = 0;
SourceLocation Loc = Tok.getLocation();
switch (Tok.getKind()) {
default:
DoneWithDeclSpec:
DS.Finish(Diags, PP);
return;
case tok::coloncolon: if (TryAnnotateCXXScopeToken())
continue;
goto DoneWithDeclSpec;
case tok::annot_cxxscope: {
if (DS.hasTypeSpecifier())
goto DoneWithDeclSpec;
Token Next = NextToken();
if (Next.is(tok::annot_template_id) &&
static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
->Kind == TNK_Type_template) {
CXXScopeSpec SS;
ParseOptionalCXXScopeSpecifier(SS);
assert(Tok.is(tok::annot_template_id) &&
"ParseOptionalCXXScopeSpecifier not working");
AnnotateTemplateIdTokenAsType(&SS);
continue;
}
if (Next.isNot(tok::identifier))
goto DoneWithDeclSpec;
CXXScopeSpec SS;
SS.setScopeRep(Tok.getAnnotationValue());
SS.setRange(Tok.getAnnotationRange());
if (Actions.isCurrentClassName(*Next.getIdentifierInfo(),
CurScope, &SS) &&
GetLookAheadToken(2).is(tok::l_paren))
goto DoneWithDeclSpec;
TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
Next.getLocation(), CurScope, &SS);
if (TypeRep == 0) {
ConsumeToken(); if (ParseImplicitInt(DS, &SS, TemplateParams, AS)) continue;
goto DoneWithDeclSpec;
}
ConsumeToken();
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
TypeRep);
if (isInvalid)
break;
DS.SetRangeEnd(Tok.getLocation());
ConsumeToken();
continue;
}
case tok::annot_typename: {
if (Tok.getAnnotationValue())
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Tok.getAnnotationValue());
else
DS.SetTypeSpecError();
DS.SetRangeEnd(Tok.getAnnotationEndLoc());
ConsumeToken();
if (!Tok.is(tok::less) || !getLang().ObjC1)
continue;
SourceLocation EndProtoLoc;
llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
DS.SetRangeEnd(EndProtoLoc);
continue;
}
case tok::identifier: {
if (getLang().CPlusPlus && TryAnnotateCXXScopeToken())
continue;
if (DS.hasTypeSpecifier())
goto DoneWithDeclSpec;
TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
Tok.getLocation(), CurScope);
if (TypeRep == 0) {
if (ParseImplicitInt(DS, 0, TemplateParams, AS)) continue;
goto DoneWithDeclSpec;
}
if (getLang().CPlusPlus && CurScope->isClassScope() &&
Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
NextToken().getKind() == tok::l_paren)
goto DoneWithDeclSpec;
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
TypeRep);
if (isInvalid)
break;
DS.SetRangeEnd(Tok.getLocation());
ConsumeToken();
if (!Tok.is(tok::less) || !getLang().ObjC1)
continue;
SourceLocation EndProtoLoc;
llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
DS.SetRangeEnd(EndProtoLoc);
continue;
}
case tok::annot_template_id: {
TemplateIdAnnotation *TemplateId
= static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
if (TemplateId->Kind != TNK_Type_template) {
goto DoneWithDeclSpec;
}
AnnotateTemplateIdTokenAsType();
continue;
}
case tok::kw___attribute:
DS.AddAttributes(ParseAttributes());
continue;
case tok::kw___declspec:
if (!PP.getLangOptions().Microsoft)
goto DoneWithDeclSpec;
FuzzyParseMicrosoftDeclSpec();
continue;
case tok::kw___forceinline:
case tok::kw___w64:
case tok::kw___cdecl:
case tok::kw___stdcall:
case tok::kw___fastcall:
if (!PP.getLangOptions().Microsoft)
goto DoneWithDeclSpec;
break;
case tok::kw_typedef:
isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
break;
case tok::kw_extern:
if (DS.isThreadSpecified())
Diag(Tok, diag::ext_thread_before) << "extern";
isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
break;
case tok::kw___private_extern__:
isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
PrevSpec);
break;
case tok::kw_static:
if (DS.isThreadSpecified())
Diag(Tok, diag::ext_thread_before) << "static";
isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
break;
case tok::kw_auto:
isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
break;
case tok::kw_register:
isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
break;
case tok::kw_mutable:
isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec);
break;
case tok::kw___thread:
isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
break;
case tok::kw_inline:
isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
break;
case tok::kw_virtual:
isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec);
break;
case tok::kw_explicit:
isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec);
break;
case tok::kw_short:
isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
break;
case tok::kw_long:
if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
else
isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
break;
case tok::kw_signed:
isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
break;
case tok::kw_unsigned:
isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
break;
case tok::kw__Complex:
isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
break;
case tok::kw__Imaginary:
isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
break;
case tok::kw_void:
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
break;
case tok::kw_char:
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
break;
case tok::kw_int:
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
break;
case tok::kw_float:
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
break;
case tok::kw_double:
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
break;
case tok::kw_wchar_t:
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
break;
case tok::kw_bool:
case tok::kw__Bool:
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
break;
case tok::kw__Decimal32:
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
break;
case tok::kw__Decimal64:
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
break;
case tok::kw__Decimal128:
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
break;
case tok::kw_class:
case tok::kw_struct:
case tok::kw_union: {
tok::TokenKind Kind = Tok.getKind();
ConsumeToken();
ParseClassSpecifier(Kind, Loc, DS, TemplateParams, AS);
continue;
}
case tok::kw_enum:
ConsumeToken();
ParseEnumSpecifier(Loc, DS, AS);
continue;
case tok::kw_const:
isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec,getLang())*2;
break;
case tok::kw_volatile:
isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
getLang())*2;
break;
case tok::kw_restrict:
isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
getLang())*2;
break;
case tok::kw_typename:
if (TryAnnotateTypeOrScopeToken())
continue;
break;
case tok::kw_typeof:
ParseTypeofSpecifier(DS);
continue;
case tok::less:
if (DS.hasTypeSpecifier() || !getLang().ObjC1)
goto DoneWithDeclSpec;
{
SourceLocation EndProtoLoc;
llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
DS.SetRangeEnd(EndProtoLoc);
Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
<< CodeModificationHint::CreateInsertion(Loc, "id")
<< SourceRange(Loc, EndProtoLoc);
continue;
}
}
if (isInvalid) {
assert(PrevSpec && "Method did not return previous specifier!");
unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
: diag::ext_duplicate_declspec;
Diag(Tok, DiagID) << PrevSpec;
}
DS.SetRangeEnd(Tok.getLocation());
ConsumeToken();
}
}
bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, int& isInvalid,
const char *&PrevSpec,
TemplateParameterLists *TemplateParams){
SourceLocation Loc = Tok.getLocation();
switch (Tok.getKind()) {
case tok::identifier: case tok::kw_typename: if (TryAnnotateTypeOrScopeToken())
return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
return false;
case tok::coloncolon: if (NextToken().is(tok::kw_new) || NextToken().is(tok::kw_delete)) return false;
if (TryAnnotateTypeOrScopeToken())
return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
return false;
case tok::annot_typename: {
if (Tok.getAnnotationValue())
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Tok.getAnnotationValue());
else
DS.SetTypeSpecError();
DS.SetRangeEnd(Tok.getAnnotationEndLoc());
ConsumeToken();
if (!Tok.is(tok::less) || !getLang().ObjC1)
return true;
SourceLocation EndProtoLoc;
llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
DS.SetRangeEnd(EndProtoLoc);
return true;
}
case tok::kw_short:
isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
break;
case tok::kw_long:
if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
else
isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
break;
case tok::kw_signed:
isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
break;
case tok::kw_unsigned:
isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
break;
case tok::kw__Complex:
isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
break;
case tok::kw__Imaginary:
isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
break;
case tok::kw_void:
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
break;
case tok::kw_char:
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
break;
case tok::kw_int:
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
break;
case tok::kw_float:
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
break;
case tok::kw_double:
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
break;
case tok::kw_wchar_t:
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
break;
case tok::kw_bool:
case tok::kw__Bool:
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
break;
case tok::kw__Decimal32:
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
break;
case tok::kw__Decimal64:
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
break;
case tok::kw__Decimal128:
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
break;
case tok::kw_class:
case tok::kw_struct:
case tok::kw_union: {
tok::TokenKind Kind = Tok.getKind();
ConsumeToken();
ParseClassSpecifier(Kind, Loc, DS, TemplateParams);
return true;
}
case tok::kw_enum:
ConsumeToken();
ParseEnumSpecifier(Loc, DS);
return true;
case tok::kw_const:
isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
getLang())*2;
break;
case tok::kw_volatile:
isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
getLang())*2;
break;
case tok::kw_restrict:
isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
getLang())*2;
break;
case tok::kw_typeof:
ParseTypeofSpecifier(DS);
return true;
case tok::kw___cdecl:
case tok::kw___stdcall:
case tok::kw___fastcall:
if (!PP.getLangOptions().Microsoft) return false;
ConsumeToken();
return true;
default:
return false;
}
if (isInvalid) {
assert(PrevSpec && "Method did not return previous specifier!");
unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
: diag::ext_duplicate_declspec;
Diag(Tok, DiagID) << PrevSpec;
}
DS.SetRangeEnd(Tok.getLocation());
ConsumeToken(); return true;
}
void Parser::
ParseStructDeclaration(DeclSpec &DS,
llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
if (Tok.is(tok::kw___extension__)) {
ExtensionRAIIObject O(Diags); ConsumeToken();
return ParseStructDeclaration(DS, Fields);
}
SourceLocation DSStart = Tok.getLocation();
ParseSpecifierQualifierList(DS);
if (Tok.is(tok::semi)) {
Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
return;
}
Fields.push_back(FieldDeclarator(DS));
while (1) {
FieldDeclarator &DeclaratorInfo = Fields.back();
if (Tok.isNot(tok::colon))
ParseDeclarator(DeclaratorInfo.D);
if (Tok.is(tok::colon)) {
ConsumeToken();
OwningExprResult Res(ParseConstantExpression());
if (Res.isInvalid())
SkipUntil(tok::semi, true, true);
else
DeclaratorInfo.BitfieldSize = Res.release();
}
if (Tok.is(tok::kw___attribute)) {
SourceLocation Loc;
AttributeList *AttrList = ParseAttributes(&Loc);
DeclaratorInfo.D.AddAttributes(AttrList, Loc);
}
if (Tok.isNot(tok::comma))
return;
ConsumeToken();
Fields.push_back(FieldDeclarator(DS));
if (Tok.is(tok::kw___attribute)) {
SourceLocation Loc;
AttributeList *AttrList = ParseAttributes(&Loc);
Fields.back().D.AddAttributes(AttrList, Loc);
}
}
}
void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
unsigned TagType, DeclPtrTy TagDecl) {
PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
PP.getSourceManager(),
"parsing struct/union body");
SourceLocation LBraceLoc = ConsumeBrace();
ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Actions.ActOnTagStartDefinition(CurScope, TagDecl);
if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Diag(Tok, diag::ext_empty_struct_union_enum)
<< DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
if (Tok.is(tok::semi)) {
Diag(Tok, diag::ext_extra_struct_semi)
<< CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
ConsumeToken();
continue;
}
DeclSpec DS;
FieldDeclarators.clear();
if (!Tok.is(tok::at)) {
ParseStructDeclaration(DS, FieldDeclarators);
for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
FieldDeclarator &FD = FieldDeclarators[i];
DeclPtrTy Field = Actions.ActOnField(CurScope, TagDecl,
DS.getSourceRange().getBegin(),
FD.D, FD.BitfieldSize);
FieldDecls.push_back(Field);
}
} else { ConsumeToken();
if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
Diag(Tok, diag::err_unexpected_at);
SkipUntil(tok::semi, true, 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, true);
continue;
}
llvm::SmallVector<DeclPtrTy, 16> Fields;
Actions.ActOnDefs(CurScope, 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)) {
Diag(Tok, diag::ext_expected_semi_decl_list);
break;
} else {
Diag(Tok, diag::err_expected_semi_decl_list);
SkipUntil(tok::r_brace, true, true);
}
}
SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
AttributeList *AttrList = 0;
if (Tok.is(tok::kw___attribute))
AttrList = ParseAttributes();
Actions.ActOnFields(CurScope,
RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
LBraceLoc, RBraceLoc,
AttrList);
StructScope.Exit();
Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
}
void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
AccessSpecifier AS) {
AttributeList *Attr = 0;
if (Tok.is(tok::kw___attribute))
Attr = ParseAttributes();
CXXScopeSpec SS;
if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS)) {
if (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)) {
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();
}
Action::TagKind TK;
if (Tok.is(tok::l_brace))
TK = Action::TK_Definition;
else if (Tok.is(tok::semi))
TK = Action::TK_Declaration;
else
TK = Action::TK_Reference;
DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK,
StartLoc, SS, Name, NameLoc, Attr, AS);
if (Tok.is(tok::l_brace))
ParseEnumBody(StartLoc, TagDecl);
const char *PrevSpec = 0;
if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec,
TagDecl.getAs<void>()))
Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
}
void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
ParseScope EnumScope(this, Scope::DeclScope);
Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
SourceLocation LBraceLoc = ConsumeBrace();
if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
DeclPtrTy LastEnumConstDecl;
while (Tok.is(tok::identifier)) {
IdentifierInfo *Ident = Tok.getIdentifierInfo();
SourceLocation IdentLoc = ConsumeToken();
SourceLocation EqualLoc;
OwningExprResult AssignedVal(Actions);
if (Tok.is(tok::equal)) {
EqualLoc = ConsumeToken();
AssignedVal = ParseConstantExpression();
if (AssignedVal.isInvalid())
SkipUntil(tok::comma, tok::r_brace, true, true);
}
DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
LastEnumConstDecl,
IdentLoc, Ident,
EqualLoc,
AssignedVal.release());
EnumConstantDecls.push_back(EnumConstDecl);
LastEnumConstDecl = EnumConstDecl;
if (Tok.isNot(tok::comma))
break;
SourceLocation CommaLoc = ConsumeToken();
if (Tok.isNot(tok::identifier) &&
!(getLang().C99 || getLang().CPlusPlus0x))
Diag(CommaLoc, diag::ext_enumerator_list_comma)
<< getLang().CPlusPlus
<< CodeModificationHint::CreateRemoval((SourceRange(CommaLoc)));
}
MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
EnumConstantDecls.size());
Action::AttrTy *AttrList = 0;
if (Tok.is(tok::kw___attribute))
AttrList = ParseAttributes();
EnumScope.Exit();
Actions.ActOnTagFinishDefinition(CurScope, EnumDecl);
}
bool Parser::isTypeQualifier() const {
switch (Tok.getKind()) {
default: return false;
case tok::kw_const:
case tok::kw_volatile:
case tok::kw_restrict:
return true;
}
}
bool Parser::isTypeSpecifierQualifier() {
switch (Tok.getKind()) {
default: return false;
case tok::identifier: case tok::kw_typename: if (TryAnnotateTypeOrScopeToken())
return isTypeSpecifierQualifier();
return false;
case tok::coloncolon: if (NextToken().is(tok::kw_new) || NextToken().is(tok::kw_delete)) return false;
if (TryAnnotateTypeOrScopeToken())
return isTypeSpecifierQualifier();
return false;
case tok::kw___attribute:
case tok::kw_typeof:
case tok::kw_short:
case tok::kw_long:
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_int:
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_class:
case tok::kw_struct:
case tok::kw_union:
case tok::kw_enum:
case tok::kw_const:
case tok::kw_volatile:
case tok::kw_restrict:
case tok::annot_typename:
return true;
case tok::less:
return getLang().ObjC1;
case tok::kw___cdecl:
case tok::kw___stdcall:
case tok::kw___fastcall:
return PP.getLangOptions().Microsoft;
}
}
bool Parser::isDeclarationSpecifier() {
switch (Tok.getKind()) {
default: return false;
case tok::identifier: if (getLang().ObjC1 && NextToken().is(tok::period))
return false;
case tok::kw_typename: if (TryAnnotateTypeOrScopeToken())
return isDeclarationSpecifier();
return false;
case tok::coloncolon: if (NextToken().is(tok::kw_new) || NextToken().is(tok::kw_delete)) return false;
if (TryAnnotateTypeOrScopeToken())
return isDeclarationSpecifier();
return false;
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_short:
case tok::kw_long:
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_int:
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_class:
case tok::kw_struct:
case tok::kw_union:
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::annot_typename:
case tok::kw_typeof:
case tok::kw___attribute:
return true;
case tok::less:
return getLang().ObjC1;
case tok::kw___declspec:
case tok::kw___cdecl:
case tok::kw___stdcall:
case tok::kw___fastcall:
return PP.getLangOptions().Microsoft;
}
}
void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
while (1) {
int isInvalid = false;
const char *PrevSpec = 0;
SourceLocation Loc = Tok.getLocation();
switch (Tok.getKind()) {
case tok::kw_const:
isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
getLang())*2;
break;
case tok::kw_volatile:
isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
getLang())*2;
break;
case tok::kw_restrict:
isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
getLang())*2;
break;
case tok::kw___ptr64:
case tok::kw___cdecl:
case tok::kw___stdcall:
case tok::kw___fastcall:
if (!PP.getLangOptions().Microsoft)
goto DoneWithTypeQuals;
break;
case tok::kw___attribute:
if (AttributesAllowed) {
DS.AddAttributes(ParseAttributes());
continue; }
default:
DoneWithTypeQuals:
DS.Finish(Diags, PP);
return;
}
if (isInvalid) {
assert(PrevSpec && "Method did not return previous specifier!");
unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
: diag::ext_duplicate_declspec;
Diag(Tok, DiagID) << PrevSpec;
}
ConsumeToken();
}
}
void Parser::ParseDeclarator(Declarator &D) {
ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
}
void Parser::ParseDeclaratorInternal(Declarator &D,
DirectDeclParseFunction DirectDeclParser) {
if (getLang().CPlusPlus &&
(Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
Tok.is(tok::annot_cxxscope))) {
CXXScopeSpec SS;
if (ParseOptionalCXXScopeSpecifier(SS)) {
if(Tok.isNot(tok::star)) {
D.getCXXScopeSpec() = SS;
if (DirectDeclParser)
(this->*DirectDeclParser)(D);
return;
}
SourceLocation Loc = ConsumeToken();
D.SetRangeEnd(Loc);
DeclSpec DS;
ParseTypeQualifierListOpt(DS);
D.ExtendWithDeclSpec(DS);
ParseDeclaratorInternal(D, DirectDeclParser);
D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Loc, DS.TakeAttributes()),
SourceLocation());
return;
}
}
tok::TokenKind Kind = Tok.getKind();
if (Kind != tok::star && Kind != tok::caret &&
(Kind != tok::amp || !getLang().CPlusPlus) &&
(Kind != tok::ampamp || !getLang().CPlusPlus)) {
if (DirectDeclParser)
(this->*DirectDeclParser)(D);
return;
}
SourceLocation Loc = ConsumeToken(); D.SetRangeEnd(Loc);
if (Kind == tok::star || Kind == tok::caret) {
DeclSpec DS;
ParseTypeQualifierListOpt(DS);
D.ExtendWithDeclSpec(DS);
ParseDeclaratorInternal(D, DirectDeclParser);
if (Kind == tok::star)
D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
DS.TakeAttributes()),
SourceLocation());
else
D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Loc, DS.TakeAttributes()),
SourceLocation());
} else {
DeclSpec DS;
if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
Diag(Loc, diag::err_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,
DS.TakeAttributes(),
Kind == tok::amp),
SourceLocation());
}
}
void Parser::ParseDirectDeclarator(Declarator &D) {
DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
if (getLang().CPlusPlus) {
if (D.mayHaveIdentifier()) {
bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec());
if (afterCXXScope) {
DeclScopeObj.EnterDeclaratorScope();
}
if (Tok.is(tok::identifier)) {
assert(Tok.getIdentifierInfo() && "Not an identifier?");
if (!D.getDeclSpec().hasTypeSpecifier() &&
Actions.isCurrentClassName(*Tok.getIdentifierInfo(),CurScope)) {
D.setConstructor(Actions.getTypeName(*Tok.getIdentifierInfo(),
Tok.getLocation(), CurScope),
Tok.getLocation());
} else
D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
ConsumeToken();
goto PastIdentifier;
} else if (Tok.is(tok::annot_template_id)) {
TemplateIdAnnotation *TemplateId
= static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
D.SetIdentifier(TemplateId->Name, Tok.getLocation());
TemplateId->Destroy();
ConsumeToken();
goto PastIdentifier;
} else if (Tok.is(tok::kw_operator)) {
SourceLocation OperatorLoc = Tok.getLocation();
SourceLocation EndLoc;
if (OverloadedOperatorKind Op = TryParseOperatorFunctionId(&EndLoc)) {
D.setOverloadedOperator(Op, OperatorLoc, EndLoc);
} else {
if (TypeTy *ConvType = ParseConversionFunctionId(&EndLoc))
D.setConversionFunction(ConvType, OperatorLoc, EndLoc);
else {
D.SetIdentifier(0, Tok.getLocation());
}
}
goto PastIdentifier;
} else if (Tok.is(tok::tilde)) {
SourceLocation TildeLoc = ConsumeToken();
if (Tok.is(tok::identifier)) {
SourceLocation NameLoc = Tok.getLocation();
SourceLocation EndLoc;
TypeResult Type = ParseClassName(EndLoc);
if (Type.isInvalid())
D.SetIdentifier(0, TildeLoc);
else
D.setDestructor(Type.get(), TildeLoc, NameLoc);
} else {
Diag(Tok, diag::err_expected_class_name);
D.SetIdentifier(0, TildeLoc);
}
goto PastIdentifier;
}
if (afterCXXScope) {
Diag(Tok, diag::err_expected_unqualified_id);
D.SetIdentifier(0, Tok.getLocation());
D.setInvalidType(true);
goto PastIdentifier;
}
}
}
if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
assert(!getLang().CPlusPlus &&
"There's a C++-specific check for tok::identifier above");
assert(Tok.getIdentifierInfo() && "Not an identifier?");
D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
ConsumeToken();
} else if (Tok.is(tok::l_paren)) {
ParseParenDeclarator(D);
} else if (D.mayOmitIdentifier()) {
D.SetIdentifier(0, Tok.getLocation());
} else {
if (D.getContext() == Declarator::MemberContext)
Diag(Tok, diag::err_expected_member_name_or_semi)
<< D.getDeclSpec().getSourceRange();
else if (getLang().CPlusPlus)
Diag(Tok, diag::err_expected_unqualified_id);
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?");
while (1) {
if (Tok.is(tok::l_paren)) {
if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
if (!isCXXFunctionDeclarator(warnIfAmbiguous))
break;
}
ParseFunctionDeclarator(ConsumeParen(), D);
} else if (Tok.is(tok::l_square)) {
ParseBracketDeclarator(D);
} else {
break;
}
}
}
void Parser::ParseParenDeclarator(Declarator &D) {
SourceLocation StartLoc = ConsumeParen();
assert(!D.isPastIdentifier() && "Should be called before passing identifier");
AttributeList *AttrList = 0;
bool RequiresArg = false;
if (Tok.is(tok::kw___attribute)) {
AttrList = ParseAttributes();
RequiresArg = true;
}
while ((Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
(Tok.is(tok::kw___fastcall))) && PP.getLangOptions().Microsoft)
ConsumeToken();
bool isGrouping;
if (!D.mayOmitIdentifier()) {
isGrouping = true;
} else if (Tok.is(tok::r_paren) || (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || isDeclarationSpecifier()) { isGrouping = false;
} else {
isGrouping = true;
}
if (isGrouping) {
bool hadGroupingParens = D.hasGroupingParens();
D.setGroupingParens(true);
if (AttrList)
D.AddAttributes(AttrList, SourceLocation());
ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
D.setGroupingParens(hadGroupingParens);
D.SetRangeEnd(Loc);
return;
}
D.SetIdentifier(0, Tok.getLocation());
ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
}
void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
AttributeList *AttrList,
bool RequiresArg) {
assert(D.isPastIdentifier() && "Should not call before identifier!");
if (Tok.is(tok::r_paren)) {
if (RequiresArg) {
Diag(Tok, diag::err_argument_required_after_attribute);
delete AttrList;
}
SourceLocation Loc = ConsumeParen();
DeclSpec DS;
bool hasExceptionSpec = false;
bool hasAnyExceptionSpec = false;
std::vector<TypeTy*> Exceptions;
if (getLang().CPlusPlus) {
ParseTypeQualifierListOpt(DS, false );
if (!DS.getSourceRange().getEnd().isInvalid())
Loc = DS.getSourceRange().getEnd();
if (Tok.is(tok::kw_throw)) {
hasExceptionSpec = true;
ParseExceptionSpecification(Loc, Exceptions, hasAnyExceptionSpec);
}
}
D.AddTypeInfo(DeclaratorChunk::getFunction(getLang().CPlusPlus,
false,
SourceLocation(),
0, 0,
DS.getTypeQualifiers(),
hasExceptionSpec,
hasAnyExceptionSpec,
Exceptions.empty() ? 0 :
&Exceptions[0],
Exceptions.size(),
LParenLoc, D),
Loc);
return;
}
if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
if (!TryAnnotateTypeOrScopeToken()) {
if (RequiresArg) {
Diag(Tok, diag::err_argument_required_after_attribute);
delete AttrList;
}
return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
}
}
llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
ParseScope PrototypeScope(this,
Scope::FunctionPrototypeScope|Scope::DeclScope);
bool IsVariadic = false;
SourceLocation EllipsisLoc;
while (1) {
if (Tok.is(tok::ellipsis)) {
IsVariadic = true;
EllipsisLoc = ConsumeToken(); break;
}
SourceLocation DSStart = Tok.getLocation();
DeclSpec DS;
if (AttrList) {
DS.AddAttributes(AttrList);
AttrList = 0; }
ParseDeclarationSpecifiers(DS);
Declarator ParmDecl(DS, Declarator::PrototypeContext);
ParseDeclarator(ParmDecl);
if (Tok.is(tok::kw___attribute)) {
SourceLocation Loc;
AttributeList *AttrList = ParseAttributes(&Loc);
ParmDecl.AddAttributes(AttrList, Loc);
}
IdentifierInfo *ParmII = ParmDecl.getIdentifier();
CachedTokens *DefArgToks = 0;
if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
ParmDecl.getNumTypeObjects() == 0) {
Diag(DSStart, diag::err_missing_param);
} else {
DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, 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,
tok::semi, false)) {
delete DefArgToks;
DefArgToks = 0;
Actions.ActOnParamDefaultArgumentError(Param);
} else
Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc);
} else {
ConsumeToken();
OwningExprResult DefArgResult(ParseAssignmentExpression());
if (DefArgResult.isInvalid()) {
Actions.ActOnParamDefaultArgumentError(Param);
SkipUntil(tok::comma, tok::r_paren, true, true);
} else {
Actions.ActOnParamDefaultArgument(Param, EqualLoc,
move(DefArgResult));
}
}
}
ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
ParmDecl.getIdentifierLoc(), Param,
DefArgToks));
}
if (Tok.isNot(tok::comma)) break;
ConsumeToken();
}
PrototypeScope.Exit();
SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
DeclSpec DS;
bool hasExceptionSpec = false;
bool hasAnyExceptionSpec = false;
std::vector<TypeTy*> Exceptions;
if (getLang().CPlusPlus) {
ParseTypeQualifierListOpt(DS, false );
if (!DS.getSourceRange().getEnd().isInvalid())
Loc = DS.getSourceRange().getEnd();
if (Tok.is(tok::kw_throw)) {
hasExceptionSpec = true;
ParseExceptionSpecification(Loc, Exceptions, hasAnyExceptionSpec);
}
}
D.AddTypeInfo(DeclaratorChunk::getFunction(true, IsVariadic,
EllipsisLoc,
&ParamInfo[0], ParamInfo.size(),
DS.getTypeQualifiers(),
hasExceptionSpec,
hasAnyExceptionSpec,
Exceptions.empty() ? 0 :
&Exceptions[0],
Exceptions.size(), LParenLoc, D),
Loc);
}
void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
Declarator &D) {
llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
if (!D.getIdentifier())
Diag(Tok, diag::ext_ident_list_in_param);
ParamsSoFar.insert(Tok.getIdentifierInfo());
ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
Tok.getLocation(),
DeclPtrTy()));
ConsumeToken();
while (Tok.is(tok::comma)) {
ConsumeToken();
if (Tok.isNot(tok::identifier)) {
Diag(Tok, diag::err_expected_ident);
SkipUntil(tok::r_paren);
return;
}
IdentifierInfo *ParmII = Tok.getIdentifierInfo();
if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
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(),
DeclPtrTy()));
}
ConsumeToken();
}
SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
D.AddTypeInfo(DeclaratorChunk::getFunction(false, false,
SourceLocation(),
&ParamInfo[0], ParamInfo.size(),
0,
false, false, 0, 0,
LParenLoc, D),
RLoc);
}
void Parser::ParseBracketDeclarator(Declarator &D) {
SourceLocation StartLoc = ConsumeBracket();
if (Tok.getKind() == tok::r_square) {
SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
OwningExprResult NumElements(Actions);
D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, StartLoc),
EndLoc);
return;
} else if (Tok.getKind() == tok::numeric_constant &&
GetLookAheadToken(1).is(tok::r_square)) {
OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
ConsumeToken();
SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
if (ExprRes.isInvalid())
ExprRes.release();
D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
ExprRes.release(), StartLoc),
EndLoc);
return;
}
SourceLocation StaticLoc;
if (Tok.is(tok::kw_static))
StaticLoc = ConsumeToken();
DeclSpec DS;
ParseTypeQualifierListOpt(DS, false );
if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
StaticLoc = ConsumeToken();
bool isStar = false;
OwningExprResult NumElements(Actions);
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)) {
NumElements = ParseAssignmentExpression();
}
if (NumElements.isInvalid()) {
D.setInvalidType(true);
SkipUntil(tok::r_square);
return;
}
SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
StaticLoc.isValid(), isStar,
NumElements.release(), StartLoc),
EndLoc);
}
void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
SourceLocation StartLoc = ConsumeToken();
if (Tok.isNot(tok::l_paren)) {
if (!getLang().CPlusPlus) {
Diag(Tok, diag::err_expected_lparen_after_id) << BuiltinII;
return;
}
OwningExprResult Result(ParseCastExpression(true));
if (Result.isInvalid()) {
DS.SetTypeSpecError();
return;
}
const char *PrevSpec = 0;
if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Result.release()))
Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
DS.SetRangeEnd(Tok.getLocation());
return;
}
SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
if (isTypeIdInParens()) {
Action::TypeResult Ty = ParseTypeName();
assert((Ty.isInvalid() || Ty.get()) &&
"Parser::ParseTypeofSpecifier(): missing type");
if (Tok.isNot(tok::r_paren)) {
MatchRHSPunctuation(tok::r_paren, LParenLoc);
return;
}
RParenLoc = ConsumeParen();
if (Ty.isInvalid())
DS.SetTypeSpecError();
else {
const char *PrevSpec = 0;
if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
Ty.get()))
Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
}
} else { OwningExprResult Result(ParseExpression());
if (Result.isInvalid() || Tok.isNot(tok::r_paren)) {
MatchRHSPunctuation(tok::r_paren, LParenLoc);
DS.SetTypeSpecError();
return;
}
RParenLoc = ConsumeParen();
const char *PrevSpec = 0;
if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Result.release()))
Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
}
DS.SetRangeEnd(RParenLoc);
}