ncal.pas 101 KB

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