ncal.pas 93 KB

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