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