| 1 |
// EXTRA_CPP_SOURCES: externmangle.cpp |
| 2 |
// REQUIRED_ARGS: -extern-std=c++11 |
| 3 |
|
| 4 |
import core.stdc.config; |
| 5 |
import core.stdc.stdint; |
| 6 |
|
| 7 |
extern(C++): |
| 8 |
|
| 9 |
struct Foo(X) |
| 10 |
{ |
| 11 |
X* v; |
| 12 |
} |
| 13 |
|
| 14 |
|
| 15 |
struct Boo(X) |
| 16 |
{ |
| 17 |
X* v; |
| 18 |
} |
| 19 |
|
| 20 |
|
| 21 |
void test1(Foo!int arg1); |
| 22 |
void test2(int* arg2, Boo!(int*) arg1); |
| 23 |
|
| 24 |
|
| 25 |
struct Test3(int X, int Y) |
| 26 |
{ |
| 27 |
} |
| 28 |
|
| 29 |
void test3(Test3!(3,3) arg1); |
| 30 |
|
| 31 |
void test4(Foo!(int*) arg1, Boo!(int*) arg2, Boo!(int*) arg3, int*, Foo!(double)); |
| 32 |
|
| 33 |
void test5(Foo!(int*) arg1, Boo!(int*) arg2, Boo!(int*) arg3); |
| 34 |
|
| 35 |
|
| 36 |
struct Goo |
| 37 |
{ |
| 38 |
struct Foo(X) |
| 39 |
{ |
| 40 |
X* v; |
| 41 |
} |
| 42 |
|
| 43 |
struct Boo(X) |
| 44 |
{ |
| 45 |
struct Xoo(Y) |
| 46 |
{ |
| 47 |
Y* v; |
| 48 |
}; |
| 49 |
X* v; |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
void test6(Foo!(Boo!(Foo!(void))) arg1); |
| 54 |
void test7(Boo!(void).Xoo!(int) arg1); |
| 55 |
} |
| 56 |
|
| 57 |
struct P1 |
| 58 |
{ |
| 59 |
struct Mem(T) |
| 60 |
{ |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
struct P2 |
| 65 |
{ |
| 66 |
struct Mem(T) |
| 67 |
{ |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
void test8(P1.Mem!int, P2.Mem!int); |
| 72 |
void test9(Foo!(int**), Foo!(int*), int**, int*); |
| 73 |
|
| 74 |
|
| 75 |
interface Test10 |
| 76 |
{ |
| 77 |
private final void test10(); |
| 78 |
public final void test11(); |
| 79 |
protected final void test12(); |
| 80 |
public final void test13() const; |
| 81 |
|
| 82 |
private void test14(); |
| 83 |
public void test15(); |
| 84 |
protected void test16(); |
| 85 |
|
| 86 |
private static void test17(); |
| 87 |
public static void test18(); |
| 88 |
protected static void test19(); |
| 89 |
}; |
| 90 |
|
| 91 |
Test10 Test10Ctor(); |
| 92 |
void Test10Dtor(ref Test10 ptr); |
| 93 |
|
| 94 |
struct Test20 |
| 95 |
{ |
| 96 |
__gshared: |
| 97 |
private extern int test20; |
| 98 |
protected extern int test21; |
| 99 |
public extern int test22; |
| 100 |
}; |
| 101 |
|
| 102 |
|
| 103 |
int test23(Test10*, Test10, Test10**, const(Test10)); |
| 104 |
int test23b(const Test10*, const Test10, Test10); |
| 105 |
|
| 106 |
void test24(int function(int,int)); |
| 107 |
|
| 108 |
void test25(int[291][6][5]* arr); |
| 109 |
int test26(int[291][6]* arr); |
| 110 |
|
| 111 |
void test27(int, ...); |
| 112 |
void test28(int); |
| 113 |
|
| 114 |
void test29(float); |
| 115 |
void test30(const float); |
| 116 |
|
| 117 |
struct Array(T) |
| 118 |
{ |
| 119 |
int dim; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
interface Module |
| 124 |
{ |
| 125 |
public static void imports(Module); |
| 126 |
public static int dim(Array!Module*); |
| 127 |
}; |
| 128 |
|
| 129 |
uint64_t testlongmangle(int a, uint b, int64_t c, uint64_t d); |
| 130 |
cpp_ulong testCppLongMangle(cpp_long a, cpp_ulong b); |
| 131 |
cpp_ulonglong testCppLongLongMangle(cpp_longlong a, cpp_ulonglong b); |
| 132 |
|
| 133 |
// direct size_t/ptrdiff_t interop is fine except on 32-bit OS X |
| 134 |
version (OSX) { version (D_LP64) {} else version = OSX_32; } |
| 135 |
version (OSX_32) |
| 136 |
cpp_size_t testCppSizeTMangle(cpp_ptrdiff_t a, cpp_size_t b); |
| 137 |
else |
| 138 |
size_t testCppSizeTMangle(ptrdiff_t a, size_t b); |
| 139 |
|
| 140 |
__gshared extern int[2][2][2] test31; |
| 141 |
__gshared extern int* test32; |
| 142 |
|
| 143 |
|
| 144 |
alias int function(Expression , void* ) apply_fp_t; |
| 145 |
|
| 146 |
interface Expression |
| 147 |
{ |
| 148 |
public final int apply(apply_fp_t fp, apply_fp_t fp2, void* param); |
| 149 |
public final int getType(); |
| 150 |
public static Expression create(int); |
| 151 |
public static void dispose(ref Expression); |
| 152 |
} |
| 153 |
|
| 154 |
//int test34(int[0][0]*); |
| 155 |
version(CRuntime_Microsoft){} |
| 156 |
else |
| 157 |
{ |
| 158 |
int test35(real arg); |
| 159 |
} |
| 160 |
|
| 161 |
const(char)* test36(const(char)*); |
| 162 |
|
| 163 |
final class Test37 |
| 164 |
{ |
| 165 |
static Test37 create() |
| 166 |
{ |
| 167 |
return new Test37; |
| 168 |
} |
| 169 |
|
| 170 |
bool test() |
| 171 |
{ |
| 172 |
return true; |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
bool test37(); |
| 177 |
|
| 178 |
interface Test38 |
| 179 |
{ |
| 180 |
final int test(int, ...); |
| 181 |
public static Test38 create(); |
| 182 |
public static void dispose(ref Test38); |
| 183 |
} |
| 184 |
|
| 185 |
extern(C++) int test39cpp(C2!char, S2!(int)*); |
| 186 |
|
| 187 |
extern(C++, class) |
| 188 |
struct S1 |
| 189 |
{ |
| 190 |
private int val; |
| 191 |
static S1* init(int); |
| 192 |
int value(); |
| 193 |
} |
| 194 |
|
| 195 |
extern(C++, class) |
| 196 |
struct S2(T) |
| 197 |
{ |
| 198 |
private T val; |
| 199 |
static S2!T* init(int); |
| 200 |
T value(); |
| 201 |
} |
| 202 |
|
| 203 |
extern(C++, struct) |
| 204 |
class C1 |
| 205 |
{ |
| 206 |
const(char)* data; |
| 207 |
|
| 208 |
static C1 init(const(char)* p); |
| 209 |
const(char)* getDataCPP(); |
| 210 |
extern(C++) const(char)* getDataD() |
| 211 |
{ |
| 212 |
return data; |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
extern(C++, struct) |
| 217 |
class C2(T) |
| 218 |
{ |
| 219 |
const(T)* data; |
| 220 |
|
| 221 |
static C2!T init(const(T)* p); |
| 222 |
const(T)* getData(); |
| 223 |
} |
| 224 |
|
| 225 |
void test39() |
| 226 |
{ |
| 227 |
S1* s1 = S1.init(42); |
| 228 |
assert(s1.value == 42); |
| 229 |
assert(S2!int.init(43).value == 43); |
| 230 |
const(char)* ptr = "test".ptr; |
| 231 |
C1 c1 = C1.init(ptr); |
| 232 |
assert(c1.getDataCPP() == ptr); |
| 233 |
assert(c1.getDataD() == ptr); |
| 234 |
C2!char c2 = C2!char.init(ptr); |
| 235 |
assert(c2.getData() == ptr); |
| 236 |
auto result = test39cpp(c2, S2!int.init(43)); |
| 237 |
assert(result == 0); |
| 238 |
} |
| 239 |
|
| 240 |
extern(C++, "foo", "bar", "baz") int doStuff(int); |
| 241 |
|
| 242 |
void test40(); |
| 243 |
|
| 244 |
void foovargs(T...)(T args) |
| 245 |
{ |
| 246 |
static if (is(T[0] == char*)) |
| 247 |
{ |
| 248 |
assert(*args[0] == 'a'); |
| 249 |
} |
| 250 |
else |
| 251 |
{ |
| 252 |
float ret = args[0] + args[1]; |
| 253 |
assert(ret == 3.0f); |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
alias FooVargs = foovargs!(int, float); |
| 258 |
alias FooVargs2 = foovargs!(char*); |
| 259 |
|
| 260 |
void test41(); |
| 261 |
void make_shared_poc(T, Args...)(ref Args args) |
| 262 |
{ |
| 263 |
assert(args[0] + args[1] == 3); |
| 264 |
} |
| 265 |
alias Make_Shared_Poc = make_shared_poc!(int, int, int); |
| 266 |
|
| 267 |
void main() |
| 268 |
{ |
| 269 |
test1(Foo!int()); |
| 270 |
test2(null, Boo!(int*)()); |
| 271 |
test3(Test3!(3,3)()); |
| 272 |
test4(Foo!(int*)(), Boo!(int*)(), Boo!(int*)(), null, Foo!(double)()); |
| 273 |
test5(Foo!(int*)(), Boo!(int*)(), Boo!(int*)()); |
| 274 |
Goo goo; |
| 275 |
goo.test6(Goo.Foo!(Goo.Boo!(Goo.Foo!(void)))()); |
| 276 |
goo.test7(Goo.Boo!(void).Xoo!(int)()); |
| 277 |
|
| 278 |
test8(P1.Mem!int(), P2.Mem!int()); |
| 279 |
test9(Foo!(int**)(), Foo!(int*)(), null, null); |
| 280 |
|
| 281 |
auto t10 = Test10Ctor(); |
| 282 |
scope(exit) Test10Dtor(t10); |
| 283 |
|
| 284 |
t10.test10(); |
| 285 |
t10.test11(); |
| 286 |
t10.test12(); |
| 287 |
t10.test13(); |
| 288 |
t10.test14(); |
| 289 |
t10.test15(); |
| 290 |
t10.test16(); |
| 291 |
t10.test17(); |
| 292 |
t10.test18(); |
| 293 |
t10.test19(); |
| 294 |
|
| 295 |
assert(Test20.test20 == 20); |
| 296 |
assert(Test20.test21 == 21); |
| 297 |
assert(Test20.test22 == 22); |
| 298 |
|
| 299 |
assert(test23(null, null, null, null) == 1); |
| 300 |
assert(test23b(null, null, null) == 1); |
| 301 |
|
| 302 |
extern(C++) static int cb(int a, int b){return a+b;} |
| 303 |
|
| 304 |
test24(&cb); |
| 305 |
int[291][6][5] arr; |
| 306 |
arr[1][1][1] = 42; |
| 307 |
test25(&arr); |
| 308 |
assert(test26(&arr[0]) == 42); |
| 309 |
|
| 310 |
test27(3,4,5); |
| 311 |
test28(3); |
| 312 |
|
| 313 |
test29(3.14f); |
| 314 |
test30(3.14f); |
| 315 |
|
| 316 |
auto t32 = &Module.imports; |
| 317 |
Array!Module arr2; |
| 318 |
arr2.dim = 20; |
| 319 |
assert(Module.dim(&arr2) == 20); |
| 320 |
|
| 321 |
assert(testlongmangle(1, 2, 3, 4) == 10); |
| 322 |
assert(testCppLongMangle(1, 2) == 3); |
| 323 |
assert(testCppLongLongMangle(3, 4) == 7); |
| 324 |
assert(testCppSizeTMangle(3, 4) == 7); |
| 325 |
assert(test31 == [[[1, 1], [1, 1]], [[1, 1], [1, 1]]]); |
| 326 |
assert(test32 == null); |
| 327 |
|
| 328 |
auto ee = Expression.create(42); |
| 329 |
extern(C++) static int efun(Expression e, void* p) |
| 330 |
{ |
| 331 |
return cast(int)(cast(size_t)p ^ e.getType()); |
| 332 |
} |
| 333 |
|
| 334 |
extern(C++) static int efun2(Expression e, void* p) |
| 335 |
{ |
| 336 |
return cast(int)(cast(size_t)p * e.getType()); |
| 337 |
} |
| 338 |
|
| 339 |
auto test33 = ee.apply(&efun, &efun2, cast(void*)&Expression.create); |
| 340 |
assert(test33 == cast(int)(cast(size_t)cast(void*)&Expression.create ^ 42) * cast(int)(cast(size_t)cast(void*)&Expression.create * 42)); |
| 341 |
Expression.dispose(ee); |
| 342 |
assert(ee is null); |
| 343 |
//assert(test34(null) == 0); |
| 344 |
version(CRuntime_Microsoft){} |
| 345 |
else |
| 346 |
{ |
| 347 |
assert(test35(3.14L) == 3); |
| 348 |
} |
| 349 |
const char* hello = "hello"; |
| 350 |
assert(test36(hello) == hello); |
| 351 |
assert(test37()); |
| 352 |
auto t38 = Test38.create(); |
| 353 |
assert(t38.test(1, 2, 3) == 1); |
| 354 |
Test38.dispose(t38); |
| 355 |
test39(); |
| 356 |
|
| 357 |
assert(doStuff(2) == 4); |
| 358 |
|
| 359 |
test40(); |
| 360 |
test41(); |
| 361 |
} |