ncal.pas 129 KB

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