Commit 73d570c8 authored by Rhys Weatherley's avatar Rhys Weatherley
Browse files

Convert GLSL AST nodes into managed types

parent 217699c8
......@@ -465,7 +465,7 @@ switch(ruleno) {
variable_identifier ::= IDENTIFIER ;
/.
case $rule_number: {
ast(1) = new IdentifierExpression(sym(1).string);
ast(1) = new (_engine->pool()) IdentifierExpression(sym(1).string);
} break;
./
......@@ -703,7 +703,7 @@ case $rule_number: {
multiplicative_expression ::= multiplicative_expression STAR unary_expression ;
/.
case $rule_number: {
ast(1) = new BinaryExpression(AST::Kind_Multiply, sym(1).expression, sym(3).expression);
ast(1) = new (_engine->pool()) BinaryExpression(AST::Kind_Multiply, sym(1).expression, sym(3).expression);
} break;
./
......@@ -2600,7 +2600,7 @@ case $rule_number: {
translation_unit ::= external_declaration_list ;
/.
case $rule_number: {
ast(1) = new TranslationUnit(sym(1).declaration_list);
ast(1) = new (_engine->pool()) TranslationUnit(sym(1).declaration_list);
} break;
./
......
......@@ -32,10 +32,6 @@
using namespace GLSL;
AST::~AST()
{
}
void AST::accept(Visitor *visitor)
{
if (visitor->preVisit(this))
......@@ -49,23 +45,6 @@ void AST::accept(AST *ast, Visitor *visitor)
ast->accept(visitor);
}
Statement *AST::makeCompound(Statement *left, Statement *right)
{
if (!left)
return right;
else if (!right)
return left;
CompoundStatement *compound = left->asCompoundStatement();
if (compound) {
compound->statements.push_back(right);
} else {
compound = new CompoundStatement();
compound->statements.push_back(left);
compound->statements.push_back(right);
}
return compound;
}
void TranslationUnit::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
......@@ -74,32 +53,18 @@ void TranslationUnit::accept0(Visitor *visitor)
visitor->endVisit(this);
}
IdentifierExpression::~IdentifierExpression()
{
}
void IdentifierExpression::accept0(Visitor *visitor)
{
visitor->visit(this);
visitor->endVisit(this);
}
LiteralExpression::~LiteralExpression()
{
}
void LiteralExpression::accept0(Visitor *visitor)
{
visitor->visit(this);
visitor->endVisit(this);
}
BinaryExpression::~BinaryExpression()
{
delete left;
delete right;
}
void BinaryExpression::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
......@@ -109,11 +74,6 @@ void BinaryExpression::accept0(Visitor *visitor)
visitor->endVisit(this);
}
UnaryExpression::~UnaryExpression()
{
delete expr;
}
void UnaryExpression::accept0(Visitor *visitor)
{
if (visitor->visit(this))
......@@ -121,13 +81,6 @@ void UnaryExpression::accept0(Visitor *visitor)
visitor->endVisit(this);
}
TernaryExpression::~TernaryExpression()
{
delete first;
delete second;
delete third;
}
void TernaryExpression::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
......@@ -138,12 +91,6 @@ void TernaryExpression::accept0(Visitor *visitor)
visitor->endVisit(this);
}
AssignmentExpression::~AssignmentExpression()
{
delete variable;
delete value;
}
void AssignmentExpression::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
......@@ -153,11 +100,6 @@ void AssignmentExpression::accept0(Visitor *visitor)
visitor->endVisit(this);
}
MemberAccessExpression::~MemberAccessExpression()
{
delete expr;
}
void MemberAccessExpression::accept0(Visitor *visitor)
{
if (visitor->visit(this))
......@@ -165,28 +107,15 @@ void MemberAccessExpression::accept0(Visitor *visitor)
visitor->endVisit(this);
}
FunctionCallExpression::~FunctionCallExpression()
{
delete expr;
for (std::vector<Expression *>::iterator it = arguments.begin(); it != arguments.end(); ++it)
delete *it;
}
void FunctionCallExpression::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
accept(expr, visitor);
for (std::vector<Expression *>::iterator it = arguments.begin(); it != arguments.end(); ++it)
accept(*it, visitor);
accept(arguments, visitor);
}
visitor->endVisit(this);
}
ExpressionStatement::~ExpressionStatement()
{
delete expr;
}
void ExpressionStatement::accept0(Visitor *visitor)
{
if (visitor->visit(this))
......@@ -194,28 +123,13 @@ void ExpressionStatement::accept0(Visitor *visitor)
visitor->endVisit(this);
}
CompoundStatement::~CompoundStatement()
{
for (std::vector<Statement *>::iterator it = statements.begin(); it != statements.end(); ++it)
delete *it;
}
void CompoundStatement::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
for (std::vector<Statement *>::iterator it = statements.begin(); it != statements.end(); ++it)
accept(*it, visitor);
}
if (visitor->visit(this))
accept(statements, visitor);
visitor->endVisit(this);
}
IfStatement::~IfStatement()
{
delete condition;
delete thenClause;
delete elseClause;
}
void IfStatement::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
......@@ -226,12 +140,6 @@ void IfStatement::accept0(Visitor *visitor)
visitor->endVisit(this);
}
WhileStatement::~WhileStatement()
{
delete condition;
delete body;
}
void WhileStatement::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
......@@ -241,12 +149,6 @@ void WhileStatement::accept0(Visitor *visitor)
visitor->endVisit(this);
}
DoStatement::~DoStatement()
{
delete body;
delete condition;
}
void DoStatement::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
......@@ -256,14 +158,6 @@ void DoStatement::accept0(Visitor *visitor)
visitor->endVisit(this);
}
ForStatement::~ForStatement()
{
delete init;
delete condition;
delete increment;
delete body;
}
void ForStatement::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
......@@ -275,21 +169,12 @@ void ForStatement::accept0(Visitor *visitor)
visitor->endVisit(this);
}
JumpStatement::~JumpStatement()
{
}
void JumpStatement::accept0(Visitor *visitor)
{
visitor->visit(this);
visitor->endVisit(this);
}
ReturnStatement::~ReturnStatement()
{
delete expr;
}
void ReturnStatement::accept0(Visitor *visitor)
{
if (visitor->visit(this))
......@@ -297,12 +182,6 @@ void ReturnStatement::accept0(Visitor *visitor)
visitor->endVisit(this);
}
SwitchStatement::~SwitchStatement()
{
delete expr;
delete body;
}
void SwitchStatement::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
......@@ -312,11 +191,6 @@ void SwitchStatement::accept0(Visitor *visitor)
visitor->endVisit(this);
}
CaseLabelStatement::~CaseLabelStatement()
{
delete expr;
}
void CaseLabelStatement::accept0(Visitor *visitor)
{
if (visitor->visit(this))
......@@ -324,15 +198,6 @@ void CaseLabelStatement::accept0(Visitor *visitor)
visitor->endVisit(this);
}
Type *Type::clone(Type *type)
{
if (!type)
return 0;
Type *c = type->clone();
c->lineno = type->lineno;
return c;
}
BasicType::BasicType(int _token)
: Type(Kind_BasicType), token(_token)
{
......@@ -350,10 +215,6 @@ BasicType::BasicType(int _token)
}
}
BasicType::~BasicType()
{
}
void BasicType::accept0(Visitor *visitor)
{
visitor->visit(this);
......@@ -373,15 +234,6 @@ bool BasicType::setPrecision(Precision precision)
return true;
}
Type *BasicType::clone() const
{
return new BasicType(token, prec);
}
NamedType::~NamedType()
{
}
void NamedType::accept0(Visitor *visitor)
{
visitor->visit(this);
......@@ -399,17 +251,6 @@ bool NamedType::setPrecision(Precision)
return false;
}
Type *NamedType::clone() const
{
return new NamedType(name);
}
ArrayType::~ArrayType()
{
delete elementType;
delete size;
}
void ArrayType::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
......@@ -432,26 +273,10 @@ bool ArrayType::setPrecision(Precision precision)
return false;
}
Type *ArrayType::clone() const
{
if (kind == Kind_ArrayType)
return new ArrayType(Type::clone(elementType), size);
else
return new ArrayType(Type::clone(elementType));
}
StructType::~StructType()
{
for (std::vector<Field *>::iterator it = fields.begin(); it != fields.end(); ++it)
delete *it;
}
void StructType::accept0(Visitor *visitor)
{
if (visitor->visit(this)) {
for (std::vector<Field *>::iterator it = fields.begin(); it != fields.end(); ++it)
accept(*it, visitor);
}
if (visitor->visit(this))
accept(fields, visitor);
visitor->endVisit(this);
}
......@@ -466,25 +291,6 @@ bool StructType::setPrecision(Precision)
return false;
}
Type *StructType::clone() const
{
StructType *stype;
if (kind == Kind_AnonymousStructType)
stype = new StructType();
else
stype = new StructType(name);
for (std::vector<Field *>::const_iterator it = fields.begin(); it != fields.end(); ++it) {
stype->fields.push_back
(new Field((*it)->name, Type::clone((*it)->type)));
}
return stype;
}
StructType::Field::~Field()
{
delete type;
}
void StructType::Field::accept0(Visitor *visitor)
{
if (visitor->visit(this))
......@@ -505,18 +311,17 @@ void StructType::Field::setInnerType(Type *innerType)
parent = &(array->elementType);
inner = array->elementType;
}
*parent = Type::clone(innerType);
}
void StructType::fixInnerTypes(Type *innerType, std::vector<Field *> &fields)
{
for (size_t index = 0; index < fields.size(); ++index)
fields[index]->setInnerType(innerType);
delete innerType; // No longer needed - cloned into all fields.
*parent = innerType;
}
void StructType::addFields(const std::vector<Field *> &list)
void StructType::fixInnerTypes(Type *innerType, List<Field *> *fields)
{
for (size_t index = 0; index < list.size(); ++index)
fields.push_back(list[index]);
if (!fields)
return;
List<Field *> *head = fields->next;
List<Field *> *current = head;
do {
current->value->setInnerType(innerType);
current = current->next;
} while (current && current != head);
}
......@@ -31,7 +31,6 @@
#include "glsl.h"
#include "glslmemorypool.h"
#include <vector>
#include <string>
namespace GLSL {
......@@ -91,7 +90,22 @@ public:
List *next;
};
class GLSL_EXPORT AST // : public Managed
// Append two lists, which are assumed to still be circular, pre-finish.
template <typename T>
List<T> *appendLists(List<T> *first, List<T> *second)
{
if (!first)
return second;
else if (!second)
return first;
List<T> *firstHead = first->next;
List<T> *secondHead = second->next;
first->next = secondHead;
second->next = firstHead;
return second;
}
class GLSL_EXPORT AST: public Managed
{
public:
enum Kind {
......@@ -183,9 +197,6 @@ public:
Kind_StructField
};
AST() : kind(Kind_Undefined), lineno(0) {}
virtual ~AST();
virtual TranslationUnit *asTranslationUnit() { return 0; }
virtual Declaration *asDeclaration() { return 0; }
......@@ -230,14 +241,9 @@ public:
virtual void accept0(Visitor *visitor) = 0;
// Efficiently make a compound statement out of "left" and "right",
// removing left-recursion in the process.
static Statement *makeCompound(Statement *left, Statement *right);
protected:
AST(Kind _kind) : kind(_kind), lineno(0) {}
protected:
template <typename T>
static List<T> *finish(List<T> *list)
{
......@@ -249,14 +255,16 @@ protected:
public: // attributes
int kind;
int lineno;
protected:
~AST() {} // Managed types cannot be deleted.
};
class GLSL_EXPORT TranslationUnit: public AST
{
public:
TranslationUnit(List<Declaration *> *declarations)
: declarations(finish(declarations))
{ kind = Kind_TranslationUnit; }
: AST(Kind_TranslationUnit), declarations(finish(declarations)) {}
virtual TranslationUnit *asTranslationUnit() { return this; }
......@@ -269,7 +277,7 @@ public: // attributes
class GLSL_EXPORT Declaration: public AST
{
protected:
Declaration(Kind _kind) { kind = _kind; }
Declaration(Kind _kind) : AST(_kind) {}
public:
virtual Declaration *asDeclaration() { return this; }
......@@ -278,7 +286,7 @@ public:
class GLSL_EXPORT Expression: public AST
{
protected:
Expression(Kind _kind) { kind = _kind; }
Expression(Kind _kind) : AST(_kind) {}
public:
virtual Expression *asExpression() { return this; }
......@@ -289,7 +297,6 @@ class GLSL_EXPORT IdentifierExpression: public Expression
public:
IdentifierExpression(const std::string *_name)
: Expression(Kind_Identifier), name(_name) {}
~IdentifierExpression();
virtual IdentifierExpression *asIdentifierExpression() { return this; }
......@@ -304,7 +311,6 @@ class GLSL_EXPORT LiteralExpression: public Expression
public:
LiteralExpression(const std::string *_value)
: Expression(Kind_Literal), value(_value) {}
~LiteralExpression();
virtual LiteralExpression *asLiteralExpression() { return this; }
......@@ -319,7 +325,6 @@ class GLSL_EXPORT BinaryExpression: public Expression
public:
BinaryExpression(Kind _kind, Expression *_left, Expression *_right)
: Expression(_kind), left(_left), right(_right) {}
~BinaryExpression();
virtual BinaryExpression *asBinaryExpression() { return this; }
......@@ -335,7 +340,6 @@ class GLSL_EXPORT UnaryExpression: public Expression
public:
UnaryExpression(Kind _kind, Expression *_expr)
: Expression(_kind), expr(_expr) {}
~UnaryExpression();
virtual UnaryExpression *asUnaryExpression() { return this; }
......@@ -350,7 +354,6 @@ class GLSL_EXPORT TernaryExpression: public Expression
public:
TernaryExpression(Kind _kind, Expression *_first, Expression *_second, Expression *_third)
: Expression(_kind), first(_first), second(_second), third(_third) {}
~TernaryExpression();
virtual TernaryExpression *asTernaryExpression() { return this; }
......@@ -367,7 +370,6 @@ class GLSL_EXPORT AssignmentExpression: public Expression
public:
AssignmentExpression(Kind _kind, Expression *_variable, Expression *_value)
: Expression(_kind), variable(_variable), value(_value) {}
~AssignmentExpression();
virtual AssignmentExpression *asAssignmentExpression() { return this; }
......@@ -383,7 +385,6 @@ class GLSL_EXPORT MemberAccessExpression: public Expression
public:
MemberAccessExpression(Expression *_expr, const std::string *_field)
: Expression(Kind_MemberAccess), expr(_expr), field(_field) {}
~MemberAccessExpression();
virtual MemberAccessExpression *asMemberAccessExpression() { return this; }
......@@ -397,13 +398,14 @@ public: // attributes
class GLSL_EXPORT FunctionCallExpression: public Expression
{
public:
FunctionCallExpression(const std::string *_name)
: Expression(Kind_FunctionCall), expr(0), name(_name) {}
FunctionCallExpression(Expression *_expr, const std::string *_name)
: Expression(Kind_MemberFunctionCall), expr(_expr), name(_name) {}
~FunctionCallExpression();
void addArgument(Expression *expr) { arguments.push_back(expr); }
FunctionCallExpression(const std::string *_name,
List<Expression *> *_arguments)
: Expression(Kind_FunctionCall), expr(0), name(_name)
, arguments(finish(_arguments)) {}
FunctionCallExpression(Expression *_expr, const std::string *_name,
List<Expression *> *_arguments)
: Expression(Kind_MemberFunctionCall), expr(_expr), name(_name)
, arguments(finish(_arguments)) {}
virtual FunctionCallExpression *asFunctionCallExpression() { return this; }
......@@ -412,7 +414,7 @@ public:
public: // attributes
Expression *expr;
const std::string *name;
std::vector<Expression *> arguments;
List<Expression *> *arguments;
};
class GLSL_EXPORT Statement: public AST
......@@ -429,7 +431,6 @@ class GLSL_EXPORT ExpressionStatement: public Statement
public:
ExpressionStatement(Expression *_expr)
: Statement(Kind_ExpressionStatement), expr(_expr) {}
~ExpressionStatement();
virtual ExpressionStatement *asExpressionStatement() { return this; }
......@@ -442,15 +443,15 @@ public: // attributes
class GLSL_EXPORT CompoundStatement: public Statement
{
public:
CompoundStatement() : Statement(Kind_CompoundStatement) {}
~CompoundStatement();
CompoundStatement(List<Statement *> *_statements)
: Statement(Kind_CompoundStatement), statements(finish(_statements)) {}
virtual CompoundStatement *asCompoundStatement() { return this; }
virtual void accept0(Visitor *visitor);
public: // attributes
std::vector<Statement *> statements;
List<Statement *> *statements;
};
class GLSL_EXPORT IfStatement: public Statement
......@@ -459,7 +460,6 @@ public:
IfStatement(Expression *_condition, Statement *_thenClause, Statement *_elseClause)
: Statement(Kind_If), condition(_condition)
, thenClause(_thenClause), elseClause(_elseClause) {}
~IfStatement();