blob: dd97e490d5d0f2ac46ea05999e1123ed3ad601f0 [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();
50 void setLimits(const TBuiltInResource&);
51 bool parseShaderStrings(TPpContext&, TInputScanner& input, bool versionWillBeError = false);
John Kesseniche512cd92016-05-03 21:17:55 -060052 void getPreamble(std::string&);
John Kesseniche01a9bc2016-03-12 20:11:22 -070053
54 void C_DECL error(const TSourceLoc&, const char* szReason, const char* szToken,
55 const char* szExtraInfoFormat, ...);
56 void C_DECL warn(const TSourceLoc&, const char* szReason, const char* szToken,
57 const char* szExtraInfoFormat, ...);
58 void C_DECL ppError(const TSourceLoc&, const char* szReason, const char* szToken,
59 const char* szExtraInfoFormat, ...);
60 void C_DECL ppWarn(const TSourceLoc&, const char* szReason, const char* szToken,
61 const char* szExtraInfoFormat, ...);
62
63 void reservedPpErrorCheck(const TSourceLoc&, const char* name, const char* op) { }
64 bool lineContinuationCheck(const TSourceLoc&, bool endOfComment) { return true; }
65 bool lineDirectiveShouldSetNextLine() const { return true; }
66 bool builtInName(const TString&);
67
68 void handlePragma(const TSourceLoc&, const TVector<TString>&);
John Kesseniche6e74942016-06-11 16:43:14 -060069 TIntermTyped* handleVariable(const TSourceLoc&, TSymbol* symbol, const TString* string);
John Kesseniche01a9bc2016-03-12 20:11:22 -070070 TIntermTyped* handleBracketDereference(const TSourceLoc&, TIntermTyped* base, TIntermTyped* index);
71 void checkIndex(const TSourceLoc&, const TType&, int& index);
72
73 void makeEditable(TSymbol*&);
74 TVariable* getEditableVariable(const char* name);
75 bool isIoResizeArray(const TType&) const;
76 void fixIoArraySize(const TSourceLoc&, TType&);
77 void handleIoResizeArrayAccess(const TSourceLoc&, TIntermTyped* base);
78 void checkIoArraysConsistency(const TSourceLoc&, bool tailOnly = false);
79 int getIoArrayImplicitSize() const;
80 void checkIoArrayConsistency(const TSourceLoc&, int requiredSize, const char* feature, TType&, const TString&);
81
82 TIntermTyped* handleBinaryMath(const TSourceLoc&, const char* str, TOperator op, TIntermTyped* left, TIntermTyped* right);
83 TIntermTyped* handleUnaryMath(const TSourceLoc&, const char* str, TOperator op, TIntermTyped* childNode);
84 TIntermTyped* handleDotDereference(const TSourceLoc&, TIntermTyped* base, const TString& field);
85 TFunction* handleFunctionDeclarator(const TSourceLoc&, TFunction& function, bool prototype);
86 TIntermAggregate* handleFunctionDefinition(const TSourceLoc&, TFunction&);
John Kessenich4678ca92016-05-13 09:33:42 -060087 void handleFunctionArgument(TFunction*, TIntermTyped*& arguments, TIntermTyped* newArg);
John Kesseniche01a9bc2016-03-12 20:11:22 -070088 TIntermTyped* handleFunctionCall(const TSourceLoc&, TFunction*, TIntermNode*);
LoopDawg592860c2016-06-09 08:57:35 -060089 void decomposeIntrinsic(const TSourceLoc&, TIntermTyped*& node, TIntermNode* arguments);
John Kesseniche01a9bc2016-03-12 20:11:22 -070090 TIntermTyped* handleLengthMethod(const TSourceLoc&, TFunction*, TIntermNode*);
91 void addInputArgumentConversions(const TFunction&, TIntermNode*&) const;
92 TIntermTyped* addOutputArgumentConversions(const TFunction&, TIntermAggregate&) const;
93 void builtInOpCheck(const TSourceLoc&, const TFunction&, TIntermOperator&);
John Kessenichd016be12016-03-13 11:24:20 -060094 TFunction* handleConstructorCall(const TSourceLoc&, const TType&);
John Kessenich630dd7d2016-06-12 23:52:12 -060095 void handleSemantic(TType& type, const TString& semantic);
John Kesseniche01a9bc2016-03-12 20:11:22 -070096
97 bool parseVectorFields(const TSourceLoc&, const TString&, int vecSize, TVectorFields&);
98 void assignError(const TSourceLoc&, const char* op, TString left, TString right);
99 void unaryOpError(const TSourceLoc&, const char* op, TString operand);
100 void binaryOpError(const TSourceLoc&, const char* op, TString left, TString right);
101 void variableCheck(TIntermTyped*& nodePtr);
102 void constantValueCheck(TIntermTyped* node, const char* token);
103 void integerCheck(const TIntermTyped* node, const char* token);
104 void globalCheck(const TSourceLoc&, const char* token);
105 bool constructorError(const TSourceLoc&, TIntermNode*, TFunction&, TOperator, TType&);
106 bool constructorTextureSamplerError(const TSourceLoc&, const TFunction&);
107 void arraySizeCheck(const TSourceLoc&, TIntermTyped* expr, TArraySize&);
108 void arraySizeRequiredCheck(const TSourceLoc&, const TArraySizes&);
109 void structArrayCheck(const TSourceLoc&, const TType& structure);
110 void arrayDimMerge(TType& type, const TArraySizes* sizes);
111 bool voidErrorCheck(const TSourceLoc&, const TString&, TBasicType);
112 void boolCheck(const TSourceLoc&, const TIntermTyped*);
John Kesseniche01a9bc2016-03-12 20:11:22 -0700113 void globalQualifierFix(const TSourceLoc&, TQualifier&);
114 bool structQualifierErrorCheck(const TSourceLoc&, const TPublicType& pType);
115 void mergeQualifiers(const TSourceLoc&, TQualifier& dst, const TQualifier& src, bool force);
116 int computeSamplerTypeIndex(TSampler&);
117 TSymbol* redeclareBuiltinVariable(const TSourceLoc&, const TString&, const TQualifier&, const TShaderQualifiers&, bool& newDeclaration);
118 void redeclareBuiltinBlock(const TSourceLoc&, TTypeList& typeList, const TString& blockName, const TString* instanceName, TArraySizes* arraySizes);
John Kessenich5aa59e22016-06-17 15:50:47 -0600119 void paramFix(TType& type);
John Kesseniche01a9bc2016-03-12 20:11:22 -0700120 void specializationCheck(const TSourceLoc&, const TType&, const char* op);
121
122 void setLayoutQualifier(const TSourceLoc&, TPublicType&, TString&);
123 void setLayoutQualifier(const TSourceLoc&, TPublicType&, TString&, const TIntermTyped*);
124 void mergeObjectLayoutQualifiers(TQualifier& dest, const TQualifier& src, bool inheritOnly);
125 void checkNoShaderLayouts(const TSourceLoc&, const TShaderQualifiers&);
126
127 const TFunction* findFunction(const TSourceLoc& loc, const TFunction& call, bool& builtIn);
128 TIntermNode* declareVariable(const TSourceLoc&, TString& identifier, const TType&, TArraySizes* typeArray = 0, TIntermTyped* initializer = 0);
129 TIntermTyped* addConstructor(const TSourceLoc&, TIntermNode*, const TType&, TOperator);
130 TIntermTyped* constructAggregate(TIntermNode*, const TType&, int, const TSourceLoc&);
131 TIntermTyped* constructBuiltIn(const TType&, TOperator, TIntermTyped*, const TSourceLoc&, bool subset);
132 void declareBlock(const TSourceLoc&, TTypeList& typeList, const TString* instanceName = 0, TArraySizes* arraySizes = 0);
133 void fixBlockLocations(const TSourceLoc&, TQualifier&, TTypeList&, bool memberWithLocation, bool memberWithoutLocation);
134 void fixBlockXfbOffsets(TQualifier&, TTypeList&);
135 void fixBlockUniformOffsets(TQualifier&, TTypeList&);
136 void addQualifierToExisting(const TSourceLoc&, TQualifier, const TString& identifier);
137 void addQualifierToExisting(const TSourceLoc&, TQualifier, TIdentifierList&);
138 void updateStandaloneQualifierDefaults(const TSourceLoc&, const TPublicType&);
139 void wrapupSwitchSubsequence(TIntermAggregate* statements, TIntermNode* branchNode);
140 TIntermNode* addSwitch(const TSourceLoc&, TIntermTyped* expression, TIntermAggregate* body);
141
142 void updateImplicitArraySize(const TSourceLoc&, TIntermNode*, int index);
143
John Kessenich119f8f62016-06-05 15:44:07 -0600144 void nestLooping() { ++loopNestingLevel; }
145 void unnestLooping() { --loopNestingLevel; }
John Kessenich0d2b6de2016-06-05 11:23:11 -0600146 void pushScope() { symbolTable.push(); }
147 void popScope() { symbolTable.pop(0); }
148
John Kesseniche01a9bc2016-03-12 20:11:22 -0700149protected:
150 void inheritGlobalDefaults(TQualifier& dst) const;
151 TVariable* makeInternalVariable(const char* name, const TType&) const;
152 TVariable* declareNonArray(const TSourceLoc&, TString& identifier, TType&, bool& newDeclaration);
153 void declareArray(const TSourceLoc&, TString& identifier, const TType&, TSymbol*&, bool& newDeclaration);
154 TIntermNode* executeInitializer(const TSourceLoc&, TIntermTyped* initializer, TVariable* variable);
155 TIntermTyped* convertInitializerList(const TSourceLoc&, const TType&, TIntermTyped* initializer);
156 TOperator mapTypeToConstructorOp(const TType&) const;
LoopDawg58910702016-06-13 09:22:28 -0600157 TOperator mapAtomicOp(const TSourceLoc& loc, TOperator op, bool isImage);
John Kesseniche01a9bc2016-03-12 20:11:22 -0700158 void outputMessage(const TSourceLoc&, const char* szReason, const char* szToken,
159 const char* szExtraInfoFormat, TPrefixType prefix,
160 va_list args);
161
162 // Current state of parsing
163 struct TPragma contextPragma;
164 int loopNestingLevel; // 0 if outside all loops
165 int structNestingLevel; // 0 if outside blocks and structures
166 int controlFlowNestingLevel; // 0 if outside all flow control
John Kesseniche01a9bc2016-03-12 20:11:22 -0700167 TList<TIntermSequence*> switchSequenceStack; // case, node, case, case, node, ...; ensure only one node between cases; stack of them for nesting
168 TList<int> switchLevel; // the statementNestingLevel the current switch statement is at, which must match the level of its case statements
169 bool inEntrypoint; // if inside a function, true if the function is the entry point
170 bool postMainReturn; // if inside a function, true if the function is the entry point and this is after a return statement
171 const TType* currentFunctionType; // the return type of the function that's currently being parsed
172 bool functionReturnsValue; // true if a non-void function has a return
173 const TString* blockName;
174 TQualifier currentBlockQualifier;
175 TBuiltInResource resources;
176 TLimits& limits;
177
178 HlslParseContext(HlslParseContext&);
179 HlslParseContext& operator=(HlslParseContext&);
180
181 TMap<TString, TExtensionBehavior> extensionBehavior; // for each extension string, what its current behavior is set to
182 static const int maxSamplerIndex = EsdNumDims * (EbtNumTypes * (2 * 2 * 2)); // see computeSamplerTypeIndex()
183 bool afterEOF;
184 TQualifier globalBufferDefaults;
185 TQualifier globalUniformDefaults;
186 TQualifier globalInputDefaults;
187 TQualifier globalOutputDefaults;
188 TString currentCaller; // name of last function body entered (not valid when at global scope)
189 TIdSetType inductiveLoopIds;
190 TVector<TIntermTyped*> needsIndexLimitationChecking;
191
192 //
193 // Geometry shader input arrays:
194 // - array sizing is based on input primitive and/or explicit size
195 //
196 // Tessellation control output arrays:
197 // - array sizing is based on output layout(vertices=...) and/or explicit size
198 //
199 // Both:
200 // - array sizing is retroactive
201 // - built-in block redeclarations interact with this
202 //
203 // Design:
204 // - use a per-context "resize-list", a list of symbols whose array sizes
205 // can be fixed
206 //
207 // - the resize-list starts empty at beginning of user-shader compilation, it does
208 // not have built-ins in it
209 //
210 // - on built-in array use: copyUp() symbol and add it to the resize-list
211 //
212 // - on user array declaration: add it to the resize-list
213 //
214 // - on block redeclaration: copyUp() symbol and add it to the resize-list
215 // * note, that appropriately gives an error if redeclaring a block that
216 // was already used and hence already copied-up
217 //
218 // - on seeing a layout declaration that sizes the array, fix everything in the
219 // resize-list, giving errors for mismatch
220 //
221 // - on seeing an array size declaration, give errors on mismatch between it and previous
222 // array-sizing declarations
223 //
224 TVector<TSymbol*> ioArraySymbolResizeList;
225};
226
227} // end namespace glslang
228
229#endif // HLSL_PARSE_INCLUDED_