TraversingVisitor::visit(block);
}
-void DeclarationCombiner::visit(FunctionDeclaration &func)
-{
- vector<FunctionDeclaration *> &decls = functions[func.name];
- if(func.definition)
- {
- for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
- {
- (*i)->definition = func.definition;
- (*i)->body.body.clear();
- }
- }
- decls.push_back(&func);
-}
-
void DeclarationCombiner::visit(VariableDeclaration &var)
{
VariableDeclaration *&ptr = variables[var.name];
{
RefPtr<Literal> literal = new Literal;
if(var.type=="bool")
- {
literal->token = (i->second ? "true" : "false");
- literal->value = static_cast<bool>(i->second);
- }
else if(var.type=="int")
- {
literal->token = lexical_cast<string>(i->second);
- literal->value = i->second;
- }
var.init_expression = literal;
}
}
}
-void BlockResolver::enter(Block &block)
+void BlockHierarchyResolver::enter(Block &block)
{
block.parent = current_block;
}
VariableResolver::VariableResolver():
stage(0),
- builtins(0),
- members(0),
+ r_members(0),
record_target(false),
- assignment_target(0),
- self_referencing(false)
+ r_self_referencing(false),
+ r_assignment_target(0)
{ }
void VariableResolver::apply(Stage &s)
{
stage = &s;
- Stage *builtin_stage = get_builtins(s.type);
- builtins = (builtin_stage ? &builtin_stage->content : 0);
+ s.types.clear();
+ s.interface_blocks.clear();
s.content.visit(*this);
}
-Block *VariableResolver::next_block(Block &block)
-{
- return block.parent ? block.parent : &block!=builtins ? builtins : 0;
-}
-
void VariableResolver::enter(Block &block)
{
block.variables.clear();
void VariableResolver::visit(VariableReference &var)
{
var.declaration = 0;
- members = 0;
- for(Block *block=current_block; (!var.declaration && block); block=next_block(*block))
+ r_members = 0;
+ /* Look for variable declarations in the block hierarchy first. Interface
+ blocks are always defined in the top level so we can't accidentally skip
+ one. */
+ for(Block *block=current_block; (!var.declaration && block); block=block->parent)
{
map<string, VariableDeclaration *>::iterator i = block->variables.find(var.name);
if(i!=block->variables.end())
if(var.declaration)
{
if(var.declaration->type_declaration)
- members = &var.declaration->type_declaration->members.variables;
+ r_members = &var.declaration->type_declaration->members.variables;
}
else
{
map<string, InterfaceBlock *>::const_iterator i = blocks.find(var.name);
if(i!=blocks.end() && i->second->instance_name==var.name)
{
- iface_ref = new InterfaceBlockReference;
- iface_ref->name = var.name;
- iface_ref->declaration = i->second;
- members = &i->second->members.variables;
+ /* The name refers to an interface block with an instance name rather
+ than a variable. Prepare a new syntax tree node accordingly. */
+ r_iface_ref = new InterfaceBlockReference;
+ r_iface_ref->name = var.name;
+ r_iface_ref->declaration = i->second;
+ r_members = &i->second->members.variables;
}
else
{
+ // Look for the variable in anonymous interface blocks.
for(i=blocks.begin(); (!var.declaration && i!=blocks.end()); ++i)
if(i->second->instance_name.empty())
{
if(record_target)
{
- if(assignment_target)
+ if(r_assignment_target)
{
+ /* More than one variable reference found in assignment target.
+ Unable to determine what the primary target is. */
record_target = false;
- assignment_target = 0;
+ r_assignment_target = 0;
}
else
- assignment_target = var.declaration;
+ r_assignment_target = var.declaration;
}
- else if(var.declaration && var.declaration==assignment_target)
- self_referencing = true;
+ else if(var.declaration && var.declaration==r_assignment_target)
+ r_self_referencing = true;
}
void VariableResolver::visit(InterfaceBlockReference &iface)
{
iface.declaration = 0;
- for(Block *block=current_block; block; block=next_block(*block))
+ for(Block *block=current_block; block; block=block->parent)
{
map<string, InterfaceBlock *>::iterator i = stage->interface_blocks.find(iface.name);
if(i!=stage->interface_blocks.end())
{
iface.declaration = i->second;
- members = &i->second->members.variables;
+ r_members = &i->second->members.variables;
break;
}
}
void VariableResolver::visit(MemberAccess &memacc)
{
- members = 0;
- iface_ref = 0;
+ r_members = 0;
+ r_iface_ref = 0;
memacc.left->visit(*this);
- if(iface_ref)
- memacc.left = iface_ref;
- iface_ref = 0;
+ if(r_iface_ref)
+ memacc.left = r_iface_ref;
+ r_iface_ref = 0;
memacc.declaration = 0;
- if(members)
+ if(r_members)
{
- map<string, VariableDeclaration *>::iterator i = members->find(memacc.member);
- if(i!=members->end())
+ map<string, VariableDeclaration *>::iterator i = r_members->find(memacc.member);
+ if(i!=r_members->end())
{
memacc.declaration = i->second;
if(i->second->type_declaration)
- members = &i->second->type_declaration->members.variables;
+ r_members = &i->second->type_declaration->members.variables;
}
else
- members = 0;
+ r_members = 0;
}
}
+void VariableResolver::visit(UnaryExpression &unary)
+{
+ TraversingVisitor::visit(unary);
+ r_members = 0;
+ r_iface_ref = 0;
+}
+
void VariableResolver::visit(BinaryExpression &binary)
{
- if(binary.oper=="[")
+ if(binary.oper->token[0]=='[')
{
{
- SetForScope<bool> set(record_target, false);
+ /* The subscript expression is not a part of the primary assignment
+ target. */
+ SetFlag set(record_target, false);
binary.right->visit(*this);
}
- members = 0;
- iface_ref = 0;
+ r_members = 0;
+ r_iface_ref = 0;
binary.left->visit(*this);
- if(iface_ref)
- binary.left = iface_ref;
- iface_ref = 0;
+ if(r_iface_ref)
+ binary.left = r_iface_ref;
}
else
{
TraversingVisitor::visit(binary);
- members = 0;
+ r_members = 0;
}
+
+ r_iface_ref = 0;
}
void VariableResolver::visit(Assignment &assign)
{
{
SetFlag set(record_target);
- assignment_target = 0;
+ r_assignment_target = 0;
assign.left->visit(*this);
+ assign.target_declaration = r_assignment_target;
}
- self_referencing = false;
+ r_self_referencing = false;
assign.right->visit(*this);
+ assign.self_referencing = (r_self_referencing || assign.oper->token[0]!='=');
- assign.self_referencing = (self_referencing || assign.oper!="=");
- assign.target_declaration = assignment_target;
+ r_members = 0;
+ r_iface_ref = 0;
+}
+
+void VariableResolver::visit(FunctionCall &call)
+{
+ TraversingVisitor::visit(call);
+ r_members = 0;
+ r_iface_ref = 0;
}
void VariableResolver::visit(StructDeclaration &strct)
}
+void FunctionResolver::apply(Stage &s)
+{
+ stage = &s;
+ s.functions.clear();
+ s.content.visit(*this);
+}
+
void FunctionResolver::visit(FunctionCall &call)
{
- map<string, vector<FunctionDeclaration *> >::iterator i = functions.find(call.name);
- if(i!=functions.end())
- call.declaration = i->second.back();
+ map<string, FunctionDeclaration *>::iterator i = stage->functions.find(call.name);
+ if(i!=stage->functions.end())
+ call.declaration = i->second;
TraversingVisitor::visit(call);
}
void FunctionResolver::visit(FunctionDeclaration &func)
{
- vector<FunctionDeclaration *> &decls = functions[func.name];
- if(func.definition)
+ FunctionDeclaration *&stage_decl = stage->functions[func.name];
+ vector<FunctionDeclaration *> &decls = declarations[func.name];
+ if(func.definition==&func)
{
+ stage_decl = &func;
+
+ // Set all previous declarations to use this definition.
for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
+ {
(*i)->definition = func.definition;
- decls.clear();
- decls.push_back(&func);
+ (*i)->body.body.clear();
+ }
}
- else if(!decls.empty() && decls.back()->definition)
- func.definition = decls.back()->definition;
else
- decls.push_back(&func);
+ {
+ func.definition = 0;
+ if(!stage_decl)
+ stage_decl = &func;
+ else
+ func.definition = stage_decl->definition;
+ }
+ decls.push_back(&func);
TraversingVisitor::visit(func);
}
iface_var->sampling = var.sampling;
iface_var->interface = iface;
iface_var->type = var.type;
- iface_var->type_declaration = var.type_declaration;
iface_var->name = name;
+ /* Geometry shader inputs are always arrays. But if we're bringing in an
+ entire block, the array is on the block and not individual variables. */
if(stage->type==Stage::GEOMETRY && !copy_block)
iface_var->array = ((var.array && var.interface!="in") || iface=="in");
else
iface_var->array = var.array;
if(iface_var->array)
iface_var->array_size = var.array_size;
- iface_var->layout = var.layout;
if(iface=="in")
{
+ iface_var->layout = var.layout;
iface_var->linked_declaration = &var;
var.linked_declaration = iface_var;
}
VariableReference *ref = new VariableReference;
ref->name = left;
assign->left = ref;
- assign->oper = "=";
+ assign->oper = &Operator::get_operator("=", Operator::BINARY);
assign->right = right;
ExpressionStatement *stmt = new ExpressionStatement;
{
if(var.declaration || !stage->previous)
return;
- /* Don't pull a variable from previous stage if we just generated an out
+ /* Don't pull a variable from previous stage if we just generated an output
interface in this stage */
if(stage->content.variables.count(var.name))
return;
{
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())
if(var.interface=="out")
{
- /* For out variables in function scope, generate a global interface and
- replace the local declaration with an assignment. */
+ /* For output variables in function scope, generate a global interface
+ and replace the local declaration with an assignment. */
if(function_scope && generate_interface(var, "out", var.name))
{
nodes_to_remove.insert(&var);
}
else if(var.interface=="in")
{
- /* Try to link in variables in global scope with out variables from
- previous stage */
+ /* Try to link input variables in global scope with output variables from
+ previous stage. */
if(current_block==&stage->content && !var.linked_declaration && stage->previous)
{
const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
{
if(iface.interface=="in")
{
+ /* Try to link input blocks with output blocks sharing the same block
+ name from previous stage. */
if(!iface.linked_block && stage->previous)
{
const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
{
vector<VariableDeclaration *> pass_vars;
+ // Pass through all input variables of this stage.
for(map<string, VariableDeclaration *>::const_iterator i=stage->content.variables.begin(); i!=stage->content.variables.end(); ++i)
if(i->second->interface=="in")
pass_vars.push_back(i->second);
const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
for(map<string, VariableDeclaration *>::const_iterator i=prev_vars.begin(); i!=prev_vars.end(); ++i)
{
- bool linked = false;
- for(vector<VariableDeclaration *>::const_iterator j=pass_vars.begin(); (!linked && j!=pass_vars.end()); ++j)
- linked = ((*j)->linked_declaration==i->second);
+ if(i->second->interface!="out")
+ continue;
- if(!linked && generate_interface(*i->second, "in", i->second->name))
+ /* Pass through output variables from the previous stage, but only
+ those which are not already linked to an input here. */
+ if(!i->second->linked_declaration && generate_interface(*i->second, "in", i->second->name))
pass_vars.push_back(i->second);
}
}
if(stage->type==Stage::GEOMETRY)
{
+ /* Special case for geometry shader: copy gl_Position from input to
+ output. */
InterfaceBlockReference *ref = new InterfaceBlockReference;
ref->name = "gl_in";
BinaryExpression *subscript = new BinaryExpression;
subscript->left = ref;
- subscript->oper = "[";
+ subscript->oper = &Operator::get_operator("[", Operator::BINARY);
subscript->right = pass.subscript;
- subscript->after = "]";
MemberAccess *memacc = new MemberAccess;
memacc->left = subscript;
{
BinaryExpression *subscript = new BinaryExpression;
subscript->left = ref;
- subscript->oper = "[";
+ subscript->oper = &Operator::get_operator("[", Operator::BINARY);
subscript->right = pass.subscript;
- subscript->after = "]";
insert_assignment(out_name, subscript);
}
else
nodes_to_remove.insert(&pass);
}
-
-DeclarationReorderer::DeclarationReorderer():
- kind(NO_DECLARATION)
-{ }
-
-void DeclarationReorderer::visit(FunctionCall &call)
-{
- FunctionDeclaration *def = call.declaration;
- if(def)
- def = def->definition;
- if(def && !ordered_funcs.count(def))
- needed_funcs.insert(def);
-}
-
-void DeclarationReorderer::visit(Block &block)
-{
- if(block.parent)
- return TraversingVisitor::visit(block);
-
- NodeList<Statement>::iterator struct_insert_point = block.body.end();
- NodeList<Statement>::iterator variable_insert_point = block.body.end();
- NodeList<Statement>::iterator function_insert_point = block.body.end();
- unsigned unordered_func_count = 0;
- bool ordered_any_funcs = false;
-
- for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); )
- {
- kind = NO_DECLARATION;
- (*i)->visit(*this);
-
- bool moved = false;
- if(kind==STRUCT && struct_insert_point!=block.body.end())
- {
- block.body.insert(struct_insert_point, *i);
- moved = true;
- }
- else if(kind>STRUCT && struct_insert_point==block.body.end())
- struct_insert_point = i;
-
- if(kind==VARIABLE && variable_insert_point!=block.body.end())
- {
- block.body.insert(variable_insert_point, *i);
- moved = true;
- }
- else if(kind>VARIABLE && variable_insert_point==block.body.end())
- variable_insert_point = i;
-
- if(kind==FUNCTION)
- {
- if(function_insert_point==block.body.end())
- function_insert_point = i;
-
- if(needed_funcs.empty())
- {
- ordered_funcs.insert(i->get());
- if(i!=function_insert_point)
- {
- block.body.insert(function_insert_point, *i);
- moved = true;
- }
- else
- ++function_insert_point;
- ordered_any_funcs = true;
- }
- else
- ++unordered_func_count;
- }
-
- if(moved)
- {
- if(function_insert_point==i)
- ++function_insert_point;
- block.body.erase(i++);
- }
- else
- ++i;
-
- if(i==block.body.end() && unordered_func_count)
- {
- if(!ordered_any_funcs)
- // A subset of the remaining functions forms a recursive loop
- /* TODO pick a function and move it up, adding any necessary
- declarations */
- break;
-
- i = function_insert_point;
- unordered_func_count = 0;
- }
- }
-}
-
-void DeclarationReorderer::visit(VariableDeclaration &var)
-{
- TraversingVisitor::visit(var);
- kind = VARIABLE;
-}
-
-void DeclarationReorderer::visit(FunctionDeclaration &func)
-{
- needed_funcs.clear();
- func.body.visit(*this);
- needed_funcs.erase(&func);
- kind = FUNCTION;
-}
-
} // namespace SL
} // namespace GL
} // namespace Msp