Thread overview
Lookhead for nested functions
Jun 14, 2019
Amex
Jun 14, 2019
FeepingCreature
Jun 15, 2019
Amex
Jun 15, 2019
rikki cattermole
Jun 14, 2019
Timon Gehr
Jun 15, 2019
Walter Bright
June 14, 2019
fails:

void foo()
{
   bar();
   void bar() { }
}

passes:

void foo()
{
   void bar() { }
   bar();
}


I realize this may not be a simple solution in all cases but surely it can be make to work in such simple cases?




June 14, 2019
On Friday, 14 June 2019 at 05:35:31 UTC, Amex wrote:
> I realize this may not be a simple solution in all cases but surely it can be make to work in such simple cases?

Nope. Consider the following code:

int foo()
{
  return bar();
  int i = 5;
  int bar() { return i; }
}

Nested functions are inextricably bound to function scope, and function scope is strictly sequential.
June 14, 2019
On 14.06.19 07:35, Amex wrote:
> fails:
> 
> void foo()
> {
>     bar();
>     void bar() { }
> }
> 
> passes:
> 
> void foo()
> {
>     void bar() { }
>     bar();
> }
> 
> 
> I realize this may not be a simple solution in all cases but surely it can be make to work in such simple cases?
> 
> 
> 
> 

If you need mutual recursion for local functions, this hack works:

void foo(){
    void bar()(){ baz(); }
    void baz(){ bar(); }
}
June 14, 2019
On 6/14/2019 5:50 AM, Timon Gehr wrote:
> If you need mutual recursion for local functions, this hack works:
> 
> void foo(){
>      void bar()(){ baz(); }
>      void baz(){ bar(); }
> }

Using function pointers also works.

The point being, the use cases are rare enough that such workarounds are reasonable given the problems of trying to compile forward dependencies.
June 15, 2019
On Friday, 14 June 2019 at 06:00:13 UTC, FeepingCreature wrote:
> On Friday, 14 June 2019 at 05:35:31 UTC, Amex wrote:
>> I realize this may not be a simple solution in all cases but surely it can be make to work in such simple cases?
>
> Nope. Consider the following code:
>
> int foo()
> {
>   return bar();
>   int i = 5;
>   int bar() { return i; }
> }
>

There is nothing wrong with this.


June 15, 2019
On 15/06/2019 10:16 PM, Amex wrote:
> On Friday, 14 June 2019 at 06:00:13 UTC, FeepingCreature wrote:
>> On Friday, 14 June 2019 at 05:35:31 UTC, Amex wrote:
>>> I realize this may not be a simple solution in all cases but surely it can be make to work in such simple cases?
>>
>> Nope. Consider the following code:
>>
>> int foo()
>> {
>>   return bar();
>>   int i = 5;
>>   int bar() { return i; }
>> }
>>
> 
> There is nothing wrong with this.

Yes there is.
i never got initialized, it is effectively =void. Assuming its stored on the stack and not a register.

E.g.

Here is an example that shows what you're suggesting.

int func() {
	int x;
	somethingElse(x);
	int y = 7;
	return y;
}

Assembled:

int onlineapp.func():
	push	RBP
	mov	RBP,RSP
	sub	RSP,010h
	xor	EAX,EAX
	mov	-8[RBP],EAX
	mov	EDI,EAX
	call	  void onlineapp.somethingElse(int)@PLT32
	mov	ECX,7
	mov	-4[RBP],ECX
	mov	EAX,ECX
	leave
	ret

The instructions:
	mov	ECX,7
	mov	-4[RBP],ECX

Never ran and hence who knows what the return value is.

If you really want this, it will require a DIP.