ncal.pas 213 KB

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