blob: 1e43deaaca7d842112127cae2dba701ee9dcece4 [file] [log] [blame]
John Kesseniche01a9bc2016-03-12 20:11:22 -07001//
2//Copyright (C) 2016 Google, Inc.
LoopDawg592860c2016-06-09 08:57:35 -06003//Copyright (C) 2016 LunarG, Inc.
John Kesseniche01a9bc2016-03-12 20:11:22 -07004//
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
42namespace glslang {
43
44class HlslParseContext : public TParseContextBase {
45public:
46 HlslParseContext(TSymbolTable&, TIntermediate&, bool parsingBuiltins,
John Kessenichb901ade2016-06-16 20:59:42 -060047 int version, EProfile, const SpvVersion& spvVersion, EShLanguage, TInfoSink&,
John Kesseniche01a9bc2016-03-12 20:11:22 -070048 bool forwardCompatible = false, EShMessages messages = EShMsgDefault);
49 virtual ~HlslParseContext();
LoopDawg62561462016-07-22 20:46:03 -060050 void initializeExtensionBehavior();
51
John Kesseniche01a9bc2016-03-12 20:11:22 -070052 void setLimits(const TBuiltInResource&);
53 bool parseShaderStrings(TPpContext&, TInputScanner& input, bool versionWillBeError = false);
John Kesseniche01a9bc2016-03-12 20:11:22 -070054
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 Kessenich7f349c72016-07-08 22:09:10 -060064 void reservedPpErrorCheck(const TSourceLoc&, const char* /*name*/, const char* /*op*/) { }
65 bool lineContinuationCheck(const TSourceLoc&, bool /*endOfComment*/) { return true; }
John Kesseniche01a9bc2016-03-12 20:11:22 -070066 bool lineDirectiveShouldSetNextLine() const { return true; }
67 bool builtInName(const TString&);
68
69 void handlePragma(const TSourceLoc&, const TVector<TString>&);
John Kesseniche6e74942016-06-11 16:43:14 -060070 TIntermTyped* handleVariable(const TSourceLoc&, TSymbol* symbol, const TString* string);
John Kesseniche01a9bc2016-03-12 20:11:22 -070071 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 Kessenichcd0a78a2016-09-09 16:32:09 -060086 bool shouldFlatten(const TType&);
87 void flattenStruct(const TVariable& variable);
88 TIntermTyped* flattenAccess(TIntermTyped* base, int member);
John Kessenicha3051662016-09-02 19:13:36 -060089 TFunction& handleFunctionDeclarator(const TSourceLoc&, TFunction& function, bool prototype);
John Kesseniche01a9bc2016-03-12 20:11:22 -070090 TIntermAggregate* handleFunctionDefinition(const TSourceLoc&, TFunction&);
John Kessenicha3051662016-09-02 19:13:36 -060091 void handleFunctionBody(const TSourceLoc&, TFunction&, TIntermNode* functionBody, TIntermNode*& node);
John Kessenich830b0cc2016-08-29 18:10:47 -060092 void remapEntrypointIO(TFunction& function);
John Kessenich07350f32016-09-02 20:23:27 -060093 void remapNonEntrypointIO(TFunction& function);
steve-lunargc4a13072016-08-09 11:28:03 -060094 TIntermNode* handleReturnValue(const TSourceLoc&, TIntermTyped*);
John Kessenich4678ca92016-05-13 09:33:42 -060095 void handleFunctionArgument(TFunction*, TIntermTyped*& arguments, TIntermTyped* newArg);
John Kesseniche01a9bc2016-03-12 20:11:22 -070096 TIntermTyped* handleFunctionCall(const TSourceLoc&, TFunction*, TIntermNode*);
LoopDawg592860c2016-06-09 08:57:35 -060097 void decomposeIntrinsic(const TSourceLoc&, TIntermTyped*& node, TIntermNode* arguments);
LoopDawg4886f692016-06-29 10:58:58 -060098 void decomposeSampleMethods(const TSourceLoc&, TIntermTyped*& node, TIntermNode* arguments);
John Kesseniche01a9bc2016-03-12 20:11:22 -070099 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 Kessenichd016be12016-03-13 11:24:20 -0600103 TFunction* handleConstructorCall(const TSourceLoc&, const TType&);
John Kessenich7735b942016-09-05 12:40:06 -0600104 void handleSemantic(TSourceLoc, TQualifier&, const TString& semantic);
105 void handlePackOffset(const TSourceLoc&, TQualifier&, const glslang::TString& location,
John Kessenichb38f0712016-07-30 10:29:54 -0600106 const glslang::TString* component);
John Kessenich7735b942016-09-05 12:40:06 -0600107 void handleRegister(const TSourceLoc&, TQualifier&, const glslang::TString* profile, const glslang::TString& desc,
John Kessenichcfd7ce82016-09-05 16:03:12 -0600108 int subComponent, const glslang::TString*);
John Kesseniche01a9bc2016-03-12 20:11:22 -0700109
LoopDawga2b79912016-07-14 14:45:14 -0600110 TIntermAggregate* handleSamplerTextureCombine(const TSourceLoc& loc, TIntermTyped* argTex, TIntermTyped* argSampler);
111
John Kesseniche01a9bc2016-03-12 20:11:22 -0700112 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 Kesseniche01a9bc2016-03-12 20:11:22 -0700128 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 Kessenich5aa59e22016-06-17 15:50:47 -0600134 void paramFix(TType& type);
John Kesseniche01a9bc2016-03-12 20:11:22 -0700135 void specializationCheck(const TSourceLoc&, const TType&, const char* op);
136
John Kessenichb9e39122016-08-17 10:22:08 -0600137 void setLayoutQualifier(const TSourceLoc&, TQualifier&, TString&);
138 void setLayoutQualifier(const TSourceLoc&, TQualifier&, TString&, const TIntermTyped*);
John Kesseniche01a9bc2016-03-12 20:11:22 -0700139 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 Kessenich5e69ec62016-07-05 00:02:40 -0600143 void declareTypedef(const TSourceLoc&, TString& identifier, const TType&, TArraySizes* typeArray = 0);
John Kesseniche01a9bc2016-03-12 20:11:22 -0700144 TIntermNode* declareVariable(const TSourceLoc&, TString& identifier, const TType&, TArraySizes* typeArray = 0, TIntermTyped* initializer = 0);
John Kessenicha26a5172016-07-28 15:29:35 -0600145 TIntermTyped* addConstructor(const TSourceLoc&, TIntermNode*, const TType&);
John Kesseniche01a9bc2016-03-12 20:11:22 -0700146 TIntermTyped* constructAggregate(TIntermNode*, const TType&, int, const TSourceLoc&);
147 TIntermTyped* constructBuiltIn(const TType&, TOperator, TIntermTyped*, const TSourceLoc&, bool subset);
John Kessenich3d157c52016-07-25 16:05:33 -0600148 void declareBlock(const TSourceLoc&, TType&, const TString* instanceName = 0, TArraySizes* arraySizes = 0);
John Kesseniche01a9bc2016-03-12 20:11:22 -0700149 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 Kessenich119f8f62016-06-05 15:44:07 -0600160 void nestLooping() { ++loopNestingLevel; }
161 void unnestLooping() { --loopNestingLevel; }
John Kessenich0d2b6de2016-06-05 11:23:11 -0600162 void pushScope() { symbolTable.push(); }
163 void popScope() { symbolTable.pop(0); }
164
John Kessenichd02dc5d2016-07-01 00:04:11 -0600165 void pushSwitchSequence(TIntermSequence* sequence) { switchSequenceStack.push_back(sequence); }
166 void popSwitchSequence() { switchSequenceStack.pop_back(); }
167
John Kesseniche01a9bc2016-03-12 20:11:22 -0700168protected:
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);
LoopDawg58910702016-06-13 09:22:28 -0600175 TOperator mapAtomicOp(const TSourceLoc& loc, TOperator op, bool isImage);
John Kesseniche01a9bc2016-03-12 20:11:22 -0700176 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 Kesseniche01a9bc2016-03-12 20:11:22 -0700185 TList<TIntermSequence*> switchSequenceStack; // case, node, case, case, node, ...; ensure only one node between cases; stack of them for nesting
John Kesseniche01a9bc2016-03-12 20:11:22 -0700186 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 Kesseniche01a9bc2016-03-12 20:11:22 -0700190 TBuiltInResource resources;
191 TLimits& limits;
192
193 HlslParseContext(HlslParseContext&);
194 HlslParseContext& operator=(HlslParseContext&);
195
John Kesseniche01a9bc2016-03-12 20:11:22 -0700196 static const int maxSamplerIndex = EsdNumDims * (EbtNumTypes * (2 * 2 * 2)); // see computeSamplerTypeIndex()
John Kesseniche01a9bc2016-03-12 20:11:22 -0700197 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 Kessenich6a70eb72016-08-28 15:00:23 -0600204 TVariable* entryPointOutput;
John Kesseniche01a9bc2016-03-12 20:11:22 -0700205
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 Kessenichcd0a78a2016-09-09 16:32:09 -0600239
240 TMap<int, TVector<TVariable*>> flattenMap;
John Kesseniche01a9bc2016-03-12 20:11:22 -0700241};
242
243} // end namespace glslang
244
245#endif // HLSL_PARSE_INCLUDED_