]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/resolve.h
Refactor add_to_chain as a common utility function
[libs/gl.git] / source / glsl / resolve.h
1 #ifndef MSP_GL_SL_RESOLVE_H_
2 #define MSP_GL_SL_RESOLVE_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 /** Forms links between nested blocks in the syntax tree. */
15 class BlockHierarchyResolver: private TraversingVisitor
16 {
17 private:
18         bool r_any_resolved;
19
20 public:
21         BlockHierarchyResolver(): r_any_resolved(false) { }
22
23         bool apply(Stage &s) { r_any_resolved = false; s.content.visit(*this); return r_any_resolved; }
24
25 private:
26         virtual void enter(Block &);
27 };
28
29 /** Resolves types of variables and base types of other types. */
30 class TypeResolver: private TraversingVisitor
31 {
32 private:
33         Stage *stage;
34         std::map<TypeDeclaration *, TypeDeclaration *> alias_map;
35         std::map<TypeDeclaration *, TypeDeclaration *> array_types;
36         NodeList<Statement>::iterator type_insert_point;
37         InterfaceBlock *iface_block;
38         bool r_any_resolved;
39
40 public:
41         TypeResolver();
42
43         bool apply(Stage &);
44
45 private:
46         TypeDeclaration *get_or_create_array_type(TypeDeclaration &);
47         void resolve_type(TypeDeclaration *&, const std::string &, bool);
48         virtual void visit(Block &);
49         virtual void visit(BasicTypeDeclaration &);
50         virtual void visit(ImageTypeDeclaration &);
51         virtual void visit(StructDeclaration &);
52         virtual void visit(VariableDeclaration &);
53         virtual void visit(InterfaceBlock &);
54         virtual void visit(FunctionDeclaration &);
55 };
56
57 /** Resolves variable references.  Variable references which match the name
58 of an interface block are turned into interface block references. */
59 class VariableResolver: private TraversingVisitor
60 {
61 private:
62         Stage *stage;
63         RefPtr<Expression> r_replacement_expr;
64         bool r_any_resolved;
65         bool record_target;
66         bool r_self_referencing;
67         Assignment::Target r_assignment_target;
68         std::vector<VariableDeclaration *> redeclared_builtins;
69         std::set<Node *> nodes_to_remove;
70
71 public:
72         VariableResolver();
73
74         bool apply(Stage &);
75
76 private:
77         virtual void enter(Block &);
78         virtual void visit(RefPtr<Expression> &);
79         void check_assignment_target(Statement *);
80         virtual void visit(VariableReference &);
81         virtual void visit(InterfaceBlockReference &);
82         virtual void visit(MemberAccess &);
83         virtual void visit(Swizzle &);
84         virtual void visit(BinaryExpression &);
85         virtual void visit(Assignment &);
86         void merge_layouts(Layout &, const Layout &);
87         virtual void visit(VariableDeclaration &);
88         virtual void visit(InterfaceBlock &);
89 };
90
91 /** Resolves types and lvalueness of expressions. */
92 class ExpressionResolver: private TraversingVisitor
93 {
94 private:
95         enum Compatibility
96         {
97                 NOT_COMPATIBLE,
98                 LEFT_CONVERTIBLE,
99                 RIGHT_CONVERTIBLE,
100                 SAME_TYPE
101         };
102
103         struct ArgumentInfo
104         {
105                 BasicTypeDeclaration *type;
106                 unsigned component_count;
107
108                 ArgumentInfo(): type(0), component_count(0) { }
109         };
110
111         Stage *stage;
112         std::vector<BasicTypeDeclaration *> basic_types;
113         NodeList<Statement>::iterator insert_point;
114         bool r_any_resolved;
115
116 public:
117         ExpressionResolver();
118
119         bool apply(Stage &);
120
121 private:
122         static bool is_scalar(BasicTypeDeclaration &);
123         static bool is_vector_or_matrix(BasicTypeDeclaration &);
124         static BasicTypeDeclaration *get_element_type(BasicTypeDeclaration &);
125         static bool can_convert(BasicTypeDeclaration &, BasicTypeDeclaration &);
126         static Compatibility get_compatibility(BasicTypeDeclaration &, BasicTypeDeclaration &);
127         BasicTypeDeclaration *find_type(BasicTypeDeclaration::Kind, unsigned);
128         BasicTypeDeclaration *find_type(BasicTypeDeclaration &, BasicTypeDeclaration::Kind, unsigned);
129         void convert_to(RefPtr<Expression> &, BasicTypeDeclaration &);
130         bool convert_to_element(RefPtr<Expression> &, BasicTypeDeclaration &);
131         bool truncate_vector(RefPtr<Expression> &, unsigned);
132         void resolve(Expression &, TypeDeclaration *, bool);
133
134         virtual void visit(Block &);
135         virtual void visit(Literal &);
136         virtual void visit(VariableReference &);
137         virtual void visit(InterfaceBlockReference &);
138         virtual void visit(MemberAccess &);
139         virtual void visit(Swizzle &);
140         virtual void visit(UnaryExpression &);
141         void visit(BinaryExpression &, bool);
142         virtual void visit(BinaryExpression &);
143         virtual void visit(Assignment &);
144         virtual void visit(TernaryExpression &);
145         void visit_constructor(FunctionCall &);
146         virtual void visit(FunctionCall &);
147         virtual void visit(BasicTypeDeclaration &);
148         virtual void visit(VariableDeclaration &);
149 };
150
151 /** Resolves function declarations and calls. */
152 class FunctionResolver: private TraversingVisitor
153 {
154 private:
155         Stage *stage;
156         std::map<std::string, std::vector<FunctionDeclaration *> > declarations;
157         bool r_any_resolved;
158
159 public:
160         bool apply(Stage &);
161
162 private:
163         virtual void visit(FunctionCall &);
164         virtual void visit(FunctionDeclaration &);
165 };
166
167 } // namespace SL
168 } // namespace GL
169 } // namespace Msp
170
171 #endif