X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fglsl%2Fsyntax.h;h=7fa116b717dd388e5a2acba6001dc34c74226bab;hp=a2630ef06b52111b407daa8dd8d1c73ea7583260;hb=3a1b9cbe2441ae670a97541dc8ccb0a2860c8302;hpb=c1b0303f65ee966a973197cbdbf177c571478674 diff --git a/source/glsl/syntax.h b/source/glsl/syntax.h index a2630ef0..7fa116b7 100644 --- a/source/glsl/syntax.h +++ b/source/glsl/syntax.h @@ -3,11 +3,15 @@ #include #include +#include #include #include +#include #include -#include "extension.h" -#include "uniform.h" +#include +#include "features.h" +#include "glsl_error.h" +#include "sourcemap.h" #pragma push_macro("interface") #undef interface @@ -16,13 +20,51 @@ namespace Msp { namespace GL { namespace SL { +struct Operator +{ + enum Type + { + NO_OPERATOR, + BINARY, + PREFIX, + POSTFIX, + TERNARY + }; + + enum Associativity + { + LEFT_TO_RIGHT, + RIGHT_TO_LEFT, + ASSOCIATIVE + }; + + char token[4]; + char token2[2]; + std::uint8_t precedence; + Type type; + Associativity assoc; + + static const Operator operators[]; + + static const Operator &get_operator(const std::string &, Type); +}; + +enum +{ + INTERNAL_SOURCE = -2, + 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: @@ -54,6 +96,9 @@ class NodeContainer: public C public: NodeContainer() { } NodeContainer(const NodeContainer &); + + void push_back_nocopy(const typename C::value_type &v) + { C::push_back(0); C::back() = v; } }; template @@ -64,17 +109,13 @@ template class NodeArray: public NodeContainer > > { }; -struct StructDeclaration; +struct TypeDeclaration; struct VariableDeclaration; +struct InterfaceBlock; struct FunctionDeclaration; struct Statement: Node { - unsigned source; - unsigned line; - - Statement(); - virtual Statement *clone() const = 0; }; @@ -82,10 +123,12 @@ struct Block: Node { NodeList body; bool use_braces; - std::map types; + std::map variables; + Block *parent; Block(); + Block(const Block &); virtual Block *clone() const { return new Block(*this); } virtual void visit(NodeVisitor &); @@ -93,33 +136,48 @@ struct Block: Node struct Expression: Node { + const Operator *oper; + + TypeDeclaration *type; + bool lvalue; + + Expression(); + virtual Expression *clone() const = 0; }; struct Literal: Expression { std::string token; + Variant value; virtual Literal *clone() const { return new Literal(*this); } virtual void visit(NodeVisitor &); }; -struct ParenthesizedExpression: Expression +struct VariableReference: Expression { - NodePtr expression; + std::string name; - virtual ParenthesizedExpression *clone() const { return new ParenthesizedExpression(*this); } + VariableDeclaration *declaration; + + VariableReference(); + VariableReference(const VariableReference &); + + virtual VariableReference *clone() const { return new VariableReference(*this); } virtual void visit(NodeVisitor &); }; -struct VariableReference: Expression +struct InterfaceBlockReference: Expression { std::string name; - VariableDeclaration *declaration; - VariableReference(); + InterfaceBlock *declaration; - virtual VariableReference *clone() const { return new VariableReference(*this); } + InterfaceBlockReference(); + InterfaceBlockReference(const InterfaceBlockReference &); + + virtual InterfaceBlockReference *clone() const { return new InterfaceBlockReference(*this); } virtual void visit(NodeVisitor &); }; @@ -127,19 +185,33 @@ struct MemberAccess: Expression { NodePtr left; std::string member; + VariableDeclaration *declaration; + int index; + + MemberAccess(); + MemberAccess(const MemberAccess &); virtual MemberAccess *clone() const { return new MemberAccess(*this); } virtual void visit(NodeVisitor &); }; +struct Swizzle: Expression +{ + NodePtr left; + std::string component_group; + unsigned count; + std::uint8_t components[4]; + + Swizzle(); + + virtual Swizzle *clone() const { return new Swizzle(*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 &); @@ -148,9 +220,7 @@ struct UnaryExpression: Expression 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 &); @@ -158,23 +228,55 @@ struct BinaryExpression: Expression struct Assignment: BinaryExpression { + struct Target + { + enum ChainType + { + MEMBER = 0x40, + SWIZZLE = 0x80, + ARRAY = 0xC0 + }; + + Statement *declaration; + std::uint8_t chain_len; + std::uint8_t chain[7]; + + Target(Statement * = 0); + + bool operator<(const Target &) const; + }; + bool self_referencing; - VariableDeclaration *target_declaration; + + Target target; Assignment(); + Assignment(const Assignment &); virtual Assignment *clone() const { return new Assignment(*this); } virtual void visit(NodeVisitor &); }; +struct TernaryExpression: Expression +{ + NodePtr condition; + NodePtr true_expr; + NodePtr false_expr; + + virtual TernaryExpression *clone() const { return new TernaryExpression(*this); } + virtual void visit(NodeVisitor &); +}; + struct FunctionCall: Expression { std::string name; - FunctionDeclaration *declaration; bool constructor; NodeArray arguments; + FunctionDeclaration *declaration; + FunctionCall(); + FunctionCall(const FunctionCall &); virtual FunctionCall *clone() const { return new FunctionCall(*this); } virtual void visit(NodeVisitor &); @@ -209,9 +311,12 @@ 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; + + Qualifier(const std::string &n = std::string()): name(n), has_value(false), value(0) { } + Qualifier(const std::string &n, int v): name(n), has_value(true), value(v) { } }; std::vector qualifiers; @@ -229,12 +334,74 @@ 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; + bool sign; + 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; + InterfaceBlock *interface_block; + StructDeclaration(); + StructDeclaration(const StructDeclaration &); + ~StructDeclaration(); virtual StructDeclaration *clone() const { return new StructDeclaration(*this); } virtual void visit(NodeVisitor &); @@ -242,21 +409,24 @@ struct StructDeclaration: Statement struct VariableDeclaration: Statement { + NodePtr 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 array_size; NodePtr init_expression; + + TypeDeclaration *type_declaration; VariableDeclaration *linked_declaration; - NodePtr layout; VariableDeclaration(); + VariableDeclaration(const VariableDeclaration &); + ~VariableDeclaration(); virtual VariableDeclaration *clone() const { return new VariableDeclaration(*this); } virtual void visit(NodeVisitor &); @@ -264,13 +434,22 @@ struct VariableDeclaration: Statement struct InterfaceBlock: Statement { + NodePtr layout; std::string interface; - std::string name; - Block members; + std::string block_name; + NodePtr members; std::string instance_name; bool array; + /* An interface block's ultimate base type is always a struct. The + immediate type may be either that same struct or an array of it. */ + TypeDeclaration *type_declaration; + StructDeclaration *struct_declaration; + InterfaceBlock *linked_block; + InterfaceBlock(); + InterfaceBlock(const InterfaceBlock &); + ~InterfaceBlock(); virtual InterfaceBlock *clone() const { return new InterfaceBlock(*this); } virtual void visit(NodeVisitor &); @@ -281,9 +460,14 @@ struct FunctionDeclaration: Statement std::string return_type; std::string name; NodeArray parameters; - FunctionDeclaration *definition; + bool virtua; + bool overrd; Block body; + std::string signature; + FunctionDeclaration *definition; + TypeDeclaration *return_type_declaration; + FunctionDeclaration(); FunctionDeclaration(const FunctionDeclaration &); @@ -303,7 +487,7 @@ struct Conditional: Statement struct Iteration: Statement { - NodePtr init_statement; + NodePtr init_statement; NodePtr condition; NodePtr loop_expression; Block body; @@ -336,86 +520,47 @@ struct Jump: Statement virtual void visit(NodeVisitor &); }; -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 &) { } -}; - -struct TraversingVisitor: NodeVisitor -{ - 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 Stage { - StageType type; + enum Type + { + SHARED, + VERTEX, + GEOMETRY, + FRAGMENT + }; + + Type type; Stage *previous; Block content; - std::map in_variables; - std::map out_variables; + std::map types; + std::map interface_blocks; + std::map functions; std::map locations; - Version required_version; - std::vector required_extensions; + std::map texture_bindings; + std::map uniform_block_bindings; + Features required_features; + std::vector diagnostics; - Stage(StageType); + Stage(Type); + + static const char *get_stage_name(Type); }; struct Module { + SourceMap source_map; Stage shared; std::list stages; Module(); }; +std::string get_unused_variable_name(const Block &, const std::string &); + +int get_layout_value(const Layout &, const std::string &, int = -1); +void add_to_chain(Assignment::Target &, Assignment::Target::ChainType, unsigned); + } // namespace SL } // namespace GL } // namespace Msp