source: dev/trunk/ab5.0/abdev/BasicCompiler_Common/src/Compiler.cpp@ 603

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

ObjectModuleに関連するクラス一式をab_commonプロジェクトに移動した。

File size: 7.4 KB
Line 
1#include "stdafx.h"
2
3using namespace ActiveBasic::Compiler;
4
5Compiler compiler;
6
7void Compiler::StaticLink( ObjectModules &staticLibraries )
8{
9 BOOST_FOREACH( ObjectModule *pStaticLibrary, staticLibraries )
10 {
11 // メタ情報
12 pNowObjectModule->StaticLink( *pStaticLibrary );
13 }
14}
15
16bool Compiler::StringToType( const std::string &typeName, Type &type ){
17 type.SetIndex( -1 );
18
19
20 /////////////////////////////////////////////////////////
21 // ☆★☆ ジェネリクスサポート ☆★☆
22
23 if( strstr( typeName.c_str(), "<" ) )
24 {
25 // ジェネリッククラスをインスタンス化した型の場合
26 int i = 0;
27 char className[VN_SIZE];
28 GetIdentifierToken( className, typeName.c_str(), i );
29
30 // ジェネリクスクラスを取得
31 const CClass *pGenericClass = this->GetObjectModule().meta.FindClassSupportedTypeDef(
32 LexicalAnalyzer::FullNameToSymbol( className )
33 );
34
35 if( !pGenericClass )
36 {
37 return false;
38 }
39
40 if( typeName[i] != '<' )
41 {
42 Jenga::Throw( "StringToType内でジェネリクス構文の解析に失敗" );
43 }
44
45 GenericTypes genericTypes;
46 while( true )
47 {
48 i++;
49
50 char typeParameter[VN_SIZE];
51 GetIdentifierToken( typeParameter, typeName.c_str(), i );
52
53 // 型パラメータの型情報を取得
54 Type baseType;
55 StringToType( typeParameter, baseType );
56
57 genericTypes.push_back( GenericType( "(non support)", baseType ) );
58
59 if( typeName[i] != ',' )
60 {
61 break;
62 }
63 }
64
65 // 基本型をセット
66 type.SetBasicType( DEF_OBJECT );
67
68 // 拡張情報をセット
69 type.SetClassPtr( pGenericClass );
70 type.SetActualGenericTypes( genericTypes );
71
72 return true;
73 }
74
75 //
76 /////////////////////////////////////////////////////////
77
78
79 if( typeName[0] == '*' ){
80 if( typeName.size() >= 3
81 && typeName[1] == 1 && ( typeName[2] == ESC_FUNCTION || typeName[2] == ESC_SUB ) )
82 {
83 // 関数ポインタを追加する
84 {
85 DWORD dwProcType = (DWORD)typeName[2];
86 const std::string &paramStr = typeName.substr( 3 );
87
88 Procedure::Kind kind = ( dwProcType == ESC_FUNCTION )
89 ? Procedure::Function
90 : Procedure::Sub;
91
92 ProcPointer *pProcPointer = new ProcPointer( kind );
93
94 //buffer[0]は'('となっている
95 extern int cp;
96 ActiveBasic::Compiler::LexicalAnalyzer::SetParamsAndReturnType( pProcPointer, paramStr.c_str(), false, cp );
97
98 this->GetObjectModule().meta.GetProcPointers().push_back( pProcPointer );
99 }
100
101 //関数ポインタ(*Function)
102 type.SetBasicType( DEF_PTR_PROC );
103 type.SetIndex( this->GetObjectModule().meta.GetProcPointers().size() - 1 );
104 return true;
105 }
106
107 const std::string &nextTypeName = typeName.substr( 1 );
108
109 if( !StringToType( nextTypeName, type ) ){
110 return false;
111 }
112
113 type.PtrLevelUp();
114
115 return true;
116 }
117
118 {
119 int basicType;
120 if( Type::StringToBasicType( typeName, basicType ) ){
121 // 基本型だったとき
122 type.SetBasicType( basicType );
123 return true;
124 }
125 }
126
127 // Object型だったとき
128 if( typeName == "Object" ){
129 type.SetType( DEF_OBJECT, this->GetObjectModule().meta.GetClasses().GetObjectClassPtr() );
130 return true;
131 }
132
133 // String型だったとき
134 if( typeName == "String" ){
135 type.SetType( DEF_OBJECT, this->GetObjectModule().meta.GetClasses().GetStringClassPtr() );
136 return true;
137 }
138
139
140 ////////////////////
141 // TypeDefされた型
142 ////////////////////
143 int i=this->GetObjectModule().meta.GetTypeDefs().GetIndex( LexicalAnalyzer::FullNameToSymbol( typeName ) );
144 if(i!=-1){
145 type = this->GetObjectModule().meta.GetTypeDefs()[i].GetBaseType();
146 return true;
147 }
148
149 //クラス
150 const CClass *pobj_c = this->GetObjectModule().meta.GetClasses().FindEx( LexicalAnalyzer::FullNameToSymbol( typeName ) );
151 if(pobj_c){
152 if( pobj_c->IsStructure() ){
153 type.SetBasicType( DEF_STRUCT );
154 }
155 else{
156 type.SetBasicType( DEF_OBJECT );
157 }
158 type.SetClassPtr( pobj_c );
159 return true;
160 }
161
162
163 /////////////////////////////////////////////////////////
164 // ☆★☆ ジェネリクスサポート ☆★☆
165
166 // 型パラメータ
167 if( this->IsCompilingClass() )
168 {
169 // クラスに属するメソッドをコンパイルしているとき
170 int formalTypeIndex = this->GetCompilingClass().GetFormalGenericTypeParameterIndex( typeName );
171 if( formalTypeIndex != -1 )
172 {
173 // コンパイル中クラスにおけるジェネリクス用の型パラメータのとき
174 type.SetBasicType( DEF_TYPE_PARAMETER );
175 type.SetClassPtr( &this->GetCompilingClass().GetFormalGenericTypes()[formalTypeIndex].GetType().GetClass() );
176 type.SetFormalTypeName( typeName );
177 type.SetFormalTypeIndex( formalTypeIndex );
178 return true;
179 }
180 }
181
182 //
183 /////////////////////////////////////////////////////////
184
185 return false;
186}
187
188const std::string Compiler::TypeToString( const Type &type )
189{
190 if( PTR_LEVEL( type.GetBasicType() ) ){
191 //ポインタレベルが1以上の場合
192 Type tempType( type );
193 tempType.PtrLevelDown();
194
195 return (std::string)"*" + TypeToString( tempType );
196 }
197 else if( type.IsObject() || type.IsStruct() ){
198 //オブジェクトまたは構造体
199
200 if( !( type.GetIndex() == 0 || type.GetIndex() == -1 ) ){
201 if( type.GetClass().GetNamespaceScopes().size() >= 1 )
202 {
203 return type.GetClass().GetNamespaceScopes().ToString() + "." + type.GetClass().GetName();
204 }
205 return type.GetClass().GetName();
206 }
207 }
208 else if( type.IsProcPtr() ){
209 if( type.GetIndex() == 0 || type.GetIndex() == -1 ){
210 return "VoidPtr";
211 }
212 else{
213 if( this->GetObjectModule().meta.GetProcPointers()[type.GetIndex()]->ReturnType().IsNull() ){
214 return "*Sub";
215 }
216 return "*Function";
217 }
218 }
219 else{
220 // 基本型
221 const char *lpszTypeName = Type::BasicTypeToCharPtr( type );
222 if( lpszTypeName )
223 {
224 return (const std::string)lpszTypeName;
225 }
226 }
227
228 compiler.errorMessenger.Output(1, NULL, cp);
229
230 return (std::string)"(null)";
231}
232
233void Compiler::ClearCompilingUserProcAndClass()
234{
235 this->pCompilingUserProc = NULL;
236 this->pCompilingClass = NULL;
237}
238
239void Compiler::SetCompilingClass( const CClass *pClass )
240{
241 this->pCompilingClass = pClass;
242}
243
244void Compiler::SetCompilingUserProc( const UserProc *pUserProc )
245{
246 this->pCompilingUserProc = pUserProc;
247
248 this->SetCompilingClass( pUserProc->GetParentClassPtr() );
249}
250
251void Compiler::StartGlobalAreaCompile()
252{
253 ClearCompilingUserProcAndClass();
254}
255
256void Compiler::StartProcedureCompile( const UserProc *pUserProc )
257{
258 //コンパイル中の関数
259 this->SetCompilingUserProc( pUserProc );
260
261 if( pUserProc->HasParentClass() )
262 {
263 // クラスの使用チェック
264 pUserProc->GetParentClass().Using();
265 }
266
267 // コンパイル中の関数が属する名前空間
268 this->GetNamespaceSupporter().SetLivingNamespaceScopes( pUserProc->GetNamespaceScopes() );
269
270 // コンパイル中の関数でImportsされている名前空間
271 this->GetNamespaceSupporter().SetImportedNamespaces( pUserProc->GetImportedNamespaces() );
272
273 // コード生成対象を選択
274 this->codeGenerator.Select( (const_cast<UserProc *>(pUserProc))->GetNativeCode() );
275}
276void Compiler::FinishProcedureCompile()
277{
278 this->pCompilingUserProc = NULL;
279 this->pCompilingClass = NULL;
280}
281
282bool Compiler::IsGlobalAreaCompiling()
283{
284 return ( pCompilingUserProc == NULL );
285}
286bool Compiler::IsLocalAreaCompiling()
287{
288 return ( pCompilingUserProc != NULL );
289}
290const UserProc &Compiler::GetCompilingUserProc()
291{
292 if( !this->IsGlobalAreaCompiling() )
293 {
294 return *pCompilingUserProc;
295 }
296
297 throw;
298}
299
300bool Compiler::IsCompilingClass()
301{
302 return ( pCompilingClass != NULL );
303}
304const CClass &Compiler::GetCompilingClass()
305{
306 if( this->IsCompilingClass() )
307 {
308 return *pCompilingClass;
309 }
310
311 throw;
312}
Note: See TracBrowser for help on using the repository browser.