Skip to content

Commit b8995b0

Browse files
committed
Transform the Module class into ModuleDecl.
Modules occupy a weird space in the AST now: they can be treated like types (Swift.Int), which is captured by ModuleType. They can be treated like values for disambiguation (Swift.print), which is captured by ModuleExpr. And we jump through hoops in various places to store "either a module or a decl". Start cleaning this up by transforming Module into ModuleDecl, a TypeDecl that's implicitly created to describe a module. Subsequent changes will start folding away the special cases (ModuleExpr -> DeclRefExpr, name lookup results stop having a separate Module case, etc.). Note that the Module -> ModuleDecl typedef is there to limit the changes needed. Much of this patch is actually dealing with the fact that Module used to have Ctx and Name public members that now need to be accessed via getASTContext() and getName(), respectively. Swift SVN r28284
1 parent a550503 commit b8995b0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+446
-365
lines changed

include/swift/AST/ASTContext.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ namespace swift {
6363
class FunctionType;
6464
class ArchetypeType;
6565
class Identifier;
66-
class Module;
66+
class ModuleDecl;
6767
class ModuleLoader;
6868
class NominalTypeDecl;
6969
class TupleTypeElt;
@@ -174,13 +174,13 @@ class ASTContext {
174174
DiagnosticEngine &Diags;
175175

176176
/// The set of top-level modules we have loaded.
177-
llvm::DenseMap<Identifier, Module*> LoadedModules;
177+
llvm::DenseMap<Identifier, ModuleDecl*> LoadedModules;
178178

179179
/// The builtin module.
180-
Module * const TheBuiltinModule;
180+
ModuleDecl * const TheBuiltinModule;
181181

182182
/// The standard library module.
183-
mutable Module *TheStdlibModule = nullptr;
183+
mutable ModuleDecl *TheStdlibModule = nullptr;
184184

185185
/// The name of the standard library module "Swift".
186186
Identifier StdlibModuleName;
@@ -220,7 +220,7 @@ class ASTContext {
220220
llvm::StringMap<Type> RemappedTypes;
221221

222222
/// Cache for generic mangling signatures.
223-
llvm::DenseMap<std::pair<GenericSignature*, Module*>,
223+
llvm::DenseMap<std::pair<GenericSignature*, ModuleDecl*>,
224224
CanGenericSignature> ManglingSignatures;
225225

226226
private:
@@ -599,28 +599,28 @@ class ASTContext {
599599

600600
/// \returns a module with a given name that was already loaded. If the
601601
/// module was not loaded, returns nullptr.
602-
Module *getLoadedModule(
602+
ModuleDecl *getLoadedModule(
603603
ArrayRef<std::pair<Identifier, SourceLoc>> ModulePath) const;
604604

605-
Module *getLoadedModule(Identifier ModuleName) const;
605+
ModuleDecl *getLoadedModule(Identifier ModuleName) const;
606606

607607
/// \brief Attempts to load a module into this ASTContext.
608608
///
609609
/// If a module by this name has already been loaded, the existing module will
610610
/// be returned.
611611
///
612612
/// \returns The requested module, or NULL if the module cannot be found.
613-
Module *getModule(ArrayRef<std::pair<Identifier, SourceLoc>> ModulePath);
613+
ModuleDecl *getModule(ArrayRef<std::pair<Identifier, SourceLoc>> ModulePath);
614614

615-
Module *getModuleByName(StringRef ModuleName);
615+
ModuleDecl *getModuleByName(StringRef ModuleName);
616616

617617
/// Returns the standard library module, or null if the library isn't present.
618618
///
619619
/// If \p loadIfAbsent is true, the ASTContext will attempt to load the module
620620
/// if it hasn't been set yet.
621-
Module *getStdlibModule(bool loadIfAbsent = false);
621+
ModuleDecl *getStdlibModule(bool loadIfAbsent = false);
622622

623-
Module *getStdlibModule() const {
623+
ModuleDecl *getStdlibModule() const {
624624
return const_cast<ASTContext *>(this)->getStdlibModule(false);
625625
}
626626

@@ -697,7 +697,7 @@ class ASTContext {
697697
DeclContext *gpContext) const;
698698

699699
/// Record compiler-known protocol information in the AST.
700-
void recordKnownProtocols(Module *Stdlib);
700+
void recordKnownProtocols(ModuleDecl *Stdlib);
701701

702702
/// \brief Retrieve the substitutions for a bound generic type, if known.
703703
Optional<ArrayRef<Substitution>>

include/swift/AST/ASTVisitor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include "swift/AST/Decl.h"
2222
#include "swift/AST/Expr.h"
23+
#include "swift/AST/Module.h"
2324
#include "swift/AST/Pattern.h"
2425
#include "swift/AST/Stmt.h"
2526
#include "swift/AST/TypeRepr.h"

include/swift/AST/ASTWalker.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace swift {
2020

2121
class Decl;
2222
class Expr;
23-
class Module;
23+
class ModuleDecl;
2424
class Stmt;
2525
class Pattern;
2626
class TypeRepr;
@@ -37,7 +37,7 @@ class ASTWalker {
3737
void *Ptr = nullptr;
3838

3939
public:
40-
ParentTy(Module *Mod) : Kind(ParentKind::Module), Ptr(Mod) {}
40+
ParentTy(ModuleDecl *Mod) : Kind(ParentKind::Module), Ptr(Mod) {}
4141
ParentTy(Decl *D) : Kind(ParentKind::Decl), Ptr(D) {}
4242
ParentTy(Stmt *S) : Kind(ParentKind::Stmt), Ptr(S) {}
4343
ParentTy(Expr *E) : Kind(ParentKind::Expr), Ptr(E) {}
@@ -51,8 +51,9 @@ class ASTWalker {
5151
return Kind;
5252
}
5353

54-
Module *getAsModule() const {
55-
return Kind == ParentKind::Module ? static_cast<Module*>(Ptr) : nullptr;
54+
ModuleDecl *getAsModule() const {
55+
return Kind == ParentKind::Module ? static_cast<ModuleDecl*>(Ptr)
56+
: nullptr;
5657
}
5758
Decl *getAsDecl() const {
5859
return Kind == ParentKind::Decl ? static_cast<Decl*>(Ptr) : nullptr;

include/swift/AST/ArchetypeBuilder.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class GenericSignature;
4343
class GenericTypeParamDecl;
4444
class GenericTypeParamType;
4545
class LazyResolver;
46-
class Module;
46+
class ModuleDecl;
4747
class Pattern;
4848
class ProtocolConformance;
4949
class ProtocolDecl;
@@ -114,7 +114,7 @@ class ArchetypeBuilder {
114114
class InferRequirementsWalker;
115115
friend class InferRequirementsWalker;
116116

117-
Module &Mod;
117+
ModuleDecl &Mod;
118118
ASTContext &Context;
119119
DiagnosticEngine &Diags;
120120
LazyResolver *Resolver = nullptr;
@@ -157,7 +157,7 @@ class ArchetypeBuilder {
157157
void visitPotentialArchetypes(F f);
158158

159159
public:
160-
ArchetypeBuilder(Module &mod, DiagnosticEngine &diags);
160+
ArchetypeBuilder(ModuleDecl &mod, DiagnosticEngine &diags);
161161

162162
/// The type of a callback used to retrieve the superclass and
163163
/// protocol requirements placed on
@@ -180,11 +180,12 @@ class ArchetypeBuilder {
180180
/// to which the given type parameter conforms. The produces the final
181181
/// results of AbstractTypeParamDecl::getProtocols() for an associated type.
182182
ArchetypeBuilder(
183-
Module &mod, DiagnosticEngine &diags, LazyResolver *resolver,
183+
ModuleDecl &mod, DiagnosticEngine &diags, LazyResolver *resolver,
184184
std::function<ArrayRef<ProtocolDecl *>(ProtocolDecl *)>
185185
getInheritedProtocols,
186186
GetConformsToCallback getConformsTo,
187-
std::function<ProtocolConformance * (Module &M, Type T, ProtocolDecl* P)>
187+
std::function<ProtocolConformance * (ModuleDecl &M, Type T,
188+
ProtocolDecl* P)>
188189
conformsToProtocol);
189190
ArchetypeBuilder(ArchetypeBuilder &&);
190191
~ArchetypeBuilder();
@@ -193,7 +194,7 @@ class ArchetypeBuilder {
193194
ASTContext &getASTContext() const { return Context; }
194195

195196
/// Retrieve the module.
196-
Module &getModule() const { return Mod; }
197+
ModuleDecl &getModule() const { return Mod; }
197198

198199
/// Retrieve the lazy resolver, if there is one.
199200
LazyResolver *getLazyResolver() const { return Resolver; }
@@ -345,7 +346,7 @@ class ArchetypeBuilder {
345346
void dump(llvm::raw_ostream &out);
346347

347348
/// FIXME: Share the guts of our mapTypeIntoContext implementation with
348-
static Type mapTypeIntoContext(Module *M,
349+
static Type mapTypeIntoContext(ModuleDecl *M,
349350
GenericParamList *genericParams,
350351
Type type,
351352
LazyResolver *resolver = nullptr);

include/swift/AST/Attr.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,7 @@ class DeclAttribute : public AttributeBase {
437437
OnGenericTypeParam = 1 << 28,
438438
OnAssociatedType = 1 << 29,
439439
OnParam = 1 << 30,
440+
OnModule = 1 << 31,
440441

441442
// More coarse-grained aggregations for use in Attr.def.
442443
OnOperator = OnInfixOperator|OnPrefixOperator|OnPostfixOperator,

include/swift/AST/ClangModuleLoader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class ClangModuleLoader : public ModuleLoader {
3434

3535
/// Returns the module that contains imports and declarations from all loaded
3636
/// Objective-C header files.
37-
virtual Module *getImportedHeaderModule() const = 0;
37+
virtual ModuleDecl *getImportedHeaderModule() const = 0;
3838

3939
/// Adds a new search path to the Clang CompilerInstance, as if specified with
4040
/// -I or -F.

include/swift/AST/Decl.h

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ namespace swift {
7171
class GenericSignature;
7272
class GenericTypeParamDecl;
7373
class GenericTypeParamType;
74-
class Module;
74+
class ModuleDecl;
7575
class NameAliasType;
7676
class EnumCaseDecl;
7777
class EnumElementDecl;
@@ -191,6 +191,7 @@ enum class DescriptiveDeclKind : uint8_t {
191191
WillSet,
192192
DidSet,
193193
EnumElement,
194+
Module,
194195
};
195196

196197
/// Keeps track of stage of circularity checking for the given protocol.
@@ -636,14 +637,15 @@ class alignas(1 << DeclAlignInBits) Decl {
636637
friend class MemberLookupTable;
637638

638639
private:
639-
DeclContext *Context;
640+
llvm::PointerUnion<DeclContext *, ASTContext *> Context;
640641

641642
Decl(const Decl&) = delete;
642643
void operator=(const Decl&) = delete;
643644

644645
protected:
645646

646-
Decl(DeclKind kind, DeclContext *DC) : OpaqueBits(0), Context(DC) {
647+
Decl(DeclKind kind, llvm::PointerUnion<DeclContext *, ASTContext *> context)
648+
: OpaqueBits(0), Context(context) {
647649
DeclBits.Kind = unsigned(kind);
648650
DeclBits.Invalid = false;
649651
DeclBits.Implicit = false;
@@ -694,7 +696,9 @@ class alignas(1 << DeclAlignInBits) Decl {
694696
/// for instance, self.
695697
bool isUserAccessible() const;
696698

697-
DeclContext *getDeclContext() const { return Context; }
699+
DeclContext *getDeclContext() const {
700+
return Context.dyn_cast<DeclContext *>();
701+
}
698702
void setDeclContext(DeclContext *DC);
699703

700704
/// Retrieve the innermost declaration context corresponding to this
@@ -703,12 +707,14 @@ class alignas(1 << DeclAlignInBits) Decl {
703707
DeclContext *getInnermostDeclContext();
704708

705709
/// \brief Retrieve the module in which this declaration resides.
706-
Module *getModuleContext() const;
710+
ModuleDecl *getModuleContext() const;
707711

708712
/// getASTContext - Return the ASTContext that this decl lives in.
709713
ASTContext &getASTContext() const {
710-
assert(Context && "Decl doesn't have an assigned context");
711-
return Context->getASTContext();
714+
if (auto dc = Context.dyn_cast<DeclContext *>())
715+
return dc->getASTContext();
716+
717+
return *Context.get<ASTContext *>();
712718
}
713719

714720
const DeclAttributes &getAttrs() const {
@@ -836,7 +842,7 @@ class alignas(1 << DeclAlignInBits) Decl {
836842
bool isPrivateStdlibDecl(bool whitelistProtocols=true) const;
837843

838844
/// Whether this declaration is weak-imported.
839-
bool isWeakImported(Module *fromModule) const;
845+
bool isWeakImported(ModuleDecl *fromModule) const;
840846

841847
// Make vanilla new/delete illegal for Decls.
842848
void *operator new(size_t Bytes) = delete;
@@ -1504,7 +1510,7 @@ class ImportDecl : public Decl {
15041510
unsigned NumPathElements;
15051511

15061512
/// The resolved module.
1507-
Module *Mod = nullptr;
1513+
ModuleDecl *Mod = nullptr;
15081514
/// The resolved decls if this is a decl import.
15091515
ArrayRef<ValueDecl *> Decls;
15101516

@@ -1562,8 +1568,8 @@ class ImportDecl : public Decl {
15621568
return getAttrs().hasAttribute<ExportedAttr>();
15631569
}
15641570

1565-
Module *getModule() const { return Mod; }
1566-
void setModule(Module *M) { Mod = M; }
1571+
ModuleDecl *getModule() const { return Mod; }
1572+
void setModule(ModuleDecl *M) { Mod = M; }
15671573

15681574
ArrayRef<ValueDecl *> getDecls() const { return Decls; }
15691575
void setDecls(ArrayRef<ValueDecl *> Ds) { Decls = Ds; }
@@ -2111,8 +2117,10 @@ class ValueDecl : public Decl {
21112117
llvm::PointerIntPair<Type, 2, OptionalEnum<Accessibility>> TypeAndAccess;
21122118

21132119
protected:
2114-
ValueDecl(DeclKind K, DeclContext *DC, DeclName name, SourceLoc NameLoc)
2115-
: Decl(K, DC), Name(name), NameLoc(NameLoc) {
2120+
ValueDecl(DeclKind K,
2121+
llvm::PointerUnion<DeclContext *, ASTContext *> context,
2122+
DeclName name, SourceLoc NameLoc)
2123+
: Decl(K, context), Name(name), NameLoc(NameLoc) {
21162124
ValueDeclBits.AlreadyInLookupTable = false;
21172125
ValueDeclBits.CheckedRedeclaration = false;
21182126
}
@@ -2324,18 +2332,18 @@ class ValueDecl : public Decl {
23242332
static bool classof(const Decl *D) {
23252333
return D->getKind() >= DeclKind::First_ValueDecl &&
23262334
D->getKind() <= DeclKind::Last_ValueDecl;
2327-
}
2328-
2335+
}
23292336
};
23302337

23312338
/// This is a common base class for declarations which declare a type.
23322339
class TypeDecl : public ValueDecl {
23332340
MutableArrayRef<TypeLoc> Inherited;
23342341

23352342
protected:
2336-
TypeDecl(DeclKind K, DeclContext *DC, Identifier name, SourceLoc NameLoc,
2343+
TypeDecl(DeclKind K, llvm::PointerUnion<DeclContext *, ASTContext *> context,
2344+
Identifier name, SourceLoc NameLoc,
23372345
MutableArrayRef<TypeLoc> inherited) :
2338-
ValueDecl(K, DC, name, NameLoc), Inherited(inherited)
2346+
ValueDecl(K, context, name, NameLoc), Inherited(inherited)
23392347
{
23402348
TypeDeclBits.CheckedInheritanceClause = false;
23412349
TypeDeclBits.ProtocolsSet = false;
@@ -2774,7 +2782,7 @@ class GenericSignature : public llvm::FoldingSetNode {
27742782
/// TODO: This is what getCanonicalSignature() ought to do, but currently
27752783
/// cannot due to implementation dependencies on 'getAllDependentTypes'
27762784
/// order matching 'getAllArchetypes' order of a generic param list.
2777-
CanGenericSignature getCanonicalManglingSignature(Module &M) const;
2785+
CanGenericSignature getCanonicalManglingSignature(ModuleDecl &M) const;
27782786

27792787
/// Uniquing for the ASTContext.
27802788
void Profile(llvm::FoldingSetNodeID &ID) {
@@ -3109,7 +3117,7 @@ class NominalTypeDecl : public TypeDecl, public DeclContext,
31093117
///
31103118
/// \returns true if any conformances were found.
31113119
bool lookupConformance(
3112-
Module *module, ProtocolDecl *protocol,
3120+
ModuleDecl *module, ProtocolDecl *protocol,
31133121
SmallVectorImpl<ProtocolConformance *> &conformances) const;
31143122

31153123
/// Retrieve all of the protocols that this nominal type conforms to.

include/swift/AST/DeclContext.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ namespace swift {
5050
class Requirement;
5151
class SourceFile;
5252
class Type;
53-
class Module;
53+
class ModuleDecl;
5454
class NominalTypeDecl;
5555
class ProtocolConformance;
5656
class ValueDecl;
@@ -336,7 +336,7 @@ class alignas(1 << DeclContextAlignInBits) DeclContext {
336336
}
337337

338338
/// Returns the module context that contains this context.
339-
Module *getParentModule() const;
339+
ModuleDecl *getParentModule() const;
340340

341341
/// Returns the module scope context that contains this context.
342342
///

include/swift/AST/DeclNodes.def

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ ABSTRACT_DECL(Value, Decl)
8484
NOMINAL_TYPE_DECL(Class, NominalTypeDecl)
8585
NOMINAL_TYPE_DECL(Protocol, NominalTypeDecl)
8686
DECL_RANGE(NominalType, Enum, Protocol)
87-
DECL_RANGE(Type, TypeAlias, Protocol)
87+
VALUE_DECL(Module, TypeDecl)
88+
DECL_RANGE(Type, TypeAlias, Module)
8889
ABSTRACT_DECL(AbstractStorage, ValueDecl)
8990
VALUE_DECL(Var, AbstractStorageDecl)
9091
VALUE_DECL(Param, VarDecl)

include/swift/AST/Mangle.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class Mangler {
9393
: Buffer(buffer), DWARFMangling(DWARFMangling), UsePunycode(usePunycode) {}
9494
void mangleContextOf(const ValueDecl *decl, BindGenerics shouldBind);
9595
void mangleContext(const DeclContext *ctx, BindGenerics shouldBind);
96-
void mangleModule(const Module *module);
96+
void mangleModule(const ModuleDecl *module);
9797
void mangleDeclName(const ValueDecl *decl);
9898
void mangleDeclType(const ValueDecl *decl, ResilienceExpansion expansion,
9999
unsigned uncurryingLevel);

0 commit comments

Comments
 (0)