Nested Functions?

Hi friend,
Good day,

Does anyone know how to implement “Nested Function” in “LLVM IR”?

Nested functions are functions that have access to locals and params in outer function. For example consider C like bellow code:

void Func()
{

int X;
int AddToX(int y)
{
    return X+y;
}
X=2;
std::cout <<  AddToX(5);

}

closures are a similar concept. There is no native support for them. You have to build your nested functions on top of LLMV IR.

1 Like

You should be able to “slap on” some leading text so the scoped symbols are not visible external to the scoping boundary (conceptually not much different than labels within a function.

decorating your function::

void Func()
{
int X;
int Func_BB3_AddToX(int y)
{
return X+y;
}
X=2;
std::cout << Func_BB3_AddToX(5);
}

You may need to slap on some basic block information in case someone uses the same inner function names in nested scopes.