2 //Copyright (C) 2002-2005 3Dlabs Inc. Ltd.
3 //Copyright (C) 2012-2013 LunarG, Inc.
7 //Redistribution and use in source and binary forms, with or without
8 //modification, are permitted provided that the following conditions
11 // Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
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.
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.
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.
36 #ifndef _PARSER_HELPER_INCLUDED_
37 #define _PARSER_HELPER_INCLUDED_
40 #include "../Include/ShHandle.h"
41 #include "SymbolTable.h"
42 #include "localintermediate.h"
51 TPragma(bool o, bool d) : optimize(o), debug(d) { }
54 TPragmaTable pragmaTable;
60 typedef std::set<int> TIdSetType;
63 // The following are extra variables needed during parsing, grouped together so
64 // they can be passed to the parser without needing a global.
68 TParseContext(TSymbolTable&, TIntermediate&, bool parsingBuiltins, int version, EProfile, EShLanguage, TInfoSink&,
69 bool forwardCompatible = false, EShMessages messages = EShMsgDefault);
70 virtual ~TParseContext();
72 void setLimits(const TBuiltInResource&);
73 bool parseShaderStrings(TPpContext&, TInputScanner& input, bool versionWillBeError = false);
74 void parserError(const char* s); // for bison's yyerror
75 const char* getPreamble();
77 void C_DECL error(const TSourceLoc&, const char* szReason, const char* szToken,
78 const char* szExtraInfoFormat, ...);
79 void C_DECL warn(const TSourceLoc&, const char* szReason, const char* szToken,
80 const char* szExtraInfoFormat, ...);
81 void C_DECL ppError(const TSourceLoc&, const char* szReason, const char* szToken,
82 const char* szExtraInfoFormat, ...);
83 void C_DECL ppWarn(const TSourceLoc&, const char* szReason, const char* szToken,
84 const char* szExtraInfoFormat, ...);
86 bool relaxedErrors() const { return (messages & EShMsgRelaxedErrors) != 0; }
87 bool suppressWarnings() const { return (messages & EShMsgSuppressWarnings) != 0; }
88 bool vulkanRules() const { return (messages & EShMsgVulkanRules) != 0; }
89 bool spirvRules() const { return (messages & EShMsgSpvRules) != 0; }
91 void reservedErrorCheck(const TSourceLoc&, const TString&);
92 void reservedPpErrorCheck(const TSourceLoc&, const char* name, const char* op);
93 bool lineContinuationCheck(const TSourceLoc&, bool endOfComment);
94 bool builtInName(const TString&);
96 void handlePragma(const TSourceLoc&, const TVector<TString>&);
97 TIntermTyped* handleVariable(const TSourceLoc&, TSymbol* symbol, const TString* string);
98 TIntermTyped* handleBracketDereference(const TSourceLoc&, TIntermTyped* base, TIntermTyped* index);
99 void checkIndex(const TSourceLoc&, const TType&, int& index);
100 void handleIndexLimits(const TSourceLoc&, TIntermTyped* base, TIntermTyped* index);
102 void makeEditable(TSymbol*&);
103 bool isIoResizeArray(const TType&) const;
104 void fixIoArraySize(const TSourceLoc&, TType&);
105 void ioArrayCheck(const TSourceLoc&, const TType&, const TString& identifier);
106 void handleIoResizeArrayAccess(const TSourceLoc&, TIntermTyped* base);
107 void checkIoArraysConsistency(const TSourceLoc&, bool tailOnly = false);
108 int getIoArrayImplicitSize() const;
109 void checkIoArrayConsistency(const TSourceLoc&, int requiredSize, const char* feature, TType&, const TString&);
111 TIntermTyped* handleBinaryMath(const TSourceLoc&, const char* str, TOperator op, TIntermTyped* left, TIntermTyped* right);
112 TIntermTyped* handleUnaryMath(const TSourceLoc&, const char* str, TOperator op, TIntermTyped* childNode);
113 TIntermTyped* handleDotDereference(const TSourceLoc&, TIntermTyped* base, const TString& field);
114 void blockMemberExtensionCheck(const TSourceLoc&, const TIntermTyped* base, const TString& field);
115 TFunction* handleFunctionDeclarator(const TSourceLoc&, TFunction& function, bool prototype);
116 TIntermAggregate* handleFunctionDefinition(const TSourceLoc&, TFunction&);
117 TIntermTyped* handleFunctionCall(const TSourceLoc&, TFunction*, TIntermNode*);
118 void checkLocation(const TSourceLoc&, TOperator);
119 TIntermTyped* handleLengthMethod(const TSourceLoc&, TFunction*, TIntermNode*);
120 void addInputArgumentConversions(const TFunction&, TIntermNode*&) const;
121 TIntermTyped* addOutputArgumentConversions(const TFunction&, TIntermAggregate&) const;
122 void builtInOpCheck(const TSourceLoc&, const TFunction&, TIntermOperator&);
123 void nonOpBuiltInCheck(const TSourceLoc&, const TFunction&, TIntermAggregate&);
124 TFunction* handleConstructorCall(const TSourceLoc&, const TPublicType&);
126 bool parseVectorFields(const TSourceLoc&, const TString&, int vecSize, TVectorFields&);
127 void assignError(const TSourceLoc&, const char* op, TString left, TString right);
128 void unaryOpError(const TSourceLoc&, const char* op, TString operand);
129 void binaryOpError(const TSourceLoc&, const char* op, TString left, TString right);
130 void variableCheck(TIntermTyped*& nodePtr);
131 bool lValueErrorCheck(const TSourceLoc&, const char* op, TIntermTyped*);
132 void rValueErrorCheck(const TSourceLoc&, const char* op, TIntermTyped*);
133 void constantValueCheck(TIntermTyped* node, const char* token);
134 void integerCheck(const TIntermTyped* node, const char* token);
135 void globalCheck(const TSourceLoc&, const char* token);
136 bool constructorError(const TSourceLoc&, TIntermNode*, TFunction&, TOperator, TType&);
137 void arraySizeCheck(const TSourceLoc&, TIntermTyped* expr, int& size);
138 bool arrayQualifierError(const TSourceLoc&, const TQualifier&);
139 bool arrayError(const TSourceLoc&, const TType&);
140 void arraySizeRequiredCheck(const TSourceLoc&, const TArraySizes&);
141 void structArrayCheck(const TSourceLoc&, const TType& structure);
142 void arrayUnsizedCheck(const TSourceLoc&, const TQualifier&, const TArraySizes*, bool initializer, bool lastMember);
143 void arrayOfArrayVersionCheck(const TSourceLoc&);
144 void arrayDimCheck(const TSourceLoc&, const TArraySizes* sizes1, const TArraySizes* sizes2);
145 void arrayDimCheck(const TSourceLoc&, const TType*, const TArraySizes*);
146 void arrayDimMerge(TType& type, const TArraySizes* sizes);
147 bool voidErrorCheck(const TSourceLoc&, const TString&, TBasicType);
148 void boolCheck(const TSourceLoc&, const TIntermTyped*);
149 void boolCheck(const TSourceLoc&, const TPublicType&);
150 void samplerCheck(const TSourceLoc&, const TType&, const TString& identifier);
151 void atomicUintCheck(const TSourceLoc&, const TType&, const TString& identifier);
152 void globalQualifierFixCheck(const TSourceLoc&, TQualifier&);
153 void globalQualifierTypeCheck(const TSourceLoc&, const TQualifier&, const TPublicType&);
154 bool structQualifierErrorCheck(const TSourceLoc&, const TPublicType& pType);
155 void mergeQualifiers(const TSourceLoc&, TQualifier& dst, const TQualifier& src, bool force);
156 void setDefaultPrecision(const TSourceLoc&, TPublicType&, TPrecisionQualifier);
157 int computeSamplerTypeIndex(TSampler&);
158 TPrecisionQualifier getDefaultPrecision(TPublicType&);
159 void precisionQualifierCheck(const TSourceLoc&, TBasicType, TQualifier&);
160 void parameterTypeCheck(const TSourceLoc&, TStorageQualifier qualifier, const TType& type);
161 bool containsFieldWithBasicType(const TType& type ,TBasicType basicType);
162 TSymbol* redeclareBuiltinVariable(const TSourceLoc&, const TString&, const TQualifier&, const TShaderQualifiers&, bool& newDeclaration);
163 void redeclareBuiltinBlock(const TSourceLoc&, TTypeList& typeList, const TString& blockName, const TString* instanceName, TArraySizes* arraySizes);
164 void paramCheckFix(const TSourceLoc&, const TStorageQualifier&, TType& type);
165 void paramCheckFix(const TSourceLoc&, const TQualifier&, TType& type);
166 void nestedBlockCheck(const TSourceLoc&);
167 void nestedStructCheck(const TSourceLoc&);
168 void arrayObjectCheck(const TSourceLoc&, const TType&, const char* op);
169 void opaqueCheck(const TSourceLoc&, const TType&, const char* op);
170 void structTypeCheck(const TSourceLoc&, TPublicType&);
171 void inductiveLoopCheck(const TSourceLoc&, TIntermNode* init, TIntermLoop* loop);
172 void arrayLimitCheck(const TSourceLoc&, const TString&, int size);
173 void limitCheck(const TSourceLoc&, int value, const char* limit, const char* feature);
175 void inductiveLoopBodyCheck(TIntermNode*, int loopIndexId, TSymbolTable&);
176 void constantIndexExpressionCheck(TIntermNode*);
178 void setLayoutQualifier(const TSourceLoc&, TPublicType&, TString&);
179 void setLayoutQualifier(const TSourceLoc&, TPublicType&, TString&, const TIntermTyped*);
180 void mergeObjectLayoutQualifiers(TQualifier& dest, const TQualifier& src, bool inheritOnly);
181 void layoutObjectCheck(const TSourceLoc&, const TSymbol&);
182 void layoutTypeCheck(const TSourceLoc&, const TType&);
183 void layoutQualifierCheck(const TSourceLoc&, const TQualifier&);
184 void checkNoShaderLayouts(const TSourceLoc&, const TShaderQualifiers&);
185 void fixOffset(const TSourceLoc&, TSymbol&);
187 const TFunction* findFunction(const TSourceLoc& loc, const TFunction& call, bool& builtIn);
188 const TFunction* findFunctionExact(const TSourceLoc& loc, const TFunction& call, bool& builtIn);
189 const TFunction* findFunction120(const TSourceLoc& loc, const TFunction& call, bool& builtIn);
190 const TFunction* findFunction400(const TSourceLoc& loc, const TFunction& call, bool& builtIn);
191 void declareTypeDefaults(const TSourceLoc&, const TPublicType&);
192 TIntermNode* declareVariable(const TSourceLoc&, TString& identifier, const TPublicType&, TArraySizes* typeArray = 0, TIntermTyped* initializer = 0);
193 TIntermTyped* addConstructor(const TSourceLoc&, TIntermNode*, const TType&, TOperator);
194 TIntermTyped* constructAggregate(TIntermNode*, const TType&, int, const TSourceLoc&);
195 TIntermTyped* constructBuiltIn(const TType&, TOperator, TIntermTyped*, const TSourceLoc&, bool subset);
196 void declareBlock(const TSourceLoc&, TTypeList& typeList, const TString* instanceName = 0, TArraySizes* arraySizes = 0);
197 void blockStageIoCheck(const TSourceLoc&, const TQualifier&);
198 void blockQualifierCheck(const TSourceLoc&, const TQualifier&);
199 void fixBlockLocations(const TSourceLoc&, TQualifier&, TTypeList&, bool memberWithLocation, bool memberWithoutLocation);
200 void fixBlockXfbOffsets(TQualifier&, TTypeList&);
201 void fixBlockUniformOffsets(TQualifier&, TTypeList&);
202 void addQualifierToExisting(const TSourceLoc&, TQualifier, const TString& identifier);
203 void addQualifierToExisting(const TSourceLoc&, TQualifier, TIdentifierList&);
204 void invariantCheck(const TSourceLoc&, const TQualifier&);
205 void updateStandaloneQualifierDefaults(const TSourceLoc&, const TPublicType&);
206 void wrapupSwitchSubsequence(TIntermAggregate* statements, TIntermNode* branchNode);
207 TIntermNode* addSwitch(const TSourceLoc&, TIntermTyped* expression, TIntermAggregate* body);
209 void updateImplicitArraySize(const TSourceLoc&, TIntermNode*, int index);
211 void setScanContext(TScanContext* c) { scanContext = c; }
212 TScanContext* getScanContext() const { return scanContext; }
213 void setPpContext(TPpContext* c) { ppContext = c; }
214 TPpContext* getPpContext() const { return ppContext; }
215 void addError() { ++numErrors; }
216 int getNumErrors() const { return numErrors; }
217 const TSourceLoc& getCurrentLoc() const { return currentScanner->getSourceLoc(); }
218 void setCurrentLine(int line) { currentScanner->setLine(line); }
219 void setCurrentColumn(int col) { currentScanner->setColumn(col); }
220 void setCurrentSourceName(const char* name) { currentScanner->setFile(name); }
221 void setCurrentString(int string) { currentScanner->setString(string); }
222 void setScanner(TInputScanner* scanner) { currentScanner = scanner; }
223 TInputScanner* getScanner() const { return currentScanner; }
225 bool lineDirectiveShouldSetNextLine() const;
227 void notifyVersion(int line, int version, const char* type_string);
228 void notifyErrorDirective(int line, const char* error_message);
229 void notifyLineDirective(int curLineNo, int newLineNo, bool hasSource, int sourceNum, const char* sourceName);
230 void notifyExtensionDirective(int line, const char* extension, const char* behavior);
232 // The following are implemented in Versions.cpp to localize version/profile/stage/extensions control
233 void initializeExtensionBehavior();
234 void requireProfile(const TSourceLoc&, int queryProfiles, const char* featureDesc);
235 void profileRequires(const TSourceLoc&, int queryProfiles, int minVersion, int numExtensions, const char* const extensions[], const char* featureDesc);
236 void profileRequires(const TSourceLoc&, int queryProfiles, int minVersion, const char* const extension, const char* featureDesc);
237 void requireStage(const TSourceLoc&, EShLanguageMask, const char* featureDesc);
238 void requireStage(const TSourceLoc&, EShLanguage, const char* featureDesc);
239 void checkDeprecated(const TSourceLoc&, int queryProfiles, int depVersion, const char* featureDesc);
240 void requireNotRemoved(const TSourceLoc&, int queryProfiles, int removedVersion, const char* featureDesc);
241 void requireExtensions(const TSourceLoc&, int numExtensions, const char* const extensions[], const char* featureDesc);
242 void ppRequireExtensions(const TSourceLoc&, int numExtensions, const char* const extensions[], const char* featureDesc);
243 TExtensionBehavior getExtensionBehavior(const char*);
244 bool extensionTurnedOn(const char* const extension);
245 bool extensionsTurnedOn(int numExtensions, const char* const extensions[]);
246 void updateExtensionBehavior(int line, const char* const extension, const char* behavior);
247 void fullIntegerCheck(const TSourceLoc&, const char* op);
248 void doubleCheck(const TSourceLoc&, const char* op);
250 void setVersionCallback(const std::function<void(int, int, const char*)>& func) { versionCallback = func; }
251 void setPragmaCallback(const std::function<void(int, const TVector<TString>&)>& func) { pragmaCallback = func; }
252 void setLineCallback(const std::function<void(int, int, bool, int, const char*)>& func) { lineCallback = func; }
253 void setExtensionCallback(const std::function<void(int, const char*, const char*)>& func) { extensionCallback = func; }
254 void setErrorCallback(const std::function<void(int, const char*)>& func) { errorCallback = func; }
257 void nonInitConstCheck(const TSourceLoc&, TString& identifier, TType& type);
258 void inheritGlobalDefaults(TQualifier& dst) const;
259 TVariable* makeInternalVariable(const char* name, const TType&) const;
260 TVariable* declareNonArray(const TSourceLoc&, TString& identifier, TType&, bool& newDeclaration);
261 void declareArray(const TSourceLoc&, TString& identifier, const TType&, TSymbol*&, bool& newDeclaration);
262 TIntermNode* executeInitializer(const TSourceLoc&, TIntermTyped* initializer, TVariable* variable);
263 TIntermTyped* convertInitializerList(const TSourceLoc&, const TType&, TIntermTyped* initializer);
264 TOperator mapTypeToConstructorOp(const TType&) const;
265 bool checkExtensionsRequested(const TSourceLoc&, int numExtensions, const char* const extensions[], const char* featureDesc);
266 void updateExtensionBehavior(const char* const extension, TExtensionBehavior);
267 void finalErrorCheck();
268 void outputMessage(const TSourceLoc&, const char* szReason, const char* szToken,
269 const char* szExtraInfoFormat, TPrefixType prefix,
274 // Generally, bison productions, the scanner, and the PP need read/write access to these; just give them direct access
277 TIntermediate& intermediate; // helper for making and hooking up pieces of the parse tree
278 TSymbolTable& symbolTable; // symbol table that goes with the current language, version, and profile
282 EShLanguage language; // vertex or fragment language
283 int version; // version, updated by #version in the shader
284 EProfile profile; // the declared profile in the shader (core by default)
285 bool forwardCompatible; // true if errors are to be given for use of deprecated features
287 // Current state of parsing
288 struct TPragma contextPragma;
289 int loopNestingLevel; // 0 if outside all loops
290 int structNestingLevel; // 0 if outside blocks and structures
291 int controlFlowNestingLevel; // 0 if outside all flow control
292 int statementNestingLevel; // 0 if outside all flow control or compound statements
293 TList<TIntermSequence*> switchSequenceStack; // case, node, case, case, node, ...; ensure only one node between cases; stack of them for nesting
294 TList<int> switchLevel; // the statementNestingLevel the current switch statement is at, which must match the level of its case statements
295 bool inMain; // if inside a function, true if the function is main
296 bool postMainReturn; // if inside a function, true if the function is main and this is after a return statement
297 const TType* currentFunctionType; // the return type of the function that's currently being parsed
298 bool functionReturnsValue; // true if a non-void function has a return
299 const TString* blockName;
300 TQualifier currentBlockQualifier;
301 TIntermAggregate *linkage; // aggregate node of objects the linker may need, if not referenced by the rest of the AST
302 TPrecisionQualifier defaultPrecision[EbtNumTypes];
303 bool tokensBeforeEOF;
304 TBuiltInResource resources;
308 TParseContext(TParseContext&);
309 TParseContext& operator=(TParseContext&);
311 EShMessages messages; // errors/warnings/rule-sets
312 TScanContext* scanContext;
313 TPpContext* ppContext;
314 TInputScanner* currentScanner;
315 int numErrors; // number of compile-time errors encountered
316 bool parsingBuiltins; // true if parsing built-in symbols/functions
317 TMap<TString, TExtensionBehavior> extensionBehavior; // for each extension string, what its current behavior is set to
318 static const int maxSamplerIndex = EsdNumDims * (EbtNumTypes * (2 * 2 * 2)); // see computeSamplerTypeIndex()
319 TPrecisionQualifier defaultSamplerPrecision[maxSamplerIndex];
321 TQualifier globalBufferDefaults;
322 TQualifier globalUniformDefaults;
323 TQualifier globalInputDefaults;
324 TQualifier globalOutputDefaults;
325 int* atomicUintOffsets; // to become an array of the right size to hold an offset per binding point
326 TString currentCaller;
327 TIdSetType inductiveLoopIds;
329 TVector<TIntermTyped*> needsIndexLimitationChecking;
332 // Geometry shader input arrays:
333 // - array sizing is based on input primitive and/or explicit size
335 // Tessellation control output arrays:
336 // - array sizing is based on output layout(vertices=...) and/or explicit size
339 // - array sizing is retroactive
340 // - built-in block redeclarations interact with this
343 // - use a per-context "resize-list", a list of symbols whose array sizes
346 // - the resize-list starts empty at beginning of user-shader compilation, it does
347 // not have built-ins in it
349 // - on built-in array use: copyUp() symbol and add it to the resize-list
351 // - on user array declaration: add it to the resize-list
353 // - on block redeclaration: copyUp() symbol and add it to the resize-list
354 // * note, that appropriately gives an error if redeclaring a block that
355 // was already used and hence already copied-up
357 // - on seeing a layout declaration that sizes the array, fix everything in the
358 // resize-list, giving errors for mismatch
360 // - on seeing an array size declaration, give errors on mismatch between it and previous
361 // array-sizing declarations
363 TVector<TSymbol*> ioArraySymbolResizeList;
365 // These, if set, will be called when a line, pragma ... is preprocessed.
366 // They will be called with any parameters to the original directive.
367 std::function<void(int, int, bool, int, const char*)> lineCallback;
368 std::function<void(int, const TVector<TString>&)> pragmaCallback;
369 std::function<void(int, int, const char*)> versionCallback;
370 std::function<void(int, const char*, const char*)> extensionCallback;
371 std::function<void(int, const char*)> errorCallback;
374 } // end namespace glslang
376 #endif // _PARSER_HELPER_INCLUDED_