Skip to content

Commit 983ac9f

Browse files
authored
Merge pull request #8019 from ahoppen/backing-type-of-DeclName
Change the backing base name type of DeclName to DeclBaseName
2 parents 1e30925 + 4f0ab96 commit 983ac9f

File tree

2 files changed

+84
-33
lines changed

2 files changed

+84
-33
lines changed

include/swift/AST/Identifier.h

Lines changed: 79 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ namespace llvm {
192192
class PointerLikeTypeTraits<swift::Identifier> {
193193
public:
194194
static inline void *getAsVoidPointer(swift::Identifier I) {
195-
return (void*)I.get();
195+
return const_cast<void *>(I.getAsOpaquePointer());
196196
}
197197
static inline swift::Identifier getFromVoidPointer(void *P) {
198198
return swift::Identifier::getFromOpaquePointer(P);
@@ -203,7 +203,54 @@ namespace llvm {
203203
} // end namespace llvm
204204

205205
namespace swift {
206-
206+
207+
/// Wrapper that may either be an Identifier or a special name
208+
/// (e.g. for subscripts)
209+
class DeclBaseName {
210+
Identifier Ident;
211+
212+
public:
213+
DeclBaseName(Identifier I) : Ident(I) {}
214+
215+
bool isSpecial() const { return false; }
216+
217+
/// Return the identifier backing the name. Assumes that the name is not
218+
/// special.
219+
Identifier getIdentifier() const {
220+
assert(!isSpecial() && "Cannot retrieve identifier from special names");
221+
return Ident;
222+
}
223+
224+
bool empty() const { return !isSpecial() && getIdentifier().empty(); }
225+
226+
const void *getAsOpaquePointer() const { return Ident.get(); }
227+
228+
static DeclBaseName getFromOpaquePointer(void *P) {
229+
return Identifier::getFromOpaquePointer(P);
230+
}
231+
};
232+
233+
} // end namespace swift
234+
235+
namespace llvm {
236+
237+
// A DeclBaseName is "pointer like".
238+
template <typename T> class PointerLikeTypeTraits;
239+
template <> class PointerLikeTypeTraits<swift::DeclBaseName> {
240+
public:
241+
static inline void *getAsVoidPointer(swift::DeclBaseName D) {
242+
return const_cast<void *>(D.getAsOpaquePointer());
243+
}
244+
static inline swift::DeclBaseName getFromVoidPointer(void *P) {
245+
return swift::DeclBaseName::getFromOpaquePointer(P);
246+
}
247+
enum { NumLowBitsAvailable = PointerLikeTypeTraits<swift::Identifier>::NumLowBitsAvailable };
248+
};
249+
250+
} // end namespace llvm
251+
252+
namespace swift {
253+
207254
/// A declaration name, which may comprise one or more identifier pieces.
208255
class DeclName {
209256
friend class ASTContext;
@@ -214,12 +261,11 @@ class DeclName {
214261
friend TrailingObjects;
215262
friend class DeclName;
216263

217-
Identifier BaseName;
264+
DeclBaseName BaseName;
218265
size_t NumArgs;
219-
220-
explicit CompoundDeclName(Identifier BaseName, size_t NumArgs)
221-
: BaseName(BaseName), NumArgs(NumArgs)
222-
{
266+
267+
explicit CompoundDeclName(DeclBaseName BaseName, size_t NumArgs)
268+
: BaseName(BaseName), NumArgs(NumArgs) {
223269
assert(NumArgs > 0 && "Should use IdentifierAndCompound");
224270
}
225271

@@ -231,56 +277,61 @@ class DeclName {
231277
}
232278

233279
/// Uniquing for the ASTContext.
234-
static void Profile(llvm::FoldingSetNodeID &id,
235-
Identifier baseName,
280+
static void Profile(llvm::FoldingSetNodeID &id, DeclBaseName baseName,
236281
ArrayRef<Identifier> argumentNames);
237-
282+
238283
void Profile(llvm::FoldingSetNodeID &id) {
239284
Profile(id, BaseName, getArgumentNames());
240285
}
241286
};
242287

243288
// A single stored identifier, along with a bit stating whether it is the
244289
// base name for a zero-argument compound name.
245-
typedef llvm::PointerIntPair<Identifier, 1, bool> IdentifierAndCompound;
290+
typedef llvm::PointerIntPair<DeclBaseName, 1, bool> BaseNameAndCompound;
246291

247292
// Either a single identifier piece stored inline (with a bit to say whether
248293
// it is simple or compound), or a reference to a compound declaration name.
249-
llvm::PointerUnion<IdentifierAndCompound, CompoundDeclName*> SimpleOrCompound;
250-
294+
llvm::PointerUnion<BaseNameAndCompound, CompoundDeclName *> SimpleOrCompound;
295+
251296
DeclName(void *Opaque)
252297
: SimpleOrCompound(decltype(SimpleOrCompound)::getFromOpaqueValue(Opaque))
253298
{}
254299

255-
void initialize(ASTContext &C, Identifier baseName,
300+
void initialize(ASTContext &C, DeclBaseName baseName,
256301
ArrayRef<Identifier> argumentNames);
257-
302+
258303
public:
259304
/// Build a null name.
260-
DeclName() : SimpleOrCompound(IdentifierAndCompound()) {}
261-
305+
DeclName() : SimpleOrCompound(BaseNameAndCompound()) {}
306+
262307
/// Build a simple value name with one component.
308+
/*implicit*/ DeclName(DeclBaseName simpleName)
309+
: SimpleOrCompound(BaseNameAndCompound(simpleName, false)) {}
310+
263311
/*implicit*/ DeclName(Identifier simpleName)
264-
: SimpleOrCompound(IdentifierAndCompound(simpleName, false)) {}
265-
312+
: DeclName(DeclBaseName(simpleName)) {}
313+
266314
/// Build a compound value name given a base name and a set of argument names.
267-
DeclName(ASTContext &C, Identifier baseName,
315+
DeclName(ASTContext &C, DeclBaseName baseName,
268316
ArrayRef<Identifier> argumentNames) {
269317
initialize(C, baseName, argumentNames);
270318
}
271319

272320
/// Build a compound value name given a base name and a set of argument names
273321
/// extracted from a parameter list.
274-
DeclName(ASTContext &C, Identifier baseName, ParameterList *paramList);
275-
322+
DeclName(ASTContext &C, DeclBaseName baseName, ParameterList *paramList);
323+
276324
/// Retrieve the 'base' name, i.e., the name that follows the introducer,
277325
/// such as the 'foo' in 'func foo(x:Int, y:Int)' or the 'bar' in
278326
/// 'var bar: Int'.
327+
// TODO: Return DeclBaseName (remove two calls to getIdentifier)
279328
Identifier getBaseName() const {
280329
if (auto compound = SimpleOrCompound.dyn_cast<CompoundDeclName*>())
281-
return compound->BaseName;
282-
283-
return SimpleOrCompound.get<IdentifierAndCompound>().getPointer();
330+
return compound->BaseName.getIdentifier();
331+
332+
return SimpleOrCompound.get<BaseNameAndCompound>()
333+
.getPointer()
334+
.getIdentifier();
284335
}
285336

286337
/// Retrieve the names of the arguments, if there are any.
@@ -294,23 +345,23 @@ class DeclName {
294345
explicit operator bool() const {
295346
if (SimpleOrCompound.dyn_cast<CompoundDeclName*>())
296347
return true;
297-
return !SimpleOrCompound.get<IdentifierAndCompound>().getPointer().empty();
348+
return !SimpleOrCompound.get<BaseNameAndCompound>().getPointer().empty();
298349
}
299350

300351
/// True if this is a simple one-component name.
301352
bool isSimpleName() const {
302353
if (SimpleOrCompound.dyn_cast<CompoundDeclName*>())
303354
return false;
304355

305-
return !SimpleOrCompound.get<IdentifierAndCompound>().getInt();
356+
return !SimpleOrCompound.get<BaseNameAndCompound>().getInt();
306357
}
307358

308359
/// True if this is a compound name.
309360
bool isCompoundName() const {
310361
if (SimpleOrCompound.dyn_cast<CompoundDeclName*>())
311362
return true;
312363

313-
return SimpleOrCompound.get<IdentifierAndCompound>().getInt();
364+
return SimpleOrCompound.get<BaseNameAndCompound>().getInt();
314365
}
315366

316367
/// True if this name is a simple one-component name identical to the

lib/AST/ASTContext.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3527,18 +3527,18 @@ GenericEnvironment *GenericEnvironment::getIncomplete(
35273527
}
35283528

35293529
void DeclName::CompoundDeclName::Profile(llvm::FoldingSetNodeID &id,
3530-
Identifier baseName,
3530+
DeclBaseName baseName,
35313531
ArrayRef<Identifier> argumentNames) {
3532-
id.AddPointer(baseName.get());
3532+
id.AddPointer(baseName.getAsOpaquePointer());
35333533
id.AddInteger(argumentNames.size());
35343534
for (auto arg : argumentNames)
35353535
id.AddPointer(arg.get());
35363536
}
35373537

3538-
void DeclName::initialize(ASTContext &C, Identifier baseName,
3538+
void DeclName::initialize(ASTContext &C, DeclBaseName baseName,
35393539
ArrayRef<Identifier> argumentNames) {
35403540
if (argumentNames.size() == 0) {
3541-
SimpleOrCompound = IdentifierAndCompound(baseName, true);
3541+
SimpleOrCompound = BaseNameAndCompound(baseName, true);
35423542
return;
35433543
}
35443544

@@ -3564,7 +3564,7 @@ void DeclName::initialize(ASTContext &C, Identifier baseName,
35643564

35653565
/// Build a compound value name given a base name and a set of argument names
35663566
/// extracted from a parameter list.
3567-
DeclName::DeclName(ASTContext &C, Identifier baseName,
3567+
DeclName::DeclName(ASTContext &C, DeclBaseName baseName,
35683568
ParameterList *paramList) {
35693569
SmallVector<Identifier, 4> names;
35703570

0 commit comments

Comments
 (0)