[fpc-devel] First cppclass test
Sven Barth
svenmauren at vr-web.de
Thu Oct 29 21:49:02 CET 2009
Hello together!
After I sent the first Native NT patches to the bugtracker I found some
time to test the cppclass-feature.
The following class compiles with 2.5.1:
TestClass = cppclass
procedure Test1; external 'cpplib.so';
function Test2: LongInt; external 'cpplib.so';
end;
The 'external' after each method is required as you can't add a
'external' after the class definition (I think I'll have to look at
Jonas' objc branch to see how he solved that...).
Adding a method with a parameter results in an internal error 2103001.
This is in symdef.tprocdef.cplusplusmangledname in the local method
getcppparaname. The reason is the hidden self parameter which isn't
checked for in cplusplusmangledname. I've attached a patch were I added
a check for hidden parameters in the parameter-for-loop. If you think
that patch is ok I'll post it to the bugtracker.
The following program compiles and runs (the result of Test2 is printed
- in my case 42 ^^), but there is a runtime error 217 after FreeClass
(with "after" I mean that a Writeln after FreeClass is executed)
program cpptest1;
{$mode objfpc}{$H+}
type
TestClass = cppclass
procedure Test1; external 'cpplib.so';
function Test2: LongInt; external 'cpplib.so';
procedure Test3(aArg1: LongInt); external 'cpplib.so';
end;
PTestClass = ^TestClass;
function NewClass: PTestClass; cdecl; external 'cpplib.so';
procedure FreeClass(aUser: PTestClass); cdecl; external 'cpplib.so';
var
c: PTestClass;
i: LongInt;
begin
c := NewClass; // internal error
try
i := c^.Test2;
Writeln(i);
finally
FreeClass(c);
end;
end.
The Pointer type is required, because a cppclass does not seem to be
handled as a implicit pointer. When I don't use a pointer type I get an
internal error 200301231 (x86/cpubase.cgsize2subreg) on the line "c :=
NewClass".
As I'd like to have a cppclass behave like a normal class, I'd like to
know where I can setup a cppclass reference as an implicit reference
inside the compiler.
All in all I have the following goals for (my) future work on this:
* make a cppclass an implicit pointer
* use only one "external 'foo'" after the class declaration
Regards,
Sven
PS: when answering, please CC me, because I'm using digest mode :)
-------------- next part --------------
--- fpc/compiler/symdef.pas 2009-10-22 20:30:05.000000000 +0200
+++ fpc-build/compiler/symdef.pas 2009-10-28 21:08:16.000000000 +0100
@@ -3488,6 +3488,9 @@
for i:=0 to paras.count-1 do
begin
hp:=tparavarsym(paras[i]);
+ { we ignore all hidden parameters (like self) }
+ if vo_is_hidden_para in hp.varoptions then
+ continue;
s2:=getcppparaname(hp.vardef);
if hp.varspez in [vs_var,vs_out] then
s2:='R'+s2;
More information about the fpc-devel
mailing list