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
363 TypeDeclaration *base_type;
365 BasicTypeDeclaration();
366 BasicTypeDeclaration(const BasicTypeDeclaration &);
368 virtual BasicTypeDeclaration *clone() const { return new BasicTypeDeclaration(*this); }
369 virtual void visit(NodeVisitor &);
372 struct ImageTypeDeclaration: TypeDeclaration
382 Dimensions dimensions;
388 TypeDeclaration *base_type;
390 ImageTypeDeclaration();
392 virtual ImageTypeDeclaration *clone() const { return new ImageTypeDeclaration(*this); }
393 virtual void visit(NodeVisitor &);
396 struct StructDeclaration: TypeDeclaration
400 InterfaceBlock *interface_block;
403 StructDeclaration(const StructDeclaration &);
404 ~StructDeclaration();
406 virtual StructDeclaration *clone() const { return new StructDeclaration(*this); }
407 virtual void visit(NodeVisitor &);
410 struct VariableDeclaration: Statement
412 NodePtr<Layout> layout;
414 std::string sampling;
415 std::string interpolation;
416 std::string interface;
417 std::string precision;
421 NodePtr<Expression> array_size;
422 NodePtr<Expression> init_expression;
424 TypeDeclaration *type_declaration;
425 VariableDeclaration *linked_declaration;
427 VariableDeclaration();
428 VariableDeclaration(const VariableDeclaration &);
429 ~VariableDeclaration();
431 virtual VariableDeclaration *clone() const { return new VariableDeclaration(*this); }
432 virtual void visit(NodeVisitor &);
435 struct InterfaceBlock: Statement
437 NodePtr<Layout> layout;
438 std::string interface;
439 std::string block_name;
440 NodePtr<Block> members;
441 std::string instance_name;
444 /* An interface block's ultimate base type is always a struct. The
445 immediate type may be either that same struct or an array of it. */
446 TypeDeclaration *type_declaration;
447 StructDeclaration *struct_declaration;
448 InterfaceBlock *linked_block;
451 InterfaceBlock(const InterfaceBlock &);
454 virtual InterfaceBlock *clone() const { return new InterfaceBlock(*this); }
455 virtual void visit(NodeVisitor &);
458 struct FunctionDeclaration: Statement
460 std::string return_type;
462 NodeArray<VariableDeclaration> parameters;
467 std::string signature;
468 FunctionDeclaration *definition;
469 TypeDeclaration *return_type_declaration;
471 FunctionDeclaration();
472 FunctionDeclaration(const FunctionDeclaration &);
474 virtual FunctionDeclaration *clone() const { return new FunctionDeclaration(*this); }
475 virtual void visit(NodeVisitor &);
478 struct Conditional: Statement
480 NodePtr<Expression> condition;
484 virtual Conditional *clone() const { return new Conditional(*this); }
485 virtual void visit(NodeVisitor &);
488 struct Iteration: Statement
490 NodePtr<Statement> init_statement;
491 NodePtr<Expression> condition;
492 NodePtr<Expression> loop_expression;
495 virtual Iteration *clone() const { return new Iteration(*this); }
496 virtual void visit(NodeVisitor &);
499 struct Passthrough: Statement
501 NodePtr<Expression> subscript;
503 virtual Passthrough *clone() const { return new Passthrough(*this); }
504 virtual void visit(NodeVisitor &);
507 struct Return: Statement
509 NodePtr<Expression> expression;
511 virtual Return *clone() const { return new Return(*this); }
512 virtual void visit(NodeVisitor &);
515 struct Jump: Statement
519 virtual Jump *clone() const { return new Jump(*this); }
520 virtual void visit(NodeVisitor &);
536 std::map<std::string, TypeDeclaration *> types;
537 std::map<std::string, InterfaceBlock *> interface_blocks;
538 std::map<std::string, FunctionDeclaration *> functions;
539 std::map<std::string, unsigned> locations;
540 std::map<std::string, unsigned> texture_bindings;
541 std::map<std::string, unsigned> uniform_block_bindings;
542 Features required_features;
543 std::vector<Diagnostic> diagnostics;
547 static const char *get_stage_name(Type);
552 SourceMap source_map;
554 std::list<Stage> stages;
559 std::string get_unused_variable_name(const Block &, const std::string &);
561 int get_layout_value(const Layout &, const std::string &, int = -1);
562 void add_to_chain(Assignment::Target &, Assignment::Target::ChainType, unsigned);
568 #pragma pop_macro("interface")