3 #include <msp/core/raii.h>
4 #include <msp/strings/format.h>
13 Validator::Validator():
18 void Validator::diagnose(Node &node, Node &provoking_node, Diagnostic::Severity severity, const string &message)
21 diag.severity = severity;
22 diag.source = node.source;
23 diag.line = node.line;
24 diag.provoking_source = provoking_node.source;
25 diag.provoking_line = provoking_node.line;
26 diag.message = message;
27 stage->diagnostics.push_back(diag);
29 last_provoker = &provoking_node;
32 void Validator::add_info(Node &node, const string &message)
35 throw logic_error("Tried to add info without a previous provoker");
36 diagnose(node, *last_provoker, Diagnostic::INFO, message);
40 TypeValidator::TypeValidator():
44 void TypeValidator::visit(BasicTypeDeclaration &type)
46 if(type.kind==BasicTypeDeclaration::VECTOR)
48 BasicTypeDeclaration::Kind base_kind = BasicTypeDeclaration::VOID;
49 if(BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type))
50 base_kind = basic_base->kind;
51 if(base_kind!=BasicTypeDeclaration::BOOL && base_kind!=BasicTypeDeclaration::INT && base_kind!=BasicTypeDeclaration::FLOAT)
52 error(type, format("Invalid base type '%s' for vector type '%s'", type.base, type.name));
54 else if(type.kind==BasicTypeDeclaration::ARRAY)
56 if(BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type))
57 if(basic_base->kind==BasicTypeDeclaration::VOID)
58 error(type, format("Invalid base type '%s' for array type '%s'", type.base, type.name));
62 void TypeValidator::visit(ImageTypeDeclaration &type)
64 BasicTypeDeclaration::Kind base_kind = BasicTypeDeclaration::VOID;
65 if(BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type))
66 base_kind = basic_base->kind;
67 if(base_kind!=BasicTypeDeclaration::INT && base_kind!=BasicTypeDeclaration::FLOAT)
68 error(type, format("Invalid base type '%s' for image type '%s'", type.base, type.name));
71 void TypeValidator::visit(StructDeclaration &strct)
73 SetFlag set_struct(in_struct);
74 TraversingVisitor::visit(strct);
77 void TypeValidator::visit(VariableDeclaration &var)
82 error(var, "Struct members can't have layouts");
83 if(var.init_expression)
84 error(var, "Struct members can't have initializers");
87 TraversingVisitor::visit(var);
91 DeclarationValidator::DeclarationValidator():
92 anonymous_block(false)
95 void DeclarationValidator::multiple_definition(const string &name, Statement &statement, Statement &previous)
97 error(statement, format("Multiple definition of %s", name));
98 add_info(previous, "Previous definition is here");
101 Statement *DeclarationValidator::find_definition(const string &name)
103 BlockDeclarationMap *decls = &declarations[current_block];
104 BlockDeclarationMap::const_iterator i = decls->find(name);
105 if(i==decls->end() && anonymous_block)
107 decls = &declarations[current_block->parent];
108 i = decls->find(name);
110 return (i!=decls->end() ? i->second : 0);
113 void DeclarationValidator::check_definition(const string &name, Statement &statement)
115 if(Statement *previous = find_definition(name))
116 multiple_definition(format("'%s'", name), statement, *previous);
118 record_definition(name, statement);
121 void DeclarationValidator::record_definition(const string &name, Statement &statement)
123 declarations[current_block].insert(make_pair(name, &statement));
125 declarations[current_block->parent].insert(make_pair(name, &statement));
128 void DeclarationValidator::visit(TypeDeclaration &type)
130 check_definition(type.name, type);
133 void DeclarationValidator::visit(StructDeclaration &strct)
135 check_definition(strct.name, strct);
136 TraversingVisitor::visit(strct);
139 void DeclarationValidator::visit(VariableDeclaration &var)
141 check_definition(var.name, var);
142 TraversingVisitor::visit(var);
145 void DeclarationValidator::visit(InterfaceBlock &iface)
147 string key = iface.interface+iface.name;
148 map<string, InterfaceBlock *>::const_iterator i = interface_blocks.find(key);
149 if(i!=interface_blocks.end())
150 multiple_definition(format("interface block '%s %s'", iface.interface, iface.name), iface, *i->second);
152 interface_blocks.insert(make_pair(key, &iface));
154 if(Statement *previous = find_definition(iface.name))
156 if(!dynamic_cast<InterfaceBlock *>(previous))
157 multiple_definition(format("'%s'", iface.name), iface, *previous);
160 record_definition(iface.name, iface);
162 if(!iface.instance_name.empty())
163 check_definition(iface.instance_name, iface);
165 if(iface.instance_name.empty() && iface.struct_declaration)
167 // Inject anonymous interface block members into the global scope
168 const map<string, VariableDeclaration *> &iface_vars = iface.struct_declaration->members.variables;
169 for(map<string, VariableDeclaration *>::const_iterator j=iface_vars.begin(); j!=iface_vars.end(); ++j)
170 check_definition(j->first, *j->second);
174 void DeclarationValidator::visit(FunctionDeclaration &func)
176 string key = func.name+func.signature;
177 map<string, FunctionDeclaration *>::const_iterator i = overloaded_functions.find(key);
178 if(i==overloaded_functions.end())
179 overloaded_functions.insert(make_pair(key, &func));
180 else if(func.return_type_declaration && i->second->return_type_declaration!=func.return_type_declaration)
182 error(func, format("Conflicting return type '%s' for function '%s'", func.return_type_declaration->name, func.name));
183 if(i->second->return_type_declaration)
184 add_info(*i->second, format("Previously declared as returning '%s'", i->second->return_type_declaration->name));
187 if(Statement *previous = find_definition(func.name))
189 if(!dynamic_cast<FunctionDeclaration *>(previous))
190 multiple_definition(format("'%s'", func.name), func, *previous);
193 record_definition(func.name, func);
195 if(func.definition==&func)
196 check_definition(func.name+func.signature, func);
198 TraversingVisitor::visit(func);
202 void ReferenceValidator::visit(BasicTypeDeclaration &type)
204 if(!type.base.empty() && !type.base_type)
205 error(type, format("Use of undeclared type '%s'", type.base));
208 void ReferenceValidator::visit(ImageTypeDeclaration &type)
210 if(!type.base.empty() && !type.base_type)
211 error(type, format("Use of undeclared type '%s'", type.base));
214 void ReferenceValidator::visit(VariableReference &var)
217 error(var, format("Use of undeclared variable '%s'", var.name));
218 else if(stage->type!=Stage::VERTEX && var.declaration->interface=="in" && !var.declaration->linked_declaration)
219 error(var, format("Use of unlinked input variable '%s'", var.name));
222 void ReferenceValidator::visit(MemberAccess &memacc)
224 if(memacc.left->type && !memacc.declaration)
225 error(memacc, format("Use of undeclared member '%s'", memacc.member));
226 TraversingVisitor::visit(memacc);
229 void ReferenceValidator::visit(InterfaceBlockReference &iface)
231 /* An interface block reference without a declaration should be impossible
232 since references are generated based on existing declarations. */
233 if(!iface.declaration)
234 error(iface, format("Use of undeclared interface block '%s'", iface.name));
235 else if(stage->type!=Stage::VERTEX && iface.declaration->interface=="in" && !iface.declaration->linked_block)
236 error(iface, format("Use of unlinked input block '%s'", iface.name));
239 void ReferenceValidator::visit(VariableDeclaration &var)
241 if(!var.type_declaration)
242 error(var, format("Use of undeclared type '%s'", var.type));
243 TraversingVisitor::visit(var);
246 void ReferenceValidator::visit(InterfaceBlock &iface)
248 if(!iface.struct_declaration)
249 error(iface, format("Interface block '%s %s' lacks a struct declaration", iface.interface, iface.name));
250 TraversingVisitor::visit(iface);
253 void ReferenceValidator::visit(FunctionDeclaration &func)
255 if(!func.return_type_declaration)
256 error(func, format("Use of undeclared type '%s'", func.return_type));
257 TraversingVisitor::visit(func);
261 void ExpressionValidator::visit(Swizzle &swizzle)
264 if(BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(swizzle.left->type))
266 if(basic->kind==BasicTypeDeclaration::INT || basic->kind==BasicTypeDeclaration::FLOAT)
268 else if(basic->kind==BasicTypeDeclaration::VECTOR)
274 static const char component_names[] = { 'x', 'y', 'z', 'w', 'r', 'g', 'b', 'a', 's', 't', 'p', 'q' };
276 for(unsigned i=0; i<swizzle.count; ++i)
278 unsigned component_flavour = (find(component_names, component_names+12, swizzle.component_group[i])-component_names)/4;
280 flavour = component_flavour;
281 else if(flavour>=0 && component_flavour!=static_cast<unsigned>(flavour))
283 error(swizzle, format("Flavour of swizzle component '%c' is inconsistent with '%c'",
284 swizzle.component_group[i], swizzle.component_group[0]));
288 if(swizzle.components[i]>=size)
289 error(swizzle, format("Access to component '%c' which is not present in '%s'",
290 swizzle.component_group[i], swizzle.left->type->name));
293 else if(swizzle.left->type)
294 error(swizzle, format("Swizzle applied to '%s' which is neither a scalar nor a vector", swizzle.left->type->name));
296 TraversingVisitor::visit(swizzle);
299 void ExpressionValidator::visit(UnaryExpression &unary)
301 if(unary.expression->type)
304 error(unary, format("No matching operator '%s' found for '%s'", unary.oper->token, unary.expression->type->name));
305 else if((unary.oper->token[1]=='+' || unary.oper->token[1]=='-') && !unary.expression->lvalue)
306 error(unary, format("Operand of '%s' is not an lvalue", unary.oper->token));
308 TraversingVisitor::visit(unary);
311 void ExpressionValidator::visit(BinaryExpression &binary)
313 if(!binary.type && binary.left->type && binary.right->type)
314 error(binary, format("No matching operator '%s' found for '%s' and '%s'",
315 binary.oper->token, binary.left->type->name, binary.right->type->name));
316 TraversingVisitor::visit(binary);
319 void ExpressionValidator::visit(Assignment &assign)
321 if(assign.left->type)
323 if(!assign.left->lvalue)
324 error(assign, "Target of assignment is not an lvalue");
325 if(assign.right->type)
327 if(assign.oper->token[0]!='=')
330 error(assign, format("No matching operator '%s' found for '%s' and '%s'",
331 string(assign.oper->token, strlen(assign.oper->token)-1), assign.left->type->name, assign.right->type->name));
333 else if(assign.left->type!=assign.right->type)
334 error(assign, format("Assignment to variable of type '%s' from expression of incompatible type '%s'",
335 assign.left->type->name, assign.right->type->name));
338 TraversingVisitor::visit(assign);
341 void ExpressionValidator::visit(VariableDeclaration &var)
343 if(var.init_expression && var.init_expression->type && var.type_declaration && var.init_expression->type!=var.type_declaration)
344 error(var, format("Initializing a variable of type '%s' with an expression of incompatible type '%s'",
345 var.type_declaration->name, var.init_expression->type->name));
346 TraversingVisitor::visit(var);