Hash :
25f5e2cf
        
        Author :
  
        
        Date :
2025-08-13T17:03:19
        
      
Vulkan: Add support for built-in gl_PrimitiveShadingRateEXT Add support for built-in gl_PrimitiveShadingRateEXT and relative const value. Bug: angleproject:397255625 Change-Id: I25d51f5805697e137b232175ee790808c45850b7 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/6845515 Reviewed-by: Charlie Lao <cclao@google.com> Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979
//
// Copyright 2024 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Parse_test.cpp:
//   Test for parsing erroneous and correct GLSL input.
//
#include <memory>
#include "GLSLANG/ShaderLang.h"
#include "angle_gl.h"
#include "compiler/translator/glsl/TranslatorESSL.h"
#include "gtest/gtest.h"
using namespace sh;
class ParseTest : public testing::Test
{
  public:
    ParseTest()
    {
        InitBuiltInResources(&mResources);
        mResources.FragmentPrecisionHigh = 1;
        mCompileOptions.intermediateTree = true;
    }
  protected:
    void TearDown() override { mTranslator.reset(); }
    testing::AssertionResult compile(const std::string &shaderString)
    {
        if (mTranslator == nullptr)
        {
            std::unique_ptr<TranslatorESSL> translator =
                std::make_unique<TranslatorESSL>(GL_FRAGMENT_SHADER, mShaderSpec);
            if (!translator->Init(mResources))
            {
                return testing::AssertionFailure() << "Failed to initialize translator";
            }
            mTranslator = std::move(translator);
        }
        const char *shaderStrings[] = {shaderString.c_str()};
        bool compilationSuccess     = mTranslator->compile(shaderStrings, 1, mCompileOptions);
        mInfoLog                    = mTranslator->getInfoSink().info.str();
        if (!compilationSuccess)
        {
            return testing::AssertionFailure() << "Shader compilation failed " << mInfoLog;
        }
        return testing::AssertionSuccess();
    }
    bool foundErrorInIntermediateTree() const { return foundInIntermediateTree("ERROR:"); }
    bool foundInIntermediateTree(const char *stringToFind) const
    {
        return mInfoLog.find(stringToFind) != std::string::npos;
    }
    std::string intermediateTree() const { return mInfoLog; }
    ShBuiltInResources mResources;
    ShCompileOptions mCompileOptions{};
    ShShaderSpec mShaderSpec = SH_WEBGL_SPEC;
  private:
    std::unique_ptr<TranslatorESSL> mTranslator;
    std::string mInfoLog;
};
TEST_F(ParseTest, UnsizedArrayConstructorNoCrash)
{
    const char kShader[] = R"(#version 310 es\n"
int A[];
int B[int[][](A)];)";
    EXPECT_FALSE(compile(kShader));
    EXPECT_TRUE(foundErrorInIntermediateTree());
    EXPECT_TRUE(foundInIntermediateTree("constructing from an unsized array"));
}
TEST_F(ParseTest, UniformBlockNameReferenceConstructorNoCrash)
{
    const char kShader[] = R"(#version 300 es
precision mediump float;
out float o;
uniform a { float r; } UBOA;
void main() {
    o = float(UBOA);
})";
    EXPECT_FALSE(compile(kShader));
    EXPECT_TRUE(foundErrorInIntermediateTree());
    EXPECT_TRUE(foundInIntermediateTree(
        "interface block cannot be used as a constructor argument for this type"));
}
TEST_F(ParseTest, Precise320NoCrash)
{
    const char kShader[] = R"(#version 320 es
precision mediump float;
void main(){
    float t;
    precise t;
})";
    EXPECT_FALSE(compile(kShader));
    EXPECT_TRUE(foundErrorInIntermediateTree());
    EXPECT_TRUE(foundInIntermediateTree("unsupported shader version"));
}
// Tests that layout(index=0) is parsed in es 100 shaders if an extension like
// EXT_shader_framebuffer_fetch is enabled, but this does not cause a crash.
TEST_F(ParseTest, ShaderFramebufferFetchLayoutIndexNoCrash)
{
    mResources.EXT_blend_func_extended      = 1;
    mResources.MaxDualSourceDrawBuffers     = 1;
    mResources.EXT_shader_framebuffer_fetch = 1;
    const char kShader[]                    = R"(
#extension GL_EXT_blend_func_extended: require
#extension GL_EXT_shader_framebuffer_fetch : require
layout(index=0)mediump vec4 c;
void main() { }
)";
    EXPECT_FALSE(compile(kShader));
    EXPECT_TRUE(foundErrorInIntermediateTree());
    EXPECT_TRUE(foundInIntermediateTree("'index' : invalid layout qualifier"));
}
// Test that with the conservative depth extension, gl_FragDepth
// can be redeclared successfully.
TEST_F(ParseTest, RedeclareFragDepthSuccess)
{
    mShaderSpec                       = SH_WEBGL2_SPEC;
    mResources.EXT_conservative_depth = 1;
    mCompileOptions.validateAST       = 1;
    const char kShader[]              = R"(#version 300 es
#extension GL_EXT_conservative_depth: enable
precision mediump float;
layout (depth_any) out float gl_FragDepth;
void main() {
}
)";
    EXPECT_TRUE(compile(kShader));
}
// Test that without the conservative depth extension, gl_FragDepth,
// cannot be redeclared.
TEST_F(ParseTest, RedeclareFragDepthNoExtFail)
{
    mShaderSpec                       = SH_WEBGL2_SPEC;
    mResources.EXT_conservative_depth = 1;
    mCompileOptions.validateAST       = 1;
    const char kShader[]              = R"(#version 300 es
precision mediump float;
layout (depth_any) out float gl_FragDepth;
void main() {
}
)";
    EXPECT_FALSE(compile(kShader));
    EXPECT_TRUE(foundErrorInIntermediateTree());
    EXPECT_TRUE(foundInIntermediateTree("reserved built-in name"));
}
// Test that even with the conservative depth extension, gl_FragDepth
// cannot be redeclared locally.
TEST_F(ParseTest, RedeclareFragDepthLocallyFail)
{
    mShaderSpec                       = SH_WEBGL2_SPEC;
    mResources.EXT_conservative_depth = 1;
    mCompileOptions.validateAST       = 1;
    const char kShader[]              = R"(#version 300 es
#extension GL_EXT_conservative_depth: enable
precision mediump float;
void main() {
    layout (depth_any) out float gl_FragDepth;
}
)";
    EXPECT_FALSE(compile(kShader));
    EXPECT_TRUE(foundErrorInIntermediateTree());
    EXPECT_TRUE(foundInIntermediateTree("gl_FragDepth can only be redeclared as fragment output"));
}
// Test that with the conservative depth extension, gl_FragDepth
// can only be redeclared once.
TEST_F(ParseTest, RedeclareFragDepthTwiceFail)
{
    mShaderSpec                       = SH_WEBGL2_SPEC;
    mResources.EXT_conservative_depth = 1;
    mCompileOptions.validateAST       = 1;
    const char kShader[]              = R"(#version 300 es
#extension GL_EXT_conservative_depth: enable
precision mediump float;
layout (depth_any) out float gl_FragDepth;
layout (depth_any) out float gl_FragDepth;
void main() {
}
)";
    EXPECT_FALSE(compile(kShader));
    EXPECT_TRUE(foundErrorInIntermediateTree());
    EXPECT_TRUE(foundInIntermediateTree("redefinition"));
}
// Test that with the conservative depth extension gl_FragDepth
// can only be redeclared globally.
TEST_F(ParseTest, RedeclareFragDepthGlobalAndLocalFail)
{
    mShaderSpec                       = SH_WEBGL2_SPEC;
    mResources.EXT_conservative_depth = 1;
    mCompileOptions.validateAST       = 1;
    const char kShader[]              = R"(#version 300 es
#extension GL_EXT_conservative_depth: enable
precision mediump float;
layout (depth_any) out float gl_FragDepth;
void main() {
    float gl_FragDepth = 1.0;
}
)";
    EXPECT_FALSE(compile(kShader));
    EXPECT_TRUE(foundErrorInIntermediateTree());
    EXPECT_TRUE(foundInIntermediateTree("reserved built-in name"));
}
TEST_F(ParseTest, Radians320NoCrash)
{
    const char kShader[] = R"(#version 320 es
precision mediump float;
vec4 s() { writeonly vec4 color; radians(color); return vec4(1); })";
    EXPECT_FALSE(compile(kShader));
    EXPECT_TRUE(foundErrorInIntermediateTree());
    EXPECT_TRUE(foundInIntermediateTree("'writeonly' : Only allowed with shader storage blocks,"));
    EXPECT_TRUE(foundInIntermediateTree(
        "'radians' : wrong operand type - no operation 'radians' exists that"));
}
TEST_F(ParseTest, CoherentCoherentNoCrash)
{
    const char kShader[] = R"(#version 310 es
uniform highp coherent coherent readonly image2D image1;\n"
void main() {
})";
    EXPECT_FALSE(compile(kShader));
    EXPECT_TRUE(foundErrorInIntermediateTree());
    EXPECT_TRUE(foundInIntermediateTree("coherent specified multiple times"));
}
TEST_F(ParseTest, LargeArrayIndexNoCrash)
{
    mShaderSpec          = SH_WEBGL2_SPEC;
    const char kShader[] = R"(#version 300 es
int rr[~1U];
out int o;
void main() {
    o = rr[1];
})";
    EXPECT_FALSE(compile(kShader));
    EXPECT_TRUE(foundErrorInIntermediateTree());
    EXPECT_TRUE(
        foundInIntermediateTree("Size of declared variable exceeds implementation-defined limit"));
}
// Tests that separating variable declaration of multiple instances of a anonymous structure
// rewrites the expression types for expressions that use the variables. At the time of writing
// the expression types were left referencing the original anonymous function.
TEST_F(ParseTest, SeparateAnonymousFunctionsRewritesExpressions)
{
    const char kShader[] = R"(
struct {
    mediump vec2 d;
} s0, s1;
void main() {
    s0 = s0;
    s1 = s1;
})";
    EXPECT_TRUE(compile(kShader));
    EXPECT_FALSE(foundInIntermediateTree("anonymous"));
}
// Tests that constant folding a division of a void variable does not crash during parsing.
TEST_F(ParseTest, ConstStructWithVoidAndDivNoCrash)
{
    const char kShader[] = R"(
const struct s { void i; } ss = s();
void main() {
    highp vec3 q = ss.i / ss.i;
})";
    EXPECT_FALSE(compile(kShader));
    EXPECT_TRUE(foundErrorInIntermediateTree());
    EXPECT_TRUE(foundInIntermediateTree("illegal use of type 'void'"));
    EXPECT_TRUE(foundInIntermediateTree("constructor does not have any arguments"));
    EXPECT_TRUE(foundInIntermediateTree("operation with void operands"));
    EXPECT_TRUE(foundInIntermediateTree(
        "wrong operand types - no operation '/' exists that takes a left-hand operand of type "
        "'const void' and a right operand of type 'const void'"));
    EXPECT_TRUE(foundInIntermediateTree(
        "cannot convert from 'const void' to 'highp 3-component vector of float'"));
}
// Tests that division of void variable returns the same errors as division of constant
// void variable (see above).
TEST_F(ParseTest, StructWithVoidAndDivErrorCheck)
{
    const char kShader[] = R"(
struct s { void i; } ss = s();
void main() {
    highp vec3 q = ss.i / ss.i;
})";
    EXPECT_FALSE(compile(kShader));
    EXPECT_TRUE(foundErrorInIntermediateTree());
    EXPECT_TRUE(foundInIntermediateTree("illegal use of type 'void'"));
    EXPECT_TRUE(foundInIntermediateTree("constructor does not have any arguments"));
    EXPECT_TRUE(foundInIntermediateTree("operation with void operands"));
    EXPECT_TRUE(foundInIntermediateTree(
        "wrong operand types - no operation '/' exists that takes a left-hand operand of type "
        "'void' and a right operand of type 'void'"));
    EXPECT_TRUE(foundInIntermediateTree(
        "cannot convert from 'void' to 'highp 3-component vector of float'"));
}
// Tests that usage of BuildIn struct type name does not crash during parsing.
TEST_F(ParseTest, BuildInStructTypeNameDeclarationNoCrash)
{
    mCompileOptions.validateAST = 1;
    const char kShader[]        = R"(
void main() {
gl_DepthRangeParameters testVariable;
})";
    EXPECT_FALSE(compile(kShader));
    EXPECT_TRUE(foundErrorInIntermediateTree());
    EXPECT_TRUE(foundInIntermediateTree("reserved built-in name"));
}
TEST_F(ParseTest, BuildInStructTypeNameFunctionArgumentNoCrash)
{
    mCompileOptions.validateAST = 1;
    const char kShader[]        = R"(
void testFunction(gl_DepthRangeParameters testParam){}
void main() {
testFunction(gl_DepthRange);
})";
    EXPECT_FALSE(compile(kShader));
    EXPECT_TRUE(foundErrorInIntermediateTree());
    EXPECT_TRUE(foundInIntermediateTree("reserved built-in name"));
}
TEST_F(ParseTest, BuildInStructTypeNameFunctionReturnValueNoCrash)
{
    mCompileOptions.validateAST = 1;
    const char kShader[]        = R"(
gl_DepthRangeParameters testFunction(){return gl_DepthRange;}
void main() {
testFunction();
})";
    EXPECT_FALSE(compile(kShader));
    EXPECT_TRUE(foundErrorInIntermediateTree());
    EXPECT_TRUE(foundInIntermediateTree("reserved built-in name"));
}
// Tests that imod of const void variable does not crash during parsing.
TEST_F(ParseTest, ConstStructVoidAndImodAndNoCrash)
{
    const char kShader[] = R"(#version 310 es
const struct s { void i; } ss = s();
void main() {
    highp vec3 q = ss.i % ss.i;
})";
    EXPECT_FALSE(compile(kShader));
    EXPECT_TRUE(foundErrorInIntermediateTree());
    EXPECT_TRUE(foundInIntermediateTree("illegal use of type 'void'"));
    EXPECT_TRUE(foundInIntermediateTree("constructor does not have any arguments"));
    EXPECT_TRUE(foundInIntermediateTree("operation with void operands"));
    EXPECT_TRUE(foundInIntermediateTree(
        "wrong operand types - no operation '%' exists that takes a left-hand operand of type "
        "'const void' and a right operand of type 'const void'"));
    EXPECT_TRUE(foundInIntermediateTree(
        "cannot convert from 'const void' to 'highp 3-component vector of float'"));
}
TEST_F(ParseTest, HugeUnsizedMultidimensionalArrayConstructorNoCrash)
{
    mCompileOptions.limitExpressionComplexity = true;
    std::ostringstream shader;
    shader << R"(#version 310 es
int E=int)";
    for (int i = 0; i < 10000; ++i)
    {
        shader << "[]";
    }
    shader << "()";
    EXPECT_FALSE(compile(shader.str()));
    EXPECT_TRUE(foundErrorInIntermediateTree());
    EXPECT_TRUE(foundInIntermediateTree("array has too many dimensions"));
}
TEST_F(ParseTest, HugeMultidimensionalArrayConstructorNoCrash)
{
    mCompileOptions.limitExpressionComplexity = true;
    std::ostringstream shader;
    shader << R"(#version 310 es
int E=int)";
    for (int i = 0; i < 10000; ++i)
    {
        shader << "[1]";
    }
    for (int i = 0; i < 10000; ++i)
    {
        shader << "(2)";
    }
    EXPECT_FALSE(compile(shader.str()));
    EXPECT_TRUE(foundErrorInIntermediateTree());
    EXPECT_TRUE(foundInIntermediateTree("array has too many dimensions"));
}
TEST_F(ParseTest, DeeplyNestedWhileStatementsNoCrash)
{
    mShaderSpec = SH_WEBGL2_SPEC;
    std::ostringstream shader;
    shader << R"(#version 300 es
void main() {
)";
    for (int i = 0; i < 1700; ++i)
    {
        shader << " while(true)";
    }
    shader << "; }";
    EXPECT_FALSE(compile(shader.str()));
    EXPECT_TRUE(foundErrorInIntermediateTree());
    EXPECT_TRUE(foundInIntermediateTree("statement is too deeply nested"));
}
TEST_F(ParseTest, DeeplyNestedForStatementsNoCrash)
{
    mShaderSpec = SH_WEBGL2_SPEC;
    std::ostringstream shader;
    shader << R"(#version 300 es
void main() {
)";
    for (int i = 0; i < 1700; ++i)
    {
        shader << " for(int i = 0; i < 10; i++)";
    }
    shader << "; }";
    EXPECT_FALSE(compile(shader.str()));
    EXPECT_TRUE(foundErrorInIntermediateTree());
    EXPECT_TRUE(foundInIntermediateTree("statement is too deeply nested"));
}
TEST_F(ParseTest, DeeplyNestedDoWhileStatementsNoCrash)
{
    mShaderSpec = SH_WEBGL2_SPEC;
    std::ostringstream shader;
    shader << R"(#version 300 es
void main() {
)";
    for (int i = 0; i < 1700; ++i)
    {
        shader << " do {";
    }
    for (int i = 0; i < 1700; ++i)
    {
        shader << "} while(true);";
    }
    shader << "}";
    EXPECT_FALSE(compile(shader.str()));
    EXPECT_TRUE(foundErrorInIntermediateTree());
    EXPECT_TRUE(foundInIntermediateTree("statement is too deeply nested"));
}
TEST_F(ParseTest, DeeplyNestedSwitchStatementsNoCrash)
{
    mShaderSpec = SH_WEBGL2_SPEC;
    std::ostringstream shader;
    shader << R"(#version 300 es
void main() {
)";
    for (int i = 0; i < 1700; ++i)
    {
        shader << " switch(1) { default: int i=0;";
    }
    for (int i = 0; i < 1700; ++i)
    {
        shader << "}";
    }
    shader << "}";
    EXPECT_FALSE(compile(shader.str()));
    EXPECT_TRUE(foundErrorInIntermediateTree());
    EXPECT_TRUE(foundInIntermediateTree("statement is too deeply nested"));
}
TEST_F(ParseTest, ManyChainedUnaryExpressionsNoCrash)
{
    mCompileOptions.limitExpressionComplexity = true;
    mShaderSpec                               = SH_WEBGL2_SPEC;
    std::ostringstream shader;
    shader << R"(#version 300 es
precision mediump float;
void main() {
  int iterations=0;)";
    for (int i = 0; i < 6000; ++i)
    {
        shader << "~";
    }
    shader << R"(++iterations;
}
)";
    EXPECT_FALSE(compile(shader.str()));
    EXPECT_TRUE(foundErrorInIntermediateTree());
    EXPECT_TRUE(foundInIntermediateTree("Expression too complex"));
}
TEST_F(ParseTest, ManyChainedAssignmentsNoCrash)
{
    mCompileOptions.limitExpressionComplexity = true;
    mShaderSpec                               = SH_WEBGL2_SPEC;
    std::ostringstream shader;
    shader << R"(#version 300 es
void main() {
    int c = 0;
)";
    for (int i = 0; i < 3750; ++i)
    {
        shader << "c=\n";
    }
    shader << "c+1; }";
    EXPECT_FALSE(compile(shader.str()));
    EXPECT_TRUE(foundErrorInIntermediateTree());
    EXPECT_TRUE(foundInIntermediateTree("Expression too complex"));
}
// Test that comma expression referring to an uniform block member through instance-name is not an
// error.
TEST_F(ParseTest, UniformBlockWorks)
{
    mShaderSpec          = SH_WEBGL2_SPEC;
    const char kShader[] = R"(#version 300 es
         uniform B { uint e; } b;
         void main() { b.e; })";
    EXPECT_TRUE(compile(kShader));
    const char kShader2[] = R"(#version 300 es
         uniform B { uint e; } b;
         mediump float f() { return .0; }
         void main() { f(), b.e; })";
    EXPECT_TRUE(compile(kShader2));
    const char kShader3[] = R"(#version 300 es
         uniform B { uint e; };
         void main() { e; })";
    EXPECT_TRUE(compile(kShader3));
    const char kShader4[] = R"(#version 300 es
        uniform B { uint e; };
        mediump float f() { return .0; }
        void main() { f(), e; })";
    EXPECT_TRUE(compile(kShader4));
    const char kShader5[] = R"(#version 300 es
        uniform B { uint e; } b[3];
        void main() { b[0].e; })";
    EXPECT_TRUE(compile(kShader5));
    const char kShader6[] = R"(#version 300 es
        uniform B { uint e; } b[3];
        mediump float f() { return .0; }
        void main() { f(), b[0].e; })";
    EXPECT_TRUE(compile(kShader6));
}
// Test that comma expression referring to an uniform block instance-name is an error.
TEST_F(ParseTest, UniformBlockInstanceNameReferenceIsError)
{
    mShaderSpec = SH_WEBGL2_SPEC;
    const char kShader[] = R"(#version 300 es
         precision mediump float;
         uniform B { uint e; } b;
         void main() { b; })";
    EXPECT_FALSE(compile(kShader));
    EXPECT_TRUE(foundErrorInIntermediateTree());
    EXPECT_TRUE(
        foundInIntermediateTree("expression statement is not allowed for interface blocks"));
    const char kShader2[] = R"(#version 300 es
         uniform B { uint e; } b;
         mediump float f() { return .0; }
         void main() { f(), b; })";
    EXPECT_FALSE(compile(kShader2));
    EXPECT_TRUE(foundErrorInIntermediateTree());
    EXPECT_TRUE(
        foundInIntermediateTree("',' : sequence operator is not allowed for interface blocks"));
}
// Test that expression statements resulting in a uniform block instance-name as an array subscript
// is an error.
TEST_F(ParseTest, UniformBlockInstanceNameReferenceSubscriptIsError)
{
    mShaderSpec = SH_WEBGL2_SPEC;
    const char kShader[] = R"(#version 300 es
         precision mediump float;
         uniform B { uint e; } b[3];
         void main() { b[0]; })";
    EXPECT_FALSE(compile(kShader));
    EXPECT_TRUE(foundErrorInIntermediateTree());
    EXPECT_TRUE(
        foundInIntermediateTree("expression statement is not allowed for interface blocks"));
    const char kShader2[] = R"(#version 300 es
         uniform B { uint e; } b[3];
         mediump float f() { return .0; }
         void main() { f(), b[0]; })";
    EXPECT_FALSE(compile(kShader2));
    EXPECT_TRUE(foundErrorInIntermediateTree());
    EXPECT_TRUE(
        foundInIntermediateTree("',' : sequence operator is not allowed for interface blocks"));
}
// Test that expressions using binary operations on a uniform block instance-name is an error.
TEST_F(ParseTest, UniformBlockInstanceNameOpIsError)
{
    mShaderSpec = SH_WEBGL2_SPEC;
    const char kShader[] = R"(#version 300 es
        precision mediump float;
        uniform B { uint e; } b;
        void main() { b = b; })";
    EXPECT_FALSE(compile(kShader));
    EXPECT_TRUE(foundErrorInIntermediateTree());
    EXPECT_TRUE(
        foundInIntermediateTree("'assign' : l-value required (can't modify a uniform \"b\")"));
    EXPECT_TRUE(foundInIntermediateTree("'=' : Invalid operation for interface blocks"));
    EXPECT_TRUE(foundInIntermediateTree(
        "'assign' : cannot convert from 'uniform interface block' to 'uniform interface block'"));
    const char kShader2[] = R"(#version 300 es
        uniform B { uint e; } b;
        void main() { b == b; })";
    EXPECT_FALSE(compile(kShader2));
    EXPECT_TRUE(foundErrorInIntermediateTree());
    EXPECT_TRUE(foundInIntermediateTree("'==' : Invalid operation for interface blocks"));
    EXPECT_TRUE(foundInIntermediateTree(
        "'==' : wrong operand types - no operation '==' exists that takes a left-hand operand of "
        "type 'uniform interface block' and a right operand of type 'uniform interface block' (or "
        "there is no acceptable conversion)"));
    const char kShader3[] = R"(#version 300 es
         uniform B { uint e; } b;
         void main() { b.e > 33u ? b : b; })";
    EXPECT_FALSE(compile(kShader3));
    EXPECT_TRUE(foundErrorInIntermediateTree());
    EXPECT_TRUE(
        foundInIntermediateTree("'?:' : ternary operator is not allowed for interface blocks"));
}
TEST_F(ParseTest, UniformBlockReferenceIsError)
{
    const char kShader[] = R"(#version 300 es
        uniform B { uint e; } b;
        void main() { B; })";
    EXPECT_FALSE(compile(kShader));
    EXPECT_TRUE(foundErrorInIntermediateTree());
    EXPECT_TRUE(foundInIntermediateTree("'B' : variable expected"));
    const char kShader2[] = R"(#version 300 es
        uniform B { uint e; };
        mediump float f() { return .0; }
        void main() { f(), B; })";
    EXPECT_FALSE(compile(kShader2));
    EXPECT_TRUE(foundErrorInIntermediateTree());
    EXPECT_TRUE(foundInIntermediateTree("'B' : variable expected"));
}
// Tests that referring to functions is a parse error.
TEST_F(ParseTest, FunctionReferenceIsError)
{
    mShaderSpec          = SH_WEBGL2_SPEC;
    const char kShader[] = R"(#version 300 es
        mediump float f() { return .0; }
        void main() { f; })";
    EXPECT_FALSE(compile(kShader));
    EXPECT_TRUE(foundErrorInIntermediateTree());
    EXPECT_TRUE(foundInIntermediateTree("'f' : variable expected"));
    const char kShader2[] = R"(#version 300 es
        mediump float f() { return .0; }
        void main() { f(), f; })";
    EXPECT_FALSE(compile(kShader2));
    EXPECT_TRUE(foundErrorInIntermediateTree());
    EXPECT_TRUE(foundInIntermediateTree("'f' : variable expected"));
}
// Tests that referring to builtin functions is a parse error.
// Shows discrepancy where the error message is unexpected, user defined functions have
// better error message.
TEST_F(ParseTest, BuiltinFunctionReferenceIsError)
{
    mShaderSpec          = SH_WEBGL2_SPEC;
    const char kShader[] = R"(#version 300 es
        void main() { sin; })";
    EXPECT_FALSE(compile(kShader));
    EXPECT_TRUE(foundErrorInIntermediateTree());
    EXPECT_TRUE(foundInIntermediateTree("'sin' : undeclared identifier"));
    const char kShader2[] = R"(#version 300 es
        void main() { sin(3.0), sin; })";
    EXPECT_FALSE(compile(kShader2));
    EXPECT_TRUE(foundErrorInIntermediateTree());
    EXPECT_TRUE(foundInIntermediateTree("'sin' : undeclared identifier"));
}
// Tests that unsized array parameters fail.
TEST_F(ParseTest, UnsizedArrayParameterIsError)
{
    mShaderSpec          = SH_WEBGL2_SPEC;
    const char kShader[] = R"(#version 300 es
int f(int a[], int i) {
    return i;
}
void main() { }
)";
    EXPECT_FALSE(compile(kShader));
    EXPECT_TRUE(foundErrorInIntermediateTree());
    EXPECT_TRUE(foundInIntermediateTree("'a' : function parameter array must specify a size"));
}
// Tests that unsized array parameters fail.
TEST_F(ParseTest, UnsizedArrayParameterIsError2)
{
    mShaderSpec          = SH_GLES3_1_SPEC;
    const char kShader[] = R"(#version 310 es
int f(int []a[1], int i) {
    return i;
}
void main() { }
)";
    EXPECT_FALSE(compile(kShader));
    EXPECT_TRUE(foundErrorInIntermediateTree());
    EXPECT_TRUE(foundInIntermediateTree("'a' : function parameter array must specify a size"));
}
// Tests that unnamed, unsized array parameters fail with same error message as named ones.
TEST_F(ParseTest, UnnamedUnsizedArrayParameterIsError)
{
    mShaderSpec          = SH_WEBGL2_SPEC;
    const char kShader[] = R"(#version 300 es
int f(int[], int i) {
    return i;
}
void main() { }
)";
    EXPECT_FALSE(compile(kShader));
    EXPECT_TRUE(foundErrorInIntermediateTree());
    EXPECT_TRUE(foundInIntermediateTree("'' : function parameter array must specify a size"));
}
// Tests that different array notatinos [1]a[2], a[1][2] etc work in parameters.
TEST_F(ParseTest, ArrayParameterVariants)
{
    mShaderSpec          = SH_GLES3_1_SPEC;
    const char kShader[] = R"(#version 310 es
int f(int[1][2] a) {
    return a[0][0];
}
int g(int a[1][2]) {
    return a[0][0];
}
int h(int[1]a[2]) {
    return a[0][0];
}
void main() {
    int[1][2] a;
    int b[1][2];
    int x1 = f(a);
    int x2 = f(b);
    int x3 = g(a);
    int x4 = g(b);
    int[1] c[2];
    int d[2][1];
    int y1 = h(c);
    int y2 = h(d);
}
)";
    EXPECT_TRUE(compile(kShader));
}
// Tests that parameters parse the [1]a[2] notation in correct order.
TEST_F(ParseTest, ArrayParameterVariantsMismatchIsError2)
{
    mShaderSpec          = SH_GLES3_1_SPEC;
    const char kShader[] = R"(#version 310 es
int f(int[1]a[2]) {
    return a[0][0];
}
void main() {
    int[1][2] a;
    int x = f(a);
}
)";
    EXPECT_FALSE(compile(kShader));
    EXPECT_TRUE(foundErrorInIntermediateTree());
    EXPECT_TRUE(foundInIntermediateTree("'f' : no matching overloaded function found"));
}
// Tests that specifying a struct in a function parameter is a parse error.
TEST_F(ParseTest, StructSpecificationFunctionParameterIsError)
{
    mShaderSpec          = SH_WEBGL2_SPEC;
    const char kShader[] = R"(#version 300 es
precision highp float;
float f(struct S {float f;} a) {
    return a.f;
}
void main() { })";
    EXPECT_FALSE(compile(kShader));
    EXPECT_TRUE(foundErrorInIntermediateTree());
    EXPECT_TRUE(
        foundInIntermediateTree("'a' : Function parameter type cannot be a structure definition"));
}
// Tests that specifying a struct in a function parameter is the same parse error as with named one.
TEST_F(ParseTest, StructSpecificationUnnamedFunctionParameterIsError)
{
    mShaderSpec          = SH_WEBGL2_SPEC;
    const char kShader[] = R"(#version 300 es
precision highp float;
float f(struct S {float f;}) {
    return a.f;
}
void main() { })";
    EXPECT_FALSE(compile(kShader));
    EXPECT_TRUE(foundErrorInIntermediateTree());
    EXPECT_TRUE(
        foundInIntermediateTree("'' : Function parameter type cannot be a structure definition"));
}
// Tests that specifying a struct in a function parameter is the same parse error as with named one.
TEST_F(ParseTest, UnnamedStructSpecificationUnnamedFunctionParameterIsError)
{
    mShaderSpec          = SH_WEBGL2_SPEC;
    const char kShader[] = R"(#version 300 es
precision highp float;
float f(struct {float f;}) {
    return a.f;
}
void main() { })";
    EXPECT_FALSE(compile(kShader));
    EXPECT_TRUE(foundErrorInIntermediateTree());
    EXPECT_TRUE(
        foundInIntermediateTree("'' : Function parameter type cannot be a structure definition"));
}
// Tests that specifying a struct in a function parameter is the same parse error as with named one.
TEST_F(ParseTest, UnnamedStructSpecificationFunctionParameterIsError)
{
    mShaderSpec          = SH_WEBGL2_SPEC;
    const char kShader[] = R"(#version 300 es
precision highp float;
float f(struct {float f;} d) {
    return a.f;
}
void main() { })";
    EXPECT_FALSE(compile(kShader));
    EXPECT_TRUE(foundErrorInIntermediateTree());
    EXPECT_TRUE(
        foundInIntermediateTree("'d' : Function parameter type cannot be a structure definition"));
}
TEST_F(ParseTest, SeparateStructStructSpecificationFunctionNoCrash)
{
    mCompileOptions.validateAST = 1;
    const char kShader[] =
        R"(struct S{int f;};struct S2{S h;} o() { return S2(S(1)); } void main(){ S2 s2 = o(); })";
    EXPECT_TRUE(compile(kShader));
}
// Test showing that prototypes get the function definition variable names.
// An example where parser loses information.
TEST_F(ParseTest, VariableNamesInPrototypesUnnamedOut)
{
    const char kShader[]   = R"(
precision highp float;
void f(out float, out float);
void main()
{
    gl_FragColor = vec4(0.5);
    f(gl_FragColor.r, gl_FragColor.g);
}
void f(out float r, out float)
{
    r = 1.0;
}
)";
    const char kExpected[] = R"(0:2: Code block
0:3:   Function Prototype: 'f' (symbol id 3001) (void)
0:3:     parameter: 'r' (symbol id 3006) (out highp float)
0:3:     parameter: '' (symbol id 3007) (out highp float)
0:4:   Function Definition:
0:4:     Function Prototype: 'main' (symbol id 3004) (void)
0:5:     Code block
0:6:       move second child to first child (mediump 4-component vector of float)
0:6:         gl_FragColor (symbol id 1917) (FragColor mediump 4-component vector of float)
0:6:         Constant union (const mediump 4-component vector of float)
0:6:           0.5 (const float)
0:6:           0.5 (const float)
0:6:           0.5 (const float)
0:6:           0.5 (const float)
0:7:       Call a function: 'f' (symbol id 3001) (void)
0:7:         vector swizzle (x) (mediump float)
0:7:           gl_FragColor (symbol id 1917) (FragColor mediump 4-component vector of float)
0:7:         vector swizzle (y) (mediump float)
0:7:           gl_FragColor (symbol id 1917) (FragColor mediump 4-component vector of float)
0:9:   Function Definition:
0:9:     Function Prototype: 'f' (symbol id 3001) (void)
0:9:       parameter: 'r' (symbol id 3006) (out highp float)
0:9:       parameter: '' (symbol id 3007) (out highp float)
0:10:     Code block
0:11:       move second child to first child (highp float)
0:11:         'r' (symbol id 3006) (out highp float)
0:11:         Constant union (const highp float)
0:11:           1.0 (const float)
)";
    EXPECT_TRUE(compile(kShader));
    EXPECT_EQ(kExpected, intermediateTree());
}
TEST_F(ParseTest, ConstInSamplerParamNoCrash)
{
    mCompileOptions.validateAST = 1;
    const char kShader[]        = R"(void n(const in sampler2D){2;} void main(){})";
    EXPECT_TRUE(compile(kShader));
}
TEST_F(ParseTest, ConstSamplerParamNoCrash)
{
    mCompileOptions.validateAST = 1;
    const char kShader[]        = R"(void n(const sampler2D){2;} void main(){})";
    EXPECT_TRUE(compile(kShader));
}
TEST_F(ParseTest, InConstSamplerParamIsError)
{
    mCompileOptions.validateAST = 1;
    const char kShader[]        = R"(void n(in const sampler2D){2;} void main(){})";
    EXPECT_FALSE(compile(kShader));
    EXPECT_TRUE(foundErrorInIntermediateTree());
    EXPECT_TRUE(foundInIntermediateTree("'const' : invalid parameter qualifier"));
}
TEST_F(ParseTest, UniformBlockInstanceUnsizedArrayIsError)
{
    const char kShader[] = R"(#version 300 es
precision mediump float;out vec4 o;uniform a{float r;}u[];void main(){o=vec4(u[0].r+u[1].r+u[1].r);})";
    EXPECT_FALSE(compile(kShader));
    EXPECT_TRUE(foundErrorInIntermediateTree());
    EXPECT_TRUE(
        foundInIntermediateTree("'u' : implicitly sized arrays only allowed for tessellation "
                                "shaders or geometry shader inputs"));
}
TEST_F(ParseTest, InputBlockInstanceUnsizedArrayIsError)
{
    const char kShader[] = R"(#version 300 es
precision mediump float;out vec4 o;in a{float r;}i[];void main(){o=vec4(i[0].r+i[1].r+i[1].r);})";
    EXPECT_FALSE(compile(kShader));
    EXPECT_TRUE(foundErrorInIntermediateTree());
    EXPECT_TRUE(
        foundInIntermediateTree("'i' : implicitly sized arrays only allowed for tessellation "
                                "shaders or geometry shader inputs"));
}
TEST_F(ParseTest, OutputBlockInstanceUnsizedArrayIsError)
{
    const char kShader[] = R"(#version 300 es
precision mediump float;out a{float r;}o[];void main(){o[0].r=1.0; o[1].r=2.0;})";
    EXPECT_FALSE(compile(kShader));
    EXPECT_TRUE(foundErrorInIntermediateTree());
    EXPECT_TRUE(
        foundInIntermediateTree("'o' : implicitly sized arrays only allowed for tessellation "
                                "shaders or geometry shader inputs"));
}