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 &);
102 class NodeList: public NodeContainer<std::list<RefPtr<T> > >
106 class NodeArray: public NodeContainer<std::vector<RefPtr<T> > >
109 struct TypeDeclaration;
110 struct VariableDeclaration;
111 struct InterfaceBlock;
112 struct FunctionDeclaration;
114 struct Statement: Node
116 virtual Statement *clone() const = 0;
121 NodeList<Statement> body;
124 std::map<std::string, VariableDeclaration *> variables;
128 Block(const Block &);
130 virtual Block *clone() const { return new Block(*this); }
131 virtual void visit(NodeVisitor &);
134 struct Expression: Node
136 const Operator *oper;
138 TypeDeclaration *type;
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;
162 VariableReference(const VariableReference &);
164 virtual VariableReference *clone() const { return new VariableReference(*this); }
165 virtual void visit(NodeVisitor &);
168 struct InterfaceBlockReference: Expression
172 InterfaceBlock *declaration;
174 InterfaceBlockReference();
175 InterfaceBlockReference(const InterfaceBlockReference &);
177 virtual InterfaceBlockReference *clone() const { return new InterfaceBlockReference(*this); }
178 virtual void visit(NodeVisitor &);
181 struct MemberAccess: Expression
183 NodePtr<Expression> left;
186 VariableDeclaration *declaration;
189 MemberAccess(const MemberAccess &);
191 virtual MemberAccess *clone() const { return new MemberAccess(*this); }
192 virtual void visit(NodeVisitor &);
195 struct Swizzle: Expression
197 NodePtr<Expression> left;
198 std::string component_group;
204 virtual Swizzle *clone() const { return new Swizzle(*this); }
205 virtual void visit(NodeVisitor &);
208 struct UnaryExpression: Expression
210 NodePtr<Expression> expression;
212 virtual UnaryExpression *clone() const { return new UnaryExpression(*this); }
213 virtual void visit(NodeVisitor &);
216 struct BinaryExpression: Expression
218 NodePtr<Expression> left;
219 NodePtr<Expression> right;
221 virtual BinaryExpression *clone() const { return new BinaryExpression(*this); }
222 virtual void visit(NodeVisitor &);
225 struct Assignment: BinaryExpression
236 Statement *declaration;
237 Msp::UInt8 chain_len;
240 Target(Statement * = 0);
242 bool operator<(const Target &) const;
245 bool self_referencing;
250 Assignment(const Assignment &);
252 virtual Assignment *clone() const { return new Assignment(*this); }
253 virtual void visit(NodeVisitor &);
256 struct TernaryExpression: Expression
258 NodePtr<Expression> condition;
259 NodePtr<Expression> true_expr;
260 NodePtr<Expression> false_expr;
262 virtual TernaryExpression *clone() const { return new TernaryExpression(*this); }
263 virtual void visit(NodeVisitor &);
266 struct FunctionCall: Expression
270 NodeArray<Expression> arguments;
272 FunctionDeclaration *declaration;
275 FunctionCall(const FunctionCall &);
277 virtual FunctionCall *clone() const { return new FunctionCall(*this); }
278 virtual void visit(NodeVisitor &);
281 struct ExpressionStatement: Statement
283 NodePtr<Expression> expression;
285 virtual ExpressionStatement *clone() const { return new ExpressionStatement(*this); }
286 virtual void visit(NodeVisitor &);
289 struct Import: Statement
293 virtual Import *clone() const { return new Import(*this); }
294 virtual void visit(NodeVisitor &);
297 struct Precision: Statement
299 std::string precision;
302 virtual Precision *clone() const { return new Precision(*this); }
303 virtual void visit(NodeVisitor &);
315 std::vector<Qualifier> qualifiers;
317 virtual Layout *clone() const { return new Layout(*this); }
318 virtual void visit(NodeVisitor &);
321 struct InterfaceLayout: Statement
323 std::string interface;
326 virtual InterfaceLayout *clone() const { return new InterfaceLayout(*this); }
327 virtual void visit(NodeVisitor &);
330 struct TypeDeclaration: Statement
334 virtual TypeDeclaration *clone() const = 0;
337 struct BasicTypeDeclaration: TypeDeclaration
355 TypeDeclaration *base_type;
357 BasicTypeDeclaration();
358 BasicTypeDeclaration(const BasicTypeDeclaration &);
360 virtual BasicTypeDeclaration *clone() const { return new BasicTypeDeclaration(*this); }
361 virtual void visit(NodeVisitor &);
364 struct ImageTypeDeclaration: TypeDeclaration
374 Dimensions dimensions;
380 TypeDeclaration *base_type;
382 ImageTypeDeclaration();
384 virtual ImageTypeDeclaration *clone() const { return new ImageTypeDeclaration(*this); }
385 virtual void visit(NodeVisitor &);
388 struct StructDeclaration: TypeDeclaration
392 InterfaceBlock *interface_block;
395 StructDeclaration(const StructDeclaration &);
396 ~StructDeclaration();
398 virtual StructDeclaration *clone() const { return new StructDeclaration(*this); }
399 virtual void visit(NodeVisitor &);
402 struct VariableDeclaration: Statement
404 NodePtr<Layout> layout;
406 std::string sampling;
407 std::string interpolation;
408 std::string interface;
409 std::string precision;
413 NodePtr<Expression> array_size;
414 NodePtr<Expression> init_expression;
416 TypeDeclaration *type_declaration;
417 VariableDeclaration *linked_declaration;
419 VariableDeclaration();
420 VariableDeclaration(const VariableDeclaration &);
421 ~VariableDeclaration();
423 virtual VariableDeclaration *clone() const { return new VariableDeclaration(*this); }
424 virtual void visit(NodeVisitor &);
427 struct InterfaceBlock: Statement
429 std::string interface;
431 NodePtr<Block> members;
432 std::string instance_name;
435 /* An interface block's ultimate base type is always a struct. The
436 immediate type may be either that same struct or an array of it. */
437 TypeDeclaration *type_declaration;
438 StructDeclaration *struct_declaration;
439 InterfaceBlock *linked_block;
442 InterfaceBlock(const InterfaceBlock &);
445 virtual InterfaceBlock *clone() const { return new InterfaceBlock(*this); }
446 virtual void visit(NodeVisitor &);
449 struct FunctionDeclaration: Statement
451 std::string return_type;
453 NodeArray<VariableDeclaration> parameters;
458 std::string signature;
459 FunctionDeclaration *definition;
460 TypeDeclaration *return_type_declaration;
462 FunctionDeclaration();
463 FunctionDeclaration(const FunctionDeclaration &);
465 virtual FunctionDeclaration *clone() const { return new FunctionDeclaration(*this); }
466 virtual void visit(NodeVisitor &);
469 struct Conditional: Statement
471 NodePtr<Expression> condition;
475 virtual Conditional *clone() const { return new Conditional(*this); }
476 virtual void visit(NodeVisitor &);
479 struct Iteration: Statement
481 NodePtr<Statement> init_statement;
482 NodePtr<Expression> condition;
483 NodePtr<Expression> loop_expression;
486 virtual Iteration *clone() const { return new Iteration(*this); }
487 virtual void visit(NodeVisitor &);
490 struct Passthrough: Statement
492 NodePtr<Expression> subscript;
494 virtual Passthrough *clone() const { return new Passthrough(*this); }
495 virtual void visit(NodeVisitor &);
498 struct Return: Statement
500 NodePtr<Expression> expression;
502 virtual Return *clone() const { return new Return(*this); }
503 virtual void visit(NodeVisitor &);
506 struct Jump: Statement
510 virtual Jump *clone() const { return new Jump(*this); }
511 virtual void visit(NodeVisitor &);
527 std::map<std::string, TypeDeclaration *> types;
528 std::map<std::string, InterfaceBlock *> interface_blocks;
529 std::map<std::string, FunctionDeclaration *> functions;
530 std::map<std::string, unsigned> locations;
531 Features required_features;
532 std::vector<Diagnostic> diagnostics;
536 static const char *get_stage_name(Type);
541 SourceMap source_map;
543 std::list<Stage> stages;
548 std::string get_unused_variable_name(const Block &, const std::string &, const std::string &);
554 #pragma pop_macro("interface")