X-Git-Url: https://git.llucax.com/software/druntime.git/blobdiff_plain/1f3a9af059166187064490d1cae80bd11fb8737c..3d4bc628056638dd5b264e8315b5e7b7fcf579e8:/src/compiler/dmd/object_.d?ds=sidebyside diff --git a/src/compiler/dmd/object_.d b/src/compiler/dmd/object_.d index 0ffa9fb..352e995 100644 --- a/src/compiler/dmd/object_.d +++ b/src/compiler/dmd/object_.d @@ -54,7 +54,7 @@ private //alias typeof(int.sizeof) size_t; //alias typeof(cast(void*)0 - cast(void*)0) ptrdiff_t; -version( X86_64 ) +version(X86_64) { alias ulong size_t; alias long ptrdiff_t; @@ -66,11 +66,11 @@ else } alias size_t hash_t; -alias int equals_t; +alias bool equals_t; -alias char[] string; -alias wchar[] wstring; -alias dchar[] dstring; +alias invariant(char)[] string; +alias invariant(wchar)[] wstring; +alias invariant(dchar)[] dstring; /** * All D class objects inherit from Object. @@ -125,6 +125,23 @@ class Object void lock(); void unlock(); } + + /** + * Create instance of class specified by classname. + * The class must either have no constructors or have + * a default constructor. + * Returns: + * null if failed + */ + static Object factory(string classname) + { + auto ci = ClassInfo.find(classname); + if (ci) + { + return ci.create(); + } + return null; + } } /** @@ -160,9 +177,11 @@ class ClassInfo : Object // 2: // has no possible pointers into GC memory // 4: // has offTi[] member // 8: // has constructors + // 16: // has xgetMembers member void* deallocator; OffsetTypeInfo[] offTi; void function(Object) defaultConstructor; // default Constructor + const(MemberInfo[]) function(in char[]) xgetMembers; /** * Search all modules for ClassInfo corresponding to classname. @@ -197,6 +216,17 @@ class ClassInfo : Object } return o; } + + /** + * Search for all members with the name 'name'. + * If name[] is null, return all members. + */ + const(MemberInfo[]) getMembers(in char[] name) + { + if (flags & 16 && xgetMembers) + return xgetMembers(name); + return null; + } } /** @@ -217,7 +247,8 @@ struct OffsetTypeInfo class TypeInfo { override hash_t toHash() - { hash_t hash; + { + hash_t hash; foreach (char c; this.toString()) hash = hash * 9 + c; @@ -282,6 +313,10 @@ class TypeInfo /// Get type information on the contents of the type; null if not available OffsetTypeInfo[] offTi() { return null; } + /// Run the destructor on the object and all its sub-objects + void destroy(void* p) {} + /// Run the postblit on the object and all its sub-objects + void postblit(void* p) {} } class TypeInfo_Typedef : TypeInfo @@ -526,6 +561,27 @@ class TypeInfo_StaticArray : TypeInfo override TypeInfo next() { return value; } override uint flags() { return value.flags(); } + override void destroy(void* p) + { + auto sz = value.tsize(); + p += sz * len; + foreach (i; 0 .. len) + { + p -= sz; + value.destroy(p); + } + } + + override void postblit(void* p) + { + auto sz = value.tsize(); + foreach (i; 0 .. len) + { + value.postblit(p); + p += sz; + } + } + TypeInfo value; size_t len; } @@ -768,7 +824,7 @@ class TypeInfo_Struct : TypeInfo // A sorry hash algorithm. // Should use the one for strings. // BUG: relies on the GC not moving objects - auto q = cast(ubyte*)p; + auto q = cast(const(ubyte)*)p; for (size_t i = 0; i < init.length; i++) { h = h * 9 + *q; @@ -821,6 +877,18 @@ class TypeInfo_Struct : TypeInfo override uint flags() { return m_flags; } + override void destroy(void* p) + { + if (xdtor) + (*xdtor)(p); + } + + override void postblit(void* p) + { + if (xpostblit) + (*xpostblit)(p); + } + string name; void[] m_init; // initializer; init.ptr == null if 0 initialize @@ -830,6 +898,10 @@ class TypeInfo_Struct : TypeInfo char[] function(in void*) xtoString; uint m_flags; + + const(MemberInfo[]) function(in char[]) xgetMembers; + void function(void*) xdtor; + void function(void*) xpostblit; } class TypeInfo_Tuple : TypeInfo @@ -891,35 +963,118 @@ class TypeInfo_Tuple : TypeInfo { assert(0); } + + override void destroy(void* p) + { + assert(0); + } + + override void postblit(void* p) + { + assert(0); + } +} + +class TypeInfo_Const : TypeInfo +{ + override string toString() + { + return cast(string) ("const(" ~ base.toString() ~ ")"); + } + + override equals_t opEquals(Object o) { return base.opEquals(o); } + override hash_t getHash(in void *p) { return base.getHash(p); } + override equals_t equals(in void *p1, in void *p2) { return base.equals(p1, p2); } + override int compare(in void *p1, in void *p2) { return base.compare(p1, p2); } + override size_t tsize() { return base.tsize(); } + override void swap(void *p1, void *p2) { return base.swap(p1, p2); } + + override TypeInfo next() { return base.next(); } + override uint flags() { return base.flags(); } + override void[] init() { return base.init(); } + + TypeInfo base; +} + +class TypeInfo_Invariant : TypeInfo_Const +{ + override string toString() + { + return cast(string) ("invariant(" ~ base.toString() ~ ")"); + } +} + +abstract class MemberInfo +{ + string name(); +} + +class MemberInfo_field : MemberInfo +{ + this(string name, TypeInfo ti, size_t offset) + { + m_name = name; + m_typeinfo = ti; + m_offset = offset; + } + + override string name() { return m_name; } + TypeInfo typeInfo() { return m_typeinfo; } + size_t offset() { return m_offset; } + + string m_name; + TypeInfo m_typeinfo; + size_t m_offset; +} + +class MemberInfo_function : MemberInfo +{ + this(string name, TypeInfo ti, void* fp, uint flags) + { + m_name = name; + m_typeinfo = ti; + m_fp = fp; + m_flags = flags; + } + + override string name() { return m_name; } + TypeInfo typeInfo() { return m_typeinfo; } + void* fp() { return m_fp; } + uint flags() { return m_flags; } + + string m_name; + TypeInfo m_typeinfo; + void* m_fp; + uint m_flags; } /////////////////////////////////////////////////////////////////////////////// -// Exception +// Throwable /////////////////////////////////////////////////////////////////////////////// -class Exception : Object +class Throwable : Object { interface TraceInfo { - int opApply( int delegate(inout char[]) ); + int opApply(int delegate(inout char[])); } string msg; string file; size_t line; TraceInfo info; - Exception next; + Throwable next; - this( string msg, Exception next = null ) + this(string msg, Throwable next = null) { this.msg = msg; this.next = next; this.info = traceContext(); } - this( string msg, string file, size_t line, Exception next = null ) + this(string msg, string file, size_t line, Throwable next = null) { this(msg, next); this.file = file; @@ -929,12 +1084,34 @@ class Exception : Object override string toString() { - return msg; + char[10] tmp = void; + char[] buf; + + for (Throwable e = this; e !is null; e = e.next) + { + if (e.file) + { + buf ~= e.classinfo.name ~ "@" ~ e.file ~ "(" ~ tmp.intToString(e.line) ~ "): " ~ e.msg; + } + else + { + buf ~= e.classinfo.name ~ ": " ~ e.msg; + } + if (e.info) + { + buf ~= "\n----------------"; + foreach (t; e.info) + buf ~= "\n" ~ t; + } + if (e.next) + buf ~= "\n"; + } + return cast(string) buf; } } -alias Exception.TraceInfo function( void* ptr = null ) TraceHandler; +alias Throwable.TraceInfo function(void* ptr = null) TraceHandler; private TraceHandler traceHandler = null; @@ -944,14 +1121,14 @@ private TraceHandler traceHandler = null; * Params: * h = The new trace handler. Set to null to use the default handler. */ -extern (C) void rt_setTraceHandler( TraceHandler h ) +extern (C) void rt_setTraceHandler(TraceHandler h) { traceHandler = h; } /** - * This function will be called when an Exception is constructed. The + * This function will be called when an exception is constructed. The * user-supplied trace handler will be called if one has been supplied, * otherwise no trace will be generated. * @@ -964,11 +1141,39 @@ extern (C) void rt_setTraceHandler( TraceHandler h ) * An object describing the current calling context or null if no handler is * supplied. */ -Exception.TraceInfo traceContext( void* ptr = null ) +Throwable.TraceInfo traceContext(void* ptr = null) { - if( traceHandler is null ) + if (traceHandler is null) return null; - return traceHandler( ptr ); + return traceHandler(ptr); +} + + +class Exception : Throwable +{ + this(string msg, Throwable next = null) + { + super(msg, next); + } + + this(string msg, string file, size_t line, Throwable next = null) + { + super(msg, file, line, next); + } +} + + +class Error : Throwable +{ + this(string msg, Throwable next = null) + { + super(msg, next); + } + + this(string msg, string file, size_t line, Throwable next = null) + { + super(msg, file, line, next); + } } @@ -1002,14 +1207,14 @@ class ModuleInfo void function() ictor; // module static constructor (order independent) - static int opApply( int delegate(inout ModuleInfo) dg ) + static int opApply(int delegate(inout ModuleInfo) dg) { int ret = 0; - foreach( m; _moduleinfo_array ) + foreach (m; _moduleinfo_array) { - ret = dg( m ); - if( ret ) + ret = dg(m); + if (ret) break; } return ret; @@ -1107,7 +1312,7 @@ void _moduleCtor2(ModuleInfo[] mi, int skip) if (m.flags & MIctorstart) { if (skip || m.flags & MIstandalone) continue; - throw new Exception( "Cyclic dependency in module " ~ m.name ); + throw new Exception("Cyclic dependency in module " ~ m.name); } m.flags |= MIctorstart;