blob: a51a5091f686d76ed4401e0a896322da8bcbf9dc [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 Kesseniche512cd92016-05-03 21:17:55 -060054 void getPreamble(std::string&);
John Kesseniche01a9bc2016-03-12 20:11:22 -070055
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 Kessenich7f349c72016-07-08 22:09:10 -060065 void reservedPpErrorCheck(const TSourceLoc&, const char* /*name*/, const char* /*op*/) { }
66 bool lineContinuationCheck(const TSourceLoc&, bool /*endOfComment*/) { return true; }
John Kesseniche01a9bc2016-03-12 20:11:22 -070067 bool lineDirectiveShouldSetNextLine() const { return true; }
68 bool builtInName(const TString&);
69
70 void handlePragma(const TSourceLoc&, const TVector<TString>&);
John Kesseniche6e74942016-06-11 16:43:14 -060071 TIntermTyped* handleVariable(const TSourceLoc&, TSymbol* symbol, const TString* string);
John Kesseniche01a9bc2016-03-12 20:11:22 -070072 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 Kessenich4678ca92016-05-13 09:33:42 -060089 void handleFunctionArgument(TFunction*, TIntermTyped*& arguments, TIntermTyped* newArg);
John Kesseniche01a9bc2016-03-12 20:11:22 -070090 TIntermTyped* handleFunctionCall(const TSourceLoc&, TFunction*, TIntermNode*);
LoopDawg592860c2016-06-09 08:57:35 -060091 void decomposeIntrinsic(const TSourceLoc&, TIntermTyped*& node, TIntermNode* arguments);
LoopDawg4886f692016-06-29 10:58:58 -060092 void decomposeSampleMethods(const TSourceLoc&, TIntermTyped*& node, TIntermNode* arguments);
John Kesseniche01a9bc2016-03-12 20:11:22 -070093 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 Kessenichd016be12016-03-13 11:24:20 -060097 TFunction* handleConstructorCall(const TSourceLoc&, const TType&);
John Kessenich630dd7d2016-06-12 23:52:12 -060098 void handleSemantic(TType& type, const TString& semantic);
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 Kesseniche01a9bc2016-03-12 20:11:22 -0700102 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 Kesseniche01a9bc2016-03-12 20:11:22 -0700118 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 Kessenich5aa59e22016-06-17 15:50:47 -0600124 void paramFix(TType& type);
John Kesseniche01a9bc2016-03-12 20:11:22 -0700125 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 Kessenich5e69ec62016-07-05 00:02:40 -0600133 void declareTypedef(const TSourceLoc&, TString& identifier, const TType&, TArraySizes* typeArray = 0);
John Kesseniche01a9bc2016-03-12 20:11:22 -0700134 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 Kessenich119f8f62016-06-05 15:44:07 -0600150 void nestLooping() { ++loopNestingLevel; }
151 void unnestLooping() { --loopNestingLevel; }
John Kessenich0d2b6de2016-06-05 11:23:11 -0600152 void pushScope() { symbolTable.push(); }
153 void popScope() { symbolTable.pop(0); }
154
John Kessenichd02dc5d2016-07-01 00:04:11 -0600155 void pushSwitchSequence(TIntermSequence* sequence) { switchSequenceStack.push_back(sequence); }
156 void popSwitchSequence() { switchSequenceStack.pop_back(); }
157
John Kesseniche01a9bc2016-03-12 20:11:22 -0700158protected:
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;
LoopDawg58910702016-06-13 09:22:28 -0600166 TOperator mapAtomicOp(const TSourceLoc& loc, TOperator op, bool isImage);
John Kesseniche01a9bc2016-03-12 20:11:22 -0700167 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 Kesseniche01a9bc2016-03-12 20:11:22 -0700176 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 -0700177 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 Kesseniche01a9bc2016-03-12 20:11:22 -0700189 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_