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

root/tags/releases/0.99.9/object.di

Revision 5285, 8.6 kB (checked in by kris, 2 years ago)

fixes #1822 :: replace inout with ref

thanks to mwarning

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