瀏覽代碼

Fixed extern aliases.

Mark Sibly 7 年之前
父節點
當前提交
39ba24b94e
共有 5 個文件被更改,包括 95 次插入65 次删除
  1. 1 9
      src/mx2cc/mx2cc.monkey2
  2. 4 15
      src/mx2cc/parser.monkey2
  3. 13 18
      src/mx2cc/test.monkey2
  4. 21 7
      src/mx2cc/translator.monkey2
  5. 56 16
      src/mx2cc/translator_cpp.monkey2

+ 1 - 9
src/mx2cc/mx2cc.monkey2

@@ -22,17 +22,9 @@ Global opts_time:Bool
 
 Global StartDir:String
 
-Const TestArgs:="mx2cc makemods"
+Const TestArgs:="mx2cc makemods"' -clean mojo"
  
 'Const TestArgs:="mx2cc makeapp src/mx2cc/test.monkey2"
-'Const TestArgs:="mx2cc geninfo modules/mojo3d/mojo3d.monkey2"
-'Const TestArgs:="mx2cc geninfo src/ted2go/Ted2.monkey2"
-
-'Const TestArgs:="mx2cc makeapp -verbose src/mx2cc/catan/main.monkey2"
-
-'Const TestArgs:="mx2cc makeapp D:\Plane-Demo-master\Plane.monkey2"
-
-'Const TestArgs:="mx2cc makemods -target=ios"' monkey libc"
 
 Function Main()
 	

+ 4 - 15
src/mx2cc/parser.monkey2

@@ -243,21 +243,6 @@ Class Parser
 		Return decls.ToArray()
 	End
 	
-	#rem
-	Method CParseAccess:Int( flags:Int )
-	
-		Select Toke
-		Case "public" flags=flags & ~(DECL_ACCESSMASK) | DECL_PUBLIC
-		Case "private" flags=flags & ~(DECL_ACCESSMASK) | DECL_PRIVATE
-		Case "internal" flags=flags & ~(DECL_ACCESSMASK) | DECL_INTERNAL
-		Case "protected" flags=flags & ~(DECL_ACCESSMASK) | DECL_PROTECTED
-		Default Return flags
-		End
-		Bump()
-		Return flags
-	End
-	#end
-	
 	Method ParseAliases( decls:Stack<Decl>,flags:Int )
 	
 		Local kind:=Parse()
@@ -277,6 +262,10 @@ Class Parser
 				
 				Parse( ":" )
 				decl.type=ParseType()
+
+				If flags & DECL_EXTERN
+					If CParse( "=" ) decl.symbol=ParseString()
+				Endif
 				
 				decl.endpos=EndPos
 				decls.Push( decl )

+ 13 - 18
src/mx2cc/test.monkey2

@@ -1,28 +1,23 @@
 
-Namespace myapp
+Namespace test
 
-#reflect myapp
+#Import "<windows.h>"
 
-Class C
-End
+Extern
+
+Alias DWORD:UInt
+Alias LPDWORD:DWORD Ptr
 
-Class C Extension
-	Method Update()
-	End
+Public
+
+Function Test( p:LPDWORD )
+	
+	Print p[0]
 End
 
 Function Main()
 	
-	For Local type:=Eachin TypeInfo.GetTypes()
-		
-		Print type
-		
-		For Local decl:=eachin type.GetDecls()
-			
-			Print "  "+decl
-			
-		Next
-		
-	Next
+	Local t:DWORD=10
 	
+	Test( Varptr t )
 End

+ 21 - 7
src/mx2cc/translator.monkey2

@@ -669,13 +669,20 @@ Method AddRef:Bool( node:SNode )
 	
 	Method UsesType( type:Type )
 		
-		Local ctype:=TCast<ClassType>( type )
+		Local xtype:=Cast<AliasType>( type )
+		If xtype
+			If xtype.adecl.IsExtern UsesFile( xtype.transFile ) ; Return
+			UsesType( xtype._alias )
+			Return
+		Endif
+		
+		Local ctype:=Cast<ClassType>( type )
 		If ctype
 			UsesFile( ctype.transFile )
 			Return
 		Endif
 		
-		Local atype:=TCast<ArrayType>( type )
+		Local atype:=Cast<ArrayType>( type )
 		If atype
 			'would rather not have to use array element type too, but it's complicated...
 			UsesType( atype.elemType )
@@ -687,7 +694,14 @@ Method AddRef:Bool( node:SNode )
 	
 	Method RefsType( type:Type )
 		
-		Local ctype:=TCast<ClassType>( type )
+		Local xtype:=Cast<AliasType>( type )
+		If xtype
+			If xtype.adecl.IsExtern UsesFile( xtype.transFile ) ; Return
+			RefsType( xtype._alias )
+			Return
+		Endif
+		
+		Local ctype:=Cast<ClassType>( type )
 		If ctype
 			'Note: Have to include extern type definitons
 			If ctype.cdecl.IsExtern UsesFile( ctype.transFile ) ; Return
@@ -695,13 +709,13 @@ Method AddRef:Bool( node:SNode )
 			Return
 		Endif
 		
-		Local etype:=TCast<EnumType>( type )
+		Local etype:=Cast<EnumType>( type )
 		If etype
 			If AddRef( etype ) _deps.refsTypes.Push( etype )
 			Return
 		Endif
 		
-		Local ftype:=TCast<FuncType>( type )
+		Local ftype:=Cast<FuncType>( type )
 		If ftype
 			RefsType( ftype.retType )
 			For Local type:=Eachin ftype.argTypes
@@ -710,13 +724,13 @@ Method AddRef:Bool( node:SNode )
 			Return
 		Endif
 		
-		Local atype:=TCast<ArrayType>( type )
+		Local atype:=Cast<ArrayType>( type )
 		If atype
 			RefsType( atype.elemType )
 			Return
 		Endif
 		
-		Local ptype:=TCast<PointerType>( type )
+		Local ptype:=Cast<PointerType>( type )
 		If ptype
 			RefsType( ptype.elemType )
 			Return

+ 56 - 16
src/mx2cc/translator_cpp.monkey2

@@ -48,13 +48,13 @@ Class Translator_CPP Extends Translator
 		Emit( "#include <bbmonkey.h>" )
 		Emit( "#include <bbtypeinfo_r.h>" )
 		Emit( "#include <bbdeclinfo_r.h>" )
+		
 		EmitBr()
 		Emit( "#include ~q_r.h~q" )
 		
-		BeginDeps()
-		
 		Local nmspaces:=New StringMap<Stack<FileDecl>>
 		
+		EmitBr()
 		For Local fdecl:=Eachin _module.fileDecls
 			Local nmspace:=fdecl.nmspace
 			Local fdecls:=nmspaces[fdecl.nmspace]
@@ -63,8 +63,11 @@ Class Translator_CPP Extends Translator
 				nmspaces[fdecl.nmspace]=fdecls
 			Endif
 			fdecls.Add( fdecl )
+			EmitExternIncludes( fdecl )
 		Next
 		
+		BeginDeps()
+		
 		For Local it:=eachin nmspaces
 			
 			Local fdecls:=it.Value
@@ -83,6 +86,7 @@ Class Translator_CPP Extends Translator
 				nmpath=nmpath.Slice( 0,i )
 			Forever
 			
+			EmitBr()
 			Emit( "#if "+rcc )
 			
 			For Local fdecl:=Eachin fdecls
@@ -170,20 +174,8 @@ Class Translator_CPP Extends Translator
 		Emit( "}" )
 	End
 	
-	Method TranslateFile( fdecl:FileDecl )
-	
-		Reset()
+	Method EmitExternIncludes( fdecl:FileDecl )
 		
-		'***** Emit header file *****
-		
-		EmitBr()
-		Emit( "#ifndef MX2_"+fdecl.ident.ToUpper()+"_H" )
-		Emit( "#define MX2_"+fdecl.ident.ToUpper()+"_H" )
-		
-		EmitBr()
-		Emit( "#include <bbmonkey.h>" )
-		If fdecl.exhfile Emit( "#include ~q"+MakeIncludePath( fdecl.exhfile,ExtractDir( fdecl.hfile ) )+"~q" )
-			
 		For Local ipath:=Eachin fdecl.imports
 		
 			If ipath.Contains( "*." ) Continue
@@ -202,6 +194,23 @@ Class Translator_CPP Extends Translator
 			Endif
 			
 		Next
+	End
+	
+	Method TranslateFile( fdecl:FileDecl )
+	
+		Reset()
+		
+		'***** Emit header file *****
+		
+		EmitBr()
+		Emit( "#ifndef MX2_"+fdecl.ident.ToUpper()+"_H" )
+		Emit( "#define MX2_"+fdecl.ident.ToUpper()+"_H" )
+		
+		EmitBr()
+		Emit( "#include <bbmonkey.h>" )
+		If fdecl.exhfile Emit( "#include ~q"+MakeIncludePath( fdecl.exhfile,ExtractDir( fdecl.hfile ) )+"~q" )
+			
+		EmitExternIncludes( fdecl )
 		
 		BeginDeps()
 		
@@ -2492,8 +2501,38 @@ Class Translator_CPP Extends Translator
 	
 	Method TransType:String( type:Type ) Override
 	
-		If TCast<VoidType>( type ) Return "void"
+		Local xtype:=Cast<AliasType>( type )
+		If xtype 
+			If xtype.adecl.symbol Return xtype.adecl.symbol
+			If xtype.adecl.IsExtern Return xtype.adecl.ident
+			Return TransType( xtype._alias )
+		Endif
+
+		If Cast<VoidType>( type ) Return "void"
+		
+		Local classType:=Cast<ClassType>( type )
+		If classType Return TransType( classType )
+		
+		Local enumType:=Cast<EnumType>( type )
+		If enumType Return TransType( enumType )
 	
+		Local primType:=Cast<PrimType>( type )
+		If primType Return TransType( primType )
+		
+		Local funcType:=Cast<FuncType>( type )
+		If funcType Return TransType( funcType )
+		
+		Local arrayType:=Cast<ArrayType>( type )
+		If arrayType Return TransType( arrayType )
+		
+		Local pointerType:=Cast<PointerType>( type )
+		If pointerType Return TransType( pointerType )
+		
+		Local genArgType:=Cast<GenArgType>( type )
+		If genArgType Return TransType( genArgType )
+#rem		
+		If TCast<VoidType>( type ) Return "void"
+		
 		Local classType:=TCast<ClassType>( type )
 		If classType Return TransType( classType )
 		
@@ -2514,6 +2553,7 @@ Class Translator_CPP Extends Translator
 		
 		Local genArgType:=TCast<GenArgType>( type )
 		If genArgType Return TransType( genArgType )
+#end
 		
 		'Throw New TransEx( "Translator_CPP.Trans() Type '"+String.FromCString( type.typeName() )+"' not recognized" )
 		Throw New TransEx( "Translator_CPP.Trans() Type not recognized" )