Author Topic: Function Pointer Example 1 Broken  (Read 1909 times)

Robert

  • Hero Member
  • *****
  • Posts: 1152
    • View Profile
Function Pointer Example 1 Broken
« on: January 24, 2020, 03:41:43 PM »
Code: [Select]

GLOBAL Func(INTEGER) AS FUNCTION INTEGER

CALL SetFunc(x0)
? Func(2)
CALL SetFunc(x1)
? Func(2)

SUB SetFunc(f(n AS INTEGER) AS FUNCTION INTEGER)
  Func = f
END SUB

'-----------------------------------------------

FUNCTION x0(x AS INTEGER)
  FUNCTION = x * x
END FUNCTION

FUNCTION x1(x AS INTEGER)
  FUNCTION = x * x * 2
END FUNCTION


Code: [Select]
SUB SetFunc(f(n AS INTEGER) AS FUNCTION INTEGER)

is being translated in the prototypes section as

Code: [Select]
void    SetFunc ((*) (intfunctionint));

instead of the correct

Code: [Select]
void    SetFunc (int(*)(int));

and in the User Subs section as

Code: [Select]
void SetFunc ((*(void) (int (functionint AS))

instead of the correct

Code: [Select]
void SetFunc (int (*f)(int n))

Robert

  • Hero Member
  • *****
  • Posts: 1152
    • View Profile
Re: Function Pointer Example 1 Broken
« Reply #1 on: January 24, 2020, 03:49:54 PM »
It was broken in BCX 6.5.0.

BCX 6.2.4. translates it O.K.

MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 1932
    • View Profile
Re: Function Pointer Example 1 Broken
« Reply #2 on: January 24, 2020, 06:41:58 PM »
Robert,

No fix needed, IMO ... it's just that the implementation changed a bit.


Code: [Select]

GLOBAL Func(DUMMY as INTEGER) AS FUNCTION INTEGER

SetFunc(x0)
? Func(2)

SetFunc(x1)
? Func(2)


SUB SetFunc (f AS Func_TYPE)   ' <<-- BCX created Func_TYPE for you.
  Func = f
END SUB

'-----------------------------------------------

FUNCTION x0(x AS INTEGER) AS INTEGER
  FUNCTION = x * x
END FUNCTION


FUNCTION x1(x AS INTEGER) AS INTEGER
  FUNCTION = x * x * 2
END FUNCTION


Robert

  • Hero Member
  • *****
  • Posts: 1152
    • View Profile
Re: Function Pointer Example 1 Broken
« Reply #3 on: January 24, 2020, 07:26:37 PM »
Thanks for that.

I have no idea where to begin with the warnings and errors in the one below, again from the Help file.

Code: [Select]

SET Funcs[] AS FARPROC
  x0,
  x1,
  x2,
  x3,
  x4
END SET

FUNCTION x0
  FUNCTION = 0
END FUNCTION

FUNCTION x1
  FUNCTION = 1
END FUNCTION

FUNCTION x2
  FUNCTION = 2
END FUNCTION

FUNCTION x3
  FUNCTION = 3
END FUNCTION

FUNCTION x4
  FUNCTION = 4
END FUNCTION

SET(PTR Funcs2[])() AS CHAR PTR
  s0$,
  s1$,
  s2$,
  s3$,
  s4$
END SET

FUNCTION s0$
  FUNCTION = "0"
END FUNCTION

FUNCTION s1$
  FUNCTION = "1"
END FUNCTION

FUNCTION s2$
  FUNCTION = "2"
END FUNCTION

FUNCTION s3$
  FUNCTION = "3"
END FUNCTION

FUNCTION s4$
  FUNCTION = "4"
END FUNCTION

SET(PTR Funcs3[])(INTEGER) AS INTEGER
  n0,
  n1,
  n2,
  n3,
  n4
END SET

FUNCTION n0(n AS INTEGER)
  FUNCTION = n * 0
END FUNCTION

FUNCTION n1(n AS INTEGER)
  FUNCTION = n * 1
END FUNCTION

FUNCTION n2(n AS INTEGER)
  FUNCTION = n * 2
END FUNCTION

FUNCTION n3(n AS INTEGER)
  FUNCTION = n * 3
END FUNCTION

FUNCTION n4(n AS INTEGER)
  FUNCTION = n * 4
END FUNCTION
DIM i

FOR i = 0 TO 4
  ? Funcs[i]()
NEXT

FOR i = 0 TO 4
  ? Funcs2$[i]()
NEXT

FOR i = 0 TO 4
  ? Funcs3[i](i)
NEXT



MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 1932
    • View Profile
Re: Function Pointer Example 1 Broken
« Reply #4 on: January 24, 2020, 09:15:04 PM »
Robert -- I tackled them one at a time.   

They are less confusing and more instructive as 3 individual examples.

Here they are ... tested with LccWin32, Pelles 9, and Mingw

Code: [Select]

 SET Funcs[] AS FARPROC
  x0,
  x1,
  x2,
  x3,
  x4
 END SET

 FUNCTION x0
  FUNCTION = 0
 END FUNCTION

 FUNCTION x1
  FUNCTION = 1
 END FUNCTION

 FUNCTION x2
  FUNCTION = 2
 END FUNCTION

 FUNCTION x3
  FUNCTION = 3
 END FUNCTION

 FUNCTION x4
  FUNCTION = 4
 END FUNCTION

'------------------------

 DIM i

 FOR i = 0 TO 4
  ? Funcs[i]()
 NEXT




Code: [Select]

SET Funcs2[] AS FARPROC
  s0$,
  s1$,
  s2$,
  s3$,
  s4$
END SET

FUNCTION s0$
  FUNCTION = "0"
END FUNCTION

FUNCTION s1$
  FUNCTION = "1"
END FUNCTION

FUNCTION s2$
  FUNCTION = "2"
END FUNCTION

FUNCTION s3$
  FUNCTION = "3"
END FUNCTION

FUNCTION s4$
  FUNCTION = "4"
END FUNCTION

'-----------------------

DIM i

FOR i = 0 TO 4
  ? Funcs2$[i]()
NEXT



Code: [Select]

GLOBAL PF_1_ARG(DUMMY as INTEGER) AS FUNCTION INTEGER


SET Funcs3[] AS PF_1_ARG_TYPE
  n0,
  n1,
  n2,
  n3,
  n4
END SET

FUNCTION n0(n AS INTEGER)
  FUNCTION = n * 0
END FUNCTION

FUNCTION n1(n AS INTEGER)
  FUNCTION = n * 1
END FUNCTION

FUNCTION n2(n AS INTEGER)
  FUNCTION = n * 2
END FUNCTION

FUNCTION n3(n AS INTEGER)
  FUNCTION = n * 3
END FUNCTION

FUNCTION n4(n AS INTEGER)
  FUNCTION = n * 4
END FUNCTION

'------------------------------

DIM i

FOR i = 0 TO 4
  ? Funcs3[i](i)
NEXT



MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 1932
    • View Profile
Re: Function Pointer Example 1 Broken
« Reply #5 on: April 01, 2021, 02:40:15 PM »
Here are a couple of updated versions of the examples above.

The examples above appear in BCXHelp and should be updated as well.

These compile cleanly with all tested compilers. (Lcc, Pelles, MSVC, GCC, Clang)

Basically, this update replaces the FARPROC definition with the custom typedef that BCX emits.

Code: [Select]
GLOBAL PTR_Funcs () AS FUNCTION INTEGER

SET Funcs[] AS PTR_Funcs_TYPE   ' BCX creates a typedef and tacks on "_TYPE" to the declaration
  x0,
  x1,
  x2,
  x3,
  x4
 END SET

 FUNCTION x0
  FUNCTION = 0
 END FUNCTION

 FUNCTION x1
  FUNCTION = 1
 END FUNCTION

 FUNCTION x2
  FUNCTION = 2
 END FUNCTION

 FUNCTION x3
  FUNCTION = 3
 END FUNCTION

 FUNCTION x4
  FUNCTION = 4
 END FUNCTION

'------------------------

 DIM i

 FOR i = 0 TO 4
  ? Funcs[i]()
 NEXT

and

Code: [Select]
GLOBAL PTR_Funcs () AS FUNCTION PCHAR
 
SET Funcs2[] AS PTR_Funcs_TYPE   ' BCX creates a typedef and tacks on "_TYPE" to the declaration
  s0$,
  s1$,
  s2$,
  s3$,
  s4$
END SET


FUNCTION s0$
  FUNCTION = "0"
END FUNCTION


FUNCTION s1$
  FUNCTION = "1"
END FUNCTION


FUNCTION s2$
  FUNCTION = "2"
END FUNCTION


FUNCTION s3$
  FUNCTION = "3"
END FUNCTION


FUNCTION s4$
  FUNCTION = "4"
END FUNCTION


'-----------------------

DIM i

FOR i = 0 TO 4
  ? Funcs2$[i]()
NEXT

'-----------------------



Robert

  • Hero Member
  • *****
  • Posts: 1152
    • View Profile
Re: Function Pointer Example 1 Broken
« Reply #6 on: April 01, 2021, 03:16:52 PM »
The examples in the BCX Help file shall be updated.