xref: /AOO41X/main/bridges/source/cpp_uno/gcc3_netbsd_intel/except.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_bridges.hxx"
30 
31 #include <stdio.h>
32 #include <dlfcn.h>
33 #include <cxxabi.h>
34 #include <hash_map>
35 
36 #include <rtl/strbuf.hxx>
37 #include <rtl/ustrbuf.hxx>
38 #include <osl/diagnose.h>
39 #include <osl/mutex.hxx>
40 
41 #include <bridges/cpp_uno/bridge.hxx>
42 #include <typelib/typedescription.hxx>
43 #include <uno/any2.h>
44 
45 #include "share.hxx"
46 
47 #ifndef RTLD_DEFAULT
48 #define RTLD_DEFAULT    ((void *) -2)
49 #endif
50 
51 using namespace ::std;
52 using namespace ::osl;
53 using namespace ::rtl;
54 using namespace ::com::sun::star::uno;
55 using namespace ::__cxxabiv1;
56 
57 
58 namespace CPPU_CURRENT_NAMESPACE
59 {
60 
61 void dummy_can_throw_anything( char const * )
62 {
63 }
64 
65 //==================================================================================================
66 static OUString toUNOname( char const * p ) SAL_THROW( () )
67 {
68 #if OSL_DEBUG_LEVEL > 1
69     char const * start = p;
70 #endif
71 
72     // example: N3com3sun4star4lang24IllegalArgumentExceptionE
73 
74 	OUStringBuffer buf( 64 );
75     OSL_ASSERT( 'N' == *p );
76     ++p; // skip N
77 
78     while ('E' != *p)
79     {
80         // read chars count
81         long n = (*p++ - '0');
82         while ('0' <= *p && '9' >= *p)
83         {
84             n *= 10;
85             n += (*p++ - '0');
86         }
87         buf.appendAscii( p, n );
88         p += n;
89         if ('E' != *p)
90             buf.append( (sal_Unicode)'.' );
91     }
92 
93 #if OSL_DEBUG_LEVEL > 1
94     OUString ret( buf.makeStringAndClear() );
95     OString c_ret( OUStringToOString( ret, RTL_TEXTENCODING_ASCII_US ) );
96     fprintf( stderr, "> toUNOname(): %s => %s\n", start, c_ret.getStr() );
97     return ret;
98 #else
99     return buf.makeStringAndClear();
100 #endif
101 }
102 
103 //==================================================================================================
104 class RTTI
105 {
106     typedef hash_map< OUString, type_info *, OUStringHash > t_rtti_map;
107 
108     Mutex m_mutex;
109 	t_rtti_map m_rttis;
110     t_rtti_map m_generatedRttis;
111 
112     void * m_hApp;
113 
114 public:
115     RTTI() SAL_THROW( () );
116     ~RTTI() SAL_THROW( () );
117 
118     type_info * getRTTI( typelib_CompoundTypeDescription * ) SAL_THROW( () );
119 };
120 //__________________________________________________________________________________________________
121 RTTI::RTTI() SAL_THROW( () )
122     : m_hApp( dlopen( 0 , RTLD_LAZY ) )
123 {
124 }
125 //__________________________________________________________________________________________________
126 RTTI::~RTTI() SAL_THROW( () )
127 {
128     dlclose( m_hApp );
129 }
130 
131 //__________________________________________________________________________________________________
132 type_info * RTTI::getRTTI( typelib_CompoundTypeDescription *pTypeDescr ) SAL_THROW( () )
133 {
134     type_info * rtti;
135 
136     OUString const & unoName = *(OUString const *)&pTypeDescr->aBase.pTypeName;
137 
138     MutexGuard guard( m_mutex );
139     t_rtti_map::const_iterator iFind( m_rttis.find( unoName ) );
140     if (iFind == m_rttis.end())
141     {
142         // RTTI symbol
143         OStringBuffer buf( 64 );
144         buf.append( RTL_CONSTASCII_STRINGPARAM("_ZTIN") );
145         sal_Int32 index = 0;
146         do
147         {
148             OUString token( unoName.getToken( 0, '.', index ) );
149             buf.append( token.getLength() );
150             OString c_token( OUStringToOString( token, RTL_TEXTENCODING_ASCII_US ) );
151             buf.append( c_token );
152         }
153         while (index >= 0);
154         buf.append( 'E' );
155 
156         OString symName( buf.makeStringAndClear() );
157         rtti = (type_info *)dlsym( RTLD_DEFAULT, symName.getStr() );
158 
159         if (rtti)
160         {
161             pair< t_rtti_map::iterator, bool > insertion(
162                 m_rttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
163             OSL_ENSURE( insertion.second, "### inserting new rtti failed?!" );
164         }
165         else
166         {
167             // try to lookup the symbol in the generated rtti map
168             t_rtti_map::const_iterator iFind( m_generatedRttis.find( unoName ) );
169             if (iFind == m_generatedRttis.end())
170             {
171                 // we must generate it !
172                 // symbol and rtti-name is nearly identical,
173                 // the symbol is prefixed with _ZTI
174                 char const * rttiName = symName.getStr() +4;
175 #if OSL_DEBUG_LEVEL > 1
176                 fprintf( stderr,"generated rtti for %s\n", rttiName );
177 #endif
178                 if (pTypeDescr->pBaseTypeDescription)
179                 {
180                     // ensure availability of base
181                     type_info * base_rtti = getRTTI(
182                         (typelib_CompoundTypeDescription *)pTypeDescr->pBaseTypeDescription );
183                     rtti = new __si_class_type_info(
184                         strdup( rttiName ), (__class_type_info *)base_rtti );
185                 }
186                 else
187                 {
188                     // this class has no base class
189                     rtti = new __class_type_info( strdup( rttiName ) );
190                 }
191 
192                 pair< t_rtti_map::iterator, bool > insertion(
193                     m_generatedRttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
194                 OSL_ENSURE( insertion.second, "### inserting new generated rtti failed?!" );
195             }
196             else // taking already generated rtti
197             {
198                 rtti = iFind->second;
199             }
200         }
201     }
202     else
203     {
204         rtti = iFind->second;
205     }
206 
207     return rtti;
208 }
209 
210 //--------------------------------------------------------------------------------------------------
211 static void deleteException( void * pExc )
212 {
213     __cxa_exception const * header = ((__cxa_exception const *)pExc - 1);
214     typelib_TypeDescription * pTD = 0;
215     OUString unoName( toUNOname( header->exceptionType->name() ) );
216     ::typelib_typedescription_getByName( &pTD, unoName.pData );
217     OSL_ENSURE( pTD, "### unknown exception type! leaving out destruction => leaking!!!" );
218     if (pTD)
219     {
220 		::uno_destructData( pExc, pTD, cpp_release );
221 		::typelib_typedescription_release( pTD );
222 	}
223 }
224 
225 //==================================================================================================
226 void raiseException( uno_Any * pUnoExc, uno_Mapping * pUno2Cpp )
227 {
228 #if defined DEBUG
229     OString cstr(
230         OUStringToOString(
231             *reinterpret_cast< OUString const * >( &pUnoExc->pType->pTypeName ),
232             RTL_TEXTENCODING_ASCII_US ) );
233     fprintf( stderr, "> uno exception occured: %s\n", cstr.getStr() );
234 #endif
235     void * pCppExc;
236     type_info * rtti;
237 
238     {
239     // construct cpp exception object
240 	typelib_TypeDescription * pTypeDescr = 0;
241 	TYPELIB_DANGER_GET( &pTypeDescr, pUnoExc->pType );
242     OSL_ASSERT( pTypeDescr );
243     if (! pTypeDescr)
244     {
245         throw RuntimeException(
246             OUString( RTL_CONSTASCII_USTRINGPARAM("cannot get typedescription for type ") ) +
247             *reinterpret_cast< OUString const * >( &pUnoExc->pType->pTypeName ),
248             Reference< XInterface >() );
249     }
250 
251 	pCppExc = __cxa_allocate_exception( pTypeDescr->nSize );
252 	::uno_copyAndConvertData( pCppExc, pUnoExc->pData, pTypeDescr, pUno2Cpp );
253 
254 	// destruct uno exception
255 	::uno_any_destruct( pUnoExc, 0 );
256     // avoiding locked counts
257     static RTTI * s_rtti = 0;
258     if (! s_rtti)
259     {
260         MutexGuard guard( Mutex::getGlobalMutex() );
261         if (! s_rtti)
262         {
263 #ifdef LEAK_STATIC_DATA
264             s_rtti = new RTTI();
265 #else
266             static RTTI rtti_data;
267             s_rtti = &rtti_data;
268 #endif
269         }
270     }
271 	rtti = (type_info *)s_rtti->getRTTI( (typelib_CompoundTypeDescription *) pTypeDescr );
272     TYPELIB_DANGER_RELEASE( pTypeDescr );
273     OSL_ENSURE( rtti, "### no rtti for throwing exception!" );
274     if (! rtti)
275     {
276         throw RuntimeException(
277             OUString( RTL_CONSTASCII_USTRINGPARAM("no rtti for type ") ) +
278             *reinterpret_cast< OUString const * >( &pUnoExc->pType->pTypeName ),
279             Reference< XInterface >() );
280     }
281     }
282 
283 	__cxa_throw( pCppExc, rtti, deleteException );
284 }
285 
286 //==================================================================================================
287 void fillUnoException( __cxa_exception * header, uno_Any * pUnoExc, uno_Mapping * pCpp2Uno )
288 {
289     if (! header)
290     {
291         RuntimeException aRE(
292             OUString( RTL_CONSTASCII_USTRINGPARAM("no exception header!") ),
293             Reference< XInterface >() );
294         Type const & rType = ::getCppuType( &aRE );
295         uno_type_any_constructAndConvert( pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno );
296 #if defined _DEBUG
297         OString cstr( OUStringToOString( aRE.Message, RTL_TEXTENCODING_ASCII_US ) );
298         OSL_ENSURE( 0, cstr.getStr() );
299 #endif
300         return;
301     }
302 
303 	typelib_TypeDescription * pExcTypeDescr = 0;
304     OUString unoName( toUNOname( header->exceptionType->name() ) );
305 #if defined DEBUG
306     OString cstr_unoName( OUStringToOString( unoName, RTL_TEXTENCODING_ASCII_US ) );
307     fprintf( stderr, "> c++ exception occured: %s\n", cstr_unoName.getStr() );
308 #endif
309 	typelib_typedescription_getByName( &pExcTypeDescr, unoName.pData );
310     if (0 == pExcTypeDescr)
311     {
312         RuntimeException aRE(
313             OUString( RTL_CONSTASCII_USTRINGPARAM("exception type not found: ") ) + unoName,
314             Reference< XInterface >() );
315         Type const & rType = ::getCppuType( &aRE );
316         uno_type_any_constructAndConvert( pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno );
317 #if defined _DEBUG
318         OString cstr( OUStringToOString( aRE.Message, RTL_TEXTENCODING_ASCII_US ) );
319         OSL_ENSURE( 0, cstr.getStr() );
320 #endif
321     }
322     else
323     {
324         // construct uno exception any
325         uno_any_constructAndConvert( pUnoExc, header->adjustedPtr, pExcTypeDescr, pCpp2Uno );
326         typelib_typedescription_release( pExcTypeDescr );
327     }
328 }
329 
330 }
331 
332