9 unsigned TypeComparer::next_tag = 1;
11 TypeComparer::TypeComparer():
18 void TypeComparer::compare(Node &node1, Node &node2)
30 T *TypeComparer::multi_visit(T &node)
32 static unsigned tag = next_tag++;
42 else if(!first || tag!=first_tag)
46 T *f = static_cast<T *>(first);
54 void TypeComparer::visit(BasicTypeDeclaration &basic)
56 if(BasicTypeDeclaration *basic1 = multi_visit(basic))
58 if(basic1->kind!=basic.kind || basic1->size!=basic.size)
60 else if(basic1->base_type && basic.base_type)
61 compare(*basic1->base_type, *basic.base_type);
63 r_result = (!basic1->base_type && !basic.base_type);
67 void TypeComparer::visit(ImageTypeDeclaration &image)
69 if(ImageTypeDeclaration *image1 = multi_visit(image))
71 if(image1->dimensions!=image.dimensions || image1->array!=image.array)
73 else if(image1->sampled!=image.sampled || image1->shadow!=image.shadow)
75 else if(image1->base_type && image.base_type)
76 compare(*image1->base_type, *image.base_type);
78 r_result = (!image1->base_type && !image.base_type);
82 void TypeComparer::visit(StructDeclaration &strct)
84 if(StructDeclaration *strct1 = multi_visit(strct))
86 if(strct1->members.body.size()!=strct.members.body.size())
91 NodeList<Statement>::const_iterator i = strct1->members.body.begin();
92 NodeList<Statement>::const_iterator j = strct.members.body.begin();
93 for(; (r_result && i!=strct1->members.body.end()); ++i, ++j)
99 void TypeComparer::visit(VariableDeclaration &var)
101 if(VariableDeclaration *var1 = multi_visit(var))
103 if(var1->name!=var.name || var1->array!=var.array)
105 else if(!var1->type_declaration || !var.type_declaration)
109 // TODO Compare array sizes
110 if(var1->type_declaration!=var.type_declaration)
111 compare(*var1->type_declaration, *var.type_declaration);
112 // TODO Compare layout qualifiers for interface block members
118 LocationCounter::LocationCounter():
122 void LocationCounter::visit(BasicTypeDeclaration &basic)
124 r_count = basic.kind==BasicTypeDeclaration::MATRIX ? basic.size>>16 : 1;
127 void LocationCounter::visit(ImageTypeDeclaration &)
132 void LocationCounter::visit(StructDeclaration &strct)
135 for(NodeList<Statement>::const_iterator i=strct.members.body.begin(); i!=strct.members.body.end(); ++i)
144 void LocationCounter::visit(VariableDeclaration &var)
147 if(var.type_declaration)
148 var.type_declaration->visit(*this);
150 if(const Literal *literal = dynamic_cast<const Literal *>(var.array_size.get()))
151 if(literal->value.check_type<int>())
152 r_count *= literal->value.value<int>();
156 void MemoryRequirementsCalculator::visit(BasicTypeDeclaration &basic)
158 if(basic.kind==BasicTypeDeclaration::BOOL)
163 else if(basic.kind==BasicTypeDeclaration::INT || basic.kind==BasicTypeDeclaration::FLOAT)
165 r_size = basic.size/8;
166 r_alignment = r_size;
168 else if(basic.kind==BasicTypeDeclaration::VECTOR || basic.kind==BasicTypeDeclaration::MATRIX)
170 basic.base_type->visit(*this);
171 unsigned n_elem = basic.size&0xFFFF;
173 if(basic.kind==BasicTypeDeclaration::VECTOR)
174 r_alignment *= (n_elem==3 ? 4 : n_elem);
176 else if(basic.kind==BasicTypeDeclaration::ARRAY)
177 basic.base_type->visit(*this);
180 void MemoryRequirementsCalculator::visit(StructDeclaration &strct)
183 unsigned max_align = 1;
184 for(NodeList<Statement>::iterator i=strct.members.body.begin(); i!=strct.members.body.end(); ++i)
192 total += r_alignment-1;
193 total -= total%r_alignment;
195 max_align = max(max_align, r_alignment);
198 r_alignment = max_align;
201 void MemoryRequirementsCalculator::visit(VariableDeclaration &var)
205 const vector<Layout::Qualifier> qualifiers = var.layout->qualifiers;
206 for(vector<Layout::Qualifier>::const_iterator i=qualifiers.begin(); (r_offset<0 && i!=qualifiers.end()); ++i)
207 if(i->name=="offset")
211 if(var.type_declaration)
212 var.type_declaration->visit(*this);
214 if(const Literal *literal = dynamic_cast<const Literal *>(var.array_size.get()))
215 if(literal->value.check_type<int>())
216 r_size += r_alignment*(literal->value.value<int>()-1);
220 set<Node *> DependencyCollector::apply(FunctionDeclaration &func)
226 void DependencyCollector::visit(VariableReference &var)
228 if(var.declaration && !locals.count(var.declaration))
230 dependencies.insert(var.declaration);
231 var.declaration->visit(*this);
235 void DependencyCollector::visit(InterfaceBlockReference &iface)
237 if(iface.declaration)
239 dependencies.insert(iface.declaration);
240 iface.declaration->visit(*this);
244 void DependencyCollector::visit(FunctionCall &call)
247 dependencies.insert(call.declaration);
248 TraversingVisitor::visit(call);
251 void DependencyCollector::visit(VariableDeclaration &var)
254 if(var.type_declaration)
256 dependencies.insert(var.type_declaration);
257 var.type_declaration->visit(*this);
260 TraversingVisitor::visit(var);