Просмотр исходного кода

Fixed being able to pass types as params for generic funcs + new struct for extern structs.

Mark Sibly 7 лет назад
Родитель
Сommit
fbfcb8ac06
3 измененных файлов с 39 добавлено и 18 удалено
  1. 29 16
      src/mx2cc/expr.monkey2
  2. 5 2
      src/mx2cc/translator_cpp.monkey2
  3. 5 0
      src/mx2cc/value.monkey2

+ 29 - 16
src/mx2cc/expr.monkey2

@@ -188,32 +188,42 @@ Class MemberExpr Extends Expr
 	
 	Method OnSemant:Value( scope:Scope ) Override
 		
-		Local value:=expr.SemantRValue( scope )
-		Local tv:=Cast<TypeValue>( value )
-	
-		If tv	
-			Local ctype:=TCast<ClassType>( tv.ttype )
+		Local evalue:=expr.Semant( scope )
+		
+		Local tvalue:=Cast<TypeValue>( evalue )
+		
+		If tvalue
+			
+			Local ctype:=TCast<ClassType>( tvalue.ttype )
+			
 			If ctype And ctype.types And Not ctype.instanceOf
-				throw New SemantEx( "Illegal use of generic class '"+ctype.ToString()+"'" )
+				Throw New SemantEx( "Class '"+ctype.Name+"' is generic" )
+				'throw New SemantEx( "Illegal use of generic class '"+ctype.ToString()+"'" )
 			Endif
-		Endif
+			
+			Local value:=tvalue.FindValue( ident )
+			
+			If Not value Throw New SemantEx( "Type '"+tvalue.ttype.Name+"' has no member named '"+ident+"'" )
+				
+			Return value
 		
-		Local tvalue:=value.FindValue( ident )
-		If tvalue Return tvalue
+		Endif
 		
-		If tv Throw New SemantEx( "Type '"+tv.ttype.Name+"' has no member named '"+ident+"'" )
+		Local value:=evalue.FindValue( ident )
 		
-		Throw New SemantEx( "Value of type '"+value.type.Name+"' has no member named '"+ident+"'" )
+		If Not value Throw New SemantEx( "Value of type '"+evalue.type.Name+"' has no member named '"+ident+"'" )
+			
+		Return value
 	End
 	
 	Method OnSemantType:Type( scope:Scope ) Override
 	
-		Local type:=expr.SemantType( scope )
+		Local etype:=expr.SemantType( scope )
 		
-		Local type2:=type.FindType( ident )
-		If Not type2 Throw New SemantEx( "Type '"+type.Name+"' has no member type named '"+ident+"'" )
+		Local type:=etype.FindType( ident )
+		If Not type Throw New SemantEx( "Type '"+etype.Name+"' has no member type named '"+ident+"'" )
 		
-		Return type2
+		Return type
 	End
 
 	Method ToString:String() Override
@@ -242,6 +252,7 @@ Class SafeMemberExpr Extends Expr
 		Local value:=Self.expr.SemantRValue( scope ).RemoveSideEffects( block )
 
 		Local thenValue:=value.FindValue( ident )
+		
 		If Not thenValue Throw New SemantEx( "Value of type '"+value.type.Name+"' has no member named '"+ident+"'" )
 
 		thenValue=thenValue.ToRValue()
@@ -534,7 +545,7 @@ Class ExtendsExpr Extends Expr
 			Throw New SemantEx( "Type '"+type.ToString()+"' is not a class or interface type" )
 		Endif
 		
-		Local value:=Self.expr.SemantRValue( scope )
+		Local value:=Self.expr.Semant( scope )
 
 		Local tvalue:=Cast<TypeValue>( value )
 		If tvalue
@@ -542,6 +553,8 @@ Class ExtendsExpr Extends Expr
 			Local ptype:=TCast<PrimType>( tvalue.ttype )
 			If ptype And ptype.ctype.DistanceToType( ctype )>=0 Return LiteralValue.BoolValue( True )
 			Return LiteralValue.BoolValue( False )
+		Else
+			value=value.ToRValue()
 		Endif
 		
 		If value.type.DistanceToType( ctype )>=0 Return LiteralValue.BoolValue( True )

+ 5 - 2
src/mx2cc/translator_cpp.monkey2

@@ -378,7 +378,7 @@ Class Translator_CPP Extends Translator
 			params+=TransType( p.type )+" "+VarName( p )
 		Next
 
-		If func.IsCtor And ctype.IsStruct
+		If func.IsCtor And ctype.IsStruct 
 			If Not ftype.argTypes.Length Or ftype.argTypes[0].Equals( ctype )
 				If params params+=","
 				params+="bbNullCtor_t"
@@ -2110,7 +2110,10 @@ Class Translator_CPP Extends Translator
 		Endif
 		
 		If ctype.IsStruct
-			If Not value.args Return cname+"{bbNullCtor}"
+			If Not value.args 
+				If Not ctype.cdecl.IsExtern Return cname+"{bbNullCtor}"
+				Return cname+"{}"
+			Endif
 			If value.args[0].type.Equals( ctype ) Return cname+"{"+TransArgs( value.args )+",bbNullCtor}"
 			Return cname+"{"+TransArgs( value.args )+"}"
 		Endif

+ 5 - 0
src/mx2cc/value.monkey2

@@ -152,6 +152,11 @@ Class TypeValue Extends Value
 		Return "<"+ttype.ToString()+">"
 	End
 	
+	Method ToRValue:Value() Override
+		Throw New SemantEx( "Unexpected type '"+ttype.Name+"'" )
+		Return Null
+	End
+	
 	Method FindValue:Value( ident:String ) Override
 		Local node:=ttype.FindNode( ident )
 		If node Return node.ToValue( Null )