ncal.pas 214 KB

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