blob: 51951e646d294e799978d632f0f1b21dcb710c92 [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),
Jamie Madill099c0f32013-06-20 11:55:52 -040041 defaultMatrixPacking(EmpColumnMajor),
Jamie Madill1566ef72013-06-20 11:55:54 -040042 defaultBlockStorage(EbsShared),
alokp@chromium.org73bc2982012-06-19 18:48:05 +000043 diagnostics(is),
shannon.woods%transgaming.com@gtempaccount.com5524db02013-04-13 03:38:16 +000044 shaderVersion(100),
45 directiveHandler(ext, diagnostics, shaderVersion),
alokp@chromium.org73bc2982012-06-19 18:48:05 +000046 preprocessor(&diagnostics, &directiveHandler),
kbr@chromium.org476541f2011-10-27 21:14:51 +000047 scanner(NULL) { }
daniel@transgaming.com0578f812010-05-17 09:58:39 +000048 TIntermediate& intermediate; // to hold and build a parse tree
49 TSymbolTable& symbolTable; // symbol table that goes with the language currently being parsed
alokp@chromium.org4888ceb2010-10-01 21:13:12 +000050 ShShaderType shaderType; // vertex or fragment language (future: pack or unpack)
51 ShShaderSpec shaderSpec; // The language specification compiler conforms to - GLES2 or WebGL.
shannon.woods%transgaming.com@gtempaccount.com5524db02013-04-13 03:38:16 +000052 int shaderVersion;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +000053 int compileOptions;
54 const char* sourcePath; // Path of source file or NULL.
daniel@transgaming.com0578f812010-05-17 09:58:39 +000055 TIntermNode* treeRoot; // root of parse tree being created
daniel@transgaming.com0578f812010-05-17 09:58:39 +000056 int loopNestingLevel; // 0 if outside all loops
kbr@chromium.org476541f2011-10-27 21:14:51 +000057 int structNestingLevel; // incremented while parsing a struct declaration
daniel@transgaming.com0578f812010-05-17 09:58:39 +000058 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
zmo@google.comdc4b4f82011-06-17 00:42:53 +000060 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 +000061 bool fragmentPrecisionHigh; // true if highp precision is supported in the fragment language.
Jamie Madill099c0f32013-06-20 11:55:52 -040062 TLayoutMatrixPacking defaultMatrixPacking;
Jamie Madill1566ef72013-06-20 11:55:54 -040063 TLayoutBlockStorage defaultBlockStorage;
alokp@chromium.org75fe6b72011-08-14 05:31:22 +000064 TString HashErrMsg;
alokp@chromium.org73bc2982012-06-19 18:48:05 +000065 TDiagnostics diagnostics;
66 TDirectiveHandler directiveHandler;
67 pp::Preprocessor preprocessor;
alokp@chromium.org75fe6b72011-08-14 05:31:22 +000068 void* scanner;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000069
shannon.woods%transgaming.com@gtempaccount.com5524db02013-04-13 03:38:16 +000070 int getShaderVersion() const { return shaderVersion; }
alokp@chromium.org6b495712012-06-29 00:06:58 +000071 int numErrors() const { return diagnostics.numErrors(); }
alokp@chromium.org646ea1e2012-06-15 17:36:31 +000072 TInfoSink& infoSink() { return diagnostics.infoSink(); }
Jamie Madill075edd82013-07-08 13:30:19 -040073 void error(const TSourceLoc& loc, const char *reason, const char* token,
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +000074 const char* extraInfo="");
Jamie Madill075edd82013-07-08 13:30:19 -040075 void warning(const TSourceLoc& loc, const char* reason, const char* token,
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +000076 const char* extraInfo="");
alokp@chromium.org8b851c62012-06-15 16:25:11 +000077 void trace(const char* str);
daniel@transgaming.com0578f812010-05-17 09:58:39 +000078 void recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000079
Jamie Madill075edd82013-07-08 13:30:19 -040080 bool parseVectorFields(const TString&, int vecSize, TVectorFields&, const TSourceLoc& line);
81 bool parseMatrixFields(const TString&, int matCols, int matRows, TMatrixFields&, const TSourceLoc& line);
alokp@chromium.org75fe6b72011-08-14 05:31:22 +000082
Jamie Madill075edd82013-07-08 13:30:19 -040083 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*);
daniel@transgaming.com0578f812010-05-17 09:58:39 +000089 bool constErrorCheck(TIntermTyped* node);
90 bool integerErrorCheck(TIntermTyped* node, const char* token);
Jamie Madill075edd82013-07-08 13:30:19 -040091 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);
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000110
Jamie Madill7a217de2013-07-08 15:10:00 -0400111 const TPragma& pragma() const { return directiveHandler.pragma(); }
alokp@chromium.org73bc2982012-06-19 18:48:05 +0000112 const TExtensionBehavior& extensionBehavior() const { return directiveHandler.extensionBehavior(); }
zmo@google.com09c323a2011-08-12 18:22:25 +0000113 bool supportsExtension(const char* extension);
Jamie Madill5d287f52013-07-12 15:38:19 -0400114 bool isExtensionEnabled(const char* extension) const;
Jamie Madill075edd82013-07-08 13:30:19 -0400115 void handleExtensionDirective(const TSourceLoc& loc, const char* extName, const char* behavior);
116 void handlePragmaDirective(const TSourceLoc& loc, const char* name, const char* value);
alokp@chromium.org75fe6b72011-08-14 05:31:22 +0000117
118 bool containsSampler(TType& type);
119 bool areAllChildConst(TIntermAggregate* aggrNode);
Jamie Madill075edd82013-07-08 13:30:19 -0400120 const TFunction* findFunction(const TSourceLoc& line, TFunction* pfnCall, int shaderVersion, bool *builtIn = 0);
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400121 bool isVariableBuiltIn(const TVariable* var);
Jamie Madill075edd82013-07-08 13:30:19 -0400122 bool executeInitializer(const TSourceLoc& line, const TString& identifier, TPublicType& pType,
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000123 TIntermTyped* initializer, TIntermNode*& intermNode, TVariable* variable = 0);
alokp@chromium.org75fe6b72011-08-14 05:31:22 +0000124
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +0000125 TPublicType addFullySpecifiedType(TQualifier qualifier, const TPublicType& typeSpecifier);
Jamie Madilla5efff92013-06-06 11:56:47 -0400126 TPublicType addFullySpecifiedType(TQualifier qualifier, TLayoutQualifier layoutQualifier, const TPublicType& typeSpecifier);
Jamie Madill075edd82013-07-08 13:30:19 -0400127 TIntermAggregate* parseSingleDeclaration(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier);
128 TIntermAggregate* parseSingleArrayDeclaration(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& indexLocation, TIntermTyped *indexExpression);
129 TIntermAggregate* parseSingleInitDeclaration(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& initLocation, TIntermTyped *initializer);
130 TIntermAggregate* parseDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration, TSymbol *identifierSymbol, const TSourceLoc& identifierLocation, const TString &identifier);
131 TIntermAggregate* parseArrayDeclarator(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& arrayLocation, TIntermNode *declaratorList, TIntermTyped *indexExpression);
132 TIntermAggregate* parseInitDeclarator(TPublicType &publicType, TIntermAggregate *declaratorList, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& initLocation, TIntermTyped *initializer);
Jamie Madilla295edf2013-06-06 11:56:48 -0400133 void parseGlobalLayoutQualifier(const TPublicType &typeQualifier);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +0000134 TFunction *addConstructorFunc(TPublicType publicType);
Jamie Madill075edd82013-07-08 13:30:19 -0400135 TIntermTyped* addConstructor(TIntermNode*, const TType*, TOperator, TFunction*, const TSourceLoc&);
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000136 TIntermTyped* foldConstConstructor(TIntermAggregate* aggrNode, const TType& type);
Jamie Madill075edd82013-07-08 13:30:19 -0400137 TIntermTyped* constructStruct(TIntermNode*, TType*, int, const TSourceLoc&, bool subset);
138 TIntermTyped* constructBuiltIn(const TType*, TOperator, TIntermNode*, const TSourceLoc&, bool subset);
139 TIntermTyped* addConstVectorNode(TVectorFields&, TIntermTyped*, const TSourceLoc&);
140 TIntermTyped* addConstMatrixNode(int , TIntermTyped*, const TSourceLoc&);
141 TIntermTyped* addConstArrayNode(int index, TIntermTyped* node, const TSourceLoc& line);
142 TIntermTyped* addConstStruct(const TString &identifier, TIntermTyped *node, const TSourceLoc& line);
143 TIntermTyped* addIndexExpression(TIntermTyped *baseExpression, const TSourceLoc& location, TIntermTyped *indexExpression);
144 TIntermTyped* addFieldSelectionExpression(TIntermTyped *baseExpression, const TSourceLoc& dotLocation, const TString &fieldString, const TSourceLoc& fieldLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +0000145
Jamie Madill98493dd2013-07-08 14:39:03 -0400146 TFieldList *addStructDeclaratorList(const TPublicType& typeSpecifier, TFieldList *fieldList);
147 TPublicType addStructure(const TSourceLoc& structLine, const TSourceLoc& nameLine, const TString *structName, TFieldList* fieldList);
kbr@chromium.org476541f2011-10-27 21:14:51 +0000148
Jamie Madill98493dd2013-07-08 14:39:03 -0400149 TIntermAggregate* addInterfaceBlock(const TPublicType& typeQualifier, const TSourceLoc& nameLine, const TString& blockName, TFieldList* fieldList,
150 const TString* instanceName, const TSourceLoc& instanceLine, TIntermTyped* arrayIndex, const TSourceLoc& arrayIndexLine);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +0000151
Jamie Madill075edd82013-07-08 13:30:19 -0400152 TLayoutQualifier parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine);
153 TLayoutQualifier parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine, const TString &intValueString, int intValue, const TSourceLoc& intValueLine);
Jamie Madilla5efff92013-06-06 11:56:47 -0400154 TLayoutQualifier joinLayoutQualifiers(TLayoutQualifier leftQualifier, TLayoutQualifier rightQualifier);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +0000155
kbr@chromium.org476541f2011-10-27 21:14:51 +0000156 // Performs an error check for embedded struct declarations.
157 // Returns true if an error was raised due to the declaration of
158 // this struct.
Jamie Madill075edd82013-07-08 13:30:19 -0400159 bool enterStructDeclaration(const TSourceLoc& line, const TString& identifier);
kbr@chromium.org476541f2011-10-27 21:14:51 +0000160 void exitStructDeclaration();
161
Jamie Madill98493dd2013-07-08 14:39:03 -0400162 bool structNestingErrorCheck(const TSourceLoc& line, const TField& field);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000163};
164
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000165int PaParseStrings(size_t count, const char* const string[], const int length[],
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000166 TParseContext* context);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000167
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000168#endif // _PARSER_HELPER_INCLUDED_