浏览代码

Fixed struct initialisation. StaticArray requires init.

Brucey 5 年之前
父节点
当前提交
d49e619942
共有 2 个文件被更改,包括 56 次插入12 次删除
  1. 45 8
      ctranslator.bmx
  2. 11 4
      parser.bmx

+ 45 - 8
ctranslator.bmx

@@ -406,7 +406,7 @@ Type TCTranslator Extends TTranslator
 		Return TransType( ty, ident )
 	End Method
 
-	Method TransValue$( ty:TType,value$ )
+	Method TransValue$( ty:TType,value$, isStructInit:Int = False )
 		If value
 			If IsPointerType(ty, 0, TType.T_POINTER) Return value
 			If TBoolType( ty ) Return "1"
@@ -470,8 +470,31 @@ Type TCTranslator Extends TTranslator
 				End If
 			End If
 			If TNumericType( ty ) Return "0" ' numeric and pointers
-			If TStringType( ty ) Return Bra("&bbEmptyString")
-			If TArrayType( ty ) Return Bra("&bbEmptyArray")
+			If TStringType( ty ) Then
+				If isStructInit Then
+					Return "&bbEmptyString"
+				Else
+					Return Bra("&bbEmptyString")
+				End If
+			End If
+			If TArrayType( ty ) Then
+				If isStructInit Then 
+					If TArrayType( ty ).isStatic Then
+						Local t:String = "{"
+						For Local i:Int = 0 Until Int(TArrayType( ty ).length)
+							If i Then
+								t :+ ","
+							End If
+							t :+ TransValue(TArrayType( ty ).elemType, "", True)
+						Next
+						Return t + "}"
+					Else
+						Return "&bbEmptyArray"
+					End If
+				Else
+					Return Bra("&bbEmptyArray")
+				End If
+			End If
 			If TObjectType( ty ) Then
 				If TObjectType( ty ).classDecl.IsExtern() Or TObjectType( ty ).classDecl.IsStruct() Then
 					If TObjectType( ty ).classDecl.IsInterface() Or IsPointerType(ty,0,TType.T_POINTER) Or (Not TObjectType( ty ).classDecl.IsStruct()) Then
@@ -479,7 +502,12 @@ Type TCTranslator Extends TTranslator
 					Else
 						If TObjectType( ty ).classDecl.IsStruct() Then
 
-							Local t:String = "((" + TransType(ty, "") + "){"
+							Local t:String
+							If Not isStructInit Then
+								t = "((" + TransType(ty, "") + "){"
+							Else
+								t = "{"
+							End If
 							Local fields:Int
 							For Local f:TFieldDecl = EachIn TObjectType( ty ).classDecl.Decls()
 								If fields Then
@@ -487,15 +515,24 @@ Type TCTranslator Extends TTranslator
 								End If
 								fields = True
 								
-								t :+ TransValue(f.ty, "")
+								t :+ TransValue(f.ty, "", True)
 							Next
-							Return t + "})"
+							If Not isStructInit Then
+								t :+ "})"
+							Else
+								t :+ "}"
+							End If
+							Return t 
 						Else
 							Return "{}"
 						End If
 					End If
 				Else
-					Return Bra("&bbNullObject")
+					If isStructInit Then
+						Return "&bbNullObject"
+					Else
+						Return Bra("&bbNullObject")
+					End If
 				End If
 			End If
 			If TFunctionPtrType( ty) Return "(&brl_blitz_NullFunctionError)" ' todo ??
@@ -5005,7 +5042,7 @@ End Rem
 					t :+ ","
 				End If
 				fields = True
-				t :+ TransValue(f.ty, "")
+				t :+ TransValue(f.ty, "", True)
 			Next
 			Emit t + "};"
 		Else

+ 11 - 4
parser.bmx

@@ -1014,8 +1014,12 @@ Type TParser Extends TGenProcessor
 		
 		' array or function pointer?
 		Repeat
-			If (_toke = "[" Or _toke = "[]") And IsArrayDef(attr & DECL_STATIC > 0)
-				ty = ParseArrayType(ty, attr & DECL_STATIC > 0)
+			If (_toke = "[" Or _toke = "[]") And IsArrayDef() Then
+				If Not IsArrayDef(attr & DECL_STATIC > 0) Then
+					Err "Invalid static array initialization."
+				Else
+					ty = ParseArrayType(ty, attr & DECL_STATIC > 0)
+				End If
 			Else If _toke = "(" Then
 				Local args:TArgDecl[] = ParseFuncParamDecl()
 				attr :| ParseCallConvention(attr & DECL_API_STDCALL)
@@ -2558,6 +2562,9 @@ End Rem
 		SetErr
 
 		If CParse("staticarray") Then
+			If toke = "const" Then
+				Err "Const cannot be used in this way"
+			End If
 			If attrs & DECL_STATIC Then
 				Err "Already declared as a static array"
 			End If
@@ -2570,7 +2577,7 @@ End Rem
 		
 		
 		If attrs & DECL_EXTERN
-			ty=ParseDeclType(attrs & DECL_API_STDCALL)
+			ty=ParseDeclType(attrs & (DECL_STATIC | DECL_API_STDCALL))
 			
 			If toke = "const" Then
 				If CParse("=") Then
@@ -2581,7 +2588,7 @@ End Rem
 '			init=ParseExpr()
 '			ty = init.exprType
 		Else
-			ty=ParseDeclType(attrs & DECL_API_STDCALL)
+			ty=ParseDeclType(attrs & (DECL_STATIC | DECL_API_STDCALL))
 
 			If CParse( "=" )
 				If (attrs & DECL_STATIC) Then