ncal.pas 129 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257
  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. (parasym.vardef.typ<>procvardef) 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. first : boolean;
  1712. begin
  1713. objectdf := nil;
  1714. { verify if trying to create an instance of a class which contains
  1715. non-implemented abstract methods }
  1716. { first verify this class type, no class than exit }
  1717. { also, this checking can only be done if the constructor is directly
  1718. called, indirect constructor calls cannot be checked.
  1719. }
  1720. if assigned(methodpointer) and
  1721. not (nf_is_self in methodpointer.flags) then
  1722. begin
  1723. if (methodpointer.resultdef.typ = objectdef) then
  1724. objectdf:=tobjectdef(methodpointer.resultdef)
  1725. else
  1726. if (methodpointer.resultdef.typ = classrefdef) and
  1727. (tclassrefdef(methodpointer.resultdef).pointeddef.typ = objectdef) and
  1728. (methodpointer.nodetype in [typen,loadvmtaddrn]) then
  1729. objectdf:=tobjectdef(tclassrefdef(methodpointer.resultdef).pointeddef);
  1730. end;
  1731. if not assigned(objectdf) then
  1732. exit;
  1733. parents := tlinkedlist.create;
  1734. AbstractMethodsList := TFPHashList.create;
  1735. { insert all parents in this class : the first item in the
  1736. list will be the base parent of the class .
  1737. }
  1738. while assigned(objectdf) do
  1739. begin
  1740. objectinfo:=tobjectinfoitem.create(objectdf);
  1741. parents.insert(objectinfo);
  1742. objectdf := objectdf.childof;
  1743. end;
  1744. { now all parents are in the correct order
  1745. insert all abstract methods in the list, and remove
  1746. those which are overriden by parent classes.
  1747. }
  1748. objectinfo:=tobjectinfoitem(parents.first);
  1749. while assigned(objectinfo) do
  1750. begin
  1751. objectdf := objectinfo.objinfo;
  1752. if assigned(objectdf.symtable) then
  1753. objectdf.symtable.SymList.ForEachCall(@verifyabstract,nil);
  1754. objectinfo:=tobjectinfoitem(objectinfo.next);
  1755. end;
  1756. if assigned(parents) then
  1757. parents.free;
  1758. { Finally give out a warning for each abstract method still in the list }
  1759. first:=true;
  1760. for i:=0 to AbstractMethodsList.Count-1 do
  1761. begin
  1762. pd:=tprocdef(AbstractMethodsList[i]);
  1763. if po_abstractmethod in pd.procoptions then
  1764. begin
  1765. if first then
  1766. begin
  1767. Message1(type_w_instance_with_abstract,objectdf.objrealname^);
  1768. first:=false;
  1769. end;
  1770. MessagePos1(pd.fileinfo,sym_h_abstract_method_list,pd.fullprocname(true));
  1771. end;
  1772. end;
  1773. if assigned(AbstractMethodsList) then
  1774. AbstractMethodsList.Free;
  1775. end;
  1776. procedure tcallnode.convert_carg_array_of_const;
  1777. var
  1778. hp : tarrayconstructornode;
  1779. oldleft : tcallparanode;
  1780. begin
  1781. oldleft:=tcallparanode(left);
  1782. if oldleft.left.nodetype<>arrayconstructorn then
  1783. begin
  1784. CGMessage1(type_e_wrong_type_in_array_constructor,oldleft.left.resultdef.typename);
  1785. exit;
  1786. end;
  1787. include(callnodeflags,cnf_uses_varargs);
  1788. { Get arrayconstructor node and insert typeconvs }
  1789. hp:=tarrayconstructornode(oldleft.left);
  1790. { Add c args parameters }
  1791. { It could be an empty set }
  1792. if assigned(hp) and
  1793. assigned(hp.left) then
  1794. begin
  1795. while assigned(hp) do
  1796. begin
  1797. left:=ccallparanode.create(hp.left,left);
  1798. { set callparanode resultdef and flags }
  1799. left.resultdef:=hp.left.resultdef;
  1800. include(tcallparanode(left).callparaflags,cpf_varargs_para);
  1801. hp.left:=nil;
  1802. hp:=tarrayconstructornode(hp.right);
  1803. end;
  1804. end;
  1805. { Remove value of old array of const parameter, but keep it
  1806. in the list because it is required for bind_parasym.
  1807. Generate a nothign to keep callparanoed.left valid }
  1808. oldleft.left.free;
  1809. oldleft.left:=cnothingnode.create;
  1810. end;
  1811. procedure tcallnode.bind_parasym;
  1812. type
  1813. pcallparanode = ^tcallparanode;
  1814. var
  1815. i : integer;
  1816. pt : tcallparanode;
  1817. oldppt : pcallparanode;
  1818. varargspara,
  1819. currpara : tparavarsym;
  1820. hiddentree : tnode;
  1821. begin
  1822. pt:=tcallparanode(left);
  1823. oldppt:=pcallparanode(@left);
  1824. { flag all callparanodes that belong to the varargs }
  1825. i:=paralength;
  1826. while (i>procdefinition.maxparacount) do
  1827. begin
  1828. include(pt.callparaflags,cpf_varargs_para);
  1829. oldppt:=pcallparanode(@pt.right);
  1830. pt:=tcallparanode(pt.right);
  1831. dec(i);
  1832. end;
  1833. { skip varargs that are inserted by array of const }
  1834. while assigned(pt) and
  1835. (cpf_varargs_para in pt.callparaflags) do
  1836. pt:=tcallparanode(pt.right);
  1837. { process normal parameters and insert hidden parameter nodes, the content
  1838. of the hidden parameters will be updated in pass1 }
  1839. for i:=procdefinition.paras.count-1 downto 0 do
  1840. begin
  1841. currpara:=tparavarsym(procdefinition.paras[i]);
  1842. if vo_is_hidden_para in currpara.varoptions then
  1843. begin
  1844. { Here we handle only the parameters that depend on
  1845. the types of the previous parameter. The typeconversion
  1846. can change the type in the next step. For example passing
  1847. an array can be change to a pointer and a deref }
  1848. if vo_is_high_para in currpara.varoptions then
  1849. begin
  1850. if not assigned(pt) or (i=0) then
  1851. internalerror(200304081);
  1852. { we need the information of the previous parameter }
  1853. hiddentree:=gen_high_tree(pt.left,tparavarsym(procdefinition.paras[i-1]).vardef);
  1854. end
  1855. else
  1856. if vo_is_typinfo_para in currpara.varoptions then
  1857. begin
  1858. if not assigned(pt) or (i=0) then
  1859. internalerror(200304082);
  1860. hiddentree:=caddrnode.create_internal(
  1861. crttinode.create(Tstoreddef(pt.resultdef),fullrtti,rdt_normal)
  1862. );
  1863. end
  1864. else
  1865. hiddentree:=cnothingnode.create;
  1866. pt:=ccallparanode.create(hiddentree,oldppt^);
  1867. oldppt^:=pt;
  1868. end;
  1869. if not assigned(pt) then
  1870. internalerror(200310052);
  1871. pt.parasym:=currpara;
  1872. oldppt:=pcallparanode(@pt.right);
  1873. pt:=tcallparanode(pt.right);
  1874. end;
  1875. { Create parasyms for varargs, first count the number of varargs paras,
  1876. then insert the parameters with numbering in reverse order. The SortParas
  1877. will set the correct order at the end}
  1878. pt:=tcallparanode(left);
  1879. i:=0;
  1880. while assigned(pt) do
  1881. begin
  1882. if cpf_varargs_para in pt.callparaflags then
  1883. inc(i);
  1884. pt:=tcallparanode(pt.right);
  1885. end;
  1886. if (i>0) then
  1887. begin
  1888. varargsparas:=tvarargsparalist.create;
  1889. pt:=tcallparanode(left);
  1890. while assigned(pt) do
  1891. begin
  1892. if cpf_varargs_para in pt.callparaflags then
  1893. begin
  1894. varargspara:=tparavarsym.create('va'+tostr(i),i,vs_value,pt.resultdef,[]);
  1895. dec(i);
  1896. { varargspara is left-right, use insert
  1897. instead of concat }
  1898. varargsparas.add(varargspara);
  1899. pt.parasym:=varargspara;
  1900. end;
  1901. pt:=tcallparanode(pt.right);
  1902. end;
  1903. varargsparas.sortparas;
  1904. end;
  1905. end;
  1906. function tcallnode.pass_typecheck:tnode;
  1907. var
  1908. candidates : tcallcandidates;
  1909. oldcallnode : tcallnode;
  1910. hpt : tnode;
  1911. pt : tcallparanode;
  1912. lastpara : longint;
  1913. paraidx,
  1914. cand_cnt : integer;
  1915. i : longint;
  1916. is_const : boolean;
  1917. statements : tstatementnode;
  1918. converted_result_data : ttempcreatenode;
  1919. label
  1920. errorexit;
  1921. begin
  1922. result:=nil;
  1923. candidates:=nil;
  1924. oldcallnode:=aktcallnode;
  1925. aktcallnode:=self;
  1926. { determine length of parameter list }
  1927. pt:=tcallparanode(left);
  1928. paralength:=0;
  1929. while assigned(pt) do
  1930. begin
  1931. inc(paralength);
  1932. pt:=tcallparanode(pt.right);
  1933. end;
  1934. { determine the type of the parameters }
  1935. if assigned(left) then
  1936. begin
  1937. tcallparanode(left).get_paratype;
  1938. if codegenerror then
  1939. goto errorexit;
  1940. end;
  1941. if assigned(methodpointer) then
  1942. typecheckpass(methodpointer);
  1943. { procedure variable ? }
  1944. if assigned(right) then
  1945. begin
  1946. set_varstate(right,vs_read,[vsf_must_be_valid]);
  1947. typecheckpass(right);
  1948. if codegenerror then
  1949. exit;
  1950. procdefinition:=tabstractprocdef(right.resultdef);
  1951. { Compare parameters from right to left }
  1952. paraidx:=procdefinition.Paras.count-1;
  1953. { Skip default parameters }
  1954. if not(po_varargs in procdefinition.procoptions) then
  1955. begin
  1956. { ignore hidden parameters }
  1957. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) do
  1958. dec(paraidx);
  1959. for i:=1 to procdefinition.maxparacount-paralength do
  1960. begin
  1961. if paraidx<0 then
  1962. internalerror(200402261);
  1963. if not assigned(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym) then
  1964. begin
  1965. CGMessage1(parser_e_wrong_parameter_size,'<Procedure Variable>');
  1966. goto errorexit;
  1967. end;
  1968. dec(paraidx);
  1969. end;
  1970. end;
  1971. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) do
  1972. dec(paraidx);
  1973. pt:=tcallparanode(left);
  1974. lastpara:=paralength;
  1975. while (paraidx>=0) and assigned(pt) do
  1976. begin
  1977. { only goto next para if we're out of the varargs }
  1978. if not(po_varargs in procdefinition.procoptions) or
  1979. (lastpara<=procdefinition.maxparacount) then
  1980. begin
  1981. repeat
  1982. dec(paraidx);
  1983. until (paraidx<0) or not(vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions);
  1984. end;
  1985. pt:=tcallparanode(pt.right);
  1986. dec(lastpara);
  1987. end;
  1988. if assigned(pt) or
  1989. ((paraidx>=0) and
  1990. not assigned(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym)) then
  1991. begin
  1992. if assigned(pt) then
  1993. current_filepos:=pt.fileinfo;
  1994. CGMessage1(parser_e_wrong_parameter_size,'<Procedure Variable>');
  1995. goto errorexit;
  1996. end;
  1997. end
  1998. else
  1999. { not a procedure variable }
  2000. begin
  2001. { do we know the procedure to call ? }
  2002. if not(assigned(procdefinition)) then
  2003. begin
  2004. candidates:=tcallcandidates.create(symtableprocentry,symtableproc,left,(nf_isproperty in flags),
  2005. { ignore possible private in delphi mode for anon. inherited (FK) }
  2006. (m_delphi in current_settings.modeswitches) and (cnf_anon_inherited in callnodeflags));
  2007. { no procedures found? then there is something wrong
  2008. with the parameter size or the procedures are
  2009. not accessible }
  2010. if candidates.count=0 then
  2011. begin
  2012. { when it's an auto inherited call and there
  2013. is no procedure found, but the procedures
  2014. were defined with overload directive and at
  2015. least two procedures are defined then we ignore
  2016. this inherited by inserting a nothingn. Only
  2017. do this ugly hack in Delphi mode as it looks more
  2018. like a bug. It's also not documented }
  2019. if (m_delphi in current_settings.modeswitches) and
  2020. (cnf_anon_inherited in callnodeflags) and
  2021. (symtableprocentry.owner.symtabletype=ObjectSymtable) and
  2022. (po_overload in tprocdef(symtableprocentry.ProcdefList[0]).procoptions) and
  2023. (symtableprocentry.ProcdefList.Count>=2) then
  2024. result:=cnothingnode.create
  2025. else
  2026. begin
  2027. { in tp mode we can try to convert to procvar if
  2028. there are no parameters specified }
  2029. if not(assigned(left)) and
  2030. not(cnf_inherited in callnodeflags) and
  2031. ((m_tp_procvar in current_settings.modeswitches) or
  2032. (m_mac_procvar in current_settings.modeswitches)) and
  2033. (not assigned(methodpointer) or
  2034. (methodpointer.nodetype <> typen)) then
  2035. begin
  2036. hpt:=cloadnode.create(tprocsym(symtableprocentry),symtableproc);
  2037. if assigned(methodpointer) then
  2038. tloadnode(hpt).set_mp(methodpointer.getcopy);
  2039. typecheckpass(hpt);
  2040. result:=hpt;
  2041. end
  2042. else
  2043. begin
  2044. if assigned(left) then
  2045. current_filepos:=left.fileinfo;
  2046. CGMessage1(parser_e_wrong_parameter_size,symtableprocentry.realname);
  2047. symtableprocentry.write_parameter_lists(nil);
  2048. end;
  2049. end;
  2050. candidates.free;
  2051. goto errorexit;
  2052. end;
  2053. { Retrieve information about the candidates }
  2054. candidates.get_information;
  2055. {$ifdef EXTDEBUG}
  2056. { Display info when multiple candidates are found }
  2057. if candidates.count>1 then
  2058. candidates.dump_info(V_Debug);
  2059. {$endif EXTDEBUG}
  2060. { Choose the best candidate and count the number of
  2061. candidates left }
  2062. cand_cnt:=candidates.choose_best(procdefinition,
  2063. assigned(left) and
  2064. not assigned(tcallparanode(left).right) and
  2065. (tcallparanode(left).left.resultdef.typ=variantdef));
  2066. { All parameters are checked, check if there are any
  2067. procedures left }
  2068. if cand_cnt>0 then
  2069. begin
  2070. { Multiple candidates left? }
  2071. if cand_cnt>1 then
  2072. begin
  2073. CGMessage(type_e_cant_choose_overload_function);
  2074. {$ifdef EXTDEBUG}
  2075. candidates.dump_info(V_Hint);
  2076. {$else EXTDEBUG}
  2077. candidates.list(false);
  2078. {$endif EXTDEBUG}
  2079. { we'll just use the first candidate to make the
  2080. call }
  2081. end;
  2082. { assign procdefinition }
  2083. if symtableproc=nil then
  2084. symtableproc:=procdefinition.owner;
  2085. end
  2086. else
  2087. begin
  2088. { No candidates left, this must be a type error,
  2089. because wrong size is already checked. procdefinition
  2090. is filled with the first (random) definition that is
  2091. found. We use this definition to display a nice error
  2092. message that the wrong type is passed }
  2093. candidates.find_wrong_para;
  2094. candidates.list(true);
  2095. {$ifdef EXTDEBUG}
  2096. candidates.dump_info(V_Hint);
  2097. {$endif EXTDEBUG}
  2098. { We can not proceed, release all procs and exit }
  2099. candidates.free;
  2100. goto errorexit;
  2101. end;
  2102. candidates.free;
  2103. end; { end of procedure to call determination }
  2104. end;
  2105. { check for hints (deprecated etc) }
  2106. if (procdefinition.typ = procdef) then
  2107. check_hints(tprocdef(procdefinition).procsym,tprocdef(procdefinition).symoptions);
  2108. { add needed default parameters }
  2109. if assigned(procdefinition) and
  2110. (paralength<procdefinition.maxparacount) then
  2111. begin
  2112. paraidx:=0;
  2113. i:=0;
  2114. while (i<paralength) do
  2115. begin
  2116. if paraidx>=procdefinition.Paras.count then
  2117. internalerror(200306181);
  2118. if not(vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) then
  2119. inc(i);
  2120. inc(paraidx);
  2121. end;
  2122. while (paraidx<procdefinition.paras.count) and (vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) do
  2123. inc(paraidx);
  2124. while (paraidx<procdefinition.paras.count) do
  2125. begin
  2126. if not assigned(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym) then
  2127. internalerror(200212142);
  2128. left:=ccallparanode.create(genconstsymtree(
  2129. tconstsym(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym)),left);
  2130. { Ignore vs_hidden parameters }
  2131. repeat
  2132. inc(paraidx);
  2133. until (paraidx>=procdefinition.paras.count) or not(vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions);
  2134. end;
  2135. end;
  2136. { recursive call? }
  2137. if assigned(current_procinfo) and
  2138. (procdefinition=current_procinfo.procdef) then
  2139. include(current_procinfo.flags,pi_is_recursive);
  2140. { handle predefined procedures }
  2141. is_const:=(po_internconst in procdefinition.procoptions) and
  2142. ((block_type in [bt_const,bt_type]) or
  2143. (assigned(left) and (tcallparanode(left).left.nodetype in [realconstn,ordconstn])));
  2144. if (procdefinition.proccalloption=pocall_internproc) or is_const then
  2145. begin
  2146. if assigned(left) then
  2147. begin
  2148. { ptr and settextbuf needs two args }
  2149. if assigned(tcallparanode(left).right) then
  2150. begin
  2151. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,left);
  2152. left:=nil;
  2153. end
  2154. else
  2155. begin
  2156. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,tcallparanode(left).left);
  2157. tcallparanode(left).left:=nil;
  2158. end;
  2159. end
  2160. else
  2161. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,nil);
  2162. result:=hpt;
  2163. goto errorexit;
  2164. end;
  2165. { ensure that the result type is set }
  2166. if not(cnf_typedefset in callnodeflags) then
  2167. begin
  2168. { constructors return their current class type, not the type where the
  2169. constructor is declared, this can be different because of inheritance }
  2170. if (procdefinition.proctypeoption=potype_constructor) and
  2171. assigned(methodpointer) and
  2172. assigned(methodpointer.resultdef) and
  2173. (methodpointer.resultdef.typ=classrefdef) then
  2174. resultdef:=tclassrefdef(methodpointer.resultdef).pointeddef
  2175. else
  2176. { Member call to a (inherited) constructor from the class, the return
  2177. value is always self, so we change it to voidtype to generate an
  2178. error and to prevent users from generating non-working code
  2179. when they expect to clone the current instance, see bug 3662 (PFV) }
  2180. if (procdefinition.proctypeoption=potype_constructor) and
  2181. is_class(tprocdef(procdefinition)._class) and
  2182. assigned(methodpointer) and
  2183. (nf_is_self in methodpointer.flags) then
  2184. resultdef:=voidtype
  2185. else
  2186. resultdef:=procdefinition.returndef;
  2187. end
  2188. else
  2189. resultdef:=typedef;
  2190. { Check object/class for methods }
  2191. if assigned(methodpointer) then
  2192. begin
  2193. { direct call to inherited abstract method, then we
  2194. can already give a error in the compiler instead
  2195. of a runtime error }
  2196. if (cnf_inherited in callnodeflags) and
  2197. (po_abstractmethod in procdefinition.procoptions) then
  2198. begin
  2199. if (m_delphi in current_settings.modeswitches) and
  2200. (cnf_anon_inherited in callnodeflags) then
  2201. begin
  2202. CGMessage(cg_h_inherited_ignored);
  2203. result:=cnothingnode.create;
  2204. exit;
  2205. end
  2206. else
  2207. CGMessage(cg_e_cant_call_abstract_method);
  2208. end;
  2209. { if an inherited con- or destructor should be }
  2210. { called in a con- or destructor then a warning }
  2211. { will be made }
  2212. { con- and destructors need a pointer to the vmt }
  2213. if (cnf_inherited in callnodeflags) and
  2214. (procdefinition.proctypeoption in [potype_constructor,potype_destructor]) and
  2215. is_object(methodpointer.resultdef) and
  2216. not(current_procinfo.procdef.proctypeoption in [potype_constructor,potype_destructor]) then
  2217. CGMessage(cg_w_member_cd_call_from_method);
  2218. if methodpointer.nodetype<>typen then
  2219. begin
  2220. { Remove all postfix operators }
  2221. hpt:=methodpointer;
  2222. while assigned(hpt) and (hpt.nodetype in [subscriptn,vecn]) do
  2223. hpt:=tunarynode(hpt).left;
  2224. if (procdefinition.proctypeoption=potype_constructor) and
  2225. assigned(symtableproc) and
  2226. (symtableproc.symtabletype=withsymtable) and
  2227. (tnode(twithsymtable(symtableproc).withrefnode).nodetype=temprefn) then
  2228. CGmessage(cg_e_cannot_call_cons_dest_inside_with);
  2229. { R.Init then R will be initialized by the constructor,
  2230. Also allow it for simple loads }
  2231. if (procdefinition.proctypeoption=potype_constructor) or
  2232. ((hpt.nodetype=loadn) and
  2233. (methodpointer.resultdef.typ=objectdef) and
  2234. not(oo_has_virtual in tobjectdef(methodpointer.resultdef).objectoptions)
  2235. ) then
  2236. { a constructor will and a method may write something to }
  2237. { the fields }
  2238. set_varstate(methodpointer,vs_readwritten,[])
  2239. else
  2240. set_varstate(methodpointer,vs_read,[vsf_must_be_valid]);
  2241. end;
  2242. { if we are calling the constructor check for abstract
  2243. methods. Ignore inherited and member calls, because the
  2244. class is then already created }
  2245. if (procdefinition.proctypeoption=potype_constructor) and
  2246. not(cnf_inherited in callnodeflags) and
  2247. not(cnf_member_call in callnodeflags) then
  2248. verifyabstractcalls;
  2249. end
  2250. else
  2251. begin
  2252. { When this is method the methodpointer must be available }
  2253. if (right=nil) and
  2254. (procdefinition.owner.symtabletype=ObjectSymtable) then
  2255. internalerror(200305061);
  2256. end;
  2257. { Set flag that the procedure uses varargs, also if they are not passed it is still
  2258. needed for x86_64 to pass the number of SSE registers used }
  2259. if po_varargs in procdefinition.procoptions then
  2260. include(callnodeflags,cnf_uses_varargs);
  2261. { Change loading of array of const to varargs }
  2262. if assigned(left) and
  2263. is_array_of_const(tparavarsym(procdefinition.paras[procdefinition.paras.count-1]).vardef) and
  2264. (procdefinition.proccalloption in [pocall_cppdecl,pocall_cdecl]) then
  2265. convert_carg_array_of_const;
  2266. { bind parasyms to the callparanodes and insert hidden parameters }
  2267. bind_parasym;
  2268. { insert type conversions for parameters }
  2269. if assigned(left) then
  2270. tcallparanode(left).insert_typeconv;
  2271. { dispinterface methode invoke? }
  2272. if assigned(methodpointer) and is_dispinterface(methodpointer.resultdef) then
  2273. begin
  2274. { if the result is used, we've to insert a call to convert the type to be on the "safe side" }
  2275. if cnf_return_value_used in callnodeflags then
  2276. begin
  2277. result:=internalstatements(statements);
  2278. converted_result_data:=ctempcreatenode.create(procdefinition.returndef,sizeof(procdefinition.returndef),tt_persistent,true);
  2279. addstatement(statements,converted_result_data);
  2280. addstatement(statements,cassignmentnode.create(ctemprefnode.create(converted_result_data),
  2281. ctypeconvnode.create_internal(translate_disp_call(methodpointer,parameters,nil,'',tprocdef(procdefinition).dispid,true),
  2282. procdefinition.returndef)));
  2283. addstatement(statements,ctempdeletenode.create_normal_temp(converted_result_data));
  2284. addstatement(statements,ctemprefnode.create(converted_result_data));
  2285. end
  2286. else
  2287. result:=translate_disp_call(methodpointer,parameters,nil,'',tprocdef(procdefinition).dispid,false);
  2288. { don't free reused nodes }
  2289. methodpointer:=nil;
  2290. parameters:=nil;
  2291. end;
  2292. errorexit:
  2293. aktcallnode:=oldcallnode;
  2294. end;
  2295. procedure tcallnode.order_parameters;
  2296. var
  2297. hp,hpcurr,hpnext,hpfirst,hpprev : tcallparanode;
  2298. currloc : tcgloc;
  2299. begin
  2300. hpfirst:=nil;
  2301. hpcurr:=tcallparanode(left);
  2302. while assigned(hpcurr) do
  2303. begin
  2304. { pull out }
  2305. hpnext:=tcallparanode(hpcurr.right);
  2306. { pull in at the correct place.
  2307. Used order:
  2308. 1. LOC_REFERENCE with smallest offset (i386 only)
  2309. 2. LOC_REFERENCE with least complexity (non-i386 only)
  2310. 3. LOC_REFERENCE with most complexity (non-i386 only)
  2311. 4. LOC_REGISTER with most complexity
  2312. 5. LOC_REGISTER with least complexity
  2313. For the moment we only look at the first parameter field. Combining it
  2314. with multiple parameter fields will make things a lot complexer (PFV)
  2315. The reason for the difference regarding complexity ordering
  2316. between LOC_REFERENCE and LOC_REGISTER is mainly for calls:
  2317. we first want to treat the LOC_REFERENCE destinations whose
  2318. calculation does not require a call, because their location
  2319. may contain registers which might otherwise have to be saved
  2320. if a call has to be evaluated first. The calculated value is
  2321. stored on the stack and will thus no longer occupy any
  2322. register.
  2323. Similarly, for the register parameters we first want to
  2324. evaluate the calls, because otherwise the already loaded
  2325. register parameters will have to be saved so the intermediate
  2326. call can be evaluated (JM) }
  2327. if not assigned(hpcurr.parasym.paraloc[callerside].location) then
  2328. internalerror(200412152);
  2329. currloc:=hpcurr.parasym.paraloc[callerside].location^.loc;
  2330. hpprev:=nil;
  2331. hp:=hpfirst;
  2332. while assigned(hp) do
  2333. begin
  2334. case currloc of
  2335. LOC_REFERENCE :
  2336. begin
  2337. case hp.parasym.paraloc[callerside].location^.loc of
  2338. LOC_REFERENCE :
  2339. begin
  2340. { Offset is calculated like:
  2341. sub esp,12
  2342. mov [esp+8],para3
  2343. mov [esp+4],para2
  2344. mov [esp],para1
  2345. call function
  2346. That means the for pushes the para with the
  2347. highest offset (see para3) needs to be pushed first
  2348. }
  2349. {$ifdef i386}
  2350. { the i386 code generator expects all reference }
  2351. { parameter to be in this order so it can use }
  2352. { pushes }
  2353. if (hpcurr.parasym.paraloc[callerside].location^.reference.offset>hp.parasym.paraloc[callerside].location^.reference.offset) then
  2354. {$else i386}
  2355. if (node_complexity(hpcurr)<node_complexity(hp)) then
  2356. {$endif i386}
  2357. break;
  2358. end;
  2359. LOC_MMREGISTER,
  2360. LOC_REGISTER,
  2361. LOC_FPUREGISTER :
  2362. break;
  2363. end;
  2364. end;
  2365. LOC_MMREGISTER,
  2366. LOC_FPUREGISTER,
  2367. LOC_REGISTER :
  2368. begin
  2369. if (hp.parasym.paraloc[callerside].location^.loc<>LOC_REFERENCE) and
  2370. (node_complexity(hpcurr)>node_complexity(hp)) then
  2371. break;
  2372. end;
  2373. end;
  2374. hpprev:=hp;
  2375. hp:=tcallparanode(hp.right);
  2376. end;
  2377. hpcurr.right:=hp;
  2378. if assigned(hpprev) then
  2379. hpprev.right:=hpcurr
  2380. else
  2381. hpfirst:=hpcurr;
  2382. { next }
  2383. hpcurr:=hpnext;
  2384. end;
  2385. left:=hpfirst;
  2386. end;
  2387. procedure tcallnode.check_stack_parameters;
  2388. var
  2389. hp : tcallparanode;
  2390. begin
  2391. hp:=tcallparanode(left);
  2392. while assigned(hp) do
  2393. begin
  2394. if assigned(hp.parasym) and
  2395. assigned(hp.parasym.paraloc[callerside].location) and
  2396. (hp.parasym.paraloc[callerside].location^.loc=LOC_REFERENCE) then
  2397. include(current_procinfo.flags,pi_has_stackparameter);
  2398. hp:=tcallparanode(hp.right);
  2399. end;
  2400. end;
  2401. procedure tcallnode.check_inlining;
  2402. var
  2403. st : tsymtable;
  2404. para : tcallparanode;
  2405. begin
  2406. { Can we inline the procedure? }
  2407. if ([po_inline,po_has_inlininginfo] <= procdefinition.procoptions) then
  2408. begin
  2409. include(callnodeflags,cnf_do_inline);
  2410. { Check if we can inline the procedure when it references proc/var that
  2411. are not in the globally available }
  2412. st:=procdefinition.owner;
  2413. if (st.symtabletype=ObjectSymtable) then
  2414. st:=st.defowner.owner;
  2415. if (pi_uses_static_symtable in tprocdef(procdefinition).inlininginfo^.flags) and
  2416. (st.symtabletype=globalsymtable) and
  2417. (not st.iscurrentunit) then
  2418. begin
  2419. Comment(V_lineinfo+V_Debug,'Not inlining "'+tprocdef(procdefinition).procsym.realname+'", references static symtable');
  2420. exclude(callnodeflags,cnf_do_inline);
  2421. end;
  2422. para:=tcallparanode(parameters);
  2423. while assigned(para) do
  2424. begin
  2425. if not para.can_be_inlined then
  2426. begin
  2427. Comment(V_lineinfo+V_Debug,'Not inlining "'+tprocdef(procdefinition).procsym.realname+'", invocation parameter contains an unsafe/unsupported construct');
  2428. exclude(callnodeflags,cnf_do_inline);
  2429. break;
  2430. end;
  2431. para:=tcallparanode(para.nextpara);
  2432. end;
  2433. end;
  2434. end;
  2435. function tcallnode.pass_1 : tnode;
  2436. begin
  2437. result:=nil;
  2438. { Check if the call can be inlined, sets the cnf_do_inline flag }
  2439. check_inlining;
  2440. { Maybe optimize the loading of the methodpointer using a temp. When the methodpointer
  2441. is a calln this is even required to not execute the calln twice.
  2442. This needs to be done after the resulttype pass, because in the resulttype we can still convert the
  2443. calln to a loadn (PFV) }
  2444. if assigned(methodpointer) then
  2445. maybe_load_in_temp(methodpointer);
  2446. { Create destination (temp or assignment-variable reuse) for function result if it not yet set }
  2447. maybe_create_funcret_node;
  2448. { Insert the self,vmt,function result in the parameters }
  2449. gen_hidden_parameters;
  2450. { Remove useless nodes from init/final blocks }
  2451. { (simplify depends on typecheck info) }
  2452. if assigned(callinitblock) then
  2453. begin
  2454. typecheckpass(callinitblock);
  2455. dosimplify(callinitblock);
  2456. end;
  2457. if assigned(callcleanupblock) then
  2458. begin
  2459. typecheckpass(callcleanupblock);
  2460. dosimplify(callcleanupblock);
  2461. end;
  2462. { Continue with checking a normal call or generate the inlined code }
  2463. if cnf_do_inline in callnodeflags then
  2464. result:=pass1_inline
  2465. else
  2466. result:=pass1_normal;
  2467. end;
  2468. function tcallnode.pass1_normal : tnode;
  2469. begin
  2470. result:=nil;
  2471. { calculate the parameter info for the procdef }
  2472. if not procdefinition.has_paraloc_info then
  2473. begin
  2474. procdefinition.requiredargarea:=paramanager.create_paraloc_info(procdefinition,callerside);
  2475. procdefinition.has_paraloc_info:=true;
  2476. end;
  2477. { calculate the parameter size needed for this call include varargs if they are available }
  2478. if assigned(varargsparas) then
  2479. pushedparasize:=paramanager.create_varargs_paraloc_info(procdefinition,varargsparas)
  2480. else
  2481. pushedparasize:=procdefinition.requiredargarea;
  2482. { record maximum parameter size used in this proc }
  2483. current_procinfo.allocate_push_parasize(pushedparasize);
  2484. { check for stacked parameters }
  2485. if assigned(left) and
  2486. (current_settings.optimizerswitches*[cs_opt_stackframe,cs_opt_level1]<>[]) then
  2487. check_stack_parameters;
  2488. if assigned(callinitblock) then
  2489. firstpass(callinitblock);
  2490. { function result node (tempref or simple load) }
  2491. if assigned(funcretnode) then
  2492. firstpass(funcretnode);
  2493. { parameters }
  2494. if assigned(left) then
  2495. tcallparanode(left).firstcallparan;
  2496. { procedure variable ? }
  2497. if assigned(right) then
  2498. firstpass(right);
  2499. if assigned(methodpointer) and
  2500. (methodpointer.nodetype<>typen) then
  2501. firstpass(methodpointer);
  2502. if assigned(callcleanupblock) then
  2503. firstpass(callcleanupblock);
  2504. if not (block_type in [bt_const,bt_type]) then
  2505. include(current_procinfo.flags,pi_do_call);
  2506. { order parameters }
  2507. order_parameters;
  2508. { get a register for the return value }
  2509. if (not is_void(resultdef)) then
  2510. begin
  2511. if paramanager.ret_in_param(resultdef,procdefinition.proccalloption) then
  2512. begin
  2513. expectloc:=LOC_REFERENCE;
  2514. end
  2515. else
  2516. { ansi/widestrings must be registered, so we can dispose them }
  2517. if is_ansistring(resultdef) or
  2518. is_widestring(resultdef) then
  2519. begin
  2520. expectloc:=LOC_REFERENCE;
  2521. end
  2522. else
  2523. { we have only to handle the result if it is used }
  2524. if (cnf_return_value_used in callnodeflags) then
  2525. begin
  2526. case resultdef.typ of
  2527. enumdef,
  2528. orddef :
  2529. begin
  2530. expectloc:=LOC_REGISTER;
  2531. end;
  2532. floatdef :
  2533. begin
  2534. expectloc:=LOC_FPUREGISTER;
  2535. end;
  2536. else
  2537. begin
  2538. expectloc:=procdefinition.funcretloc[callerside].loc;
  2539. end;
  2540. end;
  2541. end
  2542. else
  2543. expectloc:=LOC_VOID;
  2544. end
  2545. else
  2546. expectloc:=LOC_VOID;
  2547. end;
  2548. {$ifdef state_tracking}
  2549. function Tcallnode.track_state_pass(exec_known:boolean):boolean;
  2550. var hp:Tcallparanode;
  2551. value:Tnode;
  2552. begin
  2553. track_state_pass:=false;
  2554. hp:=Tcallparanode(left);
  2555. while assigned(hp) do
  2556. begin
  2557. if left.track_state_pass(exec_known) then
  2558. begin
  2559. left.resultdef:=nil;
  2560. do_typecheckpass(left);
  2561. end;
  2562. value:=aktstate.find_fact(hp.left);
  2563. if value<>nil then
  2564. begin
  2565. track_state_pass:=true;
  2566. hp.left.destroy;
  2567. hp.left:=value.getcopy;
  2568. do_typecheckpass(hp.left);
  2569. end;
  2570. hp:=Tcallparanode(hp.right);
  2571. end;
  2572. end;
  2573. {$endif}
  2574. {**************************************************************************
  2575. INLINING SUPPORT
  2576. **************************************************************************}
  2577. function tcallnode.replaceparaload(var n: tnode; arg: pointer): foreachnoderesult;
  2578. var
  2579. paras: tcallparanode;
  2580. temp: tnode;
  2581. indexnr : integer;
  2582. begin
  2583. result := fen_false;
  2584. n.fileinfo := pfileposinfo(arg)^;
  2585. if (n.nodetype = loadn) then
  2586. begin
  2587. case tloadnode(n).symtableentry.typ of
  2588. paravarsym :
  2589. begin
  2590. paras := tcallparanode(left);
  2591. while assigned(paras) and
  2592. (paras.parasym <> tloadnode(n).symtableentry) do
  2593. paras := tcallparanode(paras.right);
  2594. if assigned(paras) then
  2595. begin
  2596. n.free;
  2597. n := paras.left.getcopy;
  2598. typecheckpass(n);
  2599. result := fen_true;
  2600. end;
  2601. end;
  2602. localvarsym :
  2603. begin
  2604. { local? }
  2605. if (tloadnode(n).symtableentry.owner <> tprocdef(procdefinition).localst) then
  2606. exit;
  2607. indexnr:=tloadnode(n).symtableentry.owner.SymList.IndexOf(tloadnode(n).symtableentry);
  2608. if (indexnr >= inlinelocals.count) or
  2609. not assigned(inlinelocals[indexnr]) then
  2610. internalerror(20040720);
  2611. temp := tnode(inlinelocals[indexnr]).getcopy;
  2612. n.free;
  2613. n := temp;
  2614. typecheckpass(n);
  2615. result := fen_true;
  2616. end;
  2617. end;
  2618. end;
  2619. end;
  2620. procedure tcallnode.createlocaltemps(p:TObject;arg:pointer);
  2621. var
  2622. tempnode: ttempcreatenode;
  2623. indexnr : integer;
  2624. begin
  2625. if (TSym(p).typ <> localvarsym) then
  2626. exit;
  2627. indexnr:=TSym(p).Owner.SymList.IndexOf(p);
  2628. if (indexnr >= inlinelocals.count) then
  2629. inlinelocals.count:=indexnr+10;
  2630. if (vo_is_funcret in tabstractvarsym(p).varoptions) then
  2631. begin
  2632. if not assigned(funcretnode) then
  2633. internalerror(200709081);
  2634. inlinelocals[indexnr] := funcretnode.getcopy
  2635. end
  2636. else
  2637. begin
  2638. tempnode :=ctempcreatenode.create(tabstractvarsym(p).vardef,tabstractvarsym(p).vardef.size,tt_persistent,tabstractvarsym(p).is_regvar(false));
  2639. addstatement(inlineinitstatement,tempnode);
  2640. addstatement(inlinecleanupstatement,ctempdeletenode.create(tempnode));
  2641. { inherit addr_taken flag }
  2642. if (tabstractvarsym(p).addr_taken) then
  2643. include(tempnode.tempinfo^.flags,ti_addr_taken);
  2644. inlinelocals[indexnr] := ctemprefnode.create(tempnode);
  2645. end;
  2646. end;
  2647. function nonlocalvars(var n: tnode; arg: pointer): foreachnoderesult;
  2648. begin
  2649. result := fen_false;
  2650. { this is just to play it safe, there are more safe situations }
  2651. if (n.nodetype = derefn) or
  2652. ((n.nodetype = loadn) and
  2653. { globals and fields of (possibly global) objects could always be changed in the callee }
  2654. ((tloadnode(n).symtable.symtabletype in [globalsymtable,ObjectSymtable]) or
  2655. { statics can only be modified by functions in the same unit }
  2656. ((tloadnode(n).symtable.symtabletype = staticsymtable) and
  2657. (tloadnode(n).symtable = TSymtable(arg))))) or
  2658. ((n.nodetype = subscriptn) and
  2659. (tsubscriptnode(n).vs.owner.symtabletype = ObjectSymtable)) then
  2660. result := fen_norecurse_true;
  2661. end;
  2662. procedure tcallnode.createinlineparas;
  2663. var
  2664. para: tcallparanode;
  2665. tempnode: ttempcreatenode;
  2666. n: tnode;
  2667. paracomplexity: longint;
  2668. begin
  2669. { parameters }
  2670. para := tcallparanode(left);
  2671. while assigned(para) do
  2672. begin
  2673. if (para.parasym.typ = paravarsym) then
  2674. begin
  2675. { must take copy of para.left, because if it contains a }
  2676. { temprefn pointing to a copied temp (e.g. methodpointer), }
  2677. { then this parameter must be changed to point to the copy of }
  2678. { that temp (JM) }
  2679. n := para.left.getcopy;
  2680. para.left.free;
  2681. para.left := n;
  2682. firstpass(para.left);
  2683. { create temps for value parameters, function result and also for }
  2684. { const parameters which are passed by value instead of by reference }
  2685. { we need to take care that we use the type of the defined parameter and not of the
  2686. passed parameter, because these can be different in case of a formaldef (PFV) }
  2687. paracomplexity := node_complexity(para.left);
  2688. { check if we have to create a temp, assign the parameter's }
  2689. { contents to that temp and then substitute the paramter }
  2690. { with the temp everywhere in the function }
  2691. if
  2692. ((tparavarsym(para.parasym).varregable in [vr_none,vr_addr]) and
  2693. not(para.left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE])) or
  2694. { we can't assign to formaldef temps }
  2695. ((para.parasym.vardef.typ<>formaldef) and
  2696. (
  2697. { if paracomplexity > 1, we normally take the address of }
  2698. { the parameter expression, store it in a temp and }
  2699. { substitute the dereferenced temp in the inlined function }
  2700. { We can't do this if we can't take the address of the }
  2701. { parameter expression, so in that case assign to a temp }
  2702. not(para.left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CONSTANT]) or
  2703. ((paracomplexity > 1) and
  2704. (not valid_for_addr(para.left,false) or
  2705. (para.left.nodetype = calln) or
  2706. is_constnode(para.left))) or
  2707. { we do not need to create a temp for value parameters }
  2708. { which are not modified in the inlined function }
  2709. { const parameters can get vs_readwritten if their }
  2710. { address is taken }
  2711. ((((para.parasym.varspez = vs_value) and
  2712. (para.parasym.varstate in [vs_initialised,vs_declared,vs_read])) or
  2713. { in case of const, this is only necessary if the }
  2714. { variable would be passed by value normally, or if }
  2715. { there is such a variable somewhere in an expression }
  2716. ((para.parasym.varspez = vs_const) and
  2717. (not paramanager.push_addr_param(vs_const,para.parasym.vardef,procdefinition.proccalloption) or
  2718. (paracomplexity > 1)))) and
  2719. { however, if we pass a global variable, an object field or}
  2720. { an expression containing a pointer dereference as }
  2721. { parameter, this value could be modified in other ways as }
  2722. { well and in such cases create a temp to be on the safe }
  2723. { side }
  2724. foreachnodestatic(para.left,@nonlocalvars,pointer(symtableproc))) or
  2725. { value parameters of which we know they are modified by }
  2726. { definition have to be copied to a temp }
  2727. { the same goes for cases of "x:=f(x)" where x is passed }
  2728. { as value parameter to f(), at least if we optimized }
  2729. { invocation by setting the funcretnode to x to avoid }
  2730. { assignment afterwards (since x may be read inside the }
  2731. { function after it modified result==x) }
  2732. ((para.parasym.varspez = vs_value) and
  2733. (not(para.parasym.varstate in [vs_initialised,vs_declared,vs_read]) or
  2734. (assigned(aktassignmentnode) and
  2735. (aktassignmentnode.right=self) and
  2736. (nf_assign_done_in_right in aktassignmentnode.flags) and
  2737. aktassignmentnode.left.isequal(para.left)))) or
  2738. { the compiler expects that it can take the address of parameters passed by reference in
  2739. the case of const so we can't replace the node simply by a constant node
  2740. When playing with this code, ensure that
  2741. function f(const a,b : longint) : longint;inline;
  2742. begin
  2743. result:=a*b;
  2744. end;
  2745. [...]
  2746. ...:=f(10,20));
  2747. [...]
  2748. is still folded. (FK)
  2749. }
  2750. ((para.parasym.varspez = vs_const) and
  2751. { const para's can get vs_readwritten if their address }
  2752. { is taken }
  2753. ((para.parasym.varstate = vs_readwritten) or
  2754. { call-by-reference const's may need to be passed by }
  2755. { reference to function called in the inlined code }
  2756. (paramanager.push_addr_param(vs_const,para.parasym.vardef,procdefinition.proccalloption) and
  2757. (not valid_for_addr(para.left,false) or
  2758. is_constnode(para.left)))))
  2759. )
  2760. ) then
  2761. begin
  2762. if para.left.nodetype<>temprefn then
  2763. begin
  2764. tempnode := ctempcreatenode.create(para.parasym.vardef,para.parasym.vardef.size,tt_persistent,tparavarsym(para.parasym).is_regvar(false));
  2765. addstatement(inlineinitstatement,tempnode);
  2766. addstatement(inlinecleanupstatement,ctempdeletenode.create(tempnode));
  2767. addstatement(inlineinitstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  2768. para.left));
  2769. para.left := ctemprefnode.create(tempnode);
  2770. { inherit addr_taken flag }
  2771. if (tabstractvarsym(para.parasym).addr_taken) then
  2772. include(tempnode.tempinfo^.flags,ti_addr_taken);
  2773. end;
  2774. end
  2775. { otherwise if the parameter is "complex", take the address }
  2776. { of the parameter expression, store it in a temp and replace }
  2777. { occurrences of the parameter with dereferencings of this }
  2778. { temp }
  2779. else if (paracomplexity > 1) then
  2780. begin
  2781. tempnode := ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,tparavarsym(para.parasym).is_regvar(true));
  2782. addstatement(inlineinitstatement,tempnode);
  2783. addstatement(inlinecleanupstatement,ctempdeletenode.create(tempnode));
  2784. { inherit addr_taken flag }
  2785. if (tabstractvarsym(para.parasym).addr_taken) then
  2786. include(tempnode.tempinfo^.flags,ti_addr_taken);
  2787. addstatement(inlineinitstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  2788. caddrnode.create_internal(para.left)));
  2789. para.left := ctypeconvnode.create_internal(cderefnode.create(ctemprefnode.create(tempnode)),para.left.resultdef);
  2790. end;
  2791. end;
  2792. para := tcallparanode(para.right);
  2793. end;
  2794. { local variables }
  2795. if not assigned(tprocdef(procdefinition).localst) or
  2796. (tprocdef(procdefinition).localst.SymList.count = 0) then
  2797. exit;
  2798. inlinelocals.count:=tprocdef(procdefinition).localst.SymList.count;
  2799. tprocdef(procdefinition).localst.SymList.ForEachCall(@createlocaltemps,nil);
  2800. end;
  2801. function tcallnode.optimize_funcret_assignment(inlineblock: tblocknode): tnode;
  2802. var
  2803. hp : tstatementnode;
  2804. hp2 : tnode;
  2805. resassign : tassignmentnode;
  2806. begin
  2807. result:=nil;
  2808. if not assigned(funcretnode) or
  2809. not(cnf_return_value_used in callnodeflags) then
  2810. exit;
  2811. { tempcreatenode for the function result }
  2812. hp:=tstatementnode(inlineblock.left);
  2813. if not(assigned(hp)) or
  2814. (hp.left.nodetype <> tempcreaten) or
  2815. not(nf_is_funcret in hp.left.flags) then
  2816. exit;
  2817. { constant assignment? right must be a constant (mainly to avoid trying
  2818. to reuse local temps which may already be freed afterwards once these
  2819. checks are made looser) }
  2820. hp:=tstatementnode(hp.right);
  2821. if not(assigned(hp)) or
  2822. (hp.left.nodetype<>assignn) or
  2823. not is_constnode(tassignmentnode(hp.left).right) then
  2824. exit;
  2825. { left must be function result }
  2826. resassign:=tassignmentnode(hp.left);
  2827. hp2:=resassign.left;
  2828. { can have extra type conversion due to absolute mapping
  2829. of <fucntionname> on function result var }
  2830. if (hp2.nodetype=typeconvn) and (ttypeconvnode(hp2).convtype=tc_equal) then
  2831. hp2:=ttypeconvnode(hp2).left;
  2832. if (hp2.nodetype<>temprefn) or
  2833. not(nf_is_funcret in hp2.flags) then
  2834. exit;
  2835. { tempdelete to normal of the function result }
  2836. hp:=tstatementnode(hp.right);
  2837. if not(assigned(hp)) or
  2838. (hp.left.nodetype <> tempdeleten) then
  2839. exit;
  2840. { the function result once more }
  2841. hp:=tstatementnode(hp.right);
  2842. if not(assigned(hp)) or
  2843. (hp.left.nodetype<>temprefn) or
  2844. not(nf_is_funcret in hp.left.flags) then
  2845. exit;
  2846. { should be the end }
  2847. if assigned(hp.right) then
  2848. exit;
  2849. { we made it! }
  2850. result:=tassignmentnode(resassign).right.getcopy;
  2851. firstpass(result);
  2852. end;
  2853. function tcallnode.pass1_inline:tnode;
  2854. var
  2855. n,
  2856. body : tnode;
  2857. para : tcallparanode;
  2858. inlineblock,
  2859. inlinecleanupblock : tblocknode;
  2860. begin
  2861. result:=nil;
  2862. if not(assigned(tprocdef(procdefinition).inlininginfo) and
  2863. assigned(tprocdef(procdefinition).inlininginfo^.code)) then
  2864. internalerror(200412021);
  2865. inlinelocals:=TFPObjectList.create(true);
  2866. { inherit flags }
  2867. current_procinfo.flags := current_procinfo.flags + ((procdefinition as tprocdef).inlininginfo^.flags*inherited_inlining_flags);
  2868. { Create new code block for inlining }
  2869. inlineblock:=internalstatements(inlineinitstatement);
  2870. inlinecleanupblock:=internalstatements(inlinecleanupstatement);
  2871. if assigned(callinitblock) then
  2872. addstatement(inlineinitstatement,callinitblock.getcopy);
  2873. { replace complex parameters with temps }
  2874. createinlineparas;
  2875. { create a copy of the body and replace parameter loads with the parameter values }
  2876. body:=tprocdef(procdefinition).inlininginfo^.code.getcopy;
  2877. foreachnode(pm_preprocess,body,@replaceparaload,@fileinfo);
  2878. { Concat the body and finalization parts }
  2879. addstatement(inlineinitstatement,body);
  2880. addstatement(inlineinitstatement,inlinecleanupblock);
  2881. inlinecleanupblock:=nil;
  2882. if assigned(callcleanupblock) then
  2883. addstatement(inlineinitstatement,callcleanupblock.getcopy);
  2884. { the last statement of the new inline block must return the
  2885. location and type of the function result.
  2886. This is not needed when the result is not used, also the tempnode is then
  2887. already destroyed by a tempdelete in the callcleanupblock tree }
  2888. if not is_void(resultdef) and
  2889. (cnf_return_value_used in callnodeflags) then
  2890. begin
  2891. if assigned(funcretnode) then
  2892. addstatement(inlineinitstatement,funcretnode.getcopy)
  2893. else
  2894. begin
  2895. para:=tcallparanode(left);
  2896. while assigned(para) do
  2897. begin
  2898. if (vo_is_hidden_para in para.parasym.varoptions) and
  2899. (vo_is_funcret in para.parasym.varoptions) then
  2900. begin
  2901. addstatement(inlineinitstatement,para.left.getcopy);
  2902. break;
  2903. end;
  2904. para:=tcallparanode(para.right);
  2905. end;
  2906. end;
  2907. end;
  2908. { consider it must not be inlined if called
  2909. again inside the args or itself }
  2910. exclude(procdefinition.procoptions,po_inline);
  2911. typecheckpass(inlineblock);
  2912. dosimplify(inlineblock);
  2913. firstpass(inlineblock);
  2914. include(procdefinition.procoptions,po_inline);
  2915. result:=inlineblock;
  2916. { if the function result is used then verify that the blocknode
  2917. returns the same result type as the original callnode }
  2918. if (cnf_return_value_used in callnodeflags) and
  2919. (result.resultdef<>resultdef) then
  2920. internalerror(200709171);
  2921. { free the temps for the locals }
  2922. inlinelocals.free;
  2923. inlinelocals:=nil;
  2924. inlineinitstatement:=nil;
  2925. inlinecleanupstatement:=nil;
  2926. { if all that's left of the inlined function is an constant assignment
  2927. to the result, replace the whole block with the constant only }
  2928. n:=optimize_funcret_assignment(inlineblock);
  2929. if assigned(n) then
  2930. begin
  2931. inlineblock.free;
  2932. result:=n;
  2933. end;
  2934. {$ifdef DEBUGINLINE}
  2935. writeln;
  2936. writeln('**************************',tprocdef(procdefinition).mangledname);
  2937. printnode(output,result);
  2938. {$endif DEBUGINLINE}
  2939. end;
  2940. begin
  2941. ccallnode:=tcallnode;
  2942. ccallparanode:=tcallparanode;
  2943. end.