ncal.pas 212 KB

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