ncal.pas 231 KB

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