ncal.pas 218 KB

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