[fpc-pascal] Inline methods

LacaK lacak at zoznam.sk
Thu May 19 08:52:40 CEST 2016


>
> >> Additionally, it's possible that it's not possible to inline 
> generic routines in regular routines,
> >
> > It is not my case.
> > I use this generic in another generic and then it is specialized in 
> interface section of same unit.
> >
> > Later this specialized object is used in another class type, which 
> comes after specialization.
> > So this object is not used in regular procedure, but in methods of 
> another class ...
> >
> > I did also attempt to put all this stuff (with generics and 
> specializations) into separate unit and then I have used this unit ( 
> in interface section ) in "main" unit
> > But still does not inline ...
>
> You need to call the function in another unit than where the 
> specialization is.
>
Yes I do it so (in attached example it is in one unit, but in my real 
generic and specialization is in separate unit, that is used by another 
in interface section)

> Maybe provide a small (compileable) example that shows what you're 
> doing so that we can check that ourselves.
>
Attached

> It could also be that 2.6.x had still problems there...
>
I tried also with FPC 3.0, but result is same AFAICS

Thanks
-Laco.

-------------- next part --------------
program test_inline;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes
  { you can add units after this };

{$INLINE ON}

type

  { GT }

  generic GT<T> = object
    private
      type
        TElement = T;
        PElement = ^T;
      var
        FArray: array of T;
    public
      constructor Init;
      function Data: PElement; inline;
  end;

  T1 = specialize GT<byte>;

  { T2 }

  T2 = class
    private
      Ft1: T1;
    public
      constructor Create;
      procedure Test;
  end;

{ GT }

constructor GT.Init;
begin
  SetLength(FArray,10);
end;

function GT.Data: PElement; inline;
begin
  Result := @FArray[0];
end;

{ T2 }

constructor T2.Create;
begin
  Ft1.Init;
end;

procedure T2.Test;
var p: T1.PElement;
begin
  p := Ft1.Data; // here is CALL
end;

var
  a: T2;

begin
  a := T2.Create;
  a.Test;
  a.Free;
end.



More information about the fpc-pascal mailing list