ncal.pas 94 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This file implements the node for sub procedure calling.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit ncal;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cutils,cclasses,
  23. globtype,cpuinfo,
  24. paramgr,parabase,
  25. node,nbas,nutils,
  26. {$ifdef state_tracking}
  27. nstate,
  28. {$endif state_tracking}
  29. symbase,symtype,symsym,symdef,symtable;
  30. type
  31. tcallnodeflag = (
  32. cnf_restypeset,
  33. cnf_return_value_used,
  34. cnf_inherited,
  35. cnf_anon_inherited,
  36. cnf_new_call,
  37. cnf_dispose_call,
  38. cnf_member_call { called with implicit methodpointer tree }
  39. );
  40. tcallnodeflags = set of tcallnodeflag;
  41. tcallnode = class(tbinarynode)
  42. private
  43. {$ifndef VER1_0}
  44. { info for inlining }
  45. inlinelocals: array of tnode;
  46. {$endif VER1_0}
  47. { number of parameters passed from the source, this does not include the hidden parameters }
  48. paralength : smallint;
  49. function gen_self_tree_methodpointer:tnode;
  50. function gen_self_tree:tnode;
  51. function gen_vmt_tree:tnode;
  52. procedure bind_paraitem;
  53. { function return node, this is used to pass the data for a
  54. ret_in_param return value }
  55. _funcretnode : tnode;
  56. procedure setfuncretnode(const returnnode: tnode);
  57. procedure convert_carg_array_of_const;
  58. procedure order_parameters;
  59. procedure createinlineparas(var createstatement, deletestatement: tstatementnode);
  60. function replaceparaload(var n: tnode; arg: pointer): foreachnoderesult;
  61. {$ifndef VER1_0}
  62. procedure createlocaltemps(p:TNamedIndexItem;arg:pointer);
  63. {$endif VER1_0}
  64. protected
  65. pushedparasize : longint;
  66. public
  67. { the symbol containing the definition of the procedure }
  68. { to call }
  69. symtableprocentry : tprocsym;
  70. symtableprocentryderef : tderef;
  71. { symtable where the entry was found, needed for with support }
  72. symtableproc : tsymtable;
  73. { the definition of the procedure to call }
  74. procdefinition : tabstractprocdef;
  75. procdefinitionderef : tderef;
  76. { tree that contains the pointer to the object for this method }
  77. methodpointerinit,
  78. methodpointerdone,
  79. methodpointer : tnode;
  80. { inline function body }
  81. inlinecode : tnode;
  82. { varargs tparaitems }
  83. varargsparas : tvarargspara;
  84. { node that specifies where the result should be put for calls }
  85. { that return their result in a parameter }
  86. property funcretnode: tnode read _funcretnode write setfuncretnode;
  87. { separately specified resulttype for some compilerprocs (e.g. }
  88. { you can't have a function with an "array of char" resulttype }
  89. { the RTL) (JM) }
  90. restype: ttype;
  91. callnodeflags : tcallnodeflags;
  92. { only the processor specific nodes need to override this }
  93. { constructor }
  94. constructor create(l:tnode; v : tprocsym;st : tsymtable; mp: tnode; callflags:tcallnodeflags);virtual;
  95. constructor create_procvar(l,r:tnode);
  96. constructor createintern(const name: string; params: tnode);
  97. constructor createinternres(const name: string; params: tnode; const res: ttype);
  98. constructor createinternreturn(const name: string; params: tnode; returnnode : tnode);
  99. destructor destroy;override;
  100. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  101. procedure ppuwrite(ppufile:tcompilerppufile);override;
  102. procedure buildderefimpl;override;
  103. procedure derefimpl;override;
  104. function getcopy : tnode;override;
  105. { Goes through all symbols in a class and subclasses and calls
  106. verify abstract for each .
  107. }
  108. procedure verifyabstractcalls;
  109. { called for each definition in a class and verifies if a method
  110. is abstract or not, if it is abstract, give out a warning
  111. }
  112. procedure verifyabstract(p : tnamedindexitem;arg:pointer);
  113. procedure insertintolist(l : tnodelist);override;
  114. function pass_1 : tnode;override;
  115. function det_resulttype:tnode;override;
  116. {$ifdef state_tracking}
  117. function track_state_pass(exec_known:boolean):boolean;override;
  118. {$endif state_tracking}
  119. function docompare(p: tnode): boolean; override;
  120. procedure printnodedata(var t:text);override;
  121. function para_count:longint;
  122. private
  123. AbstractMethodsList : TStringList;
  124. end;
  125. tcallnodeclass = class of tcallnode;
  126. tcallparaflag = (
  127. cpf_is_colon_para,
  128. cpf_varargs_para { belongs this para to varargs }
  129. );
  130. tcallparaflags = set of tcallparaflag;
  131. tcallparanode = class(tbinarynode)
  132. public
  133. callparaflags : tcallparaflags;
  134. paraitem : tparaitem;
  135. used_by_callnode : boolean;
  136. { only the processor specific nodes need to override this }
  137. { constructor }
  138. constructor create(expr,next : tnode);virtual;
  139. destructor destroy;override;
  140. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  141. procedure ppuwrite(ppufile:tcompilerppufile);override;
  142. function getcopy : tnode;override;
  143. procedure insertintolist(l : tnodelist);override;
  144. procedure get_paratype;
  145. procedure insert_typeconv(do_count : boolean);
  146. procedure det_registers;
  147. procedure firstcallparan;
  148. procedure secondcallparan;virtual;abstract;
  149. function docompare(p: tnode): boolean; override;
  150. procedure printnodetree(var t:text);override;
  151. end;
  152. tcallparanodeclass = class of tcallparanode;
  153. function reverseparameters(p: tcallparanode): tcallparanode;
  154. var
  155. ccallnode : tcallnodeclass;
  156. ccallparanode : tcallparanodeclass;
  157. { Current callnode, this is needed for having a link
  158. between the callparanodes and the callnode they belong to }
  159. aktcallnode : tcallnode;
  160. implementation
  161. uses
  162. systems,
  163. verbose,globals,
  164. symconst,defutil,defcmp,
  165. htypechk,pass_1,
  166. ncnv,nld,ninl,nadd,ncon,nmem,
  167. procinfo,
  168. cgbase
  169. ;
  170. type
  171. tobjectinfoitem = class(tlinkedlistitem)
  172. objinfo : tobjectdef;
  173. constructor create(def : tobjectdef);
  174. end;
  175. {****************************************************************************
  176. HELPERS
  177. ****************************************************************************}
  178. function reverseparameters(p: tcallparanode): tcallparanode;
  179. var
  180. hp1, hp2: tcallparanode;
  181. begin
  182. hp1:=nil;
  183. while assigned(p) do
  184. begin
  185. { pull out }
  186. hp2:=p;
  187. p:=tcallparanode(p.right);
  188. { pull in }
  189. hp2.right:=hp1;
  190. hp1:=hp2;
  191. end;
  192. reverseparameters:=hp1;
  193. end;
  194. function gen_high_tree(p:tnode;paradef:tdef):tnode;
  195. var
  196. temp: tnode;
  197. len : integer;
  198. loadconst : boolean;
  199. hightree : tnode;
  200. begin
  201. len:=-1;
  202. loadconst:=true;
  203. hightree:=nil;
  204. case p.resulttype.def.deftype of
  205. arraydef :
  206. begin
  207. if (paradef.deftype<>arraydef) then
  208. internalerror(200405241);
  209. { handle special case of passing an single array to an array of array }
  210. if compare_defs(tarraydef(paradef).elementtype.def,p.resulttype.def,nothingn)>=te_equal then
  211. len:=0
  212. else
  213. begin
  214. { handle via a normal inline in_high_x node }
  215. loadconst := false;
  216. hightree := geninlinenode(in_high_x,false,p.getcopy);
  217. resulttypepass(hightree);
  218. { only substract low(array) if it's <> 0 }
  219. temp := geninlinenode(in_low_x,false,p.getcopy);
  220. resulttypepass(temp);
  221. if (temp.nodetype <> ordconstn) or
  222. (tordconstnode(temp).value <> 0) then
  223. hightree := caddnode.create(subn,hightree,temp)
  224. else
  225. temp.free;
  226. end;
  227. end;
  228. stringdef :
  229. begin
  230. if is_open_string(paradef) then
  231. begin
  232. { handle via a normal inline in_high_x node }
  233. loadconst := false;
  234. hightree := geninlinenode(in_high_x,false,p.getcopy);
  235. end
  236. else
  237. begin
  238. { passing a string to an array of char }
  239. if (p.nodetype=stringconstn) then
  240. begin
  241. len:=str_length(p);
  242. if len>0 then
  243. dec(len);
  244. end
  245. else
  246. begin
  247. hightree:=caddnode.create(subn,geninlinenode(in_length_x,false,p.getcopy),
  248. cordconstnode.create(1,s32inttype,false));
  249. loadconst:=false;
  250. end;
  251. end;
  252. end;
  253. else
  254. len:=0;
  255. end;
  256. if loadconst then
  257. hightree:=cordconstnode.create(len,s32inttype,true)
  258. else
  259. begin
  260. if not assigned(hightree) then
  261. internalerror(200304071);
  262. { Need to use explicit, because it can also be a enum }
  263. hightree:=ctypeconvnode.create_explicit(hightree,s32inttype);
  264. end;
  265. result:=hightree;
  266. end;
  267. {****************************************************************************
  268. TOBJECTINFOITEM
  269. ****************************************************************************}
  270. constructor tobjectinfoitem.create(def : tobjectdef);
  271. begin
  272. inherited create;
  273. objinfo := def;
  274. end;
  275. {****************************************************************************
  276. TCALLPARANODE
  277. ****************************************************************************}
  278. constructor tcallparanode.create(expr,next : tnode);
  279. begin
  280. inherited create(callparan,expr,next);
  281. if not assigned(expr) then
  282. internalerror(200305091);
  283. expr.set_file_line(self);
  284. callparaflags:=[];
  285. end;
  286. destructor tcallparanode.destroy;
  287. begin
  288. { When the node is used by callnode then
  289. we don't destroy left, the callnode takes care of it }
  290. if used_by_callnode then
  291. left:=nil;
  292. inherited destroy;
  293. end;
  294. constructor tcallparanode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  295. begin
  296. inherited ppuload(t,ppufile);
  297. ppufile.getsmallset(callparaflags);
  298. end;
  299. procedure tcallparanode.ppuwrite(ppufile:tcompilerppufile);
  300. begin
  301. inherited ppuwrite(ppufile);
  302. ppufile.putsmallset(callparaflags);
  303. end;
  304. function tcallparanode.getcopy : tnode;
  305. var
  306. n : tcallparanode;
  307. begin
  308. n:=tcallparanode(inherited getcopy);
  309. n.callparaflags:=callparaflags;
  310. n.paraitem:=paraitem;
  311. result:=n;
  312. end;
  313. procedure tcallparanode.insertintolist(l : tnodelist);
  314. begin
  315. end;
  316. procedure tcallparanode.get_paratype;
  317. var
  318. old_array_constructor : boolean;
  319. begin
  320. inc(parsing_para_level);
  321. if assigned(right) then
  322. tcallparanode(right).get_paratype;
  323. old_array_constructor:=allow_array_constructor;
  324. allow_array_constructor:=true;
  325. resulttypepass(left);
  326. allow_array_constructor:=old_array_constructor;
  327. if codegenerror then
  328. resulttype:=generrortype
  329. else
  330. resulttype:=left.resulttype;
  331. dec(parsing_para_level);
  332. end;
  333. procedure tcallparanode.insert_typeconv(do_count : boolean);
  334. var
  335. oldtype : ttype;
  336. {$ifdef extdebug}
  337. store_count_ref : boolean;
  338. {$endif def extdebug}
  339. begin
  340. inc(parsing_para_level);
  341. {$ifdef extdebug}
  342. if do_count then
  343. begin
  344. store_count_ref:=count_ref;
  345. count_ref:=true;
  346. end;
  347. {$endif def extdebug}
  348. { Be sure to have the resulttype }
  349. if not assigned(left.resulttype.def) then
  350. resulttypepass(left);
  351. if (left.nodetype<>nothingn) then
  352. begin
  353. { Convert tp procvars, this is needs to be done
  354. here to make the change permanent. in the overload
  355. choosing the changes are only made temporary }
  356. if (left.resulttype.def.deftype=procvardef) and
  357. (paraitem.paratype.def.deftype<>procvardef) then
  358. begin
  359. if maybe_call_procvar(left,true) then
  360. resulttype:=left.resulttype;
  361. end;
  362. { Handle varargs and hidden paras directly, no typeconvs or }
  363. { typechecking needed }
  364. if (cpf_varargs_para in callparaflags) then
  365. begin
  366. { convert pascal to C types }
  367. case left.resulttype.def.deftype of
  368. stringdef :
  369. inserttypeconv(left,charpointertype);
  370. floatdef :
  371. inserttypeconv(left,s64floattype);
  372. end;
  373. set_varstate(left,vs_used,true);
  374. resulttype:=left.resulttype;
  375. { also update paraitem type to get the correct parameter location
  376. for the new types }
  377. paraitem.paratype:=left.resulttype;
  378. end
  379. else
  380. if (paraitem.is_hidden) then
  381. begin
  382. set_varstate(left,vs_used,true);
  383. resulttype:=left.resulttype;
  384. end
  385. else
  386. begin
  387. { Do we need arrayconstructor -> set conversion, then insert
  388. it here before the arrayconstructor node breaks the tree
  389. with its conversions of enum->ord }
  390. if (left.nodetype=arrayconstructorn) and
  391. (paraitem.paratype.def.deftype=setdef) then
  392. inserttypeconv(left,paraitem.paratype);
  393. { set some settings needed for arrayconstructor }
  394. if is_array_constructor(left.resulttype.def) then
  395. begin
  396. if is_array_of_const(paraitem.paratype.def) then
  397. begin
  398. { force variant array }
  399. include(left.flags,nf_forcevaria);
  400. end
  401. else
  402. begin
  403. include(left.flags,nf_novariaallowed);
  404. { now that the resultting type is know we can insert the required
  405. typeconvs for the array constructor }
  406. if paraitem.paratype.def.deftype=arraydef then
  407. tarrayconstructornode(left).force_type(tarraydef(paraitem.paratype.def).elementtype);
  408. end;
  409. end;
  410. { check if local proc/func is assigned to procvar }
  411. if left.resulttype.def.deftype=procvardef then
  412. test_local_to_procvar(tprocvardef(left.resulttype.def),paraitem.paratype.def);
  413. { test conversions }
  414. if not(is_shortstring(left.resulttype.def) and
  415. is_shortstring(paraitem.paratype.def)) and
  416. (paraitem.paratype.def.deftype<>formaldef) then
  417. begin
  418. { Process open parameters }
  419. if paramanager.push_high_param(paraitem.paratyp,paraitem.paratype.def,aktcallnode.procdefinition.proccalloption) then
  420. begin
  421. { insert type conv but hold the ranges of the array }
  422. oldtype:=left.resulttype;
  423. inserttypeconv(left,paraitem.paratype);
  424. left.resulttype:=oldtype;
  425. end
  426. else
  427. begin
  428. { for ordinals, floats and enums, verify if we might cause
  429. some range-check errors. }
  430. if (paraitem.paratype.def.deftype in [enumdef,orddef,floatdef]) and
  431. (left.resulttype.def.deftype in [enumdef,orddef,floatdef]) and
  432. (left.nodetype in [vecn,loadn,calln]) then
  433. begin
  434. if (left.resulttype.def.size>paraitem.paratype.def.size) then
  435. begin
  436. if (cs_check_range in aktlocalswitches) then
  437. Message(type_w_smaller_possible_range_check)
  438. else
  439. Message(type_h_smaller_possible_range_check);
  440. end;
  441. end;
  442. inserttypeconv(left,paraitem.paratype);
  443. end;
  444. if codegenerror then
  445. begin
  446. dec(parsing_para_level);
  447. exit;
  448. end;
  449. end;
  450. { check var strings }
  451. if (cs_strict_var_strings in aktlocalswitches) and
  452. is_shortstring(left.resulttype.def) and
  453. is_shortstring(paraitem.paratype.def) and
  454. (paraitem.paratyp in [vs_out,vs_var]) and
  455. not(is_open_string(paraitem.paratype.def)) and
  456. not(equal_defs(left.resulttype.def,paraitem.paratype.def)) then
  457. begin
  458. aktfilepos:=left.fileinfo;
  459. CGMessage(type_e_strict_var_string_violation);
  460. end;
  461. { Handle formal parameters separate }
  462. if (paraitem.paratype.def.deftype=formaldef) then
  463. begin
  464. { load procvar if a procedure is passed }
  465. if (m_tp_procvar in aktmodeswitches) and
  466. (left.nodetype=calln) and
  467. (is_void(left.resulttype.def)) then
  468. load_procvar_from_calln(left);
  469. case paraitem.paratyp of
  470. vs_var,
  471. vs_out :
  472. begin
  473. if not valid_for_formal_var(left) then
  474. CGMessagePos(left.fileinfo,parser_e_illegal_parameter_list);
  475. end;
  476. vs_const :
  477. begin
  478. if not valid_for_formal_const(left) then
  479. CGMessagePos(left.fileinfo,parser_e_illegal_parameter_list);
  480. end;
  481. end;
  482. end
  483. else
  484. begin
  485. { check if the argument is allowed }
  486. if (paraitem.paratyp in [vs_out,vs_var]) then
  487. valid_for_var(left);
  488. end;
  489. if paraitem.paratyp = vs_var then
  490. set_unique(left);
  491. { When the address needs to be pushed then the register is
  492. not regable. Exception is when the location is also a var
  493. parameter and we can pass the address transparently }
  494. if (
  495. not(
  496. paraitem.is_hidden and
  497. (left.resulttype.def.deftype in [pointerdef,classrefdef])
  498. ) and
  499. paramanager.push_addr_param(paraitem.paratyp,paraitem.paratype.def,
  500. aktcallnode.procdefinition.proccalloption) and
  501. not(
  502. (left.nodetype=loadn) and
  503. (tloadnode(left).is_addr_param_load)
  504. )
  505. ) then
  506. make_not_regable(left);
  507. if do_count then
  508. begin
  509. if paraitem.paratyp in [vs_var,vs_out] then
  510. set_varstate(left,vs_used,false)
  511. else
  512. set_varstate(left,vs_used,true);
  513. end;
  514. { must only be done after typeconv PM }
  515. resulttype:=paraitem.paratype;
  516. end;
  517. end;
  518. { process next node }
  519. if assigned(right) then
  520. tcallparanode(right).insert_typeconv(do_count);
  521. dec(parsing_para_level);
  522. {$ifdef extdebug}
  523. if do_count then
  524. count_ref:=store_count_ref;
  525. {$endif def extdebug}
  526. end;
  527. procedure tcallparanode.det_registers;
  528. begin
  529. if assigned(right) then
  530. begin
  531. tcallparanode(right).det_registers;
  532. registersint:=right.registersint;
  533. registersfpu:=right.registersfpu;
  534. {$ifdef SUPPORT_MMX}
  535. registersmmx:=right.registersmmx;
  536. {$endif}
  537. end;
  538. firstpass(left);
  539. if left.registersint>registersint then
  540. registersint:=left.registersint;
  541. if left.registersfpu>registersfpu then
  542. registersfpu:=left.registersfpu;
  543. {$ifdef SUPPORT_MMX}
  544. if left.registersmmx>registersmmx then
  545. registersmmx:=left.registersmmx;
  546. {$endif SUPPORT_MMX}
  547. end;
  548. procedure tcallparanode.firstcallparan;
  549. begin
  550. if not assigned(left.resulttype.def) then
  551. get_paratype;
  552. det_registers;
  553. end;
  554. function tcallparanode.docompare(p: tnode): boolean;
  555. begin
  556. docompare :=
  557. inherited docompare(p) and
  558. (callparaflags = tcallparanode(p).callparaflags)
  559. ;
  560. end;
  561. procedure tcallparanode.printnodetree(var t:text);
  562. begin
  563. printnodelist(t);
  564. end;
  565. {****************************************************************************
  566. TCALLNODE
  567. ****************************************************************************}
  568. constructor tcallnode.create(l:tnode;v : tprocsym;st : tsymtable; mp: tnode; callflags:tcallnodeflags);
  569. begin
  570. inherited create(calln,l,nil);
  571. symtableprocentry:=v;
  572. symtableproc:=st;
  573. callnodeflags:=callflags+[cnf_return_value_used];
  574. methodpointer:=mp;
  575. methodpointerinit:=nil;
  576. methodpointerdone:=nil;
  577. procdefinition:=nil;
  578. _funcretnode:=nil;
  579. inlinecode:=nil;
  580. paralength:=-1;
  581. varargsparas:=nil;
  582. end;
  583. constructor tcallnode.create_procvar(l,r:tnode);
  584. begin
  585. inherited create(calln,l,r);
  586. symtableprocentry:=nil;
  587. symtableproc:=nil;
  588. methodpointer:=nil;
  589. methodpointerinit:=nil;
  590. methodpointerdone:=nil;
  591. procdefinition:=nil;
  592. callnodeflags:=[cnf_return_value_used];
  593. _funcretnode:=nil;
  594. inlinecode:=nil;
  595. paralength:=-1;
  596. varargsparas:=nil;
  597. end;
  598. constructor tcallnode.createintern(const name: string; params: tnode);
  599. var
  600. srsym: tsym;
  601. symowner: tsymtable;
  602. begin
  603. if not (cs_compilesystem in aktmoduleswitches) then
  604. begin
  605. srsym := searchsymonlyin(systemunit,name);
  606. symowner := systemunit;
  607. end
  608. else
  609. begin
  610. searchsym(name,srsym,symowner);
  611. if not assigned(srsym) then
  612. searchsym(upper(name),srsym,symowner);
  613. end;
  614. if not assigned(srsym) or
  615. (srsym.typ <> procsym) then
  616. begin
  617. {$ifdef EXTDEBUG}
  618. Comment(V_Error,'unknown compilerproc '+name);
  619. {$endif EXTDEBUG}
  620. internalerror(200107271);
  621. end;
  622. self.create(params,tprocsym(srsym),symowner,nil,[]);
  623. end;
  624. constructor tcallnode.createinternres(const name: string; params: tnode; const res: ttype);
  625. begin
  626. self.createintern(name,params);
  627. restype := res;
  628. include(callnodeflags,cnf_restypeset);
  629. { both the normal and specified resulttype either have to be returned via a }
  630. { parameter or not, but no mixing (JM) }
  631. if paramanager.ret_in_param(restype.def,pocall_compilerproc) xor
  632. paramanager.ret_in_param(symtableprocentry.first_procdef.rettype.def,symtableprocentry.first_procdef.proccalloption) then
  633. internalerror(200108291);
  634. end;
  635. constructor tcallnode.createinternreturn(const name: string; params: tnode; returnnode : tnode);
  636. begin
  637. self.createintern(name,params);
  638. _funcretnode:=returnnode;
  639. if not paramanager.ret_in_param(symtableprocentry.first_procdef.rettype.def,symtableprocentry.first_procdef.proccalloption) then
  640. internalerror(200204247);
  641. end;
  642. procedure tcallnode.setfuncretnode(const returnnode: tnode);
  643. var
  644. para: tcallparanode;
  645. begin
  646. if assigned(_funcretnode) then
  647. _funcretnode.free;
  648. _funcretnode := returnnode;
  649. { if the resulttype pass hasn't occurred yet, that one will do }
  650. { everything }
  651. if assigned(resulttype.def) then
  652. begin
  653. { these are returned as values, but we can optimize their loading }
  654. { as well }
  655. if is_ansistring(resulttype.def) or
  656. is_widestring(resulttype.def) then
  657. exit;
  658. para := tcallparanode(left);
  659. while assigned(para) do
  660. begin
  661. if para.paraitem.is_hidden and
  662. (vo_is_funcret in tvarsym(para.paraitem.parasym).varoptions) then
  663. begin
  664. para.left.free;
  665. para.left := _funcretnode.getcopy;
  666. exit;
  667. end;
  668. para := tcallparanode(para.right);
  669. end;
  670. { no hidden resultpara found, error! }
  671. if not(procdefinition.proccalloption = pocall_inline) then
  672. internalerror(200306087);
  673. end;
  674. end;
  675. destructor tcallnode.destroy;
  676. begin
  677. methodpointer.free;
  678. methodpointerinit.free;
  679. methodpointerdone.free;
  680. _funcretnode.free;
  681. inlinecode.free;
  682. if assigned(varargsparas) then
  683. varargsparas.free;
  684. inherited destroy;
  685. end;
  686. constructor tcallnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  687. begin
  688. inherited ppuload(t,ppufile);
  689. ppufile.getderef(symtableprocentryderef);
  690. {$ifdef fpc}
  691. {$warning FIXME: No withsymtable support}
  692. {$endif}
  693. symtableproc:=nil;
  694. ppufile.getderef(procdefinitionderef);
  695. ppufile.getsmallset(callnodeflags);
  696. methodpointer:=ppuloadnode(ppufile);
  697. methodpointerinit:=ppuloadnode(ppufile);
  698. methodpointerdone:=ppuloadnode(ppufile);
  699. _funcretnode:=ppuloadnode(ppufile);
  700. inlinecode:=ppuloadnode(ppufile);
  701. end;
  702. procedure tcallnode.ppuwrite(ppufile:tcompilerppufile);
  703. begin
  704. inherited ppuwrite(ppufile);
  705. ppufile.putderef(symtableprocentryderef);
  706. ppufile.putderef(procdefinitionderef);
  707. ppufile.putsmallset(callnodeflags);
  708. ppuwritenode(ppufile,methodpointer);
  709. ppuwritenode(ppufile,methodpointerinit);
  710. ppuwritenode(ppufile,methodpointerdone);
  711. ppuwritenode(ppufile,_funcretnode);
  712. ppuwritenode(ppufile,inlinecode);
  713. end;
  714. procedure tcallnode.buildderefimpl;
  715. begin
  716. inherited buildderefimpl;
  717. symtableprocentryderef.build(symtableprocentry);
  718. procdefinitionderef.build(procdefinition);
  719. if assigned(methodpointer) then
  720. methodpointer.buildderefimpl;
  721. if assigned(methodpointerinit) then
  722. methodpointerinit.buildderefimpl;
  723. if assigned(methodpointerdone) then
  724. methodpointerdone.buildderefimpl;
  725. if assigned(_funcretnode) then
  726. _funcretnode.buildderefimpl;
  727. if assigned(inlinecode) then
  728. inlinecode.buildderefimpl;
  729. end;
  730. procedure tcallnode.derefimpl;
  731. var
  732. pt : tcallparanode;
  733. currpara : tparaitem;
  734. begin
  735. inherited derefimpl;
  736. symtableprocentry:=tprocsym(symtableprocentryderef.resolve);
  737. symtableproc:=symtableprocentry.owner;
  738. procdefinition:=tprocdef(procdefinitionderef.resolve);
  739. if assigned(methodpointer) then
  740. methodpointer.derefimpl;
  741. if assigned(methodpointerinit) then
  742. methodpointerinit.derefimpl;
  743. if assigned(methodpointerdone) then
  744. methodpointerdone.derefimpl;
  745. if assigned(_funcretnode) then
  746. _funcretnode.derefimpl;
  747. if assigned(inlinecode) then
  748. inlinecode.derefimpl;
  749. { Connect paraitems }
  750. pt:=tcallparanode(left);
  751. while assigned(pt) and
  752. (cpf_varargs_para in pt.callparaflags) do
  753. pt:=tcallparanode(pt.right);
  754. currpara:=tparaitem(procdefinition.Para.last);
  755. while assigned(currpara) do
  756. begin
  757. if not assigned(pt) then
  758. internalerror(200311077);
  759. pt.paraitem:=currpara;
  760. pt:=tcallparanode(pt.right);
  761. currpara:=tparaitem(currpara.previous);
  762. end;
  763. if assigned(currpara) or assigned(pt) then
  764. internalerror(200311078);
  765. end;
  766. function tcallnode.getcopy : tnode;
  767. var
  768. n : tcallnode;
  769. hp : tparaitem;
  770. oldleft : tnode;
  771. begin
  772. { Need to use a hack here to prevent the parameters from being copied.
  773. The parameters must be copied between methodpointerinit/methodpointerdone because
  774. the can reference methodpointer }
  775. oldleft:=left;
  776. left:=nil;
  777. n:=tcallnode(inherited getcopy);
  778. left:=oldleft;
  779. n.symtableprocentry:=symtableprocentry;
  780. n.symtableproc:=symtableproc;
  781. n.procdefinition:=procdefinition;
  782. n.restype := restype;
  783. n.callnodeflags := callnodeflags;
  784. if assigned(methodpointerinit) then
  785. n.methodpointerinit:=methodpointerinit.getcopy
  786. else
  787. n.methodpointerinit:=nil;
  788. { methodpointerinit is copied, now references to the temp will also be copied
  789. correctly. We can now copy the parameters and methodpointer }
  790. if assigned(left) then
  791. n.left:=left.getcopy
  792. else
  793. n.left:=nil;
  794. if assigned(methodpointer) then
  795. n.methodpointer:=methodpointer.getcopy
  796. else
  797. n.methodpointer:=nil;
  798. if assigned(methodpointerdone) then
  799. n.methodpointerdone:=methodpointerdone.getcopy
  800. else
  801. n.methodpointerdone:=nil;
  802. if assigned(_funcretnode) then
  803. n._funcretnode:=_funcretnode.getcopy
  804. else
  805. n._funcretnode:=nil;
  806. if assigned(inlinecode) then
  807. n.inlinecode:=inlinecode.getcopy
  808. else
  809. n.inlinecode:=nil;
  810. if assigned(varargsparas) then
  811. begin
  812. n.varargsparas:=tvarargspara.create;
  813. hp:=tparaitem(varargsparas.first);
  814. while assigned(hp) do
  815. begin
  816. n.varargsparas.concat(hp.getcopy);
  817. hp:=tparaitem(hp.next);
  818. end;
  819. end
  820. else
  821. n.varargsparas:=nil;
  822. result:=n;
  823. end;
  824. procedure tcallnode.insertintolist(l : tnodelist);
  825. begin
  826. end;
  827. procedure tcallnode.convert_carg_array_of_const;
  828. var
  829. hp : tarrayconstructornode;
  830. oldleft : tcallparanode;
  831. begin
  832. oldleft:=tcallparanode(left);
  833. if oldleft.left.nodetype<>arrayconstructorn then
  834. begin
  835. CGMessage1(type_e_wrong_type_in_array_constructor,oldleft.left.resulttype.def.typename);
  836. exit;
  837. end;
  838. { Get arrayconstructor node and insert typeconvs }
  839. hp:=tarrayconstructornode(oldleft.left);
  840. hp.insert_typeconvs;
  841. { Add c args parameters }
  842. { It could be an empty set }
  843. if assigned(hp) and
  844. assigned(hp.left) then
  845. begin
  846. while assigned(hp) do
  847. begin
  848. left:=ccallparanode.create(hp.left,left);
  849. { set callparanode resulttype and flags }
  850. left.resulttype:=hp.left.resulttype;
  851. include(tcallparanode(left).callparaflags,cpf_varargs_para);
  852. hp.left:=nil;
  853. hp:=tarrayconstructornode(hp.right);
  854. end;
  855. end;
  856. { Remove value of old array of const parameter, but keep it
  857. in the list because it is required for bind_paraitem.
  858. Generate a nothign to keep callparanoed.left valid }
  859. oldleft.left.free;
  860. oldleft.left:=cnothingnode.create;
  861. end;
  862. procedure tcallnode.verifyabstract(p : tnamedindexitem;arg:pointer);
  863. var
  864. hp : tprocdef;
  865. j: integer;
  866. begin
  867. if (tsym(p).typ=procsym) then
  868. begin
  869. for j:=1 to tprocsym(p).procdef_count do
  870. begin
  871. { index starts at 1 }
  872. hp:=tprocsym(p).procdef[j];
  873. { If this is an abstract method insert into the list }
  874. if (po_abstractmethod in hp.procoptions) then
  875. AbstractMethodsList.Insert(hp.procsym.realname)
  876. else
  877. { If this symbol is an overriding method, then remove it from the list }
  878. if po_overridingmethod in hp.procoptions then
  879. AbstractMethodsList.Remove(hp.procsym.realname);
  880. end;
  881. end;
  882. end;
  883. procedure tcallnode.verifyabstractcalls;
  884. var
  885. objectdf : tobjectdef;
  886. parents : tlinkedlist;
  887. objectinfo : tobjectinfoitem;
  888. stritem : tstringlistitem;
  889. begin
  890. objectdf := nil;
  891. { verify if trying to create an instance of a class which contains
  892. non-implemented abstract methods }
  893. { first verify this class type, no class than exit }
  894. { also, this checking can only be done if the constructor is directly
  895. called, indirect constructor calls cannot be checked.
  896. }
  897. if assigned(methodpointer) then
  898. begin
  899. if (methodpointer.resulttype.def.deftype = objectdef) then
  900. objectdf:=tobjectdef(methodpointer.resulttype.def)
  901. else
  902. if (methodpointer.resulttype.def.deftype = classrefdef) and
  903. (tclassrefdef(methodpointer.resulttype.def).pointertype.def.deftype = objectdef) and
  904. (methodpointer.nodetype in [typen,loadvmtaddrn]) then
  905. objectdf:=tobjectdef(tclassrefdef(methodpointer.resulttype.def).pointertype.def);
  906. end;
  907. if not assigned(objectdf) then
  908. exit;
  909. parents := tlinkedlist.create;
  910. AbstractMethodsList := tstringlist.create;
  911. { insert all parents in this class : the first item in the
  912. list will be the base parent of the class .
  913. }
  914. while assigned(objectdf) do
  915. begin
  916. objectinfo:=tobjectinfoitem.create(objectdf);
  917. parents.insert(objectinfo);
  918. objectdf := objectdf.childof;
  919. end;
  920. { now all parents are in the correct order
  921. insert all abstract methods in the list, and remove
  922. those which are overriden by parent classes.
  923. }
  924. objectinfo:=tobjectinfoitem(parents.first);
  925. while assigned(objectinfo) do
  926. begin
  927. objectdf := objectinfo.objinfo;
  928. if assigned(objectdf.symtable) then
  929. objectdf.symtable.foreach({$ifdef FPCPROCVAR}@{$endif}verifyabstract,nil);
  930. objectinfo:=tobjectinfoitem(objectinfo.next);
  931. end;
  932. if assigned(parents) then
  933. parents.free;
  934. { Finally give out a warning for each abstract method still in the list }
  935. stritem := tstringlistitem(AbstractMethodsList.first);
  936. if assigned(stritem) then
  937. Message1(type_w_instance_with_abstract,objectdf.objrealname^);
  938. while assigned(stritem) do
  939. begin
  940. if assigned(stritem.fpstr) then
  941. Message1(sym_h_param_list,stritem.str);
  942. stritem := tstringlistitem(stritem.next);
  943. end;
  944. if assigned(AbstractMethodsList) then
  945. AbstractMethodsList.Free;
  946. end;
  947. function tcallnode.gen_self_tree_methodpointer:tnode;
  948. var
  949. hsym : tvarsym;
  950. begin
  951. { find self field in methodpointer record }
  952. hsym:=tvarsym(trecorddef(methodpointertype.def).symtable.search('self'));
  953. if not assigned(hsym) then
  954. internalerror(200305251);
  955. { Load tmehodpointer(right).self }
  956. result:=csubscriptnode.create(
  957. hsym,
  958. ctypeconvnode.create_explicit(right.getcopy,methodpointertype));
  959. end;
  960. function tcallnode.gen_self_tree:tnode;
  961. var
  962. selftree : tnode;
  963. begin
  964. selftree:=nil;
  965. { inherited }
  966. if (cnf_inherited in callnodeflags) then
  967. selftree:=load_self_node
  968. else
  969. { constructors }
  970. if (procdefinition.proctypeoption=potype_constructor) then
  971. begin
  972. { push 0 as self when allocation is needed }
  973. if (methodpointer.resulttype.def.deftype=classrefdef) or
  974. (cnf_new_call in callnodeflags) then
  975. selftree:=cpointerconstnode.create(0,voidpointertype)
  976. else
  977. begin
  978. if methodpointer.nodetype=typen then
  979. selftree:=load_self_node
  980. else
  981. selftree:=methodpointer.getcopy;
  982. end;
  983. end
  984. else
  985. { Calling a static/class method }
  986. if (po_classmethod in procdefinition.procoptions) or
  987. (po_staticmethod in procdefinition.procoptions) then
  988. begin
  989. if (procdefinition.deftype<>procdef) then
  990. internalerror(200305062);
  991. if (oo_has_vmt in tprocdef(procdefinition)._class.objectoptions) then
  992. begin
  993. { we only need the vmt, loading self is not required and there is no
  994. need to check for typen, because that will always get the
  995. loadvmtaddrnode added }
  996. selftree:=methodpointer.getcopy;
  997. if methodpointer.resulttype.def.deftype<>classrefdef then
  998. selftree:=cloadvmtaddrnode.create(selftree);
  999. end
  1000. else
  1001. selftree:=cpointerconstnode.create(0,voidpointertype);
  1002. end
  1003. else
  1004. begin
  1005. if methodpointer.nodetype=typen then
  1006. selftree:=load_self_node
  1007. else
  1008. selftree:=methodpointer.getcopy;
  1009. end;
  1010. result:=selftree;
  1011. end;
  1012. function tcallnode.gen_vmt_tree:tnode;
  1013. var
  1014. vmttree : tnode;
  1015. begin
  1016. vmttree:=nil;
  1017. if not(procdefinition.proctypeoption in [potype_constructor,potype_destructor]) then
  1018. internalerror(200305051);
  1019. { inherited call, no create/destroy }
  1020. if (cnf_inherited in callnodeflags) then
  1021. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1022. else
  1023. { do not create/destroy when called from member function
  1024. without specifying self explicit }
  1025. if (cnf_member_call in callnodeflags) then
  1026. begin
  1027. if (methodpointer.resulttype.def.deftype=classrefdef) and
  1028. (procdefinition.proctypeoption=potype_constructor) then
  1029. vmttree:=methodpointer.getcopy
  1030. else
  1031. vmttree:=cpointerconstnode.create(0,voidpointertype);
  1032. end
  1033. else
  1034. { constructor with extended syntax called from new }
  1035. if (cnf_new_call in callnodeflags) then
  1036. vmttree:=cloadvmtaddrnode.create(ctypenode.create(methodpointer.resulttype))
  1037. else
  1038. { destructor with extended syntax called from dispose }
  1039. if (cnf_dispose_call in callnodeflags) then
  1040. vmttree:=cloadvmtaddrnode.create(methodpointer.getcopy)
  1041. else
  1042. if (methodpointer.resulttype.def.deftype=classrefdef) then
  1043. begin
  1044. { constructor call via classreference => allocate memory }
  1045. if (procdefinition.proctypeoption=potype_constructor) and
  1046. is_class(tclassrefdef(methodpointer.resulttype.def).pointertype.def) then
  1047. begin
  1048. vmttree:=methodpointer.getcopy;
  1049. { Only a typenode can be passed when it is called with <class of xx>.create }
  1050. if vmttree.nodetype=typen then
  1051. vmttree:=cloadvmtaddrnode.create(vmttree);
  1052. end
  1053. else
  1054. vmttree:=cpointerconstnode.create(0,voidpointertype);
  1055. end
  1056. else
  1057. { class }
  1058. if is_class(methodpointer.resulttype.def) then
  1059. begin
  1060. { destructor: release instance, flag(vmt)=1
  1061. constructor: direct call, do nothing, leave vmt=0 }
  1062. if (procdefinition.proctypeoption=potype_destructor) then
  1063. vmttree:=cpointerconstnode.create(1,voidpointertype)
  1064. else
  1065. vmttree:=cpointerconstnode.create(0,voidpointertype);
  1066. end
  1067. else
  1068. { object }
  1069. begin
  1070. { destructor: direct call, no dispose, vmt=0
  1071. constructor: initialize object, load vmt }
  1072. if (procdefinition.proctypeoption=potype_constructor) then
  1073. { old styled inherited call? }
  1074. if (methodpointer.nodetype=typen) then
  1075. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1076. else
  1077. vmttree:=cloadvmtaddrnode.create(ctypenode.create(methodpointer.resulttype))
  1078. else
  1079. vmttree:=cpointerconstnode.create(0,voidpointertype);
  1080. end;
  1081. result:=vmttree;
  1082. end;
  1083. procedure tcallnode.bind_paraitem;
  1084. var
  1085. i : integer;
  1086. pt : tcallparanode;
  1087. oldppt : ^tcallparanode;
  1088. varargspara,
  1089. currpara : tparaitem;
  1090. used_by_callnode : boolean;
  1091. hiddentree : tnode;
  1092. newstatement : tstatementnode;
  1093. temp : ttempcreatenode;
  1094. begin
  1095. pt:=tcallparanode(left);
  1096. oldppt:=@left;
  1097. { flag all callparanodes that belong to the varargs }
  1098. i:=paralength;
  1099. while (i>procdefinition.maxparacount) do
  1100. begin
  1101. include(pt.callparaflags,cpf_varargs_para);
  1102. oldppt:[email protected];
  1103. pt:=tcallparanode(pt.right);
  1104. dec(i);
  1105. end;
  1106. { skip varargs that are inserted by array of const }
  1107. while assigned(pt) and
  1108. (cpf_varargs_para in pt.callparaflags) do
  1109. pt:=tcallparanode(pt.right);
  1110. { process normal parameters and insert hidden parameters }
  1111. currpara:=tparaitem(procdefinition.Para.last);
  1112. while assigned(currpara) do
  1113. begin
  1114. if currpara.is_hidden then
  1115. begin
  1116. { generate hidden tree }
  1117. used_by_callnode:=false;
  1118. hiddentree:=nil;
  1119. if (vo_is_funcret in tvarsym(currpara.parasym).varoptions) then
  1120. begin
  1121. { Generate funcretnode if not specified }
  1122. if assigned(funcretnode) then
  1123. begin
  1124. hiddentree:=funcretnode.getcopy;
  1125. end
  1126. else
  1127. begin
  1128. hiddentree:=internalstatements(newstatement);
  1129. { need to use resulttype instead of procdefinition.rettype,
  1130. because they can be different }
  1131. temp:=ctempcreatenode.create(resulttype,resulttype.def.size,tt_persistent);
  1132. addstatement(newstatement,temp);
  1133. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  1134. addstatement(newstatement,ctemprefnode.create(temp));
  1135. end;
  1136. end
  1137. else
  1138. if vo_is_high_value in tvarsym(currpara.parasym).varoptions then
  1139. begin
  1140. if not assigned(pt) then
  1141. internalerror(200304082);
  1142. { we need the information of the previous parameter }
  1143. hiddentree:=gen_high_tree(pt.left,tparaitem(currpara.previous).paratype.def);
  1144. end
  1145. else
  1146. if vo_is_self in tvarsym(currpara.parasym).varoptions then
  1147. begin
  1148. if assigned(right) then
  1149. hiddentree:=gen_self_tree_methodpointer
  1150. else
  1151. hiddentree:=gen_self_tree;
  1152. end
  1153. else
  1154. if vo_is_vmt in tvarsym(currpara.parasym).varoptions then
  1155. begin
  1156. hiddentree:=gen_vmt_tree;
  1157. end
  1158. else
  1159. if vo_is_parentfp in tvarsym(currpara.parasym).varoptions then
  1160. begin
  1161. if not(assigned(procdefinition.owner.defowner)) then
  1162. internalerror(200309287);
  1163. hiddentree:=cloadparentfpnode.create(tprocdef(procdefinition.owner.defowner));
  1164. end
  1165. {$ifdef powerpc}
  1166. else
  1167. { lib parameter has no special type but proccalloptions must be a syscall }
  1168. if (target_info.system in [system_powerpc_morphos,system_m68k_amiga]) and
  1169. (procdefinition.proccalloption=pocall_syscall) then
  1170. begin
  1171. hiddentree:=cloadnode.create(tprocdef(procdefinition).libsym,tprocdef(procdefinition).libsym.owner);
  1172. end
  1173. {$endif powerpc}
  1174. else
  1175. { add the hidden parameter }
  1176. if not assigned(hiddentree) then
  1177. internalerror(200304073);
  1178. { Already insert para and let the previous node point to
  1179. this new node }
  1180. pt:=ccallparanode.create(hiddentree,oldppt^);
  1181. pt.used_by_callnode:=used_by_callnode;
  1182. oldppt^:=pt;
  1183. end;
  1184. if not assigned(pt) then
  1185. internalerror(200310052);
  1186. pt.paraitem:=currpara;
  1187. oldppt:[email protected];
  1188. pt:=tcallparanode(pt.right);
  1189. currpara:=tparaitem(currpara.previous)
  1190. end;
  1191. { Create paraitems for varargs }
  1192. pt:=tcallparanode(left);
  1193. while assigned(pt) do
  1194. begin
  1195. if cpf_varargs_para in pt.callparaflags then
  1196. begin
  1197. if not assigned(varargsparas) then
  1198. varargsparas:=tvarargspara.create;
  1199. varargspara:=tparaitem.create;
  1200. varargspara.paratyp:=vs_value;
  1201. varargspara.paratype:=pt.resulttype;
  1202. { varargspara is left-right, use insert
  1203. instead of concat }
  1204. varargsparas.insert(varargspara);
  1205. pt.paraitem:=varargspara;
  1206. end;
  1207. pt:=tcallparanode(pt.right);
  1208. end;
  1209. end;
  1210. function tcallnode.det_resulttype:tnode;
  1211. var
  1212. candidates : tcallcandidates;
  1213. oldcallnode : tcallnode;
  1214. hpt : tnode;
  1215. pt : tcallparanode;
  1216. lastpara : longint;
  1217. currpara : tparaitem;
  1218. cand_cnt : integer;
  1219. i : longint;
  1220. method_must_be_valid,
  1221. is_const : boolean;
  1222. hp : tnode;
  1223. mptemp : ttempcreatenode;
  1224. newstatement : tstatementnode;
  1225. label
  1226. errorexit;
  1227. begin
  1228. result:=nil;
  1229. candidates:=nil;
  1230. oldcallnode:=aktcallnode;
  1231. aktcallnode:=nil;
  1232. { determine length of parameter list }
  1233. pt:=tcallparanode(left);
  1234. paralength:=0;
  1235. while assigned(pt) do
  1236. begin
  1237. inc(paralength);
  1238. pt:=tcallparanode(pt.right);
  1239. end;
  1240. { determine the type of the parameters }
  1241. if assigned(left) then
  1242. begin
  1243. tcallparanode(left).get_paratype;
  1244. if codegenerror then
  1245. goto errorexit;
  1246. end;
  1247. if assigned(methodpointer) then
  1248. begin
  1249. resulttypepass(methodpointer);
  1250. hp:=methodpointer;
  1251. while assigned(hp) and
  1252. (hp.nodetype=typeconvn) do
  1253. hp:=tunarynode(hp).left;
  1254. if assigned(hp) and
  1255. (
  1256. { call result must always be loaded in temp to prevent
  1257. double creation }
  1258. (hp.nodetype=calln)
  1259. { Also optimize also complex loads }
  1260. {$warning Complex loads can also be optimized}
  1261. // not(hp.nodetype in [typen,loadvmtaddrn,loadn])
  1262. ) then
  1263. begin
  1264. { methodpointer loading }
  1265. methodpointerinit:=internalstatements(newstatement);
  1266. mptemp:=ctempcreatenode.create_reg(methodpointer.resulttype,methodpointer.resulttype.def.size,tt_persistent);
  1267. addstatement(newstatement,mptemp);
  1268. addstatement(newstatement,cassignmentnode.create(
  1269. ctemprefnode.create(mptemp),
  1270. methodpointer));
  1271. resulttypepass(methodpointerinit);
  1272. { new methodpointer is only a temp reference }
  1273. methodpointer:=ctemprefnode.create(mptemp);
  1274. resulttypepass(methodpointer);
  1275. { methodpointer cleanup }
  1276. methodpointerdone:=ctempdeletenode.create(mptemp);
  1277. resulttypepass(methodpointerdone);
  1278. end;
  1279. end;
  1280. { procedure variable ? }
  1281. if assigned(right) then
  1282. begin
  1283. set_varstate(right,vs_used,true);
  1284. resulttypepass(right);
  1285. if codegenerror then
  1286. exit;
  1287. procdefinition:=tabstractprocdef(right.resulttype.def);
  1288. { Compare parameters from right to left }
  1289. currpara:=tparaitem(procdefinition.Para.last);
  1290. { Skip default parameters }
  1291. if not(po_varargs in procdefinition.procoptions) then
  1292. begin
  1293. { ignore hidden parameters }
  1294. while assigned(currpara) and (currpara.is_hidden) do
  1295. currpara:=tparaitem(currpara.previous);
  1296. for i:=1 to procdefinition.maxparacount-paralength do
  1297. begin
  1298. if not assigned(currpara) then
  1299. internalerror(200402261);
  1300. if not assigned(currpara.defaultvalue) then
  1301. begin
  1302. CGMessage(parser_e_wrong_parameter_size);
  1303. goto errorexit;
  1304. end;
  1305. currpara:=tparaitem(currpara.previous);
  1306. end;
  1307. end;
  1308. while assigned(currpara) and (currpara.is_hidden) do
  1309. currpara:=tparaitem(currpara.previous);
  1310. pt:=tcallparanode(left);
  1311. lastpara:=paralength;
  1312. while assigned(currpara) and assigned(pt) do
  1313. begin
  1314. { only goto next para if we're out of the varargs }
  1315. if not(po_varargs in procdefinition.procoptions) or
  1316. (lastpara<=procdefinition.maxparacount) then
  1317. begin
  1318. repeat
  1319. currpara:=tparaitem(currpara.previous);
  1320. until (not assigned(currpara)) or (not currpara.is_hidden);
  1321. end;
  1322. pt:=tcallparanode(pt.right);
  1323. dec(lastpara);
  1324. end;
  1325. if assigned(pt) or
  1326. (assigned(currpara) and
  1327. not assigned(currpara.defaultvalue)) then
  1328. begin
  1329. if assigned(pt) then
  1330. aktfilepos:=pt.fileinfo;
  1331. CGMessage(parser_e_wrong_parameter_size);
  1332. goto errorexit;
  1333. end;
  1334. end
  1335. else
  1336. { not a procedure variable }
  1337. begin
  1338. { do we know the procedure to call ? }
  1339. if not(assigned(procdefinition)) then
  1340. begin
  1341. candidates:=tcallcandidates.create(symtableprocentry,symtableproc,left,(nf_isproperty in flags));
  1342. { no procedures found? then there is something wrong
  1343. with the parameter size or the procedures are
  1344. not accessible }
  1345. if candidates.count=0 then
  1346. begin
  1347. { when it's an auto inherited call and there
  1348. is no procedure found, but the procedures
  1349. were defined with overload directive and at
  1350. least two procedures are defined then we ignore
  1351. this inherited by inserting a nothingn. Only
  1352. do this ugly hack in Delphi mode as it looks more
  1353. like a bug. It's also not documented }
  1354. if (m_delphi in aktmodeswitches) and
  1355. (cnf_anon_inherited in callnodeflags) and
  1356. (symtableprocentry.owner.symtabletype=objectsymtable) and
  1357. (po_overload in symtableprocentry.first_procdef.procoptions) and
  1358. (symtableprocentry.procdef_count>=2) then
  1359. result:=cnothingnode.create
  1360. else
  1361. begin
  1362. { in tp mode we can try to convert to procvar if
  1363. there are no parameters specified. Only try it
  1364. when there is only one proc definition, else the
  1365. loadnode will give a strange error }
  1366. if not(assigned(left)) and
  1367. not(cnf_inherited in callnodeflags) and
  1368. (m_tp_procvar in aktmodeswitches) and
  1369. (symtableprocentry.procdef_count=1) then
  1370. begin
  1371. hpt:=cloadnode.create(tprocsym(symtableprocentry),symtableproc);
  1372. if assigned(methodpointer) then
  1373. tloadnode(hpt).set_mp(methodpointer.getcopy);
  1374. resulttypepass(hpt);
  1375. result:=hpt;
  1376. end
  1377. else
  1378. begin
  1379. if assigned(left) then
  1380. aktfilepos:=left.fileinfo;
  1381. CGMessage(parser_e_wrong_parameter_size);
  1382. symtableprocentry.write_parameter_lists(nil);
  1383. end;
  1384. end;
  1385. goto errorexit;
  1386. end;
  1387. { Retrieve information about the candidates }
  1388. candidates.get_information;
  1389. {$ifdef EXTDEBUG}
  1390. { Display info when multiple candidates are found }
  1391. if candidates.count>1 then
  1392. candidates.dump_info(V_Debug);
  1393. {$endif EXTDEBUG}
  1394. { Choose the best candidate and count the number of
  1395. candidates left }
  1396. cand_cnt:=candidates.choose_best(procdefinition);
  1397. { All parameters are checked, check if there are any
  1398. procedures left }
  1399. if cand_cnt>0 then
  1400. begin
  1401. { Multiple candidates left? }
  1402. if cand_cnt>1 then
  1403. begin
  1404. CGMessage(type_e_cant_choose_overload_function);
  1405. {$ifdef EXTDEBUG}
  1406. candidates.dump_info(V_Hint);
  1407. {$else EXTDEBUG}
  1408. candidates.list(false);
  1409. {$endif EXTDEBUG}
  1410. { we'll just use the first candidate to make the
  1411. call }
  1412. end;
  1413. { assign procdefinition }
  1414. if symtableproc=nil then
  1415. symtableproc:=procdefinition.owner;
  1416. { update browser information }
  1417. if make_ref then
  1418. begin
  1419. tprocdef(procdefinition).lastref:=tref.create(tprocdef(procdefinition).lastref,@fileinfo);
  1420. inc(tprocdef(procdefinition).refcount);
  1421. if tprocdef(procdefinition).defref=nil then
  1422. tprocdef(procdefinition).defref:=tprocdef(procdefinition).lastref;
  1423. end;
  1424. end
  1425. else
  1426. begin
  1427. { No candidates left, this must be a type error,
  1428. because wrong size is already checked. procdefinition
  1429. is filled with the first (random) definition that is
  1430. found. We use this definition to display a nice error
  1431. message that the wrong type is passed }
  1432. candidates.find_wrong_para;
  1433. candidates.list(true);
  1434. {$ifdef EXTDEBUG}
  1435. candidates.dump_info(V_Hint);
  1436. {$endif EXTDEBUG}
  1437. { We can not proceed, release all procs and exit }
  1438. candidates.free;
  1439. goto errorexit;
  1440. end;
  1441. candidates.free;
  1442. end; { end of procedure to call determination }
  1443. end;
  1444. { add needed default parameters }
  1445. if assigned(procdefinition) and
  1446. (paralength<procdefinition.maxparacount) then
  1447. begin
  1448. currpara:=tparaitem(procdefinition.Para.first);
  1449. i:=0;
  1450. while (i<paralength) do
  1451. begin
  1452. if not assigned(currpara) then
  1453. internalerror(200306181);
  1454. if not currpara.is_hidden then
  1455. inc(i);
  1456. currpara:=tparaitem(currpara.next);
  1457. end;
  1458. while assigned(currpara) and currpara.is_hidden do
  1459. currpara:=tparaitem(currpara.next);
  1460. while assigned(currpara) do
  1461. begin
  1462. if not assigned(currpara.defaultvalue) then
  1463. internalerror(200212142);
  1464. left:=ccallparanode.create(genconstsymtree(tconstsym(currpara.defaultvalue)),left);
  1465. { Ignore vs_hidden parameters }
  1466. repeat
  1467. currpara:=tparaitem(currpara.next);
  1468. until (not assigned(currpara)) or (not currpara.is_hidden);
  1469. end;
  1470. end;
  1471. { handle predefined procedures }
  1472. is_const:=(po_internconst in procdefinition.procoptions) and
  1473. ((block_type in [bt_const,bt_type]) or
  1474. (assigned(left) and (tcallparanode(left).left.nodetype in [realconstn,ordconstn])));
  1475. if (procdefinition.proccalloption=pocall_internproc) or is_const then
  1476. begin
  1477. if assigned(left) then
  1478. begin
  1479. { ptr and settextbuf needs two args }
  1480. if assigned(tcallparanode(left).right) then
  1481. begin
  1482. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,left);
  1483. left:=nil;
  1484. end
  1485. else
  1486. begin
  1487. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,tcallparanode(left).left);
  1488. tcallparanode(left).left:=nil;
  1489. end;
  1490. end
  1491. else
  1492. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,nil);
  1493. result:=hpt;
  1494. goto errorexit;
  1495. end;
  1496. { ensure that the result type is set }
  1497. if not(cnf_restypeset in callnodeflags) then
  1498. begin
  1499. { constructors return their current class type, not the type where the
  1500. constructor is declared, this can be different because of inheritance }
  1501. if (procdefinition.proctypeoption=potype_constructor) and
  1502. assigned(methodpointer) and
  1503. assigned(methodpointer.resulttype.def) and
  1504. (methodpointer.resulttype.def.deftype=classrefdef) then
  1505. resulttype:=tclassrefdef(methodpointer.resulttype.def).pointertype
  1506. else
  1507. resulttype:=procdefinition.rettype;
  1508. end
  1509. else
  1510. resulttype:=restype;
  1511. {if resulttype.def.needs_inittable then
  1512. include(current_procinfo.flags,pi_needs_implicit_finally);}
  1513. if assigned(methodpointer) then
  1514. begin
  1515. { when methodpointer is a callnode we must load it first into a
  1516. temp to prevent the processing callnode twice }
  1517. if (methodpointer.nodetype=calln) then
  1518. internalerror(200405121);
  1519. { direct call to inherited abstract method, then we
  1520. can already give a error in the compiler instead
  1521. of a runtime error }
  1522. if (cnf_inherited in callnodeflags) and
  1523. (po_abstractmethod in procdefinition.procoptions) then
  1524. CGMessage(cg_e_cant_call_abstract_method);
  1525. { if an inherited con- or destructor should be }
  1526. { called in a con- or destructor then a warning }
  1527. { will be made }
  1528. { con- and destructors need a pointer to the vmt }
  1529. if (cnf_inherited in callnodeflags) and
  1530. (procdefinition.proctypeoption in [potype_constructor,potype_destructor]) and
  1531. is_object(methodpointer.resulttype.def) and
  1532. not(current_procinfo.procdef.proctypeoption in [potype_constructor,potype_destructor]) then
  1533. CGMessage(cg_w_member_cd_call_from_method);
  1534. if methodpointer.nodetype<>typen then
  1535. begin
  1536. { Remove all postfix operators }
  1537. hpt:=methodpointer;
  1538. while assigned(hpt) and (hpt.nodetype in [subscriptn,vecn]) do
  1539. hpt:=tunarynode(hpt).left;
  1540. if (procdefinition.proctypeoption=potype_constructor) and
  1541. assigned(symtableproc) and
  1542. (symtableproc.symtabletype=withsymtable) and
  1543. (tnode(twithsymtable(symtableproc).withrefnode).nodetype=temprefn) then
  1544. CGmessage(cg_e_cannot_call_cons_dest_inside_with);
  1545. { R.Init then R will be initialized by the constructor,
  1546. Also allow it for simple loads }
  1547. if (procdefinition.proctypeoption=potype_constructor) or
  1548. ((hpt.nodetype=loadn) and
  1549. (
  1550. (methodpointer.resulttype.def.deftype=classrefdef) or
  1551. (
  1552. (methodpointer.resulttype.def.deftype=objectdef) and
  1553. not(oo_has_virtual in tobjectdef(methodpointer.resulttype.def).objectoptions)
  1554. )
  1555. )
  1556. ) then
  1557. method_must_be_valid:=false
  1558. else
  1559. method_must_be_valid:=true;
  1560. set_varstate(methodpointer,vs_used,method_must_be_valid);
  1561. { The object is already used if it is called once }
  1562. if (hpt.nodetype=loadn) and
  1563. (tloadnode(hpt).symtableentry.typ=varsym) then
  1564. tvarsym(tloadnode(hpt).symtableentry).varstate:=vs_used;
  1565. end;
  1566. { if we are calling the constructor check for abstract
  1567. methods. Ignore inherited and member calls, because the
  1568. class is then already created }
  1569. if (procdefinition.proctypeoption=potype_constructor) and
  1570. not(cnf_inherited in callnodeflags) and
  1571. not(cnf_member_call in callnodeflags) then
  1572. verifyabstractcalls;
  1573. end
  1574. else
  1575. begin
  1576. { When this is method the methodpointer must be available }
  1577. if (right=nil) and
  1578. (procdefinition.owner.symtabletype=objectsymtable) then
  1579. internalerror(200305061);
  1580. end;
  1581. { Change loading of array of const to varargs }
  1582. if assigned(left) and
  1583. is_array_of_const(tparaitem(procdefinition.para.last).paratype.def) and
  1584. (procdefinition.proccalloption in [pocall_cppdecl,pocall_cdecl]) then
  1585. convert_carg_array_of_const;
  1586. { bind paraitems to the callparanodes and insert hidden parameters }
  1587. aktcallnode:=self;
  1588. bind_paraitem;
  1589. { methodpointer is only needed for virtual calls, and
  1590. it should then be loaded with the VMT }
  1591. if (po_virtualmethod in procdefinition.procoptions) and
  1592. not(assigned(methodpointer) and
  1593. (methodpointer.nodetype=typen)) then
  1594. begin
  1595. if not assigned(methodpointer) then
  1596. internalerror(200305063);
  1597. if (methodpointer.resulttype.def.deftype<>classrefdef) then
  1598. begin
  1599. methodpointer:=cloadvmtaddrnode.create(methodpointer);
  1600. resulttypepass(methodpointer);
  1601. end;
  1602. end
  1603. else
  1604. begin
  1605. { not needed anymore }
  1606. methodpointer.free;
  1607. methodpointer:=nil;
  1608. end;
  1609. { insert type conversions for parameters }
  1610. if assigned(left) then
  1611. tcallparanode(left).insert_typeconv(true);
  1612. errorexit:
  1613. aktcallnode:=oldcallnode;
  1614. end;
  1615. procedure tcallnode.order_parameters;
  1616. var
  1617. hp,hpcurr,hpnext,hpfirst,hpprev : tcallparanode;
  1618. currloc : tcgloc;
  1619. begin
  1620. hpfirst:=nil;
  1621. hpcurr:=tcallparanode(left);
  1622. while assigned(hpcurr) do
  1623. begin
  1624. { pull out }
  1625. hpnext:=tcallparanode(hpcurr.right);
  1626. { pull in at the correct place.
  1627. Used order:
  1628. 1. LOC_REFERENCE with smallest offset (x86 only)
  1629. 2. LOC_REFERENCE with most registers
  1630. 3. LOC_REGISTER with most registers
  1631. For the moment we only look at the first parameter field. Combining it
  1632. with multiple parameter fields will make things a lot complexer (PFV) }
  1633. currloc:=hpcurr.paraitem.paraloc[callerside].location^.loc;
  1634. hpprev:=nil;
  1635. hp:=hpfirst;
  1636. while assigned(hp) do
  1637. begin
  1638. case currloc of
  1639. LOC_REFERENCE :
  1640. begin
  1641. case hp.paraitem.paraloc[callerside].location^.loc of
  1642. LOC_REFERENCE :
  1643. begin
  1644. { Offset is calculated like:
  1645. sub esp,12
  1646. mov [esp+8],para3
  1647. mov [esp+4],para2
  1648. mov [esp],para1
  1649. call function
  1650. That means the for pushes the para with the
  1651. highest offset (see para3) needs to be pushed first
  1652. }
  1653. if (hpcurr.registersint>hp.registersint)
  1654. {$ifdef x86}
  1655. or (hpcurr.paraitem.paraloc[callerside].location^.reference.offset>hp.paraitem.paraloc[callerside].location^.reference.offset)
  1656. {$endif x86}
  1657. then
  1658. break;
  1659. end;
  1660. LOC_REGISTER,
  1661. LOC_FPUREGISTER :
  1662. break;
  1663. end;
  1664. end;
  1665. LOC_FPUREGISTER,
  1666. LOC_REGISTER :
  1667. begin
  1668. if (hp.paraitem.paraloc[callerside].location^.loc=currloc) and
  1669. (hpcurr.registersint>hp.registersint) then
  1670. break;
  1671. end;
  1672. end;
  1673. hpprev:=hp;
  1674. hp:=tcallparanode(hp.right);
  1675. end;
  1676. hpcurr.right:=hp;
  1677. if assigned(hpprev) then
  1678. hpprev.right:=hpcurr
  1679. else
  1680. hpfirst:=hpcurr;
  1681. { next }
  1682. hpcurr:=hpnext;
  1683. end;
  1684. left:=hpfirst;
  1685. end;
  1686. function tcallnode.replaceparaload(var n: tnode; arg: pointer): foreachnoderesult;
  1687. var
  1688. paras: tcallparanode;
  1689. temp: tnode;
  1690. begin
  1691. result := fen_false;
  1692. n.fileinfo := pfileposinfo(arg)^;
  1693. if (n.nodetype = loadn) then
  1694. begin
  1695. paras := tcallparanode(left);
  1696. while assigned(paras) and
  1697. (paras.paraitem.parasym <> tloadnode(n).symtableentry) do
  1698. paras := tcallparanode(paras.right);
  1699. if assigned(paras) then
  1700. begin
  1701. n.free;
  1702. n := paras.left.getcopy;
  1703. resulttypepass(n);
  1704. result := fen_true;
  1705. end
  1706. {$ifndef VER1_0}
  1707. else
  1708. begin
  1709. { local? }
  1710. if (tloadnode(n).symtableentry.typ <> varsym) or
  1711. (tloadnode(n).symtableentry.owner <> tprocdef(procdefinition).localst) then
  1712. exit;
  1713. if (tloadnode(n).symtableentry.indexnr > high(inlinelocals)) or
  1714. not assigned(inlinelocals[tloadnode(n).symtableentry.indexnr]) then
  1715. internalerror(20040720);
  1716. temp := inlinelocals[tloadnode(n).symtableentry.indexnr].getcopy;
  1717. n.free;
  1718. n := temp;
  1719. resulttypepass(n);
  1720. result := fen_true;
  1721. end;
  1722. {$endif ndef VER1_0}
  1723. end;
  1724. end;
  1725. {$ifndef VER1_0}
  1726. type
  1727. ptempnodes = ^ttempnodes;
  1728. ttempnodes = record
  1729. createstatement, deletestatement: tstatementnode;
  1730. end;
  1731. procedure tcallnode.createlocaltemps(p:TNamedIndexItem;arg:pointer);
  1732. var
  1733. tempinfo: ptempnodes absolute ptempnodes(arg);
  1734. tempnode: ttempcreatenode;
  1735. begin
  1736. if (tsymentry(p).typ <> varsym) then
  1737. exit;
  1738. if (p.indexnr > high(inlinelocals)) then
  1739. setlength(inlinelocals,p.indexnr+10);
  1740. {$ifndef VER1_0}
  1741. if (vo_is_funcret in tvarsym(p).varoptions) and
  1742. assigned(funcretnode) then
  1743. begin
  1744. if node_complexity(funcretnode) > 1 then
  1745. begin
  1746. { can this happen? }
  1747. { we may have to replace the funcretnode with the address of funcretnode }
  1748. { loaded in a temp in this case, because the expression may e.g. contain }
  1749. { a global variable that gets changed inside the function }
  1750. internalerror(2004072101);
  1751. end;
  1752. inlinelocals[tvarsym(p).indexnr] := funcretnode.getcopy
  1753. end
  1754. else
  1755. {$endif ndef VER1_0}
  1756. begin
  1757. if (cs_regvars in aktglobalswitches) and
  1758. (tvarsym(p).varregable<>vr_none) and
  1759. (not tvarsym(p).vartype.def.needs_inittable) then
  1760. tempnode := ctempcreatenode.create_reg(tvarsym(p).vartype,tvarsym(p).vartype.def.size,tt_persistent)
  1761. else
  1762. tempnode := ctempcreatenode.create(tvarsym(p).vartype,tvarsym(p).vartype.def.size,tt_persistent);
  1763. addstatement(tempinfo^.createstatement,tempnode);
  1764. if assigned(tvarsym(p).defaultconstsym) then
  1765. begin
  1766. { warning: duplicate from psub.pas:initializevars() -> must refactor }
  1767. addstatement(tempinfo^.createstatement,cassignmentnode.create(
  1768. ctemprefnode.create(tempnode),
  1769. cloadnode.create(tvarsym(p).defaultconstsym,tvarsym(p).defaultconstsym.owner)));
  1770. end;
  1771. if (vo_is_funcret in tvarsym(p).varoptions) then
  1772. begin
  1773. funcretnode := ctemprefnode.create(tempnode);
  1774. addstatement(tempinfo^.deletestatement,ctempdeletenode.create_normal_temp(tempnode));
  1775. end
  1776. else
  1777. addstatement(tempinfo^.deletestatement,ctempdeletenode.create(tempnode));
  1778. inlinelocals[p.indexnr] := ctemprefnode.create(tempnode);
  1779. end;
  1780. end;
  1781. {$endif ndef VER1_0}
  1782. procedure tcallnode.createinlineparas(var createstatement, deletestatement: tstatementnode);
  1783. var
  1784. para: tcallparanode;
  1785. tempnode: ttempcreatenode;
  1786. hp: tnode;
  1787. {$ifndef VER1_0}
  1788. tempnodes: ttempnodes;
  1789. {$endif ndef VER1_0}
  1790. begin
  1791. { parameters }
  1792. para := tcallparanode(left);
  1793. while assigned(para) do
  1794. begin
  1795. if (para.paraitem.parasym.typ = varsym) and
  1796. { para.left will already be the same as funcretnode in the following case, so don't change }
  1797. (not(vo_is_funcret in tvarsym(para.paraitem.parasym).varoptions) or
  1798. (not assigned(funcretnode))) then
  1799. begin
  1800. { create temps for value parameters, function result and also for }
  1801. { const parameters which are passed by value instead of by reference }
  1802. if (vo_is_funcret in tvarsym(para.paraitem.parasym).varoptions) or
  1803. (para.paraitem.paratyp = vs_value) or
  1804. ((para.paraitem.paratyp = vs_const) and
  1805. (not paramanager.push_addr_param(vs_const,para.left.resulttype.def,procdefinition.proccalloption) or
  1806. { the problem is that we can't take the address of a function result :( }
  1807. (node_complexity(para.left) >= NODE_COMPLEXITY_INF))) then
  1808. begin
  1809. if (cs_regvars in aktglobalswitches) and
  1810. (tvarsym(para.paraitem.parasym).varregable<>vr_none) and
  1811. (not tvarsym(para.paraitem.parasym).vartype.def.needs_inittable) then
  1812. tempnode := ctempcreatenode.create_reg(para.left.resulttype,para.left.resulttype.def.size,tt_persistent)
  1813. else
  1814. tempnode := ctempcreatenode.create(para.left.resulttype,para.left.resulttype.def.size,tt_persistent);
  1815. addstatement(createstatement,tempnode);
  1816. { assign the value of the parameter to the temp, except in case of the function result }
  1817. { (in that case, para.left is a block containing the creation of a new temp, while we }
  1818. { only need a temprefnode, so delete the old stuff) }
  1819. if not(vo_is_funcret in tvarsym(para.paraitem.parasym).varoptions) then
  1820. begin
  1821. addstatement(createstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  1822. para.left));
  1823. para.left := ctemprefnode.create(tempnode);
  1824. addstatement(deletestatement,ctempdeletenode.create(tempnode));
  1825. end
  1826. else
  1827. begin
  1828. if not(assigned(funcretnode)) then
  1829. funcretnode := ctemprefnode.create(tempnode);
  1830. para.left.free;
  1831. para.left := ctemprefnode.create(tempnode);
  1832. addstatement(deletestatement,ctempdeletenode.create_normal_temp(tempnode));
  1833. end
  1834. end
  1835. else if node_complexity(para.left) > 1 then
  1836. begin
  1837. if (cs_regvars in aktglobalswitches) and
  1838. not tvarsym(para.paraitem.parasym).vartype.def.needs_inittable then
  1839. tempnode := ctempcreatenode.create_reg(voidpointertype,voidpointertype.def.size,tt_persistent)
  1840. else
  1841. tempnode := ctempcreatenode.create(voidpointertype,voidpointertype.def.size,tt_persistent);
  1842. addstatement(createstatement,tempnode);
  1843. addstatement(createstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  1844. caddrnode.create(para.left)));
  1845. hp := cderefnode.create(ctemprefnode.create(tempnode));
  1846. inserttypeconv_explicit(hp,para.left.resulttype);
  1847. para.left := hp;
  1848. addstatement(deletestatement,ctempdeletenode.create(tempnode));
  1849. end;
  1850. para := tcallparanode(para.right);
  1851. end;
  1852. end;
  1853. {$ifndef VER1_0}
  1854. { local variables }
  1855. if not assigned(tprocdef(procdefinition).localst) or
  1856. (tprocdef(procdefinition).localst.symindex.count = 0) then
  1857. exit;
  1858. tempnodes.createstatement := createstatement;
  1859. tempnodes.deletestatement := deletestatement;
  1860. setlength(inlinelocals,tprocdef(procdefinition).localst.symindex.count);
  1861. tprocdef(procdefinition).localst.foreach({$ifdef FPCPROCVAR}@{$endif}createlocaltemps,@tempnodes);
  1862. createstatement := tempnodes.createstatement;
  1863. deletestatement := tempnodes.deletestatement;
  1864. {$endif ndef VER1_0}
  1865. end;
  1866. function tcallnode.pass_1 : tnode;
  1867. var
  1868. createstatement,deletestatement: tstatementnode;
  1869. createblock,deleteblock: tblocknode;
  1870. i: longint;
  1871. label
  1872. errorexit;
  1873. begin
  1874. result:=nil;
  1875. {!!!!!!!!
  1876. if (procdefinition.proccalloption=pocall_inline) and
  1877. { can we inline this procedure at the node level? }
  1878. (tprocdef(procdefinition).inlininginfo^.inlinenode) then
  1879. begin
  1880. { inherit flags }
  1881. current_procinfo.flags := current_procinfo.flags + ((procdefinition as tprocdef).inlininginfo^.flags*inherited_inlining_flags);
  1882. if assigned(methodpointer) then
  1883. CGMessage(cg_e_unable_inline_object_methods);
  1884. if assigned(right) then
  1885. CGMessage(cg_e_unable_inline_procvar);
  1886. if assigned(inlinecode) then
  1887. internalerror(2004071110);
  1888. if assigned(tprocdef(procdefinition).inlininginfo^.code) then
  1889. result:=tprocdef(procdefinition).inlininginfo^.code.getcopy
  1890. else
  1891. CGMessage(cg_e_no_code_for_inline_stored);
  1892. if assigned(result) then
  1893. begin
  1894. createblock := internalstatements(createstatement);
  1895. deleteblock := internalstatements(deletestatement);
  1896. { replace complex parameters with temps }
  1897. createinlineparas(createstatement,deletestatement);
  1898. { replace the parameter loads with the parameter values }
  1899. foreachnode(result,{$ifdef FPCPROCVAR}@{$endif}replaceparaload,@fileinfo);
  1900. { free the temps for the locals }
  1901. for i := 0 to high(inlinelocals) do
  1902. if assigned(inlinelocals[i]) then
  1903. inlinelocals[i].free;
  1904. setlength(inlinelocals,0);
  1905. addstatement(createstatement,result);
  1906. addstatement(createstatement,deleteblock);
  1907. { set function result location if necessary }
  1908. if assigned(funcretnode) and
  1909. (cnf_return_value_used in callnodeflags) then
  1910. addstatement(createstatement,funcretnode.getcopy);
  1911. result := createblock;
  1912. { consider it must not be inlined if called
  1913. again inside the args or itself }
  1914. procdefinition.proccalloption:=pocall_default;
  1915. firstpass(result);
  1916. procdefinition.proccalloption:=pocall_inline;
  1917. exit;
  1918. end;
  1919. end;
  1920. }
  1921. { calculate the parameter info for the procdef }
  1922. if not procdefinition.has_paraloc_info then
  1923. begin
  1924. procdefinition.requiredargarea:=paramanager.create_paraloc_info(procdefinition,callerside);
  1925. procdefinition.has_paraloc_info:=true;
  1926. end;
  1927. { calculate the parameter size needed for this call include varargs if they are available }
  1928. if assigned(varargsparas) then
  1929. pushedparasize:=paramanager.create_varargs_paraloc_info(procdefinition,varargsparas)
  1930. else
  1931. pushedparasize:=procdefinition.requiredargarea;
  1932. { record maximum parameter size used in this proc }
  1933. current_procinfo.allocate_push_parasize(pushedparasize);
  1934. { work trough all parameters to get the register requirements }
  1935. if assigned(left) then
  1936. tcallparanode(left).det_registers;
  1937. { order parameters }
  1938. order_parameters;
  1939. if assigned(methodpointerinit) then
  1940. firstpass(methodpointerinit);
  1941. if assigned(methodpointerdone) then
  1942. firstpass(methodpointerdone);
  1943. { function result node }
  1944. if assigned(_funcretnode) then
  1945. firstpass(_funcretnode);
  1946. { procedure variable ? }
  1947. if assigned(right) then
  1948. begin
  1949. firstpass(right);
  1950. { procedure does a call }
  1951. if not (block_type in [bt_const,bt_type]) then
  1952. include(current_procinfo.flags,pi_do_call);
  1953. end
  1954. else
  1955. { not a procedure variable }
  1956. begin
  1957. { calc the correture value for the register }
  1958. { handle predefined procedures }
  1959. if (procdefinition.proccalloption=pocall_inline) then
  1960. begin
  1961. { inherit flags }
  1962. current_procinfo.flags := current_procinfo.flags + ((procdefinition as tprocdef).inlininginfo^.flags*inherited_inlining_flags);
  1963. if assigned(methodpointer) then
  1964. CGMessage(cg_e_unable_inline_object_methods);
  1965. if assigned(right) then
  1966. CGMessage(cg_e_unable_inline_procvar);
  1967. if not assigned(inlinecode) then
  1968. begin
  1969. if assigned(tprocdef(procdefinition).inlininginfo^.code) then
  1970. inlinecode:=tprocdef(procdefinition).inlininginfo^.code.getcopy
  1971. else
  1972. CGMessage(cg_e_no_code_for_inline_stored);
  1973. if assigned(inlinecode) then
  1974. begin
  1975. { consider it has not inlined if called
  1976. again inside the args }
  1977. procdefinition.proccalloption:=pocall_default;
  1978. firstpass(inlinecode);
  1979. end;
  1980. end;
  1981. end
  1982. else
  1983. begin
  1984. if not (block_type in [bt_const,bt_type]) then
  1985. include(current_procinfo.flags,pi_do_call);
  1986. end;
  1987. end;
  1988. { implicit finally needed ? }
  1989. if resulttype.def.needs_inittable and
  1990. not paramanager.ret_in_param(resulttype.def,procdefinition.proccalloption) and
  1991. not assigned(funcretnode) then
  1992. include(current_procinfo.flags,pi_needs_implicit_finally);
  1993. { get a register for the return value }
  1994. if (not is_void(resulttype.def)) then
  1995. begin
  1996. if paramanager.ret_in_param(resulttype.def,procdefinition.proccalloption) then
  1997. begin
  1998. expectloc:=LOC_REFERENCE;
  1999. end
  2000. else
  2001. { for win32 records returned in EDX:EAX, we
  2002. move them to memory after ... }
  2003. if (resulttype.def.deftype=recorddef) then
  2004. begin
  2005. expectloc:=LOC_REFERENCE;
  2006. end
  2007. else
  2008. { ansi/widestrings must be registered, so we can dispose them }
  2009. if is_ansistring(resulttype.def) or
  2010. is_widestring(resulttype.def) then
  2011. begin
  2012. expectloc:=LOC_REFERENCE;
  2013. registersint:=1;
  2014. end
  2015. else
  2016. { we have only to handle the result if it is used }
  2017. if (cnf_return_value_used in callnodeflags) then
  2018. begin
  2019. case resulttype.def.deftype of
  2020. enumdef,
  2021. orddef :
  2022. begin
  2023. if (procdefinition.proctypeoption=potype_constructor) then
  2024. begin
  2025. expectloc:=LOC_REGISTER;
  2026. registersint:=1;
  2027. end
  2028. else
  2029. begin
  2030. expectloc:=LOC_REGISTER;
  2031. if is_64bit(resulttype.def) then
  2032. registersint:=2
  2033. else
  2034. registersint:=1;
  2035. end;
  2036. end;
  2037. floatdef :
  2038. begin
  2039. expectloc:=LOC_FPUREGISTER;
  2040. {$ifdef cpufpemu}
  2041. if (cs_fp_emulation in aktmoduleswitches) then
  2042. registersint:=1
  2043. else
  2044. {$endif cpufpemu}
  2045. {$ifdef m68k}
  2046. if (tfloatdef(resulttype.def).typ=s32real) then
  2047. registersint:=1
  2048. else
  2049. {$endif m68k}
  2050. registersfpu:=1;
  2051. end;
  2052. else
  2053. begin
  2054. expectloc:=LOC_REGISTER;
  2055. registersint:=1;
  2056. end;
  2057. end;
  2058. end
  2059. else
  2060. expectloc:=LOC_VOID;
  2061. end
  2062. else
  2063. expectloc:=LOC_VOID;
  2064. {$ifdef m68k}
  2065. { we need one more address register for virtual calls on m68k }
  2066. if (po_virtualmethod in procdefinition.procoptions) then
  2067. inc(registersint);
  2068. {$endif m68k}
  2069. { a fpu can be used in any procedure !! }
  2070. {$ifdef i386}
  2071. registersfpu:=procdefinition.fpu_used;
  2072. {$endif i386}
  2073. { if this is a call to a method calc the registers }
  2074. if (methodpointer<>nil) then
  2075. begin
  2076. if methodpointer.nodetype<>typen then
  2077. begin
  2078. firstpass(methodpointer);
  2079. registersfpu:=max(methodpointer.registersfpu,registersfpu);
  2080. registersint:=max(methodpointer.registersint,registersint);
  2081. {$ifdef SUPPORT_MMX }
  2082. registersmmx:=max(methodpointer.registersmmx,registersmmx);
  2083. {$endif SUPPORT_MMX}
  2084. end;
  2085. end;
  2086. { determine the registers of the procedure variable }
  2087. { is this OK for inlined procs also ?? (PM) }
  2088. if assigned(inlinecode) then
  2089. begin
  2090. registersfpu:=max(inlinecode.registersfpu,registersfpu);
  2091. registersint:=max(inlinecode.registersint,registersint);
  2092. {$ifdef SUPPORT_MMX}
  2093. registersmmx:=max(inlinecode.registersmmx,registersmmx);
  2094. {$endif SUPPORT_MMX}
  2095. end;
  2096. { determine the registers of the procedure variable }
  2097. { is this OK for inlined procs also ?? (PM) }
  2098. if assigned(right) then
  2099. begin
  2100. registersfpu:=max(right.registersfpu,registersfpu);
  2101. registersint:=max(right.registersint,registersint);
  2102. {$ifdef SUPPORT_MMX}
  2103. registersmmx:=max(right.registersmmx,registersmmx);
  2104. {$endif SUPPORT_MMX}
  2105. end;
  2106. { determine the registers of the procedure }
  2107. if assigned(left) then
  2108. begin
  2109. registersfpu:=max(left.registersfpu,registersfpu);
  2110. registersint:=max(left.registersint,registersint);
  2111. {$ifdef SUPPORT_MMX}
  2112. registersmmx:=max(left.registersmmx,registersmmx);
  2113. {$endif SUPPORT_MMX}
  2114. end;
  2115. errorexit:
  2116. if assigned(inlinecode) then
  2117. procdefinition.proccalloption:=pocall_inline;
  2118. end;
  2119. {$ifdef state_tracking}
  2120. function Tcallnode.track_state_pass(exec_known:boolean):boolean;
  2121. var hp:Tcallparanode;
  2122. value:Tnode;
  2123. begin
  2124. track_state_pass:=false;
  2125. hp:=Tcallparanode(left);
  2126. while assigned(hp) do
  2127. begin
  2128. if left.track_state_pass(exec_known) then
  2129. begin
  2130. left.resulttype.def:=nil;
  2131. do_resulttypepass(left);
  2132. end;
  2133. value:=aktstate.find_fact(hp.left);
  2134. if value<>nil then
  2135. begin
  2136. track_state_pass:=true;
  2137. hp.left.destroy;
  2138. hp.left:=value.getcopy;
  2139. do_resulttypepass(hp.left);
  2140. end;
  2141. hp:=Tcallparanode(hp.right);
  2142. end;
  2143. end;
  2144. {$endif}
  2145. function tcallnode.para_count:longint;
  2146. var
  2147. ppn : tcallparanode;
  2148. begin
  2149. result:=0;
  2150. ppn:=tcallparanode(left);
  2151. while assigned(ppn) do
  2152. begin
  2153. if not(assigned(ppn.paraitem) and
  2154. ppn.paraitem.is_hidden) then
  2155. inc(result);
  2156. ppn:=tcallparanode(ppn.right);
  2157. end;
  2158. end;
  2159. function tcallnode.docompare(p: tnode): boolean;
  2160. begin
  2161. docompare :=
  2162. inherited docompare(p) and
  2163. (symtableprocentry = tcallnode(p).symtableprocentry) and
  2164. (procdefinition = tcallnode(p).procdefinition) and
  2165. (methodpointer.isequal(tcallnode(p).methodpointer)) and
  2166. (((cnf_restypeset in callnodeflags) and (cnf_restypeset in tcallnode(p).callnodeflags) and
  2167. (equal_defs(restype.def,tcallnode(p).restype.def))) or
  2168. (not(cnf_restypeset in callnodeflags) and not(cnf_restypeset in tcallnode(p).callnodeflags)));
  2169. end;
  2170. procedure tcallnode.printnodedata(var t:text);
  2171. begin
  2172. if assigned(procdefinition) and
  2173. (procdefinition.deftype=procdef) then
  2174. writeln(t,printnodeindention,'proc = ',tprocdef(procdefinition).fullprocname(true))
  2175. else
  2176. begin
  2177. if assigned(symtableprocentry) then
  2178. writeln(t,printnodeindention,'proc = ',symtableprocentry.name)
  2179. else
  2180. writeln(t,printnodeindention,'proc = <nil>');
  2181. end;
  2182. printnode(t,methodpointer);
  2183. printnode(t,right);
  2184. printnode(t,left);
  2185. end;
  2186. begin
  2187. ccallnode:=tcallnode;
  2188. ccallparanode:=tcallparanode;
  2189. end.
  2190. {
  2191. $Log$
  2192. Revision 1.250 2004-10-10 20:22:53 peter
  2193. * symtable allocation rewritten
  2194. * loading of parameters to local temps/regs cleanup
  2195. * regvar support for parameters
  2196. * regvar support for staticsymtable (main body)
  2197. Revision 1.249 2004/10/08 17:09:43 peter
  2198. * tvarsym.varregable added, split vo_regable from varoptions
  2199. Revision 1.248 2004/09/21 17:25:12 peter
  2200. * paraloc branch merged
  2201. Revision 1.247 2004/09/13 20:29:00 peter
  2202. * use realname for abstract procs found
  2203. Revision 1.246.4.1 2004/08/31 20:43:06 peter
  2204. * paraloc patch
  2205. Revision 1.246 2004/08/28 20:00:50 peter
  2206. * use objrealname in Message1
  2207. Revision 1.245 2004/08/22 10:17:13 peter
  2208. * fixed crash when passing array constructor to formal parameter
  2209. Revision 1.244 2004/08/14 14:50:42 florian
  2210. * fixed several sparc alignment issues
  2211. + Jonas' inline node patch; non functional yet
  2212. Revision 1.243 2004/07/16 19:45:15 jonas
  2213. + temps can now also hold fpu values in registers (take care with use,
  2214. bacause of the x86 fpu stack)
  2215. * fpu parameters to node-inlined procedures can now also be put in
  2216. a register
  2217. Revision 1.242 2004/07/15 21:02:05 jonas
  2218. * the condition for when to use a temp in case of reference var/const
  2219. parameters was inverse
  2220. Revision 1.241 2004/07/15 19:55:39 jonas
  2221. + (incomplete) node_complexity function to assess the complexity of a
  2222. tree
  2223. + support for inlining value and const parameters at the node level
  2224. (all procedures without local variables and without formal parameters
  2225. can now be inlined at the node level)
  2226. Revision 1.240 2004/07/12 09:14:04 jonas
  2227. * inline procedures at the node tree level, but only under some very
  2228. limited circumstances for now (only procedures, and only if they have
  2229. no or only vs_out/vs_var parameters).
  2230. * fixed ppudump for inline procedures
  2231. * fixed ppudump for ppc
  2232. Revision 1.239 2004/06/20 08:55:29 florian
  2233. * logs truncated
  2234. Revision 1.238 2004/06/16 20:07:08 florian
  2235. * dwarf branch merged
  2236. Revision 1.237 2004/05/25 18:51:49 peter
  2237. * fix tcallnode.getcopy. the parameters need to be copied after
  2238. methodpointerinit is copied
  2239. Revision 1.236 2004/05/24 17:31:51 peter
  2240. * fix passing of array to open array of array (bug 3113)
  2241. Revision 1.235 2004/05/23 18:28:41 peter
  2242. * methodpointer is loaded into a temp when it was a calln
  2243. Revision 1.234 2004/05/23 15:06:20 peter
  2244. * implicit_finally flag must be set in pass1
  2245. * add check whether the implicit frame is generated when expected
  2246. }