Skip to content

Commit 5b2520e

Browse files
committed
Remove IfConfigDecl from the AST
The swift-syntax tree retains information about the parsed #if regions. Drop it from the semantic AST.
1 parent d762dd5 commit 5b2520e

38 files changed

+16
-476
lines changed

include/swift/AST/Decl.h

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@ enum class DescriptiveDeclKind : uint8_t {
151151
Extension,
152152
EnumCase,
153153
TopLevelCode,
154-
IfConfig,
155154
PoundDiagnostic,
156155
PatternBinding,
157156
Var,
@@ -819,11 +818,6 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
819818
HasLazyConformances : 1
820819
);
821820

822-
SWIFT_INLINE_BITFIELD(IfConfigDecl, Decl, 1,
823-
/// Whether this decl is missing its closing '#endif'.
824-
HadMissingEnd : 1
825-
);
826-
827821
SWIFT_INLINE_BITFIELD(PoundDiagnosticDecl, Decl, 1+1,
828822
/// `true` if the diagnostic is an error, `false` if it's a warning.
829823
IsError : 1,
@@ -2658,51 +2652,6 @@ class SerializedTopLevelCodeDeclContext : public DeclContext {
26582652
}
26592653
};
26602654

2661-
/// IfConfigDecl - This class represents #if/#else/#endif blocks.
2662-
/// Active and inactive block members are stored separately, with the intention
2663-
/// being that active members will be handed back to the enclosing context.
2664-
class IfConfigDecl : public Decl {
2665-
/// An array of clauses controlling each of the #if/#elseif/#else conditions.
2666-
/// The array is ASTContext allocated.
2667-
ArrayRef<IfConfigClause> Clauses;
2668-
SourceLoc EndLoc;
2669-
SourceLoc getLocFromSource() const { return Clauses[0].Loc; }
2670-
friend class Decl;
2671-
public:
2672-
2673-
IfConfigDecl(DeclContext *Parent, ArrayRef<IfConfigClause> Clauses,
2674-
SourceLoc EndLoc, bool HadMissingEnd)
2675-
: Decl(DeclKind::IfConfig, Parent), Clauses(Clauses), EndLoc(EndLoc)
2676-
{
2677-
Bits.IfConfigDecl.HadMissingEnd = HadMissingEnd;
2678-
}
2679-
2680-
ArrayRef<IfConfigClause> getClauses() const { return Clauses; }
2681-
2682-
/// Return the active clause, or null if there is no active one.
2683-
const IfConfigClause *getActiveClause() const {
2684-
for (auto &Clause : Clauses)
2685-
if (Clause.isActive) return &Clause;
2686-
return nullptr;
2687-
}
2688-
2689-
const ArrayRef<ASTNode> getActiveClauseElements() const {
2690-
if (auto *Clause = getActiveClause())
2691-
return Clause->Elements;
2692-
return {};
2693-
}
2694-
2695-
SourceLoc getEndLoc() const { return EndLoc; }
2696-
2697-
bool hadMissingEnd() const { return Bits.IfConfigDecl.HadMissingEnd; }
2698-
2699-
SourceRange getSourceRange() const;
2700-
2701-
static bool classof(const Decl *D) {
2702-
return D->getKind() == DeclKind::IfConfig;
2703-
}
2704-
};
2705-
27062655
class StringLiteralExpr;
27072656

27082657
class PoundDiagnosticDecl : public Decl {

include/swift/AST/DeclExportabilityVisitor.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,6 @@ class DeclExportabilityVisitor
170170
#define UNINTERESTING(KIND) \
171171
bool visit##KIND##Decl(const KIND##Decl *D) { return true; }
172172
UNINTERESTING(TopLevelCode);
173-
UNINTERESTING(IfConfig);
174173
UNINTERESTING(Import);
175174
UNINTERESTING(PoundDiagnostic);
176175
UNINTERESTING(PrecedenceGroup);

include/swift/AST/DeclNodes.def

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,6 @@ ABSTRACT_DECL(Value, Decl)
185185
ITERABLE_GENERIC_DECL(Extension, Decl)
186186
CONTEXT_DECL(TopLevelCode, Decl)
187187
DECL(Import, Decl)
188-
DECL(IfConfig, Decl)
189188
DECL(PoundDiagnostic, Decl)
190189
DECL(PrecedenceGroup, Decl)
191190
DECL(Missing, Decl)

include/swift/AST/TypeMemberVisitor.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,6 @@ class TypeMemberVisitor : public DeclVisitor<ImplClass, RetTy> {
4242
BAD_MEMBER(PrecedenceGroup)
4343
BAD_MEMBER(Macro)
4444

45-
// The children of these are automatically inserted into the
46-
// surrounding context.
47-
RetTy visitIfConfigDecl(IfConfigDecl *D) {
48-
return RetTy();
49-
}
50-
5145
// These decls are disregarded.
5246
RetTy visitPoundDiagnosticDecl(PoundDiagnosticDecl *D) {
5347
return RetTy();

include/swift/Parse/Parser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1023,7 +1023,7 @@ class Parser {
10231023
llvm::function_ref<void(bool)> parseElements);
10241024

10251025
/// Parse an #if ... #endif containing only attributes.
1026-
ParserStatus parseIfConfigDeclAttributes(
1026+
ParserStatus parseIfConfigAttributes(
10271027
DeclAttributes &attributes, bool ifConfigsAreDeclAttrs,
10281028
PatternBindingInitializer *initContext);
10291029

include/swift/SIL/SILWitnessVisitor.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,6 @@ template <class T> class SILWitnessVisitor : public ASTVisitor<T> {
172172
// We only care about the contained VarDecls.
173173
}
174174

175-
void visitIfConfigDecl(IfConfigDecl *icd) {
176-
// We only care about the active members, which were already subsumed by the
177-
// enclosing type.
178-
}
179-
180175
void visitPoundDiagnosticDecl(PoundDiagnosticDecl *pdd) {
181176
// We don't care about diagnostics at this stage.
182177
}

lib/APIDigester/ModuleAnalyzerNodes.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1943,8 +1943,6 @@ SwiftDeclCollector::addMembersToRoot(SDKNode *Root, IterableDeclContext *Context
19431943
// All containing variables should have been handled.
19441944
} else if (isa<EnumCaseDecl>(Member)) {
19451945
// All containing variables should have been handled.
1946-
} else if (isa<IfConfigDecl>(Member)) {
1947-
// All containing members should have been handled.
19481946
} else if (isa<PoundDiagnosticDecl>(Member)) {
19491947
// All containing members should have been handled.
19501948
} else if (isa<DestructorDecl>(Member)) {

lib/AST/ASTDumper.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1634,12 +1634,6 @@ namespace {
16341634
printFoot();
16351635
}
16361636

1637-
void visitIfConfigDecl(IfConfigDecl *ICD, StringRef label) {
1638-
printCommon(ICD, "if_config_decl", label);
1639-
printRecRange(ICD->getClauses(), &ICD->getASTContext(), "clauses");
1640-
printFoot();
1641-
}
1642-
16431637
void visitPoundDiagnosticDecl(PoundDiagnosticDecl *PDD, StringRef label) {
16441638
printCommon(PDD, "pound_diagnostic_decl", label);
16451639
printField(PDD->isError() ? "error" : "warning", "kind");

lib/AST/ASTMangler.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4871,7 +4871,6 @@ ASTMangler::BaseEntitySignature::BaseEntitySignature(const Decl *decl)
48714871
case DeclKind::Extension:
48724872
case DeclKind::TopLevelCode:
48734873
case DeclKind::Import:
4874-
case DeclKind::IfConfig:
48754874
case DeclKind::PoundDiagnostic:
48764875
case DeclKind::PrecedenceGroup:
48774876
case DeclKind::Missing:

lib/AST/ASTPrinter.cpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2284,10 +2284,6 @@ bool ShouldPrintChecker::shouldPrint(const Decl *D,
22842284
return false;
22852285
}
22862286

2287-
if (isa<IfConfigDecl>(D)) {
2288-
return false;
2289-
}
2290-
22912287
return true;
22922288
}
22932289

@@ -3373,10 +3369,6 @@ void PrintAST::visitTopLevelCodeDecl(TopLevelCodeDecl *decl) {
33733369
printASTNodes(decl->getBody()->getElements(), /*NeedIndent=*/false);
33743370
}
33753371

3376-
void PrintAST::visitIfConfigDecl(IfConfigDecl *ICD) {
3377-
// Never printed
3378-
}
3379-
33803372
void PrintAST::visitPoundDiagnosticDecl(PoundDiagnosticDecl *PDD) {
33813373
/// TODO: Should we even print #error/#warning?
33823374
if (PDD->isError()) {
@@ -5533,8 +5525,6 @@ void PrintAST::visitSwitchStmt(SwitchStmt *stmt) {
55335525
for (auto N : stmt->getRawCases()) {
55345526
if (N.is<Stmt*>())
55355527
visit(cast<CaseStmt>(N.get<Stmt*>()));
5536-
else
5537-
visit(cast<IfConfigDecl>(N.get<Decl*>()));
55385528
Printer.printNewline();
55395529
}
55405530
indent();
@@ -5633,10 +5623,6 @@ bool Decl::shouldPrintInContext(const PrintOptions &PO) const {
56335623
}
56345624
}
56355625

5636-
if (isa<IfConfigDecl>(this)) {
5637-
return false;
5638-
}
5639-
56405626
// Print everything else.
56415627
return true;
56425628
}

lib/AST/ASTScopeCreation.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -368,10 +368,6 @@ class NodeAdder
368368
VISIT_AND_IGNORE(MissingDecl)
369369
VISIT_AND_IGNORE(MissingMemberDecl)
370370

371-
// Only members of the active clause are in scope, and those
372-
// are visited separately.
373-
VISIT_AND_IGNORE(IfConfigDecl)
374-
375371
// This declaration is handled from the PatternBindingDecl
376372
VISIT_AND_IGNORE(VarDecl)
377373

lib/AST/ASTVerifier.cpp

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -3693,68 +3693,6 @@ class Verifier : public ASTWalker {
36933693
[&]{ S->dump(Out); });
36943694
}
36953695

3696-
void checkSourceRanges(IfConfigDecl *ICD) {
3697-
checkSourceRangesBase(ICD);
3698-
3699-
SourceLoc Location = ICD->getStartLoc();
3700-
for (auto &Clause : ICD->getClauses()) {
3701-
// Clause start, note that the first clause start location is the
3702-
// same as that of the whole statement
3703-
if (Location == ICD->getStartLoc()) {
3704-
if (Location != Clause.Loc) {
3705-
Out << "bad start location of IfConfigDecl first clause\n";
3706-
ICD->print(Out);
3707-
abort();
3708-
}
3709-
} else {
3710-
if (!Ctx.SourceMgr.isBeforeInBuffer(Location, Clause.Loc)) {
3711-
Out << "bad start location of IfConfigDecl clause\n";
3712-
ICD->print(Out);
3713-
abort();
3714-
}
3715-
}
3716-
Location = Clause.Loc;
3717-
3718-
// Condition if present
3719-
Expr *Cond = Clause.Cond;
3720-
if (Cond) {
3721-
if (!Ctx.SourceMgr.isBeforeInBuffer(Location, Cond->getStartLoc())) {
3722-
Out << "invalid IfConfigDecl clause condition start location\n";
3723-
ICD->print(Out);
3724-
abort();
3725-
}
3726-
Location = Cond->getEndLoc();
3727-
}
3728-
3729-
// Body elements
3730-
auto StoredLoc = Location;
3731-
for (auto &Element : Clause.Elements) {
3732-
auto StartLocation = Element.getStartLoc();
3733-
if (StartLocation.isInvalid()) {
3734-
continue;
3735-
}
3736-
3737-
if (!Ctx.SourceMgr.isBeforeInBuffer(StoredLoc, StartLocation)) {
3738-
Out << "invalid IfConfigDecl clause element start location\n";
3739-
ICD->print(Out);
3740-
abort();
3741-
}
3742-
3743-
auto EndLocation = Element.getEndLoc();
3744-
if (EndLocation.isValid() &&
3745-
Ctx.SourceMgr.isBeforeInBuffer(Location, EndLocation)) {
3746-
Location = EndLocation;
3747-
}
3748-
}
3749-
}
3750-
3751-
if (Ctx.SourceMgr.isBeforeInBuffer(ICD->getEndLoc(), Location)) {
3752-
Out << "invalid IfConfigDecl end location\n";
3753-
ICD->print(Out);
3754-
abort();
3755-
}
3756-
}
3757-
37583696
void checkSourceRanges(Pattern *P) {
37593697
PrettyStackTracePattern debugStack(Ctx, "verifying ranges", P);
37603698

lib/AST/ASTWalker.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,6 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
240240
return true;
241241
}
242242

243-
bool visitIfConfigDecl(IfConfigDecl *ICD) {
244-
// By default, just visit the elements that are actually
245-
// injected into the enclosing context.
246-
return false;
247-
}
248-
249243
bool visitPoundDiagnosticDecl(PoundDiagnosticDecl *PDD) {
250244
// By default, ignore #error/#warning.
251245
return false;
@@ -2081,8 +2075,7 @@ Stmt *Traversal::visitSwitchStmt(SwitchStmt *S) {
20812075
} else
20822076
return nullptr;
20832077
} else {
2084-
assert(isa<IfConfigDecl>(N.get<Decl*>()) ||
2085-
isa<PoundDiagnosticDecl>(N.get<Decl*>()));
2078+
assert(isa<PoundDiagnosticDecl>(N.get<Decl*>()));
20862079
if (doIt(N.get<Decl*>()))
20872080
return nullptr;
20882081
}

lib/AST/Decl.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@ DescriptiveDeclKind Decl::getDescriptiveKind() const {
157157
TRIVIAL_KIND(Extension);
158158
TRIVIAL_KIND(EnumCase);
159159
TRIVIAL_KIND(TopLevelCode);
160-
TRIVIAL_KIND(IfConfig);
161160
TRIVIAL_KIND(PoundDiagnostic);
162161
TRIVIAL_KIND(PatternBinding);
163162
TRIVIAL_KIND(PrecedenceGroup);
@@ -323,7 +322,6 @@ StringRef Decl::getDescriptiveKindName(DescriptiveDeclKind K) {
323322
ENTRY(Extension, "extension");
324323
ENTRY(EnumCase, "case");
325324
ENTRY(TopLevelCode, "top-level code");
326-
ENTRY(IfConfig, "conditional block");
327325
ENTRY(PoundDiagnostic, "diagnostic");
328326
ENTRY(PatternBinding, "pattern binding");
329327
ENTRY(Var, "var");
@@ -1444,7 +1442,6 @@ ImportKind ImportDecl::getBestImportKind(const ValueDecl *VD) {
14441442
case DeclKind::PrefixOperator:
14451443
case DeclKind::PostfixOperator:
14461444
case DeclKind::EnumCase:
1447-
case DeclKind::IfConfig:
14481445
case DeclKind::PoundDiagnostic:
14491446
case DeclKind::PrecedenceGroup:
14501447
case DeclKind::Missing:
@@ -2532,10 +2529,6 @@ SourceRange TopLevelCodeDecl::getSourceRange() const {
25322529
return Body? Body->getSourceRange() : SourceRange();
25332530
}
25342531

2535-
SourceRange IfConfigDecl::getSourceRange() const {
2536-
return SourceRange(getLoc(), EndLoc);
2537-
}
2538-
25392532
static bool isPolymorphic(const AbstractStorageDecl *storage) {
25402533
if (storage->shouldUseObjCDispatch())
25412534
return true;
@@ -3178,7 +3171,6 @@ bool ValueDecl::isInstanceMember() const {
31783171
case DeclKind::InfixOperator:
31793172
case DeclKind::PrefixOperator:
31803173
case DeclKind::PostfixOperator:
3181-
case DeclKind::IfConfig:
31823174
case DeclKind::PoundDiagnostic:
31833175
case DeclKind::PrecedenceGroup:
31843176
case DeclKind::Missing:
@@ -4102,7 +4094,6 @@ SourceLoc Decl::getAttributeInsertionLoc(bool forModifier) const {
41024094
switch (getKind()) {
41034095
case DeclKind::Module:
41044096
case DeclKind::TopLevelCode:
4105-
case DeclKind::IfConfig:
41064097
case DeclKind::PoundDiagnostic:
41074098
case DeclKind::Missing:
41084099
case DeclKind::MissingMember:

lib/AST/DeclContext.cpp

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -970,13 +970,6 @@ void IterableDeclContext::addMemberPreservingSourceOrder(Decl *member) {
970970
if (isa<EnumCaseDecl>(existingMember))
971971
continue;
972972

973-
// The elements of the active clause of an IfConfigDecl
974-
// are added to the parent type. We ignore the IfConfigDecl
975-
// since its source range overlaps with the source ranges
976-
// of the active elements.
977-
if (isa<IfConfigDecl>(existingMember))
978-
continue;
979-
980973
if (!SM.isBeforeInBuffer(existingMember->getEndLoc(), start))
981974
break;
982975

@@ -1025,14 +1018,11 @@ void IterableDeclContext::addMemberSilently(Decl *member, Decl *hint,
10251018
return;
10261019

10271020
auto shouldSkip = [](Decl *d) {
1028-
// PatternBindingDecl source ranges overlap with VarDecls,
1029-
// EnumCaseDecl source ranges overlap with EnumElementDecls,
1030-
// and IfConfigDecl source ranges overlap with the elements
1031-
// of the active clause. Skip them all here to avoid
1032-
// spurious assertions.
1021+
// PatternBindingDecl source ranges overlap with VarDecls and
1022+
// EnumCaseDecl source ranges overlap with EnumElementDecls.
1023+
// Skip them all here to avoid spurious assertions.
10331024
if (isa<PatternBindingDecl>(d) ||
1034-
isa<EnumCaseDecl>(d) ||
1035-
isa<IfConfigDecl>(d))
1025+
isa<EnumCaseDecl>(d))
10361026
return true;
10371027

10381028
// Ignore source location information of implicit declarations.

lib/AST/FrontendSourceFileDepGraphFactory.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,6 @@ StringRef DependencyKey::Builder::getTopLevelName(const Decl *decl) {
217217
case DeclKind::PatternBinding:
218218
case DeclKind::EnumCase:
219219
case DeclKind::TopLevelCode:
220-
case DeclKind::IfConfig:
221220
case DeclKind::PoundDiagnostic:
222221
case DeclKind::Missing:
223222
case DeclKind::MissingMember:

0 commit comments

Comments
 (0)