Parcourir la source

Fixes for new reflection stuff.

Mark Sibly il y a 7 ans
Parent
commit
7852e05773

+ 4 - 3
src/mx2cc/builder.monkey2

@@ -33,8 +33,6 @@ Class BuildOpts
 	
 	Field wholeArchive:Bool
 	
-'	Field reflection:Bool
-	
 	Field makedocs:bool
 	
 End
@@ -242,12 +240,16 @@ Class BuilderInstance
 			
 			currentDir=cd
 			
+			product.reflects.AddAll( fdecl.reflects )
+			
+			#rem
 			For Local ref:=Eachin fdecl.reflects
 				
 				ref="#define BB_R_"+ref.Replace( "_","_0" ).Replace( ".","_" )+" 1"
 				
 				product.reflects.Push( ref )
 			Next
+			#end
 			
 		Forever
 	
@@ -294,7 +296,6 @@ Class BuilderInstance
 			
 			If module<>mainModule 
 				product.imports.Push( module )
-'				If module.name="reflection" opts.reflection=True
 			Endif
 		Next
 		

+ 12 - 2
src/mx2cc/buildproduct.monkey2

@@ -47,7 +47,7 @@ Class BuildProduct
 			
 		Local srcs:=New StringStack
 
-		If opts.productType="app" 'And Not reflects.Empty
+		If opts.productType="app"
 
 			CC_OPTS+=" -DBB_NEWREFLECTION"
 			CPP_OPTS+=" -DBB_NEWREFLECTION"			
@@ -55,7 +55,17 @@ Class BuildProduct
 			CC_OPTS+=" -I~q"+module.cacheDir+"~q"
 			CPP_OPTS+=" -I~q"+module.cacheDir+"~q"
 			
-			Local rhead:="//REFLECT:~n"+reflects.Join( "~n" )
+			reflects.Sort()
+			
+			Local buf:=New StringStack
+			buf.Add( "//REFLECT" )
+
+			For Local ref:=Eachin reflects
+				ref="#define BB_R_"+ref.Replace( "_","_0" ).Replace( ".","_" )+" 1"
+				buf.Add( ref )
+			End
+			
+			Local rhead:=buf.Join( "~n" )
 			
 			CSaveString( rhead,module.cacheDir+"_r.h" )
 			

+ 42 - 64
src/mx2cc/test.monkey2

@@ -1,92 +1,70 @@
 
 Namespace test
 
+#Import "<reflection>"
+
+#Import "<mojo3d>"
+#Import "<std>"
+
+Using reflection..
+
+Using std..
+
+#Reflect mojo3d
+#Reflect std
+
 #Reflect test
 
-Function InvokeMethod:Variant( name:String,instance:Object,args:Variant[]=Null )
+Class C
+End
+
+Class D Extends C
 	
-	If Not instance Return Null
+	Property Position:Vec3f()
 	
-	Local type:=instance.DynamicType
+		Return New Vec3f(1,2,3)
+	End
+End
+
+Class E
 	
-	While type
+	Field _c:C=New D
 	
-		For Local decl:=Eachin type.GetDecls()
-			
-			If decl.Kind<>"Method" Continue
-			
-			Local ptypes:=decl.Type.ParamTypes
-			If ptypes.Length<>args.Length Continue
-			
-			Local fail:=False
-			For Local j:=0 Until args.Length
-				If args[j].Type.ExtendsType( ptypes[j] ) Continue
-				fail=True
-				Exit
-			Next
-			If fail Continue
-			
-			Return decl.Invoke( instance,args)
-		Next
-		
-		type=type.SuperType
-		
-	Wend
+	Field _pos:=New Vec3f( 1,2,3 )
 	
-	'search extensions...
+	Method GetC:C()
+		
+		Return _c
+	End
 	
-	Return Null
 End
 
-Function PrintAllTypes()
+Class E Extension
 	
-	For Local type:=Eachin TypeInfo.GetTypes()
+	Property D:D()
 		
-		Print type
-		
-		For Local decl:=Eachin type.GetDecls()
-			
-			Print " "+decl
-		Next
-		
-	Next
+		Return Cast<D>( GetC() )
+	End
 	
-End
-
-Class C
-End
-
-Class C Extension
+	Property Position:Vec3f()
+		
+		Return _pos
 	
-	Method Update()
+	Setter( pos:Vec3f )
 		
-		Print "It worked!"
+		_pos=pos
 	End
 	
 End
 
 Function Main()
+
+	DebugTypes()
 	
-	Print "All types:"
-	
-	PrintAllTypes()
-	
-	Local c:=New C
+	Local e:=New E
 	
-	For Local type:=Eachin TypeInfo.GetTypes()
-		
-		'look for extension
-		If type.Kind="Class Extension" And type.SuperType=Typeof(c)
-			
-			Print "Found extension!"
-			
-			Local decl:=type.GetDecl( "Update" )
-			
-			decl.Invoke( c,Null )
-		Endif
+	SetProperty( "Position",e,New Vec3f( 4,5,6 ) )
 	
-	Next
+	Print GetProperty<Vec3f>( "Position",e )
 	
-	Print "Bye!"
 End
-		

+ 29 - 51
src/mx2cc/translator_cpp.monkey2

@@ -334,8 +334,13 @@ Class Translator_CPP Extends Translator
 
 		Local params:=""
 		If func.IsExtension
-			Local tself:=func.selfType.IsStruct ? "&l_self" Else "l_self"
+
+			'new self
+			Local tself:=func.selfType.IsStruct ? "*l_self" Else "l_self"
 			params=TransType( func.selfType )+" "+tself
+			
+'			Local tself:=func.selfType.IsStruct ? "&l_self" Else "l_self"
+'			params=TransType( func.selfType )+" "+tself
 		Endif
 
 		For Local p:=Eachin func.params
@@ -1020,8 +1025,6 @@ Class Translator_CPP Extends Translator
 			
 			Local id:=plist.pdecl.ident
 			Local meta:=plist.pdecl.meta ? ","+EnquoteCppString( plist.pdecl.meta ) Else ""
-'			Local get:=plist.getFunc ? "&"+cname+"::"+FuncName( plist.getFunc ) Else "0"
-'			Local set:=plist.setFunc ? "&"+cname+"::"+FuncName( plist.setFunc ) Else "0"
 
 			UsesRefInfo( plist.type )
 			
@@ -1066,9 +1069,10 @@ Class Translator_CPP Extends Translator
 			Local fname:=FuncName( func )
 			Local meta:=func.fdecl.meta ? ","+EnquoteCppString( func.fdecl.meta ) Else ""
 				
-			If func.fdecl.IsExtension
-			
-				Local cname:=ClassName( ctype.superType )
+			If func.IsExtension
+'			If func.fdecl.IsExtension
+
+				Local cname:=ClassName( func.selfType )
 						
 				Local args:=cname+","+TransType( func.ftype.retType )
 				For Local arg:=Eachin func.ftype.argTypes
@@ -1109,9 +1113,13 @@ Class Translator_CPP Extends Translator
 
 		'superType
 		If ctype.superType
+			
+			Local sname:=ClassName( ctype.superType )
+			If Not ctype.IsStruct sname+="*"
+			
 			UsesRefInfo( ctype.superType )
 			Emit( "bbTypeInfo *superType(){" )
-			Emit( "return bbGetType<"+ClassName( ctype.superType )+"*>();" )
+			Emit( "return bbGetType<"+sname+">();" )
 			Emit( "}" )
 		Endif
 		
@@ -1891,10 +1899,12 @@ Class Translator_CPP Extends Translator
 	
 	Method Trans:String( value:SelfValue )
 	
-		If value.func.IsExtension Return "l_self"
+		If value.func.IsExtension
+			If value.ctype.IsStruct Return "(*l_self)"
+			Return "l_self"
+		Endif
 		
 		If value.ctype.IsStruct Return "(*this)"
-		
 		Return "this"
 	End
 	
@@ -1936,6 +1946,7 @@ Class Translator_CPP Extends Translator
 			
 			If member.selfType.IsStruct
 				If Not instance.IsLValue tinst="("+AllocGCTmp( instance.type )+"="+tinst+")"
+				tinst="&"+tinst
 			Else
 				If IsVolatile( instance ) tinst="("+AllocGCTmp( instance.type )+"="+tinst+")"
 			Endif
@@ -2354,42 +2365,9 @@ Class Translator_CPP Extends Translator
 
 End
 
-#rem
-Function GenTypeInfo:Bool( type:Type )
-
-	If type=Type.VoidType Return true
-
-	If TCast<PrimType>( type ) Return True
-	
-	Local ctype:=TCast<ClassType>( type )
-	If ctype Return GenTypeInfo( ctype )
-
-	Local atype:=TCast<ArrayType>( type )
-	If atype Return GenTypeInfo( atype.elemType )
-	
-	Local ptype:=TCast<PointerType>( type )
-	If ptype Return GenTypeInfo( ptype.elemType )
-	
-	Local ftype:=TCast<FuncType>( type )
-	If ftype Return GenTypeInfo( ftype )
-	
-	Return False
-End
-
-Function GenTypeInfo:Bool( ftype:FuncType )
-
-	If Not GenTypeInfo( ftype.retType ) Return False
-	
-	For Local arg:=Eachin ftype.argTypes
-		If Not GenTypeInfo( arg ) Return False
-	Next
-	
-	Return True
-End
-#end
-
 Function GenTypeInfo:Bool( vvar:VarValue )
 
+	'sanity check
 	If vvar.vdecl.kind<>"field" And vvar.vdecl.kind="global" And vvar.vdecl.kind="const" Return False
 	
 	Return True
@@ -2397,23 +2375,23 @@ End
 
 Function GenTypeInfo:Bool( func:FuncValue )
 	
-	'disable generics (for now)
-	If func.types Return False
+	'sanity check
+	If func.fdecl.kind<>"method" And func.fdecl.kind<>"function" Return False
 	
-	'disable extensions
-	'If func.fdecl.IsExtension return false
+	'disable generic method instances (for now - almost working!)
+	If func.IsExtension Return func.fdecl.IsExtension
 	
 	Return True
 End
 
 Function GenTypeInfo:Bool( ctype:ClassType )
 
-	'disable generics (for now)
-	'If ctype.types return False
-
 	'disable native types
-	If ctype.ExtendsVoid Return False	'just in case
+	If ctype.ExtendsVoid Return False
 	
+	'disable generic type instances (for now - almost working!)
+	If ctype.types Return False
+
 	'disable structs
 	'If ctype.IsStruct Return False