Skip to content

Commit d52cc9d

Browse files
authored
[Clang][AST][NFC] Store template parameter position for TemplateTypeParmType in TypeBit (#102481)
`TemplateTypeParmType` currently stores the depth, index, and whether a template type parameter is a pack in a union of `CanonicalTTPTInfo` and `TemplateTypeParmDecl*`, and only the canonical type stores the position information. These bits can be stored for all `TemplateTypeParmTypes` in `TypeBits` to avoid unnecessary indirection when accessing the position information.
1 parent 0bc6407 commit d52cc9d

File tree

2 files changed

+37
-41
lines changed

2 files changed

+37
-41
lines changed

clang/include/clang/AST/Type.h

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2134,6 +2134,23 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
21342134
unsigned hasTypeDifferentFromDecl : 1;
21352135
};
21362136

2137+
class TemplateTypeParmTypeBitfields {
2138+
friend class TemplateTypeParmType;
2139+
2140+
LLVM_PREFERRED_TYPE(TypeBitfields)
2141+
unsigned : NumTypeBits;
2142+
2143+
/// The depth of the template parameter.
2144+
unsigned Depth : 15;
2145+
2146+
/// Whether this is a template parameter pack.
2147+
LLVM_PREFERRED_TYPE(bool)
2148+
unsigned ParameterPack : 1;
2149+
2150+
/// The index of the template parameter.
2151+
unsigned Index : 16;
2152+
};
2153+
21372154
class SubstTemplateTypeParmTypeBitfields {
21382155
friend class SubstTemplateTypeParmType;
21392156

@@ -2257,6 +2274,7 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
22572274
TypeWithKeywordBitfields TypeWithKeywordBits;
22582275
ElaboratedTypeBitfields ElaboratedTypeBits;
22592276
VectorTypeBitfields VectorTypeBits;
2277+
TemplateTypeParmTypeBitfields TemplateTypeParmTypeBits;
22602278
SubstTemplateTypeParmTypeBitfields SubstTemplateTypeParmTypeBits;
22612279
SubstTemplateTypeParmPackTypeBitfields SubstTemplateTypeParmPackTypeBits;
22622280
TemplateSpecializationTypeBitfields TemplateSpecializationTypeBits;
@@ -6135,52 +6153,30 @@ class BTFTagAttributedType : public Type, public llvm::FoldingSetNode {
61356153
class TemplateTypeParmType : public Type, public llvm::FoldingSetNode {
61366154
friend class ASTContext; // ASTContext creates these
61376155

6138-
// Helper data collector for canonical types.
6139-
struct CanonicalTTPTInfo {
6140-
unsigned Depth : 15;
6141-
unsigned ParameterPack : 1;
6142-
unsigned Index : 16;
6143-
};
6144-
6145-
union {
6146-
// Info for the canonical type.
6147-
CanonicalTTPTInfo CanTTPTInfo;
6148-
6149-
// Info for the non-canonical type.
6150-
TemplateTypeParmDecl *TTPDecl;
6151-
};
6156+
// The associated TemplateTypeParmDecl for the non-canonical type.
6157+
TemplateTypeParmDecl *TTPDecl;
61526158

6153-
/// Build a non-canonical type.
6154-
TemplateTypeParmType(TemplateTypeParmDecl *TTPDecl, QualType Canon)
6159+
TemplateTypeParmType(unsigned D, unsigned I, bool PP,
6160+
TemplateTypeParmDecl *TTPDecl, QualType Canon)
61556161
: Type(TemplateTypeParm, Canon,
61566162
TypeDependence::DependentInstantiation |
6157-
(Canon->getDependence() & TypeDependence::UnexpandedPack)),
6158-
TTPDecl(TTPDecl) {}
6159-
6160-
/// Build the canonical type.
6161-
TemplateTypeParmType(unsigned D, unsigned I, bool PP)
6162-
: Type(TemplateTypeParm, QualType(this, 0),
6163-
TypeDependence::DependentInstantiation |
6164-
(PP ? TypeDependence::UnexpandedPack : TypeDependence::None)) {
6165-
CanTTPTInfo.Depth = D;
6166-
CanTTPTInfo.Index = I;
6167-
CanTTPTInfo.ParameterPack = PP;
6168-
}
6169-
6170-
const CanonicalTTPTInfo& getCanTTPTInfo() const {
6171-
QualType Can = getCanonicalTypeInternal();
6172-
return Can->castAs<TemplateTypeParmType>()->CanTTPTInfo;
6163+
(PP ? TypeDependence::UnexpandedPack : TypeDependence::None)),
6164+
TTPDecl(TTPDecl) {
6165+
assert(!TTPDecl == Canon.isNull());
6166+
TemplateTypeParmTypeBits.Depth = D;
6167+
TemplateTypeParmTypeBits.Index = I;
6168+
TemplateTypeParmTypeBits.ParameterPack = PP;
61736169
}
61746170

61756171
public:
6176-
unsigned getDepth() const { return getCanTTPTInfo().Depth; }
6177-
unsigned getIndex() const { return getCanTTPTInfo().Index; }
6178-
bool isParameterPack() const { return getCanTTPTInfo().ParameterPack; }
6179-
6180-
TemplateTypeParmDecl *getDecl() const {
6181-
return isCanonicalUnqualified() ? nullptr : TTPDecl;
6172+
unsigned getDepth() const { return TemplateTypeParmTypeBits.Depth; }
6173+
unsigned getIndex() const { return TemplateTypeParmTypeBits.Index; }
6174+
bool isParameterPack() const {
6175+
return TemplateTypeParmTypeBits.ParameterPack;
61826176
}
61836177

6178+
TemplateTypeParmDecl *getDecl() const { return TTPDecl; }
6179+
61846180
IdentifierInfo *getIdentifier() const;
61856181

61866182
bool isSugared() const { return false; }

clang/lib/AST/ASTContext.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5300,15 +5300,15 @@ QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
53005300
if (TTPDecl) {
53015301
QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
53025302
TypeParm = new (*this, alignof(TemplateTypeParmType))
5303-
TemplateTypeParmType(TTPDecl, Canon);
5303+
TemplateTypeParmType(Depth, Index, ParameterPack, TTPDecl, Canon);
53045304

53055305
TemplateTypeParmType *TypeCheck
53065306
= TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
53075307
assert(!TypeCheck && "Template type parameter canonical type broken");
53085308
(void)TypeCheck;
53095309
} else
5310-
TypeParm = new (*this, alignof(TemplateTypeParmType))
5311-
TemplateTypeParmType(Depth, Index, ParameterPack);
5310+
TypeParm = new (*this, alignof(TemplateTypeParmType)) TemplateTypeParmType(
5311+
Depth, Index, ParameterPack, /*TTPDecl=*/nullptr, /*Canon=*/QualType());
53125312

53135313
Types.push_back(TypeParm);
53145314
TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);

0 commit comments

Comments
 (0)