2
0

ncal.pas 104 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642
  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. Only try it
  1495. when there is only one proc definition, else the
  1496. loadnode will give a strange error }
  1497. if not(assigned(left)) and
  1498. not(cnf_inherited in callnodeflags) and
  1499. ((m_tp_procvar in aktmodeswitches) or
  1500. (m_mac_procvar in aktmodeswitches)) and
  1501. (symtableprocentry.procdef_count=1) and
  1502. (not assigned(methodpointer) or
  1503. (methodpointer.nodetype <> typen)) then
  1504. begin
  1505. hpt:=cloadnode.create(tprocsym(symtableprocentry),symtableproc);
  1506. if assigned(methodpointer) then
  1507. tloadnode(hpt).set_mp(get_load_methodpointer);
  1508. resulttypepass(hpt);
  1509. result:=hpt;
  1510. end
  1511. else
  1512. begin
  1513. if assigned(left) then
  1514. aktfilepos:=left.fileinfo;
  1515. CGMessage(parser_e_wrong_parameter_size);
  1516. symtableprocentry.write_parameter_lists(nil);
  1517. end;
  1518. end;
  1519. goto errorexit;
  1520. end;
  1521. { Retrieve information about the candidates }
  1522. candidates.get_information;
  1523. {$ifdef EXTDEBUG}
  1524. { Display info when multiple candidates are found }
  1525. if candidates.count>1 then
  1526. candidates.dump_info(V_Debug);
  1527. {$endif EXTDEBUG}
  1528. { Choose the best candidate and count the number of
  1529. candidates left }
  1530. cand_cnt:=candidates.choose_best(procdefinition);
  1531. { All parameters are checked, check if there are any
  1532. procedures left }
  1533. if cand_cnt>0 then
  1534. begin
  1535. { Multiple candidates left? }
  1536. if cand_cnt>1 then
  1537. begin
  1538. CGMessage(type_e_cant_choose_overload_function);
  1539. {$ifdef EXTDEBUG}
  1540. candidates.dump_info(V_Hint);
  1541. {$else EXTDEBUG}
  1542. candidates.list(false);
  1543. {$endif EXTDEBUG}
  1544. { we'll just use the first candidate to make the
  1545. call }
  1546. end;
  1547. { assign procdefinition }
  1548. if symtableproc=nil then
  1549. symtableproc:=procdefinition.owner;
  1550. { update browser information }
  1551. if make_ref then
  1552. begin
  1553. tprocdef(procdefinition).lastref:=tref.create(tprocdef(procdefinition).lastref,@fileinfo);
  1554. inc(tprocdef(procdefinition).refcount);
  1555. if tprocdef(procdefinition).defref=nil then
  1556. tprocdef(procdefinition).defref:=tprocdef(procdefinition).lastref;
  1557. end;
  1558. end
  1559. else
  1560. begin
  1561. { No candidates left, this must be a type error,
  1562. because wrong size is already checked. procdefinition
  1563. is filled with the first (random) definition that is
  1564. found. We use this definition to display a nice error
  1565. message that the wrong type is passed }
  1566. candidates.find_wrong_para;
  1567. candidates.list(true);
  1568. {$ifdef EXTDEBUG}
  1569. candidates.dump_info(V_Hint);
  1570. {$endif EXTDEBUG}
  1571. { We can not proceed, release all procs and exit }
  1572. candidates.free;
  1573. goto errorexit;
  1574. end;
  1575. candidates.free;
  1576. end; { end of procedure to call determination }
  1577. end;
  1578. { check for hints (deprecated etc) }
  1579. if (procdefinition.deftype = procdef) then
  1580. check_hints(tprocdef(procdefinition).procsym,tprocdef(procdefinition).symoptions);
  1581. { add needed default parameters }
  1582. if assigned(procdefinition) and
  1583. (paralength<procdefinition.maxparacount) then
  1584. begin
  1585. paraidx:=0;
  1586. i:=0;
  1587. while (i<paralength) do
  1588. begin
  1589. if paraidx>=procdefinition.Paras.count then
  1590. internalerror(200306181);
  1591. if not(vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) then
  1592. inc(i);
  1593. inc(paraidx);
  1594. end;
  1595. while (paraidx<procdefinition.paras.count) and (vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) do
  1596. inc(paraidx);
  1597. while (paraidx<procdefinition.paras.count) do
  1598. begin
  1599. if not assigned(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym) then
  1600. internalerror(200212142);
  1601. left:=ccallparanode.create(genconstsymtree(
  1602. tconstsym(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym)),left);
  1603. { Ignore vs_hidden parameters }
  1604. repeat
  1605. inc(paraidx);
  1606. until (paraidx>=procdefinition.paras.count) or not(vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions);
  1607. end;
  1608. end;
  1609. { handle predefined procedures }
  1610. is_const:=(po_internconst in procdefinition.procoptions) and
  1611. ((block_type in [bt_const,bt_type]) or
  1612. (assigned(left) and (tcallparanode(left).left.nodetype in [realconstn,ordconstn])));
  1613. if (procdefinition.proccalloption=pocall_internproc) or is_const then
  1614. begin
  1615. if assigned(left) then
  1616. begin
  1617. { ptr and settextbuf needs two args }
  1618. if assigned(tcallparanode(left).right) then
  1619. begin
  1620. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,left);
  1621. left:=nil;
  1622. end
  1623. else
  1624. begin
  1625. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,tcallparanode(left).left);
  1626. tcallparanode(left).left:=nil;
  1627. end;
  1628. end
  1629. else
  1630. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,nil);
  1631. result:=hpt;
  1632. goto errorexit;
  1633. end;
  1634. { ensure that the result type is set }
  1635. if not(cnf_restypeset in callnodeflags) then
  1636. begin
  1637. { constructors return their current class type, not the type where the
  1638. constructor is declared, this can be different because of inheritance }
  1639. if (procdefinition.proctypeoption=potype_constructor) and
  1640. assigned(methodpointer) and
  1641. assigned(methodpointer.resulttype.def) and
  1642. (methodpointer.resulttype.def.deftype=classrefdef) then
  1643. resulttype:=tclassrefdef(methodpointer.resulttype.def).pointertype
  1644. else
  1645. { Member call to a (inherited) constructor from the class, the return
  1646. value is always self, so we change it to voidtype to generate an
  1647. error and to prevent users from generating non-working code
  1648. when they expect to clone the current instance, see bug 3662 (PFV) }
  1649. if (procdefinition.proctypeoption=potype_constructor) and
  1650. is_class(tprocdef(procdefinition)._class) and
  1651. assigned(methodpointer) and
  1652. (nf_is_self in methodpointer.flags) then
  1653. resulttype:=voidtype
  1654. else
  1655. resulttype:=procdefinition.rettype;
  1656. end
  1657. else
  1658. resulttype:=restype;
  1659. {if resulttype.def.needs_inittable then
  1660. include(current_procinfo.flags,pi_needs_implicit_finally);}
  1661. if assigned(methodpointer) then
  1662. begin
  1663. { when methodpointer is a callnode we must load it first into a
  1664. temp to prevent the processing callnode twice }
  1665. if (methodpointer.nodetype=calln) then
  1666. internalerror(200405121);
  1667. { direct call to inherited abstract method, then we
  1668. can already give a error in the compiler instead
  1669. of a runtime error }
  1670. if (cnf_inherited in callnodeflags) and
  1671. (po_abstractmethod in procdefinition.procoptions) then
  1672. CGMessage(cg_e_cant_call_abstract_method);
  1673. { if an inherited con- or destructor should be }
  1674. { called in a con- or destructor then a warning }
  1675. { will be made }
  1676. { con- and destructors need a pointer to the vmt }
  1677. if (cnf_inherited in callnodeflags) and
  1678. (procdefinition.proctypeoption in [potype_constructor,potype_destructor]) and
  1679. is_object(methodpointer.resulttype.def) and
  1680. not(current_procinfo.procdef.proctypeoption in [potype_constructor,potype_destructor]) then
  1681. CGMessage(cg_w_member_cd_call_from_method);
  1682. if methodpointer.nodetype<>typen then
  1683. begin
  1684. { Remove all postfix operators }
  1685. hpt:=methodpointer;
  1686. while assigned(hpt) and (hpt.nodetype in [subscriptn,vecn]) do
  1687. hpt:=tunarynode(hpt).left;
  1688. if (procdefinition.proctypeoption=potype_constructor) and
  1689. assigned(symtableproc) and
  1690. (symtableproc.symtabletype=withsymtable) and
  1691. (tnode(twithsymtable(symtableproc).withrefnode).nodetype=temprefn) then
  1692. CGmessage(cg_e_cannot_call_cons_dest_inside_with);
  1693. { R.Init then R will be initialized by the constructor,
  1694. Also allow it for simple loads }
  1695. if (procdefinition.proctypeoption=potype_constructor) or
  1696. ((hpt.nodetype=loadn) and
  1697. (
  1698. (methodpointer.resulttype.def.deftype=classrefdef) or
  1699. (
  1700. (methodpointer.resulttype.def.deftype=objectdef) and
  1701. not(oo_has_virtual in tobjectdef(methodpointer.resulttype.def).objectoptions)
  1702. )
  1703. )
  1704. ) then
  1705. set_varstate(methodpointer,vs_read,[])
  1706. else
  1707. set_varstate(methodpointer,vs_read,[vsf_must_be_valid]);
  1708. { The object is already used if it is called once }
  1709. if (hpt.nodetype=loadn) and
  1710. (tloadnode(hpt).symtableentry.typ in [localvarsym,paravarsym,globalvarsym]) then
  1711. set_varstate(hpt,vs_read,[]);
  1712. // tabstractvarsym(tloadnode(hpt).symtableentry).varstate:=vs_readwritten;
  1713. end;
  1714. { if we are calling the constructor check for abstract
  1715. methods. Ignore inherited and member calls, because the
  1716. class is then already created }
  1717. if (procdefinition.proctypeoption=potype_constructor) and
  1718. not(cnf_inherited in callnodeflags) and
  1719. not(cnf_member_call in callnodeflags) then
  1720. verifyabstractcalls;
  1721. end
  1722. else
  1723. begin
  1724. { When this is method the methodpointer must be available }
  1725. if (right=nil) and
  1726. (procdefinition.owner.symtabletype=objectsymtable) then
  1727. internalerror(200305061);
  1728. end;
  1729. { Set flag that the procedure uses varargs, also if they are not passed it is still
  1730. needed for x86_64 to pass the number of SSE registers used }
  1731. if po_varargs in procdefinition.procoptions then
  1732. include(callnodeflags,cnf_uses_varargs);
  1733. { Change loading of array of const to varargs }
  1734. if assigned(left) and
  1735. is_array_of_const(tparavarsym(procdefinition.paras[procdefinition.paras.count-1]).vartype.def) and
  1736. (procdefinition.proccalloption in [pocall_cppdecl,pocall_cdecl]) then
  1737. convert_carg_array_of_const;
  1738. { bind parasyms to the callparanodes and insert hidden parameters }
  1739. bind_parasym;
  1740. { insert type conversions for parameters }
  1741. if assigned(left) then
  1742. tcallparanode(left).insert_typeconv(true);
  1743. errorexit:
  1744. aktcallnode:=oldcallnode;
  1745. end;
  1746. procedure tcallnode.order_parameters;
  1747. var
  1748. hp,hpcurr,hpnext,hpfirst,hpprev : tcallparanode;
  1749. currloc : tcgloc;
  1750. begin
  1751. hpfirst:=nil;
  1752. hpcurr:=tcallparanode(left);
  1753. while assigned(hpcurr) do
  1754. begin
  1755. { pull out }
  1756. hpnext:=tcallparanode(hpcurr.right);
  1757. { pull in at the correct place.
  1758. Used order:
  1759. 1. LOC_REFERENCE with smallest offset (x86 only)
  1760. 2. LOC_REFERENCE with most registers
  1761. 3. LOC_REGISTER with most registers
  1762. For the moment we only look at the first parameter field. Combining it
  1763. with multiple parameter fields will make things a lot complexer (PFV) }
  1764. if not assigned(hpcurr.parasym.paraloc[callerside].location) then
  1765. internalerror(200412152);
  1766. currloc:=hpcurr.parasym.paraloc[callerside].location^.loc;
  1767. hpprev:=nil;
  1768. hp:=hpfirst;
  1769. while assigned(hp) do
  1770. begin
  1771. case currloc of
  1772. LOC_REFERENCE :
  1773. begin
  1774. case hp.parasym.paraloc[callerside].location^.loc of
  1775. LOC_REFERENCE :
  1776. begin
  1777. { Offset is calculated like:
  1778. sub esp,12
  1779. mov [esp+8],para3
  1780. mov [esp+4],para2
  1781. mov [esp],para1
  1782. call function
  1783. That means the for pushes the para with the
  1784. highest offset (see para3) needs to be pushed first
  1785. }
  1786. if (hpcurr.registersint>hp.registersint)
  1787. {$ifdef x86}
  1788. or (hpcurr.parasym.paraloc[callerside].location^.reference.offset>hp.parasym.paraloc[callerside].location^.reference.offset)
  1789. {$endif x86}
  1790. then
  1791. break;
  1792. end;
  1793. LOC_REGISTER,
  1794. LOC_FPUREGISTER :
  1795. break;
  1796. end;
  1797. end;
  1798. LOC_FPUREGISTER,
  1799. LOC_REGISTER :
  1800. begin
  1801. if (hp.parasym.paraloc[callerside].location^.loc=currloc) and
  1802. (hpcurr.registersint>hp.registersint) then
  1803. break;
  1804. end;
  1805. end;
  1806. hpprev:=hp;
  1807. hp:=tcallparanode(hp.right);
  1808. end;
  1809. hpcurr.right:=hp;
  1810. if assigned(hpprev) then
  1811. hpprev.right:=hpcurr
  1812. else
  1813. hpfirst:=hpcurr;
  1814. { next }
  1815. hpcurr:=hpnext;
  1816. end;
  1817. left:=hpfirst;
  1818. end;
  1819. function tcallnode.replaceparaload(var n: tnode; arg: pointer): foreachnoderesult;
  1820. var
  1821. paras: tcallparanode;
  1822. temp: tnode;
  1823. begin
  1824. result := fen_false;
  1825. n.fileinfo := pfileposinfo(arg)^;
  1826. if (n.nodetype = loadn) then
  1827. begin
  1828. case tloadnode(n).symtableentry.typ of
  1829. paravarsym :
  1830. begin
  1831. paras := tcallparanode(left);
  1832. while assigned(paras) and
  1833. (paras.parasym <> tloadnode(n).symtableentry) do
  1834. paras := tcallparanode(paras.right);
  1835. if assigned(paras) then
  1836. begin
  1837. n.free;
  1838. n := paras.left.getcopy;
  1839. resulttypepass(n);
  1840. result := fen_true;
  1841. end;
  1842. end;
  1843. localvarsym :
  1844. begin
  1845. { local? }
  1846. if (tloadnode(n).symtableentry.owner <> tprocdef(procdefinition).localst) then
  1847. exit;
  1848. if (tloadnode(n).symtableentry.indexnr >= inlinelocals.count) or
  1849. not assigned(inlinelocals[tloadnode(n).symtableentry.indexnr]) then
  1850. internalerror(20040720);
  1851. temp := tnode(inlinelocals[tloadnode(n).symtableentry.indexnr]).getcopy;
  1852. n.free;
  1853. n := temp;
  1854. resulttypepass(n);
  1855. result := fen_true;
  1856. end;
  1857. end;
  1858. end;
  1859. end;
  1860. type
  1861. ptempnodes = ^ttempnodes;
  1862. ttempnodes = record
  1863. createstatement, deletestatement: tstatementnode;
  1864. end;
  1865. procedure tcallnode.createlocaltemps(p:TNamedIndexItem;arg:pointer);
  1866. var
  1867. tempinfo: ptempnodes absolute arg;
  1868. tempnode: ttempcreatenode;
  1869. begin
  1870. if (tsymentry(p).typ <> localvarsym) then
  1871. exit;
  1872. if (p.indexnr >= inlinelocals.count) then
  1873. inlinelocals.count:=p.indexnr+10;
  1874. if (vo_is_funcret in tabstractvarsym(p).varoptions) and
  1875. assigned(funcretnode) then
  1876. begin
  1877. if node_complexity(funcretnode) > 1 then
  1878. begin
  1879. { can this happen? }
  1880. { we may have to replace the funcretnode with the address of funcretnode }
  1881. { loaded in a temp in this case, because the expression may e.g. contain }
  1882. { a global variable that gets changed inside the function }
  1883. internalerror(2004072101);
  1884. end;
  1885. inlinelocals[tabstractvarsym(p).indexnr] := funcretnode.getcopy
  1886. end
  1887. else
  1888. begin
  1889. tempnode := ctempcreatenode.create(tabstractvarsym(p).vartype,tabstractvarsym(p).vartype.def.size,tt_persistent,tabstractvarsym(p).varregable<>vr_none);
  1890. addstatement(tempinfo^.createstatement,tempnode);
  1891. if assigned(tlocalvarsym(p).defaultconstsym) then
  1892. begin
  1893. { warning: duplicate from psub.pas:initializevars() -> must refactor }
  1894. addstatement(tempinfo^.createstatement,cassignmentnode.create(
  1895. ctemprefnode.create(tempnode),
  1896. cloadnode.create(tlocalvarsym(p).defaultconstsym,tlocalvarsym(p).defaultconstsym.owner)));
  1897. end;
  1898. if (vo_is_funcret in tlocalvarsym(p).varoptions) then
  1899. begin
  1900. funcretnode := ctemprefnode.create(tempnode);
  1901. addstatement(tempinfo^.deletestatement,ctempdeletenode.create_normal_temp(tempnode));
  1902. end
  1903. else
  1904. addstatement(tempinfo^.deletestatement,ctempdeletenode.create(tempnode));
  1905. inlinelocals[p.indexnr] := ctemprefnode.create(tempnode);
  1906. end;
  1907. end;
  1908. function nonlocalvars(var n: tnode; arg: pointer): foreachnoderesult;
  1909. begin
  1910. result := fen_false;
  1911. { this is just to play it safe, there are more safe situations }
  1912. if (n.nodetype = derefn) or
  1913. ((n.nodetype = loadn) and
  1914. { globals and fields of (possibly global) objects could always be changed in the callee }
  1915. ((tloadnode(n).symtable.symtabletype in [globalsymtable,objectsymtable]) or
  1916. { statics can only be modified by functions in the same unit }
  1917. ((tloadnode(n).symtable.symtabletype = staticsymtable) and
  1918. (tloadnode(n).symtable = tsymtable(arg))))) or
  1919. ((n.nodetype = subscriptn) and
  1920. (tsubscriptnode(n).vs.owner.symtabletype = objectsymtable)) then
  1921. result := fen_norecurse_true;
  1922. end;
  1923. procedure tcallnode.createinlineparas(var createstatement, deletestatement: tstatementnode);
  1924. var
  1925. para: tcallparanode;
  1926. tempnode: ttempcreatenode;
  1927. tempnodes: ttempnodes;
  1928. n: tnode;
  1929. paracomplexity: longint;
  1930. begin
  1931. { parameters }
  1932. para := tcallparanode(left);
  1933. while assigned(para) do
  1934. begin
  1935. if (para.parasym.typ = paravarsym) and
  1936. { para.left will already be the same as funcretnode in the following case, so don't change }
  1937. (not(vo_is_funcret in tparavarsym(para.parasym).varoptions) or
  1938. (not assigned(funcretnode))) then
  1939. begin
  1940. { must take copy of para.left, because if it contains a }
  1941. { temprefn pointing to a copied temp (e.g. methodpointer), }
  1942. { then this parameter must be changed to point to the copy of }
  1943. { that temp (JM) }
  1944. n := para.left.getcopy;
  1945. para.left.free;
  1946. para.left := n;
  1947. firstpass(para.left);
  1948. { create temps for value parameters, function result and also for }
  1949. { const parameters which are passed by value instead of by reference }
  1950. { we need to take care that we use the type of the defined parameter and not of the
  1951. passed parameter, because these can be different in case of a formaldef (PFV) }
  1952. paracomplexity := node_complexity(para.left);
  1953. { check if we have to create a temp, assign the parameter's }
  1954. { contents to that temp and then substitute the paramter }
  1955. { with the temp everywhere in the function }
  1956. if
  1957. ((tparavarsym(para.parasym).varregable = vr_none) and
  1958. not(para.left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE])) or
  1959. { we can't assign to formaldef temps }
  1960. ((para.parasym.vartype.def.deftype<>formaldef) and
  1961. (
  1962. { if paracomplexity > 1, we normally take the address of }
  1963. { the parameter expression, store it in a temp and }
  1964. { substitute the dereferenced temp in the inlined function }
  1965. { We can't do this if we can't take the address of the }
  1966. { parameter expression, so in that case assign to a temp }
  1967. ((paracomplexity > 1) and
  1968. (not valid_for_addr(para.left,false) or
  1969. (para.left.nodetype = calln) or
  1970. is_constnode(para.left))) or
  1971. { the problem is that we can't take the address of a function result :( }
  1972. (vo_is_funcret in tparavarsym(para.parasym).varoptions) or
  1973. { we do not need to create a temp for value parameters }
  1974. { which are not modified in the inlined function }
  1975. { const parameters can get vs_readwritten if their }
  1976. { address is taken }
  1977. ((((para.parasym.varspez = vs_value) and
  1978. (para.parasym.varstate in [vs_initialised,vs_declared,vs_read])) or
  1979. { in case of const, this is only necessary if the }
  1980. { variable would be passed by value normally, or if }
  1981. { there is such a variable somewhere in an expression }
  1982. ((para.parasym.varspez = vs_const) and
  1983. (not paramanager.push_addr_param(vs_const,para.parasym.vartype.def,procdefinition.proccalloption) or
  1984. (paracomplexity > 1)))) and
  1985. { however, if we pass a global variable, an object field or}
  1986. { an expression containing a pointer dereference as }
  1987. { parameter, this value could be modified in other ways as }
  1988. { well and in such cases create a temp to be on the safe }
  1989. { side }
  1990. foreachnodestatic(para.left,@nonlocalvars,pointer(symtableproc))) or
  1991. { value parameters of which we know they are modified by }
  1992. { definition have to be copied to a temp }
  1993. ((para.parasym.varspez = vs_value) and
  1994. not(para.parasym.varstate in [vs_initialised,vs_declared,vs_read])) or
  1995. { the compiler expects that it can take the address of parameters passed by reference in
  1996. the case of const so we can't replace the node simply by a constant node
  1997. When playing with this code, ensure that
  1998. function f(const a,b : longint) : longint;inline;
  1999. begin
  2000. result:=a*b;
  2001. end;
  2002. [...]
  2003. ...:=f(10,20));
  2004. [...]
  2005. is still folded. (FK)
  2006. }
  2007. ((para.parasym.varspez = vs_const) and
  2008. { const para's can get vs_readwritten if their address }
  2009. { is taken }
  2010. ((para.parasym.varstate = vs_readwritten) or
  2011. { call-by-reference const's may need to be passed by }
  2012. { reference to function called in the inlined code }
  2013. (paramanager.push_addr_param(vs_const,para.parasym.vartype.def,procdefinition.proccalloption) and
  2014. (not valid_for_addr(para.left,false) or
  2015. is_constnode(para.left)))))
  2016. )
  2017. ) then
  2018. begin
  2019. { in theory, this is always regable, but ncgcall can't }
  2020. { handle it yet in all situations (JM) }
  2021. tempnode := ctempcreatenode.create(para.parasym.vartype,para.parasym.vartype.def.size,tt_persistent,tparavarsym(para.parasym).varregable <> vr_none);
  2022. addstatement(createstatement,tempnode);
  2023. { assign the value of the parameter to the temp, except in case of the function result }
  2024. { (in that case, para.left is a block containing the creation of a new temp, while we }
  2025. { only need a temprefnode, so delete the old stuff) }
  2026. if not(vo_is_funcret in tparavarsym(para.parasym).varoptions) then
  2027. begin
  2028. addstatement(createstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  2029. para.left));
  2030. para.left := ctemprefnode.create(tempnode);
  2031. addstatement(deletestatement,ctempdeletenode.create(tempnode));
  2032. end
  2033. else
  2034. begin
  2035. if not(assigned(funcretnode)) then
  2036. funcretnode := ctemprefnode.create(tempnode);
  2037. para.left.free;
  2038. para.left := ctemprefnode.create(tempnode);
  2039. addstatement(deletestatement,ctempdeletenode.create_normal_temp(tempnode));
  2040. end
  2041. end
  2042. { otherwise if the parameter is "complex", take the address }
  2043. { of the parameter expression, store it in a temp and replace }
  2044. { occurrences of the parameter with dereferencings of this }
  2045. { temp }
  2046. else if (paracomplexity > 1) then
  2047. begin
  2048. tempnode := ctempcreatenode.create(voidpointertype,voidpointertype.def.size,tt_persistent,tparavarsym(para.parasym).varregable<>vr_none);
  2049. addstatement(createstatement,tempnode);
  2050. addstatement(createstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  2051. caddrnode.create_internal(para.left)));
  2052. para.left := ctypeconvnode.create_internal(cderefnode.create(ctemprefnode.create(tempnode)),para.left.resulttype);
  2053. addstatement(deletestatement,ctempdeletenode.create(tempnode));
  2054. end;
  2055. end;
  2056. para := tcallparanode(para.right);
  2057. end;
  2058. { local variables }
  2059. if not assigned(tprocdef(procdefinition).localst) or
  2060. (tprocdef(procdefinition).localst.symindex.count = 0) then
  2061. exit;
  2062. tempnodes.createstatement := createstatement;
  2063. tempnodes.deletestatement := deletestatement;
  2064. inlinelocals.count:=tprocdef(procdefinition).localst.symindex.count;
  2065. tprocdef(procdefinition).localst.foreach(@createlocaltemps,@tempnodes);
  2066. createstatement := tempnodes.createstatement;
  2067. deletestatement := tempnodes.deletestatement;
  2068. end;
  2069. function tcallnode.pass1_inline:tnode;
  2070. var
  2071. createstatement,deletestatement: tstatementnode;
  2072. createblock,deleteblock: tblocknode;
  2073. body : tnode;
  2074. begin
  2075. if not(assigned(tprocdef(procdefinition).inlininginfo) and
  2076. assigned(tprocdef(procdefinition).inlininginfo^.code)) then
  2077. internalerror(200412021);
  2078. { inherit flags }
  2079. current_procinfo.flags := current_procinfo.flags + ((procdefinition as tprocdef).inlininginfo^.flags*inherited_inlining_flags);
  2080. { create blocks for loading/deleting of local data }
  2081. createblock:=internalstatements(createstatement);
  2082. deleteblock:=internalstatements(deletestatement);
  2083. { add methodpointer init code to init statement }
  2084. { (fini must be done later, as it will delete the hookoncopy info) }
  2085. if assigned(methodpointerinit) then
  2086. addstatement(createstatement,methodpointerinit.getcopy);
  2087. inlinelocals:=TFPObjectList.create(true);
  2088. { get copy of the procedure body }
  2089. body:=tprocdef(procdefinition).inlininginfo^.code.getcopy;
  2090. { replace complex parameters with temps }
  2091. createinlineparas(createstatement,deletestatement);
  2092. { replace the parameter loads with the parameter values }
  2093. foreachnode(body,@replaceparaload,@fileinfo);
  2094. { copy methodpointer fini code }
  2095. if assigned(methodpointerdone) then
  2096. addstatement(deletestatement,methodpointerdone.getcopy);
  2097. { free the temps for the locals }
  2098. inlinelocals.free;
  2099. inlinelocals:=nil;
  2100. addstatement(createstatement,body);
  2101. addstatement(createstatement,deleteblock);
  2102. { set function result location if necessary }
  2103. if assigned(funcretnode) and
  2104. (cnf_return_value_used in callnodeflags) then
  2105. addstatement(createstatement,funcretnode.getcopy);
  2106. { consider it must not be inlined if called
  2107. again inside the args or itself }
  2108. exclude(procdefinition.procoptions,po_inline);
  2109. dosimplify(createblock);
  2110. firstpass(createblock);
  2111. include(procdefinition.procoptions,po_inline);
  2112. { return inlined block }
  2113. result := createblock;
  2114. {$ifdef DEBUGINLINE}
  2115. writeln;
  2116. writeln('**************************',tprocdef(procdefinition).mangledname);
  2117. printnode(output,result);
  2118. {$endif DEBUGINLINE}
  2119. end;
  2120. procedure tcallnode.check_stack_parameters;
  2121. var
  2122. hp : tcallparanode;
  2123. begin
  2124. hp:=tcallparanode(left);
  2125. while assigned(hp) do
  2126. begin
  2127. if assigned(hp.parasym) and
  2128. assigned(hp.parasym.paraloc[callerside].location) and
  2129. (hp.parasym.paraloc[callerside].location^.loc=LOC_REFERENCE) then
  2130. include(current_procinfo.flags,pi_has_stackparameter);
  2131. hp:=tcallparanode(hp.right);
  2132. end;
  2133. end;
  2134. function tcallnode.pass_1 : tnode;
  2135. var
  2136. st : tsymtable;
  2137. begin
  2138. result:=nil;
  2139. { Can we inline the procedure? }
  2140. if ([po_inline,po_has_inlininginfo] <= procdefinition.procoptions) then
  2141. begin
  2142. { Check if we can inline the procedure when it references proc/var that
  2143. are not in the globally available }
  2144. st:=procdefinition.owner;
  2145. if (st.symtabletype=objectsymtable) then
  2146. st:=st.defowner.owner;
  2147. if (pi_uses_static_symtable in tprocdef(procdefinition).inlininginfo^.flags) and
  2148. (st.symtabletype=globalsymtable) and
  2149. (not st.iscurrentunit) then
  2150. begin
  2151. Comment(V_lineinfo+V_Debug,'Not inlining "'+tprocdef(procdefinition).procsym.realname+'", references static symtable');
  2152. end
  2153. else
  2154. begin
  2155. result:=pass1_inline;
  2156. exit;
  2157. end;
  2158. end;
  2159. { calculate the parameter info for the procdef }
  2160. if not procdefinition.has_paraloc_info then
  2161. begin
  2162. procdefinition.requiredargarea:=paramanager.create_paraloc_info(procdefinition,callerside);
  2163. procdefinition.has_paraloc_info:=true;
  2164. end;
  2165. { calculate the parameter size needed for this call include varargs if they are available }
  2166. if assigned(varargsparas) then
  2167. pushedparasize:=paramanager.create_varargs_paraloc_info(procdefinition,varargsparas)
  2168. else
  2169. pushedparasize:=procdefinition.requiredargarea;
  2170. { record maximum parameter size used in this proc }
  2171. current_procinfo.allocate_push_parasize(pushedparasize);
  2172. { work trough all parameters to get the register requirements }
  2173. if assigned(left) then
  2174. begin
  2175. tcallparanode(left).det_registers;
  2176. if cs_opt_level1 in aktoptimizerswitches then
  2177. begin
  2178. { check for stacked parameters }
  2179. check_stack_parameters;
  2180. end;
  2181. end;
  2182. { order parameters }
  2183. order_parameters;
  2184. if assigned(methodpointerinit) then
  2185. firstpass(methodpointerinit);
  2186. if assigned(methodpointerdone) then
  2187. firstpass(methodpointerdone);
  2188. { function result node }
  2189. if assigned(_funcretnode) then
  2190. firstpass(_funcretnode);
  2191. { procedure variable ? }
  2192. if assigned(right) then
  2193. firstpass(right);
  2194. if not (block_type in [bt_const,bt_type]) then
  2195. include(current_procinfo.flags,pi_do_call);
  2196. { implicit finally needed ? }
  2197. if resulttype.def.needs_inittable and
  2198. not paramanager.ret_in_param(resulttype.def,procdefinition.proccalloption) and
  2199. not assigned(funcretnode) then
  2200. include(current_procinfo.flags,pi_needs_implicit_finally);
  2201. { get a register for the return value }
  2202. if (not is_void(resulttype.def)) then
  2203. begin
  2204. if paramanager.ret_in_param(resulttype.def,procdefinition.proccalloption) then
  2205. begin
  2206. expectloc:=LOC_REFERENCE;
  2207. end
  2208. else
  2209. { for win32 records returned in EDX:EAX, we
  2210. move them to memory after ... }
  2211. if (resulttype.def.deftype=recorddef) then
  2212. begin
  2213. expectloc:=LOC_REFERENCE;
  2214. end
  2215. else
  2216. { ansi/widestrings must be registered, so we can dispose them }
  2217. if is_ansistring(resulttype.def) or
  2218. is_widestring(resulttype.def) then
  2219. begin
  2220. expectloc:=LOC_REFERENCE;
  2221. registersint:=1;
  2222. end
  2223. else
  2224. { we have only to handle the result if it is used }
  2225. if (cnf_return_value_used in callnodeflags) then
  2226. begin
  2227. case resulttype.def.deftype of
  2228. enumdef,
  2229. orddef :
  2230. begin
  2231. if (procdefinition.proctypeoption=potype_constructor) then
  2232. begin
  2233. expectloc:=LOC_REGISTER;
  2234. registersint:=1;
  2235. end
  2236. else
  2237. begin
  2238. expectloc:=LOC_REGISTER;
  2239. if is_64bit(resulttype.def) then
  2240. registersint:=2
  2241. else
  2242. registersint:=1;
  2243. end;
  2244. end;
  2245. floatdef :
  2246. begin
  2247. expectloc:=LOC_FPUREGISTER;
  2248. {$ifdef cpufpemu}
  2249. if (cs_fp_emulation in aktmoduleswitches) then
  2250. registersint:=1
  2251. else
  2252. {$endif cpufpemu}
  2253. {$ifdef m68k}
  2254. if (tfloatdef(resulttype.def).typ=s32real) then
  2255. registersint:=1
  2256. else
  2257. {$endif m68k}
  2258. registersfpu:=1;
  2259. end;
  2260. else
  2261. begin
  2262. expectloc:=LOC_REGISTER;
  2263. registersint:=1;
  2264. end;
  2265. end;
  2266. end
  2267. else
  2268. expectloc:=LOC_VOID;
  2269. end
  2270. else
  2271. expectloc:=LOC_VOID;
  2272. {$ifdef m68k}
  2273. { we need one more address register for virtual calls on m68k }
  2274. if (po_virtualmethod in procdefinition.procoptions) then
  2275. inc(registersint);
  2276. {$endif m68k}
  2277. { a fpu can be used in any procedure !! }
  2278. {$ifdef i386}
  2279. registersfpu:=procdefinition.fpu_used;
  2280. {$endif i386}
  2281. { if this is a call to a method calc the registers }
  2282. if (methodpointer<>nil) then
  2283. begin
  2284. if methodpointer.nodetype<>typen then
  2285. begin
  2286. firstpass(methodpointer);
  2287. registersfpu:=max(methodpointer.registersfpu,registersfpu);
  2288. registersint:=max(methodpointer.registersint,registersint);
  2289. {$ifdef SUPPORT_MMX }
  2290. registersmmx:=max(methodpointer.registersmmx,registersmmx);
  2291. {$endif SUPPORT_MMX}
  2292. end;
  2293. end;
  2294. { determine the registers of the procedure variable }
  2295. { is this OK for inlined procs also ?? (PM) }
  2296. if assigned(right) then
  2297. begin
  2298. registersfpu:=max(right.registersfpu,registersfpu);
  2299. registersint:=max(right.registersint,registersint);
  2300. {$ifdef SUPPORT_MMX}
  2301. registersmmx:=max(right.registersmmx,registersmmx);
  2302. {$endif SUPPORT_MMX}
  2303. end;
  2304. { determine the registers of the procedure }
  2305. if assigned(left) then
  2306. begin
  2307. registersfpu:=max(left.registersfpu,registersfpu);
  2308. registersint:=max(left.registersint,registersint);
  2309. {$ifdef SUPPORT_MMX}
  2310. registersmmx:=max(left.registersmmx,registersmmx);
  2311. {$endif SUPPORT_MMX}
  2312. end;
  2313. end;
  2314. {$ifdef state_tracking}
  2315. function Tcallnode.track_state_pass(exec_known:boolean):boolean;
  2316. var hp:Tcallparanode;
  2317. value:Tnode;
  2318. begin
  2319. track_state_pass:=false;
  2320. hp:=Tcallparanode(left);
  2321. while assigned(hp) do
  2322. begin
  2323. if left.track_state_pass(exec_known) then
  2324. begin
  2325. left.resulttype.def:=nil;
  2326. do_resulttypepass(left);
  2327. end;
  2328. value:=aktstate.find_fact(hp.left);
  2329. if value<>nil then
  2330. begin
  2331. track_state_pass:=true;
  2332. hp.left.destroy;
  2333. hp.left:=value.getcopy;
  2334. do_resulttypepass(hp.left);
  2335. end;
  2336. hp:=Tcallparanode(hp.right);
  2337. end;
  2338. end;
  2339. {$endif}
  2340. function tcallnode.para_count:longint;
  2341. var
  2342. ppn : tcallparanode;
  2343. begin
  2344. result:=0;
  2345. ppn:=tcallparanode(left);
  2346. while assigned(ppn) do
  2347. begin
  2348. if not(assigned(ppn.parasym) and
  2349. (vo_is_hidden_para in ppn.parasym.varoptions)) then
  2350. inc(result);
  2351. ppn:=tcallparanode(ppn.right);
  2352. end;
  2353. end;
  2354. function tcallnode.get_load_methodpointer:tnode;
  2355. var
  2356. newstatement : tstatementnode;
  2357. begin
  2358. if assigned(methodpointerinit) then
  2359. begin
  2360. result:=internalstatements(newstatement);
  2361. addstatement(newstatement,methodpointerinit);
  2362. addstatement(newstatement,methodpointer);
  2363. addstatement(newstatement,methodpointerdone);
  2364. methodpointerinit:=nil;
  2365. methodpointer:=nil;
  2366. methodpointerdone:=nil;
  2367. end
  2368. else
  2369. begin
  2370. result:=methodpointer;
  2371. methodpointer:=nil;
  2372. end;
  2373. end;
  2374. function tcallnode.docompare(p: tnode): boolean;
  2375. begin
  2376. docompare :=
  2377. inherited docompare(p) and
  2378. (symtableprocentry = tcallnode(p).symtableprocentry) and
  2379. (procdefinition = tcallnode(p).procdefinition) and
  2380. (methodpointer.isequal(tcallnode(p).methodpointer)) and
  2381. (((cnf_restypeset in callnodeflags) and (cnf_restypeset in tcallnode(p).callnodeflags) and
  2382. (equal_defs(restype.def,tcallnode(p).restype.def))) or
  2383. (not(cnf_restypeset in callnodeflags) and not(cnf_restypeset in tcallnode(p).callnodeflags)));
  2384. end;
  2385. procedure tcallnode.printnodedata(var t:text);
  2386. begin
  2387. if assigned(procdefinition) and
  2388. (procdefinition.deftype=procdef) then
  2389. writeln(t,printnodeindention,'proc = ',tprocdef(procdefinition).fullprocname(true))
  2390. else
  2391. begin
  2392. if assigned(symtableprocentry) then
  2393. writeln(t,printnodeindention,'proc = ',symtableprocentry.name)
  2394. else
  2395. writeln(t,printnodeindention,'proc = <nil>');
  2396. end;
  2397. printnode(t,methodpointer);
  2398. printnode(t,right);
  2399. printnode(t,left);
  2400. end;
  2401. begin
  2402. ccallnode:=tcallnode;
  2403. ccallparanode:=tcallparanode;
  2404. end.