Browse Source

Added Interface support to reflection.

Mark Sibly 8 years ago
parent
commit
12a7d3254d

+ 25 - 2
modules/monkey/native/bbtypeinfo.cpp

@@ -43,13 +43,18 @@ bbTypeInfo *bbTypeInfo::superType(){
 	return 0;
 }
 	
+bbArray<bbTypeInfo*> bbTypeInfo::interfaceTypes(){
+	bbRuntimeError( "Type '"+name+"' is not a class or interface type" );
+	return {};
+}
+	
 bbBool bbTypeInfo::extendsType( bbTypeInfo *type ){
-	bbRuntimeError( "Type '"+name+"' is not a class type" );
+	bbRuntimeError( "Type '"+name+"' is not a class or interface type" );
 	return false;
 }
 	
 bbArray<bbDeclInfo*> bbTypeInfo::getDecls(){
-	bbRuntimeError( "Type '"+name+"' is not a class type" );
+	bbRuntimeError( "Type '"+name+"' is not a class or interface type" );
 	return {};
 }
 
@@ -209,9 +214,27 @@ bbClassTypeInfo::bbClassTypeInfo( bbString name,bbString kind ){
 	_classes=this;
 }
 
+bbTypeInfo *bbClassTypeInfo::superType(){
+	return 0;
+}
+
+bbArray<bbTypeInfo*> bbClassTypeInfo::interfaceTypes(){
+	return {};
+}
+
 bbBool bbClassTypeInfo::extendsType( bbTypeInfo *type ){
+
 	if( type==this ) return true;
+	
+	bbArray<bbTypeInfo*> ifaces=interfaceTypes();
+	
+	for( int i=0;i<ifaces.length();++i ){
+
+		if( ifaces[i]->extendsType( type ) ) return true;
+	}
+	
 	if( bbTypeInfo *super=superType() ) return super->extendsType( type );
+	
 	return false;
 }
 

+ 6 - 0
modules/monkey/native/bbtypeinfo.h

@@ -36,6 +36,8 @@ struct bbTypeInfo{
 	
 	virtual bbTypeInfo *superType();
 	
+	virtual bbArray<bbTypeInfo*> interfaceTypes();
+	
 	virtual bbBool extendsType( bbTypeInfo *type );
 	
 	virtual bbArray<bbDeclInfo*> getDecls();
@@ -169,6 +171,10 @@ struct bbClassTypeInfo : public bbTypeInfo{
 	
 	bbClassTypeInfo( bbString name,bbString kind );
 	
+	bbTypeInfo *superType();
+	
+	bbArray<bbTypeInfo*> interfaceTypes();
+	
 	bbBool extendsType( bbTypeInfo *type );
 	
 	bbArray<bbDeclInfo*> getDecls();

+ 11 - 1
modules/monkey/types.monkey2

@@ -474,7 +474,7 @@ Class @TypeInfo Extends Void="bbTypeInfo"
 	#end
 	Property ParamTypes:TypeInfo[]()="paramTypes"
 	
-	#rem monkeydoc class super type.
+	#rem monkeydoc Class super type.
 	
 	If the type is a class type, returns the type of the super class.
 	
@@ -483,6 +483,16 @@ Class @TypeInfo Extends Void="bbTypeInfo"
 	#end
 	Property SuperType:TypeInfo()="superType"
 	
+	
+	#rem monkeydoc Implemented interfaces.
+	
+	The interfaces implemented by the class or interface type.
+
+	If the type is not a class or interface type a runtime exception occurs.
+	
+	#end
+	Property InterfaceTypes:TypeInfo[]()="interfaceTypes"
+	
 	#rem monkeydoc Gets string representation of type.
 	#end
 	Method To:String()="toString"

+ 18 - 8
src/mx2cc/translator_cpp.monkey2

@@ -774,7 +774,7 @@ Class Translator_CPP Extends Translator
 		
 		Local decls:=New StringStack
 		
-		If Not ctype.IsAbstract
+		If ctype.IsClass And Not ctype.IsAbstract
 			If ctype.ctors.Length
 				For Local ctor:=Eachin ctype.ctors
 					Local args:=cname
@@ -842,14 +842,24 @@ Class Translator_CPP Extends Translator
 		Emit( rcname+"():bbClassTypeInfo(~q"+ctype.Name+"~q,~q"+cdecl.kind.Capitalize()+"~q){" )
 		Emit( "}" )
 
-		'superType		
-		Emit( "bbTypeInfo *superType(){" )
+		'superType
 		If ctype.superType
+			Emit( "bbTypeInfo *superType(){" )
 			Emit( "return bbGetType<"+ClassName( ctype.superType )+"*>();" )
-		Else
-			Emit( "return 0;" )
-		Endif		
-		Emit( "}" )
+			Emit( "}" )
+		Endif
+		
+		'interfaceTypes
+		If ctype.ifaceTypes
+			Emit( "bbArray<bbTypeInfo*> interfaceTypes(){" )
+			Local args:=""
+			For Local ty:=Eachin ctype.ifaceTypes
+				If args args+=","
+				args+="bbGetType<"+ClassName( ty )+"*>()"
+			Next
+			Emit( "return bbArray<bbTypeInfo*>({"+args+"},"+ctype.ifaceTypes.Length+");" )
+			Emit( "}" )
+		Endif
 		
 		Emit( "};" )
 		
@@ -1887,7 +1897,7 @@ End
 
 Function GenTypeInfo:Bool( ctype:ClassType )
 
-	If Not ctype.IsClass Return False
+	If ctype.IsStruct Return False
 
 	'This 'sort of' works, but no generics just yet!
 	If ctype.types Or ctype.scope.IsInstanceOf Return False