source: dev/BasicCompiler_Common/Variable.cpp@ 42

Last change on this file since 42 was 40, checked in by dai_9181, 18 years ago

ByRef修飾子を関数戻り値とDimステートメントで指定可能にした。

File size: 26.5 KB
Line 
1#include "../BasicCompiler_Common/common.h"
2
3#ifdef _AMD64_
4#include "../BasicCompiler64/opcode.h"
5#else
6#include "../BasicCompiler32/opcode.h"
7#endif
8
9BOOL IsVariableTopChar(char c){
10 if((c>='A'&&c<='Z')||(c>='a'&&c<='z')||c=='_') return 1;
11 return 0;
12}
13BOOL IsVariableChar(char c){
14 if((c>='A'&&c<='Z')||(c>='a'&&c<='z')||(c>='0'&&c<='9')||
15 c=='%'||c=='!'||c=='#'||c=='$'||
16 c=='_'||c=='.') return 1;
17 return 0;
18}
19BOOL IsPtrType(int type){
20 if(type==-1) return 0;
21
22 if(PTR_LEVEL(type)||type==DEF_PTR_VOID||type==DEF_PTR_PROC||
23 (type&FLAG_PTR) ) return 1;
24
25 return 0;
26}
27BOOL IsSignedType(int type){
28 switch(type){
29 case DEF_CHAR:
30 case DEF_INTEGER:
31 case DEF_LONG:
32 case DEF_INT64:
33 case DEF_SINGLE:
34 case DEF_DOUBLE:
35 return 1;
36 default:
37 break;
38 }
39 return 0;
40}
41BOOL IsNaturalWholeNumberType(int type){
42 switch(type){
43 case DEF_CHAR:
44 case DEF_BYTE:
45 case DEF_INTEGER:
46 case DEF_WORD:
47 case DEF_LONG:
48 case DEF_DWORD:
49 case DEF_INT64:
50 case DEF_QWORD:
51 return 1;
52 default:
53 break;
54 }
55 return 0;
56}
57BOOL IsWholeNumberType(int type){
58 return (
59 IsNaturalWholeNumberType(type)
60 || IsPtrType(type)
61 || type == DEF_BOOLEAN
62 );
63}
64BOOL IsRealNumberType(int type){
65 switch(type){
66 case DEF_DOUBLE:
67 case DEF_SINGLE:
68 return 1;
69 default:
70 break;
71 }
72 return 0;
73}
74BOOL Is64Type(int type){
75 switch(type){
76 case DEF_INT64:
77 case DEF_QWORD:
78 return 1;
79 default:
80 break;
81 }
82#ifdef _AMD64_
83 return IsPtrType(type);
84#else
85 return 0;
86#endif
87}
88int GetSignedType(int type){
89 switch(type){
90 case DEF_BYTE:
91 return DEF_CHAR;
92 case DEF_WORD:
93 return DEF_INTEGER;
94 case DEF_DWORD:
95 return DEF_LONG;
96 case DEF_QWORD:
97 return DEF_INT64;
98 default:
99 break;
100 }
101#ifdef _AMD64_
102 if(IsPtrType(type)) return DEF_INT64;
103#else
104 if(IsPtrType(type)) return DEF_LONG;
105#endif
106 return type;
107}
108int GetUnsignedType(int type){
109 switch(type){
110 case DEF_CHAR:
111 return DEF_BYTE;
112 case DEF_INTEGER:
113 return DEF_WORD;
114 case DEF_LONG:
115 return DEF_DWORD;
116 case DEF_INT64:
117 return DEF_QWORD;
118 }
119 return type;
120}
121int GetTypeSize(int type,LONG_PTR lpIndex){
122 if(type==DEF_LONG){
123 if(lpIndex==LITERAL_NULL||lpIndex==LITERAL_M128_0||lpIndex==LITERAL_0_255)
124 return sizeof(BYTE);
125 else if(lpIndex==LITERAL_M32768_0||lpIndex==LITERAL_0_65535)
126 return sizeof(WORD);
127
128 return sizeof(DWORD);
129 }
130
131 //整数型
132 if(type==DEF_INT64||type==DEF_QWORD)
133 return sizeof(_int64);
134 else if(type==DEF_LONG||type==DEF_DWORD)
135 return sizeof(DWORD);
136 else if(type==DEF_INTEGER||type==DEF_WORD)
137 return sizeof(WORD);
138 else if(type==DEF_CHAR||type==DEF_BYTE || type == DEF_BOOLEAN)
139 return sizeof(BYTE);
140
141 //実数型
142 else if(type==DEF_DOUBLE) return sizeof(double);
143 else if(type==DEF_SINGLE) return sizeof(float);
144
145 //ポインタ型
146 else if(IsPtrType(type)) return PTR_SIZE;
147
148 else if(type==DEF_OBJECT){
149 CClass *pobj_c;
150 pobj_c=(CClass *)lpIndex;
151 if(!pobj_c) return 0;
152 return GetSizeOfClass(pobj_c);
153 }
154 else{
155 SetError(300,NULL,cp);
156 }
157 return 0;
158}
159int GetPtrType(int type,LONG_PTR lpIndex){
160 return MAKE_PTR_TYPE(NATURAL_TYPE(type),PTR_LEVEL(type)+1);
161}
162int GetTypeFixed(char *TypeName,LONG_PTR *lpNum){
163 int type;
164
165 if(lpNum) *lpNum=-1;
166
167 if(TypeName[0]=='*'){
168 if(TypeName[1]==1&&(TypeName[2]==ESC_FUNCTION||TypeName[2]==ESC_SUB)){
169 //関数ポインタ(*Function)
170 return DEF_PTR_PROC;
171 }
172
173 type=GetTypeFixed(TypeName+1,lpNum);
174 if(type==-1) return -1;
175
176 return GetPtrType(type,*lpNum);
177 }
178
179 //整数型
180 if(lstrcmpi(TypeName,"Char")==0) return DEF_CHAR;
181 else if(lstrcmpi(TypeName,"Byte")==0) return DEF_BYTE;
182 else if(lstrcmpi(TypeName,"Integer")==0) return DEF_INTEGER;
183 else if(lstrcmpi(TypeName,"WORD")==0) return DEF_WORD;
184 else if(lstrcmpi(TypeName,"Long")==0) return DEF_LONG;
185 else if(lstrcmpi(TypeName,"DWORD")==0) return DEF_DWORD;
186 else if(lstrcmpi(TypeName,"Int64")==0) return DEF_INT64;
187 else if(lstrcmpi(TypeName,"QWORD")==0) return DEF_QWORD;
188
189 //実数型
190 else if(lstrcmpi(TypeName,"Single")==0) return DEF_SINGLE;
191 else if(lstrcmpi(TypeName,"Double")==0) return DEF_DOUBLE;
192
193 //bool型
194 else if( lstrcmpi( TypeName, "Boolean" ) == 0 ) return DEF_BOOLEAN;
195
196 //ポインタ型
197 //※VoidPtr型以外のポインタ型はTypeDef宣言により定義される
198 else if(lstrcmpi(TypeName,"VoidPtr")==0) return DEF_PTR_VOID;
199
200 //その他
201 else if(lstrcmpi(TypeName,"Any")==0) return DEF_ANY;
202
203
204 ////////////////////
205 // TypeDefされた型
206 ////////////////////
207 int i;
208 i=pobj_DBTypeDef->check(TypeName);
209 if(i!=-1){
210 return GetTypeFixed(pobj_DBTypeDef->ppobj_TypeDef[i]->lpszBaseName,lpNum);
211 }
212
213 if(!lpNum) return -1;
214
215 //クラス
216 CClass *pobj_c;
217 pobj_c=pobj_DBClass->check(TypeName);
218 if(pobj_c){
219 *lpNum=(LONG_PTR)pobj_c;
220 return DEF_OBJECT;
221 }
222
223 return -1;
224}
225void GetOriginalTypeName(char *buffer){
226 // TypeDefされた型に対して、オリジナルの型の名前に変更する
227
228 if(buffer[0]=='*'){
229 if(buffer[1]==1&&(buffer[2]==ESC_FUNCTION||buffer[2]==ESC_SUB)) return;
230
231 GetOriginalTypeName(buffer+1);
232 return;
233 }
234
235 int i;
236 i=pobj_DBTypeDef->check(buffer);
237 if(i!=-1){
238 lstrcpy(buffer,pobj_DBTypeDef->ppobj_TypeDef[i]->lpszBaseName);
239 GetOriginalTypeName(buffer);
240 }
241}
242BOOL GetTypeName(int type,LONG_PTR lpIndex,char *name){
243 if(PTR_LEVEL(type)){
244 //ポインタ型
245 name[0]='*';
246 return GetTypeName(MAKE_PTR_TYPE(NATURAL_TYPE(type),PTR_LEVEL(type)-1),lpIndex,name+1);
247 }
248
249 //整数型
250 if(type==DEF_CHAR) lstrcpy(name,"Char");
251 else if(type==DEF_BYTE) lstrcpy(name,"Byte");
252 else if(type==DEF_INTEGER) lstrcpy(name,"Integer");
253 else if(type==DEF_WORD) lstrcpy(name,"Word");
254 else if(type==DEF_LONG) lstrcpy(name,"Long");
255 else if(type==DEF_DWORD) lstrcpy(name,"DWord");
256 else if(type==DEF_INT64) lstrcpy(name,"Int64");
257 else if(type==DEF_QWORD) lstrcpy(name,"QWord");
258
259 //実数型
260 else if(type==DEF_SINGLE) lstrcpy(name,"Single");
261 else if(type==DEF_DOUBLE) lstrcpy(name,"Double");
262
263 //bool型
264 else if(type==DEF_BOOLEAN) lstrcpy(name,"Boolean");
265
266 //オブジェクト
267 else if(type==DEF_OBJECT){
268 if(lpIndex==0) lstrcpy(name,"non");
269 else{
270 lstrcpy(name,((CClass *)lpIndex)->name);
271 }
272 }
273
274 //ポインタ型
275 else if(type==DEF_PTR_VOID) lstrcpy(name,"VoidPtr");
276
277 else if(type==DEF_PTR_PROC){
278 if(lpIndex==-1) lstrcpy(name,"VoidPtr");
279 else{
280 extern PROCPTRINFO *pProcPtrInfo;
281 if(pProcPtrInfo[lpIndex].ReturnType==-1)
282 lstrcpy(name,"*Sub");
283 else lstrcpy(name,"*Function");
284 }
285 }
286
287 else{
288 extern int cp;
289 SetError(1,NULL,cp);
290 return 0;
291 }
292 return 1;
293}
294
295TYPEINFO GetStringTypeInfo(){
296 extern CClass *pobj_StringClass;
297 TYPEINFO TypeInfo = { DEF_OBJECT, (LONG_PTR)pobj_StringClass };
298 return TypeInfo;
299}
300
301BOOL GetVarFormatString(char *buffer,char *array,char *array2,char *NestMember,int *pRefType){
302 extern int cp;
303 int i,i2,i3;
304 char cPare_Open,cPare_Close;
305
306 array[0]=0;
307 array2[0]=0;
308 NestMember[0]=0;
309 *pRefType=0;
310 for(i=0;;i++){
311 if(buffer[i]=='\"'){
312 for(i++;;i++){
313 if(IsDBCSLeadByte(buffer[i])){
314 i++;
315 continue;
316 }
317 if(buffer[i]=='\"') break;
318 }
319 }
320 if(buffer[i]=='['||buffer[i]=='('){
321 if(buffer[i]=='['){
322 cPare_Open='[';
323 cPare_Close=']';
324 }
325 else{
326 cPare_Open='(';
327 cPare_Close=')';
328 }
329 buffer[i]=0;
330 for(i++,i2=0;;i++,i2++){
331 if(buffer[i]==cPare_Open){
332 if(cPare_Open=='[') i3=GetStringInBracket(array+i2,buffer+i);
333 else i3=GetStringInPare(array+i2,buffer+i);
334 i+=i3-1;
335 i2+=i3-1;
336 continue;
337 }
338 if(buffer[i]==cPare_Close){
339 array[i2]=0;
340 break;
341 }
342 array[i2]=buffer[i];
343 }
344 if(buffer[i+1]==cPare_Open){
345 for(i+=2,i2=0;;i++,i2++){
346 if(buffer[i]==cPare_Open){
347 if(cPare_Open=='[') i3=GetStringInBracket(array2+i2,buffer+i);
348 else i3=GetStringInPare(array2+i2,buffer+i);
349 i+=i3-1;
350 i2+=i3-1;
351 continue;
352 }
353 if(buffer[i]==cPare_Close){
354 array2[i2]=0;
355 break;
356 }
357 array2[i2]=buffer[i];
358 }
359 if(buffer[i+1]==cPare_Open){
360 SetError(14,buffer,cp);
361 return 0;
362 }
363 }
364 continue;
365 }
366 if(buffer[i]=='.'){
367 lstrcpy(NestMember,buffer+i+1);
368 *pRefType=DEF_OBJECT;
369 buffer[i]=0;
370 break;
371 }
372 if(buffer[i]==1&&buffer[i+1]==ESC_PSMEM){
373 lstrcpy(NestMember,buffer+i+2);
374 *pRefType=DEF_PTR_OBJECT;
375 buffer[i]=0;
376 break;
377 }
378 if(buffer[i]=='\0') break;
379 }
380 return 1;
381}
382
383void GetArrayElement(char *buffer,char *variable,char *array_element){
384 array_element[0]=0;
385
386 if(buffer[lstrlen(buffer)-1]!=']'){
387 lstrcpy(variable,buffer);
388 return;
389 }
390
391 int i,i2;
392 for(i=0;;i++){
393 if(buffer[i]=='\0') break;
394 if(buffer[i]=='['){
395 i2=GetStringInBracket(array_element,buffer+i);
396 i+=i2-1;
397 continue;
398 }
399 }
400
401 lstrcpy(variable,buffer);
402 variable[lstrlen(variable)-lstrlen(array_element)]=0;
403
404 RemoveStringBracket(array_element);
405}
406
407BOOL CheckVarNameError(char *name,int NowLine){
408 int i2;
409
410 if(!IsVariableTopChar(name[0])){
411 SetError(1,NULL,NowLine);
412 return 0;
413 }
414 for(i2=1;;i2++){
415 if(name[i2]=='\0') break;
416 if(!IsVariableChar(name[i2])){
417 SetError(1,NULL,NowLine);
418 return 0;
419 }
420 }
421 return 1;
422}
423
424int JumpSubScripts(int *ss){
425 //DIMで定義された並んだ配列の数だけアドレスを進める
426 int i,i2;
427 for(i=0,i2=1;i<255;i++){
428 if(ss[i]==-1) break;
429 i2*=ss[i]+1;
430 }
431 return i2;
432}
433void GetArrange(char *variable,char *variAnswer,int *SubScripts){
434 extern int cp;
435 int i,i2,i3,i4;
436 double dbl;
437 _int64 i64data;
438 BOOL bBracket;
439 char temporary[VN_SIZE];
440
441 for(i=0;;i++){
442 if(variable[i]=='('||variable[i]=='['){
443 if(variable[i]=='[') bBracket=1;
444 else bBracket=0;
445
446 variAnswer[i]=0;
447 for(i++,i2=0,i3=0;;i++,i2++){
448 if(variable[i]==','){
449 temporary[i2]=0;
450
451 i2=StaticCalculation(true, temporary,0,&i64data,0);
452 if(IsRealNumberType(i2)){
453 memcpy(&dbl,&i64data,sizeof(double));
454 i64data=(_int64)dbl;
455 }
456
457 if(i64data<0){
458 //error
459 SubScripts[i3]=0;
460 }
461 else SubScripts[i3]=(int)i64data;
462 i3++;
463 i2=-1;
464 continue;
465 }
466 if(variable[i]=='('){
467 i4=GetStringInPare(temporary+i2,variable+i);
468 i2+=i4-1;
469 i+=i4-1;
470 continue;
471 }
472 if(variable[i]=='['){
473 i4=GetStringInBracket(temporary+i2,variable+i);
474 i2+=i4-1;
475 i+=i4-1;
476 continue;
477 }
478 if(variable[i]==')'&&bBracket==0||
479 variable[i]==']'&&bBracket){
480 temporary[i2]=0;
481 if(i2==0){
482 SubScripts[i3]=-2;
483 break;
484 }
485
486 i2=StaticCalculation(true, temporary,0,&i64data,0);
487 if(IsRealNumberType(i2)){
488 memcpy(&dbl,&i64data,sizeof(double));
489 i64data=(_int64)dbl;
490 }
491
492 if(i64data<0){
493 //error
494 SubScripts[i3]=0;
495 }
496 else SubScripts[i3]=(int)i64data;
497 SubScripts[i3+1]=-1;
498 break;
499 }
500 if(variable[i]=='\"'){
501 SetError(1,NULL,cp);
502 return;
503 }
504 temporary[i2]=variable[i];
505 }
506 break;
507 }
508 variAnswer[i]=variable[i];
509 if(variable[i]=='\0'){
510 SubScripts[0]=-1;
511 break;
512 }
513 }
514}
515
516int GetTypeFromSimpleName(char *variable){
517 extern char DefIntVari[26],DefSngVari[26],DefStrVari[26],divNum,dsvNum,dStrvNum;
518 int i;
519 char name[VN_SIZE];
520
521 //構造体メンバの場合を考慮
522 for(i=lstrlen(variable);i>0;i--){
523 if(variable[i]=='.'){
524 i++;
525 break;
526 }
527 }
528
529 for(;;i++){
530 if(variable[i]=='('||variable[i]=='\0'){
531 name[i]=0;
532 break;
533 }
534 name[i]=variable[i];
535 }
536 //変数名から選択
537 i--;
538 if(name[i]=='#') return DEF_DOUBLE;
539 if(name[i]=='!') return DEF_SINGLE;
540 if(name[i]=='%') return DEF_INTEGER;
541 return DEF_DOUBLE;
542}
543
544
545int GetMemberType(CClass *pobj_c,char *member,LONG_PTR *plpNestIndex,BOOL bPrivateAccess,BOOL bError){
546 extern int cp;
547 int i;
548
549 //クラス、配列の構成要素を解析する
550 char VarName[VN_SIZE]; //変数名
551 char array[VN_SIZE]; //第1次配列
552 char lpPtrOffset[VN_SIZE]; //第2次配列
553 char NestMember[VN_SIZE]; //入れ子メンバ
554 int object_type; //"."参照のときは0、"->"参照のときは1
555 lstrcpy(VarName,member);
556 if(!GetVarFormatString(VarName,array,lpPtrOffset,NestMember,&object_type)) return 0;
557
558 for(i=0;i<pobj_c->iMemberNum;i++){
559 if(lstrcmp(pobj_c->ppobj_Member[i]->name,VarName)==0) break;
560 }
561 if(i==pobj_c->iMemberNum){
562 if(bError) SetError(103,VarName,cp);
563 return -1;
564 }
565
566 //アクセシビリティをチェック
567 if(pobj_c==pobj_CompilingClass){
568 //同一クラスオブジェクトの場合はプライベートアクセスを容認する
569 if(pobj_c->ppobj_Member[i]->dwAccess==ACCESS_NON){
570 if(bError) SetError(107,VarName,cp);
571 return -1;
572 }
573 }
574 else{
575 if((bPrivateAccess==0&&pobj_c->ppobj_Member[i]->dwAccess==ACCESS_PRIVATE)||
576 pobj_c->ppobj_Member[i]->dwAccess==ACCESS_NON){
577 if(bError) SetError(107,VarName,cp);
578 return -1;
579 }
580 else if(bPrivateAccess==0&&pobj_c->ppobj_Member[i]->dwAccess==ACCESS_PROTECTED){
581 if(bError) SetError(108,VarName,cp);
582 return -1;
583 }
584 }
585
586 int type;
587 type=pobj_c->ppobj_Member[i]->TypeInfo.type;
588 *plpNestIndex=pobj_c->ppobj_Member[i]->TypeInfo.u.lpIndex;
589
590 //ポインタ変数の場合
591 if(IsPtrType(type)){
592 if(pobj_c->ppobj_Member[i]->SubScripts[0]==-1){
593 lstrcpy(lpPtrOffset,array);
594 array[0]=0;
595 }
596 }
597 else{
598 if(lpPtrOffset[0]){
599 if(bError) SetError(16,member,cp);
600 return 0;
601 }
602 }
603
604 if(object_type){
605 //入れ子構造の場合
606
607 return GetMemberType(pobj_c->ppobj_Member[i]->TypeInfo.u.pobj_Class,
608 NestMember,
609 plpNestIndex,
610 0,
611 bError);
612 }
613
614 if(array[0]==0&&pobj_c->ppobj_Member[i]->SubScripts[0]!=-1){
615 return type|FLAG_PTR;
616 }
617
618 if(lpPtrOffset[0]){
619 if(PTR_LEVEL(type)){
620 type=MAKE_PTR_TYPE(NATURAL_TYPE(type),PTR_LEVEL(type)-1);
621 }
622 else{
623 //エラー
624 if(bError) SetError(1,NULL,cp);
625 return -1;
626 }
627 }
628
629 return type;
630}
631int GetVarType(const char *NameBuffer,LONG_PTR *plpIndex,BOOL bError){
632 extern BOOL bCompilingGlobal;
633 int i;
634 char variable[VN_SIZE],member[VN_SIZE],VarName[VN_SIZE],array[VN_SIZE],lpPtrOffset[VN_SIZE];
635
636 if(NameBuffer[0]=='.'){
637 GetWithName(variable);
638 lstrcat(variable,NameBuffer);
639 }
640 else lstrcpy(variable,NameBuffer);
641
642 lstrcpy(VarName,variable);
643 GetVarFormatString(VarName,array,lpPtrOffset,member,&i);
644
645 int *pSubScripts;
646 int type;
647 LONG_PTR lpIndex;
648
649 extern int MaxGlobalVarNum;
650 extern VARIABLE *GlobalVar;
651 extern int MaxLocalVarNum;
652 extern VARIABLE *LocalVar;
653
654 if(bCompilingGlobal==0){
655 /////////////////
656 // ローカル変数
657 /////////////////
658
659 for(i=MaxLocalVarNum-1;i>=0;i--){ //レキシカルスコープを考慮してバックサーチ
660 if( LocalVar[i].bLiving //現在のスコープで有効なもの
661 && LocalVar[i].ScopeLevel <= obj_LexScopes.GetNowLevel() //現在のスコープレベルを超さないもの(Returnによる解放処理中を考慮)
662 ){
663 if(lstrcmp(VarName,LocalVar[i].name)==0) break;
664 }
665 }
666 if(i>=0){
667 //ポインタ変数の場合
668 if(IsPtrType(LocalVar[i].type)){
669 if(LocalVar[i].SubScripts[0]==-1){
670 lstrcpy(lpPtrOffset,array);
671 array[0]=0;
672 }
673 }
674 else{
675 if(lpPtrOffset[0]){
676 if(bError) SetError(16,variable,cp);
677 return -1;
678 }
679 }
680
681 type=LocalVar[i].type;
682 lpIndex=LocalVar[i].u.index;
683 pSubScripts=LocalVar[i].SubScripts;
684
685 goto ok;
686 }
687 }
688
689 if(pobj_CompilingClass){
690 ///////////////////////
691 // クラスメンバの参照
692 ///////////////////////
693
694 if(lstrcmpi(variable,"This")==0){
695 //Thisオブジェクト
696 *plpIndex=(LONG_PTR)pobj_CompilingClass;
697 return DEF_OBJECT;
698 }
699
700 if(_memicmp(variable,"This.",5)==0){
701 //Thisオブジェクトのメンバを参照するとき
702 SlideString(variable+5,-5);
703 lstrcpy(VarName,variable);
704 }
705 else{
706 //クラス内メンバを参照するとき(通常)
707
708 for(i=0;i<pobj_CompilingClass->iMemberNum;i++){
709 if(lstrcmp(VarName,pobj_CompilingClass->ppobj_Member[i]->name)==0) break;
710 }
711 if(i==pobj_CompilingClass->iMemberNum) goto NonClassMember;
712 }
713
714 return GetMemberType(pobj_CompilingClass,variable,plpIndex,1,bError);
715 }
716
717NonClassMember:
718
719 //////////////////////////
720 // 静的ローカル変数
721 // ※"Static.Object.Method.Variable"
722 //////////////////////////
723
724 char temporary[VN_SIZE];
725 extern SUBINFO *pCompilingSubInfo;
726 if(pCompilingSubInfo){
727 GetNowStaticVarFullName(VarName,temporary);
728
729 for(i=0;i<MaxGlobalVarNum;i++){
730 if(lstrcmp(temporary,GlobalVar[i].name)==0) break;
731 }
732 if(i!=MaxGlobalVarNum){
733 goto GlobalOk;
734 }
735 }
736
737
738 //////////////////////////
739 // クラスの静的メンバ
740 //////////////////////////
741
742 if(member[0]){
743 lstrcpy(temporary,member);
744 char tempMember[VN_SIZE];
745 char tempArray[VN_SIZE];
746 GetVarFormatString(temporary,tempArray,lpPtrOffset,tempMember,&i);
747
748 char temp2[VN_SIZE];
749 sprintf(temp2,"%s.%s",VarName,temporary);
750 for(i=0;i<MaxGlobalVarNum;i++){
751 if(lstrcmp(temp2,GlobalVar[i].name)==0) break;
752 }
753
754 if(i!=MaxGlobalVarNum){
755 lstrcpy(member,tempMember);
756 lstrcpy(array,tempArray);
757 goto GlobalOk;
758 }
759 }
760
761 if(pobj_CompilingClass){
762 //自身のクラスから静的メンバを参照する場合
763 char temp2[VN_SIZE];
764 sprintf(temp2,"%s.%s",pobj_CompilingClass->name,VarName);
765 for(i=0;i<MaxGlobalVarNum;i++){
766 if(lstrcmp(temp2,GlobalVar[i].name)==0) break;
767 }
768
769 if(i!=MaxGlobalVarNum){
770 goto GlobalOk;
771 }
772 }
773
774
775 ////////////////////
776 // グローバル変数
777 ////////////////////
778
779 for(i=MaxGlobalVarNum-1;i>=0;i--){ //レキシカルスコープを考慮してバックサーチ
780 if( GlobalVar[i].bLiving //現在のスコープで有効なもの
781 && GlobalVar[i].ScopeLevel <= obj_LexScopes.GetNowLevel() //現在のスコープレベルを超さないもの(Returnによる解放処理中を考慮)
782 ){
783 if(lstrcmp(VarName,GlobalVar[i].name)==0) break;
784 }
785 }
786
787 if(i>=0){
788 goto GlobalOk;
789 }
790
791 //変数として見つからなかったとき
792 if(bError) SetError(3,variable,cp);
793 return -1;
794
795GlobalOk:
796
797 //ポインタ変数の場合
798 if(IsPtrType(GlobalVar[i].type)){
799 if(GlobalVar[i].SubScripts[0]==-1){
800 lstrcpy(lpPtrOffset,array);
801 array[0]=0;
802 }
803 }
804 else{
805 if(lpPtrOffset[0]){
806 if(bError) SetError(16,variable,cp);
807 return -1;
808 }
809 }
810
811 type=GlobalVar[i].type;
812 lpIndex=GlobalVar[i].u.index;
813 pSubScripts=GlobalVar[i].SubScripts;
814
815ok:
816
817 if(member[0]){
818 if(NATURAL_TYPE(type)==DEF_OBJECT)
819 return GetMemberType((CClass *)lpIndex,member,plpIndex,0,bError);
820 }
821
822 if(array[0]==0&&pSubScripts[0]!=-1){
823 //配列の先頭ポインタを示す場合
824 type|=FLAG_PTR;
825 }
826
827 if(lpPtrOffset[0]){
828 if(PTR_LEVEL(type)){
829 type=MAKE_PTR_TYPE(NATURAL_TYPE(type),PTR_LEVEL(type)-1);
830 }
831 else{
832 //エラー
833 if(bError) SetError(1,NULL,cp);
834 return -1;
835 }
836 }
837
838 *plpIndex=lpIndex;
839 return type;
840}
841
842BOOL GetVarOffsetReadOnly(const char *NameBuffer,int *pType,RELATIVE_VAR *pRelativeVar,LONG_PTR *plpIndex,int *pss){
843 //読み取り専用で変数へアクセス
844 return GetVarOffset(
845 true, //エラー表示有効
846 false, //書き込みアクセスは無し
847 NameBuffer,
848 pType,
849 pRelativeVar,
850 plpIndex,
851 pss);
852}
853BOOL GetVarOffsetReadWrite(const char *NameBuffer,int *pType,RELATIVE_VAR *pRelativeVar,LONG_PTR *plpIndex,int *pss){
854 //読み書き両用で変数へアクセス
855 return GetVarOffset(
856 true, //エラー表示有効
857 true, //書き込みアクセス
858 NameBuffer,
859 pType,
860 pRelativeVar,
861 plpIndex,
862 pss);
863}
864
865
866
867BOOL GetDimentionFormat(const char *buffer, const bool isRef, char *VarName,int *SubScripts,TYPEINFO *pTypeInfo,char *InitBuf,char *ConstractParameter){
868 int i,i2,i3,IsStr;
869 char variable[VN_SIZE],temporary[8192];
870
871 for(i=0;;i++){
872 if((buffer[i]==1&&buffer[i+1]==ESC_AS)||
873 buffer[i]=='='||
874 buffer[i]=='\0'){
875 variable[i]=0;
876 break;
877 }
878 variable[i]=buffer[i];
879 }
880
881 if(buffer[i]=='='){
882 ////////////////////////////////////
883 // 初期化データが指定されいるとき
884 ////////////////////////////////////
885
886 for(i++,i2=0,IsStr=0;;i++,i2++){
887 if(buffer[i]=='\"') IsStr^=1;
888 if(buffer[i]=='('&&IsStr==0){
889 i3=GetStringInPare(InitBuf+i2,buffer+i);
890 i+=i3-1;
891 i2+=i3-1;
892 continue;
893 }
894 if(buffer[i]=='['&&IsStr==0){
895 i3=GetStringInBracket(InitBuf+i2,buffer+i);
896 i+=i3-1;
897 i2+=i3-1;
898 continue;
899 }
900 if((buffer[i]==1&&buffer[i+1]==ESC_AS)||
901 (buffer[i]==','&&IsStr==0)||
902 buffer[i]=='\0'){
903 InitBuf[i2]=0;
904 break;
905 }
906 InitBuf[i2]=buffer[i];
907 }
908 }
909 else{
910 //初期化データなし
911 InitBuf[0]=0;
912 }
913
914 ConstractParameter[0]=0;
915 if(buffer[i]==1&&buffer[i+1]==ESC_AS){
916 /////////////////////////////
917 // "As ~" による型指定あり
918 /////////////////////////////
919
920 for(i+=2,i2=0;;i++,i2++){
921 if(buffer[i]==','||buffer[i]=='('||buffer[i]=='\0'){
922 temporary[i2]=0;
923 break;
924 }
925 temporary[i2]=buffer[i];
926 }
927 pTypeInfo->type=GetTypeFixed(temporary,&pTypeInfo->u.lpIndex);
928 if(pTypeInfo->type==-1){
929 SetError(3,temporary,cp);
930 return 0;
931 }
932
933 if(pTypeInfo->type==DEF_PTR_PROC){
934 if(temporary[0]=='*'&&
935 temporary[1]==1&&
936 (temporary[2]==ESC_FUNCTION||temporary[2]==ESC_SUB)){
937 if(buffer[i]!='('){
938 SetError(10,temporary,cp);
939 return 0;
940 }
941 i3=GetStringInPare(temporary+3,buffer+i);
942 i+=i3;
943 i2+=i3;
944
945 if(temporary[2]==ESC_FUNCTION&&buffer[i]==1&&buffer[i+1]==ESC_AS){
946 temporary[i2++]=buffer[i++];
947 temporary[i2++]=buffer[i++];
948 for(;;i++,i2++){
949 if(!IsVariableChar(buffer[i])){
950 temporary[i2]=0;
951 break;
952 }
953 temporary[i2]=buffer[i];
954 }
955 }
956 }
957 else{
958 //TypeDefをする前のベース型を取得
959 GetOriginalTypeName(temporary);
960 }
961
962 if(temporary[3]!='('){
963 SetError(10,temporary,cp);
964 return 0;
965 }
966
967 //関数ポインタ
968 pTypeInfo->u.lpIndex=AddProcPtrInfo(temporary+3,temporary[2]);
969 }
970
971 if(buffer[i]=='('){
972 //コンストラクタに渡すパラメータを取得
973 i2=GetStringInPare(ConstractParameter,buffer+i);
974 i+=i2;
975 RemoveStringPare(ConstractParameter);
976
977 if(pTypeInfo->type!=DEF_OBJECT){
978 SetError(112,variable,cp);
979 return 0;
980 }
981 }
982 }
983 else{
984 /////////////////
985 // As指定なし
986 /////////////////
987
988 if( InitBuf[0] == '\0' ){
989 //As指定も、初期値指定もない場合
990 pTypeInfo->type=GetTypeFromSimpleName(variable);
991
992 i2=lstrlen(variable)-1;
993 if(i2>=0){
994 if(!(variable[i2]=='#'||variable[i2]=='!'||variable[i2]=='%'||variable[i2]=='$'))
995 SetError(-103,variable,cp);
996 }
997 }
998 else{
999 //初期値の型を判別して自動的に型情報を付加する
1000 TYPEINFO BaseType = GetStringTypeInfo();
1001 int result = NumOpe_GetType( InitBuf, &BaseType, &pTypeInfo->u.lpIndex );
1002
1003 //エラーの場合
1004 if( result == -1 ) return 0;
1005
1006 pTypeInfo->type = result;
1007 }
1008
1009 }
1010
1011 if( InitBuf[0] != '\0' && ConstractParameter[0] != '\0' ){
1012 //初期値とコンストラクタパラメータが同時に呼び出されているとき
1013 SetError(132, NULL, cp);
1014 }
1015
1016 if( pTypeInfo->type == DEF_OBJECT && InitBuf[0] != '\0' && InitBuf[0] != '[' && isRef == false ){
1017 lstrcpy( ConstractParameter, InitBuf );
1018 InitBuf[0] = 0;
1019 }
1020
1021 GetArrange(variable,VarName,SubScripts);
1022 return 1;
1023}
1024
1025BOOL GetNowStaticVarFullName(char *VarName,char *FullName){
1026 extern SUBINFO *pCompilingSubInfo;
1027 if(!pCompilingSubInfo) return 0;
1028
1029 //Static識別
1030 lstrcpy(FullName,"Static%");
1031
1032 //クラス名
1033 if(pobj_CompilingClass){
1034 lstrcat(FullName,pobj_CompilingClass->name);
1035 lstrcat(FullName,"%");
1036 }
1037
1038 //関数(またはメソッド)名
1039 lstrcat(FullName,pCompilingSubInfo->name);
1040 lstrcat(FullName,"%");
1041
1042 //ID
1043 char temp[255];
1044 sprintf(temp,"%x",pCompilingSubInfo->id);
1045 lstrcat(FullName,temp);
1046 lstrcat(FullName,"%");
1047
1048 //変数名
1049 lstrcat(FullName,VarName);
1050
1051 return 1;
1052}
1053
1054
1055void AddGlobalVariable(char *name,int *SubScripts,TYPEINFO *pTypeInfo,int TypeSize,char *InitBuf,char *ConstractParameter,DWORD dwFlag){
1056 /////////////////////////
1057 // グローバル変数を追加
1058 /////////////////////////
1059 extern VARIABLE *GlobalVar;
1060 extern int MaxGlobalVarNum;
1061 extern int AllInitGlobalVarSize;
1062 extern int AllGlobalVarSize;
1063
1064 int i2,i3,VarSize;
1065
1066 for(i2=0;i2<MaxGlobalVarNum;i2++){
1067 if(GlobalVar[i2].bLiving&&obj_LexScopes.GetNowLevel()==GlobalVar[i2].ScopeLevel){
1068 if(lstrcmp(GlobalVar[i2].name,name)==0){
1069 //2重定義のエラー
1070 SetError(15,name,cp);
1071 return;
1072 }
1073 }
1074 }
1075
1076 GlobalVar=(VARIABLE *)HeapReAlloc(hHeap,0,GlobalVar,(MaxGlobalVarNum+1)*sizeof(VARIABLE));
1077
1078 for(i2=1,i3=0;i3<255;i3++){
1079 //配列要素数
1080 GlobalVar[MaxGlobalVarNum].SubScripts[i3]=SubScripts[i3];
1081
1082 if(SubScripts[i3]==-1) break;
1083 i2*=SubScripts[i3]+1;
1084 }
1085 VarSize=TypeSize*i2;
1086 if(VarSize%PTR_SIZE) VarSize+=PTR_SIZE-(VarSize%PTR_SIZE);
1087
1088 //新しいオブジェクトポインタを取得
1089 VARIABLE *pVar = &GlobalVar[MaxGlobalVarNum];
1090
1091 //グローバル変数の個数を増加
1092 MaxGlobalVarNum++;
1093
1094 lstrcpy(pVar->name,name);
1095 pVar->fRef = 0;
1096 if(dwFlag & DIMFLAG_CONST) pVar->bConst = true;
1097 else pVar->bConst = false;
1098 if(SubScripts[0]==-1) pVar->bArray=0;
1099 else pVar->bArray=1;
1100 pVar->type=pTypeInfo->type;
1101 pVar->u.index=pTypeInfo->u.lpIndex;
1102
1103 //コンストラクタ用パラメータ
1104 pVar->ConstractParameter=(char *)HeapAlloc(hHeap,0,lstrlen(ConstractParameter)+1);
1105 lstrcpy(pVar->ConstractParameter,ConstractParameter);
1106
1107 if(InitBuf[0]||dwFlag==DIMFLAG_INITDEBUGVAR){
1108 //初期バッファがあるとき
1109 pVar->offset=AllInitGlobalVarSize;
1110 AllInitGlobalVarSize+=VarSize;
1111 }
1112 else{
1113 //初期バッファがないとき
1114 pVar->offset=AllGlobalVarSize | 0x80000000;
1115 AllGlobalVarSize+=VarSize;
1116 }
1117
1118 //レキシカルスコープ
1119 pVar->ScopeLevel=obj_LexScopes.GetNowLevel();
1120 pVar->ScopeStartAddress=obj_LexScopes.GetStartAddress();
1121 pVar->bLiving=TRUE;
1122
1123 //エラー用
1124 pVar->source_code_address=cp;
1125
1126 //初期バッファにデータをセット
1127 extern BYTE *initGlobalBuf;
1128 initGlobalBuf=(BYTE *)HeapReAlloc(hHeap,
1129 HEAP_ZERO_MEMORY,
1130 initGlobalBuf,
1131 AllInitGlobalVarSize);
1132 if(InitBuf[0]){
1133 if( pVar->type == DEF_OBJECT && InitBuf[0] != '[' ){
1134 //単発式が初期値のオブジェクトの場合
1135 //初期値をコピーコンストラクタに渡す
1136 }
1137 else{
1138 int result = SetInitGlobalData(pVar->offset,
1139 pVar->type,
1140 pVar->u.index,
1141 pVar->SubScripts,
1142 InitBuf);
1143
1144 if(!result){
1145 //動的な式だった場合は代入演算を行う
1146
1147 //初期代入時のみ、書き込みアクセスを許可する
1148 bool bConstBack = pVar->bConst;
1149 pVar->bConst = false;
1150
1151 //代入
1152 char temporary[8192];
1153 sprintf(temporary,"%s=%s",name,InitBuf);
1154 OpcodeCalc(temporary);
1155
1156 //アクセス制限を元に戻す
1157 pVar->bConst = bConstBack;
1158 }
1159 }
1160 }
1161
1162
1163
1164
1165 if(pTypeInfo->type==DEF_OBJECT){
1166 //利用対象のクラスコンストラクタ、デストラクタに使用チェックをつける
1167 for(i2=0;i2<pTypeInfo->u.pobj_Class->iMethodNum;i2++){
1168 if(lstrcmp(pTypeInfo->u.pobj_Class->ppobj_Method[i2]->psi->name,pTypeInfo->u.pobj_Class->name)==0){
1169 //コンストラクタ
1170 pTypeInfo->u.pobj_Class->ppobj_Method[i2]->psi->bUse=1;
1171 }
1172 if(pTypeInfo->u.pobj_Class->ppobj_Method[i2]->psi->name[0]=='~'){
1173 //デストラクタ
1174 pTypeInfo->u.pobj_Class->ppobj_Method[i2]->psi->bUse=1;
1175 }
1176 }
1177 }
1178}
Note: See TracBrowser for help on using the repository browser.