1 #ifndef MSP_GL_PROGRAMSYNTAX_H_
2 #define MSP_GL_PROGRAMSYNTAX_H_
8 #include <msp/core/refptr.h>
12 #pragma push_macro("interface")
17 namespace ProgramSyntax {
25 Node(const Node &) { }
27 Node &operator=(const Node &);
31 virtual Node *clone() const = 0;
32 virtual void visit(NodeVisitor &) = 0;
36 class NodePtr: public RefPtr<T>
40 NodePtr(T *p): RefPtr<T>(p) { }
41 NodePtr(const NodePtr &p): RefPtr<T>(p ? p->clone() : 0) { }
42 NodePtr &operator=(const NodePtr &p) { RefPtr<T>::operator=(p); return *this; }
45 NodePtr(const RefPtr<U> &p): RefPtr<T>(p) { }
48 NodePtr(const NodePtr<U> &p): RefPtr<T>(p ? p->clone() : 0) { }
52 class NodeContainer: public C
56 NodeContainer(const NodeContainer &);
60 class NodeList: public NodeContainer<std::list<RefPtr<T> > >
64 class NodeArray: public NodeContainer<std::vector<RefPtr<T> > >
67 struct StructDeclaration;
68 struct VariableDeclaration;
69 struct FunctionDeclaration;
71 struct Statement: Node
78 virtual Statement *clone() const = 0;
83 NodeList<Statement> body;
85 std::map<std::string, StructDeclaration *> types;
86 std::map<std::string, VariableDeclaration *> variables;
90 virtual Block *clone() const { return new Block(*this); }
91 virtual void visit(NodeVisitor &);
94 struct Expression: Node
96 virtual Expression *clone() const = 0;
99 struct Literal: Expression
103 virtual Literal *clone() const { return new Literal(*this); }
104 virtual void visit(NodeVisitor &);
107 struct ParenthesizedExpression: Expression
109 NodePtr<Expression> expression;
111 virtual ParenthesizedExpression *clone() const { return new ParenthesizedExpression(*this); }
112 virtual void visit(NodeVisitor &);
115 struct VariableReference: Expression
118 VariableDeclaration *declaration;
122 virtual VariableReference *clone() const { return new VariableReference(*this); }
123 virtual void visit(NodeVisitor &);
126 struct MemberAccess: Expression
128 NodePtr<Expression> left;
130 VariableDeclaration *declaration;
132 virtual MemberAccess *clone() const { return new MemberAccess(*this); }
133 virtual void visit(NodeVisitor &);
136 struct UnaryExpression: Expression
139 NodePtr<Expression> expression;
144 virtual UnaryExpression *clone() const { return new UnaryExpression(*this); }
145 virtual void visit(NodeVisitor &);
148 struct BinaryExpression: Expression
150 NodePtr<Expression> left;
152 NodePtr<Expression> right;
155 virtual BinaryExpression *clone() const { return new BinaryExpression(*this); }
156 virtual void visit(NodeVisitor &);
159 struct Assignment: BinaryExpression
161 bool self_referencing;
162 VariableDeclaration *target_declaration;
166 virtual Assignment *clone() const { return new Assignment(*this); }
167 virtual void visit(NodeVisitor &);
170 struct FunctionCall: Expression
173 FunctionDeclaration *declaration;
175 NodeArray<Expression> arguments;
179 virtual FunctionCall *clone() const { return new FunctionCall(*this); }
180 virtual void visit(NodeVisitor &);
183 struct ExpressionStatement: Statement
185 NodePtr<Expression> expression;
187 virtual ExpressionStatement *clone() const { return new ExpressionStatement(*this); }
188 virtual void visit(NodeVisitor &);
191 struct Import: Statement
195 virtual Import *clone() const { return new Import(*this); }
196 virtual void visit(NodeVisitor &);
199 struct Precision: Statement
201 std::string precision;
204 virtual Precision *clone() const { return new Precision(*this); }
205 virtual void visit(NodeVisitor &);
212 // TODO the standard calls this name, not identifier
213 std::string identifier;
217 std::vector<Qualifier> qualifiers;
219 virtual Layout *clone() const { return new Layout(*this); }
220 virtual void visit(NodeVisitor &);
223 struct InterfaceLayout: Statement
225 std::string interface;
228 virtual InterfaceLayout *clone() const { return new InterfaceLayout(*this); }
229 virtual void visit(NodeVisitor &);
232 struct StructDeclaration: Statement
239 virtual StructDeclaration *clone() const { return new StructDeclaration(*this); }
240 virtual void visit(NodeVisitor &);
243 struct VariableDeclaration: Statement
246 std::string sampling;
247 std::string interpolation;
248 std::string interface;
249 std::string precision;
251 StructDeclaration *type_declaration;
254 NodePtr<Expression> array_size;
255 NodePtr<Expression> init_expression;
256 VariableDeclaration *linked_declaration;
257 NodePtr<Layout> layout;
259 VariableDeclaration();
261 virtual VariableDeclaration *clone() const { return new VariableDeclaration(*this); }
262 virtual void visit(NodeVisitor &);
265 struct InterfaceBlock: Statement
267 std::string interface;
270 std::string instance_name;
275 virtual InterfaceBlock *clone() const { return new InterfaceBlock(*this); }
276 virtual void visit(NodeVisitor &);
279 struct FunctionDeclaration: Statement
281 std::string return_type;
283 NodeArray<VariableDeclaration> parameters;
284 FunctionDeclaration *definition;
287 FunctionDeclaration();
288 FunctionDeclaration(const FunctionDeclaration &);
290 virtual FunctionDeclaration *clone() const { return new FunctionDeclaration(*this); }
291 virtual void visit(NodeVisitor &);
294 struct Conditional: Statement
296 NodePtr<Expression> condition;
300 virtual Conditional *clone() const { return new Conditional(*this); }
301 virtual void visit(NodeVisitor &);
304 struct Iteration: Statement
306 NodePtr<Node> init_statement;
307 NodePtr<Expression> condition;
308 NodePtr<Expression> loop_expression;
311 virtual Iteration *clone() const { return new Iteration(*this); }
312 virtual void visit(NodeVisitor &);
315 struct Passthrough: Statement
317 NodePtr<Expression> subscript;
319 virtual Passthrough *clone() const { return new Passthrough(*this); }
320 virtual void visit(NodeVisitor &);
323 struct Return: Statement
325 NodePtr<Expression> expression;
327 virtual Return *clone() const { return new Return(*this); }
328 virtual void visit(NodeVisitor &);
331 struct Jump: Statement
335 virtual Jump *clone() const { return new Jump(*this); }
336 virtual void visit(NodeVisitor &);
341 virtual ~NodeVisitor() { }
343 virtual void visit(Block &) { }
344 virtual void visit(Literal &) { }
345 virtual void visit(ParenthesizedExpression &) { }
346 virtual void visit(VariableReference &) { }
347 virtual void visit(MemberAccess &) { }
348 virtual void visit(UnaryExpression &) { }
349 virtual void visit(BinaryExpression &) { }
350 virtual void visit(Assignment &);
351 virtual void visit(FunctionCall &) { }
352 virtual void visit(ExpressionStatement &) { }
353 virtual void visit(Import &) { }
354 virtual void visit(Precision &) { }
355 virtual void visit(Layout &) { }
356 virtual void visit(InterfaceLayout &) { }
357 virtual void visit(StructDeclaration &) { }
358 virtual void visit(VariableDeclaration &) { }
359 virtual void visit(InterfaceBlock &) { }
360 virtual void visit(FunctionDeclaration &) { }
361 virtual void visit(Conditional &) { }
362 virtual void visit(Iteration &) { }
363 virtual void visit(Passthrough &) { }
364 virtual void visit(Return &) { }
365 virtual void visit(Jump &) { }
368 struct TraversingVisitor: NodeVisitor
370 using NodeVisitor::visit;
371 virtual void visit(Block &);
372 virtual void visit(ParenthesizedExpression &);
373 virtual void visit(MemberAccess &);
374 virtual void visit(UnaryExpression &);
375 virtual void visit(BinaryExpression &);
376 virtual void visit(FunctionCall &);
377 virtual void visit(ExpressionStatement &);
378 virtual void visit(InterfaceLayout &);
379 virtual void visit(StructDeclaration &);
380 virtual void visit(VariableDeclaration &);
381 virtual void visit(InterfaceBlock &);
382 virtual void visit(FunctionDeclaration &);
383 virtual void visit(Conditional &);
384 virtual void visit(Iteration &);
385 virtual void visit(Passthrough &);
386 virtual void visit(Return &);
401 ProgramSyntax::Block content;
402 std::map<std::string, VariableDeclaration *> in_variables;
403 std::map<std::string, VariableDeclaration *> out_variables;
404 std::map<std::string, unsigned> locations;
405 Version required_version;
406 std::vector<const Extension *> required_extensions;
414 std::list<Stage> stages;
419 } // namespace ProgramSyntax
423 #pragma pop_macro("interface")