ncal.pas 218 KB

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