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