ncal.pas 126 KB

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