source: dev/BasicCompiler_Common/Variable.cpp@ 16

Last change on this file since 16 was 11, checked in by dai_9181, 18 years ago

Const変数の書き込み規制を有効化(グローバル/ローカル変数のみ)
定数オブジェクトと定数メンバは未実装。

File size: 24.7 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 (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
682 if(_memicmp(variable,"This.",5)==0){
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
817BOOL GetVarOffsetReadOnly(char *NameBuffer,int *pType,RELATIVE_VAR *pRelativeVar,LONG_PTR *plpIndex,int *pss){
818 //読み取り専用で変数へアクセス
819 return GetVarOffset(
820 true, //エラー表示有効
821 false, //書き込みアクセスは無し
822 NameBuffer,
823 pType,
824 pRelativeVar,
825 plpIndex,
826 pss);
827}
828BOOL GetVarOffsetReadWrite(char *NameBuffer,int *pType,RELATIVE_VAR *pRelativeVar,LONG_PTR *plpIndex,int *pss){
829 //読み書き両用で変数へアクセス
830 return GetVarOffset(
831 true, //エラー表示有効
832 true, //書き込みアクセス
833 NameBuffer,
834 pType,
835 pRelativeVar,
836 plpIndex,
837 pss);
838}
839
840
841
842BOOL GetDimentionFormat(char *buffer,char *VarName,int *SubScripts,TYPEINFO *pTypeInfo,char *InitBuf,char *ConstractParameter){
843 int i,i2,i3,IsStr;
844 char variable[VN_SIZE],temporary[8192];
845
846 for(i=0;;i++){
847 if((buffer[i]==1&&buffer[i+1]==ESC_AS)||
848 buffer[i]=='='||
849 buffer[i]=='\0'){
850 variable[i]=0;
851 break;
852 }
853 variable[i]=buffer[i];
854 }
855
856 if(buffer[i]=='='){
857 ////////////////////////////////////
858 // 初期化データが指定されいるとき
859 ////////////////////////////////////
860
861 for(i++,i2=0,IsStr=0;;i++,i2++){
862 if(buffer[i]=='\"') IsStr^=1;
863 if(buffer[i]=='('&&IsStr==0){
864 i3=GetStringInPare(InitBuf+i2,buffer+i);
865 i+=i3-1;
866 i2+=i3-1;
867 continue;
868 }
869 if(buffer[i]=='['&&IsStr==0){
870 i3=GetStringInBracket(InitBuf+i2,buffer+i);
871 i+=i3-1;
872 i2+=i3-1;
873 continue;
874 }
875 if((buffer[i]==1&&buffer[i+1]==ESC_AS)||
876 (buffer[i]==','&&IsStr==0)||
877 buffer[i]=='\0'){
878 InitBuf[i2]=0;
879 break;
880 }
881 InitBuf[i2]=buffer[i];
882 }
883 }
884 else{
885 //初期化データなし
886 InitBuf[0]=0;
887 }
888
889 ConstractParameter[0]=0;
890 if(buffer[i]==1&&buffer[i+1]==ESC_AS){
891 /////////////////////////////
892 // "As ~" による型指定あり
893 /////////////////////////////
894
895 for(i+=2,i2=0;;i++,i2++){
896 if(buffer[i]==','||buffer[i]=='('||buffer[i]=='\0'){
897 temporary[i2]=0;
898 break;
899 }
900 temporary[i2]=buffer[i];
901 }
902 pTypeInfo->type=GetTypeFixed(temporary,&pTypeInfo->u.lpIndex);
903 if(pTypeInfo->type==-1){
904 SetError(3,temporary,cp);
905 return 0;
906 }
907
908 if(pTypeInfo->type==DEF_PTR_PROC){
909 if(temporary[0]=='*'&&
910 temporary[1]==1&&
911 (temporary[2]==ESC_FUNCTION||temporary[2]==ESC_SUB)){
912 if(buffer[i]!='('){
913 SetError(10,temporary,cp);
914 return 0;
915 }
916 i3=GetStringInPare(temporary+3,buffer+i);
917 i+=i3;
918 i2+=i3;
919
920 if(temporary[2]==ESC_FUNCTION&&buffer[i]==1&&buffer[i+1]==ESC_AS){
921 temporary[i2++]=buffer[i++];
922 temporary[i2++]=buffer[i++];
923 for(;;i++,i2++){
924 if(!IsVariableChar(buffer[i])){
925 temporary[i2]=0;
926 break;
927 }
928 temporary[i2]=buffer[i];
929 }
930 }
931 }
932 else{
933 //TypeDefをする前のベース型を取得
934 GetOriginalTypeName(temporary);
935 }
936
937 if(temporary[3]!='('){
938 SetError(10,temporary,cp);
939 return 0;
940 }
941
942 //関数ポインタ
943 pTypeInfo->u.lpIndex=AddProcPtrInfo(temporary+3,temporary[2]);
944 }
945
946 if(buffer[i]=='('){
947 //コンストラクタに渡すパラメータを取得
948 i2=GetStringInPare(ConstractParameter,buffer+i);
949 i+=i2;
950 RemoveStringPare(ConstractParameter);
951
952 if(pTypeInfo->type!=DEF_OBJECT){
953 SetError(112,variable,cp);
954 return 0;
955 }
956 }
957 }
958 else{
959 //As指定なし
960 pTypeInfo->type=GetTypeFromSimpleName(variable);
961
962 i2=lstrlen(variable)-1;
963 if(i2>=0){
964 if(!(variable[i2]=='#'||variable[i2]=='!'||variable[i2]=='%'||variable[i2]=='$'))
965 SetError(-103,variable,cp);
966 }
967 }
968
969 GetArrange(variable,VarName,SubScripts);
970 return 1;
971}
972
973BOOL GetNowStaticVarFullName(char *VarName,char *FullName){
974 extern SUBINFO *pCompilingSubInfo;
975 if(!pCompilingSubInfo) return 0;
976
977 //Static識別
978 lstrcpy(FullName,"Static%");
979
980 //クラス名
981 if(pobj_CompilingClass){
982 lstrcat(FullName,pobj_CompilingClass->name);
983 lstrcat(FullName,"%");
984 }
985
986 //関数名
987 lstrcat(FullName,pCompilingSubInfo->name);
988 lstrcat(FullName,"%");
989
990 //ID
991 char temp[255];
992 sprintf(temp,"%x",pCompilingSubInfo->id);
993 lstrcat(FullName,temp);
994 lstrcat(FullName,"%");
995
996 lstrcat(FullName,VarName);
997
998 return 1;
999}
1000
1001
1002void AddGlobalVariable(char *name,int *SubScripts,TYPEINFO *pTypeInfo,int TypeSize,char *InitBuf,char *ConstractParameter,DWORD dwFlag){
1003 /////////////////////////
1004 // グローバル変数を追加
1005 /////////////////////////
1006 extern VARIABLE *GlobalVar;
1007 extern int MaxGlobalVarNum;
1008 extern int AllInitGlobalVarSize;
1009 extern int AllGlobalVarSize;
1010
1011 int i2,i3,VarSize;
1012
1013 for(i2=0;i2<MaxGlobalVarNum;i2++){
1014 if(GlobalVar[i2].bLiving&&obj_LexScopes.GetNowLevel()==GlobalVar[i2].ScopeLevel){
1015 if(lstrcmp(GlobalVar[i2].name,name)==0){
1016 //2重定義のエラー
1017 SetError(15,name,cp);
1018 return;
1019 }
1020 }
1021 }
1022
1023 GlobalVar=(VARIABLE *)HeapReAlloc(hHeap,0,GlobalVar,(MaxGlobalVarNum+1)*sizeof(VARIABLE));
1024
1025 for(i2=1,i3=0;i3<255;i3++){
1026 //配列要素数
1027 GlobalVar[MaxGlobalVarNum].SubScripts[i3]=SubScripts[i3];
1028
1029 if(SubScripts[i3]==-1) break;
1030 i2*=SubScripts[i3]+1;
1031 }
1032 VarSize=TypeSize*i2;
1033 if(VarSize%PTR_SIZE) VarSize+=PTR_SIZE-(VarSize%PTR_SIZE);
1034
1035 //新しいオブジェクトポインタを取得
1036 VARIABLE *pVar = &GlobalVar[MaxGlobalVarNum];
1037
1038 //グローバル変数の個数を増加
1039 MaxGlobalVarNum++;
1040
1041 lstrcpy(pVar->name,name);
1042 if(dwFlag & DIMFLAG_CONST) pVar->bConst = true;
1043 else pVar->bConst = false;
1044 if(SubScripts[0]==-1) pVar->bArray=0;
1045 else pVar->bArray=1;
1046 pVar->type=pTypeInfo->type;
1047 pVar->u.index=pTypeInfo->u.lpIndex;
1048
1049 //コンストラクタ用パラメータ
1050 pVar->ConstractParameter=(char *)HeapAlloc(hHeap,0,lstrlen(ConstractParameter)+1);
1051 lstrcpy(pVar->ConstractParameter,ConstractParameter);
1052
1053 if(InitBuf[0]||dwFlag==DIMFLAG_INITDEBUGVAR){
1054 //初期バッファがあるとき
1055 pVar->offset=AllInitGlobalVarSize;
1056 AllInitGlobalVarSize+=VarSize;
1057 }
1058 else{
1059 //初期バッファがないとき
1060 pVar->offset=AllGlobalVarSize | 0x80000000;
1061 AllGlobalVarSize+=VarSize;
1062 }
1063
1064 //レキシカルスコープ
1065 pVar->ScopeLevel=obj_LexScopes.GetNowLevel();
1066 pVar->ScopeStartAddress=obj_LexScopes.GetStartAddress();
1067 pVar->bLiving=TRUE;
1068
1069 //エラー用
1070 pVar->source_code_address=cp;
1071
1072 //初期バッファにデータをセット
1073 extern BYTE *initGlobalBuf;
1074 initGlobalBuf=(BYTE *)HeapReAlloc(hHeap,
1075 HEAP_ZERO_MEMORY,
1076 initGlobalBuf,
1077 AllInitGlobalVarSize);
1078 if(InitBuf[0]){
1079 //初期代入時のみ、書き込みアクセスを許可する
1080 bool bConstBack = pVar->bConst;
1081 pVar->bConst = false;
1082
1083 int result = SetInitGlobalData(pVar->offset,
1084 pVar->type,
1085 pVar->u.index,
1086 pVar->SubScripts,
1087 InitBuf);
1088
1089 if(!result){
1090 //動的な式だった場合は代入演算を行う
1091 char temporary[8192];
1092 sprintf(temporary,"%s=%s",name,InitBuf);
1093 OpcodeCalc(temporary);
1094 }
1095
1096 pVar->bConst = bConstBack;
1097 }
1098
1099
1100
1101
1102 if(pTypeInfo->type==DEF_OBJECT){
1103 //利用対象のクラスコンストラクタ、デストラクタに使用チェックをつける
1104 for(i2=0;i2<pTypeInfo->u.pobj_Class->iMethodNum;i2++){
1105 if(lstrcmp(pTypeInfo->u.pobj_Class->ppobj_Method[i2]->psi->name,pTypeInfo->u.pobj_Class->name)==0){
1106 //コンストラクタ
1107 pTypeInfo->u.pobj_Class->ppobj_Method[i2]->psi->bUse=1;
1108 }
1109 if(pTypeInfo->u.pobj_Class->ppobj_Method[i2]->psi->name[0]=='~'){
1110 //デストラクタ
1111 pTypeInfo->u.pobj_Class->ppobj_Method[i2]->psi->bUse=1;
1112 }
1113 }
1114 }
1115}
Note: See TracBrowser for help on using the repository browser.