Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2021-12

Initialising method pointers with class methods

2 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Unknown author

DCC allows the subj (provided that the class type is known at compile time), FPC does not.

The attached init_methptr_with_classmeth.patch implements this feature.

-------8<-------
type C = class
	class procedure Foo;
end;
class procedure C.Foo; begin end;
type CC = class of C;
type H = class helper for C end;
type T = procedure of object;
//var aC: C = nil;
//var aCC: CC = nil;
// Still rejected:
//var ViaInstance: T = aC.Foo;
//var ViaClassRef: T = aCC.Foo;
const ViaClass: T = C.Foo;
// NB: This needs metaclass_meth_to_procvar-2.patch
//	from https://lists.freepascal.org/pipermail/fpc-devel/2021-December/044249.html
//	Otherwise: AV in FPC
var ViaMetaclass: T = CC.Foo;
// TODO: Currently, ICE 2021122302 -- needs to be fixed elsewhere.
//	See https://lists.freepascal.org/pipermail/fpc-devel/2021-December/044251.html
//var ViaHelper: T = H.Foo;
procedure Report(const s: string; const X: T);
var Status: Boolean;
begin
	Status := (TMethod(X).Code = @C.Foo) and (TMethod(X).Data = Pointer(C));
	writeln(s, ': ', Status)
end;
begin
	Report('via class', ViaClass);
	Report('via metaclass', ViaMetaclass)
end.
-------8<-------

Proposed new error message for parser_e_no_procvarobj_const: Cannot initialize a method pointer: Self pointer is not known at compile time In order to initialize a method pointer with a method, the value of the Self pointer for calling that method at run time must be known at compile time. Thus, a method pointer can be initialized either with NIL, or with a class method that is accessed via a class type or a class reference type.

βþ
Sven Barth
to Unknown author
Blaise--- wrote 53 lines
DCC allows the subj (provided that the class type is known at compile time), FPC does not. The attached init_methptr_with_classmeth.patch implements this feature. -------8<------- type C = class     class procedure Foo; end; class procedure C.Foo; begin end; type CC = class of C; type H = class helper for C end; type T = procedure of object; //var aC: C = nil; //var aCC: CC = nil; // Still rejected: //var ViaInstance: T = aC.Foo; //var ViaClassRef: T = aCC.Foo; const ViaClass: T = C.Foo; // NB: This needs metaclass_meth_to_procvar-2.patch //    from https://lists.freepascal.org/pipermail/fpc-devel/2021-December/044249.html //    Otherwise: AV in FPC var ViaMetaclass: T = CC.Foo; // TODO: Currently, ICE 2021122302 -- needs to be fixed elsewhere. //    See https://lists.freepascal.org/pipermail/fpc-devel/2021-December/044251.html //var ViaHelper: T = H.Foo; procedure Report(const s: string; const X: T); var Status: Boolean; begin     Status := (TMethod(X).Code = @C.Foo) and (TMethod(X).Data = Pointer(C));     writeln(s, ': ', Status) end; begin     Report('via class', ViaClass);     Report('via metaclass', ViaMetaclass) end. -------8<------- Proposed new error message for parser_e_no_procvarobj_const: Cannot initialize a method pointer: Self pointer is not known at compile time     In order to initialize a method pointer with a method, the value of the Self pointer for calling that method at run time must be known at compile time. Thus, a method pointer can be initialized either with NIL, or with a class method that is accessed via a class type or a class reference type.

Accepted.

Regards,
Sven