ncal.pas 242 KB

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