ncal.pas 214 KB

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