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 VariableDeclaration *declaration = 0;
223 std::uint8_t chain_len = 0;
224 std::uint8_t chain[7] = { };
226 Target(VariableDeclaration *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;
367 bool sampled = false;
369 bool multisample = false;
373 TypeDeclaration *base_type = 0;
374 ImageTypeDeclaration *base_image = 0;
376 virtual ImageTypeDeclaration *clone() const { return new ImageTypeDeclaration(*this); }
377 virtual void visit(NodeVisitor &);
380 struct StructDeclaration: TypeDeclaration
383 std::string block_name;
384 bool extended_alignment = false;
386 VariableDeclaration *block_declaration = 0;
389 StructDeclaration(const StructDeclaration &);
391 virtual StructDeclaration *clone() const { return new StructDeclaration(*this); }
392 virtual void visit(NodeVisitor &);
395 struct VariableDeclaration: Statement
397 NodePtr<Layout> layout;
398 bool constant = false;
399 std::string sampling;
400 std::string interpolation;
401 std::string interface;
402 std::string precision;
406 NodePtr<Expression> array_size;
407 NodePtr<Expression> init_expression;
409 TypeDeclaration *type_declaration = 0;
410 StructDeclaration *block_declaration = 0;
411 VariableDeclaration *linked_declaration = 0;
413 VariableDeclaration() = default;
414 VariableDeclaration(const VariableDeclaration &);
415 ~VariableDeclaration();
417 virtual VariableDeclaration *clone() const { return new VariableDeclaration(*this); }
418 virtual void visit(NodeVisitor &);
421 struct FunctionDeclaration: Statement
423 std::string return_type;
425 NodeArray<VariableDeclaration> parameters;
430 std::string signature;
431 FunctionDeclaration *definition = 0;
432 TypeDeclaration *return_type_declaration = 0;
434 FunctionDeclaration() = default;
435 FunctionDeclaration(const FunctionDeclaration &);
437 virtual FunctionDeclaration *clone() const { return new FunctionDeclaration(*this); }
438 virtual void visit(NodeVisitor &);
441 struct Conditional: Statement
443 NodePtr<Expression> condition;
447 virtual Conditional *clone() const { return new Conditional(*this); }
448 virtual void visit(NodeVisitor &);
451 struct Iteration: Statement
453 NodePtr<Statement> init_statement;
454 NodePtr<Expression> condition;
455 NodePtr<Expression> loop_expression;
458 virtual Iteration *clone() const { return new Iteration(*this); }
459 virtual void visit(NodeVisitor &);
462 struct Passthrough: Statement
464 NodePtr<Expression> subscript;
466 virtual Passthrough *clone() const { return new Passthrough(*this); }
467 virtual void visit(NodeVisitor &);
470 struct Return: Statement
472 NodePtr<Expression> expression;
474 virtual Return *clone() const { return new Return(*this); }
475 virtual void visit(NodeVisitor &);
478 struct Jump: Statement
482 virtual Jump *clone() const { return new Jump(*this); }
483 virtual void visit(NodeVisitor &);
502 std::map<std::string, TypeDeclaration *> types;
503 std::map<std::string, VariableDeclaration *> interface_blocks;
504 std::map<std::string, FunctionDeclaration *> functions;
505 std::map<std::string, unsigned> locations;
506 std::map<std::string, unsigned> texture_bindings;
507 std::map<std::string, unsigned> uniform_block_bindings;
508 unsigned n_clip_distances = 0;
509 Features required_features;
510 std::vector<Diagnostic> diagnostics;
514 static const char *get_stage_name(Type);
519 SourceMap source_map;
521 std::list<Stage> stages;
526 std::string get_unused_variable_name(const Block &, const std::string &);
527 TypeDeclaration *get_ultimate_base_type(TypeDeclaration *);
528 bool has_layout_qualifier(const Layout *, const std::string &);
529 int get_layout_value(const Layout *, const std::string &, int = -1);
530 void add_layout_qualifier(RefPtr<Layout> &, const Layout::Qualifier &);
531 void add_to_chain(Assignment::Target &, Assignment::Target::ChainType, unsigned);
532 bool targets_overlap(const Assignment::Target &, const Assignment::Target &);
538 #pragma pop_macro("interface")