ncal.pas 208 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994
  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 tcallnode.funcret_can_be_reused:boolean;
  2640. var
  2641. realassignmenttarget: tnode;
  2642. alignment: longint;
  2643. begin
  2644. result:=false;
  2645. { we are processing an assignment node? }
  2646. if not(assigned(aktassignmentnode) and
  2647. (aktassignmentnode.right=self) and
  2648. (aktassignmentnode.left.resultdef=resultdef)) then
  2649. exit;
  2650. { destination must be able to be passed as var parameter }
  2651. if not valid_for_var(aktassignmentnode.left,false) then
  2652. exit;
  2653. { destination must be a simple load so it doesn't need a temp when
  2654. it is evaluated }
  2655. if not is_simple_para_load(aktassignmentnode.left,false) then
  2656. exit;
  2657. { remove possible typecasts }
  2658. realassignmenttarget:=actualtargetnode(@aktassignmentnode.left)^;
  2659. { when it is not passed in a parameter it will only be used after the
  2660. function call }
  2661. if not paramanager.ret_in_param(resultdef,procdefinition) then
  2662. begin
  2663. { don't replace the function result if we are inlining and if the destination is complex, this
  2664. could lead to lengthy code in case the function result is used often and it is assigned e.g.
  2665. to a threadvar }
  2666. result:=not(cnf_do_inline in callnodeflags) or
  2667. (node_complexity(aktassignmentnode.left)<=1);
  2668. exit;
  2669. end;
  2670. { if the result is the same as the self parameter (in case of objects),
  2671. we can't optimise. We have to check this explicitly becaise
  2672. hidden parameters such as self have not yet been inserted at this
  2673. point
  2674. }
  2675. if assigned(methodpointer) and
  2676. realassignmenttarget.isequal(actualtargetnode(@methodpointer)^) then
  2677. exit;
  2678. { when we substitute a function result inside an inlined function,
  2679. we may take the address of this function result. Therefore the
  2680. substituted function result may not be in a register, as we cannot
  2681. take its address in that case }
  2682. if (realassignmenttarget.nodetype=temprefn) and
  2683. not(ti_addr_taken in ttemprefnode(realassignmenttarget).tempflags) and
  2684. not(ti_may_be_in_reg in ttemprefnode(realassignmenttarget).tempflags) then
  2685. begin
  2686. result:=true;
  2687. exit;
  2688. end;
  2689. if (realassignmenttarget.nodetype=loadn) and
  2690. { nested procedures may access the current procedure's locals }
  2691. (procdefinition.parast.symtablelevel=normal_function_level) and
  2692. { must be a local variable, a value para or a hidden function result }
  2693. { parameter (which can be passed by address, but in that case it got }
  2694. { through these same checks at the caller side and is thus safe ) }
  2695. { other option: we're calling a compilerproc, because those don't
  2696. rely on global state
  2697. }
  2698. ((po_compilerproc in procdefinition.procoptions) or
  2699. (
  2700. (
  2701. (tloadnode(realassignmenttarget).symtableentry.typ=localvarsym) or
  2702. (
  2703. (tloadnode(realassignmenttarget).symtableentry.typ=paravarsym) and
  2704. ((tparavarsym(tloadnode(realassignmenttarget).symtableentry).varspez = vs_value) or
  2705. (vo_is_funcret in tparavarsym(tloadnode(realassignmenttarget).symtableentry).varoptions))
  2706. )
  2707. ) and
  2708. { the address may not have been taken of the variable/parameter, because }
  2709. { otherwise it's possible that the called function can access it via a }
  2710. { global variable or other stored state }
  2711. (
  2712. not(tabstractvarsym(tloadnode(realassignmenttarget).symtableentry).addr_taken) and
  2713. (tabstractvarsym(tloadnode(realassignmenttarget).symtableentry).varregable in [vr_none,vr_addr])
  2714. )
  2715. )
  2716. ) then
  2717. begin
  2718. { If the funcret is also used as a parameter we can't optimize because the funcret
  2719. and the parameter will point to the same address. That means that a change of the result variable
  2720. will result also in a change of the parameter value }
  2721. result:=not foreachnodestatic(left,@check_funcret_used_as_para,tloadnode(realassignmenttarget).symtableentry);
  2722. { ensure that it is aligned using the default alignment }
  2723. alignment:=tabstractvarsym(tloadnode(realassignmenttarget).symtableentry).vardef.alignment;
  2724. if (used_align(alignment,target_info.alignment.localalignmin,target_info.alignment.localalignmax)<>
  2725. used_align(alignment,current_settings.alignment.localalignmin,current_settings.alignment.localalignmax)) then
  2726. result:=false;
  2727. exit;
  2728. end;
  2729. end;
  2730. procedure tcallnode.maybe_create_funcret_node;
  2731. var
  2732. temp : ttempcreatenode;
  2733. begin
  2734. if procdefinition.proctypeoption=potype_constructor then
  2735. exit;
  2736. { For the function result we need to create a temp node for:
  2737. - Inlined functions
  2738. - Types requiring initialization/finalization
  2739. - Types passed in parameters }
  2740. if not is_void(resultdef) and
  2741. not assigned(funcretnode) and
  2742. (
  2743. (cnf_do_inline in callnodeflags) or
  2744. is_managed_type(resultdef) or
  2745. paramanager.ret_in_param(resultdef,procdefinition)
  2746. ) then
  2747. begin
  2748. { Optimize calls like x:=f() where we can use x directly as
  2749. result instead of using a temp. Condition is that x cannot be accessed from f().
  2750. This implies that x is a local variable or value parameter of the current block
  2751. and its address is not passed to f. One problem: what if someone takes the
  2752. address of x, puts it in a pointer variable/field and then accesses it that way
  2753. from within the function? This is solved (in a conservative way) using the
  2754. ti_addr_taken flag.
  2755. When the result is not not passed in a parameter there are no problem because
  2756. then it means only reference counted types (eg. ansistrings) that need a decr
  2757. of the refcount before being assigned. This is all done after the call so there
  2758. is no issue with exceptions and possible use of the old value in the called
  2759. function }
  2760. if funcret_can_be_reused then
  2761. begin
  2762. funcretnode:=aktassignmentnode.left.getcopy;
  2763. include(funcretnode.flags,nf_is_funcret);
  2764. { notify the assignment node that the assignment can be removed }
  2765. include(aktassignmentnode.flags,nf_assign_done_in_right);
  2766. end
  2767. else
  2768. begin
  2769. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,
  2770. (cnf_do_inline in callnodeflags) and
  2771. not(tabstractvarsym(tprocdef(procdefinition).funcretsym).varregable in [vr_none,vr_addr]));
  2772. include(temp.flags,nf_is_funcret);
  2773. { if a managed type is returned by reference, assigning something
  2774. to the result on the caller side will take care of decreasing
  2775. the reference count }
  2776. if paramanager.ret_in_param(resultdef,procdefinition) then
  2777. temp.includetempflag(ti_nofini);
  2778. add_init_statement(temp);
  2779. { When the function result is not used in an inlined function
  2780. we need to delete the temp. This can currently only be done by
  2781. a tempdeletenode and not after converting it to a normal temp }
  2782. if not(cnf_return_value_used in callnodeflags) and
  2783. (cnf_do_inline in callnodeflags) then
  2784. add_done_statement(ctempdeletenode.create(temp))
  2785. else
  2786. add_done_statement(ctempdeletenode.create_normal_temp(temp));
  2787. funcretnode:=ctemprefnode.create(temp);
  2788. include(funcretnode.flags,nf_is_funcret);
  2789. end;
  2790. end;
  2791. end;
  2792. procedure tcallnode.gen_hidden_parameters;
  2793. var
  2794. para : tcallparanode;
  2795. begin
  2796. para:=tcallparanode(left);
  2797. while assigned(para) do
  2798. begin
  2799. { The processing of high() and typeinfo() is already
  2800. done in the typecheckpass. We only need to process the
  2801. nodes that still have a nothingn }
  2802. if (vo_is_hidden_para in para.parasym.varoptions) and
  2803. (para.left.nodetype=nothingn) then
  2804. begin
  2805. { remove dummy nothingn }
  2806. para.left.free;
  2807. para.left:=nil;
  2808. { generate the corresponding nodes for the hidden parameter type }
  2809. if (vo_is_funcret in para.parasym.varoptions) then
  2810. begin
  2811. if not assigned(funcretnode) then
  2812. internalerror(200709083);
  2813. { if funcretnode is a temprefnode, we have to keep it intact
  2814. if it may have been created in maybe_create_funcret_node(),
  2815. because then it will also be destroyed by a
  2816. ctempdeletenode.create_normal_temp() in the cleanup code
  2817. for this call code. In that case we have to copy this
  2818. ttemprefnode after the tempdeletenode to reset its
  2819. tempinfo^.hookoncopy. This is done by copying funcretnode
  2820. in tcallnode.getcopy(), but for that to work we can't reset
  2821. funcretnode to nil here. }
  2822. if (funcretnode.nodetype<>temprefn) or
  2823. (not(cnf_return_value_used in callnodeflags) and
  2824. (cnf_do_inline in callnodeflags)) then
  2825. begin
  2826. para.left:=funcretnode;
  2827. funcretnode:=nil;
  2828. end
  2829. else
  2830. para.left:=funcretnode.getcopy;
  2831. end
  2832. else
  2833. if vo_is_self in para.parasym.varoptions then
  2834. begin
  2835. if assigned(right) then
  2836. para.left:=gen_procvar_context_tree_self
  2837. else
  2838. para.left:=gen_self_tree;
  2839. { make sure that e.g. the self pointer of an advanced
  2840. record does not become a regvar, because it's a vs_var
  2841. parameter }
  2842. if paramanager.push_addr_param(para.parasym.varspez,para.parasym.vardef,
  2843. procdefinition.proccalloption) then
  2844. make_not_regable(para.left,[ra_addr_regable]);
  2845. end
  2846. else
  2847. if vo_is_vmt in para.parasym.varoptions then
  2848. begin
  2849. para.left:=gen_vmt_tree;
  2850. end
  2851. else
  2852. if vo_is_syscall_lib in para.parasym.varoptions then
  2853. gen_syscall_para(para)
  2854. else
  2855. if vo_is_range_check in para.parasym.varoptions then
  2856. begin
  2857. para.left:=cordconstnode.create(Ord(cs_check_range in current_settings.localswitches),pasbool8type,false);
  2858. end
  2859. else
  2860. if vo_is_overflow_check in para.parasym.varoptions then
  2861. begin
  2862. para.left:=cordconstnode.create(Ord(cs_check_overflow in current_settings.localswitches),pasbool8type,false);
  2863. end
  2864. else
  2865. if vo_is_msgsel in para.parasym.varoptions then
  2866. begin
  2867. para.left:=cobjcselectornode.create(cstringconstnode.createstr(tprocdef(procdefinition).messageinf.str^));
  2868. end;
  2869. end;
  2870. if not assigned(para.left) then
  2871. internalerror(200709084);
  2872. para:=tcallparanode(para.right);
  2873. end;
  2874. end;
  2875. procedure tcallnode.verifyabstract(sym:TObject;arg:pointer);
  2876. var
  2877. pd : tprocdef;
  2878. i : longint;
  2879. j : integer;
  2880. hs : string;
  2881. begin
  2882. if (tsym(sym).typ<>procsym) then
  2883. exit;
  2884. for i:=0 to tprocsym(sym).ProcdefList.Count-1 do
  2885. begin
  2886. pd:=tprocdef(tprocsym(sym).ProcdefList[i]);
  2887. hs:=pd.procsym.name+pd.typename_paras([]);
  2888. j:=AbstractMethodsList.FindIndexOf(hs);
  2889. if j<>-1 then
  2890. AbstractMethodsList[j]:=pd
  2891. else
  2892. AbstractMethodsList.Add(hs,pd);
  2893. end;
  2894. end;
  2895. procedure tcallnode.verifyabstractcalls;
  2896. var
  2897. objectdf : tobjectdef;
  2898. parents : tlinkedlist;
  2899. objectinfo : tobjectinfoitem;
  2900. pd : tprocdef;
  2901. i : integer;
  2902. begin
  2903. objectdf := nil;
  2904. { verify if trying to create an instance of a class which contains
  2905. non-implemented abstract methods }
  2906. { first verify this class type, no class than exit }
  2907. { also, this checking can only be done if the constructor is directly
  2908. called, indirect constructor calls cannot be checked.
  2909. }
  2910. if assigned(methodpointer) and
  2911. not((methodpointer.nodetype=loadn) and
  2912. (loadnf_is_self in tloadnode(methodpointer).loadnodeflags)) then
  2913. begin
  2914. if (methodpointer.resultdef.typ = objectdef) then
  2915. objectdf:=tobjectdef(methodpointer.resultdef)
  2916. else
  2917. if (methodpointer.resultdef.typ = classrefdef) and
  2918. (tclassrefdef(methodpointer.resultdef).pointeddef.typ = objectdef) and
  2919. (methodpointer.nodetype in [typen,loadvmtaddrn]) then
  2920. objectdf:=tobjectdef(tclassrefdef(methodpointer.resultdef).pointeddef);
  2921. end;
  2922. if not assigned(objectdf) then
  2923. exit;
  2924. { quick exit if nothing to check }
  2925. if objectdf.abstractcnt = 0 then
  2926. exit;
  2927. parents := tlinkedlist.create;
  2928. AbstractMethodsList := TFPHashList.create;
  2929. { insert all parents in this class : the first item in the
  2930. list will be the base parent of the class .
  2931. }
  2932. while assigned(objectdf) do
  2933. begin
  2934. objectinfo:=tobjectinfoitem.create(objectdf);
  2935. parents.insert(objectinfo);
  2936. objectdf := objectdf.childof;
  2937. end;
  2938. { now all parents are in the correct order
  2939. insert all abstract methods in the list, and remove
  2940. those which are overridden by parent classes.
  2941. }
  2942. objectinfo:=tobjectinfoitem(parents.first);
  2943. while assigned(objectinfo) do
  2944. begin
  2945. objectdf := objectinfo.objinfo;
  2946. if assigned(objectdf.symtable) then
  2947. objectdf.symtable.SymList.ForEachCall(@verifyabstract,nil);
  2948. objectinfo:=tobjectinfoitem(objectinfo.next);
  2949. end;
  2950. if assigned(parents) then
  2951. parents.free;
  2952. { Finally give out a warning for each abstract method still in the list }
  2953. for i:=0 to AbstractMethodsList.Count-1 do
  2954. begin
  2955. pd:=tprocdef(AbstractMethodsList[i]);
  2956. if po_abstractmethod in pd.procoptions then
  2957. begin
  2958. Message2(type_w_instance_with_abstract,objectdf.objrealname^,pd.procsym.RealName);
  2959. MessagePos1(pd.fileinfo,sym_h_abstract_method_list,pd.fullprocname(true));
  2960. end;
  2961. end;
  2962. if assigned(AbstractMethodsList) then
  2963. AbstractMethodsList.Free;
  2964. end;
  2965. procedure tcallnode.convert_carg_array_of_const;
  2966. var
  2967. hp : tarrayconstructornode;
  2968. oldleft : tcallparanode;
  2969. begin
  2970. oldleft:=tcallparanode(left);
  2971. if oldleft.left.nodetype<>arrayconstructorn then
  2972. begin
  2973. CGMessage1(type_e_wrong_type_in_array_constructor,oldleft.left.resultdef.typename);
  2974. exit;
  2975. end;
  2976. include(callnodeflags,cnf_uses_varargs);
  2977. { Get arrayconstructor node and insert typeconvs }
  2978. hp:=tarrayconstructornode(oldleft.left);
  2979. { Add c args parameters }
  2980. { It could be an empty set }
  2981. if assigned(hp) and
  2982. assigned(hp.left) then
  2983. begin
  2984. while assigned(hp) do
  2985. begin
  2986. left:=ccallparanode.create(hp.left,left);
  2987. { set callparanode resultdef and flags }
  2988. left.resultdef:=hp.left.resultdef;
  2989. include(tcallparanode(left).callparaflags,cpf_varargs_para);
  2990. hp.left:=nil;
  2991. hp:=tarrayconstructornode(hp.right);
  2992. end;
  2993. end;
  2994. { Remove value of old array of const parameter, but keep it
  2995. in the list because it is required for bind_parasym.
  2996. Generate a nothign to keep callparanoed.left valid }
  2997. oldleft.left.free;
  2998. oldleft.left:=cnothingnode.create;
  2999. end;
  3000. procedure tcallnode.bind_parasym;
  3001. type
  3002. pcallparanode = ^tcallparanode;
  3003. var
  3004. i : integer;
  3005. pt : tcallparanode;
  3006. oldppt : pcallparanode;
  3007. varargspara,
  3008. currpara : tparavarsym;
  3009. hiddentree : tnode;
  3010. paradef : tdef;
  3011. begin
  3012. pt:=tcallparanode(left);
  3013. oldppt:=pcallparanode(@left);
  3014. { flag all callparanodes that belong to the varargs }
  3015. i:=paralength;
  3016. while (i>procdefinition.maxparacount) do
  3017. begin
  3018. include(pt.callparaflags,cpf_varargs_para);
  3019. oldppt:=pcallparanode(@pt.right);
  3020. pt:=tcallparanode(pt.right);
  3021. dec(i);
  3022. end;
  3023. { skip varargs that are inserted by array of const }
  3024. while assigned(pt) and
  3025. (cpf_varargs_para in pt.callparaflags) do
  3026. pt:=tcallparanode(pt.right);
  3027. { process normal parameters and insert hidden parameter nodes, the content
  3028. of the hidden parameters will be updated in pass1 }
  3029. for i:=procdefinition.paras.count-1 downto 0 do
  3030. begin
  3031. currpara:=tparavarsym(procdefinition.paras[i]);
  3032. if vo_is_hidden_para in currpara.varoptions then
  3033. begin
  3034. { Here we handle only the parameters that depend on
  3035. the types of the previous parameter. The typeconversion
  3036. can change the type in the next step. For example passing
  3037. an array can be change to a pointer and a deref.
  3038. We also handle the generation of parentfp parameters, as they
  3039. must all be created before pass_1 on targets that use explicit
  3040. parentfp structs (rather than the frame pointer). The reason
  3041. is that the necessary initialisation code for the these
  3042. structures is attached to the procedure's nodetree after
  3043. the resulttype pass.
  3044. }
  3045. if vo_is_high_para in currpara.varoptions then
  3046. begin
  3047. if not assigned(pt) or (i=0) then
  3048. internalerror(200304081);
  3049. { we need the information of the previous parameter }
  3050. paradef:=tparavarsym(procdefinition.paras[i-1]).vardef;
  3051. hiddentree:=gen_high_tree(pt.left,paradef);
  3052. { for open array of managed type, a copy of high parameter is
  3053. necessary to properly initialize before the call }
  3054. if is_open_array(paradef) and
  3055. (tparavarsym(procdefinition.paras[i-1]).varspez=vs_out) and
  3056. is_managed_type(tarraydef(paradef).elementdef) then
  3057. begin
  3058. typecheckpass(hiddentree);
  3059. {this eliminates double call to fpc_dynarray_high, if any}
  3060. maybe_load_in_temp(hiddentree);
  3061. oldppt^.third:=hiddentree.getcopy;
  3062. end;
  3063. end
  3064. else
  3065. if vo_is_typinfo_para in currpara.varoptions then
  3066. begin
  3067. if not assigned(pt) or (i=0) then
  3068. internalerror(200304082);
  3069. hiddentree:=caddrnode.create_internal(
  3070. crttinode.create(Tstoreddef(pt.resultdef),fullrtti,rdt_normal)
  3071. );
  3072. end
  3073. else if vo_is_parentfp in currpara.varoptions then
  3074. begin
  3075. if assigned(right) and (right.resultdef.typ=procvardef) and
  3076. not tabstractprocdef(right.resultdef).is_addressonly then
  3077. maybe_load_in_temp(right);
  3078. if not assigned(right) then
  3079. begin
  3080. if assigned(procdefinition.owner.defowner) then
  3081. hiddentree:=cloadparentfpnode.create(tprocdef(procdefinition.owner.defowner),lpf_forpara)
  3082. { exceptfilters called from main level are not owned }
  3083. else if procdefinition.proctypeoption=potype_exceptfilter then
  3084. hiddentree:=cloadparentfpnode.create(current_procinfo.procdef,lpf_forpara)
  3085. else
  3086. internalerror(200309287);
  3087. end
  3088. else if not(po_is_block in procdefinition.procoptions) then
  3089. hiddentree:=gen_procvar_context_tree_parentfp
  3090. else
  3091. hiddentree:=gen_block_context
  3092. end
  3093. else
  3094. hiddentree:=cnothingnode.create;
  3095. pt:=ccallparanode.create(hiddentree,oldppt^);
  3096. oldppt^:=pt;
  3097. end;
  3098. if not assigned(pt) then
  3099. internalerror(200310052);
  3100. pt.parasym:=currpara;
  3101. oldppt:=pcallparanode(@pt.right);
  3102. pt:=tcallparanode(pt.right);
  3103. end;
  3104. { Create parasyms for varargs, first count the number of varargs paras,
  3105. then insert the parameters with numbering in reverse order. The SortParas
  3106. will set the correct order at the end}
  3107. pt:=tcallparanode(left);
  3108. i:=0;
  3109. while assigned(pt) do
  3110. begin
  3111. if cpf_varargs_para in pt.callparaflags then
  3112. inc(i);
  3113. pt:=tcallparanode(pt.right);
  3114. end;
  3115. if (i>0) then
  3116. begin
  3117. include(current_procinfo.flags,pi_calls_c_varargs);
  3118. varargsparas:=tvarargsparalist.create;
  3119. pt:=tcallparanode(left);
  3120. while assigned(pt) do
  3121. begin
  3122. if cpf_varargs_para in pt.callparaflags then
  3123. begin
  3124. varargspara:=cparavarsym.create('va'+tostr(i),i,vs_value,pt.resultdef,[]);
  3125. dec(i);
  3126. { varargspara is left-right, use insert
  3127. instead of concat }
  3128. varargsparas.add(varargspara);
  3129. pt.parasym:=varargspara;
  3130. end;
  3131. pt:=tcallparanode(pt.right);
  3132. end;
  3133. varargsparas.sortparas;
  3134. end;
  3135. end;
  3136. function tcallnode.pass_typecheck:tnode;
  3137. var
  3138. candidates : tcallcandidates;
  3139. oldcallnode : tcallnode;
  3140. hpt : tnode;
  3141. pt : tcallparanode;
  3142. lastpara : longint;
  3143. paraidx,
  3144. cand_cnt : integer;
  3145. i : longint;
  3146. ignorevisibility,
  3147. is_const : boolean;
  3148. statements : tstatementnode;
  3149. converted_result_data : ttempcreatenode;
  3150. calltype: tdispcalltype;
  3151. begin
  3152. result:=nil;
  3153. candidates:=nil;
  3154. oldcallnode:=aktcallnode;
  3155. aktcallnode:=self;
  3156. try
  3157. { determine length of parameter list }
  3158. pt:=tcallparanode(left);
  3159. paralength:=0;
  3160. while assigned(pt) do
  3161. begin
  3162. inc(paralength);
  3163. pt:=tcallparanode(pt.right);
  3164. end;
  3165. { determine the type of the parameters }
  3166. if assigned(left) then
  3167. begin
  3168. tcallparanode(left).get_paratype;
  3169. if codegenerror then
  3170. exit;
  3171. end;
  3172. if assigned(methodpointer) then
  3173. typecheckpass(methodpointer);
  3174. { procedure variable ? }
  3175. if assigned(right) then
  3176. begin
  3177. set_varstate(right,vs_read,[vsf_must_be_valid]);
  3178. typecheckpass(right);
  3179. if codegenerror then
  3180. exit;
  3181. procdefinition:=tabstractprocdef(right.resultdef);
  3182. { Compare parameters from right to left }
  3183. paraidx:=procdefinition.Paras.count-1;
  3184. { Skip default parameters }
  3185. if not(po_varargs in procdefinition.procoptions) then
  3186. begin
  3187. { ignore hidden parameters }
  3188. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) do
  3189. dec(paraidx);
  3190. for i:=1 to procdefinition.maxparacount-paralength do
  3191. begin
  3192. if paraidx<0 then
  3193. internalerror(200402265);
  3194. if not assigned(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym) then
  3195. begin
  3196. CGMessage1(parser_e_wrong_parameter_size,'<Procedure Variable>');
  3197. exit;
  3198. end;
  3199. dec(paraidx);
  3200. end;
  3201. end;
  3202. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) do
  3203. dec(paraidx);
  3204. pt:=tcallparanode(left);
  3205. lastpara:=paralength;
  3206. while (paraidx>=0) and assigned(pt) do
  3207. begin
  3208. { only goto next para if we're out of the varargs }
  3209. if not(po_varargs in procdefinition.procoptions) or
  3210. (lastpara<=procdefinition.maxparacount) then
  3211. begin
  3212. repeat
  3213. dec(paraidx);
  3214. until (paraidx<0) or not(vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions);
  3215. end;
  3216. pt:=tcallparanode(pt.right);
  3217. dec(lastpara);
  3218. end;
  3219. if assigned(pt) or
  3220. ((paraidx>=0) and
  3221. not assigned(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym)) then
  3222. begin
  3223. if assigned(pt) then
  3224. current_filepos:=pt.fileinfo;
  3225. CGMessage1(parser_e_wrong_parameter_size,'<Procedure Variable>');
  3226. exit;
  3227. end;
  3228. end
  3229. else
  3230. { not a procedure variable }
  3231. begin
  3232. { do we know the procedure to call ? }
  3233. if not(assigned(procdefinition)) then
  3234. begin
  3235. { ignore possible private for properties or in delphi mode for anon. inherited (FK) }
  3236. ignorevisibility:=(nf_isproperty in flags) or
  3237. ((m_delphi in current_settings.modeswitches) and (cnf_anon_inherited in callnodeflags));
  3238. candidates:=tcallcandidates.create(symtableprocentry,symtableproc,left,ignorevisibility,
  3239. not(nf_isproperty in flags),cnf_objc_id_call in callnodeflags,cnf_unit_specified in callnodeflags,
  3240. callnodeflags*[cnf_anon_inherited,cnf_inherited]=[],cnf_anon_inherited in callnodeflags,spezcontext);
  3241. { no procedures found? then there is something wrong
  3242. with the parameter size or the procedures are
  3243. not accessible }
  3244. if candidates.count=0 then
  3245. begin
  3246. { when it's an auto inherited call and there
  3247. is no procedure found, but the procedures
  3248. were defined with overload directive and at
  3249. least two procedures are defined then we ignore
  3250. this inherited by inserting a nothingn. Only
  3251. do this ugly hack in Delphi mode as it looks more
  3252. like a bug. It's also not documented }
  3253. if (m_delphi in current_settings.modeswitches) and
  3254. (cnf_anon_inherited in callnodeflags) and
  3255. (symtableprocentry.owner.symtabletype=ObjectSymtable) and
  3256. (po_overload in tprocdef(symtableprocentry.ProcdefList[0]).procoptions) and
  3257. (symtableprocentry.ProcdefList.Count>=2) then
  3258. result:=cnothingnode.create
  3259. else
  3260. begin
  3261. { in tp mode we can try to convert to procvar if
  3262. there are no parameters specified }
  3263. if not(assigned(left)) and
  3264. not(cnf_inherited in callnodeflags) and
  3265. ((m_tp_procvar in current_settings.modeswitches) or
  3266. (m_mac_procvar in current_settings.modeswitches)) and
  3267. (not assigned(methodpointer) or
  3268. (methodpointer.nodetype <> typen)) then
  3269. begin
  3270. hpt:=cloadnode.create(tprocsym(symtableprocentry),symtableproc);
  3271. if assigned(methodpointer) then
  3272. tloadnode(hpt).set_mp(methodpointer.getcopy);
  3273. typecheckpass(hpt);
  3274. result:=hpt;
  3275. end
  3276. else
  3277. begin
  3278. CGMessagePos1(fileinfo,parser_e_wrong_parameter_size,symtableprocentry.realname);
  3279. symtableprocentry.write_parameter_lists(nil);
  3280. end;
  3281. end;
  3282. candidates.free;
  3283. exit;
  3284. end;
  3285. { Retrieve information about the candidates }
  3286. candidates.get_information;
  3287. {$ifdef EXTDEBUG}
  3288. { Display info when multiple candidates are found }
  3289. if candidates.count>1 then
  3290. candidates.dump_info(V_Debug);
  3291. {$endif EXTDEBUG}
  3292. { Choose the best candidate and count the number of
  3293. candidates left }
  3294. cand_cnt:=candidates.choose_best(procdefinition,
  3295. assigned(left) and
  3296. not assigned(tcallparanode(left).right) and
  3297. (tcallparanode(left).left.resultdef.typ=variantdef));
  3298. { All parameters are checked, check if there are any
  3299. procedures left }
  3300. if cand_cnt>0 then
  3301. begin
  3302. { Multiple candidates left? }
  3303. if cand_cnt>1 then
  3304. begin
  3305. CGMessage(type_e_cant_choose_overload_function);
  3306. {$ifdef EXTDEBUG}
  3307. candidates.dump_info(V_Hint);
  3308. {$else EXTDEBUG}
  3309. candidates.list(false);
  3310. {$endif EXTDEBUG}
  3311. { we'll just use the first candidate to make the
  3312. call }
  3313. end;
  3314. { assign procdefinition }
  3315. if symtableproc=nil then
  3316. symtableproc:=procdefinition.owner;
  3317. end
  3318. else
  3319. begin
  3320. { No candidates left, this must be a type error,
  3321. because wrong size is already checked. procdefinition
  3322. is filled with the first (random) definition that is
  3323. found. We use this definition to display a nice error
  3324. message that the wrong type is passed }
  3325. candidates.find_wrong_para;
  3326. candidates.list(true);
  3327. {$ifdef EXTDEBUG}
  3328. candidates.dump_info(V_Hint);
  3329. {$endif EXTDEBUG}
  3330. { We can not proceed, release all procs and exit }
  3331. candidates.free;
  3332. exit;
  3333. end;
  3334. { if the final procedure definition is not yet owned,
  3335. ensure that it is }
  3336. procdefinition.register_def;
  3337. if procdefinition.is_specialization and (procdefinition.typ=procdef) then
  3338. maybe_add_pending_specialization(procdefinition);
  3339. candidates.free;
  3340. end; { end of procedure to call determination }
  3341. end;
  3342. { check for hints (deprecated etc) }
  3343. if procdefinition.typ = procdef then
  3344. check_hints(tprocdef(procdefinition).procsym,tprocdef(procdefinition).symoptions,tprocdef(procdefinition).deprecatedmsg);
  3345. { add reference to corresponding procsym; may not be the one
  3346. originally found/passed to the constructor because of overloads }
  3347. if procdefinition.typ = procdef then
  3348. addsymref(tprocdef(procdefinition).procsym);
  3349. { add needed default parameters }
  3350. if (paralength<procdefinition.maxparacount) then
  3351. begin
  3352. paraidx:=0;
  3353. i:=0;
  3354. while (i<paralength) do
  3355. begin
  3356. if paraidx>=procdefinition.Paras.count then
  3357. internalerror(200306181);
  3358. if not(vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) then
  3359. inc(i);
  3360. inc(paraidx);
  3361. end;
  3362. while (paraidx<procdefinition.paras.count) and (vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) do
  3363. inc(paraidx);
  3364. while (paraidx<procdefinition.paras.count) do
  3365. begin
  3366. if not assigned(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym) then
  3367. internalerror(200212142);
  3368. left:=ccallparanode.create(genconstsymtree(
  3369. tconstsym(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym)),left);
  3370. { Ignore vs_hidden parameters }
  3371. repeat
  3372. inc(paraidx);
  3373. until (paraidx>=procdefinition.paras.count) or
  3374. not(vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions);
  3375. end;
  3376. end;
  3377. { recursive call? }
  3378. if assigned(current_procinfo) and
  3379. (procdefinition=current_procinfo.procdef) then
  3380. include(current_procinfo.flags,pi_is_recursive);
  3381. { handle predefined procedures }
  3382. is_const:=(po_internconst in procdefinition.procoptions) and
  3383. ((block_type in [bt_const,bt_type,bt_const_type,bt_var_type]) or
  3384. (assigned(left) and ((tcallparanode(left).left.nodetype in [realconstn,ordconstn])
  3385. and (not assigned(tcallparanode(left).right) or (tcallparanode(tcallparanode(left).right).left.nodetype in [realconstn,ordconstn])))));
  3386. if (procdefinition.proccalloption=pocall_internproc) or is_const then
  3387. begin
  3388. if assigned(left) then
  3389. begin
  3390. { convert types to those of the prototype, this is required by functions like ror, rol, sar
  3391. some use however a dummy type (Typedfile) so this would break them }
  3392. if not(tprocdef(procdefinition).extnumber in [fpc_in_Reset_TypedFile,fpc_in_Rewrite_TypedFile]) then
  3393. begin
  3394. { bind parasyms to the callparanodes and insert hidden parameters }
  3395. bind_parasym;
  3396. { insert type conversions for parameters }
  3397. if assigned(left) then
  3398. tcallparanode(left).insert_typeconv;
  3399. end;
  3400. { ptr and settextbuf need two args }
  3401. if assigned(tcallparanode(left).right) then
  3402. begin
  3403. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,left);
  3404. left:=nil;
  3405. end
  3406. else
  3407. begin
  3408. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,tcallparanode(left).left);
  3409. tcallparanode(left).left:=nil;
  3410. end;
  3411. end
  3412. else
  3413. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,nil);
  3414. result:=hpt;
  3415. exit;
  3416. end;
  3417. { ensure that the result type is set }
  3418. if not(cnf_typedefset in callnodeflags) then
  3419. begin
  3420. { constructors return their current class type, not the type where the
  3421. constructor is declared, this can be different because of inheritance }
  3422. if (procdefinition.proctypeoption=potype_constructor) and
  3423. assigned(methodpointer) and
  3424. assigned(methodpointer.resultdef) and
  3425. (methodpointer.resultdef.typ=classrefdef) then
  3426. resultdef:=tclassrefdef(methodpointer.resultdef).pointeddef
  3427. else
  3428. { Member call to a (inherited) constructor from the class, the return
  3429. value is always self, so we change it to voidtype to generate an
  3430. error and to prevent users from generating non-working code
  3431. when they expect to clone the current instance, see bug 3662 (PFV) }
  3432. if (procdefinition.proctypeoption=potype_constructor) and
  3433. is_class(tprocdef(procdefinition).struct) and
  3434. assigned(methodpointer) and
  3435. (methodpointer.nodetype=loadn) and
  3436. (loadnf_is_self in tloadnode(methodpointer).loadnodeflags) then
  3437. resultdef:=voidtype
  3438. else
  3439. resultdef:=procdefinition.returndef;
  3440. end
  3441. else
  3442. resultdef:=typedef;
  3443. { Check object/class for methods }
  3444. if assigned(methodpointer) then
  3445. begin
  3446. { direct call to inherited abstract method, then we
  3447. can already give a error in the compiler instead
  3448. of a runtime error }
  3449. if (cnf_inherited in callnodeflags) and
  3450. (po_abstractmethod in procdefinition.procoptions) then
  3451. begin
  3452. if (m_delphi in current_settings.modeswitches) and
  3453. (cnf_anon_inherited in callnodeflags) then
  3454. begin
  3455. CGMessage(cg_h_inherited_ignored);
  3456. result:=cnothingnode.create;
  3457. exit;
  3458. end
  3459. else
  3460. CGMessage(cg_e_cant_call_abstract_method);
  3461. end;
  3462. { directly calling an interface/protocol/category/class helper
  3463. method via its type is not possible (always must be called via
  3464. the actual instance) }
  3465. if (methodpointer.nodetype=typen) and
  3466. (is_interface(methodpointer.resultdef) or
  3467. is_objc_protocol_or_category(methodpointer.resultdef)) then
  3468. CGMessage1(type_e_class_type_expected,methodpointer.resultdef.typename);
  3469. { if an inherited con- or destructor should be }
  3470. { called in a con- or destructor then a warning }
  3471. { will be made }
  3472. { con- and destructors need a pointer to the vmt }
  3473. if (cnf_inherited in callnodeflags) and
  3474. (procdefinition.proctypeoption in [potype_constructor,potype_destructor]) and
  3475. is_object(methodpointer.resultdef) and
  3476. not(current_procinfo.procdef.proctypeoption in [potype_constructor,potype_destructor]) then
  3477. CGMessage(cg_w_member_cd_call_from_method);
  3478. if methodpointer.nodetype<>typen then
  3479. begin
  3480. { Remove all postfix operators }
  3481. hpt:=methodpointer;
  3482. while assigned(hpt) and (hpt.nodetype in [subscriptn,vecn]) do
  3483. hpt:=tunarynode(hpt).left;
  3484. if ((hpt.nodetype=loadvmtaddrn) or
  3485. ((hpt.nodetype=loadn) and assigned(tloadnode(hpt).resultdef) and (tloadnode(hpt).resultdef.typ=classrefdef))) and
  3486. not (procdefinition.proctypeoption=potype_constructor) and
  3487. not (po_classmethod in procdefinition.procoptions) and
  3488. not (po_staticmethod in procdefinition.procoptions) then
  3489. { error: we are calling instance method from the class method/static method }
  3490. CGMessage(parser_e_only_class_members);
  3491. if (procdefinition.proctypeoption=potype_constructor) and
  3492. assigned(symtableproc) and
  3493. (symtableproc.symtabletype=withsymtable) and
  3494. (tnode(twithsymtable(symtableproc).withrefnode).nodetype=temprefn) then
  3495. CGmessage(cg_e_cannot_call_cons_dest_inside_with);
  3496. { skip (absolute and other simple) type conversions -- only now,
  3497. because the checks above have to take type conversions into
  3498. e.g. class reference types account }
  3499. hpt:=actualtargetnode(@hpt)^;
  3500. { R.Init then R will be initialized by the constructor,
  3501. Also allow it for simple loads }
  3502. if (procdefinition.proctypeoption=potype_constructor) or
  3503. ((hpt.nodetype=loadn) and
  3504. (((methodpointer.resultdef.typ=objectdef) and
  3505. not(oo_has_virtual in tobjectdef(methodpointer.resultdef).objectoptions)) or
  3506. (methodpointer.resultdef.typ=recorddef)
  3507. )
  3508. ) then
  3509. { a constructor will and a method may write something to }
  3510. { the fields }
  3511. set_varstate(methodpointer,vs_readwritten,[])
  3512. else
  3513. set_varstate(methodpointer,vs_read,[vsf_must_be_valid]);
  3514. end;
  3515. { if we are calling the constructor check for abstract
  3516. methods. Ignore inherited and member calls, because the
  3517. class is then already created }
  3518. if (procdefinition.proctypeoption=potype_constructor) and
  3519. not(cnf_inherited in callnodeflags) and
  3520. not(cnf_member_call in callnodeflags) then
  3521. verifyabstractcalls;
  3522. end
  3523. else
  3524. begin
  3525. { When this is method the methodpointer must be available }
  3526. if (right=nil) and
  3527. (procdefinition.owner.symtabletype in [ObjectSymtable,recordsymtable]) and
  3528. not procdefinition.no_self_node then
  3529. internalerror(200305061);
  3530. end;
  3531. { Set flag that the procedure uses varargs, also if they are not passed it is still
  3532. needed for x86_64 to pass the number of SSE registers used }
  3533. if po_varargs in procdefinition.procoptions then
  3534. include(callnodeflags,cnf_uses_varargs);
  3535. { set the appropriate node flag if the call never returns }
  3536. if po_noreturn in procdefinition.procoptions then
  3537. include(callnodeflags,cnf_call_never_returns);
  3538. { Change loading of array of const to varargs }
  3539. if assigned(left) and
  3540. is_array_of_const(tparavarsym(procdefinition.paras[procdefinition.paras.count-1]).vardef) and
  3541. (procdefinition.proccalloption in cdecl_pocalls) then
  3542. convert_carg_array_of_const;
  3543. { bind parasyms to the callparanodes and insert hidden parameters }
  3544. bind_parasym;
  3545. { insert type conversions for parameters }
  3546. if assigned(left) then
  3547. tcallparanode(left).insert_typeconv;
  3548. { dispinterface methode invoke? }
  3549. if assigned(methodpointer) and is_dispinterface(methodpointer.resultdef) then
  3550. begin
  3551. case procdefinition.proctypeoption of
  3552. potype_propgetter: calltype:=dct_propget;
  3553. potype_propsetter: calltype:=dct_propput;
  3554. else
  3555. calltype:=dct_method;
  3556. end;
  3557. { if the result is used, we've to insert a call to convert the type to be on the "safe side" }
  3558. if (cnf_return_value_used in callnodeflags) and not is_void(procdefinition.returndef) then
  3559. begin
  3560. result:=internalstatements(statements);
  3561. converted_result_data:=ctempcreatenode.create(procdefinition.returndef,sizeof(procdefinition.returndef),
  3562. tt_persistent,true);
  3563. addstatement(statements,converted_result_data);
  3564. addstatement(statements,cassignmentnode.create(ctemprefnode.create(converted_result_data),
  3565. ctypeconvnode.create_internal(
  3566. translate_disp_call(methodpointer,parameters,calltype,'',tprocdef(procdefinition).dispid,procdefinition.returndef),
  3567. procdefinition.returndef)));
  3568. addstatement(statements,ctempdeletenode.create_normal_temp(converted_result_data));
  3569. addstatement(statements,ctemprefnode.create(converted_result_data));
  3570. end
  3571. else
  3572. result:=translate_disp_call(methodpointer,parameters,calltype,'',tprocdef(procdefinition).dispid,voidtype);
  3573. { don't free reused nodes }
  3574. methodpointer:=nil;
  3575. parameters:=nil;
  3576. end;
  3577. maybe_gen_call_self_node;
  3578. if assigned(call_self_node) then
  3579. typecheckpass(call_self_node);
  3580. if assigned(call_vmt_node) then
  3581. typecheckpass(call_vmt_node);
  3582. finally
  3583. aktcallnode:=oldcallnode;
  3584. end;
  3585. end;
  3586. procedure tcallnode.order_parameters;
  3587. var
  3588. hp,hpcurr,hpnext,hpfirst,hpprev : tcallparanode;
  3589. currloc : tcgloc;
  3590. begin
  3591. hpfirst:=nil;
  3592. hpcurr:=tcallparanode(left);
  3593. { cache all info about parameters containing stack tainting calls,
  3594. since we will need it a lot below and calculting it can be expensive }
  3595. while assigned(hpcurr) do
  3596. begin
  3597. hpcurr.init_contains_stack_tainting_call_cache;
  3598. hpcurr:=tcallparanode(hpcurr.right);
  3599. end;
  3600. hpcurr:=tcallparanode(left);
  3601. while assigned(hpcurr) do
  3602. begin
  3603. { pull out }
  3604. hpnext:=tcallparanode(hpcurr.right);
  3605. { pull in at the correct place.
  3606. Used order:
  3607. 1. vs_out for a reference-counted type
  3608. 2. LOC_REFERENCE with smallest offset (i386 only)
  3609. 3. LOC_REFERENCE with least complexity (non-i386 only)
  3610. 4. LOC_REFERENCE with most complexity (non-i386 only)
  3611. 5. LOC_REGISTER with most complexity
  3612. 6. LOC_REGISTER with least complexity
  3613. For the moment we only look at the first parameter field. Combining it
  3614. with multiple parameter fields will make things a lot complexer (PFV)
  3615. The reason for the difference regarding complexity ordering
  3616. between LOC_REFERENCE and LOC_REGISTER is mainly for calls:
  3617. we first want to treat the LOC_REFERENCE destinations whose
  3618. calculation does not require a call, because their location
  3619. may contain registers which might otherwise have to be saved
  3620. if a call has to be evaluated first. The calculated value is
  3621. stored on the stack and will thus no longer occupy any
  3622. register.
  3623. Similarly, for the register parameters we first want to
  3624. evaluate the calls, because otherwise the already loaded
  3625. register parameters will have to be saved so the intermediate
  3626. call can be evaluated (JM) }
  3627. if not assigned(hpcurr.parasym.paraloc[callerside].location) then
  3628. internalerror(200412152);
  3629. currloc:=hpcurr.parasym.paraloc[callerside].location^.loc;
  3630. hpprev:=nil;
  3631. hp:=hpfirst;
  3632. { on fixed_stack targets, always evaluate parameters containing
  3633. a call with stack parameters before all other parameters,
  3634. because they will prevent any other parameters from being put
  3635. in their final place; if both the current and the next para
  3636. contain a stack tainting call, don't do anything to prevent
  3637. them from keeping on chasing eachother's tail }
  3638. while assigned(hp) do
  3639. begin
  3640. if paramanager.use_fixed_stack and
  3641. hpcurr.contains_stack_tainting_call_cached then
  3642. break;
  3643. case currloc of
  3644. LOC_REFERENCE :
  3645. begin
  3646. case hp.parasym.paraloc[callerside].location^.loc of
  3647. LOC_REFERENCE :
  3648. begin
  3649. { Offset is calculated like:
  3650. sub esp,12
  3651. mov [esp+8],para3
  3652. mov [esp+4],para2
  3653. mov [esp],para1
  3654. call function
  3655. That means the for pushes the para with the
  3656. highest offset (see para3) needs to be pushed first
  3657. }
  3658. {$if defined(i386) or defined(i8086) or defined(m68k)}
  3659. { the i386, i8086, m68k and jvm code generators expect all reference }
  3660. { parameters to be in this order so they can use }
  3661. { pushes in case of no fixed stack }
  3662. if (not paramanager.use_fixed_stack and
  3663. (hpcurr.parasym.paraloc[callerside].location^.reference.offset>
  3664. hp.parasym.paraloc[callerside].location^.reference.offset)) or
  3665. (paramanager.use_fixed_stack and
  3666. (node_complexity(hpcurr)<node_complexity(hp))) then
  3667. {$elseif defined(jvm)}
  3668. if (hpcurr.parasym.paraloc[callerside].location^.reference.offset<hp.parasym.paraloc[callerside].location^.reference.offset) then
  3669. {$else jvm}
  3670. if (node_complexity(hpcurr)<node_complexity(hp)) then
  3671. {$endif jvm}
  3672. break;
  3673. end;
  3674. LOC_MMREGISTER,
  3675. LOC_REGISTER,
  3676. LOC_FPUREGISTER :
  3677. break;
  3678. end;
  3679. end;
  3680. LOC_MMREGISTER,
  3681. LOC_FPUREGISTER,
  3682. LOC_REGISTER :
  3683. begin
  3684. if (hp.parasym.paraloc[callerside].location^.loc<>LOC_REFERENCE) and
  3685. (node_complexity(hpcurr)>node_complexity(hp)) then
  3686. break;
  3687. end;
  3688. end;
  3689. hpprev:=hp;
  3690. hp:=tcallparanode(hp.right);
  3691. end;
  3692. hpcurr.right:=hp;
  3693. if assigned(hpprev) then
  3694. hpprev.right:=hpcurr
  3695. else
  3696. hpfirst:=hpcurr;
  3697. { next }
  3698. hpcurr:=hpnext;
  3699. end;
  3700. left:=hpfirst;
  3701. { now mark each parameter that is followed by a stack-tainting call,
  3702. to determine on use_fixed_stack targets which ones can immediately be
  3703. put in their final destination. Unforunately we can never put register
  3704. parameters immediately in their final destination (even on register-
  3705. rich architectures such as the PowerPC), because the code generator
  3706. can still insert extra calls that only make use of register
  3707. parameters (fpc_move() etc. }
  3708. hpcurr:=hpfirst;
  3709. while assigned(hpcurr) do
  3710. begin
  3711. if hpcurr.contains_stack_tainting_call_cached then
  3712. begin
  3713. { all parameters before this one are followed by a stack
  3714. tainting call }
  3715. hp:=hpfirst;
  3716. while hp<>hpcurr do
  3717. begin
  3718. hp.ffollowed_by_stack_tainting_call_cached:=true;
  3719. hp:=tcallparanode(hp.right);
  3720. end;
  3721. hpfirst:=hpcurr;
  3722. end;
  3723. hpcurr:=tcallparanode(hpcurr.right);
  3724. end;
  3725. end;
  3726. procedure tcallnode.check_stack_parameters;
  3727. var
  3728. hp : tcallparanode;
  3729. begin
  3730. hp:=tcallparanode(left);
  3731. while assigned(hp) do
  3732. begin
  3733. if assigned(hp.parasym) and
  3734. assigned(hp.parasym.paraloc[callerside].location) and
  3735. (hp.parasym.paraloc[callerside].location^.loc=LOC_REFERENCE) then
  3736. include(current_procinfo.flags,pi_has_stackparameter);
  3737. hp:=tcallparanode(hp.right);
  3738. end;
  3739. end;
  3740. procedure tcallnode.check_inlining;
  3741. var
  3742. st : tsymtable;
  3743. para : tcallparanode;
  3744. begin
  3745. { Can we inline the procedure? }
  3746. if (po_inline in procdefinition.procoptions) and
  3747. (procdefinition.typ=procdef) and
  3748. tprocdef(procdefinition).has_inlininginfo and
  3749. { Prevent too deep inlining recursion and code bloat by inlining
  3750. The actual formuala is
  3751. inlinelevel+1 /-------
  3752. node count < -------------\/ 10000
  3753. This allows exponential grow of the code only to a certain limit.
  3754. Remarks
  3755. - The current approach calculates the inlining level top down, so outer call nodes (nodes closer to the leaf) might not be inlined
  3756. if the max. complexity is reached. This is done because it makes the implementation easier and because
  3757. there might be situations were it is more beneficial to inline inner nodes and do the calls to the outer nodes
  3758. if the outer nodes are in a seldomly used code path
  3759. - The code avoids to use functions from the math unit
  3760. }
  3761. (node_count(tprocdef(procdefinition).inlininginfo^.code)<round(exp((1.0/(inlinelevel+1))*ln(10000)))) then
  3762. begin
  3763. include(callnodeflags,cnf_do_inline);
  3764. { Check if we can inline the procedure when it references proc/var that
  3765. are not in the globally available }
  3766. st:=procdefinition.owner;
  3767. while (st.symtabletype in [ObjectSymtable,recordsymtable]) do
  3768. st:=st.defowner.owner;
  3769. if (pi_uses_static_symtable in tprocdef(procdefinition).inlininginfo^.flags) and
  3770. (st.symtabletype=globalsymtable) and
  3771. (not st.iscurrentunit) then
  3772. begin
  3773. Comment(V_lineinfo+V_Debug,'Not inlining "'+tprocdef(procdefinition).procsym.realname+'", references static symtable');
  3774. exclude(callnodeflags,cnf_do_inline);
  3775. end;
  3776. para:=tcallparanode(parameters);
  3777. while assigned(para) do
  3778. begin
  3779. if not para.can_be_inlined then
  3780. begin
  3781. Comment(V_lineinfo+V_Debug,'Not inlining "'+tprocdef(procdefinition).procsym.realname+
  3782. '", invocation parameter contains an unsafe/unsupported construct');
  3783. exclude(callnodeflags,cnf_do_inline);
  3784. break;
  3785. end;
  3786. para:=tcallparanode(para.nextpara);
  3787. end;
  3788. end;
  3789. end;
  3790. function tcallnode.pass_1 : tnode;
  3791. procedure mark_unregable_parameters;
  3792. var
  3793. hp : tcallparanode;
  3794. begin
  3795. hp:=tcallparanode(left);
  3796. while assigned(hp) do
  3797. begin
  3798. do_typecheckpass(hp.left);
  3799. { When the address needs to be pushed then the register is
  3800. not regable. Exception is when the location is also a var
  3801. parameter and we can pass the address transparently (but
  3802. that is handled by make_not_regable if ra_addr_regable is
  3803. passed, and make_not_regable always needs to called for
  3804. the ra_addr_taken info for non-invisble parameters) }
  3805. if (not (cpf_varargs_para in hp.callparaflags)) and (
  3806. not(
  3807. (vo_is_hidden_para in hp.parasym.varoptions) and
  3808. (hp.left.resultdef.typ in [pointerdef,classrefdef])
  3809. ) and
  3810. paramanager.push_addr_param(hp.parasym.varspez,hp.parasym.vardef,
  3811. self.procdefinition.proccalloption)
  3812. ) then
  3813. { pushing the address of a variable to take the place of a temp
  3814. as the complex function result of a function does not make its
  3815. address escape the current block, as the "address of the
  3816. function result" is not something which can be stored
  3817. persistently by the callee (it becomes invalid when the callee
  3818. returns) }
  3819. if not(vo_is_funcret in hp.parasym.varoptions) and
  3820. not(po_compilerproc in procdefinition.procoptions) then
  3821. make_not_regable(hp.left,[ra_addr_regable,ra_addr_taken])
  3822. else
  3823. make_not_regable(hp.left,[ra_addr_regable]);
  3824. hp:=tcallparanode(hp.right);
  3825. end;
  3826. end;
  3827. var
  3828. para: tcallparanode;
  3829. oldcallnode: tcallnode;
  3830. begin
  3831. result:=nil;
  3832. oldcallnode:=aktcallnode;
  3833. aktcallnode:=self;
  3834. try
  3835. { as pass_1 is never called on the methodpointer node, we must check
  3836. here that it's not a helper type }
  3837. if assigned(methodpointer) and
  3838. (methodpointer.nodetype=typen) and
  3839. is_objectpascal_helper(ttypenode(methodpointer).typedef) and
  3840. not ttypenode(methodpointer).helperallowed then
  3841. begin
  3842. CGMessage(parser_e_no_category_as_types);
  3843. { we get an internal error when trying to insert the hidden
  3844. parameters in this case }
  3845. exit;
  3846. end;
  3847. { can we get rid of the call? }
  3848. if (cs_opt_remove_emtpy_proc in current_settings.optimizerswitches) and
  3849. not(cnf_return_value_used in callnodeflags) and
  3850. (procdefinition.typ=procdef) and
  3851. tprocdef(procdefinition).isempty and
  3852. { allow only certain proc options }
  3853. ((tprocdef(procdefinition).procoptions-[po_none,po_classmethod,po_staticmethod,
  3854. po_interrupt,po_iocheck,po_assembler,po_msgstr,po_msgint,po_exports,po_external,po_overload,
  3855. po_nostackframe,po_has_mangledname,po_has_public_name,po_forward,po_global,
  3856. po_inline,po_compilerproc,po_has_importdll,po_has_importname,po_kylixlocal,po_dispid,po_delphi_nested_cc,
  3857. po_rtlproc,po_ignore_for_overload_resolution,po_auto_raised_visibility])=[]) then
  3858. begin
  3859. { check parameters for side effects }
  3860. para:=tcallparanode(left);
  3861. while assigned(para) do
  3862. begin
  3863. if (para.parasym.typ = paravarsym) and
  3864. ((para.parasym.refs>0) or
  3865. { array of consts are converted later on so we need to skip them here
  3866. else no error detection is done }
  3867. is_array_of_const(para.parasym.vardef) or
  3868. not(cs_opt_dead_values in current_settings.optimizerswitches) or
  3869. might_have_sideeffects(para.left)) then
  3870. break;
  3871. para:=tcallparanode(para.right);
  3872. end;
  3873. { finally, remove it if no parameter with side effect has been found }
  3874. if para=nil then
  3875. begin
  3876. result:=cnothingnode.create;
  3877. exit;
  3878. end;
  3879. end;
  3880. { convert Objective-C calls into a message call }
  3881. if (procdefinition.typ=procdef) and
  3882. (po_objc in tprocdef(procdefinition).procoptions) then
  3883. begin
  3884. if not(cnf_objc_processed in callnodeflags) then
  3885. objc_convert_to_message_send;
  3886. end
  3887. else
  3888. begin
  3889. { The following don't apply to obj-c: obj-c methods can never be
  3890. inlined because they're always virtual and the destination can
  3891. change at run, and for the same reason we also can't perform
  3892. WPO on them (+ they have no constructors) }
  3893. { Check if the call can be inlined, sets the cnf_do_inline flag }
  3894. check_inlining;
  3895. { must be called before maybe_load_in_temp(methodpointer), because
  3896. it converts the methodpointer into a temp in case it's a call
  3897. (and we want to know the original call)
  3898. }
  3899. register_created_object_types;
  3900. end;
  3901. { Maybe optimize the loading of the methodpointer using a temp. When the methodpointer
  3902. is a calln this is even required to not execute the calln twice.
  3903. This needs to be done after the resulttype pass, because in the resulttype we can still convert the
  3904. calln to a loadn (PFV) }
  3905. if assigned(methodpointer) then
  3906. maybe_load_in_temp(methodpointer);
  3907. if assigned(right) and (right.resultdef.typ=procvardef) and
  3908. not tabstractprocdef(right.resultdef).is_addressonly then
  3909. maybe_load_in_temp(right);
  3910. { Create destination (temp or assignment-variable reuse) for function result if it not yet set }
  3911. maybe_create_funcret_node;
  3912. { Insert the self,vmt,function result in the parameters }
  3913. gen_hidden_parameters;
  3914. { Remove useless nodes from init/final blocks }
  3915. { (simplify depends on typecheck info) }
  3916. if assigned(callinitblock) then
  3917. begin
  3918. typecheckpass(tnode(callinitblock));
  3919. doinlinesimplify(tnode(callinitblock));
  3920. end;
  3921. if assigned(callcleanupblock) then
  3922. begin
  3923. typecheckpass(tnode(callcleanupblock));
  3924. doinlinesimplify(tnode(callcleanupblock));
  3925. end;
  3926. { If a constructor calls another constructor of the same or of an
  3927. inherited class, some targets (jvm) have to generate different
  3928. entry code for the constructor. }
  3929. if (current_procinfo.procdef.proctypeoption=potype_constructor) and
  3930. (procdefinition.typ=procdef) and
  3931. (tprocdef(procdefinition).proctypeoption=potype_constructor) and
  3932. ([cnf_member_call,cnf_inherited] * callnodeflags <> []) then
  3933. current_procinfo.ConstructorCallingConstructor:=true;
  3934. { object check helper will load VMT -> needs GOT }
  3935. if (cs_check_object in current_settings.localswitches) and
  3936. (cs_create_pic in current_settings.moduleswitches) then
  3937. include(current_procinfo.flags,pi_needs_got);
  3938. { Continue with checking a normal call or generate the inlined code }
  3939. if cnf_do_inline in callnodeflags then
  3940. result:=pass1_inline
  3941. else
  3942. begin
  3943. mark_unregable_parameters;
  3944. result:=pass1_normal;
  3945. end;
  3946. finally
  3947. aktcallnode:=oldcallnode;
  3948. end;
  3949. end;
  3950. function tcallnode.pass1_normal : tnode;
  3951. begin
  3952. result:=nil;
  3953. { calculate the parameter info for the procdef }
  3954. procdefinition.init_paraloc_info(callerside);
  3955. { calculate the parameter size needed for this call include varargs if they are available }
  3956. if assigned(varargsparas) then
  3957. pushedparasize:=paramanager.create_varargs_paraloc_info(procdefinition,varargsparas)
  3958. else
  3959. pushedparasize:=procdefinition.callerargareasize;
  3960. { record maximum parameter size used in this proc }
  3961. current_procinfo.allocate_push_parasize(pushedparasize);
  3962. { check for stacked parameters }
  3963. if assigned(left) and
  3964. (current_settings.optimizerswitches*[cs_opt_stackframe,cs_opt_level1]<>[]) then
  3965. check_stack_parameters;
  3966. if assigned(callinitblock) then
  3967. firstpass(tnode(callinitblock));
  3968. { function result node (tempref or simple load) }
  3969. if assigned(funcretnode) then
  3970. firstpass(funcretnode);
  3971. { parameters }
  3972. if assigned(left) then
  3973. tcallparanode(left).firstcallparan;
  3974. { procedure variable ? }
  3975. if assigned(right) then
  3976. firstpass(right);
  3977. if assigned(methodpointer) and
  3978. (methodpointer.nodetype<>typen) then
  3979. firstpass(methodpointer);
  3980. if assigned(callcleanupblock) then
  3981. firstpass(tnode(callcleanupblock));
  3982. if not (block_type in [bt_const,bt_type,bt_const_type,bt_var_type]) then
  3983. include(current_procinfo.flags,pi_do_call);
  3984. { order parameters }
  3985. order_parameters;
  3986. { get a register for the return value }
  3987. if (not is_void(resultdef)) then
  3988. begin
  3989. if paramanager.ret_in_param(resultdef,procdefinition) then
  3990. begin
  3991. expectloc:=LOC_REFERENCE;
  3992. end
  3993. else
  3994. { ansi/widestrings must be registered, so we can dispose them }
  3995. if is_ansistring(resultdef) or
  3996. is_widestring(resultdef) or
  3997. is_unicodestring(resultdef) then
  3998. begin
  3999. expectloc:=LOC_REFERENCE;
  4000. end
  4001. else
  4002. { we have only to handle the result if it is used }
  4003. if (cnf_return_value_used in callnodeflags) then
  4004. expectloc:=get_expect_loc
  4005. else
  4006. expectloc:=LOC_VOID;
  4007. end
  4008. else
  4009. expectloc:=LOC_VOID;
  4010. { create tree for VMT entry if required }
  4011. gen_vmt_entry_load;
  4012. end;
  4013. {$ifdef state_tracking}
  4014. function Tcallnode.track_state_pass(exec_known:boolean):boolean;
  4015. var hp:Tcallparanode;
  4016. value:Tnode;
  4017. begin
  4018. track_state_pass:=false;
  4019. hp:=Tcallparanode(left);
  4020. while assigned(hp) do
  4021. begin
  4022. if left.track_state_pass(exec_known) then
  4023. begin
  4024. left.resultdef:=nil;
  4025. do_typecheckpass(left);
  4026. end;
  4027. value:=aktstate.find_fact(hp.left);
  4028. if value<>nil then
  4029. begin
  4030. track_state_pass:=true;
  4031. hp.left.destroy;
  4032. hp.left:=value.getcopy;
  4033. do_typecheckpass(hp.left);
  4034. end;
  4035. hp:=Tcallparanode(hp.right);
  4036. end;
  4037. end;
  4038. {$endif}
  4039. {**************************************************************************
  4040. INLINING SUPPORT
  4041. **************************************************************************}
  4042. function tcallnode.replaceparaload(var n: tnode; arg: pointer): foreachnoderesult;
  4043. var
  4044. paras: tcallparanode;
  4045. temp: tnode;
  4046. indexnr : integer;
  4047. begin
  4048. result := fen_false;
  4049. n.fileinfo := pfileposinfo(arg)^;
  4050. if (n.nodetype = loadn) then
  4051. begin
  4052. case tloadnode(n).symtableentry.typ of
  4053. paravarsym :
  4054. begin
  4055. paras := tcallparanode(left);
  4056. while assigned(paras) and
  4057. (paras.parasym <> tloadnode(n).symtableentry) do
  4058. paras := tcallparanode(paras.right);
  4059. if assigned(paras) then
  4060. begin
  4061. temp:=paras.left.getcopy;
  4062. { inherit modification information, this is needed by the dfa/cse }
  4063. temp.flags:=temp.flags+(n.flags*[nf_modify,nf_write,nf_address_taken]);
  4064. n.free;
  4065. n:=temp;
  4066. typecheckpass(n);
  4067. result := fen_true;
  4068. end;
  4069. end;
  4070. localvarsym :
  4071. begin
  4072. { local? }
  4073. if (tloadnode(n).symtableentry.owner <> tprocdef(procdefinition).localst) then
  4074. exit;
  4075. indexnr:=tloadnode(n).symtableentry.owner.SymList.IndexOf(tloadnode(n).symtableentry);
  4076. if (indexnr >= inlinelocals.count) or
  4077. not assigned(inlinelocals[indexnr]) then
  4078. internalerror(20040720);
  4079. temp := tnode(inlinelocals[indexnr]).getcopy;
  4080. { inherit modification information, this is needed by the dfa/cse }
  4081. temp.flags:=temp.flags+(n.flags*[nf_modify,nf_write,nf_address_taken]);
  4082. n.free;
  4083. n:=temp;
  4084. typecheckpass(n);
  4085. result := fen_true;
  4086. end;
  4087. end;
  4088. end;
  4089. end;
  4090. procedure tcallnode.createlocaltemps(p:TObject;arg:pointer);
  4091. var
  4092. tempnode: ttempcreatenode;
  4093. indexnr : integer;
  4094. begin
  4095. if (TSym(p).typ <> localvarsym) then
  4096. exit;
  4097. indexnr:=TSym(p).Owner.SymList.IndexOf(p);
  4098. if (indexnr >= inlinelocals.count) then
  4099. inlinelocals.count:=indexnr+10;
  4100. if (vo_is_funcret in tabstractvarsym(p).varoptions) then
  4101. begin
  4102. if not assigned(funcretnode) then
  4103. internalerror(200709081);
  4104. inlinelocals[indexnr] := funcretnode.getcopy
  4105. end
  4106. else
  4107. begin
  4108. tempnode :=ctempcreatenode.create(tabstractvarsym(p).vardef,
  4109. tabstractvarsym(p).vardef.size,tt_persistent,tabstractvarsym(p).is_regvar(false));
  4110. addstatement(inlineinitstatement,tempnode);
  4111. if localvartrashing <> -1 then
  4112. cnodeutils.maybe_trash_variable(inlineinitstatement,tabstractnormalvarsym(p),ctemprefnode.create(tempnode));
  4113. addstatement(inlinecleanupstatement,ctempdeletenode.create(tempnode));
  4114. { inherit addr_taken flag }
  4115. if (tabstractvarsym(p).addr_taken) then
  4116. tempnode.includetempflag(ti_addr_taken);
  4117. inlinelocals[indexnr] := ctemprefnode.create(tempnode);
  4118. end;
  4119. end;
  4120. function nonlocalvars(var n: tnode; arg: pointer): foreachnoderesult;
  4121. begin
  4122. result := fen_false;
  4123. { this is just to play it safe, there are more safe situations }
  4124. if (n.nodetype = derefn) or
  4125. ((n.nodetype = loadn) and
  4126. { globals and fields of (possibly global) objects could always be changed in the callee }
  4127. ((tloadnode(n).symtable.symtabletype in [globalsymtable,ObjectSymtable]) or
  4128. { statics can only be modified by functions in the same unit }
  4129. ((tloadnode(n).symtable.symtabletype = staticsymtable) and
  4130. (tloadnode(n).symtable = TSymtable(arg))) or
  4131. { if the addr of the symbol is taken somewhere, it can be also non-local }
  4132. (tabstractvarsym(tloadnode(n).symtableentry).addr_taken)
  4133. )
  4134. ) or
  4135. ((n.nodetype = subscriptn) and
  4136. (tsubscriptnode(n).vs.owner.symtabletype = ObjectSymtable)) then
  4137. result := fen_norecurse_true;
  4138. end;
  4139. function tcallnode.maybecreateinlineparatemp(para: tcallparanode; out complexpara: boolean): boolean;
  4140. var
  4141. tempnode: ttempcreatenode;
  4142. realtarget: tnode;
  4143. paracomplexity: longint;
  4144. pushconstaddr: boolean;
  4145. function needtemp: boolean;
  4146. begin
  4147. { We need a temp if the passed value will not be in memory, while
  4148. the parameter inside the routine must be in memory }
  4149. if (tparavarsym(para.parasym).varregable in [vr_none,vr_addr]) and
  4150. not(para.left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  4151. exit(true);
  4152. { We cannot create a formaldef temp and assign something to it }
  4153. if para.parasym.vardef.typ=formaldef then
  4154. exit(false);
  4155. { We try to handle complex expressions later by taking their address
  4156. and storing this address in a temp (which is then dereferenced when
  4157. the value is used; that doesn't work if we cannot take the address
  4158. of the expression though, in which case we store the result of the
  4159. expression in a temp }
  4160. if (complexpara and not(para.left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE]) or
  4161. (complexpara and
  4162. (not valid_for_addr(para.left,false) or
  4163. (para.left.nodetype=calln) or
  4164. is_constnode(para.left)))) then
  4165. exit(true);
  4166. { Normally, we do not need to create a temp for value parameters that
  4167. are not modified in the inlined function, and neither for const
  4168. parameters that are passed by value.
  4169. However, if we pass a global variable, an object field, a variable
  4170. whose address has been taken, or an expression containing a pointer
  4171. dereference as parameter, this value could be modified in other ways
  4172. as well (even inside the callee) and in such cases we still create a
  4173. temp to be on the safe side.
  4174. We *must not* create a temp for global variables passed by
  4175. reference to a const parameter, because if not inlined then any
  4176. changes to the original value will also be visible in the callee
  4177. (although this is technically undefined behaviour, since with
  4178. "const" the programmer tells the compiler this argument will not
  4179. change). }
  4180. if (((para.parasym.varspez=vs_value) and
  4181. (para.parasym.varstate in [vs_initialised,vs_declared,vs_read])) or
  4182. ((para.parasym.varspez=vs_const) and
  4183. not pushconstaddr)) and
  4184. foreachnodestatic(para.left,@nonlocalvars,pointer(symtableproc)) then
  4185. exit(true);
  4186. { Value parameters of which we know they are modified by definition
  4187. have to be copied to a temp; the same goes for cases of "x:=f(x)"
  4188. where x is passed as value parameter to f(), at least if we
  4189. optimized invocation by setting the funcretnode to x to avoid an
  4190. assignment afterwards (since x may be read inside the function after
  4191. it modified result==x) }
  4192. if (para.parasym.varspez=vs_value) and
  4193. (not(para.parasym.varstate in [vs_initialised,vs_declared,vs_read]) or
  4194. (assigned(aktassignmentnode) and
  4195. (aktassignmentnode.right=self) and
  4196. (nf_assign_done_in_right in aktassignmentnode.flags) and
  4197. actualtargetnode(@aktassignmentnode.left)^.isequal(actualtargetnode(@para.left)^))) then
  4198. exit(true);
  4199. { the compiler expects that it can take the address of parameters passed by reference in
  4200. the case of const so we can't replace the node simply by a constant node
  4201. When playing with this code, ensure that
  4202. function f(const a,b : longint) : longint;inline;
  4203. begin
  4204. result:=a*b;
  4205. end;
  4206. [...]
  4207. ...:=f(10,20));
  4208. [...]
  4209. is still folded. (FK)
  4210. }
  4211. if (para.parasym.varspez=vs_const) and
  4212. { const para's can get vs_readwritten if their address is taken ->
  4213. in case they are not passed by reference, to keep the same
  4214. behaviour as without inlining we have to make a copy in case the
  4215. originally passed parameter value gets changed inside the callee
  4216. }
  4217. (not pushconstaddr and
  4218. (para.parasym.varstate=vs_readwritten)
  4219. ) or
  4220. { call-by-reference const's may need to be passed by reference to
  4221. function called in the inlined code }
  4222. (pushconstaddr and
  4223. not valid_for_addr(para.left,false)) then
  4224. exit(true);
  4225. result:=false;
  4226. end;
  4227. begin
  4228. result:=false;
  4229. { determine how a parameter is passed to the inlined body
  4230. There are three options:
  4231. - insert the node tree of the callparanode directly
  4232. If a parameter is used only once, this is the best option if we can do so
  4233. - get the address of the argument, store it in a temp and insert a dereference to this temp
  4234. If the node tree cannot be inserted directly, taking the address of the argument and using it
  4235. is the second best option, but even this is not always possible
  4236. - assign the value of the argument to a newly created temp
  4237. This is the fall back which works always
  4238. Notes:
  4239. - we need to take care that we use the type of the defined parameter and not of the
  4240. passed parameter, because these can be different in case of a formaldef (PFV)
  4241. }
  4242. { pre-compute some values }
  4243. paracomplexity:=node_complexity(para.left);
  4244. if para.parasym.varspez=vs_const then
  4245. pushconstaddr:=paramanager.push_addr_param(vs_const,para.parasym.vardef,procdefinition.proccalloption)
  4246. else
  4247. pushconstaddr:=false;
  4248. realtarget:=actualtargetnode(@para.left)^;
  4249. { if the parameter is "complex", try to take the address of the
  4250. parameter expression, store it in a temp and replace occurrences of
  4251. the parameter with dereferencings of this temp
  4252. }
  4253. complexpara:=
  4254. { don't create a temp. for function results }
  4255. not(nf_is_funcret in realtarget.flags) and
  4256. { this makes only sense if the parameter is reasonably complex,
  4257. otherwise inserting directly is a better solution }
  4258. (
  4259. (paracomplexity>2) or
  4260. { don't create a temp. for the often seen case that p^ is passed to a var parameter }
  4261. ((paracomplexity>1) and
  4262. not((realtarget.nodetype=derefn) and (para.parasym.varspez in [vs_var,vs_out,vs_constref])) and
  4263. not((realtarget.nodetype=loadn) and tloadnode(realtarget).is_addr_param_load)
  4264. )
  4265. );
  4266. { We don't need temps for parameters that are already temps, except if
  4267. the passed temp could be put in a regvar while the parameter inside
  4268. the routine cannot be (e.g., because its address is taken in the
  4269. routine), or if the temp is a const and the parameter gets modified }
  4270. if (para.left.nodetype=temprefn) and
  4271. (not(ti_may_be_in_reg in ttemprefnode(para.left).tempflags) or
  4272. not(tparavarsym(para.parasym).varregable in [vr_none,vr_addr])) and
  4273. (not(ti_const in ttemprefnode(para.left).tempflags) or
  4274. (tparavarsym(para.parasym).varstate in [vs_initialised,vs_declared,vs_read])) then
  4275. exit;
  4276. { check if we have to create a temp, assign the parameter's
  4277. contents to that temp and then substitute the parameter
  4278. with the temp everywhere in the function }
  4279. if needtemp then
  4280. begin
  4281. tempnode:=ctempcreatenode.create(para.parasym.vardef,para.parasym.vardef.size,
  4282. tt_persistent,tparavarsym(para.parasym).is_regvar(false));
  4283. addstatement(inlineinitstatement,tempnode);
  4284. addstatement(inlinecleanupstatement,ctempdeletenode.create(tempnode));
  4285. addstatement(inlineinitstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  4286. para.left));
  4287. para.left := ctemprefnode.create(tempnode);
  4288. { inherit addr_taken flag }
  4289. if (tabstractvarsym(para.parasym).addr_taken) then
  4290. tempnode.includetempflag(ti_addr_taken);
  4291. { inherit const }
  4292. if tabstractvarsym(para.parasym).varspez=vs_const then
  4293. begin
  4294. tempnode.includetempflag(ti_const);
  4295. { apply less strict rules for the temp. to be a register than
  4296. ttempcreatenode does
  4297. this way, dyn. array, ansistrings etc. can be put into registers as well }
  4298. if tparavarsym(para.parasym).is_regvar(false) then
  4299. tempnode.includetempflag(ti_may_be_in_reg);
  4300. end;
  4301. result:=true;
  4302. end
  4303. end;
  4304. procedure tcallnode.createinlineparas;
  4305. var
  4306. para: tcallparanode;
  4307. n: tnode;
  4308. complexpara: boolean;
  4309. begin
  4310. { parameters }
  4311. para := tcallparanode(left);
  4312. while assigned(para) do
  4313. begin
  4314. if (para.parasym.typ = paravarsym) and
  4315. ((para.parasym.refs>0) or
  4316. not(cs_opt_dead_values in current_settings.optimizerswitches) or
  4317. might_have_sideeffects(para.left)) then
  4318. begin
  4319. { must take copy of para.left, because if it contains a }
  4320. { temprefn pointing to a copied temp (e.g. methodpointer), }
  4321. { then this parameter must be changed to point to the copy of }
  4322. { that temp (JM) }
  4323. n := para.left.getcopy;
  4324. para.left.free;
  4325. para.left := n;
  4326. firstpass(para.left);
  4327. if not maybecreateinlineparatemp(para,complexpara) and
  4328. complexpara then
  4329. wrapcomplexinlinepara(para);
  4330. end;
  4331. para := tcallparanode(para.right);
  4332. end;
  4333. { local variables }
  4334. if not assigned(tprocdef(procdefinition).localst) or
  4335. (tprocdef(procdefinition).localst.SymList.count = 0) then
  4336. exit;
  4337. inlinelocals.count:=tprocdef(procdefinition).localst.SymList.count;
  4338. tprocdef(procdefinition).localst.SymList.ForEachCall(@createlocaltemps,nil);
  4339. end;
  4340. procedure tcallnode.wrapcomplexinlinepara(para: tcallparanode);
  4341. var
  4342. ptrtype: tdef;
  4343. tempnode: ttempcreatenode;
  4344. paraaddr: taddrnode;
  4345. begin
  4346. ptrtype:=cpointerdef.getreusable(para.left.resultdef);
  4347. tempnode:=ctempcreatenode.create(ptrtype,ptrtype.size,tt_persistent,true);
  4348. addstatement(inlineinitstatement,tempnode);
  4349. addstatement(inlinecleanupstatement,ctempdeletenode.create(tempnode));
  4350. { inherit addr_taken flag }
  4351. if (tabstractvarsym(para.parasym).addr_taken) then
  4352. tempnode.includetempflag(ti_addr_taken);
  4353. { inherit read only }
  4354. if tabstractvarsym(para.parasym).varspez=vs_const then
  4355. tempnode.includetempflag(ti_const);
  4356. paraaddr:=caddrnode.create_internal(para.left);
  4357. include(paraaddr.flags,nf_typedaddr);
  4358. addstatement(inlineinitstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  4359. paraaddr));
  4360. para.left:=cderefnode.create(ctemprefnode.create(tempnode));
  4361. end;
  4362. function tcallnode.optimize_funcret_assignment(inlineblock: tblocknode): tnode;
  4363. var
  4364. hp : tstatementnode;
  4365. hp2 : tnode;
  4366. resassign : tassignmentnode;
  4367. begin
  4368. result:=nil;
  4369. if not assigned(funcretnode) or
  4370. not(cnf_return_value_used in callnodeflags) then
  4371. exit;
  4372. { tempcreatenode for the function result }
  4373. hp:=tstatementnode(inlineblock.left);
  4374. if not(assigned(hp)) or
  4375. (hp.left.nodetype <> tempcreaten) or
  4376. not(nf_is_funcret in hp.left.flags) then
  4377. exit;
  4378. { constant assignment? right must be a constant (mainly to avoid trying
  4379. to reuse local temps which may already be freed afterwards once these
  4380. checks are made looser) }
  4381. hp:=tstatementnode(hp.right);
  4382. if not(assigned(hp)) or
  4383. (hp.left.nodetype<>assignn) or
  4384. not is_constnode(tassignmentnode(hp.left).right) then
  4385. exit;
  4386. { left must be function result }
  4387. resassign:=tassignmentnode(hp.left);
  4388. hp2:=resassign.left;
  4389. { can have extra type conversion due to absolute mapping
  4390. of <fucntionname> on function result var }
  4391. if (hp2.nodetype=typeconvn) and (ttypeconvnode(hp2).convtype=tc_equal) then
  4392. hp2:=ttypeconvnode(hp2).left;
  4393. if (hp2.nodetype<>temprefn) or
  4394. not(nf_is_funcret in hp2.flags) then
  4395. exit;
  4396. { tempdelete to normal of the function result }
  4397. hp:=tstatementnode(hp.right);
  4398. if not(assigned(hp)) or
  4399. (hp.left.nodetype <> tempdeleten) then
  4400. exit;
  4401. { the function result once more }
  4402. hp:=tstatementnode(hp.right);
  4403. if not(assigned(hp)) or
  4404. (hp.left.nodetype<>temprefn) or
  4405. not(nf_is_funcret in hp.left.flags) then
  4406. exit;
  4407. { should be the end }
  4408. if assigned(hp.right) then
  4409. exit;
  4410. { we made it! }
  4411. result:=tassignmentnode(resassign).right.getcopy;
  4412. firstpass(result);
  4413. end;
  4414. { this procedure removes the user code flag because it prevents optimizations }
  4415. function removeusercodeflag(var n : tnode; arg : pointer) : foreachnoderesult;
  4416. begin
  4417. result:=fen_false;
  4418. if nf_usercode_entry in n.flags then
  4419. begin
  4420. exclude(n.flags,nf_usercode_entry);
  4421. result:=fen_norecurse_true;
  4422. end;
  4423. end;
  4424. { reference symbols that are imported from another unit }
  4425. function importglobalsyms(var n:tnode; arg:pointer):foreachnoderesult;
  4426. var
  4427. sym : tsym;
  4428. begin
  4429. result:=fen_false;
  4430. if n.nodetype=loadn then
  4431. begin
  4432. sym:=tloadnode(n).symtableentry;
  4433. if sym.typ=staticvarsym then
  4434. begin
  4435. if FindUnitSymtable(tloadnode(n).symtable).moduleid<>current_module.moduleid then
  4436. current_module.addimportedsym(sym);
  4437. end
  4438. else if (sym.typ=constsym) and (tconstsym(sym).consttyp=constresourcestring) then
  4439. begin
  4440. if tloadnode(n).symtableentry.owner.moduleid<>current_module.moduleid then
  4441. current_module.addimportedsym(sym);
  4442. end;
  4443. end
  4444. else if (n.nodetype=calln) then
  4445. begin
  4446. if (assigned(tcallnode(n).procdefinition)) and
  4447. (tcallnode(n).procdefinition.typ=procdef) and
  4448. (findunitsymtable(tcallnode(n).procdefinition.owner).moduleid<>current_module.moduleid) then
  4449. current_module.addimportedsym(tprocdef(tcallnode(n).procdefinition).procsym);
  4450. end;
  4451. end;
  4452. function tcallnode.pass1_inline:tnode;
  4453. var
  4454. n,
  4455. body : tnode;
  4456. para : tcallparanode;
  4457. inlineblock,
  4458. inlinecleanupblock : tblocknode;
  4459. begin
  4460. inc(inlinelevel);
  4461. result:=nil;
  4462. if not(assigned(tprocdef(procdefinition).inlininginfo) and
  4463. assigned(tprocdef(procdefinition).inlininginfo^.code)) then
  4464. internalerror(200412021);
  4465. inlinelocals:=TFPObjectList.create(true);
  4466. { inherit flags }
  4467. current_procinfo.flags:=current_procinfo.flags+
  4468. ((procdefinition as tprocdef).inlininginfo^.flags*inherited_inlining_flags);
  4469. { Create new code block for inlining }
  4470. inlineblock:=internalstatements(inlineinitstatement);
  4471. { make sure that valid_for_assign() returns false for this block
  4472. (otherwise assigning values to the block will result in assigning
  4473. values to the inlined function's result) }
  4474. include(inlineblock.flags,nf_no_lvalue);
  4475. inlinecleanupblock:=internalstatements(inlinecleanupstatement);
  4476. if assigned(callinitblock) then
  4477. addstatement(inlineinitstatement,callinitblock.getcopy);
  4478. { replace complex parameters with temps }
  4479. createinlineparas;
  4480. { create a copy of the body and replace parameter loads with the parameter values }
  4481. body:=tprocdef(procdefinition).inlininginfo^.code.getcopy;
  4482. foreachnodestatic(pm_postprocess,body,@removeusercodeflag,nil);
  4483. foreachnodestatic(pm_postprocess,body,@importglobalsyms,nil);
  4484. foreachnode(pm_preprocess,body,@replaceparaload,@fileinfo);
  4485. { Concat the body and finalization parts }
  4486. addstatement(inlineinitstatement,body);
  4487. addstatement(inlineinitstatement,inlinecleanupblock);
  4488. inlinecleanupblock:=nil;
  4489. if assigned(callcleanupblock) then
  4490. addstatement(inlineinitstatement,callcleanupblock.getcopy);
  4491. { the last statement of the new inline block must return the
  4492. location and type of the function result.
  4493. This is not needed when the result is not used, also the tempnode is then
  4494. already destroyed by a tempdelete in the callcleanupblock tree }
  4495. if not is_void(resultdef) and
  4496. (cnf_return_value_used in callnodeflags) then
  4497. begin
  4498. if assigned(funcretnode) then
  4499. addstatement(inlineinitstatement,funcretnode.getcopy)
  4500. else
  4501. begin
  4502. para:=tcallparanode(left);
  4503. while assigned(para) do
  4504. begin
  4505. if (vo_is_hidden_para in para.parasym.varoptions) and
  4506. (vo_is_funcret in para.parasym.varoptions) then
  4507. begin
  4508. addstatement(inlineinitstatement,para.left.getcopy);
  4509. break;
  4510. end;
  4511. para:=tcallparanode(para.right);
  4512. end;
  4513. end;
  4514. end;
  4515. { consider it must not be inlined if called
  4516. again inside the args or itself }
  4517. exclude(procdefinition.procoptions,po_inline);
  4518. typecheckpass(tnode(inlineblock));
  4519. doinlinesimplify(tnode(inlineblock));
  4520. firstpass(tnode(inlineblock));
  4521. include(procdefinition.procoptions,po_inline);
  4522. result:=inlineblock;
  4523. { if the function result is used then verify that the blocknode
  4524. returns the same result type as the original callnode }
  4525. if (cnf_return_value_used in callnodeflags) and
  4526. (result.resultdef<>resultdef) then
  4527. internalerror(200709171);
  4528. { free the temps for the locals }
  4529. inlinelocals.free;
  4530. inlinelocals:=nil;
  4531. inlineinitstatement:=nil;
  4532. inlinecleanupstatement:=nil;
  4533. { if all that's left of the inlined function is an constant assignment
  4534. to the result, replace the whole block with the constant only }
  4535. n:=optimize_funcret_assignment(inlineblock);
  4536. if assigned(n) then
  4537. begin
  4538. inlineblock.free;
  4539. result:=n;
  4540. end;
  4541. {$ifdef DEBUGINLINE}
  4542. writeln;
  4543. writeln('**************************',tprocdef(procdefinition).mangledname);
  4544. printnode(output,result);
  4545. {$endif DEBUGINLINE}
  4546. dec(inlinelevel);
  4547. end;
  4548. end.