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

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

DllProc::pNextDataを排除した

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