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 {
24 Node &operator=(const Node &);
28 virtual Node *clone() const = 0;
29 virtual void visit(NodeVisitor &) = 0;
33 class NodePtr: public RefPtr<T>
37 NodePtr(T *p): RefPtr<T>(p) { }
38 NodePtr(const NodePtr &p): RefPtr<T>(p ? p->clone() : 0) { }
41 NodePtr(const RefPtr<U> &p): RefPtr<T>(p) { }
44 NodePtr(const NodePtr<U> &p): RefPtr<T>(p ? p->clone() : 0) { }
48 class NodeContainer: public C
52 NodeContainer(const NodeContainer &);
56 class NodeList: public NodeContainer<std::list<RefPtr<T> > >
60 class NodeArray: public NodeContainer<std::vector<RefPtr<T> > >
63 struct StructDeclaration;
64 struct VariableDeclaration;
65 struct FunctionDeclaration;
67 struct Statement: Node
74 virtual Statement *clone() const = 0;
79 NodeList<Statement> body;
81 std::map<std::string, StructDeclaration *> types;
82 std::map<std::string, VariableDeclaration *> variables;
86 virtual Block *clone() const { return new Block(*this); }
87 virtual void visit(NodeVisitor &);
90 struct Expression: Node
92 virtual Expression *clone() const = 0;
95 struct Literal: Expression
99 virtual Literal *clone() const { return new Literal(*this); }
100 virtual void visit(NodeVisitor &);
103 struct ParenthesizedExpression: Expression
105 NodePtr<Expression> expression;
107 virtual ParenthesizedExpression *clone() const { return new ParenthesizedExpression(*this); }
108 virtual void visit(NodeVisitor &);
111 struct VariableReference: Expression
114 VariableDeclaration *declaration;
118 virtual VariableReference *clone() const { return new VariableReference(*this); }
119 virtual void visit(NodeVisitor &);
122 struct MemberAccess: Expression
124 NodePtr<Expression> left;
126 VariableDeclaration *declaration;
128 virtual MemberAccess *clone() const { return new MemberAccess(*this); }
129 virtual void visit(NodeVisitor &);
132 struct UnaryExpression: Expression
135 NodePtr<Expression> expression;
140 virtual UnaryExpression *clone() const { return new UnaryExpression(*this); }
141 virtual void visit(NodeVisitor &);
144 struct BinaryExpression: Expression
146 NodePtr<Expression> left;
148 NodePtr<Expression> right;
151 virtual BinaryExpression *clone() const { return new BinaryExpression(*this); }
152 virtual void visit(NodeVisitor &);
155 struct Assignment: BinaryExpression
157 bool self_referencing;
158 VariableDeclaration *target_declaration;
162 virtual Assignment *clone() const { return new Assignment(*this); }
163 virtual void visit(NodeVisitor &);
166 struct FunctionCall: Expression
169 FunctionDeclaration *declaration;
171 NodeArray<Expression> arguments;
175 virtual FunctionCall *clone() const { return new FunctionCall(*this); }
176 virtual void visit(NodeVisitor &);
179 struct ExpressionStatement: Statement
181 NodePtr<Expression> expression;
183 virtual ExpressionStatement *clone() const { return new ExpressionStatement(*this); }
184 virtual void visit(NodeVisitor &);
187 struct Import: Statement
191 virtual Import *clone() const { return new Import(*this); }
192 virtual void visit(NodeVisitor &);
195 struct Precision: Statement
197 std::string precision;
200 virtual Precision *clone() const { return new Precision(*this); }
201 virtual void visit(NodeVisitor &);
208 // TODO the standard calls this name, not identifier
209 std::string identifier;
213 std::vector<Qualifier> qualifiers;
215 virtual Layout *clone() const { return new Layout(*this); }
216 virtual void visit(NodeVisitor &);
219 struct InterfaceLayout: Statement
221 std::string interface;
224 virtual InterfaceLayout *clone() const { return new InterfaceLayout(*this); }
225 virtual void visit(NodeVisitor &);
228 struct StructDeclaration: Statement
235 virtual StructDeclaration *clone() const { return new StructDeclaration(*this); }
236 virtual void visit(NodeVisitor &);
239 struct VariableDeclaration: Statement
242 std::string sampling;
243 std::string interpolation;
244 std::string interface;
245 std::string precision;
247 StructDeclaration *type_declaration;
250 NodePtr<Expression> array_size;
251 NodePtr<Expression> init_expression;
252 VariableDeclaration *linked_declaration;
253 NodePtr<Layout> layout;
255 VariableDeclaration();
257 virtual VariableDeclaration *clone() const { return new VariableDeclaration(*this); }
258 virtual void visit(NodeVisitor &);
261 struct InterfaceBlock: Statement
263 std::string interface;
266 std::string instance_name;
271 virtual InterfaceBlock *clone() const { return new InterfaceBlock(*this); }
272 virtual void visit(NodeVisitor &);
275 struct FunctionDeclaration: Statement
277 std::string return_type;
279 NodeArray<VariableDeclaration> parameters;
280 FunctionDeclaration *definition;
283 FunctionDeclaration();
284 FunctionDeclaration(const FunctionDeclaration &);
286 virtual FunctionDeclaration *clone() const { return new FunctionDeclaration(*this); }
287 virtual void visit(NodeVisitor &);
290 struct Conditional: Statement
292 NodePtr<Expression> condition;
296 virtual Conditional *clone() const { return new Conditional(*this); }
297 virtual void visit(NodeVisitor &);
300 struct Iteration: Statement
302 NodePtr<Node> init_statement;
303 NodePtr<Expression> condition;
304 NodePtr<Expression> loop_expression;
307 virtual Iteration *clone() const { return new Iteration(*this); }
308 virtual void visit(NodeVisitor &);
311 struct Passthrough: Statement
313 NodePtr<Expression> subscript;
315 virtual Passthrough *clone() const { return new Passthrough(*this); }
316 virtual void visit(NodeVisitor &);
319 struct Return: Statement
321 NodePtr<Expression> expression;
323 virtual Return *clone() const { return new Return(*this); }
324 virtual void visit(NodeVisitor &);
327 struct Jump: Statement
331 virtual Jump *clone() const { return new Jump(*this); }
332 virtual void visit(NodeVisitor &);
337 virtual ~NodeVisitor() { }
339 virtual void visit(Block &) { }
340 virtual void visit(Literal &) { }
341 virtual void visit(ParenthesizedExpression &) { }
342 virtual void visit(VariableReference &) { }
343 virtual void visit(MemberAccess &) { }
344 virtual void visit(UnaryExpression &) { }
345 virtual void visit(BinaryExpression &) { }
346 virtual void visit(Assignment &);
347 virtual void visit(FunctionCall &) { }
348 virtual void visit(ExpressionStatement &) { }
349 virtual void visit(Import &) { }
350 virtual void visit(Precision &) { }
351 virtual void visit(Layout &) { }
352 virtual void visit(InterfaceLayout &) { }
353 virtual void visit(StructDeclaration &) { }
354 virtual void visit(VariableDeclaration &) { }
355 virtual void visit(InterfaceBlock &) { }
356 virtual void visit(FunctionDeclaration &) { }
357 virtual void visit(Conditional &) { }
358 virtual void visit(Iteration &) { }
359 virtual void visit(Passthrough &) { }
360 virtual void visit(Return &) { }
361 virtual void visit(Jump &) { }
364 struct TraversingVisitor: NodeVisitor
366 using NodeVisitor::visit;
367 virtual void visit(Block &);
368 virtual void visit(ParenthesizedExpression &);
369 virtual void visit(MemberAccess &);
370 virtual void visit(UnaryExpression &);
371 virtual void visit(BinaryExpression &);
372 virtual void visit(FunctionCall &);
373 virtual void visit(ExpressionStatement &);
374 virtual void visit(InterfaceLayout &);
375 virtual void visit(StructDeclaration &);
376 virtual void visit(VariableDeclaration &);
377 virtual void visit(InterfaceBlock &);
378 virtual void visit(FunctionDeclaration &);
379 virtual void visit(Conditional &);
380 virtual void visit(Iteration &);
381 virtual void visit(Passthrough &);
382 virtual void visit(Return &);
397 ProgramSyntax::Block content;
398 std::map<std::string, VariableDeclaration *> in_variables;
399 std::map<std::string, VariableDeclaration *> out_variables;
400 std::map<std::string, unsigned> locations;
401 Version required_version;
402 std::vector<const Extension *> required_extensions;
410 std::list<Stage> stages;
415 } // namespace ProgramSyntax
419 #pragma pop_macro("interface")