1 // 2 // Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved. 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 9 #include "compiler/translator/Diagnostics.h" 10 #include "compiler/translator/DirectiveHandler.h" 11 #include "compiler/translator/localintermediate.h" 12 #include "compiler/translator/ShHandle.h" 13 #include "compiler/translator/SymbolTable.h" 14 #include "compiler/preprocessor/Preprocessor.h" 15 16 struct TMatrixFields { 17 bool wholeRow; 18 bool wholeCol; 19 int row; 20 int col; 21 }; 22 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 { 28 TParseContext(TSymbolTable& symt, TExtensionBehavior& ext, TIntermediate& interm, ShShaderType type, ShShaderSpec spec, int options, bool checksPrecErrors, const char* sourcePath, TInfoSink& is) : 29 intermediate(interm), 30 symbolTable(symt), 31 shaderType(type), 32 shaderSpec(spec), 33 compileOptions(options), 34 sourcePath(sourcePath), 35 treeRoot(0), 36 loopNestingLevel(0), 37 structNestingLevel(0), 38 currentFunctionType(NULL), 39 functionReturnsValue(false), 40 checksPrecisionErrors(checksPrecErrors), 41 defaultMatrixPacking(EmpColumnMajor), 42 defaultBlockStorage(EbsShared), 43 diagnostics(is), 44 shaderVersion(100), 45 directiveHandler(ext, diagnostics, shaderVersion), 46 preprocessor(&diagnostics, &directiveHandler), 47 scanner(NULL) { } 48 TIntermediate& intermediate; // to hold and build a parse tree 49 TSymbolTable& symbolTable; // symbol table that goes with the language currently being parsed 50 ShShaderType shaderType; // vertex or fragment language (future: pack or unpack) 51 ShShaderSpec shaderSpec; // The language specification compiler conforms to - GLES2 or WebGL. 52 int shaderVersion; 53 int compileOptions; 54 const char* sourcePath; // Path of source file or NULL. 55 TIntermNode* treeRoot; // root of parse tree being created 56 int loopNestingLevel; // 0 if outside all loops 57 int structNestingLevel; // incremented while parsing a struct declaration 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 60 bool checksPrecisionErrors; // true if an error will be generated when a variable is declared without precision, explicit or implicit. 61 bool fragmentPrecisionHigh; // true if highp precision is supported in the fragment language. 62 TLayoutMatrixPacking defaultMatrixPacking; 63 TLayoutBlockStorage defaultBlockStorage; 64 TString HashErrMsg; 65 TDiagnostics diagnostics; 66 TDirectiveHandler directiveHandler; 67 pp::Preprocessor preprocessor; 68 void* scanner; 69 70 int getShaderVersion() const { return shaderVersion; } 71 int numErrors() const { return diagnostics.numErrors(); } 72 TInfoSink& infoSink() { return diagnostics.infoSink(); } 73 void error(const TSourceLoc& loc, const char *reason, const char* token, 74 const char* extraInfo=""); 75 void warning(const TSourceLoc& loc, const char* reason, const char* token, 76 const char* extraInfo=""); 77 void trace(const char* str); 78 void recover(); 79 80 bool parseVectorFields(const TString&, int vecSize, TVectorFields&, const TSourceLoc& line); 81 bool parseMatrixFields(const TString&, int matCols, int matRows, TMatrixFields&, const TSourceLoc& line); 82 83 bool reservedErrorCheck(const TSourceLoc& line, const TString& identifier); 84 void assignError(const TSourceLoc& line, const char* op, TString left, TString right); 85 void unaryOpError(const TSourceLoc& line, const char* op, TString operand); 86 void binaryOpError(const TSourceLoc& line, const char* op, TString left, TString right); 87 bool precisionErrorCheck(const TSourceLoc& line, TPrecision precision, TBasicType type); 88 bool lValueErrorCheck(const TSourceLoc& line, const char* op, TIntermTyped*); 89 bool constErrorCheck(TIntermTyped* node); 90 bool integerErrorCheck(TIntermTyped* node, const char* token); 91 bool globalErrorCheck(const TSourceLoc& line, bool global, const char* token); 92 bool constructorErrorCheck(const TSourceLoc& line, TIntermNode*, TFunction&, TOperator, TType*); 93 bool arraySizeErrorCheck(const TSourceLoc& line, TIntermTyped* expr, int& size); 94 bool arrayQualifierErrorCheck(const TSourceLoc& line, TPublicType type); 95 bool arrayTypeErrorCheck(const TSourceLoc& line, TPublicType type); 96 bool arrayErrorCheck(const TSourceLoc& line, const TString& identifier, const TPublicType &type, TVariable*& variable); 97 bool voidErrorCheck(const TSourceLoc&, const TString&, const TPublicType&); 98 bool boolErrorCheck(const TSourceLoc&, const TIntermTyped*); 99 bool boolErrorCheck(const TSourceLoc&, const TPublicType&); 100 bool samplerErrorCheck(const TSourceLoc& line, const TPublicType& pType, const char* reason); 101 bool structQualifierErrorCheck(const TSourceLoc& line, const TPublicType& pType); 102 bool locationDeclaratorListCheck(const TSourceLoc& line, const TPublicType &pType); 103 bool parameterSamplerErrorCheck(const TSourceLoc& line, TQualifier qualifier, const TType& type); 104 bool nonInitConstErrorCheck(const TSourceLoc& line, const TString& identifier, TPublicType& type, bool array); 105 bool nonInitErrorCheck(const TSourceLoc& line, const TString& identifier, const TPublicType& type, TVariable*& variable); 106 bool paramErrorCheck(const TSourceLoc& line, TQualifier qualifier, TQualifier paramQualifier, TType* type); 107 bool extensionErrorCheck(const TSourceLoc& line, const TString&); 108 bool singleDeclarationErrorCheck(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier); 109 bool layoutLocationErrorCheck(const TSourceLoc& location, const TLayoutQualifier &layoutQualifier); 110 111 const TPragma& pragma() const { return directiveHandler.pragma(); } 112 const TExtensionBehavior& extensionBehavior() const { return directiveHandler.extensionBehavior(); } 113 bool supportsExtension(const char* extension); 114 bool isExtensionEnabled(const char* extension) const; 115 void handleExtensionDirective(const TSourceLoc& loc, const char* extName, const char* behavior); 116 void handlePragmaDirective(const TSourceLoc& loc, const char* name, const char* value); 117 118 bool containsSampler(TType& type); 119 bool areAllChildConst(TIntermAggregate* aggrNode); 120 const TFunction* findFunction(const TSourceLoc& line, TFunction* pfnCall, int shaderVersion, bool *builtIn = 0); 121 bool executeInitializer(const TSourceLoc& line, const TString& identifier, TPublicType& pType, 122 TIntermTyped* initializer, TIntermNode*& intermNode, TVariable* variable = 0); 123 124 TPublicType addFullySpecifiedType(TQualifier qualifier, const TPublicType& typeSpecifier); 125 TPublicType addFullySpecifiedType(TQualifier qualifier, TLayoutQualifier layoutQualifier, const TPublicType& typeSpecifier); 126 TIntermAggregate* parseSingleDeclaration(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier); 127 TIntermAggregate* parseSingleArrayDeclaration(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& indexLocation, TIntermTyped *indexExpression); 128 TIntermAggregate* parseSingleInitDeclaration(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& initLocation, TIntermTyped *initializer); 129 TIntermAggregate* parseDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration, TSymbol *identifierSymbol, const TSourceLoc& identifierLocation, const TString &identifier); 130 TIntermAggregate* parseArrayDeclarator(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& arrayLocation, TIntermNode *declaratorList, TIntermTyped *indexExpression); 131 TIntermAggregate* parseInitDeclarator(TPublicType &publicType, TIntermAggregate *declaratorList, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& initLocation, TIntermTyped *initializer); 132 void parseGlobalLayoutQualifier(const TPublicType &typeQualifier); 133 TFunction *addConstructorFunc(TPublicType publicType); 134 TIntermTyped* addConstructor(TIntermNode*, const TType*, TOperator, TFunction*, const TSourceLoc&); 135 TIntermTyped* foldConstConstructor(TIntermAggregate* aggrNode, const TType& type); 136 TIntermTyped* constructStruct(TIntermNode*, TType*, int, const TSourceLoc&, bool subset); 137 TIntermTyped* constructBuiltIn(const TType*, TOperator, TIntermNode*, const TSourceLoc&, bool subset); 138 TIntermTyped* addConstVectorNode(TVectorFields&, TIntermTyped*, const TSourceLoc&); 139 TIntermTyped* addConstMatrixNode(int , TIntermTyped*, const TSourceLoc&); 140 TIntermTyped* addConstArrayNode(int index, TIntermTyped* node, const TSourceLoc& line); 141 TIntermTyped* addConstStruct(const TString &identifier, TIntermTyped *node, const TSourceLoc& line); 142 TIntermTyped* addIndexExpression(TIntermTyped *baseExpression, const TSourceLoc& location, TIntermTyped *indexExpression); 143 TIntermTyped* addFieldSelectionExpression(TIntermTyped *baseExpression, const TSourceLoc& dotLocation, const TString &fieldString, const TSourceLoc& fieldLocation); 144 145 TFieldList *addStructDeclaratorList(const TPublicType& typeSpecifier, TFieldList *fieldList); 146 TPublicType addStructure(const TSourceLoc& structLine, const TSourceLoc& nameLine, const TString *structName, TFieldList* fieldList); 147 148 TIntermAggregate* addInterfaceBlock(const TPublicType& typeQualifier, const TSourceLoc& nameLine, const TString& blockName, TFieldList* fieldList, 149 const TString* instanceName, const TSourceLoc& instanceLine, TIntermTyped* arrayIndex, const TSourceLoc& arrayIndexLine); 150 151 TLayoutQualifier parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine); 152 TLayoutQualifier parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine, const TString &intValueString, int intValue, const TSourceLoc& intValueLine); 153 TLayoutQualifier joinLayoutQualifiers(TLayoutQualifier leftQualifier, TLayoutQualifier rightQualifier); 154 TPublicType joinInterpolationQualifiers(const TSourceLoc &interpolationLoc, TQualifier interpolationQualifier, 155 const TSourceLoc &storageLoc, TQualifier storageQualifier); 156 157 // Performs an error check for embedded struct declarations. 158 // Returns true if an error was raised due to the declaration of 159 // this struct. 160 bool enterStructDeclaration(const TSourceLoc& line, const TString& identifier); 161 void exitStructDeclaration(); 162 163 bool structNestingErrorCheck(const TSourceLoc& line, const TField& field); 164 }; 165 166 int PaParseStrings(size_t count, const char* const string[], const int length[], 167 TParseContext* context); 168 169 #endif // _PARSER_HELPER_INCLUDED_ 170