Skip to content

Commit 77abb6b

Browse files
committed
first self review
1 parent 276fca4 commit 77abb6b

File tree

3 files changed

+26
-34
lines changed

3 files changed

+26
-34
lines changed

clang/lib/AST/ExprConstant.cpp

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3843,9 +3843,8 @@ static bool handleScalarCast(EvalInfo &Info, const FPOptions FPO, const Expr *E,
38433843
if (!HandleConversionToBool(Original, BoolResult))
38443844
return false;
38453845
uint64_t IntResult = BoolResult;
3846-
Result = APValue(Info.Ctx.MakeIntValue(IntResult, DestTy));
3847-
// TODO destty is wrong here if destty is float....
3848-
// can we use sourcety here?
3846+
Result = APValue(Info.Ctx.MakeIntValue(
3847+
IntResult, Info.Ctx.getIntTypeForBitwidth(64, true)));
38493848
}
38503849
if (DestTy->isFloatingType()) {
38513850
APValue Result2 = APValue(APFloat(0.0));
@@ -3897,8 +3896,6 @@ static bool handleScalarCast(EvalInfo &Info, const FPOptions FPO, const Expr *E,
38973896
}
38983897
}
38993898

3900-
// Info.FFDiag(E, diag::err_convertvector_constexpr_unsupported_vector_cast)
3901-
// << SourceTy << DestTy;
39023899
return false;
39033900
}
39043901

@@ -3917,7 +3914,7 @@ static bool constructAggregate(EvalInfo &Info, const FPOptions FPO,
39173914
while (!WorkList.empty() && ElI < Elements.size()) {
39183915
auto [Res, Type, BitWidth] = WorkList.pop_back_val();
39193916

3920-
if (Type->isRealFloatingType() || Type->isBooleanType()) {
3917+
if (Type->isRealFloatingType()) {
39213918
if (!handleScalarCast(Info, FPO, E, ElTypes[ElI], Type, Elements[ElI],
39223919
*Res))
39233920
return false;
@@ -3978,10 +3975,10 @@ static bool constructAggregate(EvalInfo &Info, const FPOptions FPO,
39783975
// we need to traverse backwards
39793976
// Visit the base classes.
39803977
if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
3981-
// todo assert there is only 1 base at most
3982-
for (size_t I = 0, E = CXXRD->getNumBases(); I != E; ++I) {
3983-
const CXXBaseSpecifier &BS = CXXRD->bases_begin()[I];
3984-
ReverseList.emplace_back(&Res->getStructBase(I), BS.getType(), 0u);
3978+
if (CXXRD->getNumBases() > 0) {
3979+
assert(CXXRD->getNumBases() == 1);
3980+
const CXXBaseSpecifier &BS = CXXRD->bases_begin()[0];
3981+
ReverseList.emplace_back(&Res->getStructBase(0), BS.getType(), 0u);
39853982
}
39863983
}
39873984

@@ -4057,13 +4054,12 @@ static unsigned elementwiseSize(EvalInfo &Info, QualType BaseTy) {
40574054
}
40584055
if (Type->isRecordType()) {
40594056
const RecordDecl *RD = Type->getAsRecordDecl();
4060-
// const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD);
40614057

40624058
// Visit the base classes.
40634059
if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
4064-
// todo assert there is only 1 base at most
4065-
for (size_t I = 0, E = CXXRD->getNumBases(); I != E; ++I) {
4066-
const CXXBaseSpecifier &BS = CXXRD->bases_begin()[I];
4060+
if (CXXRD->getNumBases() > 0) {
4061+
assert(CXXRD->getNumBases() == 1);
4062+
const CXXBaseSpecifier &BS = CXXRD->bases_begin()[0];
40674063
WorkList.push_back(BS.getType());
40684064
}
40694065
}
@@ -4126,7 +4122,6 @@ static bool flattenAPValue(const ASTContext &Ctx, APValue Value,
41264122
for (FieldDecl *FD : RD->fields()) {
41274123
if (FD->isUnnamedBitField())
41284124
continue;
4129-
// if (FD->isBitField()) {
41304125
ReverseList.emplace_back(Work.getStructField(FD->getFieldIndex()),
41314126
FD->getType());
41324127
}

clang/test/SemaHLSL/Types/BuiltinVector/TruncationConstantExpr.hlsl

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,8 @@
66
// good way to constexpr `any` for bool vector conditions, and the condition for
77
// _Static_assert must be an integral constant.
88

9-
struct S {
10-
int3 A;
11-
float B;
12-
};
13-
149
export void fn() {
1510

16-
_Static_assert(((float4)(int[6]){1,2,3,4,5,6}).x == 1.0, "Woo!");
17-
18-
// This compiling successfully verifies that the array constant expression
19-
// gets truncated to a float at compile time for instantiation via the
20-
// flat cast
21-
_Static_assert(((int)(int[2]){1,2}) == 1, "Woo!");
22-
23-
// This compiling successfully verifies that the struct constant expression
24-
// gets truncated to an integer at compile time for instatiation via the
25-
// flat cast
26-
_Static_assert(((int)(S){{1,2,3},1.0}) == 1, "Woo!");
27-
28-
_Static_assert(((float)(float[2]){1.0,2.0}) == 1.0, "Woo!");
29-
3011
// This compiling successfully verifies that the vector constant expression
3112
// gets truncated to an integer at compile time for instantiation.
3213
_Static_assert(((int)1.xxxx) + 0 == 1, "Woo!");

clang/test/SemaHLSL/Types/ElementwiseCastConstantExpr.hlsl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ struct B2 : B1 {
2727
};
2828

2929
export void fn() {
30+
/*
31+
_Static_assert(((float4)(int[6]){1,2,3,4,5,6}).x == 1.0, "Woo!");
32+
33+
// This compiling successfully verifies that the array constant expression
34+
// gets truncated to a float at compile time for instantiation via the
35+
// flat cast
36+
_Static_assert(((int)(int[2]){1,2}) == 1, "Woo!");
3037
3138
// truncation tests
3239
// result type int
@@ -73,4 +80,13 @@ export void fn() {
7380
_Static_assert(SB.U[1] == 1000, "Woo!");
7481
_Static_assert(SB.I == 8, "Woo!");
7582
_Static_assert(SB.I2 == 0, "Woo!");
83+
*/
84+
// Make sure we read bitfields correctly
85+
constexpr Base BB = {222.22, {100, 200}, -2, 7};
86+
constexpr int Arr[5] = (int[5])BB;
87+
_Static_assert(Arr[0] == 222, "Woo!");
88+
_Static_assert(Arr[1] == 100, "Woo!");
89+
_Static_assert(Arr[2] == 200, "Woo!");
90+
_Static_assert(Arr[3] == -2, "Woo!");
91+
_Static_assert(Arr[4] == 7, "Woo!");
7692
}

0 commit comments

Comments
 (0)