Incompatible type for arg... address of function.... is nested
lazarus / 62-General
----------------------------------------------------------------------
440bx · 2019-03-12 20:49
Hello,
I am porting some Delphi 2 code which compiles and runs fine (that is,
on the platform it was built to run on.) I am getting the following
error from FPC:
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 (5089,66)
Error: Incompatible type for arg no. 2: Got "
",
expected ""the parameter types
and result all seem fine but, the message has "is nested" after the
function result.
The function isn't nested into another function or procedure. It's a
normal function.
What is the meaning of "is nested" and what am I supposed to change to
make it acceptable to FPC ?
just in case it helps, the error takes place on the line that calls
SetWindowsHookEx: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 case Captured of TRUE: begin
Scroll32Hook := SetWindowsHookEx(WH_MSGFILTER,
@ScrollHookMsgFilter,
0, { local hook }
GetCurrentThreadId); end;
FALSE: begin if Scroll32Hook <> 0 then
UnhookWindowsHookEx(Scroll32Hook); Scroll32Hook := 0;
end; end; { case Captured }
the hook function is:
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
ScrollHookMsgFilter(Code : longint;
wParam : ptrint; Msg : ptrint) :
ptrint; stdcall;var MsgPtr : PMsg absolute Msg; BoxInfoPtr :
PBoxInfo; begin BoxInfoPtr := pointer(GetWindowLong(GetCapture,
0)); with BoxInfoPtr^, MsgPtr^ do begin Scroll32PosB := Pt;
ScreenToClient(hWnd, Scroll32PosB); end; Result := 0;end;
NOTE: I'm currently porting it to FPC in 32bit. I am not yet focusing
on changing the data types and function calls to make it runnable in
64bit (e.g, GetWindowLong to GetWindowLongPtr, etc.)
Thank you for your help.
----------------------------------------------------------------------
marcov · 2019-03-12 21:06
Nested procvars are a separate type of proc/methods vars.
So you have normal procedures variables for global functions,
procedure "of object" for methods, and "is nested" for nested
functions.
Nested functions are a bit like method vars, in that the procvars are
two pointers and have limited validity.
method vars contain code: function address and data :self, and
validity till the object is freed. nested procvar contain code:
function address and data: framepointer, and validity till the outer
procedure is finished and the framepointer is thus no longer valid
In short, it is is a fairly pascal specific feature, and I doubt it
will be of use in windows interfacing. I suspect you are accidentally
trying to passa nested function to something expecting a global
function, and you are not in {$mode delphi}
----------------------------------------------------------------------
jamie · 2019-03-12 21:12
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
SetWindowsHookEx(idHook:longint; lpfn:HOOKPROC; hmod:HINST;
dwThreadId:DWORD):HHOOK;
Try Casting around your address of your callback...
HOCKRPOC(@YourcallBack)
----------------------------------------------------------------------
440bx · 2019-03-12 21:27
Quote from: marcov on March 12, 2019, 10:06:47 pmNested procvars are a
separate type of proc/methods vars.
So you have normal procedures variables for global functions,
procedure "of object" for methods, and "is nested" for nested
functions.
Nested functions are a bit like method vars, in that the procvars are
two pointers and have limited validity.
method vars contain code: function address and data :self, and
validity till the object is freed. nested procvar contain code:
function address and data: framepointer, and validity till the outer
procedure is finished and the framepointer is thus no longer valid
In short, it is is a fairly pascal specific feature, and I doubt it
will be of use in windows interfacing. I suspect you are accidentally
trying to passa nested function to something expecting a global
function, and you are not in {$mode delphi}
@Marco
The thing is, the function is not a method, and is not nested. It's
just a plain vanilla function. Nothing special about it other than
being called by a hook. I am not in {$MODE DELPHI}, I haven't needed
it to specify callbacks in other cases (such as the examples I've
given in the bug tracker for various enumeration APIs, none have
required {$MODE DELPHI}.)
Maybe there is something in the code that makes it believe the
function is nested into another. I'll double check the code.
@Jamie
Quote from: jamie on March 12, 2019, 10:12:10 pmTry Casting around
your address of your callback...
HOCKRPOC(@YourcallBack)
Thank you Jamie, that works. I don't see why I have to cast it but,
for now, I'll take it. I'm sure that's going to bite me later but, at
least for now, I can move forward.
There must be something in the code that is confusing FPC. It will be
interesting to find out what it is.
----------------------------------------------------------------------
Remy Lebeau · 2019-03-12 23:24
Quote from: 440bx on March 12, 2019, 10:27:27 pmThe thing is, the
function is not a method, and is not nested. It's just a plain
vanilla function.
...
There must be something in the code that is confusing FPC. It will be
interesting to find out what it is.
Kind of hard to judge that when you did not show the full code.
----------------------------------------------------------------------
440bx · 2019-03-13 00:42
Quote from: Remy Lebeau on March 13, 2019, 12:24:27 amQuote from:
440bx on March 12, 2019, 10:27:27 pmThe thing is, the function is not
a method, and is not nested. It's just a plain vanilla function.
...
There must be something in the code that is confusing FPC. It will be
interesting to find out what it is.
Kind of hard to judge that when you did not show the full code.
I know but, the unit is almost 6000 lines and in the middle of being
edited for porting.
I should have probably mentioned that I figured out what the problem
was. Kind of hard to explain how thing that got there but, in the
midst of conditional directives and definitions, there was an
unmatched brace. It didn't bother Delphi but, it somehow ended up
creating a partial definition for a function and FPC saw the whole
remainder of the program as being part of the function.
What helped me figure it out is that, once I had eliminated all the
other syntactic details FPC didn't like and, it got to the end of the
unit, it complained that it expected a semicolon and not a period
after the "end" (the "end." that marked the unit's end).
The compiler was telling the truth, it was seeing a whole slew of
functions and procedures inside another one and complained that the
function was not properly terminated (with an "end;".)
Remy, thank you for trying to help, I appreciate it.
----------------------------------------------------------------------
Remy Lebeau · 2019-03-13 01:42
Quote from: 440bx on March 13, 2019, 01:42:22 amin the midst of
conditional directives and definitions, there was an unmatched brace.
It didn't bother Delphi
Delphi allows you to take the address of a nested function, such as
for use as an API callback, but it is not safe/recommended to do so
because of the tricks the compiler has to play with the stack frames
to allow nested functions to access local variables of outer frames.
I guess FreePascal is more restrictive about using the address of
nested functions in function pointers.
----------------------------------------------------------------------
440bx · 2019-03-13 03:05
Quote from: Remy Lebeau on March 13, 2019, 02:42:37 amDelphi allows
you to take the address of a nested function, such as for use as an
API callback, but it is not safe/recommended to do so because of the
tricks the compiler has to play with the stack frames to allow nested
functions to access local variables of outer frames. I guess
FreePascal is more restrictive about using the address of nested
functions in function pointers.
What happened is that, because Delphi and FPC don't treat nested
comments the same way (using braces), Delphi was seeing the code as I
meant it to be. For FPC it resulted in a "lone" function header which
gobbled up the entire rest of the unit. I didn't realize that until I
had corrected all the other things that Delphi was ok with but FPC
objected to.
Once the code was syntactically acceptable to FPC then it complained
about expecting a semicolon instead of a period and then it was also
possible to see some warnings about nested comments. The combination
of the error and the warning messages was enough to find where the
culprit misplaced brace was.
This is the second time a misplaced brace gives me a headache. Never
noticed them before because Delphi doesn't do nested comments.
----------------------------------------------------------------------
Thaddy · 2019-03-13 06:41
Quote from: 440bx on March 13, 2019, 04:05:24 am
This is the second time a misplaced brace gives me a headache. Never
noticed them before because Delphi doesn't do nested comments.
And you can do the same in FPC because you can switch them off
(default in $mode delphi)
Some people never read documentation....
https://www.freepascal.org/docs-
html/current/prog/progsu105.html#x113-1140001.3.22 which is rather
annoying especially if it is someone who likes to switch anything
useless or useful anyway.
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
{$modeswitch nestedcomments-}
----------------------------------------------------------------------
440bx · 2019-03-13 06:57
Quote from: Thaddy on March 13, 2019, 07:41:03 amQuote from: 440bx on
March 13, 2019, 04:05:24 am
This is the second time a misplaced brace gives me a headache. Never
noticed them before because Delphi doesn't do nested comments.
And you can do the same in FPC because you can switch them off
(default in $mode delphi)
Some people never read documentation....
one directive I would gladly use is {$thaddy off} but it's not
documented and, I'm pretty sure it wouldn't work anyway.
----------------------------------------------------------------------
Thaddy · 2019-03-13 09:29
Quote from: 440bx on March 13, 2019, 07:57:17 amone directive I would
gladly use is {$thaddy off}
I would be proud of that! When I am dead... Still you'd be better off
to actually read the documentation. Which is excellent. Which you
refuse to do.... Which in turn is completely silly.
The documentation is extremely good.