Skip to content

Conversation

@jayrm
Copy link
Member

@jayrm jayrm commented Apr 30, 2021

Implicitly emit the deleting destructor for extern "c++" mangling for better g++ ABI compatibility

In an extern "c++" block, fbc will allocate an entry in the virtual table (if it has one) for the deleting destructor:
Example:

extern "c++"
type T
    __ as integer
    declare constructor
    declare destructor
end type
end extern

When user defines a complete destructor (typical in fbc), the deleting destructor will also be implicitly generated:

'' pseudo code for illustration only
destructor T alias "T.D0" ()
if( @this <> NULL ) then
delete this
end if
end destructor

Where the delete operator will first invoke the complete destructor (~T::T()) followed by deallocating the memory.

Some simple tests have been added to check the usage of passing an fbc declared type to a g++ implemented class, however the tests are quite simple and do not fully explore virtual tables and inheritance. More testing is needed. However, the change should not adversely effect non extern "c++" source codes.

Note: without support for thiscall calling convention, this is not expected to work on 32-bit windows where the default calling convention is thiscall for c++ classes by default.

References:
sf.net: #923 Incompatibility in ABI destructor with GCC C++
fb.net: C++ ABI Compatible?

Background:
Previously, fbc did not add the deleting destructor to the type's virtual table making it difficult to use (some) g++ compiled code directly.

jayrm added 4 commits April 30, 2021 07:53
- internal changes
- rename FB_PROCATTRIB_DESTRUCTOR to FB_PROCATTRIB_DESTRUCTOR1 for the complete destructor
- add FB_PROCATTRIB_DESTRUCTOR0 for the deleting destructor
- add mangling for complete dtor (D1) and deleting dtor (D0)
- internal changes
- FB_STRUCTEXT - rename dtor to dtor1 and add dtor0
- SYMBDEFAULTMEMBERS - rename dtor to dtor1 and add dtor0
- symbSetCompDtor() - rename to symbSetCompDtor1() and add symbSetCompDtor0()
- symbGetCompDtor() - rename to symbGetCompDtor1() and add symbGetCompDtor0()
- emit the deleting destructor only in c++ mangling
- if type is in an extern "c++" block, implicitly generate and emit the deleting destructor (D0)
@jayrm jayrm merged commit 947a9f4 into freebasic:master Apr 30, 2021
@jayrm jayrm deleted the bugfix-923 branch May 1, 2021 04:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant