Browse Source

Fixed extern aliases.

Mark Sibly 7 years ago
parent
commit
39ba24b94e

+ 1 - 9
src/mx2cc/mx2cc.monkey2

@@ -22,17 +22,9 @@ Global opts_time:Bool
 
 
 Global StartDir:String
 Global StartDir:String
 
 
-Const TestArgs:="mx2cc makemods"
+Const TestArgs:="mx2cc makemods"' -clean mojo"
  
  
 'Const TestArgs:="mx2cc makeapp src/mx2cc/test.monkey2"
 '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()
 Function Main()
 	
 	

+ 4 - 15
src/mx2cc/parser.monkey2

@@ -243,21 +243,6 @@ Class Parser
 		Return decls.ToArray()
 		Return decls.ToArray()
 	End
 	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 )
 	Method ParseAliases( decls:Stack<Decl>,flags:Int )
 	
 	
 		Local kind:=Parse()
 		Local kind:=Parse()
@@ -277,6 +262,10 @@ Class Parser
 				
 				
 				Parse( ":" )
 				Parse( ":" )
 				decl.type=ParseType()
 				decl.type=ParseType()
+
+				If flags & DECL_EXTERN
+					If CParse( "=" ) decl.symbol=ParseString()
+				Endif
 				
 				
 				decl.endpos=EndPos
 				decl.endpos=EndPos
 				decls.Push( decl )
 				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
 End
 
 
 Function Main()
 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
 End

+ 21 - 7
src/mx2cc/translator.monkey2

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

+ 56 - 16
src/mx2cc/translator_cpp.monkey2

@@ -48,13 +48,13 @@ Class Translator_CPP Extends Translator
 		Emit( "#include <bbmonkey.h>" )
 		Emit( "#include <bbmonkey.h>" )
 		Emit( "#include <bbtypeinfo_r.h>" )
 		Emit( "#include <bbtypeinfo_r.h>" )
 		Emit( "#include <bbdeclinfo_r.h>" )
 		Emit( "#include <bbdeclinfo_r.h>" )
+		
 		EmitBr()
 		EmitBr()
 		Emit( "#include ~q_r.h~q" )
 		Emit( "#include ~q_r.h~q" )
 		
 		
-		BeginDeps()
-		
 		Local nmspaces:=New StringMap<Stack<FileDecl>>
 		Local nmspaces:=New StringMap<Stack<FileDecl>>
 		
 		
+		EmitBr()
 		For Local fdecl:=Eachin _module.fileDecls
 		For Local fdecl:=Eachin _module.fileDecls
 			Local nmspace:=fdecl.nmspace
 			Local nmspace:=fdecl.nmspace
 			Local fdecls:=nmspaces[fdecl.nmspace]
 			Local fdecls:=nmspaces[fdecl.nmspace]
@@ -63,8 +63,11 @@ Class Translator_CPP Extends Translator
 				nmspaces[fdecl.nmspace]=fdecls
 				nmspaces[fdecl.nmspace]=fdecls
 			Endif
 			Endif
 			fdecls.Add( fdecl )
 			fdecls.Add( fdecl )
+			EmitExternIncludes( fdecl )
 		Next
 		Next
 		
 		
+		BeginDeps()
+		
 		For Local it:=eachin nmspaces
 		For Local it:=eachin nmspaces
 			
 			
 			Local fdecls:=it.Value
 			Local fdecls:=it.Value
@@ -83,6 +86,7 @@ Class Translator_CPP Extends Translator
 				nmpath=nmpath.Slice( 0,i )
 				nmpath=nmpath.Slice( 0,i )
 			Forever
 			Forever
 			
 			
+			EmitBr()
 			Emit( "#if "+rcc )
 			Emit( "#if "+rcc )
 			
 			
 			For Local fdecl:=Eachin fdecls
 			For Local fdecl:=Eachin fdecls
@@ -170,20 +174,8 @@ Class Translator_CPP Extends Translator
 		Emit( "}" )
 		Emit( "}" )
 	End
 	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
 		For Local ipath:=Eachin fdecl.imports
 		
 		
 			If ipath.Contains( "*." ) Continue
 			If ipath.Contains( "*." ) Continue
@@ -202,6 +194,23 @@ Class Translator_CPP Extends Translator
 			Endif
 			Endif
 			
 			
 		Next
 		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()
 		BeginDeps()
 		
 		
@@ -2492,8 +2501,38 @@ Class Translator_CPP Extends Translator
 	
 	
 	Method TransType:String( type:Type ) Override
 	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 )
 		Local classType:=TCast<ClassType>( type )
 		If classType Return TransType( classType )
 		If classType Return TransType( classType )
 		
 		
@@ -2514,6 +2553,7 @@ Class Translator_CPP Extends Translator
 		
 		
 		Local genArgType:=TCast<GenArgType>( type )
 		Local genArgType:=TCast<GenArgType>( type )
 		If genArgType Return TransType( genArgType )
 		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 '"+String.FromCString( type.typeName() )+"' not recognized" )
 		Throw New TransEx( "Translator_CPP.Trans() Type not recognized" )
 		Throw New TransEx( "Translator_CPP.Trans() Type not recognized" )