Skip to content

Commit d1fba2b

Browse files
committed
Move ParseScope and friends into Sema
1 parent 8a3ae34 commit d1fba2b

20 files changed

+240
-231
lines changed

clang/include/clang/Parse/Parser.h

Lines changed: 2 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,6 @@ class Parser : public CodeCompletionHandler {
210210
const Token &getCurToken() const { return Tok; }
211211
Scope *getCurScope() const { return Actions.getCurScope(); }
212212

213-
void incrementMSManglingNumber() const {
214-
return Actions.incrementMSManglingNumber();
215-
}
216-
217213
// Type forwarding. All of these are statically 'void*', but they may all be
218214
// different actual classes based on the actions in place.
219215
typedef OpaquePtr<DeclGroupRef> DeclGroupPtrTy;
@@ -384,78 +380,6 @@ class Parser : public CodeCompletionHandler {
384380
return MightBeCXXScopeToken() && TryAnnotateCXXScopeToken(EnteringContext);
385381
}
386382

387-
//===--------------------------------------------------------------------===//
388-
// Scope manipulation
389-
390-
/// ParseScope - Introduces a new scope for parsing. The kind of
391-
/// scope is determined by ScopeFlags. Objects of this type should
392-
/// be created on the stack to coincide with the position where the
393-
/// parser enters the new scope, and this object's constructor will
394-
/// create that new scope. Similarly, once the object is destroyed
395-
/// the parser will exit the scope.
396-
class ParseScope {
397-
Parser *Self;
398-
ParseScope(const ParseScope &) = delete;
399-
void operator=(const ParseScope &) = delete;
400-
401-
public:
402-
// ParseScope - Construct a new object to manage a scope in the
403-
// parser Self where the new Scope is created with the flags
404-
// ScopeFlags, but only when we aren't about to enter a compound statement.
405-
ParseScope(Parser *Self, unsigned ScopeFlags, bool EnteredScope = true,
406-
bool BeforeCompoundStmt = false)
407-
: Self(Self) {
408-
if (EnteredScope && !BeforeCompoundStmt)
409-
Self->EnterScope(ScopeFlags);
410-
else {
411-
if (BeforeCompoundStmt)
412-
Self->incrementMSManglingNumber();
413-
414-
this->Self = nullptr;
415-
}
416-
}
417-
418-
// Exit - Exit the scope associated with this object now, rather
419-
// than waiting until the object is destroyed.
420-
void Exit() {
421-
if (Self) {
422-
Self->ExitScope();
423-
Self = nullptr;
424-
}
425-
}
426-
427-
~ParseScope() { Exit(); }
428-
};
429-
430-
/// Introduces zero or more scopes for parsing. The scopes will all be exited
431-
/// when the object is destroyed.
432-
class MultiParseScope {
433-
Parser &Self;
434-
unsigned NumScopes = 0;
435-
436-
MultiParseScope(const MultiParseScope &) = delete;
437-
438-
public:
439-
MultiParseScope(Parser &Self) : Self(Self) {}
440-
void Enter(unsigned ScopeFlags) {
441-
Self.EnterScope(ScopeFlags);
442-
++NumScopes;
443-
}
444-
void Exit() {
445-
while (NumScopes) {
446-
Self.ExitScope();
447-
--NumScopes;
448-
}
449-
}
450-
~MultiParseScope() { Exit(); }
451-
};
452-
453-
/// EnterScope - Start a new scope.
454-
void EnterScope(unsigned ScopeFlags);
455-
456-
/// ExitScope - Pop a scope off the scope stack.
457-
void ExitScope();
458-
459383
//===--------------------------------------------------------------------===//
460384
// Diagnostic Emission and Error recovery.
461385

@@ -546,11 +470,6 @@ class Parser : public CodeCompletionHandler {
546470

547471
StackExhaustionHandler StackHandler;
548472

549-
/// ScopeCache - Cache scopes to reduce malloc traffic.
550-
static constexpr int ScopeCacheSize = 16;
551-
unsigned NumCachedScopes;
552-
Scope *ScopeCache[ScopeCacheSize];
553-
554473
/// Identifiers used for SEH handling in Borland. These are only
555474
/// allowed in particular circumstances
556475
// __except block
@@ -2532,7 +2451,7 @@ class Parser : public CodeCompletionHandler {
25322451
assert(SS.isSet() && "C++ scope was not set!");
25332452

25342453
CreatedScope = true;
2535-
P.EnterScope(0); // Not a decl scope.
2454+
P.Actions.EnterScope(0); // Not a decl scope.
25362455

25372456
if (!P.Actions.ActOnCXXEnterDeclaratorScope(P.getCurScope(), SS))
25382457
EnteredScope = true;
@@ -2544,7 +2463,7 @@ class Parser : public CodeCompletionHandler {
25442463
P.Actions.ActOnCXXExitDeclaratorScope(P.getCurScope(), SS);
25452464
}
25462465
if (CreatedScope)
2547-
P.ExitScope();
2466+
P.Actions.ExitScope();
25482467
}
25492468
};
25502469

clang/include/clang/Sema/Scope.h

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ class raw_ostream;
2929
} // namespace llvm
3030

3131
namespace clang {
32-
3332
class Decl;
3433
class DeclContext;
3534
class UsingDirectiveDecl;
3635
class VarDecl;
36+
class Sema;
3737

3838
/// Scope - A scope is a transient data structure that is used while parsing the
3939
/// program. It assists with resolving identifiers to the appropriate
@@ -667,6 +667,45 @@ class Scope {
667667
void dump() const;
668668
};
669669

670+
/// ParseScope - Introduces a new scope for parsing. The kind of
671+
/// scope is determined by ScopeFlags. Objects of this type should
672+
/// be created on the stack to coincide with the position where the
673+
/// parser enters the new scope, and this object's constructor will
674+
/// create that new scope. Similarly, once the object is destroyed
675+
/// the parser will exit the scope.
676+
class ParseScope {
677+
Sema *S;
678+
ParseScope(const ParseScope &) = delete;
679+
void operator=(const ParseScope &) = delete;
680+
681+
public:
682+
// ParseScope - Construct a new object to manage a scope in the
683+
// parser Self where the new Scope is created with the flags
684+
// ScopeFlags, but only when we aren't about to enter a compound statement.
685+
ParseScope(Sema &S, unsigned ScopeFlags, bool EnteredScope = true,
686+
bool BeforeCompoundStmt = false);
687+
688+
// Exit - Exit the scope associated with this object now, rather
689+
// than waiting until the object is destroyed.
690+
void Exit();
691+
692+
~ParseScope() { Exit(); }
693+
};
694+
695+
/// Introduces zero or more scopes for parsing. The scopes will all be exited
696+
/// when the object is destroyed.
697+
class MultiParseScope {
698+
Sema &S;
699+
unsigned NumScopes = 0;
700+
701+
MultiParseScope(const MultiParseScope &) = delete;
702+
703+
public:
704+
MultiParseScope(Sema &S) : S(S) {}
705+
void Enter(unsigned ScopeFlags);
706+
void Exit();
707+
~MultiParseScope() { Exit(); }
708+
};
670709
} // namespace clang
671710

672711
#endif // LLVM_CLANG_SEMA_SCOPE_H

clang/include/clang/Sema/Sema.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,6 +1200,15 @@ class Sema final : public SemaBase {
12001200
/// Scope actions.
12011201
void ActOnTranslationUnitScope(Scope *S);
12021202

1203+
/// EnterScope - Start a new scope.
1204+
void EnterScope(unsigned ScopeFlags);
1205+
1206+
/// ExitScope - Pop a scope off the scope stack.
1207+
void ExitScope();
1208+
1209+
/// Delete the scope stack and all cached scopes.
1210+
void FreeScopes();
1211+
12031212
/// Determine whether \param D is function like (function or function
12041213
/// template) for parsing.
12051214
bool isDeclaratorFunctionLike(Declarator &D);
@@ -1575,10 +1584,12 @@ class Sema final : public SemaBase {
15751584
sema::SemaPPCallbacks *SemaPPCallbackHandler;
15761585

15771586
/// The parser's current scope.
1578-
///
1579-
/// The parser maintains this state here.
15801587
Scope *CurScope;
15811588

1589+
/// ScopeCache - Cache scopes to reduce malloc traffic.
1590+
static constexpr unsigned MaxScopeCacheSize = 16;
1591+
SmallVector<std::unique_ptr<Scope>, MaxScopeCacheSize> ScopeCache;
1592+
15821593
mutable IdentifierInfo *Ident_super;
15831594

15841595
std::unique_ptr<SemaAMDGPU> AMDGPUPtr;
@@ -4218,7 +4229,7 @@ class Sema final : public SemaBase {
42184229
TopLevelStmtDecl *ActOnStartTopLevelStmtDecl(Scope *S);
42194230
void ActOnFinishTopLevelStmtDecl(TopLevelStmtDecl *D, Stmt *Statement);
42204231

4221-
void ActOnPopScope(SourceLocation Loc, Scope *S);
4232+
void ActOnPopScope(Scope *S);
42224233

42234234
/// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with
42244235
/// no declarator (e.g. "struct foo;") is parsed.

clang/lib/Interpreter/IncrementalParser.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ IncrementalParser::ParseOrWrapTopLevelDecl() {
6363
P->ConsumeAnyToken();
6464
// FIXME: Clang does not call ExitScope on finalizing the regular TU, we
6565
// might want to do that around HandleEndOfTranslationUnit.
66-
P->ExitScope();
66+
S.ExitScope();
6767
S.CurContext = nullptr;
6868
// Start a new PTU.
69-
P->EnterScope(Scope::DeclScope);
69+
S.EnterScope(Scope::DeclScope);
7070
S.ActOnTranslationUnitScope(P->getCurScope());
7171
}
7272

clang/lib/Parse/ParseCXXInlineMethods.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,8 @@ struct Parser::ReenterTemplateScopeRAII {
328328
TemplateParameterDepthRAII CurTemplateDepthTracker;
329329

330330
ReenterTemplateScopeRAII(Parser &P, Decl *MaybeTemplated, bool Enter = true)
331-
: P(P), Scopes(P), CurTemplateDepthTracker(P.TemplateParameterDepth) {
331+
: P(P), Scopes(P.Actions),
332+
CurTemplateDepthTracker(P.TemplateParameterDepth) {
332333
if (Enter) {
333334
CurTemplateDepthTracker.addDepth(
334335
P.ReenterTemplateScopes(Scopes, MaybeTemplated));
@@ -510,7 +511,7 @@ void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) {
510511
// };
511512
// Setup the CurScope to match the function DeclContext - we have such
512513
// assumption in IsInFnTryBlockHandler().
513-
ParseScope FnScope(this, Scope::FnScope);
514+
ParseScope FnScope(Actions, Scope::FnScope);
514515
Sema::ContextRAII FnContext(Actions, FunctionToPush,
515516
/*NewThisContext=*/false);
516517
Sema::FunctionScopeRAII PopFnContext(Actions);
@@ -597,8 +598,8 @@ void Parser::ParseLexedMethodDef(LexedMethod &LM) {
597598

598599
// Parse the method body. Function body parsing code is similar enough
599600
// to be re-used for method bodies as well.
600-
ParseScope FnScope(this, Scope::FnScope | Scope::DeclScope |
601-
Scope::CompoundStmtScope);
601+
ParseScope FnScope(Actions, Scope::FnScope | Scope::DeclScope |
602+
Scope::CompoundStmtScope);
602603
Sema::FPFeaturesStateRAII SaveFPFeatures(Actions);
603604

604605
Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D);

clang/lib/Parse/ParseDecl.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -677,9 +677,9 @@ void Parser::ParseGNUAttributeArgs(
677677
if (normalizeAttrName(AttrName->getName()) == "enable_if" &&
678678
D && D->isFunctionDeclarator()) {
679679
const DeclaratorChunk::FunctionTypeInfo& FTI = D->getFunctionTypeInfo();
680-
PrototypeScope.emplace(this, Scope::FunctionPrototypeScope |
681-
Scope::FunctionDeclarationScope |
682-
Scope::DeclScope);
680+
PrototypeScope.emplace(Actions, Scope::FunctionPrototypeScope |
681+
Scope::FunctionDeclarationScope |
682+
Scope::DeclScope);
683683
for (unsigned i = 0; i != FTI.NumParams; ++i) {
684684
ParmVarDecl *Param = cast<ParmVarDecl>(FTI.Params[i].Param);
685685
Actions.ActOnReenterCXXMethodParameter(getCurScope(), Param);
@@ -2454,7 +2454,7 @@ Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(
24542454
if (ThisDecl && P.getLangOpts().CPlusPlus) {
24552455
Scope *S = nullptr;
24562456
if (D.getCXXScopeSpec().isSet()) {
2457-
P.EnterScope(0);
2457+
P.Actions.EnterScope(0);
24582458
S = P.getCurScope();
24592459
}
24602460
if (ThisDecl && !ThisDecl->isInvalidDecl()) {
@@ -2472,7 +2472,7 @@ Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(
24722472
if (Entered)
24732473
P.Actions.ActOnCXXExitDeclInitializer(S, ThisDecl);
24742474
if (S)
2475-
P.ExitScope();
2475+
P.Actions.ExitScope();
24762476
}
24772477
ThisDecl = nullptr;
24782478
}
@@ -4826,7 +4826,7 @@ void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
48264826
if (T.consumeOpen())
48274827
return;
48284828

4829-
ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
4829+
ParseScope StructScope(Actions, Scope::ClassScope|Scope::DeclScope);
48304830
Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
48314831

48324832
// `LateAttrParseExperimentalExtOnly=true` requests that only attributes
@@ -5338,7 +5338,7 @@ void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
53385338
void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl,
53395339
SkipBodyInfo *SkipBody) {
53405340
// Enter the scope of the enum body and start the definition.
5341-
ParseScope EnumScope(this, Scope::DeclScope | Scope::EnumScope);
5341+
ParseScope EnumScope(Actions, Scope::DeclScope | Scope::EnumScope);
53425342
Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
53435343

53445344
BalancedDelimiterTracker T(*this, tok::l_brace);
@@ -5681,8 +5681,8 @@ Parser::DeclGroupPtrTy Parser::ParseTopLevelStmtDecl() {
56815681
// Parse a top-level-stmt.
56825682
Parser::StmtVector Stmts;
56835683
ParsedStmtContext SubStmtCtx = ParsedStmtContext();
5684-
ParseScope FnScope(this, Scope::FnScope | Scope::DeclScope |
5685-
Scope::CompoundStmtScope);
5684+
ParseScope FnScope(Actions, Scope::FnScope | Scope::DeclScope |
5685+
Scope::CompoundStmtScope);
56865686
TopLevelStmtDecl *TLSD = Actions.ActOnStartTopLevelStmtDecl(getCurScope());
56875687
StmtResult R = ParseStatementOrDeclaration(Stmts, SubStmtCtx);
56885688
Actions.ActOnFinishTopLevelStmtDecl(TLSD, R.get());
@@ -6804,9 +6804,9 @@ void Parser::ParseDirectDeclarator(Declarator &D) {
68046804
// Enter function-declaration scope, limiting any declarators to the
68056805
// function prototype scope, including parameter declarators.
68066806
ParseScope PrototypeScope(
6807-
this, Scope::FunctionPrototypeScope | Scope::DeclScope |
6808-
(IsFunctionDeclaration ? Scope::FunctionDeclarationScope
6809-
: Scope::NoScope));
6807+
Actions, Scope::FunctionPrototypeScope | Scope::DeclScope |
6808+
(IsFunctionDeclaration ? Scope::FunctionDeclarationScope
6809+
: Scope::NoScope));
68106810

68116811
// The paren may be part of a C++ direct initializer, eg. "int x(1);".
68126812
// In such a case, check if we actually have a function declarator; if it
@@ -7085,7 +7085,7 @@ void Parser::ParseParenDeclarator(Declarator &D) {
70857085

70867086
// Enter function-declaration scope, limiting any declarators to the
70877087
// function prototype scope, including parameter declarators.
7088-
ParseScope PrototypeScope(this,
7088+
ParseScope PrototypeScope(Actions,
70897089
Scope::FunctionPrototypeScope | Scope::DeclScope |
70907090
(D.isFunctionDeclaratorAFunctionDeclaration()
70917091
? Scope::FunctionDeclarationScope
@@ -8126,7 +8126,7 @@ TypeResult Parser::ParseTypeFromString(StringRef TypeStr, StringRef Context,
81268126
ConsumeAnyToken();
81278127

81288128
// Enter a new scope.
8129-
ParseScope LocalScope(this, 0);
8129+
ParseScope LocalScope(Actions, 0);
81308130

81318131
// Parse the type.
81328132
TypeResult Result = ParseTypeName(nullptr);

0 commit comments

Comments
 (0)