ncal.pas 234 KB

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