]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/generate.h
Fix scoping of GLSL loop variables and function parameters
[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         virtual void visit(FunctionDeclaration &);
60         virtual void visit(Iteration &);
61         using TraversingVisitor::visit;
62 };
63
64 class FunctionResolver: private TraversingVisitor
65 {
66 private:
67         std::map<std::string, std::vector<FunctionDeclaration *> > functions;
68
69 public:
70         void apply(Stage &s) { visit(s.content); }
71
72 private:
73         virtual void visit(FunctionCall &);
74         virtual void visit(FunctionDeclaration &);
75         using TraversingVisitor::visit;
76 };
77
78 class InterfaceGenerator: private TraversingVisitor
79 {
80 private:
81         Stage *stage;
82         std::string in_prefix;
83         std::string out_prefix;
84         unsigned scope_level;
85         Block *current_block;
86         NodeList<Statement>::iterator iface_insert_point;
87         NodeList<Statement>::iterator assignment_insert_point;
88         std::set<Node *> nodes_to_remove;
89
90 public:
91         InterfaceGenerator();
92
93         void apply(Stage &);
94
95 private:
96         static std::string get_out_prefix(Stage::Type);
97         std::string change_prefix(const std::string &, const std::string &) const;
98         virtual void visit(Block &);
99         bool generate_interface(VariableDeclaration &, const std::string &, const std::string &);
100         ExpressionStatement &insert_assignment(const std::string &, Expression *);
101         virtual void visit(VariableReference &);
102         virtual void visit(VariableDeclaration &);
103         virtual void visit(Passthrough &);
104         using TraversingVisitor::visit;
105 };
106
107 class DeclarationReorderer: private TraversingVisitor
108 {
109 private:
110         enum DeclarationKind
111         {
112                 NO_DECLARATION,
113                 LAYOUT,
114                 STRUCT,
115                 VARIABLE,
116                 FUNCTION
117         };
118
119         unsigned scope_level;
120         DeclarationKind kind;
121         std::set<Node *> ordered_funcs;
122         std::set<Node *> needed_funcs;
123
124 public:
125         DeclarationReorderer();
126
127         void apply(Stage &s) { visit(s.content); }
128
129 private:
130         virtual void visit(Block &);
131         virtual void visit(FunctionCall &);
132         virtual void visit(InterfaceLayout &) { kind = LAYOUT; }
133         virtual void visit(StructDeclaration &) { kind = STRUCT; }
134         virtual void visit(VariableDeclaration &);
135         virtual void visit(InterfaceBlock &) { kind = VARIABLE; }
136         virtual void visit(FunctionDeclaration &);
137         using TraversingVisitor::visit;
138 };
139
140 } // namespace SL
141 } // namespace GL
142 } // namespace Msp
143
144 #endif