ncal.pas 208 KB

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