1//
2// Copyright (c) 2002-2010 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/ExtensionBehavior.h"
10#include "compiler/localintermediate.h"
11#include "compiler/ShHandle.h"
12#include "compiler/SymbolTable.h"
13
14struct TMatrixFields {
15    bool wholeRow;
16    bool wholeCol;
17    int row;
18    int col;
19};
20
21struct TPragma {
22    TPragma(bool o, bool d) : optimize(o), debug(d) { }
23    bool optimize;
24    bool debug;
25    TPragmaTable pragmaTable;
26};
27
28//
29// The following are extra variables needed during parsing, grouped together so
30// they can be passed to the parser without needing a global.
31//
32struct TParseContext {
33    TParseContext(TSymbolTable& symt, const TExtensionBehavior& ext, TIntermediate& interm, ShShaderType type, ShShaderSpec spec, TInfoSink& is) :
34            intermediate(interm), symbolTable(symt), extensionBehavior(ext), infoSink(is), shaderType(type), shaderSpec(spec), treeRoot(0),
35            recoveredFromError(false), numErrors(0), lexAfterType(false), loopNestingLevel(0),
36            inTypeParen(false), scanner(NULL), contextPragma(true, false) {  }
37    TIntermediate& intermediate; // to hold and build a parse tree
38    TSymbolTable& symbolTable;   // symbol table that goes with the language currently being parsed
39    TExtensionBehavior extensionBehavior;  // mapping between supported extensions and current behavior.
40    TInfoSink& infoSink;
41    ShShaderType shaderType;              // vertex or fragment language (future: pack or unpack)
42    ShShaderSpec shaderSpec;              // The language specification compiler conforms to - GLES2 or WebGL.
43    TIntermNode* treeRoot;       // root of parse tree being created
44    bool recoveredFromError;     // true if a parse error has occurred, but we continue to parse
45    int numErrors;
46    bool lexAfterType;           // true if we've recognized a type, so can only be looking for an identifier
47    int loopNestingLevel;        // 0 if outside all loops
48    bool inTypeParen;            // true if in parentheses, looking only for an identifier
49    const TType* currentFunctionType;  // the return type of the function that's currently being parsed
50    bool functionReturnsValue;   // true if a non-void function has a return
51
52    void error(TSourceLoc loc, const char *reason, const char* token,
53               const char* extraInfoFormat, ...);
54    void warning(TSourceLoc loc, const char* reason, const char* token,
55                 const char* extraInfoFormat, ...);
56    bool reservedErrorCheck(int line, const TString& identifier);
57    void recover();
58
59    bool parseVectorFields(const TString&, int vecSize, TVectorFields&, int line);
60    bool parseMatrixFields(const TString&, int matSize, TMatrixFields&, int line);
61    void assignError(int line, const char* op, TString left, TString right);
62    void unaryOpError(int line, const char* op, TString operand);
63    void binaryOpError(int line, const char* op, TString left, TString right);
64    bool precisionErrorCheck(int line, TPrecision precision, TBasicType type);
65    bool lValueErrorCheck(int line, const char* op, TIntermTyped*);
66    bool constErrorCheck(TIntermTyped* node);
67    bool integerErrorCheck(TIntermTyped* node, const char* token);
68    bool globalErrorCheck(int line, bool global, const char* token);
69    bool constructorErrorCheck(int line, TIntermNode*, TFunction&, TOperator, TType*);
70    bool arraySizeErrorCheck(int line, TIntermTyped* expr, int& size);
71    bool arrayQualifierErrorCheck(int line, TPublicType type);
72    bool arrayTypeErrorCheck(int line, TPublicType type);
73    bool arrayErrorCheck(int line, TString& identifier, TPublicType type, TVariable*& variable);
74    bool voidErrorCheck(int, const TString&, const TPublicType&);
75    bool boolErrorCheck(int, const TIntermTyped*);
76    bool boolErrorCheck(int, const TPublicType&);
77    bool samplerErrorCheck(int line, const TPublicType& pType, const char* reason);
78    bool structQualifierErrorCheck(int line, const TPublicType& pType);
79    bool parameterSamplerErrorCheck(int line, TQualifier qualifier, const TType& type);
80    bool containsSampler(TType& type);
81    bool nonInitConstErrorCheck(int line, TString& identifier, TPublicType& type);
82    bool nonInitErrorCheck(int line, TString& identifier, TPublicType& type);
83    bool paramErrorCheck(int line, TQualifier qualifier, TQualifier paramQualifier, TType* type);
84    bool extensionErrorCheck(int line, const TString&);
85    const TFunction* findFunction(int line, TFunction* pfnCall, bool *builtIn = 0);
86    bool executeInitializer(TSourceLoc line, TString& identifier, TPublicType& pType,
87                            TIntermTyped* initializer, TIntermNode*& intermNode, TVariable* variable = 0);
88    bool areAllChildConst(TIntermAggregate* aggrNode);
89    TIntermTyped* addConstructor(TIntermNode*, const TType*, TOperator, TFunction*, TSourceLoc);
90    TIntermTyped* foldConstConstructor(TIntermAggregate* aggrNode, const TType& type);
91    TIntermTyped* constructStruct(TIntermNode*, TType*, int, TSourceLoc, bool subset);
92    TIntermTyped* constructBuiltIn(const TType*, TOperator, TIntermNode*, TSourceLoc, bool subset);
93    TIntermTyped* addConstVectorNode(TVectorFields&, TIntermTyped*, TSourceLoc);
94    TIntermTyped* addConstMatrixNode(int , TIntermTyped*, TSourceLoc);
95    TIntermTyped* addConstArrayNode(int index, TIntermTyped* node, TSourceLoc line);
96    TIntermTyped* addConstStruct(TString& , TIntermTyped*, TSourceLoc);
97    bool arraySetMaxSize(TIntermSymbol*, TType*, int, bool, TSourceLoc);
98    void* scanner;
99    struct TPragma contextPragma;
100    TString HashErrMsg;
101    bool AfterEOF;
102};
103
104int PaParseStrings(int count, const char* const string[], const int length[],
105                   TParseContext* context);
106
107typedef TParseContext* TParseContextPointer;
108extern TParseContextPointer& GetGlobalParseContext();
109#define GlobalParseContext GetGlobalParseContext()
110
111typedef struct TThreadParseContextRec
112{
113    TParseContext *lpGlobalParseContext;
114} TThreadParseContext;
115
116#endif // _PARSER_HELPER_INCLUDED_
117