ncal.pas 201 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817
  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. { $define DEBUGINLINE}
  20. interface
  21. uses
  22. cutils,cclasses,
  23. globtype,constexp,
  24. paramgr,parabase,cgbase,
  25. node,nbas,nutils,
  26. {$ifdef state_tracking}
  27. nstate,
  28. {$endif state_tracking}
  29. symbase,symtype,symsym,symdef,symtable;
  30. type
  31. tcallnodeflag = (
  32. cnf_typedefset,
  33. cnf_return_value_used,
  34. cnf_do_inline,
  35. cnf_inherited,
  36. cnf_anon_inherited,
  37. cnf_new_call,
  38. cnf_dispose_call,
  39. cnf_member_call, { called with implicit methodpointer tree }
  40. cnf_uses_varargs, { varargs are used in the declaration }
  41. cnf_create_failed, { exception thrown in constructor -> don't call beforedestruction }
  42. cnf_objc_processed, { the procedure name has been set to the appropriate objc_msgSend* variant -> don't process again }
  43. cnf_objc_id_call, { the procedure is a member call via id -> any ObjC method of any ObjC type in scope is fair game }
  44. cnf_unit_specified, { the unit in which the procedure has to be searched has been specified }
  45. cnf_call_never_returns, { information for the dfa that a subroutine never returns }
  46. cnf_call_self_node_done { the call_self_node has been generated if necessary
  47. (to prevent it from potentially happening again in a wrong context in case of constant propagation or so) }
  48. );
  49. tcallnodeflags = set of tcallnodeflag;
  50. tcallparanode = class;
  51. tcallnode = class(tbinarynode)
  52. private
  53. { number of parameters passed from the source, this does not include the hidden parameters }
  54. paralength : smallint;
  55. function getforcedprocname: TSymStr;
  56. function is_simple_para_load(p:tnode; may_be_in_reg: boolean):boolean;
  57. procedure maybe_load_in_temp(var p:tnode);
  58. function gen_high_tree(var p:tnode;paradef:tdef):tnode;
  59. function gen_procvar_context_tree_self:tnode;
  60. function gen_procvar_context_tree_parentfp:tnode;
  61. function gen_self_tree:tnode;
  62. function use_caller_self(check_for_callee_self: boolean): boolean;
  63. procedure maybe_gen_call_self_node;
  64. function gen_vmt_tree:tnode;
  65. function gen_block_context:tnode;
  66. procedure gen_hidden_parameters;
  67. function funcret_can_be_reused:boolean;
  68. procedure maybe_create_funcret_node;
  69. procedure bind_parasym;
  70. procedure add_init_statement(n:tnode);
  71. procedure add_done_statement(n:tnode);
  72. procedure convert_carg_array_of_const;
  73. procedure order_parameters;
  74. procedure check_inlining;
  75. function pass1_normal:tnode;
  76. procedure register_created_object_types;
  77. function get_expect_loc: tcgloc;
  78. protected
  79. function safe_call_self_node: tnode;
  80. procedure gen_vmt_entry_load; virtual;
  81. procedure gen_syscall_para(para: tcallparanode); virtual;
  82. procedure objc_convert_to_message_send;virtual;
  83. protected
  84. { inlining support }
  85. inlinelocals : TFPObjectList;
  86. inlineinitstatement,
  87. inlinecleanupstatement : tstatementnode;
  88. procedure createinlineparas;
  89. procedure wrapcomplexinlinepara(para: tcallparanode); virtual;
  90. function replaceparaload(var n: tnode; arg: pointer): foreachnoderesult;
  91. procedure createlocaltemps(p:TObject;arg:pointer);
  92. function optimize_funcret_assignment(inlineblock: tblocknode): tnode;
  93. function pass1_inline:tnode;
  94. protected
  95. pushedparasize : longint;
  96. { Objective-C support: force the call node to call the routine with
  97. this name rather than the name of symtableprocentry (don't store
  98. to ppu, is set while processing the node). Also used on the JVM
  99. target for calling virtual methods, as this is name-based and not
  100. based on VMT entry locations }
  101. {$ifdef symansistr}
  102. fforcedprocname: TSymStr;
  103. {$else symansistr}
  104. fforcedprocname: pshortstring;
  105. {$endif symansistr}
  106. property forcedprocname: TSymStr read getforcedprocname;
  107. public
  108. { the symbol containing the definition of the procedure }
  109. { to call }
  110. symtableprocentry : tprocsym;
  111. symtableprocentryderef : tderef;
  112. { symtable where the entry was found, needed for with support }
  113. symtableproc : TSymtable;
  114. { the definition of the procedure to call }
  115. procdefinition : tabstractprocdef;
  116. procdefinitionderef : tderef;
  117. { tree that contains the pointer to the object for this method }
  118. methodpointer : tnode;
  119. { tree representing the VMT entry to call (if any) }
  120. vmt_entry : tnode;
  121. { tree that contains the self/vmt parameter when this node was created
  122. (so it's still valid when this node is processed in an inline
  123. context)
  124. }
  125. call_self_node,
  126. call_vmt_node: tnode;
  127. { initialize/finalization of temps }
  128. callinitblock,
  129. callcleanupblock : tblocknode;
  130. { function return node for initialized types or supplied return variable.
  131. When the result is passed in a parameter then it is set to nil }
  132. funcretnode : tnode;
  133. { varargs parasyms }
  134. varargsparas : tvarargsparalist;
  135. { separately specified resultdef for some compilerprocs (e.g. }
  136. { you can't have a function with an "array of char" resultdef }
  137. { the RTL) (JM) }
  138. typedef: tdef;
  139. callnodeflags : tcallnodeflags;
  140. { only the processor specific nodes need to override this }
  141. { constructor }
  142. constructor create(l:tnode; v : tprocsym;st : TSymtable; mp: tnode; callflags:tcallnodeflags);virtual;
  143. constructor create_procvar(l,r:tnode);
  144. constructor createintern(const name: string; params: tnode);
  145. constructor createinternfromunit(const fromunit, procname: string; params: tnode);
  146. constructor createinternres(const name: string; params: tnode; res:tdef);
  147. constructor createinternresfromunit(const fromunit, procname: string; params: tnode; res:tdef);
  148. constructor createinternreturn(const name: string; params: tnode; returnnode : tnode);
  149. constructor createinternmethod(mp: tnode; const name: string; params: tnode);
  150. constructor createinternmethodres(mp: tnode; const name: string; params: tnode; res:tdef);
  151. destructor destroy;override;
  152. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  153. procedure ppuwrite(ppufile:tcompilerppufile);override;
  154. procedure buildderefimpl;override;
  155. procedure derefimpl;override;
  156. function dogetcopy : tnode;override;
  157. { Goes through all symbols in a class and subclasses and calls
  158. verify abstract for each .
  159. }
  160. procedure verifyabstractcalls;
  161. { called for each definition in a class and verifies if a method
  162. is abstract or not, if it is abstract, give out a warning
  163. }
  164. procedure verifyabstract(sym:TObject;arg:pointer);
  165. procedure insertintolist(l : tnodelist);override;
  166. function pass_1 : tnode;override;
  167. function pass_typecheck:tnode;override;
  168. {$ifdef state_tracking}
  169. function track_state_pass(exec_known:boolean):boolean;override;
  170. {$endif state_tracking}
  171. function docompare(p: tnode): boolean; override;
  172. procedure printnodedata(var t:text);override;
  173. function para_count:longint;
  174. function required_para_count:longint;
  175. { checks if there are any parameters which end up at the stack, i.e.
  176. which have LOC_REFERENCE and set pi_has_stackparameter if this applies }
  177. procedure check_stack_parameters;
  178. { force the name of the to-be-called routine to a particular string,
  179. used for Objective-C message sending. }
  180. property parameters : tnode read left write left;
  181. property pushed_parasize: longint read pushedparasize;
  182. private
  183. AbstractMethodsList : TFPHashList;
  184. end;
  185. tcallnodeclass = class of tcallnode;
  186. tcallparaflag = (
  187. cpf_is_colon_para,
  188. cpf_varargs_para { belongs this para to varargs }
  189. );
  190. tcallparaflags = set of tcallparaflag;
  191. tcallparanode = class(ttertiarynode)
  192. private
  193. fcontains_stack_tainting_call_cached,
  194. ffollowed_by_stack_tainting_call_cached : boolean;
  195. protected
  196. { in case of copy-out parameters: initialization code, and the code to
  197. copy back the parameter value after the call (including any required
  198. finalization code }
  199. fparainit,
  200. fparacopyback: tnode;
  201. procedure handlemanagedbyrefpara(orgparadef: tdef);virtual;
  202. { on some targets, value parameters that are passed by reference must
  203. be copied to a temp location by the caller (and then a reference to
  204. this temp location must be passed) }
  205. procedure copy_value_by_ref_para;
  206. public
  207. callparaflags : tcallparaflags;
  208. parasym : tparavarsym;
  209. { only the processor specific nodes need to override this }
  210. { constructor }
  211. constructor create(expr,next : tnode);virtual;
  212. destructor destroy;override;
  213. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  214. procedure ppuwrite(ppufile:tcompilerppufile);override;
  215. procedure buildderefimpl; override;
  216. procedure derefimpl; override;
  217. function dogetcopy : tnode;override;
  218. procedure insertintolist(l : tnodelist);override;
  219. function pass_typecheck : tnode;override;
  220. function pass_1 : tnode;override;
  221. procedure get_paratype;
  222. procedure firstcallparan;
  223. procedure insert_typeconv;
  224. procedure secondcallparan;virtual;abstract;
  225. function docompare(p: tnode): boolean; override;
  226. procedure printnodetree(var t:text);override;
  227. { returns whether a parameter contains a type conversion from }
  228. { a refcounted into a non-refcounted type }
  229. function can_be_inlined: boolean;
  230. property paravalue : tnode read left write left;
  231. property nextpara : tnode read right write right;
  232. { third is reused to store the parameter name (only while parsing
  233. vardispatch calls, never in real node tree) and copy of 'high'
  234. parameter tree when the parameter is an open array of managed type }
  235. property parametername : tnode read third write third;
  236. { returns whether the evaluation of this parameter involves a
  237. stack tainting call }
  238. function contains_stack_tainting_call: boolean;
  239. { initialises the fcontains_stack_tainting_call_cached field with the
  240. result of contains_stack_tainting_call so that it can be quickly
  241. accessed via the contains_stack_tainting_call_cached property }
  242. procedure init_contains_stack_tainting_call_cache;
  243. { returns result of contains_stack_tainting_call cached during last
  244. call to init_contains_stack_tainting_call_cache }
  245. property contains_stack_tainting_call_cached: boolean read fcontains_stack_tainting_call_cached;
  246. { returns whether this parameter is followed by at least one other
  247. parameter whose evaluation involves a stack tainting parameter
  248. (result is only valid after order_parameters has been called) }
  249. property followed_by_stack_tainting_call_cached: boolean read ffollowed_by_stack_tainting_call_cached;
  250. property paracopyback: tnode read fparacopyback;
  251. end;
  252. tcallparanodeclass = class of tcallparanode;
  253. tdispcalltype = (
  254. dct_method,
  255. dct_propget,
  256. dct_propput
  257. );
  258. function reverseparameters(p: tcallparanode): tcallparanode;
  259. function translate_disp_call(selfnode,parametersnode: tnode; calltype: tdispcalltype; const methodname : ansistring;
  260. dispid : longint;resultdef : tdef) : tnode;
  261. var
  262. ccallnode : tcallnodeclass = tcallnode;
  263. ccallparanode : tcallparanodeclass = tcallparanode;
  264. { Current callnode, this is needed for having a link
  265. between the callparanodes and the callnode they belong to }
  266. aktcallnode : tcallnode;
  267. const
  268. { track current inlining depth }
  269. inlinelevel : longint = 0;
  270. implementation
  271. uses
  272. systems,
  273. verbose,globals,
  274. symconst,defutil,defcmp,
  275. htypechk,pass_1,
  276. ncnv,nld,ninl,nadd,ncon,nmem,nset,nobjc,
  277. ngenutil,objcutil,
  278. procinfo,cpuinfo,
  279. wpobase;
  280. type
  281. tobjectinfoitem = class(tlinkedlistitem)
  282. objinfo : tobjectdef;
  283. constructor create(def : tobjectdef);
  284. end;
  285. {****************************************************************************
  286. HELPERS
  287. ****************************************************************************}
  288. function reverseparameters(p: tcallparanode): tcallparanode;
  289. var
  290. hp1, hp2: tcallparanode;
  291. begin
  292. hp1:=nil;
  293. while assigned(p) do
  294. begin
  295. { pull out }
  296. hp2:=p;
  297. p:=tcallparanode(p.right);
  298. { pull in }
  299. hp2.right:=hp1;
  300. hp1:=hp2;
  301. end;
  302. reverseparameters:=hp1;
  303. end;
  304. function translate_disp_call(selfnode,parametersnode: tnode; calltype: tdispcalltype; const methodname : ansistring;
  305. dispid : longint;resultdef : tdef) : tnode;
  306. const
  307. DISPATCH_METHOD = $1;
  308. DISPATCH_PROPERTYGET = $2;
  309. DISPATCH_PROPERTYPUT = $4;
  310. DISPATCH_PROPERTYPUTREF = $8;
  311. DISPATCH_CONSTRUCT = $4000;
  312. calltypes: array[tdispcalltype] of byte = (
  313. DISPATCH_METHOD, DISPATCH_PROPERTYGET, DISPATCH_PROPERTYPUT
  314. );
  315. var
  316. statements : tstatementnode;
  317. result_data,
  318. params : ttempcreatenode;
  319. paramssize : cardinal;
  320. calldescnode : tdataconstnode;
  321. resultvalue : tnode;
  322. para : tcallparanode;
  323. namedparacount,
  324. paracount : longint;
  325. assignmenttype,
  326. vardatadef,
  327. pvardatadef : tdef;
  328. useresult: boolean;
  329. restype: byte;
  330. selftemp: ttempcreatenode;
  331. selfpara: tnode;
  332. names : ansistring;
  333. variantdispatch : boolean;
  334. function is_byref_para(out assign_type: tdef): boolean;
  335. begin
  336. result:=(assigned(para.parasym) and (para.parasym.varspez in [vs_var,vs_out,vs_constref])) or
  337. (variantdispatch and valid_for_var(para.left,false));
  338. if result or (para.left.resultdef.typ in [variantdef]) then
  339. assign_type:=voidpointertype
  340. else
  341. case para.left.resultdef.size of
  342. 1..4:
  343. assign_type:=u32inttype;
  344. 8:
  345. assign_type:=u64inttype;
  346. else
  347. internalerror(2007042801);
  348. end;
  349. end;
  350. function getvardef(sourcedef: TDef): longint;
  351. begin
  352. if is_ansistring(sourcedef) then
  353. result:=varStrArg
  354. else
  355. if is_unicodestring(sourcedef) then
  356. result:=varUStrArg
  357. else
  358. if is_interfacecom_or_dispinterface(sourcedef) then
  359. begin
  360. { distinct IDispatch and IUnknown interfaces }
  361. if def_is_related(tobjectdef(sourcedef),interface_idispatch) then
  362. result:=vardispatch
  363. else
  364. result:=varunknown;
  365. end
  366. else
  367. result:=sourcedef.getvardef;
  368. end;
  369. begin
  370. variantdispatch:=selfnode.resultdef.typ=variantdef;
  371. result:=internalstatements(statements);
  372. result_data:=nil;
  373. selftemp:=nil;
  374. selfpara:=nil;
  375. useresult := assigned(resultdef) and not is_void(resultdef);
  376. if useresult then
  377. begin
  378. { get temp for the result }
  379. result_data:=ctempcreatenode.create(colevarianttype,colevarianttype.size,tt_persistent,true);
  380. addstatement(statements,result_data);
  381. end;
  382. { first, count and check parameters }
  383. para:=tcallparanode(parametersnode);
  384. paracount:=0;
  385. namedparacount:=0;
  386. while assigned(para) do
  387. begin
  388. typecheckpass(para.left);
  389. { skip hidden dispinterface parameters like $self, $result,
  390. but count skipped variantdispatch parameters. }
  391. if (not variantdispatch) and (para.left.nodetype=nothingn) then
  392. begin
  393. para:=tcallparanode(para.nextpara);
  394. continue;
  395. end;
  396. inc(paracount);
  397. if assigned(para.parametername) then
  398. inc(namedparacount);
  399. { insert some extra casts }
  400. if para.left.nodetype=stringconstn then
  401. inserttypeconv_internal(para.left,cwidestringtype)
  402. { force automatable boolean type }
  403. else if is_boolean(para.left.resultdef) then
  404. inserttypeconv_internal(para.left,bool16type)
  405. { force automatable float type }
  406. else if is_extended(para.left.resultdef)
  407. and (current_settings.fputype<>fpu_none) then
  408. inserttypeconv_internal(para.left,s64floattype)
  409. else if is_shortstring(para.left.resultdef) then
  410. inserttypeconv_internal(para.left,cwidestringtype)
  411. { skip this check if we've already typecasted to automatable type }
  412. else if (para.left.nodetype<>nothingn) and (not is_automatable(para.left.resultdef)) then
  413. CGMessagePos1(para.left.fileinfo,type_e_not_automatable,para.left.resultdef.typename);
  414. para:=tcallparanode(para.nextpara);
  415. end;
  416. { create a temp to store parameter values }
  417. params:=ctempcreatenode.create(cformaltype,0,tt_persistent,false);
  418. addstatement(statements,params);
  419. calldescnode:=cdataconstnode.create;
  420. if not variantdispatch then { generate a tdispdesc record }
  421. begin
  422. { dispid }
  423. calldescnode.append(dispid,sizeof(dispid));
  424. { restype }
  425. if useresult then
  426. restype:=getvardef(resultdef)
  427. else
  428. restype:=0;
  429. calldescnode.appendbyte(restype);
  430. end;
  431. calldescnode.appendbyte(calltypes[calltype]);
  432. calldescnode.appendbyte(paracount);
  433. calldescnode.appendbyte(namedparacount);
  434. { build up parameters and description }
  435. para:=tcallparanode(parametersnode);
  436. paramssize:=0;
  437. names := '';
  438. while assigned(para) do
  439. begin
  440. { Skipped parameters are actually (varType=varError, vError=DISP_E_PARAMNOTFOUND).
  441. Generate only varType here, the value will be added by RTL. }
  442. if para.left.nodetype=nothingn then
  443. begin
  444. if variantdispatch then
  445. calldescnode.appendbyte(varError);
  446. para:=tcallparanode(para.nextpara);
  447. continue;
  448. end;
  449. if assigned(para.parametername) then
  450. begin
  451. if para.parametername.nodetype=stringconstn then
  452. names:=names+tstringconstnode(para.parametername).value_str+#0
  453. else
  454. internalerror(200611041);
  455. end;
  456. restype:=getvardef(para.left.resultdef);
  457. if is_byref_para(assignmenttype) then
  458. restype:=restype or $80;
  459. { assign the argument/parameter to the temporary location }
  460. { for Variants, we always pass a pointer, RTL helpers must handle it
  461. depending on byref bit }
  462. if assignmenttype=voidpointertype then
  463. addstatement(statements,cassignmentnode.create(
  464. ctypeconvnode.create_internal(ctemprefnode.create_offset(params,paramssize),
  465. voidpointertype),
  466. ctypeconvnode.create_internal(caddrnode.create_internal(para.left),voidpointertype)))
  467. else
  468. addstatement(statements,cassignmentnode.create(
  469. ctypeconvnode.create_internal(ctemprefnode.create_offset(params,paramssize),
  470. assignmenttype),
  471. ctypeconvnode.create_internal(para.left,assignmenttype)));
  472. inc(paramssize,max(voidpointertype.size,assignmenttype.size));
  473. calldescnode.appendbyte(restype);
  474. para.left:=nil;
  475. para:=tcallparanode(para.nextpara);
  476. end;
  477. { Set final size for parameter block }
  478. params.size:=paramssize;
  479. { old argument list skeleton isn't needed anymore }
  480. parametersnode.free;
  481. pvardatadef:=tpointerdef(search_system_type('PVARDATA').typedef);
  482. if useresult then
  483. resultvalue:=caddrnode.create(ctemprefnode.create(result_data))
  484. else
  485. resultvalue:=cpointerconstnode.create(0,voidpointertype);
  486. if variantdispatch then
  487. begin
  488. calldescnode.append(pointer(methodname)^,length(methodname));
  489. calldescnode.appendbyte(0);
  490. calldescnode.append(pointer(names)^,length(names));
  491. { actual call }
  492. vardatadef:=trecorddef(search_system_type('TVARDATA').typedef);
  493. { the Variant should behave similar to hidden 'self' parameter of objects/records,
  494. see issues #26773 and #27044 }
  495. if not valid_for_var(selfnode,false) then
  496. begin
  497. selftemp:=ctempcreatenode.create(selfnode.resultdef,selfnode.resultdef.size,tt_persistent,false);
  498. addstatement(statements,selftemp);
  499. addstatement(statements,cassignmentnode.create(ctemprefnode.create(selftemp),selfnode));
  500. selfpara:=ctemprefnode.create(selftemp);
  501. end
  502. else
  503. selfpara:=selfnode;
  504. addstatement(statements,ccallnode.createintern('fpc_dispinvoke_variant',
  505. { parameters are passed always reverted, i.e. the last comes first }
  506. ccallparanode.create(caddrnode.create(ctemprefnode.create(params)),
  507. ccallparanode.create(caddrnode.create(calldescnode),
  508. ccallparanode.create(ctypeconvnode.create_internal(selfpara,vardatadef),
  509. ccallparanode.create(ctypeconvnode.create_internal(resultvalue,pvardatadef),nil)))))
  510. );
  511. if assigned(selftemp) then
  512. addstatement(statements,ctempdeletenode.create(selftemp));
  513. end
  514. else
  515. begin
  516. addstatement(statements,ccallnode.createintern('fpc_dispatch_by_id',
  517. { parameters are passed always reverted, i.e. the last comes first }
  518. ccallparanode.create(caddrnode.create(ctemprefnode.create(params)),
  519. ccallparanode.create(caddrnode.create(calldescnode),
  520. ccallparanode.create(ctypeconvnode.create_internal(selfnode,voidpointertype),
  521. ccallparanode.create(ctypeconvnode.create_internal(resultvalue,pvardatadef),nil)))))
  522. );
  523. end;
  524. addstatement(statements,ctempdeletenode.create(params));
  525. if useresult then
  526. begin
  527. { clean up }
  528. addstatement(statements,ctempdeletenode.create_normal_temp(result_data));
  529. addstatement(statements,ctemprefnode.create(result_data));
  530. end;
  531. end;
  532. {****************************************************************************
  533. TOBJECTINFOITEM
  534. ****************************************************************************}
  535. constructor tobjectinfoitem.create(def : tobjectdef);
  536. begin
  537. inherited create;
  538. objinfo := def;
  539. end;
  540. {****************************************************************************
  541. TCALLPARANODE
  542. ****************************************************************************}
  543. procedure tcallparanode.handlemanagedbyrefpara(orgparadef: tdef);
  544. var
  545. temp: ttempcreatenode;
  546. npara: tcallparanode;
  547. paraaddrtype: tdef;
  548. begin
  549. { release memory for reference counted out parameters }
  550. if (parasym.varspez=vs_out) and
  551. is_managed_type(orgparadef) and
  552. (not is_open_array(resultdef) or
  553. is_managed_type(tarraydef(resultdef).elementdef)) and
  554. not(target_info.system in systems_garbage_collected_managed_types) then
  555. begin
  556. paraaddrtype:=cpointerdef.getreusable(orgparadef);
  557. { create temp with address of the parameter }
  558. temp:=ctempcreatenode.create(
  559. paraaddrtype,paraaddrtype.size,tt_persistent,true);
  560. { put this code in the init/done statement of the call node, because
  561. we should finalize all out parameters before other parameters
  562. are evaluated (in case e.g. a managed out parameter is also
  563. passed by value, we must not pass the pointer to the now possibly
  564. freed data as the value parameter, but the finalized/nil value }
  565. aktcallnode.add_init_statement(temp);
  566. aktcallnode.add_init_statement(
  567. cassignmentnode.create(
  568. ctemprefnode.create(temp),
  569. caddrnode.create(left)));
  570. if not is_open_array(resultdef) or
  571. not is_managed_type(tarraydef(resultdef).elementdef) then
  572. { finalize the entire parameter }
  573. aktcallnode.add_init_statement(
  574. cnodeutils.finalize_data_node(
  575. cderefnode.create(ctemprefnode.create(temp))))
  576. else
  577. begin
  578. { passing a (part of, in case of slice) dynamic array as an
  579. open array -> finalize the dynamic array contents, not the
  580. dynamic array itself }
  581. npara:=ccallparanode.create(
  582. { array length = high + 1 }
  583. caddnode.create(addn,third.getcopy,genintconstnode(1)),
  584. ccallparanode.create(caddrnode.create_internal
  585. (crttinode.create(tstoreddef(tarraydef(resultdef).elementdef),initrtti,rdt_normal)),
  586. ccallparanode.create(caddrnode.create_internal(
  587. cderefnode.create(ctemprefnode.create(temp))),nil)));
  588. aktcallnode.add_init_statement(
  589. ccallnode.createintern('fpc_finalize_array',npara));
  590. end;
  591. left:=cderefnode.create(ctemprefnode.create(temp));
  592. firstpass(left);
  593. aktcallnode.add_done_statement(ctempdeletenode.create(temp));
  594. end;
  595. end;
  596. procedure tcallparanode.copy_value_by_ref_para;
  597. var
  598. initstat,
  599. copybackstat,
  600. finistat: tstatementnode;
  601. finiblock: tblocknode;
  602. paratemp: ttempcreatenode;
  603. arraysize,
  604. arraybegin: tnode;
  605. lefttemp: ttempcreatenode;
  606. vardatatype,
  607. temparraydef: tdef;
  608. begin
  609. { this routine is for targets where by-reference value parameters need
  610. to be copied by the caller. It's basically the node-level equivalent
  611. of thlcgobj.g_copyvalueparas }
  612. { in case of an array constructor, we don't need a copy since the array
  613. constructor itself is already constructed on the fly (and hence if
  614. it's modified by the caller, that's no problem) }
  615. if not is_array_constructor(left.resultdef) then
  616. begin
  617. fparainit:=internalstatements(initstat);
  618. fparacopyback:=internalstatements(copybackstat);
  619. finiblock:=internalstatements(finistat);
  620. paratemp:=nil;
  621. { making a copy of an open array, an array of const or a dynamic
  622. array requires dynamic memory allocation since we don't know the
  623. size at compile time }
  624. if is_open_array(left.resultdef) or
  625. is_array_of_const(left.resultdef) or
  626. (is_dynamic_array(left.resultdef) and
  627. is_open_array(parasym.vardef)) then
  628. begin
  629. paratemp:=ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  630. if is_dynamic_array(left.resultdef) then
  631. begin
  632. { note that in insert_typeconv, this dynamic array was
  633. already converted into an open array (-> dereferenced)
  634. and then its resultdef was restored to the original
  635. dynamic array one -> get the address before treating it
  636. as a dynamic array here }
  637. { first restore the actual resultdef of left }
  638. temparraydef:=left.resultdef;
  639. left.resultdef:=parasym.vardef;
  640. { get its address }
  641. lefttemp:=ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  642. addstatement(initstat,lefttemp);
  643. addstatement(finistat,ctempdeletenode.create(lefttemp));
  644. addstatement(initstat,
  645. cassignmentnode.create(
  646. ctemprefnode.create(lefttemp),
  647. caddrnode.create_internal(left)
  648. )
  649. );
  650. { restore the resultdef }
  651. left.resultdef:=temparraydef;
  652. { now treat that address (correctly) as the original
  653. dynamic array to get its start and length }
  654. arraybegin:=cvecnode.create(
  655. ctypeconvnode.create_explicit(ctemprefnode.create(lefttemp),
  656. left.resultdef),
  657. genintconstnode(0)
  658. );
  659. arraysize:=caddnode.create(muln,
  660. geninlinenode(in_length_x,false,
  661. ctypeconvnode.create_explicit(ctemprefnode.create(lefttemp),
  662. left.resultdef)
  663. ),
  664. genintconstnode(tarraydef(left.resultdef).elementdef.size)
  665. );
  666. end
  667. else
  668. begin
  669. { no problem here that left is used multiple times, as
  670. sizeof() will simply evaluate to the high parameter }
  671. arraybegin:=left.getcopy;
  672. arraysize:=geninlinenode(in_sizeof_x,false,left);
  673. end;
  674. addstatement(initstat,paratemp);
  675. { paratemp:=getmem(sizeof(para)) }
  676. addstatement(initstat,
  677. cassignmentnode.create(
  678. ctemprefnode.create(paratemp),
  679. ccallnode.createintern('fpc_getmem',
  680. ccallparanode.create(
  681. arraysize.getcopy,nil
  682. )
  683. )
  684. )
  685. );
  686. { move(para,temp,sizeof(arr)) (no "left.getcopy" below because
  687. we replace left afterwards) }
  688. addstatement(initstat,
  689. ccallnode.createintern('MOVE',
  690. ccallparanode.create(
  691. arraysize,
  692. ccallparanode.create(
  693. cderefnode.create(ctemprefnode.create(paratemp)),
  694. ccallparanode.create(
  695. arraybegin,nil
  696. )
  697. )
  698. )
  699. )
  700. );
  701. { no reference count increases, that's still done on the callee
  702. side because for compatibility with targets that perform this
  703. copy on the callee side, that should only be done for non-
  704. assember functions (and we can't know that 100% certain here,
  705. e.g. in case of external declarations) (*) }
  706. { free the memory again after the call: freemem(paratemp) }
  707. addstatement(finistat,
  708. ccallnode.createintern('fpc_freemem',
  709. ccallparanode.create(
  710. ctemprefnode.create(paratemp),nil
  711. )
  712. )
  713. );
  714. { replace the original parameter with a dereference of the
  715. temp typecasted to the same type as the original parameter
  716. (don't free left, it has been reused above) }
  717. left:=ctypeconvnode.create_internal(
  718. cderefnode.create(ctemprefnode.create(paratemp)),
  719. left.resultdef);
  720. end
  721. else if is_shortstring(parasym.vardef) then
  722. begin
  723. { the shortstring parameter may have a different size than the
  724. parameter type -> assign and truncate/extend }
  725. paratemp:=ctempcreatenode.create(parasym.vardef,parasym.vardef.size,tt_persistent,false);
  726. addstatement(initstat,paratemp);
  727. { assign shortstring }
  728. addstatement(initstat,
  729. cassignmentnode.create(
  730. ctemprefnode.create(paratemp),left
  731. )
  732. );
  733. { replace parameter with temp (don't free left, it has been
  734. reused above) }
  735. left:=ctemprefnode.create(paratemp);
  736. end
  737. else if parasym.vardef.typ=variantdef then
  738. begin
  739. vardatatype:=search_system_type('TVARDATA').typedef;
  740. paratemp:=ctempcreatenode.create(vardatatype,vardatatype.size,tt_persistent,false);
  741. addstatement(initstat,paratemp);
  742. addstatement(initstat,
  743. ccallnode.createintern('fpc_variant_copy_overwrite',
  744. ccallparanode.create(
  745. ctypeconvnode.create_explicit(ctemprefnode.create(paratemp),
  746. vardatatype
  747. ),
  748. ccallparanode.create(ctypeconvnode.create_explicit(left,
  749. vardatatype),
  750. nil
  751. )
  752. )
  753. )
  754. );
  755. { replace parameter with temp (don't free left, it has been
  756. reused above) }
  757. left:=ctypeconvnode.create_explicit(ctemprefnode.create(paratemp),parasym.vardef);
  758. end
  759. else if is_managed_type(left.resultdef) then
  760. begin
  761. { don't increase/decrease the reference count here, will be done by
  762. the callee (see (*) above) -> typecast to array of byte
  763. for the assignment to the temp }
  764. temparraydef:=carraydef.getreusable(u8inttype,left.resultdef.size);
  765. paratemp:=ctempcreatenode.create(temparraydef,temparraydef.size,tt_persistent,false);
  766. addstatement(initstat,paratemp);
  767. addstatement(initstat,
  768. cassignmentnode.create(
  769. ctemprefnode.create(paratemp),
  770. ctypeconvnode.create_internal(left,temparraydef)
  771. )
  772. );
  773. left:=ctypeconvnode.create_explicit(ctemprefnode.create(paratemp),left.resultdef);
  774. end
  775. else
  776. begin
  777. paratemp:=ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,false);
  778. addstatement(initstat,paratemp);
  779. addstatement(initstat,
  780. cassignmentnode.create(ctemprefnode.create(paratemp),left)
  781. );
  782. { replace parameter with temp (don't free left, it has been
  783. reused above) }
  784. left:=ctemprefnode.create(paratemp);
  785. end;
  786. addstatement(finistat,ctempdeletenode.create(paratemp));
  787. addstatement(copybackstat,finiblock);
  788. firstpass(fparainit);
  789. firstpass(left);
  790. firstpass(fparacopyback);
  791. end;
  792. end;
  793. constructor tcallparanode.create(expr,next : tnode);
  794. begin
  795. inherited create(callparan,expr,next,nil);
  796. if not assigned(expr) then
  797. internalerror(200305091);
  798. expr.fileinfo:=fileinfo;
  799. callparaflags:=[];
  800. if expr.nodetype = typeconvn then
  801. ttypeconvnode(expr).warn_pointer_to_signed:=false;
  802. end;
  803. destructor tcallparanode.destroy;
  804. begin
  805. fparainit.free;
  806. fparacopyback.free;
  807. inherited destroy;
  808. end;
  809. constructor tcallparanode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  810. begin
  811. inherited ppuload(t,ppufile);
  812. ppufile.getsmallset(callparaflags);
  813. fparainit:=ppuloadnode(ppufile);
  814. fparacopyback:=ppuloadnode(ppufile);
  815. end;
  816. procedure tcallparanode.ppuwrite(ppufile:tcompilerppufile);
  817. begin
  818. inherited ppuwrite(ppufile);
  819. ppufile.putsmallset(callparaflags);
  820. ppuwritenode(ppufile,fparainit);
  821. ppuwritenode(ppufile,fparacopyback);
  822. end;
  823. procedure tcallparanode.buildderefimpl;
  824. begin
  825. inherited buildderefimpl;
  826. if assigned(fparainit) then
  827. fparainit.buildderefimpl;
  828. if assigned(fparacopyback) then
  829. fparacopyback.buildderefimpl;
  830. end;
  831. procedure tcallparanode.derefimpl;
  832. begin
  833. inherited derefimpl;
  834. if assigned(fparainit) then
  835. fparainit.derefimpl;
  836. if assigned(fparacopyback) then
  837. fparacopyback.derefimpl;
  838. end;
  839. function tcallparanode.dogetcopy : tnode;
  840. var
  841. n : tcallparanode;
  842. initcopy: tnode;
  843. begin
  844. initcopy:=nil;
  845. { must be done before calling inherited getcopy, because can create
  846. tempcreatenodes for values used in left }
  847. if assigned(fparainit) then
  848. initcopy:=fparainit.getcopy;
  849. n:=tcallparanode(inherited dogetcopy);
  850. n.callparaflags:=callparaflags;
  851. n.parasym:=parasym;
  852. n.fparainit:=initcopy;
  853. if assigned(fparacopyback) then
  854. n.fparacopyback:=fparacopyback.getcopy;
  855. result:=n;
  856. end;
  857. procedure tcallparanode.insertintolist(l : tnodelist);
  858. begin
  859. end;
  860. function tcallparanode.pass_typecheck : tnode;
  861. begin
  862. { need to use get_paratype }
  863. internalerror(200709251);
  864. result:=nil;
  865. end;
  866. function tcallparanode.pass_1 : tnode;
  867. begin
  868. { need to use firstcallparan }
  869. internalerror(200709252);
  870. result:=nil;
  871. end;
  872. procedure tcallparanode.get_paratype;
  873. var
  874. old_array_constructor : boolean;
  875. begin
  876. if assigned(right) then
  877. tcallparanode(right).get_paratype;
  878. old_array_constructor:=allow_array_constructor;
  879. allow_array_constructor:=true;
  880. if assigned(fparainit) then
  881. typecheckpass(fparainit);
  882. typecheckpass(left);
  883. if assigned(third) then
  884. typecheckpass(third);
  885. if assigned(fparacopyback) then
  886. typecheckpass(fparacopyback);
  887. allow_array_constructor:=old_array_constructor;
  888. if codegenerror then
  889. resultdef:=generrordef
  890. else
  891. resultdef:=left.resultdef;
  892. end;
  893. procedure tcallparanode.firstcallparan;
  894. begin
  895. if assigned(right) then
  896. tcallparanode(right).firstcallparan;
  897. if not assigned(left.resultdef) then
  898. get_paratype;
  899. if assigned(parasym) and
  900. (parasym.varspez in [vs_var,vs_out,vs_constref]) and
  901. { for record constructors }
  902. (left.nodetype<>nothingn) then
  903. handlemanagedbyrefpara(left.resultdef);
  904. { for targets that have to copy "value parameters by reference" on the
  905. caller side
  906. aktcallnode may not be assigned in case firstcallparan is called for
  907. fake parameters to inline nodes (in that case, we don't have a real
  908. call and hence no "caller side" either)
  909. }
  910. if assigned(aktcallnode) and
  911. (target_info.system in systems_caller_copy_addr_value_para) and
  912. ((assigned(parasym) and
  913. (parasym.varspez=vs_value)) or
  914. (cpf_varargs_para in callparaflags)) and
  915. (left.nodetype<>nothingn) and
  916. not(vo_has_local_copy in parasym.varoptions) and
  917. ((not is_open_array(parasym.vardef) and
  918. not is_array_of_const(parasym.vardef)) or
  919. not(aktcallnode.procdefinition.proccalloption in cdecl_pocalls)) and
  920. paramanager.push_addr_param(vs_value,parasym.vardef,
  921. aktcallnode.procdefinition.proccalloption) then
  922. copy_value_by_ref_para;
  923. { does it need to load RTTI? }
  924. if assigned(parasym) and (parasym.varspez=vs_out) and
  925. (cs_create_pic in current_settings.moduleswitches) and
  926. (
  927. is_rtti_managed_type(left.resultdef) or
  928. (
  929. is_open_array(resultdef) and
  930. is_managed_type(tarraydef(resultdef).elementdef)
  931. )
  932. ) and
  933. not(target_info.system in systems_garbage_collected_managed_types) then
  934. include(current_procinfo.flags,pi_needs_got);
  935. if assigned(fparainit) then
  936. firstpass(fparainit);
  937. firstpass(left);
  938. if assigned(fparacopyback) then
  939. firstpass(fparacopyback);
  940. if assigned(third) then
  941. firstpass(third);
  942. expectloc:=left.expectloc;
  943. end;
  944. procedure tcallparanode.insert_typeconv;
  945. var
  946. olddef : tdef;
  947. hp : tnode;
  948. block : tblocknode;
  949. statements : tstatementnode;
  950. temp : ttempcreatenode;
  951. owningprocdef: tprocdef;
  952. begin
  953. { Be sure to have the resultdef }
  954. if not assigned(left.resultdef) then
  955. typecheckpass(left);
  956. if (left.nodetype<>nothingn) then
  957. begin
  958. { convert loads of the function result variable into procvars
  959. representing the current function in case the formal parameter is
  960. a procvar (CodeWarrior Pascal contains the same kind of
  961. automatic disambiguation; you can use the function name in both
  962. meanings, so we cannot statically pick either the function result
  963. or the function definition in pexpr) }
  964. if (m_mac in current_settings.modeswitches) and
  965. (parasym.vardef.typ=procvardef) and
  966. is_ambiguous_funcret_load(left,owningprocdef) then
  967. begin
  968. hp:=cloadnode.create_procvar(owningprocdef.procsym,owningprocdef,owningprocdef.procsym.owner);
  969. typecheckpass(hp);
  970. left.free;
  971. left:=hp;
  972. end;
  973. { Convert tp procvars, this is needs to be done
  974. here to make the change permanent. in the overload
  975. choosing the changes are only made temporarily }
  976. if (left.resultdef.typ=procvardef) and
  977. not(parasym.vardef.typ in [procvardef,formaldef]) then
  978. begin
  979. if maybe_call_procvar(left,true) then
  980. resultdef:=left.resultdef
  981. end;
  982. { Remove implicitly inserted typecast to pointer for
  983. @procvar in macpas }
  984. if (m_mac_procvar in current_settings.modeswitches) and
  985. (parasym.vardef.typ=procvardef) and
  986. (left.nodetype=typeconvn) and
  987. is_voidpointer(left.resultdef) and
  988. (ttypeconvnode(left).left.nodetype=typeconvn) and
  989. (ttypeconvnode(ttypeconvnode(left).left).convtype=tc_proc_2_procvar) then
  990. begin
  991. hp:=left;
  992. left:=ttypeconvnode(left).left;
  993. ttypeconvnode(hp).left:=nil;
  994. hp.free;
  995. end;
  996. maybe_global_proc_to_nested(left,parasym.vardef);
  997. { Handle varargs and hidden paras directly, no typeconvs or }
  998. { pass_typechecking needed }
  999. if (cpf_varargs_para in callparaflags) then
  1000. begin
  1001. { this should only happen vor C varargs }
  1002. { the necessary conversions have already been performed in }
  1003. { tarrayconstructornode.insert_typeconvs }
  1004. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1005. insert_varargstypeconv(left,true);
  1006. resultdef:=left.resultdef;
  1007. { also update parasym type to get the correct parameter location
  1008. for the new types }
  1009. parasym.vardef:=left.resultdef;
  1010. end
  1011. else
  1012. if (vo_is_hidden_para in parasym.varoptions) then
  1013. begin
  1014. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1015. resultdef:=left.resultdef;
  1016. end
  1017. else
  1018. begin
  1019. { Do we need arrayconstructor -> set conversion, then insert
  1020. it here before the arrayconstructor node breaks the tree
  1021. with its conversions of enum->ord }
  1022. if (left.nodetype=arrayconstructorn) and
  1023. (parasym.vardef.typ=setdef) then
  1024. inserttypeconv(left,parasym.vardef);
  1025. { set some settings needed for arrayconstructor }
  1026. if is_array_constructor(left.resultdef) then
  1027. begin
  1028. if left.nodetype<>arrayconstructorn then
  1029. internalerror(200504041);
  1030. if is_array_of_const(parasym.vardef) then
  1031. begin
  1032. { force variant array }
  1033. include(left.flags,nf_forcevaria);
  1034. end
  1035. else
  1036. begin
  1037. include(left.flags,nf_novariaallowed);
  1038. { now that the resultting type is know we can insert the required
  1039. typeconvs for the array constructor }
  1040. if parasym.vardef.typ=arraydef then
  1041. tarrayconstructornode(left).force_type(tarraydef(parasym.vardef).elementdef);
  1042. end;
  1043. end;
  1044. { check if local proc/func is assigned to procvar }
  1045. if left.resultdef.typ=procvardef then
  1046. test_local_to_procvar(tprocvardef(left.resultdef),parasym.vardef);
  1047. { test conversions }
  1048. if not(is_shortstring(left.resultdef) and
  1049. is_shortstring(parasym.vardef)) and
  1050. (parasym.vardef.typ<>formaldef) and
  1051. not(parasym.univpara) then
  1052. begin
  1053. { Process open parameters }
  1054. if paramanager.keep_para_array_range(parasym.varspez,parasym.vardef,aktcallnode.procdefinition.proccalloption) then
  1055. begin
  1056. { insert type conv but hold the ranges of the array }
  1057. olddef:=left.resultdef;
  1058. inserttypeconv(left,parasym.vardef);
  1059. left.resultdef:=olddef;
  1060. end
  1061. else
  1062. begin
  1063. check_ranges(left.fileinfo,left,parasym.vardef);
  1064. inserttypeconv(left,parasym.vardef);
  1065. end;
  1066. if codegenerror then
  1067. exit;
  1068. end;
  1069. { truncate shortstring value parameters at the caller side if }
  1070. { they are passed by value (if passed by reference, then the }
  1071. { callee will truncate when copying in the string) }
  1072. { This happens e.g. on x86_64 for small strings }
  1073. if is_shortstring(left.resultdef) and
  1074. is_shortstring(parasym.vardef) and
  1075. (parasym.varspez=vs_value) and
  1076. not paramanager.push_addr_param(parasym.varspez,parasym.vardef,
  1077. aktcallnode.procdefinition.proccalloption) and
  1078. ((is_open_string(left.resultdef) and
  1079. (tstringdef(parasym.vardef).len < 255)) or
  1080. (not is_open_string(left.resultdef) and
  1081. { when a stringconstn is typeconverted, then only its }
  1082. { def is modified, not the contents (needed because in }
  1083. { Delphi/TP, if you pass a longer string to a const }
  1084. { parameter, then the callee has to see this longer }
  1085. { string) }
  1086. (((left.nodetype<>stringconstn) and
  1087. (tstringdef(parasym.vardef).len<tstringdef(left.resultdef).len)) or
  1088. ((left.nodetype=stringconstn) and
  1089. (tstringdef(parasym.vardef).len<tstringconstnode(left).len))))) then
  1090. begin
  1091. block:=internalstatements(statements);
  1092. { temp for the new string }
  1093. temp:=ctempcreatenode.create(parasym.vardef,parasym.vardef.size,
  1094. tt_persistent,true);
  1095. addstatement(statements,temp);
  1096. { assign parameter to temp }
  1097. addstatement(statements,cassignmentnode.create(ctemprefnode.create(temp),left));
  1098. left:=nil;
  1099. { release temp after next use }
  1100. addstatement(statements,ctempdeletenode.create_normal_temp(temp));
  1101. addstatement(statements,ctemprefnode.create(temp));
  1102. typecheckpass(tnode(block));
  1103. left:=block;
  1104. end;
  1105. { check var strings }
  1106. if (cs_strict_var_strings in current_settings.localswitches) and
  1107. is_shortstring(left.resultdef) and
  1108. is_shortstring(parasym.vardef) and
  1109. (parasym.varspez in [vs_out,vs_var,vs_constref]) and
  1110. not(is_open_string(parasym.vardef)) and
  1111. not(equal_defs(left.resultdef,parasym.vardef)) then
  1112. begin
  1113. CGMessagePos(left.fileinfo,type_e_strict_var_string_violation);
  1114. end;
  1115. { passing a value to an "univ" parameter implies an explicit
  1116. typecast to the parameter type. Must be done before the
  1117. valid_for_var() check, since the typecast can result in
  1118. an invalid lvalue in case of var/out parameters. }
  1119. if (parasym.univpara) then
  1120. begin
  1121. { load procvar if a procedure is passed }
  1122. if ((m_tp_procvar in current_settings.modeswitches) or
  1123. (m_mac_procvar in current_settings.modeswitches)) and
  1124. (left.nodetype=calln) and
  1125. (is_void(left.resultdef)) then
  1126. begin
  1127. load_procvar_from_calln(left);
  1128. { load_procvar_from_calln() creates a loadn for a
  1129. a procedure, which means that the type conversion
  1130. below will type convert the first instruction
  1131. bytes of the procedure -> convert to a procvar }
  1132. left:=ctypeconvnode.create_proc_to_procvar(left);
  1133. typecheckpass(left);
  1134. end;
  1135. inserttypeconv_explicit(left,parasym.vardef);
  1136. end;
  1137. { Handle formal parameters separate }
  1138. if (parasym.vardef.typ=formaldef) then
  1139. begin
  1140. { load procvar if a procedure is passed }
  1141. if ((m_tp_procvar in current_settings.modeswitches) or
  1142. (m_mac_procvar in current_settings.modeswitches)) and
  1143. (left.nodetype=calln) and
  1144. (is_void(left.resultdef)) then
  1145. load_procvar_from_calln(left);
  1146. case parasym.varspez of
  1147. vs_var,
  1148. vs_constref,
  1149. vs_out :
  1150. begin
  1151. if not valid_for_formal_var(left,true) then
  1152. CGMessagePos(left.fileinfo,parser_e_illegal_parameter_list);
  1153. end;
  1154. vs_const :
  1155. begin
  1156. if not valid_for_formal_const(left,true) then
  1157. CGMessagePos(left.fileinfo,parser_e_illegal_parameter_list)
  1158. else if (target_info.system in systems_managed_vm) and
  1159. (left.resultdef.typ in [orddef,floatdef]) then
  1160. begin
  1161. left:=cinlinenode.create(in_box_x,false,ccallparanode.create(left,nil));
  1162. typecheckpass(left);
  1163. end;
  1164. end;
  1165. end;
  1166. end
  1167. else
  1168. begin
  1169. { check if the argument is allowed }
  1170. if (parasym.varspez in [vs_out,vs_var]) then
  1171. valid_for_var(left,true);
  1172. end;
  1173. if parasym.varspez in [vs_var,vs_out,vs_constref] then
  1174. set_unique(left);
  1175. case parasym.varspez of
  1176. vs_out :
  1177. begin
  1178. { first set written separately to avoid false }
  1179. { uninitialized warnings (tbs/tb0542) }
  1180. set_varstate(left,vs_written,[]);
  1181. set_varstate(left,vs_readwritten,[]);
  1182. { compilerprocs never capture the address of their
  1183. parameters }
  1184. if not(po_compilerproc in aktcallnode.procdefinition.procoptions) then
  1185. make_not_regable(left,[ra_addr_regable,ra_addr_taken])
  1186. else
  1187. make_not_regable(left,[ra_addr_regable])
  1188. end;
  1189. vs_var,
  1190. vs_constref:
  1191. begin
  1192. set_varstate(left,vs_readwritten,[vsf_must_be_valid,vsf_use_hints]);
  1193. { constref takes also the address, but storing it is actually the compiler
  1194. is not supposed to expect }
  1195. if parasym.varspez=vs_var then
  1196. { compilerprocs never capture the address of their
  1197. parameters }
  1198. if not(po_compilerproc in aktcallnode.procdefinition.procoptions) then
  1199. make_not_regable(left,[ra_addr_regable,ra_addr_taken])
  1200. else
  1201. make_not_regable(left,[ra_addr_regable])
  1202. end;
  1203. else
  1204. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1205. end;
  1206. { must only be done after typeconv PM }
  1207. resultdef:=parasym.vardef;
  1208. end;
  1209. end;
  1210. { process next node }
  1211. if assigned(right) then
  1212. tcallparanode(right).insert_typeconv;
  1213. end;
  1214. function tcallparanode.can_be_inlined: boolean;
  1215. var
  1216. n: tnode;
  1217. begin
  1218. n:=left;
  1219. result:=false;
  1220. while assigned(n) and
  1221. (n.nodetype=typeconvn) do
  1222. begin
  1223. { look for type conversion nodes which convert a }
  1224. { refcounted type into a non-refcounted type }
  1225. if not is_managed_type(n.resultdef) and
  1226. is_managed_type(ttypeconvnode(n).left.resultdef) then
  1227. exit;
  1228. n:=ttypeconvnode(n).left;
  1229. end;
  1230. { also check for dereferencing constant pointers, like }
  1231. { tsomerecord(nil^) passed to a const r: tsomerecord }
  1232. { parameter }
  1233. if (n.nodetype=derefn) then
  1234. begin
  1235. repeat
  1236. n:=tunarynode(n).left;
  1237. until (n.nodetype<>typeconvn);
  1238. if (n.nodetype in [niln,pointerconstn]) then
  1239. exit
  1240. end;
  1241. result:=true;
  1242. end;
  1243. function check_contains_stack_tainting_call(var n: tnode; arg: pointer): foreachnoderesult;
  1244. begin
  1245. if (n.nodetype=calln) and
  1246. tcallnode(n).procdefinition.stack_tainting_parameter(callerside) then
  1247. result:=fen_norecurse_true
  1248. else
  1249. result:=fen_false;
  1250. end;
  1251. function tcallparanode.contains_stack_tainting_call: boolean;
  1252. begin
  1253. result:=foreachnodestatic(pm_postprocess,left,@check_contains_stack_tainting_call,nil);
  1254. end;
  1255. procedure tcallparanode.init_contains_stack_tainting_call_cache;
  1256. begin
  1257. fcontains_stack_tainting_call_cached:=contains_stack_tainting_call;
  1258. end;
  1259. function tcallparanode.docompare(p: tnode): boolean;
  1260. begin
  1261. docompare :=
  1262. inherited docompare(p) and
  1263. fparainit.isequal(tcallparanode(p).fparainit) and
  1264. fparacopyback.isequal(tcallparanode(p).fparacopyback) and
  1265. (callparaflags = tcallparanode(p).callparaflags)
  1266. ;
  1267. end;
  1268. procedure tcallparanode.printnodetree(var t:text);
  1269. begin
  1270. printnodelist(t);
  1271. end;
  1272. {****************************************************************************
  1273. TCALLNODE
  1274. ****************************************************************************}
  1275. constructor tcallnode.create(l:tnode;v : tprocsym;st : TSymtable; mp: tnode; callflags:tcallnodeflags);
  1276. begin
  1277. inherited create(calln,l,nil);
  1278. symtableprocentry:=v;
  1279. symtableproc:=st;
  1280. callnodeflags:=callflags+[cnf_return_value_used];
  1281. methodpointer:=mp;
  1282. callinitblock:=nil;
  1283. callcleanupblock:=nil;
  1284. procdefinition:=nil;
  1285. funcretnode:=nil;
  1286. paralength:=-1;
  1287. varargsparas:=nil;
  1288. if assigned(current_structdef) and
  1289. assigned(mp) then
  1290. begin
  1291. { only needed when calling a destructor from an exception block in a
  1292. contructor of a TP-style object }
  1293. if is_object(current_structdef) and
  1294. (current_procinfo.procdef.proctypeoption=potype_constructor) and
  1295. (cnf_create_failed in callflags) then
  1296. call_vmt_node:=load_vmt_pointer_node;
  1297. end;
  1298. end;
  1299. constructor tcallnode.create_procvar(l,r:tnode);
  1300. begin
  1301. create(l,nil,nil,nil,[]);
  1302. right:=r;
  1303. end;
  1304. constructor tcallnode.createintern(const name: string; params: tnode);
  1305. var
  1306. srsym: tsym;
  1307. begin
  1308. srsym := tsym(systemunit.Find(name));
  1309. { in case we are looking for a non-external compilerproc of which we
  1310. only have parsed the declaration until now (the symbol name will
  1311. still be uppercased, because it needs to be matched when we
  1312. encounter the implementation) }
  1313. if not assigned(srsym) and
  1314. (cs_compilesystem in current_settings.moduleswitches) then
  1315. srsym := tsym(systemunit.Find(upper(name)));
  1316. if not assigned(srsym) or
  1317. (srsym.typ<>procsym) then
  1318. Message1(cg_f_unknown_compilerproc,name);
  1319. create(params,tprocsym(srsym),srsym.owner,nil,[]);
  1320. end;
  1321. constructor tcallnode.createinternfromunit(const fromunit, procname: string; params: tnode);
  1322. var
  1323. srsym: tsym;
  1324. srsymtable: tsymtable;
  1325. begin
  1326. srsym:=nil;
  1327. if not searchsym_in_named_module(fromunit,procname,srsym,srsymtable) or
  1328. (srsym.typ<>procsym) then
  1329. Message1(cg_f_unknown_compilerproc,fromunit+'.'+procname);
  1330. create(params,tprocsym(srsym),srsymtable,nil,[]);
  1331. end;
  1332. constructor tcallnode.createinternres(const name: string; params: tnode; res:tdef);
  1333. var
  1334. pd : tprocdef;
  1335. begin
  1336. createintern(name,params);
  1337. typedef:=res;
  1338. include(callnodeflags,cnf_typedefset);
  1339. pd:=tprocdef(symtableprocentry.ProcdefList[0]);
  1340. { both the normal and specified resultdef either have to be returned via a }
  1341. { parameter or not, but no mixing (JM) }
  1342. if paramanager.ret_in_param(typedef,pd) xor
  1343. paramanager.ret_in_param(pd.returndef,pd) then
  1344. internalerror(2001082911);
  1345. end;
  1346. constructor tcallnode.createinternresfromunit(const fromunit, procname: string; params: tnode; res:tdef);
  1347. var
  1348. pd : tprocdef;
  1349. begin
  1350. createinternfromunit(fromunit,procname,params);
  1351. typedef:=res;
  1352. include(callnodeflags,cnf_typedefset);
  1353. pd:=tprocdef(symtableprocentry.ProcdefList[0]);
  1354. { both the normal and specified resultdef either have to be returned via a }
  1355. { parameter or not, but no mixing (JM) }
  1356. if paramanager.ret_in_param(typedef,pd) xor
  1357. paramanager.ret_in_param(pd.returndef,pd) then
  1358. internalerror(200108291);
  1359. end;
  1360. constructor tcallnode.createinternreturn(const name: string; params: tnode; returnnode : tnode);
  1361. begin
  1362. createintern(name,params);
  1363. funcretnode:=returnnode;
  1364. end;
  1365. constructor tcallnode.createinternmethod(mp: tnode; const name: string; params: tnode);
  1366. var
  1367. ps: tsym;
  1368. recdef: tabstractrecorddef;
  1369. begin
  1370. typecheckpass(mp);
  1371. if mp.resultdef.typ=classrefdef then
  1372. recdef:=tabstractrecorddef(tclassrefdef(mp.resultdef).pointeddef)
  1373. else
  1374. recdef:=tabstractrecorddef(mp.resultdef);
  1375. ps:=search_struct_member(recdef,name);
  1376. if not assigned(ps) or
  1377. (ps.typ<>procsym) then
  1378. internalerror(2011062806);
  1379. create(params,tprocsym(ps),ps.owner,mp,[]);
  1380. end;
  1381. constructor tcallnode.createinternmethodres(mp: tnode; const name: string; params: tnode; res: tdef);
  1382. begin
  1383. createinternmethod(mp,name,params);
  1384. typedef:=res;
  1385. include(callnodeflags,cnf_typedefset)
  1386. end;
  1387. destructor tcallnode.destroy;
  1388. begin
  1389. methodpointer.free;
  1390. callinitblock.free;
  1391. callcleanupblock.free;
  1392. funcretnode.free;
  1393. if assigned(varargsparas) then
  1394. varargsparas.free;
  1395. call_self_node.free;
  1396. call_vmt_node.free;
  1397. vmt_entry.free;
  1398. {$ifndef symansistr}
  1399. stringdispose(fforcedprocname);
  1400. {$endif symansistr}
  1401. inherited destroy;
  1402. end;
  1403. constructor tcallnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1404. begin
  1405. callinitblock:=tblocknode(ppuloadnode(ppufile));
  1406. methodpointer:=ppuloadnode(ppufile);
  1407. call_self_node:=ppuloadnode(ppufile);
  1408. call_vmt_node:=ppuloadnode(ppufile);
  1409. callcleanupblock:=tblocknode(ppuloadnode(ppufile));
  1410. funcretnode:=ppuloadnode(ppufile);
  1411. inherited ppuload(t,ppufile);
  1412. ppufile.getderef(symtableprocentryderef);
  1413. { TODO: FIXME: No withsymtable support}
  1414. symtableproc:=nil;
  1415. ppufile.getderef(procdefinitionderef);
  1416. ppufile.getsmallset(callnodeflags);
  1417. end;
  1418. procedure tcallnode.ppuwrite(ppufile:tcompilerppufile);
  1419. begin
  1420. ppuwritenode(ppufile,callinitblock);
  1421. ppuwritenode(ppufile,methodpointer);
  1422. ppuwritenode(ppufile,call_self_node);
  1423. ppuwritenode(ppufile,call_vmt_node);
  1424. ppuwritenode(ppufile,callcleanupblock);
  1425. ppuwritenode(ppufile,funcretnode);
  1426. inherited ppuwrite(ppufile);
  1427. ppufile.putderef(symtableprocentryderef);
  1428. ppufile.putderef(procdefinitionderef);
  1429. ppufile.putsmallset(callnodeflags);
  1430. end;
  1431. procedure tcallnode.buildderefimpl;
  1432. begin
  1433. inherited buildderefimpl;
  1434. symtableprocentryderef.build(symtableprocentry);
  1435. procdefinitionderef.build(procdefinition);
  1436. if assigned(methodpointer) then
  1437. methodpointer.buildderefimpl;
  1438. if assigned(call_self_node) then
  1439. call_self_node.buildderefimpl;
  1440. if assigned(call_vmt_node) then
  1441. call_vmt_node.buildderefimpl;
  1442. if assigned(callinitblock) then
  1443. callinitblock.buildderefimpl;
  1444. if assigned(callcleanupblock) then
  1445. callcleanupblock.buildderefimpl;
  1446. if assigned(funcretnode) then
  1447. funcretnode.buildderefimpl;
  1448. end;
  1449. procedure tcallnode.derefimpl;
  1450. var
  1451. pt : tcallparanode;
  1452. i : integer;
  1453. begin
  1454. inherited derefimpl;
  1455. symtableprocentry:=tprocsym(symtableprocentryderef.resolve);
  1456. if assigned(symtableprocentry) then
  1457. symtableproc:=symtableprocentry.owner;
  1458. procdefinition:=tabstractprocdef(procdefinitionderef.resolve);
  1459. if assigned(methodpointer) then
  1460. methodpointer.derefimpl;
  1461. if assigned(call_self_node) then
  1462. call_self_node.derefimpl;
  1463. if assigned(call_vmt_node) then
  1464. call_vmt_node.derefimpl;
  1465. if assigned(callinitblock) then
  1466. callinitblock.derefimpl;
  1467. if assigned(callcleanupblock) then
  1468. callcleanupblock.derefimpl;
  1469. if assigned(funcretnode) then
  1470. funcretnode.derefimpl;
  1471. { generic method has no procdefinition }
  1472. if assigned(procdefinition) then
  1473. begin
  1474. { Connect parasyms }
  1475. pt:=tcallparanode(left);
  1476. while assigned(pt) and
  1477. (cpf_varargs_para in pt.callparaflags) do
  1478. pt:=tcallparanode(pt.right);
  1479. for i:=procdefinition.paras.count-1 downto 0 do
  1480. begin
  1481. if not assigned(pt) then
  1482. internalerror(200311077);
  1483. pt.parasym:=tparavarsym(procdefinition.paras[i]);
  1484. pt:=tcallparanode(pt.right);
  1485. end;
  1486. if assigned(pt) then
  1487. internalerror(200311078);
  1488. end;
  1489. end;
  1490. function tcallnode.dogetcopy : tnode;
  1491. var
  1492. n : tcallnode;
  1493. i : integer;
  1494. hp,hpn : tparavarsym;
  1495. oldleft : tnode;
  1496. para: tcallparanode;
  1497. begin
  1498. { Need to use a hack here to prevent the parameters from being copied.
  1499. The parameters must be copied between callinitblock/callcleanupblock because
  1500. they can reference methodpointer }
  1501. oldleft:=left;
  1502. left:=nil;
  1503. n:=tcallnode(inherited dogetcopy);
  1504. left:=oldleft;
  1505. n.symtableprocentry:=symtableprocentry;
  1506. n.symtableproc:=symtableproc;
  1507. n.procdefinition:=procdefinition;
  1508. n.typedef := typedef;
  1509. n.callnodeflags := callnodeflags;
  1510. if assigned(callinitblock) then
  1511. n.callinitblock:=tblocknode(callinitblock.dogetcopy)
  1512. else
  1513. n.callinitblock:=nil;
  1514. { callinitblock is copied, now references to the temp will also be copied
  1515. correctly. We can now copy the parameters, funcret and methodpointer }
  1516. if assigned(left) then
  1517. n.left:=left.dogetcopy
  1518. else
  1519. n.left:=nil;
  1520. if assigned(methodpointer) then
  1521. n.methodpointer:=methodpointer.dogetcopy
  1522. else
  1523. n.methodpointer:=nil;
  1524. if assigned(call_self_node) then
  1525. n.call_self_node:=call_self_node.dogetcopy
  1526. else
  1527. n.call_self_node:=nil;
  1528. if assigned(call_vmt_node) then
  1529. n.call_vmt_node:=call_vmt_node.dogetcopy
  1530. else
  1531. n.call_vmt_node:=nil;
  1532. if assigned(vmt_entry) then
  1533. n.vmt_entry:=vmt_entry.dogetcopy
  1534. else
  1535. n.vmt_entry:=nil;
  1536. { must be copied before the funcretnode, because the callcleanup block
  1537. may contain a ttempdeletenode that sets the tempinfo of the
  1538. corresponding temp to ti_nextref_set_hookoncopy_nil, and this nextref
  1539. itself may be the funcretnode }
  1540. if assigned(callcleanupblock) then
  1541. n.callcleanupblock:=tblocknode(callcleanupblock.dogetcopy)
  1542. else
  1543. n.callcleanupblock:=nil;
  1544. if assigned(funcretnode) then
  1545. n.funcretnode:=funcretnode.dogetcopy
  1546. else
  1547. n.funcretnode:=nil;
  1548. if assigned(varargsparas) then
  1549. begin
  1550. n.varargsparas:=tvarargsparalist.create(true);
  1551. for i:=0 to varargsparas.count-1 do
  1552. begin
  1553. hp:=tparavarsym(varargsparas[i]);
  1554. hpn:=cparavarsym.create(hp.realname,hp.paranr,hp.varspez,hp.vardef,[]);
  1555. n.varargsparas.add(hpn);
  1556. para:=tcallparanode(n.left);
  1557. while assigned(para) do
  1558. begin
  1559. if (para.parasym=hp) then
  1560. para.parasym:=hpn;
  1561. para:=tcallparanode(para.right);
  1562. end;
  1563. end;
  1564. end
  1565. else
  1566. n.varargsparas:=nil;
  1567. {$ifdef symansistr}
  1568. n.fforcedprocname:=fforcedprocname;
  1569. {$else symansistr}
  1570. if assigned(fforcedprocname) then
  1571. n.fforcedprocname:=stringdup(fforcedprocname^)
  1572. else
  1573. n.fforcedprocname:=nil;
  1574. {$endif symansistr}
  1575. result:=n;
  1576. end;
  1577. function tcallnode.docompare(p: tnode): boolean;
  1578. begin
  1579. docompare :=
  1580. inherited docompare(p) and
  1581. (symtableprocentry = tcallnode(p).symtableprocentry) and
  1582. (procdefinition = tcallnode(p).procdefinition) and
  1583. { this implicitly also compares the vmt_entry node, as it is
  1584. deterministically based on the methodpointer }
  1585. (methodpointer.isequal(tcallnode(p).methodpointer)) and
  1586. (((cnf_typedefset in callnodeflags) and (cnf_typedefset in tcallnode(p).callnodeflags) and
  1587. (equal_defs(typedef,tcallnode(p).typedef))) or
  1588. (not(cnf_typedefset in callnodeflags) and not(cnf_typedefset in tcallnode(p).callnodeflags)));
  1589. end;
  1590. procedure tcallnode.printnodedata(var t:text);
  1591. begin
  1592. if assigned(procdefinition) and
  1593. (procdefinition.typ=procdef) then
  1594. writeln(t,printnodeindention,'proc = ',tprocdef(procdefinition).fullprocname(true))
  1595. else
  1596. begin
  1597. if assigned(symtableprocentry) then
  1598. writeln(t,printnodeindention,'proc = ',symtableprocentry.name)
  1599. else
  1600. writeln(t,printnodeindention,'proc = <nil>');
  1601. end;
  1602. if assigned(methodpointer) then
  1603. begin
  1604. writeln(t,printnodeindention,'methodpointer =');
  1605. printnode(t,methodpointer);
  1606. end;
  1607. if assigned(callinitblock) then
  1608. begin
  1609. writeln(t,printnodeindention,'callinitblock =');
  1610. printnode(t,callinitblock);
  1611. end;
  1612. if assigned(callcleanupblock) then
  1613. begin
  1614. writeln(t,printnodeindention,'callcleanupblock =');
  1615. printnode(t,callcleanupblock);
  1616. end;
  1617. if assigned(right) then
  1618. begin
  1619. writeln(t,printnodeindention,'right =');
  1620. printnode(t,right);
  1621. end;
  1622. if assigned(left) then
  1623. begin
  1624. writeln(t,printnodeindention,'left =');
  1625. printnode(t,left);
  1626. end;
  1627. end;
  1628. procedure tcallnode.insertintolist(l : tnodelist);
  1629. begin
  1630. end;
  1631. procedure tcallnode.add_init_statement(n:tnode);
  1632. var
  1633. lastinitstatement : tstatementnode;
  1634. begin
  1635. if not assigned(callinitblock) then
  1636. callinitblock:=internalstatements(lastinitstatement)
  1637. else
  1638. lastinitstatement:=laststatement(callinitblock);
  1639. { all these nodes must be immediately typechecked, because this routine }
  1640. { can be called from pass_1 (i.e., after typecheck has already run) and }
  1641. { moreover, the entire blocks themselves are also only typechecked in }
  1642. { pass_1, while the the typeinfo is already required after the }
  1643. { typecheck pass for simplify purposes (not yet perfect, because the }
  1644. { statementnodes themselves are not typechecked this way) }
  1645. firstpass(n);
  1646. addstatement(lastinitstatement,n);
  1647. end;
  1648. procedure tcallnode.add_done_statement(n:tnode);
  1649. var
  1650. lastdonestatement : tstatementnode;
  1651. begin
  1652. if not assigned(callcleanupblock) then
  1653. callcleanupblock:=internalstatements(lastdonestatement)
  1654. else
  1655. lastdonestatement:=laststatement(callcleanupblock);
  1656. { see comments in add_init_statement }
  1657. firstpass(n);
  1658. addstatement(lastdonestatement,n);
  1659. end;
  1660. function tcallnode.para_count:longint;
  1661. var
  1662. ppn : tcallparanode;
  1663. begin
  1664. result:=0;
  1665. ppn:=tcallparanode(left);
  1666. while assigned(ppn) do
  1667. begin
  1668. if not(assigned(ppn.parasym) and
  1669. (vo_is_hidden_para in ppn.parasym.varoptions)) then
  1670. inc(result);
  1671. ppn:=tcallparanode(ppn.right);
  1672. end;
  1673. end;
  1674. function tcallnode.required_para_count: longint;
  1675. var
  1676. ppn : tcallparanode;
  1677. begin
  1678. result:=0;
  1679. ppn:=tcallparanode(left);
  1680. while assigned(ppn) do
  1681. begin
  1682. if not(assigned(ppn.parasym) and
  1683. ((vo_is_hidden_para in ppn.parasym.varoptions) or
  1684. assigned(ppn.parasym.defaultconstsym))) then
  1685. inc(result);
  1686. ppn:=tcallparanode(ppn.right);
  1687. end;
  1688. end;
  1689. function tcallnode.is_simple_para_load(p:tnode; may_be_in_reg: boolean):boolean;
  1690. var
  1691. hp : tnode;
  1692. begin
  1693. hp:=p;
  1694. while assigned(hp) and
  1695. (hp.nodetype=typeconvn) and
  1696. (ttypeconvnode(hp).convtype=tc_equal) do
  1697. hp:=tunarynode(hp).left;
  1698. result:=(hp.nodetype in [typen,loadvmtaddrn,loadn,temprefn,arrayconstructorn,addrn]);
  1699. if result and
  1700. not(may_be_in_reg) then
  1701. case hp.nodetype of
  1702. loadn:
  1703. result:=(tabstractvarsym(tloadnode(hp).symtableentry).varregable in [vr_none,vr_addr]);
  1704. temprefn:
  1705. result:=not(ti_may_be_in_reg in ttemprefnode(hp).tempinfo^.flags);
  1706. end;
  1707. end;
  1708. function tcallnode.getforcedprocname: TSymStr;
  1709. begin
  1710. {$ifdef symansistr}
  1711. result:=fforcedprocname;
  1712. {$else}
  1713. if assigned(fforcedprocname) then
  1714. result:=fforcedprocname^
  1715. else
  1716. result:='';
  1717. {$endif}
  1718. end;
  1719. function look_for_call(var n: tnode; arg: pointer): foreachnoderesult;
  1720. begin
  1721. case n.nodetype of
  1722. calln,asn:
  1723. result := fen_norecurse_true;
  1724. typen,loadvmtaddrn,loadn,temprefn,arrayconstructorn:
  1725. result := fen_norecurse_false;
  1726. else
  1727. result := fen_false;
  1728. end;
  1729. end;
  1730. procedure tcallnode.maybe_load_in_temp(var p:tnode);
  1731. var
  1732. loadp,
  1733. refp : tnode;
  1734. hdef : tdef;
  1735. ptemp : ttempcreatenode;
  1736. usederef : boolean;
  1737. begin
  1738. { Load all complex loads into a temp to prevent
  1739. double calls to a function. We can't simply check for a hp.nodetype=calln }
  1740. if assigned(p) and
  1741. foreachnodestatic(p,@look_for_call,nil) then
  1742. begin
  1743. { temp create }
  1744. usederef:=(p.resultdef.typ in [arraydef,recorddef]) or
  1745. is_shortstring(p.resultdef) or
  1746. is_object(p.resultdef);
  1747. if usederef then
  1748. hdef:=cpointerdef.getreusable(p.resultdef)
  1749. else
  1750. hdef:=p.resultdef;
  1751. ptemp:=ctempcreatenode.create(hdef,hdef.size,tt_persistent,true);
  1752. if usederef then
  1753. begin
  1754. loadp:=caddrnode.create_internal(p);
  1755. refp:=cderefnode.create(ctemprefnode.create(ptemp));
  1756. end
  1757. else
  1758. begin
  1759. loadp:=p;
  1760. refp:=ctemprefnode.create(ptemp)
  1761. end;
  1762. add_init_statement(ptemp);
  1763. add_init_statement(cassignmentnode.create(
  1764. ctemprefnode.create(ptemp),
  1765. loadp));
  1766. add_done_statement(ctempdeletenode.create(ptemp));
  1767. { new tree is only a temp reference }
  1768. p:=refp;
  1769. typecheckpass(p);
  1770. end;
  1771. end;
  1772. function tcallnode.gen_high_tree(var p:tnode;paradef:tdef):tnode;
  1773. { When passing an array to an open array, or a string to an open string,
  1774. some code is needed that generates the high bound of the array. This
  1775. function returns a tree containing the nodes for it. }
  1776. var
  1777. temp: tnode;
  1778. len : integer;
  1779. loadconst : boolean;
  1780. hightree,l,r : tnode;
  1781. defkind: tdeftyp;
  1782. begin
  1783. len:=-1;
  1784. loadconst:=true;
  1785. hightree:=nil;
  1786. { constant strings are internally stored as array of char, but if the
  1787. parameter is a string also treat it like one }
  1788. defkind:=p.resultdef.typ;
  1789. if (p.nodetype=stringconstn) and
  1790. (paradef.typ=stringdef) then
  1791. defkind:=stringdef;
  1792. case defkind of
  1793. arraydef :
  1794. begin
  1795. if (paradef.typ<>arraydef) then
  1796. internalerror(200405241);
  1797. { passing a string to an array of char }
  1798. if (p.nodetype=stringconstn) and
  1799. is_char(tarraydef(paradef).elementdef) then
  1800. begin
  1801. len:=tstringconstnode(p).len;
  1802. if len>0 then
  1803. dec(len);
  1804. end
  1805. else
  1806. { handle special case of passing an single array to an array of array }
  1807. if compare_defs(tarraydef(paradef).elementdef,p.resultdef,nothingn)>=te_equal then
  1808. len:=0
  1809. else
  1810. begin
  1811. { handle via a normal inline in_high_x node }
  1812. loadconst:=false;
  1813. { slice? }
  1814. if (p.nodetype=inlinen) and (tinlinenode(p).inlinenumber=in_slice_x) then
  1815. with Tcallparanode(Tinlinenode(p).left) do
  1816. begin
  1817. {Array slice using slice builtin function.}
  1818. l:=Tcallparanode(right).left;
  1819. hightree:=caddnode.create(subn,l,genintconstnode(1));
  1820. Tcallparanode(right).left:=nil;
  1821. {Remove the inline node.}
  1822. temp:=p;
  1823. p:=left;
  1824. Tcallparanode(tinlinenode(temp).left).left:=nil;
  1825. temp.free;
  1826. typecheckpass(hightree);
  1827. end
  1828. else if (p.nodetype=vecn) and (Tvecnode(p).right.nodetype=rangen) then
  1829. begin
  1830. {Array slice using .. operator.}
  1831. with Trangenode(Tvecnode(p).right) do
  1832. begin
  1833. l:=left; {Get lower bound.}
  1834. r:=right; {Get upper bound.}
  1835. end;
  1836. {In the procedure the array range is 0..(upper_bound-lower_bound).}
  1837. hightree:=caddnode.create(subn,r,l);
  1838. {Replace the rangnode in the tree by its lower_bound, and
  1839. dispose the rangenode.}
  1840. temp:=Tvecnode(p).right;
  1841. Tvecnode(p).right:=l.getcopy;
  1842. {Typecheckpass can only be performed *after* the l.getcopy since it
  1843. can modify the tree, and l is in the hightree.}
  1844. typecheckpass(hightree);
  1845. with Trangenode(temp) do
  1846. begin
  1847. left:=nil;
  1848. right:=nil;
  1849. end;
  1850. temp.free;
  1851. {Tree changed from p[l..h] to p[l], recalculate resultdef.}
  1852. p.resultdef:=nil;
  1853. typecheckpass(p);
  1854. end
  1855. else
  1856. begin
  1857. maybe_load_in_temp(p);
  1858. hightree:=geninlinenode(in_high_x,false,p.getcopy);
  1859. typecheckpass(hightree);
  1860. { only substract low(array) if it's <> 0 }
  1861. temp:=geninlinenode(in_low_x,false,p.getcopy);
  1862. typecheckpass(temp);
  1863. if (temp.nodetype <> ordconstn) or
  1864. (tordconstnode(temp).value <> 0) then
  1865. begin
  1866. hightree:=caddnode.create(subn,hightree,temp);
  1867. include(hightree.flags,nf_internal);
  1868. end
  1869. else
  1870. temp.free;
  1871. end;
  1872. end;
  1873. end;
  1874. stringdef :
  1875. begin
  1876. if is_open_string(paradef) then
  1877. begin
  1878. { a stringconstn is not a simple parameter and hence would be
  1879. loaded in a temp, but in that case the high() node
  1880. a) goes wrong (it cannot deal with a temp node)
  1881. b) would give a generic result instead of one specific to
  1882. this constant string
  1883. }
  1884. if p.nodetype<>stringconstn then
  1885. maybe_load_in_temp(p);
  1886. { handle via a normal inline in_high_x node }
  1887. loadconst := false;
  1888. hightree := geninlinenode(in_high_x,false,p.getcopy);
  1889. end
  1890. else
  1891. { handle special case of passing an single string to an array of string }
  1892. if compare_defs(tarraydef(paradef).elementdef,p.resultdef,nothingn)>=te_equal then
  1893. len:=0
  1894. else
  1895. { passing a string to an array of char }
  1896. if (p.nodetype=stringconstn) and
  1897. is_char(tarraydef(paradef).elementdef) then
  1898. begin
  1899. len:=tstringconstnode(p).len;
  1900. if len>0 then
  1901. dec(len);
  1902. end
  1903. else
  1904. begin
  1905. maybe_load_in_temp(p);
  1906. hightree:=caddnode.create(subn,geninlinenode(in_length_x,false,p.getcopy),
  1907. cordconstnode.create(1,sinttype,false));
  1908. loadconst:=false;
  1909. end;
  1910. end;
  1911. else
  1912. len:=0;
  1913. end;
  1914. if loadconst then
  1915. hightree:=cordconstnode.create(len,sinttype,true)
  1916. else
  1917. begin
  1918. if not assigned(hightree) then
  1919. internalerror(200304071);
  1920. { Need to use explicit, because it can also be a enum }
  1921. hightree:=ctypeconvnode.create_internal(hightree,sinttype);
  1922. end;
  1923. result:=hightree;
  1924. end;
  1925. function tcallnode.gen_procvar_context_tree_self:tnode;
  1926. begin
  1927. { Load tmehodpointer(right).self }
  1928. result:=genloadfield(ctypeconvnode.create_internal(
  1929. right.getcopy,methodpointertype),
  1930. 'self');
  1931. end;
  1932. function tcallnode.gen_procvar_context_tree_parentfp: tnode;
  1933. begin
  1934. { Load tnestedprocpointer(right).parentfp }
  1935. result:=genloadfield(ctypeconvnode.create_internal(
  1936. right.getcopy,nestedprocpointertype),
  1937. 'parentfp');
  1938. end;
  1939. function tcallnode.gen_self_tree:tnode;
  1940. var
  1941. selftree : tnode;
  1942. selfdef : tdef;
  1943. temp : ttempcreatenode;
  1944. begin
  1945. selftree:=nil;
  1946. { When methodpointer was a callnode we must load it first into a
  1947. temp to prevent processing the callnode twice }
  1948. if (methodpointer.nodetype=calln) then
  1949. internalerror(200405121);
  1950. { Objective-C: objc_convert_to_message_send() already did all necessary
  1951. transformation on the methodpointer }
  1952. if (procdefinition.typ=procdef) and
  1953. (po_objc in tprocdef(procdefinition).procoptions) then
  1954. selftree:=methodpointer.getcopy
  1955. { inherited }
  1956. else if (cnf_inherited in callnodeflags) then
  1957. begin
  1958. selftree:=safe_call_self_node.getcopy;
  1959. { we can call an inherited class static/method from a regular method
  1960. -> self node must change from instance pointer to vmt pointer)
  1961. }
  1962. if (procdefinition.procoptions*[po_classmethod,po_staticmethod] <> []) and
  1963. (selftree.resultdef.typ<>classrefdef) then
  1964. selftree:=cloadvmtaddrnode.create(selftree);
  1965. end
  1966. else
  1967. { constructors }
  1968. if (procdefinition.proctypeoption=potype_constructor) then
  1969. begin
  1970. if (methodpointer.resultdef.typ=classrefdef) or
  1971. (cnf_new_call in callnodeflags) then
  1972. if not is_javaclass(tdef(procdefinition.owner.defowner)) then
  1973. begin
  1974. if (cnf_new_call in callnodeflags) then
  1975. { old-style object: push 0 as self }
  1976. selftree:=cpointerconstnode.create(0,voidpointertype)
  1977. else
  1978. begin
  1979. { class-style: push classtype }
  1980. selftree:=methodpointer.getcopy;
  1981. if selftree.nodetype=typen then
  1982. begin
  1983. selftree:=cloadvmtaddrnode.create(selftree);
  1984. tloadvmtaddrnode(selftree).forcall:=true;
  1985. end;
  1986. end;
  1987. end
  1988. else
  1989. { special handling for Java constructors, handled in
  1990. tjvmcallnode.extra_pre_call_code }
  1991. selftree:=cnothingnode.create
  1992. else
  1993. begin
  1994. if methodpointer.nodetype=typen then
  1995. if (methodpointer.resultdef.typ<>objectdef) then
  1996. begin
  1997. if not(target_info.system in systems_jvm) then
  1998. begin
  1999. { TSomeRecord.Constructor call. We need to allocate }
  2000. { self node as a temp node of the result type }
  2001. temp:=ctempcreatenode.create(methodpointer.resultdef,methodpointer.resultdef.size,tt_persistent,false);
  2002. add_init_statement(temp);
  2003. add_done_statement(ctempdeletenode.create_normal_temp(temp));
  2004. selftree:=ctemprefnode.create(temp);
  2005. end
  2006. else
  2007. begin
  2008. { special handling for Java constructors, handled in
  2009. tjvmcallnode.extra_pre_call_code }
  2010. selftree:=cnothingnode.create
  2011. end;
  2012. end
  2013. else
  2014. selftree:=safe_call_self_node.getcopy
  2015. else
  2016. selftree:=methodpointer.getcopy;
  2017. end;
  2018. end
  2019. else
  2020. { Calling a static/class method }
  2021. if (po_classmethod in procdefinition.procoptions) or
  2022. (po_staticmethod in procdefinition.procoptions) then
  2023. begin
  2024. if (procdefinition.typ<>procdef) then
  2025. internalerror(200305062);
  2026. { if the method belongs to a helper then we need to use the
  2027. extended type for references to Self }
  2028. if is_objectpascal_helper(tprocdef(procdefinition).struct) then
  2029. selfdef:=tobjectdef(tprocdef(procdefinition).struct).extendeddef
  2030. else
  2031. selfdef:=tprocdef(procdefinition).struct;
  2032. if ((selfdef.typ in [recorddef,objectdef]) and
  2033. (oo_has_vmt in tabstractrecorddef(selfdef).objectoptions)) or
  2034. { all Java classes have a "VMT" }
  2035. (target_info.system in systems_jvm) then
  2036. begin
  2037. { we only need the vmt, loading self is not required and there is no
  2038. need to check for typen, because that will always get the
  2039. loadvmtaddrnode added }
  2040. selftree:=methodpointer.getcopy;
  2041. if (methodpointer.resultdef.typ<>classrefdef) or
  2042. (methodpointer.nodetype = typen) then
  2043. selftree:=cloadvmtaddrnode.create(selftree);
  2044. end
  2045. else
  2046. selftree:=cpointerconstnode.create(0,voidpointertype);
  2047. end
  2048. else
  2049. begin
  2050. if methodpointer.nodetype=typen then
  2051. selftree:=safe_call_self_node.getcopy
  2052. else
  2053. selftree:=methodpointer.getcopy;
  2054. end;
  2055. result:=selftree;
  2056. end;
  2057. function tcallnode.use_caller_self(check_for_callee_self: boolean): boolean;
  2058. var
  2059. i: longint;
  2060. ps: tparavarsym;
  2061. begin
  2062. result:=false;
  2063. { is there a self parameter? }
  2064. if check_for_callee_self then
  2065. begin
  2066. ps:=nil;
  2067. for i:=0 to procdefinition.paras.count-1 do
  2068. begin
  2069. ps:=tparavarsym(procdefinition.paras[i]);
  2070. if vo_is_self in ps.varoptions then
  2071. break;
  2072. ps:=nil;
  2073. end;
  2074. if not assigned(ps) then
  2075. exit;
  2076. end;
  2077. { we need to load the'self' parameter of the current routine as the
  2078. 'self' parameter of the called routine if
  2079. 1) we're calling an inherited routine
  2080. 2) we're calling a constructor via type.constructorname and
  2081. type is not a classrefdef (i.e., we're calling a constructor like
  2082. a regular method)
  2083. 3) we're calling any regular (non-class/non-static) method via
  2084. a typenode (the methodpointer is then that typenode, but the
  2085. passed self node must become the current self node)
  2086. In other cases, we either don't have to pass the 'self' parameter of
  2087. the current routine to the called one, or methodpointer will already
  2088. contain it (e.g. because a method was called via "method", in which
  2089. case the parser already passed 'self' as the method pointer, or via
  2090. "self.method") }
  2091. if (cnf_inherited in callnodeflags) or
  2092. ((procdefinition.proctypeoption=potype_constructor) and
  2093. not((methodpointer.resultdef.typ=classrefdef) or
  2094. (cnf_new_call in callnodeflags)) and
  2095. (methodpointer.nodetype=typen) and
  2096. (methodpointer.resultdef.typ=objectdef)) or
  2097. (assigned(methodpointer) and
  2098. (procdefinition.proctypeoption<>potype_constructor) and
  2099. not(po_classmethod in procdefinition.procoptions) and
  2100. not(po_staticmethod in procdefinition.procoptions) and
  2101. (methodpointer.nodetype=typen)) then
  2102. result:=true;
  2103. end;
  2104. procedure tcallnode.maybe_gen_call_self_node;
  2105. begin
  2106. if cnf_call_self_node_done in callnodeflags then
  2107. exit;
  2108. include(callnodeflags,cnf_call_self_node_done);
  2109. if use_caller_self(true) then
  2110. call_self_node:=load_self_node;
  2111. end;
  2112. procedure tcallnode.register_created_object_types;
  2113. function checklive(def: tdef): boolean;
  2114. begin
  2115. if assigned(current_procinfo) and
  2116. not(po_inline in current_procinfo.procdef.procoptions) and
  2117. not wpoinfomanager.symbol_live(current_procinfo.procdef.mangledname) then
  2118. begin
  2119. {$ifdef debug_deadcode}
  2120. writeln(' NOT adding creadion of ',def.typename,' because performed in dead stripped proc: ',current_procinfo.procdef.typename);
  2121. {$endif debug_deadcode}
  2122. result:=false;
  2123. end
  2124. else
  2125. result:=true;
  2126. end;
  2127. var
  2128. crefdef,
  2129. systobjectdef : tdef;
  2130. begin
  2131. { only makes sense for methods }
  2132. if not assigned(methodpointer) then
  2133. exit;
  2134. if (methodpointer.resultdef.typ=classrefdef) then
  2135. begin
  2136. { constructor call via classreference => allocate memory }
  2137. if (procdefinition.proctypeoption=potype_constructor) then
  2138. begin
  2139. { Only a typenode can be passed when it is called with <class of xx>.create }
  2140. if (methodpointer.nodetype=typen) then
  2141. begin
  2142. if checklive(methodpointer.resultdef) then
  2143. { we know the exact class type being created }
  2144. tclassrefdef(methodpointer.resultdef).pointeddef.register_created_object_type
  2145. end
  2146. else
  2147. begin
  2148. { the loadvmtaddrnode is already created in case of classtype.create }
  2149. if (methodpointer.nodetype=loadvmtaddrn) and
  2150. (tloadvmtaddrnode(methodpointer).left.nodetype=typen) then
  2151. begin
  2152. if checklive(methodpointer.resultdef) then
  2153. tclassrefdef(methodpointer.resultdef).pointeddef.register_created_object_type
  2154. end
  2155. else
  2156. begin
  2157. if checklive(methodpointer.resultdef) then
  2158. begin
  2159. { special case: if the classref comes from x.classtype (with classtype,
  2160. being tobject.classtype) then the created instance is x or a descendant
  2161. of x (rather than tobject or a descendant of tobject)
  2162. }
  2163. systobjectdef:=search_system_type('TOBJECT').typedef;
  2164. if (methodpointer.nodetype=calln) and
  2165. { not a procvar call }
  2166. not assigned(right) and
  2167. { procdef is owned by system.tobject }
  2168. (tprocdef(tcallnode(methodpointer).procdefinition).owner.defowner=systobjectdef) and
  2169. { we're calling system.tobject.classtype }
  2170. (tcallnode(methodpointer).symtableprocentry.name='CLASSTYPE') and
  2171. { could again be a classrefdef, but unlikely }
  2172. (tcallnode(methodpointer).methodpointer.resultdef.typ=objectdef) and
  2173. { don't go through this trouble if it was already a tobject }
  2174. (tcallnode(methodpointer).methodpointer.resultdef<>systobjectdef) then
  2175. begin
  2176. { register this object type as classref, so all descendents will also
  2177. be marked as instantiatable (only the pointeddef will actually be
  2178. recorded, so it's no problem that the clasrefdef is only temporary)
  2179. }
  2180. crefdef:=cclassrefdef.create(tcallnode(methodpointer).methodpointer.resultdef);
  2181. { and register it }
  2182. crefdef.register_created_object_type;
  2183. end
  2184. else
  2185. { the created class can be any child class as well -> register classrefdef }
  2186. methodpointer.resultdef.register_created_object_type;
  2187. end;
  2188. end;
  2189. end;
  2190. end
  2191. end
  2192. else
  2193. { Old style object }
  2194. if is_object(methodpointer.resultdef) then
  2195. begin
  2196. { constructor with extended syntax called from new }
  2197. if (cnf_new_call in callnodeflags) then
  2198. begin
  2199. if checklive(methodpointer.resultdef) then
  2200. methodpointer.resultdef.register_created_object_type;
  2201. end
  2202. else
  2203. { normal object call like obj.proc }
  2204. if not(cnf_dispose_call in callnodeflags) and
  2205. not(cnf_inherited in callnodeflags) and
  2206. not(cnf_member_call in callnodeflags) then
  2207. begin
  2208. if (procdefinition.proctypeoption=potype_constructor) then
  2209. begin
  2210. if (methodpointer.nodetype<>typen) and
  2211. checklive(methodpointer.resultdef) then
  2212. methodpointer.resultdef.register_created_object_type;
  2213. end
  2214. end;
  2215. end;
  2216. end;
  2217. function tcallnode.get_expect_loc: tcgloc;
  2218. var
  2219. realresdef: tstoreddef;
  2220. begin
  2221. if not assigned(typedef) then
  2222. realresdef:=tstoreddef(resultdef)
  2223. else
  2224. realresdef:=tstoreddef(typedef);
  2225. if realresdef.is_intregable then
  2226. result:=LOC_REGISTER
  2227. else if (realresdef.typ=floatdef) and
  2228. not(cs_fp_emulation in current_settings.moduleswitches) then
  2229. if use_vectorfpu(realresdef) then
  2230. result:=LOC_MMREGISTER
  2231. else
  2232. result:=LOC_FPUREGISTER
  2233. else
  2234. result:=LOC_REFERENCE
  2235. end;
  2236. function tcallnode.safe_call_self_node: tnode;
  2237. begin
  2238. if not assigned(call_self_node) then
  2239. begin
  2240. CGMessage(parser_e_illegal_expression);
  2241. call_self_node:=cerrornode.create;
  2242. end;
  2243. result:=call_self_node;
  2244. end;
  2245. procedure tcallnode.gen_vmt_entry_load;
  2246. var
  2247. vmt_def: trecorddef;
  2248. begin
  2249. if not assigned(right) and
  2250. (forcedprocname='') and
  2251. (po_virtualmethod in procdefinition.procoptions) and
  2252. not is_objectpascal_helper(tprocdef(procdefinition).struct) and
  2253. assigned(methodpointer) and
  2254. (methodpointer.nodetype<>typen) then
  2255. begin
  2256. vmt_entry:=load_vmt_for_self_node(methodpointer.getcopy);
  2257. { get the right entry in the VMT }
  2258. vmt_entry:=cderefnode.create(vmt_entry);
  2259. typecheckpass(vmt_entry);
  2260. vmt_def:=trecorddef(vmt_entry.resultdef);
  2261. { tobjectdef(tprocdef(procdefinition).struct) can be a parent of the
  2262. methodpointer's resultdef, but the vmtmethodoffset of the method
  2263. in that objectdef is obviously the same as in any child class }
  2264. vmt_entry:=csubscriptnode.create(
  2265. trecordsymtable(vmt_def.symtable).findfieldbyoffset(
  2266. tobjectdef(tprocdef(procdefinition).struct).vmtmethodoffset(tprocdef(procdefinition).extnumber)
  2267. ),
  2268. vmt_entry
  2269. );
  2270. firstpass(vmt_entry);
  2271. end;
  2272. end;
  2273. procedure tcallnode.gen_syscall_para(para: tcallparanode);
  2274. begin
  2275. { unsupported }
  2276. internalerror(2014040101);
  2277. end;
  2278. procedure tcallnode.objc_convert_to_message_send;
  2279. var
  2280. block,
  2281. selftree : tnode;
  2282. statements : tstatementnode;
  2283. field : tfieldvarsym;
  2284. temp : ttempcreatenode;
  2285. selfrestype,
  2286. objcsupertype : tdef;
  2287. srsym : tsym;
  2288. srsymtable : tsymtable;
  2289. msgsendname : string;
  2290. begin
  2291. if not(m_objectivec1 in current_settings.modeswitches) then
  2292. Message(parser_f_modeswitch_objc_required);
  2293. { typecheck pass must already have run on the call node,
  2294. because pass1 calls this method
  2295. }
  2296. { default behaviour: call objc_msgSend and friends;
  2297. 64 bit targets for Mac OS X can override this as they
  2298. can call messages via an indirect function call similar to
  2299. dynamically linked functions, ARM maybe as well (not checked)
  2300. Which variant of objc_msgSend is used depends on the
  2301. result type, and on whether or not it's an inherited call.
  2302. }
  2303. { make sure we don't perform this transformation twice in case
  2304. firstpass would be called multiple times }
  2305. include(callnodeflags,cnf_objc_processed);
  2306. { make sure the methodpointer doesn't get translated into a call
  2307. as well (endless loop) }
  2308. if methodpointer.nodetype=loadvmtaddrn then
  2309. tloadvmtaddrnode(methodpointer).forcall:=true;
  2310. { A) set the appropriate objc_msgSend* variant to call }
  2311. { The AArch64 abi does not require special handling for struct returns }
  2312. {$ifndef aarch64}
  2313. { record returned via implicit pointer }
  2314. if paramanager.ret_in_param(resultdef,procdefinition) then
  2315. begin
  2316. if not(cnf_inherited in callnodeflags) then
  2317. msgsendname:='OBJC_MSGSEND_STRET'
  2318. {$if defined(onlymacosx10_6) or defined(arm) }
  2319. else if (target_info.system in systems_objc_nfabi) then
  2320. msgsendname:='OBJC_MSGSENDSUPER2_STRET'
  2321. {$endif onlymacosx10_6 or arm}
  2322. else
  2323. msgsendname:='OBJC_MSGSENDSUPER_STRET'
  2324. end
  2325. {$ifdef i386}
  2326. { special case for fpu results on i386 for non-inherited calls }
  2327. { TODO: also for x86_64 "extended" results }
  2328. else if (resultdef.typ=floatdef) and
  2329. not(cnf_inherited in callnodeflags) then
  2330. msgsendname:='OBJC_MSGSEND_FPRET'
  2331. {$endif i386}
  2332. { default }
  2333. else
  2334. {$endif aarch64}
  2335. if not(cnf_inherited in callnodeflags) then
  2336. msgsendname:='OBJC_MSGSEND'
  2337. {$if defined(onlymacosx10_6) or defined(arm) or defined(aarch64)}
  2338. else if (target_info.system in systems_objc_nfabi) then
  2339. msgsendname:='OBJC_MSGSENDSUPER2'
  2340. {$endif onlymacosx10_6 or arm}
  2341. else
  2342. msgsendname:='OBJC_MSGSENDSUPER';
  2343. { get the mangled name }
  2344. srsym:=nil;
  2345. if not searchsym_in_named_module('OBJC',msgsendname,srsym,srsymtable) or
  2346. (srsym.typ<>procsym) or
  2347. (tprocsym(srsym).ProcdefList.count<>1) then
  2348. Message1(cg_f_unknown_compilerproc,'objc.'+msgsendname);
  2349. {$ifdef symansistr}
  2350. fforcedprocname:=tprocdef(tprocsym(srsym).ProcdefList[0]).mangledname;
  2351. {$else symansistr}
  2352. fforcedprocname:=stringdup(tprocdef(tprocsym(srsym).ProcdefList[0]).mangledname);
  2353. {$endif symansistr}
  2354. { B) Handle self }
  2355. { 1) in case of sending a message to a superclass, self is a pointer to
  2356. an objc_super record
  2357. }
  2358. if (cnf_inherited in callnodeflags) then
  2359. begin
  2360. block:=internalstatements(statements);
  2361. objcsupertype:=search_named_unit_globaltype('OBJC','OBJC_SUPER',true).typedef;
  2362. if (objcsupertype.typ<>recorddef) then
  2363. internalerror(2009032901);
  2364. { temp for the for the objc_super record }
  2365. temp:=ctempcreatenode.create(objcsupertype,objcsupertype.size,tt_persistent,false);
  2366. addstatement(statements,temp);
  2367. { initialize objc_super record }
  2368. selftree:=safe_call_self_node.getcopy;
  2369. { we can call an inherited class static/method from a regular method
  2370. -> self node must change from instance pointer to vmt pointer)
  2371. }
  2372. if (po_classmethod in procdefinition.procoptions) and
  2373. (selftree.resultdef.typ<>classrefdef) then
  2374. begin
  2375. selftree:=cloadvmtaddrnode.create(selftree);
  2376. { since we're in a class method of the current class, its
  2377. information has already been initialized (and that of all of
  2378. its parent classes too) }
  2379. tloadvmtaddrnode(selftree).forcall:=true;
  2380. typecheckpass(selftree);
  2381. end;
  2382. selfrestype:=selftree.resultdef;
  2383. field:=tfieldvarsym(trecorddef(objcsupertype).symtable.find('RECEIVER'));
  2384. if not assigned(field) then
  2385. internalerror(2009032902);
  2386. { first the destination object/class instance }
  2387. addstatement(statements,
  2388. cassignmentnode.create(
  2389. csubscriptnode.create(field,ctemprefnode.create(temp)),
  2390. selftree
  2391. )
  2392. );
  2393. { and secondly, the class type in which the selector must be looked
  2394. up (the parent class in case of an instance method, the parent's
  2395. metaclass in case of a class method) }
  2396. field:=tfieldvarsym(trecorddef(objcsupertype).symtable.find('_CLASS'));
  2397. if not assigned(field) then
  2398. internalerror(2009032903);
  2399. addstatement(statements,
  2400. cassignmentnode.create(
  2401. csubscriptnode.create(field,ctemprefnode.create(temp)),
  2402. objcsuperclassnode(selftree.resultdef)
  2403. )
  2404. );
  2405. { result of this block is the address of this temp }
  2406. addstatement(statements,ctypeconvnode.create_internal(
  2407. caddrnode.create_internal(ctemprefnode.create(temp)),selfrestype)
  2408. );
  2409. { replace the method pointer with the address of this temp }
  2410. methodpointer.free;
  2411. methodpointer:=block;
  2412. typecheckpass(block);
  2413. end
  2414. else
  2415. { 2) regular call (not inherited) }
  2416. begin
  2417. { a) If we're calling a class method, use a class ref. }
  2418. if (po_classmethod in procdefinition.procoptions) and
  2419. ((methodpointer.nodetype=typen) or
  2420. (methodpointer.resultdef.typ<>classrefdef)) then
  2421. begin
  2422. methodpointer:=cloadvmtaddrnode.create(methodpointer);
  2423. { no need to obtain the class ref by calling class(), sending
  2424. this message will initialize it if necessary }
  2425. tloadvmtaddrnode(methodpointer).forcall:=true;
  2426. firstpass(methodpointer);
  2427. end;
  2428. end;
  2429. end;
  2430. function tcallnode.gen_vmt_tree:tnode;
  2431. var
  2432. vmttree : tnode;
  2433. begin
  2434. vmttree:=nil;
  2435. if not(procdefinition.proctypeoption in [potype_constructor,potype_destructor]) then
  2436. internalerror(200305051);
  2437. { When methodpointer was a callnode we must load it first into a
  2438. temp to prevent the processing callnode twice }
  2439. if (methodpointer.nodetype=calln) then
  2440. internalerror(200405122);
  2441. { Handle classes and legacy objects separate to make it
  2442. more maintainable }
  2443. if (methodpointer.resultdef.typ=classrefdef) then
  2444. begin
  2445. if not is_class(tclassrefdef(methodpointer.resultdef).pointeddef) then
  2446. internalerror(200501041);
  2447. { constructor call via classreference => allocate memory }
  2448. if (procdefinition.proctypeoption=potype_constructor) then
  2449. begin
  2450. vmttree:=cpointerconstnode.create(1,voidpointertype);
  2451. end
  2452. else { <class of xx>.destroy is not valid }
  2453. InternalError(2014020601);
  2454. end
  2455. else
  2456. { Class style objects }
  2457. if is_class(methodpointer.resultdef) then
  2458. begin
  2459. { inherited call, no create/destroy }
  2460. if (cnf_inherited in callnodeflags) then
  2461. vmttree:=cpointerconstnode.create(0,voidpointertype)
  2462. else
  2463. { do not create/destroy when called from member function
  2464. without specifying self explicit }
  2465. if (cnf_member_call in callnodeflags) then
  2466. begin
  2467. { destructor (in the same class, since cnf_member_call):
  2468. if not called from a destructor then
  2469. call beforedestruction and release instance, vmt=1
  2470. else
  2471. don't release instance, vmt=0
  2472. constructor (in the same class, since cnf_member_call):
  2473. if called from a constructor then
  2474. don't call afterconstruction, vmt=0
  2475. else
  2476. call afterconstrution but not NewInstance, vmt=-1 }
  2477. if (procdefinition.proctypeoption=potype_destructor) then
  2478. if (current_procinfo.procdef.proctypeoption<>potype_constructor) then
  2479. vmttree:=cpointerconstnode.create(1,voidpointertype)
  2480. else
  2481. vmttree:=cpointerconstnode.create(0,voidpointertype)
  2482. else if (current_procinfo.procdef.proctypeoption=potype_constructor) and
  2483. (procdefinition.proctypeoption=potype_constructor) then
  2484. vmttree:=cpointerconstnode.create(0,voidpointertype)
  2485. else
  2486. vmttree:=cpointerconstnode.create(TConstPtrUInt(-1),voidpointertype);
  2487. end
  2488. else
  2489. { normal call to method like cl1.proc }
  2490. begin
  2491. { destructor:
  2492. if not called from exception block in constructor
  2493. call beforedestruction and release instance, vmt=1
  2494. else
  2495. don't call beforedestruction and release instance, vmt=-1
  2496. constructor:
  2497. if called from a constructor in the same class using self.create then
  2498. don't call afterconstruction, vmt=0
  2499. else
  2500. call afterconstruction, vmt=1 }
  2501. if (procdefinition.proctypeoption=potype_destructor) then
  2502. if not(cnf_create_failed in callnodeflags) then
  2503. vmttree:=cpointerconstnode.create(1,voidpointertype)
  2504. else
  2505. vmttree:=cpointerconstnode.create(TConstPtrUInt(-1),voidpointertype)
  2506. else
  2507. begin
  2508. if (current_procinfo.procdef.proctypeoption=potype_constructor) and
  2509. (procdefinition.proctypeoption=potype_constructor) and
  2510. (methodpointer.nodetype=loadn) and
  2511. (loadnf_is_self in tloadnode(methodpointer).loadnodeflags) then
  2512. vmttree:=cpointerconstnode.create(0,voidpointertype)
  2513. else
  2514. vmttree:=cpointerconstnode.create(TConstPtrUInt(-1),voidpointertype);
  2515. end;
  2516. end;
  2517. end
  2518. else
  2519. { Old style object }
  2520. begin
  2521. { constructor with extended syntax called from new }
  2522. if (cnf_new_call in callnodeflags) then
  2523. vmttree:=cloadvmtaddrnode.create(ctypenode.create(methodpointer.resultdef))
  2524. else
  2525. { destructor with extended syntax called from dispose }
  2526. { value -1 is what fpc_help_constructor() changes VMT to when it allocates memory }
  2527. if (cnf_dispose_call in callnodeflags) then
  2528. vmttree:=cpointerconstnode.create(TConstPtrUInt(-1),voidpointertype)
  2529. else
  2530. { destructor called from exception block in constructor }
  2531. if (cnf_create_failed in callnodeflags) then
  2532. vmttree:=ctypeconvnode.create_internal(call_vmt_node.getcopy,voidpointertype)
  2533. else
  2534. { inherited call, no create/destroy }
  2535. if (cnf_inherited in callnodeflags) then
  2536. vmttree:=cpointerconstnode.create(0,voidpointertype)
  2537. else
  2538. { do not create/destroy when called from member function
  2539. without specifying self explicit }
  2540. if (cnf_member_call in callnodeflags) then
  2541. begin
  2542. { destructor: don't release instance, vmt=0
  2543. constructor: don't initialize instance, vmt=0 }
  2544. vmttree:=cpointerconstnode.create(0,voidpointertype)
  2545. end
  2546. else
  2547. { normal object call like obj.proc }
  2548. begin
  2549. { destructor: direct call, no dispose, vmt=0
  2550. constructor: initialize object, load vmt }
  2551. if (procdefinition.proctypeoption=potype_constructor) then
  2552. begin
  2553. { old styled inherited call? }
  2554. if (methodpointer.nodetype=typen) then
  2555. vmttree:=cpointerconstnode.create(0,voidpointertype)
  2556. else
  2557. vmttree:=cloadvmtaddrnode.create(ctypenode.create(methodpointer.resultdef))
  2558. end
  2559. else
  2560. vmttree:=cpointerconstnode.create(0,voidpointertype);
  2561. end;
  2562. end;
  2563. result:=vmttree;
  2564. end;
  2565. function tcallnode.gen_block_context: tnode;
  2566. begin
  2567. { the self parameter of a block invocation is that address of the
  2568. block literal (which is what right contains) }
  2569. result:=right.getcopy;
  2570. end;
  2571. function check_funcret_used_as_para(var n: tnode; arg: pointer): foreachnoderesult;
  2572. var
  2573. destsym : tsym absolute arg;
  2574. begin
  2575. result := fen_false;
  2576. if (n.nodetype=loadn) and
  2577. (tloadnode(n).symtableentry = destsym) then
  2578. result := fen_norecurse_true;
  2579. end;
  2580. function tcallnode.funcret_can_be_reused:boolean;
  2581. var
  2582. realassignmenttarget: tnode;
  2583. alignment: longint;
  2584. begin
  2585. result:=false;
  2586. { we are processing an assignment node? }
  2587. if not(assigned(aktassignmentnode) and
  2588. (aktassignmentnode.right=self) and
  2589. (aktassignmentnode.left.resultdef=resultdef)) then
  2590. exit;
  2591. { destination must be able to be passed as var parameter }
  2592. if not valid_for_var(aktassignmentnode.left,false) then
  2593. exit;
  2594. { destination must be a simple load so it doesn't need a temp when
  2595. it is evaluated }
  2596. if not is_simple_para_load(aktassignmentnode.left,false) then
  2597. exit;
  2598. { remove possible typecasts }
  2599. realassignmenttarget:=actualtargetnode(@aktassignmentnode.left)^;
  2600. { when it is not passed in a parameter it will only be used after the
  2601. function call }
  2602. if not paramanager.ret_in_param(resultdef,procdefinition) then
  2603. begin
  2604. { don't replace the function result if we are inlining and if the destination is complex, this
  2605. could lead to lengthy code in case the function result is used often and it is assigned e.g.
  2606. to a threadvar }
  2607. result:=not(cnf_do_inline in callnodeflags) or
  2608. (node_complexity(aktassignmentnode.left)<=1);
  2609. exit;
  2610. end;
  2611. { if the result is the same as the self parameter (in case of objects),
  2612. we can't optimise. We have to check this explicitly becaise
  2613. hidden parameters such as self have not yet been inserted at this
  2614. point
  2615. }
  2616. if assigned(methodpointer) and
  2617. realassignmenttarget.isequal(actualtargetnode(@methodpointer)^) then
  2618. exit;
  2619. { when we substitute a function result inside an inlined function,
  2620. we may take the address of this function result. Therefore the
  2621. substituted function result may not be in a register, as we cannot
  2622. take its address in that case }
  2623. if (realassignmenttarget.nodetype=temprefn) and
  2624. not(ti_addr_taken in ttemprefnode(realassignmenttarget).tempinfo^.flags) and
  2625. not(ti_may_be_in_reg in ttemprefnode(realassignmenttarget).tempinfo^.flags) then
  2626. begin
  2627. result:=true;
  2628. exit;
  2629. end;
  2630. if (realassignmenttarget.nodetype=loadn) and
  2631. { nested procedures may access the current procedure's locals }
  2632. (procdefinition.parast.symtablelevel=normal_function_level) and
  2633. { must be a local variable, a value para or a hidden function result }
  2634. { parameter (which can be passed by address, but in that case it got }
  2635. { through these same checks at the caller side and is thus safe ) }
  2636. { other option: we're calling a compilerproc, because those don't
  2637. rely on global state
  2638. }
  2639. ((po_compilerproc in procdefinition.procoptions) or
  2640. (
  2641. (
  2642. (tloadnode(realassignmenttarget).symtableentry.typ=localvarsym) or
  2643. (
  2644. (tloadnode(realassignmenttarget).symtableentry.typ=paravarsym) and
  2645. ((tparavarsym(tloadnode(realassignmenttarget).symtableentry).varspez = vs_value) or
  2646. (vo_is_funcret in tparavarsym(tloadnode(realassignmenttarget).symtableentry).varoptions))
  2647. )
  2648. ) and
  2649. { the address may not have been taken of the variable/parameter, because }
  2650. { otherwise it's possible that the called function can access it via a }
  2651. { global variable or other stored state }
  2652. (
  2653. not(tabstractvarsym(tloadnode(realassignmenttarget).symtableentry).addr_taken) and
  2654. (tabstractvarsym(tloadnode(realassignmenttarget).symtableentry).varregable in [vr_none,vr_addr])
  2655. )
  2656. )
  2657. ) then
  2658. begin
  2659. { If the funcret is also used as a parameter we can't optimize because the funcret
  2660. and the parameter will point to the same address. That means that a change of the result variable
  2661. will result also in a change of the parameter value }
  2662. result:=not foreachnodestatic(left,@check_funcret_used_as_para,tloadnode(realassignmenttarget).symtableentry);
  2663. { ensure that it is aligned using the default alignment }
  2664. alignment:=tabstractvarsym(tloadnode(realassignmenttarget).symtableentry).vardef.alignment;
  2665. if (used_align(alignment,target_info.alignment.localalignmin,target_info.alignment.localalignmax)<>
  2666. used_align(alignment,current_settings.alignment.localalignmin,current_settings.alignment.localalignmax)) then
  2667. result:=false;
  2668. exit;
  2669. end;
  2670. end;
  2671. procedure tcallnode.maybe_create_funcret_node;
  2672. var
  2673. temp : ttempcreatenode;
  2674. begin
  2675. if procdefinition.proctypeoption=potype_constructor then
  2676. exit;
  2677. { For the function result we need to create a temp node for:
  2678. - Inlined functions
  2679. - Types requiring initialization/finalization
  2680. - Types passed in parameters }
  2681. if not is_void(resultdef) and
  2682. not assigned(funcretnode) and
  2683. (
  2684. (cnf_do_inline in callnodeflags) or
  2685. is_managed_type(resultdef) or
  2686. paramanager.ret_in_param(resultdef,procdefinition)
  2687. ) then
  2688. begin
  2689. { Optimize calls like x:=f() where we can use x directly as
  2690. result instead of using a temp. Condition is that x cannot be accessed from f().
  2691. This implies that x is a local variable or value parameter of the current block
  2692. and its address is not passed to f. One problem: what if someone takes the
  2693. address of x, puts it in a pointer variable/field and then accesses it that way
  2694. from within the function? This is solved (in a conservative way) using the
  2695. ti_addr_taken flag.
  2696. When the result is not not passed in a parameter there are no problem because
  2697. then it means only reference counted types (eg. ansistrings) that need a decr
  2698. of the refcount before being assigned. This is all done after the call so there
  2699. is no issue with exceptions and possible use of the old value in the called
  2700. function }
  2701. if funcret_can_be_reused then
  2702. begin
  2703. funcretnode:=aktassignmentnode.left.getcopy;
  2704. include(funcretnode.flags,nf_is_funcret);
  2705. { notify the assignment node that the assignment can be removed }
  2706. include(aktassignmentnode.flags,nf_assign_done_in_right);
  2707. end
  2708. else
  2709. begin
  2710. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,
  2711. (cnf_do_inline in callnodeflags) and
  2712. not(tabstractvarsym(tprocdef(procdefinition).funcretsym).varregable in [vr_none,vr_addr]));
  2713. include(temp.flags,nf_is_funcret);
  2714. { if a managed type is returned by reference, assigning something
  2715. to the result on the caller side will take care of decreasing
  2716. the reference count }
  2717. if paramanager.ret_in_param(resultdef,procdefinition) then
  2718. include(temp.tempinfo^.flags,ti_nofini);
  2719. add_init_statement(temp);
  2720. { When the function result is not used in an inlined function
  2721. we need to delete the temp. This can currently only be done by
  2722. a tempdeletenode and not after converting it to a normal temp }
  2723. if not(cnf_return_value_used in callnodeflags) and
  2724. (cnf_do_inline in callnodeflags) then
  2725. add_done_statement(ctempdeletenode.create(temp))
  2726. else
  2727. add_done_statement(ctempdeletenode.create_normal_temp(temp));
  2728. funcretnode:=ctemprefnode.create(temp);
  2729. include(funcretnode.flags,nf_is_funcret);
  2730. end;
  2731. end;
  2732. end;
  2733. procedure tcallnode.gen_hidden_parameters;
  2734. var
  2735. para : tcallparanode;
  2736. begin
  2737. para:=tcallparanode(left);
  2738. while assigned(para) do
  2739. begin
  2740. { The processing of high() and typeinfo() is already
  2741. done in the typecheckpass. We only need to process the
  2742. nodes that still have a nothingn }
  2743. if (vo_is_hidden_para in para.parasym.varoptions) and
  2744. (para.left.nodetype=nothingn) then
  2745. begin
  2746. { remove dummy nothingn }
  2747. para.left.free;
  2748. para.left:=nil;
  2749. { generate the corresponding nodes for the hidden parameter type }
  2750. if (vo_is_funcret in para.parasym.varoptions) then
  2751. begin
  2752. if not assigned(funcretnode) then
  2753. internalerror(200709083);
  2754. para.left:=funcretnode;
  2755. funcretnode:=nil;
  2756. end
  2757. else
  2758. if vo_is_self in para.parasym.varoptions then
  2759. begin
  2760. if assigned(right) then
  2761. para.left:=gen_procvar_context_tree_self
  2762. else
  2763. para.left:=gen_self_tree;
  2764. { make sure that e.g. the self pointer of an advanced
  2765. record does not become a regvar, because it's a vs_var
  2766. parameter }
  2767. if paramanager.push_addr_param(para.parasym.varspez,para.parasym.vardef,
  2768. procdefinition.proccalloption) then
  2769. make_not_regable(para.left,[ra_addr_regable]);
  2770. end
  2771. else
  2772. if vo_is_vmt in para.parasym.varoptions then
  2773. begin
  2774. para.left:=gen_vmt_tree;
  2775. end
  2776. else
  2777. if vo_is_syscall_lib in para.parasym.varoptions then
  2778. gen_syscall_para(para)
  2779. else
  2780. if vo_is_parentfp in para.parasym.varoptions then
  2781. begin
  2782. if not assigned(right) then
  2783. begin
  2784. if assigned(procdefinition.owner.defowner) then
  2785. para.left:=cloadparentfpnode.create(tprocdef(procdefinition.owner.defowner),lpf_forpara)
  2786. { exceptfilters called from main level are not owned }
  2787. else if procdefinition.proctypeoption=potype_exceptfilter then
  2788. para.left:=cloadparentfpnode.create(current_procinfo.procdef,lpf_forpara)
  2789. else
  2790. internalerror(200309287);
  2791. end
  2792. else if not(po_is_block in procdefinition.procoptions) then
  2793. para.left:=gen_procvar_context_tree_parentfp
  2794. else
  2795. para.left:=gen_block_context
  2796. end
  2797. else
  2798. if vo_is_range_check in para.parasym.varoptions then
  2799. begin
  2800. para.left:=cordconstnode.create(Ord(cs_check_range in current_settings.localswitches),pasbool8type,false);
  2801. end
  2802. else
  2803. if vo_is_overflow_check in para.parasym.varoptions then
  2804. begin
  2805. para.left:=cordconstnode.create(Ord(cs_check_overflow in current_settings.localswitches),pasbool8type,false);
  2806. end
  2807. else
  2808. if vo_is_msgsel in para.parasym.varoptions then
  2809. begin
  2810. para.left:=cobjcselectornode.create(cstringconstnode.createstr(tprocdef(procdefinition).messageinf.str^));
  2811. end;
  2812. end;
  2813. if not assigned(para.left) then
  2814. internalerror(200709084);
  2815. para:=tcallparanode(para.right);
  2816. end;
  2817. end;
  2818. procedure tcallnode.verifyabstract(sym:TObject;arg:pointer);
  2819. var
  2820. pd : tprocdef;
  2821. i : longint;
  2822. j : integer;
  2823. hs : string;
  2824. begin
  2825. if (tsym(sym).typ<>procsym) then
  2826. exit;
  2827. for i:=0 to tprocsym(sym).ProcdefList.Count-1 do
  2828. begin
  2829. pd:=tprocdef(tprocsym(sym).ProcdefList[i]);
  2830. hs:=pd.procsym.name+pd.typename_paras([]);
  2831. j:=AbstractMethodsList.FindIndexOf(hs);
  2832. if j<>-1 then
  2833. AbstractMethodsList[j]:=pd
  2834. else
  2835. AbstractMethodsList.Add(hs,pd);
  2836. end;
  2837. end;
  2838. procedure tcallnode.verifyabstractcalls;
  2839. var
  2840. objectdf : tobjectdef;
  2841. parents : tlinkedlist;
  2842. objectinfo : tobjectinfoitem;
  2843. pd : tprocdef;
  2844. i : integer;
  2845. begin
  2846. objectdf := nil;
  2847. { verify if trying to create an instance of a class which contains
  2848. non-implemented abstract methods }
  2849. { first verify this class type, no class than exit }
  2850. { also, this checking can only be done if the constructor is directly
  2851. called, indirect constructor calls cannot be checked.
  2852. }
  2853. if assigned(methodpointer) and
  2854. not((methodpointer.nodetype=loadn) and
  2855. (loadnf_is_self in tloadnode(methodpointer).loadnodeflags)) then
  2856. begin
  2857. if (methodpointer.resultdef.typ = objectdef) then
  2858. objectdf:=tobjectdef(methodpointer.resultdef)
  2859. else
  2860. if (methodpointer.resultdef.typ = classrefdef) and
  2861. (tclassrefdef(methodpointer.resultdef).pointeddef.typ = objectdef) and
  2862. (methodpointer.nodetype in [typen,loadvmtaddrn]) then
  2863. objectdf:=tobjectdef(tclassrefdef(methodpointer.resultdef).pointeddef);
  2864. end;
  2865. if not assigned(objectdf) then
  2866. exit;
  2867. { quick exit if nothing to check }
  2868. if objectdf.abstractcnt = 0 then
  2869. exit;
  2870. parents := tlinkedlist.create;
  2871. AbstractMethodsList := TFPHashList.create;
  2872. { insert all parents in this class : the first item in the
  2873. list will be the base parent of the class .
  2874. }
  2875. while assigned(objectdf) do
  2876. begin
  2877. objectinfo:=tobjectinfoitem.create(objectdf);
  2878. parents.insert(objectinfo);
  2879. objectdf := objectdf.childof;
  2880. end;
  2881. { now all parents are in the correct order
  2882. insert all abstract methods in the list, and remove
  2883. those which are overridden by parent classes.
  2884. }
  2885. objectinfo:=tobjectinfoitem(parents.first);
  2886. while assigned(objectinfo) do
  2887. begin
  2888. objectdf := objectinfo.objinfo;
  2889. if assigned(objectdf.symtable) then
  2890. objectdf.symtable.SymList.ForEachCall(@verifyabstract,nil);
  2891. objectinfo:=tobjectinfoitem(objectinfo.next);
  2892. end;
  2893. if assigned(parents) then
  2894. parents.free;
  2895. { Finally give out a warning for each abstract method still in the list }
  2896. for i:=0 to AbstractMethodsList.Count-1 do
  2897. begin
  2898. pd:=tprocdef(AbstractMethodsList[i]);
  2899. if po_abstractmethod in pd.procoptions then
  2900. begin
  2901. Message2(type_w_instance_with_abstract,objectdf.objrealname^,pd.procsym.RealName);
  2902. MessagePos1(pd.fileinfo,sym_h_abstract_method_list,pd.fullprocname(true));
  2903. end;
  2904. end;
  2905. if assigned(AbstractMethodsList) then
  2906. AbstractMethodsList.Free;
  2907. end;
  2908. procedure tcallnode.convert_carg_array_of_const;
  2909. var
  2910. hp : tarrayconstructornode;
  2911. oldleft : tcallparanode;
  2912. begin
  2913. oldleft:=tcallparanode(left);
  2914. if oldleft.left.nodetype<>arrayconstructorn then
  2915. begin
  2916. CGMessage1(type_e_wrong_type_in_array_constructor,oldleft.left.resultdef.typename);
  2917. exit;
  2918. end;
  2919. include(callnodeflags,cnf_uses_varargs);
  2920. { Get arrayconstructor node and insert typeconvs }
  2921. hp:=tarrayconstructornode(oldleft.left);
  2922. { Add c args parameters }
  2923. { It could be an empty set }
  2924. if assigned(hp) and
  2925. assigned(hp.left) then
  2926. begin
  2927. while assigned(hp) do
  2928. begin
  2929. left:=ccallparanode.create(hp.left,left);
  2930. { set callparanode resultdef and flags }
  2931. left.resultdef:=hp.left.resultdef;
  2932. include(tcallparanode(left).callparaflags,cpf_varargs_para);
  2933. hp.left:=nil;
  2934. hp:=tarrayconstructornode(hp.right);
  2935. end;
  2936. end;
  2937. { Remove value of old array of const parameter, but keep it
  2938. in the list because it is required for bind_parasym.
  2939. Generate a nothign to keep callparanoed.left valid }
  2940. oldleft.left.free;
  2941. oldleft.left:=cnothingnode.create;
  2942. end;
  2943. procedure tcallnode.bind_parasym;
  2944. type
  2945. pcallparanode = ^tcallparanode;
  2946. var
  2947. i : integer;
  2948. pt : tcallparanode;
  2949. oldppt : pcallparanode;
  2950. varargspara,
  2951. currpara : tparavarsym;
  2952. hiddentree : tnode;
  2953. paradef : tdef;
  2954. begin
  2955. pt:=tcallparanode(left);
  2956. oldppt:=pcallparanode(@left);
  2957. { flag all callparanodes that belong to the varargs }
  2958. i:=paralength;
  2959. while (i>procdefinition.maxparacount) do
  2960. begin
  2961. include(pt.callparaflags,cpf_varargs_para);
  2962. oldppt:=pcallparanode(@pt.right);
  2963. pt:=tcallparanode(pt.right);
  2964. dec(i);
  2965. end;
  2966. { skip varargs that are inserted by array of const }
  2967. while assigned(pt) and
  2968. (cpf_varargs_para in pt.callparaflags) do
  2969. pt:=tcallparanode(pt.right);
  2970. { process normal parameters and insert hidden parameter nodes, the content
  2971. of the hidden parameters will be updated in pass1 }
  2972. for i:=procdefinition.paras.count-1 downto 0 do
  2973. begin
  2974. currpara:=tparavarsym(procdefinition.paras[i]);
  2975. if vo_is_hidden_para in currpara.varoptions then
  2976. begin
  2977. { Here we handle only the parameters that depend on
  2978. the types of the previous parameter. The typeconversion
  2979. can change the type in the next step. For example passing
  2980. an array can be change to a pointer and a deref }
  2981. if vo_is_high_para in currpara.varoptions then
  2982. begin
  2983. if not assigned(pt) or (i=0) then
  2984. internalerror(200304081);
  2985. { we need the information of the previous parameter }
  2986. paradef:=tparavarsym(procdefinition.paras[i-1]).vardef;
  2987. hiddentree:=gen_high_tree(pt.left,paradef);
  2988. { for open array of managed type, a copy of high parameter is
  2989. necessary to properly initialize before the call }
  2990. if is_open_array(paradef) and
  2991. (tparavarsym(procdefinition.paras[i-1]).varspez=vs_out) and
  2992. is_managed_type(tarraydef(paradef).elementdef) then
  2993. begin
  2994. typecheckpass(hiddentree);
  2995. {this eliminates double call to fpc_dynarray_high, if any}
  2996. maybe_load_in_temp(hiddentree);
  2997. oldppt^.third:=hiddentree.getcopy;
  2998. end;
  2999. end
  3000. else
  3001. if vo_is_typinfo_para in currpara.varoptions then
  3002. begin
  3003. if not assigned(pt) or (i=0) then
  3004. internalerror(200304082);
  3005. hiddentree:=caddrnode.create_internal(
  3006. crttinode.create(Tstoreddef(pt.resultdef),fullrtti,rdt_normal)
  3007. );
  3008. end
  3009. else
  3010. hiddentree:=cnothingnode.create;
  3011. pt:=ccallparanode.create(hiddentree,oldppt^);
  3012. oldppt^:=pt;
  3013. end;
  3014. if not assigned(pt) then
  3015. internalerror(200310052);
  3016. pt.parasym:=currpara;
  3017. oldppt:=pcallparanode(@pt.right);
  3018. pt:=tcallparanode(pt.right);
  3019. end;
  3020. { Create parasyms for varargs, first count the number of varargs paras,
  3021. then insert the parameters with numbering in reverse order. The SortParas
  3022. will set the correct order at the end}
  3023. pt:=tcallparanode(left);
  3024. i:=0;
  3025. while assigned(pt) do
  3026. begin
  3027. if cpf_varargs_para in pt.callparaflags then
  3028. inc(i);
  3029. pt:=tcallparanode(pt.right);
  3030. end;
  3031. if (i>0) then
  3032. begin
  3033. include(current_procinfo.flags,pi_calls_c_varargs);
  3034. varargsparas:=tvarargsparalist.create;
  3035. pt:=tcallparanode(left);
  3036. while assigned(pt) do
  3037. begin
  3038. if cpf_varargs_para in pt.callparaflags then
  3039. begin
  3040. varargspara:=cparavarsym.create('va'+tostr(i),i,vs_value,pt.resultdef,[]);
  3041. dec(i);
  3042. { varargspara is left-right, use insert
  3043. instead of concat }
  3044. varargsparas.add(varargspara);
  3045. pt.parasym:=varargspara;
  3046. end;
  3047. pt:=tcallparanode(pt.right);
  3048. end;
  3049. varargsparas.sortparas;
  3050. end;
  3051. end;
  3052. function tcallnode.pass_typecheck:tnode;
  3053. var
  3054. candidates : tcallcandidates;
  3055. oldcallnode : tcallnode;
  3056. hpt : tnode;
  3057. pt : tcallparanode;
  3058. lastpara : longint;
  3059. paraidx,
  3060. cand_cnt : integer;
  3061. i : longint;
  3062. ignorevisibility,
  3063. is_const : boolean;
  3064. statements : tstatementnode;
  3065. converted_result_data : ttempcreatenode;
  3066. calltype: tdispcalltype;
  3067. begin
  3068. result:=nil;
  3069. candidates:=nil;
  3070. oldcallnode:=aktcallnode;
  3071. aktcallnode:=self;
  3072. try
  3073. { determine length of parameter list }
  3074. pt:=tcallparanode(left);
  3075. paralength:=0;
  3076. while assigned(pt) do
  3077. begin
  3078. inc(paralength);
  3079. pt:=tcallparanode(pt.right);
  3080. end;
  3081. { determine the type of the parameters }
  3082. if assigned(left) then
  3083. begin
  3084. tcallparanode(left).get_paratype;
  3085. if codegenerror then
  3086. exit;
  3087. end;
  3088. if assigned(methodpointer) then
  3089. typecheckpass(methodpointer);
  3090. { procedure variable ? }
  3091. if assigned(right) then
  3092. begin
  3093. set_varstate(right,vs_read,[vsf_must_be_valid]);
  3094. typecheckpass(right);
  3095. if codegenerror then
  3096. exit;
  3097. procdefinition:=tabstractprocdef(right.resultdef);
  3098. { Compare parameters from right to left }
  3099. paraidx:=procdefinition.Paras.count-1;
  3100. { Skip default parameters }
  3101. if not(po_varargs in procdefinition.procoptions) then
  3102. begin
  3103. { ignore hidden parameters }
  3104. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) do
  3105. dec(paraidx);
  3106. for i:=1 to procdefinition.maxparacount-paralength do
  3107. begin
  3108. if paraidx<0 then
  3109. internalerror(200402265);
  3110. if not assigned(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym) then
  3111. begin
  3112. CGMessage1(parser_e_wrong_parameter_size,'<Procedure Variable>');
  3113. exit;
  3114. end;
  3115. dec(paraidx);
  3116. end;
  3117. end;
  3118. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) do
  3119. dec(paraidx);
  3120. pt:=tcallparanode(left);
  3121. lastpara:=paralength;
  3122. while (paraidx>=0) and assigned(pt) do
  3123. begin
  3124. { only goto next para if we're out of the varargs }
  3125. if not(po_varargs in procdefinition.procoptions) or
  3126. (lastpara<=procdefinition.maxparacount) then
  3127. begin
  3128. repeat
  3129. dec(paraidx);
  3130. until (paraidx<0) or not(vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions);
  3131. end;
  3132. pt:=tcallparanode(pt.right);
  3133. dec(lastpara);
  3134. end;
  3135. if assigned(pt) or
  3136. ((paraidx>=0) and
  3137. not assigned(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym)) then
  3138. begin
  3139. if assigned(pt) then
  3140. current_filepos:=pt.fileinfo;
  3141. CGMessage1(parser_e_wrong_parameter_size,'<Procedure Variable>');
  3142. exit;
  3143. end;
  3144. end
  3145. else
  3146. { not a procedure variable }
  3147. begin
  3148. { do we know the procedure to call ? }
  3149. if not(assigned(procdefinition)) then
  3150. begin
  3151. { ignore possible private for properties or in delphi mode for anon. inherited (FK) }
  3152. ignorevisibility:=(nf_isproperty in flags) or
  3153. ((m_delphi in current_settings.modeswitches) and (cnf_anon_inherited in callnodeflags));
  3154. candidates:=tcallcandidates.create(symtableprocentry,symtableproc,left,ignorevisibility,
  3155. not(nf_isproperty in flags),cnf_objc_id_call in callnodeflags,cnf_unit_specified in callnodeflags,
  3156. callnodeflags*[cnf_anon_inherited,cnf_inherited]=[],cnf_anon_inherited in callnodeflags);
  3157. { no procedures found? then there is something wrong
  3158. with the parameter size or the procedures are
  3159. not accessible }
  3160. if candidates.count=0 then
  3161. begin
  3162. { when it's an auto inherited call and there
  3163. is no procedure found, but the procedures
  3164. were defined with overload directive and at
  3165. least two procedures are defined then we ignore
  3166. this inherited by inserting a nothingn. Only
  3167. do this ugly hack in Delphi mode as it looks more
  3168. like a bug. It's also not documented }
  3169. if (m_delphi in current_settings.modeswitches) and
  3170. (cnf_anon_inherited in callnodeflags) and
  3171. (symtableprocentry.owner.symtabletype=ObjectSymtable) and
  3172. (po_overload in tprocdef(symtableprocentry.ProcdefList[0]).procoptions) and
  3173. (symtableprocentry.ProcdefList.Count>=2) then
  3174. result:=cnothingnode.create
  3175. else
  3176. begin
  3177. { in tp mode we can try to convert to procvar if
  3178. there are no parameters specified }
  3179. if not(assigned(left)) and
  3180. not(cnf_inherited in callnodeflags) and
  3181. ((m_tp_procvar in current_settings.modeswitches) or
  3182. (m_mac_procvar in current_settings.modeswitches)) and
  3183. (not assigned(methodpointer) or
  3184. (methodpointer.nodetype <> typen)) then
  3185. begin
  3186. hpt:=cloadnode.create(tprocsym(symtableprocentry),symtableproc);
  3187. if assigned(methodpointer) then
  3188. tloadnode(hpt).set_mp(methodpointer.getcopy);
  3189. typecheckpass(hpt);
  3190. result:=hpt;
  3191. end
  3192. else
  3193. begin
  3194. CGMessagePos1(fileinfo,parser_e_wrong_parameter_size,symtableprocentry.realname);
  3195. symtableprocentry.write_parameter_lists(nil);
  3196. end;
  3197. end;
  3198. candidates.free;
  3199. exit;
  3200. end;
  3201. { Retrieve information about the candidates }
  3202. candidates.get_information;
  3203. {$ifdef EXTDEBUG}
  3204. { Display info when multiple candidates are found }
  3205. if candidates.count>1 then
  3206. candidates.dump_info(V_Debug);
  3207. {$endif EXTDEBUG}
  3208. { Choose the best candidate and count the number of
  3209. candidates left }
  3210. cand_cnt:=candidates.choose_best(procdefinition,
  3211. assigned(left) and
  3212. not assigned(tcallparanode(left).right) and
  3213. (tcallparanode(left).left.resultdef.typ=variantdef));
  3214. { All parameters are checked, check if there are any
  3215. procedures left }
  3216. if cand_cnt>0 then
  3217. begin
  3218. { Multiple candidates left? }
  3219. if cand_cnt>1 then
  3220. begin
  3221. CGMessage(type_e_cant_choose_overload_function);
  3222. {$ifdef EXTDEBUG}
  3223. candidates.dump_info(V_Hint);
  3224. {$else EXTDEBUG}
  3225. candidates.list(false);
  3226. {$endif EXTDEBUG}
  3227. { we'll just use the first candidate to make the
  3228. call }
  3229. end;
  3230. { assign procdefinition }
  3231. if symtableproc=nil then
  3232. symtableproc:=procdefinition.owner;
  3233. end
  3234. else
  3235. begin
  3236. { No candidates left, this must be a type error,
  3237. because wrong size is already checked. procdefinition
  3238. is filled with the first (random) definition that is
  3239. found. We use this definition to display a nice error
  3240. message that the wrong type is passed }
  3241. candidates.find_wrong_para;
  3242. candidates.list(true);
  3243. {$ifdef EXTDEBUG}
  3244. candidates.dump_info(V_Hint);
  3245. {$endif EXTDEBUG}
  3246. { We can not proceed, release all procs and exit }
  3247. candidates.free;
  3248. exit;
  3249. end;
  3250. candidates.free;
  3251. end; { end of procedure to call determination }
  3252. end;
  3253. { check for hints (deprecated etc) }
  3254. if procdefinition.typ = procdef then
  3255. check_hints(tprocdef(procdefinition).procsym,tprocdef(procdefinition).symoptions,tprocdef(procdefinition).deprecatedmsg);
  3256. { add reference to corresponding procsym; may not be the one
  3257. originally found/passed to the constructor because of overloads }
  3258. if procdefinition.typ = procdef then
  3259. addsymref(tprocdef(procdefinition).procsym);
  3260. { add needed default parameters }
  3261. if (paralength<procdefinition.maxparacount) then
  3262. begin
  3263. paraidx:=0;
  3264. i:=0;
  3265. while (i<paralength) do
  3266. begin
  3267. if paraidx>=procdefinition.Paras.count then
  3268. internalerror(200306181);
  3269. if not(vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) then
  3270. inc(i);
  3271. inc(paraidx);
  3272. end;
  3273. while (paraidx<procdefinition.paras.count) and (vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) do
  3274. inc(paraidx);
  3275. while (paraidx<procdefinition.paras.count) do
  3276. begin
  3277. if not assigned(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym) then
  3278. internalerror(200212142);
  3279. left:=ccallparanode.create(genconstsymtree(
  3280. tconstsym(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym)),left);
  3281. { Ignore vs_hidden parameters }
  3282. repeat
  3283. inc(paraidx);
  3284. until (paraidx>=procdefinition.paras.count) or
  3285. not(vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions);
  3286. end;
  3287. end;
  3288. { recursive call? }
  3289. if assigned(current_procinfo) and
  3290. (procdefinition=current_procinfo.procdef) then
  3291. include(current_procinfo.flags,pi_is_recursive);
  3292. { handle predefined procedures }
  3293. is_const:=(po_internconst in procdefinition.procoptions) and
  3294. ((block_type in [bt_const,bt_type,bt_const_type,bt_var_type]) or
  3295. (assigned(left) and ((tcallparanode(left).left.nodetype in [realconstn,ordconstn])
  3296. and (not assigned(tcallparanode(left).right) or (tcallparanode(left).right.nodetype in [realconstn,ordconstn])))));
  3297. if (procdefinition.proccalloption=pocall_internproc) or is_const then
  3298. begin
  3299. if assigned(left) then
  3300. begin
  3301. { convert types to those of the prototype, this is required by functions like ror, rol, sar
  3302. some use however a dummy type (Typedfile) so this would break them }
  3303. if not(tprocdef(procdefinition).extnumber in [fpc_in_Reset_TypedFile,fpc_in_Rewrite_TypedFile]) then
  3304. begin
  3305. { bind parasyms to the callparanodes and insert hidden parameters }
  3306. bind_parasym;
  3307. { insert type conversions for parameters }
  3308. if assigned(left) then
  3309. tcallparanode(left).insert_typeconv;
  3310. end;
  3311. { ptr and settextbuf need two args }
  3312. if assigned(tcallparanode(left).right) then
  3313. begin
  3314. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,left);
  3315. left:=nil;
  3316. end
  3317. else
  3318. begin
  3319. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,tcallparanode(left).left);
  3320. tcallparanode(left).left:=nil;
  3321. end;
  3322. end
  3323. else
  3324. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,nil);
  3325. result:=hpt;
  3326. exit;
  3327. end;
  3328. { ensure that the result type is set }
  3329. if not(cnf_typedefset in callnodeflags) then
  3330. begin
  3331. { constructors return their current class type, not the type where the
  3332. constructor is declared, this can be different because of inheritance }
  3333. if (procdefinition.proctypeoption=potype_constructor) and
  3334. assigned(methodpointer) and
  3335. assigned(methodpointer.resultdef) and
  3336. (methodpointer.resultdef.typ=classrefdef) then
  3337. resultdef:=tclassrefdef(methodpointer.resultdef).pointeddef
  3338. else
  3339. { Member call to a (inherited) constructor from the class, the return
  3340. value is always self, so we change it to voidtype to generate an
  3341. error and to prevent users from generating non-working code
  3342. when they expect to clone the current instance, see bug 3662 (PFV) }
  3343. if (procdefinition.proctypeoption=potype_constructor) and
  3344. is_class(tprocdef(procdefinition).struct) and
  3345. assigned(methodpointer) and
  3346. (methodpointer.nodetype=loadn) and
  3347. (loadnf_is_self in tloadnode(methodpointer).loadnodeflags) then
  3348. resultdef:=voidtype
  3349. else
  3350. resultdef:=procdefinition.returndef;
  3351. end
  3352. else
  3353. resultdef:=typedef;
  3354. { Check object/class for methods }
  3355. if assigned(methodpointer) then
  3356. begin
  3357. { direct call to inherited abstract method, then we
  3358. can already give a error in the compiler instead
  3359. of a runtime error }
  3360. if (cnf_inherited in callnodeflags) and
  3361. (po_abstractmethod in procdefinition.procoptions) then
  3362. begin
  3363. if (m_delphi in current_settings.modeswitches) and
  3364. (cnf_anon_inherited in callnodeflags) then
  3365. begin
  3366. CGMessage(cg_h_inherited_ignored);
  3367. result:=cnothingnode.create;
  3368. exit;
  3369. end
  3370. else
  3371. CGMessage(cg_e_cant_call_abstract_method);
  3372. end;
  3373. { directly calling an interface/protocol/category/class helper
  3374. method via its type is not possible (always must be called via
  3375. the actual instance) }
  3376. if (methodpointer.nodetype=typen) and
  3377. (is_interface(methodpointer.resultdef) or
  3378. is_objc_protocol_or_category(methodpointer.resultdef)) then
  3379. CGMessage1(type_e_class_type_expected,methodpointer.resultdef.typename);
  3380. { if an inherited con- or destructor should be }
  3381. { called in a con- or destructor then a warning }
  3382. { will be made }
  3383. { con- and destructors need a pointer to the vmt }
  3384. if (cnf_inherited in callnodeflags) and
  3385. (procdefinition.proctypeoption in [potype_constructor,potype_destructor]) and
  3386. is_object(methodpointer.resultdef) and
  3387. not(current_procinfo.procdef.proctypeoption in [potype_constructor,potype_destructor]) then
  3388. CGMessage(cg_w_member_cd_call_from_method);
  3389. if methodpointer.nodetype<>typen then
  3390. begin
  3391. { Remove all postfix operators }
  3392. hpt:=methodpointer;
  3393. while assigned(hpt) and (hpt.nodetype in [subscriptn,vecn]) do
  3394. hpt:=tunarynode(hpt).left;
  3395. if ((hpt.nodetype=loadvmtaddrn) or
  3396. ((hpt.nodetype=loadn) and assigned(tloadnode(hpt).resultdef) and (tloadnode(hpt).resultdef.typ=classrefdef))) and
  3397. not (procdefinition.proctypeoption=potype_constructor) and
  3398. not (po_classmethod in procdefinition.procoptions) and
  3399. not (po_staticmethod in procdefinition.procoptions) then
  3400. { error: we are calling instance method from the class method/static method }
  3401. CGMessage(parser_e_only_class_members);
  3402. if (procdefinition.proctypeoption=potype_constructor) and
  3403. assigned(symtableproc) and
  3404. (symtableproc.symtabletype=withsymtable) and
  3405. (tnode(twithsymtable(symtableproc).withrefnode).nodetype=temprefn) then
  3406. CGmessage(cg_e_cannot_call_cons_dest_inside_with);
  3407. { skip (absolute and other simple) type conversions -- only now,
  3408. because the checks above have to take type conversions into
  3409. e.g. class reference types account }
  3410. hpt:=actualtargetnode(@hpt)^;
  3411. { R.Init then R will be initialized by the constructor,
  3412. Also allow it for simple loads }
  3413. if (procdefinition.proctypeoption=potype_constructor) or
  3414. ((hpt.nodetype=loadn) and
  3415. (((methodpointer.resultdef.typ=objectdef) and
  3416. not(oo_has_virtual in tobjectdef(methodpointer.resultdef).objectoptions)) or
  3417. (methodpointer.resultdef.typ=recorddef)
  3418. )
  3419. ) then
  3420. { a constructor will and a method may write something to }
  3421. { the fields }
  3422. set_varstate(methodpointer,vs_readwritten,[])
  3423. else
  3424. set_varstate(methodpointer,vs_read,[vsf_must_be_valid]);
  3425. end;
  3426. { if we are calling the constructor check for abstract
  3427. methods. Ignore inherited and member calls, because the
  3428. class is then already created }
  3429. if (procdefinition.proctypeoption=potype_constructor) and
  3430. not(cnf_inherited in callnodeflags) and
  3431. not(cnf_member_call in callnodeflags) then
  3432. verifyabstractcalls;
  3433. end
  3434. else
  3435. begin
  3436. { When this is method the methodpointer must be available }
  3437. if (right=nil) and
  3438. (procdefinition.owner.symtabletype in [ObjectSymtable,recordsymtable]) and
  3439. not procdefinition.no_self_node then
  3440. internalerror(200305061);
  3441. end;
  3442. { Set flag that the procedure uses varargs, also if they are not passed it is still
  3443. needed for x86_64 to pass the number of SSE registers used }
  3444. if po_varargs in procdefinition.procoptions then
  3445. include(callnodeflags,cnf_uses_varargs);
  3446. { set the appropriate node flag if the call never returns }
  3447. if po_noreturn in procdefinition.procoptions then
  3448. include(callnodeflags,cnf_call_never_returns);
  3449. { Change loading of array of const to varargs }
  3450. if assigned(left) and
  3451. is_array_of_const(tparavarsym(procdefinition.paras[procdefinition.paras.count-1]).vardef) and
  3452. (procdefinition.proccalloption in cdecl_pocalls) then
  3453. convert_carg_array_of_const;
  3454. { bind parasyms to the callparanodes and insert hidden parameters }
  3455. bind_parasym;
  3456. { insert type conversions for parameters }
  3457. if assigned(left) then
  3458. tcallparanode(left).insert_typeconv;
  3459. { dispinterface methode invoke? }
  3460. if assigned(methodpointer) and is_dispinterface(methodpointer.resultdef) then
  3461. begin
  3462. case procdefinition.proctypeoption of
  3463. potype_propgetter: calltype:=dct_propget;
  3464. potype_propsetter: calltype:=dct_propput;
  3465. else
  3466. calltype:=dct_method;
  3467. end;
  3468. { if the result is used, we've to insert a call to convert the type to be on the "safe side" }
  3469. if (cnf_return_value_used in callnodeflags) and not is_void(procdefinition.returndef) then
  3470. begin
  3471. result:=internalstatements(statements);
  3472. converted_result_data:=ctempcreatenode.create(procdefinition.returndef,sizeof(procdefinition.returndef),
  3473. tt_persistent,true);
  3474. addstatement(statements,converted_result_data);
  3475. addstatement(statements,cassignmentnode.create(ctemprefnode.create(converted_result_data),
  3476. ctypeconvnode.create_internal(
  3477. translate_disp_call(methodpointer,parameters,calltype,'',tprocdef(procdefinition).dispid,procdefinition.returndef),
  3478. procdefinition.returndef)));
  3479. addstatement(statements,ctempdeletenode.create_normal_temp(converted_result_data));
  3480. addstatement(statements,ctemprefnode.create(converted_result_data));
  3481. end
  3482. else
  3483. result:=translate_disp_call(methodpointer,parameters,calltype,'',tprocdef(procdefinition).dispid,voidtype);
  3484. { don't free reused nodes }
  3485. methodpointer:=nil;
  3486. parameters:=nil;
  3487. end;
  3488. maybe_gen_call_self_node;
  3489. if assigned(call_self_node) then
  3490. typecheckpass(call_self_node);
  3491. if assigned(call_vmt_node) then
  3492. typecheckpass(call_vmt_node);
  3493. finally
  3494. aktcallnode:=oldcallnode;
  3495. end;
  3496. end;
  3497. procedure tcallnode.order_parameters;
  3498. var
  3499. hp,hpcurr,hpnext,hpfirst,hpprev : tcallparanode;
  3500. currloc : tcgloc;
  3501. begin
  3502. hpfirst:=nil;
  3503. hpcurr:=tcallparanode(left);
  3504. { cache all info about parameters containing stack tainting calls,
  3505. since we will need it a lot below and calculting it can be expensive }
  3506. while assigned(hpcurr) do
  3507. begin
  3508. hpcurr.init_contains_stack_tainting_call_cache;
  3509. hpcurr:=tcallparanode(hpcurr.right);
  3510. end;
  3511. hpcurr:=tcallparanode(left);
  3512. while assigned(hpcurr) do
  3513. begin
  3514. { pull out }
  3515. hpnext:=tcallparanode(hpcurr.right);
  3516. { pull in at the correct place.
  3517. Used order:
  3518. 1. vs_out for a reference-counted type
  3519. 2. LOC_REFERENCE with smallest offset (i386 only)
  3520. 3. LOC_REFERENCE with least complexity (non-i386 only)
  3521. 4. LOC_REFERENCE with most complexity (non-i386 only)
  3522. 5. LOC_REGISTER with most complexity
  3523. 6. LOC_REGISTER with least complexity
  3524. For the moment we only look at the first parameter field. Combining it
  3525. with multiple parameter fields will make things a lot complexer (PFV)
  3526. The reason for the difference regarding complexity ordering
  3527. between LOC_REFERENCE and LOC_REGISTER is mainly for calls:
  3528. we first want to treat the LOC_REFERENCE destinations whose
  3529. calculation does not require a call, because their location
  3530. may contain registers which might otherwise have to be saved
  3531. if a call has to be evaluated first. The calculated value is
  3532. stored on the stack and will thus no longer occupy any
  3533. register.
  3534. Similarly, for the register parameters we first want to
  3535. evaluate the calls, because otherwise the already loaded
  3536. register parameters will have to be saved so the intermediate
  3537. call can be evaluated (JM) }
  3538. if not assigned(hpcurr.parasym.paraloc[callerside].location) then
  3539. internalerror(200412152);
  3540. currloc:=hpcurr.parasym.paraloc[callerside].location^.loc;
  3541. hpprev:=nil;
  3542. hp:=hpfirst;
  3543. { on fixed_stack targets, always evaluate parameters containing
  3544. a call with stack parameters before all other parameters,
  3545. because they will prevent any other parameters from being put
  3546. in their final place; if both the current and the next para
  3547. contain a stack tainting call, don't do anything to prevent
  3548. them from keeping on chasing eachother's tail }
  3549. while assigned(hp) do
  3550. begin
  3551. if paramanager.use_fixed_stack and
  3552. hpcurr.contains_stack_tainting_call_cached then
  3553. break;
  3554. case currloc of
  3555. LOC_REFERENCE :
  3556. begin
  3557. case hp.parasym.paraloc[callerside].location^.loc of
  3558. LOC_REFERENCE :
  3559. begin
  3560. { Offset is calculated like:
  3561. sub esp,12
  3562. mov [esp+8],para3
  3563. mov [esp+4],para2
  3564. mov [esp],para1
  3565. call function
  3566. That means the for pushes the para with the
  3567. highest offset (see para3) needs to be pushed first
  3568. }
  3569. {$if defined(i386) or defined(i8086) or defined(m68k)}
  3570. { the i386, i8086, m68k and jvm code generators expect all reference }
  3571. { parameters to be in this order so they can use }
  3572. { pushes in case of no fixed stack }
  3573. if (not paramanager.use_fixed_stack and
  3574. (hpcurr.parasym.paraloc[callerside].location^.reference.offset>
  3575. hp.parasym.paraloc[callerside].location^.reference.offset)) or
  3576. (paramanager.use_fixed_stack and
  3577. (node_complexity(hpcurr)<node_complexity(hp))) then
  3578. {$elseif defined(jvm)}
  3579. if (hpcurr.parasym.paraloc[callerside].location^.reference.offset<hp.parasym.paraloc[callerside].location^.reference.offset) then
  3580. {$else jvm}
  3581. if (node_complexity(hpcurr)<node_complexity(hp)) then
  3582. {$endif jvm}
  3583. break;
  3584. end;
  3585. LOC_MMREGISTER,
  3586. LOC_REGISTER,
  3587. LOC_FPUREGISTER :
  3588. break;
  3589. end;
  3590. end;
  3591. LOC_MMREGISTER,
  3592. LOC_FPUREGISTER,
  3593. LOC_REGISTER :
  3594. begin
  3595. if (hp.parasym.paraloc[callerside].location^.loc<>LOC_REFERENCE) and
  3596. (node_complexity(hpcurr)>node_complexity(hp)) then
  3597. break;
  3598. end;
  3599. end;
  3600. hpprev:=hp;
  3601. hp:=tcallparanode(hp.right);
  3602. end;
  3603. hpcurr.right:=hp;
  3604. if assigned(hpprev) then
  3605. hpprev.right:=hpcurr
  3606. else
  3607. hpfirst:=hpcurr;
  3608. { next }
  3609. hpcurr:=hpnext;
  3610. end;
  3611. left:=hpfirst;
  3612. { now mark each parameter that is followed by a stack-tainting call,
  3613. to determine on use_fixed_stack targets which ones can immediately be
  3614. put in their final destination. Unforunately we can never put register
  3615. parameters immediately in their final destination (even on register-
  3616. rich architectures such as the PowerPC), because the code generator
  3617. can still insert extra calls that only make use of register
  3618. parameters (fpc_move() etc. }
  3619. hpcurr:=hpfirst;
  3620. while assigned(hpcurr) do
  3621. begin
  3622. if hpcurr.contains_stack_tainting_call_cached then
  3623. begin
  3624. { all parameters before this one are followed by a stack
  3625. tainting call }
  3626. hp:=hpfirst;
  3627. while hp<>hpcurr do
  3628. begin
  3629. hp.ffollowed_by_stack_tainting_call_cached:=true;
  3630. hp:=tcallparanode(hp.right);
  3631. end;
  3632. hpfirst:=hpcurr;
  3633. end;
  3634. hpcurr:=tcallparanode(hpcurr.right);
  3635. end;
  3636. end;
  3637. procedure tcallnode.check_stack_parameters;
  3638. var
  3639. hp : tcallparanode;
  3640. begin
  3641. hp:=tcallparanode(left);
  3642. while assigned(hp) do
  3643. begin
  3644. if assigned(hp.parasym) and
  3645. assigned(hp.parasym.paraloc[callerside].location) and
  3646. (hp.parasym.paraloc[callerside].location^.loc=LOC_REFERENCE) then
  3647. include(current_procinfo.flags,pi_has_stackparameter);
  3648. hp:=tcallparanode(hp.right);
  3649. end;
  3650. end;
  3651. procedure tcallnode.check_inlining;
  3652. var
  3653. st : tsymtable;
  3654. para : tcallparanode;
  3655. begin
  3656. { Can we inline the procedure? }
  3657. if (po_inline in procdefinition.procoptions) and
  3658. (procdefinition.typ=procdef) and
  3659. tprocdef(procdefinition).has_inlininginfo and
  3660. { Prevent too deep inlining recursion and code bloat by inlining
  3661. The actual formuala is
  3662. inlinelevel+1 /-------
  3663. node count < -------------\/ 10000
  3664. This allows exponential grow of the code only to a certain limit.
  3665. Remarks
  3666. - The current approach calculates the inlining level top down, so outer call nodes (nodes closer to the leaf) might not be inlined
  3667. if the max. complexity is reached. This is done because it makes the implementation easier and because
  3668. there might be situations were it is more beneficial to inline inner nodes and do the calls to the outer nodes
  3669. if the outer nodes are in a seldomly used code path
  3670. - The code avoids to use functions from the math unit
  3671. }
  3672. (node_count(tprocdef(procdefinition).inlininginfo^.code)<round(exp((1.0/(inlinelevel+1))*ln(10000)))) then
  3673. begin
  3674. include(callnodeflags,cnf_do_inline);
  3675. { Check if we can inline the procedure when it references proc/var that
  3676. are not in the globally available }
  3677. st:=procdefinition.owner;
  3678. while (st.symtabletype in [ObjectSymtable,recordsymtable]) do
  3679. st:=st.defowner.owner;
  3680. if (pi_uses_static_symtable in tprocdef(procdefinition).inlininginfo^.flags) and
  3681. (st.symtabletype=globalsymtable) and
  3682. (not st.iscurrentunit) then
  3683. begin
  3684. Comment(V_lineinfo+V_Debug,'Not inlining "'+tprocdef(procdefinition).procsym.realname+'", references static symtable');
  3685. exclude(callnodeflags,cnf_do_inline);
  3686. end;
  3687. para:=tcallparanode(parameters);
  3688. while assigned(para) do
  3689. begin
  3690. if not para.can_be_inlined then
  3691. begin
  3692. Comment(V_lineinfo+V_Debug,'Not inlining "'+tprocdef(procdefinition).procsym.realname+
  3693. '", invocation parameter contains an unsafe/unsupported construct');
  3694. exclude(callnodeflags,cnf_do_inline);
  3695. break;
  3696. end;
  3697. para:=tcallparanode(para.nextpara);
  3698. end;
  3699. end;
  3700. end;
  3701. function tcallnode.pass_1 : tnode;
  3702. procedure mark_unregable_parameters;
  3703. var
  3704. hp : tcallparanode;
  3705. begin
  3706. hp:=tcallparanode(left);
  3707. while assigned(hp) do
  3708. begin
  3709. do_typecheckpass(hp.left);
  3710. { When the address needs to be pushed then the register is
  3711. not regable. Exception is when the location is also a var
  3712. parameter and we can pass the address transparently (but
  3713. that is handled by make_not_regable if ra_addr_regable is
  3714. passed, and make_not_regable always needs to called for
  3715. the ra_addr_taken info for non-invisble parameters) }
  3716. if (not (cpf_varargs_para in hp.callparaflags)) and (
  3717. not(
  3718. (vo_is_hidden_para in hp.parasym.varoptions) and
  3719. (hp.left.resultdef.typ in [pointerdef,classrefdef])
  3720. ) and
  3721. paramanager.push_addr_param(hp.parasym.varspez,hp.parasym.vardef,
  3722. self.procdefinition.proccalloption)
  3723. ) then
  3724. { pushing the address of a variable to take the place of a temp
  3725. as the complex function result of a function does not make its
  3726. address escape the current block, as the "address of the
  3727. function result" is not something which can be stored
  3728. persistently by the callee (it becomes invalid when the callee
  3729. returns) }
  3730. if not(vo_is_funcret in hp.parasym.varoptions) and
  3731. not(po_compilerproc in procdefinition.procoptions) then
  3732. make_not_regable(hp.left,[ra_addr_regable,ra_addr_taken])
  3733. else
  3734. make_not_regable(hp.left,[ra_addr_regable]);
  3735. hp:=tcallparanode(hp.right);
  3736. end;
  3737. end;
  3738. var
  3739. para: tcallparanode;
  3740. oldcallnode: tcallnode;
  3741. begin
  3742. result:=nil;
  3743. oldcallnode:=aktcallnode;
  3744. aktcallnode:=self;
  3745. try
  3746. { as pass_1 is never called on the methodpointer node, we must check
  3747. here that it's not a helper type }
  3748. if assigned(methodpointer) and
  3749. (methodpointer.nodetype=typen) and
  3750. is_objectpascal_helper(ttypenode(methodpointer).typedef) and
  3751. not ttypenode(methodpointer).helperallowed then
  3752. begin
  3753. CGMessage(parser_e_no_category_as_types);
  3754. { we get an internal error when trying to insert the hidden
  3755. parameters in this case }
  3756. exit;
  3757. end;
  3758. { can we get rid of the call? }
  3759. if (cs_opt_remove_emtpy_proc in current_settings.optimizerswitches) and
  3760. not(cnf_return_value_used in callnodeflags) and
  3761. (procdefinition.typ=procdef) and
  3762. tprocdef(procdefinition).isempty and
  3763. { allow only certain proc options }
  3764. ((tprocdef(procdefinition).procoptions-[po_none,po_classmethod,po_staticmethod,
  3765. po_interrupt,po_iocheck,po_assembler,po_msgstr,po_msgint,po_exports,po_external,po_overload,
  3766. po_nostackframe,po_has_mangledname,po_has_public_name,po_forward,po_global,
  3767. po_inline,po_compilerproc,po_has_importdll,po_has_importname,po_kylixlocal,po_dispid,po_delphi_nested_cc,
  3768. po_rtlproc,po_ignore_for_overload_resolution,po_auto_raised_visibility])=[]) then
  3769. begin
  3770. { check parameters for side effects }
  3771. para:=tcallparanode(left);
  3772. while assigned(para) do
  3773. begin
  3774. if (para.parasym.typ = paravarsym) and
  3775. ((para.parasym.refs>0) or
  3776. { array of consts are converted later on so we need to skip them here
  3777. else no error detection is done }
  3778. is_array_of_const(para.parasym.vardef) or
  3779. not(cs_opt_dead_values in current_settings.optimizerswitches) or
  3780. might_have_sideeffects(para.left)) then
  3781. break;
  3782. para:=tcallparanode(para.right);
  3783. end;
  3784. { finally, remove it if no parameter with side effect has been found }
  3785. if para=nil then
  3786. begin
  3787. result:=cnothingnode.create;
  3788. exit;
  3789. end;
  3790. end;
  3791. { convert Objective-C calls into a message call }
  3792. if (procdefinition.typ=procdef) and
  3793. (po_objc in tprocdef(procdefinition).procoptions) then
  3794. begin
  3795. if not(cnf_objc_processed in callnodeflags) then
  3796. objc_convert_to_message_send;
  3797. end
  3798. else
  3799. begin
  3800. { The following don't apply to obj-c: obj-c methods can never be
  3801. inlined because they're always virtual and the destination can
  3802. change at run, and for the same reason we also can't perform
  3803. WPO on them (+ they have no constructors) }
  3804. { Check if the call can be inlined, sets the cnf_do_inline flag }
  3805. check_inlining;
  3806. { must be called before maybe_load_in_temp(methodpointer), because
  3807. it converts the methodpointer into a temp in case it's a call
  3808. (and we want to know the original call)
  3809. }
  3810. register_created_object_types;
  3811. end;
  3812. { Maybe optimize the loading of the methodpointer using a temp. When the methodpointer
  3813. is a calln this is even required to not execute the calln twice.
  3814. This needs to be done after the resulttype pass, because in the resulttype we can still convert the
  3815. calln to a loadn (PFV) }
  3816. if assigned(methodpointer) then
  3817. maybe_load_in_temp(methodpointer);
  3818. { Create destination (temp or assignment-variable reuse) for function result if it not yet set }
  3819. maybe_create_funcret_node;
  3820. { Insert the self,vmt,function result in the parameters }
  3821. gen_hidden_parameters;
  3822. { Remove useless nodes from init/final blocks }
  3823. { (simplify depends on typecheck info) }
  3824. if assigned(callinitblock) then
  3825. begin
  3826. typecheckpass(tnode(callinitblock));
  3827. doinlinesimplify(tnode(callinitblock));
  3828. end;
  3829. if assigned(callcleanupblock) then
  3830. begin
  3831. typecheckpass(tnode(callcleanupblock));
  3832. doinlinesimplify(tnode(callcleanupblock));
  3833. end;
  3834. { If a constructor calls another constructor of the same or of an
  3835. inherited class, some targets (jvm) have to generate different
  3836. entry code for the constructor. }
  3837. if (current_procinfo.procdef.proctypeoption=potype_constructor) and
  3838. (procdefinition.typ=procdef) and
  3839. (tprocdef(procdefinition).proctypeoption=potype_constructor) and
  3840. ([cnf_member_call,cnf_inherited] * callnodeflags <> []) then
  3841. current_procinfo.ConstructorCallingConstructor:=true;
  3842. { object check helper will load VMT -> needs GOT }
  3843. if (cs_check_object in current_settings.localswitches) and
  3844. (cs_create_pic in current_settings.moduleswitches) then
  3845. include(current_procinfo.flags,pi_needs_got);
  3846. { Continue with checking a normal call or generate the inlined code }
  3847. if cnf_do_inline in callnodeflags then
  3848. result:=pass1_inline
  3849. else
  3850. begin
  3851. mark_unregable_parameters;
  3852. result:=pass1_normal;
  3853. end;
  3854. finally
  3855. aktcallnode:=oldcallnode;
  3856. end;
  3857. end;
  3858. function tcallnode.pass1_normal : tnode;
  3859. begin
  3860. result:=nil;
  3861. { calculate the parameter info for the procdef }
  3862. procdefinition.init_paraloc_info(callerside);
  3863. { calculate the parameter size needed for this call include varargs if they are available }
  3864. if assigned(varargsparas) then
  3865. pushedparasize:=paramanager.create_varargs_paraloc_info(procdefinition,varargsparas)
  3866. else
  3867. pushedparasize:=procdefinition.callerargareasize;
  3868. { record maximum parameter size used in this proc }
  3869. current_procinfo.allocate_push_parasize(pushedparasize);
  3870. { check for stacked parameters }
  3871. if assigned(left) and
  3872. (current_settings.optimizerswitches*[cs_opt_stackframe,cs_opt_level1]<>[]) then
  3873. check_stack_parameters;
  3874. if assigned(callinitblock) then
  3875. firstpass(tnode(callinitblock));
  3876. { function result node (tempref or simple load) }
  3877. if assigned(funcretnode) then
  3878. firstpass(funcretnode);
  3879. { parameters }
  3880. if assigned(left) then
  3881. tcallparanode(left).firstcallparan;
  3882. { procedure variable ? }
  3883. if assigned(right) then
  3884. firstpass(right);
  3885. if assigned(methodpointer) and
  3886. (methodpointer.nodetype<>typen) then
  3887. firstpass(methodpointer);
  3888. if assigned(callcleanupblock) then
  3889. firstpass(tnode(callcleanupblock));
  3890. if not (block_type in [bt_const,bt_type,bt_const_type,bt_var_type]) then
  3891. include(current_procinfo.flags,pi_do_call);
  3892. { order parameters }
  3893. order_parameters;
  3894. { get a register for the return value }
  3895. if (not is_void(resultdef)) then
  3896. begin
  3897. if paramanager.ret_in_param(resultdef,procdefinition) then
  3898. begin
  3899. expectloc:=LOC_REFERENCE;
  3900. end
  3901. else
  3902. { ansi/widestrings must be registered, so we can dispose them }
  3903. if is_ansistring(resultdef) or
  3904. is_widestring(resultdef) or
  3905. is_unicodestring(resultdef) then
  3906. begin
  3907. expectloc:=LOC_REFERENCE;
  3908. end
  3909. else
  3910. { we have only to handle the result if it is used }
  3911. if (cnf_return_value_used in callnodeflags) then
  3912. expectloc:=get_expect_loc
  3913. else
  3914. expectloc:=LOC_VOID;
  3915. end
  3916. else
  3917. expectloc:=LOC_VOID;
  3918. { create tree for VMT entry if required }
  3919. gen_vmt_entry_load;
  3920. end;
  3921. {$ifdef state_tracking}
  3922. function Tcallnode.track_state_pass(exec_known:boolean):boolean;
  3923. var hp:Tcallparanode;
  3924. value:Tnode;
  3925. begin
  3926. track_state_pass:=false;
  3927. hp:=Tcallparanode(left);
  3928. while assigned(hp) do
  3929. begin
  3930. if left.track_state_pass(exec_known) then
  3931. begin
  3932. left.resultdef:=nil;
  3933. do_typecheckpass(left);
  3934. end;
  3935. value:=aktstate.find_fact(hp.left);
  3936. if value<>nil then
  3937. begin
  3938. track_state_pass:=true;
  3939. hp.left.destroy;
  3940. hp.left:=value.getcopy;
  3941. do_typecheckpass(hp.left);
  3942. end;
  3943. hp:=Tcallparanode(hp.right);
  3944. end;
  3945. end;
  3946. {$endif}
  3947. {**************************************************************************
  3948. INLINING SUPPORT
  3949. **************************************************************************}
  3950. function tcallnode.replaceparaload(var n: tnode; arg: pointer): foreachnoderesult;
  3951. var
  3952. paras: tcallparanode;
  3953. temp: tnode;
  3954. indexnr : integer;
  3955. begin
  3956. result := fen_false;
  3957. n.fileinfo := pfileposinfo(arg)^;
  3958. if (n.nodetype = loadn) then
  3959. begin
  3960. case tloadnode(n).symtableentry.typ of
  3961. paravarsym :
  3962. begin
  3963. paras := tcallparanode(left);
  3964. while assigned(paras) and
  3965. (paras.parasym <> tloadnode(n).symtableentry) do
  3966. paras := tcallparanode(paras.right);
  3967. if assigned(paras) then
  3968. begin
  3969. temp:=paras.left.getcopy;
  3970. { inherit modification information, this is needed by the dfa/cse }
  3971. temp.flags:=temp.flags+(n.flags*[nf_modify,nf_write,nf_address_taken]);
  3972. n.free;
  3973. n:=temp;
  3974. typecheckpass(n);
  3975. result := fen_true;
  3976. end;
  3977. end;
  3978. localvarsym :
  3979. begin
  3980. { local? }
  3981. if (tloadnode(n).symtableentry.owner <> tprocdef(procdefinition).localst) then
  3982. exit;
  3983. indexnr:=tloadnode(n).symtableentry.owner.SymList.IndexOf(tloadnode(n).symtableentry);
  3984. if (indexnr >= inlinelocals.count) or
  3985. not assigned(inlinelocals[indexnr]) then
  3986. internalerror(20040720);
  3987. temp := tnode(inlinelocals[indexnr]).getcopy;
  3988. { inherit modification information, this is needed by the dfa/cse }
  3989. temp.flags:=temp.flags+(n.flags*[nf_modify,nf_write,nf_address_taken]);
  3990. n.free;
  3991. n:=temp;
  3992. typecheckpass(n);
  3993. result := fen_true;
  3994. end;
  3995. end;
  3996. end;
  3997. end;
  3998. procedure tcallnode.createlocaltemps(p:TObject;arg:pointer);
  3999. var
  4000. tempnode: ttempcreatenode;
  4001. indexnr : integer;
  4002. begin
  4003. if (TSym(p).typ <> localvarsym) then
  4004. exit;
  4005. indexnr:=TSym(p).Owner.SymList.IndexOf(p);
  4006. if (indexnr >= inlinelocals.count) then
  4007. inlinelocals.count:=indexnr+10;
  4008. if (vo_is_funcret in tabstractvarsym(p).varoptions) then
  4009. begin
  4010. if not assigned(funcretnode) then
  4011. internalerror(200709081);
  4012. inlinelocals[indexnr] := funcretnode.getcopy
  4013. end
  4014. else
  4015. begin
  4016. tempnode :=ctempcreatenode.create(tabstractvarsym(p).vardef,
  4017. tabstractvarsym(p).vardef.size,tt_persistent,tabstractvarsym(p).is_regvar(false));
  4018. addstatement(inlineinitstatement,tempnode);
  4019. if localvartrashing <> -1 then
  4020. cnodeutils.maybe_trash_variable(inlineinitstatement,tabstractnormalvarsym(p),ctemprefnode.create(tempnode));
  4021. addstatement(inlinecleanupstatement,ctempdeletenode.create(tempnode));
  4022. { inherit addr_taken flag }
  4023. if (tabstractvarsym(p).addr_taken) then
  4024. include(tempnode.tempinfo^.flags,ti_addr_taken);
  4025. inlinelocals[indexnr] := ctemprefnode.create(tempnode);
  4026. end;
  4027. end;
  4028. function nonlocalvars(var n: tnode; arg: pointer): foreachnoderesult;
  4029. begin
  4030. result := fen_false;
  4031. { this is just to play it safe, there are more safe situations }
  4032. if (n.nodetype = derefn) or
  4033. ((n.nodetype = loadn) and
  4034. { globals and fields of (possibly global) objects could always be changed in the callee }
  4035. ((tloadnode(n).symtable.symtabletype in [globalsymtable,ObjectSymtable]) or
  4036. { statics can only be modified by functions in the same unit }
  4037. ((tloadnode(n).symtable.symtabletype = staticsymtable) and
  4038. (tloadnode(n).symtable = TSymtable(arg))) or
  4039. { if the addr of the symbol is taken somewhere, it can be also non-local }
  4040. (tabstractvarsym(tloadnode(n).symtableentry).addr_taken)
  4041. )) or
  4042. ((n.nodetype = subscriptn) and
  4043. (tsubscriptnode(n).vs.owner.symtabletype = ObjectSymtable)) then
  4044. result := fen_norecurse_true;
  4045. end;
  4046. procedure tcallnode.createinlineparas;
  4047. var
  4048. para: tcallparanode;
  4049. tempnode: ttempcreatenode;
  4050. n,
  4051. realtarget: tnode;
  4052. paracomplexity: longint;
  4053. pushconstaddr: boolean;
  4054. trytotakeaddress : Boolean;
  4055. begin
  4056. { parameters }
  4057. para := tcallparanode(left);
  4058. pushconstaddr := false;
  4059. while assigned(para) do
  4060. begin
  4061. if (para.parasym.typ = paravarsym) and
  4062. ((para.parasym.refs>0) or
  4063. not(cs_opt_dead_values in current_settings.optimizerswitches) or
  4064. might_have_sideeffects(para.left)) then
  4065. begin
  4066. { must take copy of para.left, because if it contains a }
  4067. { temprefn pointing to a copied temp (e.g. methodpointer), }
  4068. { then this parameter must be changed to point to the copy of }
  4069. { that temp (JM) }
  4070. n := para.left.getcopy;
  4071. para.left.free;
  4072. para.left := n;
  4073. firstpass(para.left);
  4074. { determine how a parameter is passed to the inlined body
  4075. There are three options:
  4076. - insert the node tree of the callparanode directly
  4077. If a parameter is used only once, this is the best option if we can do so
  4078. - get the address of the argument, store it in a temp and insert a dereference to this temp
  4079. If the node tree cannot be inserted directly, taking the address of the argument and using it
  4080. is the second best option, but even this is not always possible
  4081. - assign the value of the argument to a newly created temp
  4082. This is the fall back which works always
  4083. Notes:
  4084. - we need to take care that we use the type of the defined parameter and not of the
  4085. passed parameter, because these can be different in case of a formaldef (PFV)
  4086. }
  4087. { pre-compute some values }
  4088. paracomplexity:=node_complexity(para.left);
  4089. if para.parasym.varspez=vs_const then
  4090. pushconstaddr:=paramanager.push_addr_param(vs_const,para.parasym.vardef,procdefinition.proccalloption);
  4091. realtarget:=actualtargetnode(@para.left)^;
  4092. { if the parameter is "complex", try to take the address
  4093. of the parameter expression, store it in a temp and replace
  4094. occurrences of the parameter with dereferencings of this
  4095. temp
  4096. }
  4097. trytotakeaddress:=
  4098. { don't create a temp. for function results }
  4099. not(nf_is_funcret in realtarget.flags) and
  4100. { this makes only sense if the parameter is reasonable complex else inserting directly is a better solution }
  4101. (
  4102. (paracomplexity>2) or
  4103. { don't create a temp. for the often seen case that p^ is passed to a var parameter }
  4104. ((paracomplexity>1) and
  4105. not((realtarget.nodetype=derefn) and (para.parasym.varspez in [vs_var,vs_out,vs_constref])) and
  4106. not((realtarget.nodetype=loadn) and tloadnode(realtarget).is_addr_param_load)
  4107. )
  4108. );
  4109. { check if we have to create a temp, assign the parameter's
  4110. contents to that temp and then substitute the parameter
  4111. with the temp everywhere in the function }
  4112. if
  4113. ((tparavarsym(para.parasym).varregable in [vr_none,vr_addr]) and
  4114. not(para.left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE])) or
  4115. { we can't assign to formaldef temps }
  4116. ((para.parasym.vardef.typ<>formaldef) and
  4117. (
  4118. { can we take the address of the argument? }
  4119. (trytotakeaddress and not(para.left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE])) or
  4120. (trytotakeaddress and
  4121. (not valid_for_addr(para.left,false) or
  4122. (para.left.nodetype = calln) or
  4123. is_constnode(para.left))) or
  4124. { we do not need to create a temp for value parameters }
  4125. { which are not modified in the inlined function }
  4126. { const parameters can get vs_readwritten if their }
  4127. { address is taken }
  4128. ((((para.parasym.varspez = vs_value) and
  4129. (para.parasym.varstate in [vs_initialised,vs_declared,vs_read])) or
  4130. { in case of const, this is only necessary if the
  4131. variable would be passed by value normally and if it is modified or if
  4132. there is such a variable somewhere in an expression }
  4133. ((para.parasym.varspez = vs_const) and
  4134. (not pushconstaddr))) and
  4135. { however, if we pass a global variable, an object field or}
  4136. { an expression containing a pointer dereference as }
  4137. { parameter, this value could be modified in other ways as }
  4138. { well and in such cases create a temp to be on the safe }
  4139. { side }
  4140. foreachnodestatic(para.left,@nonlocalvars,pointer(symtableproc))) or
  4141. { value parameters of which we know they are modified by }
  4142. { definition have to be copied to a temp }
  4143. { the same goes for cases of "x:=f(x)" where x is passed }
  4144. { as value parameter to f(), at least if we optimized }
  4145. { invocation by setting the funcretnode to x to avoid }
  4146. { assignment afterwards (since x may be read inside the }
  4147. { function after it modified result==x) }
  4148. ((para.parasym.varspez = vs_value) and
  4149. (not(para.parasym.varstate in [vs_initialised,vs_declared,vs_read]) or
  4150. (assigned(aktassignmentnode) and
  4151. (aktassignmentnode.right=self) and
  4152. (nf_assign_done_in_right in aktassignmentnode.flags) and
  4153. actualtargetnode(@aktassignmentnode.left)^.isequal(actualtargetnode(@para.left)^)))) or
  4154. { the compiler expects that it can take the address of parameters passed by reference in
  4155. the case of const so we can't replace the node simply by a constant node
  4156. When playing with this code, ensure that
  4157. function f(const a,b : longint) : longint;inline;
  4158. begin
  4159. result:=a*b;
  4160. end;
  4161. [...]
  4162. ...:=f(10,20));
  4163. [...]
  4164. is still folded. (FK)
  4165. }
  4166. ((para.parasym.varspez = vs_const) and
  4167. { const para's can get vs_readwritten if their address }
  4168. { is taken -> in case they are not passed by reference, }
  4169. { to keep the same behaviour as without inlining we have }
  4170. { to make a copy in case the originally passed parameter }
  4171. { value gets changed inside the callee }
  4172. ((not pushconstaddr and
  4173. (para.parasym.varstate = vs_readwritten)
  4174. ) or
  4175. { call-by-reference const's may need to be passed by }
  4176. { reference to function called in the inlined code }
  4177. (pushconstaddr and
  4178. not valid_for_addr(para.left,false))
  4179. )
  4180. )
  4181. )
  4182. ) then
  4183. begin
  4184. { don't create a new temp unnecessarily, but make sure we
  4185. do create a new one if the old one could be a regvar and
  4186. the new one cannot be one }
  4187. if not(tparavarsym(para.parasym).varspez in [vs_out,vs_var]) and (((para.left.nodetype<>temprefn) or
  4188. (((tparavarsym(para.parasym).varregable in [vr_none,vr_addr])) and
  4189. (ti_may_be_in_reg in ttemprefnode(para.left).tempinfo^.flags)))) then
  4190. begin
  4191. tempnode := ctempcreatenode.create(para.parasym.vardef,para.parasym.vardef.size,
  4192. tt_persistent,tparavarsym(para.parasym).is_regvar(false));
  4193. addstatement(inlineinitstatement,tempnode);
  4194. if localvartrashing <> -1 then
  4195. cnodeutils.maybe_trash_variable(inlineinitstatement,para.parasym,ctemprefnode.create(tempnode));
  4196. addstatement(inlinecleanupstatement,ctempdeletenode.create(tempnode));
  4197. addstatement(inlineinitstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  4198. para.left));
  4199. para.left := ctemprefnode.create(tempnode);
  4200. { inherit addr_taken flag }
  4201. if (tabstractvarsym(para.parasym).addr_taken) then
  4202. include(tempnode.tempinfo^.flags,ti_addr_taken);
  4203. end;
  4204. end
  4205. else if trytotakeaddress then
  4206. wrapcomplexinlinepara(para);
  4207. end;
  4208. para := tcallparanode(para.right);
  4209. end;
  4210. { local variables }
  4211. if not assigned(tprocdef(procdefinition).localst) or
  4212. (tprocdef(procdefinition).localst.SymList.count = 0) then
  4213. exit;
  4214. inlinelocals.count:=tprocdef(procdefinition).localst.SymList.count;
  4215. tprocdef(procdefinition).localst.SymList.ForEachCall(@createlocaltemps,nil);
  4216. end;
  4217. procedure tcallnode.wrapcomplexinlinepara(para: tcallparanode);
  4218. var
  4219. ptrtype: tdef;
  4220. tempnode: ttempcreatenode;
  4221. paraaddr: taddrnode;
  4222. begin
  4223. ptrtype:=cpointerdef.getreusable(para.left.resultdef);
  4224. tempnode:=ctempcreatenode.create(ptrtype,ptrtype.size,tt_persistent,true);
  4225. addstatement(inlineinitstatement,tempnode);
  4226. addstatement(inlinecleanupstatement,ctempdeletenode.create(tempnode));
  4227. { inherit addr_taken flag }
  4228. if (tabstractvarsym(para.parasym).addr_taken) then
  4229. include(tempnode.tempinfo^.flags,ti_addr_taken);
  4230. { inherit read only }
  4231. if tabstractvarsym(para.parasym).varspez=vs_const then
  4232. include(tempnode.tempinfo^.flags,ti_const);
  4233. paraaddr:=caddrnode.create_internal(para.left);
  4234. include(paraaddr.flags,nf_typedaddr);
  4235. addstatement(inlineinitstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  4236. paraaddr));
  4237. para.left:=cderefnode.create(ctemprefnode.create(tempnode));
  4238. end;
  4239. function tcallnode.optimize_funcret_assignment(inlineblock: tblocknode): tnode;
  4240. var
  4241. hp : tstatementnode;
  4242. hp2 : tnode;
  4243. resassign : tassignmentnode;
  4244. begin
  4245. result:=nil;
  4246. if not assigned(funcretnode) or
  4247. not(cnf_return_value_used in callnodeflags) then
  4248. exit;
  4249. { tempcreatenode for the function result }
  4250. hp:=tstatementnode(inlineblock.left);
  4251. if not(assigned(hp)) or
  4252. (hp.left.nodetype <> tempcreaten) or
  4253. not(nf_is_funcret in hp.left.flags) then
  4254. exit;
  4255. { constant assignment? right must be a constant (mainly to avoid trying
  4256. to reuse local temps which may already be freed afterwards once these
  4257. checks are made looser) }
  4258. hp:=tstatementnode(hp.right);
  4259. if not(assigned(hp)) or
  4260. (hp.left.nodetype<>assignn) or
  4261. not is_constnode(tassignmentnode(hp.left).right) then
  4262. exit;
  4263. { left must be function result }
  4264. resassign:=tassignmentnode(hp.left);
  4265. hp2:=resassign.left;
  4266. { can have extra type conversion due to absolute mapping
  4267. of <fucntionname> on function result var }
  4268. if (hp2.nodetype=typeconvn) and (ttypeconvnode(hp2).convtype=tc_equal) then
  4269. hp2:=ttypeconvnode(hp2).left;
  4270. if (hp2.nodetype<>temprefn) or
  4271. not(nf_is_funcret in hp2.flags) then
  4272. exit;
  4273. { tempdelete to normal of the function result }
  4274. hp:=tstatementnode(hp.right);
  4275. if not(assigned(hp)) or
  4276. (hp.left.nodetype <> tempdeleten) then
  4277. exit;
  4278. { the function result once more }
  4279. hp:=tstatementnode(hp.right);
  4280. if not(assigned(hp)) or
  4281. (hp.left.nodetype<>temprefn) or
  4282. not(nf_is_funcret in hp.left.flags) then
  4283. exit;
  4284. { should be the end }
  4285. if assigned(hp.right) then
  4286. exit;
  4287. { we made it! }
  4288. result:=tassignmentnode(resassign).right.getcopy;
  4289. firstpass(result);
  4290. end;
  4291. { this procedure removes the user code flag because it prevents optimizations }
  4292. function removeusercodeflag(var n : tnode; arg : pointer) : foreachnoderesult;
  4293. begin
  4294. result:=fen_false;
  4295. if nf_usercode_entry in n.flags then
  4296. begin
  4297. exclude(n.flags,nf_usercode_entry);
  4298. result:=fen_norecurse_true;
  4299. end;
  4300. end;
  4301. function tcallnode.pass1_inline:tnode;
  4302. var
  4303. n,
  4304. body : tnode;
  4305. para : tcallparanode;
  4306. inlineblock,
  4307. inlinecleanupblock : tblocknode;
  4308. begin
  4309. inc(inlinelevel);
  4310. result:=nil;
  4311. if not(assigned(tprocdef(procdefinition).inlininginfo) and
  4312. assigned(tprocdef(procdefinition).inlininginfo^.code)) then
  4313. internalerror(200412021);
  4314. inlinelocals:=TFPObjectList.create(true);
  4315. { inherit flags }
  4316. current_procinfo.flags:=current_procinfo.flags+
  4317. ((procdefinition as tprocdef).inlininginfo^.flags*inherited_inlining_flags);
  4318. { Create new code block for inlining }
  4319. inlineblock:=internalstatements(inlineinitstatement);
  4320. { make sure that valid_for_assign() returns false for this block
  4321. (otherwise assigning values to the block will result in assigning
  4322. values to the inlined function's result) }
  4323. include(inlineblock.flags,nf_no_lvalue);
  4324. inlinecleanupblock:=internalstatements(inlinecleanupstatement);
  4325. if assigned(callinitblock) then
  4326. addstatement(inlineinitstatement,callinitblock.getcopy);
  4327. { replace complex parameters with temps }
  4328. createinlineparas;
  4329. { create a copy of the body and replace parameter loads with the parameter values }
  4330. body:=tprocdef(procdefinition).inlininginfo^.code.getcopy;
  4331. foreachnodestatic(pm_postprocess,body,@removeusercodeflag,nil);
  4332. foreachnode(pm_preprocess,body,@replaceparaload,@fileinfo);
  4333. { Concat the body and finalization parts }
  4334. addstatement(inlineinitstatement,body);
  4335. addstatement(inlineinitstatement,inlinecleanupblock);
  4336. inlinecleanupblock:=nil;
  4337. if assigned(callcleanupblock) then
  4338. addstatement(inlineinitstatement,callcleanupblock.getcopy);
  4339. { the last statement of the new inline block must return the
  4340. location and type of the function result.
  4341. This is not needed when the result is not used, also the tempnode is then
  4342. already destroyed by a tempdelete in the callcleanupblock tree }
  4343. if not is_void(resultdef) and
  4344. (cnf_return_value_used in callnodeflags) then
  4345. begin
  4346. if assigned(funcretnode) then
  4347. addstatement(inlineinitstatement,funcretnode.getcopy)
  4348. else
  4349. begin
  4350. para:=tcallparanode(left);
  4351. while assigned(para) do
  4352. begin
  4353. if (vo_is_hidden_para in para.parasym.varoptions) and
  4354. (vo_is_funcret in para.parasym.varoptions) then
  4355. begin
  4356. addstatement(inlineinitstatement,para.left.getcopy);
  4357. break;
  4358. end;
  4359. para:=tcallparanode(para.right);
  4360. end;
  4361. end;
  4362. end;
  4363. { consider it must not be inlined if called
  4364. again inside the args or itself }
  4365. exclude(procdefinition.procoptions,po_inline);
  4366. typecheckpass(tnode(inlineblock));
  4367. doinlinesimplify(tnode(inlineblock));
  4368. firstpass(tnode(inlineblock));
  4369. include(procdefinition.procoptions,po_inline);
  4370. result:=inlineblock;
  4371. { if the function result is used then verify that the blocknode
  4372. returns the same result type as the original callnode }
  4373. if (cnf_return_value_used in callnodeflags) and
  4374. (result.resultdef<>resultdef) then
  4375. internalerror(200709171);
  4376. { free the temps for the locals }
  4377. inlinelocals.free;
  4378. inlinelocals:=nil;
  4379. inlineinitstatement:=nil;
  4380. inlinecleanupstatement:=nil;
  4381. { if all that's left of the inlined function is an constant assignment
  4382. to the result, replace the whole block with the constant only }
  4383. n:=optimize_funcret_assignment(inlineblock);
  4384. if assigned(n) then
  4385. begin
  4386. inlineblock.free;
  4387. result:=n;
  4388. end;
  4389. {$ifdef DEBUGINLINE}
  4390. writeln;
  4391. writeln('**************************',tprocdef(procdefinition).mangledname);
  4392. printnode(output,result);
  4393. {$endif DEBUGINLINE}
  4394. dec(inlinelevel);
  4395. end;
  4396. end.