Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit 5186774

Browse files
ibuclawdlang-bot
authored andcommitted
core.stdcpp: Upstream differences between dmd and gdc/ldc druntimes
- Change destructors from `extern(D)` -> `@weak extern(C++)` - Change CppRuntime_Microsoft `exception.what` from `extern(D)` -> `@weak extern(C++)` - Add `assert(0)` to CppRuntime_Microsoft `exception._Doraise` - Add missing `bad_exception.what` for GenericBaseException - Add `@nogc:` to core.stdcpp.typeinfo - Add `nothrow` to `type_info.before` and `type_info.name` - Mark CppRuntime_Gcc `type_info` as abstract - Add definitions to CppRuntime_Gcc and CppRuntime_Clang `bad_cast.what` and `bad_typeid.what`
1 parent 3229912 commit 5186774

File tree

8 files changed

+195
-26
lines changed

8 files changed

+195
-26
lines changed

mak/SRCS

+1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ SRCS=\
110110
src\core\stdcpp\new_.d \
111111
src\core\stdcpp\string.d \
112112
src\core\stdcpp\string_view.d \
113+
src\core\stdcpp\typeinfo.d \
113114
src\core\stdcpp\type_traits.d \
114115
src\core\stdcpp\utility.d \
115116
src\core\stdcpp\vector.d \

src/core/stdcpp/exception.d

+9-3
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,16 @@ else version (CppRuntime_Microsoft)
105105
///
106106
this(const(char)* message = "unknown", int = 1) nothrow { msg = message; }
107107
///
108-
extern(D) ~this() nothrow {}
108+
@weak ~this() nothrow {}
109109

110110
///
111-
extern(D) const(char)* what() const nothrow { return msg != null ? msg : "unknown exception"; }
111+
@weak const(char)* what() const nothrow { return msg != null ? msg : "unknown exception"; }
112112

113113
// TODO: do we want this? exceptions are classes... ref types.
114114
// final ref exception opAssign(ref const(exception) e) nothrow { msg = e.msg; return this; }
115115

116116
protected:
117-
@weak void _Doraise() const {}
117+
@weak void _Doraise() const { assert(0); }
118118

119119
protected:
120120
const(char)* msg;
@@ -130,4 +130,10 @@ class bad_exception : exception
130130
@nogc:
131131
///
132132
this(const(char)* message = "bad exception") { super(message); }
133+
134+
version (GenericBaseException)
135+
{
136+
///
137+
@weak override const(char)* what() const nothrow { return "bad exception"; }
138+
}
133139
}

src/core/stdcpp/typeinfo.d

+29-23
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ version (CppRuntime_DigitalMars)
1818
import core.stdcpp.exception;
1919

2020
extern (C++, "std"):
21+
@nogc:
2122

2223
class type_info
2324
{
@@ -29,8 +30,8 @@ version (CppRuntime_DigitalMars)
2930

3031
//bool operator==(const type_info rhs) const;
3132
//bool operator!=(const type_info rhs) const;
32-
final bool before(const type_info rhs) const;
33-
final const(char)* name() const;
33+
final bool before(const type_info rhs) const nothrow;
34+
final const(char)* name() const nothrow;
3435
protected:
3536
//type_info();
3637
private:
@@ -61,6 +62,7 @@ else version (CppRuntime_Microsoft)
6162
import core.stdcpp.exception;
6263

6364
extern (C++, "std"):
65+
@nogc:
6466

6567
struct __type_info_node
6668
{
@@ -72,12 +74,11 @@ else version (CppRuntime_Microsoft)
7274

7375
class type_info
7476
{
75-
//virtual ~this();
76-
void dtor() { } // reserve slot in vtbl[]
77+
@weak ~this() nothrow {}
7778
//bool operator==(const type_info rhs) const;
7879
//bool operator!=(const type_info rhs) const;
79-
final bool before(const type_info rhs) const;
80-
final const(char)* name(__type_info_node* p = &__type_info_root_node) const;
80+
final bool before(const type_info rhs) const nothrow;
81+
final const(char)* name(__type_info_node* p = &__type_info_root_node) const nothrow;
8182

8283
private:
8384
void* pdata;
@@ -87,13 +88,13 @@ else version (CppRuntime_Microsoft)
8788

8889
class bad_cast : exception
8990
{
90-
this(const(char)* msg = "bad cast");
91+
this(const(char)* msg = "bad cast") @nogc nothrow { super(msg); }
9192
//virtual ~this();
9293
}
9394

9495
class bad_typeid : exception
9596
{
96-
this(const(char)* msg = "bad typeid");
97+
this(const(char)* msg = "bad typeid") @nogc nothrow { super(msg); }
9798
//virtual ~this();
9899
}
99100
}
@@ -107,15 +108,17 @@ else version (CppRuntime_Gcc)
107108
}
108109

109110
extern (C++, "std"):
111+
@nogc:
110112

111-
class type_info
113+
abstract class type_info
112114
{
113-
void dtor1(); // consume destructor slot in vtbl[]
114-
void dtor2(); // consume destructor slot in vtbl[]
115-
@weak final const(char)* name()() const nothrow {
115+
@weak ~this() {}
116+
@weak final const(char)* name() const nothrow
117+
{
116118
return _name[0] == '*' ? _name + 1 : _name;
117119
}
118-
@weak final bool before()(const type_info _arg) const {
120+
@weak final bool before(const type_info _arg) const nothrow
121+
{
119122
import core.stdc.string : strcmp;
120123
return (_name[0] == '*' && _arg._name[0] == '*')
121124
? _name < _arg._name
@@ -127,29 +130,32 @@ else version (CppRuntime_Gcc)
127130
bool __do_catch(const type_info, void**, uint) const;
128131
bool __do_upcast(const __class_type_info*, void**) const;
129132

133+
protected:
130134
const(char)* _name;
131-
this(const(char)*);
135+
136+
this(const(char)* name) { _name = name; }
132137
}
133138

134139
class bad_cast : exception
135140
{
136-
this();
141+
this() nothrow {}
137142
//~this();
138-
@weak override const(char)* what() const;
143+
@weak override const(char)* what() const nothrow { return "bad cast"; }
139144
}
140145

141146
class bad_typeid : exception
142147
{
143-
this();
148+
this() nothrow {}
144149
//~this();
145-
@weak override const(char)* what() const;
150+
@weak override const(char)* what() const nothrow { return "bad typeid"; }
146151
}
147152
}
148153
else version (CppRuntime_Clang)
149154
{
150155
import core.stdcpp.exception;
151156

152157
extern (C++, "std"):
158+
@nogc:
153159

154160
abstract class type_info
155161
{
@@ -167,21 +173,21 @@ else version (CppRuntime_Clang)
167173
protected:
168174
const(char)* __type_name;
169175

170-
extern(D) this(const(char)* __n) { __type_name = __n; }
176+
this(const(char)* __n) { __type_name = __n; }
171177
}
172178

173179
class bad_cast : exception
174180
{
175-
this();
181+
this() nothrow {}
176182
//~this();
177-
@weak override const(char)* what() const;
183+
@weak override const(char)* what() const nothrow { return "bad cast"; }
178184
}
179185

180186
class bad_typeid : exception
181187
{
182-
this();
188+
this() nothrow {}
183189
//~this();
184-
@weak override const(char)* what() const;
190+
@weak override const(char)* what() const nothrow { return "bad typeid"; }
185191
}
186192
}
187193
else

test/stdcpp/Makefile

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ ifeq (osx,$(OS))
1313
# TESTS+=vector
1414
endif
1515
ifeq (linux,$(OS))
16+
TESTS11+=exception typeinfo
17+
TESTS+=typeinfo
1618
# TESTS+=string
1719
# TESTS+=vector
1820
OLDABITESTS+=string

test/stdcpp/src/exception.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <exception>
2+
3+
void throw_exception()
4+
{
5+
throw std::exception();
6+
}
7+
8+
void throw_bad_exception()
9+
{
10+
throw std::bad_exception();
11+
}
12+
13+
class custom_exception : public std::exception
14+
{
15+
const char* what() const noexcept { return "custom_exception"; }
16+
};
17+
18+
void throw_custom_exception()
19+
{
20+
throw custom_exception();
21+
}

test/stdcpp/src/exception_test.d

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import core.stdc.string;
2+
import core.stdcpp.exception;
3+
4+
unittest
5+
{
6+
try
7+
{
8+
throw_exception();
9+
}
10+
catch (exception e)
11+
{
12+
const what = e.what();
13+
assert(!strcmp(what, "unknown") || // druntime override
14+
!strcmp(what, "std::exception"));
15+
}
16+
try
17+
{
18+
throw_bad_exception();
19+
}
20+
catch (exception e)
21+
{
22+
const what = e.what();
23+
assert(!strcmp(what, "bad exception") || // druntime override
24+
!strcmp(what, "std::bad_exception"));
25+
}
26+
try
27+
{
28+
throw_custom_exception();
29+
}
30+
catch (exception e)
31+
{
32+
assert(!strcmp(e.what(), "custom_exception"));
33+
}
34+
}
35+
36+
extern(C++):
37+
void throw_exception();
38+
void throw_bad_exception();
39+
void throw_custom_exception();

test/stdcpp/src/typeinfo.cpp

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <typeinfo>
2+
3+
void throw_bad_cast()
4+
{
5+
throw std::bad_cast();
6+
}
7+
8+
void throw_bad_typeid()
9+
{
10+
throw std::bad_typeid();
11+
}
12+
13+
const std::type_info& typeid_int()
14+
{
15+
return typeid(int);
16+
}
17+
18+
const std::type_info& typeid_double()
19+
{
20+
return typeid(double);
21+
}
22+
23+
class Toil { };
24+
25+
const std::type_info& typeid_toil()
26+
{
27+
return typeid(Toil);
28+
}
29+
30+
const std::type_info& typeid_const_toil()
31+
{
32+
return typeid(const Toil&);
33+
}
34+
35+
class Trouble { };
36+
37+
const std::type_info& typeid_trouble()
38+
{
39+
return typeid(Trouble);
40+
}

test/stdcpp/src/typeinfo_test.d

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import core.stdc.string;
2+
import core.stdcpp.typeinfo;
3+
4+
unittest
5+
{
6+
try
7+
{
8+
throw_bad_cast();
9+
}
10+
catch (bad_cast e)
11+
{
12+
const what = e.what();
13+
assert(!strcmp(what, "bad cast") || // druntime override
14+
!strcmp(what, "std::bad_cast"));
15+
}
16+
try
17+
{
18+
throw_bad_typeid();
19+
}
20+
catch (bad_typeid e)
21+
{
22+
const what = e.what();
23+
assert(!strcmp(what, "bad typeid") || // druntime override
24+
!strcmp(what, "std::bad_typeid"));
25+
}
26+
27+
const tid1 = typeid_int();
28+
const tid2 = typeid_double();
29+
assert(tid1 != tid2);
30+
assert(!strcmp(tid1.name(), "i"));
31+
assert(!strcmp(tid2.name(), "d"));
32+
33+
const tid3 = typeid_toil();
34+
const tid4 = typeid_const_toil();
35+
assert(tid3 == tid4);
36+
assert(!strcmp(tid3.name(), "4Toil"));
37+
38+
const tid5 = typeid_trouble();
39+
assert(tid4 != tid5);
40+
assert(!strcmp(tid5.name(), "7Trouble"));
41+
42+
assert(tid2.before(tid1));
43+
assert(tid3.before(tid2));
44+
assert(tid4.before(tid5));
45+
}
46+
47+
extern(C++):
48+
void throw_bad_cast();
49+
void throw_bad_typeid();
50+
const(type_info) typeid_int();
51+
const(type_info) typeid_double();
52+
const(type_info) typeid_toil();
53+
const(type_info) typeid_const_toil();
54+
const(type_info) typeid_trouble();

0 commit comments

Comments
 (0)