]> git.tdb.fi Git - libs/gl.git/blobdiff - source/programsyntax.h
Add functions for setting arrays of 2x2 and 3x3 matrix uniforms
[libs/gl.git] / source / programsyntax.h
index 9a0cc6ea3f4bc8c867a2d381494b816b937495eb..590429519118614f6a59a83796da97f50693ff9c 100644 (file)
@@ -7,6 +7,10 @@
 #include <vector>
 #include <msp/core/refptr.h>
 #include "extension.h"
+#include "uniform.h"
+
+#pragma push_macro("interface")
+#undef interface
 
 namespace Msp {
 namespace GL {
@@ -25,13 +29,54 @@ public:
        virtual void visit(NodeVisitor &) = 0;
 };
 
+template<typename T>
+class NodePtr: public RefPtr<T>
+{
+public:
+       NodePtr() { }
+       NodePtr(T *p): RefPtr<T>(p) { }
+       NodePtr(const NodePtr &p): RefPtr<T>(p ? p->clone() : 0) { }
+
+       template<typename U>
+       NodePtr(const RefPtr<U> &p): RefPtr<T>(p) { }
+
+       template<typename U>
+       NodePtr(const NodePtr<U> &p): RefPtr<T>(p ? p->clone() : 0) { }
+};
+
+template<typename C>
+class NodeContainer: public C
+{
+public:
+       NodeContainer() { }
+       NodeContainer(const NodeContainer &);
+};
+
+template<typename T>
+class NodeList: public NodeContainer<std::list<RefPtr<T> > >
+{ };
+
+template<typename T>
+class NodeArray: public NodeContainer<std::vector<RefPtr<T> > >
+{ };
+
 struct StructDeclaration;
 struct VariableDeclaration;
 struct FunctionDeclaration;
 
+struct Statement: Node
+{
+       unsigned source;
+       unsigned line;
+
+       Statement();
+
+       virtual Statement *clone() const = 0;
+};
+
 struct Block: Node
 {
-       std::list<RefPtr<Node> > body;
+       NodeList<Statement> body;
        bool use_braces;
        std::map<std::string, StructDeclaration *> types;
        std::map<std::string, VariableDeclaration *> variables;
@@ -57,7 +102,7 @@ struct Literal: Expression
 
 struct ParenthesizedExpression: Expression
 {
-       RefPtr<Expression> expression;
+       NodePtr<Expression> expression;
 
        virtual ParenthesizedExpression *clone() const { return new ParenthesizedExpression(*this); }
        virtual void visit(NodeVisitor &);
@@ -76,7 +121,7 @@ struct VariableReference: Expression
 
 struct MemberAccess: Expression
 {
-       RefPtr<Expression> left;
+       NodePtr<Expression> left;
        std::string member;
        VariableDeclaration *declaration;
 
@@ -87,7 +132,7 @@ struct MemberAccess: Expression
 struct UnaryExpression: Expression
 {
        std::string oper;
-       RefPtr<Expression> expression;
+       NodePtr<Expression> expression;
        bool prefix;
 
        UnaryExpression();
@@ -98,9 +143,9 @@ struct UnaryExpression: Expression
 
 struct BinaryExpression: Expression
 {
-       RefPtr<Expression> left;
+       NodePtr<Expression> left;
        std::string oper;
-       RefPtr<Expression> right;
+       NodePtr<Expression> right;
        std::string after;
 
        virtual BinaryExpression *clone() const { return new BinaryExpression(*this); }
@@ -123,7 +168,7 @@ struct FunctionCall: Expression
        std::string name;
        FunctionDeclaration *declaration;
        bool constructor;
-       std::vector<RefPtr<Expression> > arguments;
+       NodeArray<Expression> arguments;
 
        FunctionCall();
 
@@ -131,15 +176,15 @@ struct FunctionCall: Expression
        virtual void visit(NodeVisitor &);
 };
 
-struct ExpressionStatement: Node
+struct ExpressionStatement: Statement
 {
-       RefPtr<Expression> expression;
+       NodePtr<Expression> expression;
 
        virtual ExpressionStatement *clone() const { return new ExpressionStatement(*this); }
        virtual void visit(NodeVisitor &);
 };
 
-struct Import: Node
+struct Import: Statement
 {
        std::string module;
 
@@ -147,10 +192,20 @@ struct Import: Node
        virtual void visit(NodeVisitor &);
 };
 
+struct Precision: Statement
+{
+       std::string precision;
+       std::string type;
+
+       virtual Precision *clone() const { return new Precision(*this); }
+       virtual void visit(NodeVisitor &);
+};
+
 struct Layout: Node
 {
        struct Qualifier
        {
+               // TODO the standard calls this name, not identifier
                std::string identifier;
                std::string value;
        };
@@ -161,7 +216,7 @@ struct Layout: Node
        virtual void visit(NodeVisitor &);
 };
 
-struct InterfaceLayout: Node
+struct InterfaceLayout: Statement
 {
        std::string interface;
        Layout layout;
@@ -170,7 +225,7 @@ struct InterfaceLayout: Node
        virtual void visit(NodeVisitor &);
 };
 
-struct StructDeclaration: Node
+struct StructDeclaration: Statement
 {
        std::string name;
        Block members;
@@ -181,19 +236,21 @@ struct StructDeclaration: Node
        virtual void visit(NodeVisitor &);
 };
 
-struct VariableDeclaration: Node
+struct VariableDeclaration: Statement
 {
        bool constant;
        std::string sampling;
+       std::string interpolation;
        std::string interface;
+       std::string precision;
        std::string type;
        StructDeclaration *type_declaration;
        std::string name;
        bool array;
-       RefPtr<Expression> array_size;
-       RefPtr<Expression> init_expression;
+       NodePtr<Expression> array_size;
+       NodePtr<Expression> init_expression;
        VariableDeclaration *linked_declaration;
-       RefPtr<Layout> layout;
+       NodePtr<Layout> layout;
 
        VariableDeclaration();
 
@@ -201,7 +258,7 @@ struct VariableDeclaration: Node
        virtual void visit(NodeVisitor &);
 };
 
-struct InterfaceBlock: Node
+struct InterfaceBlock: Statement
 {
        std::string interface;
        std::string name;
@@ -215,11 +272,11 @@ struct InterfaceBlock: Node
        virtual void visit(NodeVisitor &);
 };
 
-struct FunctionDeclaration: Node
+struct FunctionDeclaration: Statement
 {
        std::string return_type;
        std::string name;
-       std::vector<RefPtr<VariableDeclaration> > parameters;
+       NodeArray<VariableDeclaration> parameters;
        FunctionDeclaration *definition;
        Block body;
 
@@ -230,9 +287,9 @@ struct FunctionDeclaration: Node
        virtual void visit(NodeVisitor &);
 };
 
-struct Conditional: Node
+struct Conditional: Statement
 {
-       RefPtr<Expression> condition;
+       NodePtr<Expression> condition;
        Block body;
        Block else_body;
 
@@ -240,33 +297,41 @@ struct Conditional: Node
        virtual void visit(NodeVisitor &);
 };
 
-struct Iteration: Node
+struct Iteration: Statement
 {
-       RefPtr<Node> init_statement;
-       RefPtr<Expression> condition;
-       RefPtr<Expression> loop_expression;
+       NodePtr<Node> init_statement;
+       NodePtr<Expression> condition;
+       NodePtr<Expression> loop_expression;
        Block body;
 
        virtual Iteration *clone() const { return new Iteration(*this); }
        virtual void visit(NodeVisitor &);
 };
 
-struct Passthrough: Node
+struct Passthrough: Statement
 {
-       RefPtr<Expression> subscript;
+       NodePtr<Expression> subscript;
 
        virtual Passthrough *clone() const { return new Passthrough(*this); }
        virtual void visit(NodeVisitor &);
 };
 
-struct Return: Node
+struct Return: Statement
 {
-       RefPtr<Expression> expression;
+       NodePtr<Expression> expression;
 
        virtual Return *clone() const { return new Return(*this); }
        virtual void visit(NodeVisitor &);
 };
 
+struct Jump: Statement
+{
+       std::string keyword;
+
+       virtual Jump *clone() const { return new Jump(*this); }
+       virtual void visit(NodeVisitor &);
+};
+
 struct NodeVisitor
 {
        virtual ~NodeVisitor() { }
@@ -282,6 +347,7 @@ struct NodeVisitor
        virtual void visit(FunctionCall &) { }
        virtual void visit(ExpressionStatement &) { }
        virtual void visit(Import &) { }
+       virtual void visit(Precision &) { }
        virtual void visit(Layout &) { }
        virtual void visit(InterfaceLayout &) { }
        virtual void visit(StructDeclaration &) { }
@@ -292,10 +358,12 @@ struct NodeVisitor
        virtual void visit(Iteration &) { }
        virtual void visit(Passthrough &) { }
        virtual void visit(Return &) { }
+       virtual void visit(Jump &) { }
 };
 
 struct TraversingVisitor: NodeVisitor
 {
+       using NodeVisitor::visit;
        virtual void visit(Block &);
        virtual void visit(ParenthesizedExpression &);
        virtual void visit(MemberAccess &);
@@ -331,6 +399,7 @@ struct Stage
        std::map<std::string, VariableDeclaration *> out_variables;
        std::map<std::string, unsigned> locations;
        Version required_version;
+       std::vector<const Extension *> required_extensions;
 
        Stage(StageType);
 };
@@ -347,4 +416,6 @@ struct Module
 } // namespace GL
 } // namespace Msp
 
+#pragma pop_macro("interface")
+
 #endif