source: dev/trunk/abdev/BasicCompiler_Common/include/Procedure.h@ 224

Last change on this file since 224 was 224, checked in by dai_9181, 17 years ago
File size: 11.7 KB
Line 
1#pragma once
2
3#include <jenga/include/smoothie/Source.h>
4
5#include <Hashmap.h>
6#include <option.h>
7#include <Program.h>
8#include <Class.h>
9#include <Method.h>
10#include <Procedure.h>
11#include <Parameter.h>
12#include <Variable.h>
13#include <NativeCode.h>
14
15class CClass;
16class CMethod;
17
18class Procedure : public Symbol
19{
20public:
21 // 種類
22 enum Kind{
23 Sub,
24 Function,
25 };
26
27private:
28 Kind kind;
29
30 bool isCdecl;
31 mutable bool isUsing;
32
33protected:
34
35 // パラメータ
36 Parameters params;
37
38 // 戻り値の型
39 Type returnType;
40
41 // ソースコードの位置
42 int codePos;
43
44 // XMLシリアライズ用
45private:
46private:
47 friend class boost::serialization::access;
48 template<class Archive> void serialize(Archive& ar, const unsigned int version)
49 {
50 trace_for_serialize( "serializing - Procedure" );
51
52 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP( Symbol );
53 ar & BOOST_SERIALIZATION_NVP( kind );
54 ar & BOOST_SERIALIZATION_NVP( isCdecl );
55 ar & BOOST_SERIALIZATION_NVP( isUsing );
56 ar & BOOST_SERIALIZATION_NVP( params );
57 ar & BOOST_SERIALIZATION_NVP( returnType );
58 ar & BOOST_SERIALIZATION_NVP( codePos );
59 }
60
61public:
62 Procedure( const NamespaceScopes &namespaceScopes, const string &name, Kind kind, bool isCdecl )
63 : Symbol( namespaceScopes, name )
64 , kind( kind )
65 , isCdecl( isCdecl )
66 , isUsing( false )
67 , codePos( -1 )
68 {
69 }
70 Procedure()
71 {
72 }
73 ~Procedure(){
74 BOOST_FOREACH( Parameter *pParam, params ){
75 delete pParam;
76 }
77 }
78
79 bool IsSub() const
80 {
81 return ( kind == Sub );
82 }
83 bool IsFunction() const
84 {
85 return ( kind == Function );
86 }
87
88 bool IsCdecl() const
89 {
90 return isCdecl;
91 }
92 void Using() const
93 {
94 isUsing = true;
95 }
96 bool IsUsing() const
97 {
98 return isUsing;
99 }
100
101 int GetCodePos() const
102 {
103 return codePos;
104 }
105
106 const Parameters &Params() const
107 {
108 return params;
109 }
110 const Type &ReturnType() const
111 {
112 return returnType;
113 }
114};
115
116class UserProc : public Procedure, public Jenga::Common::ObjectInHashmap<UserProc>
117{
118public:
119 string _paramStr;
120
121private:
122 NamespaceScopesCollection importedNamespaces;
123
124 // 親クラスと対応するメソッド
125 const CClass *pParentClass;
126 CMethod *pMethod;
127
128 bool isMacro;
129
130 // パラメータの追加情報
131 int secondParmNum;
132 Parameters realParams;
133 int realSecondParmNum;
134
135 // 各種フラグ
136 bool isExport;
137 mutable bool isSystem;
138 mutable bool isAutoGeneration;
139 mutable bool isCompiled;
140
141 mutable DWORD beginOpAddress;
142 mutable DWORD endOpAddress;
143
144 // ローカル変数
145 mutable Variables localVars;
146
147 // 識別ID
148 int id;
149
150 // ネイティブコード
151 NativeCode nativeCode;
152
153 // XMLシリアライズ用
154private:
155 friend class boost::serialization::access;
156 template<class Archive> void serialize(Archive& ar, const unsigned int version)
157 {
158 trace_for_serialize( "serializing - UserProc" );
159
160 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP( Procedure );
161 ar & BOOST_SERIALIZATION_NVP( _paramStr );
162 ar & BOOST_SERIALIZATION_NVP( importedNamespaces );
163 ar & boost::serialization::make_nvp("pParentClass", const_cast<CClass *&>(pParentClass) );
164 ar & BOOST_SERIALIZATION_NVP( pMethod );
165 ar & BOOST_SERIALIZATION_NVP( isMacro );
166 ar & BOOST_SERIALIZATION_NVP( secondParmNum );
167 ar & BOOST_SERIALIZATION_NVP( realParams );
168 ar & BOOST_SERIALIZATION_NVP( realSecondParmNum );
169 ar & BOOST_SERIALIZATION_NVP( isExport );
170 ar & BOOST_SERIALIZATION_NVP( isSystem );
171 ar & BOOST_SERIALIZATION_NVP( isAutoGeneration );
172 ar & BOOST_SERIALIZATION_NVP( isCompiled );
173 ar & BOOST_SERIALIZATION_NVP( beginOpAddress );
174 ar & BOOST_SERIALIZATION_NVP( endOpAddress );
175 ar & BOOST_SERIALIZATION_NVP( localVars );
176 ar & BOOST_SERIALIZATION_NVP( id );
177 ar & BOOST_SERIALIZATION_NVP( nativeCode );
178 }
179
180public:
181
182 UserProc( const NamespaceScopes &namespaceScopes, const NamespaceScopesCollection &importedNamespaces, const string &name, Kind kind, bool isMacro, bool isCdecl, bool isExport, int id )
183 : Procedure( namespaceScopes, name, kind, isCdecl )
184 , importedNamespaces( importedNamespaces )
185 , pParentClass( NULL )
186 , pMethod( NULL )
187 , isMacro( isMacro )
188 , isExport( isExport )
189 , isSystem( false )
190 , isAutoGeneration( false )
191 , isCompiled( false )
192 , beginOpAddress( 0 )
193 , endOpAddress( 0 )
194 , id( id )
195 {
196 }
197 UserProc()
198 {
199 }
200 ~UserProc()
201 {
202 BOOST_FOREACH( Parameter *pParam, realParams ){
203 delete pParam;
204 }
205 }
206
207 virtual const std::string &GetKeyName() const
208 {
209 return GetName();
210 }
211
212 virtual bool IsDuplication( const UserProc *pUserProc ) const
213 {
214 if( this->GetParentClassPtr() == pUserProc->GetParentClassPtr()
215 && pUserProc->IsEqualSymbol( *this )
216 && this->Params().Equals( pUserProc->Params() ) )
217 {
218 return true;
219 }
220 return false;
221 }
222
223 bool IsMacro() const
224 {
225 return isMacro;
226 }
227
228 int GetSecondParmNum() const
229 {
230 return secondParmNum;
231 }
232 const Parameters &RealParams() const
233 {
234 return realParams;
235 }
236 int GetRealSecondParmNum() const
237 {
238 return realSecondParmNum;
239 }
240
241 void ExportOff(){
242 isExport = false;
243 }
244 bool IsExport() const
245 {
246 return isExport;
247 }
248 void ThisIsSystemProc() const
249 {
250 isSystem = true;
251 }
252 bool IsSystem() const
253 {
254 return isSystem;
255 }
256 void ThisIsAutoGenerationProc() const
257 {
258 isAutoGeneration = true;
259 }
260 bool IsAutoGeneration() const
261 {
262 return isAutoGeneration;
263 }
264 void CompleteCompile() const
265 {
266 isCompiled = true;
267 }
268 void KillCompileStatus() const
269 {
270 isCompiled = false;
271 }
272 bool IsCompiled() const
273 {
274 return isCompiled;
275 }
276 bool IsDestructor() const
277 {
278 return ( GetName()[0] == '~' );
279 }
280
281 // バイナリコード位置とサイズ
282 DWORD GetBeginOpAddress() const
283 {
284 return beginOpAddress;
285 }
286 void SetBeginOpAddress( DWORD beginOpAddress ) const
287 {
288 this->beginOpAddress = beginOpAddress;
289 }
290 DWORD GetEndOpAddress() const
291 {
292 return endOpAddress;
293 }
294 void SetEndOpAddress( DWORD endOpAddress ) const
295 {
296 this->endOpAddress = endOpAddress;
297 }
298 int GetCodeSize() const
299 {
300 return endOpAddress - beginOpAddress;
301 }
302
303 virtual const NamespaceScopes &GetNamespaceScopes() const;
304 const NamespaceScopesCollection &GetImportedNamespaces() const;
305
306 Variables &GetLocalVars() const
307 {
308 return localVars;
309 }
310
311 int GetId() const
312 {
313 return id;
314 }
315
316 std::string GetFullName() const;
317
318 bool IsVirtual() const;
319
320 void SetParentClass( const CClass *pParentClass ){
321 this->pParentClass = pParentClass;
322 }
323 const CClass *GetParentClassPtr() const
324 {
325 return pParentClass;
326 }
327 const CClass &GetParentClass() const
328 {
329 return *pParentClass;
330 }
331 bool HasParentClass() const
332 {
333 return ( pParentClass != NULL );
334 }
335 bool IsGlobalProcedure() const
336 {
337 return ( pParentClass == NULL );
338 }
339 void SetMethod( CMethod *pMethod ){
340 this->pMethod = pMethod;
341 }
342
343 bool SetParamsAndReturnType( const char *sourceOfParams, int nowLine, bool isStatic );
344
345
346
347 /////////////////////////////////////////////////////////////////
348 // コンパイル中の関数を管理
349 /////////////////////////////////////////////////////////////////
350private:
351 static const UserProc *pCompilingUserProc;
352public:
353 static void CompileStartForGlobalArea(){
354 pCompilingUserProc = NULL;
355 }
356 static void CompileStartForUserProc( const UserProc *pUserProc ){
357 pCompilingUserProc = pUserProc;
358 }
359 static bool IsGlobalAreaCompiling(){
360 return ( pCompilingUserProc == NULL );
361 }
362 static bool IsLocalAreaCompiling(){
363 return ( pCompilingUserProc != NULL );
364 }
365 static const UserProc &CompilingUserProc(){
366 return *pCompilingUserProc;
367 }
368};
369
370class UserProcs : public Jenga::Common::Hashmap<UserProc>
371{
372 std::vector<std::string> macroNames;
373
374 // XMLシリアライズ用
375private:
376 friend class boost::serialization::access;
377 template<class Archive> void serialize(Archive& ar, const unsigned int version)
378 {
379 trace_for_serialize( "serializing - UserProcs" );
380
381 ar & boost::serialization::make_nvp("Hashmap_UserProcImpl",
382 boost::serialization::base_object<Jenga::Common::Hashmap<UserProc>>(*this));
383 ar & BOOST_SERIALIZATION_NVP( macroNames );
384 }
385
386
387public:
388 UserProcs()
389 {
390 }
391 ~UserProcs()
392 {
393 }
394
395 bool Insert( UserProc *pUserProc, int nowLine );
396
397 UserProc *Add( const NamespaceScopes &namespaceScopes, const NamespaceScopesCollection &importedNamespaces, char *buffer,int nowLine,bool isVirtual,CClass *pobj_c, bool isStatic);
398
399 void EnumGlobalProcs( const char *simpleName, const char *localName, std::vector<const UserProc *> &subs );
400};
401
402class DllProc : public Procedure, public Jenga::Common::ObjectInHashmap<DllProc>
403{
404 string dllFileName;
405 string alias;
406 int lookupAddress;
407
408 // XMLシリアライズ用
409private:
410 friend class boost::serialization::access;
411 template<class Archive> void serialize(Archive& ar, const unsigned int version)
412 {
413 trace_for_serialize( "serializing - DllProc" );
414
415 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP( Procedure );
416 ar & BOOST_SERIALIZATION_NVP( dllFileName );
417 ar & BOOST_SERIALIZATION_NVP( alias );
418 ar & BOOST_SERIALIZATION_NVP( lookupAddress );
419 }
420
421public:
422 // ハッシュリスト用
423 DllProc *pNextData;
424
425 DllProc( const NamespaceScopes &namespaceScopes, const string &name, Kind kind, bool isCdecl, const string &dllFileName, const string &alias )
426 : Procedure( namespaceScopes, name, kind, isCdecl )
427 , dllFileName( dllFileName )
428 , alias( alias )
429 , lookupAddress( 0 )
430 , pNextData( NULL )
431 {
432 }
433 DllProc()
434 {
435 }
436 ~DllProc()
437 {
438 }
439
440 virtual const std::string &GetKeyName() const
441 {
442 return GetName();
443 }
444
445 virtual bool IsDuplication( const DllProc *pDllProc ) const
446 {
447 if( pDllProc->IsEqualSymbol( *this )
448 && this->Params().Equals( pDllProc->Params() ) )
449 {
450 return true;
451 }
452 return false;
453 }
454
455 const string &GetDllFileName() const
456 {
457 return dllFileName;
458 }
459 const string &GetAlias() const
460 {
461 return alias;
462 }
463
464 void SetLookupAddress( int lookupAddress ){
465 this->lookupAddress = lookupAddress;
466 }
467 int GetLookupAddress() const
468 {
469 return lookupAddress;
470 }
471
472 bool SetParamsAndReturnType( const char *sourceOfParams, int nowLine );
473};
474class DllProcs : public Jenga::Common::Hashmap<DllProc>
475{
476 // XMLシリアライズ用
477private:
478 friend class boost::serialization::access;
479 template<class Archive> void serialize(Archive& ar, const unsigned int version)
480 {
481 trace_for_serialize( "serializing - DllProcs" );
482
483 ar & boost::serialization::make_nvp("Hashmap_DllProc",
484 boost::serialization::base_object<Jenga::Common::Hashmap<DllProc>>(*this));
485 }
486
487public:
488 void Add(const NamespaceScopes &namespaceScopes, char *buffer,int nowLine);
489};
490
491void CollectProcedures( const BasicSource &source, UserProcs &userProcs, DllProcs &dllProcs );
492
493class ProcPointer : public Procedure
494{
495 // XMLシリアライズ用
496private:
497 friend class boost::serialization::access;
498 template<class Archive> void serialize(Archive& ar, const unsigned int version)
499 {
500 trace_for_serialize( "serializing - ProcPointer" );
501
502 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP( Procedure );
503 }
504
505public:
506 ProcPointer( Kind kind )
507 : Procedure( NamespaceScopes(), std::string(), kind, false )
508 {
509 }
510 ProcPointer()
511 {
512 }
513 ~ProcPointer(){}
514
515 virtual bool SetParamsAndReturnType( const char *sourceOfParams, int nowLine );
516};
517
518class ProcPointers : public vector<ProcPointer *>
519{
520 // XMLシリアライズ用
521private:
522 friend class boost::serialization::access;
523 template<class Archive> void serialize(Archive& ar, const unsigned int version)
524 {
525 trace_for_serialize( "serializing - ProcPointers" );
526
527 ar & boost::serialization::make_nvp("vector_ProcPointer",
528 boost::serialization::base_object<vector<ProcPointer *>>(*this));
529 }
530
531public:
532 ProcPointers()
533 {
534 }
535 ~ProcPointers()
536 {
537 Clear();
538 }
539
540 virtual int Add( const string &typeExpression );
541 virtual void Clear();
542};
Note: See TracBrowser for help on using the repository browser.