blob: 1725e5f5c92bc6308d2e26c4f3f5179f88019943 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +00002// Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// 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
alokp@chromium.org8b851c62012-06-15 16:25:11 +00009#include "compiler/Diagnostics.h"
10#include "compiler/DirectiveHandler.h"
alokp@chromium.orgad771eb2010-09-07 17:36:23 +000011#include "compiler/localintermediate.h"
daniel@transgaming.comb401a922012-10-26 18:58:24 +000012#include "compiler/preprocessor/Preprocessor.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000013#include "compiler/ShHandle.h"
14#include "compiler/SymbolTable.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000015
16struct TMatrixFields {
daniel@transgaming.com0578f812010-05-17 09:58:39 +000017 bool wholeRow;
18 bool wholeCol;
19 int row;
20 int col;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000021};
22
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000023//
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//
27struct TParseContext {
zmo@google.comdc4b4f82011-06-17 00:42:53 +000028 TParseContext(TSymbolTable& symt, TExtensionBehavior& ext, TIntermediate& interm, ShShaderType type, ShShaderSpec spec, int options, bool checksPrecErrors, const char* sourcePath, TInfoSink& is) :
kbr@chromium.org476541f2011-10-27 21:14:51 +000029 intermediate(interm),
30 symbolTable(symt),
kbr@chromium.org476541f2011-10-27 21:14:51 +000031 shaderType(type),
32 shaderSpec(spec),
33 compileOptions(options),
34 sourcePath(sourcePath),
35 treeRoot(0),
kbr@chromium.org476541f2011-10-27 21:14:51 +000036 loopNestingLevel(0),
37 structNestingLevel(0),
kbr@chromium.org476541f2011-10-27 21:14:51 +000038 currentFunctionType(NULL),
39 functionReturnsValue(false),
40 checksPrecisionErrors(checksPrecErrors),
alokp@chromium.org73bc2982012-06-19 18:48:05 +000041 diagnostics(is),
shannon.woods%transgaming.com@gtempaccount.com5524db02013-04-13 03:38:16 +000042 shaderVersion(100),
43 directiveHandler(ext, diagnostics, shaderVersion),
alokp@chromium.org73bc2982012-06-19 18:48:05 +000044 preprocessor(&diagnostics, &directiveHandler),
kbr@chromium.org476541f2011-10-27 21:14:51 +000045 scanner(NULL) { }
daniel@transgaming.com0578f812010-05-17 09:58:39 +000046 TIntermediate& intermediate; // to hold and build a parse tree
47 TSymbolTable& symbolTable; // symbol table that goes with the language currently being parsed
alokp@chromium.org4888ceb2010-10-01 21:13:12 +000048 ShShaderType shaderType; // vertex or fragment language (future: pack or unpack)
49 ShShaderSpec shaderSpec; // The language specification compiler conforms to - GLES2 or WebGL.
shannon.woods%transgaming.com@gtempaccount.com5524db02013-04-13 03:38:16 +000050 int shaderVersion;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +000051 int compileOptions;
52 const char* sourcePath; // Path of source file or NULL.
daniel@transgaming.com0578f812010-05-17 09:58:39 +000053 TIntermNode* treeRoot; // root of parse tree being created
daniel@transgaming.com0578f812010-05-17 09:58:39 +000054 int loopNestingLevel; // 0 if outside all loops
kbr@chromium.org476541f2011-10-27 21:14:51 +000055 int structNestingLevel; // incremented while parsing a struct declaration
daniel@transgaming.com0578f812010-05-17 09:58:39 +000056 const TType* currentFunctionType; // the return type of the function that's currently being parsed
57 bool functionReturnsValue; // true if a non-void function has a return
zmo@google.comdc4b4f82011-06-17 00:42:53 +000058 bool checksPrecisionErrors; // true if an error will be generated when a variable is declared without precision, explicit or implicit.
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +000059 bool fragmentPrecisionHigh; // true if highp precision is supported in the fragment language.
alokp@chromium.org75fe6b72011-08-14 05:31:22 +000060 TString HashErrMsg;
alokp@chromium.org73bc2982012-06-19 18:48:05 +000061 TDiagnostics diagnostics;
62 TDirectiveHandler directiveHandler;
63 pp::Preprocessor preprocessor;
alokp@chromium.org75fe6b72011-08-14 05:31:22 +000064 void* scanner;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000065
shannon.woods%transgaming.com@gtempaccount.com5524db02013-04-13 03:38:16 +000066 int getShaderVersion() const { return shaderVersion; }
alokp@chromium.org6b495712012-06-29 00:06:58 +000067 int numErrors() const { return diagnostics.numErrors(); }
alokp@chromium.org646ea1e2012-06-15 17:36:31 +000068 TInfoSink& infoSink() { return diagnostics.infoSink(); }
alokp@chromium.org044a5cf2010-11-12 15:42:16 +000069 void error(TSourceLoc loc, const char *reason, const char* token,
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +000070 const char* extraInfo="");
alokp@chromium.org044a5cf2010-11-12 15:42:16 +000071 void warning(TSourceLoc loc, const char* reason, const char* token,
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +000072 const char* extraInfo="");
alokp@chromium.org8b851c62012-06-15 16:25:11 +000073 void trace(const char* str);
daniel@transgaming.com0578f812010-05-17 09:58:39 +000074 void recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000075
daniel@transgaming.com0578f812010-05-17 09:58:39 +000076 bool parseVectorFields(const TString&, int vecSize, TVectorFields&, int line);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +000077 bool parseMatrixFields(const TString&, int matCols, int matRows, TMatrixFields&, int line);
alokp@chromium.org75fe6b72011-08-14 05:31:22 +000078
79 bool reservedErrorCheck(int line, const TString& identifier);
daniel@transgaming.com0578f812010-05-17 09:58:39 +000080 void assignError(int line, const char* op, TString left, TString right);
81 void unaryOpError(int line, const char* op, TString operand);
82 void binaryOpError(int line, const char* op, TString left, TString right);
daniel@transgaming.coma5d76232010-05-17 09:58:47 +000083 bool precisionErrorCheck(int line, TPrecision precision, TBasicType type);
daniel@transgaming.com0578f812010-05-17 09:58:39 +000084 bool lValueErrorCheck(int line, const char* op, TIntermTyped*);
85 bool constErrorCheck(TIntermTyped* node);
86 bool integerErrorCheck(TIntermTyped* node, const char* token);
87 bool globalErrorCheck(int line, bool global, const char* token);
88 bool constructorErrorCheck(int line, TIntermNode*, TFunction&, TOperator, TType*);
89 bool arraySizeErrorCheck(int line, TIntermTyped* expr, int& size);
90 bool arrayQualifierErrorCheck(int line, TPublicType type);
91 bool arrayTypeErrorCheck(int line, TPublicType type);
92 bool arrayErrorCheck(int line, TString& identifier, TPublicType type, TVariable*& variable);
93 bool voidErrorCheck(int, const TString&, const TPublicType&);
94 bool boolErrorCheck(int, const TIntermTyped*);
95 bool boolErrorCheck(int, const TPublicType&);
96 bool samplerErrorCheck(int line, const TPublicType& pType, const char* reason);
97 bool structQualifierErrorCheck(int line, const TPublicType& pType);
98 bool parameterSamplerErrorCheck(int line, TQualifier qualifier, const TType& type);
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +000099 bool nonInitConstErrorCheck(int line, TString& identifier, TPublicType& type, bool array);
zmo@google.comfd747b82011-04-23 01:30:07 +0000100 bool nonInitErrorCheck(int line, TString& identifier, TPublicType& type, TVariable*& variable);
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000101 bool paramErrorCheck(int line, TQualifier qualifier, TQualifier paramQualifier, TType* type);
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000102 bool extensionErrorCheck(int line, const TString&);
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000103
alokp@chromium.org73bc2982012-06-19 18:48:05 +0000104 const TExtensionBehavior& extensionBehavior() const { return directiveHandler.extensionBehavior(); }
zmo@google.com09c323a2011-08-12 18:22:25 +0000105 bool supportsExtension(const char* extension);
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000106 void handleExtensionDirective(int line, const char* extName, const char* behavior);
107
108 const TPragma& pragma() const { return directiveHandler.pragma(); }
109 void handlePragmaDirective(int line, const char* name, const char* value);
alokp@chromium.org75fe6b72011-08-14 05:31:22 +0000110
111 bool containsSampler(TType& type);
112 bool areAllChildConst(TIntermAggregate* aggrNode);
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000113 const TFunction* findFunction(int line, TFunction* pfnCall, int shaderVersion, bool *builtIn = 0);
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000114 bool executeInitializer(TSourceLoc line, TString& identifier, TPublicType& pType,
115 TIntermTyped* initializer, TIntermNode*& intermNode, TVariable* variable = 0);
alokp@chromium.org75fe6b72011-08-14 05:31:22 +0000116 bool arraySetMaxSize(TIntermSymbol*, TType*, int, bool, TSourceLoc);
117
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +0000118 TPublicType addFullySpecifiedType(TQualifier qualifier, const TPublicType& typeSpecifier);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +0000119 TFunction *addConstructorFunc(TPublicType publicType);
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000120 TIntermTyped* addConstructor(TIntermNode*, const TType*, TOperator, TFunction*, TSourceLoc);
121 TIntermTyped* foldConstConstructor(TIntermAggregate* aggrNode, const TType& type);
122 TIntermTyped* constructStruct(TIntermNode*, TType*, int, TSourceLoc, bool subset);
123 TIntermTyped* constructBuiltIn(const TType*, TOperator, TIntermNode*, TSourceLoc, bool subset);
124 TIntermTyped* addConstVectorNode(TVectorFields&, TIntermTyped*, TSourceLoc);
125 TIntermTyped* addConstMatrixNode(int , TIntermTyped*, TSourceLoc);
126 TIntermTyped* addConstArrayNode(int index, TIntermTyped* node, TSourceLoc line);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +0000127 TIntermTyped* addConstStruct(const TString &identifier, TIntermTyped *node, TSourceLoc line);
128 TIntermTyped* addIndexExpression(TIntermTyped *baseExpression, TSourceLoc location, TIntermTyped *indexExpression);
129 TIntermTyped* addFieldSelectionExpression(TIntermTyped *baseExpression, TSourceLoc dotLocation, const TString &fieldString, TSourceLoc fieldLocation);
130
131 TTypeList *addStructDeclaratorList(const TPublicType& typeSpecifier, TTypeList *typeList);
132 TPublicType addStructure(TSourceLoc structLine, TSourceLoc nameLine, const TString &structName, TTypeList* typeList);
kbr@chromium.org476541f2011-10-27 21:14:51 +0000133
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +0000134 TIntermAggregate* addInterfaceBlock(const TPublicType& typeQualifier, TSourceLoc nameLine, const TString& blockName, TTypeList* typeList,
135 const TString& instanceName, TSourceLoc instanceLine, TIntermTyped* arrayIndex, TSourceLoc arrayIndexLine);
136
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +0000137 TLayoutQualifierId addLayoutQualifierId(const TString &qualifierType, TSourceLoc qualifierTypeLine);
138 TLayoutQualifierId addLayoutQualifierId(const TString &qualifierType, TSourceLoc qualifierTypeLine, const TString &intValueString, int intValue, TSourceLoc intValueLine);
139 TLayoutQualifier* makeLayoutQualifierFromId(TLayoutQualifierId layoutQualifierId);
140 TLayoutQualifier* extendLayoutQualifier(TLayoutQualifier *layoutQualifier, TLayoutQualifierId layoutQualifierId);
141
kbr@chromium.org476541f2011-10-27 21:14:51 +0000142 // Performs an error check for embedded struct declarations.
143 // Returns true if an error was raised due to the declaration of
144 // this struct.
145 bool enterStructDeclaration(TSourceLoc line, const TString& identifier);
146 void exitStructDeclaration();
147
148 bool structNestingErrorCheck(TSourceLoc line, const TType& fieldType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000149};
150
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000151int PaParseStrings(size_t count, const char* const string[], const int length[],
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000152 TParseContext* context);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000153
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000154#endif // _PARSER_HELPER_INCLUDED_