Browse Source

Can now return CStrings from extern functions and store CStrings in extern vars.

Mark Sibly 9 years ago
parent
commit
a0341b05da
3 changed files with 27 additions and 15 deletions
  1. 9 10
      src/mx2cc/translator_cpp.monkey2
  2. 8 1
      src/mx2cc/type.monkey2
  3. 10 4
      src/mx2cc/var.monkey2

+ 9 - 10
src/mx2cc/translator_cpp.monkey2

@@ -1240,9 +1240,6 @@ Class Translator_CPP Extends Translator
 	
 		Uses( value.type )
 	
-'		Local ctype:=TCast<ClassType>( value.value.type )
-'		If ctype Uses( ctype )
-
 		Local t:="("+Trans( value.value )+")"
 		
 		If IsCValueType( value.type ) Return TransType( value.type )+t
@@ -1297,12 +1294,18 @@ Class Translator_CPP Extends Translator
 		Local ptype:=TCast<PrimType>( value.type )
 		Select ptype
 		Case Type.FloatType,Type.DoubleType
+		
 			Local t:=value.value
 			If t.Find( "." )=-1 And t.Find( "e" )=-1 And t.Find( "E" )=-1 t+=".0"
+			
 			If ptype=Type.FloatType Return t+"f"
 			Return t
+			
 		Case Type.StringType
-			Return "BB_T("+EnquoteCppString( value.value )+")"
+		
+			Local str:=value.value
+			If str.Length Return "bbString(L"+EnquoteCppString( str )+","+str.Length+")"
+			Return "bbString()"
 		End
 		
 		Refs( value.type )
@@ -1427,17 +1430,13 @@ Class Translator_CPP Extends Translator
 		If value.inits Return ArrayName( atype )+"({"+TransArgs( value.inits )+"},"+value.inits.Length+")"
 		
 		Return ArrayName( atype )+"("+TransArgs( value.sizes )+")"
-	
-'		If value.inits Return ArrayName( atype )+"::create({"+TransArgs( value.inits )+"},"+value.inits.Length+")"
-		
-'		Return ArrayName( atype )+"::create("+TransArgs( value.sizes )+")"
 	End
 	
 	Method Trans:String( value:ArrayIndexValue )
+
 		If value.args.Length=1 Return Trans( value.value )+"["+TransArgs( value.args )+"]"
+
 		Return Trans( value.value )+".at("+TransArgs( value.args )+")"
-		
-'		Return Trans( value.value )+"->at("+TransArgs( value.args )+")"
 	End
 	
 	Method Trans:String( value:StringIndexValue )

+ 8 - 1
src/mx2cc/type.monkey2

@@ -552,7 +552,14 @@ Class FuncType Extends Type
 			args[i]=args[i].UpCast( argTypes[i] )
 		Next
 		
-		Return New InvokeValue( value,args )
+		Local r:Value=New InvokeValue( value,args )
+		
+		'Autocast CString to String
+		If r.type.Equals( Type.CStringClass )
+			r=New UpCastValue( Type.StringType,r )
+		Endif
+		
+		Return r
 	End
 	
 	Method Equals:Bool( type:Type ) Override

+ 10 - 4
src/mx2cc/var.monkey2

@@ -111,6 +111,8 @@ Class VarValue Extends Value
 	
 	Method ToValue:Value( instance:Value ) Override
 	
+		Local r:Value=Self
+	
 		If vdecl.kind="field"
 		
 			If Not instance Throw New SemantEx( "Field '"+vdecl.ident+"' cannot be accessed without an instance" )
@@ -118,12 +120,17 @@ Class VarValue Extends Value
 			If Not instance.type.ExtendsType( cscope.ctype )
 				Throw New SemantEx( "Field '"+vdecl.ident+"' cannot be accessed from an instance of a different class" )
 			Endif
-
-			Return New MemberVarValue( instance,Self )
+			
+			r=New MemberVarValue( instance,Self )
 			
 		Endif
 		
-		Return Self
+		'Autocast CString to String
+		If r.type.Equals( Type.CStringClass )
+			r=New UpCastValue( Type.StringType,r )
+		Endif
+		
+		Return r
 	End
 	
 	Method CheckAccess( tscope:Scope ) Override
@@ -145,7 +152,6 @@ Class MemberVarValue Extends Value
 		If member.vdecl.kind="field"
 			If member.cscope.ctype.IsStruct
 				If instance.IsLValue flags|=VALUE_LVALUE|VALUE_ASSIGNABLE
-'				If instance.IsAssignable flags|=VALUE_ASSIGNABLE
 			Else
 				flags|=VALUE_LVALUE|VALUE_ASSIGNABLE
 			Endif