]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/reflect.h
Use default member initializers for simple types
[libs/gl.git] / source / glsl / reflect.h
1 #ifndef MSP_GL_SL_REFLECT_H_
2 #define MSP_GL_SL_REFLECT_H_
3
4 #include "visitor.h"
5
6 namespace Msp {
7 namespace GL {
8 namespace SL {
9
10 bool is_scalar(const BasicTypeDeclaration &);
11 bool is_vector_or_matrix(const BasicTypeDeclaration &);
12 BasicTypeDeclaration *get_element_type(BasicTypeDeclaration &);
13 bool can_convert(const BasicTypeDeclaration &, const BasicTypeDeclaration &);
14
15 /** Compares two types for equality.  Struct types are compared recursively. */
16 class TypeComparer: private NodeVisitor
17 {
18 private:
19         Node *first = 0;
20         Node *second = 0;
21         unsigned first_tag = 0;
22         bool r_result = false;
23
24         static unsigned next_tag;
25
26 public:
27         bool apply(TypeDeclaration &t1, TypeDeclaration &t2) { compare(t1, t2); return r_result; }
28
29 private:
30         void compare(Node &, Node &);
31         template<typename T>
32         T *multi_visit(T &);
33         virtual void visit(Literal &);
34         virtual void visit(VariableReference &);
35         virtual void visit(UnaryExpression &);
36         virtual void visit(BinaryExpression &);
37         virtual void visit(TernaryExpression &);
38         virtual void visit(BasicTypeDeclaration &);
39         virtual void visit(ImageTypeDeclaration &);
40         virtual void visit(StructDeclaration &);
41         virtual void visit(VariableDeclaration &);
42 };
43
44 /** Determines the number of interface locations required by a variable. */
45 class LocationCounter: private NodeVisitor
46 {
47 private:
48         unsigned r_count = 0;
49
50 public:
51         unsigned apply(VariableDeclaration &v) { v.visit(*this); return r_count; }
52
53 private:
54         virtual void visit(BasicTypeDeclaration &);
55         virtual void visit(ImageTypeDeclaration &);
56         virtual void visit(StructDeclaration &);
57         virtual void visit(VariableDeclaration &);
58 };
59
60 /** Determines the size and alignment of a variable or a type, in bytes. */
61 class MemoryRequirementsCalculator: private NodeVisitor
62 {
63 public:
64         struct Result
65         {
66                 unsigned size;
67                 unsigned alignment;
68                 unsigned stride;
69
70                 Result(unsigned s, unsigned a): size(s), alignment(a), stride(s+a-1-(s+a-1)%a) { }
71         };
72 private:
73         unsigned r_size = 0;
74         unsigned r_alignment = 1;
75         int r_offset = -1;
76
77 public:
78         Result apply(VariableDeclaration &v) { v.visit(*this); return Result(r_size, r_alignment); }
79         Result apply(TypeDeclaration &t) { t.visit(*this); return Result(r_size, r_alignment); }
80
81 private:
82         virtual void visit(BasicTypeDeclaration &);
83         virtual void visit(StructDeclaration &);
84         virtual void visit(VariableDeclaration &);
85 };
86
87 /** Collects dependencies of a function.  This includes global variables,
88 interface blocks, other functions and types. */
89 class DependencyCollector: private TraversingVisitor
90 {
91 private:
92         std::set<Node *> dependencies;
93         std::set<Node *> locals;
94         std::set<FunctionDeclaration *> visited_functions;
95
96 public:
97         std::set<Node *> apply(FunctionDeclaration &);
98
99 private:
100         virtual void visit(VariableReference &);
101         virtual void visit(InterfaceBlockReference &);
102         virtual void visit(FunctionCall &);
103         virtual void visit(VariableDeclaration &);
104         virtual void visit(FunctionDeclaration &);
105 };
106
107 } // namespace SL
108 } // namespace GL
109 } // namespace Msp
110
111 #endif