瀏覽代碼

Improved generation of extern objects.
Extern objects have a Null superclass as default.
Added ability to import header directly.
Fixed issue with null test with eachin and non local variable.

woollybah 11 年之前
父節點
當前提交
cd6faadecc
共有 5 個文件被更改,包括 103 次插入19 次删除
  1. 50 11
      ctranslator.bmx
  2. 1 0
      decl.bmx
  3. 5 1
      iparser.bmx
  4. 42 7
      parser.bmx
  5. 5 0
      type.bmx

+ 50 - 11
ctranslator.bmx

@@ -175,7 +175,7 @@ Type TCTranslator Extends TTranslator
 			Return "$" + p
 		End If
 		If TArrayType( ty ) Return TransIfcType(TArrayType( ty ).elemType) + "&[]"
-		If TObjectType( ty ) Return ":" + TObjectType(ty).classDecl.ident
+		If TObjectType( ty ) Return ":" + TObjectType(ty).classDecl.ident + p
 		If TFunctionPtrType( ty ) Return TransIfcType(TFunctionPtrType(ty).func.retType) + TransIfcArgs(TFunctionPtrType(ty).func)
 		If TExternObjectType( ty ) Return ":" + TExternObjectType(ty).classDecl.ident + p
 		InternalErr
@@ -323,7 +323,12 @@ Type TCTranslator Extends TTranslator
 						Continue
 					End If
 				End If
-				t:+TransTemplateCast( ty,args[i].exprType,args[i].Trans() )
+				
+				If decl.argDecls[i].castTo Then
+					t:+ Bra(decl.argDecls[i].castTo) + args[i].Trans()
+				Else
+					t:+TransTemplateCast( ty,args[i].exprType,args[i].Trans() )
+				End If
 			Else
 				decl.argDecls[i].Semant()
 				' default values
@@ -598,8 +603,12 @@ t:+"NULLNULLNULL"
 						'Return "(" + obj + TransSubExpr( lhs ) + ")->" + decl.munged+TransArgs( args,decl, Null)
 						Return TransSubExpr( lhs ) + "->" + decl.munged+TransArgs( args,decl, Null)
 					Else
-						Local class:String = TransSubExpr( lhs ) + "->clas" + tSuper
-						Return class + "->" + TransFuncPrefix(cdecl, decl) + decl.ident+TransArgs( args,decl, TransSubExpr( lhs ) )
+						If decl.scope.IsExtern()
+							Return decl.munged + Bra(TransArgs( args,decl, TransSubExpr( lhs ) ))
+						Else
+							Local class:String = TransSubExpr( lhs ) + "->clas" + tSuper
+							Return class + "->" + TransFuncPrefix(cdecl, decl) + decl.ident+TransArgs( args,decl, TransSubExpr( lhs ) )
+						End If
 					End If
 				Else If TNewObjectExpr(lhs) Then
 					Local cdecl:TClassDecl = TNewObjectExpr(lhs).classDecl
@@ -617,9 +626,14 @@ t:+"NULLNULLNULL"
 				Else If TMemberVarExpr(lhs) Then
 					Local cdecl:TClassDecl = TObjectType(TMemberVarExpr(lhs).decl.ty).classDecl
 					Local obj:String = Bra(TransObject(cdecl))
-					Local class:String = Bra("(" + obj + TransSubExpr( lhs ) + ")->clas" + tSuper)
-					'Local class:String = TransFuncClass(cdecl)
-					Return class + "->" + TransFuncPrefix(cdecl, decl) + decl.ident+TransArgs( args,decl, TransSubExpr( lhs ) )
+					
+					If decl.scope.IsExtern()
+						Return decl.munged + Bra(TransArgs( args,decl, TransSubExpr( lhs ) ))
+					Else
+						Local class:String = Bra("(" + obj + TransSubExpr( lhs ) + ")->clas" + tSuper)
+						'Local class:String = TransFuncClass(cdecl)
+						Return class + "->" + TransFuncPrefix(cdecl, decl) + decl.ident+TransArgs( args,decl, TransSubExpr( lhs ) )
+					End If
 				Else If TInvokeExpr(lhs) Then
 					' create a local variable of the inner invocation
 					Local lvar:String = CreateLocal(lhs)
@@ -693,7 +707,11 @@ t:+"NULLNULLNULL"
 		If decl.ident = "Object"
 			Return "BBOBJECT"
 		Else
-			Return "struct " + decl.munged + "_obj*"
+			If decl.IsExtern() Then
+				Return "struct " + decl.munged + "*"
+			Else
+				Return "struct " + decl.munged + "_obj*"
+			End If
 		End If
 	End Method
 
@@ -1770,7 +1788,7 @@ EndRem
 			Else
 				s :+ lhs+TransAssignOp( stmt.op )+rhs
 			End If
-		Else If TFunctionPtrType(stmt.lhs.exprType) And TInvokeExpr(stmt.rhs) And Not TInvokeExpr(stmt.rhs).invokedWithBraces Then
+		Else If (TFunctionPtrType(stmt.lhs.exprType) <> Null Or IsPointerType(stmt.lhs.exprType, TType.T_BYTE)) And TInvokeExpr(stmt.rhs) And Not TInvokeExpr(stmt.rhs).invokedWithBraces Then
 			rhs = TInvokeExpr(stmt.rhs).decl.munged
 			s :+ lhs+TransAssignOp( stmt.op )+rhs
 		Else
@@ -2248,7 +2266,10 @@ End Rem
 	Method EmitClassProto( classDecl:TClassDecl )
 
 		Local classid$=classDecl.munged
-		Local superid$=classDecl.superClass.actual.munged
+		Local superid$
+		If classDecl.superClass Then
+			superid=classDecl.superClass.actual.munged
+		End If
 
 		If Not classDecl.IsExtern() Then
 			If opt_issuperstrict Then
@@ -3142,7 +3163,11 @@ End Rem
 
 	Method EmitIfcClassDecl(classDecl:TClassDecl)
 
-		Emit classDecl.ident + "^" + classDecl.superClass.ident + "{", False
+		If classDecl.superClass Then
+			Emit classDecl.ident + "^" + classDecl.superClass.ident + "{", False
+		Else
+			Emit classDecl.ident + "^Null{", False
+		End If
 
 		'PushMungScope
 		BeginLocalScope
@@ -3194,6 +3219,16 @@ End Rem
 
 			Emit "}=" + Enquote(classDecl.munged), False
 		Else
+			For Local decl:TDecl=EachIn classDecl.Decls()
+
+				Local fdecl:TFuncDecl=TFuncDecl( decl )
+				If fdecl
+					EmitIfcClassFuncDecl fdecl
+					Continue
+				EndIf
+
+			Next
+
 			Emit "}E=0", False
 		End If
 
@@ -3337,6 +3372,10 @@ End Rem
 				EmitModuleInclude(TModuleDecl(mdecl), included)
 			Next
 		Next
+		
+		For Local header:String=EachIn app.headers
+			Emit "#include ~q../" + header + "~q"
+		Next
 
 		Emit "int " + app.munged + "();"
 		

+ 1 - 0
decl.bmx

@@ -2007,6 +2007,7 @@ Type TAppDecl Extends TScopeDecl
 	Field semantedGlobals:TList=New TList'<TGlobalDecl>			'in-order (ie: dependancy sorted) list of _semanted globals
 
 	Field fileImports:TList=New TList'StringList
+	Field headers:TList = New TList
 	
 	Field stringConsts:TMap = New TMap
 	Field stringConstCount:Int

+ 5 - 1
iparser.bmx

@@ -1067,7 +1067,11 @@ End Rem
 			If CParse("*") Then
 				If TIdentType(ty) Then
 					ty = TType.MapToPointerType(ty)
-					'ty = New TIdentPtrType.Create(TIdentType(ty).ident, TIdentType(ty).args)
+
+					While CParse( "*" )
+						ty = TType.MapToPointerType(ty)
+					Wend
+
 				End If
 			End If
 			

+ 42 - 7
parser.bmx

@@ -148,8 +148,16 @@ Type TForEachinStmt Extends TLoopStmt
 '				Local varTmp:TLocalDecl=New TLocalDecl.Create( varid,varty,nextObjExpr )
 '				block.stmts.AddFirst New TDeclStmt.Create( varTmp )
 
+				Local cExpr:TExpr
+				
+				If TIdentType(varty) And TIdentType(varty).ident = "Object" Then
+					cExpr = nextObjExpr
+				Else
+					cExpr = New TCastExpr.Create( varty, nextObjExpr,CAST_EXPLICIT )
+				End If
+
 				' local variable
-				Local varTmp:TLocalDecl=New TLocalDecl.Create( varid,varty,New TCastExpr.Create( varty, nextObjExpr,CAST_EXPLICIT ) )
+				Local varTmp:TLocalDecl=New TLocalDecl.Create( varid,varty,cExpr )
 
 				' local var as expression
 				Local expr:TExpr=New TVarExpr.Create( varTmp )
@@ -165,7 +173,29 @@ Type TForEachinStmt Extends TLoopStmt
 				block.stmts.AddFirst New TIfStmt.Create( expr,thenBlock,elseBlock )
 				block.stmts.AddFirst New TDeclStmt.Create( varTmp )
 			Else
-				block.stmts.AddFirst New TAssignStmt.Create( "=",New TIdentExpr.Create( varid ),New TCastExpr.Create( varty, nextObjExpr,CAST_EXPLICIT ) )
+				
+				If Not varty Then
+					Local decl:TValDecl = block.scope.FindValDecl(varid)
+					
+					If decl Then
+						decl.Semant()
+						
+						varty = decl.ty.Copy()
+					End If
+				End If
+				
+				' var = Null
+				Local expr:TExpr=New TBinaryCompareExpr.Create( "=",New TIdentExpr.Create( varid ), New TNullExpr.Create(TType.nullObjectType))
+
+				' then continue
+				Local thenBlock:TBlockDecl=New TBlockDecl.Create( block.scope )
+				Local elseBlock:TBlockDecl=New TBlockDecl.Create( block.scope )
+				thenBlock.AddStmt New TContinueStmt
+
+				block.stmts.AddFirst New TIfStmt.Create( expr,thenBlock,elseBlock )
+				'block.stmts.AddFirst New TDeclStmt.Create( varTmp )
+
+				block.stmts.AddFirst New TAssignStmt.Create( "=",New TIdentExpr.Create( varid ),New TCastExpr.Create( varty, nextObjExpr,CAST_EXPLICIT ))
 			EndIf
 
 			Local whileStmt:TWhileStmt=New TWhileStmt.Create( hasNextExpr,block,Null )
@@ -556,6 +586,7 @@ Type TParser
 			ty=ParseType()
 
 			If CParse("ptr") Then
+
 				ty = TType.MapToPointerType(ty)
 
 				While CParse("ptr")
@@ -2235,7 +2266,9 @@ End Rem
 				superTy=ParseIdentType()
 			EndIf
 		Else
-			superTy=New TIdentType.Create( "brl.classes.object" )
+			If Not (attrs & DECL_EXTERN) Then
+				superTy=New TIdentType.Create( "brl.classes.object" )
+			End If
 		EndIf
 Rem
 		If CParse( "implements" )
@@ -2406,11 +2439,13 @@ End Rem
 			' try to import interface
 			Local par:TIParser = New TIParser
 			If par.ParseModuleImport(_module, modpath, origPath, path, , , filepath) Return
+		Else If filepath.startswith("-") Then
+			If Not _app.fileimports.Contains(filepath) Then
+				_app.fileimports.AddLast filepath
+			End If
 		Else
-			If filepath.startswith("-") Then
-				If Not _app.fileimports.Contains(filepath) Then
-					_app.fileimports.AddLast filepath
-				End If
+			If filepath.EndsWith(".h") And filepath.Find("*") = -1 Then
+				_app.headers.AddLast filepath
 			End If
 		End If
 

+ 5 - 0
type.bmx

@@ -769,6 +769,11 @@ Type TIdentType Extends TType
 			ty = New TObjectType.Create(TObjectType(ty).classDecl)
 			ty._flags :| T_VAR
 		End If
+
+		If (_flags & T_POINTER) And TObjectType(ty) Then
+			ty = New TObjectType.Create(TObjectType(ty).classDecl)
+			ty._flags :| (_flags & T_POINTER)
+		End If
 		
 		Return ty
 	End Method