#include "stdafx.h" #include "../BasicCompiler_Common/common.h" #define STRING_SYSTEM_DECLAREHANDLE "*_System_DeclareHandle_" void DifferentTypeError(const Type &varType, const Type &calcFormalType, const int iWarning, const char *pszFuncName, const int ParmNum) { ////////////////////////// // キャストに関する警告 ////////////////////////// std::ostringstream s; Type calcType(calcFormalType); if (IS_LITERAL(calcType.GetIndex())) { calcType.SetIndex( -1 ); } if (pszFuncName) { s << boost::format("\"%s\"の第%dパラメータが、") % pszFuncName % (ParmNum + 1); } std::string varTypeName = compiler.TypeToString(varType); if (memcmp(varTypeName.c_str(), STRING_SYSTEM_DECLAREHANDLE, lstrlen(STRING_SYSTEM_DECLAREHANDLE)) == 0) { varTypeName = varTypeName.substr(strlen(STRING_SYSTEM_DECLAREHANDLE)); } std::string calcTypeName = compiler.TypeToString(calcType); if (memcmp(calcTypeName.c_str(), STRING_SYSTEM_DECLAREHANDLE, strlen(STRING_SYSTEM_DECLAREHANDLE)) == 0) { calcTypeName = calcTypeName.substr(strlen(STRING_SYSTEM_DECLAREHANDLE)); } s << boost::format("%sから%s") % calcTypeName % varTypeName; extern int cp; int errorCode; switch (iWarning) { case 1: errorCode = -101; break; case 2: errorCode = -102; break; case 3: errorCode = 50; break; } compiler.errorMessenger.Output(errorCode, s.str().c_str(), cp); } bool CheckDifferentType( const Type &varType,const Type &calcFormalType,const char *pszFuncName,const int ParmNum) { Type calcType( calcFormalType ); if( varType.IsStruct() || calcType.IsStruct() ) { //いずれかが構造体場合 if( !varType.Equals( calcType ) ) { DifferentTypeError( varType, calcType,3,pszFuncName,ParmNum); return false; } } if( varType.IsObject() || calcType.IsObject() ) { //いずれかがオブジェクトではない場合 if( (!varType.IsObject()) || (!calcType.IsObject()) ) { DifferentTypeError( varType, calcType,3,pszFuncName,ParmNum); return false; } if( varType.IsDelegate() && calcType.IsDelegate() ) { // デリゲート同士の比較の場合 // ※共変戻り値及び反辺引数をサポートすること const ::Delegate *pVarDelegate = &compiler.GetObjectModule().meta.ToDelegate( varType.GetClass() ); const ::Delegate *pCalcDelegate = &compiler.GetObjectModule().meta.ToDelegate( calcType.GetClass() ); if( !pVarDelegate->IsSimilar( *pCalcDelegate ) ) { // 等しいと見なされないとき DifferentTypeError( varType, calcType,3,pszFuncName,ParmNum); return false; } } else { // 一般的なクラスの比較の場合 if( !varType.GetClass().IsEqualsOrSubClass( &calcType.GetClass() ) ) { //等しくなく、派生クラスでもないとき DifferentTypeError( varType, calcType,3,pszFuncName,ParmNum); return false; } } } if( calcType.GetBasicType() & FLAG_PTR ) { //配列先頭フラグがたっている場合は、ポインタ型として扱う calcType.SetBasicType( MAKE_PTR_TYPE(NATURAL_TYPE(calcType.GetBasicType()),PTR_LEVEL(calcType.GetBasicType())+1) ); } if( varType.IsPointer() || calcType.IsPointer() ) { /* 右辺及び左辺のいずれかがポインタ型の場合は、 型チェックを行う。 ・同一の種類のポインタ型以外はエラーとする */ if( varType.IsPointer() && calcType.GetIndex() == LITERAL_NULL ) { //リテラルNULL値の場合 return true; } if( varType.IsVoidPtr() ) { //左辺がVoidPtr型の場合は、ポインタ型すべてを受け入れる if( calcType.IsPointer() ) { return true; } } if( calcType.IsVoidPtr() ) { //右辺がVoidPtr型の場合は、ポインタ型すべてを受け入れる if( varType.IsPointer() ) { return true; } } if( varType.GetBasicType() != calcType.GetBasicType() ) { DifferentTypeError( varType, calcType, 2,pszFuncName,ParmNum); return true; } if( varType.IsObjectPtr() ) { //双方がオブジェクトポインタ型の場合 if( varType.GetIndex() != calcType.GetIndex() ) { const CClass *pobj_tempClass = &calcType.GetClass(); while(pobj_tempClass&&(!IS_LITERAL((LONG_PTR)pobj_tempClass))){ pobj_tempClass=&pobj_tempClass->GetSuperClass(); if( &varType.GetClass() == pobj_tempClass ) { //継承先が等しいとき return true; } } DifferentTypeError( varType, calcType, 2,pszFuncName,ParmNum); return true; } } } if( varType.IsDouble() ) { if( calcType.Is64() ) { //64ビット整数値の場合は警告を出す DifferentTypeError( varType, calcType, 1,pszFuncName,ParmNum); } } else if( varType.IsSingle() ) { if( calcType.Is64() || calcType.IsDouble() ) { //64ビット整数値、またはDouble型の場合は警告を出す DifferentTypeError( varType, calcType, 1,pszFuncName,ParmNum); } } else if( varType.GetSize() == sizeof(char) ) { if( calcType.GetSize() > sizeof(char) && calcType.GetIndex() != LITERAL_NULL && calcType.GetIndex() != LITERAL_M128_0 && calcType.GetIndex() != LITERAL_0_255 ) { //8ビット整数値より大きな型で、リテラル値でもない場合は警告を出す DifferentTypeError( varType, calcType, 1,pszFuncName,ParmNum); } } else if( varType.GetSize() == sizeof(short) ) { if( calcType.GetSize() > sizeof(short) && calcType.GetIndex() != LITERAL_NULL && calcType.GetIndex() != LITERAL_M128_0 && calcType.GetIndex() != LITERAL_0_255 && calcType.GetIndex() != LITERAL_M32768_0 && calcType.GetIndex() != LITERAL_0_65535 ) { //16ビット整数値より大きな型で、リテラル値でもない場合は警告を出す DifferentTypeError( varType, calcType, 1,pszFuncName,ParmNum); } } else if( varType.GetSize() == sizeof(long) ) { if( calcType.IsReal() || ( calcType.IsWhole() && calcType.GetSize() > sizeof(long) && calcType.GetIndex() != LITERAL_NULL ) ) { /* 32ビット整数値より大きな型、または実数、 またはリテラル値でもない場合は警告を出す */ DifferentTypeError( varType, calcType, 1,pszFuncName,ParmNum); } } else if( varType.GetSize() == sizeof(_int64) ) { if( calcType.IsReal() ) { //実数の場合は警告を出す DifferentTypeError( varType, calcType, 1,pszFuncName,ParmNum); } } return true; }