#include <msp/core/raii.h>
#include <msp/strings/format.h>
#include "optimize.h"
+#include "reflect.h"
using namespace std;
InlineContentInjector::InlineContentInjector():
source_func(0),
- pass(DEPENDS)
+ pass(REFERENCED)
{ }
const string &InlineContentInjector::apply(Stage &stage, FunctionDeclaration &target_func, Block &tgt_blk, const NodeList<Statement>::iterator &ins_pt, FunctionCall &call)
{
source_func = call.declaration->definition;
- // Collect all declarations the inlined function depends on.
- pass = DEPENDS;
- source_func->visit(*this);
-
/* Populate referenced_names from the target function so we can rename
variables from the inlined function that would conflict. */
pass = REFERENCED;
tgt_blk.body.splice(ins_pt, staging_block.body);
- NodeReorderer().apply(stage, target_func, dependencies);
+ NodeReorderer().apply(stage, target_func, DependencyCollector().apply(*source_func));
return r_result_name;
}
if(i!=staging_block.variables.end())
var.name = i->second->name;
}
- else if(pass==DEPENDS && var.declaration)
- {
- dependencies.insert(var.declaration);
- var.declaration->visit(*this);
- }
else if(pass==REFERENCED)
referenced_names.insert(var.name);
}
void InlineContentInjector::visit(InterfaceBlockReference &iface)
{
- if(pass==DEPENDS && iface.declaration)
- {
- dependencies.insert(iface.declaration);
- iface.declaration->visit(*this);
- }
- else if(pass==REFERENCED)
+ if(pass==REFERENCED)
referenced_names.insert(iface.name);
}
void InlineContentInjector::visit(FunctionCall &call)
{
- if(pass==DEPENDS && call.declaration)
- dependencies.insert(call.declaration);
- else if(pass==REFERENCED)
+ if(pass==REFERENCED)
referenced_names.insert(call.name);
TraversingVisitor::visit(call);
}
}
}
}
- else if(pass==DEPENDS && var.type_declaration)
- {
- dependencies.insert(var.type_declaration);
- var.type_declaration->visit(*this);
- }
else if(pass==REFERENCED)
referenced_names.insert(var.type);
}
#include "reflect.h"
+using namespace std;
+
namespace Msp {
namespace GL {
namespace SL {
r_count *= literal->value.value<int>();
}
+
+set<Node *> DependencyCollector::apply(FunctionDeclaration &func)
+{
+ func.visit(*this);
+ return dependencies;
+}
+
+void DependencyCollector::visit(VariableReference &var)
+{
+ if(var.declaration && !locals.count(var.declaration))
+ {
+ dependencies.insert(var.declaration);
+ var.declaration->visit(*this);
+ }
+}
+
+void DependencyCollector::visit(InterfaceBlockReference &iface)
+{
+ if(iface.declaration)
+ {
+ dependencies.insert(iface.declaration);
+ iface.declaration->visit(*this);
+ }
+}
+
+void DependencyCollector::visit(FunctionCall &call)
+{
+ if(call.declaration)
+ dependencies.insert(call.declaration);
+ TraversingVisitor::visit(call);
+}
+
+void DependencyCollector::visit(VariableDeclaration &var)
+{
+ locals.insert(&var);
+ if(var.type_declaration)
+ {
+ dependencies.insert(var.type_declaration);
+ var.type_declaration->visit(*this);
+ }
+
+ TraversingVisitor::visit(var);
+}
+
} // namespace SL
} // namespace GL
} // namespace Msp
virtual void visit(VariableDeclaration &);
};
+/** Collects dependencies of a function. This includes global variables,
+interface blocks, other functions and types. */
+class DependencyCollector: private TraversingVisitor
+{
+private:
+ std::set<Node *> dependencies;
+ std::set<Node *> locals;
+
+public:
+ std::set<Node *> apply(FunctionDeclaration &);
+
+private:
+ virtual void visit(VariableReference &);
+ virtual void visit(InterfaceBlockReference &);
+ virtual void visit(FunctionCall &);
+ virtual void visit(VariableDeclaration &);
+};
+
} // namespace SL
} // namespace GL
} // namespace Msp