forked from qt-creator/qt-creator
Build AST nodes from within the parser.
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -111,11 +111,19 @@ void FunctionCallExpression::accept0(Visitor *visitor)
|
||||
{
|
||||
if (visitor->visit(this)) {
|
||||
accept(expr, visitor);
|
||||
accept(id, visitor);
|
||||
accept(arguments, visitor);
|
||||
}
|
||||
visitor->endVisit(this);
|
||||
}
|
||||
|
||||
void FunctionIdentifier::accept0(Visitor *visitor)
|
||||
{
|
||||
if (visitor->visit(this))
|
||||
accept(type, visitor);
|
||||
visitor->endVisit(this);
|
||||
}
|
||||
|
||||
void ExpressionStatement::accept0(Visitor *visitor)
|
||||
{
|
||||
if (visitor->visit(this))
|
||||
@@ -198,8 +206,8 @@ void CaseLabelStatement::accept0(Visitor *visitor)
|
||||
visitor->endVisit(this);
|
||||
}
|
||||
|
||||
BasicType::BasicType(int _token)
|
||||
: Type(Kind_BasicType), token(_token)
|
||||
BasicType::BasicType(int _token, const char *_name, Category _category)
|
||||
: Type(Kind_BasicType), token(_token), name(_name), categ(_category)
|
||||
{
|
||||
switch (token) {
|
||||
case GLSLParserTable::T_VOID:
|
||||
@@ -314,14 +322,22 @@ void StructType::Field::setInnerType(Type *innerType)
|
||||
*parent = innerType;
|
||||
}
|
||||
|
||||
void StructType::fixInnerTypes(Type *innerType, List<Field *> *fields)
|
||||
List<StructType::Field *> *StructType::fixInnerTypes(Type *innerType, List<Field *> *fields)
|
||||
{
|
||||
if (!fields)
|
||||
return;
|
||||
return fields;
|
||||
List<Field *> *head = fields->next;
|
||||
List<Field *> *current = head;
|
||||
do {
|
||||
current->value->setInnerType(innerType);
|
||||
current = current->next;
|
||||
} while (current && current != head);
|
||||
return fields;
|
||||
}
|
||||
|
||||
void PrecisionDeclaration::accept0(Visitor *visitor)
|
||||
{
|
||||
if (visitor->visit(this))
|
||||
accept(type, visitor);
|
||||
visitor->endVisit(this);
|
||||
}
|
||||
|
||||
@@ -37,7 +37,6 @@ namespace GLSL {
|
||||
|
||||
class AST;
|
||||
class TranslationUnit;
|
||||
class Declaration;
|
||||
class Expression;
|
||||
class IdentifierExpression;
|
||||
class LiteralExpression;
|
||||
@@ -47,6 +46,7 @@ class TernaryExpression;
|
||||
class AssignmentExpression;
|
||||
class MemberAccessExpression;
|
||||
class FunctionCallExpression;
|
||||
class FunctionIdentifier;
|
||||
class Statement;
|
||||
class ExpressionStatement;
|
||||
class CompoundStatement;
|
||||
@@ -63,6 +63,8 @@ class BasicType;
|
||||
class NamedType;
|
||||
class ArrayType;
|
||||
class StructType;
|
||||
class Declaration;
|
||||
class PrecisionDeclaration;
|
||||
class Visitor;
|
||||
|
||||
template <typename T>
|
||||
@@ -145,6 +147,7 @@ public:
|
||||
Kind_GreaterEqual,
|
||||
Kind_LogicalAnd,
|
||||
Kind_LogicalOr,
|
||||
Kind_LogicalXor,
|
||||
Kind_BitwiseAnd,
|
||||
Kind_BitwiseOr,
|
||||
Kind_BitwiseXor,
|
||||
@@ -156,6 +159,7 @@ public:
|
||||
Kind_MemberAccess,
|
||||
Kind_FunctionCall,
|
||||
Kind_MemberFunctionCall,
|
||||
Kind_FunctionIdentifier,
|
||||
|
||||
// Assignment expressions
|
||||
Kind_Assign,
|
||||
@@ -186,8 +190,6 @@ public:
|
||||
Kind_CaseLabel,
|
||||
Kind_DefaultLabel,
|
||||
|
||||
// Declarations
|
||||
|
||||
// Types
|
||||
Kind_BasicType,
|
||||
Kind_NamedType,
|
||||
@@ -195,13 +197,14 @@ public:
|
||||
Kind_OpenArrayType,
|
||||
Kind_StructType,
|
||||
Kind_AnonymousStructType,
|
||||
Kind_StructField
|
||||
Kind_StructField,
|
||||
|
||||
// Declarations
|
||||
Kind_PrecisionDeclaration
|
||||
};
|
||||
|
||||
virtual TranslationUnit *asTranslationUnit() { return 0; }
|
||||
|
||||
virtual Declaration *asDeclaration() { return 0; }
|
||||
|
||||
virtual Expression *asExpression() { return 0; }
|
||||
virtual IdentifierExpression *asIdentifierExpression() { return 0; }
|
||||
virtual LiteralExpression *asLiteralExpression() { return 0; }
|
||||
@@ -211,6 +214,7 @@ public:
|
||||
virtual AssignmentExpression *asAssignmentExpression() { return 0; }
|
||||
virtual MemberAccessExpression *asMemberAccessExpression() { return 0; }
|
||||
virtual FunctionCallExpression *asFunctionCallExpression() { return 0; }
|
||||
virtual FunctionIdentifier *asFunctionIdentifier() { return 0; }
|
||||
|
||||
virtual Statement *asStatement() { return 0; }
|
||||
virtual ExpressionStatement *asExpressionStatement() { return 0; }
|
||||
@@ -230,6 +234,9 @@ public:
|
||||
virtual ArrayType *asArrayType() { return 0; }
|
||||
virtual StructType *asStructType() { return 0; }
|
||||
|
||||
virtual Declaration *asDeclaration() { return 0; }
|
||||
virtual PrecisionDeclaration *asPrecisionDeclaration() { return 0; }
|
||||
|
||||
void accept(Visitor *visitor);
|
||||
static void accept(AST *ast, Visitor *visitor);
|
||||
|
||||
@@ -275,15 +282,6 @@ public: // attributes
|
||||
List<Declaration *> *declarations;
|
||||
};
|
||||
|
||||
class GLSL_EXPORT Declaration: public AST
|
||||
{
|
||||
protected:
|
||||
Declaration(Kind _kind) : AST(_kind) {}
|
||||
|
||||
public:
|
||||
virtual Declaration *asDeclaration() { return this; }
|
||||
};
|
||||
|
||||
class GLSL_EXPORT Expression: public AST
|
||||
{
|
||||
protected:
|
||||
@@ -399,13 +397,13 @@ public: // attributes
|
||||
class GLSL_EXPORT FunctionCallExpression: public Expression
|
||||
{
|
||||
public:
|
||||
FunctionCallExpression(const std::string *_name,
|
||||
FunctionCallExpression(FunctionIdentifier *_id,
|
||||
List<Expression *> *_arguments)
|
||||
: Expression(Kind_FunctionCall), expr(0), name(_name)
|
||||
: Expression(Kind_FunctionCall), expr(0), id(_id)
|
||||
, arguments(finish(_arguments)) {}
|
||||
FunctionCallExpression(Expression *_expr, const std::string *_name,
|
||||
FunctionCallExpression(Expression *_expr, FunctionIdentifier *_id,
|
||||
List<Expression *> *_arguments)
|
||||
: Expression(Kind_MemberFunctionCall), expr(_expr), name(_name)
|
||||
: Expression(Kind_MemberFunctionCall), expr(_expr), id(_id)
|
||||
, arguments(finish(_arguments)) {}
|
||||
|
||||
virtual FunctionCallExpression *asFunctionCallExpression() { return this; }
|
||||
@@ -414,10 +412,27 @@ public:
|
||||
|
||||
public: // attributes
|
||||
Expression *expr;
|
||||
const std::string *name;
|
||||
FunctionIdentifier *id;
|
||||
List<Expression *> *arguments;
|
||||
};
|
||||
|
||||
class GLSL_EXPORT FunctionIdentifier: public AST
|
||||
{
|
||||
public:
|
||||
FunctionIdentifier(const std::string *_name)
|
||||
: AST(Kind_FunctionIdentifier), name(_name), type(0) {}
|
||||
FunctionIdentifier(Type *_type)
|
||||
: AST(Kind_FunctionIdentifier), name(0), type(_type) {}
|
||||
|
||||
virtual FunctionIdentifier *asFunctionIdentifier() { return this; }
|
||||
|
||||
virtual void accept0(Visitor *visitor);
|
||||
|
||||
public: // attributes
|
||||
const std::string *name;
|
||||
Type *type;
|
||||
};
|
||||
|
||||
class GLSL_EXPORT Statement: public AST
|
||||
{
|
||||
protected:
|
||||
@@ -444,6 +459,8 @@ public: // attributes
|
||||
class GLSL_EXPORT CompoundStatement: public Statement
|
||||
{
|
||||
public:
|
||||
CompoundStatement()
|
||||
: Statement(Kind_CompoundStatement), statements(0) {}
|
||||
CompoundStatement(List<Statement *> *_statements)
|
||||
: Statement(Kind_CompoundStatement), statements(finish(_statements)) {}
|
||||
|
||||
@@ -582,29 +599,59 @@ protected:
|
||||
public:
|
||||
enum Precision
|
||||
{
|
||||
PrecNotValid, // Precision not valid (e.g. structs and samplers).
|
||||
PrecNotValid, // Precision not valid (e.g. structs).
|
||||
PrecUnspecified, // Precision not known, but can be validly set.
|
||||
Lowp,
|
||||
Mediump,
|
||||
Highp
|
||||
};
|
||||
|
||||
enum Category
|
||||
{
|
||||
Void,
|
||||
Primitive,
|
||||
Vector2,
|
||||
Vector3,
|
||||
Vector4,
|
||||
Matrix,
|
||||
Sampler1D,
|
||||
Sampler2D,
|
||||
Sampler3D,
|
||||
SamplerCube,
|
||||
Sampler1DShadow,
|
||||
Sampler2DShadow,
|
||||
SamplerCubeShadow,
|
||||
Sampler1DArray,
|
||||
Sampler2DArray,
|
||||
SamplerCubeArray,
|
||||
Sampler1DArrayShadow,
|
||||
Sampler2DArrayShadow,
|
||||
SamplerCubeArrayShadow,
|
||||
Sampler2DRect,
|
||||
Sampler2DRectShadow,
|
||||
Sampler2DMS,
|
||||
Sampler2DMSArray,
|
||||
SamplerBuffer,
|
||||
Array,
|
||||
Struct
|
||||
};
|
||||
|
||||
virtual Type *asType() { return this; }
|
||||
|
||||
virtual Precision precision() const = 0;
|
||||
|
||||
// Set the precision for the innermost basic type. Returns false if it
|
||||
// is not valid to set a precision (e.g. structs, samplers, etc).
|
||||
// is not valid to set a precision (e.g. structs).
|
||||
virtual bool setPrecision(Precision precision) = 0;
|
||||
|
||||
virtual Category category() const = 0;
|
||||
};
|
||||
|
||||
class GLSL_EXPORT BasicType: public Type
|
||||
{
|
||||
public:
|
||||
// Pass the parser's token code: T_VOID, T_VEC4, etc.
|
||||
BasicType(int _token);
|
||||
BasicType(int _token, Precision _prec)
|
||||
: Type(Kind_BasicType), prec(_prec), token(_token) {}
|
||||
BasicType(int _token, const char *_name, Category _category);
|
||||
|
||||
virtual BasicType *asBasicType() { return this; }
|
||||
|
||||
@@ -613,9 +660,13 @@ public:
|
||||
virtual Precision precision() const;
|
||||
virtual bool setPrecision(Precision precision);
|
||||
|
||||
virtual Category category() const { return categ; }
|
||||
|
||||
public: // attributes
|
||||
Precision prec;
|
||||
int token;
|
||||
const char *name;
|
||||
Category categ;
|
||||
};
|
||||
|
||||
class GLSL_EXPORT NamedType: public Type
|
||||
@@ -630,6 +681,8 @@ public:
|
||||
virtual Precision precision() const;
|
||||
virtual bool setPrecision(Precision precision);
|
||||
|
||||
virtual Category category() const { return Struct; }
|
||||
|
||||
public: // attributes
|
||||
const std::string *name;
|
||||
};
|
||||
@@ -649,6 +702,8 @@ public:
|
||||
virtual Precision precision() const;
|
||||
virtual bool setPrecision(Precision precision);
|
||||
|
||||
virtual Category category() const { return Array; }
|
||||
|
||||
public: // attributes
|
||||
Type *elementType;
|
||||
Expression *size;
|
||||
@@ -691,19 +746,40 @@ public:
|
||||
|
||||
// Fix the inner types of a field list. The "innerType" will
|
||||
// be copied into the "array holes" of all fields.
|
||||
static void fixInnerTypes(Type *innerType, List<Field *> *fields);
|
||||
static List<Field *> *fixInnerTypes(Type *innerType, List<Field *> *fields);
|
||||
|
||||
// Add a new group of fields after having their inner types fixed.
|
||||
void addFields(List<Field *> *list)
|
||||
{
|
||||
fields = appendLists(fields, list);
|
||||
}
|
||||
virtual Category category() const { return Struct; }
|
||||
|
||||
public: // attributes
|
||||
const std::string *name;
|
||||
List<Field *> *fields;
|
||||
};
|
||||
|
||||
class GLSL_EXPORT Declaration: public AST
|
||||
{
|
||||
protected:
|
||||
Declaration(Kind _kind) : AST(_kind) {}
|
||||
|
||||
public:
|
||||
virtual Declaration *asDeclaration() { return this; }
|
||||
};
|
||||
|
||||
class GLSL_EXPORT PrecisionDeclaration: public Declaration
|
||||
{
|
||||
public:
|
||||
PrecisionDeclaration(Type::Precision _precision, Type *_type)
|
||||
: Declaration(Kind_PrecisionDeclaration)
|
||||
, precision(_precision), type(_type) {}
|
||||
|
||||
virtual PrecisionDeclaration *asPrecisionDeclaration() { return this; }
|
||||
|
||||
virtual void accept0(Visitor *visitor);
|
||||
|
||||
public: // attributes
|
||||
Type::Precision precision;
|
||||
Type *type;
|
||||
};
|
||||
|
||||
} // namespace GLSL
|
||||
|
||||
#endif // GLSLAST_H
|
||||
|
||||
@@ -69,6 +69,9 @@ public:
|
||||
virtual bool visit(FunctionCallExpression *) { return true; }
|
||||
virtual void endVisit(FunctionCallExpression *) {}
|
||||
|
||||
virtual bool visit(FunctionIdentifier *) { return true; }
|
||||
virtual void endVisit(FunctionIdentifier *) {}
|
||||
|
||||
virtual bool visit(ExpressionStatement *) { return true; }
|
||||
virtual void endVisit(ExpressionStatement *) {}
|
||||
|
||||
@@ -113,6 +116,9 @@ public:
|
||||
|
||||
virtual bool visit(StructType::Field *) { return true; }
|
||||
virtual void endVisit(StructType::Field *) {}
|
||||
|
||||
virtual bool visit(PrecisionDeclaration *) { return true; }
|
||||
virtual void endVisit(PrecisionDeclaration *) {}
|
||||
};
|
||||
|
||||
} // namespace GLSL
|
||||
|
||||
@@ -372,9 +372,12 @@ int Lexer::yylex_helper(const char **position, int *line)
|
||||
_yyval.string = _engine->identifier(word, _it - word - 1);
|
||||
return Parser::T_IDENTIFIER;
|
||||
} else if (std::isdigit(ch)) {
|
||||
const char *word = _it - 2;
|
||||
while (std::isalnum(_yychar) || _yychar == '.') {
|
||||
yyinp();
|
||||
}
|
||||
if (_engine)
|
||||
_yyval.string = _engine->identifier(word, _it - word - 1);
|
||||
return Parser::T_NUMBER;
|
||||
}
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -51,7 +51,27 @@ public:
|
||||
List<Declaration *> *declaration_list;
|
||||
Expression *expression;
|
||||
List<Expression *> *expression_list;
|
||||
Statement *statement;
|
||||
List<Statement *> *statement_list;
|
||||
Type *type;
|
||||
StructType::Field *field;
|
||||
List<StructType::Field *> *field_list;
|
||||
TranslationUnit *translation_unit;
|
||||
FunctionIdentifier *function_identifier;
|
||||
AST::Kind kind;
|
||||
Type::Precision precision;
|
||||
struct {
|
||||
Statement *thenClause;
|
||||
Statement *elseClause;
|
||||
} ifstmt;
|
||||
struct {
|
||||
Expression *condition;
|
||||
Expression *increment;
|
||||
} forstmt;
|
||||
struct {
|
||||
FunctionIdentifier *id;
|
||||
List<Expression *> *arguments;
|
||||
} function;
|
||||
// ### ast nodes...
|
||||
};
|
||||
|
||||
@@ -65,6 +85,9 @@ private:
|
||||
Value &sym(int n) { return _symStack[_tos + n - 1]; }
|
||||
AST *&ast(int n) { return _symStack[_tos + n - 1].ast; }
|
||||
const std::string *&string(int n) { return _symStack[_tos + n - 1].string; }
|
||||
Expression *&expression(int n) { return _symStack[_tos + n - 1].expression; }
|
||||
Statement *&statement(int n) { return _symStack[_tos + n - 1].statement; }
|
||||
Type *&type(int n) { return _symStack[_tos + n - 1].type; }
|
||||
|
||||
inline int consumeToken() { return _index++; }
|
||||
inline const Token &tokenAt(int index) const { return _tokens.at(index); }
|
||||
@@ -111,6 +134,13 @@ private:
|
||||
return node;
|
||||
}
|
||||
|
||||
Type *makeBasicType(int token, BasicType::Category category)
|
||||
{
|
||||
Type *type = new (_engine->pool()) BasicType(token, spell[token], category);
|
||||
type->lineno = yyloc >= 0 ? (_tokens[yyloc].line + 1) : 0;
|
||||
return type;
|
||||
}
|
||||
|
||||
private:
|
||||
Engine *_engine;
|
||||
int _tos;
|
||||
|
||||
Reference in New Issue
Block a user