void LegacyConverter::visit(InterfaceBlock &iface)
{
- if(!supports_interface_blocks(iface.interface))
+ if(!supports_interface_blocks(iface.interface) && iface.type_declaration)
{
if(!iface.instance_name.empty())
unsupported("ARB_uniform_buffer_object required for interface block instances");
- else
+ else if(iface.struct_declaration)
{
- stage->content.body.splice(uniform_insert_point, iface.members.body);
+ stage->content.body.splice(uniform_insert_point, iface.struct_declaration->members.body);
nodes_to_remove.insert(&iface);
+ nodes_to_remove.insert(iface.struct_declaration);
}
+ else
+ /* If the interface block is an array, it should have an instance
+ name too, so this should never be reached */
+ throw logic_error("Unexpected interface block configuration");
}
}
string combined;
for(list<Stage>::const_iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
for(vector<Diagnostic>::const_iterator j=i->diagnostics.begin(); j!=i->diagnostics.end(); ++j)
- append(combined, "\n", format("%s:%d: %s", module->source_map.get_name(j->source), j->line, j->message));
+ if(j->source!=INTERNAL_SOURCE)
+ append(combined, "\n", format("%s:%d: %s", module->source_map.get_name(j->source), j->line, j->message));
return combined;
}
if(resolve<BlockHierarchyResolver>(stage, flags, RESOLVE_BLOCKS))
;
else if(resolve<TypeResolver>(stage, flags, RESOLVE_TYPES))
- flags |= RESOLVE_VARIABLES|RESOLVE_EXPRESSIONS;
+ flags |= RESOLVE_BLOCKS|RESOLVE_VARIABLES|RESOLVE_EXPRESSIONS;
else if(resolve<VariableResolver>(stage, flags, RESOLVE_VARIABLES))
flags |= RESOLVE_EXPRESSIONS;
else if(resolve<FunctionResolver>(stage, flags, RESOLVE_FUNCTIONS))
string text;
if(iface.declaration)
text += format("%%%d ", get_label(*iface.declaration));
- text += format("%s (iface)", iface.name);
+ text += format("%s (iface) -> %s", iface.name, format_type(iface.type));
append(text);
}
head += " (builtin)";
else if(block.linked_block)
head += " (linked)";
- annotated_branch(head, block.members);
+ append(head);
+
+ begin_sub();
+ last_branch();
+ if(block.type_declaration)
+ append(format("Type: %%%d %s", get_label(*block.type_declaration), block.type_declaration->name));
+ else if(block.members)
+ block.members->visit(*this);
+ end_sub();
}
void DumpTree::visit(FunctionDeclaration &func)
TypeResolver::TypeResolver():
stage(0),
+ iface_block(0),
r_any_resolved(false)
{ }
void TypeResolver::visit(VariableDeclaration &var)
{
resolve_type(var.type_declaration, var.type, var.array);
+ if(iface_block && var.interface==iface_block->interface)
+ var.interface.clear();
+}
+
+void TypeResolver::visit(InterfaceBlock &iface)
+{
+ if(iface.members)
+ {
+ SetForScope<InterfaceBlock *> set_iface(iface_block, &iface);
+ iface.members->visit(*this);
+
+ StructDeclaration *strct = new StructDeclaration;
+ strct->source = INTERNAL_SOURCE;
+ strct->name = format("_%s_%s", iface.interface, iface.name);
+ strct->members.body.splice(strct->members.body.begin(), iface.members->body);
+ stage->content.body.insert(type_insert_point, strct);
+ stage->types.insert(make_pair(strct->name, strct));
+
+ iface.members = 0;
+ strct->interface_block = &iface;
+ iface.struct_declaration = strct;
+ }
+
+ TypeDeclaration *type = iface.struct_declaration;
+ if(type && iface.array)
+ type = get_or_create_array_type(*type);
+ r_any_resolved = (type!=iface.type_declaration);
+ iface.type_declaration = type;
}
void TypeResolver::visit(FunctionDeclaration &func)
r_replacement_expr = 0;
expr->visit(*this);
if(r_replacement_expr)
+ {
expr = r_replacement_expr;
+ r_any_resolved = true;
+ }
r_replacement_expr = 0;
}
{
// Look for the variable in anonymous interface blocks.
for(i=blocks.begin(); (!declaration && i!=blocks.end()); ++i)
- if(i->second->instance_name.empty())
+ if(i->second->instance_name.empty() && i->second->struct_declaration)
{
- map<string, VariableDeclaration *>::iterator j = i->second->members.variables.find(var.name);
- if(j!=i->second->members.variables.end())
+ const map<string, VariableDeclaration *> &iface_vars = i->second->struct_declaration->members.variables;
+ map<string, VariableDeclaration *>::const_iterator j = iface_vars.find(var.name);
+ if(j!=iface_vars.end())
declaration = j->second;
}
}
{
visit_and_replace(memacc.left);
- map<string, VariableDeclaration *> *members = 0;
- if(StructDeclaration *strct = dynamic_cast<StructDeclaration *>(memacc.left->type))
- members = &strct->members.variables;
- else if(InterfaceBlockReference *iface_ref = dynamic_cast<InterfaceBlockReference *>(memacc.left.get()))
- {
- if(iface_ref->declaration)
- members = &iface_ref->declaration->members.variables;
- }
-
VariableDeclaration *declaration = 0;
- if(members)
+ if(StructDeclaration *strct = dynamic_cast<StructDeclaration *>(memacc.left->type))
{
- map<string, VariableDeclaration *>::iterator i = members->find(memacc.member);
- if(i!=members->end())
+ map<string, VariableDeclaration *>::iterator i = strct->members.variables.find(memacc.member);
+ if(i!=strct->members.variables.end())
declaration = i->second;
}
void VariableResolver::visit(VariableDeclaration &var)
{
- if(!block_interface.empty() && var.interface.empty())
- var.interface = block_interface;
-
if(var.layout)
var.layout->visit(*this);
if(var.array_size)
if(!iface.instance_name.empty())
stage->interface_blocks.insert(make_pair("_"+iface.instance_name, &iface));
- SetForScope<string> set_iface(block_interface, iface.interface);
TraversingVisitor::visit(iface);
}
void ExpressionResolver::visit(InterfaceBlockReference &iface)
{
- resolve(iface, 0, true);
+ if(iface.declaration)
+ resolve(iface, iface.declaration->type_declaration, true);
}
void ExpressionResolver::visit(MemberAccess &memacc)
InterfaceGenerator::InterfaceGenerator():
stage(0),
function_scope(false),
- iface_block(0),
copy_block(false),
iface_target_block(0)
{ }
InterfaceBlock *in_block = new InterfaceBlock;
in_block->interface = "in";
in_block->name = out_block.name;
+ in_block->members = new Block;
in_block->instance_name = out_block.instance_name;
if(stage->type==Stage::GEOMETRY)
in_block->array = true;
{
SetFlag set_copy(copy_block, true);
- SetForScope<Block *> set_target(iface_target_block, &in_block->members);
- SetForScope<NodeList<Statement>::iterator> set_ins_pt(iface_insert_point, in_block->members.body.end());
- out_block.members.visit(*this);
+ SetForScope<Block *> set_target(iface_target_block, in_block->members.get());
+ SetForScope<NodeList<Statement>::iterator> set_ins_pt(iface_insert_point, in_block->members->body.end());
+ if(out_block.struct_declaration)
+ out_block.struct_declaration->members.visit(*this);
+ else if(out_block.members)
+ out_block.members->visit(*this);
}
iface_target_block->body.insert(iface_insert_point, in_block);
}
for(j=prev_blocks.begin(); j!=prev_blocks.end(); ++j)
- if(j->second->instance_name.empty())
+ if(j->second->instance_name.empty() && j->second->struct_declaration)
{
- i = j->second->members.variables.find(var.name);
- if(i!=j->second->members.variables.end())
+ const map<string, VariableDeclaration *> &iface_vars = j->second->struct_declaration->members.variables;
+ i = iface_vars.find(var.name);
+ if(i!=iface_vars.end())
{
generate_interface(*j->second);
return;
void InterfaceGenerator::visit(VariableDeclaration &var)
{
if(copy_block)
- {
generate_interface(var, "in", var.name);
- return;
- }
-
- if(iface_block)
- {
- if(iface_block->linked_block)
- {
- // Link all variables to their counterparts in the linked block.
- const map<string, VariableDeclaration *> &linked_vars = iface_block->linked_block->members.variables;
- map<string, VariableDeclaration *>::const_iterator i = linked_vars.find(var.name);
- if(i!=linked_vars.end())
- {
- var.linked_declaration = i->second;
- var.linked_declaration->linked_declaration = &var;
- }
- }
- return;
- }
-
- if(var.interface=="out")
+ else if(var.interface=="out")
{
/* For output variables in function scope, generate a global interface
and replace the local declaration with an assignment. */
}
}
- SetForScope<InterfaceBlock *> set_iface(iface_block, &iface);
TraversingVisitor::visit(iface);
}
std::map<TypeDeclaration *, TypeDeclaration *> alias_map;
std::map<TypeDeclaration *, TypeDeclaration *> array_types;
NodeList<Statement>::iterator type_insert_point;
+ InterfaceBlock *iface_block;
bool r_any_resolved;
public:
virtual void visit(ImageTypeDeclaration &);
virtual void visit(StructDeclaration &);
virtual void visit(VariableDeclaration &);
+ virtual void visit(InterfaceBlock &);
virtual void visit(FunctionDeclaration &);
};
private:
Stage *stage;
RefPtr<Expression> r_replacement_expr;
- std::string block_interface;
bool r_any_resolved;
bool record_target;
bool r_self_referencing;
std::string in_prefix;
std::string out_prefix;
bool function_scope;
- InterfaceBlock *iface_block;
bool copy_block;
Block *iface_target_block;
NodeList<Statement>::iterator iface_insert_point;
unused_nodes.erase(var.type_declaration);
}
+void UnusedTypeRemover::visit(InterfaceBlock &iface)
+{
+ unused_nodes.erase(iface.type_declaration);
+}
+
void UnusedTypeRemover::visit(FunctionDeclaration &func)
{
unused_nodes.erase(func.return_type_declaration);
BlockVariableMap &global_variables = variables.back();
for(BlockVariableMap::iterator i=global_variables.begin(); i!=global_variables.end(); ++i)
{
+ string interface = i->first->interface;
+ bool linked = i->first->linked_declaration;
+ map<VariableDeclaration *, Node *>::iterator j = aggregates.find(i->first);
+ if(j!=aggregates.end())
+ if(InterfaceBlock *iface = dynamic_cast<InterfaceBlock *>(j->second))
+ {
+ interface = iface->interface;
+ linked = iface->linked_block;
+ }
+
/* Don't remove output variables which are used by the next stage or the
graphics API. */
- if(i->first->interface=="out" && (stage.type==Stage::FRAGMENT || i->first->linked_declaration || !i->first->name.compare(0, 3, "gl_")))
+ if(interface=="out" && (stage.type==Stage::FRAGMENT || linked || !i->first->name.compare(0, 3, "gl_")))
continue;
// Mark other unreferenced global variables as unused.
{
SetForScope<Node *> set(aggregate, &iface);
unused_nodes.insert(&iface);
- TraversingVisitor::visit(iface);
+ iface.struct_declaration->members.visit(*this);
}
void UnusedVariableRemover::visit(FunctionDeclaration &func)
virtual void visit(ImageTypeDeclaration &);
virtual void visit(StructDeclaration &);
virtual void visit(VariableDeclaration &);
+ virtual void visit(InterfaceBlock &);
virtual void visit(FunctionDeclaration &);
};
bool first = true;
for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
{
- if((*i)->source==BUILTIN_SOURCE)
+ if((*i)->source<=BUILTIN_SOURCE)
continue;
if(!first)
append('\n');
append(format("%s ", var.interpolation));
if(!var.sampling.empty())
append(format("%s ", var.sampling));
- if(!var.interface.empty() && var.interface!=block_interface)
+ if(!var.interface.empty())
{
string interface = var.interface;
if(mode==Compiler::PROGRAM && stage && stage->required_features.glsl_version<Version(1, 30))
void Formatter::visit(InterfaceBlock &iface)
{
- SetForScope<string> set(block_interface, iface.interface);
append(format("%s %s\n", iface.interface, iface.name));
- iface.members.visit(*this);
+ if(iface.struct_declaration)
+ iface.struct_declaration->members.visit(*this);
if(!iface.instance_name.empty())
{
append(' ');
unsigned source_line;
unsigned indent;
bool parameter_list;
- std::string block_interface;
public:
Formatter();
throw parse_error(tokenizer.get_location(), iface->interface, "an interface qualifier");
iface->name = expect_identifier();
- parse_block(iface->members, true, &Parser::parse_variable_declaration_with_layout);
+ iface->members = new Block;
+ parse_block(*iface->members, true, &Parser::parse_variable_declaration_with_layout);
if(!check(";"))
{
iface->instance_name = expect_identifier();
}
-StructDeclaration::StructDeclaration()
+StructDeclaration::StructDeclaration():
+ interface_block(0)
{
members.use_braces = true;
}
+StructDeclaration::StructDeclaration(const StructDeclaration &other):
+ TypeDeclaration(other),
+ members(other.members),
+ interface_block(0)
+{ }
+
+StructDeclaration::~StructDeclaration()
+{
+ if(interface_block && interface_block->struct_declaration==this)
+ interface_block->struct_declaration = 0;
+}
+
void StructDeclaration::visit(NodeVisitor &visitor)
{
visitor.visit(*this);
InterfaceBlock::InterfaceBlock():
array(false),
+ type_declaration(0),
+ struct_declaration(0),
linked_block(0)
-{
- members.use_braces = true;
-}
+{ }
InterfaceBlock::InterfaceBlock(const InterfaceBlock &other):
Statement(other),
members(other.members),
instance_name(other.instance_name),
array(other.array),
+ type_declaration(0),
+ struct_declaration(0),
linked_block(0)
{ }
{
if(linked_block && linked_block->linked_block==this)
linked_block->linked_block = 0;
+ if(struct_declaration && struct_declaration->interface_block==this)
+ struct_declaration->interface_block = 0;
}
void InterfaceBlock::visit(NodeVisitor &visitor)
enum
{
+ INTERNAL_SOURCE = -2,
BUILTIN_SOURCE = -1,
GENERATED_SOURCE = 0
};
{
Block members;
+ InterfaceBlock *interface_block;
+
StructDeclaration();
+ StructDeclaration(const StructDeclaration &);
+ ~StructDeclaration();
virtual StructDeclaration *clone() const { return new StructDeclaration(*this); }
virtual void visit(NodeVisitor &);
{
std::string interface;
std::string name;
- Block members;
+ NodePtr<Block> members;
std::string instance_name;
bool array;
+ /* An interface block's ultimate base type is always a struct. The
+ immediate type may be either that same struct or an array of it. */
+ TypeDeclaration *type_declaration;
+ StructDeclaration *struct_declaration;
InterfaceBlock *linked_block;
InterfaceBlock();
if(!iface.instance_name.empty())
check_definition(iface.instance_name, iface);
- SetFlag set_anon(anonymous_block, iface.instance_name.empty());
- TraversingVisitor::visit(iface);
+ if(iface.instance_name.empty() && iface.struct_declaration)
+ {
+ // Inject anonymous interface block members into the global scope
+ const map<string, VariableDeclaration *> &iface_vars = iface.struct_declaration->members.variables;
+ for(map<string, VariableDeclaration *>::const_iterator j=iface_vars.begin(); j!=iface_vars.end(); ++j)
+ check_definition(j->first, *j->second);
+ }
}
void DeclarationValidator::visit(FunctionDeclaration &func)
TraversingVisitor::visit(var);
}
+void ReferenceValidator::visit(InterfaceBlock &iface)
+{
+ if(!iface.struct_declaration)
+ error(iface, format("Interface block '%s %s' lacks a struct declaration", iface.interface, iface.name));
+ TraversingVisitor::visit(iface);
+}
+
void ReferenceValidator::visit(FunctionDeclaration &func)
{
if(!func.return_type_declaration)
virtual void visit(VariableReference &);
virtual void visit(InterfaceBlockReference &);
virtual void visit(VariableDeclaration &);
+ virtual void visit(InterfaceBlock &);
virtual void visit(FunctionDeclaration &);
};
void TraversingVisitor::visit(InterfaceBlock &iface)
{
- iface.members.visit(*this);
+ if(iface.members)
+ iface.members->visit(*this);
}
void TraversingVisitor::visit(FunctionDeclaration &func)