]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/generate.h
Resolve the return types of functions
[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 /** Combines multiple declarations of the same identifier into one. */
15 class DeclarationCombiner: private TraversingVisitor
16 {
17 private:
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         void apply(Stage &);
24
25 private:
26         virtual void visit(Block &);
27         virtual void visit(VariableDeclaration &);
28         virtual void visit(FunctionDeclaration &) { }
29 };
30
31 /** Manipulates specialization constants.  If values are specified, turns
32 specialization constants into normal constants.  Without values assigns
33 automatic constant_ids to specialization constants. */
34 class ConstantSpecializer: private TraversingVisitor
35 {
36 private:
37         const std::map<std::string, int> *values;
38
39 public:
40         ConstantSpecializer();
41
42         void apply(Stage &, const std::map<std::string, int> *);
43
44 private:
45         virtual void visit(VariableDeclaration &);
46 };
47
48 /** Forms links between nested blocks in the syntax tree. */
49 class BlockHierarchyResolver: private TraversingVisitor
50 {
51 public:
52         void apply(Stage &s) { s.content.visit(*this); }
53
54 private:
55         virtual void enter(Block &);
56 };
57
58 /** Resolves types of variables and base types of other types. */
59 class TypeResolver: private TraversingVisitor
60 {
61 private:
62         Stage *stage;
63
64 public:
65         TypeResolver();
66
67         void apply(Stage &);
68
69 private:
70         virtual void visit(BasicTypeDeclaration &);
71         virtual void visit(ImageTypeDeclaration &);
72         virtual void visit(StructDeclaration &);
73         virtual void visit(VariableDeclaration &);
74         virtual void visit(FunctionDeclaration &);
75 };
76
77 /** Resolves variable references.  Variable references which match the name
78 of an interface block are turned into interface block references. */
79 class VariableResolver: private TraversingVisitor
80 {
81 private:
82         Stage *stage;
83         std::map<std::string, VariableDeclaration *> *r_members;
84         RefPtr<InterfaceBlockReference> r_iface_ref;
85         std::string block_interface;
86         bool record_target;
87         bool r_self_referencing;
88         VariableDeclaration *r_assignment_target;
89
90 public:
91         VariableResolver();
92
93         void apply(Stage &);
94
95 private:
96         virtual void enter(Block &);
97         virtual void visit(VariableReference &);
98         virtual void visit(InterfaceBlockReference &);
99         virtual void visit(MemberAccess &);
100         virtual void visit(UnaryExpression &);
101         virtual void visit(BinaryExpression &);
102         virtual void visit(Assignment &);
103         virtual void visit(FunctionCall &);
104         virtual void visit(VariableDeclaration &);
105         virtual void visit(InterfaceBlock &);
106 };
107
108 /** Resolves function declarations and calls. */
109 class FunctionResolver: private TraversingVisitor
110 {
111 private:
112         Stage *stage;
113         std::map<std::string, std::vector<FunctionDeclaration *> > declarations;
114
115 public:
116         void apply(Stage &);
117
118 private:
119         virtual void visit(FunctionCall &);
120         virtual void visit(FunctionDeclaration &);
121 };
122
123 /** Materializes implicitly declared interfaces.
124
125 Out variable declarations inside functions are moved to the global scope.
126
127 Passthrough statements are processed, generating out variables to match in
128 variables and copying values.
129
130 Unresolved variables are looked up in the previous stage's out variables. */
131 class InterfaceGenerator: private TraversingVisitor
132 {
133 private:
134         Stage *stage;
135         std::string in_prefix;
136         std::string out_prefix;
137         bool function_scope;
138         InterfaceBlock *iface_block;
139         bool copy_block;
140         Block *iface_target_block;
141         NodeList<Statement>::iterator iface_insert_point;
142         NodeList<Statement>::iterator assignment_insert_point;
143         std::set<Node *> nodes_to_remove;
144
145 public:
146         InterfaceGenerator();
147
148         void apply(Stage &);
149
150 private:
151         static std::string get_out_prefix(Stage::Type);
152         std::string change_prefix(const std::string &, const std::string &) const;
153         virtual void visit(Block &);
154         VariableDeclaration *generate_interface(VariableDeclaration &, const std::string &, const std::string &);
155         InterfaceBlock *generate_interface(InterfaceBlock &);
156         ExpressionStatement &insert_assignment(const std::string &, Expression *);
157         virtual void visit(VariableReference &);
158         virtual void visit(VariableDeclaration &);
159         virtual void visit(InterfaceBlock &);
160         virtual void visit(FunctionDeclaration &);
161         virtual void visit(Passthrough &);
162 };
163
164 } // namespace SL
165 } // namespace GL
166 } // namespace Msp
167
168 #endif