9using namespace shader::parser;
10using namespace shader;
17for (int i = 2; i < 4; i++) [[unroll]] { content += i; })";
27 auto [
output, _, error] = process_test_local(input);
33for (int i = 2; i < 4; i++, y++) [[unroll]] { content += i; })";
35 {int i = 2; { content += i; }
37 i++, y++; { content += i; }
40 auto [
output, _, error] = process_test_local(input);
46for (int i = 2; i < 4 && i < y; i++, y++) [[unroll]] { cont += i; })";
50 if(i < 4 && i < y) { cont += i; }
54 if(i < 4 && i < y) { cont += i; }
57 auto [
output, _, error] = process_test_local(input);
63for (; i < j;) [[unroll_n(2)]] { content += i; })";
68 if(i < j) { content += i; }
70 if(i < j) { content += i; }
73 auto [
output, _, error] = process_test_local(input);
79for (; i < j;) [[unroll_n(2)]] { for (; j < k;) [[unroll_n(2)]] {} })";
103 auto [
output, _, error] = process_test_local(input);
108 string input = R
"(for (; i < j;) [[unroll_n(2)]] { break; })";
109 auto [
output, _, error] = process_test_local(input);
110 EXPECT_EQ(error,
"Unrolled loop cannot contain \"break\" statement.");
113 string input = R
"(for (; i < j;) [[unroll_n(2)]] { continue; })";
114 auto [
output, _, error] = process_test_local(input);
115 EXPECT_EQ(error,
"Unrolled loop cannot contain \"continue\" statement.");
119for (; i < j;) [[unroll_n(2)]] { for (; j < k;) {break;continue;} })";
124 if(i < j) { for (; j < k;) {break;continue;} }
126 if(i < j) { for (; j < k;) {break;continue;} }
129 auto [
output, _, error] = process_test_local(input);
134 string input = R
"(for (int i = 3; i > 2; i++) [[unroll]] {})";
135 auto [
output, _, error] = process_test_local(input);
136 EXPECT_EQ(error,
"Unsupported condition in unrolled loop.");
140for (int a = 0; a < 2; a += 1) [[unroll]] { a; }
141for (uint b = 1; b < 3; ++b ) [[unroll]] { b; }
142for (int c = 2; c > 0; c-- ) [[unroll]] { c; }
143for (int d = 2; d > 0; --d ) [[unroll]] { d; }
144for (int e = 0; e < 4; e += 2) [[unroll]] { e; }
145for (int f = 4; f > 0; f -= 2) [[unroll]] { f; }
146for (int g = 1; g < 4; g *= 2) [[unroll]] { g; }
147for (int h = 4; h > 1; h /= 2) [[unroll]] { h; }
148for (int i = 0; i < 2; i=i+1 ) [[unroll]] { i; }
179 auto [
output, _, error] = process_test_local(input, Language::BSL);
186for (; i < 2; i++) [[unroll]] { i; }
188 auto [
output, _, error] = process_test_local(input, Language::BSL);
189 EXPECT_EQ(error,
"Init statement needs to define the loop variable for unrolled loops");
193for (float i = 0; i < 2; i++) [[unroll]] { i; }
195 auto [
output, _, error] = process_test_local(input, Language::BSL);
196 EXPECT_EQ(error,
"Loop variable needs to be an integer type for unrolled loops");
200for (int i = 0, j = 0; i < 2; i++) [[unroll]] { i; }
202 auto [
output, _, error] = process_test_local(input, Language::BSL);
203 EXPECT_EQ(error,
"Multiple variables declared in unrolled loop");
207for (float i = 0.0; i < 2.0; i += 1.0) [[unroll]] { i; }
209 auto [
output, _, error] = process_test_local(input, Language::BSL);
210 EXPECT_EQ(error,
"Loop variable needs to be an integer type for unrolled loops");
214for (int i; i < 2; i++) [[unroll]] { i; }
216 auto [
output, _, error] = process_test_local(input, Language::BSL);
218 "Loop variable needs to be assigned a value (using assignment) for "
223for (int i = atomicAdd(i, i); i < 2; i++) [[unroll]] { i; }
225 auto [
output, _, error] = process_test_local(input, Language::BSL);
226 EXPECT_EQ(error,
"Non-constant expression in unrolled loop control");
230for (int i = 0; i < 2; i++, i++) [[unroll]] { i; }
232 auto [
output, _, error] = process_test_local(input, Language::BSL);
233 EXPECT_EQ(error,
"Comma operator is not allowed in unrolled loop statement");
237for (int i = 0; i < atomicAdd(i, i); i++) [[unroll]] { i; }
239 auto [
output, _, error] = process_test_local(input, Language::BSL);
240 EXPECT_EQ(error,
"Non-constant expression in unrolled loop control");
244for (int i = 0; i < 65; i++) [[unroll]] { i; }
246 auto [
output, _, error] = process_test_local(input, Language::BSL);
247 EXPECT_EQ(error,
"Loop unrolling generates too many iterations (over 64)");
252for (int i = 0; i < 2; j++) [[unroll]] { i; }
254 auto [
output, _, error] = process_test_local(input, Language::BSL);
255 EXPECT_EQ(error,
"Unrolled loop expression must assign to 'i'");
259for (int i = 0; i < 2; i + 1) [[unroll]] { i; }
261 auto [
output, _, error] = process_test_local(input, Language::BSL);
263 "Expected '++i', '--i', 'i++', 'i--' or 'i = expr', 'i += expr', 'i -= expr', 'i /= "
264 "expr', 'i *= expr' for unrolled loop expression");
268for (int i = 1; i < 2; i <<= 1) [[unroll]] { i; }
270 auto [
output, _, error] = process_test_local(input, Language::BSL);
272 "Expected '++i', '--i', 'i++', 'i--' or 'i = expr', 'i += expr', 'i -= expr', 'i /= "
273 "expr', 'i *= expr' for unrolled loop expression");
277for (int i = 0; i < 2; i += atomicAdd(i, i)) [[unroll]] { i; }
279 auto [
output, _, error] = process_test_local(input, Language::BSL);
280 EXPECT_EQ(error,
"Non-constant expression in unrolled loop control");
284TEST(shader_tool, StaticBranch)
289 [[compilation_constant]] const int use_color_band;
292 if (use_color_band) [[static_branch]] {
298void func([[resource_table]] Resources &srt)
300 if (srt.use_color_band) [[static_branch]] {
304 if (srt.use_color_band == 1) [[static_branch]] {
310 if (srt.use_color_band) [[static_branch]] {
312 } else if (srt.use_color_band) [[static_branch]] {
316 if (srt.use_color_band) [[static_branch]] {
318 } else if (srt.use_color_band) [[static_branch]] {
324 if (srt.use_color_band) [[static_branch]] {
325 if (srt.use_color_band) [[static_branch]] {
332#define access_Resources_use_color_band() use_color_band
333#ifdef CREATE_INFO_RES_PASS_Resources
334CREATE_INFO_RES_PASS_Resources
336#ifdef CREATE_INFO_RES_BATCH_Resources
337CREATE_INFO_RES_BATCH_Resources
339#ifdef CREATE_INFO_RES_GEOMETRY_Resources
340CREATE_INFO_RES_GEOMETRY_Resources
342#ifdef CREATE_INFO_RES_SHARED_VARS_Resources
343CREATE_INFO_RES_SHARED_VARS_Resources
352Resources Resources_ctor_();
353void _fn(Resources this_);
354Resources Resources_new_();
357 Resources Resources_ctor_() {Resources r;r._pad=0;return r;}
361#if defined(CREATE_INFO_Resources)
363 void _fn(Resources this_) {
365#if SRT_CONSTANT_use_color_band
375 Resources Resources_new_()
385#if defined(CREATE_INFO_Resources)
387void func(Resources srt)
390#if SRT_CONSTANT_use_color_band
397#if SRT_CONSTANT_use_color_band == 1
409#if SRT_CONSTANT_use_color_band
414#elif SRT_CONSTANT_use_color_band
421#if SRT_CONSTANT_use_color_band
426#elif SRT_CONSTANT_use_color_band
438#if SRT_CONSTANT_use_color_band
442#if SRT_CONSTANT_use_color_band
459 auto [
output, _, error] = process_test_string(input);
465void func([[resource_table]] Resources &srt)
467 if (srt.use_color_band) [[static_branch]] {
469 } else if (srt.use_color_band) {
474 auto [
output, _, error] = process_test_string(input);
475 EXPECT_EQ(error,
"Expecting next if statement to also be a static branch.");
479void func([[resource_table]] Resources &srt)
481 if (use_color_band) [[static_branch]] {
486 auto [
output, _, error] = process_test_string(input);
488 "Expecting compilation or specialization constant. Make sure SRT arguments "
489 "have the [[resource_table]] attribute.");
493void func([[resource_table]] Resources &srt)
495 if (srt.use_color_band && srt.use_color_band) [[static_branch]] {
500 auto [
output, _, error] = process_test_string(input);
501 EXPECT_EQ(error,
"Expecting single condition.");
505template<int N> int implicit_test(int x)
521template int implicit_test<0>();
522template int implicit_test<1>();
523template int implicit_test<2>();
526 int implicit_testT0(int x)
534 int implicit_testT1(int x)
546 int implicit_testT2(int x)
559 auto [
output, _, error] = process_test_string(input, Language::BSL);
565template<int B> int chain_test(int y)
570 else if constexpr (B == 1) {
581template int chain_test<1>();
582template int chain_test<0>();
585 int chain_testT1(int y)
596 int chain_testT0(int y)
611 auto [
output, _, error] = process_test_string(input, Language::BSL);
617template<int i> int f(int a)
619 if constexpr (i == 0) {
625 else if (i + a == 1) {
628 else if constexpr (i == 2) {
636template int f<0>(int);
637template int f<1>(int);
638template int f<2>(int);
639template int f<3>(int);
684 auto [
output, _, error] = process_test_string(input, Language::BSL);
692 if constexpr (a == 0) {
697 auto [
output, _, error] = process_test_string(input, Language::BSL);
698 EXPECT_EQ(error,
"Constexpr if condition is not a constant expression");
707 [[compilation_constant]] const int i;
712 if (res.i + 1 + E::ENUM == ENUM) [[static_branch]] {
715 else if (res.i == 1) [[static_branch]] {
727static constexpr int ENUM = 1;
728#define access_Res_i() i
731#ifdef CREATE_INFO_RES_PASS_Res
732CREATE_INFO_RES_PASS_Res
734#ifdef CREATE_INFO_RES_BATCH_Res
735CREATE_INFO_RES_BATCH_Res
737#ifdef CREATE_INFO_RES_GEOMETRY_Res
738CREATE_INFO_RES_GEOMETRY_Res
740#ifdef CREATE_INFO_RES_SHARED_VARS_Res
741CREATE_INFO_RES_SHARED_VARS_Res
750Res Res_ctor_() {Res r;r._pad=0;return r;}
751#if defined(CREATE_INFO_Res)
756 #if SRT_CONSTANT_i+ 1 + 1== 1
761 #elif SRT_CONSTANT_i== 1
778 auto [
output, _, error] = process_test_string(input, Language::BSL);
784TEST(shader_tool, BoolCast)
787 string input = R
"(int i; if (i == 0) {})";
788 auto [
output, _, error] = process_test_local(input, Language::BSL);
792 string input = R
"(int i; if (i == 0u) {})";
793 auto [
output, _, error] = process_test_local(input, Language::BSL);
798 string input = R
"(if (1) {})";
799 auto [
output, _, error] = process_test_local(input, Language::BSL);
809 auto [
output, _, error] = process_test_local(input, Language::BSL);
820 auto [
output, _, error] = process_test_local(input, Language::BSL);
826TEST(shader_tool, ForceInline)
831[[force_inline]] void f() { f(); }
834 auto [
output, _, error] = process_test_string(input, Language::BSL);
835 EXPECT_EQ(error,
"Cannot inline recursive call");
840[[force_inline]] int f2(int a) {
843[[force_inline]] int f3(int b) {
850 auto [
output, _, error] = process_test_string(input, Language::BSL);
851 EXPECT_EQ(error,
"Cannot inline recursive call");
857[[force_inline]] int get_val() { return 42; }
859 int x = get_val() + 10;
862 auto [
output, _, error] = process_test_string(input, Language::BSL);
863 EXPECT_EQ(error,
"Cannot inline function call inside a compound expression");
869[[force_inline]] int add_one(int val) {
874 for (int i = 0; i < 10; ++i) {
879 auto [
output, _, error] = process_test_string(input, Language::BSL);
881 EXPECT_EQ(error,
"Cannot inline function call inside a compound expression");
887[[force_inline]] bool is_active() { return true; }
894 auto [
output, _, error] = process_test_string(input, Language::BSL);
895 EXPECT_EQ(error,
"Cannot inline function call inside a compound expression");
901[[force_inline]] int inner(int a) {
904[[force_inline]] int outer(int b) {
908 int result = outer(5);
911 auto [
output, _, error] = process_test_string(input, Language::BSL);
#define EXPECT_EQ(result, expect)
TEST(shader_tool, ExpressionParser)
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)