This tool will move variable declarations to their innermost possible used scopes. For a declaration, find the innermost scope we can move it into, without breaking the code's original semantics.

User Instructions

The translator, moveDeclarationToInnermostScope, accepts the following options:


For the following input code,

void AccumulateForce(int *idxBound, int *idxList, int len,
double *tmp, double *force)
register int ii ;
register int jj ;
int count ;
int *list ;
int idx ;
double sum ;
for (ii=0; ii<len; ++ii) {
count = idxBound[ii+1] - idxBound[ii] ;
list = &idxList[idxBound[ii]] ;
sum = 0.0 ;
for (jj=0; jj<count; ++jj) {
idx = list[jj] ;
sum += tmp[idx] ;
force[ii] += sum ;
return ;

you can run the move tool as follows to produce the output file below:

moveDeclarationToInnermostScope -rose:unparse_tokens -rose:merge_decl_assign -c

There are several things to notice about this command line. The moveDeclarationToInnermostScope tool acts as a front-end to an underlying compiler, and the command line options for that compiler will be honored. Here, we also have some ROSE/tool specific command line options. The '-rose:unparse_tokens' option tells ROSE to take extra care to preserve the source-code formatting from the input source file when producing the output file. The '-rose:merge_decl_assign' option is specific to the rescoping tool, and indicates that any moved declarations should try to be combined with pre-existing assignment statements in the target scope.

The output file will look like

void AccumulateForce(int *idxBound, int *idxList, int len,
double *tmp, double *force)
for (register int ii = 0; ii<len; ++ii) {
int count = idxBound[ii + 1] - idxBound[ii];
int *list = &idxList[idxBound[ii]];
double sum = 0.0;
for (register int jj = 0; jj<count; ++jj) {
int idx = list[jj];
sum += tmp[idx] ;
force[ii] += sum ;
return ;

Looking at the transformed source code above, there are several points of interest:

Internal Algorithm

Focusing on finding target scopes, since multiple (iterative) declaration moves are unnecessary. If we know the final scopes to be moved into, we can copy-move a declaration to all target scopes in one shot

Analysis: findFinalTargetScopes (declaration, &target_scopes)
while (!scope_tree_worklist.empty())
current_scope_tree = scope_tree_worklist.front(); …
collectCandidateTargetScopes(decl, current_scope_tree);
if (found a bottom scope) target_scopes.push_back(candidate)
else scope_tree_worklist.push_back(candiate)
if (target_scopes.size()>0)
copyMoveVariableDeclaration(decl, target_scopes);

This page is generated from $ROSE/docs/Rose/Tools/declarationMoveTool.dox

Collaboration diagram for Declaration move tool: