Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

root/trunk/object.di

Revision 5652, 9.6 kB (checked in by Marenz, 1 year ago)

Merged branch dmd-1.067 into trunk

  • Property svn:eol-style set to native
Line 
1 /// module defining Object, the root class of all D objects, interfaces, ClassInfo and TypeInfo
2 /// it is implicitly included by the compiler in all D files
3 module object;
4 /// unsigned integer type of the size of a pointer
5 alias typeof(int.sizeof)                    size_t;
6 /// signed integer type of the size of a pointer
7 alias typeof(cast(void*)0 - cast(void*)0)   ptrdiff_t;
8
9 /// type of hashes used in associative arrays
10 alias size_t hash_t;
11 /// type returned by equality comparisons
12 alias int equals_t;
13
14 version (PhobosCompatibility)
15 {
16         alias char[]  string;
17         alias wchar[] wstring;
18         alias dchar[] dstring;
19 }
20
21 /// root class for all objects in D
22 class Object
23 {
24     void dispose();
25     /// returns a string representation of the object (for debugging purposes)
26     char[] toString();
27     /// returns a hash
28     hash_t toHash();
29     /// compares two objects, returns a number ret, such (a op b) is rewritten as (a.opCmp(b) op 0)
30     /// thus if a>b a.opCmp(b)>0
31     int    opCmp(Object o);
32     /// returns 0 if this==o
33     equals_t    opEquals(Object o);
34
35     interface Monitor
36     {
37         void lock();
38         void unlock();
39     }
40 }
41
42 /// interface, if COM objects (IUnknown) they might not be casted to Object
43 struct Interface
44 {
45     /// class info of the interface
46     ClassInfo   classinfo;
47     void*[]     vtbl;
48     /// offset to Interface 'this' from Object 'this'
49     ptrdiff_t   offset;
50 }
51
52 struct PointerMap
53 {
54     // Conservative pointer mask (one word, scan it, we don't know if it's
55     // really a pointer)
56     size_t[] bits = [1, 1, 0];
57
58     size_t size();
59     bool mustScanWordAt(size_t offset);
60     bool isPointerAt(size_t offset);
61     bool canUpdatePointers();
62 }
63
64 struct PointerMapBuilder
65 {
66     private size_t[] m_bits = null;
67     private size_t m_size = 0;
68
69     void size(size_t bytes);
70     void mustScanWordAt(size_t offset);
71     void inlineAt(size_t offset, PointerMap pm);
72     PointerMap convertToPointerMap();
73 }
74
75 /// class information
76 class ClassInfo : Object
77 {
78     byte[]      init;   // class static initializer
79     char[]      name;   /// class name
80     void*[]     vtbl;   // virtual function pointer table
81     Interface[] interfaces; /// implemented interfaces
82     ClassInfo   base; /// base class
83     void*       destructor; /// compiler dependent storage of destructor function pointer
84     void*       classInvariant; /// compiler dependent storage of classInvariant function pointer
85     /// flags
86     /// 1: IUnknown
87     /// 2: has no possible pointers into GC memory
88     /// 4: has offTi[] member
89     /// 8: has constructors
90     //  32: has typeinfo
91     uint        flags;
92     void*       deallocator;
93     OffsetTypeInfo[] offTi; /// offsets of its members (not supported by all compilers)
94     void*       defaultConstructor; /// compiler dependent storage of constructor function pointer
95     /// TypeInfo information about this class
96     TypeInfo typeinfo;
97     version (D_HavePointerMap) {
98         PointerMap pointermap;
99     }
100     /// finds the classinfo of the class with the given name
101     static ClassInfo find(char[] classname);
102     /// creates an instance of this class (works only if there is a constructor without arguments)
103     Object create();
104 }
105
106 /// offset of the different fields (at the moment works only with ldc)
107 struct OffsetTypeInfo
108 {
109     size_t   offset;
110     TypeInfo ti;
111 }
112
113 /// information on a type
114 class TypeInfo
115 {
116     /// returns the hash of the type of this TypeInfo at p
117     hash_t   getHash(void *p);
118     /// returns 0 if the types of this TypeInfo stored at p1 and p2 are different
119     equals_t      equals(void *p1, void *p2);
120     /// compares the types of this TypeInfo stored at p1 and p2
121     int      compare(void *p1, void *p2);
122      /// Return alignment of type
123     size_t   talign() { return tsize(); }
124     /// returns the size of a type with the current TypeInfo
125     size_t   tsize();
126     /// swaps the two types stored at p1 and p2
127     void     swap(void *p1, void *p2);
128     /// "next" TypeInfo (for an array its elements, for a pointer what it is pointed to,...)
129     TypeInfo next();
130     void[]   init();
131     /// flags, 1: has possible pointers into GC memory
132     uint     flags();
133     PointerMap pointermap();
134     /// offsets of the various elements
135     OffsetTypeInfo[] offTi();
136
137     /** Return internal info on arguments fitting into 8byte.
138        * See X86-64 ABI 3.2.3
139      */
140     version (X86_64) int argTypes(out TypeInfo arg1, out TypeInfo arg2);
141 }
142
143 class TypeInfo_Typedef : TypeInfo
144 {
145     TypeInfo base;
146     char[]   name;
147     void[]   m_init;
148 }
149
150 class TypeInfo_Enum : TypeInfo_Typedef
151 {
152 }
153
154 class TypeInfo_Pointer : TypeInfo
155 {
156     TypeInfo m_next;
157 }
158
159 class TypeInfo_Array : TypeInfo
160 {
161     /// typeinfo of the elements, might be null for basic arrays, it is safer to use next()
162     TypeInfo value;
163
164     //ensure derived array TypeInfos use correct method
165     //if this declaration is forgotten, e.g. TypeInfo_Ai will have a wrong vtbl entry
166     PointerMap pointermap();
167 }
168
169 class TypeInfo_StaticArray : TypeInfo
170 {
171     TypeInfo value;
172     size_t   len;
173 }
174
175 class TypeInfo_AssociativeArray : TypeInfo
176 {
177     TypeInfo value;
178     TypeInfo key;
179 }
180
181 class TypeInfo_Function : TypeInfo
182 {
183     TypeInfo next;
184 }
185
186 class TypeInfo_Delegate : TypeInfo
187 {
188     TypeInfo next;
189 }
190
191 class TypeInfo_Class : TypeInfo
192 {
193     ClassInfo info;
194 }
195
196 class TypeInfo_Interface : TypeInfo
197 {
198     ClassInfo info;
199 }
200
201 class TypeInfo_Struct : TypeInfo
202 {
203     char[] name;
204     void[] m_init;
205
206     hash_t function()   xtoHash;
207     int function(void*) xopEquals;
208     int function(void*) xopCmp;
209     char[] function()   xtoString;
210
211     uint m_flags;
212
213     version (D_HavePointerMap) {
214         PointerMap m_pointermap;
215     }
216 }
217
218 class TypeInfo_Tuple : TypeInfo
219 {
220     TypeInfo[]  elements;
221 }
222
223 /// information about a module (can be used for example to get its unittests)
224 class ModuleInfo
225 {
226     /// name of the module
227     char[]          name;
228     ///
229     ModuleInfo[]    importedModules;
230     ///
231     ClassInfo[]     localClasses;
232     uint            flags;
233
234     void function() ctor;
235     void function() dtor;
236     /// unit tests of the module
237     void function() unitTest;
238
239     version(GNU){}
240     else{
241         void* xgetMembers;
242         void function() ictor;
243     }
244
245     /// loops on all the modules loaded
246     static int opApply( int delegate( ref ModuleInfo ) );
247 }
248
249 /// base class for all exceptions/errors
250 /// it is a good practice to pass line and file to the exception, which can be obtained with
251 /// __FILE__ and __LINE__, and then passed to the exception constructor
252 class Exception : Object
253 {
254     /// Information about a frame in the stack
255     struct FrameInfo{
256         /// line number in the source of the most likely start adress (0 if not available)
257         long line;
258         /// number of the stack frame (starting at 0 for the top frame)
259         ptrdiff_t iframe;
260         /// offset from baseSymb: within the function, or from the closest symbol
261         ptrdiff_t offsetSymb;
262         /// adress of the symbol in this execution
263         size_t baseSymb;
264         /// offset within the image (from this you can use better methods to get line number
265         /// a posteriory)
266         ptrdiff_t offsetImg;
267         /// base adress of the image (will be dependent on randomization schemes)
268         size_t baseImg;
269         /// adress of the function, or at which the ipc will return
270         /// (which most likely is the one after the adress where it started)
271         /// this is the raw adress returned by the backtracing function
272         size_t address;
273         /// file (image) of the current adress
274         char[] file;
275         /// name of the function, if possible demangled
276         char[] func;
277         /// extra information (for example calling arguments)
278         char[] extra;
279         /// if the address is exact or it is the return address
280         bool exactAddress;
281         /// if this function is an internal functions (for example the backtracing function itself)
282         /// if true by default the frame is not printed
283         bool internalFunction;
284         alias void function(FrameInfo*,void delegate(char[])) FramePrintHandler;
285         /// the default printing function
286         static FramePrintHandler defaultFramePrintingFunction;
287         /// writes out the current frame info
288         void writeOut(void delegate(char[])sink);
289         /// clears the frame information stored
290         void clear();
291     }
292     /// trace information has the following interface
293     interface TraceInfo
294     {
295         int opApply( int delegate( ref FrameInfo fInfo) );
296         void writeOut(void delegate(char[])sink);
297     }
298     /// message of the exception
299     char[]      msg;
300     /// file name
301     char[]      file;
302     /// line number
303     size_t      line;  // long would be better to be consistent
304     /// trace of where the exception was raised
305     TraceInfo   info;
306     /// next exception (if an exception made an other exception raise)
307     Exception   next;
308
309     /// designated constructor (breakpoint this if you want to catch all explict Exception creations,
310     /// special exception just allocate and init the structure directly)
311     this(char[] msg, char[] file, long line, Exception next, TraceInfo info );
312     this(char[] msg, Exception next=null);
313     this(char[] msg, char[] file, long line, Exception next = null);
314     /// returns the message of the exception, should not be used (because it should not allocate,
315     /// and thus only a small message is returned)
316     char[] toString();
317     /// writes out the message of the exception, by default writes toString
318     /// override this is you have a better message for the exception
319     void writeOutMsg(void delegate(char[]) sink);
320     /// writes out the exception message, file, line number, stacktrace (if available) and any
321     /// subexceptions
322     void writeOut(void delegate(char[]) sink);
323 }
Note: See TracBrowser for help on using the browser.