source: dev/trunk/ab5.0/abdev/BasicCompiler_Common/error.cpp@ 828

Last change on this file since 828 was 828, checked in by イグトランス (egtra), 12 years ago

egtraブランチの内容をマージ。

File size: 6.1 KB
RevLine 
[206]1#include "stdafx.h"
2
[4]3#include "../BasicCompiler_Common/common.h"
4
5#define STRING_SYSTEM_DECLAREHANDLE "*_System_DeclareHandle_"
[828]6void DifferentTypeError(const Type &varType, const Type &calcFormalType, const int iWarning, const char *pszFuncName, const int ParmNum)
7{
[4]8 //////////////////////////
9 // キャストに関する警告
10 //////////////////////////
[828]11 std::ostringstream s;
12 Type calcType(calcFormalType);
[4]13
[828]14 if (IS_LITERAL(calcType.GetIndex()))
[290]15 {
16 calcType.SetIndex( -1 );
17 }
[4]18
[828]19 if (pszFuncName)
20 {
21 s << boost::format("\"%s\"の第%dパラメータが、") % pszFuncName % (ParmNum + 1);
22 }
[4]23
[828]24 std::string varTypeName = compiler.TypeToString(varType);
25 if (memcmp(varTypeName.c_str(), STRING_SYSTEM_DECLAREHANDLE, lstrlen(STRING_SYSTEM_DECLAREHANDLE)) == 0)
[290]26 {
[828]27 varTypeName = varTypeName.substr(strlen(STRING_SYSTEM_DECLAREHANDLE));
[4]28 }
[290]29
[828]30 std::string calcTypeName = compiler.TypeToString(calcType);
31 if (memcmp(calcTypeName.c_str(), STRING_SYSTEM_DECLAREHANDLE, strlen(STRING_SYSTEM_DECLAREHANDLE)) == 0)
[290]32 {
[828]33 calcTypeName = calcTypeName.substr(strlen(STRING_SYSTEM_DECLAREHANDLE));
[4]34 }
[828]35 s << boost::format("%sから%s") % calcTypeName % varTypeName;
[4]36
37 extern int cp;
[828]38 int errorCode;
39 switch (iWarning)
40 {
41 case 1: errorCode = -101; break;
42 case 2: errorCode = -102; break;
43 case 3: errorCode = 50; break;
44 }
45 compiler.errorMessenger.Output(errorCode, s.str().c_str(), cp);
[4]46}
47
[290]48bool CheckDifferentType( const Type &varType,const Type &calcFormalType,const char *pszFuncName,const int ParmNum)
49{
50 Type calcType( calcFormalType );
[4]51
[290]52 if( varType.IsStruct() || calcType.IsStruct() )
53 {
[64]54 //いずれかが構造体場合
[290]55 if( !varType.Equals( calcType ) )
56 {
57 DifferentTypeError( varType, calcType,3,pszFuncName,ParmNum);
[64]58 return false;
59 }
60 }
61
[290]62 if( varType.IsObject() || calcType.IsObject() )
63 {
[424]64 //いずれかがオブジェクトではない場合
65 if( (!varType.IsObject()) || (!calcType.IsObject()) )
[290]66 {
67 DifferentTypeError( varType, calcType,3,pszFuncName,ParmNum);
[45]68 return false;
[4]69 }
[59]70
[448]71 if( varType.IsDelegate() && calcType.IsDelegate() )
[290]72 {
[448]73 // デリゲート同士の比較の場合
74 // ※共変戻り値及び反辺引数をサポートすること
[562]75 const ::Delegate *pVarDelegate = &compiler.GetObjectModule().meta.ToDelegate( varType.GetClass() );
76 const ::Delegate *pCalcDelegate = &compiler.GetObjectModule().meta.ToDelegate( calcType.GetClass() );
77 if( !pVarDelegate->IsSimilar( *pCalcDelegate ) )
[448]78 {
79 // 等しいと見なされないとき
80 DifferentTypeError( varType, calcType,3,pszFuncName,ParmNum);
81 return false;
82 }
[59]83 }
[448]84 else
85 {
86 // 一般的なクラスの比較の場合
87
88 if( !varType.GetClass().IsEqualsOrSubClass( &calcType.GetClass() ) )
89 {
90 //等しくなく、派生クラスでもないとき
91 DifferentTypeError( varType, calcType,3,pszFuncName,ParmNum);
92 return false;
93 }
94 }
[4]95 }
96
[290]97 if( calcType.GetBasicType() & FLAG_PTR )
98 {
[4]99 //配列先頭フラグがたっている場合は、ポインタ型として扱う
[290]100 calcType.SetBasicType( MAKE_PTR_TYPE(NATURAL_TYPE(calcType.GetBasicType()),PTR_LEVEL(calcType.GetBasicType())+1) );
[4]101 }
102
[290]103 if( varType.IsPointer() || calcType.IsPointer() )
104 {
[4]105 /* 右辺及び左辺のいずれかがポインタ型の場合は、
106 型チェックを行う。
107 ・同一の種類のポインタ型以外はエラーとする */
108
[290]109 if( varType.IsPointer() && calcType.GetIndex() == LITERAL_NULL )
110 {
[4]111 //リテラルNULL値の場合
[45]112 return true;
[4]113 }
114
[290]115 if( varType.IsVoidPtr() )
116 {
[4]117 //左辺がVoidPtr型の場合は、ポインタ型すべてを受け入れる
[290]118 if( calcType.IsPointer() )
119 {
120 return true;
121 }
[4]122 }
123
[290]124 if( calcType.IsVoidPtr() )
125 {
[4]126 //右辺がVoidPtr型の場合は、ポインタ型すべてを受け入れる
[290]127 if( varType.IsPointer() )
128 {
129 return true;
130 }
[4]131 }
132
[290]133 if( varType.GetBasicType() != calcType.GetBasicType() )
134 {
135 DifferentTypeError( varType, calcType, 2,pszFuncName,ParmNum);
[45]136 return true;
[4]137 }
138
[290]139 if( varType.IsObjectPtr() )
140 {
[4]141 //双方がオブジェクトポインタ型の場合
[290]142 if( varType.GetIndex() != calcType.GetIndex() )
143 {
144 const CClass *pobj_tempClass = &calcType.GetClass();
[4]145 while(pobj_tempClass&&(!IS_LITERAL((LONG_PTR)pobj_tempClass))){
[204]146 pobj_tempClass=&pobj_tempClass->GetSuperClass();
[4]147
[290]148 if( &varType.GetClass() == pobj_tempClass )
149 {
[4]150 //継承先が等しいとき
[45]151 return true;
[4]152 }
153 }
[290]154 DifferentTypeError( varType, calcType, 2,pszFuncName,ParmNum);
[45]155 return true;
[4]156 }
157 }
158 }
159
[290]160 if( varType.IsDouble() )
161 {
162 if( calcType.Is64() )
163 {
[4]164 //64ビット整数値の場合は警告を出す
[290]165 DifferentTypeError( varType, calcType, 1,pszFuncName,ParmNum);
[4]166 }
167 }
[290]168 else if( varType.IsSingle() )
169 {
170 if( calcType.Is64() || calcType.IsDouble() )
171 {
[4]172 //64ビット整数値、またはDouble型の場合は警告を出す
[290]173 DifferentTypeError( varType, calcType, 1,pszFuncName,ParmNum);
[4]174 }
175 }
[290]176 else if( varType.GetSize() == sizeof(char) )
177 {
178 if( calcType.GetSize() > sizeof(char)
179 && calcType.GetIndex() != LITERAL_NULL
180 && calcType.GetIndex() != LITERAL_M128_0
181 && calcType.GetIndex() != LITERAL_0_255 )
182 {
[4]183 //8ビット整数値より大きな型で、リテラル値でもない場合は警告を出す
[290]184 DifferentTypeError( varType, calcType, 1,pszFuncName,ParmNum);
[4]185 }
186 }
[290]187 else if( varType.GetSize() == sizeof(short) )
188 {
189 if( calcType.GetSize() > sizeof(short)
190 && calcType.GetIndex() != LITERAL_NULL
191 && calcType.GetIndex() != LITERAL_M128_0
192 && calcType.GetIndex() != LITERAL_0_255
193 && calcType.GetIndex() != LITERAL_M32768_0
194 && calcType.GetIndex() != LITERAL_0_65535 )
195 {
[4]196 //16ビット整数値より大きな型で、リテラル値でもない場合は警告を出す
[290]197 DifferentTypeError( varType, calcType, 1,pszFuncName,ParmNum);
[4]198 }
199 }
[290]200 else if( varType.GetSize() == sizeof(long) )
201 {
202 if( calcType.IsReal()
203 || ( calcType.IsWhole() && calcType.GetSize() > sizeof(long) && calcType.GetIndex() != LITERAL_NULL ) )
204 {
[4]205 /* 32ビット整数値より大きな型、または実数、
206 またはリテラル値でもない場合は警告を出す */
[290]207 DifferentTypeError( varType, calcType, 1,pszFuncName,ParmNum);
[4]208 }
209 }
[290]210 else if( varType.GetSize() == sizeof(_int64) )
211 {
212 if( calcType.IsReal() )
213 {
[4]214 //実数の場合は警告を出す
[290]215 DifferentTypeError( varType, calcType, 1,pszFuncName,ParmNum);
[4]216 }
217 }
[45]218
219 return true;
[4]220}
Note: See TracBrowser for help on using the repository browser.