ncal.pas 240 KB

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