]> git.tdb.fi Git - libs/gl.git/blob - source/programsyntax.h
Inject the global context to the syntax tree
[libs/gl.git] / source / programsyntax.h
1 #ifndef MSP_GL_PROGRAMSYNTAX_H_
2 #define MSP_GL_PROGRAMSYNTAX_H_
3
4 #include <list>
5 #include <map>
6 #include <string>
7 #include <vector>
8
9 namespace Msp {
10 namespace GL {
11 namespace ProgramSyntax {
12
13 struct NodeVisitor;
14
15 struct Node
16 {
17 private:
18         Node &operator=(const Node &);
19 public:
20         virtual ~Node() { }
21
22         virtual Node *clone() const = 0;
23         virtual void visit(NodeVisitor &) = 0;
24 };
25
26 template<typename T>
27 class NodePtr
28 {
29 private:
30         T *node;
31
32 public:
33         NodePtr(T *n = 0): node(n) { }
34         NodePtr(const NodePtr &p): node(clone(p.node)) { }
35         NodePtr &operator=(const NodePtr &p) { delete node; node = clone(p.node); return *this; }
36         ~NodePtr() { delete node; }
37
38 private:
39         static T *clone(T *n) { return n ? n->clone() : 0; }
40
41 public:
42         T *operator->() { return node; }
43         const T *operator->() const { return node; }
44         operator void *() const { return node; }
45 };
46
47 struct VariableDeclaration;
48
49 struct Block: Node
50 {
51         std::list<NodePtr<Node> > body;
52         bool use_braces;
53
54         Block();
55
56         virtual Block *clone() const { return new Block(*this); }
57         virtual void visit(NodeVisitor &);
58 };
59
60 struct Expression: Node
61 {
62         virtual Expression *clone() const = 0;
63 };
64
65 struct Literal: Expression
66 {
67         std::string token;
68
69         virtual Literal *clone() const { return new Literal(*this); }
70         virtual void visit(NodeVisitor &);
71 };
72
73 struct ParenthesizedExpression: Expression
74 {
75         NodePtr<Expression> expression;
76
77         virtual ParenthesizedExpression *clone() const { return new ParenthesizedExpression(*this); }
78         virtual void visit(NodeVisitor &);
79 };
80
81 struct VariableReference: Expression
82 {
83         std::string name;
84
85         virtual VariableReference *clone() const { return new VariableReference(*this); }
86         virtual void visit(NodeVisitor &);
87 };
88
89 struct MemberAccess: Expression
90 {
91         NodePtr<Expression> left;
92         std::string member;
93
94         virtual MemberAccess *clone() const { return new MemberAccess(*this); }
95         virtual void visit(NodeVisitor &);
96 };
97
98 struct UnaryExpression: Expression
99 {
100         std::string oper;
101         NodePtr<Expression> expression;
102         bool prefix;
103
104         UnaryExpression();
105
106         virtual UnaryExpression *clone() const { return new UnaryExpression(*this); }
107         virtual void visit(NodeVisitor &);
108 };
109
110 struct BinaryExpression: Expression
111 {
112         NodePtr<Expression> left;
113         std::string oper;
114         NodePtr<Expression> right;
115         std::string after;
116         bool assignment;
117
118         BinaryExpression();
119
120         virtual BinaryExpression *clone() const { return new BinaryExpression(*this); }
121         virtual void visit(NodeVisitor &);
122 };
123
124 struct FunctionCall: Expression
125 {
126         std::string name;
127         bool constructor;
128         std::vector<NodePtr<Expression> > arguments;
129
130         FunctionCall();
131
132         virtual FunctionCall *clone() const { return new FunctionCall(*this); }
133         virtual void visit(NodeVisitor &);
134 };
135
136 struct ExpressionStatement: Node
137 {
138         NodePtr<Expression> expression;
139
140         virtual ExpressionStatement *clone() const { return new ExpressionStatement(*this); }
141         virtual void visit(NodeVisitor &);
142 };
143
144 struct Layout: Node
145 {
146         struct Qualifier
147         {
148                 std::string identifier;
149                 std::string value;
150         };
151
152         std::vector<Qualifier> qualifiers;
153         std::string interface;
154
155         virtual Layout *clone() const { return new Layout(*this); }
156         virtual void visit(NodeVisitor &);
157 };
158
159 struct StructDeclaration: Node
160 {
161         std::string name;
162         Block members;
163
164         StructDeclaration();
165
166         virtual StructDeclaration *clone() const { return new StructDeclaration(*this); }
167         virtual void visit(NodeVisitor &);
168 };
169
170 struct VariableDeclaration: Node
171 {
172         bool constant;
173         std::string sampling;
174         std::string interface;
175         std::string type;
176         std::string name;
177         bool array;
178         NodePtr<Expression> array_size;
179         NodePtr<Expression> init_expression;
180
181         VariableDeclaration();
182
183         virtual VariableDeclaration *clone() const { return new VariableDeclaration(*this); }
184         virtual void visit(NodeVisitor &);
185 };
186
187 struct InterfaceBlock: Node
188 {
189         std::string interface;
190         std::string name;
191         Block members;
192
193         InterfaceBlock();
194
195         virtual InterfaceBlock *clone() const { return new InterfaceBlock(*this); }
196         virtual void visit(NodeVisitor &);
197 };
198
199 struct FunctionDeclaration: Node
200 {
201         std::string return_type;
202         std::string name;
203         std::vector<NodePtr<VariableDeclaration> > parameters;
204         bool definition;
205         Block body;
206
207         FunctionDeclaration();
208
209         virtual FunctionDeclaration *clone() const { return new FunctionDeclaration(*this); }
210         virtual void visit(NodeVisitor &);
211 };
212
213 struct Conditional: Node
214 {
215         Expression *condition;
216         Block body;
217         Block else_body;
218
219         virtual Conditional *clone() const { return new Conditional(*this); }
220         virtual void visit(NodeVisitor &);
221 };
222
223 struct Iteration: Node
224 {
225         NodePtr<Node> init_statement;
226         NodePtr<Expression> condition;
227         NodePtr<Expression> loop_expression;
228         Block body;
229
230         virtual Iteration *clone() const { return new Iteration(*this); }
231         virtual void visit(NodeVisitor &);
232 };
233
234 struct Return: Node
235 {
236         NodePtr<Expression> expression;
237
238         virtual Return *clone() const { return new Return(*this); }
239         virtual void visit(NodeVisitor &);
240 };
241
242 struct NodeVisitor
243 {
244         virtual ~NodeVisitor() { }
245
246         virtual void visit(Block &) { }
247         virtual void visit(Literal &) { }
248         virtual void visit(ParenthesizedExpression &) { }
249         virtual void visit(VariableReference &) { }
250         virtual void visit(MemberAccess &) { }
251         virtual void visit(UnaryExpression &) { }
252         virtual void visit(BinaryExpression &) { }
253         virtual void visit(FunctionCall &) { }
254         virtual void visit(ExpressionStatement &) { }
255         virtual void visit(Layout &) { }
256         virtual void visit(StructDeclaration &) { }
257         virtual void visit(VariableDeclaration &) { }
258         virtual void visit(InterfaceBlock &) { }
259         virtual void visit(FunctionDeclaration &) { }
260         virtual void visit(Conditional &) { }
261         virtual void visit(Iteration &) { }
262         virtual void visit(Return &) { }
263 };
264
265 enum ContextType
266 {
267         GLOBAL,
268         VERTEX,
269         GEOMETRY,
270         FRAGMENT
271 };
272
273 struct Context
274 {
275         ContextType type;
276         bool present;
277         ProgramSyntax::Block content;
278
279         Context(ContextType);
280 };
281
282 struct Module
283 {
284         Context global_context;
285         Context vertex_context;
286         Context geometry_context;
287         Context fragment_context;
288         std::map<std::string, StructDeclaration *> structs;
289
290         Module();
291 };
292
293 } // namespace ProgramSyntax
294 } // namespace GL
295 } // namespace Msp
296
297 #endif