Index: BasicCompiler64/NumOpe.cpp
===================================================================
--- BasicCompiler64/NumOpe.cpp	(revision 69)
+++ BasicCompiler64/NumOpe.cpp	(revision 75)
@@ -19,6 +19,5 @@
 
 		extern CClass *pobj_StringClass;
-		TYPEINFO baseTypeInfo = { DEF_OBJECT, (LONG_PTR)pobj_StringClass };
-		Operator_New( *pobj_StringClass, "", parameter, baseTypeInfo );
+		Operator_New( *pobj_StringClass, "", parameter, Type( DEF_OBJECT, *pobj_StringClass ) );
 
 		free( parameter );
@@ -70,23 +69,43 @@
 }
 
-int NumOpe(int *pReg,const char *Command,int BaseType,LONG_PTR lpBaseIndex,LONG_PTR *plpIndex,BOOL *pbUseHeap){
-	extern HANDLE hHeap;
+void ExtendRegToBigType( int reg, int bigBasicType, int baseBasicType ){
+	switch( Type::GetBasicSize( bigBasicType ) ){
+		case sizeof(_int64):
+			ExtendTypeTo64(baseBasicType,reg);
+			break;
+		case sizeof(long):
+			ExtendTypeTo32(baseBasicType,reg);
+			break;
+		case sizeof(short):
+			ExtendTypeTo16(baseBasicType,reg);
+			break;
+	}
+}
+
+bool NumOpe( int *pReg,
+			const char *expression,
+			const Type &baseType,
+			Type &resultType,
+			BOOL *pbUseHeap ){
+
 	int i,i2,i3,i4;
 	char temporary[1024],temp2[1024],temp3[1024];
 
-	if(Command[0]=='\0'){
+	if(expression[0]=='\0'){
 		SetError(1,NULL,cp);
-		return 0;
-	}
-
-	if(Command[0]==1&& Command[1]==ESC_NEW ){
+		return false;
+	}
+
+	if(expression[0]==1&& expression[1]==ESC_NEW ){
 		//New演算子（オブジェクト生成）
-		TYPEINFO baseTypeInfo = { BaseType, lpBaseIndex };
-		int resultType = Operator_New(Command+2,plpIndex, baseTypeInfo );
+
+		if( !Operator_New( expression+2, baseType, resultType ) ){
+			return false;
+		}
 
 		//mov reg,rax
 		op_mov_RR( *pReg, REG_RAX );
 
-		return resultType;
+		return true;
 	}
 
@@ -100,5 +119,5 @@
 	long stack[255];
 	int pnum;
-	if(!GetNumOpeElements(Command,&pnum,values,calc,stack)){
+	if(!GetNumOpeElements(expression,&pnum,values,calc,stack)){
 		for(i=0;i<pnum;i++){
 			if(values[i]) HeapDefaultFree(values[i]);
@@ -156,5 +175,5 @@
 	double dbl;
 	int sp;
-	int type[255];
+	int type_stack[255];
 	LONG_PTR index_stack[255];
 	BOOL bUseHeap[255];
@@ -167,8 +186,7 @@
 
 		if(idCalc){
-			if(type[sp-2]==DEF_OBJECT){
+			if(type_stack[sp-2]==DEF_OBJECT){
 				//オーバーロードされたオペレータを呼び出す
-				TYPEINFO BaseTypeInfo={BaseType,lpBaseIndex};
-				i2=CallOperatorProc(idCalc,&BaseTypeInfo,type,index_stack,bUseHeap,sp);
+				i2=CallOperatorProc(idCalc,baseType,type_stack,index_stack,bUseHeap,sp);
 				if(i2==0){
 					if(idCalc==CALC_EQUAL) lstrcpy(temp2,"==");
@@ -183,5 +201,5 @@
 			}
 
-			if(!CheckCalcType(idCalc,type,sp)) goto error;
+			if(!CheckCalcType(idCalc,type_stack,sp)) goto error;
 		}
 
@@ -209,9 +227,6 @@
 StrLiteral:
 					
-					if(BaseType==DEF_OBJECT){
-						CClass *pobj_Class;
-						pobj_Class=(CClass *)lpBaseIndex;
-						TYPEINFO BaseTypeInfo = {BaseType,lpBaseIndex};
-						if(IsStringObjectType(BaseTypeInfo)){
+					if( baseType.IsObject() ){
+						if( baseType.IsStringObject() ){
 							//要求タイプがStringのとき
 
@@ -220,5 +235,5 @@
 
 							extern CClass *pobj_StringClass;
-							type[sp]=DEF_OBJECT;
+							type_stack[sp]=DEF_OBJECT;
 							index_stack[sp]=(LONG_PTR)pobj_StringClass;
 							bLiteralCalculation=0;
@@ -232,5 +247,5 @@
 					}
 
-					type[sp]=typeOfPtrChar;
+					type_stack[sp]=typeOfPtrChar;
 					bLiteralCalculation=0;
 
@@ -277,6 +292,7 @@
 						int idProc;
 						void *pInfo;
-						idProc=GetProc(temporary,&pInfo);
-
+						idProc=GetProc(temporary,(void **)&pInfo);
+
+						Type resultType;
 						if(idProc){
 							//閉じカッコ")"に続く文字がNULLでないとき
@@ -302,6 +318,6 @@
 								////////////////
 
-								i2=CallProc(idProc,pInfo,temporary,temp2,&index_stack[sp]);
-								if(i2==-1){
+								CallProc(idProc,pInfo,temporary,temp2,resultType);
+								if(resultType.IsNull()){
 									//戻り値が存在しないとき
 									for(i2=2;;i2++){
@@ -325,10 +341,17 @@
 
 								//大きな型への暗黙の変換
-								type[sp]=AutoBigCast(BaseType,i2);
+								type_stack[sp]=AutoBigCast(baseType.GetBasicType(),resultType.GetBasicType());
+								index_stack[sp] = resultType.GetIndex();
 								bLiteralCalculation=0;
 
-								SetUseRegFromRax(i2,UseReg,XmmReg);
-
-								if(IsRealNumberType(i2)) bXmm=1;
+								if( type_stack[sp] != resultType.GetBasicType() ){
+									// 大きな型へ変換された場合
+									// ※レジスタの値をキャストする
+									ExtendRegToBigType( REG_RAX, type_stack[sp], resultType.GetBasicType() );
+								}
+
+								SetUseRegFromRax(resultType.GetBasicType(),UseReg,XmmReg);
+
+								if(resultType.IsReal()) bXmm=1;
 								else bXmm=0;
 
@@ -342,5 +365,5 @@
 							else pobj_reg->LockReg();
 
-							if(i2==DEF_STRUCT){
+							if(resultType.IsStruct()){
 								//構造体が戻ったときはヒープ領域にインスタンスが格納されている
 								//※後にfreeする必要あり
@@ -360,14 +383,15 @@
 
 							//マクロ関数の場合
-							i2=NumOpe(&UseReg,temp3,0,0,&index_stack[sp]);
-
-							if(!IS_LITERAL(index_stack[sp])){
+							NumOpe(&UseReg,temp3,Type(),resultType);
+
+							if(!IS_LITERAL(resultType.GetIndex())){
 								//リテラル値ではなかったとき
 								bLiteralCalculation=0;
 							}
 
-							type[sp]=i2;
-
-							if(IsRealNumberType(i2)) pobj_reg->LockXmmReg();
+							type_stack[sp] = resultType.GetBasicType();
+							index_stack[sp] = resultType.GetIndex();
+
+							if(resultType.IsReal()) pobj_reg->LockXmmReg();
 							else pobj_reg->LockReg();
 
@@ -381,16 +405,15 @@
 					//インデクサ（getアクセサ）
 					char variable[VN_SIZE],array_element[VN_SIZE];
-					CClass *pobj_c;
 					GetArrayElement(term,variable,array_element);
 					if(array_element[0]){
-						i2=GetVarType(variable,(LONG_PTR *)&pobj_c,0);
-						if(i2==DEF_OBJECT){
-							TYPEINFO RetTypeInfo;
-							CallIndexerGetterProc(UseReg,pobj_c,variable,array_element,RetTypeInfo);
-							type[sp]=RetTypeInfo.type;
-							index_stack[sp]=RetTypeInfo.u.lpIndex;
+						Type resultType;
+						GetVarType(variable,resultType,0);
+						if( resultType.IsObject() ){
+							CallIndexerGetterProc(UseReg,&resultType.GetClass(),variable,array_element,resultType);
+							type_stack[sp]=resultType.GetBasicType();
+							index_stack[sp]=resultType.GetIndex();
 							bLiteralCalculation=0;
 
-							if(IsRealNumberType(RetTypeInfo.type)) pobj_reg->LockXmmReg();
+							if(resultType.IsReal()) pobj_reg->LockXmmReg();
 							else pobj_reg->LockReg();
 							sp++;
@@ -402,7 +425,7 @@
 					// Nothing
 					if( lstrcmp( term, "Nothing" ) == 0 ){
-						type[sp] = DEF_OBJECT;
-						if( BaseType == DEF_OBJECT ){
-							index_stack[sp] = lpBaseIndex;
+						type_stack[sp] = DEF_OBJECT;
+						if( baseType.IsObject() ){
+							index_stack[sp] = baseType.GetIndex();
 						}
 						else{
@@ -427,9 +450,10 @@
 
 					RELATIVE_VAR RelativeVar;
+					Type varType;
 					if(GetVarOffset(
 						false,	//エラー表示あり
 						false,	//読み込み専用
 						term,
-						&i2,&RelativeVar,&index_stack[sp])){
+						&RelativeVar,varType)){
 						//////////
 						// 変数
@@ -437,38 +461,38 @@
 
 						//大きな型への暗黙の変換
-						type[sp]=AutoBigCast(BaseType,i2);
+						type_stack[sp]=AutoBigCast(baseType.GetBasicType(),varType.GetBasicType());
+						index_stack[sp] = varType.GetIndex();
 						bLiteralCalculation=0;
 
-						if(type[sp]!=i2){
-							//大きな型へ変換された場合（レジスタを0に初期化する）
-
-							//xor reg,reg
-							op_zero_reg(UseReg);
-						}
-
-						if(i2&FLAG_PTR){
+						if(varType.GetBasicType()&FLAG_PTR){
 							//配列ポインタ
-							type[sp]=GetPtrType(i2^FLAG_PTR,index_stack[sp]);
+							type_stack[sp]=GetPtrType(varType.GetBasicType()^FLAG_PTR);
 
 							SetVarPtrToReg(UseReg,&RelativeVar);
 						}
-						else if(IsRealNumberType(i2)){
+						else if(varType.IsReal()){
 							//実数型
 							bXmm=1;
 
-							if(i2==DEF_DOUBLE)
+							if( varType.IsDouble() )
 								SetXmmReg_DoubleVariable(&RelativeVar,XmmReg);
-							if(i2==DEF_SINGLE)
+							if( varType.IsSingle() )
 								SetXmmReg_SingleVariable(&RelativeVar,XmmReg);
 						}
-						else if(IsWholeNumberType(i2) || i2==DEF_OBJECT){
+						else if( varType.IsWhole() || varType.IsObject()){
 							//整数型
-							SetReg_WholeVariable(i2,&RelativeVar,UseReg);
-						}
-						else if(i2==DEF_STRUCT){
+							SetReg_WholeVariable(varType.GetBasicType(),&RelativeVar,UseReg);
+						}
+						else if( varType.IsStruct() ){
 							//構造体ポインタをUseRegへ格納（構造体は値型）
 							SetVarPtrToReg(UseReg,&RelativeVar);
 						}
 						else SetError(11,term,cp);
+
+						if( type_stack[sp] != varType.GetBasicType() ){
+							// 大きな型へ変換された場合
+							// ※レジスタの値をキャストする
+							ExtendRegToBigType( UseReg, type_stack[sp], varType.GetBasicType() );
+						}
 
 						if(bXmm==0&&UseReg==REG_R14){
@@ -477,9 +501,9 @@
 						}
 						if(bXmm&&XmmReg==REG_XMM4){
-							if(i2==DEF_DOUBLE){
+							if(varType.IsDouble()){
 								//movsd qword ptr[rsp+offset],xmm4	※スタックフレームを利用
 								pobj_sf->push(REG_XMM4,sizeof(double));
 							}
-							if(i2==DEF_SINGLE){
+							if(varType.IsSingle()){
 								//movss dword ptr[rsp+offset],xmm4	※スタックフレームを利用
 								pobj_sf->push(REG_XMM4,sizeof(float));
@@ -500,5 +524,5 @@
 					i3 = CDBConst::obj.GetType(term);
 					if(i3){
-						type[sp] = i3;
+						type_stack[sp] = i3;
 						if(IsRealNumberType(i3)){
 							//実数
@@ -536,5 +560,5 @@
 					i3=GetTypeFixed(term,&lp);
 					if(i3!=-1){
-						type[sp]=i3|FLAG_CAST;
+						type_stack[sp]=i3|FLAG_CAST;
 						index_stack[sp]=lp;
 						sp++;
@@ -559,16 +583,21 @@
 						//////////////////////////////////////////////////////
 
-							TYPEINFO RetTypeInfo;
-							CallPropertyMethod(term,NULL,&RetTypeInfo);
+							Type resultType;
+							CallPropertyMethod(term,NULL,resultType);
 
 							//大きな型への暗黙の変換
-							type[sp]=AutoBigCast(BaseType,RetTypeInfo.type);
-
-							index_stack[sp]=RetTypeInfo.u.lpIndex;
+							type_stack[sp]=AutoBigCast(baseType.GetBasicType(),resultType.GetBasicType());
+							index_stack[sp]=resultType.GetIndex();
 							bLiteralCalculation=0;
 
-							SetUseRegFromRax(RetTypeInfo.type,UseReg,XmmReg);
-
-							if(IsRealNumberType(type[sp])) bXmm=1;
+							if( type_stack[sp] != resultType.GetBasicType() ){
+								// 大きな型へ変換された場合
+								// ※レジスタの値をキャストする
+								ExtendRegToBigType( REG_RAX, type_stack[sp], resultType.GetBasicType() );
+							}
+
+							SetUseRegFromRax(resultType.GetBasicType(),UseReg,XmmReg);
+
+							if(IsRealNumberType(type_stack[sp])) bXmm=1;
 							else bXmm=0;
 
@@ -578,5 +607,5 @@
 						}////////////////////////////////////////////
 
-						if(type[sp]==DEF_STRUCT){
+						if(type_stack[sp]==DEF_STRUCT){
 							//構造体が戻ったときはヒープ領域にインスタンスが格納されている
 							//※後にfreeする必要あり
@@ -595,11 +624,11 @@
 					bError=1;
 					SetError(3,term,cp);
-					type[sp]=DEF_DOUBLE;
+					type_stack[sp]=DEF_DOUBLE;
 				}
 				else{
 					//リテラル値
-					type[sp]=GetLiteralValue(term,&i64data,BaseType);
+					type_stack[sp]=GetLiteralValue(term,&i64data,baseType.GetBasicType());
 Literal:
-					if(type[sp]==DEF_DOUBLE){
+					if(type_stack[sp]==DEF_DOUBLE){
 						//64ビット浮動小数型
 						bXmm=1;
@@ -627,5 +656,5 @@
 						}
 					}
-					else if(type[sp]==DEF_SINGLE){
+					else if(type_stack[sp]==DEF_SINGLE){
 						//32ビット浮動小数型
 						bXmm=1;
@@ -680,10 +709,10 @@
 			case CALC_OR:
 			case CALC_AND:
-				if(!CalcTwoTerm_Logical(idCalc,type,index_stack,&sp)) goto error;
+				if(!CalcTwoTerm_Logical(idCalc,type_stack,index_stack,&sp)) goto error;
 				break;
 			case CALC_NOT:
 				//value[sp-1]=Not value[sp-1]
 				//NOT演算子
-				if(!Calc_Not(type,sp)) goto error;
+				if(!Calc_Not(type_stack,sp)) goto error;
 				break;
 
@@ -695,5 +724,5 @@
 			case CALC_NOTEQUAL:	//value[sp-2] <> value[sp-1]
 			case CALC_EQUAL:	//value[sp-2] =  value[sp-1]				
-				if(!CalcTwoTerm_Relational(idCalc,type,index_stack,&sp)) goto error;
+				if(!CalcTwoTerm_Relational(idCalc,type_stack,index_stack,&sp)) goto error;
 				break;
 
@@ -701,5 +730,5 @@
 			case CALC_SHL:	//value[sp-2] << value[sp-1]
 			case CALC_SHR:	//value[sp-2] >> value[sp-1]
-				if(!Calc_Shift(idCalc,type,&sp)) goto error;
+				if(!Calc_Shift(idCalc,type_stack,&sp)) goto error;
 				break;
 
@@ -708,37 +737,37 @@
 			case CALC_SUBTRACTION:
 			case CALC_PRODUCT:
-				if(!CalcTwoTerm_Arithmetic(idCalc,type,index_stack,&sp)) goto error;
+				if(!CalcTwoTerm_Arithmetic(idCalc,type_stack,index_stack,&sp)) goto error;
 				break;
 			case CALC_MOD:
 				//value[sp-2]%=value[sp-1]
 				//剰余演算
-				if(!Calc_Mod(type,index_stack,&sp)) goto error;
+				if(!Calc_Mod(type_stack,index_stack,&sp)) goto error;
 				break;
 			case CALC_QUOTIENT:
 				//value[sp-2]/=value[sp-1];
 				//除算
-				if(!Calc_Divide(type,&sp,BaseType)) goto error;
+				if(!Calc_Divide(type_stack,&sp,baseType.GetBasicType())) goto error;
 				break;
 			case CALC_INTQUOTIENT:
 				//value[sp-2]/=value[sp-1]
 				//整数除算
-				if(!Calc_IntDivide(type,index_stack,&sp)) goto error;
+				if(!Calc_IntDivide(type_stack,index_stack,&sp)) goto error;
 				break;
 			case CALC_MINUSMARK:
 				//value[sp-1]=-value[sp-1]
 				//符号反転
-				if(!Calc_MinusMark(type,sp)) goto error;
+				if(!Calc_MinusMark(type_stack,sp)) goto error;
 				break;
 			case CALC_POWER:
 				//べき乗演算（浮動小数点演算のみ）
-				if(!Calc_Power(type,&sp)) goto error;
+				if(!Calc_Power(type_stack,&sp)) goto error;
 				break;
 			case CALC_AS:
 				//キャスト
-				if(!Calc_Cast(type,index_stack,&sp)) goto error;
+				if(!Calc_Cast(type_stack,index_stack,&sp)) goto error;
 				break;
 			case CALC_BYVAL:
 				//ポインタ型→参照型
-				if( PTR_LEVEL( type[sp-1] ) <= 0 ){
+				if( PTR_LEVEL( type_stack[sp-1] ) <= 0 ){
 					//ポインタ型ではないとき
 					SetError( 3, NULL, cp );
@@ -746,5 +775,5 @@
 				}
 
-				type[sp-1] = PTR_LEVEL_DOWN( type[sp-1] );
+				type_stack[sp-1] = PTR_LEVEL_DOWN( type_stack[sp-1] );
 
 				break;
@@ -765,6 +794,6 @@
 	if(bLiteralCalculation){
 		//右辺値が数値の定数式の場合
-		LONG_PTR lpClassIndex;
-		i2=StaticCalculation(true, Command,BaseType,&i64data,&lpClassIndex);
+		Type resultType;
+		StaticCalculation(true, expression,baseType.GetBasicType(),&i64data,resultType);
 
 		obp=BeforeObp;
@@ -775,10 +804,10 @@
 		*pobj_reg=objReg_Backup;
 
-		if(IsRealNumberType(i2)){
-			if(IsRealNumberType(BaseType)) i2=BaseType;
+		if(resultType.IsReal()){
+			if(baseType.IsReal()) resultType=baseType;
 
 			XmmReg=pobj_reg->LockXmmReg();
 
-			if(i2==DEF_DOUBLE){
+			if(resultType.IsDouble()){
 				i3 = dataTable.Add( i64data );
 
@@ -793,5 +822,5 @@
 				obp+=sizeof(long);
 			}
-			if(i2==DEF_SINGLE){
+			if(resultType.IsSingle()){
 				memcpy(&dbl,&i64data,sizeof(_int64));
 
@@ -815,11 +844,11 @@
 		}
 		else{
-			if(!Is64Type(i2)){
+			if(!resultType.Is64()){
 				//整数（符号有り/無し）
 
 				i3=(long)i64data;
 
-				if(GetTypeSize(i2,-1)==sizeof(char)) i3=i3&0x000000FF;
-				if(GetTypeSize(i2,-1)==sizeof(short)) i3=i3&0x0000FFFF;
+				if(resultType.GetBasicSize()==sizeof(char)) i3=i3&0x000000FF;
+				if(resultType.GetBasicSize()==sizeof(short)) i3=i3&0x0000FFFF;
 
 				i64data=(_int64)i3;
@@ -832,6 +861,6 @@
 		}
 
-		type[0]=i2;
-		index_stack[0]=lpClassIndex;
+		type_stack[0]=resultType.GetBasicType();
+		index_stack[0]=resultType.GetIndex();
 	}
 	else{
@@ -840,8 +869,7 @@
 	}
 
-	if(plpIndex) *plpIndex=index_stack[0];
 	if(pbUseHeap) *pbUseHeap=bUseHeap[0];
 
-	if(IsRealNumberType(type[0]))
+	if(IsRealNumberType(type_stack[0]))
 		*pReg=pobj_reg->UnlockXmmReg();
 	else
@@ -858,6 +886,7 @@
 	}
 
-	int RetType;
-	RetType=type[0];
+	resultType.SetType( type_stack[0], index_stack[0] );
+
+	bool isSuccessful = true;
 	goto finish;
 
@@ -878,5 +907,5 @@
 	}
 
-	RetType=-1;
+	isSuccessful = false;
 	goto finish;
 
@@ -893,4 +922,4 @@
 	delete pobj_BackReloc;
 
-	return RetType;
+	return isSuccessful;
 }
