Find out expected result type (FPC internals) lazarus / 64-FPC development ---------------------------------------------------------------------- Fibonacci · 2025-06-29 17:04 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 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: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 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: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 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; ---------------------------------------------------------------------- Thaddy · 2025-06-30 10:49 One observation: The behavior is the same as the write compilerproc. Does the code cross-over to those internals somewhere? ---------------------------------------------------------------------- Fibonacci · 2025-06-30 12:28 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Function Copy(S : AStringType; Index,Count : SizeInt) : String;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 · 2025-06-30 21:06 Quote from: Fibonacci on June 29, 2025, 07:04:24 pmThe 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 · 2025-07-01 00:13 As I suspected, the result type isnt known upfront - its set by the function itself. Appreciate the answer. I'll try a different approach.