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