Skip to content

Commit 5ef6326

Browse files
committed
[NFC] Add representations for DeclNameRef/Loc with module selector
1 parent a53b6ab commit 5ef6326

File tree

5 files changed

+136
-37
lines changed

5 files changed

+136
-37
lines changed

include/swift/AST/DeclNameLoc.h

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,59 +29,66 @@ class ASTContext;
2929
class DeclNameLoc {
3030
/// Source location information.
3131
///
32-
/// If \c NumArgumentLabels == 0, this is the SourceLoc for the base name.
33-
/// Otherwise, it points to an array of SourceLocs, which contains:
32+
/// If \c NumArgumentLabels == 0 and \c !HasModuleSelectorLoc, this is the
33+
/// SourceLoc for the base name. Otherwise, it points to an array of
34+
/// SourceLocs, which contains:
3435
/// * The base name location
36+
/// * The module selector location
3537
/// * The left parentheses location
3638
/// * The right parentheses location
3739
/// * The locations of each of the argument labels.
3840
const void *LocationInfo;
3941

4042
/// The number of argument labels stored in the name.
41-
unsigned NumArgumentLabels;
43+
uint32_t NumArgumentLabels;
44+
bool HasModuleSelectorLoc;
4245

4346
enum {
4447
BaseNameIndex = 0,
45-
LParenIndex = 1,
46-
RParenIndex = 2,
47-
FirstArgumentLabelIndex = 3,
48+
ModuleSelectorIndex = 1,
49+
LParenIndex = 2,
50+
RParenIndex = 3,
51+
FirstArgumentLabelIndex = 4,
4852
};
4953

5054
/// Retrieve a pointer to either the only source location that was
5155
/// stored or to the array of source locations that was stored.
5256
SourceLoc const * getSourceLocs() const {
53-
if (NumArgumentLabels == 0)
57+
if (NumArgumentLabels == 0 && !HasModuleSelectorLoc)
5458
return reinterpret_cast<SourceLoc const *>(&LocationInfo);
5559

5660
return reinterpret_cast<SourceLoc const *>(LocationInfo);
5761
}
5862

5963
public:
6064
/// Create an invalid declaration name location.
61-
DeclNameLoc() : LocationInfo(0), NumArgumentLabels(0) { }
65+
DeclNameLoc()
66+
: LocationInfo(0), NumArgumentLabels(0), HasModuleSelectorLoc(false) { }
6267

6368
/// Create declaration name location information for a base name.
6469
explicit DeclNameLoc(SourceLoc baseNameLoc)
6570
: LocationInfo(baseNameLoc.getOpaquePointerValue()),
66-
NumArgumentLabels(0) { }
71+
NumArgumentLabels(0), HasModuleSelectorLoc(false) { }
6772

6873
explicit DeclNameLoc(ASTContext &ctx, SourceLoc moduleSelectorLoc,
6974
SourceLoc baseNameLoc)
70-
: DeclNameLoc(baseNameLoc) { }
75+
: DeclNameLoc(ctx, moduleSelectorLoc, baseNameLoc,
76+
SourceLoc(), {}, SourceLoc()) { }
7177

7278
/// Create declaration name location information for a compound
7379
/// name.
7480
DeclNameLoc(ASTContext &ctx, SourceLoc baseNameLoc,
7581
SourceLoc lParenLoc,
7682
ArrayRef<SourceLoc> argumentLabelLocs,
77-
SourceLoc rParenLoc);
83+
SourceLoc rParenLoc)
84+
: DeclNameLoc(ctx, SourceLoc(), baseNameLoc,
85+
lParenLoc, argumentLabelLocs, rParenLoc) { }
7886

7987
DeclNameLoc(ASTContext &ctx, SourceLoc moduleSelectorLoc,
8088
SourceLoc baseNameLoc,
8189
SourceLoc lParenLoc,
8290
ArrayRef<SourceLoc> argumentLabelLocs,
83-
SourceLoc rParenLoc)
84-
: DeclNameLoc(ctx, baseNameLoc, lParenLoc, argumentLabelLocs, rParenLoc) { }
91+
SourceLoc rParenLoc);
8592

8693
/// Whether the location information is valid.
8794
bool isValid() const { return getBaseNameLoc().isValid(); }
@@ -117,11 +124,12 @@ class DeclNameLoc {
117124
}
118125

119126
SourceLoc getModuleSelectorLoc() const {
120-
return SourceLoc();
127+
if (!HasModuleSelectorLoc) return SourceLoc();
128+
return getSourceLocs()[ModuleSelectorIndex];
121129
}
122130

123131
SourceLoc getStartLoc() const {
124-
return getBaseNameLoc();
132+
return HasModuleSelectorLoc ? getModuleSelectorLoc() : getBaseNameLoc();
125133
}
126134

127135
SourceLoc getEndLoc() const {
@@ -130,9 +138,7 @@ class DeclNameLoc {
130138

131139
/// Retrieve the complete source range for this declaration name.
132140
SourceRange getSourceRange() const {
133-
if (NumArgumentLabels == 0) return getBaseNameLoc();
134-
135-
return SourceRange(getBaseNameLoc(), getRParenLoc());
141+
return SourceRange(getStartLoc(), getEndLoc());
136142
}
137143
};
138144

include/swift/AST/Identifier.h

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -640,39 +640,86 @@ void simple_display(llvm::raw_ostream &out, DeclName name);
640640
/// An in-source reference to another declaration, including qualification
641641
/// information.
642642
class DeclNameRef {
643-
DeclName FullName;
643+
friend class ASTContext;
644+
645+
/// Contains the name and module for a DeclNameRef with a module selector.
646+
struct alignas(Identifier) SelectiveDeclNameRef : llvm::FoldingSetNode {
647+
Identifier moduleSelector; // Note: currently can never be empty().
648+
DeclName fullName;
649+
650+
SelectiveDeclNameRef(Identifier moduleSelector, DeclName fullName)
651+
: moduleSelector(moduleSelector), fullName(fullName) { }
652+
653+
/// Uniquing for the ASTContext.
654+
static void Profile(llvm::FoldingSetNodeID &id, Identifier moduleSelector,
655+
DeclName fullName);
656+
657+
void Profile(llvm::FoldingSetNodeID &id) {
658+
Profile(id, moduleSelector, fullName);
659+
}
660+
661+
// Make vanilla new/delete illegal for SelectiveDeclNameRef.
662+
void *operator new(size_t Bytes) = delete;
663+
void operator delete(void *Data) = delete;
664+
665+
// Only allow allocation of SelectiveDeclNameRef using the allocator
666+
// in ASTContext or by doing a placement new.
667+
void *operator new(size_t Bytes, const ASTContext &C,
668+
unsigned Alignment = alignof(SelectiveDeclNameRef));
669+
void *operator new(size_t Bytes, void *Mem) {
670+
assert(Mem);
671+
return Mem;
672+
}
673+
};
674+
675+
llvm::PointerUnion<DeclName, SelectiveDeclNameRef *> storage;
676+
677+
explicit DeclNameRef(void *Opaque)
678+
: storage(decltype(storage)::getFromOpaqueValue(Opaque)) { }
679+
680+
void initialize(ASTContext &C, Identifier moduleScope, DeclName fullName);
644681

645682
public:
646683
static DeclNameRef createSubscript();
647684
static DeclNameRef createConstructor();
648685

649-
DeclNameRef() : FullName() { }
686+
DeclNameRef() : storage(DeclName()) { }
650687

651-
void *getOpaqueValue() const { return FullName.getOpaqueValue(); }
688+
void *getOpaqueValue() const {
689+
return storage.getOpaqueValue();
690+
}
652691
static DeclNameRef getFromOpaqueValue(void *p);
653692

654693
explicit DeclNameRef(ASTContext &C, Identifier moduleSelector,
655-
DeclName fullName)
656-
: FullName(fullName) { }
694+
DeclName fullName) {
695+
initialize(C, moduleSelector, fullName);
696+
}
657697

658698
explicit DeclNameRef(ASTContext &C, Identifier moduleSelector,
659-
DeclBaseName baseName, ArrayRef<Identifier> argLabels)
660-
: FullName(C, baseName, argLabels) { }
699+
DeclBaseName baseName, ArrayRef<Identifier> argLabels) {
700+
initialize(C, moduleSelector, DeclName(C, baseName, argLabels));
701+
}
661702

662703
explicit DeclNameRef(DeclName FullName)
663-
: FullName(BaseName) { }
704+
: storage(FullName) { }
664705

665706
bool hasModuleSelector() const {
666-
return false;
707+
return storage.is<SelectiveDeclNameRef *>();
667708
}
668709

669710
Identifier getModuleSelector() const {
670-
return Identifier();
711+
if (!hasModuleSelector())
712+
return Identifier();
713+
714+
return storage.get<SelectiveDeclNameRef *>()->moduleSelector;
671715
}
672716

673717
/// The name of the declaration being referenced.
674718
DeclName getFullName() const {
675-
return FullName;
719+
if (!hasModuleSelector())
720+
return storage.get<DeclName>();
721+
722+
return storage.get<SelectiveDeclNameRef *>()->fullName;
676723
}
677724

678725
/// The base name of the declaration being referenced.
@@ -784,7 +831,7 @@ class DeclNameRef {
784831
};
785832

786833
inline DeclNameRef DeclNameRef::getFromOpaqueValue(void *p) {
787-
return DeclNameRef(DeclName::getFromOpaqueValue(p));
834+
return DeclNameRef(p);
788835
}
789836

790837
inline DeclNameRef DeclNameRef::withoutArgumentLabels(ASTContext &C) const {
@@ -968,7 +1015,7 @@ namespace llvm {
9681015
static inline swift::DeclNameRef getFromVoidPointer(void *ptr) {
9691016
return swift::DeclNameRef::getFromOpaqueValue(ptr);
9701017
}
971-
enum { NumLowBitsAvailable = PointerLikeTypeTraits<swift::DeclName>::NumLowBitsAvailable };
1018+
enum { NumLowBitsAvailable = PointerLikeTypeTraits<swift::DeclName>::NumLowBitsAvailable - 1 };
9721019
};
9731020

9741021
// DeclNameRefs hash just like DeclNames.

lib/AST/ASTContext.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,7 @@ struct ASTContext::Implementation {
469469
llvm::DenseMap<BuiltinIntegerWidth, BuiltinIntegerType*> IntegerTypes;
470470
llvm::FoldingSet<BuiltinVectorType> BuiltinVectorTypes;
471471
llvm::FoldingSet<DeclName::CompoundDeclName> CompoundNames;
472+
llvm::FoldingSet<DeclNameRef::SelectiveDeclNameRef> SelectiveNameRefs;
472473
llvm::DenseMap<UUID, OpenedArchetypeType *> OpenedExistentialArchetypes;
473474
llvm::FoldingSet<IndexSubset> IndexSubsets;
474475
llvm::FoldingSet<AutoDiffDerivativeFunctionIdentifier>
@@ -4558,6 +4559,44 @@ DeclName::DeclName(ASTContext &C, DeclBaseName baseName,
45584559
initialize(C, baseName, names);
45594560
}
45604561

4562+
// Only allow allocation of SelectiveDeclNameRefs using the allocator in
4563+
// ASTContext.
4564+
void *DeclNameRef::SelectiveDeclNameRef::
4565+
operator new(size_t Bytes, const ASTContext &C, unsigned Alignment) {
4566+
return C.Allocate(Bytes, Alignment);
4567+
}
4568+
4569+
void DeclNameRef::SelectiveDeclNameRef::Profile(llvm::FoldingSetNodeID &id,
4570+
Identifier moduleSelector,
4571+
DeclName fullName) {
4572+
assert(!moduleSelector.empty() &&
4573+
"Looking up SelectiveDeclNameRef with empty module?");
4574+
id.AddPointer(moduleSelector.getAsOpaquePointer());
4575+
id.AddPointer(fullName.getOpaqueValue());
4576+
}
4577+
4578+
void DeclNameRef::initialize(ASTContext &C, Identifier moduleSelector,
4579+
DeclName fullName) {
4580+
if (moduleSelector.empty()) {
4581+
storage = fullName;
4582+
return;
4583+
}
4584+
4585+
llvm::FoldingSetNodeID id;
4586+
SelectiveDeclNameRef::Profile(id, moduleSelector, fullName);
4587+
4588+
void *insert = nullptr;
4589+
if (SelectiveDeclNameRef *selectiveRef
4590+
= C.getImpl().SelectiveNameRefs.FindNodeOrInsertPos(id, insert)) {
4591+
storage = selectiveRef;
4592+
return;
4593+
}
4594+
4595+
auto selectiveRef = new (C) SelectiveDeclNameRef(moduleSelector, fullName);
4596+
storage = selectiveRef;
4597+
C.getImpl().SelectiveNameRefs.InsertNode(selectiveRef, insert);
4598+
}
4599+
45614600
/// Find the implementation of the named type in the given module.
45624601
static NominalTypeDecl *findUnderlyingTypeInModule(ASTContext &ctx,
45634602
Identifier name,

lib/AST/DeclNameLoc.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,24 @@
1919

2020
using namespace swift;
2121

22-
DeclNameLoc::DeclNameLoc(ASTContext &ctx, SourceLoc baseNameLoc,
22+
DeclNameLoc::DeclNameLoc(ASTContext &ctx, SourceLoc moduleSelectorLoc,
23+
SourceLoc baseNameLoc,
2324
SourceLoc lParenLoc,
2425
ArrayRef<SourceLoc> argumentLabelLocs,
2526
SourceLoc rParenLoc)
26-
: NumArgumentLabels(argumentLabelLocs.size()) {
27-
assert(NumArgumentLabels > 0 && "Use other constructor");
27+
: NumArgumentLabels(argumentLabelLocs.size()),
28+
HasModuleSelectorLoc(moduleSelectorLoc.isValid())
29+
{
30+
if (NumArgumentLabels == 0 && !HasModuleSelectorLoc) {
31+
LocationInfo = baseNameLoc.getOpaquePointerValue();
32+
return;
33+
}
2834

2935
// Copy the location information into permanent storage.
30-
auto storedLocs = ctx.Allocate<SourceLoc>(NumArgumentLabels + 3);
36+
auto storedLocs =
37+
ctx.Allocate<SourceLoc>(FirstArgumentLabelIndex + NumArgumentLabels);
3138
storedLocs[BaseNameIndex] = baseNameLoc;
39+
storedLocs[ModuleSelectorIndex] = moduleSelectorLoc;
3240
storedLocs[LParenIndex] = lParenLoc;
3341
storedLocs[RParenIndex] = rParenLoc;
3442
std::memcpy(storedLocs.data() + FirstArgumentLabelIndex,

lib/Sema/CSSimplify.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7192,8 +7192,7 @@ performMemberLookup(ConstraintKind constraintKind, DeclNameRef memberName,
71927192
// anything else, because the cost of the general search is so
71937193
// high.
71947194
if (auto info = getArgumentInfo(memberLocator)) {
7195-
memberName.getFullName() = DeclName(ctx, memberName.getBaseName(),
7196-
info->Labels);
7195+
memberName = memberName.withArgumentLabels(ctx, info->Labels);
71977196
}
71987197
}
71997198

0 commit comments

Comments
 (0)