]> git.tdb.fi Git - libs/gl.git/blobdiff - source/programsyntax.h
Inline trivial shader functions that are only called once
[libs/gl.git] / source / programsyntax.h
index 88f333ac1559d2e662dbaeca7903aecfbdbfa1ed..9a0cc6ea3f4bc8c867a2d381494b816b937495eb 100644 (file)
@@ -5,6 +5,8 @@
 #include <map>
 #include <string>
 #include <vector>
+#include <msp/core/refptr.h>
+#include "extension.h"
 
 namespace Msp {
 namespace GL {
@@ -23,39 +25,13 @@ public:
        virtual void visit(NodeVisitor &) = 0;
 };
 
-template<typename T>
-class NodePtr
-{
-private:
-       T *node;
-
-public:
-       NodePtr(T *n = 0): node(n) { }
-       NodePtr(const NodePtr &p): node(clone(p.node)) { }
-       NodePtr &operator=(const NodePtr &p) { delete node; node = clone(p.node); return *this; }
-#if __cplusplus>=201103L
-       NodePtr(NodePtr &&p): node(p.node) { p.node = 0; }
-       NodePtr &operator=(NodePtr &&p) { delete node; node = p.node; p.node = 0; return *this; }
-#endif
-       ~NodePtr() { delete node; }
-
-private:
-       static T *clone(T *n) { return n ? n->clone() : 0; }
-
-public:
-       T *operator->() { return node; }
-       const T *operator->() const { return node; }
-       T &operator*() { return *node; }
-       const T &operator*() const { return *node; }
-       operator void *() const { return node; }
-};
-
 struct StructDeclaration;
 struct VariableDeclaration;
+struct FunctionDeclaration;
 
 struct Block: Node
 {
-       std::list<NodePtr<Node> > body;
+       std::list<RefPtr<Node> > body;
        bool use_braces;
        std::map<std::string, StructDeclaration *> types;
        std::map<std::string, VariableDeclaration *> variables;
@@ -81,7 +57,7 @@ struct Literal: Expression
 
 struct ParenthesizedExpression: Expression
 {
-       NodePtr<Expression> expression;
+       RefPtr<Expression> expression;
 
        virtual ParenthesizedExpression *clone() const { return new ParenthesizedExpression(*this); }
        virtual void visit(NodeVisitor &);
@@ -100,7 +76,7 @@ struct VariableReference: Expression
 
 struct MemberAccess: Expression
 {
-       NodePtr<Expression> left;
+       RefPtr<Expression> left;
        std::string member;
        VariableDeclaration *declaration;
 
@@ -111,7 +87,7 @@ struct MemberAccess: Expression
 struct UnaryExpression: Expression
 {
        std::string oper;
-       NodePtr<Expression> expression;
+       RefPtr<Expression> expression;
        bool prefix;
 
        UnaryExpression();
@@ -122,23 +98,32 @@ struct UnaryExpression: Expression
 
 struct BinaryExpression: Expression
 {
-       NodePtr<Expression> left;
+       RefPtr<Expression> left;
        std::string oper;
-       NodePtr<Expression> right;
+       RefPtr<Expression> right;
        std::string after;
-       bool assignment;
-
-       BinaryExpression();
 
        virtual BinaryExpression *clone() const { return new BinaryExpression(*this); }
        virtual void visit(NodeVisitor &);
 };
 
+struct Assignment: BinaryExpression
+{
+       bool self_referencing;
+       VariableDeclaration *target_declaration;
+
+       Assignment();
+
+       virtual Assignment *clone() const { return new Assignment(*this); }
+       virtual void visit(NodeVisitor &);
+};
+
 struct FunctionCall: Expression
 {
        std::string name;
+       FunctionDeclaration *declaration;
        bool constructor;
-       std::vector<NodePtr<Expression> > arguments;
+       std::vector<RefPtr<Expression> > arguments;
 
        FunctionCall();
 
@@ -148,12 +133,20 @@ struct FunctionCall: Expression
 
 struct ExpressionStatement: Node
 {
-       NodePtr<Expression> expression;
+       RefPtr<Expression> expression;
 
        virtual ExpressionStatement *clone() const { return new ExpressionStatement(*this); }
        virtual void visit(NodeVisitor &);
 };
 
+struct Import: Node
+{
+       std::string module;
+
+       virtual Import *clone() const { return new Import(*this); }
+       virtual void visit(NodeVisitor &);
+};
+
 struct Layout: Node
 {
        struct Qualifier
@@ -163,12 +156,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;
@@ -189,9 +190,10 @@ struct VariableDeclaration: Node
        StructDeclaration *type_declaration;
        std::string name;
        bool array;
-       NodePtr<Expression> array_size;
-       NodePtr<Expression> init_expression;
+       RefPtr<Expression> array_size;
+       RefPtr<Expression> init_expression;
        VariableDeclaration *linked_declaration;
+       RefPtr<Layout> layout;
 
        VariableDeclaration();
 
@@ -217,11 +219,12 @@ struct FunctionDeclaration: Node
 {
        std::string return_type;
        std::string name;
-       std::vector<NodePtr<VariableDeclaration> > parameters;
-       bool definition;
+       std::vector<RefPtr<VariableDeclaration> > parameters;
+       FunctionDeclaration *definition;
        Block body;
 
        FunctionDeclaration();
+       FunctionDeclaration(const FunctionDeclaration &);
 
        virtual FunctionDeclaration *clone() const { return new FunctionDeclaration(*this); }
        virtual void visit(NodeVisitor &);
@@ -229,7 +232,7 @@ struct FunctionDeclaration: Node
 
 struct Conditional: Node
 {
-       NodePtr<Expression> condition;
+       RefPtr<Expression> condition;
        Block body;
        Block else_body;
 
@@ -239,9 +242,9 @@ struct Conditional: Node
 
 struct Iteration: Node
 {
-       NodePtr<Node> init_statement;
-       NodePtr<Expression> condition;
-       NodePtr<Expression> loop_expression;
+       RefPtr<Node> init_statement;
+       RefPtr<Expression> condition;
+       RefPtr<Expression> loop_expression;
        Block body;
 
        virtual Iteration *clone() const { return new Iteration(*this); }
@@ -250,7 +253,7 @@ struct Iteration: Node
 
 struct Passthrough: Node
 {
-       NodePtr<Expression> subscript;
+       RefPtr<Expression> subscript;
 
        virtual Passthrough *clone() const { return new Passthrough(*this); }
        virtual void visit(NodeVisitor &);
@@ -258,7 +261,7 @@ struct Passthrough: Node
 
 struct Return: Node
 {
-       NodePtr<Expression> expression;
+       RefPtr<Expression> expression;
 
        virtual Return *clone() const { return new Return(*this); }
        virtual void visit(NodeVisitor &);
@@ -275,9 +278,12 @@ struct NodeVisitor
        virtual void visit(MemberAccess &) { }
        virtual void visit(UnaryExpression &) { }
        virtual void visit(BinaryExpression &) { }
+       virtual void visit(Assignment &);
        virtual void visit(FunctionCall &) { }
        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 &) { }
@@ -297,6 +303,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 &);
@@ -322,6 +329,8 @@ struct Stage
        ProgramSyntax::Block content;
        std::map<std::string, VariableDeclaration *> in_variables;
        std::map<std::string, VariableDeclaration *> out_variables;
+       std::map<std::string, unsigned> locations;
+       Version required_version;
 
        Stage(StageType);
 };