1 #ifndef MSP_GL_SL_SYNTAX_H_
2 #define MSP_GL_SL_SYNTAX_H_
9 #include <msp/core/inttypes.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")
47 static const Operator operators[];
49 static const Operator &get_operator(const std::string &, Type);
66 Node(): source(GENERATED_SOURCE), line(1) { }
67 Node(const Node &n): source(n.source), line(n.line) { }
69 Node &operator=(const Node &);
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) { RefPtr<T>::operator=(p); return *this; }
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
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 InterfaceBlock;
115 struct FunctionDeclaration;
117 struct Statement: Node
119 virtual Statement *clone() const = 0;
124 NodeList<Statement> body;
127 std::map<std::string, VariableDeclaration *> variables;
131 Block(const Block &);
133 virtual Block *clone() const { return new Block(*this); }
134 virtual void visit(NodeVisitor &);
137 struct Expression: Node
139 const Operator *oper;
141 TypeDeclaration *type;
146 virtual Expression *clone() const = 0;
149 struct Literal: Expression
154 virtual Literal *clone() const { return new Literal(*this); }
155 virtual void visit(NodeVisitor &);
158 struct VariableReference: Expression
162 VariableDeclaration *declaration;
165 VariableReference(const VariableReference &);
167 virtual VariableReference *clone() const { return new VariableReference(*this); }
168 virtual void visit(NodeVisitor &);
171 struct InterfaceBlockReference: Expression
175 InterfaceBlock *declaration;
177 InterfaceBlockReference();
178 InterfaceBlockReference(const InterfaceBlockReference &);
180 virtual InterfaceBlockReference *clone() const { return new InterfaceBlockReference(*this); }
181 virtual void visit(NodeVisitor &);
184 struct MemberAccess: Expression
186 NodePtr<Expression> left;
189 VariableDeclaration *declaration;
193 MemberAccess(const MemberAccess &);
195 virtual MemberAccess *clone() const { return new MemberAccess(*this); }
196 virtual void visit(NodeVisitor &);
199 struct Swizzle: Expression
201 NodePtr<Expression> left;
202 std::string component_group;
208 virtual Swizzle *clone() const { return new Swizzle(*this); }
209 virtual void visit(NodeVisitor &);
212 struct UnaryExpression: Expression
214 NodePtr<Expression> expression;
216 virtual UnaryExpression *clone() const { return new UnaryExpression(*this); }
217 virtual void visit(NodeVisitor &);
220 struct BinaryExpression: Expression
222 NodePtr<Expression> left;
223 NodePtr<Expression> right;
225 virtual BinaryExpression *clone() const { return new BinaryExpression(*this); }
226 virtual void visit(NodeVisitor &);
229 struct Assignment: BinaryExpression
240 Statement *declaration;
241 Msp::UInt8 chain_len;
244 Target(Statement * = 0);
246 bool operator<(const Target &) const;
249 bool self_referencing;
254 Assignment(const Assignment &);
256 virtual Assignment *clone() const { return new Assignment(*this); }
257 virtual void visit(NodeVisitor &);
260 struct TernaryExpression: Expression
262 NodePtr<Expression> condition;
263 NodePtr<Expression> true_expr;
264 NodePtr<Expression> false_expr;
266 virtual TernaryExpression *clone() const { return new TernaryExpression(*this); }
267 virtual void visit(NodeVisitor &);
270 struct FunctionCall: Expression
274 NodeArray<Expression> arguments;
276 FunctionDeclaration *declaration;
279 FunctionCall(const FunctionCall &);
281 virtual FunctionCall *clone() const { return new FunctionCall(*this); }
282 virtual void visit(NodeVisitor &);
285 struct ExpressionStatement: Statement
287 NodePtr<Expression> expression;
289 virtual ExpressionStatement *clone() const { return new ExpressionStatement(*this); }
290 virtual void visit(NodeVisitor &);
293 struct Import: Statement
297 virtual Import *clone() const { return new Import(*this); }
298 virtual void visit(NodeVisitor &);
301 struct Precision: Statement
303 std::string precision;
306 virtual Precision *clone() const { return new Precision(*this); }
307 virtual void visit(NodeVisitor &);
318 Qualifier(const std::string &n = std::string()): name(n), has_value(false), value(0) { }
319 Qualifier(const std::string &n, int v): name(n), has_value(true), value(v) { }
322 std::vector<Qualifier> qualifiers;
324 virtual Layout *clone() const { return new Layout(*this); }
325 virtual void visit(NodeVisitor &);
328 struct InterfaceLayout: Statement
330 std::string interface;
333 virtual InterfaceLayout *clone() const { return new InterfaceLayout(*this); }
334 virtual void visit(NodeVisitor &);
337 struct TypeDeclaration: Statement
341 virtual TypeDeclaration *clone() const = 0;
344 struct BasicTypeDeclaration: TypeDeclaration
362 TypeDeclaration *base_type;
364 BasicTypeDeclaration();
365 BasicTypeDeclaration(const BasicTypeDeclaration &);
367 virtual BasicTypeDeclaration *clone() const { return new BasicTypeDeclaration(*this); }
368 virtual void visit(NodeVisitor &);
371 struct ImageTypeDeclaration: TypeDeclaration
381 Dimensions dimensions;
387 TypeDeclaration *base_type;
389 ImageTypeDeclaration();
391 virtual ImageTypeDeclaration *clone() const { return new ImageTypeDeclaration(*this); }
392 virtual void visit(NodeVisitor &);
395 struct StructDeclaration: TypeDeclaration
399 InterfaceBlock *interface_block;
402 StructDeclaration(const StructDeclaration &);
403 ~StructDeclaration();
405 virtual StructDeclaration *clone() const { return new StructDeclaration(*this); }
406 virtual void visit(NodeVisitor &);
409 struct VariableDeclaration: Statement
411 NodePtr<Layout> layout;
413 std::string sampling;
414 std::string interpolation;
415 std::string interface;
416 std::string precision;
420 NodePtr<Expression> array_size;
421 NodePtr<Expression> init_expression;
423 TypeDeclaration *type_declaration;
424 VariableDeclaration *linked_declaration;
426 VariableDeclaration();
427 VariableDeclaration(const VariableDeclaration &);
428 ~VariableDeclaration();
430 virtual VariableDeclaration *clone() const { return new VariableDeclaration(*this); }
431 virtual void visit(NodeVisitor &);
434 struct InterfaceBlock: Statement
436 NodePtr<Layout> layout;
437 std::string interface;
438 std::string block_name;
439 NodePtr<Block> members;
440 std::string instance_name;
443 /* An interface block's ultimate base type is always a struct. The
444 immediate type may be either that same struct or an array of it. */
445 TypeDeclaration *type_declaration;
446 StructDeclaration *struct_declaration;
447 InterfaceBlock *linked_block;
450 InterfaceBlock(const InterfaceBlock &);
453 virtual InterfaceBlock *clone() const { return new InterfaceBlock(*this); }
454 virtual void visit(NodeVisitor &);
457 struct FunctionDeclaration: Statement
459 std::string return_type;
461 NodeArray<VariableDeclaration> parameters;
466 std::string signature;
467 FunctionDeclaration *definition;
468 TypeDeclaration *return_type_declaration;
470 FunctionDeclaration();
471 FunctionDeclaration(const FunctionDeclaration &);
473 virtual FunctionDeclaration *clone() const { return new FunctionDeclaration(*this); }
474 virtual void visit(NodeVisitor &);
477 struct Conditional: Statement
479 NodePtr<Expression> condition;
483 virtual Conditional *clone() const { return new Conditional(*this); }
484 virtual void visit(NodeVisitor &);
487 struct Iteration: Statement
489 NodePtr<Statement> init_statement;
490 NodePtr<Expression> condition;
491 NodePtr<Expression> loop_expression;
494 virtual Iteration *clone() const { return new Iteration(*this); }
495 virtual void visit(NodeVisitor &);
498 struct Passthrough: Statement
500 NodePtr<Expression> subscript;
502 virtual Passthrough *clone() const { return new Passthrough(*this); }
503 virtual void visit(NodeVisitor &);
506 struct Return: Statement
508 NodePtr<Expression> expression;
510 virtual Return *clone() const { return new Return(*this); }
511 virtual void visit(NodeVisitor &);
514 struct Jump: Statement
518 virtual Jump *clone() const { return new Jump(*this); }
519 virtual void visit(NodeVisitor &);
535 std::map<std::string, TypeDeclaration *> types;
536 std::map<std::string, InterfaceBlock *> interface_blocks;
537 std::map<std::string, FunctionDeclaration *> functions;
538 std::map<std::string, unsigned> locations;
539 std::map<std::string, unsigned> texture_bindings;
540 std::map<std::string, unsigned> uniform_block_bindings;
541 Features required_features;
542 std::vector<Diagnostic> diagnostics;
546 static const char *get_stage_name(Type);
551 SourceMap source_map;
553 std::list<Stage> stages;
558 std::string get_unused_variable_name(const Block &, const std::string &);
560 int get_layout_value(const Layout &, const std::string &, int = -1);
561 void add_to_chain(Assignment::Target &, Assignment::Target::ChainType, unsigned);
567 #pragma pop_macro("interface")