ncal.pas 103 KB

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