Mark Sibly 8 năm trước cách đây
mục cha
commit
66b30ae062

+ 56 - 22
src/mx2cc/class.monkey2

@@ -6,8 +6,26 @@ Class ClassDecl Extends Decl
 	Field genArgs:String[]
 	Field superType:Expr
 	Field ifaceTypes:Expr[]
+	Field hasCtor:Bool=False
+	Field hasDefaultCtor:Bool=True
 	
 	Method ToNode:SNode( scope:Scope ) Override
+
+		hasCtor=False
+		For Local decl:=Eachin members
+			Local fdecl:=Cast<FuncDecl>( decl )
+			If Not fdecl Or fdecl.ident<>"new" Continue
+			hasCtor=True
+			Local isdefault:=True
+			For Local pdecl:=Eachin fdecl.type.params
+				If pdecl.init Continue
+				isdefault=False
+				Exit
+			Next
+			If Not isdefault Continue
+			hasDefaultCtor=True
+			Exit
+		Next
 	
 		Local types:=New Type[genArgs.Length]
 		For Local i:=0 Until types.Length
@@ -53,7 +71,7 @@ Class ClassType Extends Type
 	Field fields:=New Stack<VarValue>
 
 	Field extendsVoid:Bool
-	Field hasDefaultCtor:Bool
+	Field defaultCtor:FuncValue
 	
 	Method New( cdecl:ClassDecl,outer:Scope,types:Type[],instanceOf:ClassType )
 	
@@ -231,8 +249,11 @@ Class ClassType Extends Type
 				SemantMembers()
 				
 				Builder.semantingModule.genInstances.Push( Self )
+				
 			Else
+				
 				Builder.semantMembers.AddLast( Self )
+				
 			Endif
 			
 			transFile.classes.Push( Self )
@@ -243,7 +264,7 @@ Class ClassType Extends Type
 	End
 	
 	Method SemantMembers()
-	
+		
 		If membersSemanted Return
 	
 		If membersSemanting SemantError( "ClassType.SemantMembers()" )
@@ -275,25 +296,6 @@ Class ClassType Extends Type
 
 		Next
 		
-		'default ctor check
-		'
-		Local flist:=Cast<FuncList>( scope.GetNode( "new" ) )
-		If flist
-			hasDefaultCtor=False
-			For Local func:=Eachin flist.funcs
-				If func.ftype.argTypes Continue
-				hasDefaultCtor=True
-			Next
-		Else If Not cdecl.IsExtension
-			If superType And Not superType.hasDefaultCtor
-				Try
-					Throw New SemantEx( "Super class '"+superType.Name+"' has no default constructor" )
-				Catch ex:SemantEx
-				End
-			Endif
-			hasDefaultCtor=True
-		Endif
-		
 		If (cdecl.kind="class" Or cdecl.kind="struct") And Not scope.IsGeneric
 		
 			'Enum unimplemented superclass abstract methods
@@ -347,13 +349,45 @@ Class ClassType Extends Type
 			Endif
 		
 		Endif
-		
+
 		Self.abstractMethods=abstractMethods.ToArray()
 		
 		'Finished semanting funcs
 		'
 		membersSemanting=False
 		membersSemanted=True
+
+		'default ctor check
+		'
+		If Not cdecl.IsExtension And superType And Not superType.cdecl.hasDefaultCtor
+			Local flist:=Cast<FuncList>( scope.GetNode( "new" ) )
+			If Not flist New SemantEx( "Super class '"+superType.Name+"' has no default constructor!!!!" )
+		Endif
+		
+		#rem
+		If flist
+			For Local func:=Eachin flist.funcs
+				hasDefaultCtor=True
+				If func.params
+					For Local p:=Eachin func.params
+						If p.init Continue
+						hasDefaultCtor=False
+						Exit
+					Next
+					If Not hasDefaultCtor Continue
+				Endif
+				Exit
+			Next
+		Else If Not cdecl.IsExtension
+			If superType
+				superType.scope.GetNode( "new" )
+				If Not superType.hasDefaultCtor
+					New SemantEx( "Super class '"+superType.Name+"' has no default constructor" )
+				Endif
+			Endif
+			hasDefaultCtor=True
+		Endif
+		#end
 		
 		'Semant non-func members
 		'

+ 24 - 6
src/mx2cc/func.monkey2

@@ -210,7 +210,19 @@ Class FuncValue Extends Value
 		'
 		If IsCtor
 		
-			If cscope.ctype.cdecl.kind="struct"
+			If cscope.ctype.cdecl.kind="class"
+			
+				Local isdefault:=True
+				
+				For Local p:=Eachin pdecls
+					If p.init Continue
+					isdefault=False
+					Exit
+				Next
+				
+				If isdefault cscope.ctype.defaultCtor=Self
+		
+			Else If cscope.ctype.cdecl.kind="struct"
 			
 				If ftype.argTypes.Length And ftype.argTypes[0].Equals( cscope.ctype )
 					Local ok:=False
@@ -221,7 +233,7 @@ Class FuncValue Extends Value
 					Next
 					If Not ok Throw New SemantEx( "Illegal struct constructor - 'copy constructors' are automatically generated and cannot be redefined" )
 				Endif
-			
+						
 			Endif
 		
 		Else If IsMethod
@@ -395,10 +407,16 @@ Class FuncValue Extends Value
 			If fdecl.ident="new" And Not invokeNew
 			
 				Local superType:=cscope.ctype.superType
-				If superType And Not superType.hasDefaultCtor
-				
-					New SemantEx( "Super class '"+superType.Name+"' has no default constructor",pnode )
-					
+				If superType
+					If superType.cdecl.hasDefaultCtor
+						Local ctor:=superType.FindNode( "new" )
+						If ctor
+							Local invoke:=Cast<InvokeValue>( ctor.ToValue( Null ).Invoke( null ) )
+							invokeNew=New InvokeNewValue( superType,invoke.args )
+						Endif
+					Else
+						New SemantEx( "Super class '"+superType.Name+"' has no default constructor!!!!",pnode )
+					Endif
 				Endif
 			
 			Endif

+ 1 - 1
src/mx2cc/mx2cc.monkey2

@@ -38,7 +38,7 @@ Const MX2CC_VERSION_EXT:=""
 
 Global StartDir:String
 
-'Const TestArgs:="mx2cc makedocs mojo"	'monkey libc miniz stb-image stb-image-write stb-vorbis std mojo mojo3d"
+'Const TestArgs:="mx2cc makemods"
  
 Const TestArgs:="mx2cc makeapp src/mx2cc/test.monkey2"
 

+ 11 - 39
src/mx2cc/test.monkey2

@@ -1,50 +1,22 @@
 
-Class C
-	Field x:Int,y:Int
-End
+#Import "test2"
 
-Enum E
-	A,B,C
-End
+Class B
 
-Function F2:Int( n:Int )
-	Local i:=n*2
-	Return i
+	Method New( t:Int=0 )
+		Print "B.New"
+	End
+	
 End
 
-Function F1:Int( n:Int )
-	Local i:=F2(n*2)
-	Return i
+Class C Extends B
+
+	Method New()
+		Print "C.New"
+	End
 End
 
 Function Main()
 
-	Local e:E
 	Local c:=New C
-	Local c2:C
-	Local t:=New C[20]
-	t[5]=c
-	
-	DebugStop()
-
-	Print "HERE!"
-	
-	Local i:=0
-	While i<10
-		Print "i="+i
-		i+=1
-	Wend
-
-	For Local i:=0 Until 10
-		If i<5
-			Print "i<5"
-		Else If i<7
-			Print "i<7"
-		Else
-			Print "i"
-		endif
-		Print F1(i)
-	Next
-	
-	Print "HERE!"
 End

+ 10 - 2
src/mx2cc/test2.monkey2

@@ -1,4 +1,12 @@
 
-Function Test()
-	InternalTest()
+
+Class VertexFormat
+	
+	Property Pitch:Int() Virtual
+		Return 0
+	End
+
+	Method UpdateGLAttribs() Virtual
+	End
 End
+

+ 6 - 2
src/mx2cc/translator.monkey2

@@ -475,7 +475,12 @@ Class Translator
 		
 		Local atype:=TCast<ArrayType>( type )
 		If atype
-			Refs( atype.elemType )
+			Local ctype:=TCast<ClassType>( atype.elemType )
+			If ctype And ctype.IsStruct
+				Uses( ctype )
+			Else
+				Refs( atype.elemType )
+			Endif
 			Return
 		Endif
 		
@@ -546,7 +551,6 @@ Class Translator
 		UsesRefInfo( vvar.type )
 	
 		Uses( vvar.transFile )
-		
 	End
 	
 	Method UsesRefInfo( func:FuncValue )

+ 30 - 15
src/mx2cc/translator_cpp.monkey2

@@ -1747,29 +1747,44 @@ Class Translator_CPP Extends Translator
 		If Not value.value Return TransNull( value.type )
 		
 		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
+		If ptype
 			
-		Case Type.StringType
+			If ptype.IsIntegral
+				    
+				If value.value="0" Return TransType( value.type )+"(0)"
+				
+				Select value.type
+				Case Type.IntType  Return value.value
+				Case Type.UIntType Return value.value+"u"
+				Case Type.LongType Return value.value+"l"
+				Case Type.ULongType Return value.value+"ul"
+				End
+				
+				Return TransType( value.type )+"("+value.value+")"
+				
+			Else If ptype.IsReal
+
+				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
+
+			Else If ptype=Type.StringType
+
+				Local str:=value.value
+				If str.Length Return "bbString("+EnquoteCppString( str )+","+str.Length+")"
+				Return "bbString()"
+				
+			Endif
 		
-			Local str:=value.value
-			If str.Length Return "bbString("+EnquoteCppString( str )+","+str.Length+")"
-			Return "bbString()"
-		End
+		Endif
 		
 		Refs( value.type )
 		
 		Local etype:=TCast<EnumType>( value.type )
 		If etype Return EnumValueName( etype,value.value )
 		
-		If value.value="0" Return TransType( value.type )+"(0)"
-		
 		Return value.value
 	End
 	

+ 2 - 1
src/mx2cc/value.monkey2

@@ -171,6 +171,7 @@ Class UpCastValue Extends Value
 	Method New( type:Type,value:Value )
 		Self.type=type
 		Self.value=value
+
 	End
 	
 	Method ToString:String() Override
@@ -263,7 +264,7 @@ Class LiteralValue Extends Value
 			If ptype.IsSignedIntegral
 				value=String( Cast<Long>( value ) )
 			Else If ptype.IsUnsignedIntegral
-				value=String( Cast<ulong>( value ) )
+				value=String( Cast<ULong>( value ) )
 			Else If ptype.IsReal
 				value=String( Cast<Double>( value ) )
 			Else