• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (C) 2016-2018 Google, Inc.
3 // Copyright (C) 2016 LunarG, Inc.
4 // Copyright (C) 2023 Mobica Limited.
5 //
6 // All rights reserved.
7 //
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions
10 // are met:
11 //
12 //    Redistributions of source code must retain the above copyright
13 //    notice, this list of conditions and the following disclaimer.
14 //
15 //    Redistributions in binary form must reproduce the above
16 //    copyright notice, this list of conditions and the following
17 //    disclaimer in the documentation and/or other materials provided
18 //    with the distribution.
19 //
20 //    Neither the name of Google, Inc., nor the names of its
21 //    contributors may be used to endorse or promote products derived
22 //    from this software without specific prior written permission.
23 //
24 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 // POSSIBILITY OF SUCH DAMAGE.
36 //
37 
38 #ifndef HLSLGRAMMAR_H_
39 #define HLSLGRAMMAR_H_
40 
41 #include "hlslParseHelper.h"
42 #include "hlslOpMap.h"
43 #include "hlslTokenStream.h"
44 
45 namespace glslang {
46 
47     class TFunctionDeclarator;
48 
49     // Should just be the grammar aspect of HLSL.
50     // Described in more detail in hlslGrammar.cpp.
51 
52     class HlslGrammar : public HlslTokenStream {
53     public:
HlslGrammar(HlslScanContext & scanner,HlslParseContext & parseContext)54         HlslGrammar(HlslScanContext& scanner, HlslParseContext& parseContext)
55             : HlslTokenStream(scanner), parseContext(parseContext), intermediate(parseContext.intermediate),
56               typeIdentifiers(false), unitNode(nullptr) { }
~HlslGrammar()57         virtual ~HlslGrammar() { }
58 
59         bool parse();
60 
61     protected:
62         HlslGrammar();
63         HlslGrammar& operator=(const HlslGrammar&);
64 
65         void expected(const char*);
66         void unimplemented(const char*);
67         bool acceptIdentifier(HlslToken&);
68         bool acceptCompilationUnit();
69         bool acceptDeclarationList(TIntermNode*&);
70         bool acceptDeclaration(TIntermNode*&);
71         bool acceptControlDeclaration(TIntermNode*& node);
72         bool acceptSamplerDeclarationDX9(TType&);
73         bool acceptSamplerState();
74         bool acceptFullySpecifiedType(TType&, const TAttributes&);
75         bool acceptFullySpecifiedType(TType&, TIntermNode*& nodeList, const TAttributes&, bool forbidDeclarators = false);
76         bool acceptPreQualifier(TQualifier&);
77         bool acceptPostQualifier(TQualifier&);
78         bool acceptLayoutQualifierList(TQualifier&);
79         bool acceptType(TType&);
80         bool acceptType(TType&, TIntermNode*& nodeList);
81         bool acceptTemplateVecMatBasicType(TBasicType&, TPrecisionQualifier&);
82         bool acceptVectorTemplateType(TType&);
83         bool acceptMatrixTemplateType(TType&);
84         bool acceptTessellationDeclType(TBuiltInVariable&);
85         bool acceptTessellationPatchTemplateType(TType&);
86         bool acceptStreamOutTemplateType(TType&, TLayoutGeometry&);
87         bool acceptOutputPrimitiveGeometry(TLayoutGeometry&);
88         bool acceptAnnotations(TQualifier&);
89         bool acceptSamplerTypeDX9(TType &);
90         bool acceptSamplerType(TType&);
91         bool acceptTextureType(TType&);
92         bool acceptSubpassInputType(TType&);
93         bool acceptStructBufferType(TType&);
94         bool acceptTextureBufferType(TType&);
95         bool acceptConstantBufferType(TType&);
96         bool acceptStruct(TType&, TIntermNode*& nodeList);
97         bool acceptStructDeclarationList(TTypeList*&, TIntermNode*& nodeList, TVector<TFunctionDeclarator>&);
98         bool acceptMemberFunctionDefinition(TIntermNode*& nodeList, const TType&, TString& memberName,
99                                             TFunctionDeclarator&);
100         bool acceptFunctionParameters(TFunction&);
101         bool acceptParameterDeclaration(TFunction&);
102         bool acceptFunctionDefinition(TFunctionDeclarator&, TIntermNode*& nodeList, TVector<HlslToken>* deferredTokens);
103         bool acceptFunctionBody(TFunctionDeclarator& declarator, TIntermNode*& nodeList);
104         bool acceptParenExpression(TIntermTyped*&);
105         bool acceptExpression(TIntermTyped*&);
106         bool acceptInitializer(TIntermTyped*&);
107         bool acceptAssignmentExpression(TIntermTyped*&);
108         bool acceptConditionalExpression(TIntermTyped*&);
109         bool acceptBinaryExpression(TIntermTyped*&, PrecedenceLevel);
110         bool acceptUnaryExpression(TIntermTyped*&);
111         bool acceptPostfixExpression(TIntermTyped*&);
112         bool acceptConstructor(TIntermTyped*&);
113         bool acceptFunctionCall(const TSourceLoc&, TString& name, TIntermTyped*&, TIntermTyped* objectBase);
114         bool acceptArguments(TFunction*, TIntermTyped*&);
115         bool acceptLiteral(TIntermTyped*&);
116         bool acceptSimpleStatement(TIntermNode*&);
117         bool acceptCompoundStatement(TIntermNode*&);
118         bool acceptScopedStatement(TIntermNode*&);
119         bool acceptScopedCompoundStatement(TIntermNode*&);
120         bool acceptStatement(TIntermNode*&);
121         bool acceptNestedStatement(TIntermNode*&);
122         void acceptAttributes(TAttributes&);
123         bool acceptSelectionStatement(TIntermNode*&, const TAttributes&);
124         bool acceptSwitchStatement(TIntermNode*&, const TAttributes&);
125         bool acceptIterationStatement(TIntermNode*&, const TAttributes&);
126         bool acceptJumpStatement(TIntermNode*&);
127         bool acceptCaseLabel(TIntermNode*&);
128         bool acceptDefaultLabel(TIntermNode*&);
129         void acceptArraySpecifier(TArraySizes*&);
130         bool acceptPostDecls(TQualifier&);
131         bool acceptDefaultParameterDeclaration(const TType&, TIntermTyped*&);
132 
133         bool captureBlockTokens(TVector<HlslToken>& tokens);
134         const char* getTypeString(EHlslTokenClass tokenClass) const;
135 
136         HlslParseContext& parseContext;  // state of parsing and helper functions for building the intermediate
137         TIntermediate& intermediate;     // the final product, the intermediate representation, includes the AST
138         bool typeIdentifiers;            // shader uses some types as identifiers
139         TIntermNode* unitNode;
140     };
141 
142 } // end namespace glslang
143 
144 #endif // HLSLGRAMMAR_H_
145