ncal.pas 243 KB

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