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;
192 MemberAccess(const MemberAccess &);
194 virtual MemberAccess *clone() const { return new MemberAccess(*this); }
195 virtual void visit(NodeVisitor &);
198 struct Swizzle: Expression
200 NodePtr<Expression> left;
201 std::string component_group;
207 virtual Swizzle *clone() const { return new Swizzle(*this); }
208 virtual void visit(NodeVisitor &);
211 struct UnaryExpression: Expression
213 NodePtr<Expression> expression;
215 virtual UnaryExpression *clone() const { return new UnaryExpression(*this); }
216 virtual void visit(NodeVisitor &);
219 struct BinaryExpression: Expression
221 NodePtr<Expression> left;
222 NodePtr<Expression> right;
224 virtual BinaryExpression *clone() const { return new BinaryExpression(*this); }
225 virtual void visit(NodeVisitor &);
228 struct Assignment: BinaryExpression
239 Statement *declaration;
240 Msp::UInt8 chain_len;
243 Target(Statement * = 0);
245 bool operator<(const Target &) const;
248 bool self_referencing;
253 Assignment(const Assignment &);
255 virtual Assignment *clone() const { return new Assignment(*this); }
256 virtual void visit(NodeVisitor &);
259 struct TernaryExpression: Expression
261 NodePtr<Expression> condition;
262 NodePtr<Expression> true_expr;
263 NodePtr<Expression> false_expr;
265 virtual TernaryExpression *clone() const { return new TernaryExpression(*this); }
266 virtual void visit(NodeVisitor &);
269 struct FunctionCall: Expression
273 NodeArray<Expression> arguments;
275 FunctionDeclaration *declaration;
278 FunctionCall(const FunctionCall &);
280 virtual FunctionCall *clone() const { return new FunctionCall(*this); }
281 virtual void visit(NodeVisitor &);
284 struct ExpressionStatement: Statement
286 NodePtr<Expression> expression;
288 virtual ExpressionStatement *clone() const { return new ExpressionStatement(*this); }
289 virtual void visit(NodeVisitor &);
292 struct Import: Statement
296 virtual Import *clone() const { return new Import(*this); }
297 virtual void visit(NodeVisitor &);
300 struct Precision: Statement
302 std::string precision;
305 virtual Precision *clone() const { return new Precision(*this); }
306 virtual void visit(NodeVisitor &);
318 std::vector<Qualifier> qualifiers;
320 virtual Layout *clone() const { return new Layout(*this); }
321 virtual void visit(NodeVisitor &);
324 struct InterfaceLayout: Statement
326 std::string interface;
329 virtual InterfaceLayout *clone() const { return new InterfaceLayout(*this); }
330 virtual void visit(NodeVisitor &);
333 struct TypeDeclaration: Statement
337 virtual TypeDeclaration *clone() const = 0;
340 struct BasicTypeDeclaration: TypeDeclaration
358 TypeDeclaration *base_type;
360 BasicTypeDeclaration();
361 BasicTypeDeclaration(const BasicTypeDeclaration &);
363 virtual BasicTypeDeclaration *clone() const { return new BasicTypeDeclaration(*this); }
364 virtual void visit(NodeVisitor &);
367 struct ImageTypeDeclaration: TypeDeclaration
377 Dimensions dimensions;
383 TypeDeclaration *base_type;
385 ImageTypeDeclaration();
387 virtual ImageTypeDeclaration *clone() const { return new ImageTypeDeclaration(*this); }
388 virtual void visit(NodeVisitor &);
391 struct StructDeclaration: TypeDeclaration
395 InterfaceBlock *interface_block;
398 StructDeclaration(const StructDeclaration &);
399 ~StructDeclaration();
401 virtual StructDeclaration *clone() const { return new StructDeclaration(*this); }
402 virtual void visit(NodeVisitor &);
405 struct VariableDeclaration: Statement
407 NodePtr<Layout> layout;
409 std::string sampling;
410 std::string interpolation;
411 std::string interface;
412 std::string precision;
416 NodePtr<Expression> array_size;
417 NodePtr<Expression> init_expression;
419 TypeDeclaration *type_declaration;
420 VariableDeclaration *linked_declaration;
422 VariableDeclaration();
423 VariableDeclaration(const VariableDeclaration &);
424 ~VariableDeclaration();
426 virtual VariableDeclaration *clone() const { return new VariableDeclaration(*this); }
427 virtual void visit(NodeVisitor &);
430 struct InterfaceBlock: Statement
432 NodePtr<Layout> layout;
433 std::string interface;
434 std::string block_name;
435 NodePtr<Block> members;
436 std::string instance_name;
439 /* An interface block's ultimate base type is always a struct. The
440 immediate type may be either that same struct or an array of it. */
441 TypeDeclaration *type_declaration;
442 StructDeclaration *struct_declaration;
443 InterfaceBlock *linked_block;
446 InterfaceBlock(const InterfaceBlock &);
449 virtual InterfaceBlock *clone() const { return new InterfaceBlock(*this); }
450 virtual void visit(NodeVisitor &);
453 struct FunctionDeclaration: Statement
455 std::string return_type;
457 NodeArray<VariableDeclaration> parameters;
462 std::string signature;
463 FunctionDeclaration *definition;
464 TypeDeclaration *return_type_declaration;
466 FunctionDeclaration();
467 FunctionDeclaration(const FunctionDeclaration &);
469 virtual FunctionDeclaration *clone() const { return new FunctionDeclaration(*this); }
470 virtual void visit(NodeVisitor &);
473 struct Conditional: Statement
475 NodePtr<Expression> condition;
479 virtual Conditional *clone() const { return new Conditional(*this); }
480 virtual void visit(NodeVisitor &);
483 struct Iteration: Statement
485 NodePtr<Statement> init_statement;
486 NodePtr<Expression> condition;
487 NodePtr<Expression> loop_expression;
490 virtual Iteration *clone() const { return new Iteration(*this); }
491 virtual void visit(NodeVisitor &);
494 struct Passthrough: Statement
496 NodePtr<Expression> subscript;
498 virtual Passthrough *clone() const { return new Passthrough(*this); }
499 virtual void visit(NodeVisitor &);
502 struct Return: Statement
504 NodePtr<Expression> expression;
506 virtual Return *clone() const { return new Return(*this); }
507 virtual void visit(NodeVisitor &);
510 struct Jump: Statement
514 virtual Jump *clone() const { return new Jump(*this); }
515 virtual void visit(NodeVisitor &);
531 std::map<std::string, TypeDeclaration *> types;
532 std::map<std::string, InterfaceBlock *> interface_blocks;
533 std::map<std::string, FunctionDeclaration *> functions;
534 std::map<std::string, unsigned> locations;
535 Features required_features;
536 std::vector<Diagnostic> diagnostics;
540 static const char *get_stage_name(Type);
545 SourceMap source_map;
547 std::list<Stage> stages;
552 std::string get_unused_variable_name(const Block &, const std::string &);
558 #pragma pop_macro("interface")