ncal.pas 202 KB

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