source: dev/BasicCompiler_Common/Variable.cpp@ 6

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