19 using namespace clang;
24 NamedDecl *Parser::ParseCXXInlineMethodDef(
29 assert(Tok.
isOneOf(tok::l_brace, tok::colon, tok::kw_try, tok::equal) &&
30 "Current token not a '{', ':', '=', or 'try'!");
33 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->data()
35 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->size() : 0);
43 TemplateParams,
nullptr,
53 HandleMemberFunctionDeclDelays(D, FnD);
68 ? diag::warn_cxx98_compat_defaulted_deleted_function
69 : diag::ext_defaulted_deleted_function)
73 if (
auto *DeclAsFunction = dyn_cast<FunctionDecl>(FnD)) {
74 DeclAsFunction->setRangeEnd(KWEndLoc);
78 ? diag::warn_cxx98_compat_defaulted_deleted_function
79 : diag::ext_defaulted_deleted_function)
82 if (
auto *DeclAsFunction = dyn_cast<FunctionDecl>(FnD)) {
83 DeclAsFunction->setRangeEnd(KWEndLoc);
86 llvm_unreachable(
"function definition after = not 'delete' or 'default'");
89 if (Tok.
is(tok::comma)) {
90 Diag(KWLoc, diag::err_default_delete_in_multiple_declaration)
93 }
else if (ExpectAndConsume(tok::semi, diag::err_expected_after,
94 Delete ?
"delete" :
"default")) {
102 trySkippingFunctionBody()) {
116 (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
117 TemplateInfo.Kind != ParsedTemplateInfo::ExplicitSpecialization)) &&
121 LexTemplateFunctionForLateParsing(Toks);
134 LexedMethod* LM =
new LexedMethod(
this, FnD);
135 getCurrentClass().LateParsedDeclarations.push_back(LM);
141 if (ConsumeAndStoreFunctionPrologue(Toks)) {
148 llvm::any_of(Toks, [](
const Token &Tok) {
149 return Tok.is(tok::code_completion);
162 delete getCurrentClass().LateParsedDeclarations.back();
163 getCurrentClass().LateParsedDeclarations.pop_back();
167 ConsumeAndStoreUntil(tok::r_brace, Toks,
false);
171 if (
kind == tok::kw_try) {
172 while (Tok.
is(tok::kw_catch)) {
173 ConsumeAndStoreUntil(tok::l_brace, Toks,
false);
174 ConsumeAndStoreUntil(tok::r_brace, Toks,
false);
187 delete getCurrentClass().LateParsedDeclarations.back();
188 getCurrentClass().LateParsedDeclarations.pop_back();
198 void Parser::ParseCXXNonStaticMemberInitializer(
Decl *VarD) {
199 assert(Tok.
isOneOf(tok::l_brace, tok::equal) &&
200 "Current token not a '{' or '='!");
202 LateParsedMemberInitializer *MI =
203 new LateParsedMemberInitializer(
this, VarD);
204 getCurrentClass().LateParsedDeclarations.push_back(MI);
208 if (
kind == tok::equal) {
213 if (
kind == tok::l_brace) {
219 ConsumeAndStoreUntil(tok::r_brace, Toks,
true);
222 ConsumeAndStoreInitializer(Toks, CIK_DefaultInitializer);
235 Parser::LateParsedDeclaration::~LateParsedDeclaration() {}
236 void Parser::LateParsedDeclaration::ParseLexedMethodDeclarations() {}
237 void Parser::LateParsedDeclaration::ParseLexedMemberInitializers() {}
238 void Parser::LateParsedDeclaration::ParseLexedMethodDefs() {}
239 void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
240 void Parser::LateParsedDeclaration::ParseLexedPragmas() {}
242 Parser::LateParsedClass::LateParsedClass(
Parser *
P, ParsingClass *C)
245 Parser::LateParsedClass::~LateParsedClass() {
246 Self->DeallocateParsedClasses(Class);
249 void Parser::LateParsedClass::ParseLexedMethodDeclarations() {
250 Self->ParseLexedMethodDeclarations(*Class);
253 void Parser::LateParsedClass::ParseLexedMemberInitializers() {
254 Self->ParseLexedMemberInitializers(*Class);
257 void Parser::LateParsedClass::ParseLexedMethodDefs() {
258 Self->ParseLexedMethodDefs(*Class);
261 void Parser::LateParsedClass::ParseLexedAttributes() {
262 Self->ParseLexedAttributes(*Class);
265 void Parser::LateParsedClass::ParseLexedPragmas() {
266 Self->ParseLexedPragmas(*Class);
269 void Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() {
270 Self->ParseLexedMethodDeclaration(*
this);
273 void Parser::LexedMethod::ParseLexedMethodDefs() {
274 Self->ParseLexedMethodDef(*
this);
277 void Parser::LateParsedMemberInitializer::ParseLexedMemberInitializers() {
278 Self->ParseLexedMemberInitializer(*
this);
281 void Parser::LateParsedAttribute::ParseLexedAttributes() {
282 Self->ParseLexedAttribute(*
this,
true,
false);
285 void Parser::LateParsedPragma::ParseLexedPragmas() {
286 Self->ParseLexedPragma(*
this);
311 !
Class.TopLevelClass),
314 if (
Class.TopLevelClass)
320 Class.TagOrTemplate);
323 if (
Class.TopLevelClass)
327 Class.TagOrTemplate);
335 void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) {
336 ReenterClassScopeRAII InClassScope(*
this, Class);
338 for (LateParsedDeclaration *LateD : Class.LateParsedDeclarations)
339 LateD->ParseLexedMethodDeclarations();
342 void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) {
344 ReenterTemplateScopeRAII InFunctionTemplateScope(*
this, LM.Method);
354 for (
unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
355 auto Param = cast<ParmVarDecl>(LM.DefaultArgs[I].Param);
357 bool HasUnparsed = Param->hasUnparsedDefaultArg();
359 std::unique_ptr<CachedTokens> Toks = std::move(LM.DefaultArgs[I].Toks);
365 Token LastDefaultArgToken = Toks->back();
371 Toks->push_back(DefArgEnd);
374 Toks->push_back(Tok);
375 PP.EnterTokenStream(*Toks,
true,
true);
381 assert(Tok.
is(tok::equal) &&
"Default argument not starting with '='");
392 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
393 DefArgResult = ParseBraceInitializer();
404 assert(Toks->size() >= 3 &&
"expected a token in default arg");
407 (*Toks)[Toks->size() - 3].getLocation());
420 }
else if (HasUnparsed) {
421 assert(Param->hasInheritedDefaultArg());
423 if (
const auto *FunTmpl = dyn_cast<FunctionTemplateDecl>(LM.Method))
432 Param->setUninstantiatedDefaultArg(
435 Param->setDefaultArg(OldParam->
getInit());
445 Token LastExceptionSpecToken = Toks->back();
446 Token ExceptionSpecEnd;
451 Toks->push_back(ExceptionSpecEnd);
454 Toks->push_back(Tok);
455 PP.EnterTokenStream(*Toks,
true,
true);
468 = dyn_cast<FunctionTemplateDecl>(LM.Method))
469 Method = dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl());
471 Method = dyn_cast<CXXMethodDecl>(LM.Method);
474 Actions, Method ? Method->
getParent() :
nullptr,
486 = tryParseExceptionSpecification(
false, SpecificationRange,
488 DynamicExceptionRanges, NoexceptExpr,
489 ExceptionSpecTokens);
498 DynamicExceptionRanges,
500 NoexceptExpr.
get() :
nullptr);
512 LM.ExceptionSpecTokens =
nullptr;
515 InFunctionTemplateScope.Scopes.Exit();
524 void Parser::ParseLexedMethodDefs(ParsingClass &Class) {
525 ReenterClassScopeRAII InClassScope(*
this, Class);
527 for (LateParsedDeclaration *D :
Class.LateParsedDeclarations)
528 D->ParseLexedMethodDefs();
531 void Parser::ParseLexedMethodDef(LexedMethod &LM) {
533 ReenterTemplateScopeRAII InFunctionTemplateScope(*
this, LM.D);
537 assert(!LM.Toks.empty() &&
"Empty body!");
538 Token LastBodyToken = LM.Toks.back();
544 LM.Toks.push_back(BodyEnd);
547 LM.Toks.push_back(Tok);
548 PP.EnterTokenStream(LM.Toks,
true,
true);
552 assert(Tok.
isOneOf(tok::l_brace, tok::colon, tok::kw_try)
553 &&
"Inline method not starting with '{', ':' or 'try'");
561 if (Tok.
is(tok::kw_try)) {
562 ParseFunctionTryBlock(LM.D, FnScope);
571 if (Tok.
is(tok::colon)) {
572 ParseConstructorInitializer(LM.D);
575 if (!Tok.
is(tok::l_brace)) {
590 !isa<FunctionTemplateDecl>(LM.D) ||
591 cast<FunctionTemplateDecl>(LM.D)->getTemplateParameters()->getDepth()
592 < TemplateParameterDepth) &&
593 "TemplateParameterDepth should be greater than the depth of "
594 "current template being instantiated!");
596 ParseFunctionStatementBody(LM.D, FnScope);
604 if (
auto *FD = dyn_cast_or_null<FunctionDecl>(LM.D))
605 if (isa<CXXMethodDecl>(FD) ||
613 void Parser::ParseLexedMemberInitializers(ParsingClass &Class) {
614 ReenterClassScopeRAII InClassScope(*
this, Class);
616 if (!
Class.LateParsedDeclarations.empty()) {
626 for (LateParsedDeclaration *D :
Class.LateParsedDeclarations)
627 D->ParseLexedMemberInitializers();
633 void Parser::ParseLexedMemberInitializer(LateParsedMemberInitializer &MI) {
634 if (!MI.Field || MI.Field->isInvalidDecl())
641 MI.Toks.push_back(Tok);
642 PP.EnterTokenStream(MI.Toks,
true,
true);
651 ExprResult Init = ParseCXXMemberInitializer(MI.Field,
false,
659 if (!Init.isInvalid()) {
664 Diag(EndLoc, diag::err_expected_semi_decl_list);
678 void Parser::ParseLexedAttributes(ParsingClass &Class) {
679 ReenterClassScopeRAII InClassScope(*
this, Class);
681 for (LateParsedDeclaration *LateD :
Class.LateParsedDeclarations)
682 LateD->ParseLexedAttributes();
686 void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs,
Decl *D,
687 bool EnterScope,
bool OnDefinition) {
688 assert(LAs.parseSoon() &&
689 "Attribute list should be marked for immediate parsing.");
690 for (
unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
693 ParseLexedAttribute(*LAs[i],
EnterScope, OnDefinition);
704 void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
705 bool EnterScope,
bool OnDefinition) {
713 LA.Toks.push_back(AttrEnd);
717 LA.Toks.push_back(Tok);
718 PP.EnterTokenStream(LA.Toks,
true,
true);
724 if (LA.Decls.size() > 0) {
725 Decl *D = LA.Decls[0];
733 if (LA.Decls.size() == 1) {
735 ReenterTemplateScopeRAII InDeclScope(*
this, D,
EnterScope);
745 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs,
nullptr,
754 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs,
nullptr,
759 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
762 if (OnDefinition && !Attrs.empty() && !Attrs.begin()->isCXX11Attribute() &&
763 Attrs.begin()->isKnownToGCC())
764 Diag(Tok, diag::warn_attribute_on_function_definition)
767 for (
unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i)
779 void Parser::ParseLexedPragmas(ParsingClass &Class) {
780 ReenterClassScopeRAII InClassScope(*
this, Class);
782 for (LateParsedDeclaration *D :
Class.LateParsedDeclarations)
783 D->ParseLexedPragmas();
786 void Parser::ParseLexedPragma(LateParsedPragma &LP) {
788 PP.EnterTokenStream(LP.toks(),
true,
793 assert(Tok.
isAnnotation() &&
"Expected annotation token.");
795 case tok::annot_attr_openmp:
796 case tok::annot_pragma_openmp: {
799 (void)ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs);
803 llvm_unreachable(
"Unexpected token.");
815 bool StopAtSemi,
bool ConsumeFinalToken) {
818 bool isFirstTokenConsumed =
true;
821 if (Tok.
is(T1) || Tok.
is(T2)) {
822 if (ConsumeFinalToken) {
831 case tok::annot_module_begin:
832 case tok::annot_module_end:
833 case tok::annot_module_include:
841 ConsumeAndStoreUntil(tok::r_paren, Toks,
false);
847 ConsumeAndStoreUntil(tok::r_square, Toks,
false);
853 ConsumeAndStoreUntil(tok::r_brace, Toks,
false);
862 if (ParenCount && !isFirstTokenConsumed)
868 if (BracketCount && !isFirstTokenConsumed)
874 if (BraceCount && !isFirstTokenConsumed)
890 isFirstTokenConsumed =
false;
900 bool Parser::ConsumeAndStoreFunctionPrologue(
CachedTokens &Toks) {
901 if (Tok.
is(tok::kw_try)) {
906 if (Tok.
isNot(tok::colon)) {
912 ConsumeAndStoreUntil(tok::l_brace, tok::r_brace, Toks,
915 if (Tok.
isNot(tok::l_brace))
936 bool MightBeTemplateArgument =
false;
940 if (Tok.
is(tok::kw_decltype)) {
943 if (Tok.
isNot(tok::l_paren))
948 if (!ConsumeAndStoreUntil(tok::r_paren, Toks,
true)) {
950 Diag(OpenLoc, diag::note_matching) << tok::l_paren;
956 if (Tok.
is(tok::coloncolon)) {
960 if (Tok.
is(tok::kw_template)) {
966 if (Tok.
is(tok::identifier)) {
972 }
while (Tok.
is(tok::coloncolon));
974 if (Tok.
is(tok::code_completion)) {
976 ConsumeCodeCompletionToken();
977 if (Tok.
isOneOf(tok::identifier, tok::coloncolon, tok::kw_decltype)) {
984 if (Tok.
is(tok::comma)) {
990 if (Tok.
is(tok::less))
991 MightBeTemplateArgument =
true;
993 if (MightBeTemplateArgument) {
1000 if (!ConsumeAndStoreUntil(tok::l_paren, tok::l_brace, Toks,
1007 }
else if (Tok.
isNot(tok::l_paren) && Tok.
isNot(tok::l_brace)) {
1011 << tok::l_paren << tok::l_brace;
1017 Toks.push_back(Tok);
1018 bool IsLParen = (
kind == tok::l_paren);
1024 assert(
kind == tok::l_brace &&
"Must be left paren or brace here.");
1031 const Token &PreviousToken = Toks[Toks.size() - 2];
1032 if (!MightBeTemplateArgument &&
1033 !PreviousToken.isOneOf(tok::identifier, tok::greater,
1034 tok::greatergreater)) {
1040 TentativeParsingAction PA(*
this);
1042 !Tok.
isOneOf(tok::comma, tok::ellipsis, tok::l_brace)) {
1055 tok::TokenKind CloseKind = IsLParen ? tok::r_paren : tok::r_brace;
1056 if (!ConsumeAndStoreUntil(CloseKind, Toks,
true)) {
1057 Diag(Tok, diag::err_expected) << CloseKind;
1058 Diag(OpenLoc, diag::note_matching) <<
kind;
1063 if (Tok.
is(tok::ellipsis)) {
1064 Toks.push_back(Tok);
1070 if (Tok.
is(tok::comma)) {
1071 Toks.push_back(Tok);
1073 }
else if (Tok.
is(tok::l_brace)) {
1088 Toks.push_back(Tok);
1091 }
else if (!MightBeTemplateArgument) {
1092 return Diag(Tok.
getLocation(), diag::err_expected_either) << tok::l_brace
1100 bool Parser::ConsumeAndStoreConditional(
CachedTokens &Toks) {
1102 assert(Tok.
is(tok::question));
1103 Toks.push_back(Tok);
1106 while (Tok.
isNot(tok::colon)) {
1107 if (!ConsumeAndStoreUntil(tok::question, tok::colon, Toks,
1113 if (Tok.
is(tok::question) && !ConsumeAndStoreConditional(Toks))
1118 Toks.push_back(Tok);
1128 : TentativeParsingAction(Self), Self(Self), EndKind(EndKind) {
1131 TentativeParsingAction Inner(Self);
1132 Self.ConsumeAndStoreUntil(EndKind, Toks,
true,
false);
1142 auto Buffer = std::make_unique<Token[]>(Toks.size());
1143 std::copy(Toks.begin() + 1, Toks.end(), Buffer.get());
1144 Buffer[Toks.size() - 1] = Self.Tok;
1145 Self.PP.EnterTokenStream(std::move(Buffer), Toks.size(),
true,
1148 Self.Tok = Toks.front();
1164 bool Parser::ConsumeAndStoreInitializer(
CachedTokens &Toks,
1165 CachedInitKind CIK) {
1167 bool IsFirstToken =
true;
1172 unsigned AngleCount = 0;
1173 unsigned KnownTemplateCount = 0;
1182 if (KnownTemplateCount)
1195 UnannotatedTentativeParsingAction PA(*
this,
1196 CIK == CIK_DefaultInitializer
1197 ? tok::semi : tok::r_paren);
1200 TPResult Result = TPResult::Error;
1203 case CIK_DefaultInitializer:
1204 Result = TryParseInitDeclaratorList();
1207 if (Result == TPResult::Ambiguous && Tok.
isNot(tok::semi))
1208 Result = TPResult::False;
1211 case CIK_DefaultArgument:
1212 bool InvalidAsDeclaration =
false;
1213 Result = TryParseParameterDeclarationClause(
1214 &InvalidAsDeclaration,
true);
1217 if (Result == TPResult::Ambiguous && InvalidAsDeclaration)
1218 Result = TPResult::False;
1225 PA.RevertAnnotations();
1228 if (Result != TPResult::False && Result != TPResult::Error)
1233 ++KnownTemplateCount;
1237 case tok::annot_module_begin:
1238 case tok::annot_module_end:
1239 case tok::annot_module_include:
1252 if (!ConsumeAndStoreConditional(Toks))
1256 case tok::greatergreatergreater:
1259 if (AngleCount) --AngleCount;
1260 if (KnownTemplateCount) --KnownTemplateCount;
1262 case tok::greatergreater:
1265 if (AngleCount) --AngleCount;
1266 if (KnownTemplateCount) --KnownTemplateCount;
1269 if (AngleCount) --AngleCount;
1270 if (KnownTemplateCount) --KnownTemplateCount;
1273 case tok::kw_template:
1277 Toks.push_back(Tok);
1279 if (Tok.
is(tok::identifier)) {
1280 Toks.push_back(Tok);
1282 if (Tok.
is(tok::less)) {
1284 ++KnownTemplateCount;
1285 Toks.push_back(Tok);
1291 case tok::kw_operator:
1294 Toks.push_back(Tok);
1298 case tok::greatergreatergreater:
1299 case tok::greatergreater:
1302 Toks.push_back(Tok);
1312 Toks.push_back(Tok);
1314 ConsumeAndStoreUntil(tok::r_paren, Toks,
false);
1318 Toks.push_back(Tok);
1320 ConsumeAndStoreUntil(tok::r_square, Toks,
false);
1324 Toks.push_back(Tok);
1326 ConsumeAndStoreUntil(tok::r_brace, Toks,
false);
1336 if (CIK == CIK_DefaultArgument)
1338 if (ParenCount && !IsFirstToken)
1340 Toks.push_back(Tok);
1344 if (BracketCount && !IsFirstToken)
1346 Toks.push_back(Tok);
1350 if (BraceCount && !IsFirstToken)
1352 Toks.push_back(Tok);
1356 case tok::code_completion:
1357 Toks.push_back(Tok);
1358 ConsumeCodeCompletionToken();
1361 case tok::string_literal:
1362 case tok::wide_string_literal:
1363 case tok::utf8_string_literal:
1364 case tok::utf16_string_literal:
1365 case tok::utf32_string_literal:
1366 Toks.push_back(Tok);
1367 ConsumeStringToken();
1370 if (CIK == CIK_DefaultInitializer)
1375 Toks.push_back(Tok);
1379 IsFirstToken =
false;