ncal.pas 103 KB

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