2 ms·
> Are D's nested functions truly functions Yes. They're even inlinable! > for example are they ABI-compatible with a top-level function? If they are annotate
by WalterBright 6y ago
> Are D's nested functions truly functions
Yes. They're even inlinable!
> for example are they ABI-compatible with a top-level function?
If they are annotated with `static`, yes. If they are not so annotated, they are ABI-compatible with non-static member functions. In fact, in D, a pointer to a non-static member is interchangeable with a pointer to a non-static nested function. This makes for much convenience when dealing with algorithms that accept such pointers (called "delegates" in D):
> how do they get access to the stack frame?
And extra argument is passed to the function which is the frame pointer to the calling function. (This is called a "static link". The "dynamic link" is the return address.) This can be thought of as the "this" pointer to the "struct" that represents the caller's stack frame, hence the binary compatibility with member functions.
To go back to the enclosing enclosing enclosing function's stack frame, you dereference the static link 3 times.
The tricky part is optimizing these dereferences away when there are no captured variables in a stack frame.