xref: /aosp_15_r20/external/angle/src/compiler/translator/ValidateAST.h (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1 //
2 // Copyright 2019 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 
7 #ifndef COMPILER_TRANSLATOR_VALIDATEAST_H_
8 #define COMPILER_TRANSLATOR_VALIDATEAST_H_
9 
10 #include "compiler/translator/BaseTypes.h"
11 #include "compiler/translator/Common.h"
12 
13 namespace sh
14 {
15 class TDiagnostics;
16 class TIntermNode;
17 
18 // The following options (stored in Compiler) tell the validator what to validate.  Some validations
19 // are conditional to certain passes.
20 struct ValidateASTOptions
21 {
22     // TODO: add support for the flags marked with TODO. http://anglebug.com/42261441
23 
24     // Check that every node always has only one parent,
25     bool validateSingleParent = true;
26     // Check that all symbols reference TVariables that have been declared.  For built-ins, this
27     // makes sure that the same GLSL built-in uses the same TVariable consistently.
28     bool validateVariableReferences = true;
29     // Whether validateVariableReferences should also include specialization constants.  Their
30     // declaration is output after their usage is discovered, so this is disabled until then.
31     bool validateSpecConstReferences = false;
32     // Validate that:
33     //
34     // - TIntermUnary only contains unary operators
35     // - TIntermBinary only contains binary operators
36     // - TIntermBranch only contains branch operators
37     // - EOpInitialize is only used in TIntermDeclaration
38     bool validateOps = true;
39     // Check that TIntermUnary and TIntermAggregate nodes with a built-in op reference a function
40     // with said op.
41     bool validateBuiltInOps = true;
42     // Check that all EOpCallFunctionInAST have their corresponding function definitions in the AST,
43     // with matching symbol ids. There should also be at least a prototype declaration before the
44     // function is called.
45     bool validateFunctionCall = true;
46     // Check that EOpCallInternalRawFunction is not used.  This OP is deprecated and needs to be
47     // removed.  http://anglebug.com/42264589
48     bool validateNoRawFunctionCalls = true;
49     // Check that there are no null nodes where they are not allowed, for example as children of
50     // TIntermDeclaration or TIntermBlock.
51     bool validateNullNodes = true;
52     // Check that symbols that reference variables have consistent qualifiers and symbol ids with
53     // the variable declaration.  The following needs to be validated:
54     //
55     // Implemented:
56     //
57     //  - Function parameters having one of EvqParam* qualifiers.
58     //  - No const qualifier on opaque function parameters.
59     //  - gl_ClipDistance, gl_CullDistance and gl_LastFragData are correctly qualified even when
60     //    redeclared in the shader.
61     //
62     // TODO:
63     //
64     //  - Function-local variables must have the EvqTemporary qualifier.
65     //  - Symbol references and declarations have identical qualifiers.
66     bool validateQualifiers = true;
67     // Check that every symbol has its precision specified.  That includes variables, block members,
68     // function parameters and return values.
69     bool validatePrecision = true;
70     // Check that variable declarations that can't have initializers don't have initializers
71     // (varyings, uniforms for example).
72     bool validateInitializers = true;  // TODO
73     // Check that there is only one TFunction with each function name referenced in the nodes (no
74     // two TFunctions with the same name, taking internal/non-internal namespaces into account).
75     bool validateUniqueFunctions = true;  // TODO
76     // Check that references to structs are matched with the corresponding struct declaration.
77     bool validateStructUsage = true;
78     // Check that expression nodes have the correct type considering their operand(s).  The
79     // following validation is possible:
80     //
81     // Implemented:
82     //
83     //  - Binary node that indexes T[] should have type T
84     //  - Binary nodes with EOpIndexDirect* should have a constant as the right node
85     //  - Switch nodes should have an integer type in the selector
86     //
87     // TODO:
88     //
89     //  - Function calls (including built-ins) have the same return type in the node and function.
90     //  - Unary and binary operators have the correct type based on operands
91     //  - Swizzle result has same type as the operand except for vector size
92     //  - Ternary operator has the same type as the operands
93     //  - Case expressions have the same type as the switch selector
94     bool validateExpressionTypes = true;
95     // If SeparateDeclarations has been run, check for the absence of multi declarations as well.
96     bool validateMultiDeclarations = false;
97     // If PruneNoOps has been run, check that no statements are ever added after branches in the
98     // same block.  Those statements would be dead code.
99     bool validateNoStatementsAfterBranch = false;
100     // Check that swizzle is not applied to swizzle.  Swizzles of swizzles are folded in
101     // TIntermSwizzle::fold.
102     bool validateNoSwizzleOfSwizzle = true;
103     // Check that constructors' types don't have qualifiers such as invariant, row_major etc.
104     bool validateNoQualifiersOnConstructors = true;
105 
106     // Once set, disallows any further transformations on the tree.  Used before AST post-processing
107     // which requires that the tree remains unmodified.
108     bool validateNoMoreTransformations = false;
109 };
110 
111 // Check for errors and output error messages on the context.
112 // Returns true if there are no errors.
113 bool ValidateAST(TIntermNode *root, TDiagnostics *diagnostics, const ValidateASTOptions &options);
114 
115 }  // namespace sh
116 
117 #endif  // COMPILER_TRANSLATOR_VALIDATESWITCH_H_
118