ncal.pas 210 KB

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