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