]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/generate.h
Make it deprecated to redeclare non-builtin variables
[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 /** Manipulates specialization constants.  If values are specified, turns
15 specialization constants into normal constants.  Without values assigns
16 automatic constant_ids to specialization constants. */
17 class ConstantSpecializer: private TraversingVisitor
18 {
19 private:
20         const std::map<std::string, int> *values;
21
22 public:
23         ConstantSpecializer();
24
25         void apply(Stage &, const std::map<std::string, int> *);
26
27 private:
28         virtual void visit(VariableDeclaration &);
29 };
30
31 /** Forms links between nested blocks in the syntax tree. */
32 class BlockHierarchyResolver: private TraversingVisitor
33 {
34 private:
35         bool r_any_resolved;
36
37 public:
38         BlockHierarchyResolver(): r_any_resolved(false) { }
39
40         bool apply(Stage &s) { r_any_resolved = false; s.content.visit(*this); return r_any_resolved; }
41
42 private:
43         virtual void enter(Block &);
44 };
45
46 /** Resolves types of variables and base types of other types. */
47 class TypeResolver: private TraversingVisitor
48 {
49 private:
50         Stage *stage;
51         std::map<TypeDeclaration *, TypeDeclaration *> alias_map;
52         std::map<TypeDeclaration *, TypeDeclaration *> array_types;
53         NodeList<Statement>::iterator type_insert_point;
54         InterfaceBlock *iface_block;
55         bool r_any_resolved;
56
57 public:
58         TypeResolver();
59
60         bool apply(Stage &);
61
62 private:
63         TypeDeclaration *get_or_create_array_type(TypeDeclaration &);
64         void resolve_type(TypeDeclaration *&, const std::string &, bool);
65         virtual void visit(Block &);
66         virtual void visit(BasicTypeDeclaration &);
67         virtual void visit(ImageTypeDeclaration &);
68         virtual void visit(StructDeclaration &);
69         virtual void visit(VariableDeclaration &);
70         virtual void visit(InterfaceBlock &);
71         virtual void visit(FunctionDeclaration &);
72 };
73
74 /** Resolves variable references.  Variable references which match the name
75 of an interface block are turned into interface block references. */
76 class VariableResolver: private TraversingVisitor
77 {
78 private:
79         Stage *stage;
80         RefPtr<Expression> r_replacement_expr;
81         bool r_any_resolved;
82         bool record_target;
83         bool r_self_referencing;
84         Assignment::Target r_assignment_target;
85         std::vector<VariableDeclaration *> redeclared_builtins;
86         std::set<Node *> nodes_to_remove;
87
88 public:
89         VariableResolver();
90
91         bool apply(Stage &);
92
93 private:
94         virtual void enter(Block &);
95         virtual void visit(RefPtr<Expression> &);
96         void check_assignment_target(Statement *);
97         virtual void visit(VariableReference &);
98         virtual void visit(InterfaceBlockReference &);
99         void add_to_chain(Assignment::Target::ChainType, unsigned);
100         virtual void visit(MemberAccess &);
101         virtual void visit(Swizzle &);
102         virtual void visit(BinaryExpression &);
103         virtual void visit(Assignment &);
104         void merge_layouts(Layout &, const Layout &);
105         virtual void visit(VariableDeclaration &);
106         virtual void visit(InterfaceBlock &);
107 };
108
109 /** Resolves types and lvalueness of expressions. */
110 class ExpressionResolver: private TraversingVisitor
111 {
112 private:
113         enum Compatibility
114         {
115                 NOT_COMPATIBLE,
116                 LEFT_CONVERTIBLE,
117                 RIGHT_CONVERTIBLE,
118                 SAME_TYPE
119         };
120
121         Stage *stage;
122         std::vector<BasicTypeDeclaration *> basic_types;
123         bool r_any_resolved;
124
125 public:
126         ExpressionResolver();
127
128         bool apply(Stage &);
129
130 private:
131         static bool is_scalar(BasicTypeDeclaration &);
132         static bool is_vector_or_matrix(BasicTypeDeclaration &);
133         static BasicTypeDeclaration *get_element_type(BasicTypeDeclaration &);
134         static bool can_convert(BasicTypeDeclaration &, BasicTypeDeclaration &);
135         static Compatibility get_compatibility(BasicTypeDeclaration &, BasicTypeDeclaration &);
136         BasicTypeDeclaration *find_type(BasicTypeDeclaration::Kind, unsigned);
137         BasicTypeDeclaration *find_type(BasicTypeDeclaration &, BasicTypeDeclaration::Kind, unsigned);
138         void convert_to(RefPtr<Expression> &, BasicTypeDeclaration &);
139         bool convert_to_element(RefPtr<Expression> &, BasicTypeDeclaration &);
140         void resolve(Expression &, TypeDeclaration *, bool);
141
142         virtual void visit(Literal &);
143         virtual void visit(ParenthesizedExpression &);
144         virtual void visit(VariableReference &);
145         virtual void visit(InterfaceBlockReference &);
146         virtual void visit(MemberAccess &);
147         virtual void visit(Swizzle &);
148         virtual void visit(UnaryExpression &);
149         void visit(BinaryExpression &, bool);
150         virtual void visit(BinaryExpression &);
151         virtual void visit(Assignment &);
152         virtual void visit(TernaryExpression &);
153         virtual void visit(FunctionCall &);
154         virtual void visit(BasicTypeDeclaration &);
155         virtual void visit(VariableDeclaration &);
156 };
157
158 /** Resolves function declarations and calls. */
159 class FunctionResolver: private TraversingVisitor
160 {
161 private:
162         Stage *stage;
163         std::map<std::string, std::vector<FunctionDeclaration *> > declarations;
164         bool r_any_resolved;
165
166 public:
167         bool apply(Stage &);
168
169 private:
170         virtual void visit(FunctionCall &);
171         virtual void visit(FunctionDeclaration &);
172 };
173
174 /** Materializes implicitly declared interfaces.
175
176 Out variable declarations inside functions are moved to the global scope.
177
178 Passthrough statements are processed, generating out variables to match in
179 variables and copying values.
180
181 Unresolved variables are looked up in the previous stage's out variables. */
182 class InterfaceGenerator: private TraversingVisitor
183 {
184 private:
185         Stage *stage;
186         std::string in_prefix;
187         std::string out_prefix;
188         bool function_scope;
189         bool copy_block;
190         std::vector<VariableDeclaration *> declared_inputs;
191         Block *iface_target_block;
192         NodeList<Statement>::iterator iface_insert_point;
193         NodeList<Statement>::iterator assignment_insert_point;
194         std::set<Node *> nodes_to_remove;
195
196 public:
197         InterfaceGenerator();
198
199         void apply(Stage &);
200
201 private:
202         static std::string get_out_prefix(Stage::Type);
203         std::string change_prefix(const std::string &, const std::string &) const;
204         virtual void visit(Block &);
205         VariableDeclaration *generate_interface(VariableDeclaration &, const std::string &, const std::string &);
206         InterfaceBlock *generate_interface(InterfaceBlock &);
207         ExpressionStatement &insert_assignment(const std::string &, Expression *);
208         virtual void visit(VariableReference &);
209         virtual void visit(VariableDeclaration &);
210         virtual void visit(InterfaceBlock &);
211         virtual void visit(FunctionDeclaration &);
212         virtual void visit(Passthrough &);
213 };
214
215 } // namespace SL
216 } // namespace GL
217 } // namespace Msp
218
219 #endif