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