3 #include <msp/core/raii.h>
4 #include <msp/strings/format.h>
13 Validator::Validator():
17 void Validator::diagnose(Node &node, Diagnostic::Severity severity, const string &message)
20 diag.severity = severity;
21 diag.source = node.source;
22 diag.line = node.line;
23 diag.message = message;
24 stage->diagnostics.push_back(diag);
28 TypeValidator::TypeValidator():
32 void TypeValidator::visit(BasicTypeDeclaration &type)
34 if(type.kind==BasicTypeDeclaration::VECTOR)
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));
42 else if(type.kind==BasicTypeDeclaration::ARRAY)
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));
50 void TypeValidator::visit(ImageTypeDeclaration &type)
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));
59 void TypeValidator::visit(StructDeclaration &strct)
61 SetFlag set_struct(in_struct);
62 TraversingVisitor::visit(strct);
65 void TypeValidator::visit(VariableDeclaration &var)
70 error(var, "Struct members can't have layouts");
71 if(var.init_expression)
72 error(var, "Struct members can't have initializers");
75 TraversingVisitor::visit(var);
79 DeclarationValidator::DeclarationValidator():
80 anonymous_block(false)
83 void DeclarationValidator::multiple_definition(const string &name, Statement &statement, Statement &previous)
85 error(statement, format("Multiple definition of %s", name));
86 diagnose(previous, Diagnostic::INFO, "Previous definition is here");
89 Statement *DeclarationValidator::find_definition(const string &name)
91 BlockDeclarationMap *decls = &declarations[current_block];
92 BlockDeclarationMap::const_iterator i = decls->find(name);
93 if(i==decls->end() && anonymous_block)
95 decls = &declarations[current_block->parent];
96 i = decls->find(name);
98 return (i!=decls->end() ? i->second : 0);
101 void DeclarationValidator::check_definition(const string &name, Statement &statement)
103 if(Statement *previous = find_definition(name))
104 multiple_definition(format("'%s'", name), statement, *previous);
106 record_definition(name, statement);
109 void DeclarationValidator::record_definition(const string &name, Statement &statement)
111 declarations[current_block].insert(make_pair(name, &statement));
113 declarations[current_block->parent].insert(make_pair(name, &statement));
116 void DeclarationValidator::visit(TypeDeclaration &type)
118 check_definition(type.name, type);
121 void DeclarationValidator::visit(StructDeclaration &strct)
123 check_definition(strct.name, strct);
124 TraversingVisitor::visit(strct);
127 void DeclarationValidator::visit(VariableDeclaration &var)
129 check_definition(var.name, var);
130 TraversingVisitor::visit(var);
133 void DeclarationValidator::visit(InterfaceBlock &iface)
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);
140 interface_blocks.insert(make_pair(key, &iface));
142 if(Statement *previous = find_definition(iface.name))
144 if(!dynamic_cast<InterfaceBlock *>(previous))
145 multiple_definition(format("'%s'", iface.name), iface, *previous);
148 record_definition(iface.name, iface);
150 if(!iface.instance_name.empty())
151 check_definition(iface.instance_name, iface);
153 if(iface.instance_name.empty() && iface.struct_declaration)
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);
162 void DeclarationValidator::visit(FunctionDeclaration &func)
164 if(Statement *previous = find_definition(func.name))
166 FunctionDeclaration *prev_func = dynamic_cast<FunctionDeclaration *>(previous);
167 if(prev_func && prev_func->definition==&func)
168 declarations[current_block][func.name] = &func;
170 multiple_definition(format("'%s'", func.name), func, *previous);
173 record_definition(func.name, func);
175 TraversingVisitor::visit(func);
179 void ReferenceValidator::visit(BasicTypeDeclaration &type)
181 if(!type.base.empty() && !type.base_type)
182 error(type, format("Use of undeclared type '%s'", type.base));
185 void ReferenceValidator::visit(ImageTypeDeclaration &type)
187 if(!type.base.empty() && !type.base_type)
188 error(type, format("Use of undeclared type '%s'", type.base));
191 void ReferenceValidator::visit(VariableReference &var)
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));
199 void ReferenceValidator::visit(InterfaceBlockReference &iface)
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));
209 void ReferenceValidator::visit(VariableDeclaration &var)
211 if(!var.type_declaration)
212 error(var, format("Use of undeclared type '%s'", var.type));
213 TraversingVisitor::visit(var);
216 void ReferenceValidator::visit(InterfaceBlock &iface)
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);
223 void ReferenceValidator::visit(FunctionDeclaration &func)
225 if(!func.return_type_declaration)
226 error(func, format("Use of undeclared type '%s'", func.return_type));
227 TraversingVisitor::visit(func);
231 void ExpressionValidator::visit(Swizzle &swizzle)
234 if(BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(swizzle.left->type))
236 if(basic->kind==BasicTypeDeclaration::INT || basic->kind==BasicTypeDeclaration::FLOAT)
238 else if(basic->kind==BasicTypeDeclaration::VECTOR)
244 static const char component_names[] = { 'x', 'y', 'z', 'w', 'r', 'g', 'b', 'a', 's', 't', 'p', 'q' };
246 for(unsigned i=0; i<swizzle.count; ++i)
248 unsigned component_flavour = (find(component_names, component_names+12, swizzle.component_group[i])-component_names)/4;
250 flavour = component_flavour;
251 else if(flavour>=0 && component_flavour!=static_cast<unsigned>(flavour))
253 error(swizzle, format("Flavour of swizzle component '%c' is inconsistent with '%c'",
254 swizzle.component_group[i], swizzle.component_group[0]));
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));
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));
266 TraversingVisitor::visit(swizzle);
269 void ExpressionValidator::visit(UnaryExpression &unary)
271 if(unary.expression->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));
278 TraversingVisitor::visit(unary);
281 void ExpressionValidator::visit(BinaryExpression &binary)
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);
289 void ExpressionValidator::visit(Assignment &assign)
291 if(assign.left->type)
293 if(!assign.left->lvalue)
294 error(assign, "Target of assignment is not an lvalue");
295 if(assign.right->type)
297 if(assign.oper->token[0]!='=')
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));
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));
308 TraversingVisitor::visit(assign);
311 void ExpressionValidator::visit(VariableDeclaration &var)
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);