3 #include <msp/core/raii.h>
4 #include <msp/strings/format.h>
5 #include <msp/strings/utils.h>
14 Validator::Validator():
19 void Validator::diagnose(Node &node, Node &provoking_node, Diagnostic::Severity severity, const string &message)
22 diag.severity = severity;
23 diag.source = node.source;
24 diag.line = node.line;
25 diag.provoking_source = provoking_node.source;
26 diag.provoking_line = provoking_node.line;
27 diag.message = message;
28 stage->diagnostics.push_back(diag);
30 last_provoker = &provoking_node;
33 void Validator::add_info(Node &node, const string &message)
36 throw logic_error("Tried to add info without a previous provoker");
37 diagnose(node, *last_provoker, Diagnostic::INFO, message);
41 DeclarationValidator::DeclarationValidator():
48 const char *DeclarationValidator::describe_variable(ScopeType scope)
52 case GLOBAL: return "global variable";
53 case STRUCT: return "struct member";
54 case INTERFACE_BLOCK: return "interface block member";
55 case FUNCTION_PARAM: return "function parameter";
56 case FUNCTION: return "local variable";
57 default: return "variable";
61 void DeclarationValidator::visit(Layout &layout)
63 for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); ++i)
68 if(i->name=="location")
69 allowed = (variable && scope==GLOBAL);
70 else if(i->name=="binding" || i->name=="set")
74 TypeDeclaration *type = variable->type_declaration;
75 while(BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(type))
76 type = basic->base_type;
77 allowed = (scope==GLOBAL && dynamic_cast<ImageTypeDeclaration *>(type));
78 err_descr = "variable of non-opaque type";
83 else if(i->name=="constant_id")
85 allowed = (variable && scope==GLOBAL);
88 if(!variable->constant)
91 err_descr = "non-constant variable";
95 BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(variable->type_declaration);
96 if(!basic || basic->kind<BasicTypeDeclaration::BOOL || basic->kind>BasicTypeDeclaration::INT)
99 err_descr = format("variable of type '%s'",
100 (variable->type_declaration ? variable->type_declaration->name : variable->type));
105 else if(i->name=="offset")
106 allowed = (variable && scope==INTERFACE_BLOCK);
107 else if(i->name=="points")
109 allowed = (stage->type==Stage::GEOMETRY && iface_layout && (iface_layout->interface=="in" || iface_layout->interface=="out"));
112 else if(i->name=="lines" || i->name=="lines_adjacency" || i->name=="triangles" || i->name=="triangles_adjacency")
114 allowed = (stage->type==Stage::GEOMETRY && iface_layout && iface_layout->interface=="in");
117 else if(i->name=="line_strip" || i->name=="triangle_strip")
119 allowed = (stage->type==Stage::GEOMETRY && iface_layout && iface_layout->interface=="out");
122 else if(i->name=="invocations")
123 allowed = (stage->type==Stage::GEOMETRY && iface_layout && iface_layout->interface=="in");
124 else if(i->name=="max_vertices")
125 allowed = (stage->type==Stage::GEOMETRY && iface_layout && iface_layout->interface=="out");
126 else if(i->name=="std140" || i->name=="std430")
128 allowed = (iface_block && !variable && iface_block->interface=="uniform");
134 if(err_descr.empty())
137 err_descr = describe_variable(scope);
139 err_descr = "interface block";
140 else if(iface_layout)
141 err_descr = format("interface '%s'", iface_layout->interface);
143 err_descr = "unknown declaration";
145 error(layout, format("Layout qualifier '%s' not allowed on %s", i->name, err_descr));
147 else if(value && !i->has_value)
148 error(layout, format("Layout qualifier '%s' requires a value", i->name));
149 else if(!value && i->has_value)
150 error(layout, format("Layout qualifier '%s' does not allow a value", i->name));
154 void DeclarationValidator::visit(InterfaceLayout &layout)
156 SetForScope<InterfaceLayout *> set_layout(iface_layout, &layout);
157 TraversingVisitor::visit(layout);
160 void DeclarationValidator::visit(BasicTypeDeclaration &type)
162 BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type);
163 BasicTypeDeclaration::Kind base_kind = (basic_base ? basic_base->kind : BasicTypeDeclaration::VOID);
165 if(type.kind==BasicTypeDeclaration::VECTOR && (base_kind<BasicTypeDeclaration::BOOL || base_kind>BasicTypeDeclaration::FLOAT))
166 error(type, format("Invalid base type '%s' for vector type '%s'", type.base, type.name));
167 else if(type.kind==BasicTypeDeclaration::MATRIX && base_kind!=BasicTypeDeclaration::VECTOR)
168 error(type, format("Invalid base type '%s' for matrix type '%s'", type.base, type.name));
169 else if(type.kind==BasicTypeDeclaration::ARRAY && basic_base && base_kind==BasicTypeDeclaration::VOID)
170 error(type, format("Invalid base type '%s' for array type '%s'", type.base, type.name));
173 void DeclarationValidator::visit(ImageTypeDeclaration &type)
175 BasicTypeDeclaration::Kind base_kind = BasicTypeDeclaration::VOID;
176 if(BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type))
177 base_kind = basic_base->kind;
178 if(base_kind!=BasicTypeDeclaration::INT && base_kind!=BasicTypeDeclaration::FLOAT)
179 error(type, format("Invalid base type '%s' for image type '%s'", type.base, type.name));
182 void DeclarationValidator::visit(StructDeclaration &strct)
184 SetForScope<ScopeType> set_scope(scope, (scope!=INTERFACE_BLOCK ? STRUCT : scope));
185 TraversingVisitor::visit(strct);
188 void DeclarationValidator::visit(VariableDeclaration &var)
190 SetForScope<VariableDeclaration *> set_var(variable, &var);
192 const char *descr = describe_variable(scope);
196 if(scope!=GLOBAL && scope!=INTERFACE_BLOCK)
197 error(var, format("Layout qualifier not allowed on %s", descr));
199 var.layout->visit(*this);
204 if(scope==STRUCT || scope==INTERFACE_BLOCK)
205 error(var, format("Constant qualifier not allowed on %s", descr));
208 if(!var.interpolation.empty() || !var.sampling.empty())
210 if(var.interface!="in" && stage->type==Stage::VERTEX)
211 error(var, "Interpolation qualifier not allowed on vertex input");
212 else if(var.interface!="out" && stage->type==Stage::FRAGMENT)
213 error(var, "Interpolation qualifier not allowed on fragment output");
214 else if((var.interface!="in" && var.interface!="out") || (scope==FUNCTION_PARAM || scope==FUNCTION))
215 error(var, "Interpolation qualifier not allowed on non-interpolated variable");
218 if(!var.interface.empty())
220 if(iface_block && var.interface!=iface_block->interface)
221 error(var, format("Mismatched interface qualifier '%s' inside '%s' block", var.interface, iface_block->interface));
222 else if(scope==STRUCT || scope==FUNCTION)
223 error(var, format("Interface qualifier not allowed on %s", descr));
226 TypeDeclaration *type = var.type_declaration;
227 while(BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(type))
228 type = basic->base_type;
229 if(dynamic_cast<ImageTypeDeclaration *>(type))
231 if(scope!=GLOBAL && scope!=FUNCTION_PARAM)
232 error(var, format("Type '%s' not allowed on %s", type->name, descr));
233 else if(scope==GLOBAL && var.interface!="uniform")
234 error(var, format("Type '%s' only allowed with uniform interface", type->name));
237 if(var.init_expression)
239 if(scope==GLOBAL && !var.constant)
240 error(var, format("Initializer not allowed on non-constant %s", descr));
241 else if(scope!=GLOBAL && scope!=FUNCTION)
242 error(var, format("Initializer not allowed on %s", descr));
244 var.init_expression->visit(*this);
248 void DeclarationValidator::visit(InterfaceBlock &iface)
250 SetForScope<ScopeType> set_scope(scope, INTERFACE_BLOCK);
251 SetForScope<InterfaceBlock *> set_iface(iface_block, &iface);
253 if(stage->type==Stage::VERTEX && iface.interface=="in")
254 error(iface, "Interface block not allowed on vertex shader input");
255 else if(stage->type==Stage::FRAGMENT && iface.interface=="out")
256 error(iface, "Interface block not allowed on fragment shader output");
258 TraversingVisitor::visit(iface);
259 if(iface.struct_declaration)
260 iface.struct_declaration->visit(*this);
263 void DeclarationValidator::visit(FunctionDeclaration &func)
265 SetForScope<ScopeType> set_scope(scope, FUNCTION_PARAM);
266 for(NodeArray<VariableDeclaration>::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
269 func.body.visit(*this);
273 IdentifierValidator::IdentifierValidator():
274 anonymous_block(false)
277 void IdentifierValidator::multiple_definition(const string &name, Statement &statement, Statement &previous)
279 error(statement, format("Multiple definition of %s", name));
280 add_info(previous, "Previous definition is here");
283 Statement *IdentifierValidator::find_definition(const string &name)
285 BlockDeclarationMap *decls = &declarations[current_block];
286 BlockDeclarationMap::const_iterator i = decls->find(name);
287 if(i==decls->end() && anonymous_block)
289 decls = &declarations[current_block->parent];
290 i = decls->find(name);
292 return (i!=decls->end() ? i->second : 0);
295 void IdentifierValidator::check_definition(const string &name, Statement &statement)
297 if(Statement *previous = find_definition(name))
298 multiple_definition(format("'%s'", name), statement, *previous);
300 record_definition(name, statement);
303 void IdentifierValidator::record_definition(const string &name, Statement &statement)
305 declarations[current_block].insert(make_pair(name, &statement));
307 declarations[current_block->parent].insert(make_pair(name, &statement));
310 void IdentifierValidator::visit(TypeDeclaration &type)
312 check_definition(type.name, type);
315 void IdentifierValidator::visit(StructDeclaration &strct)
317 check_definition(strct.name, strct);
318 TraversingVisitor::visit(strct);
321 void IdentifierValidator::visit(VariableDeclaration &var)
323 check_definition(var.name, var);
324 TraversingVisitor::visit(var);
327 void IdentifierValidator::visit(InterfaceBlock &iface)
329 string key = iface.interface+iface.block_name;
330 map<string, InterfaceBlock *>::const_iterator i = interface_blocks.find(key);
331 if(i!=interface_blocks.end())
332 multiple_definition(format("interface block '%s %s'", iface.interface, iface.block_name), iface, *i->second);
334 interface_blocks.insert(make_pair(key, &iface));
336 if(Statement *previous = find_definition(iface.block_name))
338 if(!dynamic_cast<InterfaceBlock *>(previous))
339 multiple_definition(format("'%s'", iface.block_name), iface, *previous);
342 record_definition(iface.block_name, iface);
344 if(!iface.instance_name.empty())
345 check_definition(iface.instance_name, iface);
347 if(iface.instance_name.empty() && iface.struct_declaration)
349 // Inject anonymous interface block members into the global scope
350 const map<string, VariableDeclaration *> &iface_vars = iface.struct_declaration->members.variables;
351 for(map<string, VariableDeclaration *>::const_iterator j=iface_vars.begin(); j!=iface_vars.end(); ++j)
352 check_definition(j->first, *j->second);
356 void IdentifierValidator::visit(FunctionDeclaration &func)
358 string key = func.name+func.signature;
359 map<string, FunctionDeclaration *>::const_iterator i = overloaded_functions.find(key);
360 if(i==overloaded_functions.end())
361 overloaded_functions.insert(make_pair(key, &func));
362 else if(func.return_type_declaration && i->second->return_type_declaration!=func.return_type_declaration)
364 error(func, format("Conflicting return type '%s' for function '%s'", func.return_type_declaration->name, func.name));
365 if(i->second->return_type_declaration)
366 add_info(*i->second, format("Previously declared as returning '%s'", i->second->return_type_declaration->name));
369 if(Statement *previous = find_definition(func.name))
371 if(!dynamic_cast<FunctionDeclaration *>(previous))
372 multiple_definition(format("'%s'", func.name), func, *previous);
375 record_definition(func.name, func);
377 if(func.definition==&func)
378 check_definition(func.name+func.signature, func);
380 TraversingVisitor::visit(func);
384 void ReferenceValidator::visit(BasicTypeDeclaration &type)
386 if(!type.base.empty() && !type.base_type)
387 error(type, format("Use of undeclared type '%s'", type.base));
390 void ReferenceValidator::visit(ImageTypeDeclaration &type)
392 if(!type.base.empty() && !type.base_type)
393 error(type, format("Use of undeclared type '%s'", type.base));
396 void ReferenceValidator::visit(VariableReference &var)
399 error(var, format("Use of undeclared variable '%s'", var.name));
400 else if(stage->type!=Stage::VERTEX && var.declaration->interface=="in" && var.name.compare(0, 3, "gl_") && !var.declaration->linked_declaration)
401 error(var, format("Use of unlinked input variable '%s'", var.name));
404 void ReferenceValidator::visit(MemberAccess &memacc)
406 if(memacc.left->type && !memacc.declaration)
407 error(memacc, format("Use of undeclared member '%s'", memacc.member));
408 TraversingVisitor::visit(memacc);
411 void ReferenceValidator::visit(InterfaceBlockReference &iface)
413 /* An interface block reference without a declaration should be impossible
414 since references are generated based on existing declarations. */
415 if(!iface.declaration)
416 error(iface, format("Use of undeclared interface block '%s'", iface.name));
417 else if(stage->type!=Stage::VERTEX && iface.declaration->interface=="in" && !iface.declaration->linked_block)
418 error(iface, format("Use of unlinked input block '%s'", iface.name));
421 void ReferenceValidator::visit(FunctionCall &call)
423 if((!call.constructor && !call.declaration) || (call.constructor && !call.type))
425 bool have_declaration = call.constructor;
426 if(!call.constructor)
428 map<string, FunctionDeclaration *>::iterator i = stage->functions.lower_bound(call.name);
429 have_declaration = (i!=stage->functions.end() && i->second->name==call.name);
434 bool valid_types = true;
436 for(NodeArray<Expression>::const_iterator j=call.arguments.begin(); (valid_types && j!=call.arguments.end()); ++j)
439 append(signature, ", ", (*j)->type->name);
445 error(call, format("No matching %s found for '%s(%s)'", (call.constructor ? "constructor" : "overload"), call.name, signature));
448 error(call, format("Call to undeclared function '%s'", call.name));
450 TraversingVisitor::visit(call);
453 void ReferenceValidator::visit(VariableDeclaration &var)
455 if(!var.type_declaration)
456 error(var, format("Use of undeclared type '%s'", var.type));
457 TraversingVisitor::visit(var);
460 void ReferenceValidator::visit(InterfaceBlock &iface)
462 if(!iface.struct_declaration)
463 error(iface, format("Interface block '%s %s' lacks a struct declaration", iface.interface, iface.block_name));
464 TraversingVisitor::visit(iface);
467 void ReferenceValidator::visit(FunctionDeclaration &func)
469 if(!func.return_type_declaration)
470 error(func, format("Use of undeclared type '%s'", func.return_type));
471 TraversingVisitor::visit(func);
475 ExpressionValidator::ExpressionValidator():
479 void ExpressionValidator::visit(Swizzle &swizzle)
482 if(BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(swizzle.left->type))
484 if(basic->kind==BasicTypeDeclaration::INT || basic->kind==BasicTypeDeclaration::FLOAT)
486 else if(basic->kind==BasicTypeDeclaration::VECTOR)
492 static const char component_names[] = { 'x', 'y', 'z', 'w', 'r', 'g', 'b', 'a', 's', 't', 'p', 'q' };
494 for(unsigned i=0; i<swizzle.count; ++i)
496 unsigned component_flavour = (find(component_names, component_names+12, swizzle.component_group[i])-component_names)/4;
498 flavour = component_flavour;
499 else if(flavour>=0 && component_flavour!=static_cast<unsigned>(flavour))
501 error(swizzle, format("Flavour of swizzle component '%c' is inconsistent with '%c'",
502 swizzle.component_group[i], swizzle.component_group[0]));
506 if(swizzle.components[i]>=size)
507 error(swizzle, format("Access to component '%c' which is not present in '%s'",
508 swizzle.component_group[i], swizzle.left->type->name));
511 else if(swizzle.left->type)
512 error(swizzle, format("Swizzle applied to '%s' which is neither a scalar nor a vector", swizzle.left->type->name));
514 TraversingVisitor::visit(swizzle);
517 void ExpressionValidator::visit(UnaryExpression &unary)
519 if(unary.expression->type)
522 error(unary, format("No matching operator '%s' found for '%s'", unary.oper->token, unary.expression->type->name));
523 else if((unary.oper->token[1]=='+' || unary.oper->token[1]=='-') && !unary.expression->lvalue)
524 error(unary, format("Operand of '%s' is not an lvalue", unary.oper->token));
526 TraversingVisitor::visit(unary);
529 void ExpressionValidator::visit(BinaryExpression &binary)
531 if(!binary.type && binary.left->type && binary.right->type)
533 if(binary.oper->token[0]=='[')
534 error(binary, format("Can't index element of '%s' with '%s'",
535 binary.left->type->name, binary.right->type->name));
537 error(binary, format("No matching operator '%s' found for '%s' and '%s'",
538 binary.oper->token, binary.left->type->name, binary.right->type->name));
540 TraversingVisitor::visit(binary);
543 void ExpressionValidator::visit(Assignment &assign)
545 if(assign.left->type)
547 if(!assign.left->lvalue)
548 error(assign, "Target of assignment is not an lvalue");
549 if(assign.right->type)
551 if(assign.oper->token[0]!='=')
554 error(assign, format("No matching operator '%s' found for '%s' and '%s'",
555 string(assign.oper->token, strlen(assign.oper->token)-1), assign.left->type->name, assign.right->type->name));
557 else if(assign.left->type!=assign.right->type)
558 error(assign, format("Assignment to variable of type '%s' from expression of incompatible type '%s'",
559 assign.left->type->name, assign.right->type->name));
562 TraversingVisitor::visit(assign);
565 void ExpressionValidator::visit(TernaryExpression &ternary)
567 if(ternary.condition->type)
569 BasicTypeDeclaration *basic_cond = dynamic_cast<BasicTypeDeclaration *>(ternary.condition->type);
570 if(!basic_cond || basic_cond->kind!=BasicTypeDeclaration::BOOL)
571 error(ternary, "Ternary operator condition is not a boolean");
572 else if(!ternary.type && ternary.true_expr->type && ternary.false_expr->type)
573 error(ternary, format("Ternary operator has incompatible types '%s' and '%s'",
574 ternary.true_expr->type->name, ternary.false_expr->type->name));
576 TraversingVisitor::visit(ternary);
579 void ExpressionValidator::visit(VariableDeclaration &var)
581 if(var.init_expression && var.init_expression->type && var.type_declaration && var.init_expression->type!=var.type_declaration)
582 error(var, format("Initializing a variable of type '%s' with an expression of incompatible type '%s'",
583 var.type_declaration->name, var.init_expression->type->name));
584 TraversingVisitor::visit(var);
587 void ExpressionValidator::visit(FunctionDeclaration &func)
589 SetForScope<FunctionDeclaration *> set_func(current_function, &func);
590 TraversingVisitor::visit(func);
593 void ExpressionValidator::visit(Return &ret)
595 if(current_function && current_function->return_type_declaration)
597 TypeDeclaration *return_type = current_function->return_type_declaration;
598 BasicTypeDeclaration *basic_return = dynamic_cast<BasicTypeDeclaration *>(return_type);
601 if(ret.expression->type && ret.expression->type!=return_type)
602 error(ret, format("Return expression type '%s' is incompatible with declared return type '%s'",
603 ret.expression->type->name, return_type->name));
605 else if(!basic_return || basic_return->kind!=BasicTypeDeclaration::VOID)
606 error(ret, "Return statement without an expression in a function not returning 'void'");
609 TraversingVisitor::visit(ret);