John Kessenich | e01a9bc | 2016-03-12 20:11:22 -0700 | [diff] [blame] | 1 | // |
| 2 | //Copyright (C) 2016 Google, Inc. |
LoopDawg | 592860c | 2016-06-09 08:57:35 -0600 | [diff] [blame] | 3 | //Copyright (C) 2016 LunarG, Inc. |
John Kessenich | e01a9bc | 2016-03-12 20:11:22 -0700 | [diff] [blame] | 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 3Dlabs Inc. Ltd. 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 | #ifndef HLSL_PARSE_INCLUDED_ |
| 37 | #define HLSL_PARSE_INCLUDED_ |
| 38 | |
| 39 | #include "../glslang/MachineIndependent/parseVersions.h" |
| 40 | #include "../glslang/MachineIndependent/ParseHelper.h" |
| 41 | |
| 42 | namespace glslang { |
| 43 | |
| 44 | class HlslParseContext : public TParseContextBase { |
| 45 | public: |
| 46 | HlslParseContext(TSymbolTable&, TIntermediate&, bool parsingBuiltins, |
John Kessenich | b901ade | 2016-06-16 20:59:42 -0600 | [diff] [blame] | 47 | int version, EProfile, const SpvVersion& spvVersion, EShLanguage, TInfoSink&, |
John Kessenich | e01a9bc | 2016-03-12 20:11:22 -0700 | [diff] [blame] | 48 | bool forwardCompatible = false, EShMessages messages = EShMsgDefault); |
| 49 | virtual ~HlslParseContext(); |
LoopDawg | 6256146 | 2016-07-22 20:46:03 -0600 | [diff] [blame^] | 50 | void initializeExtensionBehavior(); |
| 51 | |
John Kessenich | e01a9bc | 2016-03-12 20:11:22 -0700 | [diff] [blame] | 52 | void setLimits(const TBuiltInResource&); |
| 53 | bool parseShaderStrings(TPpContext&, TInputScanner& input, bool versionWillBeError = false); |
John Kessenich | e512cd9 | 2016-05-03 21:17:55 -0600 | [diff] [blame] | 54 | void getPreamble(std::string&); |
John Kessenich | e01a9bc | 2016-03-12 20:11:22 -0700 | [diff] [blame] | 55 | |
| 56 | void C_DECL error(const TSourceLoc&, const char* szReason, const char* szToken, |
| 57 | const char* szExtraInfoFormat, ...); |
| 58 | void C_DECL warn(const TSourceLoc&, const char* szReason, const char* szToken, |
| 59 | const char* szExtraInfoFormat, ...); |
| 60 | void C_DECL ppError(const TSourceLoc&, const char* szReason, const char* szToken, |
| 61 | const char* szExtraInfoFormat, ...); |
| 62 | void C_DECL ppWarn(const TSourceLoc&, const char* szReason, const char* szToken, |
| 63 | const char* szExtraInfoFormat, ...); |
| 64 | |
John Kessenich | 7f349c7 | 2016-07-08 22:09:10 -0600 | [diff] [blame] | 65 | void reservedPpErrorCheck(const TSourceLoc&, const char* /*name*/, const char* /*op*/) { } |
| 66 | bool lineContinuationCheck(const TSourceLoc&, bool /*endOfComment*/) { return true; } |
John Kessenich | e01a9bc | 2016-03-12 20:11:22 -0700 | [diff] [blame] | 67 | bool lineDirectiveShouldSetNextLine() const { return true; } |
| 68 | bool builtInName(const TString&); |
| 69 | |
| 70 | void handlePragma(const TSourceLoc&, const TVector<TString>&); |
John Kessenich | e6e7494 | 2016-06-11 16:43:14 -0600 | [diff] [blame] | 71 | TIntermTyped* handleVariable(const TSourceLoc&, TSymbol* symbol, const TString* string); |
John Kessenich | e01a9bc | 2016-03-12 20:11:22 -0700 | [diff] [blame] | 72 | TIntermTyped* handleBracketDereference(const TSourceLoc&, TIntermTyped* base, TIntermTyped* index); |
| 73 | void checkIndex(const TSourceLoc&, const TType&, int& index); |
| 74 | |
| 75 | void makeEditable(TSymbol*&); |
| 76 | TVariable* getEditableVariable(const char* name); |
| 77 | bool isIoResizeArray(const TType&) const; |
| 78 | void fixIoArraySize(const TSourceLoc&, TType&); |
| 79 | void handleIoResizeArrayAccess(const TSourceLoc&, TIntermTyped* base); |
| 80 | void checkIoArraysConsistency(const TSourceLoc&, bool tailOnly = false); |
| 81 | int getIoArrayImplicitSize() const; |
| 82 | void checkIoArrayConsistency(const TSourceLoc&, int requiredSize, const char* feature, TType&, const TString&); |
| 83 | |
| 84 | TIntermTyped* handleBinaryMath(const TSourceLoc&, const char* str, TOperator op, TIntermTyped* left, TIntermTyped* right); |
| 85 | TIntermTyped* handleUnaryMath(const TSourceLoc&, const char* str, TOperator op, TIntermTyped* childNode); |
| 86 | TIntermTyped* handleDotDereference(const TSourceLoc&, TIntermTyped* base, const TString& field); |
| 87 | TFunction* handleFunctionDeclarator(const TSourceLoc&, TFunction& function, bool prototype); |
| 88 | TIntermAggregate* handleFunctionDefinition(const TSourceLoc&, TFunction&); |
John Kessenich | 4678ca9 | 2016-05-13 09:33:42 -0600 | [diff] [blame] | 89 | void handleFunctionArgument(TFunction*, TIntermTyped*& arguments, TIntermTyped* newArg); |
John Kessenich | e01a9bc | 2016-03-12 20:11:22 -0700 | [diff] [blame] | 90 | TIntermTyped* handleFunctionCall(const TSourceLoc&, TFunction*, TIntermNode*); |
LoopDawg | 592860c | 2016-06-09 08:57:35 -0600 | [diff] [blame] | 91 | void decomposeIntrinsic(const TSourceLoc&, TIntermTyped*& node, TIntermNode* arguments); |
LoopDawg | 4886f69 | 2016-06-29 10:58:58 -0600 | [diff] [blame] | 92 | void decomposeSampleMethods(const TSourceLoc&, TIntermTyped*& node, TIntermNode* arguments); |
John Kessenich | e01a9bc | 2016-03-12 20:11:22 -0700 | [diff] [blame] | 93 | TIntermTyped* handleLengthMethod(const TSourceLoc&, TFunction*, TIntermNode*); |
| 94 | void addInputArgumentConversions(const TFunction&, TIntermNode*&) const; |
| 95 | TIntermTyped* addOutputArgumentConversions(const TFunction&, TIntermAggregate&) const; |
| 96 | void builtInOpCheck(const TSourceLoc&, const TFunction&, TIntermOperator&); |
John Kessenich | d016be1 | 2016-03-13 11:24:20 -0600 | [diff] [blame] | 97 | TFunction* handleConstructorCall(const TSourceLoc&, const TType&); |
John Kessenich | 630dd7d | 2016-06-12 23:52:12 -0600 | [diff] [blame] | 98 | void handleSemantic(TType& type, const TString& semantic); |
John Kessenich | e01a9bc | 2016-03-12 20:11:22 -0700 | [diff] [blame] | 99 | |
LoopDawg | a2b7991 | 2016-07-14 14:45:14 -0600 | [diff] [blame] | 100 | TIntermAggregate* handleSamplerTextureCombine(const TSourceLoc& loc, TIntermTyped* argTex, TIntermTyped* argSampler); |
| 101 | |
John Kessenich | e01a9bc | 2016-03-12 20:11:22 -0700 | [diff] [blame] | 102 | bool parseVectorFields(const TSourceLoc&, const TString&, int vecSize, TVectorFields&); |
| 103 | void assignError(const TSourceLoc&, const char* op, TString left, TString right); |
| 104 | void unaryOpError(const TSourceLoc&, const char* op, TString operand); |
| 105 | void binaryOpError(const TSourceLoc&, const char* op, TString left, TString right); |
| 106 | void variableCheck(TIntermTyped*& nodePtr); |
| 107 | void constantValueCheck(TIntermTyped* node, const char* token); |
| 108 | void integerCheck(const TIntermTyped* node, const char* token); |
| 109 | void globalCheck(const TSourceLoc&, const char* token); |
| 110 | bool constructorError(const TSourceLoc&, TIntermNode*, TFunction&, TOperator, TType&); |
| 111 | bool constructorTextureSamplerError(const TSourceLoc&, const TFunction&); |
| 112 | void arraySizeCheck(const TSourceLoc&, TIntermTyped* expr, TArraySize&); |
| 113 | void arraySizeRequiredCheck(const TSourceLoc&, const TArraySizes&); |
| 114 | void structArrayCheck(const TSourceLoc&, const TType& structure); |
| 115 | void arrayDimMerge(TType& type, const TArraySizes* sizes); |
| 116 | bool voidErrorCheck(const TSourceLoc&, const TString&, TBasicType); |
| 117 | void boolCheck(const TSourceLoc&, const TIntermTyped*); |
John Kessenich | e01a9bc | 2016-03-12 20:11:22 -0700 | [diff] [blame] | 118 | void globalQualifierFix(const TSourceLoc&, TQualifier&); |
| 119 | bool structQualifierErrorCheck(const TSourceLoc&, const TPublicType& pType); |
| 120 | void mergeQualifiers(const TSourceLoc&, TQualifier& dst, const TQualifier& src, bool force); |
| 121 | int computeSamplerTypeIndex(TSampler&); |
| 122 | TSymbol* redeclareBuiltinVariable(const TSourceLoc&, const TString&, const TQualifier&, const TShaderQualifiers&, bool& newDeclaration); |
| 123 | void redeclareBuiltinBlock(const TSourceLoc&, TTypeList& typeList, const TString& blockName, const TString* instanceName, TArraySizes* arraySizes); |
John Kessenich | 5aa59e2 | 2016-06-17 15:50:47 -0600 | [diff] [blame] | 124 | void paramFix(TType& type); |
John Kessenich | e01a9bc | 2016-03-12 20:11:22 -0700 | [diff] [blame] | 125 | void specializationCheck(const TSourceLoc&, const TType&, const char* op); |
| 126 | |
| 127 | void setLayoutQualifier(const TSourceLoc&, TPublicType&, TString&); |
| 128 | void setLayoutQualifier(const TSourceLoc&, TPublicType&, TString&, const TIntermTyped*); |
| 129 | void mergeObjectLayoutQualifiers(TQualifier& dest, const TQualifier& src, bool inheritOnly); |
| 130 | void checkNoShaderLayouts(const TSourceLoc&, const TShaderQualifiers&); |
| 131 | |
| 132 | const TFunction* findFunction(const TSourceLoc& loc, const TFunction& call, bool& builtIn); |
John Kessenich | 5e69ec6 | 2016-07-05 00:02:40 -0600 | [diff] [blame] | 133 | void declareTypedef(const TSourceLoc&, TString& identifier, const TType&, TArraySizes* typeArray = 0); |
John Kessenich | e01a9bc | 2016-03-12 20:11:22 -0700 | [diff] [blame] | 134 | TIntermNode* declareVariable(const TSourceLoc&, TString& identifier, const TType&, TArraySizes* typeArray = 0, TIntermTyped* initializer = 0); |
| 135 | TIntermTyped* addConstructor(const TSourceLoc&, TIntermNode*, const TType&, TOperator); |
| 136 | TIntermTyped* constructAggregate(TIntermNode*, const TType&, int, const TSourceLoc&); |
| 137 | TIntermTyped* constructBuiltIn(const TType&, TOperator, TIntermTyped*, const TSourceLoc&, bool subset); |
| 138 | void declareBlock(const TSourceLoc&, TTypeList& typeList, const TString* instanceName = 0, TArraySizes* arraySizes = 0); |
| 139 | void fixBlockLocations(const TSourceLoc&, TQualifier&, TTypeList&, bool memberWithLocation, bool memberWithoutLocation); |
| 140 | void fixBlockXfbOffsets(TQualifier&, TTypeList&); |
| 141 | void fixBlockUniformOffsets(TQualifier&, TTypeList&); |
| 142 | void addQualifierToExisting(const TSourceLoc&, TQualifier, const TString& identifier); |
| 143 | void addQualifierToExisting(const TSourceLoc&, TQualifier, TIdentifierList&); |
| 144 | void updateStandaloneQualifierDefaults(const TSourceLoc&, const TPublicType&); |
| 145 | void wrapupSwitchSubsequence(TIntermAggregate* statements, TIntermNode* branchNode); |
| 146 | TIntermNode* addSwitch(const TSourceLoc&, TIntermTyped* expression, TIntermAggregate* body); |
| 147 | |
| 148 | void updateImplicitArraySize(const TSourceLoc&, TIntermNode*, int index); |
| 149 | |
John Kessenich | 119f8f6 | 2016-06-05 15:44:07 -0600 | [diff] [blame] | 150 | void nestLooping() { ++loopNestingLevel; } |
| 151 | void unnestLooping() { --loopNestingLevel; } |
John Kessenich | 0d2b6de | 2016-06-05 11:23:11 -0600 | [diff] [blame] | 152 | void pushScope() { symbolTable.push(); } |
| 153 | void popScope() { symbolTable.pop(0); } |
| 154 | |
John Kessenich | d02dc5d | 2016-07-01 00:04:11 -0600 | [diff] [blame] | 155 | void pushSwitchSequence(TIntermSequence* sequence) { switchSequenceStack.push_back(sequence); } |
| 156 | void popSwitchSequence() { switchSequenceStack.pop_back(); } |
| 157 | |
John Kessenich | e01a9bc | 2016-03-12 20:11:22 -0700 | [diff] [blame] | 158 | protected: |
| 159 | void inheritGlobalDefaults(TQualifier& dst) const; |
| 160 | TVariable* makeInternalVariable(const char* name, const TType&) const; |
| 161 | TVariable* declareNonArray(const TSourceLoc&, TString& identifier, TType&, bool& newDeclaration); |
| 162 | void declareArray(const TSourceLoc&, TString& identifier, const TType&, TSymbol*&, bool& newDeclaration); |
| 163 | TIntermNode* executeInitializer(const TSourceLoc&, TIntermTyped* initializer, TVariable* variable); |
| 164 | TIntermTyped* convertInitializerList(const TSourceLoc&, const TType&, TIntermTyped* initializer); |
| 165 | TOperator mapTypeToConstructorOp(const TType&) const; |
LoopDawg | 5891070 | 2016-06-13 09:22:28 -0600 | [diff] [blame] | 166 | TOperator mapAtomicOp(const TSourceLoc& loc, TOperator op, bool isImage); |
John Kessenich | e01a9bc | 2016-03-12 20:11:22 -0700 | [diff] [blame] | 167 | void outputMessage(const TSourceLoc&, const char* szReason, const char* szToken, |
| 168 | const char* szExtraInfoFormat, TPrefixType prefix, |
| 169 | va_list args); |
| 170 | |
| 171 | // Current state of parsing |
| 172 | struct TPragma contextPragma; |
| 173 | int loopNestingLevel; // 0 if outside all loops |
| 174 | int structNestingLevel; // 0 if outside blocks and structures |
| 175 | int controlFlowNestingLevel; // 0 if outside all flow control |
John Kessenich | e01a9bc | 2016-03-12 20:11:22 -0700 | [diff] [blame] | 176 | TList<TIntermSequence*> switchSequenceStack; // case, node, case, case, node, ...; ensure only one node between cases; stack of them for nesting |
John Kessenich | e01a9bc | 2016-03-12 20:11:22 -0700 | [diff] [blame] | 177 | bool inEntrypoint; // if inside a function, true if the function is the entry point |
| 178 | bool postMainReturn; // if inside a function, true if the function is the entry point and this is after a return statement |
| 179 | const TType* currentFunctionType; // the return type of the function that's currently being parsed |
| 180 | bool functionReturnsValue; // true if a non-void function has a return |
| 181 | const TString* blockName; |
| 182 | TQualifier currentBlockQualifier; |
| 183 | TBuiltInResource resources; |
| 184 | TLimits& limits; |
| 185 | |
| 186 | HlslParseContext(HlslParseContext&); |
| 187 | HlslParseContext& operator=(HlslParseContext&); |
| 188 | |
John Kessenich | e01a9bc | 2016-03-12 20:11:22 -0700 | [diff] [blame] | 189 | static const int maxSamplerIndex = EsdNumDims * (EbtNumTypes * (2 * 2 * 2)); // see computeSamplerTypeIndex() |
| 190 | bool afterEOF; |
| 191 | TQualifier globalBufferDefaults; |
| 192 | TQualifier globalUniformDefaults; |
| 193 | TQualifier globalInputDefaults; |
| 194 | TQualifier globalOutputDefaults; |
| 195 | TString currentCaller; // name of last function body entered (not valid when at global scope) |
| 196 | TIdSetType inductiveLoopIds; |
| 197 | TVector<TIntermTyped*> needsIndexLimitationChecking; |
| 198 | |
| 199 | // |
| 200 | // Geometry shader input arrays: |
| 201 | // - array sizing is based on input primitive and/or explicit size |
| 202 | // |
| 203 | // Tessellation control output arrays: |
| 204 | // - array sizing is based on output layout(vertices=...) and/or explicit size |
| 205 | // |
| 206 | // Both: |
| 207 | // - array sizing is retroactive |
| 208 | // - built-in block redeclarations interact with this |
| 209 | // |
| 210 | // Design: |
| 211 | // - use a per-context "resize-list", a list of symbols whose array sizes |
| 212 | // can be fixed |
| 213 | // |
| 214 | // - the resize-list starts empty at beginning of user-shader compilation, it does |
| 215 | // not have built-ins in it |
| 216 | // |
| 217 | // - on built-in array use: copyUp() symbol and add it to the resize-list |
| 218 | // |
| 219 | // - on user array declaration: add it to the resize-list |
| 220 | // |
| 221 | // - on block redeclaration: copyUp() symbol and add it to the resize-list |
| 222 | // * note, that appropriately gives an error if redeclaring a block that |
| 223 | // was already used and hence already copied-up |
| 224 | // |
| 225 | // - on seeing a layout declaration that sizes the array, fix everything in the |
| 226 | // resize-list, giving errors for mismatch |
| 227 | // |
| 228 | // - on seeing an array size declaration, give errors on mismatch between it and previous |
| 229 | // array-sizing declarations |
| 230 | // |
| 231 | TVector<TSymbol*> ioArraySymbolResizeList; |
| 232 | }; |
| 233 | |
| 234 | } // end namespace glslang |
| 235 | |
| 236 | #endif // HLSL_PARSE_INCLUDED_ |