Recent

Author Topic: Find out expected result type (FPC internals)  (Read 549 times)

Fibonacci

  • Hero Member
  • *****
  • Posts: 786
  • Internal Error Hunter
Find out expected result type (FPC internals)
« on: June 29, 2025, 07:04:24 pm »
Disclaimer:
This is about compiler code, not normal program code. Im looking for someone familiar with FPC internals.




In the large "case" statement in "tinlinenode" in "ninl.pas" I added a custom entry that calls "handle_foo" when "in_foo" is matched.

My "handle_foo" is able to get all params, process them however I want, and return anything I want.

This test program compiles and works as expected:

Code: Pascal  [Select][+][-]
  1. const in_foo = 10001;
  2. function foo(i: integer): integer; [internproc: in_foo];
  3.  
  4. var
  5.   i: integer;
  6. begin
  7.   i := foo(123);
  8. end.

However, the following program also compiles and works:

Code: Pascal  [Select][+][-]
  1. const in_foo = 10001;
  2. function foo(i: integer): integer; [internproc: in_foo];
  3.  
  4. var
  5.   s: string;
  6. begin
  7.   s := foo(123);
  8. end.

Note that the declared return type of "foo" is "integer", but it doesnt matter. My "handle_foo" function decides what type to return.



The problem:

The problem is that I need to know what the expected result type is inside my "handle_foo". Ive been digging around long enough and just cant figure it out!

Whats the goal? I just want to add an intrinsic function, generally speaking.

Maybe my approach is wrong and I shouldnt do it in "ninl.pas"? Earlier I was playing around with "statement_syssym" in "pexpr.pas", but I thought I found an easier way in "ninl.pas".



A truncated version of my "handle_foo", just to give you a better idea of what Im talking about:

Code: Pascal  [Select][+][-]
  1. function tinlinenode.handle_foo: tnode;
  2. var
  3.   param, p: tnode;
  4. begin
  5.   param := left;
  6.   while param <> nil do begin
  7.     p := param;
  8.     if p.nodetype = callparan then p := tcallparanode(param).paravalue;
  9.  
  10.     if (not (nf_explicit in p.flags)) and (p.nodetype <> nothingn) then begin
  11.       case p.nodetype of
  12.         ordconstn:    writeln('int = ', tordconstnode(p).value.svalue);
  13.         stringconstn: writeln('str = ' , tstringconstnode(p).asrawbytestring);
  14.       end;
  15.     end;
  16.  
  17.     // next
  18.     param := tcallparanode(param).right;
  19.   end;
  20.  
  21.   // result, int
  22.   result := cordconstnode.create(1337, s32inttype, true);
  23.   // or string
  24.   //result := cstringconstnode.createstr('string result');
  25. end;

Thaddy

  • Hero Member
  • *****
  • Posts: 17413
  • Ceterum censeo Trumpum esse delendum (Tnx Charlie)
Re: Find out expected result type (FPC internals)
« Reply #1 on: June 30, 2025, 12:49:15 pm »
One observation:
The behavior is the same as the write compilerproc. Does the code cross-over to those internals somewhere?
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Fibonacci

  • Hero Member
  • *****
  • Posts: 786
  • Internal Error Hunter
Re: Find out expected result type (FPC internals)
« Reply #2 on: June 30, 2025, 02:28:56 pm »
I have looked into how SizeOf works, and it turns out it simply returns a "sizesinttype". Theres no validation of the result type at that point. The return value is always of type "sizesinttype", which is PtrInt, and thats it. Any type checking happens much later, on the assignment phase I guess.

I also looked at Copy, there are two versions:

Code: Pascal  [Select][+][-]
  1. Function Copy(S : AStringType; Index,Count : SizeInt) : String;
  2. Function Copy(A : DynArrayType; Index,Count : SizeInt) : DynArray;

Depending on the first param:
- if its a AStringType, the return type is whatever type of "resultdef" (of the param) is (any string-like type)
- if its a DynArrayType, the return is "carraydef"

So the return type is derived from the type of the first argument. Nowhere in this logic is the expected result type checked. The function just returns something, and whether its assignable to the expected result type is validated much later, presumably during assignment - but again, thats just my guess as a complete newbie, since Im only starting to play around with FPC internals.

PascalDragon

  • Hero Member
  • *****
  • Posts: 6035
  • Compiler Developer
Re: Find out expected result type (FPC internals)
« Reply #3 on: June 30, 2025, 11:06:31 pm »
The problem is that I need to know what the expected result type is inside my "handle_foo". Ive been digging around long enough and just cant figure it out!

A node's result type is determined by its pass_typecheck method where you need to set the resultdef field to the correct type definition.

Fibonacci

  • Hero Member
  • *****
  • Posts: 786
  • Internal Error Hunter
Re: Find out expected result type (FPC internals)
« Reply #4 on: July 01, 2025, 02:13:30 am »
As I suspected, the result type isnt known upfront - its set by the function itself.

Appreciate the answer. I'll try a different approach.

 

TinyPortal © 2005-2018