Browse Source

Set type for nested InitListExpr if it match required type. (#252)

Xiang Li 8 years ago
parent
commit
cf4ef478fa

+ 6 - 0
tools/clang/lib/CodeGen/CGHLSLMS.cpp

@@ -4479,6 +4479,9 @@ static bool ExpTypeMatch(Expr *E, QualType Ty, ASTContext &Ctx, CodeGenTypes &Ty
           break;
       }
       bMatch &= i == NumInitElements;
+      if (bMatch && initList->getType()->isVoidType()) {
+        initList->setType(Ty);
+      }
       return bMatch;
     } else if (Ty->isArrayType() && !Ty->isIncompleteArrayType()) {
       const ConstantArrayType *AT = Ctx.getAsConstantArrayType(Ty);
@@ -4495,6 +4498,9 @@ static bool ExpTypeMatch(Expr *E, QualType Ty, ASTContext &Ctx, CodeGenTypes &Ty
         if (!bMatch)
           break;
       }
+      if (bMatch && initList->getType()->isVoidType()) {
+        initList->setType(Ty);
+      }
       return bMatch;
     } else {
       return false;

+ 12 - 0
tools/clang/test/CodeGenHLSL/initlist_type.hlsl

@@ -0,0 +1,12 @@
+// RUN: %dxc -E main -T ps_6_0 %s
+
+float4 main(int4 b:B) : SV_TARGET
+{
+	float m[2][2] =
+	{
+		{ -1., 3.2 },
+		{ 6.2, 0.1 }
+	};
+
+  return m[b.x][b.y];
+}

+ 5 - 0
tools/clang/unittests/HLSL/CompilerTest.cpp

@@ -397,6 +397,7 @@ public:
   TEST_METHOD(CodeGenIndexabletemp1)
   TEST_METHOD(CodeGenIndexabletemp2)
   TEST_METHOD(CodeGenIndexabletemp3)
+  TEST_METHOD(CodeGenInitListType)
   TEST_METHOD(CodeGenInoutSE)
   TEST_METHOD(CodeGenInout1)
   TEST_METHOD(CodeGenInout2)
@@ -2482,6 +2483,10 @@ TEST_F(CompilerTest, CodeGenIndexabletemp3) {
   CodeGenTest(L"..\\CodeGenHLSL\\indexabletemp3.hlsl");
 }
 
+TEST_F(CompilerTest, CodeGenInitListType) {
+  CodeGenTestCheck(L"..\\CodeGenHLSL\\initlist_type.hlsl");
+}
+
 TEST_F(CompilerTest, CodeGenInoutSE) {
   CodeGenTestCheck(L"..\\CodeGenHLSL\\inout_se.hlsl");
 }