Skip to content

Commit 732b215

Browse files
authored
Merge pull request #9413 from rintaro/ast-eliminate-ifconfigstmt
[AST] Eliminate IfConfigStmt
2 parents bb05532 + c8d717e commit 732b215

28 files changed

+256
-369
lines changed

include/swift/AST/Decl.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ namespace swift {
4444
class GenericEnvironment;
4545
class ArchetypeType;
4646
class ASTContext;
47+
struct ASTNode;
4748
class ASTPrinter;
4849
class ASTWalker;
4950
class ConstructorDecl;
@@ -2027,34 +2028,33 @@ class SerializedTopLevelCodeDeclContext : public SerializedLocalDeclContext {
20272028
}
20282029
};
20292030

2030-
/// IfConfigDecl - This class represents the declaration-side representation of
2031-
/// #if/#else/#endif blocks. Active and inactive block members are stored
2032-
/// separately, with the intention being that active members will be handed
2033-
/// back to the enclosing declaration.
2031+
/// IfConfigDecl - This class represents #if/#else/#endif blocks.
2032+
/// Active and inactive block members are stored separately, with the intention
2033+
/// being that active members will be handed back to the enclosing context.
20342034
class IfConfigDecl : public Decl {
20352035
/// An array of clauses controlling each of the #if/#elseif/#else conditions.
20362036
/// The array is ASTContext allocated.
2037-
ArrayRef<IfConfigClause<Decl *>> Clauses;
2037+
ArrayRef<IfConfigClause> Clauses;
20382038
SourceLoc EndLoc;
20392039
public:
20402040

2041-
IfConfigDecl(DeclContext *Parent, ArrayRef<IfConfigClause<Decl *>> Clauses,
2041+
IfConfigDecl(DeclContext *Parent, ArrayRef<IfConfigClause> Clauses,
20422042
SourceLoc EndLoc, bool HadMissingEnd)
20432043
: Decl(DeclKind::IfConfig, Parent), Clauses(Clauses), EndLoc(EndLoc)
20442044
{
20452045
IfConfigDeclBits.HadMissingEnd = HadMissingEnd;
20462046
}
20472047

2048-
ArrayRef<IfConfigClause<Decl *>> getClauses() const { return Clauses; }
2048+
ArrayRef<IfConfigClause> getClauses() const { return Clauses; }
20492049

20502050
/// Return the active clause, or null if there is no active one.
2051-
const IfConfigClause<Decl *> *getActiveClause() const {
2051+
const IfConfigClause *getActiveClause() const {
20522052
for (auto &Clause : Clauses)
20532053
if (Clause.isActive) return &Clause;
20542054
return nullptr;
20552055
}
20562056

2057-
const ArrayRef<Decl*> getActiveMembers() const {
2057+
const ArrayRef<ASTNode> getActiveClauseElements() const {
20582058
if (auto *Clause = getActiveClause())
20592059
return Clause->Elements;
20602060
return {};

include/swift/AST/IfConfigClause.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222
namespace swift {
2323
class Expr;
2424
class SourceLoc;
25+
struct ASTNode;
2526

2627
/// This represents one part of a #if block. If the condition field is
2728
/// non-null, then this represents a #if or a #elseif, otherwise it represents
2829
/// an #else block.
29-
template <typename ElemTy>
3030
struct IfConfigClause {
3131
/// The location of the #if, #elseif, or #else keyword.
3232
SourceLoc Loc;
@@ -36,13 +36,13 @@ struct IfConfigClause {
3636
Expr *Cond;
3737

3838
/// Elements inside the clause
39-
ArrayRef<ElemTy> Elements;
39+
ArrayRef<ASTNode> Elements;
4040

4141
/// True if this is the active clause of the #if block.
4242
bool isActive;
4343

44-
IfConfigClause<ElemTy>(SourceLoc Loc, Expr *Cond,
45-
ArrayRef<ElemTy> Elements, bool isActive)
44+
IfConfigClause(SourceLoc Loc, Expr *Cond,
45+
ArrayRef<ASTNode> Elements, bool isActive)
4646
: Loc(Loc), Cond(Cond), Elements(Elements), isActive(isActive) {
4747
}
4848
};

include/swift/AST/PrintOptions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ struct PrintOptions {
279279
/// Print all decls that have at least this level of access.
280280
Accessibility AccessibilityFilter = Accessibility::Private;
281281

282-
/// Print IfConfigDecls and IfConfigStmts.
282+
/// Print IfConfigDecls.
283283
bool PrintIfConfig = true;
284284

285285
/// Whether we are printing for sil.

include/swift/AST/Stmt.h

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -655,46 +655,6 @@ class GuardStmt : public LabeledConditionalStmt {
655655
static bool classof(const Stmt *S) { return S->getKind() == StmtKind::Guard; }
656656
};
657657

658-
/// IfConfigStmt - This class models the statement-side representation of
659-
/// #if/#else/#endif blocks.
660-
class IfConfigStmt : public Stmt {
661-
/// An array of clauses controlling each of the #if/#elseif/#else conditions.
662-
/// The array is ASTContext allocated.
663-
ArrayRef<IfConfigClause<ASTNode>> Clauses;
664-
SourceLoc EndLoc;
665-
bool HadMissingEnd;
666-
667-
public:
668-
IfConfigStmt(ArrayRef<IfConfigClause<ASTNode>> Clauses, SourceLoc EndLoc,
669-
bool HadMissingEnd)
670-
: Stmt(StmtKind::IfConfig, /*implicit=*/false),
671-
Clauses(Clauses), EndLoc(EndLoc), HadMissingEnd(HadMissingEnd) {}
672-
673-
SourceLoc getIfLoc() const { return Clauses[0].Loc; }
674-
675-
SourceLoc getStartLoc() const { return getIfLoc(); }
676-
SourceLoc getEndLoc() const { return EndLoc; }
677-
678-
bool hadMissingEnd() const { return HadMissingEnd; }
679-
680-
const ArrayRef<IfConfigClause<ASTNode>> &getClauses() const {
681-
return Clauses;
682-
}
683-
684-
ArrayRef<ASTNode> getActiveClauseElements() const {
685-
for (auto &Clause : Clauses)
686-
if (Clause.isActive)
687-
return Clause.Elements;
688-
return ArrayRef<ASTNode>();
689-
}
690-
691-
// Implement isa/cast/dyncast/etc.
692-
static bool classof(const Stmt *S) {
693-
return S->getKind() == StmtKind::IfConfig;
694-
}
695-
};
696-
697-
698658
/// WhileStmt - while statement. After type-checking, the condition is of
699659
/// type Builtin.Int1.
700660
class WhileStmt : public LabeledConditionalStmt {

include/swift/AST/StmtNodes.def

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ STMT(Catch, Stmt)
6161
STMT(Break, Stmt)
6262
STMT(Continue, Stmt)
6363
STMT(Fallthrough, Stmt)
64-
STMT(IfConfig, Stmt)
6564
STMT(Fail, Stmt)
6665
STMT(Throw, Stmt)
6766

include/swift/Parse/Parser.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,11 @@ class Parser {
716716
ParserResult<TypeDecl> parseDeclAssociatedType(ParseDeclOptions Flags,
717717
DeclAttributes &Attributes);
718718

719-
ParserResult<IfConfigDecl> parseDeclIfConfig(ParseDeclOptions Flags);
719+
/// Parse a #if ... #endif directive.
720+
/// Delegate callback function to parse elements in the blocks.
721+
ParserResult<IfConfigDecl> parseIfConfig(
722+
llvm::function_ref<void(SmallVectorImpl<ASTNode> &, bool)> parseElements);
723+
720724
/// Parse a #line/#sourceLocation directive.
721725
/// 'isLine = true' indicates parsing #line instead of #sourcelocation
722726
ParserStatus parseLineDirective(bool isLine = false);
@@ -1277,8 +1281,6 @@ class Parser {
12771281
ParserResult<PoundAvailableInfo> parseStmtConditionPoundAvailable();
12781282
ParserResult<Stmt> parseStmtIf(LabeledStmtInfo LabelInfo);
12791283
ParserResult<Stmt> parseStmtGuard();
1280-
ParserResult<Stmt> parseStmtIfConfig(BraceItemListKind Kind
1281-
= BraceItemListKind::Brace);
12821284
ParserResult<Stmt> parseStmtWhile(LabeledStmtInfo LabelInfo);
12831285
ParserResult<Stmt> parseStmtRepeat(LabeledStmtInfo LabelInfo);
12841286
ParserResult<Stmt> parseStmtDo(LabeledStmtInfo LabelInfo);

lib/AST/ASTDumper.cpp

Lines changed: 26 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,33 +1036,41 @@ namespace {
10361036
}
10371037
PrintWithColorRAII(OS, ParenthesisColor) << ')';
10381038
}
1039+
1040+
void printASTNodes(const ArrayRef<ASTNode> &Elements, StringRef Name) {
1041+
OS.indent(Indent);
1042+
PrintWithColorRAII(OS, ParenthesisColor) << "(";
1043+
PrintWithColorRAII(OS, ASTNodeColor) << Name;
1044+
for (auto Elt : Elements) {
1045+
OS << '\n';
1046+
if (auto *SubExpr = Elt.dyn_cast<Expr*>())
1047+
printRec(SubExpr);
1048+
else if (auto *SubStmt = Elt.dyn_cast<Stmt*>())
1049+
printRec(SubStmt);
1050+
else
1051+
printRec(Elt.get<Decl*>());
1052+
}
1053+
PrintWithColorRAII(OS, ParenthesisColor) << ')';
1054+
}
10391055

10401056
void visitIfConfigDecl(IfConfigDecl *ICD) {
1041-
OS.indent(Indent);
1042-
PrintWithColorRAII(OS, ParenthesisColor) << '(';
1043-
OS << "#if_decl\n";
1057+
printCommon(ICD, "if_config_decl");
10441058
Indent += 2;
10451059
for (auto &Clause : ICD->getClauses()) {
1060+
OS << '\n';
1061+
OS.indent(Indent);
1062+
PrintWithColorRAII(OS, StmtColor) << (Clause.Cond ? "#if:" : "#else:");
1063+
if (Clause.isActive)
1064+
PrintWithColorRAII(OS, DeclModifierColor) << " active";
10461065
if (Clause.Cond) {
1047-
PrintWithColorRAII(OS, ParenthesisColor) << '(';
1048-
OS << "#if:";
1049-
if (Clause.isActive) OS << " active";
10501066
OS << "\n";
10511067
printRec(Clause.Cond);
1052-
} else {
1053-
OS << '\n';
1054-
PrintWithColorRAII(OS, ParenthesisColor) << '(';
1055-
OS << "#else:";
1056-
if (Clause.isActive) OS << " active";
1057-
OS << "\n";
10581068
}
10591069

1060-
for (auto D : Clause.Elements) {
1061-
OS << '\n';
1062-
printRec(D);
1063-
}
1064-
1065-
PrintWithColorRAII(OS, ParenthesisColor) << ')';
1070+
OS << '\n';
1071+
Indent += 2;
1072+
printASTNodes(Clause.Elements, "elements");
1073+
Indent -= 2;
10661074
}
10671075

10681076
Indent -= 2;
@@ -1422,35 +1430,6 @@ class PrintStmt : public StmtVisitor<PrintStmt> {
14221430
PrintWithColorRAII(OS, ParenthesisColor) << ')';
14231431
}
14241432

1425-
void visitIfConfigStmt(IfConfigStmt *S) {
1426-
printCommon(S, "#if_stmt");
1427-
Indent += 2;
1428-
for (auto &Clause : S->getClauses()) {
1429-
OS << '\n';
1430-
OS.indent(Indent);
1431-
if (Clause.Cond) {
1432-
PrintWithColorRAII(OS, ParenthesisColor) << '(';
1433-
PrintWithColorRAII(OS, StmtColor) << "#if:";
1434-
if (Clause.isActive)
1435-
PrintWithColorRAII(OS, DeclModifierColor) << " active";
1436-
OS << '\n';
1437-
printRec(Clause.Cond);
1438-
} else {
1439-
PrintWithColorRAII(OS, StmtColor) << "#else";
1440-
if (Clause.isActive)
1441-
PrintWithColorRAII(OS, DeclModifierColor) << " active";
1442-
}
1443-
1444-
OS << '\n';
1445-
Indent += 2;
1446-
printASTNodes(Clause.Elements, "elements");
1447-
Indent -= 2;
1448-
}
1449-
1450-
Indent -= 2;
1451-
PrintWithColorRAII(OS, ParenthesisColor) << ')';
1452-
}
1453-
14541433
void visitDoStmt(DoStmt *S) {
14551434
printCommon(S, "do_stmt") << '\n';
14561435
printRec(S->getBody());

lib/AST/ASTPrinter.cpp

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2360,7 +2360,21 @@ void PrintAST::visitTopLevelCodeDecl(TopLevelCodeDecl *decl) {
23602360
}
23612361

23622362
void PrintAST::visitIfConfigDecl(IfConfigDecl *ICD) {
2363-
// FIXME: Pretty print #if decls
2363+
if (!Options.PrintIfConfig)
2364+
return;
2365+
2366+
for (auto &Clause : ICD->getClauses()) {
2367+
if (&Clause == &*ICD->getClauses().begin())
2368+
Printer << tok::pound_if << " /* condition */"; // FIXME: print condition
2369+
else if (Clause.Cond)
2370+
Printer << tok::pound_elseif << " /* condition */"; // FIXME: print condition
2371+
else
2372+
Printer << tok::pound_else;
2373+
printASTNodes(Clause.Elements);
2374+
Printer.printNewline();
2375+
indent();
2376+
}
2377+
Printer << tok::pound_endif;
23642378
}
23652379

23662380
void PrintAST::visitTypeAliasDecl(TypeAliasDecl *decl) {
@@ -3267,27 +3281,6 @@ void PrintAST::visitGuardStmt(GuardStmt *stmt) {
32673281
visit(stmt->getBody());
32683282
}
32693283

3270-
void PrintAST::visitIfConfigStmt(IfConfigStmt *stmt) {
3271-
if (!Options.PrintIfConfig)
3272-
return;
3273-
3274-
for (auto &Clause : stmt->getClauses()) {
3275-
if (&Clause == &*stmt->getClauses().begin())
3276-
Printer << tok::pound_if << " "; // FIXME: print condition
3277-
else if (Clause.Cond)
3278-
Printer << tok::pound_elseif << ""; // FIXME: print condition
3279-
else
3280-
Printer << tok::pound_else;
3281-
Printer.printNewline();
3282-
if (printASTNodes(Clause.Elements)) {
3283-
Printer.printNewline();
3284-
indent();
3285-
}
3286-
}
3287-
Printer.printNewline();
3288-
Printer << tok::pound_endif;
3289-
}
3290-
32913284
void PrintAST::visitWhileStmt(WhileStmt *stmt) {
32923285
Printer << tok::kw_while << " ";
32933286
// FIXME: print condition

lib/AST/ASTScope.cpp

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -188,24 +188,6 @@ static bool hasAccessors(AbstractStorageDecl *asd) {
188188
llvm_unreachable("Unhandled ContinuationKind in switch.");
189189
}
190190

191-
/// Determine whether this is a top-level code declaration that isn't just
192-
/// wrapping an #if.
193-
static bool isRealTopLevelCodeDecl(Decl *decl) {
194-
auto topLevelCode = dyn_cast<TopLevelCodeDecl>(decl);
195-
if (!topLevelCode) return false;
196-
197-
// Drop top-level statements containing just an IfConfigStmt.
198-
// FIXME: The modeling of IfConfig is weird.
199-
auto braceStmt = topLevelCode->getBody();
200-
auto elements = braceStmt->getElements();
201-
if (elements.size() == 1 &&
202-
elements[0].is<Stmt *>() &&
203-
isa<IfConfigStmt>(elements[0].get<Stmt *>()))
204-
return false;
205-
206-
return true;
207-
}
208-
209191
void ASTScope::expand() const {
210192
assert(!isExpanded() && "Already expanded the children of this node");
211193
ASTContext &ctx = getASTContext();
@@ -313,7 +295,7 @@ void ASTScope::expand() const {
313295

314296
// If the declaration is a top-level code declaration, turn the source
315297
// file into a continuation. We're done.
316-
if (isRealTopLevelCodeDecl(decl)) {
298+
if (isa<TopLevelCodeDecl>(decl)) {
317299
addActiveContinuation(this);
318300
break;
319301
}
@@ -950,7 +932,6 @@ ASTScope *ASTScope::createIfNeeded(const ASTScope *parent, Decl *decl) {
950932
}
951933

952934
case DeclKind::TopLevelCode:
953-
if (!isRealTopLevelCodeDecl(decl)) return nullptr;
954935
return new (ctx) ASTScope(parent, cast<TopLevelCodeDecl>(decl));
955936

956937
case DeclKind::Protocol:
@@ -1153,7 +1134,6 @@ ASTScope *ASTScope::createIfNeeded(const ASTScope *parent, Stmt *stmt) {
11531134
case StmtKind::Break:
11541135
case StmtKind::Continue:
11551136
case StmtKind::Fallthrough:
1156-
case StmtKind::IfConfig:
11571137
case StmtKind::Fail:
11581138
case StmtKind::Throw:
11591139
// Nothing to do for these statements.

0 commit comments

Comments
 (0)