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
69 virtual Statement *clone() const = 0;
74 NodeList<Statement> body;
76 std::map<std::string, StructDeclaration *> types;
77 std::map<std::string, VariableDeclaration *> variables;
81 virtual Block *clone() const { return new Block(*this); }
82 virtual void visit(NodeVisitor &);
85 struct Expression: Node
87 virtual Expression *clone() const = 0;
90 struct Literal: Expression
94 virtual Literal *clone() const { return new Literal(*this); }
95 virtual void visit(NodeVisitor &);
98 struct ParenthesizedExpression: Expression
100 NodePtr<Expression> expression;
102 virtual ParenthesizedExpression *clone() const { return new ParenthesizedExpression(*this); }
103 virtual void visit(NodeVisitor &);
106 struct VariableReference: Expression
109 VariableDeclaration *declaration;
113 virtual VariableReference *clone() const { return new VariableReference(*this); }
114 virtual void visit(NodeVisitor &);
117 struct MemberAccess: Expression
119 NodePtr<Expression> left;
121 VariableDeclaration *declaration;
123 virtual MemberAccess *clone() const { return new MemberAccess(*this); }
124 virtual void visit(NodeVisitor &);
127 struct UnaryExpression: Expression
130 NodePtr<Expression> expression;
135 virtual UnaryExpression *clone() const { return new UnaryExpression(*this); }
136 virtual void visit(NodeVisitor &);
139 struct BinaryExpression: Expression
141 NodePtr<Expression> left;
143 NodePtr<Expression> right;
146 virtual BinaryExpression *clone() const { return new BinaryExpression(*this); }
147 virtual void visit(NodeVisitor &);
150 struct Assignment: BinaryExpression
152 bool self_referencing;
153 VariableDeclaration *target_declaration;
157 virtual Assignment *clone() const { return new Assignment(*this); }
158 virtual void visit(NodeVisitor &);
161 struct FunctionCall: Expression
164 FunctionDeclaration *declaration;
166 NodeArray<Expression> arguments;
170 virtual FunctionCall *clone() const { return new FunctionCall(*this); }
171 virtual void visit(NodeVisitor &);
174 struct ExpressionStatement: Statement
176 NodePtr<Expression> expression;
178 virtual ExpressionStatement *clone() const { return new ExpressionStatement(*this); }
179 virtual void visit(NodeVisitor &);
182 struct Import: Statement
186 virtual Import *clone() const { return new Import(*this); }
187 virtual void visit(NodeVisitor &);
190 struct Precision: Statement
192 std::string precision;
195 virtual Precision *clone() const { return new Precision(*this); }
196 virtual void visit(NodeVisitor &);
203 // TODO the standard calls this name, not identifier
204 std::string identifier;
208 std::vector<Qualifier> qualifiers;
210 virtual Layout *clone() const { return new Layout(*this); }
211 virtual void visit(NodeVisitor &);
214 struct InterfaceLayout: Statement
216 std::string interface;
219 virtual InterfaceLayout *clone() const { return new InterfaceLayout(*this); }
220 virtual void visit(NodeVisitor &);
223 struct StructDeclaration: Statement
230 virtual StructDeclaration *clone() const { return new StructDeclaration(*this); }
231 virtual void visit(NodeVisitor &);
234 struct VariableDeclaration: Statement
237 std::string sampling;
238 std::string interpolation;
239 std::string interface;
240 std::string precision;
242 StructDeclaration *type_declaration;
245 NodePtr<Expression> array_size;
246 NodePtr<Expression> init_expression;
247 VariableDeclaration *linked_declaration;
248 NodePtr<Layout> layout;
250 VariableDeclaration();
252 virtual VariableDeclaration *clone() const { return new VariableDeclaration(*this); }
253 virtual void visit(NodeVisitor &);
256 struct InterfaceBlock: Statement
258 std::string interface;
261 std::string instance_name;
266 virtual InterfaceBlock *clone() const { return new InterfaceBlock(*this); }
267 virtual void visit(NodeVisitor &);
270 struct FunctionDeclaration: Statement
272 std::string return_type;
274 NodeArray<VariableDeclaration> parameters;
275 FunctionDeclaration *definition;
278 FunctionDeclaration();
279 FunctionDeclaration(const FunctionDeclaration &);
281 virtual FunctionDeclaration *clone() const { return new FunctionDeclaration(*this); }
282 virtual void visit(NodeVisitor &);
285 struct Conditional: Statement
287 NodePtr<Expression> condition;
291 virtual Conditional *clone() const { return new Conditional(*this); }
292 virtual void visit(NodeVisitor &);
295 struct Iteration: Statement
297 NodePtr<Node> init_statement;
298 NodePtr<Expression> condition;
299 NodePtr<Expression> loop_expression;
302 virtual Iteration *clone() const { return new Iteration(*this); }
303 virtual void visit(NodeVisitor &);
306 struct Passthrough: Statement
308 NodePtr<Expression> subscript;
310 virtual Passthrough *clone() const { return new Passthrough(*this); }
311 virtual void visit(NodeVisitor &);
314 struct Return: Statement
316 NodePtr<Expression> expression;
318 virtual Return *clone() const { return new Return(*this); }
319 virtual void visit(NodeVisitor &);
322 struct Jump: Statement
326 virtual Jump *clone() const { return new Jump(*this); }
327 virtual void visit(NodeVisitor &);
332 virtual ~NodeVisitor() { }
334 virtual void visit(Block &) { }
335 virtual void visit(Literal &) { }
336 virtual void visit(ParenthesizedExpression &) { }
337 virtual void visit(VariableReference &) { }
338 virtual void visit(MemberAccess &) { }
339 virtual void visit(UnaryExpression &) { }
340 virtual void visit(BinaryExpression &) { }
341 virtual void visit(Assignment &);
342 virtual void visit(FunctionCall &) { }
343 virtual void visit(ExpressionStatement &) { }
344 virtual void visit(Import &) { }
345 virtual void visit(Precision &) { }
346 virtual void visit(Layout &) { }
347 virtual void visit(InterfaceLayout &) { }
348 virtual void visit(StructDeclaration &) { }
349 virtual void visit(VariableDeclaration &) { }
350 virtual void visit(InterfaceBlock &) { }
351 virtual void visit(FunctionDeclaration &) { }
352 virtual void visit(Conditional &) { }
353 virtual void visit(Iteration &) { }
354 virtual void visit(Passthrough &) { }
355 virtual void visit(Return &) { }
356 virtual void visit(Jump &) { }
359 struct TraversingVisitor: NodeVisitor
361 using NodeVisitor::visit;
362 virtual void visit(Block &);
363 virtual void visit(ParenthesizedExpression &);
364 virtual void visit(MemberAccess &);
365 virtual void visit(UnaryExpression &);
366 virtual void visit(BinaryExpression &);
367 virtual void visit(FunctionCall &);
368 virtual void visit(ExpressionStatement &);
369 virtual void visit(InterfaceLayout &);
370 virtual void visit(StructDeclaration &);
371 virtual void visit(VariableDeclaration &);
372 virtual void visit(InterfaceBlock &);
373 virtual void visit(FunctionDeclaration &);
374 virtual void visit(Conditional &);
375 virtual void visit(Iteration &);
376 virtual void visit(Passthrough &);
377 virtual void visit(Return &);
392 ProgramSyntax::Block content;
393 std::map<std::string, VariableDeclaration *> in_variables;
394 std::map<std::string, VariableDeclaration *> out_variables;
395 std::map<std::string, unsigned> locations;
396 Version required_version;
397 std::vector<const Extension *> required_extensions;
405 std::list<Stage> stages;
410 } // namespace ProgramSyntax
414 #pragma pop_macro("interface")