2 // Copyright (c) 2002-2012 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.
6 #ifndef _PARSER_HELPER_INCLUDED_
7 #define _PARSER_HELPER_INCLUDED_
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"
16 struct TMatrixFields {
24 // The following are extra variables needed during parsing, grouped together so
25 // they can be passed to the parser without needing a global.
27 struct TParseContext {
28 TParseContext(TSymbolTable& symt, TExtensionBehavior& ext, TIntermediate& interm, ShShaderType type, ShShaderSpec spec, int options, bool checksPrecErrors, const char* sourcePath, TInfoSink& is) :
33 compileOptions(options),
34 sourcePath(sourcePath),
38 structNestingLevel(0),
40 currentFunctionType(NULL),
41 functionReturnsValue(false),
42 checksPrecisionErrors(checksPrecErrors),
44 directiveHandler(ext, diagnostics),
45 preprocessor(&diagnostics, &directiveHandler),
47 TIntermediate& intermediate; // to hold and build a parse tree
48 TSymbolTable& symbolTable; // symbol table that goes with the language currently being parsed
49 ShShaderType shaderType; // vertex or fragment language (future: pack or unpack)
50 ShShaderSpec shaderSpec; // The language specification compiler conforms to - GLES2 or WebGL.
52 const char* sourcePath; // Path of source file or NULL.
53 TIntermNode* treeRoot; // root of parse tree being created
54 bool lexAfterType; // true if we've recognized a type, so can only be looking for an identifier
55 int loopNestingLevel; // 0 if outside all loops
56 int structNestingLevel; // incremented while parsing a struct declaration
57 bool inTypeParen; // true if in parentheses, looking only for an identifier
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.
63 TDiagnostics diagnostics;
64 TDirectiveHandler directiveHandler;
65 pp::Preprocessor preprocessor;
68 int numErrors() const { return diagnostics.numErrors(); }
69 TInfoSink& infoSink() { return diagnostics.infoSink(); }
70 void error(TSourceLoc loc, const char *reason, const char* token,
71 const char* extraInfo="");
72 void warning(TSourceLoc loc, const char* reason, const char* token,
73 const char* extraInfo="");
74 void trace(const char* str);
77 bool parseVectorFields(const TString&, int vecSize, TVectorFields&, int line);
78 bool parseMatrixFields(const TString&, int matSize, TMatrixFields&, int line);
80 bool reservedErrorCheck(int line, const TString& identifier);
81 void assignError(int line, const char* op, TString left, TString right);
82 void unaryOpError(int line, const char* op, TString operand);
83 void binaryOpError(int line, const char* op, TString left, TString right);
84 bool precisionErrorCheck(int line, TPrecision precision, TBasicType type);
85 bool lValueErrorCheck(int line, const char* op, TIntermTyped*);
86 bool constErrorCheck(TIntermTyped* node);
87 bool integerErrorCheck(TIntermTyped* node, const char* token);
88 bool globalErrorCheck(int line, bool global, const char* token);
89 bool constructorErrorCheck(int line, TIntermNode*, TFunction&, TOperator, TType*);
90 bool arraySizeErrorCheck(int line, TIntermTyped* expr, int& size);
91 bool arrayQualifierErrorCheck(int line, TPublicType type);
92 bool arrayTypeErrorCheck(int line, TPublicType type);
93 bool arrayErrorCheck(int line, TString& identifier, TPublicType type, TVariable*& variable);
94 bool voidErrorCheck(int, const TString&, const TPublicType&);
95 bool boolErrorCheck(int, const TIntermTyped*);
96 bool boolErrorCheck(int, const TPublicType&);
97 bool samplerErrorCheck(int line, const TPublicType& pType, const char* reason);
98 bool structQualifierErrorCheck(int line, const TPublicType& pType);
99 bool parameterSamplerErrorCheck(int line, TQualifier qualifier, const TType& type);
100 bool nonInitConstErrorCheck(int line, TString& identifier, TPublicType& type, bool array);
101 bool nonInitErrorCheck(int line, TString& identifier, TPublicType& type, TVariable*& variable);
102 bool paramErrorCheck(int line, TQualifier qualifier, TQualifier paramQualifier, TType* type);
103 bool extensionErrorCheck(int line, const TString&);
105 const TExtensionBehavior& extensionBehavior() const { return directiveHandler.extensionBehavior(); }
106 bool supportsExtension(const char* extension);
107 void handleExtensionDirective(int line, const char* extName, const char* behavior);
109 const TPragma& pragma() const { return directiveHandler.pragma(); }
110 void handlePragmaDirective(int line, const char* name, const char* value);
112 bool containsSampler(TType& type);
113 bool areAllChildConst(TIntermAggregate* aggrNode);
114 const TFunction* findFunction(int line, TFunction* pfnCall, bool *builtIn = 0);
115 bool executeInitializer(TSourceLoc line, TString& identifier, TPublicType& pType,
116 TIntermTyped* initializer, TIntermNode*& intermNode, TVariable* variable = 0);
117 bool arraySetMaxSize(TIntermSymbol*, TType*, int, bool, TSourceLoc);
119 TIntermTyped* addConstructor(TIntermNode*, const TType*, TOperator, TFunction*, TSourceLoc);
120 TIntermTyped* foldConstConstructor(TIntermAggregate* aggrNode, const TType& type);
121 TIntermTyped* addConstVectorNode(TVectorFields&, TIntermTyped*, TSourceLoc);
122 TIntermTyped* addConstMatrixNode(int , TIntermTyped*, TSourceLoc);
123 TIntermTyped* addConstArrayNode(int index, TIntermTyped* node, TSourceLoc line);
124 TIntermTyped* addConstStruct(TString& , TIntermTyped*, TSourceLoc);
126 // Performs an error check for embedded struct declarations.
127 // Returns true if an error was raised due to the declaration of
129 bool enterStructDeclaration(TSourceLoc line, const TString& identifier);
130 void exitStructDeclaration();
132 bool structNestingErrorCheck(TSourceLoc line, const TType& fieldType);
135 int PaParseStrings(int count, const char* const string[], const int length[],
136 TParseContext* context);
138 #endif // _PARSER_HELPER_INCLUDED_