Skip to content

Commit bab1239

Browse files
authored
Merge pull request swiftlang#28866 from brentdax/raiding-the-compound
[NFC] Use CompoundDeclName for zero-arg names
2 parents 576df04 + d0e9578 commit bab1239

File tree

2 files changed

+19
-36
lines changed

2 files changed

+19
-36
lines changed

include/swift/AST/Identifier.h

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -402,9 +402,7 @@ class DeclName {
402402
size_t NumArgs;
403403

404404
explicit CompoundDeclName(DeclBaseName BaseName, size_t NumArgs)
405-
: BaseName(BaseName), NumArgs(NumArgs) {
406-
assert(NumArgs > 0 && "Should use IdentifierAndCompound");
407-
}
405+
: BaseName(BaseName), NumArgs(NumArgs) { }
408406

409407
ArrayRef<Identifier> getArgumentNames() const {
410408
return {getTrailingObjects<Identifier>(), NumArgs};
@@ -422,28 +420,24 @@ class DeclName {
422420
}
423421
};
424422

425-
// A single stored identifier, along with a bit stating whether it is the
426-
// base name for a zero-argument compound name.
427-
typedef llvm::PointerIntPair<DeclBaseName, 1, bool> BaseNameAndCompound;
428-
429-
// Either a single identifier piece stored inline (with a bit to say whether
430-
// it is simple or compound), or a reference to a compound declaration name.
431-
llvm::PointerUnion<BaseNameAndCompound, CompoundDeclName *> SimpleOrCompound;
423+
/// Either a single identifier piece stored inline, or a reference to a
424+
/// compound declaration name.
425+
llvm::PointerUnion<DeclBaseName, CompoundDeclName *> BaseNameOrCompound;
432426

433427
explicit DeclName(void *Opaque)
434-
: SimpleOrCompound(decltype(SimpleOrCompound)::getFromOpaqueValue(Opaque))
428+
: BaseNameOrCompound(decltype(BaseNameOrCompound)::getFromOpaqueValue(Opaque))
435429
{}
436430

437431
void initialize(ASTContext &C, DeclBaseName baseName,
438432
ArrayRef<Identifier> argumentNames);
439433

440434
public:
441435
/// Build a null name.
442-
DeclName() : SimpleOrCompound(BaseNameAndCompound()) {}
436+
DeclName() : BaseNameOrCompound(DeclBaseName()) {}
443437

444438
/// Build a simple value name with one component.
445439
/*implicit*/ DeclName(DeclBaseName simpleName)
446-
: SimpleOrCompound(BaseNameAndCompound(simpleName, false)) {}
440+
: BaseNameOrCompound(simpleName) {}
447441

448442
/*implicit*/ DeclName(Identifier simpleName)
449443
: DeclName(DeclBaseName(simpleName)) {}
@@ -462,10 +456,10 @@ class DeclName {
462456
/// such as the 'foo' in 'func foo(x:Int, y:Int)' or the 'bar' in
463457
/// 'var bar: Int'.
464458
DeclBaseName getBaseName() const {
465-
if (auto compound = SimpleOrCompound.dyn_cast<CompoundDeclName*>())
459+
if (auto compound = BaseNameOrCompound.dyn_cast<CompoundDeclName*>())
466460
return compound->BaseName;
467461

468-
return SimpleOrCompound.get<BaseNameAndCompound>().getPointer();
462+
return BaseNameOrCompound.get<DeclBaseName>();
469463
}
470464

471465
/// Assert that the base name is not special and return its identifier.
@@ -478,7 +472,7 @@ class DeclName {
478472

479473
/// Retrieve the names of the arguments, if there are any.
480474
ArrayRef<Identifier> getArgumentNames() const {
481-
if (auto compound = SimpleOrCompound.dyn_cast<CompoundDeclName*>())
475+
if (auto compound = BaseNameOrCompound.dyn_cast<CompoundDeclName*>())
482476
return compound->getArgumentNames();
483477

484478
return { };
@@ -487,25 +481,19 @@ class DeclName {
487481
bool isSpecial() const { return getBaseName().isSpecial(); }
488482

489483
explicit operator bool() const {
490-
if (SimpleOrCompound.dyn_cast<CompoundDeclName*>())
484+
if (BaseNameOrCompound.dyn_cast<CompoundDeclName*>())
491485
return true;
492-
return !SimpleOrCompound.get<BaseNameAndCompound>().getPointer().empty();
486+
return !BaseNameOrCompound.get<DeclBaseName>().empty();
493487
}
494488

495489
/// True if this is a simple one-component name.
496490
bool isSimpleName() const {
497-
if (SimpleOrCompound.dyn_cast<CompoundDeclName*>())
498-
return false;
499-
500-
return !SimpleOrCompound.get<BaseNameAndCompound>().getInt();
491+
return BaseNameOrCompound.is<DeclBaseName>();
501492
}
502493

503494
/// True if this is a compound name.
504495
bool isCompoundName() const {
505-
if (SimpleOrCompound.dyn_cast<CompoundDeclName*>())
506-
return true;
507-
508-
return SimpleOrCompound.get<BaseNameAndCompound>().getInt();
496+
return !isSimpleName();
509497
}
510498

511499
/// True if this name is a simple one-component name identical to the
@@ -540,7 +528,7 @@ class DeclName {
540528
/// matches a simple name lookup or when the full compound name matches.
541529
bool matchesRef(DeclName refName) const {
542530
// Identical names always match.
543-
if (SimpleOrCompound == refName.SimpleOrCompound)
531+
if (BaseNameOrCompound == refName.BaseNameOrCompound)
544532
return true;
545533
// If the reference is a simple name, try simple name matching.
546534
if (refName.isSimpleName())
@@ -594,7 +582,7 @@ class DeclName {
594582
return lhs.compare(rhs) >= 0;
595583
}
596584

597-
void *getOpaqueValue() const { return SimpleOrCompound.getOpaqueValue(); }
585+
void *getOpaqueValue() const { return BaseNameOrCompound.getOpaqueValue(); }
598586
static DeclName getFromOpaqueValue(void *p) { return DeclName(p); }
599587

600588
/// Get a string representation of the name,
@@ -913,7 +901,7 @@ namespace llvm {
913901
static inline swift::DeclName getFromVoidPointer(void *ptr) {
914902
return swift::DeclName::getFromOpaqueValue(ptr);
915903
}
916-
enum { NumLowBitsAvailable = PointerLikeTypeTraits<swift::DeclBaseName>::NumLowBitsAvailable - 2 };
904+
enum { NumLowBitsAvailable = PointerLikeTypeTraits<swift::DeclBaseName>::NumLowBitsAvailable - 1 };
917905
};
918906

919907
// DeclNames hash just like pointers.

lib/AST/ASTContext.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3939,18 +3939,13 @@ void DeclName::CompoundDeclName::Profile(llvm::FoldingSetNodeID &id,
39393939

39403940
void DeclName::initialize(ASTContext &C, DeclBaseName baseName,
39413941
ArrayRef<Identifier> argumentNames) {
3942-
if (argumentNames.empty()) {
3943-
SimpleOrCompound = BaseNameAndCompound(baseName, true);
3944-
return;
3945-
}
3946-
39473942
llvm::FoldingSetNodeID id;
39483943
CompoundDeclName::Profile(id, baseName, argumentNames);
39493944

39503945
void *insert = nullptr;
39513946
if (CompoundDeclName *compoundName
39523947
= C.getImpl().CompoundNames.FindNodeOrInsertPos(id, insert)) {
3953-
SimpleOrCompound = compoundName;
3948+
BaseNameOrCompound = compoundName;
39543949
return;
39553950
}
39563951

@@ -3960,7 +3955,7 @@ void DeclName::initialize(ASTContext &C, DeclBaseName baseName,
39603955
auto compoundName = new (buf) CompoundDeclName(baseName,argumentNames.size());
39613956
std::uninitialized_copy(argumentNames.begin(), argumentNames.end(),
39623957
compoundName->getArgumentNames().begin());
3963-
SimpleOrCompound = compoundName;
3958+
BaseNameOrCompound = compoundName;
39643959
C.getImpl().CompoundNames.InsertNode(compoundName, insert);
39653960
}
39663961

0 commit comments

Comments
 (0)