]> git.tdb.fi Git - libs/gl.git/blobdiff - source/glsl/syntax.cpp
Avoid copying raw pointers in the syntax tree
[libs/gl.git] / source / glsl / syntax.cpp
index 9705c989370d87b6672c7fc543a8fc9710c46502..957b49cea61a149fc213f9dbe71ecc9fa9774d1b 100644 (file)
@@ -1,4 +1,5 @@
 #include "syntax.h"
+#include "visitor.h"
 
 using namespace std;
 
@@ -6,6 +7,56 @@ namespace Msp {
 namespace GL {
 namespace SL {
 
+const Operator Operator::operators[] =
+{
+       { "[", 2, BINARY, LEFT_TO_RIGHT },
+       { "(", 2, BINARY, LEFT_TO_RIGHT },
+       { ".", 2, BINARY, LEFT_TO_RIGHT },
+       { "++", 2, POSTFIX, LEFT_TO_RIGHT },
+       { "--", 2, POSTFIX, LEFT_TO_RIGHT },
+       { "++", 3, PREFIX, RIGHT_TO_LEFT },
+       { "--", 3, PREFIX, RIGHT_TO_LEFT },
+       { "+", 3, PREFIX, RIGHT_TO_LEFT },
+       { "-", 3, PREFIX, RIGHT_TO_LEFT },
+       { "~", 3, PREFIX, RIGHT_TO_LEFT },
+       { "!", 3, PREFIX, RIGHT_TO_LEFT },
+       { "*", 4, BINARY, LEFT_TO_RIGHT },
+       { "/", 4, BINARY, LEFT_TO_RIGHT },
+       { "%", 4, BINARY, LEFT_TO_RIGHT },
+       { "+", 5, BINARY, LEFT_TO_RIGHT },
+       { "-", 5, BINARY, LEFT_TO_RIGHT },
+       { "<<", 6, BINARY, LEFT_TO_RIGHT },
+       { ">>", 6, BINARY, LEFT_TO_RIGHT },
+       { "<", 7, BINARY, LEFT_TO_RIGHT },
+       { ">", 7, BINARY, LEFT_TO_RIGHT },
+       { "<=", 7, BINARY, LEFT_TO_RIGHT },
+       { ">=", 7, BINARY, LEFT_TO_RIGHT },
+       { "==", 8, BINARY, LEFT_TO_RIGHT },
+       { "!=", 8, BINARY, LEFT_TO_RIGHT },
+       { "&", 9, BINARY, LEFT_TO_RIGHT },
+       { "^", 10, BINARY, LEFT_TO_RIGHT },
+       { "|", 11, BINARY, LEFT_TO_RIGHT },
+       { "&&", 12, BINARY, LEFT_TO_RIGHT },
+       { "^^", 13, BINARY, LEFT_TO_RIGHT },
+       { "||", 14, BINARY, LEFT_TO_RIGHT },
+       { "?", 15, BINARY, RIGHT_TO_LEFT },
+       { ":", 15, BINARY, RIGHT_TO_LEFT },
+       { "=", 16, BINARY, RIGHT_TO_LEFT },
+       { "+=", 16, BINARY, RIGHT_TO_LEFT },
+       { "-=", 16, BINARY, RIGHT_TO_LEFT },
+       { "*=", 16, BINARY, RIGHT_TO_LEFT },
+       { "/=", 16, BINARY, RIGHT_TO_LEFT },
+       { "%=", 16, BINARY, RIGHT_TO_LEFT },
+       { "<<=", 16, BINARY, RIGHT_TO_LEFT },
+       { ">>=", 16, BINARY, RIGHT_TO_LEFT },
+       { "&=", 16, BINARY, RIGHT_TO_LEFT },
+       { "^=", 16, BINARY, RIGHT_TO_LEFT },
+       { "|=", 16, BINARY, RIGHT_TO_LEFT },
+       { ",", 17, BINARY, LEFT_TO_RIGHT },
+       { { 0 }, 18, NO_OPERATOR, LEFT_TO_RIGHT }
+};
+
+
 template<typename C>
 NodeContainer<C>::NodeContainer(const NodeContainer &c):
        C(c)
@@ -47,12 +98,27 @@ VariableReference::VariableReference():
        declaration(0)
 { }
 
+VariableReference::VariableReference(const VariableReference &other):
+       name(other.name),
+       declaration(0)
+{ }
+
 void VariableReference::visit(NodeVisitor &visitor)
 {
        visitor.visit(*this);
 }
 
 
+MemberAccess::MemberAccess():
+       declaration(0)
+{ }
+
+MemberAccess::MemberAccess(const MemberAccess &other):
+       left(other.left),
+       member(other.member),
+       declaration(0)
+{ }
+
 void MemberAccess::visit(NodeVisitor &visitor)
 {
        visitor.visit(*this);
@@ -80,6 +146,11 @@ Assignment::Assignment():
        target_declaration(0)
 { }
 
+Assignment::Assignment(const Assignment &other):
+       self_referencing(other.self_referencing),
+       target_declaration(0)
+{ }
+
 void Assignment::visit(NodeVisitor &visitor)
 {
        visitor.visit(*this);
@@ -91,6 +162,13 @@ FunctionCall::FunctionCall():
        constructor(false)
 { }
 
+FunctionCall::FunctionCall(const FunctionCall &other):
+       name(other.name),
+       declaration(0),
+       constructor(other.constructor),
+       arguments(other.arguments)
+{ }
+
 void FunctionCall::visit(NodeVisitor &visitor)
 {
        visitor.visit(*this);
@@ -145,6 +223,22 @@ VariableDeclaration::VariableDeclaration():
        linked_declaration(0)
 { }
 
+VariableDeclaration::VariableDeclaration(const VariableDeclaration &other):
+       constant(other.constant),
+       sampling(other.sampling),
+       interpolation(other.interpolation),
+       interface(other.interface),
+       precision(other.precision),
+       type(other.type),
+       type_declaration(0),
+       name(other.name),
+       array(other.array),
+       array_size(other.array_size),
+       init_expression(other.init_expression),
+       linked_declaration(0),
+       layout(other.layout)
+{ }
+
 void VariableDeclaration::visit(NodeVisitor &visitor)
 {
        visitor.visit(*this);
@@ -171,7 +265,7 @@ FunctionDeclaration::FunctionDeclaration(const FunctionDeclaration &other):
        return_type(other.return_type),
        name(other.name),
        parameters(other.parameters),
-       definition(other.definition==&other ? this : other.definition),
+       definition(other.definition==&other ? this : 0),
        body(other.body)
 { }
 
@@ -211,121 +305,20 @@ void Jump::visit(NodeVisitor &visitor)
 }
 
 
-void NodeVisitor::visit(Assignment &assign)
-{
-       visit(static_cast<BinaryExpression &>(assign));
-}
-
-
-void TraversingVisitor::visit(Block &block)
-{
-       for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
-               (*i)->visit(*this);
-}
-
-void TraversingVisitor::visit(ParenthesizedExpression &parexpr)
-{
-       parexpr.expression->visit(*this);
-}
-
-void TraversingVisitor::visit(MemberAccess &memacc)
-{
-       memacc.left->visit(*this);
-}
-
-void TraversingVisitor::visit(UnaryExpression &unary)
-{
-       unary.expression->visit(*this);
-}
-
-void TraversingVisitor::visit(BinaryExpression &binary)
-{
-       binary.left->visit(*this);
-       binary.right->visit(*this);
-}
-
-void TraversingVisitor::visit(FunctionCall &call)
-{
-       for(NodeArray<Expression>::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
-               (*i)->visit(*this);
-}
-
-void TraversingVisitor::visit(ExpressionStatement &expr)
-{
-       expr.expression->visit(*this);
-}
-
-void TraversingVisitor::visit(InterfaceLayout &layout)
-{
-       layout.layout.visit(*this);
-}
-
-void TraversingVisitor::visit(StructDeclaration &strct)
-{
-       strct.members.visit(*this);
-}
-
-void TraversingVisitor::visit(VariableDeclaration &var)
-{
-       if(var.layout)
-               var.layout->visit(*this);
-       if(var.init_expression)
-               var.init_expression->visit(*this);
-       if(var.array_size)
-               var.array_size->visit(*this);
-}
-
-void TraversingVisitor::visit(InterfaceBlock &iface)
-{
-       iface.members.visit(*this);
-}
-
-void TraversingVisitor::visit(FunctionDeclaration &func)
-{
-       for(NodeArray<VariableDeclaration>::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
-               (*i)->visit(*this);
-       func.body.visit(*this);
-}
-
-void TraversingVisitor::visit(Conditional &cond)
-{
-       cond.condition->visit(*this);
-       cond.body.visit(*this);
-       cond.else_body.visit(*this);
-}
-
-void TraversingVisitor::visit(Iteration &iter)
-{
-       if(iter.init_statement)
-               iter.init_statement->visit(*this);
-       if(iter.condition)
-               iter.condition->visit(*this);
-       if(iter.loop_expression)
-               iter.loop_expression->visit(*this);
-       iter.body.visit(*this);
-}
-
-void TraversingVisitor::visit(Passthrough &pass)
-{
-       if(pass.subscript)
-               pass.subscript->visit(*this);
-}
-
-void TraversingVisitor::visit(Return &ret)
-{
-       if(ret.expression)
-               ret.expression->visit(*this);
-}
-
-
-Stage::Stage(StageType t):
+Stage::Stage(Stage::Type t):
        type(t),
        previous(0)
 { }
 
+const char *Stage::get_stage_name(Type type)
+{
+       static const char *names[] = { "shared", "vertex", "geometry", "fragment" };
+       return names[type];
+}
+
 
 Module::Module():
-       shared(SHARED)
+       shared(Stage::SHARED)
 { }
 
 } // namespace SL