]> git.tdb.fi Git - libs/gl.git/commitdiff
Support layout declarations on variables
authorMikko Rasa <tdb@tdb.fi>
Sat, 19 Nov 2016 16:23:58 +0000 (18:23 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sat, 19 Nov 2016 16:23:58 +0000 (18:23 +0200)
source/programcompiler.cpp
source/programcompiler.h
source/programparser.cpp
source/programsyntax.cpp
source/programsyntax.h

index aef17fa2369fdadc46366c7b4868751ba2d1fc1e..ece48cf06e2606219e76e6fb86bd03577a98074a 100644 (file)
@@ -334,7 +334,13 @@ void ProgramCompiler::Formatter::visit(Layout &layout)
                if(!i->value.empty())
                        formatted += format("=%s", i->value);
        }
-       formatted += format(") %s;", layout.interface);
+       formatted += ')';
+}
+
+void ProgramCompiler::Formatter::visit(InterfaceLayout &layout)
+{
+       layout.layout.visit(*this);
+       formatted += format(" %s;", layout.interface);
 }
 
 void ProgramCompiler::Formatter::visit(StructDeclaration &strct)
@@ -346,6 +352,11 @@ void ProgramCompiler::Formatter::visit(StructDeclaration &strct)
 
 void ProgramCompiler::Formatter::visit(VariableDeclaration &var)
 {
+       if(var.layout)
+       {
+               var.layout->visit(*this);
+               formatted += ' ';
+       }
        if(var.constant)
                formatted += "const ";
        if(!var.sampling.empty())
index 8ae22901e89dcbfb19bf08fc2a25929aaddf9188..2a31a962b989a5cec0021e82584f75a7036ccac4 100644 (file)
@@ -52,6 +52,7 @@ private:
                virtual void visit(ProgramSyntax::ExpressionStatement &);
                virtual void visit(ProgramSyntax::Import &);
                virtual void visit(ProgramSyntax::Layout &);
+               virtual void visit(ProgramSyntax::InterfaceLayout &);
                virtual void visit(ProgramSyntax::StructDeclaration &);
                virtual void visit(ProgramSyntax::VariableDeclaration &);
                virtual void visit(ProgramSyntax::InterfaceBlock &);
index ddea96409dea4475e911e94fac01fda171451b57..596ed0456b8e789ec2bd5ac636790a7d97bc8c4c 100644 (file)
@@ -335,7 +335,24 @@ Node *ProgramParser::parse_global_declaration()
        if(token=="import")
                return parse_import();
        else if(token=="layout")
-               return parse_layout();
+       {
+               RefPtr<Layout> layout = parse_layout();
+               token = peek_token();
+               if(is_interface_qualifier(token) && peek_token(1)==";")
+               {
+                       RefPtr<InterfaceLayout> iface_lo = new InterfaceLayout;
+                       iface_lo->layout.qualifiers = layout->qualifiers;
+                       iface_lo->interface = parse_token();
+                       expect(";");
+                       return iface_lo.release();
+               }
+               else
+               {
+                       VariableDeclaration *var = parse_variable_declaration();
+                       var->layout = layout.release();
+                       return var;
+               }
+       }
        else if(token=="struct")
                return parse_struct_declaration();
        else if(is_sampling_qualifier(token) || token=="const")
@@ -421,8 +438,6 @@ Layout *ProgramParser::parse_layout()
                expect(",");
        }
        expect(")");
-       layout->interface = parse_token();
-       expect(";");
 
        return layout.release();
 }
index 098130f87c2e4920597eadb2838295b782dbf740..c6e0f6d40e9bc9ca97583adf07454af208bcadf9 100644 (file)
@@ -100,6 +100,12 @@ void Layout::visit(NodeVisitor &visitor)
 }
 
 
+void InterfaceLayout::visit(NodeVisitor &visitor)
+{
+       visitor.visit(*this);
+}
+
+
 StructDeclaration::StructDeclaration()
 {
        members.use_braces = true;
@@ -222,6 +228,11 @@ 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);
@@ -229,6 +240,8 @@ void TraversingVisitor::visit(StructDeclaration &strct)
 
 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)
index 2f9f1fc08a50f47eb3a8fc6f4757217cb39f0b09..796c4b938f10f6f84eb1fbcdcdd0c3fd9bd6f062 100644 (file)
@@ -182,12 +182,20 @@ struct Layout: Node
        };
 
        std::vector<Qualifier> qualifiers;
-       std::string interface;
 
        virtual Layout *clone() const { return new Layout(*this); }
        virtual void visit(NodeVisitor &);
 };
 
+struct InterfaceLayout: Node
+{
+       std::string interface;
+       Layout layout;
+
+       virtual InterfaceLayout *clone() const { return new InterfaceLayout(*this); }
+       virtual void visit(NodeVisitor &);
+};
+
 struct StructDeclaration: Node
 {
        std::string name;
@@ -211,6 +219,7 @@ struct VariableDeclaration: Node
        NodePtr<Expression> array_size;
        NodePtr<Expression> init_expression;
        VariableDeclaration *linked_declaration;
+       NodePtr<Layout> layout;
 
        VariableDeclaration();
 
@@ -300,6 +309,7 @@ struct NodeVisitor
        virtual void visit(ExpressionStatement &) { }
        virtual void visit(Import &) { }
        virtual void visit(Layout &) { }
+       virtual void visit(InterfaceLayout &) { }
        virtual void visit(StructDeclaration &) { }
        virtual void visit(VariableDeclaration &) { }
        virtual void visit(InterfaceBlock &) { }
@@ -319,6 +329,7 @@ struct TraversingVisitor: NodeVisitor
        virtual void visit(BinaryExpression &);
        virtual void visit(FunctionCall &);
        virtual void visit(ExpressionStatement &);
+       virtual void visit(InterfaceLayout &);
        virtual void visit(StructDeclaration &);
        virtual void visit(VariableDeclaration &);
        virtual void visit(InterfaceBlock &);