浏览代码

Added error when using generic types; Added 'Self' to debug locals.

Mark Sibly 9 年之前
父节点
当前提交
5d43a5b443

+ 0 - 1
modules/monkey/native/bbdebug.h

@@ -3,7 +3,6 @@
 #define BB_DEBUG_H
 
 #include "bbstring.h"
-#include "bbobject.h"
 
 struct bbDBFiber;
 struct bbDBFrame;

+ 4 - 23
modules/monkey/native/bbobject.h

@@ -33,29 +33,6 @@ struct bbObject : public bbGCNode{
 struct bbThrowable : public bbObject{
 };
 
-/*
-struct bbException : public bbThrowable{
-
-	bbException();
-	
-	bbException( bbString message );
-	
-	bbString message()const{
-		return _message;
-	}
-	
-	bbArray<bbString> *debugStack()const{
-		return _debugStack;
-	}
-	
-	private:
-	
-	bbGCVar<bbArray<bbString>> _debugStack;
-	
-	bbString _message;
-};
-*/
-
 struct bbInterface{
 
 	virtual ~bbInterface(){
@@ -73,6 +50,10 @@ template<class T,class...A> T *bbGCNew( A...a ){
 	return p;
 }
 
+inline bbDBAssertSelf( void *p ){
+	bbDebugAssert( p,"'Self' is null" );
+}
+
 inline bbString bbDBObjectValue( bbObject *p ){
 	char buf[64];
 	sprintf( buf,"@%p",p );

+ 18 - 14
src/mx2new/class.monkey2

@@ -444,7 +444,7 @@ Class ClassType Extends Type
 			If TypesEqual( inst.types,types ) Return inst
 		Next
 		
-		Local inst:=New ClassType( cdecl,scope.outer,types,self )
+		Local inst:=New ClassType( cdecl,scope.outer,types,Self )
 		instances.Push( inst )
 		
 		inst.Semant()
@@ -646,21 +646,13 @@ Class ClassScope Extends Scope
 
 	Field ctype:ClassType
 	
-	Field genTypes:=New StringMap<Type>
+	Field itype:Type
 	
 	Method New( ctype:ClassType,outer:Scope )
 		Super.New( outer )
 
 		Self.ctype=ctype
 		
-		'so code can access 'List' instead of 'List<T>'
-		genTypes[ctype.cdecl.ident]=ctype
-
-		'so code can access gen args
-		Local genArgs:=ctype.cdecl.genArgs
-		For Local i:=0 Until genArgs.Length
-			genTypes[genArgs[i]]=ctype.types[i]
-		Next
 	End
 	
 	Property Name:String() Override
@@ -706,10 +698,22 @@ Class ClassScope Extends Scope
 	
 	Method FindType:Type( ident:String ) Override
 	
-		Local type:=genTypes[ident]
-		If type Return type
-	
-		type=ctype.FindType( ident )
+		If ident=ctype.cdecl.ident
+			If Not itype
+				If ctype.types And Not ctype.instanceOf
+					itype=ctype.GenInstance( ctype.types )
+				Else 
+					itype=ctype
+				Endif
+			Endif
+			Return itype
+		Endif
+		
+		For Local i:=0 Until ctype.cdecl.genArgs.Length
+			If ident=ctype.cdecl.genArgs[i] Return ctype.types[i]
+		Next
+		
+		Local type:=ctype.FindType( ident )
 		If type Return type
 		
 		If outer Return outer.FindType( ident )

+ 11 - 2
src/mx2new/expr.monkey2

@@ -79,13 +79,22 @@ Class Expr Extends PNode
 		Return Null
 	End
 	
-	Method SemantType:Type( scope:Scope )
+	Method SemantType:Type( scope:Scope,generic:Bool=False )
 
 		Try
 			semanting.Push( Self )
 
 			Local type:=OnSemantType( scope )
 			
+			Local ctype:=TCast<ClassType>( type )
+			If ctype
+				If generic
+'					If Not ctype.types Or ctype.instanceOf Throw New SemantEx( "Type '"+ctype.ToString()+"' is not generic" )
+				Else
+					If ctype.types And Not ctype.instanceOf Throw New SemantEx( "Type '"+ctype.ToString()+"' is generic" )
+				Endif
+			Endif
+			
 			semanting.Pop()
 			Return type
 		
@@ -266,7 +275,7 @@ Class GenericExpr Extends Expr
 	
 		Local types:=SemantTypes( Self.types,scope )
 		
-		Local type:=Self.expr.SemantType( scope )
+		Local type:=Self.expr.SemantType( scope,True )
 		
 		Return type.GenInstance( types )
 	End

+ 4 - 0
src/mx2new/func.monkey2

@@ -129,6 +129,10 @@ Class FuncValue Extends Value
 		Return fdecl.kind="method" And fdecl.ident<>"new"
 	End
 	
+	Property IsVirtual:Bool()
+		Return IsMethod And (cscope.ctype.IsVirtual Or fdecl.IsVirtual Or fdecl.IsOverride)
+	End
+	
 	Property IsFunction:Bool()
 		Return fdecl.kind="function"
 	End

+ 2 - 0
src/mx2new/stmtexpr.monkey2

@@ -149,6 +149,8 @@ Class EvalStmtExpr Extends StmtExpr
 	
 		Local value:=expr.SemantRValue( block )
 		
+		If Not value.HasSideEffects Throw New SemantEx( "Statement has no effect" )
+		
 		Return New EvalStmt( Self,value )
 	End
 

+ 25 - 0
src/mx2new/translator_cpp.monkey2

@@ -712,6 +712,18 @@ Class Translator_CPP Extends Translator
 			
 		Endif
 		
+		If debug And func.IsMethod
+		
+			If Not func.IsVirtual And Not func.IsExtension
+				'			
+				'Can't do this yet as it breaks mx2cc!
+				'
+				'Emit( "bbDBAssertSelf(this);" )
+				'
+			Endif
+			
+		Endif
+		
 		EmitBlock( func )
 		
 		Emit( "}" )
@@ -811,6 +823,19 @@ Class Translator_CPP Extends Translator
 		
 			Emit( "bbDBFrame db_f{~q"+func.Name+":"+func.ftype.retType.Name+"("+func.ParamNames+")~q,~q"+func.pnode.srcfile.path+"~q};" )
 			
+			If func.IsMethod
+				
+				Select func.cscope.ctype.cdecl.kind
+				Case "struct"
+					Emit( ClassName( func.cscope.ctype )+"*self=&"+Trans(func.selfValue)+";" )
+					Emit( "bbDBLocal(~qSelf~q,self);" )
+				Case "class"
+					Emit( ClassName( func.cscope.ctype )+"*self="+Trans(func.selfValue)+";" )
+					Emit( "bbDBLocal(~qSelf~q,&self);" )
+				End
+				
+			Endif
+			
 			For Local vvar:=Eachin func.params
 				Emit( "bbDBLocal(~q"+vvar.vdecl.ident+"~q,&"+Trans( vvar )+");" )
 			Next

+ 11 - 2
src/mx2new/typeexpr.monkey2

@@ -21,7 +21,7 @@ Class TypeExpr Extends PNode
 		Return Null
 	End
 	
-	Method Semant:Type( scope:Scope )
+	Method Semant:Type( scope:Scope,generic:Bool=False )
 	
 		Try
 		
@@ -29,6 +29,15 @@ Class TypeExpr Extends PNode
 			
 			Local type:=OnSemant( scope )
 			
+			Local ctype:=TCast<ClassType>( type )
+			If ctype
+				If generic
+'					If Not ctype.types Or ctype.instanceOf Throw New SemantEx( "Type '"+ctype.ToString()+"' is not generic" )
+				Else
+					If ctype.types And Not ctype.instanceOf Throw New SemantEx( "Type '"+ctype.ToString()+" is generic" )
+				Endif
+			Endif
+			
 			PNode.semanting.Pop()
 			Return type
 		
@@ -169,7 +178,7 @@ Class GenericTypeExpr Extends TypeExpr
 	
 	Method OnSemant:Type( scope:Scope ) Override
 	
-		Local type:=Self.type.Semant( scope )
+		Local type:=Self.type.Semant( scope,True )
 		
 		Local args:=New Type[Self.args.Length]
 		For Local i:=0 Until args.Length

+ 2 - 5
src/mx2new/var.monkey2

@@ -88,11 +88,13 @@ Class VarValue Extends Value
 		Endif
 		
 		If Not type.IsGeneric And Not vdecl.IsExtern And Not Cast<Block>( scope )
+		
 			If vdecl.kind="global" Or vdecl.kind="const"
 				transFile.globals.Push( Self )
 			Else
 				scope.transMembers.Push( Self )
 			Endif
+			
 		Endif
 	
 		Return Self
@@ -118,11 +120,6 @@ Class VarValue Extends Value
 
 			Return New MemberVarValue( instance,Self )
 			
-'			If instance And instance.type.DistanceToType( cscope.ctype )>=0
-'				Return New MemberVarValue( instance,Self )
-'			Endif
-'			Throw New SemantEx( "Field '"+ToString()+"' cannot be accessed without an instance" )
-
 		Endif
 		
 		Return Self