ncal.pas 218 KB

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