Home | History | Annotate | Download | only in compiler
      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/Diagnostics.h"
     10 #include "compiler/DirectiveHandler.h"
     11 #include "compiler/localintermediate.h"
     12 #include "compiler/preprocessor/Preprocessor.h"
     13 #include "compiler/ShHandle.h"
     14 #include "compiler/SymbolTable.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             diagnostics(is),
     42             directiveHandler(ext, diagnostics),
     43             preprocessor(&diagnostics, &directiveHandler),
     44             scanner(NULL) {  }
     45     TIntermediate& intermediate; // to hold and build a parse tree
     46     TSymbolTable& symbolTable;   // symbol table that goes with the language currently being parsed
     47     ShShaderType shaderType;              // vertex or fragment language (future: pack or unpack)
     48     ShShaderSpec shaderSpec;              // The language specification compiler conforms to - GLES2 or WebGL.
     49     int compileOptions;
     50     const char* sourcePath;      // Path of source file or NULL.
     51     TIntermNode* treeRoot;       // root of parse tree being created
     52     int loopNestingLevel;        // 0 if outside all loops
     53     int structNestingLevel;      // incremented while parsing a struct declaration
     54     const TType* currentFunctionType;  // the return type of the function that's currently being parsed
     55     bool functionReturnsValue;   // true if a non-void function has a return
     56     bool checksPrecisionErrors;  // true if an error will be generated when a variable is declared without precision, explicit or implicit.
     57     bool fragmentPrecisionHigh;  // true if highp precision is supported in the fragment language.
     58     TString HashErrMsg;
     59     TDiagnostics diagnostics;
     60     TDirectiveHandler directiveHandler;
     61     pp::Preprocessor preprocessor;
     62     void* scanner;
     63 
     64     int numErrors() const { return diagnostics.numErrors(); }
     65     TInfoSink& infoSink() { return diagnostics.infoSink(); }
     66     void error(const TSourceLoc& loc, const char *reason, const char* token,
     67                const char* extraInfo="");
     68     void warning(const TSourceLoc& loc, const char* reason, const char* token,
     69                  const char* extraInfo="");
     70     void trace(const char* str);
     71     void recover();
     72 
     73     bool parseVectorFields(const TString&, int vecSize, TVectorFields&, const TSourceLoc& line);
     74     bool parseMatrixFields(const TString&, int matSize, TMatrixFields&, const TSourceLoc& line);
     75 
     76     bool reservedErrorCheck(const TSourceLoc& line, const TString& identifier);
     77     void assignError(const TSourceLoc& line, const char* op, TString left, TString right);
     78     void unaryOpError(const TSourceLoc& line, const char* op, TString operand);
     79     void binaryOpError(const TSourceLoc& line, const char* op, TString left, TString right);
     80     bool precisionErrorCheck(const TSourceLoc& line, TPrecision precision, TBasicType type);
     81     bool lValueErrorCheck(const TSourceLoc& line, const char* op, TIntermTyped*);
     82     bool constErrorCheck(TIntermTyped* node);
     83     bool integerErrorCheck(TIntermTyped* node, const char* token);
     84     bool globalErrorCheck(const TSourceLoc& line, bool global, const char* token);
     85     bool constructorErrorCheck(const TSourceLoc& line, TIntermNode*, TFunction&, TOperator, TType*);
     86     bool arraySizeErrorCheck(const TSourceLoc& line, TIntermTyped* expr, int& size);
     87     bool arrayQualifierErrorCheck(const TSourceLoc& line, TPublicType type);
     88     bool arrayTypeErrorCheck(const TSourceLoc& line, TPublicType type);
     89     bool arrayErrorCheck(const TSourceLoc& line, TString& identifier, TPublicType type, TVariable*& variable);
     90     bool voidErrorCheck(const TSourceLoc&, const TString&, const TPublicType&);
     91     bool boolErrorCheck(const TSourceLoc&, const TIntermTyped*);
     92     bool boolErrorCheck(const TSourceLoc&, const TPublicType&);
     93     bool samplerErrorCheck(const TSourceLoc& line, const TPublicType& pType, const char* reason);
     94     bool structQualifierErrorCheck(const TSourceLoc& line, const TPublicType& pType);
     95     bool parameterSamplerErrorCheck(const TSourceLoc& line, TQualifier qualifier, const TType& type);
     96     bool nonInitConstErrorCheck(const TSourceLoc& line, TString& identifier, TPublicType& type, bool array);
     97     bool nonInitErrorCheck(const TSourceLoc& line, TString& identifier, TPublicType& type, TVariable*& variable);
     98     bool paramErrorCheck(const TSourceLoc& line, TQualifier qualifier, TQualifier paramQualifier, TType* type);
     99     bool extensionErrorCheck(const TSourceLoc& line, const TString&);
    100 
    101     const TPragma& pragma() const { return directiveHandler.pragma(); }
    102     const TExtensionBehavior& extensionBehavior() const { return directiveHandler.extensionBehavior(); }
    103     bool supportsExtension(const char* extension);
    104     bool isExtensionEnabled(const char* extension) const;
    105 
    106     bool containsSampler(TType& type);
    107     bool areAllChildConst(TIntermAggregate* aggrNode);
    108     const TFunction* findFunction(const TSourceLoc& line, TFunction* pfnCall, bool *builtIn = 0);
    109     bool executeInitializer(const TSourceLoc& line, TString& identifier, TPublicType& pType,
    110                             TIntermTyped* initializer, TIntermNode*& intermNode, TVariable* variable = 0);
    111 
    112     TIntermTyped* addConstructor(TIntermNode*, const TType*, TOperator, TFunction*, const TSourceLoc&);
    113     TIntermTyped* foldConstConstructor(TIntermAggregate* aggrNode, const TType& type);
    114     TIntermTyped* constructStruct(TIntermNode*, TType*, int, const TSourceLoc&, bool subset);
    115     TIntermTyped* constructBuiltIn(const TType*, TOperator, TIntermNode*, const TSourceLoc&, bool subset);
    116     TIntermTyped* addConstVectorNode(TVectorFields&, TIntermTyped*, const TSourceLoc&);
    117     TIntermTyped* addConstMatrixNode(int , TIntermTyped*, const TSourceLoc&);
    118     TIntermTyped* addConstArrayNode(int index, TIntermTyped* node, const TSourceLoc& line);
    119     TIntermTyped* addConstStruct(TString& , TIntermTyped*, const TSourceLoc&);
    120     TIntermTyped* addIndexExpression(TIntermTyped *baseExpression, const TSourceLoc& location, TIntermTyped *indexExpression);
    121 
    122     // Performs an error check for embedded struct declarations.
    123     // Returns true if an error was raised due to the declaration of
    124     // this struct.
    125     bool enterStructDeclaration(const TSourceLoc& line, const TString& identifier);
    126     void exitStructDeclaration();
    127 
    128     bool structNestingErrorCheck(const TSourceLoc& line, const TField& field);
    129 };
    130 
    131 int PaParseStrings(size_t count, const char* const string[], const int length[],
    132                    TParseContext* context);
    133 
    134 #endif // _PARSER_HELPER_INCLUDED_
    135