xref: /freebsd/contrib/libcxxrt/typeinfo.cc (revision 40a8ac8f62b535d30349faf28cf47106b7041b83)
1 /*
2  * Copyright 2010-2012 PathScale, Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  *    this list of conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  *    this list of conditions and the following disclaimer in the documentation
12  *    and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
15  * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include "typeinfo.h"
28 #include <string.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 
32 using std::type_info;
33 
34 type_info::~type_info() {}
35 
36 bool type_info::operator==(const type_info &other) const
37 {
38 #ifdef LIBCXXRT_MERGED_TYPEINFO
39 	return __type_name == other.__type_name;
40 #else
41 	return __type_name == other.__type_name || strcmp(__type_name, other.__type_name) == 0;
42 #endif
43 }
44 bool type_info::operator!=(const type_info &other) const
45 {
46 	return !operator==(other);
47 }
48 bool type_info::before(const type_info &other) const
49 {
50 #ifdef LIBCXXRT_MERGED_TYPEINFO
51 	return __type_name < other.__type_name;
52 #else
53 	return strcmp(__type_name, other.__type_name) < 0;
54 #endif
55 }
56 const char* type_info::name() const
57 {
58 	return __type_name;
59 }
60 type_info::type_info (const type_info& rhs)
61 {
62 	__type_name = rhs.__type_name;
63 }
64 type_info& type_info::operator= (const type_info& rhs)
65 {
66 	return *new type_info(rhs);
67 }
68 
69 ABI_NAMESPACE::__fundamental_type_info::~__fundamental_type_info() {}
70 ABI_NAMESPACE::__array_type_info::~__array_type_info() {}
71 ABI_NAMESPACE::__function_type_info::~__function_type_info() {}
72 ABI_NAMESPACE::__enum_type_info::~__enum_type_info() {}
73 ABI_NAMESPACE::__class_type_info::~__class_type_info() {}
74 ABI_NAMESPACE::__si_class_type_info::~__si_class_type_info() {}
75 ABI_NAMESPACE::__vmi_class_type_info::~__vmi_class_type_info() {}
76 ABI_NAMESPACE::__pbase_type_info::~__pbase_type_info() {}
77 ABI_NAMESPACE::__pointer_type_info::~__pointer_type_info() {}
78 ABI_NAMESPACE::__pointer_to_member_type_info::~__pointer_to_member_type_info() {}
79 
80 // From libelftc
81 extern "C" char    *__cxa_demangle_gnu3(const char *);
82 
83 extern "C" char* __cxa_demangle(const char* mangled_name,
84                                 char* buf,
85                                 size_t* n,
86                                 int* status)
87 {
88 	// TODO: We should probably just be linking against libelf-tc, rather than
89 	// copying their code.  This requires them to do an actual release,
90 	// however, and for our changes to be pushed upstream.  We also need to
91 	// call a different demangling function here depending on the ABI (e.g.
92 	// ARM).
93 	char *demangled = __cxa_demangle_gnu3(mangled_name);
94 	if (NULL != demangled)
95 	{
96 		size_t len = strlen(demangled);
97 		if (buf == NULL)
98 		{
99 			if (n)
100 			{
101 				*n = len;
102 			}
103 			return demangled;
104 		}
105 		if (*n < len+1)
106 		{
107 			buf = (char*)realloc(buf, len+1);
108 		}
109 		if (0 != buf)
110 		{
111 			memcpy(buf, demangled, len);
112 			buf[len] = 0;
113 			if (n)
114 			{
115 				*n = len;
116 			}
117 			if (status)
118 			{
119 				*status = 0;
120 			}
121 		}
122 		else
123 		{
124 			if (status)
125 			{
126 				*status = -1;
127 			}
128 		}
129 		free(demangled);
130 	}
131 	else
132 	{
133 		if (status)
134 		{
135 			*status = -2;
136 		}
137 		return NULL;
138 	}
139 	return buf;
140 }
141