]> git.tdb.fi Git - libs/gl.git/blobdiff - source/glsl/syntax.cpp
Mark constant data as const
[libs/gl.git] / source / glsl / syntax.cpp
index e7a5c155e851e2ef179ebc59ef8324ec8b0580c8..bb8d7dab046f2ea1293927d93aa3e415818e12d4 100644 (file)
@@ -1,4 +1,7 @@
+#include <msp/core/algorithm.h>
+#include <msp/core/maputils.h>
 #include "syntax.h"
+#include "visitor.h"
 
 using namespace std;
 
@@ -8,71 +11,80 @@ 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 }
+       { "[", "]", 2, BINARY, LEFT_TO_RIGHT },
+       { "(", ")", 2, POSTFIX, LEFT_TO_RIGHT },
+       { ".", { }, 2, POSTFIX, 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, ASSOCIATIVE },
+       { "/", { }, 4, BINARY, LEFT_TO_RIGHT },
+       { "%", { }, 4, BINARY, LEFT_TO_RIGHT },
+       { "+", { }, 5, BINARY, ASSOCIATIVE },
+       { "-", { }, 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, ASSOCIATIVE },
+       { "^", { }, 10, BINARY, ASSOCIATIVE },
+       { "|", { }, 11, BINARY, ASSOCIATIVE },
+       { "&&", { }, 12, BINARY, ASSOCIATIVE },
+       { "^^", { }, 13, BINARY, ASSOCIATIVE },
+       { "||", { }, 14, BINARY, ASSOCIATIVE },
+       { "?", ":", 15, TERNARY, 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 }
 };
 
+const Operator &Operator::get_operator(const string &token, Type type)
+{
+       for(const Operator *i=operators; i->type; ++i)
+               if(i->type==type && i->token==token)
+                       return *i;
+       throw key_error(token);
+}
+
 
 template<typename C>
 NodeContainer<C>::NodeContainer(const NodeContainer &c):
        C(c)
 {
-       for(typename C::iterator i=this->begin(); i!=this->end(); ++i)
-               *i = (*i)->clone();
+       for(auto &i: *this)
+               i = i->clone();
 }
 
 
-Statement::Statement():
-       source(0),
-       line(1)
+Block::Block():
+       use_braces(false),
+       parent(0)
 { }
 
-
-Block::Block():
-       use_braces(false)
+Block::Block(const Block &other):
+       Node(other),
+       body(other.body),
+       use_braces(other.use_braces),
+       parent(0)
 { }
 
 void Block::visit(NodeVisitor &visitor)
@@ -81,37 +93,81 @@ void Block::visit(NodeVisitor &visitor)
 }
 
 
+Expression::Expression():
+       oper(0),
+       type(0),
+       lvalue(false)
+{ }
+
+
 void Literal::visit(NodeVisitor &visitor)
 {
        visitor.visit(*this);
 }
 
 
-void ParenthesizedExpression::visit(NodeVisitor &visitor)
+VariableReference::VariableReference():
+       declaration(0)
+{ }
+
+VariableReference::VariableReference(const VariableReference &other):
+       Expression(other),
+       name(other.name),
+       declaration(0)
+{ }
+
+void VariableReference::visit(NodeVisitor &visitor)
 {
        visitor.visit(*this);
 }
 
 
-VariableReference::VariableReference():
+InterfaceBlockReference::InterfaceBlockReference():
        declaration(0)
 { }
 
-void VariableReference::visit(NodeVisitor &visitor)
+InterfaceBlockReference::InterfaceBlockReference(const InterfaceBlockReference &other):
+       Expression(other),
+       name(other.name),
+       declaration(0)
+{ }
+
+void InterfaceBlockReference::visit(NodeVisitor &visitor)
 {
        visitor.visit(*this);
 }
 
 
+MemberAccess::MemberAccess():
+       declaration(0),
+       index(-1)
+{ }
+
+MemberAccess::MemberAccess(const MemberAccess &other):
+       Expression(other),
+       left(other.left),
+       member(other.member),
+       declaration(0),
+       index(-1)
+{ }
+
 void MemberAccess::visit(NodeVisitor &visitor)
 {
        visitor.visit(*this);
 }
 
 
-UnaryExpression::UnaryExpression():
-       prefix(true)
-{ }
+Swizzle::Swizzle():
+       count(0)
+{
+       fill(components, components+4, 0);
+}
+
+void Swizzle::visit(NodeVisitor &visitor)
+{
+       visitor.visit(*this);
+}
+
 
 void UnaryExpression::visit(NodeVisitor &visitor)
 {
@@ -126,8 +182,12 @@ void BinaryExpression::visit(NodeVisitor &visitor)
 
 
 Assignment::Assignment():
-       self_referencing(false),
-       target_declaration(0)
+       self_referencing(false)
+{ }
+
+Assignment::Assignment(const Assignment &other):
+       BinaryExpression(other),
+       self_referencing(other.self_referencing)
 { }
 
 void Assignment::visit(NodeVisitor &visitor)
@@ -136,9 +196,41 @@ void Assignment::visit(NodeVisitor &visitor)
 }
 
 
+Assignment::Target::Target(Statement *d):
+       declaration(d),
+       chain_len(0)
+{
+       fill(chain, chain+7, 0);
+}
+
+bool Assignment::Target::operator<(const Target &other) const
+{
+       if(declaration!=other.declaration)
+               return declaration<other.declaration;
+       for(unsigned i=0; (i<7 && i<chain_len && i<other.chain_len); ++i)
+               if(chain[i]!=other.chain[i])
+                       return chain[i]<other.chain[i];
+       return chain_len<other.chain_len;
+}
+
+
+void TernaryExpression::visit(NodeVisitor &visitor)
+{
+       visitor.visit(*this);
+}
+
+
 FunctionCall::FunctionCall():
-       declaration(0),
-       constructor(false)
+       constructor(false),
+       declaration(0)
+{ }
+
+FunctionCall::FunctionCall(const FunctionCall &other):
+       Expression(other),
+       name(other.name),
+       constructor(other.constructor),
+       arguments(other.arguments),
+       declaration(0)
 { }
 
 void FunctionCall::visit(NodeVisitor &visitor)
@@ -177,11 +269,59 @@ void InterfaceLayout::visit(NodeVisitor &visitor)
 }
 
 
-StructDeclaration::StructDeclaration()
+BasicTypeDeclaration::BasicTypeDeclaration():
+       kind(ALIAS),
+       size(0),
+       sign(true),
+       base_type(0)
+{ }
+
+BasicTypeDeclaration::BasicTypeDeclaration(const BasicTypeDeclaration &other):
+       TypeDeclaration(other),
+       kind(other.kind),
+       size(other.size),
+       sign(other.sign),
+       base(other.base),
+       base_type(0)
+{ }
+
+void BasicTypeDeclaration::visit(NodeVisitor &visitor)
+{
+       visitor.visit(*this);
+}
+
+
+ImageTypeDeclaration::ImageTypeDeclaration():
+       dimensions(TWO),
+       array(false),
+       sampled(true),
+       shadow(false)
+{ }
+
+void ImageTypeDeclaration::visit(NodeVisitor &visitor)
+{
+       visitor.visit(*this);
+}
+
+
+StructDeclaration::StructDeclaration():
+       interface_block(0)
 {
        members.use_braces = true;
 }
 
+StructDeclaration::StructDeclaration(const StructDeclaration &other):
+       TypeDeclaration(other),
+       members(other.members),
+       interface_block(0)
+{ }
+
+StructDeclaration::~StructDeclaration()
+{
+       if(interface_block && interface_block->struct_declaration==this)
+               interface_block->struct_declaration = 0;
+}
+
 void StructDeclaration::visit(NodeVisitor &visitor)
 {
        visitor.visit(*this);
@@ -190,11 +330,34 @@ void StructDeclaration::visit(NodeVisitor &visitor)
 
 VariableDeclaration::VariableDeclaration():
        constant(false),
-       type_declaration(0),
        array(false),
+       type_declaration(0),
        linked_declaration(0)
 { }
 
+VariableDeclaration::VariableDeclaration(const VariableDeclaration &other):
+       Statement(other),
+       layout(other.layout),
+       constant(other.constant),
+       sampling(other.sampling),
+       interpolation(other.interpolation),
+       interface(other.interface),
+       precision(other.precision),
+       type(other.type),
+       name(other.name),
+       array(other.array),
+       array_size(other.array_size),
+       init_expression(other.init_expression),
+       type_declaration(0),
+       linked_declaration(0)
+{ }
+
+VariableDeclaration::~VariableDeclaration()
+{
+       if(linked_declaration && linked_declaration->linked_declaration==this)
+               linked_declaration->linked_declaration = 0;
+}
+
 void VariableDeclaration::visit(NodeVisitor &visitor)
 {
        visitor.visit(*this);
@@ -202,9 +365,30 @@ void VariableDeclaration::visit(NodeVisitor &visitor)
 
 
 InterfaceBlock::InterfaceBlock():
-       array(false)
+       array(false),
+       type_declaration(0),
+       struct_declaration(0),
+       linked_block(0)
+{ }
+
+InterfaceBlock::InterfaceBlock(const InterfaceBlock &other):
+       Statement(other),
+       interface(other.interface),
+       block_name(other.block_name),
+       members(other.members),
+       instance_name(other.instance_name),
+       array(other.array),
+       type_declaration(0),
+       struct_declaration(0),
+       linked_block(0)
+{ }
+
+InterfaceBlock::~InterfaceBlock()
 {
-       members.use_braces = true;
+       if(linked_block && linked_block->linked_block==this)
+               linked_block->linked_block = 0;
+       if(struct_declaration && struct_declaration->interface_block==this)
+               struct_declaration->interface_block = 0;
 }
 
 void InterfaceBlock::visit(NodeVisitor &visitor)
@@ -214,15 +398,23 @@ void InterfaceBlock::visit(NodeVisitor &visitor)
 
 
 FunctionDeclaration::FunctionDeclaration():
-       definition(0)
+       virtua(false),
+       overrd(false),
+       definition(0),
+       return_type_declaration(0)
 { }
 
 FunctionDeclaration::FunctionDeclaration(const FunctionDeclaration &other):
+       Statement(other),
        return_type(other.return_type),
        name(other.name),
        parameters(other.parameters),
-       definition(other.definition==&other ? this : other.definition),
-       body(other.body)
+       virtua(other.virtua),
+       overrd(other.overrd),
+       body(other.body),
+       signature(other.signature),
+       definition(other.definition==&other ? this : 0),
+       return_type_declaration(0)
 { }
 
 void FunctionDeclaration::visit(NodeVisitor &visitor)
@@ -261,123 +453,56 @@ 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);
-}
+Stage::Stage(Stage::Type t):
+       type(t),
+       previous(0)
+{ }
 
-void TraversingVisitor::visit(InterfaceLayout &layout)
+const char *Stage::get_stage_name(Type type)
 {
-       layout.layout.visit(*this);
+       static const char *const names[] = { "shared", "vertex", "geometry", "fragment" };
+       return names[type];
 }
 
-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);
-}
+Module::Module():
+       shared(Stage::SHARED)
+{ }
 
-void TraversingVisitor::visit(InterfaceBlock &iface)
-{
-       iface.members.visit(*this);
-}
 
-void TraversingVisitor::visit(FunctionDeclaration &func)
+string get_unused_variable_name(const Block &block, const string &base)
 {
-       for(NodeArray<VariableDeclaration>::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
-               (*i)->visit(*this);
-       func.body.visit(*this);
-}
+       string name = base;
 
-void TraversingVisitor::visit(Conditional &cond)
-{
-       cond.condition->visit(*this);
-       cond.body.visit(*this);
-       cond.else_body.visit(*this);
-}
+       unsigned number = 1;
+       unsigned base_size = name.size();
+       while(1)
+       {
+               bool unused = true;
+               for(const Block *b=&block; (unused && b); b=b->parent)
+                       unused = !b->variables.count(name);
+               if(unused)
+                       return name;
 
-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);
+               name.erase(base_size);
+               name += format("_%d", number);
+               ++number;
+       }
 }
 
-void TraversingVisitor::visit(Passthrough &pass)
+int get_layout_value(const Layout &layout, const string &name, int def_value)
 {
-       if(pass.subscript)
-               pass.subscript->visit(*this);
+       auto i = find_member(layout.qualifiers, name, &Layout::Qualifier::name);
+       return (i!=layout.qualifiers.end() ? i->value : def_value);
 }
 
-void TraversingVisitor::visit(Return &ret)
+void add_to_chain(Assignment::Target &target, Assignment::Target::ChainType type, unsigned index)
 {
-       if(ret.expression)
-               ret.expression->visit(*this);
+       if(target.chain_len<7)
+               target.chain[target.chain_len] = type | min<unsigned>(index, 0x3F);
+       ++target.chain_len;
 }
 
-
-Stage::Stage(StageType t):
-       type(t),
-       previous(0)
-{ }
-
-
-Module::Module():
-       shared(SHARED)
-{ }
-
 } // namespace SL
 } // namespace GL
 } // namespace Msp