ncal.pas 206 KB

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