"Fossies" - the Fresh Open Source Software Archive

Member "FreeBASIC-1.08.1-source/tests/cpp/derived-fbc.bas" (6 Jul 2021, 7488 Bytes) of package /linux/privat/FreeBASIC-1.08.1-source.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Visual Basic source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 ' TEST_MODE : MULTI_MODULE_TEST
    2 
    3 '' test mapping of mangling between c++ derived class and fbc type
    4 
    5 #inclib "stdc++"
    6 
    7 '' helper macro to track progress
    8 #define DLOG( msg ) '' print #msg
    9 
   10 #if ENABLE_CHECK_BUGS
   11     #define DOTEST
   12     #define DOFUNCS
   13 #else
   14     '' thiscall is not supported in -gen gas
   15     #if __FB_BACKEND__ <> "gas"
   16         #define DOTEST
   17     #endif
   18 
   19     '' structures returned by value from c++
   20     '' needs some work on arm targets (bugs!)
   21     #if not defined( __FB_ARM__ )
   22         #define DOFUNCS
   23     #endif
   24 #endif
   25 
   26 '' !!! TODO !!! this default should be handled in fbc
   27 #if defined(__FB_WIN32__) and not defined(__FB_64BIT__)
   28     #define thiscall __thiscall
   29 #else
   30     #define thiscall
   31 #endif
   32 
   33 #ifndef NULL
   34 #define NULL 0
   35 #endif
   36 
   37 extern "c++"
   38     '' getters to retrieve call information
   39     '' from the c++ side
   40     declare sub setInitial( byval flag as long )
   41     declare sub resetChecks()
   42     declare function getPtr1() as any ptr
   43     declare function getPtr2() as any ptr
   44     declare function getMsg1() as zstring ptr
   45     declare function getVal1() as long
   46     declare function getVal2() as long
   47     declare function getVal3() as long
   48 end extern
   49 
   50 extern "c++"
   51     '' BASE UDT to test with and we declare the same on c++ side
   52     type UDT_BASE extends OBJECT
   53         value as long
   54 
   55         declare constructor thiscall ()
   56         declare constructor thiscall ( byref rhs as const UDT_BASE )
   57         declare constructor thiscall ( byref rhs as const long )
   58         declare virtual destructor thiscall ()
   59         declare virtual sub method thiscall ( byref rhs as const long ) 
   60         declare operator let thiscall ( byref rhs as const UDT_BASE )
   61 
   62     end type
   63 
   64     '' DERIVED UDT to test with and we declare the same on c++ side
   65     type UDT_DERIVED extends UDT_BASE
   66         declare constructor thiscall ()
   67         declare constructor thiscall ( byref rhs as const UDT_DERIVED )
   68         declare constructor thiscall ( byref rhs as const long )
   69         declare virtual destructor thiscall ()
   70         declare virtual sub method thiscall ( byref rhs as const long )
   71         declare operator let thiscall ( byref rhs as const UDT_DERIVED )
   72 
   73     end type
   74 
   75     '' global operators
   76     declare operator +( byref lhs as const UDT_BASE, byref rhs as const UDT_BASE ) as UDT_BASE
   77     declare operator -( byref lhs as const UDT_BASE, byref rhs as const UDT_BASE ) as UDT_BASE
   78     declare operator +( byref lhs as const long, byref rhs as const UDT_BASE ) as UDT_BASE
   79     declare operator -( byref lhs as const long, byref rhs as const UDT_BASE ) as UDT_BASE
   80 
   81     '' global operators
   82     declare operator +( byref lhs as const UDT_DERIVED, byref rhs as const UDT_DERIVED ) as UDT_DERIVED
   83     declare operator -( byref lhs as const UDT_DERIVED, byref rhs as const UDT_DERIVED ) as UDT_DERIVED
   84     declare operator +( byref lhs as const long, byref rhs as const UDT_DERIVED ) as UDT_DERIVED
   85     declare operator -( byref lhs as const long, byref rhs as const UDT_DERIVED ) as UDT_DERIVED
   86 
   87 end extern
   88 
   89 #macro checkResults( p1, p2, v1, v2, v3, m1 )
   90     assert( m1 = *getMsg1() )
   91     assert( p1 = getPtr1() )
   92     assert( p2 = getPtr2() )
   93     assert( v1 = getVal1() )
   94     assert( v2 = getVal2() )
   95     assert( v3 = getVal3() )
   96 #endmacro
   97 
   98 #ifdef DOTEST
   99 
  100 '' enable results for ctor/dtor/copy-ctor/let
  101 setInitial( 1 )
  102 
  103 DLOG( constructor UDT_BASE() )
  104 scope
  105     resetChecks()
  106     dim a as UDT_BASE
  107     checkResults( @a, NULL, -1, -1, 0, "UDT_BASE::UDT_BASE()" )
  108     assert( a.value = 0 )
  109 end scope
  110 
  111 DLOG( destructor UDT_BASE() )
  112 scope
  113     dim p as any ptr
  114     scope
  115         resetChecks()
  116         dim a as UDT_BASE
  117         p = @a
  118         checkResults( @a, NULL, -1, -1, 0, "UDT_BASE::UDT_BASE()" )
  119         assert( a.value = 0 )
  120     end scope
  121     checkResults( p, NULL, 0, -1, -1, "UDT_BASE::~UDT_BASE()" )
  122 end scope
  123 
  124 DLOG( constructor UDT_BASE( byref rhs as const long ) )
  125 scope
  126     dim a as long = 1
  127     resetChecks()
  128     dim b as UDT_BASE = a
  129     checkResults( @b, @a, -1, 1, 1, "UDT_BASE::UDT_BASE( int const& rhs )" )
  130     assert( b.value = 1 )
  131 end scope
  132 
  133 DLOG( constructor UDT_BASE( byref rhs as const UDT ) )
  134 scope
  135     dim a as UDT_BASE = 2
  136     resetChecks()
  137     dim b as UDT_BASE = a
  138     checkResults( @b, @a, -1, 2, 2, "UDT_BASE::UDT_BASE( UDT_BASE const& rhs )" )
  139     assert( b.value = 2 )
  140 end scope
  141 
  142 DLOG( sub UDT_BASE.method( byref rhs as const long ) )
  143 scope
  144     dim a1 as long = 1, a2 as long = 2
  145     resetChecks()
  146     dim b as UDT_BASE = a1
  147     checkResults( @b, @a1, -1, 1, 1, "UDT_BASE::UDT_BASE( int const& rhs )" )
  148     b.method( a2 )
  149     checkResults( @b, @a2,  1, 2, 2, "void UDT_BASE::method( int const& rhs )" )
  150     assert( b.value = a2 )
  151 end scope
  152 
  153 DLOG( operator UDT_BASE.let( byref rhs as const UDT ) )
  154 scope
  155     dim a as UDT_BASE = 2
  156     dim b as UDT_BASE
  157     resetChecks()
  158     b = a
  159     checkResults( @b, @a, 0, 2, 2, "UDT_BASE& UDT_BASE::operator=( UDT_BASE const& rhs )" )
  160     assert( b.value = 2 )
  161 end scope
  162 
  163 DLOG( constructor UDT_DERIVED() )
  164 scope
  165     resetChecks()
  166     dim a as UDT_DERIVED
  167     checkResults( @a, NULL, -1, -1, 0, "UDT_DERIVED::UDT_DERIVED()" )
  168     assert( a.value = 0 )
  169 end scope
  170 
  171 DLOG( destructor UDT_DERIVED() )
  172 scope
  173     dim p as any ptr
  174     scope
  175         resetChecks()
  176         dim a as UDT_DERIVED
  177         p = @a
  178         checkResults( @a, NULL, -1, -1, 0, "UDT_DERIVED::UDT_DERIVED()" )
  179         assert( a.value = 0 )
  180     end scope
  181     checkResults( p, NULL, 0, -1, -1, "UDT_BASE::~UDT_BASE()" )
  182 end scope
  183 
  184 DLOG( constructor UDT_DERIVED( byref rhs as const long ) )
  185 scope
  186     dim a as long = 1
  187     resetChecks()
  188     dim b as UDT_DERIVED = a
  189     checkResults( @b, @a, -1, 1, 1, "UDT_DERIVED::UDT_DERIVED( int const& rhs )" )
  190     assert( b.value = 1 )
  191 end scope
  192 
  193 DLOG( constructor UDT_DERIVED( byref rhs as const UDT ) )
  194 scope
  195     dim a as UDT_DERIVED = 2
  196     resetChecks()
  197     dim b as UDT_DERIVED = a
  198     checkResults( @b, @a, -1, 2, 2, "UDT_DERIVED::UDT_DERIVED( UDT_DERIVED const& rhs )" )
  199     assert( b.value = 2 )
  200 end scope
  201 
  202 DLOG( sub UDT_DERIVED.method( byref rhs as const long ) )
  203 scope
  204     dim a1 as long = 3, a2 as long = 4
  205     resetChecks()
  206     dim b as UDT_DERIVED = a1
  207     checkResults( @b, @a1, -1, 3, 3, "UDT_DERIVED::UDT_DERIVED( int const& rhs )" )
  208     b.method( a2 )
  209     checkResults( @b, @a2,  3, 4, 4, "void UDT_DERIVED::method( int const& rhs )" )
  210     assert( b.value = a2 )
  211 end scope
  212 
  213 DLOG( operator UDT_DERIVED.let( byref rhs as const UDT ) )
  214 scope
  215     dim a as UDT_DERIVED = 2
  216     dim b as UDT_DERIVED
  217     resetChecks()
  218     b = a
  219     checkResults( @b, @a, 0, 2, 2, "UDT_DERIVED& UDT_DERIVED::operator=( UDT_DERIVED const& rhs )" )
  220     assert( b.value = 2 )
  221 end scope
  222 
  223 '' disable results for ctor/dtor/copy-ctor/let
  224 setInitial( 0 )
  225 
  226 #ifdef DOFUNCS
  227 DLOG( operator +( byref lhs as const UDT_BASE, byref rhs as const UDT_BASE ) as UDT_BASE )
  228 scope
  229     dim as UDT_BASE a = 3
  230     dim as UDT_BASE b = 11
  231     dim as UDT_BASE c
  232     resetChecks()
  233     c = (a + b)
  234     checkResults( @a, @b, 3, 11, 14, "UDT_BASE operator+( UDT_BASE const& lhs, UDT_BASE const& rhs )" )
  235 end scope
  236 
  237 DLOG( operator -( byref lhs as const UDT_BASE, byref rhs as const UDT_BASE ) as UDT_BASE )
  238 scope
  239     dim as UDT_BASE a = 17
  240     dim as UDT_BASE b = 5
  241     dim as UDT_BASE c
  242     resetChecks()
  243     c = a - b
  244     checkResults( @a, @b, 17, 5, 12, "UDT_BASE operator-( UDT_BASE const& lhs, UDT_BASE const& rhs )" )
  245 end scope
  246 
  247 DLOG( operator +( byref lhs as const long, byref rhs as const UDT_DERIVED ) as UDT_DERIVED )
  248 scope
  249     dim as long a = 3
  250     dim as UDT_DERIVED b = 11
  251     dim as UDT_DERIVED c
  252     resetChecks()
  253     c = a + b
  254     checkResults( @a, @b, 3, 11, 14, "UDT_DERIVED operator+( int const& lhs, UDT_DERIVED const& rhs )" )
  255 end scope
  256 
  257 DLOG( operator -( byref lhs as const long, byref rhs as const UDT_DERIVED ) as UDT_DERIVED )
  258 scope
  259     dim as long a = 17
  260     dim as UDT_DERIVED b = 5
  261     dim as UDT_DERIVED c
  262     resetChecks()
  263     c = a - b
  264     checkResults( @a, @b, 17, 5, 12, "UDT_DERIVED operator-( int const& lhs, UDT_DERIVED const& rhs )" )
  265 end scope
  266 #endif
  267 
  268 #endif '' DOTEST