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