source: dev/trunk/abdev/BasicCompiler_Common/Subroutine.cpp@ 290

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

ジェネリクスのベースを実装

File size: 9.7 KB
Line 
1#include "stdafx.h"
2
3#include <jenga/include/smoothie/Smoothie.h>
4#include <jenga/include/smoothie/LexicalAnalysis.h>
5
6#include <Compiler.h>
7#include <Procedure.h>
8#include <NamespaceSupporter.h>
9
10#include "../BasicCompiler_Common/common.h"
11
12#ifdef _AMD64_
13#include "../BasicCompiler64/opcode.h"
14#else
15#include "../BasicCompiler32/opcode.h"
16#endif
17
18int GetCallProcName(char *buffer,char *name){
19 int i2,i3,IsStr=0;
20
21 for(i2=0;;i2++){
22 if(buffer[i2]=='\"') IsStr^=1;
23 if(IsDBCSLeadByte(buffer[i2])){
24 name[i2]=buffer[i2];
25 i2++;
26 name[i2]=buffer[i2];
27 continue;
28 }
29 if(buffer[i2]=='['&&IsStr==0){
30 i3=GetStringInBracket(name+i2,buffer+i2);
31 i2+=i3-1;
32 continue;
33 }
34 if(buffer[i2]=='('&&IsStr==0){
35 name[i2]=0;
36 break;
37 }
38 if(buffer[i2]=='='&&IsStr==0){
39 name[i2]=0;
40 break;
41 }
42
43 name[i2]=buffer[i2];
44 if(buffer[i2]=='\0') break;
45 }
46 return i2;
47}
48
49int GetProc(char *name,void **ppInfo){
50
51 //ユーザー定義関数
52 *ppInfo=(void *)GetSubHash(name);
53 if(*ppInfo) return PROC_DEFAULT;
54
55 //DLL関数
56 *ppInfo=(void *)GetDeclareHash(name);
57 if(*ppInfo) return PROC_DLL;
58
59 //コンパイラ埋め込み型
60 *ppInfo=(void *)(_int64)GetFunctionFromName(name);
61 if(*ppInfo) return PROC_BUILTIN;
62
63 //関数ポインタ
64 Type type;
65 if( !GetVarType( name, type, false ) ){
66 return 0;
67 }
68 if( type.IsProcPtr() ){
69 return PROC_PTR;
70 }
71
72 return 0;
73}
74
75void SplitObjectName(const char *name,char *ObjectName, ReferenceKind &referenceFind )
76{
77 referenceFind = RefNon;
78
79 int i4;
80 for(i4=lstrlen(name)-1;i4>=0;i4--){
81 if(name[i4]=='.'||(name[i4]==1&&name[i4+1]==ESC_PSMEM))
82 break;
83 }
84 if(i4==-1) ObjectName[0]=0;
85 else{
86 //参照タイプを判別
87 if(name[i4]=='.')
88 {
89 referenceFind = RefDot;
90 }
91 else
92 {
93 referenceFind = RefPointer;
94 }
95
96 if(i4==0) GetWithName(ObjectName);
97 else{
98 memcpy(ObjectName,name,i4);
99 ObjectName[i4]=0;
100 }
101 }
102}
103
104bool CallProc( int kind, const void *pProc, const char *fullCallName, const char *lpszParms, Type &resultType, bool isCallOn ){
105
106 //GetSubHash内でエラー提示が行われた場合
107 if(pProc==(Procedure *)-1){
108 return false;
109 }
110
111 if(kind==PROC_DEFAULT){
112 /////////////////////
113 // ユーザー定義関数
114 /////////////////////
115
116 const UserProc *pUserProc = (const UserProc *)pProc;
117
118 //オブジェクト名を取得
119 char ObjectName[VN_SIZE];
120 ReferenceKind referenceKind;
121 SplitObjectName(fullCallName,ObjectName, referenceKind );
122
123
124 ////////////////////////
125 // オーバーロードを解決
126 ////////////////////////
127
128 std::vector<const UserProc *> subs;
129 GetOverloadSubHash(fullCallName,subs);
130 if(subs.size()){
131 //オーバーロードを解決
132 pUserProc=OverloadSolutionWithStrParam(fullCallName,subs,lpszParms,ObjectName);
133
134 if(!pUserProc){
135 return false;
136 }
137 }
138
139 resultType = pUserProc->ReturnType();
140
141 if( isCallOn ){
142 if( !Opcode_CallProc(lpszParms,pUserProc,0,ObjectName ) ){
143 return false;
144 }
145 }
146 }
147 else if(kind==PROC_DLL){
148 /////////////////////////
149 // DLL関数
150 /////////////////////////
151 DllProc *pDllProc = (DllProc *)pProc;
152
153 resultType = pDllProc->ReturnType();
154
155 if( isCallOn ){
156 if( !Opcode_CallDllProc(lpszParms,pDllProc) ){
157 return false;
158 }
159 }
160 }
161 else if(kind==PROC_BUILTIN){
162 /////////////////////////
163 // 組み込み関数
164 /////////////////////////
165 int FuncId = (int)(_int64)pProc;
166
167 if( !Opcode_CallFunc( lpszParms, FuncId, resultType, isCallOn ) ){
168 return false;
169 }
170 }
171 else if(kind==PROC_PTR){
172 /////////////////
173 // 関数ポインタ
174 /////////////////
175
176 Type type;
177 GetVarType(fullCallName,type,false);
178
179 ProcPointer *pProcPtr = compiler.GetObjectModule().meta.GetProcPointers()[type.GetIndex()];
180 resultType = pProcPtr->ReturnType();
181
182 if( isCallOn ){
183 if( !Opcode_CallProcPtr(fullCallName,lpszParms,pProcPtr) ){
184 return false;
185 }
186 }
187 }
188 else{
189 return false;
190 }
191
192 return true;
193}
194bool CallPropertyMethod( const char *variable, const char *rightSide, Type &resultType){
195 //プロパティ用のメソッドを呼び出す
196
197 //配列要素を取得
198 char VarName[VN_SIZE],ArrayElements[VN_SIZE];
199 GetArrayElement(variable,VarName,ArrayElements);
200
201 //オブジェクト名を取得
202 char ObjectName[VN_SIZE];
203 ReferenceKind referenceKind;
204 SplitObjectName(VarName,ObjectName, referenceKind );
205
206 //オーバーロード用の関数リストを作成
207 std::vector<const UserProc *> subs;
208 GetOverloadSubHash(VarName,subs);
209 if(subs.size()==0){
210 return false;
211 }
212
213 //パラメータを整備
214 char *Parameter;
215 Parameter=(char *)HeapAlloc(hHeap,0,lstrlen(ArrayElements)+lstrlen(rightSide)+32);
216 lstrcpy(Parameter,ArrayElements);
217 if(rightSide){
218 if(Parameter[0]&&rightSide[0]) lstrcat(Parameter,",");
219 lstrcat(Parameter,rightSide);
220 }
221
222 //オーバーロードを解決
223 const UserProc *pUserProc = OverloadSolutionWithStrParam(VarName,subs,Parameter,ObjectName);
224
225 if(pUserProc){
226 //呼び出し
227 Opcode_CallProc(Parameter,pUserProc,0,ObjectName);
228
229 resultType = pUserProc->ReturnType();
230 }
231
232 HeapDefaultFree(Parameter);
233
234 return true;
235}
236
237bool GetReturnTypeOfPropertyMethod( const char *variable, const char *rightSide, Type &resultType ){
238 //プロパティ用のメソッドを呼び出す
239
240 //配列要素を取得
241 char VarName[VN_SIZE],ArrayElements[VN_SIZE];
242 GetArrayElement(variable,VarName,ArrayElements);
243
244 //オブジェクト名を取得
245 char ObjectName[VN_SIZE];
246 ReferenceKind referenceKind;
247 SplitObjectName(VarName,ObjectName, referenceKind );
248
249 //オーバーロード用の関数リストを作成
250 std::vector<const UserProc *> subs;
251 GetOverloadSubHash(VarName,subs);
252 if(subs.size()==0){
253 return 0;
254 }
255
256 //パラメータを整備
257 char *Parameter;
258 Parameter=(char *)HeapAlloc(hHeap,0,lstrlen(ArrayElements)+lstrlen(rightSide)+32);
259 lstrcpy(Parameter,ArrayElements);
260 if(rightSide){
261 if(Parameter[0]&&rightSide[0]) lstrcat(Parameter,",");
262 lstrcat(Parameter,rightSide);
263 }
264
265 //オーバーロードを解決
266 const UserProc *pUserProc = OverloadSolutionWithStrParam(VarName,subs,Parameter,ObjectName);
267
268 if(pUserProc){
269 resultType = pUserProc->ReturnType();
270 }
271
272 return 1;
273}
274
275//インデクサ(getter)の戻り値を取得
276bool GetReturnTypeOfIndexerGetterProc( const CClass &objClass, Type &resultType ){
277 vector<const UserProc *> subs;
278 objClass.GetMethods().Enum( CALC_ARRAY_GET, subs );
279 if( subs.size() == 0 ){
280 return false;
281 }
282
283 resultType = subs[0]->ReturnType();
284
285 return true;
286}
287
288void CollectProcedures( const BasicSource &source, UserProcs &userProcs, DllProcs &dllProcs )
289{
290 extern HANDLE hHeap;
291 int i,i2,i3;
292 char temporary[8192];
293
294 // 名前空間管理
295 NamespaceScopes &namespaceScopes = compiler.GetNamespaceSupporter().GetLivingNamespaceScopes();
296 namespaceScopes.clear();
297
298 // Importsされた名前空間の管理
299 NamespaceScopesCollection &importedNamespaces = compiler.GetNamespaceSupporter().GetImportedNamespaces();
300 importedNamespaces.clear();
301
302 i=-1;
303 while(1){
304 i++;
305
306 if(source[i]==1&&(source[i+1]==ESC_CLASS||source[i+1]==ESC_INTERFACE)){
307 /* Class ~ End Class
308 Interface ~ End Interface
309 を飛び越す */
310 i3=GetEndXXXCommand(source[i+1]);
311 for(i+=2,i2=0;;i++,i2++){
312 if(source[i]=='\0') break;
313 if(source[i]==1&&source[i+1]==(char)i3){
314 i++;
315 break;
316 }
317 }
318 if(source[i]=='\0') break;
319 continue;
320 }
321
322 if( source[i] == 1 && source[i+1] == ESC_NAMESPACE ){
323 for(i+=2,i2=0;;i2++,i++){
324 if( IsCommandDelimitation( source[i] ) ){
325 temporary[i2]=0;
326 break;
327 }
328 temporary[i2]=source[i];
329 }
330 namespaceScopes.push_back( temporary );
331
332 continue;
333 }
334 else if( source[i] == 1 && source[i+1] == ESC_ENDNAMESPACE ){
335 if( namespaceScopes.size() <= 0 ){
336 SetError(12, "End Namespace", i );
337 }
338 else{
339 namespaceScopes.pop_back();
340 }
341
342 i += 2;
343 continue;
344 }
345 else if( source[i] == 1 && source[i+1] == ESC_IMPORTS ){
346 for(i+=2,i2=0;;i2++,i++){
347 if( IsCommandDelimitation( source[i] ) ){
348 temporary[i2]=0;
349 break;
350 }
351 temporary[i2]=source[i];
352 }
353 if( !compiler.GetNamespaceSupporter().ImportsNamespace( temporary ) )
354 {
355 SetError(64,temporary,cp );
356 }
357
358 continue;
359 }
360 else if( source[i] == 1 && source[i+1] == ESC_CLEARNAMESPACEIMPORTED ){
361 importedNamespaces.clear();
362 continue;
363 }
364
365 if(source[i]==1&&source[i+1]==ESC_DECLARE){
366 for(i+=2,i2=0;;i2++,i++){
367 if(source[i]=='\n'){
368 temporary[i2]=0;
369 break;
370 }
371 temporary[i2]=source[i];
372 if(source[i]=='\0') break;
373 }
374 dllProcs.Add(namespaceScopes,temporary,i);
375
376 continue;
377 }
378 if(source[i]==1&&(source[i+1]==ESC_SUB||source[i+1]==ESC_FUNCTION||source[i+1]==ESC_MACRO)){
379 char statementChar = source[i+1];
380
381 for(i2=0;;i2++,i++){
382 if(IsCommandDelimitation(source[i])){
383 temporary[i2]=0;
384 break;
385 }
386 temporary[i2]=source[i];
387 if(source[i]=='\0') break;
388 }
389 userProcs.Add(namespaceScopes, importedNamespaces, temporary,i,false,NULL,false);
390
391 /* Sub ~ End Sub
392 Function ~ End Function
393 Macro ~ End Macro
394 を飛び越す */
395 char endStatementChar = GetEndXXXCommand( statementChar );
396 for(i2=0;;i++,i2++){
397 if( source[i] == '\0' ) break;
398 if( source[i] == 1 && source[i+1] == endStatementChar ){
399 i++;
400 break;
401 }
402 }
403 if(source[i]=='\0') break;
404 continue;
405 }
406
407 //次の行
408 for(;;i++){
409 if(IsCommandDelimitation(source[i])) break;
410 }
411 if(source[i]=='\0') break;
412 }
413}
414
415bool IsNeedProcCompile(){
416 compiler.GetObjectModule().meta.GetUserProcs().Iterator_Reset();
417 while( compiler.GetObjectModule().meta.GetUserProcs().Iterator_HasNext() )
418 {
419 UserProc *pUserProc = compiler.GetObjectModule().meta.GetUserProcs().Iterator_GetNext();
420 if( pUserProc->IsUsing() && pUserProc->IsCompiled() == false ){
421 return true;
422 }
423 }
424 return false;
425}
Note: See TracBrowser for help on using the repository browser.