#include "clang/Parse/Parser.h"
#include "clang/Parse/ParseDiagnostic.h"
#include "clang/Parse/DeclSpec.h"
#include "clang/Parse/Scope.h"
#include "llvm/Support/raw_ostream.h"
#include "ExtensionRAIIObject.h"
#include "ParsePragma.h"
using namespace clang;
Parser::Parser(Preprocessor &pp, Action &actions)
: CrashInfo(*this), PP(pp), Actions(actions), Diags(PP.getDiagnostics()),
GreaterThanIsOperator(true) {
Tok.setKind(tok::eof);
CurScope = 0;
NumCachedScopes = 0;
ParenCount = BracketCount = BraceCount = 0;
ObjCImpDecl = DeclPtrTy();
PackHandler.reset(new
PragmaPackHandler(&PP.getIdentifierTable().get("pack"), actions));
PP.AddPragmaHandler(0, PackHandler.get());
UnusedHandler.reset(new
PragmaUnusedHandler(&PP.getIdentifierTable().get("unused"), actions,
*this));
PP.AddPragmaHandler(0, UnusedHandler.get());
PushTopClassStack();
}
void PrettyStackTraceParserEntry::print(llvm::raw_ostream &OS) const {
const Token &Tok = P.getCurToken();
if (Tok.is(tok::eof)) {
OS << "<eof> parser at end of file\n";
return;
}
if (Tok.getLocation().isInvalid()) {
OS << "<unknown> parser at unknown location\n";
return;
}
const Preprocessor &PP = P.getPreprocessor();
Tok.getLocation().print(OS, PP.getSourceManager());
OS << ": current parser token '" << PP.getSpelling(Tok) << "'\n";
}
DiagnosticBuilder Parser::Diag(SourceLocation Loc, unsigned DiagID) {
return Diags.Report(FullSourceLoc(Loc, PP.getSourceManager()), DiagID);
}
DiagnosticBuilder Parser::Diag(const Token &Tok, unsigned DiagID) {
return Diag(Tok.getLocation(), DiagID);
}
void Parser::SuggestParentheses(SourceLocation Loc, unsigned DK,
SourceRange ParenRange) {
SourceLocation EndLoc = PP.getLocForEndOfToken(ParenRange.getEnd());
if (!ParenRange.getEnd().isFileID() || EndLoc.isInvalid()) {
Diag(Loc, DK);
return;
}
Diag(Loc, DK)
<< CodeModificationHint::CreateInsertion(ParenRange.getBegin(), "(")
<< CodeModificationHint::CreateInsertion(EndLoc, ")");
}
SourceLocation Parser::MatchRHSPunctuation(tok::TokenKind RHSTok,
SourceLocation LHSLoc) {
if (Tok.is(RHSTok))
return ConsumeAnyToken();
SourceLocation R = Tok.getLocation();
const char *LHSName = "unknown";
diag::kind DID = diag::err_parse_error;
switch (RHSTok) {
default: break;
case tok::r_paren : LHSName = "("; DID = diag::err_expected_rparen; break;
case tok::r_brace : LHSName = "{"; DID = diag::err_expected_rbrace; break;
case tok::r_square: LHSName = "["; DID = diag::err_expected_rsquare; break;
case tok::greater: LHSName = "<"; DID = diag::err_expected_greater; break;
}
Diag(Tok, DID);
Diag(LHSLoc, diag::note_matching) << LHSName;
SkipUntil(RHSTok);
return R;
}
bool Parser::ExpectAndConsume(tok::TokenKind ExpectedTok, unsigned DiagID,
const char *Msg, tok::TokenKind SkipToTok) {
if (Tok.is(ExpectedTok)) {
ConsumeAnyToken();
return false;
}
const char *Spelling = 0;
SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
if (EndLoc.isValid() &&
(Spelling = tok::getTokenSimpleSpelling(ExpectedTok))) {
Diag(EndLoc, DiagID)
<< Msg
<< CodeModificationHint::CreateInsertion(EndLoc, Spelling);
} else
Diag(Tok, DiagID) << Msg;
if (SkipToTok != tok::unknown)
SkipUntil(SkipToTok);
return true;
}
bool Parser::SkipUntil(const tok::TokenKind *Toks, unsigned NumToks,
bool StopAtSemi, bool DontConsume) {
bool isFirstTokenSkipped = true;
while (1) {
for (unsigned i = 0; i != NumToks; ++i) {
if (Tok.is(Toks[i])) {
if (DontConsume) {
} else {
ConsumeAnyToken();
}
return true;
}
}
switch (Tok.getKind()) {
case tok::eof:
return false;
case tok::l_paren:
ConsumeParen();
SkipUntil(tok::r_paren, false);
break;
case tok::l_square:
ConsumeBracket();
SkipUntil(tok::r_square, false);
break;
case tok::l_brace:
ConsumeBrace();
SkipUntil(tok::r_brace, false);
break;
case tok::r_paren:
if (ParenCount && !isFirstTokenSkipped)
return false; ConsumeParen();
break;
case tok::r_square:
if (BracketCount && !isFirstTokenSkipped)
return false; ConsumeBracket();
break;
case tok::r_brace:
if (BraceCount && !isFirstTokenSkipped)
return false; ConsumeBrace();
break;
case tok::string_literal:
case tok::wide_string_literal:
ConsumeStringToken();
break;
case tok::semi:
if (StopAtSemi)
return false;
default:
ConsumeToken();
break;
}
isFirstTokenSkipped = false;
}
}
void Parser::EnterScope(unsigned ScopeFlags) {
if (NumCachedScopes) {
Scope *N = ScopeCache[--NumCachedScopes];
N->Init(CurScope, ScopeFlags);
CurScope = N;
} else {
CurScope = new Scope(CurScope, ScopeFlags);
}
}
void Parser::ExitScope() {
assert(CurScope && "Scope imbalance!");
if (!CurScope->decl_empty())
Actions.ActOnPopScope(Tok.getLocation(), CurScope);
Scope *OldScope = CurScope;
CurScope = OldScope->getParent();
if (NumCachedScopes == ScopeCacheSize)
delete OldScope;
else
ScopeCache[NumCachedScopes++] = OldScope;
}
Parser::~Parser() {
delete CurScope;
for (unsigned i = 0, e = NumCachedScopes; i != e; ++i)
delete ScopeCache[i];
PP.RemovePragmaHandler(0, PackHandler.get());
PackHandler.reset();
PP.RemovePragmaHandler(0, UnusedHandler.get());
UnusedHandler.reset();
}
void Parser::Initialize() {
ConsumeToken();
assert(CurScope == 0 && "A scope is already active?");
EnterScope(Scope::DeclScope);
Actions.ActOnTranslationUnitScope(Tok.getLocation(), CurScope);
if (Tok.is(tok::eof) &&
!getLang().CPlusPlus) Diag(Tok, diag::ext_empty_source_file);
if (getLang().ObjC1) {
ObjCTypeQuals[objc_in] = &PP.getIdentifierTable().get("in");
ObjCTypeQuals[objc_out] = &PP.getIdentifierTable().get("out");
ObjCTypeQuals[objc_inout] = &PP.getIdentifierTable().get("inout");
ObjCTypeQuals[objc_oneway] = &PP.getIdentifierTable().get("oneway");
ObjCTypeQuals[objc_bycopy] = &PP.getIdentifierTable().get("bycopy");
ObjCTypeQuals[objc_byref] = &PP.getIdentifierTable().get("byref");
}
Ident_super = &PP.getIdentifierTable().get("super");
}
bool Parser::ParseTopLevelDecl(DeclGroupPtrTy &Result) {
Result = DeclGroupPtrTy();
if (Tok.is(tok::eof)) {
Actions.ActOnEndOfTranslationUnit();
return true;
}
Result = ParseExternalDeclaration();
return false;
}
void Parser::ParseTranslationUnit() {
Initialize();
DeclGroupPtrTy Res;
while (!ParseTopLevelDecl(Res))
;
ExitScope();
assert(CurScope == 0 && "Scope imbalance!");
}
Parser::DeclGroupPtrTy Parser::ParseExternalDeclaration() {
DeclPtrTy SingleDecl;
switch (Tok.getKind()) {
case tok::semi:
Diag(Tok, diag::ext_top_level_semi)
<< CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
ConsumeToken();
return DeclGroupPtrTy();
case tok::r_brace:
Diag(Tok, diag::err_expected_external_declaration);
ConsumeBrace();
return DeclGroupPtrTy();
case tok::eof:
Diag(Tok, diag::err_expected_external_declaration);
return DeclGroupPtrTy();
case tok::kw___extension__: {
ExtensionRAIIObject O(Diags); ConsumeToken();
return ParseExternalDeclaration();
}
case tok::kw_asm: {
OwningExprResult Result(ParseSimpleAsm());
ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
"top-level asm block");
if (Result.isInvalid())
return DeclGroupPtrTy();
SingleDecl = Actions.ActOnFileScopeAsmDecl(Tok.getLocation(), move(Result));
break;
}
case tok::at:
SingleDecl = ParseObjCAtDirectives();
break;
case tok::minus:
case tok::plus:
if (!getLang().ObjC1) {
Diag(Tok, diag::err_expected_external_declaration);
ConsumeToken();
return DeclGroupPtrTy();
}
SingleDecl = ParseObjCMethodDefinition();
break;
case tok::kw_using:
case tok::kw_namespace:
case tok::kw_typedef:
case tok::kw_template:
case tok::kw_export: case tok::kw_static_assert:
{
SourceLocation DeclEnd;
return ParseDeclaration(Declarator::FileContext, DeclEnd);
}
default:
return ParseDeclarationOrFunctionDefinition();
}
return Actions.ConvertDeclToDeclGroup(SingleDecl);
}
Parser::DeclGroupPtrTy
Parser::ParseDeclarationOrFunctionDefinition(
TemplateParameterLists *TemplateParams,
AccessSpecifier AS) {
DeclSpec DS;
ParseDeclarationSpecifiers(DS, TemplateParams, AS);
if (Tok.is(tok::semi)) {
ConsumeToken();
DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
return Actions.ConvertDeclToDeclGroup(TheDecl);
}
if (getLang().ObjC2 && Tok.is(tok::at)) {
SourceLocation AtLoc = ConsumeToken(); if (!Tok.isObjCAtKeyword(tok::objc_interface) &&
!Tok.isObjCAtKeyword(tok::objc_protocol)) {
Diag(Tok, diag::err_objc_unexpected_attr);
SkipUntil(tok::semi); return DeclGroupPtrTy();
}
const char *PrevSpec = 0;
if (DS.SetTypeSpecType(DeclSpec::TST_unspecified, AtLoc, PrevSpec))
Diag(AtLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
DeclPtrTy TheDecl;
if (Tok.isObjCAtKeyword(tok::objc_protocol))
TheDecl = ParseObjCAtProtocolDeclaration(AtLoc, DS.getAttributes());
else
TheDecl = ParseObjCAtInterfaceDeclaration(AtLoc, DS.getAttributes());
return Actions.ConvertDeclToDeclGroup(TheDecl);
}
if (Tok.is(tok::string_literal) && getLang().CPlusPlus &&
DS.getStorageClassSpec() == DeclSpec::SCS_extern &&
DS.getParsedSpecifiers() == DeclSpec::PQ_StorageClassSpecifier) {
DeclPtrTy TheDecl = ParseLinkage(Declarator::FileContext);
return Actions.ConvertDeclToDeclGroup(TheDecl);
}
Declarator DeclaratorInfo(DS, Declarator::FileContext);
ParseDeclarator(DeclaratorInfo);
if (!DeclaratorInfo.hasName()) {
SkipUntil(tok::r_brace, true, true);
if (Tok.is(tok::semi))
ConsumeToken();
return DeclGroupPtrTy();
}
if (Tok.is(tok::equal) || Tok.is(tok::comma) || Tok.is(tok::semi) || Tok.is(tok::kw_asm) || Tok.is(tok::kw___attribute) || (getLang().CPlusPlus &&
Tok.is(tok::l_paren))) { DeclGroupPtrTy DG =
ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
ExpectAndConsume(tok::semi, diag::err_expected_semi_declation);
return DG;
}
if (DeclaratorInfo.isFunctionDeclarator() &&
(Tok.is(tok::l_brace) || (!getLang().CPlusPlus &&
isDeclarationSpecifier()) || (getLang().CPlusPlus &&
(Tok.is(tok::colon) || Tok.is(tok::kw_try))))) { if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
Diag(Tok, diag::err_function_declared_typedef);
if (Tok.is(tok::l_brace)) {
ConsumeBrace();
SkipUntil(tok::r_brace, true);
} else {
SkipUntil(tok::semi);
}
return DeclGroupPtrTy();
}
DeclPtrTy TheDecl = ParseFunctionDefinition(DeclaratorInfo);
return Actions.ConvertDeclToDeclGroup(TheDecl);
}
if (DeclaratorInfo.isFunctionDeclarator())
Diag(Tok, diag::err_expected_fn_body);
else
Diag(Tok, diag::err_invalid_token_after_toplevel_declarator);
SkipUntil(tok::semi);
return DeclGroupPtrTy();
}
Parser::DeclPtrTy Parser::ParseFunctionDefinition(Declarator &D) {
const DeclaratorChunk &FnTypeInfo = D.getTypeObject(0);
assert(FnTypeInfo.Kind == DeclaratorChunk::Function &&
"This isn't a function declarator!");
const DeclaratorChunk::FunctionTypeInfo &FTI = FnTypeInfo.Fun;
if (getLang().ImplicitInt && D.getDeclSpec().isEmpty()) {
const char *PrevSpec;
D.getMutableDeclSpec().SetTypeSpecType(DeclSpec::TST_int,
D.getIdentifierLoc(),
PrevSpec);
D.SetRangeBegin(D.getDeclSpec().getSourceRange().getBegin());
}
if (!FTI.hasPrototype && FTI.NumArgs != 0)
ParseKNRParamDeclarations(D);
if (Tok.isNot(tok::l_brace) && Tok.isNot(tok::colon) &&
Tok.isNot(tok::kw_try)) {
Diag(Tok, diag::err_expected_fn_body);
SkipUntil(tok::l_brace, true, true);
if (Tok.isNot(tok::l_brace))
return DeclPtrTy();
}
ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
DeclPtrTy Res = Actions.ActOnStartOfFunctionDef(CurScope, D);
if (Tok.is(tok::kw_try))
return ParseFunctionTryBlock(Res);
if (Tok.is(tok::colon))
ParseConstructorInitializer(Res);
return ParseFunctionStatementBody(Res);
}
void Parser::ParseKNRParamDeclarations(Declarator &D) {
DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope|Scope::DeclScope);
while (isDeclarationSpecifier()) {
SourceLocation DSStart = Tok.getLocation();
DeclSpec DS;
ParseDeclarationSpecifiers(DS);
if (Tok.is(tok::semi)) {
Diag(DSStart, diag::err_declaration_does_not_declare_param);
ConsumeToken();
continue;
}
if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
DS.getStorageClassSpec() != DeclSpec::SCS_register) {
Diag(DS.getStorageClassSpecLoc(),
diag::err_invalid_storage_class_in_func_decl);
DS.ClearStorageClassSpecs();
}
if (DS.isThreadSpecified()) {
Diag(DS.getThreadSpecLoc(),
diag::err_invalid_storage_class_in_func_decl);
DS.ClearStorageClassSpecs();
}
Declarator ParmDeclarator(DS, Declarator::KNRTypeListContext);
ParseDeclarator(ParmDeclarator);
while (1) {
Action::AttrTy *AttrList;
if (Tok.is(tok::kw___attribute))
AttrList = ParseAttributes();
Action::DeclPtrTy Param =
Actions.ActOnParamDeclarator(CurScope, ParmDeclarator);
if (Param &&
ParmDeclarator.getIdentifier()) {
for (unsigned i = 0; ; ++i) {
if (i == FTI.NumArgs) {
Diag(ParmDeclarator.getIdentifierLoc(), diag::err_no_matching_param)
<< ParmDeclarator.getIdentifier();
break;
}
if (FTI.ArgInfo[i].Ident == ParmDeclarator.getIdentifier()) {
if (FTI.ArgInfo[i].Param) {
Diag(ParmDeclarator.getIdentifierLoc(),
diag::err_param_redefinition)
<< ParmDeclarator.getIdentifier();
} else {
FTI.ArgInfo[i].Param = Param;
}
break;
}
}
}
if (Tok.isNot(tok::comma))
break;
ConsumeToken();
ParmDeclarator.clear();
ParseDeclarator(ParmDeclarator);
}
if (Tok.is(tok::semi)) {
ConsumeToken();
} else {
Diag(Tok, diag::err_parse_error);
SkipUntil(tok::semi, true);
if (Tok.is(tok::semi))
ConsumeToken();
}
}
Actions.ActOnFinishKNRParamDeclarations(CurScope, D, Tok.getLocation());
}
Parser::OwningExprResult Parser::ParseAsmStringLiteral() {
if (!isTokenStringLiteral()) {
Diag(Tok, diag::err_expected_string_literal);
return ExprError();
}
OwningExprResult Res(ParseStringLiteralExpression());
if (Res.isInvalid()) return move(Res);
return move(Res);
}
Parser::OwningExprResult Parser::ParseSimpleAsm(SourceLocation *EndLoc) {
assert(Tok.is(tok::kw_asm) && "Not an asm!");
SourceLocation Loc = ConsumeToken();
if (Tok.isNot(tok::l_paren)) {
Diag(Tok, diag::err_expected_lparen_after) << "asm";
return ExprError();
}
Loc = ConsumeParen();
OwningExprResult Result(ParseAsmStringLiteral());
if (Result.isInvalid()) {
SkipUntil(tok::r_paren, true, true);
if (EndLoc)
*EndLoc = Tok.getLocation();
ConsumeAnyToken();
} else {
Loc = MatchRHSPunctuation(tok::r_paren, Loc);
if (EndLoc)
*EndLoc = Loc;
}
return move(Result);
}
bool Parser::TryAnnotateTypeOrScopeToken() {
assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon)
|| Tok.is(tok::kw_typename)) &&
"Cannot be a type or scope token!");
if (Tok.is(tok::kw_typename)) {
SourceLocation TypenameLoc = ConsumeToken();
CXXScopeSpec SS;
bool HadNestedNameSpecifier = ParseOptionalCXXScopeSpecifier(SS);
if (!HadNestedNameSpecifier) {
Diag(Tok.getLocation(), diag::err_expected_qualified_after_typename);
return false;
}
TypeResult Ty;
if (Tok.is(tok::identifier)) {
Ty = Actions.ActOnTypenameType(TypenameLoc, SS, *Tok.getIdentifierInfo(),
Tok.getLocation());
} else if (Tok.is(tok::annot_template_id)) {
TemplateIdAnnotation *TemplateId
= static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
if (TemplateId->Kind == TNK_Function_template) {
Diag(Tok, diag::err_typename_refers_to_non_type_template)
<< Tok.getAnnotationRange();
return false;
}
AnnotateTemplateIdTokenAsType(0);
assert(Tok.is(tok::annot_typename) &&
"AnnotateTemplateIdTokenAsType isn't working properly");
if (Tok.getAnnotationValue())
Ty = Actions.ActOnTypenameType(TypenameLoc, SS, SourceLocation(),
Tok.getAnnotationValue());
else
Ty = true;
} else {
Diag(Tok, diag::err_expected_type_name_after_typename)
<< SS.getRange();
return false;
}
Tok.setKind(tok::annot_typename);
Tok.setAnnotationValue(Ty.isInvalid()? 0 : Ty.get());
Tok.setAnnotationEndLoc(Tok.getLocation());
Tok.setLocation(TypenameLoc);
PP.AnnotateCachedTokens(Tok);
return true;
}
CXXScopeSpec SS;
if (getLang().CPlusPlus)
ParseOptionalCXXScopeSpecifier(SS);
if (Tok.is(tok::identifier)) {
if (TypeTy *Ty = Actions.getTypeName(*Tok.getIdentifierInfo(),
Tok.getLocation(), CurScope, &SS)) {
Tok.setKind(tok::annot_typename);
Tok.setAnnotationValue(Ty);
Tok.setAnnotationEndLoc(Tok.getLocation());
if (SS.isNotEmpty()) Tok.setLocation(SS.getBeginLoc());
PP.AnnotateCachedTokens(Tok);
return true;
}
if (!getLang().CPlusPlus) {
return false;
}
if (NextToken().is(tok::less)) {
TemplateTy Template;
if (TemplateNameKind TNK
= Actions.isTemplateName(*Tok.getIdentifierInfo(),
CurScope, Template, &SS))
AnnotateTemplateIdToken(Template, TNK, &SS);
}
}
if (Tok.is(tok::annot_template_id)) {
TemplateIdAnnotation *TemplateId
= static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
if (TemplateId->Kind == TNK_Type_template) {
AnnotateTemplateIdTokenAsType(&SS);
return true;
}
}
if (SS.isEmpty())
return false;
if (PP.isBacktrackEnabled())
PP.RevertCachedTokens(1);
else
PP.EnterToken(Tok);
Tok.setKind(tok::annot_cxxscope);
Tok.setAnnotationValue(SS.getScopeRep());
Tok.setAnnotationRange(SS.getRange());
PP.AnnotateCachedTokens(Tok);
return true;
}
bool Parser::TryAnnotateCXXScopeToken() {
assert(getLang().CPlusPlus &&
"Call sites of this function should be guarded by checking for C++");
assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) &&
"Cannot be a type or scope token!");
CXXScopeSpec SS;
if (!ParseOptionalCXXScopeSpecifier(SS))
return Tok.is(tok::annot_template_id);
if (PP.isBacktrackEnabled())
PP.RevertCachedTokens(1);
else
PP.EnterToken(Tok);
Tok.setKind(tok::annot_cxxscope);
Tok.setAnnotationValue(SS.getScopeRep());
Tok.setAnnotationRange(SS.getRange());
PP.AnnotateCachedTokens(Tok);
return true;
}