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

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

DllProc::pNextDataを排除した

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