/import/object.di

http://github.com/AlexeyProkhin/druntime · Unknown · 523 lines · 432 code · 91 blank · 0 comment · 0 complexity · 8e70d8ff053bc14f361d41f085385483 MD5 · raw file

  1. /**
  2. * Contains all implicitly declared types and variables.
  3. *
  4. * Copyright: Copyright Digital Mars 2000 - 2011.
  5. * License: <a href="http://www.boost.org/LICENSE_1_0.txt">Boost License 1.0</a>.
  6. * Authors: Walter Bright, Sean Kelly
  7. *
  8. * Copyright Digital Mars 2000 - 2011.
  9. * Distributed under the Boost Software License, Version 1.0.
  10. * (See accompanying file LICENSE_1_0.txt or copy at
  11. * http://www.boost.org/LICENSE_1_0.txt)
  12. */
  13. module object;
  14. alias typeof(int.sizeof) size_t;
  15. alias typeof(cast(void*)0 - cast(void*)0) ptrdiff_t;
  16. alias ptrdiff_t sizediff_t;
  17. alias size_t hash_t;
  18. alias bool equals_t;
  19. alias immutable(char)[] string;
  20. alias immutable(wchar)[] wstring;
  21. alias immutable(dchar)[] dstring;
  22. class Object
  23. {
  24. string toString();
  25. hash_t toHash();
  26. int opCmp(Object o);
  27. equals_t opEquals(Object o);
  28. equals_t opEquals(Object lhs, Object rhs);
  29. interface Monitor
  30. {
  31. void lock();
  32. void unlock();
  33. }
  34. static Object factory(string classname);
  35. }
  36. bool opEquals(Object lhs, Object rhs);
  37. //bool opEquals(TypeInfo lhs, TypeInfo rhs);
  38. void setSameMutex(shared Object ownee, shared Object owner);
  39. struct Interface
  40. {
  41. TypeInfo_Class classinfo;
  42. void*[] vtbl;
  43. ptrdiff_t offset; // offset to Interface 'this' from Object 'this'
  44. }
  45. struct OffsetTypeInfo
  46. {
  47. size_t offset;
  48. TypeInfo ti;
  49. }
  50. class TypeInfo
  51. {
  52. hash_t getHash(in void* p);
  53. equals_t equals(in void* p1, in void* p2);
  54. int compare(in void* p1, in void* p2);
  55. size_t tsize();
  56. void swap(void* p1, void* p2);
  57. TypeInfo next();
  58. void[] init();
  59. uint flags();
  60. // 1: // has possible pointers into GC memory
  61. OffsetTypeInfo[] offTi();
  62. void destroy(void* p);
  63. void postblit(void* p);
  64. size_t talign();
  65. version (X86_64) int argTypes(out TypeInfo arg1, out TypeInfo arg2);
  66. }
  67. class TypeInfo_Typedef : TypeInfo
  68. {
  69. TypeInfo base;
  70. string name;
  71. void[] m_init;
  72. }
  73. class TypeInfo_Enum : TypeInfo_Typedef
  74. {
  75. }
  76. class TypeInfo_Pointer : TypeInfo
  77. {
  78. TypeInfo m_next;
  79. }
  80. class TypeInfo_Array : TypeInfo
  81. {
  82. TypeInfo value;
  83. }
  84. class TypeInfo_StaticArray : TypeInfo
  85. {
  86. TypeInfo value;
  87. size_t len;
  88. }
  89. class TypeInfo_AssociativeArray : TypeInfo
  90. {
  91. TypeInfo value;
  92. TypeInfo key;
  93. TypeInfo impl;
  94. }
  95. class TypeInfo_Function : TypeInfo
  96. {
  97. TypeInfo next;
  98. }
  99. class TypeInfo_Delegate : TypeInfo
  100. {
  101. TypeInfo next;
  102. }
  103. class TypeInfo_Class : TypeInfo
  104. {
  105. @property TypeInfo_Class info() { return this; }
  106. @property TypeInfo typeinfo() { return this; }
  107. byte[] init; // class static initializer
  108. string name; // class name
  109. void*[] vtbl; // virtual function pointer table
  110. Interface[] interfaces;
  111. TypeInfo_Class base;
  112. void* destructor;
  113. void function(Object) classInvariant;
  114. uint m_flags;
  115. // 1: // is IUnknown or is derived from IUnknown
  116. // 2: // has no possible pointers into GC memory
  117. // 4: // has offTi[] member
  118. // 8: // has constructors
  119. // 16: // has xgetMembers member
  120. // 32: // has typeinfo member
  121. void* deallocator;
  122. OffsetTypeInfo[] m_offTi;
  123. void* defaultConstructor;
  124. const(MemberInfo[]) function(string) xgetMembers;
  125. static TypeInfo_Class find(in char[] classname);
  126. Object create();
  127. const(MemberInfo[]) getMembers(in char[] classname);
  128. }
  129. alias TypeInfo_Class ClassInfo;
  130. class TypeInfo_Interface : TypeInfo
  131. {
  132. ClassInfo info;
  133. }
  134. class TypeInfo_Struct : TypeInfo
  135. {
  136. string name;
  137. void[] m_init;
  138. uint function(in void*) xtoHash;
  139. equals_t function(in void*, in void*) xopEquals;
  140. int function(in void*, in void*) xopCmp;
  141. string function(in void*) xtoString;
  142. uint m_flags;
  143. const(MemberInfo[]) function(in char[]) xgetMembers;
  144. void function(void*) xdtor;
  145. void function(void*) xpostblit;
  146. uint m_align;
  147. version (X86_64)
  148. {
  149. TypeInfo m_arg1;
  150. TypeInfo m_arg2;
  151. }
  152. }
  153. class TypeInfo_Tuple : TypeInfo
  154. {
  155. TypeInfo[] elements;
  156. }
  157. class TypeInfo_Const : TypeInfo
  158. {
  159. TypeInfo next;
  160. }
  161. class TypeInfo_Invariant : TypeInfo_Const
  162. {
  163. }
  164. class TypeInfo_Shared : TypeInfo_Const
  165. {
  166. }
  167. class TypeInfo_Inout : TypeInfo_Const
  168. {
  169. }
  170. abstract class MemberInfo
  171. {
  172. string name();
  173. }
  174. class MemberInfo_field : MemberInfo
  175. {
  176. this(string name, TypeInfo ti, size_t offset);
  177. override string name();
  178. TypeInfo typeInfo();
  179. size_t offset();
  180. }
  181. class MemberInfo_function : MemberInfo
  182. {
  183. enum
  184. {
  185. Virtual = 1,
  186. Member = 2,
  187. Static = 4,
  188. }
  189. this(string name, TypeInfo ti, void* fp, uint flags);
  190. override string name();
  191. TypeInfo typeInfo();
  192. void* fp();
  193. uint flags();
  194. }
  195. struct ModuleInfo
  196. {
  197. struct New
  198. {
  199. uint flags;
  200. uint index;
  201. }
  202. struct Old
  203. {
  204. string name;
  205. ModuleInfo*[] importedModules;
  206. TypeInfo_Class[] localClasses;
  207. uint flags;
  208. void function() ctor;
  209. void function() dtor;
  210. void function() unitTest;
  211. void* xgetMembers;
  212. void function() ictor;
  213. void function() tlsctor;
  214. void function() tlsdtor;
  215. uint index;
  216. void*[1] reserved;
  217. }
  218. union
  219. {
  220. New n;
  221. Old o;
  222. }
  223. @property bool isNew();
  224. @property uint index();
  225. @property void index(uint i);
  226. @property uint flags();
  227. @property void flags(uint f);
  228. @property void function() tlsctor();
  229. @property void function() tlsdtor();
  230. @property void* xgetMembers();
  231. @property void function() ctor();
  232. @property void function() dtor();
  233. @property void function() ictor();
  234. @property void function() unitTest();
  235. @property ModuleInfo*[] importedModules();
  236. @property TypeInfo_Class[] localClasses();
  237. @property string name();
  238. static int opApply(scope int delegate(ref ModuleInfo*) dg);
  239. }
  240. ModuleInfo*[] _moduleinfo_tlsdtors;
  241. uint _moduleinfo_tlsdtors_i;
  242. extern (C) void _moduleTlsCtor();
  243. extern (C) void _moduleTlsDtor();
  244. class Throwable : Object
  245. {
  246. interface TraceInfo
  247. {
  248. int opApply(scope int delegate(ref char[]));
  249. int opApply(scope int delegate(ref size_t, ref char[]));
  250. string toString();
  251. }
  252. string msg;
  253. string file;
  254. size_t line;
  255. TraceInfo info;
  256. Throwable next;
  257. this(string msg, Throwable next = null);
  258. this(string msg, string file, size_t line, Throwable next = null);
  259. override string toString();
  260. }
  261. class Exception : Throwable
  262. {
  263. this(string msg, string file = __FILE__, size_t line = __LINE__, Throwable next = null);
  264. this(string msg, Throwable next, string file = __FILE__, size_t line = __LINE__);
  265. }
  266. class Error : Throwable
  267. {
  268. this(string msg, Throwable next = null);
  269. this(string msg, string file, size_t line, Throwable next = null);
  270. Throwable bypassedException;
  271. }
  272. extern (C)
  273. {
  274. // from druntime/src/compiler/dmd/aaA.d
  275. size_t _aaLen(void* p);
  276. void* _aaGet(void** pp, TypeInfo keyti, size_t valuesize, ...);
  277. void* _aaGetRvalue(void* p, TypeInfo keyti, size_t valuesize, ...);
  278. void* _aaIn(void* p, TypeInfo keyti);
  279. void _aaDel(void* p, TypeInfo keyti, ...);
  280. void[] _aaValues(void* p, size_t keysize, size_t valuesize);
  281. void[] _aaKeys(void* p, size_t keysize);
  282. void* _aaRehash(void** pp, TypeInfo keyti);
  283. extern (D) typedef scope int delegate(void *) _dg_t;
  284. int _aaApply(void* aa, size_t keysize, _dg_t dg);
  285. extern (D) typedef scope int delegate(void *, void *) _dg2_t;
  286. int _aaApply2(void* aa, size_t keysize, _dg2_t dg);
  287. void* _d_assocarrayliteralT(TypeInfo_AssociativeArray ti, size_t length, ...);
  288. }
  289. struct AssociativeArray(Key, Value)
  290. {
  291. void* p;
  292. size_t length() @property { return _aaLen(p); }
  293. Value[Key] rehash() @property
  294. {
  295. auto p = _aaRehash(&p, typeid(Value[Key]));
  296. return *cast(Value[Key]*)(&p);
  297. }
  298. Value[] values() @property
  299. {
  300. auto a = _aaValues(p, Key.sizeof, Value.sizeof);
  301. return *cast(Value[]*) &a;
  302. }
  303. Key[] keys() @property
  304. {
  305. auto a = _aaKeys(p, Key.sizeof);
  306. return *cast(Key[]*) &a;
  307. }
  308. int opApply(scope int delegate(ref Key, ref Value) dg)
  309. {
  310. return _aaApply2(p, Key.sizeof, cast(_dg2_t)dg);
  311. }
  312. int opApply(scope int delegate(ref Value) dg)
  313. {
  314. return _aaApply(p, Key.sizeof, cast(_dg_t)dg);
  315. }
  316. int delegate(int delegate(ref Key) dg) byKey()
  317. {
  318. int foo(int delegate(ref Key) dg)
  319. {
  320. int byKeydg(ref Key key, ref Value value)
  321. {
  322. return dg(key);
  323. }
  324. return _aaApply2(p, Key.sizeof, cast(_dg2_t)&byKeydg);
  325. }
  326. return &foo;
  327. }
  328. int delegate(int delegate(ref Value) dg) byValue()
  329. {
  330. return &opApply;
  331. }
  332. Value get(Key key, lazy Value defaultValue)
  333. {
  334. auto r = key in *cast(Value[Key]*)(&p);
  335. return r ? *r : defaultValue;
  336. }
  337. static if (is(typeof({ Value[Key] r; r[Key.init] = Value.init; }())))
  338. @property Value[Key] dup()
  339. {
  340. Value[Key] result;
  341. foreach (k, v; this)
  342. {
  343. result[k] = v;
  344. }
  345. return result;
  346. }
  347. }
  348. unittest
  349. {
  350. auto a = [ 1:"one", 2:"two", 3:"three" ];
  351. auto b = a.dup;
  352. assert(b == [ 1:"one", 2:"two", 3:"three" ]);
  353. }
  354. void clear(T)(T obj) if (is(T == class))
  355. {
  356. if (!obj) return;
  357. auto ci = obj.classinfo;
  358. auto defaultCtor =
  359. cast(void function(Object)) ci.defaultConstructor;
  360. version(none) // enforce isn't available in druntime
  361. _enforce(defaultCtor || (ci.flags & 8) == 0);
  362. immutable size = ci.init.length;
  363. auto ci2 = ci;
  364. do
  365. {
  366. auto dtor = cast(void function(Object))ci2.destructor;
  367. if (dtor)
  368. dtor(obj);
  369. ci2 = ci2.base;
  370. } while (ci2)
  371. auto buf = (cast(void*) obj)[0 .. size];
  372. buf[] = ci.init;
  373. if (defaultCtor)
  374. defaultCtor(obj);
  375. }
  376. void clear(T)(ref T obj) if (is(T == struct))
  377. {
  378. static if (is(typeof(obj.__dtor())))
  379. {
  380. obj.__dtor();
  381. }
  382. auto buf = (cast(ubyte*) &obj)[0 .. T.sizeof];
  383. auto init = cast(ubyte[])typeid(T).init();
  384. if(init.ptr is null) // null ptr means initialize to 0s
  385. buf[] = 0;
  386. else
  387. buf[] = init[];
  388. }
  389. void clear(T : U[n], U, size_t n)(ref T obj)
  390. {
  391. obj = T.init;
  392. }
  393. void clear(T)(ref T obj)
  394. if (!is(T == struct) && !is(T == class) && !_isStaticArray!T)
  395. {
  396. obj = T.init;
  397. }
  398. template _isStaticArray(T : U[N], U, size_t N)
  399. {
  400. enum bool _isStaticArray = true;
  401. }
  402. template _isStaticArray(T)
  403. {
  404. enum bool _isStaticArray = false;
  405. }
  406. private
  407. {
  408. extern (C) void _d_arrayshrinkfit(TypeInfo ti, void[] arr);
  409. extern (C) size_t _d_arraysetcapacity(TypeInfo ti, size_t newcapacity, void *arrptr);
  410. }
  411. @property size_t capacity(T)(T[] arr)
  412. {
  413. return _d_arraysetcapacity(typeid(T[]), 0, cast(void *)&arr);
  414. }
  415. size_t reserve(T)(ref T[] arr, size_t newcapacity)
  416. {
  417. return _d_arraysetcapacity(typeid(T[]), newcapacity, cast(void *)&arr);
  418. }
  419. void assumeSafeAppend(T)(T[] arr)
  420. {
  421. _d_arrayshrinkfit(typeid(T[]), *(cast(void[]*)&arr));
  422. }
  423. bool _ArrayEq(T1, T2)(T1[] a1, T2[] a2)
  424. {
  425. if (a1.length != a2.length)
  426. return false;
  427. foreach(i, a; a1)
  428. { if (a != a2[i])
  429. return false;
  430. }
  431. return true;
  432. }