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

Last change on this file since 244 was 225, checked in by dai_9181, 17 years ago

CodeGeneratorクラスのベースを実装

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 <CodeGenerator.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 NativeCode &GetNativeCode()
317 {
318 return nativeCode;
319 }
320
321 std::string GetFullName() const;
322
323 bool IsVirtual() const;
324
325 void SetParentClass( const CClass *pParentClass ){
326 this->pParentClass = pParentClass;
327 }
328 const CClass *GetParentClassPtr() const
329 {
330 return pParentClass;
331 }
332 const CClass &GetParentClass() const
333 {
334 return *pParentClass;
335 }
336 bool HasParentClass() const
337 {
338 return ( pParentClass != NULL );
339 }
340 bool IsGlobalProcedure() const
341 {
342 return ( pParentClass == NULL );
343 }
344 void SetMethod( CMethod *pMethod ){
345 this->pMethod = pMethod;
346 }
347
348 bool SetParamsAndReturnType( const char *sourceOfParams, int nowLine, bool isStatic );
349
350
351
352 /////////////////////////////////////////////////////////////////
353 // コンパイル中の関数を管理
354 /////////////////////////////////////////////////////////////////
355private:
356 static const UserProc *pCompilingUserProc;
357public:
358 static void CompileStartForGlobalArea(){
359 pCompilingUserProc = NULL;
360 }
361 static void CompileStartForUserProc( const UserProc *pUserProc ){
362 pCompilingUserProc = pUserProc;
363 }
364 static bool IsGlobalAreaCompiling(){
365 return ( pCompilingUserProc == NULL );
366 }
367 static bool IsLocalAreaCompiling(){
368 return ( pCompilingUserProc != NULL );
369 }
370 static const UserProc &CompilingUserProc(){
371 return *pCompilingUserProc;
372 }
373};
374
375class UserProcs : public Jenga::Common::Hashmap<UserProc>
376{
377 std::vector<std::string> macroNames;
378
379 // XMLシリアライズ用
380private:
381 friend class boost::serialization::access;
382 template<class Archive> void serialize(Archive& ar, const unsigned int version)
383 {
384 trace_for_serialize( "serializing - UserProcs" );
385
386 ar & boost::serialization::make_nvp("Hashmap_UserProcImpl",
387 boost::serialization::base_object<Jenga::Common::Hashmap<UserProc>>(*this));
388 ar & BOOST_SERIALIZATION_NVP( macroNames );
389 }
390
391
392public:
393 UserProcs()
394 {
395 }
396 ~UserProcs()
397 {
398 }
399
400 bool Insert( UserProc *pUserProc, int nowLine );
401
402 UserProc *Add( const NamespaceScopes &namespaceScopes, const NamespaceScopesCollection &importedNamespaces, char *buffer,int nowLine,bool isVirtual,CClass *pobj_c, bool isStatic);
403
404 void EnumGlobalProcs( const char *simpleName, const char *localName, std::vector<const UserProc *> &subs );
405};
406
407class DllProc : public Procedure, public Jenga::Common::ObjectInHashmap<DllProc>
408{
409 string dllFileName;
410 string alias;
411 int lookupAddress;
412
413 // XMLシリアライズ用
414private:
415 friend class boost::serialization::access;
416 template<class Archive> void serialize(Archive& ar, const unsigned int version)
417 {
418 trace_for_serialize( "serializing - DllProc" );
419
420 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP( Procedure );
421 ar & BOOST_SERIALIZATION_NVP( dllFileName );
422 ar & BOOST_SERIALIZATION_NVP( alias );
423 ar & BOOST_SERIALIZATION_NVP( lookupAddress );
424 }
425
426public:
427 // ハッシュリスト用
428 DllProc *pNextData;
429
430 DllProc( const NamespaceScopes &namespaceScopes, const string &name, Kind kind, bool isCdecl, const string &dllFileName, const string &alias )
431 : Procedure( namespaceScopes, name, kind, isCdecl )
432 , dllFileName( dllFileName )
433 , alias( alias )
434 , lookupAddress( 0 )
435 , pNextData( NULL )
436 {
437 }
438 DllProc()
439 {
440 }
441 ~DllProc()
442 {
443 }
444
445 virtual const std::string &GetKeyName() const
446 {
447 return GetName();
448 }
449
450 virtual bool IsDuplication( const DllProc *pDllProc ) const
451 {
452 if( pDllProc->IsEqualSymbol( *this )
453 && this->Params().Equals( pDllProc->Params() ) )
454 {
455 return true;
456 }
457 return false;
458 }
459
460 const string &GetDllFileName() const
461 {
462 return dllFileName;
463 }
464 const string &GetAlias() const
465 {
466 return alias;
467 }
468
469 void SetLookupAddress( int lookupAddress ){
470 this->lookupAddress = lookupAddress;
471 }
472 int GetLookupAddress() const
473 {
474 return lookupAddress;
475 }
476
477 bool SetParamsAndReturnType( const char *sourceOfParams, int nowLine );
478};
479class DllProcs : public Jenga::Common::Hashmap<DllProc>
480{
481 // XMLシリアライズ用
482private:
483 friend class boost::serialization::access;
484 template<class Archive> void serialize(Archive& ar, const unsigned int version)
485 {
486 trace_for_serialize( "serializing - DllProcs" );
487
488 ar & boost::serialization::make_nvp("Hashmap_DllProc",
489 boost::serialization::base_object<Jenga::Common::Hashmap<DllProc>>(*this));
490 }
491
492public:
493 void Add(const NamespaceScopes &namespaceScopes, char *buffer,int nowLine);
494};
495
496void CollectProcedures( const BasicSource &source, UserProcs &userProcs, DllProcs &dllProcs );
497
498class ProcPointer : public Procedure
499{
500 // XMLシリアライズ用
501private:
502 friend class boost::serialization::access;
503 template<class Archive> void serialize(Archive& ar, const unsigned int version)
504 {
505 trace_for_serialize( "serializing - ProcPointer" );
506
507 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP( Procedure );
508 }
509
510public:
511 ProcPointer( Kind kind )
512 : Procedure( NamespaceScopes(), std::string(), kind, false )
513 {
514 }
515 ProcPointer()
516 {
517 }
518 ~ProcPointer(){}
519
520 virtual bool SetParamsAndReturnType( const char *sourceOfParams, int nowLine );
521};
522
523class ProcPointers : public vector<ProcPointer *>
524{
525 // XMLシリアライズ用
526private:
527 friend class boost::serialization::access;
528 template<class Archive> void serialize(Archive& ar, const unsigned int version)
529 {
530 trace_for_serialize( "serializing - ProcPointers" );
531
532 ar & boost::serialization::make_nvp("vector_ProcPointer",
533 boost::serialization::base_object<vector<ProcPointer *>>(*this));
534 }
535
536public:
537 ProcPointers()
538 {
539 }
540 ~ProcPointers()
541 {
542 Clear();
543 }
544
545 virtual int Add( const string &typeExpression );
546 virtual void Clear();
547};
Note: See TracBrowser for help on using the repository browser.