source: dev/trunk/abdev/BasicCompiler_Common/src/DataTable.cpp@ 409

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

クラス継承を行う際、dynamicMembersの中身をコピーする方式をやめ、基底クラスのものを参照するように修正した。

File size: 9.5 KB
Line 
1#include "stdafx.h"
2
3#include <jenga/include/smoothie/Smoothie.h>
4
5#include <DataTable.h>
6
7#include <memory.h>
8#include <stdlib.h>
9
10int DataTable::AddBinary( const void *buffer, int size ){
11 int retSize = this->size;
12
13 Realloc( this->size + size );
14 memcpy( (char *)this->buffer + retSize, buffer, size );
15
16 return retSize;
17}
18int DataTable::Add( _int64 i64data ){
19 int retSize = size;
20 AddBinary( &i64data, sizeof( _int64 ) );
21 return retSize;
22}
23int DataTable::Add( int i32data ){
24 int retSize = size;
25 AddBinary( &i32data, sizeof( int ) );
26 return retSize;
27}
28int DataTable::Add( double dbl ){
29 int retSize = size;
30 AddBinary( &dbl, sizeof( double ) );
31 return retSize;
32}
33int DataTable::Add( float flt ){
34 int retSize = size;
35 AddBinary( &flt, sizeof( float ) );
36 return retSize;
37}
38int DataTable::AddString( const char *str, int length ){
39 int retSize = size;
40
41 if( Smoothie::IsUnicode() ){
42 //Shift-JIS → Unicode
43 int size = MultiByteToWideChar(
44 CP_ACP,
45 0,
46 str, length + 1,
47 NULL, 0 ) * 2;
48
49 LPWSTR pwstr = (LPWSTR)malloc( size );
50
51 MultiByteToWideChar(
52 CP_ACP,
53 0,
54 str, length + 1,
55 pwstr, length + 1 );
56
57 AddBinary( pwstr, size );
58
59 free( pwstr );
60 }
61 else{
62 AddBinary( str, length + 1 );
63 }
64
65 return retSize;
66}
67int DataTable::AddString( const char *str )
68{
69 return AddString( str, lstrlen( str ) );
70}
71int DataTable::AddString( const std::string &str )
72{
73 return AddString( str.c_str(), static_cast<int>(str.length()) );
74}
75int DataTable::AddSpace( int size )
76{
77 int retSize = this->size;
78 Realloc( this->size + size );
79 return retSize;
80}
81void DataTable::AddAlignment( int size )
82{
83 if( this->size % size == 0 )
84 {
85 // 既に境界のとき
86 return;
87 }
88 Realloc( this->size + ( size - (int)(this->size%size) ) );
89}
90
91bool DataTable::MakeConstObjectToProcessStaticBuffer( const CClass &objClass, const Jenga::Common::Strings &initMemberValues, int &dataTableOffset )
92{
93 // クラスに必要なバッファサイズを取得
94 int size = objClass.GetSize();
95
96 // クラスのバッファイメージを作成
97 BYTE *buffer = (BYTE *)calloc( size, 1 );
98
99 // クラスのバッファイメージをデータ領域へ追加
100 dataTableOffset = this->AddBinary( buffer, size );
101
102 this->lastMadeConstObjectDataTableOffset = dataTableOffset;
103
104 // com_vtblスケジュール
105 this->schedules.push_back( Schedule( Schedule::ComVtbl, &objClass, dataTableOffset ) );
106
107 // vtblスケジュール
108 this->schedules.push_back( Schedule( Schedule::Vtbl, &objClass, dataTableOffset + PTR_SIZE ) );
109
110 // TypeInfoスケジュール
111 int offsetForTypeInfo = objClass.GetMemberOffset( "_system_object_member_typeInfo" );
112 //this->schedules.push_back( Schedule( Schedule::TypeInfo, &objClass, dataTableOffset + offsetForTypeInfo ) );
113
114 BOOST_FOREACH( const std::string &initMemberValue, initMemberValues )
115 {
116 int i = 0;
117
118 // メンバ名
119 char memberName[VN_SIZE];
120 for( i=0; ; i++ )
121 {
122 if( initMemberValue[i] == '\0' )
123 {
124 // エラー
125 SetError();
126 return false;
127 }
128 if( initMemberValue[i] == '=' )
129 {
130 memberName[i] = 0;
131 break;
132 }
133 memberName[i] = initMemberValue[i];
134 }
135
136 // 初期値
137 const char *initValue = initMemberValue.c_str() + i + 1;
138
139 // メンバを取得
140 const CMember *member = objClass.FindDynamicMember( memberName );
141
142 // メンバオフセットを取得
143 int memberOffset = objClass.GetMemberOffset( member->GetName().c_str() );
144
145 if( member->GetType().IsPointer() && initValue[0] == '[' )
146 {
147 // ポインタ型でバッファ指定のとき
148 int memberDataTableOffset;
149 if( !this->MakeLiteralArrayBuffer( initValue, member->GetType(),memberDataTableOffset ) )
150 {
151 return false;
152 }
153
154 this->Overwrite( dataTableOffset + memberOffset, memberDataTableOffset );
155 this->schedules.push_back( Schedule( Schedule::DataTable, dataTableOffset + memberOffset ) );
156 }
157 else if( member->GetType().IsWhole() )
158 {
159 // 整数
160 Type resultType;
161 _int64 i64data;
162 if( !StaticCalculation( true, initValue, member->GetType().GetBasicType(), &i64data, resultType ) ){
163 return false;
164 }
165
166 this->Overwrite( dataTableOffset + memberOffset, static_cast<long>(i64data) );
167 }
168 else if( member->GetType().IsStringClass() )
169 {
170 // 文字列型
171 char temporary[VN_SIZE];
172 lstrcpy( temporary, initValue );
173 RemoveStringQuotes( temporary );
174 int memberDataTableOffset = MakeConstStringObjectToProcessStaticBuffer( temporary );
175 this->Overwrite( dataTableOffset + memberOffset, memberDataTableOffset );
176 this->schedules.push_back( Schedule( Schedule::DataTable, dataTableOffset + memberOffset ) );
177 }
178 }
179
180 return true;
181}
182bool DataTable::MakeConstObjectToProcessStaticBuffer( const char *expression, Type &resultType, int &dataTableOffset )
183{
184 char CreateParameter[VN_SIZE];
185 int i,i2;
186
187 i=0;
188
189 // クラス名を取得
190 char typeName[VN_SIZE];
191 for(i2=0;;i++,i2++){
192 if(expression[i]=='['){
193 typeName[i2]=0;
194
195 // メンバの初期値を取得
196 i2=GetStringInBracket(CreateParameter,expression+i);
197 RemoveStringBracket(CreateParameter);
198 i+=i2;
199 if(expression[i]!='\0'){
200 SetError(42,NULL,cp);
201 return false;
202 }
203 break;
204 }
205 typeName[i2]=expression[i];
206 if(expression[i]=='\0'){
207 CreateParameter[0]=0;
208 break;
209 }
210 }
211
212 // パラメータを取得
213 Jenga::Common::Strings initMemberValues;
214 SplitParameter( CreateParameter, initMemberValues );
215
216 if( !compiler.StringToType( typeName, resultType ) ){
217 SetError(3,typeName,cp);
218 return false;
219 }
220
221 if( !resultType.IsObject() ){
222 ////////////////////////
223 // 通常のデータ型の場合
224 ////////////////////////
225
226 SetError(121,NULL,cp);
227 return false;
228 }
229
230 return MakeConstObjectToProcessStaticBuffer( resultType.GetClass(), initMemberValues, dataTableOffset );
231}
232
233int DataTable::MakeConstStringObjectToProcessStaticBuffer( const char *str )
234{
235 const CClass &strClass = *compiler.GetObjectModule().meta.GetClasses().GetStringClassPtr();
236 const CClass &objClass = strClass.GetSuperClass();
237
238 // クラスに必要なバッファサイズを取得
239 int size = strClass.GetSize();
240
241 // メンバ位置を取得
242 int offsetForTypeInfo = strClass.GetMemberOffset( "_system_object_member_typeInfo" );
243 int offsetForLength = strClass.GetMemberOffset( "m_Length" );
244 int offsetForChars = strClass.GetMemberOffset( "Chars" );
245
246 // 現在のデータ領域のヘッダ位置を取得
247 int headOffset = this->GetSize();
248
249 // クラスのバッファイメージを作成
250 BYTE *buffer = (BYTE *)calloc( size, 1 );
251 *(long *)(buffer + offsetForLength) = lstrlen( str );
252 *(LONG_PTR *)(buffer + offsetForChars) = headOffset + size;
253
254 // クラスのバッファイメージをデータ領域へ追加
255 int dataTableOffset = this->AddBinary( buffer, size );
256
257 // スケジューリング
258 this->schedules.push_back( Schedule( Schedule::ComVtbl, &strClass, headOffset ) );
259 this->schedules.push_back( Schedule( Schedule::Vtbl, &strClass, headOffset + PTR_SIZE ) );
260 this->schedules.push_back( Schedule( Schedule::TypeInfo, &strClass, headOffset + offsetForTypeInfo ) );
261 this->schedules.push_back( Schedule( Schedule::DataTable, headOffset + offsetForChars ) );
262
263 // 文字列バッファをデータ領域へ追加
264 this->AddString( str );
265
266 return dataTableOffset;
267}
268
269bool DataTable::MakeLiteralArrayBuffer( const char *expression, const Type &baseType, int &dataTableOffset )
270{
271 if( !baseType.IsPointer() ){
272 SetError(1,NULL,cp);
273 return false;
274 }
275 Type tempBaseType( baseType );
276 tempBaseType.PtrLevelDown();
277
278 char *buffer = (char *)malloc( lstrlen( expression ) + 1 );
279 lstrcpy( buffer, expression );
280 RemoveStringBracket( buffer );
281
282 Jenga::Common::Strings parameters;
283 SplitParameter( buffer, parameters );
284
285 // アラインメント
286 AddAlignment( tempBaseType.GetSize() );
287
288 // データテーブルに空間を確保
289 dataTableOffset = AddSpace( static_cast<int>(tempBaseType.GetSize() * parameters.size()) );
290
291 bool isSuccessful = true;
292 int i = 0;
293 BOOST_FOREACH( const std::string &paramStr, parameters )
294 {
295 if( paramStr[0] == '\"' )
296 {
297 // 文字列
298 std::string tempParamStr = paramStr;
299 if( !RemoveStringQuotes( tempParamStr ) )
300 {
301 SetError();
302 }
303
304 // 文字列を追加
305 _int64 strOffset;
306 if( tempBaseType.IsStringClass() )
307 {
308 // Stringクラス
309 strOffset = MakeConstStringObjectToProcessStaticBuffer( tempParamStr.c_str() );
310 }
311 else
312 {
313 // Charポインタ
314 strOffset = this->AddString( tempParamStr );
315 }
316
317 // ポインタ値を上書き
318 int tempOffset = dataTableOffset + i * tempBaseType.GetSize();
319 this->OverwriteBinary( tempOffset, &strOffset, tempBaseType.GetSize() );
320
321 // DataTableスケジュール
322 this->schedules.push_back( Schedule( Schedule::DataTable, tempOffset ) );
323 }
324 else
325 {
326 // 数値
327 Type resultType;
328 _int64 i64data;
329 if( !StaticCalculation( true, paramStr.c_str(), tempBaseType.GetBasicType(), &i64data, resultType ) ){
330 isSuccessful = false;
331 break;
332 }
333 if( !resultType.IsWhole() ){
334 // TODO: 実数に未対応
335 SetError();
336 isSuccessful = false;
337 break;
338 }
339
340 // 上書き
341 this->OverwriteBinary( dataTableOffset + i * tempBaseType.GetSize(), &i64data, tempBaseType.GetSize() );
342 }
343
344 i++;
345 }
346
347 free( buffer );
348
349 return isSuccessful;
350}
351
352void DataTable::ResetDataSectionBaseOffset( long dataSectionBaseOffset )
353{
354 BOOST_FOREACH( const Schedule &schedule, schedules )
355 {
356 if( schedule.GetType() == Schedule::DataTable )
357 {
358#ifdef _WIN64
359 OverwriteInt64(
360 schedule.GetOffset(),
361 GetInt64( schedule.GetOffset() ) + dataSectionBaseOffset
362 );
363#else
364 Overwrite(
365 schedule.GetOffset(),
366 GetLong( schedule.GetOffset() ) + dataSectionBaseOffset
367 );
368#endif
369 }
370 }
371}
Note: See TracBrowser for help on using the repository browser.