ncal.pas 240 KB

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