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