Recent

Author Topic: Can a normal function/procedure be aliased ?  (Read 3581 times)

440bx

  • Hero Member
  • *****
  • Posts: 3946
Can a normal function/procedure be aliased ?
« on: May 06, 2019, 07:49:04 pm »
Hello,

It's probably easier to understand what I'd like using an example than trying to explain it.   

Given a function like:
Code: Pascal  [Select][+][-]
  1. function Abc(parameter : parametertype) : resulttype;
  2. var
  3.   .. some variable declarations here
  4. begin
  5.    some statements here
  6. end;

What I'd like is to be able to refer to function Abc as function Xyz as well.

Note: I cannot use "inline" to simulate an alias because these are callback functions being passed to various Windows APIs.  IOW, the pointer to Xyz must equal the pointer to Abc and, pointers to both must be obtainable.

Thank you for your help.



(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Thausand

  • Sr. Member
  • ****
  • Posts: 292
Re: Can a normal function/procedure be aliased ?
« Reply #1 on: May 06, 2019, 07:57:07 pm »
Hi hello 440bx,

IOW, the pointer to Xyz must equal the pointer to Abc and, pointers to both must be obtainable.
Not sure me understand rest you write but answer quote is have keyword "absolute" and make do.

add example:

make show other solution to  :).

Code: Pascal  [Select][+][-]
  1. program alias_proc;
  2.  
  3. {$MODE OBJFPC}{$H+}
  4.  
  5. type
  6.   TTestProc = Procedure;
  7.  
  8. procedure test;
  9. begin
  10.   WriteLn('test');
  11. end;
  12.  
  13. var
  14.   IsOne : TTestProc = @test;
  15.   isTwo : TTestProc = @test;
  16.   isThree : TTestProc absolute IsOne;
  17.  
  18. begin
  19.   IsOne();
  20.   IsTwo();
  21.   IsThree();
  22. end.
  23.  

Make sure i write i not know if understand what write before quote so maybe example wrong. Then i sorry.
« Last Edit: May 06, 2019, 08:15:51 pm by Thausand »

furious programming

  • Hero Member
  • *****
  • Posts: 853
Re: Can a normal function/procedure be aliased ?
« Reply #2 on: May 06, 2019, 08:12:20 pm »
@440bx: to be able to pass different functions with the same parameters list and the type of the result, you can declare a procedural type and use it to declare a parameter in the function import. Then you can declare any number of your own functions and use their pointers in calling the imported one.

But real function alias is a constant with pointer to another function. Example:

Code: Pascal  [Select][+][-]
  1. const
  2.   MyLowerCase: function(const AValue: String): String = @System.LowerCase;

understand rest you write but answer quote is have keyword "absolute" and make do.

The absolute keyword can be used only to declare variable. So if you want to declare function in this way, you need to get the procedural variable or constant and declare your own that will point to the original one. Something like this:

Code: Pascal  [Select][+][-]
  1. type
  2.   TLowerCaseFunc = function(const AValue: String): String;
  3.  
  4. var
  5.   MyLowerCase: TLowerCaseFunc;
  6.   MyAnotherLowerCase: TLowerCaseFunc absolute MyLowerCase;

Or like this:

Code: Pascal  [Select][+][-]
  1. type
  2.   TLowerCaseFunc = function(const AValue: String): String;
  3.  
  4. const
  5.   MyLowerCase: TLowerCaseFunc = @System.LowerCase;
  6.  
  7. var
  8.   MyAnotherLowerCase: TLowerCaseFunc absolute MyLowerCase;
« Last Edit: May 06, 2019, 08:18:19 pm by furious programming »
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

Thausand

  • Sr. Member
  • ****
  • Posts: 292
Re: Can a normal function/procedure be aliased ?
« Reply #3 on: May 06, 2019, 08:21:33 pm »
hi hello furious programming,

The absolute keyword can be used only to declare variable. So if you want to declare function in this way, you need to get the procedural variable or constant and declare your own that will point to the original one. Something like this:
Yes that correct. I add example later for see.

Many thanksy for write example my explain.

I make sure have quote 440bx is write pointers make point same location. Then have two var point same location use absolute like example you write :)
« Last Edit: May 06, 2019, 08:23:24 pm by Thausand »

furious programming

  • Hero Member
  • *****
  • Posts: 853
Re: Can a normal function/procedure be aliased ?
« Reply #4 on: May 06, 2019, 08:31:21 pm »
I think that an alias is simply an another name for the same thing. If so, my first example will be appropriate. But it is a pity, however, that type inference is not supported in such cases and the following declaration is incorrect and can not be compiled:

Code: Pascal  [Select][+][-]
  1. const
  2.   MyLowerCase = @System.LowerCase;  // Error: Illegal expression
« Last Edit: May 06, 2019, 08:35:44 pm by furious programming »
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

440bx

  • Hero Member
  • *****
  • Posts: 3946
Re: Can a normal function/procedure be aliased ?
« Reply #5 on: May 06, 2019, 08:54:54 pm »
@Thausand

Not sure me understand rest you write but answer quote is have keyword "absolute" and make do.

No problem.  I'm not sure I understood you either but, no matter, I still appreciate your help.

@furious programming

@440bx: to be able to pass different functions with the same parameters list and the type of the result, you can declare a procedural type and use it to declare a parameter in the function import.
Yes, using a procedure variable would definitely be a way to create an alias for a function/procedure.

I wasn't clear that I was hoping for a solution that didn't require declaring a procedural variable.  My failure.

I was hoping there was something like the "alias modifier" which is used to assign additional names/synonyms to functions/procedures.  Unfortunately, the "alias modifier" cannot be used to create internal synonyms.

Very likely, I will end up using your procedural variables suggestion.

I appreciate your help, thank you.
 
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Can a normal function/procedure be aliased ?
« Reply #6 on: May 06, 2019, 08:59:03 pm »
If you want to define aliases, use the "alias" modifier; the following defines fopendir, exports it as FPC_SYSC_OPENDIR and reimports it as fpopendir2


Code: Pascal  [Select][+][-]
  1. function Fpopendir(dirname : pchar): pdir;  [public, alias : 'FPC_SYSC_OPENDIR'];
  2. begin
  3.  result:=nil;
  4. end;
  5. Function  FpOpendir2    (dirname : pChar): pDir;  external name 'FPC_SYSC_OPENDIR';
  6.  

The *nix RTL uses this a lot to import functions from the implementation of units.

Thausand

  • Sr. Member
  • ****
  • Posts: 292
Re: Can a normal function/procedure be aliased ?
« Reply #7 on: May 06, 2019, 09:02:08 pm »
...
But it is a pity, however, that type inference is not supported in such cases and the following declaration is incorrect and can not be compiled:
Is pitty but i not know if make better language Pascal if have. Time is telling ? :)

I have good read here http://www.deltics.co.nz/blog/posts/244

440bx

  • Hero Member
  • *****
  • Posts: 3946
Re: Can a normal function/procedure be aliased ?
« Reply #8 on: May 06, 2019, 09:05:10 pm »
If you want to define aliases, use the "alias" modifier; the following defines fopendir, exports it as FPC_SYSC_OPENDIR and reimports it as fpopendir2
Reimporting it... that's a good idea, I like it.

Thank you.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Can a normal function/procedure be aliased ?
« Reply #9 on: May 06, 2019, 09:06:05 pm »
Slight discretion advised, do not overuse. You pollute the global namespace

440bx

  • Hero Member
  • *****
  • Posts: 3946
Re: Can a normal function/procedure be aliased ?
« Reply #10 on: May 06, 2019, 09:15:22 pm »
Slight discretion advised, do not overuse. You pollute the global namespace
Yes, definitely.  Not to mention that, likely, both the Import and Export tables of the executable get larger and larger.

Nice to have the option though.  It could come in very handy in some cases, particularly in DLLs.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

creaothceann

  • Full Member
  • ***
  • Posts: 117
Re: Can a normal function/procedure be aliased ?
« Reply #11 on: May 06, 2019, 11:51:42 pm »
Alternatively you could use a macro.

440bx

  • Hero Member
  • *****
  • Posts: 3946
Re: Can a normal function/procedure be aliased ?
« Reply #12 on: May 07, 2019, 12:11:04 am »
Alternatively you could use a macro.
That's a good idea too.  I have a tendency to forget that FPC does have a simple macro facility.  It has the advantage of not cluttering the exe's import and export tables with potentially misleading data.

Thank you.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

JernejL

  • Jr. Member
  • **
  • Posts: 92
Re: Can a normal function/procedure be aliased ?
« Reply #13 on: May 07, 2019, 07:50:09 am »
I suppose you could also declare 2nd function that just passes 1:1 parameters and add keyword inline, if inlining works optimally, it would have zero overhead.
 

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Can a normal function/procedure be aliased ?
« Reply #14 on: May 07, 2019, 09:26:55 am »
Slight discretion advised, do not overuse. You pollute the global namespace
Yes, definitely.  Not to mention that, likely, both the Import and Export tables of the executable get larger and larger.
If it isn't imported from a library why would it be in the Import and Export tables?
Only those with export modifier or in the exports section are put into the Export table and only those with external <LIBRARY> or external <LIBRARY> name <FUNC> are part of the Import table (and the suggestion by marcov is external name <FUNC> which has a small, but in this case significant different meaning due to the missing library name).

 

TinyPortal © 2005-2018