Bladeren bron

WIP Support for Class Blah<T> Extends T + cleanups.

Mark Sibly 9 jaren geleden
bovenliggende
commit
f76ff5d109
3 gewijzigde bestanden met toevoegingen van 37 en 6 verwijderingen
  1. 9 3
      src/mx2cc/class.monkey2
  2. 10 3
      src/mx2cc/expr.monkey2
  3. 18 0
      src/mx2cc/translator_cpp.monkey2

+ 9 - 3
src/mx2cc/class.monkey2

@@ -143,11 +143,17 @@ Class ClassType Extends Type
 					
 					extendsVoid=True
 					
+				Else If TCast<GenArgType>( type )	'eg: class Blah<T> Extends T
+				
+					superType=Null
+				
 				Else
 				
 					superType=TCast<ClassType>( type )
 					
-					If Not superType Or superType.cdecl.kind<>cdecl.kind Throw New SemantEx( "Type '"+type.ToString()+"' is not a valid super class type" )
+					If Not superType Throw New SemantEx( "Type '"+type.ToString()+"' is not a valid super class type" )
+					
+					If superType.cdecl.kind<>cdecl.kind Throw New SemantEx( "'"+cdecl.kind.Capitalize()+"' cannot extend '"+superType.cdecl.kind.Capitalize()+"'" )
 					
 					If superType.state=SNODE_SEMANTING Throw New SemantEx( "Cyclic inheritance error for '"+ToString()+"'",cdecl )
 					
@@ -178,13 +184,13 @@ Class ClassType Extends Type
 			
 				Try
 					Local type:=iface.SemantType( scope )
+					
+					If TCast<GenArgType>( type ) Continue
 
 					Local ifaceType:=TCast<ClassType>( type )
 					
 					If Not ifaceType Or (ifaceType.cdecl.kind<>"interface" And ifaceType.cdecl.kind<>"protocol" ) Throw New SemantEx( "Type '"+type.ToString()+"' is not a valid interface type" )
 					
-					If cdecl.kind="interface" And ifaceType.cdecl.kind="protocol" Throw New SemantEx( "Interfaces cannot extends protocols" )
-					
 					If ifaceType.state=SNODE_SEMANTING Throw New SemantEx( "Cyclic inheritance error",cdecl )
 					
 					If ifaces.Contains( ifaceType ) Throw New SemantEx( "Duplicate interface '"+ifaceType.ToString()+"'" )

+ 10 - 3
src/mx2cc/expr.monkey2

@@ -88,8 +88,7 @@ Class Expr Extends PNode
 			
 			Local ctype:=TCast<ClassType>( type )
 			If Not canBeGeneric And ctype And ctype.types And Not ctype.instanceOf
-				DebugStop()
-				Print "!!!!!"
+				Throw New SemantEx( "Illegal use of generic class '"+ctype.ToString()+"'" )
 			Endif
 			
 			semanting.Pop()
@@ -187,12 +186,20 @@ 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 )
+			If ctype And ctype.types And Not ctype.instanceOf
+				throw New SemantEx( "Illegal use of generic class '"+ctype.ToString()+"'" )
+			Endif
+		Endif
 		
 		Local tvalue:=value.FindValue( ident )
 		If tvalue Return tvalue
 		
-		Local tv:=Cast<TypeValue>( value )
 		If tv Throw New SemantEx( "Type '"+tv.ttype.Name+"' has no member named '"+ident+"'" )
+		
 		Throw New SemantEx( "Value of type '"+value.type.Name+"' has no member named '"+ident+"'" )
 	End
 	

+ 18 - 0
src/mx2cc/translator_cpp.monkey2

@@ -1829,7 +1829,22 @@ Class Translator_CPP Extends Translator
 		Case "shl" op="<<"
 		Case "shr" op=">>"
 		End
+
+		Local lhs:=Trans( value.lhs )
+		Local rhs:=Trans( value.rhs )
 		
+		Local etype:=TCast<EnumType>( value.type )
+		If etype
+'			Refs( etype )
+			Return EnumName( etype )+"(int("+lhs+")"+op+"int("+rhs+"))"
+		Endif
+		
+'		Uses( value.type )
+'		Uses( value.lhs.type )
+'		Uses( value.rhs.type )
+		
+		Return "("+lhs+op+rhs+")"
+#rem		
 '		If TCast<PointerType>( value.lhs.type ) Uses( value.lhs.type )
 '		If TCast<PointerType>( value.rhs.type ) Uses( value.rhs.type )
 		
@@ -1845,6 +1860,7 @@ Class Translator_CPP Extends Translator
 		If etype t=EnumName( etype )+"("+t+")"
 		
 		Return t
+#end
 	End
 	
 	Method Trans:String( value:IfThenElseValue )
@@ -1895,6 +1911,8 @@ Class Translator_CPP Extends Translator
 		
 		For Local arg:=Eachin args
 		
+			Decls( arg.type )
+		
 			Local t:=Trans( arg )
 			
 			If IsVolatile( arg )