Skip to content

Commit c678b96

Browse files
committed
Change the backing base name type of DeclName to DeclBaseName
The new DeclBaseName type will later abstract over normal Identifiers as they exist now and special names that don't have an identifier in the surface language like subscripts
1 parent d7892e8 commit c678b96

File tree

2 files changed

+85
-33
lines changed

2 files changed

+85
-33
lines changed

include/swift/AST/Identifier.h

Lines changed: 80 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,55 @@ 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+
// TODO: Check if P is a special name
230+
return Identifier::getFromOpaquePointer(P);
231+
}
232+
};
233+
234+
} // end namespace swift
235+
236+
namespace llvm {
237+
238+
// A DeclBaseName is "pointer like".
239+
template <typename T> class PointerLikeTypeTraits;
240+
template <> class PointerLikeTypeTraits<swift::DeclBaseName> {
241+
public:
242+
static inline void *getAsVoidPointer(swift::DeclBaseName D) {
243+
return const_cast<void *>(D.getAsOpaquePointer());
244+
}
245+
static inline swift::DeclBaseName getFromVoidPointer(void *P) {
246+
return swift::Identifier::getFromOpaquePointer(P);
247+
}
248+
enum { NumLowBitsAvailable = 2 };
249+
};
250+
251+
} // end namespace llvm
252+
253+
namespace swift {
254+
207255
/// A declaration name, which may comprise one or more identifier pieces.
208256
class DeclName {
209257
friend class ASTContext;
@@ -214,12 +262,11 @@ class DeclName {
214262
friend TrailingObjects;
215263
friend class DeclName;
216264

217-
Identifier BaseName;
265+
DeclBaseName BaseName;
218266
size_t NumArgs;
219-
220-
explicit CompoundDeclName(Identifier BaseName, size_t NumArgs)
221-
: BaseName(BaseName), NumArgs(NumArgs)
222-
{
267+
268+
explicit CompoundDeclName(DeclBaseName BaseName, size_t NumArgs)
269+
: BaseName(BaseName), NumArgs(NumArgs) {
223270
assert(NumArgs > 0 && "Should use IdentifierAndCompound");
224271
}
225272

@@ -231,56 +278,61 @@ class DeclName {
231278
}
232279

233280
/// Uniquing for the ASTContext.
234-
static void Profile(llvm::FoldingSetNodeID &id,
235-
Identifier baseName,
281+
static void Profile(llvm::FoldingSetNodeID &id, DeclBaseName baseName,
236282
ArrayRef<Identifier> argumentNames);
237-
283+
238284
void Profile(llvm::FoldingSetNodeID &id) {
239285
Profile(id, BaseName, getArgumentNames());
240286
}
241287
};
242288

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

247293
// Either a single identifier piece stored inline (with a bit to say whether
248294
// it is simple or compound), or a reference to a compound declaration name.
249-
llvm::PointerUnion<IdentifierAndCompound, CompoundDeclName*> SimpleOrCompound;
250-
295+
llvm::PointerUnion<BaseNameAndCompound, CompoundDeclName *> SimpleOrCompound;
296+
251297
DeclName(void *Opaque)
252298
: SimpleOrCompound(decltype(SimpleOrCompound)::getFromOpaqueValue(Opaque))
253299
{}
254300

255-
void initialize(ASTContext &C, Identifier baseName,
301+
void initialize(ASTContext &C, DeclBaseName baseName,
256302
ArrayRef<Identifier> argumentNames);
257-
303+
258304
public:
259305
/// Build a null name.
260-
DeclName() : SimpleOrCompound(IdentifierAndCompound()) {}
261-
306+
DeclName() : SimpleOrCompound(BaseNameAndCompound()) {}
307+
262308
/// Build a simple value name with one component.
309+
/*implicit*/ DeclName(DeclBaseName simpleName)
310+
: SimpleOrCompound(BaseNameAndCompound(simpleName, false)) {}
311+
263312
/*implicit*/ DeclName(Identifier simpleName)
264-
: SimpleOrCompound(IdentifierAndCompound(simpleName, false)) {}
265-
313+
: DeclName(DeclBaseName(simpleName)) {}
314+
266315
/// Build a compound value name given a base name and a set of argument names.
267-
DeclName(ASTContext &C, Identifier baseName,
316+
DeclName(ASTContext &C, DeclBaseName baseName,
268317
ArrayRef<Identifier> argumentNames) {
269318
initialize(C, baseName, argumentNames);
270319
}
271320

272321
/// Build a compound value name given a base name and a set of argument names
273322
/// extracted from a parameter list.
274-
DeclName(ASTContext &C, Identifier baseName, ParameterList *paramList);
275-
323+
DeclName(ASTContext &C, DeclBaseName baseName, ParameterList *paramList);
324+
276325
/// Retrieve the 'base' name, i.e., the name that follows the introducer,
277326
/// such as the 'foo' in 'func foo(x:Int, y:Int)' or the 'bar' in
278327
/// 'var bar: Int'.
328+
// TODO: Return DeclBaseName (remove two calls to getIdentifier)
279329
Identifier getBaseName() const {
280330
if (auto compound = SimpleOrCompound.dyn_cast<CompoundDeclName*>())
281-
return compound->BaseName;
282-
283-
return SimpleOrCompound.get<IdentifierAndCompound>().getPointer();
331+
return compound->BaseName.getIdentifier();
332+
333+
return SimpleOrCompound.get<BaseNameAndCompound>()
334+
.getPointer()
335+
.getIdentifier();
284336
}
285337

286338
/// Retrieve the names of the arguments, if there are any.
@@ -294,23 +346,23 @@ class DeclName {
294346
explicit operator bool() const {
295347
if (SimpleOrCompound.dyn_cast<CompoundDeclName*>())
296348
return true;
297-
return !SimpleOrCompound.get<IdentifierAndCompound>().getPointer().empty();
349+
return !SimpleOrCompound.get<BaseNameAndCompound>().getPointer().empty();
298350
}
299351

300352
/// True if this is a simple one-component name.
301353
bool isSimpleName() const {
302354
if (SimpleOrCompound.dyn_cast<CompoundDeclName*>())
303355
return false;
304356

305-
return !SimpleOrCompound.get<IdentifierAndCompound>().getInt();
357+
return !SimpleOrCompound.get<BaseNameAndCompound>().getInt();
306358
}
307359

308360
/// True if this is a compound name.
309361
bool isCompoundName() const {
310362
if (SimpleOrCompound.dyn_cast<CompoundDeclName*>())
311363
return true;
312364

313-
return SimpleOrCompound.get<IdentifierAndCompound>().getInt();
365+
return SimpleOrCompound.get<BaseNameAndCompound>().getInt();
314366
}
315367

316368
/// 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
@@ -3539,18 +3539,18 @@ GenericEnvironment *GenericEnvironment::getIncomplete(
35393539
}
35403540

35413541
void DeclName::CompoundDeclName::Profile(llvm::FoldingSetNodeID &id,
3542-
Identifier baseName,
3542+
DeclBaseName baseName,
35433543
ArrayRef<Identifier> argumentNames) {
3544-
id.AddPointer(baseName.get());
3544+
id.AddPointer(baseName.getAsOpaquePointer());
35453545
id.AddInteger(argumentNames.size());
35463546
for (auto arg : argumentNames)
35473547
id.AddPointer(arg.get());
35483548
}
35493549

3550-
void DeclName::initialize(ASTContext &C, Identifier baseName,
3550+
void DeclName::initialize(ASTContext &C, DeclBaseName baseName,
35513551
ArrayRef<Identifier> argumentNames) {
35523552
if (argumentNames.size() == 0) {
3553-
SimpleOrCompound = IdentifierAndCompound(baseName, true);
3553+
SimpleOrCompound = BaseNameAndCompound(baseName, true);
35543554
return;
35553555
}
35563556

@@ -3576,7 +3576,7 @@ void DeclName::initialize(ASTContext &C, Identifier baseName,
35763576

35773577
/// Build a compound value name given a base name and a set of argument names
35783578
/// extracted from a parameter list.
3579-
DeclName::DeclName(ASTContext &C, Identifier baseName,
3579+
DeclName::DeclName(ASTContext &C, DeclBaseName baseName,
35803580
ParameterList *paramList) {
35813581
SmallVector<Identifier, 4> names;
35823582

0 commit comments

Comments
 (0)