X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fprogramsyntax.h;h=590429519118614f6a59a83796da97f50693ff9c;hp=0013d82a97a7cd8c890e3f0a4456ae5dd4875400;hb=56beca9d8b4f7b4edac81411d31e24df88e84ac3;hpb=6e6ee01b68056b23c6709d7f60396710dd7623b9 diff --git a/source/programsyntax.h b/source/programsyntax.h index 0013d82a..59042951 100644 --- a/source/programsyntax.h +++ b/source/programsyntax.h @@ -1,9 +1,16 @@ #ifndef MSP_GL_PROGRAMSYNTAX_H_ #define MSP_GL_PROGRAMSYNTAX_H_ +#include #include #include #include +#include +#include "extension.h" +#include "uniform.h" + +#pragma push_macro("interface") +#undef interface namespace Msp { namespace GL { @@ -13,33 +20,184 @@ struct NodeVisitor; struct Node { +private: + Node &operator=(const Node &); +public: virtual ~Node() { } + virtual Node *clone() const = 0; virtual void visit(NodeVisitor &) = 0; }; +template +class NodePtr: public RefPtr +{ +public: + NodePtr() { } + NodePtr(T *p): RefPtr(p) { } + NodePtr(const NodePtr &p): RefPtr(p ? p->clone() : 0) { } + + template + NodePtr(const RefPtr &p): RefPtr(p) { } + + template + NodePtr(const NodePtr &p): RefPtr(p ? p->clone() : 0) { } +}; + +template +class NodeContainer: public C +{ +public: + NodeContainer() { } + NodeContainer(const NodeContainer &); +}; + +template +class NodeList: public NodeContainer > > +{ }; + +template +class NodeArray: public NodeContainer > > +{ }; + +struct StructDeclaration; +struct VariableDeclaration; +struct FunctionDeclaration; + +struct Statement: Node +{ + unsigned source; + unsigned line; + + Statement(); + + virtual Statement *clone() const = 0; +}; + struct Block: Node { - std::vector body; + NodeList body; bool use_braces; + std::map types; + std::map variables; Block(); - virtual ~Block(); + virtual Block *clone() const { return new Block(*this); } + virtual void visit(NodeVisitor &); +}; + +struct Expression: Node +{ + virtual Expression *clone() const = 0; +}; + +struct Literal: Expression +{ + std::string token; + + virtual Literal *clone() const { return new Literal(*this); } + virtual void visit(NodeVisitor &); +}; + +struct ParenthesizedExpression: Expression +{ + NodePtr expression; + + virtual ParenthesizedExpression *clone() const { return new ParenthesizedExpression(*this); } + virtual void visit(NodeVisitor &); +}; + +struct VariableReference: Expression +{ + std::string name; + VariableDeclaration *declaration; + + VariableReference(); + + virtual VariableReference *clone() const { return new VariableReference(*this); } + virtual void visit(NodeVisitor &); +}; + +struct MemberAccess: Expression +{ + NodePtr left; + std::string member; + VariableDeclaration *declaration; + + virtual MemberAccess *clone() const { return new MemberAccess(*this); } + virtual void visit(NodeVisitor &); +}; + +struct UnaryExpression: Expression +{ + std::string oper; + NodePtr expression; + bool prefix; + + UnaryExpression(); + + virtual UnaryExpression *clone() const { return new UnaryExpression(*this); } + virtual void visit(NodeVisitor &); +}; + +struct BinaryExpression: Expression +{ + NodePtr left; + std::string oper; + NodePtr right; + std::string after; + + 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; + NodeArray arguments; + + FunctionCall(); + + virtual FunctionCall *clone() const { return new FunctionCall(*this); } + virtual void visit(NodeVisitor &); +}; + +struct ExpressionStatement: Statement +{ + NodePtr expression; + + virtual ExpressionStatement *clone() const { return new ExpressionStatement(*this); } virtual void visit(NodeVisitor &); }; -struct Expression +struct Import: Statement { - std::vector tokens; + std::string module; - bool empty() const { return tokens.empty(); } + virtual Import *clone() const { return new Import(*this); } + virtual void visit(NodeVisitor &); }; -struct ExpressionStatement: Node +struct Precision: Statement { - Expression expression; + std::string precision; + std::string type; + virtual Precision *clone() const { return new Precision(*this); } virtual void visit(NodeVisitor &); }; @@ -47,93 +205,130 @@ struct Layout: Node { struct Qualifier { + // TODO the standard calls this name, not identifier std::string identifier; std::string value; }; std::vector qualifiers; + + virtual Layout *clone() const { return new Layout(*this); } + virtual void visit(NodeVisitor &); +}; + +struct InterfaceLayout: Statement +{ std::string interface; + Layout layout; + virtual InterfaceLayout *clone() const { return new InterfaceLayout(*this); } virtual void visit(NodeVisitor &); }; -struct StructDeclaration: Node +struct StructDeclaration: Statement { std::string name; Block members; StructDeclaration(); + virtual StructDeclaration *clone() const { return new StructDeclaration(*this); } 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; - Expression array_size; - Expression init_expression; + NodePtr array_size; + NodePtr init_expression; + VariableDeclaration *linked_declaration; + NodePtr layout; VariableDeclaration(); + virtual VariableDeclaration *clone() const { return new VariableDeclaration(*this); } virtual void visit(NodeVisitor &); }; -struct InterfaceBlock: Node +struct InterfaceBlock: Statement { std::string interface; std::string name; Block members; + std::string instance_name; + bool array; InterfaceBlock(); + virtual InterfaceBlock *clone() const { return new InterfaceBlock(*this); } virtual void visit(NodeVisitor &); }; -struct FunctionDeclaration: Node +struct FunctionDeclaration: Statement { std::string return_type; std::string name; - std::vector parameters; - bool definition; + NodeArray parameters; + FunctionDeclaration *definition; Block body; FunctionDeclaration(); - ~FunctionDeclaration(); + FunctionDeclaration(const FunctionDeclaration &); + virtual FunctionDeclaration *clone() const { return new FunctionDeclaration(*this); } virtual void visit(NodeVisitor &); }; -struct Conditional: Node +struct Conditional: Statement { - Expression condition; + NodePtr condition; Block body; Block else_body; + virtual Conditional *clone() const { return new Conditional(*this); } virtual void visit(NodeVisitor &); }; -struct Iteration: Node +struct Iteration: Statement { - Node *init_statement; - Expression condition; - Expression loop_expression; + NodePtr init_statement; + NodePtr condition; + NodePtr loop_expression; Block body; - Iteration(); - virtual ~Iteration(); + virtual Iteration *clone() const { return new Iteration(*this); } + virtual void visit(NodeVisitor &); +}; +struct Passthrough: Statement +{ + NodePtr subscript; + + virtual Passthrough *clone() const { return new Passthrough(*this); } virtual void visit(NodeVisitor &); }; -struct Return: Node +struct Return: Statement { - Expression expression; + NodePtr 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 &); }; @@ -142,41 +337,77 @@ struct NodeVisitor virtual ~NodeVisitor() { } virtual void visit(Block &) { } + virtual void visit(Literal &) { } + virtual void visit(ParenthesizedExpression &) { } + virtual void visit(VariableReference &) { } + 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(Precision &) { } virtual void visit(Layout &) { } + virtual void visit(InterfaceLayout &) { } virtual void visit(StructDeclaration &) { } virtual void visit(VariableDeclaration &) { } virtual void visit(InterfaceBlock &) { } virtual void visit(FunctionDeclaration &) { } virtual void visit(Conditional &) { } virtual void visit(Iteration &) { } + virtual void visit(Passthrough &) { } virtual void visit(Return &) { } + virtual void visit(Jump &) { } }; -enum ContextType +struct TraversingVisitor: NodeVisitor { - GLOBAL, + using NodeVisitor::visit; + virtual void visit(Block &); + virtual void visit(ParenthesizedExpression &); + virtual void visit(MemberAccess &); + virtual void visit(UnaryExpression &); + 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 &); + virtual void visit(FunctionDeclaration &); + virtual void visit(Conditional &); + virtual void visit(Iteration &); + virtual void visit(Passthrough &); + virtual void visit(Return &); +}; + +enum StageType +{ + SHARED, VERTEX, GEOMETRY, FRAGMENT }; -struct Context +struct Stage { - ContextType type; - bool present; + StageType type; + Stage *previous; ProgramSyntax::Block content; + std::map in_variables; + std::map out_variables; + std::map locations; + Version required_version; + std::vector required_extensions; - Context(ContextType); + Stage(StageType); }; struct Module { - Context global_context; - Context vertex_context; - Context geometry_context; - Context fragment_context; - std::map structs; + Stage shared; + std::list stages; Module(); }; @@ -185,4 +416,6 @@ struct Module } // namespace GL } // namespace Msp +#pragma pop_macro("interface") + #endif