]> git.tdb.fi Git - libs/gl.git/blobdiff - source/glsl/syntax.h
Give declaration nodes to all GLSL types.
[libs/gl.git] / source / glsl / syntax.h
index 3fb848d2ceb81a91fdc07ab19f0e072b0b71010c..62c289d9fbb6ec48f61be268b7eec67072718e98 100644 (file)
@@ -3,10 +3,12 @@
 
 #include <list>
 #include <map>
+#include <set>
 #include <string>
 #include <vector>
 #include <msp/core/refptr.h>
 #include "features.h"
+#include "glsl_error.h"
 #include "sourcemap.h"
 
 #pragma push_macro("interface")
@@ -29,7 +31,8 @@ struct Operator
        enum Associativity
        {
                LEFT_TO_RIGHT,
-               RIGHT_TO_LEFT
+               RIGHT_TO_LEFT,
+               ASSOCIATIVE
        };
 
        char token[4];
@@ -38,15 +41,25 @@ struct Operator
        Associativity assoc;
 
        static const Operator operators[];
+
+       static const Operator &get_operator(const std::string &, Type);
+};
+
+enum
+{
+       BUILTIN_SOURCE = -1,
+       GENERATED_SOURCE = 0
 };
 
 struct NodeVisitor;
 
 struct Node
 {
-protected:
-       Node() { }
-       Node(const Node &) { }
+       int source;
+       unsigned line;
+
+       Node(): source(GENERATED_SOURCE), line(1) { }
+       Node(const Node &n): source(n.source), line(n.line) { }
 private:
        Node &operator=(const Node &);
 public:
@@ -90,15 +103,11 @@ class NodeArray: public NodeContainer<std::vector<RefPtr<T> > >
 
 struct StructDeclaration;
 struct VariableDeclaration;
+struct InterfaceBlock;
 struct FunctionDeclaration;
 
 struct Statement: Node
 {
-       unsigned source;
-       unsigned line;
-
-       Statement();
-
        virtual Statement *clone() const = 0;
 };
 
@@ -106,8 +115,7 @@ struct Block: Node
 {
        NodeList<Statement> body;
        bool use_braces;
-       bool anonymous;
-       std::map<std::string, StructDeclaration *> types;
+
        std::map<std::string, VariableDeclaration *> variables;
        Block *parent;
 
@@ -120,6 +128,10 @@ struct Block: Node
 
 struct Expression: Node
 {
+       const Operator *oper;
+
+       Expression();
+
        virtual Expression *clone() const = 0;
 };
 
@@ -142,6 +154,7 @@ struct ParenthesizedExpression: Expression
 struct VariableReference: Expression
 {
        std::string name;
+
        VariableDeclaration *declaration;
 
        VariableReference();
@@ -151,10 +164,24 @@ struct VariableReference: Expression
        virtual void visit(NodeVisitor &);
 };
 
+struct InterfaceBlockReference: Expression
+{
+       std::string name;
+
+       InterfaceBlock *declaration;
+
+       InterfaceBlockReference();
+       InterfaceBlockReference(const InterfaceBlockReference &);
+
+       virtual InterfaceBlockReference *clone() const { return new InterfaceBlockReference(*this); }
+       virtual void visit(NodeVisitor &);
+};
+
 struct MemberAccess: Expression
 {
        NodePtr<Expression> left;
        std::string member;
+
        VariableDeclaration *declaration;
 
        MemberAccess();
@@ -166,11 +193,7 @@ struct MemberAccess: Expression
 
 struct UnaryExpression: Expression
 {
-       std::string oper;
        NodePtr<Expression> expression;
-       bool prefix;
-
-       UnaryExpression();
 
        virtual UnaryExpression *clone() const { return new UnaryExpression(*this); }
        virtual void visit(NodeVisitor &);
@@ -179,9 +202,7 @@ struct UnaryExpression: Expression
 struct BinaryExpression: Expression
 {
        NodePtr<Expression> left;
-       std::string oper;
        NodePtr<Expression> right;
-       std::string after;
 
        virtual BinaryExpression *clone() const { return new BinaryExpression(*this); }
        virtual void visit(NodeVisitor &);
@@ -190,6 +211,7 @@ struct BinaryExpression: Expression
 struct Assignment: BinaryExpression
 {
        bool self_referencing;
+
        VariableDeclaration *target_declaration;
 
        Assignment();
@@ -202,10 +224,11 @@ struct Assignment: BinaryExpression
 struct FunctionCall: Expression
 {
        std::string name;
-       FunctionDeclaration *declaration;
        bool constructor;
        NodeArray<Expression> arguments;
 
+       FunctionDeclaration *declaration;
+
        FunctionCall();
        FunctionCall(const FunctionCall &);
 
@@ -242,9 +265,9 @@ struct Layout: Node
 {
        struct Qualifier
        {
-               // TODO the standard calls this name, not identifier
-               std::string identifier;
-               std::string value;
+               std::string name;
+               bool has_value;
+               int value;
        };
 
        std::vector<Qualifier> qualifiers;
@@ -262,9 +285,66 @@ struct InterfaceLayout: Statement
        virtual void visit(NodeVisitor &);
 };
 
-struct StructDeclaration: Statement
+struct TypeDeclaration: Statement
 {
        std::string name;
+
+       virtual TypeDeclaration *clone() const = 0;
+};
+
+struct BasicTypeDeclaration: TypeDeclaration
+{
+       enum Kind
+       {
+               ALIAS,
+               VOID,
+               BOOL,
+               INT,
+               FLOAT,
+               VECTOR,
+               MATRIX,
+               ARRAY
+       };
+
+       Kind kind;
+       unsigned size;
+       std::string base;
+
+       TypeDeclaration *base_type;
+
+       BasicTypeDeclaration();
+       BasicTypeDeclaration(const BasicTypeDeclaration &);
+
+       virtual BasicTypeDeclaration *clone() const { return new BasicTypeDeclaration(*this); }
+       virtual void visit(NodeVisitor &);
+};
+
+struct ImageTypeDeclaration: TypeDeclaration
+{
+       enum Dimensions
+       {
+               ONE = 1,
+               TWO,
+               THREE,
+               CUBE
+       };
+
+       Dimensions dimensions;
+       bool array;
+       bool sampled;
+       bool shadow;
+       std::string base;
+
+       TypeDeclaration *base_type;
+
+       ImageTypeDeclaration();
+
+       virtual ImageTypeDeclaration *clone() const { return new ImageTypeDeclaration(*this); }
+       virtual void visit(NodeVisitor &);
+};
+
+struct StructDeclaration: TypeDeclaration
+{
        Block members;
 
        StructDeclaration();
@@ -275,22 +355,24 @@ struct StructDeclaration: Statement
 
 struct VariableDeclaration: Statement
 {
+       NodePtr<Layout> layout;
        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;
        NodePtr<Expression> array_size;
        NodePtr<Expression> init_expression;
+
+       TypeDeclaration *type_declaration;
        VariableDeclaration *linked_declaration;
-       NodePtr<Layout> layout;
 
        VariableDeclaration();
        VariableDeclaration(const VariableDeclaration &);
+       ~VariableDeclaration();
 
        virtual VariableDeclaration *clone() const { return new VariableDeclaration(*this); }
        virtual void visit(NodeVisitor &);
@@ -304,7 +386,11 @@ struct InterfaceBlock: Statement
        std::string instance_name;
        bool array;
 
+       InterfaceBlock *linked_block;
+
        InterfaceBlock();
+       InterfaceBlock(const InterfaceBlock &);
+       ~InterfaceBlock();
 
        virtual InterfaceBlock *clone() const { return new InterfaceBlock(*this); }
        virtual void visit(NodeVisitor &);
@@ -315,9 +401,10 @@ struct FunctionDeclaration: Statement
        std::string return_type;
        std::string name;
        NodeArray<VariableDeclaration> parameters;
-       FunctionDeclaration *definition;
        Block body;
 
+       FunctionDeclaration *definition;
+
        FunctionDeclaration();
        FunctionDeclaration(const FunctionDeclaration &);
 
@@ -337,7 +424,7 @@ struct Conditional: Statement
 
 struct Iteration: Statement
 {
-       NodePtr<Node> init_statement;
+       NodePtr<Statement> init_statement;
        NodePtr<Expression> condition;
        NodePtr<Expression> loop_expression;
        Block body;
@@ -383,10 +470,12 @@ struct Stage
        Type type;
        Stage *previous;
        Block content;
-       std::map<std::string, VariableDeclaration *> in_variables;
-       std::map<std::string, VariableDeclaration *> out_variables;
+       std::map<std::string, TypeDeclaration *> types;
+       std::map<std::string, InterfaceBlock *> interface_blocks;
+       std::map<std::string, FunctionDeclaration *> functions;
        std::map<std::string, unsigned> locations;
        Features required_features;
+       std::vector<Diagnostic> diagnostics;
 
        Stage(Type);