Browse Source

More work on externs.

Mark Sibly 9 years ago
parent
commit
1b933a247b
4 changed files with 104 additions and 27 deletions
  1. 17 4
      src/mx2new/enum.monkey2
  2. 72 6
      src/mx2new/mung.monkey2
  3. 3 3
      src/mx2new/parser.monkey2
  4. 12 14
      src/mx2new/translator_cpp.monkey2

+ 17 - 4
src/mx2new/enum.monkey2

@@ -15,7 +15,7 @@ End
 Class EnumType Extends Type
 
 	Field edecl:EnumDecl
-	Field scope:Scope
+	Field scope:EnumScope
 	
 	Field superType:EnumType
 	Field nextInit:Int
@@ -23,7 +23,7 @@ Class EnumType Extends Type
 	Method New( edecl:EnumDecl,outer:Scope )
 		Self.pnode=edecl
 		Self.edecl=edecl
-		Self.scope=New Scope( outer )
+		Self.scope=New EnumScope( Self,outer )
 	End
 	
 	Method ToString:String() Override
@@ -66,8 +66,10 @@ Class EnumType Extends Type
 			Local vdecl:=Cast<VarDecl>( decl )
 			
 			If edecl.IsExtern
-			
-				Local value:=New LiteralValue( Self,vdecl.symbol )
+
+				Local symbol:=vdecl.symbol
+				If Not symbol symbol="@"+vdecl.ident			
+				Local value:=New LiteralValue( Self,symbol )
 				scope.Insert( decl.ident,value )
 				
 			Else
@@ -128,3 +130,14 @@ Class EnumType Extends Type
 
 End
 
+Class EnumScope Extends Scope
+
+	Field etype:EnumType
+
+	Method New( etype:EnumType,outer:Scope )
+		Super.New( outer )
+		
+		Self.etype=etype
+	End
+
+End

+ 72 - 6
src/mx2new/mung.monkey2

@@ -107,7 +107,7 @@ Function ScopeName:String( scope:Scope )
 	If fscope
 		Return MungIdent( fscope.fdecl.nmspace ).Replace( ".","_" )
 	Endif
-
+	
 	Local cscope:=Cast<ClassScope>( scope )
 	If cscope 
 		Local ctype:=cscope.ctype
@@ -119,13 +119,55 @@ Function ScopeName:String( scope:Scope )
 	Return ScopeName( scope.outer )
 End
 
+Function EnumName:String( etype:EnumType )
+
+	Local edecl:=etype.edecl
+	
+	If edecl.symbol
+		If edecl.symbol.EndsWith( "::" ) Return "int"
+		Return edecl.symbol
+	Endif
+	
+	If edecl.IsExtern Return edecl.ident
+	
+	Return "bbInt"
+End
+
+Function EnumValueName:String( etype:EnumType,value:String )
+
+	If Not value.StartsWith( "@" ) Return value
+	
+	value=value.Slice( 1 )
+	
+	Local edecl:=etype.edecl
+
+	If edecl.symbol
+		If edecl.symbol.EndsWith( "::" ) 
+			If edecl.symbol<>"::" value=edecl.symbol+value
+			Return "int("+value+")"
+		Endif
+		Local i:=edecl.symbol.FindLast( "::" )
+		If i<>-1 Return edecl.symbol.Slice( 0,i+2 )+value
+	Endif
+	
+	Return value
+End
+
 Function ClassName:String( ctype:ClassType )
 
 	Local cdecl:=ctype.cdecl
 
 	If cdecl.symbol Return cdecl.symbol
 	
-	If cdecl.IsExtern Return cdecl.ident
+	If cdecl.IsExtern
+	
+		Local symbol:=cdecl.ident
+		
+		Local cscope:=Cast<ClassScope>( ctype.scope.outer )
+		If cscope symbol=ClassName( cscope.ctype )+"::"+symbol
+	
+		Return symbol
+	Endif
 
 	Return "t_"+ScopeName( ctype.scope )
 End
@@ -136,7 +178,17 @@ Function FuncName:String( func:FuncValue )
 
 	If fdecl.symbol Return fdecl.symbol
 	
-	If fdecl.IsExtern Return fdecl.ident
+	If fdecl.IsExtern
+
+		Local symbol:=fdecl.ident
+		
+		If fdecl.kind="function"
+			Local cscope:=Cast<ClassScope>( func.scope )
+			If cscope symbol=ClassName( cscope.ctype )+"::"+symbol
+		Endif
+		
+		Return symbol
+	Endif
 	
 	If fdecl.kind="function" Or func.types
 
@@ -169,11 +221,25 @@ End
 
 Function VarName:String( vvar:VarValue )
 
-	If vvar.vdecl.symbol Return vvar.vdecl.symbol
+	Local vdecl:=vvar.vdecl
+	
+	If vdecl.symbol Return vdecl.symbol
+	
+	If vdecl.IsExtern 
+	
+		Local symbol:=vdecl.ident
+		
+		If vdecl.kind="global" Or vdecl.kind="const"
+			Local cscope:=Cast<ClassScope>( vvar.scope )
+			If cscope symbol=ClassName( cscope.ctype )+"::"+symbol
+		Endif
+		
+		Return symbol
+	Endif
 
-	Local sym:=MungIdent( vvar.vdecl.ident )
+	Local sym:=MungIdent( vdecl.ident )
 
-	Select vvar.vdecl.kind
+	Select vdecl.kind
 	
 	Case "local","param","capture"
 	

+ 3 - 3
src/mx2new/parser.monkey2

@@ -240,7 +240,7 @@ Class Parser
 				If flags & DECL_EXTERN
 					Parse( ":" )
 					decl.type=ParseType()
-					If CParse( "=" ) decl.symbol=ParseString() Else decl.symbol=decl.ident
+					If CParse( "=" ) decl.symbol=ParseString() 'Else decl.symbol=decl.ident
 				Else If CParse( ":" )
 					decl.type=ParseType()
 					If CParse( "=" ) decl.init=ParseExpr()
@@ -540,7 +540,7 @@ Class Parser
 			If CParse( "extends" ) decl.superType=ParseType()
 			
 			If flags & DECL_EXTERN
-				If CParse( "=" ) decl.symbol=ParseString() Else decl.symbol=decl.ident
+				If CParse( "=" ) decl.symbol=ParseString() 'Else decl.symbol=decl.ident
 			Endif
 			
 			ParseEol()
@@ -565,7 +565,7 @@ Class Parser
 				decl.docs=Docs()
 				
 				If flags & DECL_EXTERN
-					If CParse( "=" ) decl.symbol=ParseString() Else decl.symbol=decl.ident
+					If CParse( "=" ) decl.symbol=ParseString() 'Else decl.symbol=decl.ident
 				Else
 					If CParse( "=" ) decl.init=ParseExpr()
 				Endif

+ 12 - 14
src/mx2new/translator_cpp.monkey2

@@ -810,7 +810,7 @@ Class Translator_CPP Extends Translator
 				rhs=lhs+op.Slice( 0,-1 )+rhs
 				op="="
 			Endif
-			rhs=etype.edecl.symbol+"("+rhs+")"
+			rhs=EnumName( etype )+"("+rhs+")"
 		Endif
 		
 		Emit( lhs+op+rhs+";" )
@@ -1056,16 +1056,11 @@ Class Translator_CPP Extends Translator
 		Endif
 
 		Local etype:=Cast<EnumType>( type )
-		If etype
-			If etype.edecl.IsExtern Return etype.edecl.symbol+"(0)"
-			Return "0"
-		Endif
-		
+		If etype Return EnumName( etype )+"(0)"
+
 		If IsValue( type ) Return TransType( type )+"{}"
 		
-		Return "(("+TransType( type )+")(0))"
-		
-'		Return "nullptr"
+		Return "(("+TransType( type )+")0)"
 	End
 
 	Method Trans:String( value:LiteralValue )
@@ -1082,6 +1077,9 @@ Class Translator_CPP Extends Translator
 			Return "BB_T("+EnquoteCppString( value.value )+")"
 		End
 		
+		Local etype:=Cast<EnumType>( value.type )
+		If etype Return EnumValueName( etype,value.value )
+		
 		If value.value="0" Return TransType( value.type )+"(0)"
 		
 		Return value.value
@@ -1198,7 +1196,7 @@ Class Translator_CPP Extends Translator
 		Local t:=op+Trans( value.value )
 		
 		Local etype:=Cast<EnumType>( value.type )
-		If etype And etype.edecl.IsExtern t=etype.edecl.symbol+t
+		If etype And etype.edecl.IsExtern t=EnumName( etype )+"("+t+")"
 		
 		Return t
 	End
@@ -1206,7 +1204,8 @@ Class Translator_CPP Extends Translator
 	Method Trans:String( value:BinaryopValue )
 		Local op:=value.op
 		Select op
-		Case "<=>" 
+		Case "<=>"
+ 
 			Return "bbCompare("+Trans( value.lhs )+","+Trans( value.rhs )+")"
 			
 		Case "=","<>","<",">","<=",">="
@@ -1230,7 +1229,7 @@ Class Translator_CPP Extends Translator
 		Local t:="("+Trans( value.lhs )+op+Trans( value.rhs )+")"
 		
 		Local etype:=Cast<EnumType>( value.type )
-		If etype And etype.edecl.IsExtern t=etype.edecl.symbol+t
+		If etype And etype.edecl.IsExtern t=EnumName( etype )+t
 		
 		Return t
 	End
@@ -1329,8 +1328,7 @@ Class Translator_CPP Extends Translator
 	End
 	
 	Method TransType:String( type:EnumType )
-		If type.edecl.IsExtern Return type.edecl.symbol
-		Return "bbInt"
+		Return EnumName( type )
 	End
 	
 	Method TransType:String( type:PrimType )