1 #ifndef MSP_GL_SL_SYNTAX_H_
2 #define MSP_GL_SL_SYNTAX_H_
10 #include <msp/core/refptr.h>
11 #include <msp/core/variant.h>
13 #include "glsl_error.h"
14 #include "sourcemap.h"
16 #pragma push_macro("interface")
43 std::uint8_t precedence;
47 static const Operator operators[];
49 static const Operator &get_operator(const std::string &, Type);
63 int source = GENERATED_SOURCE;
67 Node(const Node &) = default;
69 Node &operator=(const Node &);
71 virtual ~Node() = default;
73 virtual Node *clone() const = 0;
74 virtual void visit(NodeVisitor &) = 0;
78 class NodePtr: public RefPtr<T>
82 NodePtr(T *p): RefPtr<T>(p) { }
83 NodePtr(const NodePtr &p): RefPtr<T>(p ? p->clone() : 0) { }
84 NodePtr &operator=(const NodePtr &p) = default;
87 NodePtr(const RefPtr<U> &p): RefPtr<T>(p) { }
90 NodePtr(const NodePtr<U> &p): RefPtr<T>(p ? p->clone() : 0) { }
94 class NodeContainer: public C
97 NodeContainer() = default;
98 NodeContainer(const NodeContainer &);
100 void push_back_nocopy(const typename C::value_type &v)
101 { C::push_back(0); C::back() = v; }
105 class NodeList: public NodeContainer<std::list<RefPtr<T> > >
109 class NodeArray: public NodeContainer<std::vector<RefPtr<T> > >
112 struct TypeDeclaration;
113 struct VariableDeclaration;
114 struct FunctionDeclaration;
116 struct Statement: Node
118 virtual Statement *clone() const = 0;
123 NodeList<Statement> body;
124 bool use_braces = false;
126 std::map<std::string, VariableDeclaration *> variables;
130 Block(const Block &);
132 virtual Block *clone() const { return new Block(*this); }
133 virtual void visit(NodeVisitor &);
136 struct Expression: Node
138 const Operator *oper = 0;
140 TypeDeclaration *type = 0;
143 virtual Expression *clone() const = 0;
146 struct Literal: Expression
151 virtual Literal *clone() const { return new Literal(*this); }
152 virtual void visit(NodeVisitor &);
155 struct VariableReference: Expression
159 VariableDeclaration *declaration = 0;
161 VariableReference() = default;
162 VariableReference(const VariableReference &);
164 virtual VariableReference *clone() const { return new VariableReference(*this); }
165 virtual void visit(NodeVisitor &);
168 struct MemberAccess: Expression
170 NodePtr<Expression> left;
173 VariableDeclaration *declaration = 0;
176 MemberAccess() = default;
177 MemberAccess(const MemberAccess &);
179 virtual MemberAccess *clone() const { return new MemberAccess(*this); }
180 virtual void visit(NodeVisitor &);
183 struct Swizzle: Expression
185 NodePtr<Expression> left;
186 std::string component_group;
188 std::uint8_t components[4] = { 0, 0, 0, 0 };
190 virtual Swizzle *clone() const { return new Swizzle(*this); }
191 virtual void visit(NodeVisitor &);
194 struct UnaryExpression: Expression
196 NodePtr<Expression> expression;
198 virtual UnaryExpression *clone() const { return new UnaryExpression(*this); }
199 virtual void visit(NodeVisitor &);
202 struct BinaryExpression: Expression
204 NodePtr<Expression> left;
205 NodePtr<Expression> right;
207 virtual BinaryExpression *clone() const { return new BinaryExpression(*this); }
208 virtual void visit(NodeVisitor &);
211 struct Assignment: BinaryExpression
222 Statement *declaration = 0;
223 std::uint8_t chain_len = 0;
224 std::uint8_t chain[7] = { };
226 Target(Statement *d = 0): declaration(d) { }
228 bool operator<(const Target &) const;
231 bool self_referencing = false;
235 Assignment() = default;
236 Assignment(const Assignment &);
238 virtual Assignment *clone() const { return new Assignment(*this); }
239 virtual void visit(NodeVisitor &);
242 struct TernaryExpression: Expression
244 NodePtr<Expression> condition;
245 NodePtr<Expression> true_expr;
246 NodePtr<Expression> false_expr;
248 virtual TernaryExpression *clone() const { return new TernaryExpression(*this); }
249 virtual void visit(NodeVisitor &);
252 struct FunctionCall: Expression
255 bool constructor = false;
256 NodeArray<Expression> arguments;
258 FunctionDeclaration *declaration = 0;
260 FunctionCall() = default;
261 FunctionCall(const FunctionCall &);
263 virtual FunctionCall *clone() const { return new FunctionCall(*this); }
264 virtual void visit(NodeVisitor &);
267 struct ExpressionStatement: Statement
269 NodePtr<Expression> expression;
271 virtual ExpressionStatement *clone() const { return new ExpressionStatement(*this); }
272 virtual void visit(NodeVisitor &);
275 struct Import: Statement
279 virtual Import *clone() const { return new Import(*this); }
280 virtual void visit(NodeVisitor &);
283 struct Precision: Statement
285 std::string precision;
288 virtual Precision *clone() const { return new Precision(*this); }
289 virtual void visit(NodeVisitor &);
300 Qualifier(const std::string &n = std::string()): name(n), has_value(false), value(0) { }
301 Qualifier(const std::string &n, int v): name(n), has_value(true), value(v) { }
304 std::vector<Qualifier> qualifiers;
306 virtual Layout *clone() const { return new Layout(*this); }
307 virtual void visit(NodeVisitor &);
310 struct InterfaceLayout: Statement
312 std::string interface;
315 virtual InterfaceLayout *clone() const { return new InterfaceLayout(*this); }
316 virtual void visit(NodeVisitor &);
319 struct TypeDeclaration: Statement
323 virtual TypeDeclaration *clone() const = 0;
326 struct BasicTypeDeclaration: TypeDeclaration
343 bool extended_alignment = false;
346 TypeDeclaration *base_type = 0;
348 BasicTypeDeclaration() = default;
349 BasicTypeDeclaration(const BasicTypeDeclaration &);
351 virtual BasicTypeDeclaration *clone() const { return new BasicTypeDeclaration(*this); }
352 virtual void visit(NodeVisitor &);
355 struct ImageTypeDeclaration: TypeDeclaration
365 Dimensions dimensions = TWO;
371 TypeDeclaration *base_type = 0;
373 virtual ImageTypeDeclaration *clone() const { return new ImageTypeDeclaration(*this); }
374 virtual void visit(NodeVisitor &);
377 struct StructDeclaration: TypeDeclaration
380 std::string block_name;
381 bool extended_alignment = false;
383 VariableDeclaration *block_declaration = 0;
386 StructDeclaration(const StructDeclaration &);
388 virtual StructDeclaration *clone() const { return new StructDeclaration(*this); }
389 virtual void visit(NodeVisitor &);
392 struct VariableDeclaration: Statement
394 NodePtr<Layout> layout;
395 bool constant = false;
396 std::string sampling;
397 std::string interpolation;
398 std::string interface;
399 std::string precision;
403 NodePtr<Expression> array_size;
404 NodePtr<Expression> init_expression;
406 TypeDeclaration *type_declaration = 0;
407 StructDeclaration *block_declaration = 0;
408 VariableDeclaration *linked_declaration = 0;
410 VariableDeclaration() = default;
411 VariableDeclaration(const VariableDeclaration &);
412 ~VariableDeclaration();
414 virtual VariableDeclaration *clone() const { return new VariableDeclaration(*this); }
415 virtual void visit(NodeVisitor &);
418 struct FunctionDeclaration: Statement
420 std::string return_type;
422 NodeArray<VariableDeclaration> parameters;
427 std::string signature;
428 FunctionDeclaration *definition = 0;
429 TypeDeclaration *return_type_declaration = 0;
431 FunctionDeclaration() = default;
432 FunctionDeclaration(const FunctionDeclaration &);
434 virtual FunctionDeclaration *clone() const { return new FunctionDeclaration(*this); }
435 virtual void visit(NodeVisitor &);
438 struct Conditional: Statement
440 NodePtr<Expression> condition;
444 virtual Conditional *clone() const { return new Conditional(*this); }
445 virtual void visit(NodeVisitor &);
448 struct Iteration: Statement
450 NodePtr<Statement> init_statement;
451 NodePtr<Expression> condition;
452 NodePtr<Expression> loop_expression;
455 virtual Iteration *clone() const { return new Iteration(*this); }
456 virtual void visit(NodeVisitor &);
459 struct Passthrough: Statement
461 NodePtr<Expression> subscript;
463 virtual Passthrough *clone() const { return new Passthrough(*this); }
464 virtual void visit(NodeVisitor &);
467 struct Return: Statement
469 NodePtr<Expression> expression;
471 virtual Return *clone() const { return new Return(*this); }
472 virtual void visit(NodeVisitor &);
475 struct Jump: Statement
479 virtual Jump *clone() const { return new Jump(*this); }
480 virtual void visit(NodeVisitor &);
496 std::map<std::string, TypeDeclaration *> types;
497 std::map<std::string, VariableDeclaration *> interface_blocks;
498 std::map<std::string, FunctionDeclaration *> functions;
499 std::map<std::string, unsigned> locations;
500 std::map<std::string, unsigned> texture_bindings;
501 std::map<std::string, unsigned> uniform_block_bindings;
502 unsigned n_clip_distances = 0;
503 Features required_features;
504 std::vector<Diagnostic> diagnostics;
508 static const char *get_stage_name(Type);
513 SourceMap source_map;
515 std::list<Stage> stages;
520 std::string get_unused_variable_name(const Block &, const std::string &);
521 TypeDeclaration *get_ultimate_base_type(TypeDeclaration *);
522 bool has_layout_qualifier(const Layout *, const std::string &);
523 int get_layout_value(const Layout *, const std::string &, int = -1);
524 void add_layout_qualifier(RefPtr<Layout> &, const Layout::Qualifier &);
525 void add_to_chain(Assignment::Target &, Assignment::Target::ChainType, unsigned);
526 bool targets_overlap(const Assignment::Target &, const Assignment::Target &);
532 #pragma pop_macro("interface")