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