ncal.pas 237 KB

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