source: dev/BasicCompiler_Common/Variable.cpp@ 59

Last change on this file since 59 was 57, checked in by dai_9181, 18 years ago

バージョンをβ12に上げた。
Dimに初期値をしたとき、コピーコンストラクタが正常に呼び出せないバグを修正。

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