+StructOrganizer::StructOrganizer():
+ offset(-1)
+{ }
+
+void StructOrganizer::visit(StructDeclaration &strct)
+{
+ SetForScope<int> set_offset(offset, 0);
+ TraversingVisitor::visit(strct);
+}
+
+void StructOrganizer::visit(VariableDeclaration &var)
+{
+ if(offset>=0)
+ {
+ int *layout_offset = 0;
+ bool has_matrix_order = false;
+ if(var.layout)
+ {
+ for(Layout::Qualifier &q: var.layout->qualifiers)
+ {
+ if(q.name=="offset" && q.has_value)
+ {
+ layout_offset = &q.value;
+ if(q.value>=offset)
+ offset = q.value;
+ }
+ else if(q.name=="column_major" || q.name=="row_major")
+ has_matrix_order = true;
+ }
+ }
+
+ MemoryRequirementsCalculator::Result mem_reqs = MemoryRequirementsCalculator().apply(var);
+ offset += mem_reqs.alignment-1;
+ offset -= offset%mem_reqs.alignment;
+
+ if(layout_offset)
+ *layout_offset = offset;
+ else
+ {
+ if(!var.layout)
+ var.layout = new Layout;
+
+ var.layout->qualifiers.push_back(Layout::Qualifier("offset", offset));
+ }
+
+ if(!has_matrix_order)
+ {
+ const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(var.type_declaration);
+ while(basic && basic->kind==BasicTypeDeclaration::ARRAY)
+ basic = dynamic_cast<const BasicTypeDeclaration *>(basic->base_type);
+ if(basic && basic->kind==BasicTypeDeclaration::MATRIX)
+ var.layout->qualifiers.push_back(Layout::Qualifier("column_major"));
+ }
+
+ offset += mem_reqs.size;
+ }
+}
+
+
+void LocationAllocator::apply(Module &module, const Features &features)