]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/validate.cpp
Validate member names
[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(MemberAccess &memacc)
200 {
201         if(memacc.left->type && !memacc.declaration)
202                 error(memacc, format("Use of undeclared member '%s'", memacc.member));
203         TraversingVisitor::visit(memacc);
204 }
205
206 void ReferenceValidator::visit(InterfaceBlockReference &iface)
207 {
208         /* An interface block reference without a declaration should be impossible
209         since references are generated based on existing declarations. */
210         if(!iface.declaration)
211                 error(iface, format("Use of undeclared interface block '%s'", iface.name));
212         else if(stage->type!=Stage::VERTEX && iface.declaration->interface=="in" && !iface.declaration->linked_block)
213                 error(iface, format("Use of unlinked input block '%s'", iface.name));
214 }
215
216 void ReferenceValidator::visit(VariableDeclaration &var)
217 {
218         if(!var.type_declaration)
219                 error(var, format("Use of undeclared type '%s'", var.type));
220         TraversingVisitor::visit(var);
221 }
222
223 void ReferenceValidator::visit(InterfaceBlock &iface)
224 {
225         if(!iface.struct_declaration)
226                 error(iface, format("Interface block '%s %s' lacks a struct declaration", iface.interface, iface.name));
227         TraversingVisitor::visit(iface);
228 }
229
230 void ReferenceValidator::visit(FunctionDeclaration &func)
231 {
232         if(!func.return_type_declaration)
233                 error(func, format("Use of undeclared type '%s'", func.return_type));
234         TraversingVisitor::visit(func);
235 }
236
237
238 void ExpressionValidator::visit(Swizzle &swizzle)
239 {
240         unsigned size = 0;
241         if(BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(swizzle.left->type))
242         {
243                 if(basic->kind==BasicTypeDeclaration::INT || basic->kind==BasicTypeDeclaration::FLOAT)
244                         size = 1;
245                 else if(basic->kind==BasicTypeDeclaration::VECTOR)
246                         size = basic->size;
247         }
248
249         if(size)
250         {
251                 static const char component_names[] = { 'x', 'y', 'z', 'w', 'r', 'g', 'b', 'a', 's', 't', 'p', 'q' };
252                 int flavour = -1;
253                 for(unsigned i=0; i<swizzle.count; ++i)
254                 {
255                         unsigned component_flavour = (find(component_names, component_names+12, swizzle.component_group[i])-component_names)/4;
256                         if(flavour==-1)
257                                 flavour = component_flavour;
258                         else if(flavour>=0 && component_flavour!=static_cast<unsigned>(flavour))
259                         {
260                                 error(swizzle, format("Flavour of swizzle component '%c' is inconsistent with '%c'",
261                                         swizzle.component_group[i], swizzle.component_group[0]));
262                                 flavour = -2;
263                         }
264
265                         if(swizzle.components[i]>=size)
266                                 error(swizzle, format("Access to component '%c' which is not present in '%s'",
267                                         swizzle.component_group[i], swizzle.left->type->name));
268                 }
269         }
270         else if(swizzle.left->type)
271                 error(swizzle, format("Swizzle applied to '%s' which is neither a scalar nor a vector", swizzle.left->type->name));
272
273         TraversingVisitor::visit(swizzle);
274 }
275
276 void ExpressionValidator::visit(UnaryExpression &unary)
277 {
278         if(unary.expression->type)
279         {
280                 if(!unary.type)
281                         error(unary, format("No matching operator '%s' found for '%s'", unary.oper->token, unary.expression->type->name));
282                 else if((unary.oper->token[1]=='+' || unary.oper->token[1]=='-') && !unary.expression->lvalue)
283                         error(unary, format("Operand of '%s' is not an lvalue", unary.oper->token));
284         }
285         TraversingVisitor::visit(unary);
286 }
287
288 void ExpressionValidator::visit(BinaryExpression &binary)
289 {
290         if(!binary.type && binary.left->type && binary.right->type)
291                 error(binary, format("No matching operator '%s' found for '%s' and '%s'",
292                         binary.oper->token, binary.left->type->name, binary.right->type->name));
293         TraversingVisitor::visit(binary);
294 }
295
296 void ExpressionValidator::visit(Assignment &assign)
297 {
298         if(assign.left->type)
299         {
300                 if(!assign.left->lvalue)
301                         error(assign, "Target of assignment is not an lvalue");
302                 if(assign.right->type)
303                 {
304                         if(assign.oper->token[0]!='=')
305                         {
306                                 if(!assign.type)
307                                         error(assign, format("No matching operator '%s' found for '%s' and '%s'",
308                                                 string(assign.oper->token, strlen(assign.oper->token)-1), assign.left->type->name, assign.right->type->name));
309                         }
310                         else if(assign.left->type!=assign.right->type)
311                                 error(assign, format("Assignment to variable of type '%s' from expression of incompatible type '%s'",
312                                         assign.left->type->name, assign.right->type->name));
313                 }
314         }
315         TraversingVisitor::visit(assign);
316 }
317
318 void ExpressionValidator::visit(VariableDeclaration &var)
319 {
320         if(var.init_expression && var.init_expression->type && var.type_declaration && var.init_expression->type!=var.type_declaration)
321                 error(var, format("Initializing a variable of type '%s' with an expression of incompatible type '%s'",
322                         var.type_declaration->name, var.init_expression->type->name));
323         TraversingVisitor::visit(var);
324 }
325
326 } // namespace SL
327 } // namespace GL
328 } // namespace Msp