]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/validate.cpp
Transform interface block contents into structs
[libs/gl.git] / source / glsl / validate.cpp
1 #include <cstring>
2 #include <msp/core/raii.h>
3 #include <msp/strings/format.h>
4 #include "validate.h"
5
6 using namespace std;
7
8 namespace Msp {
9 namespace GL {
10 namespace SL {
11
12 Validator::Validator():
13         stage(0)
14 { }
15
16 void Validator::diagnose(Node &node, Diagnostic::Severity severity, const string &message)
17 {
18         Diagnostic diag;
19         diag.severity = severity;
20         diag.source = node.source;
21         diag.line = node.line;
22         diag.message = message;
23         stage->diagnostics.push_back(diag);
24 }
25
26
27 TypeValidator::TypeValidator():
28         in_struct(false)
29 { }
30
31 void TypeValidator::visit(BasicTypeDeclaration &type)
32 {
33         if(type.kind==BasicTypeDeclaration::VECTOR)
34         {
35                 BasicTypeDeclaration::Kind base_kind = BasicTypeDeclaration::VOID;
36                 if(BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type))
37                         base_kind = basic_base->kind;
38                 if(base_kind!=BasicTypeDeclaration::BOOL && base_kind!=BasicTypeDeclaration::INT && base_kind!=BasicTypeDeclaration::FLOAT)
39                         error(type, format("Invalid base type '%s' for vector type '%s'", type.base, type.name));
40         }
41         else if(type.kind==BasicTypeDeclaration::ARRAY)
42         {
43                 if(BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type))
44                         if(basic_base->kind==BasicTypeDeclaration::VOID)
45                                 error(type, format("Invalid base type '%s' for array type '%s'", type.base, type.name));
46         }
47 }
48
49 void TypeValidator::visit(ImageTypeDeclaration &type)
50 {
51         BasicTypeDeclaration::Kind base_kind = BasicTypeDeclaration::VOID;
52         if(BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type))
53                 base_kind = basic_base->kind;
54         if(base_kind!=BasicTypeDeclaration::INT && base_kind!=BasicTypeDeclaration::FLOAT)
55                 error(type, format("Invalid base type '%s' for image type '%s'", type.base, type.name));
56 }
57
58 void TypeValidator::visit(StructDeclaration &strct)
59 {
60         SetFlag set_struct(in_struct);
61         TraversingVisitor::visit(strct);
62 }
63
64 void TypeValidator::visit(VariableDeclaration &var)
65 {
66         if(in_struct)
67         {
68                 if(var.layout)
69                         error(var, "Struct members can't have layouts");
70                 if(var.init_expression)
71                         error(var, "Struct members can't have initializers");
72         }
73
74         TraversingVisitor::visit(var);
75 }
76
77
78 DeclarationValidator::DeclarationValidator():
79         anonymous_block(false)
80 { }
81
82 void DeclarationValidator::multiple_definition(const string &name, Statement &statement, Statement &previous)
83 {
84         error(statement, format("Multiple definition of %s", name));
85         diagnose(previous, Diagnostic::INFO, "Previous definition is here");
86 }
87
88 Statement *DeclarationValidator::find_definition(const string &name)
89 {
90         BlockDeclarationMap *decls = &declarations[current_block];
91         BlockDeclarationMap::const_iterator i = decls->find(name);
92         if(i==decls->end() && anonymous_block)
93         {
94                 decls = &declarations[current_block->parent];
95                 i = decls->find(name);
96         }
97         return (i!=decls->end() ? i->second : 0);
98 }
99
100 void DeclarationValidator::check_definition(const string &name, Statement &statement)
101 {
102         if(Statement *previous = find_definition(name))
103                 multiple_definition(format("'%s'", name), statement, *previous);
104         else
105                 record_definition(name, statement);
106 }
107
108 void DeclarationValidator::record_definition(const string &name, Statement &statement)
109 {
110         declarations[current_block].insert(make_pair(name, &statement));
111         if(anonymous_block)
112                 declarations[current_block->parent].insert(make_pair(name, &statement));
113 }
114
115 void DeclarationValidator::visit(TypeDeclaration &type)
116 {
117         check_definition(type.name, type);
118 }
119
120 void DeclarationValidator::visit(StructDeclaration &strct)
121 {
122         check_definition(strct.name, strct);
123         TraversingVisitor::visit(strct);
124 }
125
126 void DeclarationValidator::visit(VariableDeclaration &var)
127 {
128         check_definition(var.name, var);
129         TraversingVisitor::visit(var);
130 }
131
132 void DeclarationValidator::visit(InterfaceBlock &iface)
133 {
134         string key = iface.interface+iface.name;
135         map<string, InterfaceBlock *>::const_iterator i = interface_blocks.find(key);
136         if(i!=interface_blocks.end())
137                 multiple_definition(format("interface block '%s %s'", iface.interface, iface.name), iface, *i->second);
138         else
139                 interface_blocks.insert(make_pair(key, &iface));
140
141         if(Statement *previous = find_definition(iface.name))
142         {
143                 if(!dynamic_cast<InterfaceBlock *>(previous))
144                         multiple_definition(format("'%s'", iface.name), iface, *previous);
145         }
146         else
147                 record_definition(iface.name, iface);
148
149         if(!iface.instance_name.empty())
150                 check_definition(iface.instance_name, iface);
151
152         if(iface.instance_name.empty() && iface.struct_declaration)
153         {
154                 // Inject anonymous interface block members into the global scope
155                 const map<string, VariableDeclaration *> &iface_vars = iface.struct_declaration->members.variables;
156                 for(map<string, VariableDeclaration *>::const_iterator j=iface_vars.begin(); j!=iface_vars.end(); ++j)
157                         check_definition(j->first, *j->second);
158         }
159 }
160
161 void DeclarationValidator::visit(FunctionDeclaration &func)
162 {
163         if(Statement *previous = find_definition(func.name))
164         {
165                 FunctionDeclaration *prev_func = dynamic_cast<FunctionDeclaration *>(previous);
166                 if(prev_func && prev_func->definition==&func)
167                         declarations[current_block][func.name] = &func;
168                 else
169                         multiple_definition(format("'%s'", func.name), func, *previous);
170         }
171         else
172                 record_definition(func.name, func);
173
174         TraversingVisitor::visit(func);
175 }
176
177
178 void ReferenceValidator::visit(BasicTypeDeclaration &type)
179 {
180         if(!type.base.empty() && !type.base_type)
181                 error(type, format("Use of undeclared type '%s'", type.base));
182 }
183
184 void ReferenceValidator::visit(ImageTypeDeclaration &type)
185 {
186         if(!type.base.empty() && !type.base_type)
187                 error(type, format("Use of undeclared type '%s'", type.base));
188 }
189
190 void ReferenceValidator::visit(VariableReference &var)
191 {
192         if(!var.declaration)
193                 error(var, format("Use of undeclared variable '%s'", var.name));
194         else if(stage->type!=Stage::VERTEX && var.declaration->interface=="in" && !var.declaration->linked_declaration)
195                 error(var, format("Use of unlinked input variable '%s'", var.name));
196 }
197
198 void ReferenceValidator::visit(InterfaceBlockReference &iface)
199 {
200         /* An interface block reference without a declaration should be impossible
201         since references are generated based on existing declarations. */
202         if(!iface.declaration)
203                 error(iface, format("Use of undeclared interface block '%s'", iface.name));
204         else if(stage->type!=Stage::VERTEX && iface.declaration->interface=="in" && !iface.declaration->linked_block)
205                 error(iface, format("Use of unlinked input block '%s'", iface.name));
206 }
207
208 void ReferenceValidator::visit(VariableDeclaration &var)
209 {
210         if(!var.type_declaration)
211                 error(var, format("Use of undeclared type '%s'", var.type));
212         TraversingVisitor::visit(var);
213 }
214
215 void ReferenceValidator::visit(InterfaceBlock &iface)
216 {
217         if(!iface.struct_declaration)
218                 error(iface, format("Interface block '%s %s' lacks a struct declaration", iface.interface, iface.name));
219         TraversingVisitor::visit(iface);
220 }
221
222 void ReferenceValidator::visit(FunctionDeclaration &func)
223 {
224         if(!func.return_type_declaration)
225                 error(func, format("Use of undeclared type '%s'", func.return_type));
226         TraversingVisitor::visit(func);
227 }
228
229
230 void ExpressionValidator::visit(UnaryExpression &unary)
231 {
232         if(unary.expression->type)
233         {
234                 if(!unary.type)
235                         error(unary, format("No matching operator '%s' found for '%s'", unary.oper->token, unary.expression->type->name));
236                 else if((unary.oper->token[1]=='+' || unary.oper->token[1]=='-') && !unary.expression->lvalue)
237                         error(unary, format("Operand of '%s' is not an lvalue", unary.oper->token));
238         }
239         TraversingVisitor::visit(unary);
240 }
241
242 void ExpressionValidator::visit(BinaryExpression &binary)
243 {
244         if(!binary.type && binary.left->type && binary.right->type)
245                 error(binary, format("No matching operator '%s' found for '%s' and '%s'",
246                         binary.oper->token, binary.left->type->name, binary.right->type->name));
247         TraversingVisitor::visit(binary);
248 }
249
250 void ExpressionValidator::visit(Assignment &assign)
251 {
252         if(assign.left->type)
253         {
254                 if(!assign.left->lvalue)
255                         error(assign, "Target of assignment is not an lvalue");
256                 if(assign.right->type)
257                 {
258                         if(assign.oper->token[0]!='=')
259                         {
260                                 if(!assign.type)
261                                         error(assign, format("No matching operator '%s' found for '%s' and '%s'",
262                                                 string(assign.oper->token, strlen(assign.oper->token)-1), assign.left->type->name, assign.right->type->name));
263                         }
264                         else if(assign.left->type!=assign.right->type)
265                                 error(assign, format("Assignment to variable of type '%s' from expression of incompatible type '%s'",
266                                         assign.left->type->name, assign.right->type->name));
267                 }
268         }
269         TraversingVisitor::visit(assign);
270 }
271
272 void ExpressionValidator::visit(VariableDeclaration &var)
273 {
274         if(var.init_expression && var.init_expression->type && var.type_declaration && var.init_expression->type!=var.type_declaration)
275                 error(var, format("Initializing a variable of type '%s' with an expression of incompatible type '%s'",
276                         var.type_declaration->name, var.init_expression->type->name));
277         TraversingVisitor::visit(var);
278 }
279
280 } // namespace SL
281 } // namespace GL
282 } // namespace Msp