1 #ifndef MSP_GL_PROGRAMSYNTAX_H_
2 #define MSP_GL_PROGRAMSYNTAX_H_
8 #include <msp/core/refptr.h>
13 namespace ProgramSyntax {
20 Node &operator=(const Node &);
24 virtual Node *clone() const = 0;
25 virtual void visit(NodeVisitor &) = 0;
29 class NodePtr: public RefPtr<T>
33 NodePtr(T *p): RefPtr<T>(p) { }
34 NodePtr(const NodePtr &p): RefPtr<T>(p ? p->clone() : 0) { }
37 NodePtr(const RefPtr<U> &p): RefPtr<T>(p) { }
40 NodePtr(const NodePtr<U> &p): RefPtr<T>(p ? p->clone() : 0) { }
44 class NodeContainer: public C
48 NodeContainer(const NodeContainer &);
52 class NodeList: public NodeContainer<std::list<RefPtr<T> > >
56 class NodeArray: public NodeContainer<std::vector<RefPtr<T> > >
59 struct StructDeclaration;
60 struct VariableDeclaration;
61 struct FunctionDeclaration;
67 std::map<std::string, StructDeclaration *> types;
68 std::map<std::string, VariableDeclaration *> variables;
72 virtual Block *clone() const { return new Block(*this); }
73 virtual void visit(NodeVisitor &);
76 struct Expression: Node
78 virtual Expression *clone() const = 0;
81 struct Literal: Expression
85 virtual Literal *clone() const { return new Literal(*this); }
86 virtual void visit(NodeVisitor &);
89 struct ParenthesizedExpression: Expression
91 NodePtr<Expression> expression;
93 virtual ParenthesizedExpression *clone() const { return new ParenthesizedExpression(*this); }
94 virtual void visit(NodeVisitor &);
97 struct VariableReference: Expression
100 VariableDeclaration *declaration;
104 virtual VariableReference *clone() const { return new VariableReference(*this); }
105 virtual void visit(NodeVisitor &);
108 struct MemberAccess: Expression
110 NodePtr<Expression> left;
112 VariableDeclaration *declaration;
114 virtual MemberAccess *clone() const { return new MemberAccess(*this); }
115 virtual void visit(NodeVisitor &);
118 struct UnaryExpression: Expression
121 NodePtr<Expression> expression;
126 virtual UnaryExpression *clone() const { return new UnaryExpression(*this); }
127 virtual void visit(NodeVisitor &);
130 struct BinaryExpression: Expression
132 NodePtr<Expression> left;
134 NodePtr<Expression> right;
137 virtual BinaryExpression *clone() const { return new BinaryExpression(*this); }
138 virtual void visit(NodeVisitor &);
141 struct Assignment: BinaryExpression
143 bool self_referencing;
144 VariableDeclaration *target_declaration;
148 virtual Assignment *clone() const { return new Assignment(*this); }
149 virtual void visit(NodeVisitor &);
152 struct FunctionCall: Expression
155 FunctionDeclaration *declaration;
157 NodeArray<Expression> arguments;
161 virtual FunctionCall *clone() const { return new FunctionCall(*this); }
162 virtual void visit(NodeVisitor &);
165 struct ExpressionStatement: Node
167 NodePtr<Expression> expression;
169 virtual ExpressionStatement *clone() const { return new ExpressionStatement(*this); }
170 virtual void visit(NodeVisitor &);
177 virtual Import *clone() const { return new Import(*this); }
178 virtual void visit(NodeVisitor &);
181 struct Precision: Node
183 std::string precision;
186 virtual Precision *clone() const { return new Precision(*this); }
187 virtual void visit(NodeVisitor &);
194 // TODO the standard calls this name, not identifier
195 std::string identifier;
199 std::vector<Qualifier> qualifiers;
201 virtual Layout *clone() const { return new Layout(*this); }
202 virtual void visit(NodeVisitor &);
205 struct InterfaceLayout: Node
207 std::string interface;
210 virtual InterfaceLayout *clone() const { return new InterfaceLayout(*this); }
211 virtual void visit(NodeVisitor &);
214 struct StructDeclaration: Node
221 virtual StructDeclaration *clone() const { return new StructDeclaration(*this); }
222 virtual void visit(NodeVisitor &);
225 struct VariableDeclaration: Node
228 std::string sampling;
229 std::string interface;
230 std::string precision;
232 StructDeclaration *type_declaration;
235 NodePtr<Expression> array_size;
236 NodePtr<Expression> init_expression;
237 VariableDeclaration *linked_declaration;
238 NodePtr<Layout> layout;
240 VariableDeclaration();
242 virtual VariableDeclaration *clone() const { return new VariableDeclaration(*this); }
243 virtual void visit(NodeVisitor &);
246 struct InterfaceBlock: Node
248 std::string interface;
251 std::string instance_name;
256 virtual InterfaceBlock *clone() const { return new InterfaceBlock(*this); }
257 virtual void visit(NodeVisitor &);
260 struct FunctionDeclaration: Node
262 std::string return_type;
264 NodeArray<VariableDeclaration> parameters;
265 FunctionDeclaration *definition;
268 FunctionDeclaration();
269 FunctionDeclaration(const FunctionDeclaration &);
271 virtual FunctionDeclaration *clone() const { return new FunctionDeclaration(*this); }
272 virtual void visit(NodeVisitor &);
275 struct Conditional: Node
277 NodePtr<Expression> condition;
281 virtual Conditional *clone() const { return new Conditional(*this); }
282 virtual void visit(NodeVisitor &);
285 struct Iteration: Node
287 NodePtr<Node> init_statement;
288 NodePtr<Expression> condition;
289 NodePtr<Expression> loop_expression;
292 virtual Iteration *clone() const { return new Iteration(*this); }
293 virtual void visit(NodeVisitor &);
296 struct Passthrough: Node
298 NodePtr<Expression> subscript;
300 virtual Passthrough *clone() const { return new Passthrough(*this); }
301 virtual void visit(NodeVisitor &);
306 NodePtr<Expression> expression;
308 virtual Return *clone() const { return new Return(*this); }
309 virtual void visit(NodeVisitor &);
316 virtual Jump *clone() const { return new Jump(*this); }
317 virtual void visit(NodeVisitor &);
322 virtual ~NodeVisitor() { }
324 virtual void visit(Block &) { }
325 virtual void visit(Literal &) { }
326 virtual void visit(ParenthesizedExpression &) { }
327 virtual void visit(VariableReference &) { }
328 virtual void visit(MemberAccess &) { }
329 virtual void visit(UnaryExpression &) { }
330 virtual void visit(BinaryExpression &) { }
331 virtual void visit(Assignment &);
332 virtual void visit(FunctionCall &) { }
333 virtual void visit(ExpressionStatement &) { }
334 virtual void visit(Import &) { }
335 virtual void visit(Precision &) { }
336 virtual void visit(Layout &) { }
337 virtual void visit(InterfaceLayout &) { }
338 virtual void visit(StructDeclaration &) { }
339 virtual void visit(VariableDeclaration &) { }
340 virtual void visit(InterfaceBlock &) { }
341 virtual void visit(FunctionDeclaration &) { }
342 virtual void visit(Conditional &) { }
343 virtual void visit(Iteration &) { }
344 virtual void visit(Passthrough &) { }
345 virtual void visit(Return &) { }
346 virtual void visit(Jump &) { }
349 struct TraversingVisitor: NodeVisitor
351 using NodeVisitor::visit;
352 virtual void visit(Block &);
353 virtual void visit(ParenthesizedExpression &);
354 virtual void visit(MemberAccess &);
355 virtual void visit(UnaryExpression &);
356 virtual void visit(BinaryExpression &);
357 virtual void visit(FunctionCall &);
358 virtual void visit(ExpressionStatement &);
359 virtual void visit(InterfaceLayout &);
360 virtual void visit(StructDeclaration &);
361 virtual void visit(VariableDeclaration &);
362 virtual void visit(InterfaceBlock &);
363 virtual void visit(FunctionDeclaration &);
364 virtual void visit(Conditional &);
365 virtual void visit(Iteration &);
366 virtual void visit(Passthrough &);
367 virtual void visit(Return &);
382 ProgramSyntax::Block content;
383 std::map<std::string, VariableDeclaration *> in_variables;
384 std::map<std::string, VariableDeclaration *> out_variables;
385 std::map<std::string, unsigned> locations;
386 Version required_version;
394 std::list<Stage> stages;
399 } // namespace ProgramSyntax