X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fglsl%2Fsyntax.h;h=a3b217d40895f2d6e4ae790abec56082094b6e95;hb=6f06479b765946bdd3174b2c05e7a1dda24c31c2;hp=f7e778b89ef65d417afd1f44751930311c5e94ed;hpb=bd8816692056230c36504dcccd76c6946dff47b1;p=libs%2Fgl.git diff --git a/source/glsl/syntax.h b/source/glsl/syntax.h index f7e778b8..a3b217d4 100644 --- a/source/glsl/syntax.h +++ b/source/glsl/syntax.h @@ -6,8 +6,11 @@ #include #include #include +#include #include +#include #include "features.h" +#include "glsl_error.h" #include "sourcemap.h" #pragma push_macro("interface") @@ -24,34 +27,48 @@ struct Operator NO_OPERATOR, BINARY, PREFIX, - POSTFIX + POSTFIX, + TERNARY }; enum Associativity { LEFT_TO_RIGHT, - RIGHT_TO_LEFT + RIGHT_TO_LEFT, + ASSOCIATIVE }; char token[4]; - unsigned precedence; + 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 = GENERATED_SOURCE; + unsigned line = 1; + + Node() = default; + Node(const Node &) = default; private: Node &operator=(const Node &); public: - virtual ~Node() { } + virtual ~Node() = default; virtual Node *clone() const = 0; virtual void visit(NodeVisitor &) = 0; @@ -61,10 +78,10 @@ template class NodePtr: public RefPtr { public: - NodePtr() { } + NodePtr() = default; NodePtr(T *p): RefPtr(p) { } NodePtr(const NodePtr &p): RefPtr(p ? p->clone() : 0) { } - NodePtr &operator=(const NodePtr &p) { RefPtr::operator=(p); return *this; } + NodePtr &operator=(const NodePtr &p) = default; template NodePtr(const RefPtr &p): RefPtr(p) { } @@ -77,8 +94,11 @@ template class NodeContainer: public C { public: - NodeContainer() { } + NodeContainer() = default; NodeContainer(const NodeContainer &); + + void push_back_nocopy(const typename C::value_type &v) + { C::push_back(0); C::back() = v; } }; template @@ -89,31 +109,25 @@ 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; }; struct Block: Node { NodeList body; - bool use_braces; - std::map types; + bool use_braces = false; + std::map variables; - std::map interfaces; - Block *parent; + Block *parent = 0; - Block(); + Block() = default; Block(const Block &); virtual Block *clone() const { return new Block(*this); } @@ -122,31 +136,30 @@ struct Block: Node struct Expression: Node { + const Operator *oper = 0; + + TypeDeclaration *type = 0; + bool lvalue = false; + 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 -{ - NodePtr expression; - - virtual ParenthesizedExpression *clone() const { return new ParenthesizedExpression(*this); } - virtual void visit(NodeVisitor &); -}; - struct VariableReference: Expression { std::string name; - VariableDeclaration *declaration; - VariableReference(); + VariableDeclaration *declaration = 0; + + VariableReference() = default; VariableReference(const VariableReference &); virtual VariableReference *clone() const { return new VariableReference(*this); } @@ -156,9 +169,10 @@ struct VariableReference: Expression struct InterfaceBlockReference: Expression { std::string name; - InterfaceBlock *declaration; - InterfaceBlockReference(); + InterfaceBlock *declaration = 0; + + InterfaceBlockReference() = default; InterfaceBlockReference(const InterfaceBlockReference &); virtual InterfaceBlockReference *clone() const { return new InterfaceBlockReference(*this); } @@ -169,22 +183,31 @@ struct MemberAccess: Expression { NodePtr left; std::string member; - VariableDeclaration *declaration; - MemberAccess(); + VariableDeclaration *declaration = 0; + int index = -1; + + MemberAccess() = default; 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 = 0; + std::uint8_t components[4] = { 0, 0, 0, 0 }; + + 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 &); @@ -193,9 +216,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 &); @@ -203,24 +224,54 @@ struct BinaryExpression: Expression struct Assignment: BinaryExpression { - bool self_referencing; - VariableDeclaration *target_declaration; + struct Target + { + enum ChainType + { + MEMBER = 0x40, + SWIZZLE = 0x80, + ARRAY = 0xC0 + }; + + Statement *declaration = 0; + std::uint8_t chain_len = 0; + std::uint8_t chain[7] = { }; - Assignment(); + Target(Statement *d = 0): declaration(d) { } + + bool operator<(const Target &) const; + }; + + bool self_referencing = false; + + Target target; + + Assignment() = default; 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; + bool constructor = false; NodeArray arguments; - FunctionCall(); + FunctionDeclaration *declaration = 0; + + FunctionCall() = default; FunctionCall(const FunctionCall &); virtual FunctionCall *clone() const { return new FunctionCall(*this); } @@ -259,6 +310,9 @@ struct Layout: Node 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; @@ -276,12 +330,72 @@ 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 = ALIAS; + unsigned size = 0; + bool sign = true; + std::string base; + + TypeDeclaration *base_type = 0; + + BasicTypeDeclaration() = default; + 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 = TWO; + bool array = false; + bool sampled = true; + bool shadow = false; + std::string base; + + TypeDeclaration *base_type = 0; + + virtual ImageTypeDeclaration *clone() const { return new ImageTypeDeclaration(*this); } + virtual void visit(NodeVisitor &); +}; + +struct StructDeclaration: TypeDeclaration +{ Block members; + InterfaceBlock *interface_block = 0; + StructDeclaration(); + StructDeclaration(const StructDeclaration &); + ~StructDeclaration(); virtual StructDeclaration *clone() const { return new StructDeclaration(*this); } virtual void visit(NodeVisitor &); @@ -289,22 +403,24 @@ struct StructDeclaration: Statement struct VariableDeclaration: Statement { - bool constant; + NodePtr layout; + bool constant = false; std::string sampling; std::string interpolation; std::string interface; std::string precision; std::string type; - StructDeclaration *type_declaration; std::string name; - bool array; + bool array = false; NodePtr array_size; NodePtr init_expression; - VariableDeclaration *linked_declaration; - NodePtr layout; - VariableDeclaration(); + TypeDeclaration *type_declaration = 0; + VariableDeclaration *linked_declaration = 0; + + VariableDeclaration() = default; VariableDeclaration(const VariableDeclaration &); + ~VariableDeclaration(); virtual VariableDeclaration *clone() const { return new VariableDeclaration(*this); } virtual void visit(NodeVisitor &); @@ -312,15 +428,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; - InterfaceBlock *linked_block; + bool array = false; - InterfaceBlock(); + /* 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 = 0; + StructDeclaration *struct_declaration = 0; + InterfaceBlock *linked_block = 0; + + InterfaceBlock() = default; InterfaceBlock(const InterfaceBlock &); + ~InterfaceBlock(); virtual InterfaceBlock *clone() const { return new InterfaceBlock(*this); } virtual void visit(NodeVisitor &); @@ -331,10 +454,15 @@ struct FunctionDeclaration: Statement std::string return_type; std::string name; NodeArray parameters; - FunctionDeclaration *definition; + bool virtua = false; + bool overrd = false; Block body; - FunctionDeclaration(); + std::string signature; + FunctionDeclaration *definition = 0; + TypeDeclaration *return_type_declaration = 0; + + FunctionDeclaration() = default; FunctionDeclaration(const FunctionDeclaration &); virtual FunctionDeclaration *clone() const { return new FunctionDeclaration(*this); } @@ -353,7 +481,7 @@ struct Conditional: Statement struct Iteration: Statement { - NodePtr init_statement; + NodePtr init_statement; NodePtr condition; NodePtr loop_expression; Block body; @@ -399,8 +527,14 @@ struct Stage Type type; Stage *previous; Block content; + std::map types; + std::map interface_blocks; + std::map functions; std::map locations; + std::map texture_bindings; + std::map uniform_block_bindings; Features required_features; + std::vector diagnostics; Stage(Type); @@ -416,6 +550,11 @@ struct Module 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