Skip to content

Commit 36de2d6

Browse files
committed
[NFC] [AST] Reduce the size of TemplateParmPosition
I found this when reading the codes. I think it makes sense to reduce the space for TemplateParmPosition. It is hard to image the depth of template parameter is larger than 2^20 and the index is larger than 2^12. So I think the patch might be reasonable. Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D123298
1 parent b84673b commit 36de2d6

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

clang/include/clang/AST/DeclTemplate.h

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,23 +1148,40 @@ class FunctionTemplateDecl : public RedeclarableTemplateDecl {
11481148
/// parameters and is not part of the Decl hierarchy. Just a facility.
11491149
class TemplateParmPosition {
11501150
protected:
1151-
// FIXME: These probably don't need to be ints. int:5 for depth, int:8 for
1152-
// position? Maybe?
1153-
unsigned Depth;
1154-
unsigned Position;
1151+
enum { DepthWidth = 20, PositionWidth = 12 };
1152+
unsigned Depth : DepthWidth;
1153+
unsigned Position : PositionWidth;
11551154

1156-
TemplateParmPosition(unsigned D, unsigned P) : Depth(D), Position(P) {}
1155+
static constexpr unsigned MaxDepth = (1U << DepthWidth) - 1;
1156+
static constexpr unsigned MaxPosition = (1U << PositionWidth) - 1;
1157+
1158+
TemplateParmPosition(unsigned D, unsigned P) : Depth(D), Position(P) {
1159+
// The input may fill maximum values to show that it is invalid.
1160+
// Add one here to convert it to zero.
1161+
assert((D + 1) <= MaxDepth &&
1162+
"The depth of template parmeter position is more than 2^20!");
1163+
assert((P + 1) <= MaxPosition &&
1164+
"The position of template parmeter position is more than 2^12!");
1165+
}
11571166

11581167
public:
11591168
TemplateParmPosition() = delete;
11601169

11611170
/// Get the nesting depth of the template parameter.
11621171
unsigned getDepth() const { return Depth; }
1163-
void setDepth(unsigned D) { Depth = D; }
1172+
void setDepth(unsigned D) {
1173+
assert((D + 1) <= MaxDepth &&
1174+
"The depth of template parmeter position is more than 2^20!");
1175+
Depth = D;
1176+
}
11641177

11651178
/// Get the position of the template parameter within its parameter list.
11661179
unsigned getPosition() const { return Position; }
1167-
void setPosition(unsigned P) { Position = P; }
1180+
void setPosition(unsigned P) {
1181+
assert((P + 1) <= MaxPosition &&
1182+
"The position of template parmeter position is more than 2^12!");
1183+
Position = P;
1184+
}
11681185

11691186
/// Get the index of the template parameter within its parameter list.
11701187
unsigned getIndex() const { return Position; }

0 commit comments

Comments
 (0)