#include <list>
#include <map>
+#include <set>
#include <string>
#include <vector>
+#include <cstdint>
#include <msp/core/refptr.h>
-#include "extension.h"
-#include "uniform.h"
+#include <msp/core/variant.h>
+#include "features.h"
+#include "glsl_error.h"
+#include "sourcemap.h"
#pragma push_macro("interface")
#undef interface
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;
+ unsigned line;
+
+ Node(): source(GENERATED_SOURCE), line(1) { }
+ Node(const Node &n): source(n.source), line(n.line) { }
private:
Node &operator=(const Node &);
public:
public:
NodeContainer() { }
NodeContainer(const NodeContainer &);
+
+ void push_back_nocopy(const typename C::value_type &v)
+ { C::push_back(0); C::back() = v; }
};
template<typename T>
class NodeArray: public NodeContainer<std::vector<RefPtr<T> > >
{ };
-struct StructDeclaration;
+struct TypeDeclaration;
struct VariableDeclaration;
+struct InterfaceBlock;
struct FunctionDeclaration;
struct Statement: Node
{
- unsigned source;
- unsigned line;
-
- Statement();
-
virtual Statement *clone() const = 0;
};
{
NodeList<Statement> body;
bool use_braces;
- std::map<std::string, StructDeclaration *> types;
+
std::map<std::string, VariableDeclaration *> variables;
+ Block *parent;
Block();
+ Block(const Block &);
virtual Block *clone() const { return new Block(*this); }
virtual void visit(NodeVisitor &);
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> 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 &);
};
{
NodePtr<Expression> 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<Expression> 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> expression;
- bool prefix;
-
- UnaryExpression();
virtual UnaryExpression *clone() const { return new UnaryExpression(*this); }
virtual void visit(NodeVisitor &);
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 &);
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<Expression> condition;
+ NodePtr<Expression> true_expr;
+ NodePtr<Expression> 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<Expression> arguments;
+ FunctionDeclaration *declaration;
+
FunctionCall();
+ FunctionCall(const FunctionCall &);
virtual FunctionCall *clone() const { return new FunctionCall(*this); }
virtual void visit(NodeVisitor &);
{
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<Qualifier> qualifiers;
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 &);
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 &);
struct InterfaceBlock: Statement
{
+ NodePtr<Layout> layout;
std::string interface;
- std::string name;
- Block members;
+ std::string block_name;
+ NodePtr<Block> 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 &);
std::string return_type;
std::string name;
NodeArray<VariableDeclaration> parameters;
- FunctionDeclaration *definition;
+ bool virtua;
+ bool overrd;
Block body;
+ std::string signature;
+ FunctionDeclaration *definition;
+ TypeDeclaration *return_type_declaration;
+
FunctionDeclaration();
FunctionDeclaration(const FunctionDeclaration &);
struct Iteration: Statement
{
- NodePtr<Node> init_statement;
+ NodePtr<Statement> init_statement;
NodePtr<Expression> condition;
NodePtr<Expression> loop_expression;
Block body;
virtual void visit(NodeVisitor &);
};
-enum StageType
-{
- SHARED,
- VERTEX,
- GEOMETRY,
- FRAGMENT
-};
-
struct Stage
{
- StageType type;
+ enum Type
+ {
+ SHARED,
+ VERTEX,
+ GEOMETRY,
+ FRAGMENT
+ };
+
+ 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;
- Version required_version;
- std::vector<const Extension *> required_extensions;
+ std::map<std::string, unsigned> texture_bindings;
+ std::map<std::string, unsigned> uniform_block_bindings;
+ Features required_features;
+ std::vector<Diagnostic> diagnostics;
- Stage(StageType);
+ Stage(Type);
+
+ static const char *get_stage_name(Type);
};
struct Module
{
+ SourceMap source_map;
Stage shared;
std::list<Stage> 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