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