Sorry if this is a known issue, I couldn't find this on a cursory search of the forums. Also this is all with VAX 1557.
I frequently want to extract functions that have a single variable which I want to be the implied return value.
void foo()
{
int a;
a = 1 + 2;
return a;
}
I would like to refactor the line 'a = 1 + 2' and have something like this:
void foo()
{
int a;
a = addFunction();
return a;
}
int addFunction()
{
return 1 + 2;
}
Unfortunately what I get is this:
void foo()
{
int a;
a = addFunction(a);
return a;
}
int addFunction( int a )
{
a = 1 + 2; return a;
}
Which of course make sense if VAX has no way of knowing that 'a' is only used as an L-Value within the new function. If I select '1 + 2;' I get an invalid method. If I select '1 + 2' but not the trailing semicolon I get the correct method. The trailing semicolon issue seems like a bug to me. However ultimately it would be nice to properly identify 'a' as simply the return L Value and removed.
A small aside:
An alternative valid implementation could be:
void addFunction( int& a )
{
a = 1 + 2;
}
This form seems to be better at preserving the behavior of the original function. And it doesn't require the variable be copiable, or if there is any secondary costs/operations that occur due to the copy.
One motivation to do implicit returns is that it may be required to generate valid code. One way to strongly suggest that the user intends for a return would be to include the declaration of the variable intended to be returned in the extracted function. In my example, both lines 'int a;' and 'a = 1 + 2;' Which would result in VAX realizing that since the symbol 'a' is used after the extraction a new symbol 'a' must be created and assigned via the return of the new function.
void foo()
{
/* Lines refactored
int a;
a = 1 + 2;
*/
// New VAX generated code
int a = addFunction();
// Original Code
return a;
}
int addFunction()
{
int a;
a = 1 + 2;
return a;
}
Currently VAX generates uncompilable code. This:
int foo()
{
addFunction();
return a;
}
void addFunction()
{
int a;
a = 1 + 2;
}
VAX generates some truly strange code if you miss the last semicolon. Extracting 'int a' and 'a = 1 + 2' generates:
bool addFunction()
{
return int a;
a = 1 + 2;
}
Which of course doesn't compile.
It would be nice if VAX would check if the extract would actually generate working code simply by checking to see if the extracted symbols are used after the extracted section. If only one symbol is used, it can be implicitly defined as being assigned by the return. Multiple symbols may well be impossible to properly handle in a general sense (except for simple value types). A warning or something could be nice.(configurable on/off of course).
Regards,
Colin Branch