ncal.pas 240 KB

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