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

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