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