source: dev/trunk/ab5.0/abdev/BasicCompiler_Common/include/Procedure.h@ 516

Last change on this file since 516 was 511, checked in by dai_9181, 17 years ago

幾つかの構文解析系の処理をLexicalAnalyzerに実装し直した

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