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

Last change on this file since 465 was 465, checked in by dai_9181, 16 years ago

Messenger/ErrorMessengerクラスを導入。SetError関数によるエラー生成を廃止した。

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