Last update February 9, 2005

Reflection In D



In the last hour or so, I banged out some ideas for a reflection module. Much of this information could be gleaned from the D ABI (which you can glean by browsing object.d in Phobos) but I think it would be nice to package all of it up in a nice, clean, object-oriented reflection API.

These ideas are very very preliminary, but I thought they might make a good starting point for some discussion about what can and can't be done with the information currently available in the ABI.

Please feel free to contribute your own ideas to this skeletal API and to make changes to the stuff I've already mapped out.


TypeEnum?

	{TYPE PRIMITIVE}?, {TYPE ENUM}?, {TYPE STRUCT}?, {TYPE OBJECT}?, {TYPE ARRAY}?, {TYPE DELEGATE}?

PrimitiveEnum?
	{PRIM VOID}?, {PRIM BIT}?, {PRIM BYTE}?, {PRIM UBYTE}?, {PRIM SHORT}?, {PRIM USHORT}?, {PRIM INT}?,
	{PRIM UINT}?, {PRIM LONG}?, {PRIM ULONG}?, {PRIM CENT}?, {PRIM UCENT}?, {PRIM FLOAT}?, {PRIM DOUBLE}?,
	{PRIM REAL}?, {PRIM IFLOAT}?, {PRIM IDOUBLE}?, {PRIM IREAL}?, {PRIM CFLOAT}?, {PRIM CDOUBLE}?,
	{PRIM CREAL}?, {PRIM CHAR}?, {PRIM WCHAR}?, {PRIM DCHAR}?

AttributeEnum?
	{ATTRIB DEPRECATED}?, {ATTRIB PRIVATE}?, {ATTRIB PACKAGE}?, {ATTRIB PROTECTED}?, {ATTRIB PUBLIC}?,
	{ATTRIB EXPORT}?, {ATTRIB STATIC}?, {ATTRIB FINAL}?, {ATTRIB OVERRIDE}?, {ATTRIB ABSTRACT}?,
	{ATTRIB CONST}?, {ATTRIB AUTO}?

ReferenceEnum?
	{REFERENCE IN}?, {REFERENCE OUT}?, {REFERENCE INOUT}?

Reflection Methods
	static TypeInfo? Reflection.getTypeInfo(Primitive)
	static TypeInfo? Reflection.getTypeInfo(Enum)
	static TypeInfo? Reflection.getTypeInfo(Struct)
	static TypeInfo? Reflection.getTypeInfo(Object)
	static TypeInfo? Reflection.getTypeInfo(Array)
	static TypeInfo? Reflection.getTypeInfo(Delegate)

TypeInfo? Methods
	TypeEnum? getTypeIdentifier()
	PrimitiveInfo? getPrimitiveInfo()
	EnumInfo? getEnumInfo()
	StructInfo? getStructInfo()
	ObjectInfo? getObjectInfo()
	ArrayInfo? getArrayInfo()
	DelegateInfo? getDelegateInfo()

PrimitiveInfo? Methods
	int getSize()
	PrimitiveEnum? getType()

EnumInfo? Methods
	bool isAnonymous()
	PrimitiveEnum? getBaseType()
	// TODO: should these be templated?
	int[String] getEnumerationHash()
	uint[String] getEnumerationHash()
	long[String] getEnumerationHash()
	ulong[String] getEnumerationHash()
	cent[String] getEnumerationHash()
	ucent[String] getEnumerationHash()

StructInfo? Methods
	String[] getNames()
	TypeInfo?[] getTypes()

ObjectInfo? Methods
	ClassInfo? getClass()
	InterfaceInfo?[] getInterfaces()
	// excludes statics
	MemberInfo?[] getMembers()

ClassInfo? Methods
	ClassInfo? getSuperclass()
	ModuleInfo? getModule()
	MethodInfo?[] getConstructors()
	MethodInfo?[] getDestructors()
	InterfaceInfo?[] getInterfaces()
	MethodInfo?[] getMethods()
	MemberInfo?[] getStaticMembers()
	TypeInfo?[] getMemberTypes()
	String[] getMemberNames()

InterfaceInfo? Methods
	InterfaceInfo?[] getSuperinterfaces()
	MethodInfo?[] getMethods()

ModuleInfo? Methods
	ModuleInfo? getParentModule()
	String getFullyQualifiedModule()

MemberInfo? Methods
	AttributeEnum?[] getAttributes()
	TypeInfo? getType()

MethodInfo? Methods
	ClassInfo? getClass()
	AttributeEnum?[] getAttributes()
	String getName()
	// Should this be a function pointer? Or something else?
	Delegate getDelegate()
	TypeInfo? getReturnType()
	ReferenceEnum?[] getArgumentsInOut()
	TypeInfo?[] getArgumentTypes()
	String[] getArgumentNames()
	void call(Object returnObject, Object[] arguments)

I have no idea what to do with delegates, mixins, templates (including templatized classes), free functions, and unit tests. What do other people think about how these types of entities should be handled in a reflection API?


XsZero? @ 2005-02-09:

mixins can't be handled - they're inlined, right?

delegates can have a DelegateInfo? and return their Object reference (possibly null) and their MethodInfo?. For nested functions, I'd say just return the outer method -- you can't really access the inner ones anyway? OTOH, it should be possible to invoke the delegate via reflection, so that might not be the best idea; but holding a reference to such a delegate is a disaster waiting to happen, as it includes the stack pointer.. in any case, the type of delegate should also be determinable..

free functions (you mean those outside classes, right?) can be returned by the ModuleInfo?.

btw, call() in MethodInfo? should take another parameter - its this object (except for statics/globals).

the potential unit test can be returned as static MethodInfo? in ClassInfo? (with getUnitTest() or something returning null if it doesn't exist).

templates are the biggest problem; however, since only concrete template instances will exist anyway, my guess is they can just get their ClassInfo?. There can be isTemplateInstance() in ClassInfo? and MethodInfo? and perhaps also getTemplateParameters() to determine which types were used for it. Beyond that, I don't see anything useful that could be done with templates, because you can't somehow magically produce new template instantiations (it's all done at compile-time). Maybe, one could get getAllClassesOfSameTemplate() or something to get all the instantiations?

FrontPage | News | TestPage | MessageBoard | Search | Contributors | Folders | Index | Help | Preferences | Edit

Edit text of this page (date of last change: February 9, 2005 20:06 (diff))