ncal.pas 222 KB

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