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:
const in_foo = 10001;
function foo(i: integer): integer; [internproc: in_foo];
var
i: integer;
begin
i := foo(123);
end.
However, the following program also compiles and works:
const in_foo = 10001;
function foo(i: integer): integer; [internproc: in_foo];
var
s: string;
begin
s := foo(123);
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:
function tinlinenode.handle_foo: tnode;
var
param, p: tnode;
begin
param := left;
while param <> nil do begin
p := param;
if p.nodetype = callparan then p := tcallparanode(param).paravalue;
if (not (nf_explicit in p.flags)) and (p.nodetype <> nothingn) then begin
case p.nodetype of
ordconstn: writeln('int = ', tordconstnode(p).value.svalue);
stringconstn: writeln('str = ' , tstringconstnode(p).asrawbytestring);
end;
end;
// next
param := tcallparanode(param).right;
end;
// result, int
result := cordconstnode.create(1337, s32inttype, true);
// or string
//result := cstringconstnode.createstr('string result');
end;