blob: 5b585dcdac3c0265ec166943c324f366db65ebdb [file] [log] [blame]
John Kesseniche01a9bc2016-03-12 20:11:22 -07001//
John Kessenich927608b2017-01-06 12:34:14 -07002// Copyright (C) 2016 Google, Inc.
3// Copyright (C) 2016 LunarG, Inc.
John Kesseniche01a9bc2016-03-12 20:11:22 -07004//
John Kessenich927608b2017-01-06 12:34:14 -07005// All rights reserved.
John Kesseniche01a9bc2016-03-12 20:11:22 -07006//
John Kessenich927608b2017-01-06 12:34:14 -07007// Redistribution and use in source and binary forms, with or without
8// modification, are permitted provided that the following conditions
9// are met:
John Kesseniche01a9bc2016-03-12 20:11:22 -070010//
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//
John Kessenich927608b2017-01-06 12:34:14 -070023// 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.
John Kesseniche01a9bc2016-03-12 20:11:22 -070035//
36#ifndef HLSL_PARSE_INCLUDED_
37#define HLSL_PARSE_INCLUDED_
38
39#include "../glslang/MachineIndependent/parseVersions.h"
40#include "../glslang/MachineIndependent/ParseHelper.h"
41
42namespace glslang {
43
steve-lunarg1868b142016-10-20 13:07:10 -060044class TAttributeMap; // forward declare
45
John Kesseniche01a9bc2016-03-12 20:11:22 -070046class HlslParseContext : public TParseContextBase {
47public:
48 HlslParseContext(TSymbolTable&, TIntermediate&, bool parsingBuiltins,
John Kessenichb901ade2016-06-16 20:59:42 -060049 int version, EProfile, const SpvVersion& spvVersion, EShLanguage, TInfoSink&,
steve-lunargf1e0c872016-10-31 15:13:43 -060050 const TString sourceEntryPointName,
John Kesseniche01a9bc2016-03-12 20:11:22 -070051 bool forwardCompatible = false, EShMessages messages = EShMsgDefault);
52 virtual ~HlslParseContext();
Henrik Rydgård9a931b32016-12-21 12:48:08 +010053 void initializeExtensionBehavior() override;
LoopDawg62561462016-07-22 20:46:03 -060054
Henrik Rydgård9a931b32016-12-21 12:48:08 +010055 void setLimits(const TBuiltInResource&) override;
56 bool parseShaderStrings(TPpContext&, TInputScanner& input, bool versionWillBeError = false) override;
57 virtual const char* getGlobalUniformBlockName() override { return "$Global"; }
John Kesseniche01a9bc2016-03-12 20:11:22 -070058
Henrik Rydgård9a931b32016-12-21 12:48:08 +010059 void reservedPpErrorCheck(const TSourceLoc&, const char* /*name*/, const char* /*op*/) override { }
60 bool lineContinuationCheck(const TSourceLoc&, bool /*endOfComment*/) override { return true; }
61 bool lineDirectiveShouldSetNextLine() const override { return true; }
John Kesseniche01a9bc2016-03-12 20:11:22 -070062 bool builtInName(const TString&);
63
Henrik Rydgård9a931b32016-12-21 12:48:08 +010064 void handlePragma(const TSourceLoc&, const TVector<TString>&) override;
John Kesseniche6e74942016-06-11 16:43:14 -060065 TIntermTyped* handleVariable(const TSourceLoc&, TSymbol* symbol, const TString* string);
John Kesseniche01a9bc2016-03-12 20:11:22 -070066 TIntermTyped* handleBracketDereference(const TSourceLoc&, TIntermTyped* base, TIntermTyped* index);
steve-lunarg6b43d272016-10-06 20:12:24 -060067 TIntermTyped* handleBracketOperator(const TSourceLoc&, TIntermTyped* base, TIntermTyped* index);
John Kesseniche01a9bc2016-03-12 20:11:22 -070068 void checkIndex(const TSourceLoc&, const TType&, int& index);
69
John Kesseniche01a9bc2016-03-12 20:11:22 -070070 TIntermTyped* handleBinaryMath(const TSourceLoc&, const char* str, TOperator op, TIntermTyped* left, TIntermTyped* right);
71 TIntermTyped* handleUnaryMath(const TSourceLoc&, const char* str, TOperator op, TIntermTyped* childNode);
72 TIntermTyped* handleDotDereference(const TSourceLoc&, TIntermTyped* base, const TString& field);
John Kessenich7dc630f2016-09-16 01:44:43 -060073 void assignLocations(TVariable& variable);
John Kessenicha3051662016-09-02 19:13:36 -060074 TFunction& handleFunctionDeclarator(const TSourceLoc&, TFunction& function, bool prototype);
John Kessenich02467d82017-01-19 15:41:47 -070075 TIntermAggregate* handleFunctionDefinition(const TSourceLoc&, TFunction&, const TAttributeMap&, TIntermNode*& entryPointTree);
76 TIntermNode* transformEntryPoint(const TSourceLoc&, TFunction&, const TAttributeMap&);
John Kessenicha3051662016-09-02 19:13:36 -060077 void handleFunctionBody(const TSourceLoc&, TFunction&, TIntermNode* functionBody, TIntermNode*& node);
John Kessenich727b3742017-02-03 17:57:55 -070078 void remapEntryPointIO(TFunction& function, TVariable*& returnValue, TVector<TVariable*>& inputs, TVector<TVariable*>& outputs);
John Kessenich6fccb3c2016-09-19 16:01:41 -060079 void remapNonEntryPointIO(TFunction& function);
steve-lunargc4a13072016-08-09 11:28:03 -060080 TIntermNode* handleReturnValue(const TSourceLoc&, TIntermTyped*);
John Kessenich4678ca92016-05-13 09:33:42 -060081 void handleFunctionArgument(TFunction*, TIntermTyped*& arguments, TIntermTyped* newArg);
John Kessenichfdf63472017-01-13 12:27:52 -070082 TIntermTyped* handleAssign(const TSourceLoc&, TOperator, TIntermTyped* left, TIntermTyped* right);
83 TIntermTyped* handleAssignToMatrixSwizzle(const TSourceLoc&, TOperator, TIntermTyped* left, TIntermTyped* right);
steve-lunarg26d31452016-12-23 18:56:57 -070084 TIntermTyped* handleFunctionCall(const TSourceLoc&, TFunction*, TIntermTyped*);
LoopDawg592860c2016-06-09 08:57:35 -060085 void decomposeIntrinsic(const TSourceLoc&, TIntermTyped*& node, TIntermNode* arguments);
LoopDawg4886f692016-06-29 10:58:58 -060086 void decomposeSampleMethods(const TSourceLoc&, TIntermTyped*& node, TIntermNode* arguments);
steve-lunarg5da1f032017-02-12 17:50:28 -070087 void decomposeStructBufferMethods(const TSourceLoc&, TIntermTyped*& node, TIntermNode* arguments);
steve-lunargf49cdf42016-11-17 15:04:20 -070088 void decomposeGeometryMethods(const TSourceLoc&, TIntermTyped*& node, TIntermNode* arguments);
John Kesseniche01a9bc2016-03-12 20:11:22 -070089 TIntermTyped* handleLengthMethod(const TSourceLoc&, TFunction*, TIntermNode*);
steve-lunarg26d31452016-12-23 18:56:57 -070090 void addInputArgumentConversions(const TFunction&, TIntermTyped*&);
steve-lunargef33ec02016-11-02 12:42:34 -060091 TIntermTyped* addOutputArgumentConversions(const TFunction&, TIntermOperator&);
John Kesseniche01a9bc2016-03-12 20:11:22 -070092 void builtInOpCheck(const TSourceLoc&, const TFunction&, TIntermOperator&);
John Kessenichd016be12016-03-13 11:24:20 -060093 TFunction* handleConstructorCall(const TSourceLoc&, const TType&);
John Kessenich7735b942016-09-05 12:40:06 -060094 void handleSemantic(TSourceLoc, TQualifier&, const TString& semantic);
95 void handlePackOffset(const TSourceLoc&, TQualifier&, const glslang::TString& location,
John Kessenichb38f0712016-07-30 10:29:54 -060096 const glslang::TString* component);
John Kessenich7735b942016-09-05 12:40:06 -060097 void handleRegister(const TSourceLoc&, TQualifier&, const glslang::TString* profile, const glslang::TString& desc,
John Kessenichcfd7ce82016-09-05 16:03:12 -060098 int subComponent, const glslang::TString*);
John Kesseniche01a9bc2016-03-12 20:11:22 -070099
LoopDawga2b79912016-07-14 14:45:14 -0600100 TIntermAggregate* handleSamplerTextureCombine(const TSourceLoc& loc, TIntermTyped* argTex, TIntermTyped* argSampler);
101
John Kessenichc142c882017-01-13 19:34:22 -0700102 bool parseMatrixSwizzleSelector(const TSourceLoc&, const TString&, int cols, int rows, TSwizzleSelectors<TMatrixSelector>&);
103 int getMatrixComponentsColumn(int rows, const TSwizzleSelectors<TMatrixSelector>&);
John Kesseniche01a9bc2016-03-12 20:11:22 -0700104 void assignError(const TSourceLoc&, const char* op, TString left, TString right);
105 void unaryOpError(const TSourceLoc&, const char* op, TString operand);
106 void binaryOpError(const TSourceLoc&, const char* op, TString left, TString right);
107 void variableCheck(TIntermTyped*& nodePtr);
108 void constantValueCheck(TIntermTyped* node, const char* token);
109 void integerCheck(const TIntermTyped* node, const char* token);
110 void globalCheck(const TSourceLoc&, const char* token);
111 bool constructorError(const TSourceLoc&, TIntermNode*, TFunction&, TOperator, TType&);
112 bool constructorTextureSamplerError(const TSourceLoc&, const TFunction&);
113 void arraySizeCheck(const TSourceLoc&, TIntermTyped* expr, TArraySize&);
114 void arraySizeRequiredCheck(const TSourceLoc&, const TArraySizes&);
115 void structArrayCheck(const TSourceLoc&, const TType& structure);
116 void arrayDimMerge(TType& type, const TArraySizes* sizes);
117 bool voidErrorCheck(const TSourceLoc&, const TString&, TBasicType);
118 void boolCheck(const TSourceLoc&, const TIntermTyped*);
John Kesseniche01a9bc2016-03-12 20:11:22 -0700119 void globalQualifierFix(const TSourceLoc&, TQualifier&);
120 bool structQualifierErrorCheck(const TSourceLoc&, const TPublicType& pType);
John Kessenich34e7ee72016-09-16 17:10:39 -0600121 void mergeQualifiers(TQualifier& dst, const TQualifier& src);
John Kesseniche01a9bc2016-03-12 20:11:22 -0700122 int computeSamplerTypeIndex(TSampler&);
John Kessenichd3f11222016-11-05 10:15:53 -0600123 TSymbol* redeclareBuiltinVariable(const TSourceLoc&, const TString&, const TQualifier&, const TShaderQualifiers&);
John Kesseniche01a9bc2016-03-12 20:11:22 -0700124 void redeclareBuiltinBlock(const TSourceLoc&, TTypeList& typeList, const TString& blockName, const TString* instanceName, TArraySizes* arraySizes);
John Kessenich5aa59e22016-06-17 15:50:47 -0600125 void paramFix(TType& type);
John Kesseniche01a9bc2016-03-12 20:11:22 -0700126 void specializationCheck(const TSourceLoc&, const TType&, const char* op);
127
John Kessenichb9e39122016-08-17 10:22:08 -0600128 void setLayoutQualifier(const TSourceLoc&, TQualifier&, TString&);
129 void setLayoutQualifier(const TSourceLoc&, TQualifier&, TString&, const TIntermTyped*);
John Kesseniche01a9bc2016-03-12 20:11:22 -0700130 void mergeObjectLayoutQualifiers(TQualifier& dest, const TQualifier& src, bool inheritOnly);
131 void checkNoShaderLayouts(const TSourceLoc&, const TShaderQualifiers&);
132
steve-lunarg26d31452016-12-23 18:56:57 -0700133 const TFunction* findFunction(const TSourceLoc& loc, TFunction& call, bool& builtIn, TIntermTyped*& args);
John Kessenichdd402602017-02-08 13:59:30 -0700134 void declareTypedef(const TSourceLoc&, TString& identifier, const TType&);
John Kessenich727b3742017-02-03 17:57:55 -0700135 void declareStruct(const TSourceLoc&, TString& structName, TType&);
John Kessenich854fe242017-03-02 14:30:59 -0700136 TSymbol* lookupUserType(const TString&, TType&);
John Kesseniche82061d2016-09-27 14:38:57 -0600137 TIntermNode* declareVariable(const TSourceLoc&, TString& identifier, TType&, TIntermTyped* initializer = 0);
John Kessenich98ad4852016-11-27 17:39:07 -0700138 void lengthenList(const TSourceLoc&, TIntermSequence& list, int size);
John Kessenicha26a5172016-07-28 15:29:35 -0600139 TIntermTyped* addConstructor(const TSourceLoc&, TIntermNode*, const TType&);
John Kesseniche01a9bc2016-03-12 20:11:22 -0700140 TIntermTyped* constructAggregate(TIntermNode*, const TType&, int, const TSourceLoc&);
141 TIntermTyped* constructBuiltIn(const TType&, TOperator, TIntermTyped*, const TSourceLoc&, bool subset);
John Kessenich3d157c52016-07-25 16:05:33 -0600142 void declareBlock(const TSourceLoc&, TType&, const TString* instanceName = 0, TArraySizes* arraySizes = 0);
Henrik Rydgård9a931b32016-12-21 12:48:08 +0100143 void finalizeGlobalUniformBlockLayout(TVariable& block) override;
John Kesseniche01a9bc2016-03-12 20:11:22 -0700144 void fixBlockLocations(const TSourceLoc&, TQualifier&, TTypeList&, bool memberWithLocation, bool memberWithoutLocation);
145 void fixBlockXfbOffsets(TQualifier&, TTypeList&);
John Kessenich6dbc0a72016-09-27 19:13:05 -0600146 void fixBlockUniformOffsets(const TQualifier&, TTypeList&);
John Kesseniche01a9bc2016-03-12 20:11:22 -0700147 void addQualifierToExisting(const TSourceLoc&, TQualifier, const TString& identifier);
148 void addQualifierToExisting(const TSourceLoc&, TQualifier, TIdentifierList&);
149 void updateStandaloneQualifierDefaults(const TSourceLoc&, const TPublicType&);
150 void wrapupSwitchSubsequence(TIntermAggregate* statements, TIntermNode* branchNode);
151 TIntermNode* addSwitch(const TSourceLoc&, TIntermTyped* expression, TIntermAggregate* body);
152
153 void updateImplicitArraySize(const TSourceLoc&, TIntermNode*, int index);
154
John Kessenicha1e2d492016-09-20 13:22:58 -0600155 void nestLooping() { ++loopNestingLevel; }
156 void unnestLooping() { --loopNestingLevel; }
157 void nestAnnotations() { ++annotationNestingLevel; }
158 void unnestAnnotations() { --annotationNestingLevel; }
John Kessenich6dbc0a72016-09-27 19:13:05 -0600159 int getAnnotationNestingLevel() { return annotationNestingLevel; }
John Kessenicha1e2d492016-09-20 13:22:58 -0600160 void pushScope() { symbolTable.push(); }
161 void popScope() { symbolTable.pop(0); }
John Kessenich0d2b6de2016-06-05 11:23:11 -0600162
John Kessenichd02dc5d2016-07-01 00:04:11 -0600163 void pushSwitchSequence(TIntermSequence* sequence) { switchSequenceStack.push_back(sequence); }
164 void popSwitchSequence() { switchSequenceStack.pop_back(); }
165
John Kessenich65ee2302017-02-06 18:44:52 -0700166 virtual void growGlobalUniformBlock(TSourceLoc&, TType&, TString& memberName, TTypeList* typeList = nullptr) override;
John Kessenich88c44642017-02-03 14:06:36 -0700167
steve-lunarg90707962016-10-07 19:35:40 -0600168 // Apply L-value conversions. E.g, turning a write to a RWTexture into an ImageStore.
169 TIntermTyped* handleLvalue(const TSourceLoc&, const char* op, TIntermTyped* node);
steve-lunarg0de16da2016-10-08 10:54:52 -0600170 bool lValueErrorCheck(const TSourceLoc&, const char* op, TIntermTyped*) override;
steve-lunarg90707962016-10-07 19:35:40 -0600171
steve-lunarg4f2da272016-10-10 15:24:57 -0600172 TLayoutFormat getLayoutFromTxType(const TSourceLoc&, const TType&);
173
steve-lunargf49cdf42016-11-17 15:04:20 -0700174 bool handleOutputGeometry(const TSourceLoc&, const TLayoutGeometry& geometry);
175 bool handleInputGeometry(const TSourceLoc&, const TLayoutGeometry& geometry);
176
steve-lunargf1e0c872016-10-31 15:13:43 -0600177 // Potentially rename shader entry point function
178 void renameShaderFunction(TString*& name) const;
179
steve-lunarga2b01a02016-11-28 17:09:54 -0700180 // Reset data for incrementally built referencing of flattened composite structures
181 void initFlattening() { flattenLevel.push_back(0); flattenOffset.push_back(0); }
182 void finalizeFlattening() { flattenLevel.pop_back(); flattenOffset.pop_back(); }
183
steve-lunargdd8287a2017-02-23 18:04:12 -0700184 // Share struct buffer deep types
185 void shareStructBufferType(TType&);
186
John Kesseniche01a9bc2016-03-12 20:11:22 -0700187protected:
steve-lunarga2b01a02016-11-28 17:09:54 -0700188 struct TFlattenData {
189 TFlattenData() : nextBinding(TQualifier::layoutBindingEnd) { }
190 TFlattenData(int nb) : nextBinding(nb) { }
191
192 TVector<TVariable*> members; // individual flattened variables
193 TVector<int> offsets; // offset to next tree level
194 int nextBinding; // next binding to use.
195 };
196
John Kessenichaa6d5622016-12-30 16:42:57 -0700197 void fixConstInit(const TSourceLoc&, TString& identifier, TType& type, TIntermTyped*& initializer);
John Kesseniche01a9bc2016-03-12 20:11:22 -0700198 void inheritGlobalDefaults(TQualifier& dst) const;
199 TVariable* makeInternalVariable(const char* name, const TType&) const;
steve-lunarga2e75312016-12-14 15:22:25 -0700200 TVariable* makeInternalVariable(const TString& name, const TType& type) const {
201 return makeInternalVariable(name.c_str(), type);
202 }
steve-lunarga2b01a02016-11-28 17:09:54 -0700203 TVariable* declareNonArray(const TSourceLoc&, TString& identifier, TType&, bool track);
John Kessenichd3f11222016-11-05 10:15:53 -0600204 void declareArray(const TSourceLoc&, TString& identifier, const TType&, TSymbol*&, bool track);
John Kesseniche01a9bc2016-03-12 20:11:22 -0700205 TIntermNode* executeInitializer(const TSourceLoc&, TIntermTyped* initializer, TVariable* variable);
206 TIntermTyped* convertInitializerList(const TSourceLoc&, const TType&, TIntermTyped* initializer);
John Kessenich8ce6e2b2016-11-27 23:00:14 -0700207 bool isZeroConstructor(const TIntermNode*);
LoopDawg58910702016-06-13 09:22:28 -0600208 TOperator mapAtomicOp(const TSourceLoc& loc, TOperator op, bool isImage);
John Kesseniche01a9bc2016-03-12 20:11:22 -0700209
steve-lunarg90707962016-10-07 19:35:40 -0600210 // Return true if this node requires L-value conversion (e.g, to an imageStore).
211 bool shouldConvertLValue(const TIntermNode*) const;
212
steve-lunarge0b9deb2016-09-16 13:26:37 -0600213 // Array and struct flattening
steve-lunarga2e75312016-12-14 15:22:25 -0700214 TIntermTyped* flattenAccess(TIntermTyped* base, int member);
steve-lunarge0b9deb2016-09-16 13:26:37 -0600215 bool shouldFlattenUniform(const TType&) const;
steve-lunarga2b01a02016-11-28 17:09:54 -0700216 bool wasFlattened(const TIntermTyped* node) const;
217 bool wasFlattened(int id) const { return flattenMap.find(id) != flattenMap.end(); }
218 int addFlattenedMember(const TSourceLoc& loc, const TVariable&, const TType&, TFlattenData&, const TString& name, bool track);
219 bool isFinalFlattening(const TType& type) const { return !(type.isStruct() || type.isArray()); }
220
steve-lunarga2e75312016-12-14 15:22:25 -0700221 // Structure splitting (splits interstage builtin types into its own struct)
steve-lunarg132d3312016-12-19 15:48:01 -0700222 TIntermTyped* splitAccessStruct(const TSourceLoc& loc, TIntermTyped*& base, int& member);
223 void splitAccessArray(const TSourceLoc& loc, TIntermTyped* base, TIntermTyped* index);
224 TType& split(TType& type, TString name, const TType* outerStructType = nullptr);
steve-lunarga2e75312016-12-14 15:22:25 -0700225 void split(TIntermTyped*);
226 void split(const TVariable&);
227 bool wasSplit(const TIntermTyped* node) const;
228 bool wasSplit(int id) const { return splitIoVars.find(id) != splitIoVars.end(); }
229 TVariable* getSplitIoVar(const TIntermTyped* node) const;
230 TVariable* getSplitIoVar(const TVariable* var) const;
231 TVariable* getSplitIoVar(int id) const;
232 void addInterstageIoToLinkage();
steve-lunarg858c9282017-01-07 08:54:10 -0700233 void addPatchConstantInvocation();
steve-lunarga2e75312016-12-14 15:22:25 -0700234
steve-lunarge0b9deb2016-09-16 13:26:37 -0600235 void flatten(const TSourceLoc& loc, const TVariable& variable);
steve-lunarga2b01a02016-11-28 17:09:54 -0700236 int flatten(const TSourceLoc& loc, const TVariable& variable, const TType&, TFlattenData&, TString name);
237 int flattenStruct(const TSourceLoc& loc, const TVariable& variable, const TType&, TFlattenData&, TString name);
238 int flattenArray(const TSourceLoc& loc, const TVariable& variable, const TType&, TFlattenData&, TString name);
steve-lunarge0b9deb2016-09-16 13:26:37 -0600239
John Kessenichbf472862017-02-05 20:27:30 -0700240 bool hasUniform(const TQualifier& qualifier) const;
241 void clearUniform(TQualifier& qualifier);
242 bool isInputBuiltIn(const TQualifier& qualifier) const;
243 bool hasInput(const TQualifier& qualifier) const;
244 void correctOutput(TQualifier& qualifier);
245 bool isOutputBuiltIn(const TQualifier& qualifier) const;
246 bool hasOutput(const TQualifier& qualifier) const;
247 void correctInput(TQualifier& qualifier);
248 void correctUniform(TQualifier& qualifier);
249 void clearUniformInputOutput(TQualifier& qualifier);
250
steve-lunarg5da1f032017-02-12 17:50:28 -0700251 // Test method names
252 bool isSamplerMethod(const TString& name) const;
253 bool isStructBufferMethod(const TString& name) const;
254
steve-lunargdd8287a2017-02-23 18:04:12 -0700255 TType* getStructBufferContentType(const TType& type) const;
256 bool isStructBufferType(const TType& type) const { return getStructBufferContentType(type) != nullptr; }
257 TIntermTyped* indexStructBufferContent(const TSourceLoc& loc, TIntermTyped* buffer) const;
258
259 // Return true if this type is a reference. This is not currently a type method in case that's
260 // a language specific answer.
261 bool isReference(const TType& type) const { return isStructBufferType(type); }
262
steve-lunarg858c9282017-01-07 08:54:10 -0700263 // Pass through to base class after remembering builtin mappings.
264 using TParseContextBase::trackLinkage;
265 void trackLinkage(TSymbol& variable) override;
266
Alex Szpakowski7d39ad52017-01-08 17:54:48 -0400267 void finish() override; // post-processing
steve-lunarga2e75312016-12-14 15:22:25 -0700268
John Kesseniche01a9bc2016-03-12 20:11:22 -0700269 // Current state of parsing
270 struct TPragma contextPragma;
271 int loopNestingLevel; // 0 if outside all loops
John Kessenicha1e2d492016-09-20 13:22:58 -0600272 int annotationNestingLevel; // 0 if outside all annotations
John Kesseniche01a9bc2016-03-12 20:11:22 -0700273 int structNestingLevel; // 0 if outside blocks and structures
274 int controlFlowNestingLevel; // 0 if outside all flow control
John Kesseniche01a9bc2016-03-12 20:11:22 -0700275 TList<TIntermSequence*> switchSequenceStack; // case, node, case, case, node, ...; ensure only one node between cases; stack of them for nesting
John Kessenich517fe7a2016-11-26 13:31:47 -0700276 bool postEntryPointReturn; // if inside a function, true if the function is the entry point and this is after a return statement
John Kesseniche01a9bc2016-03-12 20:11:22 -0700277 const TType* currentFunctionType; // the return type of the function that's currently being parsed
278 bool functionReturnsValue; // true if a non-void function has a return
John Kesseniche01a9bc2016-03-12 20:11:22 -0700279 TBuiltInResource resources;
280 TLimits& limits;
281
282 HlslParseContext(HlslParseContext&);
283 HlslParseContext& operator=(HlslParseContext&);
284
John Kesseniche01a9bc2016-03-12 20:11:22 -0700285 static const int maxSamplerIndex = EsdNumDims * (EbtNumTypes * (2 * 2 * 2)); // see computeSamplerTypeIndex()
John Kesseniche01a9bc2016-03-12 20:11:22 -0700286 TQualifier globalBufferDefaults;
287 TQualifier globalUniformDefaults;
288 TQualifier globalInputDefaults;
289 TQualifier globalOutputDefaults;
290 TString currentCaller; // name of last function body entered (not valid when at global scope)
291 TIdSetType inductiveLoopIds;
292 TVector<TIntermTyped*> needsIndexLimitationChecking;
John Kesseniche01a9bc2016-03-12 20:11:22 -0700293
294 //
295 // Geometry shader input arrays:
296 // - array sizing is based on input primitive and/or explicit size
297 //
298 // Tessellation control output arrays:
299 // - array sizing is based on output layout(vertices=...) and/or explicit size
300 //
301 // Both:
302 // - array sizing is retroactive
303 // - built-in block redeclarations interact with this
304 //
305 // Design:
306 // - use a per-context "resize-list", a list of symbols whose array sizes
307 // can be fixed
308 //
309 // - the resize-list starts empty at beginning of user-shader compilation, it does
310 // not have built-ins in it
311 //
312 // - on built-in array use: copyUp() symbol and add it to the resize-list
313 //
314 // - on user array declaration: add it to the resize-list
315 //
316 // - on block redeclaration: copyUp() symbol and add it to the resize-list
317 // * note, that appropriately gives an error if redeclaring a block that
318 // was already used and hence already copied-up
319 //
John Kessenichecba76f2017-01-06 00:34:48 -0700320 // - on seeing a layout declaration that sizes the array, fix everything in the
John Kesseniche01a9bc2016-03-12 20:11:22 -0700321 // resize-list, giving errors for mismatch
322 //
323 // - on seeing an array size declaration, give errors on mismatch between it and previous
324 // array-sizing declarations
325 //
326 TVector<TSymbol*> ioArraySymbolResizeList;
John Kessenichcd0a78a2016-09-09 16:32:09 -0600327
steve-lunarga2b01a02016-11-28 17:09:54 -0700328 TMap<int, TFlattenData> flattenMap;
329 TVector<int> flattenLevel; // nested postfix operator level for flattening
330 TVector<int> flattenOffset; // cumulative offset for flattening
331
John Kessenichbf472862017-02-05 20:27:30 -0700332 // IO-type map. Maps a pure symbol-table form of a structure-member list into
333 // each of the (up to) three kinds of IO, as each as different allowed decorations,
334 // but HLSL allows mixing all in the same structure.
335 struct tIoKinds {
336 TTypeList* input;
337 TTypeList* output;
338 TTypeList* uniform;
339 };
340 TMap<const TTypeList*, tIoKinds> ioTypeMap;
steve-lunarga2e75312016-12-14 15:22:25 -0700341
342 // Structure splitting data:
steve-lunarg132d3312016-12-19 15:48:01 -0700343 TMap<int, TVariable*> splitIoVars; // variables with the builtin interstage IO removed, indexed by unique ID.
344
steve-lunargdd8287a2017-02-23 18:04:12 -0700345 // Structuredbuffer shared types. Typically there are only a few.
346 TVector<TType*> structBufferTypes;
347
steve-lunarg132d3312016-12-19 15:48:01 -0700348 // The builtin interstage IO map considers e.g, EvqPosition on input and output separately, so that we
349 // can build the linkage correctly if position appears on both sides. Otherwise, multiple positions
350 // are considered identical.
351 struct tInterstageIoData {
steve-lunarg858c9282017-01-07 08:54:10 -0700352 tInterstageIoData(TBuiltInVariable bi, TStorageQualifier q) :
353 builtIn(bi), storage(q) { }
354
steve-lunarg132d3312016-12-19 15:48:01 -0700355 tInterstageIoData(const TType& memberType, const TType& storageType) :
356 builtIn(memberType.getQualifier().builtIn),
357 storage(storageType.getQualifier().storage) { }
358
359 TBuiltInVariable builtIn;
360 TStorageQualifier storage;
361
362 // ordering for maps
363 bool operator<(const tInterstageIoData d) const {
364 return (builtIn != d.builtIn) ? (builtIn < d.builtIn) : (storage < d.storage);
365 }
366 };
367
John Kessenichbf472862017-02-05 20:27:30 -0700368 TMap<tInterstageIoData, TVariable*> interstageBuiltInIo; // individual builtin interstage IO vars, indexed by builtin type.
steve-lunarg132d3312016-12-19 15:48:01 -0700369
370 // We have to move array references to structs containing builtin interstage IO to the split variables.
371 // This is only handled for one level. This stores the index, because we'll need it in the future, since
372 // unlike normal array references, here the index happens before we discover what it applies to.
373 TIntermTyped* builtInIoIndex;
374 TIntermTyped* builtInIoBase;
steve-lunarga2e75312016-12-14 15:22:25 -0700375
John Kessenich7dc630f2016-09-16 01:44:43 -0600376 unsigned int nextInLocation;
377 unsigned int nextOutLocation;
steve-lunargf1e0c872016-10-31 15:13:43 -0600378
steve-lunarg858c9282017-01-07 08:54:10 -0700379 TString sourceEntryPointName;
380 TFunction* entryPointFunction;
381 TIntermNode* entryPointFunctionBody;
382
383 TString patchConstantFunctionName; // hull shader patch constant function name, from function level attribute.
384 TMap<TBuiltInVariable, TSymbol*> builtInLinkageSymbols; // used for tessellation, finding declared builtins
385
John Kesseniche01a9bc2016-03-12 20:11:22 -0700386};
387
388} // end namespace glslang
389
390#endif // HLSL_PARSE_INCLUDED_