daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1 | // |
Nicolas Capens | 16004fc | 2014-06-11 11:29:11 -0400 | [diff] [blame] | 2 | // Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved. |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | #ifndef _PARSER_HELPER_INCLUDED_ |
| 7 | #define _PARSER_HELPER_INCLUDED_ |
| 8 | |
Jamie Madill | d4a3a31 | 2014-06-25 16:04:56 -0400 | [diff] [blame] | 9 | #include "compiler/translator/Compiler.h" |
Geoff Lang | 1773282 | 2013-08-29 13:46:49 -0400 | [diff] [blame] | 10 | #include "compiler/translator/Diagnostics.h" |
| 11 | #include "compiler/translator/DirectiveHandler.h" |
Jamie Madill | b1a85f4 | 2014-08-19 15:23:24 -0400 | [diff] [blame] | 12 | #include "compiler/translator/Intermediate.h" |
Geoff Lang | 1773282 | 2013-08-29 13:46:49 -0400 | [diff] [blame] | 13 | #include "compiler/translator/SymbolTable.h" |
daniel@transgaming.com | b401a92 | 2012-10-26 18:58:24 +0000 | [diff] [blame] | 14 | #include "compiler/preprocessor/Preprocessor.h" |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 15 | |
| 16 | struct TMatrixFields { |
daniel@transgaming.com | 0578f81 | 2010-05-17 09:58:39 +0000 | [diff] [blame] | 17 | bool wholeRow; |
| 18 | bool wholeCol; |
| 19 | int row; |
| 20 | int col; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 21 | }; |
| 22 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 23 | // |
| 24 | // The following are extra variables needed during parsing, grouped together so |
| 25 | // they can be passed to the parser without needing a global. |
| 26 | // |
| 27 | struct TParseContext { |
Jamie Madill | 183bde5 | 2014-07-02 15:31:19 -0400 | [diff] [blame] | 28 | TParseContext(TSymbolTable& symt, TExtensionBehavior& ext, TIntermediate& interm, sh::GLenum type, ShShaderSpec spec, int options, bool checksPrecErrors, const char* sourcePath, TInfoSink& is) : |
kbr@chromium.org | 476541f | 2011-10-27 21:14:51 +0000 | [diff] [blame] | 29 | intermediate(interm), |
| 30 | symbolTable(symt), |
kbr@chromium.org | 476541f | 2011-10-27 21:14:51 +0000 | [diff] [blame] | 31 | shaderType(type), |
| 32 | shaderSpec(spec), |
| 33 | compileOptions(options), |
| 34 | sourcePath(sourcePath), |
| 35 | treeRoot(0), |
kbr@chromium.org | 476541f | 2011-10-27 21:14:51 +0000 | [diff] [blame] | 36 | loopNestingLevel(0), |
| 37 | structNestingLevel(0), |
kbr@chromium.org | 476541f | 2011-10-27 21:14:51 +0000 | [diff] [blame] | 38 | currentFunctionType(NULL), |
| 39 | functionReturnsValue(false), |
| 40 | checksPrecisionErrors(checksPrecErrors), |
Jamie Madill | 099c0f3 | 2013-06-20 11:55:52 -0400 | [diff] [blame] | 41 | defaultMatrixPacking(EmpColumnMajor), |
Jamie Madill | 1566ef7 | 2013-06-20 11:55:54 -0400 | [diff] [blame] | 42 | defaultBlockStorage(EbsShared), |
alokp@chromium.org | 73bc298 | 2012-06-19 18:48:05 +0000 | [diff] [blame] | 43 | diagnostics(is), |
shannon.woods%transgaming.com@gtempaccount.com | 5524db0 | 2013-04-13 03:38:16 +0000 | [diff] [blame] | 44 | shaderVersion(100), |
| 45 | directiveHandler(ext, diagnostics, shaderVersion), |
alokp@chromium.org | 73bc298 | 2012-06-19 18:48:05 +0000 | [diff] [blame] | 46 | preprocessor(&diagnostics, &directiveHandler), |
kbr@chromium.org | 476541f | 2011-10-27 21:14:51 +0000 | [diff] [blame] | 47 | scanner(NULL) { } |
daniel@transgaming.com | 0578f81 | 2010-05-17 09:58:39 +0000 | [diff] [blame] | 48 | TIntermediate& intermediate; // to hold and build a parse tree |
| 49 | TSymbolTable& symbolTable; // symbol table that goes with the language currently being parsed |
Jamie Madill | 183bde5 | 2014-07-02 15:31:19 -0400 | [diff] [blame] | 50 | sh::GLenum shaderType; // vertex or fragment language (future: pack or unpack) |
alokp@chromium.org | 4888ceb | 2010-10-01 21:13:12 +0000 | [diff] [blame] | 51 | ShShaderSpec shaderSpec; // The language specification compiler conforms to - GLES2 or WebGL. |
shannon.woods%transgaming.com@gtempaccount.com | 5524db0 | 2013-04-13 03:38:16 +0000 | [diff] [blame] | 52 | int shaderVersion; |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 53 | int compileOptions; |
| 54 | const char* sourcePath; // Path of source file or NULL. |
daniel@transgaming.com | 0578f81 | 2010-05-17 09:58:39 +0000 | [diff] [blame] | 55 | TIntermNode* treeRoot; // root of parse tree being created |
daniel@transgaming.com | 0578f81 | 2010-05-17 09:58:39 +0000 | [diff] [blame] | 56 | int loopNestingLevel; // 0 if outside all loops |
kbr@chromium.org | 476541f | 2011-10-27 21:14:51 +0000 | [diff] [blame] | 57 | int structNestingLevel; // incremented while parsing a struct declaration |
daniel@transgaming.com | 0578f81 | 2010-05-17 09:58:39 +0000 | [diff] [blame] | 58 | const TType* currentFunctionType; // the return type of the function that's currently being parsed |
| 59 | bool functionReturnsValue; // true if a non-void function has a return |
zmo@google.com | dc4b4f8 | 2011-06-17 00:42:53 +0000 | [diff] [blame] | 60 | bool checksPrecisionErrors; // true if an error will be generated when a variable is declared without precision, explicit or implicit. |
shannon.woods%transgaming.com@gtempaccount.com | cbb6b6a | 2013-04-13 03:27:47 +0000 | [diff] [blame] | 61 | bool fragmentPrecisionHigh; // true if highp precision is supported in the fragment language. |
Jamie Madill | 099c0f3 | 2013-06-20 11:55:52 -0400 | [diff] [blame] | 62 | TLayoutMatrixPacking defaultMatrixPacking; |
Jamie Madill | 1566ef7 | 2013-06-20 11:55:54 -0400 | [diff] [blame] | 63 | TLayoutBlockStorage defaultBlockStorage; |
alokp@chromium.org | 75fe6b7 | 2011-08-14 05:31:22 +0000 | [diff] [blame] | 64 | TString HashErrMsg; |
alokp@chromium.org | 73bc298 | 2012-06-19 18:48:05 +0000 | [diff] [blame] | 65 | TDiagnostics diagnostics; |
| 66 | TDirectiveHandler directiveHandler; |
| 67 | pp::Preprocessor preprocessor; |
alokp@chromium.org | 75fe6b7 | 2011-08-14 05:31:22 +0000 | [diff] [blame] | 68 | void* scanner; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 69 | |
shannon.woods%transgaming.com@gtempaccount.com | 5524db0 | 2013-04-13 03:38:16 +0000 | [diff] [blame] | 70 | int getShaderVersion() const { return shaderVersion; } |
alokp@chromium.org | 6b49571 | 2012-06-29 00:06:58 +0000 | [diff] [blame] | 71 | int numErrors() const { return diagnostics.numErrors(); } |
alokp@chromium.org | 646ea1e | 2012-06-15 17:36:31 +0000 | [diff] [blame] | 72 | TInfoSink& infoSink() { return diagnostics.infoSink(); } |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 73 | void error(const TSourceLoc& loc, const char *reason, const char* token, |
maxvujovic@gmail.com | c6b3b3c | 2012-06-27 22:49:39 +0000 | [diff] [blame] | 74 | const char* extraInfo=""); |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 75 | void warning(const TSourceLoc& loc, const char* reason, const char* token, |
maxvujovic@gmail.com | c6b3b3c | 2012-06-27 22:49:39 +0000 | [diff] [blame] | 76 | const char* extraInfo=""); |
alokp@chromium.org | 8b851c6 | 2012-06-15 16:25:11 +0000 | [diff] [blame] | 77 | void trace(const char* str); |
daniel@transgaming.com | 0578f81 | 2010-05-17 09:58:39 +0000 | [diff] [blame] | 78 | void recover(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 79 | |
Jamie Madill | 5c09702 | 2014-08-20 16:38:32 -0400 | [diff] [blame] | 80 | // This method is guaranteed to succeed, even if no variable with 'name' exists. |
| 81 | const TVariable *getNamedVariable(const TSourceLoc &location, const TString *name, const TSymbol *symbol); |
| 82 | |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 83 | bool parseVectorFields(const TString&, int vecSize, TVectorFields&, const TSourceLoc& line); |
| 84 | bool parseMatrixFields(const TString&, int matCols, int matRows, TMatrixFields&, const TSourceLoc& line); |
alokp@chromium.org | 75fe6b7 | 2011-08-14 05:31:22 +0000 | [diff] [blame] | 85 | |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 86 | bool reservedErrorCheck(const TSourceLoc& line, const TString& identifier); |
| 87 | void assignError(const TSourceLoc& line, const char* op, TString left, TString right); |
| 88 | void unaryOpError(const TSourceLoc& line, const char* op, TString operand); |
| 89 | void binaryOpError(const TSourceLoc& line, const char* op, TString left, TString right); |
| 90 | bool precisionErrorCheck(const TSourceLoc& line, TPrecision precision, TBasicType type); |
| 91 | bool lValueErrorCheck(const TSourceLoc& line, const char* op, TIntermTyped*); |
daniel@transgaming.com | 0578f81 | 2010-05-17 09:58:39 +0000 | [diff] [blame] | 92 | bool constErrorCheck(TIntermTyped* node); |
| 93 | bool integerErrorCheck(TIntermTyped* node, const char* token); |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 94 | bool globalErrorCheck(const TSourceLoc& line, bool global, const char* token); |
| 95 | bool constructorErrorCheck(const TSourceLoc& line, TIntermNode*, TFunction&, TOperator, TType*); |
| 96 | bool arraySizeErrorCheck(const TSourceLoc& line, TIntermTyped* expr, int& size); |
| 97 | bool arrayQualifierErrorCheck(const TSourceLoc& line, TPublicType type); |
| 98 | bool arrayTypeErrorCheck(const TSourceLoc& line, TPublicType type); |
| 99 | bool arrayErrorCheck(const TSourceLoc& line, const TString& identifier, const TPublicType &type, TVariable*& variable); |
| 100 | bool voidErrorCheck(const TSourceLoc&, const TString&, const TPublicType&); |
| 101 | bool boolErrorCheck(const TSourceLoc&, const TIntermTyped*); |
| 102 | bool boolErrorCheck(const TSourceLoc&, const TPublicType&); |
| 103 | bool samplerErrorCheck(const TSourceLoc& line, const TPublicType& pType, const char* reason); |
| 104 | bool structQualifierErrorCheck(const TSourceLoc& line, const TPublicType& pType); |
| 105 | bool locationDeclaratorListCheck(const TSourceLoc& line, const TPublicType &pType); |
| 106 | bool parameterSamplerErrorCheck(const TSourceLoc& line, TQualifier qualifier, const TType& type); |
| 107 | bool nonInitConstErrorCheck(const TSourceLoc& line, const TString& identifier, TPublicType& type, bool array); |
| 108 | bool nonInitErrorCheck(const TSourceLoc& line, const TString& identifier, const TPublicType& type, TVariable*& variable); |
| 109 | bool paramErrorCheck(const TSourceLoc& line, TQualifier qualifier, TQualifier paramQualifier, TType* type); |
| 110 | bool extensionErrorCheck(const TSourceLoc& line, const TString&); |
| 111 | bool singleDeclarationErrorCheck(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier); |
| 112 | bool layoutLocationErrorCheck(const TSourceLoc& location, const TLayoutQualifier &layoutQualifier); |
alokp@chromium.org | 8b851c6 | 2012-06-15 16:25:11 +0000 | [diff] [blame] | 113 | |
Jamie Madill | 7a217de | 2013-07-08 15:10:00 -0400 | [diff] [blame] | 114 | const TPragma& pragma() const { return directiveHandler.pragma(); } |
alokp@chromium.org | 73bc298 | 2012-06-19 18:48:05 +0000 | [diff] [blame] | 115 | const TExtensionBehavior& extensionBehavior() const { return directiveHandler.extensionBehavior(); } |
zmo@google.com | 09c323a | 2011-08-12 18:22:25 +0000 | [diff] [blame] | 116 | bool supportsExtension(const char* extension); |
Jamie Madill | 5d287f5 | 2013-07-12 15:38:19 -0400 | [diff] [blame] | 117 | bool isExtensionEnabled(const char* extension) const; |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 118 | void handleExtensionDirective(const TSourceLoc& loc, const char* extName, const char* behavior); |
| 119 | void handlePragmaDirective(const TSourceLoc& loc, const char* name, const char* value); |
alokp@chromium.org | 75fe6b7 | 2011-08-14 05:31:22 +0000 | [diff] [blame] | 120 | |
| 121 | bool containsSampler(TType& type); |
| 122 | bool areAllChildConst(TIntermAggregate* aggrNode); |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 123 | const TFunction* findFunction(const TSourceLoc& line, TFunction* pfnCall, int shaderVersion, bool *builtIn = 0); |
| 124 | bool executeInitializer(const TSourceLoc& line, const TString& identifier, TPublicType& pType, |
daniel@transgaming.com | 0578f81 | 2010-05-17 09:58:39 +0000 | [diff] [blame] | 125 | TIntermTyped* initializer, TIntermNode*& intermNode, TVariable* variable = 0); |
alokp@chromium.org | 75fe6b7 | 2011-08-14 05:31:22 +0000 | [diff] [blame] | 126 | |
shannonwoods@chromium.org | 0f376ca | 2013-05-30 00:19:23 +0000 | [diff] [blame] | 127 | TPublicType addFullySpecifiedType(TQualifier qualifier, const TPublicType& typeSpecifier); |
Jamie Madill | a5efff9 | 2013-06-06 11:56:47 -0400 | [diff] [blame] | 128 | TPublicType addFullySpecifiedType(TQualifier qualifier, TLayoutQualifier layoutQualifier, const TPublicType& typeSpecifier); |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 129 | TIntermAggregate* parseSingleDeclaration(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier); |
| 130 | TIntermAggregate* parseSingleArrayDeclaration(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& indexLocation, TIntermTyped *indexExpression); |
| 131 | TIntermAggregate* parseSingleInitDeclaration(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& initLocation, TIntermTyped *initializer); |
Jamie Madill | 47e3ec0 | 2014-08-20 16:38:33 -0400 | [diff] [blame] | 132 | TIntermAggregate* parseInvariantDeclaration(const TSourceLoc &invariantLoc, const TSourceLoc &identifierLoc, const TString *identifier, const TSymbol *symbol); |
| 133 | |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 134 | TIntermAggregate* parseDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration, TSymbol *identifierSymbol, const TSourceLoc& identifierLocation, const TString &identifier); |
| 135 | TIntermAggregate* parseArrayDeclarator(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& arrayLocation, TIntermNode *declaratorList, TIntermTyped *indexExpression); |
| 136 | TIntermAggregate* parseInitDeclarator(TPublicType &publicType, TIntermAggregate *declaratorList, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& initLocation, TIntermTyped *initializer); |
Jamie Madill | a295edf | 2013-06-06 11:56:48 -0400 | [diff] [blame] | 137 | void parseGlobalLayoutQualifier(const TPublicType &typeQualifier); |
shannonwoods@chromium.org | 1885113 | 2013-05-30 00:19:54 +0000 | [diff] [blame] | 138 | TFunction *addConstructorFunc(TPublicType publicType); |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 139 | TIntermTyped* addConstructor(TIntermNode*, const TType*, TOperator, TFunction*, const TSourceLoc&); |
daniel@transgaming.com | 0578f81 | 2010-05-17 09:58:39 +0000 | [diff] [blame] | 140 | TIntermTyped* foldConstConstructor(TIntermAggregate* aggrNode, const TType& type); |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 141 | TIntermTyped* addConstVectorNode(TVectorFields&, TIntermTyped*, const TSourceLoc&); |
| 142 | TIntermTyped* addConstMatrixNode(int , TIntermTyped*, const TSourceLoc&); |
| 143 | TIntermTyped* addConstArrayNode(int index, TIntermTyped* node, const TSourceLoc& line); |
| 144 | TIntermTyped* addConstStruct(const TString &identifier, TIntermTyped *node, const TSourceLoc& line); |
| 145 | TIntermTyped* addIndexExpression(TIntermTyped *baseExpression, const TSourceLoc& location, TIntermTyped *indexExpression); |
| 146 | TIntermTyped* addFieldSelectionExpression(TIntermTyped *baseExpression, const TSourceLoc& dotLocation, const TString &fieldString, const TSourceLoc& fieldLocation); |
shannonwoods@chromium.org | a910088 | 2013-05-30 00:11:39 +0000 | [diff] [blame] | 147 | |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 148 | TFieldList *addStructDeclaratorList(const TPublicType& typeSpecifier, TFieldList *fieldList); |
| 149 | TPublicType addStructure(const TSourceLoc& structLine, const TSourceLoc& nameLine, const TString *structName, TFieldList* fieldList); |
kbr@chromium.org | 476541f | 2011-10-27 21:14:51 +0000 | [diff] [blame] | 150 | |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 151 | TIntermAggregate* addInterfaceBlock(const TPublicType& typeQualifier, const TSourceLoc& nameLine, const TString& blockName, TFieldList* fieldList, |
| 152 | const TString* instanceName, const TSourceLoc& instanceLine, TIntermTyped* arrayIndex, const TSourceLoc& arrayIndexLine); |
shannonwoods@chromium.org | 5668c5d | 2013-05-30 00:11:48 +0000 | [diff] [blame] | 153 | |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 154 | TLayoutQualifier parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine); |
| 155 | TLayoutQualifier parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine, const TString &intValueString, int intValue, const TSourceLoc& intValueLine); |
Jamie Madill | a5efff9 | 2013-06-06 11:56:47 -0400 | [diff] [blame] | 156 | TLayoutQualifier joinLayoutQualifiers(TLayoutQualifier leftQualifier, TLayoutQualifier rightQualifier); |
Jamie Madill | f2e0f9b | 2013-08-26 16:39:42 -0400 | [diff] [blame] | 157 | TPublicType joinInterpolationQualifiers(const TSourceLoc &interpolationLoc, TQualifier interpolationQualifier, |
| 158 | const TSourceLoc &storageLoc, TQualifier storageQualifier); |
shannonwoods@chromium.org | 302adfe | 2013-05-30 00:21:06 +0000 | [diff] [blame] | 159 | |
kbr@chromium.org | 476541f | 2011-10-27 21:14:51 +0000 | [diff] [blame] | 160 | // Performs an error check for embedded struct declarations. |
| 161 | // Returns true if an error was raised due to the declaration of |
| 162 | // this struct. |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 163 | bool enterStructDeclaration(const TSourceLoc& line, const TString& identifier); |
kbr@chromium.org | 476541f | 2011-10-27 21:14:51 +0000 | [diff] [blame] | 164 | void exitStructDeclaration(); |
| 165 | |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 166 | bool structNestingErrorCheck(const TSourceLoc& line, const TField& field); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 167 | }; |
| 168 | |
shannon.woods@transgaming.com | d64b3da | 2013-02-28 23:19:26 +0000 | [diff] [blame] | 169 | int PaParseStrings(size_t count, const char* const string[], const int length[], |
alokp@chromium.org | 044a5cf | 2010-11-12 15:42:16 +0000 | [diff] [blame] | 170 | TParseContext* context); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 171 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 172 | #endif // _PARSER_HELPER_INCLUDED_ |