ソースを参照

WIP selective reflection.

Mark Sibly 7 年 前
コミット
ba6724b8af

+ 9 - 2
src/mx2cc/builder.monkey2

@@ -29,7 +29,7 @@ Class BuildOpts
 	
 	Field wholeArchive:Bool
 	
-	Field reflection:Bool
+'	Field reflection:Bool
 	
 	Field makedocs:bool
 	
@@ -243,6 +243,13 @@ Class BuilderInstance
 			
 			currentDir=cd
 			
+			For Local ref:=Eachin fdecl.reflects
+				
+				ref="#define BB_R_"+ref.Replace( "_","_0" ).Replace( ".","_" )+" 1"
+				
+				product.reflects.Push( ref )
+			Next
+			
 		Forever
 	
 	End
@@ -288,7 +295,7 @@ Class BuilderInstance
 			
 			If module<>mainModule 
 				product.imports.Push( module )
-				If module.name="reflection" opts.reflection=True
+'				If module.name="reflection" opts.reflection=True
 			Endif
 		Next
 		

+ 19 - 12
src/mx2cc/buildproduct.monkey2

@@ -23,6 +23,8 @@ Class BuildProduct
 	
 	Field ASSET_FILES:=New StringStack
 	
+	Field reflects:=New StringStack
+	
 	Method New( module:Module,opts:BuildOpts )
 		Self.module=module
 		Self.opts=opts
@@ -33,33 +35,37 @@ Class BuildProduct
 		copts+=" -I~q"+MODULES_DIR+"~q"
 		copts+=" -I~q"+MODULES_DIR+"monkey/native~q"
 		If APP_DIR copts+=" -I~q"+APP_DIR+"~q"
-			
+
 		CC_OPTS+=copts
 		CPP_OPTS+=copts
 	End
 
 	Method Build()
 		
-		If Not CreateDir( module.cacheDir ) Throw New BuildEx( "Error creating dir '"+module.cacheDir+"'" )
-
-		If opts.reflection
-			CC_OPTS+=" -DBB_REFLECTION"
-			CPP_OPTS+=" -DBB_REFLECTION"
-		Endif
-
 		If opts.verbose=0 Print "Compiling..."
 		
+		If Not CreateDir( module.cacheDir ) Throw New BuildEx( "Error creating dir '"+module.cacheDir+"'" )
+			
 		Local srcs:=New StringStack
 
-		If opts.productType="app"
-		
+		If opts.productType="app" And Not reflects.Empty
+
+			CC_OPTS+=" -DBB_NEWREFLECTION"
+			CPP_OPTS+=" -DBB_NEWREFLECTION"			
+			
+			CC_OPTS+=" -I~q"+module.cacheDir+"~q"
+			CPP_OPTS+=" -I~q"+module.cacheDir+"~q"
+			
+			Local rhead:="//REFLECT:~n"+reflects.Join( "~n" )
+			
+			CSaveString( rhead,module.cacheDir+"_r.h" )
+			
 			srcs.Push( module.rfile )
 			
 			For Local imp:=Eachin imports
 			
 				srcs.Push( imp.rfile )
 			Next
-			
 		Endif
 		
 		For Local fdecl:=Eachin module.fileDecls
@@ -334,7 +340,8 @@ Class GccBuildProduct Extends BuildProduct
 
 '		Local obj:=module.cacheDir+MungPath( MakeRelativePath( src,module.cacheDir ) )
 		Local obj:=module.cacheDir+MungPath( MakeRelativePath( src,module.cfileDir ) )
-		If rfile And opts.reflection obj+="_r"
+'		If rfile And opts.reflection obj+="_r"
+		If rfile obj+="_r"
 			
 		obj+=toolchain="msvc" ? ".obj" Else ".o"
 	

+ 1 - 0
src/mx2cc/decl.monkey2

@@ -108,6 +108,7 @@ Class FileDecl Extends Decl
 	Field nmspace:String
 	Field usings:String[]
 	Field imports:String[]
+	Field reflects:String[]
 	Field errors:ParseEx[]
 
 	Field module:Module	

+ 5 - 2
src/mx2cc/parser.monkey2

@@ -46,6 +46,7 @@ Class Parser
 		
 		_fdecl.usings=_usings.ToArray()
 		_fdecl.imports=_imports.ToArray()
+		_fdecl.reflects=_reflects.ToArray()
 		_fdecl.errors=_errors.ToArray()
 		_fdecl.endpos=EndPos
 		
@@ -2294,8 +2295,6 @@ Class Parser
 						
 					path+="["+SrcPos+"]"
 						
-					'Print "path="+path
-					
 					_imports.Push( path )
 				Endif
 				
@@ -2308,6 +2307,10 @@ Class Parser
 					Local path:=p.ParseIdent()
 					
 					While p.CParse( "." )
+						If p.CParse( "." )
+							path+=".."
+							Exit
+						Endif
 						path+="."+p.ParseIdent()
 					Wend
 					

+ 21 - 3
src/mx2cc/test.monkey2

@@ -1,12 +1,30 @@
 
-#Reflect mojo.app
-#Reflect mojo.graphics
+Namespace std.test
 
-#Import "where.monkey2"
+#Import "<std>"
+
+#Reflect std.graphics
+
+Using std..
+
+Function F()
+
+End
+
+Class C 'Extends Resource
+	
+End
 
 Function Main()
 
 	Print "Hello World"
 	
+'	Local socket:Socket
+	
+	Local ti:=Typeof<Pixmap>
+	'Local ti:=Typeof<C>
+	
+	Print ti
+	
 End
 	

+ 1 - 1
src/mx2cc/translator.monkey2

@@ -339,7 +339,7 @@ Class Translator
 				Emit( "struct "+cname+";" )
 				
 				If GenTypeInfo( ctype ) 
-					Emit( "#ifdef BB_REFLECTION" )
+					Emit( "#ifdef BB_NEWREFLECTION" )
 					If ctype.IsStruct 
 						Emit( "bbTypeInfo *bbGetType( "+cname+" const& );" )
 					Else

+ 84 - 42
src/mx2cc/translator_cpp.monkey2

@@ -45,72 +45,114 @@ Class Translator_CPP Extends Translator
 		Reset()
 	
 		Emit( "#include <bbmonkey.h>" )
+		Emit( "#if BB_NEWREFLECTION" )
+		Emit( "#include ~q_r.h~q" )
+		Emit( "#endif" )
 		
 		EmitBr()
 		
-		Emit( "#ifdef BB_REFLECTION" )
-		
-		BeginDeps()
+		Local nmspaces:=New StringMap<Stack<FileDecl>>
 		
 		For Local fdecl:=Eachin _module.fileDecls
-		
-			EmitTypeInfo( fdecl )
-	
-			For Local ctype:=Eachin fdecl.classes
-			
-				If Not GenTypeInfo( ctype ) Continue
-				
-				EmitTypeInfo( ctype )
-				
-			Next
-		
+			Local nmspace:=fdecl.nmspace
+			Local fdecls:=nmspaces[fdecl.nmspace]
+			If Not fdecls
+				fdecls=New Stack<FileDecl>
+				nmspaces[fdecl.nmspace]=fdecls
+			Endif
+			fdecls.Add( fdecl )
 		Next
 		
-		EndDeps( ExtractDir( _module.rfile ) )
-		
-		Emit( "#else" )
-		
 		BeginDeps()
-
-		For Local fdecl:=Eachin _module.fileDecls
 		
-			For Local ctype:=Eachin fdecl.classes
+		For Local it:=eachin nmspaces
 			
-				If Not GenTypeInfo( ctype ) Continue
-				
-				Uses( ctype )
-				
-				Local cname:=ClassName( ctype )
-				Local rname:=""
-				
-				If ctype.IsStruct
-					Emit( "bbTypeInfo *bbGetType("+cname+" const&){" )
-					rname="bbVoidTypeInfo"
+			Local fdecls:=it.Value
+			
+			Local nmpath:=it.Key,rcc:=""
+			
+			Repeat
+				Local mpath:="BB_R_"+nmpath.Replace( "_","_0" ).Replace( ".","_" )
+				If rcc
+					rcc+=" || "+mpath+"__"
 				Else
-					Emit( "bbTypeInfo *bbGetType("+cname+"* const&){" )
-					rname="bbObjectTypeInfo"
+					rcc=mpath+" || "+mpath+"__"
 				Endif
-								
-				Emit( "return &"+rname+"::instance;" )
-				Emit( "}" )
+				Local i:=nmpath.FindLast( "." )
+				If i=-1 Exit
+				nmpath=nmpath.Slice( 0,i )
+			Forever
+			
+			Emit( "#if "+rcc )
+			
+			For Local fdecl:=Eachin fdecls
+		
+				EmitTypeInfo( fdecl )
+		
+				For Local ctype:=Eachin fdecl.classes
 				
-				Emit( "bbTypeInfo *"+cname+"::typeof()const{" )
-				Emit( "return &"+rname+"::instance;" )
-				Emit( "}" )
+					If Not GenTypeInfo( ctype ) Continue
+					
+					EmitTypeInfo( ctype )
+					
+				Next
 				
 			Next
+			
+			Emit( "#else" )
+			
+			For Local fdecl:=Eachin fdecls
+		
+				EmitNullTypeInfo( fdecl )
+		
+				For Local ctype:=Eachin fdecl.classes
+				
+					If Not GenTypeInfo( ctype ) Continue
+					
+					EmitNullTypeInfo( ctype )
+					
+				Next
+				
+			Next
+
+			Emit( "#endif" )
 		
 		Next
 		
 		EndDeps( ExtractDir( _module.rfile ) )
 		
-		Emit( "#endif" )
-		
 		Local src:=_buf.Join( "~n" )
 		
 		CSaveString( src,_module.rfile )
 	End
 	
+	Method EmitNullTypeInfo( fdecl:FileDecl )
+		
+	End
+	
+	Method EmitNullTypeInfo( ctype:ClassType )
+
+		Local cname:=ClassName( ctype )
+		Local rname:=""
+		
+		If ctype.IsStruct
+			Emit( "bbTypeInfo *bbGetType("+cname+" const&){" )
+			rname="bbVoidTypeInfo"
+		Else
+			Emit( "bbTypeInfo *bbGetType("+cname+"* const&){" )
+			rname="bbObjectTypeInfo"
+		Endif
+						
+		Emit( "return &"+rname+"::instance;" )
+		Emit( "}" )
+		
+		Emit( "bbTypeInfo *"+cname+"::typeof()const{" )
+		Emit( "return &"+rname+"::instance;" )
+		Emit( "}" )
+		
+	End
+	
+	
 	Method TranslateFile( fdecl:FileDecl )
 	
 		Reset()
@@ -2061,7 +2103,7 @@ Class Translator_CPP Extends Translator
 	End
 	
 	Method Trans:String( value:TypeofTypeValue )
-	
+		
 		Refs( value.ttype )
 	
 		Return "bbGetType<"+TransType( value.ttype )+">()"