1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
|
{
$Id: pdecvar.pas,v 1.98 2005/03/09 23:44:18 peter Exp $
Copyright (c) 1998-2002 by Florian Klaempfl
Parses variable declarations. Used for var statement and record
definitions
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
****************************************************************************
}
unit pdecvar;
{$i fpcdefs.inc}
interface
uses
symsym,symdef;
function read_property_dec(aclass:tobjectdef):tpropertysym;
procedure read_var_decs(is_record,is_object,is_threadvar:boolean);
implementation
uses
{ common }
cutils,cclasses,
{ global }
globtype,globals,tokens,verbose,
systems,
{ symtable }
symconst,symbase,symtype,symtable,defutil,defcmp,
fmodule,
{ pass 1 }
node,pass_1,
nmat,nadd,ncal,nset,ncnv,ninl,ncon,nld,nflw,nmem,
{ codegen }
ncgutil,
{ parser }
scanner,
pbase,pexpr,ptype,ptconst,pdecsub,
{ link }
import
;
function read_property_dec(aclass:tobjectdef):tpropertysym;
{ convert a node tree to symlist and return the last
symbol }
function parse_symlist(pl:tsymlist;var def:tdef):boolean;
var
idx : longint;
sym : tsym;
srsymtable : tsymtable;
st : tsymtable;
p : tnode;
begin
result:=true;
def:=nil;
if token=_ID then
begin
if assigned(aclass) then
sym:=search_class_member(aclass,pattern)
else
searchsym(pattern,sym,srsymtable);
if assigned(sym) then
begin
case sym.typ of
fieldvarsym :
begin
pl.addsym(sl_load,sym);
def:=tfieldvarsym(sym).vartype.def;
end;
procsym :
begin
pl.addsym(sl_call,sym);
end;
else
begin
Message1(parser_e_illegal_field_or_method,orgpattern);
result:=false;
end;
end;
end
else
begin
Message1(parser_e_illegal_field_or_method,orgpattern);
result:=false;
end;
consume(_ID);
repeat
case token of
_ID,
_SEMICOLON :
begin
break;
end;
_POINT :
begin
consume(_POINT);
if assigned(def) then
begin
st:=def.getsymtable(gs_record);
if assigned(st) then
begin
sym:=searchsymonlyin(st,pattern);
if assigned(sym) then
begin
pl.addsym(sl_subscript,sym);
case sym.typ of
fieldvarsym :
def:=tfieldvarsym(sym).vartype.def;
else
begin
Message1(sym_e_illegal_field,orgpattern);
result:=false;
end;
end;
end
else
begin
Message1(sym_e_illegal_field,orgpattern);
result:=false;
end;
end
else
begin
Message(parser_e_invalid_qualifier);
result:=false;
end;
end
else
begin
Message(parser_e_invalid_qualifier);
result:=false;
end;
consume(_ID);
end;
_LECKKLAMMER :
begin
consume(_LECKKLAMMER);
repeat
if def.deftype=arraydef then
begin
idx:=0;
p:=comp_expr(true);
if (not codegenerror) then
begin
if (p.nodetype=ordconstn) then
begin
if compare_defs(p.resulttype.def,tarraydef(def).rangetype.def,nothingn)>=te_equal then
idx:=tordconstnode(p).value
else
IncompatibleTypes(p.resulttype.def,tarraydef(def).rangetype.def);
end
else
Message(type_e_ordinal_expr_expected)
end;
p.free;
pl.addconst(sl_vec,idx);
def:=tarraydef(def).elementtype.def;
end
else
begin
Message(parser_e_invalid_qualifier);
result:=false;
end;
until not try_to_consume(_COMMA);
consume(_RECKKLAMMER);
end;
else
begin
Message(parser_e_ill_property_access_sym);
result:=false;
break;
end;
end;
until false;
end
else
begin
Message(parser_e_ill_property_access_sym);
result:=false;
end;
end;
var
sym : tsym;
p : tpropertysym;
overriden : tsym;
varspez : tvarspez;
tt : ttype;
arraytype : ttype;
def : tdef;
pt : tnode;
propname : stringid;
sc : tsinglelist;
paranr : word;
oldregisterdef : boolean;
hreadparavs,
hparavs : tparavarsym;
readprocdef,
writeprocdef : tprocvardef;
oldsymtablestack : tsymtable;
begin
{ Generate temp procvardefs to search for matching read/write
procedures. the readprocdef will store all definitions }
oldregisterdef:=registerdef;
registerdef:=false;
paranr:=0;
readprocdef:=tprocvardef.create(normal_function_level);
writeprocdef:=tprocvardef.create(normal_function_level);
registerdef:=oldregisterdef;
{ make it method pointers }
if assigned(aclass) then
begin
include(readprocdef.procoptions,po_methodpointer);
include(writeprocdef.procoptions,po_methodpointer);
end;
if token<>_ID then
begin
consume(_ID);
consume(_SEMICOLON);
exit;
end;
{ Generate propertysym and insert in symtablestack }
p:=tpropertysym.create(orgpattern);
symtablestack.insert(p);
propname:=pattern;
consume(_ID);
{ Set the symtablestack to the parast of readprop so
temp defs will be destroyed after declaration }
readprocdef.parast.next:=symtablestack;
symtablestack:=readprocdef.parast;
{ property parameters ? }
if token=_LECKKLAMMER then
begin
if (sp_published in current_object_option) then
Message(parser_e_cant_publish_that_property);
{ create a list of the parameters }
sc:=tsinglelist.create;
consume(_LECKKLAMMER);
inc(testcurobject);
repeat
if token=_VAR then
begin
consume(_VAR);
varspez:=vs_var;
end
else if token=_CONST then
begin
consume(_CONST);
varspez:=vs_const;
end
else if (idtoken=_OUT) and (m_out in aktmodeswitches) then
begin
consume(_OUT);
varspez:=vs_out;
end
else
varspez:=vs_value;
sc.reset;
repeat
inc(paranr);
hreadparavs:=tparavarsym.create(orgpattern,10*paranr,varspez,generrortype,[]);
readprocdef.parast.insert(hreadparavs);
sc.insert(hreadparavs);
consume(_ID);
until not try_to_consume(_COMMA);
if try_to_consume(_COLON) then
begin
{ for records, don't search the recordsymtable for
the symbols of the types }
oldsymtablestack:=symtablestack;
symtablestack:=symtablestack.next;
if try_to_consume(_ARRAY) then
begin
consume(_OF);
{ define range and type of range }
tt.setdef(tarraydef.create(0,-1,s32inttype));
{ define field type }
single_type(arraytype,false);
tarraydef(tt.def).setelementtype(arraytype);
end
else
single_type(tt,false);
symtablestack:=oldsymtablestack;
end
else
tt:=cformaltype;
hreadparavs:=tparavarsym(sc.first);
while assigned(hreadparavs) do
begin
hreadparavs.vartype:=tt;
{ also update the writeprocdef }
hparavs:=tparavarsym.create(hreadparavs.realname,hreadparavs.paranr,vs_value,tt,[]);
writeprocdef.parast.insert(hparavs);
hreadparavs:=tparavarsym(hreadparavs.listnext);
end;
until not try_to_consume(_SEMICOLON);
sc.free;
dec(testcurobject);
consume(_RECKKLAMMER);
{ the parser need to know if a property has parameters, the
index parameter doesn't count (PFV) }
if paranr>0 then
include(p.propoptions,ppo_hasparameters);
end;
{ overriden property ? }
{ force property interface
there is a property parameter
a global property }
if (token=_COLON) or (paranr>0) or (aclass=nil) then
begin
consume(_COLON);
{ insert types in global symtable }
oldsymtablestack:=symtablestack;
while not(symtablestack.symtabletype in [globalsymtable,staticsymtable]) do
symtablestack:=symtablestack.next;
single_type(p.proptype,false);
symtablestack:=oldsymtablestack;
if (idtoken=_INDEX) then
begin
consume(_INDEX);
pt:=comp_expr(true);
if is_constnode(pt) and
is_ordinal(pt.resulttype.def)
{$ifndef cpu64bit}
and (not is_64bitint(pt.resulttype.def))
{$endif}
then
p.index:=tordconstnode(pt).value
else
begin
Message(parser_e_invalid_property_index_value);
p.index:=0;
end;
p.indextype.setdef(pt.resulttype.def);
include(p.propoptions,ppo_indexed);
{ concat a longint to the para templates }
inc(paranr);
hparavs:=tparavarsym.create('$index',10*paranr,vs_value,p.indextype,[]);
readprocdef.parast.insert(hparavs);
hparavs:=tparavarsym.create('$index',10*paranr,vs_value,p.indextype,[]);
writeprocdef.parast.insert(hparavs);
pt.free;
end;
end
else
begin
{ do an property override }
overriden:=search_class_member(aclass.childof,propname);
if assigned(overriden) and (overriden.typ=propertysym) then
begin
p.dooverride(tpropertysym(overriden));
end
else
begin
p.proptype:=generrortype;
message(parser_e_no_property_found_to_override);
end;
end;
if (sp_published in current_object_option) and
not(p.proptype.def.is_publishable) then
Message(parser_e_cant_publish_that_property);
if try_to_consume(_READ) then
begin
p.readaccess.clear;
if parse_symlist(p.readaccess,def) then
begin
sym:=p.readaccess.firstsym^.sym;
case sym.typ of
procsym :
begin
{ read is function returning the type of the property }
readprocdef.rettype:=p.proptype;
{ Insert hidden parameters }
handle_calling_convention(readprocdef);
{ search procdefs matching readprocdef }
{ we ignore hidden stuff here because the property access symbol might have
non default calling conventions which might change the hidden stuff;
see tw3216.pp (FK) }
p.readaccess.procdef:=Tprocsym(sym).search_procdef_bypara(readprocdef.paras,p.proptype.def,[cpo_allowdefaults,cpo_ignorehidden,cpo_allowconvert]);
if not assigned(p.readaccess.procdef) then
Message(parser_e_ill_property_access_sym);
end;
fieldvarsym :
begin
if not assigned(def) then
internalerror(200310071);
if compare_defs(def,p.proptype.def,nothingn)>=te_equal then
begin
{ property parameters are allowed if this is
an indexed property, because the index is then
the parameter.
Note: In the help of Kylix it is written
that it isn't allowed, but the compiler accepts it (PFV) }
if (ppo_hasparameters in p.propoptions) then
Message(parser_e_ill_property_access_sym);
end
else
IncompatibleTypes(def,p.proptype.def);
end;
else
Message(parser_e_ill_property_access_sym);
end;
end;
end;
if try_to_consume(_WRITE) then
begin
p.writeaccess.clear;
if parse_symlist(p.writeaccess,def) then
begin
sym:=p.writeaccess.firstsym^.sym;
case sym.typ of
procsym :
begin
{ write is a procedure with an extra value parameter
of the of the property }
writeprocdef.rettype:=voidtype;
inc(paranr);
hparavs:=tparavarsym.create('$value',10*paranr,vs_value,p.proptype,[]);
writeprocdef.parast.insert(hparavs);
{ Insert hidden parameters }
handle_calling_convention(writeprocdef);
{ search procdefs matching writeprocdef }
p.writeaccess.procdef:=Tprocsym(sym).search_procdef_bypara(writeprocdef.paras,writeprocdef.rettype.def,[cpo_allowdefaults,cpo_allowconvert]);
if not assigned(p.writeaccess.procdef) then
Message(parser_e_ill_property_access_sym);
end;
fieldvarsym :
begin
if not assigned(def) then
internalerror(200310072);
if compare_defs(def,p.proptype.def,nothingn)>=te_equal then
begin
{ property parameters are allowed if this is
an indexed property, because the index is then
the parameter.
Note: In the help of Kylix it is written
that it isn't allowed, but the compiler accepts it (PFV) }
if (ppo_hasparameters in p.propoptions) then
Message(parser_e_ill_property_access_sym);
end
else
IncompatibleTypes(def,p.proptype.def);
end;
else
Message(parser_e_ill_property_access_sym);
end;
end;
end;
if assigned(aclass) then
begin
include(p.propoptions,ppo_stored);
if try_to_consume(_STORED) then
begin
p.storedaccess.clear;
case token of
_ID:
begin
{ in the case that idtoken=_DEFAULT }
{ we have to do nothing except }
{ setting ppo_stored, it's the same }
{ as stored true }
if idtoken<>_DEFAULT then
begin
if parse_symlist(p.storedaccess,def) then
begin
sym:=p.storedaccess.firstsym^.sym;
case sym.typ of
procsym :
begin
p.storedaccess.procdef:=Tprocsym(sym).search_procdef_nopara_boolret;
if not assigned(p.storedaccess.procdef) then
message(parser_e_ill_property_storage_sym);
end;
fieldvarsym :
begin
if not assigned(def) then
internalerror(200310073);
if (ppo_hasparameters in p.propoptions) or
not(is_boolean(def)) then
Message(parser_e_stored_property_must_be_boolean);
end;
else
Message(parser_e_ill_property_access_sym);
end;
end;
end;
end;
_FALSE:
begin
consume(_FALSE);
exclude(p.propoptions,ppo_stored);
end;
_TRUE:
consume(_TRUE);
end;
end;
end;
if try_to_consume(_DEFAULT) then
begin
if not(is_ordinal(p.proptype.def) or
{$ifndef cpu64bit}
is_64bitint(p.proptype.def) or
{$endif cpu64bit}
is_class(p.proptype.def) or
is_single(p.proptype.def) or
(p.proptype.def.deftype in [classrefdef,pointerdef]) or
((p.proptype.def.deftype=setdef) and
(tsetdef(p.proptype.def).settype=smallset))) or
((p.proptype.def.deftype=arraydef) and
(ppo_indexed in p.propoptions)) or
(ppo_hasparameters in p.propoptions) then
begin
Message(parser_e_property_cant_have_a_default_value);
{ Error recovery }
pt:=comp_expr(true);
pt.free;
end
else
begin
{ Get the result of the default, the firstpass is
needed to support values like -1 }
pt:=comp_expr(true);
if (p.proptype.def.deftype=setdef) and
(pt.nodetype=arrayconstructorn) then
begin
arrayconstructor_to_set(pt);
do_resulttypepass(pt);
end;
inserttypeconv(pt,p.proptype);
if not(is_constnode(pt)) then
Message(parser_e_property_default_value_must_const);
{ Set default value }
case pt.nodetype of
setconstn :
p.default:=plongint(tsetconstnode(pt).value_set)^;
ordconstn :
p.default:=tordconstnode(pt).value;
niln :
p.default:=0;
realconstn:
p.default:=longint(single(trealconstnode(pt).value_real));
end;
pt.free;
end;
end
else if try_to_consume(_NODEFAULT) then
begin
p.default:=0;
end;
{ remove temporary procvardefs }
symtablestack:=symtablestack.next;
readprocdef.free;
writeprocdef.free;
result:=p;
end;
const
variantrecordlevel : longint = 0;
procedure read_var_decs(is_record,is_object,is_threadvar:boolean);
{ reads the filed of a record into a }
{ symtablestack, if record=false }
{ variants are forbidden, so this procedure }
{ can be used to read object fields }
{ if absolute is true, ABSOLUTE and file }
{ types are allowed }
{ => the procedure is also used to read }
{ a sequence of variable declaration }
procedure insert_syms(sc : tsinglelist;tt : ttype;is_threadvar : boolean; addsymopts : tsymoptions);
{ inserts the symbols of sc in st with def as definition or sym as ttypesym, sc is disposed }
var
vs : tabstractvarsym;
hstaticvs : tglobalvarsym;
begin
vs:=tabstractvarsym(sc.first);
while assigned(vs) do
begin
vs.vartype:=tt;
{ insert any additional hint directives }
vs.symoptions := vs.symoptions + addsymopts;
if (sp_static in current_object_option) then
include(vs.symoptions,sp_static);
if is_threadvar then
include(vs.varoptions,vo_is_thread_var);
{ static data fields are inserted in the globalsymtable }
if (symtablestack.symtabletype=objectsymtable) and
(sp_static in current_object_option) then
begin
hstaticvs:=tglobalvarsym.create('$'+lower(symtablestack.name^)+'_'+vs.name,vs_value,tt,[]);
symtablestack.defowner.owner.insert(hstaticvs);
insertbssdata(hstaticvs);
end
else
begin
{ external data is not possible here }
case symtablestack.symtabletype of
globalsymtable,
staticsymtable :
insertbssdata(tglobalvarsym(vs));
recordsymtable,
objectsymtable :
tabstractrecordsymtable(symtablestack).insertfield(tfieldvarsym(vs),false);
end;
end;
vs:=tabstractvarsym(vs.listnext);
end;
end;
procedure read_default_value(sc : tsinglelist;tt : ttype;is_threadvar : boolean);
var
vs : tabstractnormalvarsym;
tcsym : ttypedconstsym;
begin
vs:=tabstractnormalvarsym(sc.first);
if assigned(vs.listnext) then
Message(parser_e_initialized_only_one_var);
if is_threadvar then
Message(parser_e_initialized_not_for_threadvar);
if symtablestack.symtabletype=localsymtable then
begin
consume(_EQUAL);
tcsym:=ttypedconstsym.createtype('default'+vs.realname,tt,false);
vs.defaultconstsym:=tcsym;
symtablestack.insert(tcsym);
insertconstdata(tcsym);
readtypedconst(tt,tcsym,false);
{ The variable has a value assigned }
vs.varstate:=vs_assigned;
end
else
begin
tcsym:=ttypedconstsym.createtype(vs.realname,tt,true);
tcsym.fileinfo:=vs.fileinfo;
symtablestack.replace(vs,tcsym);
vs.free;
insertconstdata(tcsym);
consume(_EQUAL);
readtypedconst(tt,tcsym,true);
end;
end;
var
sc : tsinglelist;
old_block_type : tblock_type;
symdone : boolean;
{ to handle absolute }
abssym : tabsolutevarsym;
{ c var }
newtype : ttypesym;
is_dll,
hasdefaultvalue,
is_gpc_name,is_cdecl,
extern_var,export_var : boolean;
old_current_object_option : tsymoptions;
hs,sorg,C_name,dll_name : string;
tt,casetype : ttype;
{ maxsize contains the max. size of a variant }
{ startvarrec contains the start of the variant part of a record }
maxsize, startvarrecsize : longint;
usedalign,
maxalignment,startvarrecalign,
maxpadalign, startpadalign: shortint;
hp,pt : tnode;
fieldvs : tfieldvarsym;
vs,vs2 : tabstractvarsym;
srsym : tsym;
oldsymtablestack,
srsymtable : tsymtable;
unionsymtable : trecordsymtable;
offset : longint;
uniondef : trecorddef;
unionsym : tfieldvarsym;
uniontype : ttype;
dummysymoptions : tsymoptions;
semicolonatend,semicoloneaten: boolean;
{$ifdef powerpc}
tempdef: tdef;
is_first_field: boolean;
{$endif powerpc}
begin
{$ifdef powerpc}
is_first_field := true;
{$endif powerpc}
old_current_object_option:=current_object_option;
{ all variables are public if not in a object declaration }
if not is_object then
current_object_option:=[sp_public];
old_block_type:=block_type;
block_type:=bt_type;
is_gpc_name:=false;
{ Force an expected ID error message }
if not (token in [_ID,_CASE,_END]) then
consume(_ID);
{ read vars }
sc:=tsinglelist.create;
while (token=_ID) and
not(is_object and (idtoken in [_PUBLIC,_PRIVATE,_PUBLISHED,_PROTECTED])) do
begin
sorg:=orgpattern;
semicoloneaten:=false;
hasdefaultvalue:=false;
symdone:=false;
sc.reset;
repeat
case symtablestack.symtabletype of
localsymtable :
vs:=tlocalvarsym.create(orgpattern,vs_value,generrortype,[]);
staticsymtable,
globalsymtable :
vs:=tglobalvarsym.create(orgpattern,vs_value,generrortype,[]);
recordsymtable,
objectsymtable :
vs:=tfieldvarsym.create(orgpattern,vs_value,generrortype,[]);
else
internalerror(200411064);
end;
symtablestack.insert(vs);
if assigned(vs.owner) then
sc.insert(vs)
else
vs.free;
consume(_ID);
until not try_to_consume(_COMMA);
consume(_COLON);
if (m_gpc in aktmodeswitches) and
not(is_record or is_object or is_threadvar) and
(token=_ID) and (orgpattern='__asmname__') then
begin
consume(_ID);
C_name:=get_stringconst;
Is_gpc_name:=true;
end;
{ this is needed for Delphi mode at least
but should be OK for all modes !! (PM) }
ignore_equal:=true;
if is_record or is_object then
begin
{ for records, don't search the recordsymtable for
the symbols of the types }
oldsymtablestack:=symtablestack;
symtablestack:=symtablestack.next;
read_type(tt,'',false);
symtablestack:=oldsymtablestack;
end
else
read_type(tt,'',false);
ignore_equal:=false;
{ Process procvar directives }
if (tt.def.deftype=procvardef) and
(tt.def.typesym=nil) and
check_proc_directive(true) then
begin
newtype:=ttypesym.create('unnamed',tt);
parse_var_proc_directives(tsym(newtype));
semicoloneaten:=true;
newtype.restype.def:=nil;
tt.def.typesym:=nil;
newtype.free;
end;
{$ifdef powerpc}
{ from gcc/gcc/config/rs6000/rs6000.h:
/* APPLE LOCAL begin Macintosh alignment 2002-1-22 ff */
/* Return the alignment of a struct based on the Macintosh PowerPC
alignment rules. In general the alignment of a struct is
determined by the greatest alignment of its elements. However, the
PowerPC rules cause the alignment of a struct to peg at word
alignment except when the first field has greater than word
(32-bit) alignment, in which case the alignment is determined by
the alignment of the first field. */
}
if (target_info.system in [system_powerpc_darwin, system_powerpc_macos]) and
is_record and
is_first_field and
(trecordsymtable(symtablestack).usefieldalignment = -1) then
begin
tempdef := tt.def;
while tempdef.deftype = arraydef do
tempdef := tarraydef(tempdef).elementtype.def;
if tempdef.deftype <> recorddef then
maxpadalign := tempdef.alignment
else
maxpadalign := trecorddef(tempdef).padalignment;
if (maxpadalign > 4) and
(maxpadalign > trecordsymtable(symtablestack).padalignment) then
trecordsymtable(symtablestack).padalignment := maxpadalign;
is_first_field := false;
end;
{$endif powerpc}
{ types that use init/final are not allowed in variant parts, but
classes are allowed }
if (variantrecordlevel>0) and
(tt.def.needs_inittable and not is_class(tt.def)) then
Message(parser_e_cant_use_inittable_here);
if is_gpc_name then
begin
vs:=tabstractvarsym(sc.first);
if assigned(vs.listnext) then
Message(parser_e_absolute_only_one_var);
vs.vartype:=tt;
if vs.typ=globalvarsym then
begin
tglobalvarsym(vs).set_mangledname(target_info.Cprefix+sorg);
include(vs.varoptions,vo_is_C_var);
include(vs.varoptions,vo_is_external);
end
else
Message(parser_e_no_local_var_external);
symdone:=true;
end;
{ check for absolute }
if not symdone and
(idtoken=_ABSOLUTE) and not(is_record or is_object or is_threadvar) then
begin
consume(_ABSOLUTE);
abssym:=nil;
{ only allowed for one var }
vs:=tabstractvarsym(sc.first);
if assigned(vs.listnext) then
Message(parser_e_absolute_only_one_var);
{ parse the rest }
pt:=expr;
{ check allowed absolute types }
if (pt.nodetype=stringconstn) or
(is_constcharnode(pt)) then
begin
abssym:=tabsolutevarsym.create(vs.realname,tt);
abssym.fileinfo:=vs.fileinfo;
if pt.nodetype=stringconstn then
hs:=strpas(tstringconstnode(pt).value_str)
else
hs:=chr(tordconstnode(pt).value);
consume(token);
abssym.abstyp:=toasm;
abssym.asmname:=stringdup(hs);
{ replace the varsym }
symtablestack.replace(vs,abssym);
vs.free;
end
{ address }
else if is_constintnode(pt) and
((target_info.system in [system_i386_go32v2,system_i386_watcom,
system_i386_wdosx,system_i386_win32]) or
(m_objfpc in aktmodeswitches) or
(m_delphi in aktmodeswitches)) then
begin
abssym:=tabsolutevarsym.create(vs.realname,tt);
abssym.fileinfo:=vs.fileinfo;
abssym.abstyp:=toaddr;
abssym.addroffset:=tordconstnode(pt).value;
{$ifdef i386}
abssym.absseg:=false;
if (target_info.system in [system_i386_go32v2,system_i386_watcom]) and
try_to_consume(_COLON) then
begin
pt.free;
pt:=expr;
if is_constintnode(pt) then
begin
abssym.addroffset:=abssym.addroffset shl 4+tordconstnode(pt).value;
abssym.absseg:=true;
end
else
Message(type_e_ordinal_expr_expected);
end;
{$endif i386}
symtablestack.replace(vs,abssym);
vs.free;
end
{ variable }
else
begin
{ remove subscriptn before checking for loadn }
hp:=pt;
while (hp.nodetype in [subscriptn,typeconvn,vecn]) do
hp:=tunarynode(hp).left;
if (hp.nodetype=loadn) then
begin
{ we should check the result type of loadn }
if not (tloadnode(hp).symtableentry.typ in [fieldvarsym,globalvarsym,localvarsym,
paravarsym,typedconstsym]) then
Message(parser_e_absolute_only_to_var_or_const);
abssym:=tabsolutevarsym.create(vs.realname,tt);
abssym.fileinfo:=vs.fileinfo;
abssym.abstyp:=tovar;
abssym.ref:=node_to_symlist(pt);
symtablestack.replace(vs,abssym);
vs.free;
end
else
Message(parser_e_absolute_only_to_var_or_const);
end;
if assigned(abssym) then
begin
{ try to consume the hint directives with absolute symbols }
dummysymoptions:=[];
try_consume_hintdirective(dummysymoptions);
abssym.symoptions := abssym.symoptions + dummysymoptions;
end;
pt.free;
symdone:=true;
end;
{ Process procvar directives before = and ; }
if (tt.def.deftype=procvardef) and
(tt.def.typesym=nil) and
check_proc_directive(true) then
begin
newtype:=ttypesym.create('unnamed',tt);
parse_var_proc_directives(tsym(newtype));
newtype.restype.def:=nil;
tt.def.typesym:=nil;
newtype.free;
end;
{ try to parse the hint directives }
dummysymoptions:=[];
try_consume_hintdirective(dummysymoptions);
{ Records and objects can't have default values }
if is_record or is_object then
begin
{ for a record there doesn't need to be a ; before the END or ) }
if not(token in [_END,_RKLAMMER]) and
not(semicoloneaten) then
consume(_SEMICOLON);
end
else
{ Handling of Delphi typed const = initialized vars }
if (token=_EQUAL) and
not(m_tp7 in aktmodeswitches) and
(symtablestack.symtabletype<>parasymtable) then
begin
{ Add calling convention for procvar }
if (tt.def.deftype=procvardef) and
(tt.def.typesym=nil) then
handle_calling_convention(tprocvardef(tt.def));
read_default_value(sc,tt,is_threadvar);
consume(_SEMICOLON);
{ for locals we've created typedconstsym with a different name }
if symtablestack.symtabletype<>localsymtable then
symdone:=true;
hasdefaultvalue:=true;
end
else
begin
if not(semicoloneaten) then
consume(_SEMICOLON);
end;
{ Support calling convention for procvars after semicolon }
if not(hasdefaultvalue) and
(tt.def.deftype=procvardef) and
(tt.def.typesym=nil) then
begin
{ Parse procvar directives after ; }
if check_proc_directive(true) then
begin
newtype:=ttypesym.create('unnamed',tt);
parse_var_proc_directives(tsym(newtype));
newtype.restype.def:=nil;
tt.def.typesym:=nil;
newtype.free;
end;
{ Add calling convention for procvar }
handle_calling_convention(tprocvardef(tt.def));
{ Handling of Delphi typed const = initialized vars }
if (token=_EQUAL) and
not(is_record or is_object) and
not(m_tp7 in aktmodeswitches) and
(symtablestack.symtabletype<>parasymtable) then
begin
read_default_value(sc,tt,is_threadvar);
consume(_SEMICOLON);
symdone:=true;
hasdefaultvalue:=true;
end;
end;
{ Check for EXTERNAL etc directives or, in macpas, if cs_external_var is set}
if not symdone and not(is_record or is_object or is_threadvar) then
begin
if (
(token=_ID) and
(m_cvar_support in aktmodeswitches) and
(idtoken in [_EXPORT,_EXTERNAL,_PUBLIC,_CVAR])
) or
(
(m_mac in aktmodeswitches) and
((cs_external_var in aktlocalswitches) or (cs_externally_visible in aktlocalswitches))
) then
begin
{ only allowed for one var }
vs:=tabstractvarsym(sc.first);
if assigned(vs.listnext) then
Message(parser_e_absolute_only_one_var);
{ set type of the var }
vs.vartype:=tt;
vs.symoptions := vs.symoptions + dummysymoptions;
{ defaults }
is_dll:=false;
is_cdecl:=false;
extern_var:=false;
export_var:=false;
C_name:=sorg;
semicolonatend:= false;
{ cdecl }
if idtoken=_CVAR then
begin
consume(_CVAR);
consume(_SEMICOLON);
is_cdecl:=true;
C_name:=target_info.Cprefix+sorg;
end;
{ external }
if idtoken=_EXTERNAL then
begin
consume(_EXTERNAL);
extern_var:=true;
semicolonatend:= true;
end;
{ macpas specific handling due to some switches}
if (m_mac in aktmodeswitches) then
begin
if (cs_external_var in aktlocalswitches) then
begin {The effect of this is the same as if cvar; external; has been given as directives.}
is_cdecl:=true;
C_name:=target_info.Cprefix+sorg;
extern_var:=true;
end
else if (cs_externally_visible in aktlocalswitches) then
begin {The effect of this is the same as if cvar has been given as directives.}
is_cdecl:=true;
C_name:=target_info.Cprefix+sorg;
end;
vs.varregable := vr_none;
end;
{ export }
if idtoken in [_EXPORT,_PUBLIC] then
begin
consume(_ID);
if extern_var then
Message(parser_e_not_external_and_export)
else
begin
export_var:=true;
semicolonatend:= true;
end;
end;
{ external and export need a name after when no cdecl is used }
if not is_cdecl then
begin
{ dll name ? }
if (extern_var) and (idtoken<>_NAME) then
begin
is_dll:=true;
dll_name:=get_stringconst;
end;
if try_to_consume(_NAME) then
C_name:=get_stringconst
else
C_name:=sorg;
end;
{ consume the ; when export or external is used }
if semicolonatend then
consume(_SEMICOLON);
{ set some vars options }
if is_dll then
include(vs.varoptions,vo_is_dll_var)
else
include(vs.varoptions,vo_is_C_var);
if (is_dll) and
(target_info.system = system_powerpc_darwin) then
C_Name := target_info.Cprefix+C_Name;
if export_var then
begin
inc(vs.refs);
include(vs.varoptions,vo_is_exported);
end;
if extern_var then
include(vs.varoptions,vo_is_external);
if vs.typ=globalvarsym then
begin
tglobalvarsym(vs).set_mangledname(C_Name);
{ insert in the datasegment when it is not external }
if (not extern_var) then
insertbssdata(tglobalvarsym(vs));
{ now we can insert it in the import lib if its a dll, or
add it to the externals }
if extern_var then
begin
vs.varregable := vr_none;
if is_dll then
begin
if not(current_module.uses_imports) then
begin
current_module.uses_imports:=true;
importlib.preparelib(current_module.realmodulename^);
end;
importlib.importvariable(tglobalvarsym(vs),C_name,dll_name);
end
else
if target_info.DllScanSupported then
current_module.Externals.insert(tExternalsItem.create(vs.mangledname));
end;
end
else
Message(parser_e_no_local_var_external);
symdone:=true;
end;
end;
{ Check for STATIC directive }
if not symdone and (is_object) and
(cs_static_keyword in aktmoduleswitches) and (idtoken=_STATIC) then
begin
include(current_object_option,sp_static);
insert_syms(sc,tt,false,dummysymoptions);
exclude(current_object_option,sp_static);
consume(_STATIC);
consume(_SEMICOLON);
symdone:=true;
end;
{ insert it in the symtable, if not done yet }
if not symdone then
begin
{ save object option, because we can turn of the sp_published }
if (sp_published in current_object_option) and
not(is_class(tt.def)) then
begin
Message(parser_e_cant_publish_that);
exclude(current_object_option,sp_published);
{ recover by changing access type to public }
vs2:=tabstractvarsym(sc.first);
while assigned (vs2) do
begin
exclude(vs2.symoptions,sp_published);
include(vs2.symoptions,sp_public);
vs2:=tabstractvarsym(vs2.listnext);
end;
end
else
if (sp_published in current_object_option) and
not(oo_can_have_published in tobjectdef(tt.def).objectoptions) then
begin
Message(parser_e_only_publishable_classes_can__be_published);
exclude(current_object_option,sp_published);
end;
insert_syms(sc,tt,is_threadvar,dummysymoptions);
current_object_option:=old_current_object_option;
end;
end;
{ Check for Case }
if is_record and (token=_CASE) then
begin
maxsize:=0;
maxalignment:=0;
maxpadalign:=0;
consume(_CASE);
sorg:=orgpattern;
hs:=pattern;
searchsym(hs,srsym,srsymtable);
{ may be only a type: }
if assigned(srsym) and (srsym.typ in [typesym,unitsym]) then
begin
{ for records, don't search the recordsymtable for
the symbols of the types }
oldsymtablestack:=symtablestack;
symtablestack:=symtablestack.next;
read_type(casetype,'',true);
symtablestack:=oldsymtablestack;
end
else
begin
consume(_ID);
consume(_COLON);
{ for records, don't search the recordsymtable for
the symbols of the types }
oldsymtablestack:=symtablestack;
symtablestack:=symtablestack.next;
read_type(casetype,'',true);
symtablestack:=oldsymtablestack;
fieldvs:=tfieldvarsym.create(sorg,vs_value,casetype,[]);
tabstractrecordsymtable(symtablestack).insertfield(fieldvs,true);
end;
if not(is_ordinal(casetype.def))
{$ifndef cpu64bit}
or is_64bitint(casetype.def)
{$endif cpu64bit}
then
Message(type_e_ordinal_expr_expected);
consume(_OF);
UnionSymtable:=trecordsymtable.create(aktpackrecords);
Unionsymtable.next:=symtablestack;
registerdef:=false;
UnionDef:=trecorddef.create(unionsymtable);
uniondef.isunion:=true;
if assigned(symtablestack.defowner) then
Uniondef.owner:=symtablestack.defowner.owner;
registerdef:=true;
startvarrecsize:=UnionSymtable.datasize;
startvarrecalign:=UnionSymtable.fieldalignment;
startpadalign:=Unionsymtable.padalignment;
symtablestack:=UnionSymtable;
repeat
repeat
pt:=comp_expr(true);
if not(pt.nodetype=ordconstn) then
Message(parser_e_illegal_expression);
pt.free;
if token=_COMMA then
consume(_COMMA)
else
break;
until false;
consume(_COLON);
{ read the vars }
consume(_LKLAMMER);
inc(variantrecordlevel);
if token<>_RKLAMMER then
read_var_decs(true,false,false);
dec(variantrecordlevel);
consume(_RKLAMMER);
{ calculates maximal variant size }
maxsize:=max(maxsize,unionsymtable.datasize);
maxalignment:=max(maxalignment,unionsymtable.fieldalignment);
maxpadalign:=max(maxpadalign,unionsymtable.padalignment);
{ the items of the next variant are overlayed }
unionsymtable.datasize:=startvarrecsize;
unionsymtable.fieldalignment:=startvarrecalign;
unionsymtable.padalignment:=startpadalign;
if (token<>_END) and (token<>_RKLAMMER) then
consume(_SEMICOLON)
else
break;
until (token=_END) or (token=_RKLAMMER);
{ at last set the record size to that of the biggest variant }
unionsymtable.datasize:=maxsize;
unionsymtable.fieldalignment:=maxalignment;
uniontype.def:=uniondef;
uniontype.sym:=nil;
UnionSym:=tfieldvarsym.create('$case',vs_value,uniontype,[]);
symtablestack:=symtablestack.next;
unionsymtable.addalignmentpadding;
{$ifdef powerpc}
{ parent inherits the alignment padding if the variant is the first "field" of the parent record/variant }
if (target_info.system in [system_powerpc_darwin, system_powerpc_macos]) and
is_first_field and
(trecordsymtable(symtablestack).usefieldalignment = -1) and
(maxpadalign > trecordsymtable(symtablestack).padalignment) then
trecordsymtable(symtablestack).padalignment:=maxpadalign;
{$endif powerpc}
{ Align the offset where the union symtable is added }
if (trecordsymtable(symtablestack).usefieldalignment=-1) then
usedalign:=used_align(unionsymtable.recordalignment,aktalignment.recordalignmin,aktalignment.maxCrecordalign)
else
usedalign:=used_align(unionsymtable.recordalignment,aktalignment.recordalignmin,aktalignment.recordalignmax);
offset:=align(trecordsymtable(symtablestack).datasize,usedalign);
trecordsymtable(symtablestack).datasize:=offset+unionsymtable.datasize;
if unionsymtable.recordalignment>trecordsymtable(symtablestack).fieldalignment then
trecordsymtable(symtablestack).fieldalignment:=unionsymtable.recordalignment;
trecordsymtable(symtablestack).insertunionst(Unionsymtable,offset);
Unionsym.owner:=nil;
unionsym.free;
uniondef.owner:=nil;
uniondef.free;
end;
block_type:=old_block_type;
current_object_option:=old_current_object_option;
{ free the list }
sc.free;
{$ifdef powerpc}
is_first_field := false;
{$endif powerpc}
end;
end.
{
$Log: pdecvar.pas,v $
Revision 1.98 2005/03/09 23:44:18 peter
support external <dllname>; also for variables
Revision 1.97 2005/02/14 17:13:07 peter
* truncate log
Revision 1.96 2005/02/03 17:11:40 peter
* more procvar directive fixes
Revision 1.95 2005/02/02 19:03:27 florian
* fixed proc. vars with calling specifiers in usual var declarations
Revision 1.94 2005/02/01 23:18:54 florian
* fixed:
r1 = record
p : procedure stdcall;
i : longint;
end;
Revision 1.93 2005/02/01 08:46:13 michael
* Patch from peter: fix macpas anonymous function procvar
Revision 1.92 2005/01/30 17:17:19 florian
* variables exported by $J/$Z in macpas mode are never regable
Revision 1.91 2005/01/06 13:30:41 florian
* widechararray patch from Peter
Revision 1.90 2005/01/04 16:52:07 peter
* don't typecast index of indexed properties
Revision 1.89 2005/01/04 16:37:38 peter
* give error when property doesn't reference a method or field
}
|