diff --git a/src/libs/glsl/glsl.g b/src/libs/glsl/glsl.g index a1ea60544a3..d851c25037c 100644 --- a/src/libs/glsl/glsl.g +++ b/src/libs/glsl/glsl.g @@ -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; ./ diff --git a/src/libs/glsl/glslast.cpp b/src/libs/glsl/glslast.cpp index e9784010529..b6ab81930a6 100644 --- a/src/libs/glsl/glslast.cpp +++ b/src/libs/glsl/glslast.cpp @@ -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::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::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::iterator it = statements.begin(); it != statements.end(); ++it) - delete *it; -} - void CompoundStatement::accept0(Visitor *visitor) { - if (visitor->visit(this)) { - for (std::vector::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::iterator it = fields.begin(); it != fields.end(); ++it) - delete *it; -} - void StructType::accept0(Visitor *visitor) { - if (visitor->visit(this)) { - for (std::vector::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::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); + *parent = innerType; } -void StructType::fixInnerTypes(Type *innerType, std::vector &fields) +void StructType::fixInnerTypes(Type *innerType, List *fields) { - for (size_t index = 0; index < fields.size(); ++index) - fields[index]->setInnerType(innerType); - delete innerType; // No longer needed - cloned into all fields. -} - -void StructType::addFields(const std::vector &list) -{ - for (size_t index = 0; index < list.size(); ++index) - fields.push_back(list[index]); + if (!fields) + return; + List *head = fields->next; + List *current = head; + do { + current->value->setInnerType(innerType); + current = current->next; + } while (current && current != head); } diff --git a/src/libs/glsl/glslast.h b/src/libs/glsl/glslast.h index 836ee77b0f6..7be3cae9a61 100644 --- a/src/libs/glsl/glslast.h +++ b/src/libs/glsl/glslast.h @@ -31,7 +31,6 @@ #include "glsl.h" #include "glslmemorypool.h" -#include #include 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 +List *appendLists(List *first, List *second) +{ + if (!first) + return second; + else if (!second) + return first; + List *firstHead = first->next; + List *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 static List *finish(List *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 *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 *_arguments) + : Expression(Kind_FunctionCall), expr(0), name(_name) + , arguments(finish(_arguments)) {} + FunctionCallExpression(Expression *_expr, const std::string *_name, + List *_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 arguments; + List *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 *_statements) + : Statement(Kind_CompoundStatement), statements(finish(_statements)) {} virtual CompoundStatement *asCompoundStatement() { return this; } virtual void accept0(Visitor *visitor); public: // attributes - std::vector statements; + List *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(); virtual IfStatement *asIfStatement() { return this; } @@ -476,7 +476,6 @@ class GLSL_EXPORT WhileStatement: public Statement public: WhileStatement(Expression *_condition, Statement *_body) : Statement(Kind_While), condition(_condition), body(_body) {} - ~WhileStatement(); virtual WhileStatement *asWhileStatement() { return this; } @@ -492,7 +491,6 @@ class GLSL_EXPORT DoStatement: public Statement public: DoStatement(Statement *_body, Expression *_condition) : Statement(Kind_Do), body(_body), condition(_condition) {} - ~DoStatement(); virtual DoStatement *asDoStatement() { return this; } @@ -508,7 +506,6 @@ class GLSL_EXPORT ForStatement: public Statement public: ForStatement(Statement *_init, Expression *_condition, Expression *_increment, Statement *_body) : Statement(Kind_For), init(_init), condition(_condition), increment(_increment), body(_body) {} - ~ForStatement(); virtual ForStatement *asForStatement() { return this; } @@ -525,7 +522,6 @@ class GLSL_EXPORT JumpStatement: public Statement { public: JumpStatement(Kind _kind) : Statement(_kind) {} - ~JumpStatement(); virtual JumpStatement *asJumpStatement() { return this; } @@ -538,7 +534,6 @@ public: ReturnStatement() : Statement(Kind_Return), expr(0) {} ReturnStatement(Expression *_expr) : Statement(Kind_ReturnExpression), expr(_expr) {} - ~ReturnStatement(); virtual ReturnStatement *asReturnStatement() { return this; } @@ -553,7 +548,6 @@ class GLSL_EXPORT SwitchStatement: public Statement public: SwitchStatement(Expression *_expr, Statement *_body) : Statement(Kind_Switch), expr(_expr), body(_body) {} - ~SwitchStatement(); virtual SwitchStatement *asSwitchStatement() { return this; } @@ -570,7 +564,6 @@ public: CaseLabelStatement() : Statement(Kind_DefaultLabel), expr(0) {} CaseLabelStatement(Expression *_expr) : Statement(Kind_CaseLabel), expr(_expr) {} - ~CaseLabelStatement(); virtual CaseLabelStatement *asCaseLabelStatement() { return this; } @@ -602,9 +595,6 @@ public: // Set the precision for the innermost basic type. Returns false if it // is not valid to set a precision (e.g. structs, samplers, etc). virtual bool setPrecision(Precision precision) = 0; - - virtual Type *clone() const = 0; - static Type *clone(Type *type); }; class GLSL_EXPORT BasicType: public Type @@ -614,7 +604,6 @@ public: BasicType(int _token); BasicType(int _token, Precision _prec) : Type(Kind_BasicType), prec(_prec), token(_token) {} - ~BasicType(); virtual BasicType *asBasicType() { return this; } @@ -623,8 +612,6 @@ public: virtual Precision precision() const; virtual bool setPrecision(Precision precision); - virtual Type *clone() const; - public: // attributes Precision prec; int token; @@ -634,7 +621,6 @@ class GLSL_EXPORT NamedType: public Type { public: NamedType(const std::string *_name) : Type(Kind_NamedType), name(_name) {} - ~NamedType(); virtual NamedType *asNamedType() { return this; } @@ -643,8 +629,6 @@ public: virtual Precision precision() const; virtual bool setPrecision(Precision precision); - virtual Type *clone() const; - public: // attributes const std::string *name; }; @@ -656,7 +640,6 @@ public: : Type(Kind_OpenArrayType), elementType(_elementType), size(0) {} ArrayType(Type *_elementType, Expression *_size) : Type(Kind_ArrayType), elementType(_elementType), size(_size) {} - ~ArrayType(); virtual ArrayType *asArrayType() { return this; } @@ -665,8 +648,6 @@ public: virtual Precision precision() const; virtual bool setPrecision(Precision precision); - virtual Type *clone() const; - public: // attributes Type *elementType; Expression *size; @@ -675,20 +656,6 @@ public: // attributes class GLSL_EXPORT StructType: public Type { public: - StructType() : Type(Kind_AnonymousStructType) {} - StructType(const std::string *_name) - : Type(Kind_StructType), name(_name) {} - ~StructType(); - - virtual StructType *asStructType() { return this; } - - virtual void accept0(Visitor *visitor); - - virtual Precision precision() const; - virtual bool setPrecision(Precision precision); - - virtual Type *clone() const; - class Field: public AST { public: @@ -701,8 +668,6 @@ public: Field(const std::string *_name, Type *_type) : AST(Kind_StructField), name(_name), type(_type) {} - ~Field(); - virtual void accept0(Visitor *visitor); void setInnerType(Type *innerType); @@ -711,15 +676,31 @@ public: Type *type; }; - // Fix the inner types of a field list. The "innerType" will - // be cloned into all the fields and then deleted. - static void fixInnerTypes(Type *innerType, std::vector &fields); + StructType(List *_fields) + : Type(Kind_AnonymousStructType), fields(finish(_fields)) {} + StructType(const std::string *_name, List *_fields) + : Type(Kind_StructType), name(_name), fields(finish(_fields)) {} - void addFields(const std::vector &list); + virtual StructType *asStructType() { return this; } + + virtual void accept0(Visitor *visitor); + + virtual Precision precision() const; + virtual bool setPrecision(Precision precision); + + // 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 *fields); + + // Add a new group of fields after having their inner types fixed. + void addFields(List *list) + { + fields = appendLists(fields, list); + } public: // attributes const std::string *name; - std::vector fields; + List *fields; }; } // namespace GLSL diff --git a/src/libs/glsl/glslparser.cpp b/src/libs/glsl/glslparser.cpp index e1e1e1a0dc7..6f128b43a63 100644 --- a/src/libs/glsl/glslparser.cpp +++ b/src/libs/glsl/glslparser.cpp @@ -168,7 +168,7 @@ switch(ruleno) { #line 466 "./glsl.g" case 0: { - ast(1) = new IdentifierExpression(sym(1).string); + ast(1) = new (_engine->pool()) IdentifierExpression(sym(1).string); } break; #line 473 "./glsl.g" @@ -372,7 +372,7 @@ case 33: { #line 704 "./glsl.g" case 34: { - 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; #line 711 "./glsl.g" @@ -1998,7 +1998,7 @@ case 304: { #line 2601 "./glsl.g" case 305: { - ast(1) = new TranslationUnit(sym(1).declaration_list); + ast(1) = new (_engine->pool()) TranslationUnit(sym(1).declaration_list); } break; #line 2608 "./glsl.g"