]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/generate.h
Rewrite syntax tree modifications
[libs/gl.git] / source / glsl / generate.h
1 #ifndef MSP_GL_SL_GENERATE_H_
2 #define MSP_GL_SL_GENERATE_H_
3
4 #include <map>
5 #include <set>
6 #include <string>
7 #include <vector>
8 #include "visitor.h"
9
10 namespace Msp {
11 namespace GL {
12 namespace SL {
13
14 class DeclarationCombiner: private TraversingVisitor
15 {
16 private:
17         bool toplevel;
18         std::map<std::string, std::vector<FunctionDeclaration *> > functions;
19         std::map<std::string, VariableDeclaration *> variables;
20         std::set<Node *> nodes_to_remove;
21
22 public:
23         DeclarationCombiner();
24
25         void apply(Stage &);
26
27 private:
28         virtual void visit(Block &);
29         virtual void visit(FunctionDeclaration &);
30         virtual void visit(VariableDeclaration &);
31         using TraversingVisitor::visit;
32 };
33
34 class VariableResolver: private TraversingVisitor
35 {
36 private:
37         std::vector<Block *> blocks;
38         StructDeclaration *type;
39         bool anonymous;
40         std::string block_interface;
41         bool record_target;
42         VariableDeclaration *assignment_target;
43         bool self_referencing;
44
45 public:
46         VariableResolver();
47
48         void apply(Stage &);
49
50 private:
51         virtual void visit(Block &);
52         virtual void visit(VariableReference &);
53         virtual void visit(MemberAccess &);
54         virtual void visit(BinaryExpression &);
55         virtual void visit(Assignment &);
56         virtual void visit(StructDeclaration &);
57         virtual void visit(VariableDeclaration &);
58         virtual void visit(InterfaceBlock &);
59         using TraversingVisitor::visit;
60 };
61
62 class FunctionResolver: private TraversingVisitor
63 {
64 private:
65         std::map<std::string, std::vector<FunctionDeclaration *> > functions;
66
67 public:
68         void apply(Stage &s) { visit(s.content); }
69
70 private:
71         virtual void visit(FunctionCall &);
72         virtual void visit(FunctionDeclaration &);
73         using TraversingVisitor::visit;
74 };
75
76 class InterfaceGenerator: private TraversingVisitor
77 {
78 private:
79         Stage *stage;
80         std::string in_prefix;
81         std::string out_prefix;
82         unsigned scope_level;
83         Block *current_block;
84         NodeList<Statement>::iterator iface_insert_point;
85         NodeList<Statement>::iterator assignment_insert_point;
86         std::set<Node *> nodes_to_remove;
87
88 public:
89         InterfaceGenerator();
90
91         void apply(Stage &);
92
93 private:
94         static std::string get_out_prefix(Stage::Type);
95         std::string change_prefix(const std::string &, const std::string &) const;
96         virtual void visit(Block &);
97         bool generate_interface(VariableDeclaration &, const std::string &, const std::string &);
98         ExpressionStatement &insert_assignment(const std::string &, Expression *);
99         virtual void visit(VariableReference &);
100         virtual void visit(VariableDeclaration &);
101         virtual void visit(Passthrough &);
102         using TraversingVisitor::visit;
103 };
104
105 class DeclarationReorderer: private TraversingVisitor
106 {
107 private:
108         enum DeclarationKind
109         {
110                 NO_DECLARATION,
111                 LAYOUT,
112                 STRUCT,
113                 VARIABLE,
114                 FUNCTION
115         };
116
117         unsigned scope_level;
118         DeclarationKind kind;
119         std::set<Node *> ordered_funcs;
120         std::set<Node *> needed_funcs;
121
122 public:
123         DeclarationReorderer();
124
125         void apply(Stage &s) { visit(s.content); }
126
127 private:
128         virtual void visit(Block &);
129         virtual void visit(FunctionCall &);
130         virtual void visit(InterfaceLayout &) { kind = LAYOUT; }
131         virtual void visit(StructDeclaration &) { kind = STRUCT; }
132         virtual void visit(VariableDeclaration &);
133         virtual void visit(InterfaceBlock &) { kind = VARIABLE; }
134         virtual void visit(FunctionDeclaration &);
135         using TraversingVisitor::visit;
136 };
137
138 } // namespace SL
139 } // namespace GL
140 } // namespace Msp
141
142 #endif