ncal.pas 215 KB

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