ncal.pas 112 KB

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