-
Notifications
You must be signed in to change notification settings - Fork 173
/
Copy pathncal.pas
3398 lines (3076 loc) · 136 KB
/
ncal.pas
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
{
This file implements the node for sub procedure calling.
Copyright (c) 1998-2002 by Florian Klaempfl
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 ncal;
{$i fpcdefs.inc}
interface
uses
cutils,cclasses,
globtype,constexp,
paramgr,parabase,
node,nbas,nutils,
{$ifdef state_tracking}
nstate,
{$endif state_tracking}
symbase,symtype,symsym,symdef,symtable;
type
tcallnodeflag = (
cnf_typedefset,
cnf_return_value_used,
cnf_do_inline,
cnf_inherited,
cnf_anon_inherited,
cnf_new_call,
cnf_dispose_call,
cnf_member_call, { called with implicit methodpointer tree }
cnf_uses_varargs, { varargs are used in the declaration }
cnf_create_failed { exception thrown in constructor -> don't call beforedestruction }
);
tcallnodeflags = set of tcallnodeflag;
tcallparanode = class;
tcallnode = class(tbinarynode)
private
{ number of parameters passed from the source, this does not include the hidden parameters }
paralength : smallint;
function is_simple_para_load(p:tnode; may_be_in_reg: boolean):boolean;
procedure maybe_load_in_temp(var p:tnode);
function gen_high_tree(var p:tnode;paradef:tdef):tnode;
function gen_self_tree_methodpointer:tnode;
function gen_self_tree:tnode;
function gen_vmt_tree:tnode;
procedure gen_hidden_parameters;
function funcret_can_be_reused:boolean;
procedure maybe_create_funcret_node;
procedure bind_parasym;
procedure add_init_statement(n:tnode);
procedure add_done_statement(n:tnode);
procedure convert_carg_array_of_const;
procedure order_parameters;
procedure check_inlining;
function pass1_normal:tnode;
procedure register_created_object_types;
private
{ inlining support }
inlinelocals : TFPObjectList;
inlineinitstatement,
inlinecleanupstatement : tstatementnode;
procedure createinlineparas;
function replaceparaload(var n: tnode; arg: pointer): foreachnoderesult;
procedure createlocaltemps(p:TObject;arg:pointer);
function optimize_funcret_assignment(inlineblock: tblocknode): tnode;
function pass1_inline:tnode;
protected
pushedparasize : longint;
public
{ the symbol containing the definition of the procedure }
{ to call }
symtableprocentry : tprocsym;
symtableprocentryderef : tderef;
{ symtable where the entry was found, needed for with support }
symtableproc : TSymtable;
{ the definition of the procedure to call }
procdefinition : tabstractprocdef;
procdefinitionderef : tderef;
{ tree that contains the pointer to the object for this method }
methodpointer : tnode;
{ initialize/finalization of temps }
callinitblock,
callcleanupblock : tblocknode;
{ function return node for initialized types or supplied return variable.
When the result is passed in a parameter then it is set to nil }
funcretnode : tnode;
{ varargs parasyms }
varargsparas : tvarargsparalist;
{ separately specified resultdef for some compilerprocs (e.g. }
{ you can't have a function with an "array of char" resultdef }
{ the RTL) (JM) }
typedef: tdef;
callnodeflags : tcallnodeflags;
{ only the processor specific nodes need to override this }
{ constructor }
constructor create(l:tnode; v : tprocsym;st : TSymtable; mp: tnode; callflags:tcallnodeflags);virtual;
constructor create_procvar(l,r:tnode);
constructor createintern(const name: string; params: tnode);
constructor createinternres(const name: string; params: tnode; res:tdef);
constructor createinternreturn(const name: string; params: tnode; returnnode : tnode);
destructor destroy;override;
constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
procedure ppuwrite(ppufile:tcompilerppufile);override;
procedure buildderefimpl;override;
procedure derefimpl;override;
function dogetcopy : tnode;override;
{ Goes through all symbols in a class and subclasses and calls
verify abstract for each .
}
procedure verifyabstractcalls;
{ called for each definition in a class and verifies if a method
is abstract or not, if it is abstract, give out a warning
}
procedure verifyabstract(sym:TObject;arg:pointer);
procedure insertintolist(l : tnodelist);override;
function pass_1 : tnode;override;
function pass_typecheck:tnode;override;
{$ifdef state_tracking}
function track_state_pass(exec_known:boolean):boolean;override;
{$endif state_tracking}
function docompare(p: tnode): boolean; override;
procedure printnodedata(var t:text);override;
function para_count:longint;
function required_para_count:longint;
{ checks if there are any parameters which end up at the stack, i.e.
which have LOC_REFERENCE and set pi_has_stackparameter if this applies }
procedure check_stack_parameters;
property parameters : tnode read left write left;
private
AbstractMethodsList : TFPHashList;
end;
tcallnodeclass = class of tcallnode;
tcallparaflag = (
cpf_is_colon_para,
cpf_varargs_para { belongs this para to varargs }
);
tcallparaflags = set of tcallparaflag;
tcallparanode = class(ttertiarynode)
public
callparaflags : tcallparaflags;
parasym : tparavarsym;
{ only the processor specific nodes need to override this }
{ constructor }
constructor create(expr,next : tnode);virtual;
destructor destroy;override;
constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
procedure ppuwrite(ppufile:tcompilerppufile);override;
function dogetcopy : tnode;override;
procedure insertintolist(l : tnodelist);override;
function pass_typecheck : tnode;override;
function pass_1 : tnode;override;
procedure get_paratype;
procedure firstcallparan;
procedure insert_typeconv;
procedure secondcallparan;virtual;abstract;
function docompare(p: tnode): boolean; override;
procedure printnodetree(var t:text);override;
{ returns whether a parameter contains a type conversion from }
{ a refcounted into a non-refcounted type }
function can_be_inlined: boolean;
property nextpara : tnode read right write right;
property parametername : tnode read third write third;
end;
tcallparanodeclass = class of tcallparanode;
function reverseparameters(p: tcallparanode): tcallparanode;
function translate_disp_call(selfnode,parametersnode,putvalue : tnode;methodname : ansistring = '';dispid : longint = 0;useresult : boolean = false) : tnode;
var
ccallnode : tcallnodeclass;
ccallparanode : tcallparanodeclass;
{ Current callnode, this is needed for having a link
between the callparanodes and the callnode they belong to }
aktcallnode : tcallnode;
implementation
uses
systems,
verbose,globals,
symconst,defutil,defcmp,
htypechk,pass_1,
ncnv,nld,ninl,nadd,ncon,nmem,nset,
procinfo,cpuinfo,
cgbase,
wpobase
;
type
tobjectinfoitem = class(tlinkedlistitem)
objinfo : tobjectdef;
constructor create(def : tobjectdef);
end;
{****************************************************************************
HELPERS
****************************************************************************}
function reverseparameters(p: tcallparanode): tcallparanode;
var
hp1, hp2: tcallparanode;
begin
hp1:=nil;
while assigned(p) do
begin
{ pull out }
hp2:=p;
p:=tcallparanode(p.right);
{ pull in }
hp2.right:=hp1;
hp1:=hp2;
end;
reverseparameters:=hp1;
end;
function translate_disp_call(selfnode,parametersnode,putvalue : tnode;methodname : ansistring = '';dispid : longint = 0;useresult : boolean = false) : tnode;
const
DISPATCH_METHOD = $1;
DISPATCH_PROPERTYGET = $2;
DISPATCH_PROPERTYPUT = $4;
DISPATCH_PROPERTYPUTREF = $8;
DISPATCH_CONSTRUCT = $4000;
var
statements : tstatementnode;
result_data,
params : ttempcreatenode;
paramssize : cardinal;
calldescnode : tdataconstnode;
resultvalue : tnode;
para : tcallparanode;
currargpos,
namedparacount,
paracount : longint;
assignmenttype,
vardatadef,
pvardatadef : tdef;
dispatchbyref : boolean;
calldesc : packed record
calltype,argcount,namedargcount : byte;
{ size of argtypes is unknown at compile time
so this is basically a dummy }
argtypes : array[0..255] of byte;
{ argtypes is followed by method name
names of named parameters, each being
a zero terminated string
}
end;
names : ansistring;
dispintfinvoke,
variantdispatch : boolean;
procedure increase_paramssize;
begin
{ for now we pass everything by reference
case para.left.resultdef.typ of
variantdef:
inc(paramssize,para.left.resultdef.size);
else
}
inc(paramssize,sizeof(voidpointertype.size));
{
end;
}
end;
begin
variantdispatch:=selfnode.resultdef.typ=variantdef;
dispintfinvoke:=not(variantdispatch);
result:=internalstatements(statements);
fillchar(calldesc,sizeof(calldesc),0);
if useresult then
begin
{ get temp for the result }
result_data:=ctempcreatenode.create(colevarianttype,colevarianttype.size,tt_persistent,true);
addstatement(statements,result_data);
end;
{ first, count and check parameters }
para:=tcallparanode(parametersnode);
paracount:=0;
namedparacount:=0;
paramssize:=0;
while assigned(para) do
begin
inc(paracount);
typecheckpass(para.left);
{ insert some extra casts }
if is_constintnode(para.left) and not(is_64bitint(para.left.resultdef)) then
begin
para.left:=ctypeconvnode.create_internal(para.left,s32inttype);
typecheckpass(para.left);
end
else if para.left.nodetype=stringconstn then
begin
para.left:=ctypeconvnode.create_internal(para.left,cwidestringtype);
typecheckpass(para.left);
end
{ force automatable boolean type }
else if is_boolean(para.left.resultdef) then
begin
para.left:=ctypeconvnode.create_internal(para.left,bool16type);
typecheckpass(para.left);
end
{ force automatable float type }
else if is_extended(para.left.resultdef)
and (current_settings.fputype<>fpu_none) then
begin
para.left:=ctypeconvnode.create_internal(para.left,s64floattype);
typecheckpass(para.left);
end;
if assigned(para.parametername) then
begin
typecheckpass(para.left);
inc(namedparacount);
end;
if para.left.nodetype<>nothingn then
if not is_automatable(para.left.resultdef) then
CGMessagePos1(para.left.fileinfo,type_e_not_automatable,para.left.resultdef.typename);
{ we've to know the parameter size to allocate the temp. space }
increase_paramssize;
para:=tcallparanode(para.nextpara);
end;
if assigned(putvalue) then
calldesc.calltype:=DISPATCH_PROPERTYPUT
else
calldesc.calltype:=DISPATCH_METHOD;
calldesc.argcount:=paracount;
{ allocate space }
params:=ctempcreatenode.create(voidtype,paramssize,tt_persistent,true);
addstatement(statements,params);
calldescnode:=cdataconstnode.create;
if dispintfinvoke then
calldescnode.append(dispid,sizeof(dispid));
{ build up parameters and description }
para:=tcallparanode(parametersnode);
currargpos:=0;
paramssize:=0;
names := '';
while assigned(para) do
begin
if assigned(para.parametername) then
begin
if para.parametername.nodetype=stringconstn then
names:=names+tstringconstnode(para.parametername).value_str+#0
else
internalerror(200611041);
end;
dispatchbyref:=para.left.resultdef.typ in [variantdef];
{ assign the argument/parameter to the temporary location }
if para.left.nodetype<>nothingn then
if dispatchbyref then
addstatement(statements,cassignmentnode.create(
ctypeconvnode.create_internal(cderefnode.create(caddnode.create(addn,
caddrnode.create(ctemprefnode.create(params)),
cordconstnode.create(qword(paramssize),ptruinttype,false)
)),voidpointertype),
ctypeconvnode.create_internal(caddrnode.create_internal(para.left),voidpointertype)))
else
begin
case para.left.resultdef.size of
1..4:
assignmenttype:=u32inttype;
8:
assignmenttype:=u64inttype;
else
internalerror(2007042801);
end;
addstatement(statements,cassignmentnode.create(
ctypeconvnode.create_internal(cderefnode.create(caddnode.create(addn,
caddrnode.create(ctemprefnode.create(params)),
cordconstnode.create(paramssize,ptruinttype,false)
)),assignmenttype),
ctypeconvnode.create_internal(para.left,assignmenttype)));
end;
if is_ansistring(para.left.resultdef) then
calldesc.argtypes[currargpos]:=varStrArg
else
calldesc.argtypes[currargpos]:=para.left.resultdef.getvardef;
if dispatchbyref then
calldesc.argtypes[currargpos]:=calldesc.argtypes[currargpos] or $80;
increase_paramssize;
para.left:=nil;
inc(currargpos);
para:=tcallparanode(para.nextpara);
end;
{ old argument list skeleton isn't needed anymore }
parametersnode.free;
calldescnode.append(calldesc,3+calldesc.argcount);
pvardatadef:=tpointerdef(search_system_type('PVARDATA').typedef);
if useresult then
resultvalue:=caddrnode.create(ctemprefnode.create(result_data))
else
resultvalue:=cpointerconstnode.create(0,voidpointertype);
if variantdispatch then
begin
methodname:=methodname+#0;
calldescnode.append(pointer(methodname)^,length(methodname));
calldescnode.append(pointer(names)^,length(names));
{ actual call }
vardatadef:=trecorddef(search_system_type('TVARDATA').typedef);
addstatement(statements,ccallnode.createintern('fpc_dispinvoke_variant',
{ parameters are passed always reverted, i.e. the last comes first }
ccallparanode.create(caddrnode.create(ctemprefnode.create(params)),
ccallparanode.create(caddrnode.create(calldescnode),
ccallparanode.create(ctypeconvnode.create_internal(selfnode,vardatadef),
ccallparanode.create(ctypeconvnode.create_internal(resultvalue,pvardatadef),nil)))))
);
end
else
begin
addstatement(statements,ccallnode.createintern('fpc_dispatch_by_id',
{ parameters are passed always reverted, i.e. the last comes first }
ccallparanode.create(caddrnode.create(ctemprefnode.create(params)),
ccallparanode.create(caddrnode.create(calldescnode),
ccallparanode.create(ctypeconvnode.create_internal(selfnode,voidpointertype),
ccallparanode.create(ctypeconvnode.create_internal(resultvalue,pvardatadef),nil)))))
);
end;
if useresult then
begin
{ clean up }
addstatement(statements,ctempdeletenode.create_normal_temp(result_data));
addstatement(statements,ctemprefnode.create(result_data));
end;
end;
{****************************************************************************
TOBJECTINFOITEM
****************************************************************************}
constructor tobjectinfoitem.create(def : tobjectdef);
begin
inherited create;
objinfo := def;
end;
{****************************************************************************
TCALLPARANODE
****************************************************************************}
constructor tcallparanode.create(expr,next : tnode);
begin
inherited create(callparan,expr,next,nil);
if not assigned(expr) then
internalerror(200305091);
expr.fileinfo:=fileinfo;
callparaflags:=[];
if expr.nodetype = typeconvn then
ttypeconvnode(expr).warn_pointer_to_signed:=false;
end;
destructor tcallparanode.destroy;
begin
inherited destroy;
end;
constructor tcallparanode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
begin
inherited ppuload(t,ppufile);
ppufile.getsmallset(callparaflags);
end;
procedure tcallparanode.ppuwrite(ppufile:tcompilerppufile);
begin
inherited ppuwrite(ppufile);
ppufile.putsmallset(callparaflags);
end;
function tcallparanode.dogetcopy : tnode;
var
n : tcallparanode;
begin
n:=tcallparanode(inherited dogetcopy);
n.callparaflags:=callparaflags;
n.parasym:=parasym;
result:=n;
end;
procedure tcallparanode.insertintolist(l : tnodelist);
begin
end;
function tcallparanode.pass_typecheck : tnode;
begin
{ need to use get_paratype }
internalerror(200709251);
result:=nil;
end;
function tcallparanode.pass_1 : tnode;
begin
{ need to use firstcallparan }
internalerror(200709252);
result:=nil;
end;
procedure tcallparanode.get_paratype;
var
old_array_constructor : boolean;
begin
if assigned(right) then
tcallparanode(right).get_paratype;
old_array_constructor:=allow_array_constructor;
allow_array_constructor:=true;
typecheckpass(left);
allow_array_constructor:=old_array_constructor;
if codegenerror then
resultdef:=generrordef
else
resultdef:=left.resultdef;
end;
procedure tcallparanode.firstcallparan;
begin
if assigned(right) then
tcallparanode(right).firstcallparan;
if not assigned(left.resultdef) then
get_paratype;
firstpass(left);
expectloc:=left.expectloc;
end;
procedure tcallparanode.insert_typeconv;
var
olddef : tdef;
hp : tnode;
block : tblocknode;
statements : tstatementnode;
temp : ttempcreatenode;
begin
{ Be sure to have the resultdef }
if not assigned(left.resultdef) then
typecheckpass(left);
if (left.nodetype<>nothingn) then
begin
{ Convert tp procvars, this is needs to be done
here to make the change permanent. in the overload
choosing the changes are only made temporary }
if (left.resultdef.typ=procvardef) and
not(parasym.vardef.typ in [procvardef,formaldef]) then
begin
if maybe_call_procvar(left,true) then
resultdef:=left.resultdef;
end;
{ Remove implicitly inserted typecast to pointer for
@procvar in macpas }
if (m_mac_procvar in current_settings.modeswitches) and
(parasym.vardef.typ=procvardef) and
(left.nodetype=typeconvn) and
is_voidpointer(left.resultdef) and
(ttypeconvnode(left).left.nodetype=typeconvn) and
(ttypeconvnode(ttypeconvnode(left).left).convtype=tc_proc_2_procvar) then
begin
hp:=left;
left:=ttypeconvnode(left).left;
ttypeconvnode(hp).left:=nil;
hp.free;
end;
{ Handle varargs and hidden paras directly, no typeconvs or }
{ pass_typechecking needed }
if (cpf_varargs_para in callparaflags) then
begin
{ this should only happen vor C varargs }
{ the necessary conversions have already been performed in }
{ tarrayconstructornode.insert_typeconvs }
set_varstate(left,vs_read,[vsf_must_be_valid]);
insert_varargstypeconv(left,true);
resultdef:=left.resultdef;
{ also update parasym type to get the correct parameter location
for the new types }
parasym.vardef:=left.resultdef;
end
else
if (vo_is_hidden_para in parasym.varoptions) then
begin
set_varstate(left,vs_read,[vsf_must_be_valid]);
resultdef:=left.resultdef;
end
else
begin
{ Do we need arrayconstructor -> set conversion, then insert
it here before the arrayconstructor node breaks the tree
with its conversions of enum->ord }
if (left.nodetype=arrayconstructorn) and
(parasym.vardef.typ=setdef) then
inserttypeconv(left,parasym.vardef);
{ set some settings needed for arrayconstructor }
if is_array_constructor(left.resultdef) then
begin
if left.nodetype<>arrayconstructorn then
internalerror(200504041);
if is_array_of_const(parasym.vardef) then
begin
{ force variant array }
include(left.flags,nf_forcevaria);
end
else
begin
include(left.flags,nf_novariaallowed);
{ now that the resultting type is know we can insert the required
typeconvs for the array constructor }
if parasym.vardef.typ=arraydef then
tarrayconstructornode(left).force_type(tarraydef(parasym.vardef).elementdef);
end;
end;
{ check if local proc/func is assigned to procvar }
if left.resultdef.typ=procvardef then
test_local_to_procvar(tprocvardef(left.resultdef),parasym.vardef);
{ test conversions }
if not(is_shortstring(left.resultdef) and
is_shortstring(parasym.vardef)) and
(parasym.vardef.typ<>formaldef) then
begin
{ Process open parameters }
if paramanager.push_high_param(parasym.varspez,parasym.vardef,aktcallnode.procdefinition.proccalloption) then
begin
{ insert type conv but hold the ranges of the array }
olddef:=left.resultdef;
inserttypeconv(left,parasym.vardef);
left.resultdef:=olddef;
end
else
begin
check_ranges(left.fileinfo,left,parasym.vardef);
inserttypeconv(left,parasym.vardef);
end;
if codegenerror then
exit;
end;
{ truncate shortstring value parameters at the caller side if }
{ they are passed by value (if passed by reference, then the }
{ callee will truncate when copying in the string) }
{ This happens e.g. on x86_64 for small strings }
if is_shortstring(left.resultdef) and
is_shortstring(parasym.vardef) and
(parasym.varspez=vs_value) and
not paramanager.push_addr_param(parasym.varspez,parasym.vardef,
aktcallnode.procdefinition.proccalloption) and
((is_open_string(left.resultdef) and
(tstringdef(parasym.vardef).len < 255)) or
(not is_open_string(left.resultdef) and
{ when a stringconstn is typeconverted, then only its }
{ def is modified, not the contents (needed because in }
{ Delphi/TP, if you pass a longer string to a const }
{ parameter, then the callee has to see this longer }
{ string) }
(((left.nodetype<>stringconstn) and
(tstringdef(parasym.vardef).len<tstringdef(left.resultdef).len)) or
((left.nodetype=stringconstn) and
(tstringdef(parasym.vardef).len<tstringconstnode(left).len))))) then
begin
block:=internalstatements(statements);
{ temp for the new string }
temp:=ctempcreatenode.create(parasym.vardef,parasym.vardef.size,
tt_persistent,true);
addstatement(statements,temp);
{ assign parameter to temp }
addstatement(statements,cassignmentnode.create(ctemprefnode.create(temp),left));
left:=nil;
{ release temp after next use }
addstatement(statements,ctempdeletenode.create_normal_temp(temp));
addstatement(statements,ctemprefnode.create(temp));
typecheckpass(tnode(block));
left:=block;
end;
{ check var strings }
if (cs_strict_var_strings in current_settings.localswitches) and
is_shortstring(left.resultdef) and
is_shortstring(parasym.vardef) and
(parasym.varspez in [vs_out,vs_var]) and
not(is_open_string(parasym.vardef)) and
not(equal_defs(left.resultdef,parasym.vardef)) then
begin
CGMessagePos(left.fileinfo,type_e_strict_var_string_violation);
end;
{ Handle formal parameters separate }
if (parasym.vardef.typ=formaldef) then
begin
{ load procvar if a procedure is passed }
if ((m_tp_procvar in current_settings.modeswitches) or
(m_mac_procvar in current_settings.modeswitches)) and
(left.nodetype=calln) and
(is_void(left.resultdef)) then
load_procvar_from_calln(left);
case parasym.varspez of
vs_var,
vs_out :
begin
if not valid_for_formal_var(left,true) then
CGMessagePos(left.fileinfo,parser_e_illegal_parameter_list);
end;
vs_const :
begin
if not valid_for_formal_const(left,true) then
CGMessagePos(left.fileinfo,parser_e_illegal_parameter_list);
end;
end;
end
else
begin
{ check if the argument is allowed }
if (parasym.varspez in [vs_out,vs_var]) then
valid_for_var(left,true);
end;
if parasym.varspez in [vs_var,vs_out] then
set_unique(left);
{ When the address needs to be pushed then the register is
not regable. Exception is when the location is also a var
parameter and we can pass the address transparently (but
that is handled by make_not_regable if ra_addr_regable is
passed, and make_not_regable always needs to called for
the ra_addr_taken info for non-invisble parameters }
if (
not(
(vo_is_hidden_para in parasym.varoptions) and
(left.resultdef.typ in [pointerdef,classrefdef])
) and
paramanager.push_addr_param(parasym.varspez,parasym.vardef,
aktcallnode.procdefinition.proccalloption)
) then
{ pushing the address of a variable to take the place of a temp }
{ as the complex function result of a function does not make its }
{ address escape the current block, as the "address of the }
{ function result" is not something which can be stored }
{ persistently by the callee (it becomes invalid when the callee }
{ returns) }
if not(vo_is_funcret in parasym.varoptions) then
make_not_regable(left,[ra_addr_regable,ra_addr_taken])
else
make_not_regable(left,[ra_addr_regable]);
case parasym.varspez of
vs_out :
begin
{ first set written separately to avoid false }
{ uninitialized warnings (tbs/tb0542) }
set_varstate(left,vs_written,[]);
set_varstate(left,vs_readwritten,[]);
end;
vs_var :
set_varstate(left,vs_readwritten,[vsf_must_be_valid,vsf_use_hints]);
else
set_varstate(left,vs_read,[vsf_must_be_valid]);
end;
{ must only be done after typeconv PM }
resultdef:=parasym.vardef;
end;
end;
{ process next node }
if assigned(right) then
tcallparanode(right).insert_typeconv;
end;
function tcallparanode.can_be_inlined: boolean;
var
n: tnode;
begin
n:=left;
result:=false;
while assigned(n) and
(n.nodetype=typeconvn) do
begin
{ look for type conversion nodes which convert a }
{ refcounted type into a non-refcounted type }
if (not n.resultdef.needs_inittable or
is_class(n.resultdef)) and
(ttypeconvnode(n).left.resultdef.needs_inittable and
not is_class(ttypeconvnode(n).left.resultdef)) then
exit;
n:=ttypeconvnode(n).left;
end;
{ also check for dereferencing constant pointers, like }
{ tsomerecord(nil^) passed to a const r: tsomerecord }
{ parameter }
if (n.nodetype=derefn) then
begin
repeat
n:=tunarynode(n).left;
until (n.nodetype<>typeconvn);
if (n.nodetype in [niln,pointerconstn]) then
exit
end;
result:=true;
end;
function tcallparanode.docompare(p: tnode): boolean;
begin
docompare :=
inherited docompare(p) and
(callparaflags = tcallparanode(p).callparaflags)
;
end;
procedure tcallparanode.printnodetree(var t:text);
begin
printnodelist(t);
end;
{****************************************************************************
TCALLNODE
****************************************************************************}
constructor tcallnode.create(l:tnode;v : tprocsym;st : TSymtable; mp: tnode; callflags:tcallnodeflags);
begin
inherited create(calln,l,nil);
symtableprocentry:=v;
symtableproc:=st;
callnodeflags:=callflags+[cnf_return_value_used];
methodpointer:=mp;
callinitblock:=nil;
callcleanupblock:=nil;
procdefinition:=nil;
funcretnode:=nil;
paralength:=-1;
varargsparas:=nil;
end;
constructor tcallnode.create_procvar(l,r:tnode);
begin
inherited create(calln,l,r);
symtableprocentry:=nil;
symtableproc:=nil;
methodpointer:=nil;
callinitblock:=nil;
callcleanupblock:=nil;
procdefinition:=nil;
callnodeflags:=[cnf_return_value_used];
funcretnode:=nil;
paralength:=-1;
varargsparas:=nil;
end;
constructor tcallnode.createintern(const name: string; params: tnode);
var
srsym: tsym;
begin
srsym := tsym(systemunit.Find(name));
if not assigned(srsym) and
(cs_compilesystem in current_settings.moduleswitches) then
srsym := tsym(systemunit.Find(upper(name)));
if not assigned(srsym) or
(srsym.typ<>procsym) then
Message1(cg_f_unknown_compilerproc,name);
create(params,tprocsym(srsym),srsym.owner,nil,[]);
end;
constructor tcallnode.createinternres(const name: string; params: tnode; res:tdef);
var
pd : tprocdef;
begin
createintern(name,params);
typedef := res;
include(callnodeflags,cnf_typedefset);
pd:=tprocdef(symtableprocentry.ProcdefList[0]);
{ both the normal and specified resultdef either have to be returned via a }
{ parameter or not, but no mixing (JM) }
if paramanager.ret_in_param(typedef,pd.proccalloption) xor
paramanager.ret_in_param(pd.returndef,pd.proccalloption) then
internalerror(200108291);
end;
constructor tcallnode.createinternreturn(const name: string; params: tnode; returnnode : tnode);
begin
createintern(name,params);
funcretnode:=returnnode;
end;
destructor tcallnode.destroy;
begin
methodpointer.free;
callinitblock.free;
callcleanupblock.free;
funcretnode.free;
if assigned(varargsparas) then
varargsparas.free;
inherited destroy;
end;
constructor tcallnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
begin
callinitblock:=tblocknode(ppuloadnode(ppufile));
methodpointer:=ppuloadnode(ppufile);
callcleanupblock:=tblocknode(ppuloadnode(ppufile));
funcretnode:=ppuloadnode(ppufile);
inherited ppuload(t,ppufile);
ppufile.getderef(symtableprocentryderef);
{ TODO: FIXME: No withsymtable support}
symtableproc:=nil;
ppufile.getderef(procdefinitionderef);
ppufile.getsmallset(callnodeflags);
end;
procedure tcallnode.ppuwrite(ppufile:tcompilerppufile);
begin
ppuwritenode(ppufile,callinitblock);
ppuwritenode(ppufile,methodpointer);
ppuwritenode(ppufile,callcleanupblock);
ppuwritenode(ppufile,funcretnode);
inherited ppuwrite(ppufile);
ppufile.putderef(symtableprocentryderef);
ppufile.putderef(procdefinitionderef);
ppufile.putsmallset(callnodeflags);
end;