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")
45 static const Operator operators[];
47 static const Operator &get_operator(const std::string &, Type);
64 Node(): source(GENERATED_SOURCE), line(1) { }
65 Node(const Node &n): source(n.source), line(n.line) { }
67 Node &operator=(const Node &);
71 virtual Node *clone() const = 0;
72 virtual void visit(NodeVisitor &) = 0;
76 class NodePtr: public RefPtr<T>
80 NodePtr(T *p): RefPtr<T>(p) { }
81 NodePtr(const NodePtr &p): RefPtr<T>(p ? p->clone() : 0) { }
82 NodePtr &operator=(const NodePtr &p) { RefPtr<T>::operator=(p); return *this; }
85 NodePtr(const RefPtr<U> &p): RefPtr<T>(p) { }
88 NodePtr(const NodePtr<U> &p): RefPtr<T>(p ? p->clone() : 0) { }
92 class NodeContainer: public C
96 NodeContainer(const NodeContainer &);
100 class NodeList: public NodeContainer<std::list<RefPtr<T> > >
104 class NodeArray: public NodeContainer<std::vector<RefPtr<T> > >
107 struct TypeDeclaration;
108 struct VariableDeclaration;
109 struct InterfaceBlock;
110 struct FunctionDeclaration;
112 struct Statement: Node
114 virtual Statement *clone() const = 0;
119 NodeList<Statement> body;
122 std::map<std::string, VariableDeclaration *> variables;
126 Block(const Block &);
128 virtual Block *clone() const { return new Block(*this); }
129 virtual void visit(NodeVisitor &);
132 struct Expression: Node
134 const Operator *oper;
136 TypeDeclaration *type;
141 virtual Expression *clone() const = 0;
144 struct Literal: Expression
149 virtual Literal *clone() const { return new Literal(*this); }
150 virtual void visit(NodeVisitor &);
153 struct ParenthesizedExpression: Expression
155 NodePtr<Expression> expression;
157 virtual ParenthesizedExpression *clone() const { return new ParenthesizedExpression(*this); }
158 virtual void visit(NodeVisitor &);
161 struct VariableReference: Expression
165 VariableDeclaration *declaration;
168 VariableReference(const VariableReference &);
170 virtual VariableReference *clone() const { return new VariableReference(*this); }
171 virtual void visit(NodeVisitor &);
174 struct InterfaceBlockReference: Expression
178 InterfaceBlock *declaration;
180 InterfaceBlockReference();
181 InterfaceBlockReference(const InterfaceBlockReference &);
183 virtual InterfaceBlockReference *clone() const { return new InterfaceBlockReference(*this); }
184 virtual void visit(NodeVisitor &);
187 struct MemberAccess: Expression
189 NodePtr<Expression> left;
192 VariableDeclaration *declaration;
195 MemberAccess(const MemberAccess &);
197 virtual MemberAccess *clone() const { return new MemberAccess(*this); }
198 virtual void visit(NodeVisitor &);
201 struct Swizzle: Expression
203 NodePtr<Expression> left;
204 std::string component_group;
210 virtual Swizzle *clone() const { return new Swizzle(*this); }
211 virtual void visit(NodeVisitor &);
214 struct UnaryExpression: Expression
216 NodePtr<Expression> expression;
218 virtual UnaryExpression *clone() const { return new UnaryExpression(*this); }
219 virtual void visit(NodeVisitor &);
222 struct BinaryExpression: Expression
224 NodePtr<Expression> left;
225 NodePtr<Expression> right;
227 virtual BinaryExpression *clone() const { return new BinaryExpression(*this); }
228 virtual void visit(NodeVisitor &);
231 struct Assignment: BinaryExpression
242 Statement *declaration;
243 Msp::UInt8 chain_len;
246 Target(Statement * = 0);
248 bool operator<(const Target &) const;
251 bool self_referencing;
256 Assignment(const Assignment &);
258 virtual Assignment *clone() const { return new Assignment(*this); }
259 virtual void visit(NodeVisitor &);
262 struct FunctionCall: Expression
266 NodeArray<Expression> arguments;
268 FunctionDeclaration *declaration;
271 FunctionCall(const FunctionCall &);
273 virtual FunctionCall *clone() const { return new FunctionCall(*this); }
274 virtual void visit(NodeVisitor &);
277 struct ExpressionStatement: Statement
279 NodePtr<Expression> expression;
281 virtual ExpressionStatement *clone() const { return new ExpressionStatement(*this); }
282 virtual void visit(NodeVisitor &);
285 struct Import: Statement
289 virtual Import *clone() const { return new Import(*this); }
290 virtual void visit(NodeVisitor &);
293 struct Precision: Statement
295 std::string precision;
298 virtual Precision *clone() const { return new Precision(*this); }
299 virtual void visit(NodeVisitor &);
311 std::vector<Qualifier> qualifiers;
313 virtual Layout *clone() const { return new Layout(*this); }
314 virtual void visit(NodeVisitor &);
317 struct InterfaceLayout: Statement
319 std::string interface;
322 virtual InterfaceLayout *clone() const { return new InterfaceLayout(*this); }
323 virtual void visit(NodeVisitor &);
326 struct TypeDeclaration: Statement
330 virtual TypeDeclaration *clone() const = 0;
333 struct BasicTypeDeclaration: TypeDeclaration
351 TypeDeclaration *base_type;
353 BasicTypeDeclaration();
354 BasicTypeDeclaration(const BasicTypeDeclaration &);
356 virtual BasicTypeDeclaration *clone() const { return new BasicTypeDeclaration(*this); }
357 virtual void visit(NodeVisitor &);
360 struct ImageTypeDeclaration: TypeDeclaration
370 Dimensions dimensions;
376 TypeDeclaration *base_type;
378 ImageTypeDeclaration();
380 virtual ImageTypeDeclaration *clone() const { return new ImageTypeDeclaration(*this); }
381 virtual void visit(NodeVisitor &);
384 struct StructDeclaration: TypeDeclaration
388 InterfaceBlock *interface_block;
391 StructDeclaration(const StructDeclaration &);
392 ~StructDeclaration();
394 virtual StructDeclaration *clone() const { return new StructDeclaration(*this); }
395 virtual void visit(NodeVisitor &);
398 struct VariableDeclaration: Statement
400 NodePtr<Layout> layout;
402 std::string sampling;
403 std::string interpolation;
404 std::string interface;
405 std::string precision;
409 NodePtr<Expression> array_size;
410 NodePtr<Expression> init_expression;
412 TypeDeclaration *type_declaration;
413 VariableDeclaration *linked_declaration;
415 VariableDeclaration();
416 VariableDeclaration(const VariableDeclaration &);
417 ~VariableDeclaration();
419 virtual VariableDeclaration *clone() const { return new VariableDeclaration(*this); }
420 virtual void visit(NodeVisitor &);
423 struct InterfaceBlock: Statement
425 std::string interface;
427 NodePtr<Block> members;
428 std::string instance_name;
431 /* An interface block's ultimate base type is always a struct. The
432 immediate type may be either that same struct or an array of it. */
433 TypeDeclaration *type_declaration;
434 StructDeclaration *struct_declaration;
435 InterfaceBlock *linked_block;
438 InterfaceBlock(const InterfaceBlock &);
441 virtual InterfaceBlock *clone() const { return new InterfaceBlock(*this); }
442 virtual void visit(NodeVisitor &);
445 struct FunctionDeclaration: Statement
447 std::string return_type;
449 NodeArray<VariableDeclaration> parameters;
452 std::string signature;
453 FunctionDeclaration *definition;
454 TypeDeclaration *return_type_declaration;
456 FunctionDeclaration();
457 FunctionDeclaration(const FunctionDeclaration &);
459 virtual FunctionDeclaration *clone() const { return new FunctionDeclaration(*this); }
460 virtual void visit(NodeVisitor &);
463 struct Conditional: Statement
465 NodePtr<Expression> condition;
469 virtual Conditional *clone() const { return new Conditional(*this); }
470 virtual void visit(NodeVisitor &);
473 struct Iteration: Statement
475 NodePtr<Statement> init_statement;
476 NodePtr<Expression> condition;
477 NodePtr<Expression> loop_expression;
480 virtual Iteration *clone() const { return new Iteration(*this); }
481 virtual void visit(NodeVisitor &);
484 struct Passthrough: Statement
486 NodePtr<Expression> subscript;
488 virtual Passthrough *clone() const { return new Passthrough(*this); }
489 virtual void visit(NodeVisitor &);
492 struct Return: Statement
494 NodePtr<Expression> expression;
496 virtual Return *clone() const { return new Return(*this); }
497 virtual void visit(NodeVisitor &);
500 struct Jump: Statement
504 virtual Jump *clone() const { return new Jump(*this); }
505 virtual void visit(NodeVisitor &);
521 std::map<std::string, TypeDeclaration *> types;
522 std::map<std::string, InterfaceBlock *> interface_blocks;
523 std::map<std::string, FunctionDeclaration *> functions;
524 std::map<std::string, unsigned> locations;
525 Features required_features;
526 std::vector<Diagnostic> diagnostics;
530 static const char *get_stage_name(Type);
535 SourceMap source_map;
537 std::list<Stage> stages;
546 #pragma pop_macro("interface")