ncal.pas 231 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509
  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. begin
  2489. result := nil;
  2490. case intrinsiccode of
  2491. in_str_x_string:
  2492. begin
  2493. { rare optimization opportunity which takes some extra time,
  2494. so check only at level 3+ }
  2495. if not(cs_opt_level3 in current_settings.optimizerswitches) then
  2496. exit;
  2497. { If n is a constant, attempt to convert, for example:
  2498. "Str(5, Output);" to "Output := '5';" }
  2499. { Format of the internal function (also for fpc_shortstr_uint) is:
  2500. $fpc_shortstr_sint(Int64;Int64;out OpenString;<const Int64>); }
  2501. { Remember the parameters are in reverse order - the leftmost one
  2502. can usually be ignored }
  2503. para := GetParaFromIndex(1);
  2504. if Assigned(para) then
  2505. begin
  2506. { Output variable }
  2507. outnode := para.left;
  2508. para := GetParaFromIndex(2);
  2509. if Assigned(para) then
  2510. begin
  2511. { Maximum length }
  2512. maxlennode := para.left;
  2513. if is_integer(maxlennode.resultdef) then
  2514. begin
  2515. para := GetParaFromIndex(3);
  2516. while (maxlennode.nodetype = typeconvn) and (ttypeconvnode(maxlennode).convtype in [tc_equal, tc_int_2_int]) do
  2517. begin
  2518. maxlennode := ttypeconvnode(maxlennode).left;
  2519. end;
  2520. if Assigned(para) and is_constintnode(maxlennode) then
  2521. begin
  2522. { Numeric value }
  2523. valnode := para.left;
  2524. if is_integer(valnode.resultdef) and not Assigned(GetParaFromIndex(4)) then
  2525. begin
  2526. while (valnode.nodetype = typeconvn) and (ttypeconvnode(valnode).convtype in [tc_equal, tc_int_2_int]) do
  2527. begin
  2528. valnode := ttypeconvnode(valnode).left;
  2529. end;
  2530. if is_constintnode(valnode) then
  2531. begin
  2532. MaxStrLen := TOrdConstNode(maxlennode).value.svalue;
  2533. { If we've gotten this far, we can convert the node into a direct assignment }
  2534. StringLiteral := tostr(tordconstnode(valnode).value);
  2535. if MaxStrLen <> -1 then
  2536. SetLength(StringLiteral, Integer(MaxStrLen));
  2537. result := cassignmentnode.create(
  2538. outnode.getcopy,
  2539. cstringconstnode.createstr(StringLiteral)
  2540. );
  2541. end;
  2542. end;
  2543. end;
  2544. end;
  2545. end;
  2546. end;
  2547. end;
  2548. else
  2549. ;
  2550. end;
  2551. end;
  2552. function tcallnode.safe_call_self_node: tnode;
  2553. begin
  2554. if not assigned(call_self_node) then
  2555. begin
  2556. CGMessage(parser_e_illegal_expression);
  2557. call_self_node:=cerrornode.create;
  2558. end;
  2559. result:=call_self_node;
  2560. end;
  2561. procedure tcallnode.gen_vmt_entry_load;
  2562. var
  2563. vmt_def: trecorddef;
  2564. begin
  2565. if not assigned(right) and
  2566. not assigned(overrideprocnamedef) and
  2567. (po_virtualmethod in procdefinition.procoptions) and
  2568. not is_objectpascal_helper(tprocdef(procdefinition).struct) and
  2569. assigned(methodpointer) and
  2570. (methodpointer.nodetype<>typen) then
  2571. begin
  2572. vmt_entry:=load_vmt_for_self_node(methodpointer.getcopy);
  2573. { get the right entry in the VMT }
  2574. vmt_entry:=cderefnode.create(vmt_entry);
  2575. typecheckpass(vmt_entry);
  2576. vmt_def:=trecorddef(vmt_entry.resultdef);
  2577. { tobjectdef(tprocdef(procdefinition).struct) can be a parent of the
  2578. methodpointer's resultdef, but the vmtmethodoffset of the method
  2579. in that objectdef is obviously the same as in any child class }
  2580. vmt_entry:=csubscriptnode.create(
  2581. trecordsymtable(vmt_def.symtable).findfieldbyoffset(
  2582. tobjectdef(tprocdef(procdefinition).struct).vmtmethodoffset(tprocdef(procdefinition).extnumber)
  2583. ),
  2584. vmt_entry
  2585. );
  2586. firstpass(vmt_entry);
  2587. end;
  2588. end;
  2589. procedure tcallnode.gen_syscall_para(para: tcallparanode);
  2590. begin
  2591. { unsupported }
  2592. internalerror(2014040101);
  2593. end;
  2594. procedure tcallnode.objc_convert_to_message_send;
  2595. var
  2596. block,
  2597. selftree : tnode;
  2598. statements : tstatementnode;
  2599. field : tfieldvarsym;
  2600. temp : ttempcreatenode;
  2601. selfrestype,
  2602. objcsupertype : tdef;
  2603. srsym : tsym;
  2604. srsymtable : tsymtable;
  2605. msgsendname : string;
  2606. begin
  2607. if not(m_objectivec1 in current_settings.modeswitches) then
  2608. Message(parser_f_modeswitch_objc_required);
  2609. { typecheck pass must already have run on the call node,
  2610. because pass1 calls this method
  2611. }
  2612. { default behaviour: call objc_msgSend and friends;
  2613. 64 bit targets for Mac OS X can override this as they
  2614. can call messages via an indirect function call similar to
  2615. dynamically linked functions, ARM maybe as well (not checked)
  2616. Which variant of objc_msgSend is used depends on the
  2617. result type, and on whether or not it's an inherited call.
  2618. }
  2619. { make sure we don't perform this transformation twice in case
  2620. firstpass would be called multiple times }
  2621. include(callnodeflags,cnf_objc_processed);
  2622. { make sure the methodpointer doesn't get translated into a call
  2623. as well (endless loop) }
  2624. if methodpointer.nodetype=loadvmtaddrn then
  2625. tloadvmtaddrnode(methodpointer).forcall:=true;
  2626. { A) set the appropriate objc_msgSend* variant to call }
  2627. { The AArch64 abi does not require special handling for struct returns }
  2628. {$ifndef aarch64}
  2629. { record returned via implicit pointer }
  2630. if paramanager.ret_in_param(resultdef,procdefinition) then
  2631. begin
  2632. if not(cnf_inherited in callnodeflags) then
  2633. msgsendname:='OBJC_MSGSEND_STRET'
  2634. else if (target_info.system in systems_objc_nfabi) and
  2635. (not MacOSXVersionMin.isvalid or
  2636. (MacOSXVersionMin.relationto(10,6,0)>=0)) then
  2637. msgsendname:='OBJC_MSGSENDSUPER2_STRET'
  2638. else
  2639. msgsendname:='OBJC_MSGSENDSUPER_STRET'
  2640. end
  2641. {$ifdef i386}
  2642. { special case for fpu results on i386 for non-inherited calls }
  2643. { TODO: also for x86_64 "extended" results }
  2644. else if (resultdef.typ=floatdef) and
  2645. not(cnf_inherited in callnodeflags) then
  2646. msgsendname:='OBJC_MSGSEND_FPRET'
  2647. {$endif i386}
  2648. { default }
  2649. else
  2650. {$endif aarch64}
  2651. if not(cnf_inherited in callnodeflags) then
  2652. msgsendname:='OBJC_MSGSEND'
  2653. else if (target_info.system in systems_objc_nfabi) and
  2654. (not MacOSXVersionMin.isvalid or
  2655. (MacOSXVersionMin.relationto(10,6,0)>=0)) then
  2656. msgsendname:='OBJC_MSGSENDSUPER2'
  2657. else
  2658. msgsendname:='OBJC_MSGSENDSUPER';
  2659. { get the mangled name }
  2660. srsym:=nil;
  2661. if not searchsym_in_named_module('OBJC',msgsendname,srsym,srsymtable) or
  2662. (srsym.typ<>procsym) or
  2663. (tprocsym(srsym).ProcdefList.count<>1) then
  2664. Message1(cg_f_unknown_compilerproc,'objc.'+msgsendname);
  2665. foverrideprocnamedef:=tprocdef(tprocsym(srsym).ProcdefList[0]);
  2666. { B) Handle self }
  2667. { 1) in case of sending a message to a superclass, self is a pointer to
  2668. an objc_super record
  2669. }
  2670. if (cnf_inherited in callnodeflags) then
  2671. begin
  2672. block:=internalstatements(statements);
  2673. objcsupertype:=search_named_unit_globaltype('OBJC','OBJC_SUPER',true).typedef;
  2674. if (objcsupertype.typ<>recorddef) then
  2675. internalerror(2009032901);
  2676. { temp for the for the objc_super record }
  2677. temp:=ctempcreatenode.create(objcsupertype,objcsupertype.size,tt_persistent,false);
  2678. addstatement(statements,temp);
  2679. { initialize objc_super record }
  2680. selftree:=safe_call_self_node.getcopy;
  2681. { we can call an inherited class static/method from a regular method
  2682. -> self node must change from instance pointer to vmt pointer)
  2683. }
  2684. if (po_classmethod in procdefinition.procoptions) and
  2685. (selftree.resultdef.typ<>classrefdef) then
  2686. begin
  2687. selftree:=cloadvmtaddrnode.create(selftree);
  2688. { since we're in a class method of the current class, its
  2689. information has already been initialized (and that of all of
  2690. its parent classes too) }
  2691. tloadvmtaddrnode(selftree).forcall:=true;
  2692. typecheckpass(selftree);
  2693. end;
  2694. selfrestype:=selftree.resultdef;
  2695. field:=tfieldvarsym(trecorddef(objcsupertype).symtable.find('RECEIVER'));
  2696. if not assigned(field) then
  2697. internalerror(2009032902);
  2698. { first the destination object/class instance }
  2699. addstatement(statements,
  2700. cassignmentnode.create(
  2701. csubscriptnode.create(field,ctemprefnode.create(temp)),
  2702. selftree
  2703. )
  2704. );
  2705. { and secondly, the class type in which the selector must be looked
  2706. up (the parent class in case of an instance method, the parent's
  2707. metaclass in case of a class method) }
  2708. field:=tfieldvarsym(trecorddef(objcsupertype).symtable.find('_CLASS'));
  2709. if not assigned(field) then
  2710. internalerror(2009032903);
  2711. addstatement(statements,
  2712. cassignmentnode.create(
  2713. csubscriptnode.create(field,ctemprefnode.create(temp)),
  2714. objcsuperclassnode(selftree.resultdef)
  2715. )
  2716. );
  2717. { result of this block is the address of this temp }
  2718. addstatement(statements,ctypeconvnode.create_internal(
  2719. caddrnode.create_internal(ctemprefnode.create(temp)),selfrestype)
  2720. );
  2721. { replace the method pointer with the address of this temp }
  2722. methodpointer.free;
  2723. methodpointer:=block;
  2724. typecheckpass(block);
  2725. end
  2726. else
  2727. { 2) regular call (not inherited) }
  2728. begin
  2729. { a) If we're calling a class method, use a class ref. }
  2730. if (po_classmethod in procdefinition.procoptions) and
  2731. ((methodpointer.nodetype=typen) or
  2732. (methodpointer.resultdef.typ<>classrefdef)) then
  2733. begin
  2734. methodpointer:=cloadvmtaddrnode.create(methodpointer);
  2735. { no need to obtain the class ref by calling class(), sending
  2736. this message will initialize it if necessary }
  2737. tloadvmtaddrnode(methodpointer).forcall:=true;
  2738. firstpass(methodpointer);
  2739. end;
  2740. end;
  2741. end;
  2742. function tcallnode.gen_vmt_tree:tnode;
  2743. var
  2744. vmttree : tnode;
  2745. begin
  2746. vmttree:=nil;
  2747. if not(procdefinition.proctypeoption in [potype_constructor,potype_destructor]) then
  2748. internalerror(200305051);
  2749. { When methodpointer was a callnode we must load it first into a
  2750. temp to prevent the processing callnode twice }
  2751. if (methodpointer.nodetype=calln) then
  2752. internalerror(200405122);
  2753. { Handle classes and legacy objects separate to make it
  2754. more maintainable }
  2755. if (methodpointer.resultdef.typ=classrefdef) then
  2756. begin
  2757. if not is_class(tclassrefdef(methodpointer.resultdef).pointeddef) then
  2758. internalerror(200501041);
  2759. { constructor call via classreference => allocate memory }
  2760. if (procdefinition.proctypeoption=potype_constructor) then
  2761. begin
  2762. vmttree:=cpointerconstnode.create(1,voidpointertype);
  2763. end
  2764. else { <class of xx>.destroy is not valid }
  2765. InternalError(2014020601);
  2766. end
  2767. else
  2768. { Class style objects }
  2769. if is_class(methodpointer.resultdef) then
  2770. begin
  2771. { inherited call, no create/destroy }
  2772. if (cnf_inherited in callnodeflags) then
  2773. vmttree:=cpointerconstnode.create(0,voidpointertype)
  2774. else
  2775. { do not create/destroy when called from member function
  2776. without specifying self explicit }
  2777. if (cnf_member_call in callnodeflags) then
  2778. begin
  2779. { destructor (in the same class, since cnf_member_call):
  2780. if not called from a destructor then
  2781. call beforedestruction and release instance, vmt=1
  2782. else
  2783. don't release instance, vmt=0
  2784. constructor (in the same class, since cnf_member_call):
  2785. if called from a constructor then
  2786. don't call afterconstruction, vmt=0
  2787. else
  2788. call afterconstrution but not NewInstance, vmt=-1 }
  2789. if (procdefinition.proctypeoption=potype_destructor) then
  2790. if (current_procinfo.procdef.proctypeoption<>potype_constructor) then
  2791. vmttree:=cpointerconstnode.create(1,voidpointertype)
  2792. else
  2793. vmttree:=cpointerconstnode.create(0,voidpointertype)
  2794. else if (current_procinfo.procdef.proctypeoption=potype_constructor) and
  2795. (procdefinition.proctypeoption=potype_constructor) then
  2796. vmttree:=cpointerconstnode.create(0,voidpointertype)
  2797. else
  2798. vmttree:=cpointerconstnode.create(TConstPtrUInt(-1),voidpointertype);
  2799. end
  2800. else
  2801. { normal call to method like cl1.proc }
  2802. begin
  2803. { destructor:
  2804. if not(called from exception block in constructor) or
  2805. (called from afterconstruction)
  2806. call beforedestruction and release instance, vmt=1
  2807. else
  2808. don't call beforedestruction and release instance, vmt=-1
  2809. constructor:
  2810. if called from a constructor in the same class using self.create then
  2811. don't call afterconstruction, vmt=0
  2812. else
  2813. call afterconstruction, vmt=1 }
  2814. if (procdefinition.proctypeoption=potype_destructor) then
  2815. if (cnf_create_failed in callnodeflags) and
  2816. is_class(methodpointer.resultdef) then
  2817. vmttree:=call_vmt_node.getcopy
  2818. else if not(cnf_create_failed in callnodeflags) then
  2819. vmttree:=cpointerconstnode.create(1,voidpointertype)
  2820. else
  2821. vmttree:=cpointerconstnode.create(TConstPtrUInt(-1),voidpointertype)
  2822. else
  2823. begin
  2824. if (current_procinfo.procdef.proctypeoption=potype_constructor) and
  2825. (procdefinition.proctypeoption=potype_constructor) and
  2826. (methodpointer.nodetype=loadn) and
  2827. (loadnf_is_self in tloadnode(methodpointer).loadnodeflags) then
  2828. vmttree:=cpointerconstnode.create(0,voidpointertype)
  2829. else
  2830. vmttree:=cpointerconstnode.create(TConstPtrUInt(-1),voidpointertype);
  2831. end;
  2832. end;
  2833. end
  2834. else
  2835. { Old style object }
  2836. begin
  2837. { constructor with extended syntax called from new }
  2838. if (cnf_new_call in callnodeflags) then
  2839. vmttree:=cloadvmtaddrnode.create(ctypenode.create(methodpointer.resultdef))
  2840. else
  2841. { destructor with extended syntax called from dispose }
  2842. { value -1 is what fpc_help_constructor() changes VMT to when it allocates memory }
  2843. if (cnf_dispose_call in callnodeflags) then
  2844. vmttree:=cpointerconstnode.create(TConstPtrUInt(-1),voidpointertype)
  2845. else
  2846. { destructor called from exception block in constructor }
  2847. if (cnf_create_failed in callnodeflags) then
  2848. vmttree:=ctypeconvnode.create_internal(call_vmt_node.getcopy,voidpointertype)
  2849. else
  2850. { inherited call, no create/destroy }
  2851. if (cnf_inherited in callnodeflags) then
  2852. vmttree:=cpointerconstnode.create(0,voidpointertype)
  2853. else
  2854. { do not create/destroy when called from member function
  2855. without specifying self explicit }
  2856. if (cnf_member_call in callnodeflags) then
  2857. begin
  2858. { destructor: don't release instance, vmt=0
  2859. constructor: don't initialize instance, vmt=0 }
  2860. vmttree:=cpointerconstnode.create(0,voidpointertype)
  2861. end
  2862. else
  2863. { normal object call like obj.proc }
  2864. begin
  2865. { destructor: direct call, no dispose, vmt=0
  2866. constructor: initialize object, load vmt }
  2867. if (procdefinition.proctypeoption=potype_constructor) then
  2868. begin
  2869. { old styled inherited call? }
  2870. if (methodpointer.nodetype=typen) then
  2871. vmttree:=cpointerconstnode.create(0,voidpointertype)
  2872. else
  2873. vmttree:=cloadvmtaddrnode.create(ctypenode.create(methodpointer.resultdef))
  2874. end
  2875. else
  2876. vmttree:=cpointerconstnode.create(0,voidpointertype);
  2877. end;
  2878. end;
  2879. result:=vmttree;
  2880. end;
  2881. function tcallnode.gen_block_context: tnode;
  2882. begin
  2883. { the self parameter of a block invocation is that address of the
  2884. block literal (which is what right contains) }
  2885. result:=right.getcopy;
  2886. end;
  2887. function check_funcret_used_as_para(var n: tnode; arg: pointer): foreachnoderesult;
  2888. var
  2889. destsym : tsym absolute arg;
  2890. begin
  2891. result := fen_false;
  2892. if (n.nodetype=loadn) and
  2893. (tloadnode(n).symtableentry = destsym) then
  2894. result := fen_norecurse_true;
  2895. end;
  2896. function check_funcret_temp_used_as_para(var n: tnode; arg: pointer): foreachnoderesult;
  2897. var
  2898. tempinfo : ptempinfo absolute arg;
  2899. begin
  2900. result := fen_false;
  2901. if (n.nodetype=temprefn) and
  2902. (ttemprefnode(n).tempinfo = tempinfo) then
  2903. result := fen_norecurse_true;
  2904. end;
  2905. function tcallnode.funcret_can_be_reused:boolean;
  2906. var
  2907. realassignmenttarget: tnode;
  2908. alignment: longint;
  2909. begin
  2910. result:=false;
  2911. { we are processing an assignment node? }
  2912. if not(assigned(aktassignmentnode) and
  2913. (aktassignmentnode.right=self) and
  2914. (aktassignmentnode.left.resultdef=resultdef)) then
  2915. exit;
  2916. { destination must be able to be passed as var parameter }
  2917. if not valid_for_var(aktassignmentnode.left,false) then
  2918. exit;
  2919. { destination must be a simple load so it doesn't need a temp when
  2920. it is evaluated }
  2921. if not is_simple_para_load(aktassignmentnode.left,false) then
  2922. exit;
  2923. { remove possible typecasts }
  2924. realassignmenttarget:=actualtargetnode(@aktassignmentnode.left)^;
  2925. { when the result is returned by value (instead of by writing it to the
  2926. address passed in a hidden parameter), aktassignmentnode.left will
  2927. only be changed once the function has returned and we don't have to
  2928. perform any checks regarding whether it may alias with one of the
  2929. parameters -- unless this is an inline function, in which case
  2930. writes to the function result will directly change it and we do have
  2931. to check for potential aliasing }
  2932. if not paramanager.ret_in_param(resultdef,procdefinition) then
  2933. begin
  2934. if not(cnf_do_inline in callnodeflags) then
  2935. begin
  2936. result:=true;
  2937. exit;
  2938. end
  2939. else
  2940. begin
  2941. { don't replace the function result if we are inlining and if
  2942. the destination is complex, this could lead to lengthy
  2943. code in case the function result is used often and it is
  2944. assigned e.g. to a threadvar }
  2945. if node_complexity(aktassignmentnode.left)>1 then
  2946. exit;
  2947. end;
  2948. end;
  2949. { if the result is the same as the self parameter (in case of objects),
  2950. we can't optimise. We have to check this explicitly becaise
  2951. hidden parameters such as self have not yet been inserted at this
  2952. point
  2953. }
  2954. if assigned(methodpointer) and
  2955. realassignmenttarget.isequal(actualtargetnode(@methodpointer)^) then
  2956. exit;
  2957. { when we substitute a function result inside an inlined function,
  2958. we may take the address of this function result. Therefore the
  2959. substituted function result may not be in a register, as we cannot
  2960. take its address in that case }
  2961. if (realassignmenttarget.nodetype=temprefn) and
  2962. not(ti_addr_taken in ttemprefnode(realassignmenttarget).tempflags) and
  2963. not(ti_may_be_in_reg in ttemprefnode(realassignmenttarget).tempflags) then
  2964. begin
  2965. result:=not foreachnodestatic(left,@check_funcret_temp_used_as_para,ttemprefnode(realassignmenttarget).tempinfo);
  2966. exit;
  2967. end;
  2968. if (realassignmenttarget.nodetype=loadn) and
  2969. { nested procedures may access the current procedure's locals }
  2970. (procdefinition.parast.symtablelevel=normal_function_level) and
  2971. { must be a local variable, a value para or a hidden function result }
  2972. { parameter (which can be passed by address, but in that case it got }
  2973. { through these same checks at the caller side and is thus safe ) }
  2974. { other option: we're calling a compilerproc, because those don't
  2975. rely on global state
  2976. }
  2977. ((po_compilerproc in procdefinition.procoptions) or
  2978. (
  2979. (
  2980. (tloadnode(realassignmenttarget).symtableentry.typ=localvarsym) or
  2981. (
  2982. (tloadnode(realassignmenttarget).symtableentry.typ=paravarsym) and
  2983. ((tparavarsym(tloadnode(realassignmenttarget).symtableentry).varspez = vs_value) or
  2984. (vo_is_funcret in tparavarsym(tloadnode(realassignmenttarget).symtableentry).varoptions))
  2985. )
  2986. ) and
  2987. { the address may not have been taken of the variable/parameter, because }
  2988. { otherwise it's possible that the called function can access it via a }
  2989. { global variable or other stored state }
  2990. (
  2991. not(tabstractvarsym(tloadnode(realassignmenttarget).symtableentry).addr_taken) and
  2992. (tabstractvarsym(tloadnode(realassignmenttarget).symtableentry).varregable in [vr_none,vr_addr])
  2993. )
  2994. )
  2995. ) then
  2996. begin
  2997. { If the funcret is also used as a parameter we can't optimize because the funcret
  2998. and the parameter will point to the same address. That means that a change of the result variable
  2999. will result also in a change of the parameter value }
  3000. result:=not foreachnodestatic(left,@check_funcret_used_as_para,tloadnode(realassignmenttarget).symtableentry);
  3001. { ensure that it is aligned using the default alignment }
  3002. alignment:=tabstractvarsym(tloadnode(realassignmenttarget).symtableentry).vardef.alignment;
  3003. if (used_align(alignment,target_info.alignment.localalignmin,target_info.alignment.localalignmax)<>
  3004. used_align(alignment,current_settings.alignment.localalignmin,current_settings.alignment.localalignmax)) then
  3005. result:=false;
  3006. exit;
  3007. end;
  3008. end;
  3009. procedure tcallnode.maybe_create_funcret_node;
  3010. var
  3011. temp : ttempcreatenode;
  3012. begin
  3013. if procdefinition.proctypeoption=potype_constructor then
  3014. exit;
  3015. { For the function result we need to create a temp node for:
  3016. - Inlined functions
  3017. - Types requiring initialization/finalization
  3018. - Types passed in parameters }
  3019. if not is_void(resultdef) and
  3020. not assigned(funcretnode) and
  3021. (
  3022. (cnf_do_inline in callnodeflags) or
  3023. is_managed_type(resultdef) or
  3024. paramanager.ret_in_param(resultdef,procdefinition)
  3025. ) then
  3026. begin
  3027. { Optimize calls like x:=f() where we can use x directly as
  3028. result instead of using a temp. Condition is that x cannot be accessed from f().
  3029. This implies that x is a local variable or value parameter of the current block
  3030. and its address is not passed to f. One problem: what if someone takes the
  3031. address of x, puts it in a pointer variable/field and then accesses it that way
  3032. from within the function? This is solved (in a conservative way) using the
  3033. ti_addr_taken flag.
  3034. When the result is not not passed in a parameter there are no problem because
  3035. then it means only reference counted types (eg. ansistrings) that need a decr
  3036. of the refcount before being assigned. This is all done after the call so there
  3037. is no issue with exceptions and possible use of the old value in the called
  3038. function }
  3039. if funcret_can_be_reused then
  3040. begin
  3041. funcretnode:=aktassignmentnode.left.getcopy;
  3042. include(funcretnode.flags,nf_is_funcret);
  3043. { notify the assignment node that the assignment can be removed }
  3044. include(aktassignmentnode.flags,nf_assign_done_in_right);
  3045. end
  3046. else
  3047. begin
  3048. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,
  3049. (cnf_do_inline in callnodeflags) and
  3050. not(tabstractvarsym(tprocdef(procdefinition).funcretsym).varregable in [vr_none,vr_addr]));
  3051. include(temp.flags,nf_is_funcret);
  3052. { if a managed type is returned by reference, assigning something
  3053. to the result on the caller side will take care of decreasing
  3054. the reference count }
  3055. if paramanager.ret_in_param(resultdef,procdefinition) then
  3056. temp.includetempflag(ti_nofini);
  3057. add_init_statement(temp);
  3058. { When the function result is not used in an inlined function
  3059. we need to delete the temp. This can currently only be done by
  3060. a tempdeletenode and not after converting it to a normal temp }
  3061. if not(cnf_return_value_used in callnodeflags) and
  3062. (cnf_do_inline in callnodeflags) then
  3063. add_done_statement(ctempdeletenode.create(temp))
  3064. else
  3065. add_done_statement(ctempdeletenode.create_normal_temp(temp));
  3066. funcretnode:=ctemprefnode.create(temp);
  3067. include(funcretnode.flags,nf_is_funcret);
  3068. end;
  3069. end;
  3070. end;
  3071. procedure tcallnode.gen_hidden_parameters;
  3072. var
  3073. para : tcallparanode;
  3074. begin
  3075. para:=tcallparanode(left);
  3076. while assigned(para) do
  3077. begin
  3078. { The processing of high() and typeinfo() is already
  3079. done in the typecheckpass. We only need to process the
  3080. nodes that still have a nothingn }
  3081. if (vo_is_hidden_para in para.parasym.varoptions) and
  3082. (para.left.nodetype=nothingn) then
  3083. begin
  3084. { remove dummy nothingn }
  3085. para.left.free;
  3086. para.left:=nil;
  3087. { generate the corresponding nodes for the hidden parameter type }
  3088. if (vo_is_funcret in para.parasym.varoptions) then
  3089. begin
  3090. if not assigned(funcretnode) then
  3091. internalerror(200709083);
  3092. { if funcretnode is a temprefnode, we have to keep it intact
  3093. if it may have been created in maybe_create_funcret_node(),
  3094. because then it will also be destroyed by a
  3095. ctempdeletenode.create_normal_temp() in the cleanup code
  3096. for this call code. In that case we have to copy this
  3097. ttemprefnode after the tempdeletenode to reset its
  3098. tempinfo^.hookoncopy. This is done by copying funcretnode
  3099. in tcallnode.getcopy(), but for that to work we can't reset
  3100. funcretnode to nil here. }
  3101. if (funcretnode.nodetype<>temprefn) or
  3102. (not(cnf_return_value_used in callnodeflags) and
  3103. (cnf_do_inline in callnodeflags)) then
  3104. begin
  3105. para.left:=funcretnode;
  3106. funcretnode:=nil;
  3107. end
  3108. else
  3109. para.left:=funcretnode.getcopy;
  3110. end
  3111. else
  3112. if vo_is_self in para.parasym.varoptions then
  3113. begin
  3114. if assigned(right) then
  3115. para.left:=gen_procvar_context_tree_self
  3116. else
  3117. para.left:=gen_self_tree;
  3118. { make sure that e.g. the self pointer of an advanced
  3119. record does not become a regvar, because it's a vs_var
  3120. parameter }
  3121. if paramanager.push_addr_param(para.parasym.varspez,para.parasym.vardef,
  3122. procdefinition.proccalloption) then
  3123. make_not_regable(para.left,[ra_addr_regable]);
  3124. end
  3125. else
  3126. if vo_is_vmt in para.parasym.varoptions then
  3127. begin
  3128. para.left:=gen_vmt_tree;
  3129. end
  3130. else
  3131. if vo_is_syscall_lib in para.parasym.varoptions then
  3132. gen_syscall_para(para)
  3133. else
  3134. if vo_is_range_check in para.parasym.varoptions then
  3135. begin
  3136. para.left:=cordconstnode.create(Ord(cs_check_range in current_settings.localswitches),pasbool1type,false);
  3137. end
  3138. else
  3139. if vo_is_overflow_check in para.parasym.varoptions then
  3140. begin
  3141. para.left:=cordconstnode.create(Ord(cs_check_overflow in current_settings.localswitches),pasbool1type,false);
  3142. end
  3143. else
  3144. if vo_is_msgsel in para.parasym.varoptions then
  3145. begin
  3146. para.left:=cobjcselectornode.create(cstringconstnode.createstr(tprocdef(procdefinition).messageinf.str^));
  3147. end;
  3148. end;
  3149. if not assigned(para.left) then
  3150. internalerror(200709084);
  3151. para:=tcallparanode(para.right);
  3152. end;
  3153. end;
  3154. procedure tcallnode.verifyabstract(sym:TObject;arg:pointer);
  3155. var
  3156. pd : tprocdef;
  3157. i : longint;
  3158. j : integer;
  3159. hs : string;
  3160. begin
  3161. if (tsym(sym).typ<>procsym) then
  3162. exit;
  3163. for i:=0 to tprocsym(sym).ProcdefList.Count-1 do
  3164. begin
  3165. pd:=tprocdef(tprocsym(sym).ProcdefList[i]);
  3166. hs:=pd.procsym.name+pd.typename_paras([]);
  3167. j:=AbstractMethodsList.FindIndexOf(hs);
  3168. if j<>-1 then
  3169. AbstractMethodsList[j]:=pd
  3170. else
  3171. AbstractMethodsList.Add(hs,pd);
  3172. end;
  3173. end;
  3174. procedure tcallnode.verifyabstractcalls;
  3175. var
  3176. objectdf : tobjectdef;
  3177. parents : tlinkedlist;
  3178. objectinfo : tobjectinfoitem;
  3179. pd : tprocdef;
  3180. i : integer;
  3181. begin
  3182. objectdf := nil;
  3183. { verify if trying to create an instance of a class which contains
  3184. non-implemented abstract methods }
  3185. { first verify this class type, no class than exit }
  3186. { also, this checking can only be done if the constructor is directly
  3187. called, indirect constructor calls cannot be checked.
  3188. }
  3189. if assigned(methodpointer) and
  3190. not((methodpointer.nodetype=loadn) and
  3191. (loadnf_is_self in tloadnode(methodpointer).loadnodeflags)) then
  3192. begin
  3193. if (methodpointer.resultdef.typ = objectdef) then
  3194. objectdf:=tobjectdef(methodpointer.resultdef)
  3195. else
  3196. if (methodpointer.resultdef.typ = classrefdef) and
  3197. (tclassrefdef(methodpointer.resultdef).pointeddef.typ = objectdef) and
  3198. (methodpointer.nodetype in [typen,loadvmtaddrn]) then
  3199. objectdf:=tobjectdef(tclassrefdef(methodpointer.resultdef).pointeddef);
  3200. end;
  3201. if not assigned(objectdf) then
  3202. exit;
  3203. { quick exit if nothing to check }
  3204. if objectdf.abstractcnt = 0 then
  3205. exit;
  3206. parents := tlinkedlist.create;
  3207. AbstractMethodsList := TFPHashList.create;
  3208. { insert all parents in this class : the first item in the
  3209. list will be the base parent of the class .
  3210. }
  3211. while assigned(objectdf) do
  3212. begin
  3213. objectinfo:=tobjectinfoitem.create(objectdf);
  3214. parents.insert(objectinfo);
  3215. objectdf := objectdf.childof;
  3216. end;
  3217. { now all parents are in the correct order
  3218. insert all abstract methods in the list, and remove
  3219. those which are overridden by parent classes.
  3220. }
  3221. objectinfo:=tobjectinfoitem(parents.first);
  3222. while assigned(objectinfo) do
  3223. begin
  3224. objectdf := objectinfo.objinfo;
  3225. if assigned(objectdf.symtable) then
  3226. objectdf.symtable.SymList.ForEachCall(@verifyabstract,nil);
  3227. objectinfo:=tobjectinfoitem(objectinfo.next);
  3228. end;
  3229. if assigned(parents) then
  3230. parents.free;
  3231. { Finally give out a warning for each abstract method still in the list }
  3232. for i:=0 to AbstractMethodsList.Count-1 do
  3233. begin
  3234. pd:=tprocdef(AbstractMethodsList[i]);
  3235. if po_abstractmethod in pd.procoptions then
  3236. begin
  3237. Message2(type_w_instance_with_abstract,objectdf.typesymbolprettyname,pd.customprocname([pno_proctypeoption, pno_paranames,pno_ownername, pno_noclassmarker, pno_prettynames]));
  3238. MessagePos1(pd.fileinfo,sym_h_abstract_method_list,pd.fullprocname(true));
  3239. end;
  3240. end;
  3241. if assigned(AbstractMethodsList) then
  3242. AbstractMethodsList.Free;
  3243. end;
  3244. procedure tcallnode.convert_carg_array_of_const;
  3245. var
  3246. hp : tarrayconstructornode;
  3247. oldleft : tcallparanode;
  3248. begin
  3249. oldleft:=tcallparanode(left);
  3250. if oldleft.left.nodetype<>arrayconstructorn then
  3251. begin
  3252. CGMessage1(type_e_wrong_type_in_array_constructor,oldleft.left.resultdef.typename);
  3253. exit;
  3254. end;
  3255. include(callnodeflags,cnf_uses_varargs);
  3256. { Get arrayconstructor node and insert typeconvs }
  3257. hp:=tarrayconstructornode(oldleft.left);
  3258. { Add c args parameters }
  3259. { It could be an empty set }
  3260. if assigned(hp) and
  3261. assigned(hp.left) then
  3262. begin
  3263. while assigned(hp) do
  3264. begin
  3265. left:=ccallparanode.create(hp.left,left);
  3266. { set callparanode resultdef and flags }
  3267. left.resultdef:=hp.left.resultdef;
  3268. include(tcallparanode(left).callparaflags,cpf_varargs_para);
  3269. hp.left:=nil;
  3270. hp:=tarrayconstructornode(hp.right);
  3271. end;
  3272. end;
  3273. { Remove value of old array of const parameter, but keep it
  3274. in the list because it is required for bind_parasym.
  3275. Generate a nothign to keep callparanoed.left valid }
  3276. oldleft.left.free;
  3277. oldleft.left:=cnothingnode.create;
  3278. end;
  3279. procedure tcallnode.bind_parasym;
  3280. type
  3281. pcallparanode = ^tcallparanode;
  3282. var
  3283. i : integer;
  3284. pt : tcallparanode;
  3285. oldppt : pcallparanode;
  3286. varargspara,
  3287. currpara : tparavarsym;
  3288. hiddentree : tnode;
  3289. paradef : tdef;
  3290. begin
  3291. pt:=tcallparanode(left);
  3292. oldppt:=pcallparanode(@left);
  3293. { flag all callparanodes that belong to the varargs }
  3294. i:=paralength;
  3295. while (i>procdefinition.maxparacount) do
  3296. begin
  3297. include(pt.callparaflags,cpf_varargs_para);
  3298. oldppt:=pcallparanode(@pt.right);
  3299. pt:=tcallparanode(pt.right);
  3300. dec(i);
  3301. end;
  3302. { skip varargs that are inserted by array of const }
  3303. while assigned(pt) and
  3304. (cpf_varargs_para in pt.callparaflags) do
  3305. pt:=tcallparanode(pt.right);
  3306. { process normal parameters and insert hidden parameter nodes, the content
  3307. of the hidden parameters will be updated in pass1 }
  3308. for i:=procdefinition.paras.count-1 downto 0 do
  3309. begin
  3310. currpara:=tparavarsym(procdefinition.paras[i]);
  3311. if vo_is_hidden_para in currpara.varoptions then
  3312. begin
  3313. { Here we handle only the parameters that depend on
  3314. the types of the previous parameter. The typeconversion
  3315. can change the type in the next step. For example passing
  3316. an array can be change to a pointer and a deref.
  3317. We also handle the generation of parentfp parameters, as they
  3318. must all be created before pass_1 on targets that use explicit
  3319. parentfp structs (rather than the frame pointer). The reason
  3320. is that the necessary initialisation code for the these
  3321. structures is attached to the procedure's nodetree after
  3322. the resulttype pass.
  3323. }
  3324. if vo_is_high_para in currpara.varoptions then
  3325. begin
  3326. if not assigned(pt) or (i=0) then
  3327. internalerror(200304081);
  3328. { we need the information of the previous parameter }
  3329. paradef:=tparavarsym(procdefinition.paras[i-1]).vardef;
  3330. hiddentree:=gen_high_tree(pt.left,paradef);
  3331. { for open array of managed type, a copy of high parameter is
  3332. necessary to properly initialize before the call }
  3333. if is_open_array(paradef) and
  3334. (tparavarsym(procdefinition.paras[i-1]).varspez=vs_out) and
  3335. is_managed_type(tarraydef(paradef).elementdef) then
  3336. begin
  3337. typecheckpass(hiddentree);
  3338. {this eliminates double call to fpc_dynarray_high, if any}
  3339. maybe_load_in_temp(hiddentree);
  3340. oldppt^.third:=hiddentree.getcopy;
  3341. end;
  3342. end
  3343. else
  3344. if vo_is_typinfo_para in currpara.varoptions then
  3345. begin
  3346. if not assigned(pt) or (i=0) then
  3347. internalerror(200304082);
  3348. hiddentree:=caddrnode.create_internal(
  3349. crttinode.create(Tstoreddef(pt.resultdef),fullrtti,rdt_normal)
  3350. );
  3351. end
  3352. else if vo_is_parentfp in currpara.varoptions then
  3353. begin
  3354. if assigned(right) and (right.resultdef.typ=procvardef) and
  3355. not tabstractprocdef(right.resultdef).is_addressonly then
  3356. maybe_load_in_temp(right);
  3357. if not assigned(right) then
  3358. begin
  3359. if assigned(procdefinition.owner.defowner) then
  3360. begin
  3361. if paramanager.can_opt_unused_para(currpara) then
  3362. { If parentfp is unused by the target proc, create a dummy
  3363. pointerconstnode which will be discarded later. }
  3364. hiddentree:=cpointerconstnode.create(0,currpara.vardef)
  3365. else
  3366. begin
  3367. hiddentree:=cloadparentfpnode.create(tprocdef(procdefinition.owner.defowner),lpf_forpara);
  3368. if is_nested_pd(current_procinfo.procdef) then
  3369. current_procinfo.set_needs_parentfp(tprocdef(procdefinition.owner.defowner).parast.symtablelevel);
  3370. end;
  3371. end
  3372. { exceptfilters called from main level are not owned }
  3373. else if procdefinition.proctypeoption=potype_exceptfilter then
  3374. hiddentree:=cloadparentfpnode.create(current_procinfo.procdef,lpf_forpara)
  3375. else
  3376. internalerror(200309287);
  3377. end
  3378. else if not(po_is_block in procdefinition.procoptions) then
  3379. hiddentree:=gen_procvar_context_tree_parentfp
  3380. else
  3381. hiddentree:=gen_block_context
  3382. end
  3383. else
  3384. hiddentree:=cnothingnode.create;
  3385. pt:=ccallparanode.create(hiddentree,oldppt^);
  3386. oldppt^:=pt;
  3387. end;
  3388. if not assigned(pt) then
  3389. internalerror(200310052);
  3390. pt.parasym:=currpara;
  3391. oldppt:=pcallparanode(@pt.right);
  3392. pt:=tcallparanode(pt.right);
  3393. end;
  3394. { Create parasyms for varargs, first count the number of varargs paras,
  3395. then insert the parameters with numbering in reverse order. The SortParas
  3396. will set the correct order at the end}
  3397. pt:=tcallparanode(left);
  3398. i:=0;
  3399. while assigned(pt) do
  3400. begin
  3401. if cpf_varargs_para in pt.callparaflags then
  3402. inc(i);
  3403. pt:=tcallparanode(pt.right);
  3404. end;
  3405. if (i>0) then
  3406. begin
  3407. include(current_procinfo.flags,pi_calls_c_varargs);
  3408. varargsparas:=tvarargsparalist.create;
  3409. pt:=tcallparanode(left);
  3410. while assigned(pt) do
  3411. begin
  3412. if cpf_varargs_para in pt.callparaflags then
  3413. begin
  3414. varargspara:=cparavarsym.create('va'+tostr(i),i,vs_value,pt.resultdef,[]);
  3415. dec(i);
  3416. { varargspara is left-right, use insert
  3417. instead of concat }
  3418. varargsparas.add(varargspara);
  3419. pt.parasym:=varargspara;
  3420. end;
  3421. pt:=tcallparanode(pt.right);
  3422. end;
  3423. varargsparas.sortparas;
  3424. end;
  3425. end;
  3426. function tcallnode.pass_typecheck:tnode;
  3427. function is_undefined_recursive(def:tdef):boolean;
  3428. begin
  3429. { might become more refined in the future }
  3430. if def.typ=undefineddef then
  3431. result:=true
  3432. else if def.typ=arraydef then
  3433. result:=is_undefined_recursive(tarraydef(def).elementdef)
  3434. else
  3435. result:=false;
  3436. end;
  3437. var
  3438. candidates : tcallcandidates;
  3439. oldcallnode : tcallnode;
  3440. hpt,tmp : tnode;
  3441. pt : tcallparanode;
  3442. lastpara : longint;
  3443. paraidx,
  3444. cand_cnt : integer;
  3445. i : longint;
  3446. ignoregenericparacall,
  3447. ignorevisibility,
  3448. is_const : boolean;
  3449. statements : tstatementnode;
  3450. converted_result_data : ttempcreatenode;
  3451. calltype: tdispcalltype;
  3452. invokesym : tsym;
  3453. begin
  3454. result:=nil;
  3455. candidates:=nil;
  3456. oldcallnode:=aktcallnode;
  3457. aktcallnode:=self;
  3458. try
  3459. { determine length of parameter list }
  3460. pt:=tcallparanode(left);
  3461. paralength:=0;
  3462. while assigned(pt) do
  3463. begin
  3464. inc(paralength);
  3465. pt:=tcallparanode(pt.right);
  3466. end;
  3467. { determine the type of the parameters }
  3468. if assigned(left) then
  3469. begin
  3470. tcallparanode(left).get_paratype;
  3471. if codegenerror then
  3472. exit;
  3473. end;
  3474. if assigned(methodpointer) then
  3475. typecheckpass(methodpointer);
  3476. { procedure variable ? }
  3477. if assigned(right) then
  3478. begin
  3479. set_varstate(right,vs_read,[vsf_must_be_valid]);
  3480. typecheckpass(right);
  3481. if codegenerror then
  3482. exit;
  3483. if is_invokable(right.resultdef) then
  3484. begin
  3485. procdefinition:=get_invoke_procdef(tobjectdef(right.resultdef));
  3486. if assigned(methodpointer) then
  3487. internalerror(2021041004);
  3488. methodpointer:=right;
  3489. { don't convert again when this is used as the self parameter }
  3490. include(right.flags,nf_load_procvar);
  3491. right:=nil;
  3492. end
  3493. else
  3494. procdefinition:=tabstractprocdef(right.resultdef);
  3495. { Compare parameters from right to left }
  3496. paraidx:=procdefinition.Paras.count-1;
  3497. { Skip default parameters }
  3498. if not(po_varargs in procdefinition.procoptions) then
  3499. begin
  3500. { ignore hidden parameters }
  3501. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) do
  3502. dec(paraidx);
  3503. for i:=1 to procdefinition.maxparacount-paralength do
  3504. begin
  3505. if paraidx<0 then
  3506. internalerror(200402265);
  3507. if not assigned(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym) then
  3508. begin
  3509. CGMessage1(parser_e_wrong_parameter_size,'<Procedure Variable>');
  3510. exit;
  3511. end;
  3512. dec(paraidx);
  3513. end;
  3514. end;
  3515. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) do
  3516. dec(paraidx);
  3517. pt:=tcallparanode(left);
  3518. lastpara:=paralength;
  3519. while (paraidx>=0) and assigned(pt) do
  3520. begin
  3521. { only goto next para if we're out of the varargs }
  3522. if not(po_varargs in procdefinition.procoptions) or
  3523. (lastpara<=procdefinition.maxparacount) then
  3524. begin
  3525. repeat
  3526. dec(paraidx);
  3527. until (paraidx<0) or not(vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions);
  3528. end;
  3529. pt:=tcallparanode(pt.right);
  3530. dec(lastpara);
  3531. end;
  3532. if assigned(pt) or
  3533. ((paraidx>=0) and
  3534. not assigned(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym)) then
  3535. begin
  3536. if assigned(pt) then
  3537. current_filepos:=pt.fileinfo;
  3538. CGMessage1(parser_e_wrong_parameter_size,'<Procedure Variable>');
  3539. exit;
  3540. end;
  3541. end
  3542. else
  3543. { not a procedure variable }
  3544. begin
  3545. { do we know the procedure to call ? }
  3546. if not(assigned(procdefinition)) then
  3547. begin
  3548. { according to bug reports 32539 and 20551, real variant of sqr/abs should be used when they are called for variants to be
  3549. delphi compatible, this is in contrast to normal overloading behaviour, so fix this by a terrible hack to be compatible }
  3550. if assigned(left) and assigned(tcallparanode(left).left) and
  3551. (tcallparanode(left).left.resultdef.typ=variantdef) and assigned(symtableproc.name) and (symtableproc.name^='SYSTEM') then
  3552. begin
  3553. if symtableprocentry.Name='SQR' then
  3554. begin
  3555. result:=cinlinenode.createintern(in_sqr_real,false,tcallparanode(left).left.getcopy);
  3556. exit;
  3557. end;
  3558. if symtableprocentry.Name='ABS' then
  3559. begin
  3560. result:=cinlinenode.createintern(in_abs_real,false,tcallparanode(left).left.getcopy);
  3561. exit;
  3562. end;
  3563. end;
  3564. { ignore possible private for properties or in delphi mode for anon. inherited (FK) }
  3565. ignorevisibility:=(nf_isproperty in flags) or
  3566. ((m_delphi in current_settings.modeswitches) and (cnf_anon_inherited in callnodeflags)) or
  3567. (cnf_ignore_visibility in callnodeflags);
  3568. candidates:=tcallcandidates.create(symtableprocentry,symtableproc,left,ignorevisibility,
  3569. not(nf_isproperty in flags),cnf_objc_id_call in callnodeflags,cnf_unit_specified in callnodeflags,
  3570. callnodeflags*[cnf_anon_inherited,cnf_inherited]=[],cnf_anon_inherited in callnodeflags,spezcontext);
  3571. { no procedures found? then there is something wrong
  3572. with the parameter size or the procedures are
  3573. not accessible }
  3574. if candidates.count=0 then
  3575. begin
  3576. { when it's an auto inherited call and there
  3577. is no procedure found, but the procedures
  3578. were defined with overload directive and at
  3579. least two procedures are defined then we ignore
  3580. this inherited by inserting a nothingn. Only
  3581. do this ugly hack in Delphi mode as it looks more
  3582. like a bug. It's also not documented }
  3583. if (m_delphi in current_settings.modeswitches) and
  3584. (cnf_anon_inherited in callnodeflags) and
  3585. (symtableprocentry.owner.symtabletype=ObjectSymtable) and
  3586. (po_overload in tprocdef(symtableprocentry.ProcdefList[0]).procoptions) and
  3587. (symtableprocentry.ProcdefList.Count>=2) then
  3588. result:=cnothingnode.create
  3589. else
  3590. begin
  3591. { in tp mode we can try to convert to procvar if
  3592. there are no parameters specified }
  3593. if not(assigned(left)) and
  3594. ([cnf_inherited,cnf_no_convert_procvar]*callnodeflags=[]) and
  3595. ((m_tp_procvar in current_settings.modeswitches) or
  3596. (m_mac_procvar in current_settings.modeswitches)) and
  3597. (not assigned(methodpointer) or
  3598. (methodpointer.nodetype <> typen)) then
  3599. begin
  3600. hpt:=cloadnode.create(tprocsym(symtableprocentry),symtableproc);
  3601. if assigned(methodpointer) then
  3602. tloadnode(hpt).set_mp(methodpointer.getcopy);
  3603. typecheckpass(hpt);
  3604. result:=hpt;
  3605. end
  3606. else
  3607. begin
  3608. CGMessagePos1(fileinfo,parser_e_wrong_parameter_size,symtableprocentry.realname);
  3609. symtableprocentry.write_parameter_lists(nil);
  3610. end;
  3611. end;
  3612. candidates.free;
  3613. exit;
  3614. end;
  3615. { Retrieve information about the candidates }
  3616. candidates.get_information;
  3617. {$ifdef EXTDEBUG}
  3618. { Display info when multiple candidates are found }
  3619. if candidates.count>1 then
  3620. candidates.dump_info(V_Debug);
  3621. {$endif EXTDEBUG}
  3622. { Choose the best candidate and count the number of
  3623. candidates left }
  3624. cand_cnt:=candidates.choose_best(procdefinition,
  3625. assigned(left) and
  3626. not assigned(tcallparanode(left).right) and
  3627. (tcallparanode(left).left.resultdef.typ=variantdef));
  3628. { All parameters are checked, check if there are any
  3629. procedures left }
  3630. if cand_cnt>0 then
  3631. begin
  3632. { Multiple candidates left? }
  3633. if cand_cnt>1 then
  3634. begin
  3635. { if we're inside a generic and call another function
  3636. with generic types as arguments we don't complain in
  3637. the generic, but only during the specialization }
  3638. ignoregenericparacall:=false;
  3639. if assigned(current_procinfo) and (df_generic in current_procinfo.procdef.defoptions) then
  3640. begin
  3641. pt:=tcallparanode(left);
  3642. while assigned(pt) do
  3643. begin
  3644. if is_undefined_recursive(pt.resultdef) then
  3645. begin
  3646. ignoregenericparacall:=true;
  3647. break;
  3648. end;
  3649. pt:=tcallparanode(pt.right);
  3650. end;
  3651. end;
  3652. if not ignoregenericparacall then
  3653. begin
  3654. CGMessage(type_e_cant_choose_overload_function);
  3655. {$ifdef EXTDEBUG}
  3656. candidates.dump_info(V_Hint);
  3657. {$else EXTDEBUG}
  3658. candidates.list(false);
  3659. {$endif EXTDEBUG}
  3660. end;
  3661. { we'll just use the first candidate to make the
  3662. call }
  3663. end;
  3664. { assign procdefinition }
  3665. if symtableproc=nil then
  3666. symtableproc:=procdefinition.owner;
  3667. end
  3668. else
  3669. begin
  3670. { No candidates left, this must be a type error,
  3671. because wrong size is already checked. procdefinition
  3672. is filled with the first (random) definition that is
  3673. found. We use this definition to display a nice error
  3674. message that the wrong type is passed }
  3675. candidates.find_wrong_para;
  3676. candidates.list(true);
  3677. {$ifdef EXTDEBUG}
  3678. candidates.dump_info(V_Hint);
  3679. {$endif EXTDEBUG}
  3680. { We can not proceed, release all procs and exit }
  3681. candidates.free;
  3682. exit;
  3683. end;
  3684. { if the final procedure definition is not yet owned,
  3685. ensure that it is }
  3686. procdefinition.register_def;
  3687. if procdefinition.is_specialization and (procdefinition.typ=procdef) then
  3688. maybe_add_pending_specialization(procdefinition,candidates.para_anon_syms);
  3689. candidates.free;
  3690. end; { end of procedure to call determination }
  3691. end;
  3692. if procdefinition.typ = procdef then
  3693. begin
  3694. { check for hints (deprecated etc) }
  3695. check_hints(tprocdef(procdefinition).procsym,tprocdef(procdefinition).symoptions,tprocdef(procdefinition).deprecatedmsg);
  3696. { add reference to corresponding procsym; may not be the one
  3697. originally found/passed to the constructor because of overloads }
  3698. addsymref(tprocdef(procdefinition).procsym,procdefinition);
  3699. { ensure that the generic is considered as used as for an
  3700. implicit specialization must only be called after the final
  3701. overload was picked }
  3702. if assigned(tprocdef(procdefinition).genericdef) and
  3703. assigned(tprocdef(tprocdef(procdefinition).genericdef).procsym) and
  3704. (tprocdef(tprocdef(procdefinition).genericdef).procsym.refs=0) then
  3705. addsymref(tprocdef(tprocdef(procdefinition).genericdef).procsym);
  3706. end;
  3707. { add needed default parameters }
  3708. if (paralength<procdefinition.maxparacount) then
  3709. begin
  3710. paraidx:=0;
  3711. i:=0;
  3712. while (i<paralength) do
  3713. begin
  3714. if paraidx>=procdefinition.Paras.count then
  3715. internalerror(200306181);
  3716. if not(vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) then
  3717. inc(i);
  3718. inc(paraidx);
  3719. end;
  3720. while (paraidx<procdefinition.paras.count) and (vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) do
  3721. inc(paraidx);
  3722. while (paraidx<procdefinition.paras.count) do
  3723. begin
  3724. if not assigned(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym) then
  3725. internalerror(200212142);
  3726. left:=ccallparanode.create(genconstsymtree(
  3727. tconstsym(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym)),left);
  3728. { Ignore vs_hidden parameters }
  3729. repeat
  3730. inc(paraidx);
  3731. until (paraidx>=procdefinition.paras.count) or
  3732. not(vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions);
  3733. end;
  3734. end;
  3735. { recursive call? }
  3736. if assigned(current_procinfo) and
  3737. (procdefinition=current_procinfo.procdef) then
  3738. include(current_procinfo.flags,pi_is_recursive);
  3739. { handle predefined procedures }
  3740. is_const:=(po_internconst in procdefinition.procoptions) and
  3741. ((block_type in [bt_const,bt_type,bt_const_type,bt_var_type]) or
  3742. (assigned(left) and ((tcallparanode(left).left.nodetype in [realconstn,ordconstn])
  3743. and (not assigned(tcallparanode(left).right) or (tcallparanode(tcallparanode(left).right).left.nodetype in [realconstn,ordconstn])))));
  3744. if (procdefinition.proccalloption=pocall_internproc) or is_const then
  3745. begin
  3746. if assigned(left) then
  3747. begin
  3748. { convert types to those of the prototype, this is required by functions like ror, rol, sar
  3749. some use however a dummy type (Typedfile) so this would break them }
  3750. if not(tinlinenumber(tprocdef(procdefinition).extnumber) in
  3751. [in_Reset_TypedFile,in_Rewrite_TypedFile,in_reset_typedfile_name,in_rewrite_typedfile_name]) then
  3752. begin
  3753. { bind parasyms to the callparanodes and insert hidden parameters }
  3754. bind_parasym;
  3755. { insert type conversions for parameters }
  3756. if assigned(left) then
  3757. tcallparanode(left).insert_typeconv;
  3758. end;
  3759. { ptr and settextbuf need two args }
  3760. if assigned(tcallparanode(left).right) then
  3761. begin
  3762. hpt:=geninlinenode(tinlinenumber(tprocdef(procdefinition).extnumber),is_const,left);
  3763. left:=nil;
  3764. end
  3765. else
  3766. begin
  3767. hpt:=geninlinenode(tinlinenumber(tprocdef(procdefinition).extnumber),is_const,tcallparanode(left).left);
  3768. tcallparanode(left).left:=nil;
  3769. end;
  3770. end
  3771. else
  3772. hpt:=geninlinenode(tinlinenumber(tprocdef(procdefinition).extnumber),is_const,nil);
  3773. result:=hpt;
  3774. exit;
  3775. end;
  3776. { in case this is an Objective-C message that returns a related object type by convention,
  3777. override the default result type }
  3778. if po_objc_related_result_type in procdefinition.procoptions then
  3779. begin
  3780. { don't crash in case of syntax errors }
  3781. if assigned(methodpointer) then
  3782. begin
  3783. include(callnodeflags,cnf_typedefset);
  3784. typedef:=methodpointer.resultdef;
  3785. if typedef.typ=classrefdef then
  3786. typedef:=tclassrefdef(typedef).pointeddef;
  3787. end;
  3788. end;
  3789. { ensure that the result type is set }
  3790. if not(cnf_typedefset in callnodeflags) then
  3791. begin
  3792. { constructors return their current class type, not the type where the
  3793. constructor is declared, this can be different because of inheritance }
  3794. if (procdefinition.proctypeoption=potype_constructor) and
  3795. assigned(methodpointer) and
  3796. assigned(methodpointer.resultdef) and
  3797. (methodpointer.resultdef.typ=classrefdef) then
  3798. resultdef:=tclassrefdef(methodpointer.resultdef).pointeddef
  3799. else
  3800. { Member call to a (inherited) constructor from the class, the return
  3801. value is always self, so we change it to voidtype to generate an
  3802. error and to prevent users from generating non-working code
  3803. when they expect to clone the current instance, see bug 3662 (PFV) }
  3804. if (procdefinition.proctypeoption=potype_constructor) and
  3805. is_class(tprocdef(procdefinition).struct) and
  3806. assigned(methodpointer) and
  3807. (methodpointer.nodetype=loadn) and
  3808. (loadnf_is_self in tloadnode(methodpointer).loadnodeflags) then
  3809. resultdef:=voidtype
  3810. else
  3811. resultdef:=procdefinition.returndef;
  3812. end
  3813. else
  3814. resultdef:=typedef;
  3815. { Check object/class for methods }
  3816. if assigned(methodpointer) then
  3817. begin
  3818. { direct call to inherited abstract method, then we
  3819. can already give a error in the compiler instead
  3820. of a runtime error }
  3821. if (cnf_inherited in callnodeflags) and
  3822. (po_abstractmethod in procdefinition.procoptions) then
  3823. begin
  3824. if (m_delphi in current_settings.modeswitches) and
  3825. (cnf_anon_inherited in callnodeflags) then
  3826. begin
  3827. CGMessage(cg_h_inherited_ignored);
  3828. result:=cnothingnode.create;
  3829. exit;
  3830. end
  3831. else
  3832. CGMessage(cg_e_cant_call_abstract_method);
  3833. end;
  3834. { directly calling an interface/protocol/category/class helper
  3835. method via its type is not possible (always must be called via
  3836. the actual instance) }
  3837. if (methodpointer.nodetype=typen) and
  3838. ((
  3839. is_interface(methodpointer.resultdef) and not
  3840. is_objectpascal_helper(tdef(procdefinition.owner.defowner))
  3841. ) or
  3842. is_objc_protocol_or_category(methodpointer.resultdef)) then
  3843. CGMessage1(type_e_class_type_expected,methodpointer.resultdef.typename);
  3844. { if an inherited con- or destructor should be }
  3845. { called in a con- or destructor then a warning }
  3846. { will be made }
  3847. { con- and destructors need a pointer to the vmt }
  3848. if (cnf_inherited in callnodeflags) and
  3849. (procdefinition.proctypeoption in [potype_constructor,potype_destructor]) and
  3850. is_object(methodpointer.resultdef) and
  3851. not(current_procinfo.procdef.proctypeoption in [potype_constructor,potype_destructor]) then
  3852. CGMessage(cg_w_member_cd_call_from_method);
  3853. if methodpointer.nodetype<>typen then
  3854. begin
  3855. { if the value a type helper works on is a derefentiation (before
  3856. removing postix operators) we need to pass the original pointer
  3857. as Self as the Self value might be changed by the helper }
  3858. if is_objectpascal_helper(tdef(procdefinition.owner.defowner)) and
  3859. not is_implicit_pointer_object_type(tobjectdef(procdefinition.owner.defowner).extendeddef) then
  3860. begin
  3861. hpt:=methodpointer;
  3862. hpt:=actualtargetnode(@hpt)^;
  3863. if hpt.nodetype=derefn then
  3864. begin
  3865. tmp:=tderefnode(hpt).left;
  3866. tderefnode(hpt).left:=nil;
  3867. methodpointer.free;
  3868. methodpointer:=tmp;
  3869. end;
  3870. end;
  3871. hpt:=methodpointer;
  3872. { Remove all postfix operators }
  3873. while assigned(hpt) and (hpt.nodetype in [subscriptn,vecn]) do
  3874. hpt:=tunarynode(hpt).left;
  3875. if ((hpt.nodetype=loadvmtaddrn) or
  3876. ((hpt.nodetype=loadn) and assigned(tloadnode(hpt).resultdef) and (tloadnode(hpt).resultdef.typ=classrefdef))) and
  3877. not (procdefinition.proctypeoption=potype_constructor) and
  3878. not (po_classmethod in procdefinition.procoptions) and
  3879. not (po_staticmethod in procdefinition.procoptions) then
  3880. { error: we are calling instance method from the class method/static method }
  3881. CGMessage(parser_e_only_class_members);
  3882. if (procdefinition.proctypeoption=potype_constructor) and
  3883. assigned(symtableproc) and
  3884. (symtableproc.symtabletype=withsymtable) and
  3885. (tnode(twithsymtable(symtableproc).withrefnode).nodetype=temprefn) then
  3886. CGmessage(cg_e_cannot_call_cons_dest_inside_with);
  3887. { skip (absolute and other simple) type conversions -- only now,
  3888. because the checks above have to take type conversions into
  3889. e.g. class reference types account }
  3890. hpt:=actualtargetnode(@hpt)^;
  3891. { R.Init then R will be initialized by the constructor,
  3892. Also allow it for simple loads }
  3893. if (procdefinition.proctypeoption=potype_constructor) or
  3894. ((hpt.nodetype=loadn) and
  3895. (((methodpointer.resultdef.typ=objectdef) and
  3896. not(oo_has_virtual in tobjectdef(methodpointer.resultdef).objectoptions)) or
  3897. (methodpointer.resultdef.typ=recorddef)
  3898. )
  3899. ) then
  3900. { a constructor will and a method may write something to }
  3901. { the fields }
  3902. set_varstate(methodpointer,vs_readwritten,[])
  3903. else
  3904. set_varstate(methodpointer,vs_read,[vsf_must_be_valid]);
  3905. end;
  3906. { if we are calling the constructor check for abstract
  3907. methods. Ignore inherited and member calls, because the
  3908. class is then already created }
  3909. if (procdefinition.proctypeoption=potype_constructor) and
  3910. not(cnf_inherited in callnodeflags) and
  3911. not(cnf_member_call in callnodeflags) then
  3912. verifyabstractcalls;
  3913. end
  3914. else
  3915. begin
  3916. { When this is method the methodpointer must be available }
  3917. if (right=nil) and
  3918. (procdefinition.owner.symtabletype in [ObjectSymtable,recordsymtable]) and
  3919. not procdefinition.no_self_node then
  3920. internalerror(200305061);
  3921. end;
  3922. { Set flag that the procedure uses varargs, also if they are not passed it is still
  3923. needed for x86_64 to pass the number of SSE registers used }
  3924. if po_varargs in procdefinition.procoptions then
  3925. include(callnodeflags,cnf_uses_varargs);
  3926. { set the appropriate node flag if the call never returns }
  3927. if po_noreturn in procdefinition.procoptions then
  3928. include(callnodeflags,cnf_call_never_returns);
  3929. { Change loading of array of const to varargs }
  3930. if assigned(left) and
  3931. is_array_of_const(tparavarsym(procdefinition.paras[procdefinition.paras.count-1]).vardef) and
  3932. (procdefinition.proccalloption in cdecl_pocalls) then
  3933. convert_carg_array_of_const;
  3934. { bind parasyms to the callparanodes and insert hidden parameters }
  3935. bind_parasym;
  3936. { insert type conversions for parameters }
  3937. if assigned(left) then
  3938. tcallparanode(left).insert_typeconv;
  3939. { dispinterface methode invoke? }
  3940. if assigned(methodpointer) and is_dispinterface(methodpointer.resultdef) then
  3941. begin
  3942. case procdefinition.proctypeoption of
  3943. potype_propgetter: calltype:=dct_propget;
  3944. potype_propsetter: calltype:=dct_propput;
  3945. else
  3946. calltype:=dct_method;
  3947. end;
  3948. { if the result is used, we've to insert a call to convert the type to be on the "safe side" }
  3949. if (cnf_return_value_used in callnodeflags) and not is_void(procdefinition.returndef) then
  3950. begin
  3951. result:=internalstatements(statements);
  3952. converted_result_data:=ctempcreatenode.create(procdefinition.returndef,sizeof(procdefinition.returndef),
  3953. tt_persistent,true);
  3954. addstatement(statements,converted_result_data);
  3955. addstatement(statements,cassignmentnode.create(ctemprefnode.create(converted_result_data),
  3956. ctypeconvnode.create_internal(
  3957. translate_disp_call(methodpointer,parameters,calltype,'',tprocdef(procdefinition).dispid,procdefinition.returndef),
  3958. procdefinition.returndef)));
  3959. addstatement(statements,ctempdeletenode.create_normal_temp(converted_result_data));
  3960. addstatement(statements,ctemprefnode.create(converted_result_data));
  3961. end
  3962. else
  3963. result:=translate_disp_call(methodpointer,parameters,calltype,'',tprocdef(procdefinition).dispid,voidtype);
  3964. { don't free reused nodes }
  3965. methodpointer:=nil;
  3966. parameters:=nil;
  3967. end;
  3968. maybe_gen_call_self_node;
  3969. if assigned(call_self_node) then
  3970. typecheckpass(call_self_node);
  3971. if assigned(call_vmt_node) then
  3972. typecheckpass(call_vmt_node);
  3973. if assigned(current_procinfo) and
  3974. (procdefinition.typ=procdef) and
  3975. (procdefinition.parast.symtablelevel<=current_procinfo.procdef.parast.symtablelevel) and
  3976. (procdefinition.parast.symtablelevel>normal_function_level) and
  3977. (current_procinfo.procdef.parast.symtablelevel>normal_function_level) then
  3978. current_procinfo.add_captured_sym(tprocdef(procdefinition).procsym,procdefinition,fileinfo);
  3979. finally
  3980. aktcallnode:=oldcallnode;
  3981. end;
  3982. end;
  3983. function tcallnode.simplify(forinline : boolean) : tnode;
  3984. begin
  3985. { See if there's any special handling we can do based on the intrinsic code }
  3986. if (intrinsiccode <> Default(TInlineNumber)) then
  3987. result := handle_compilerproc
  3988. else
  3989. result := nil;
  3990. end;
  3991. procedure tcallnode.order_parameters;
  3992. var
  3993. hp,hpcurr,hpnext,hpfirst,hpprev : tcallparanode;
  3994. currloc : tcgloc;
  3995. indexcount: Integer;
  3996. begin
  3997. indexcount:=0;
  3998. hpfirst:=nil;
  3999. hpcurr:=tcallparanode(left);
  4000. { cache all info about parameters containing stack tainting calls,
  4001. since we will need it a lot below and calculting it can be expensive }
  4002. while assigned(hpcurr) do
  4003. begin
  4004. { Also remember the original parameter order for the sake of
  4005. tcallnode.simplify }
  4006. if hpcurr.originalindex = -1 then
  4007. begin
  4008. hpcurr.originalindex := indexcount;
  4009. Inc(indexcount);
  4010. end;
  4011. hpcurr.init_contains_stack_tainting_call_cache;
  4012. hpcurr:=tcallparanode(hpcurr.right);
  4013. end;
  4014. hpcurr:=tcallparanode(left);
  4015. while assigned(hpcurr) do
  4016. begin
  4017. { pull out }
  4018. hpnext:=tcallparanode(hpcurr.right);
  4019. { pull in at the correct place.
  4020. Used order:
  4021. 1. vs_out for a reference-counted type
  4022. 2. LOC_REFERENCE with smallest offset (i386 only)
  4023. 3. LOC_REFERENCE with least complexity (non-i386 only)
  4024. 4. LOC_REFERENCE with most complexity (non-i386 only)
  4025. 5. LOC_REGISTER with most complexity
  4026. 6. LOC_REGISTER with least complexity
  4027. For the moment we only look at the first parameter field. Combining it
  4028. with multiple parameter fields will make things a lot complexer (PFV)
  4029. The reason for the difference regarding complexity ordering
  4030. between LOC_REFERENCE and LOC_REGISTER is mainly for calls:
  4031. we first want to treat the LOC_REFERENCE destinations whose
  4032. calculation does not require a call, because their location
  4033. may contain registers which might otherwise have to be saved
  4034. if a call has to be evaluated first. The calculated value is
  4035. stored on the stack and will thus no longer occupy any
  4036. register.
  4037. Similarly, for the register parameters we first want to
  4038. evaluate the calls, because otherwise the already loaded
  4039. register parameters will have to be saved so the intermediate
  4040. call can be evaluated (JM) }
  4041. if not assigned(hpcurr.parasym.paraloc[callerside].location) then
  4042. internalerror(200412152);
  4043. currloc:=hpcurr.parasym.paraloc[callerside].location^.loc;
  4044. hpprev:=nil;
  4045. hp:=hpfirst;
  4046. { on fixed_stack targets, always evaluate parameters containing
  4047. a call with stack parameters before all other parameters,
  4048. because they will prevent any other parameters from being put
  4049. in their final place; if both the current and the next para
  4050. contain a stack tainting call, don't do anything to prevent
  4051. them from keeping on chasing eachother's tail }
  4052. while assigned(hp) do
  4053. begin
  4054. if paramanager.use_fixed_stack and
  4055. hpcurr.contains_stack_tainting_call_cached then
  4056. break;
  4057. case currloc of
  4058. LOC_REFERENCE :
  4059. begin
  4060. case hp.parasym.paraloc[callerside].location^.loc of
  4061. LOC_REFERENCE :
  4062. begin
  4063. { Offset is calculated like:
  4064. sub esp,12
  4065. mov [esp+8],para3
  4066. mov [esp+4],para2
  4067. mov [esp],para1
  4068. call function
  4069. That means the for pushes the para with the
  4070. highest offset (see para3) needs to be pushed first
  4071. }
  4072. {$if defined(i386) or defined(i8086) or defined(m68k) or defined(z80)}
  4073. { the i386, i8086, m68k, z80 and jvm code generators expect all reference }
  4074. { parameters to be in this order so they can use }
  4075. { pushes in case of no fixed stack }
  4076. if (not paramanager.use_fixed_stack and
  4077. (hpcurr.parasym.paraloc[callerside].location^.reference.offset>
  4078. hp.parasym.paraloc[callerside].location^.reference.offset)) or
  4079. (paramanager.use_fixed_stack and
  4080. (node_complexity(hpcurr.left)<node_complexity(hp.left))) then
  4081. {$elseif defined(jvm) or defined(wasm)}
  4082. if (hpcurr.parasym.paraloc[callerside].location^.reference.offset<hp.parasym.paraloc[callerside].location^.reference.offset) then
  4083. {$else jvm}
  4084. if (node_complexity(hpcurr.left)<node_complexity(hp.left)) then
  4085. {$endif jvm}
  4086. break;
  4087. end;
  4088. LOC_MMREGISTER,
  4089. LOC_REGISTER,
  4090. LOC_FPUREGISTER :
  4091. break;
  4092. else
  4093. ;
  4094. end;
  4095. end;
  4096. LOC_MMREGISTER,
  4097. LOC_FPUREGISTER,
  4098. LOC_REGISTER :
  4099. begin
  4100. if (hp.parasym.paraloc[callerside].location^.loc<>LOC_REFERENCE) and
  4101. (node_complexity(hpcurr.left)>node_complexity(hp.left)) then
  4102. break;
  4103. end;
  4104. else
  4105. ;
  4106. end;
  4107. hpprev:=hp;
  4108. hp:=tcallparanode(hp.right);
  4109. end;
  4110. hpcurr.right:=hp;
  4111. if assigned(hpprev) then
  4112. hpprev.right:=hpcurr
  4113. else
  4114. hpfirst:=hpcurr;
  4115. { next }
  4116. hpcurr:=hpnext;
  4117. end;
  4118. left:=hpfirst;
  4119. { now mark each parameter that is followed by a stack-tainting call,
  4120. to determine on use_fixed_stack targets which ones can immediately be
  4121. put in their final destination. Unforunately we can never put register
  4122. parameters immediately in their final destination (even on register-
  4123. rich architectures such as the PowerPC), because the code generator
  4124. can still insert extra calls that only make use of register
  4125. parameters (fpc_move() etc. }
  4126. hpcurr:=hpfirst;
  4127. while assigned(hpcurr) do
  4128. begin
  4129. if hpcurr.contains_stack_tainting_call_cached then
  4130. begin
  4131. { all parameters before this one are followed by a stack
  4132. tainting call }
  4133. hp:=hpfirst;
  4134. while hp<>hpcurr do
  4135. begin
  4136. hp.ffollowed_by_stack_tainting_call_cached:=true;
  4137. hp:=tcallparanode(hp.right);
  4138. end;
  4139. hpfirst:=hpcurr;
  4140. end;
  4141. hpcurr:=tcallparanode(hpcurr.right);
  4142. end;
  4143. end;
  4144. procedure tcallnode.check_stack_parameters;
  4145. var
  4146. hp : tcallparanode;
  4147. loc : pcgparalocation;
  4148. begin
  4149. hp:=tcallparanode(left);
  4150. while assigned(hp) do
  4151. begin
  4152. if assigned(hp.parasym) then
  4153. begin
  4154. loc:=hp.parasym.paraloc[callerside].location;
  4155. while assigned(loc) do
  4156. begin
  4157. if loc^.loc=LOC_REFERENCE then
  4158. begin
  4159. include(current_procinfo.flags,pi_has_stackparameter);
  4160. exit;
  4161. end;
  4162. loc:=loc^.next;
  4163. end;
  4164. end;
  4165. hp:=tcallparanode(hp.right);
  4166. end;
  4167. end;
  4168. procedure tcallnode.check_inlining;
  4169. var
  4170. st : tsymtable;
  4171. para : tcallparanode;
  4172. begin
  4173. { Can we inline the procedure? }
  4174. if (po_inline in procdefinition.procoptions) and
  4175. (procdefinition.typ=procdef) and
  4176. tprocdef(procdefinition).has_inlininginfo and
  4177. { Prevent too deep inlining recursion and code bloat by inlining
  4178. The actual formuala is
  4179. inlinelevel/3+1 /-------
  4180. node count < -----------------\/ 10000
  4181. This allows exponential grow of the code only to a certain limit.
  4182. Remarks
  4183. - The current approach calculates the inlining level top down, so outer call nodes (nodes closer to the leaf) might not be inlined
  4184. if the max. complexity is reached. This is done because it makes the implementation easier and because
  4185. there might be situations were it is more beneficial to inline inner nodes and do the calls to the outer nodes
  4186. if the outer nodes are in a seldomly used code path
  4187. - The code avoids to use functions from the math unit
  4188. }
  4189. (node_count(tprocdef(procdefinition).inlininginfo^.code)<round(exp((1.0/(inlinelevel/3.0+1))*ln(10000)))) then
  4190. begin
  4191. include(callnodeflags,cnf_do_inline);
  4192. { Check if we can inline the procedure when it references proc/var that
  4193. are not in the globally available }
  4194. st:=procdefinition.owner;
  4195. while (st.symtabletype in [ObjectSymtable,recordsymtable]) do
  4196. st:=st.defowner.owner;
  4197. if not(tf_supports_hidden_symbols in target_info.flags) and
  4198. (pi_uses_static_symtable in tprocdef(procdefinition).inlininginfo^.flags) and
  4199. (st.symtabletype=globalsymtable) and
  4200. (not st.iscurrentunit) then
  4201. begin
  4202. Comment(V_lineinfo+V_Debug,'Not inlining "'+tprocdef(procdefinition).procsym.realname+'", references private symbols from other unit');
  4203. exclude(callnodeflags,cnf_do_inline);
  4204. end;
  4205. para:=tcallparanode(parameters);
  4206. while assigned(para) do
  4207. begin
  4208. if not para.can_be_inlined then
  4209. begin
  4210. Comment(V_lineinfo+V_Debug,'Not inlining "'+tprocdef(procdefinition).procsym.realname+
  4211. '", invocation parameter contains an unsafe/unsupported construct');
  4212. exclude(callnodeflags,cnf_do_inline);
  4213. break;
  4214. end;
  4215. para:=tcallparanode(para.nextpara);
  4216. end;
  4217. end;
  4218. end;
  4219. function tcallnode.pass_1 : tnode;
  4220. procedure mark_unregable_parameters;
  4221. var
  4222. hp : tcallparanode;
  4223. begin
  4224. hp:=tcallparanode(left);
  4225. while assigned(hp) do
  4226. begin
  4227. do_typecheckpass(hp.left);
  4228. { When the address needs to be pushed then the register is
  4229. not regable. Exception is when the location is also a var
  4230. parameter and we can pass the address transparently (but
  4231. that is handled by make_not_regable if ra_addr_regable is
  4232. passed, and make_not_regable always needs to called for
  4233. the ra_addr_taken info for non-invisble parameters) }
  4234. if (not (cpf_varargs_para in hp.callparaflags)) and (
  4235. not(
  4236. (vo_is_hidden_para in hp.parasym.varoptions) and
  4237. (hp.left.resultdef.typ in [pointerdef,classrefdef])
  4238. ) and
  4239. paramanager.push_addr_param(hp.parasym.varspez,hp.parasym.vardef,
  4240. self.procdefinition.proccalloption)
  4241. ) then
  4242. { pushing the address of a variable to take the place of a temp
  4243. as the complex function result of a function does not make its
  4244. address escape the current block, as the "address of the
  4245. function result" is not something which can be stored
  4246. persistently by the callee (it becomes invalid when the callee
  4247. returns) }
  4248. if not(vo_is_funcret in hp.parasym.varoptions) and
  4249. not(po_compilerproc in procdefinition.procoptions) then
  4250. make_not_regable(hp.left,[ra_addr_regable,ra_addr_taken])
  4251. else
  4252. make_not_regable(hp.left,[ra_addr_regable]);
  4253. hp:=tcallparanode(hp.right);
  4254. end;
  4255. end;
  4256. var
  4257. para: tcallparanode;
  4258. oldcallnode: tcallnode;
  4259. begin
  4260. result:=nil;
  4261. oldcallnode:=aktcallnode;
  4262. aktcallnode:=self;
  4263. try
  4264. { as pass_1 is never called on the methodpointer node, we must check
  4265. here that it's not a helper type }
  4266. if assigned(methodpointer) and
  4267. (methodpointer.nodetype=typen) and
  4268. is_objectpascal_helper(ttypenode(methodpointer).typedef) and
  4269. not ttypenode(methodpointer).helperallowed then
  4270. begin
  4271. CGMessage(parser_e_no_category_as_types);
  4272. { we get an internal error when trying to insert the hidden
  4273. parameters in this case }
  4274. exit;
  4275. end;
  4276. { can we get rid of the call? }
  4277. if (cs_opt_remove_empty_proc in current_settings.optimizerswitches) and
  4278. not(cnf_return_value_used in callnodeflags) and
  4279. (procdefinition.typ=procdef) and
  4280. tprocdef(procdefinition).isempty and
  4281. { allow only certain proc options }
  4282. ((tprocdef(procdefinition).procoptions-[po_none,po_classmethod,po_staticmethod,
  4283. po_interrupt,po_iocheck,po_assembler,po_msgstr,po_msgint,po_exports,po_external,po_overload,
  4284. po_nostackframe,po_has_mangledname,po_has_public_name,po_forward,po_global,
  4285. po_inline,po_compilerproc,po_has_importdll,po_has_importname,po_kylixlocal,po_dispid,po_delphi_nested_cc,
  4286. po_rtlproc,po_ignore_for_overload_resolution,po_auto_raised_visibility])=[]) then
  4287. begin
  4288. { check parameters for side effects }
  4289. para:=tcallparanode(left);
  4290. while assigned(para) do
  4291. begin
  4292. if (para.parasym.typ = paravarsym) and
  4293. ((para.parasym.refs>0) or
  4294. { array of consts are converted later on so we need to skip them here
  4295. else no error detection is done }
  4296. is_array_of_const(para.parasym.vardef) or
  4297. not(cs_opt_dead_values in current_settings.optimizerswitches) or
  4298. might_have_sideeffects(para.left)) then
  4299. break;
  4300. para:=tcallparanode(para.right);
  4301. end;
  4302. { finally, remove it if no parameter with side effect has been found }
  4303. if para=nil then
  4304. begin
  4305. result:=cnothingnode.create;
  4306. exit;
  4307. end;
  4308. end;
  4309. { convert Objective-C calls into a message call }
  4310. if (procdefinition.typ=procdef) and
  4311. (po_objc in tprocdef(procdefinition).procoptions) then
  4312. begin
  4313. if not(cnf_objc_processed in callnodeflags) then
  4314. objc_convert_to_message_send;
  4315. end
  4316. else
  4317. begin
  4318. { The following don't apply to obj-c: obj-c methods can never be
  4319. inlined because they're always virtual and the destination can
  4320. change at run, and for the same reason we also can't perform
  4321. WPO on them (+ they have no constructors) }
  4322. { Check if the call can be inlined, sets the cnf_do_inline flag }
  4323. check_inlining;
  4324. { must be called before maybe_load_in_temp(methodpointer), because
  4325. it converts the methodpointer into a temp in case it's a call
  4326. (and we want to know the original call)
  4327. }
  4328. register_created_object_types;
  4329. end;
  4330. { Maybe optimize the loading of the methodpointer using a temp. When the methodpointer
  4331. is a calln this is even required to not execute the calln twice.
  4332. This needs to be done after the resulttype pass, because in the resulttype we can still convert the
  4333. calln to a loadn (PFV) }
  4334. if assigned(methodpointer) then
  4335. maybe_load_in_temp(methodpointer);
  4336. if assigned(right) and (right.resultdef.typ=procvardef) and
  4337. not tabstractprocdef(right.resultdef).is_addressonly then
  4338. maybe_load_in_temp(right);
  4339. { the return value might be stored on the current stack by allocating a temp. }
  4340. if not(paramanager.ret_in_param(procdefinition.returndef,procdefinition)) then
  4341. inc(current_procinfo.estimatedtempsize,procdefinition.returndef.size);
  4342. { Create destination (temp or assignment-variable reuse) for function result if it not yet set }
  4343. maybe_create_funcret_node;
  4344. { Insert the self,vmt,function result in the parameters }
  4345. gen_hidden_parameters;
  4346. { Remove useless nodes from init/final blocks }
  4347. { (simplify depends on typecheck info) }
  4348. if assigned(callinitblock) then
  4349. begin
  4350. typecheckpass(tnode(callinitblock));
  4351. doinlinesimplify(tnode(callinitblock));
  4352. end;
  4353. if assigned(callcleanupblock) then
  4354. begin
  4355. typecheckpass(tnode(callcleanupblock));
  4356. doinlinesimplify(tnode(callcleanupblock));
  4357. end;
  4358. { If a constructor calls another constructor of the same or of an
  4359. inherited class, some targets (jvm) have to generate different
  4360. entry code for the constructor. }
  4361. if (current_procinfo.procdef.proctypeoption=potype_constructor) and
  4362. (procdefinition.typ=procdef) and
  4363. (tprocdef(procdefinition).proctypeoption=potype_constructor) and
  4364. ([cnf_member_call,cnf_inherited] * callnodeflags <> []) then
  4365. current_procinfo.ConstructorCallingConstructor:=true;
  4366. { Continue with checking a normal call or generate the inlined code }
  4367. if cnf_do_inline in callnodeflags then
  4368. result:=pass1_inline
  4369. else
  4370. begin
  4371. if (po_inline in procdefinition.procoptions) and not(po_compilerproc in procdefinition.procoptions) and
  4372. (procdefinition.typ=procdef) and
  4373. not (pio_inline_not_possible in tprocdef(procdefinition).implprocoptions) then
  4374. begin
  4375. Message1(cg_n_no_inline,tprocdef(procdefinition).customprocname([pno_proctypeoption, pno_paranames,pno_ownername, pno_noclassmarker, pno_prettynames]));
  4376. end;
  4377. mark_unregable_parameters;
  4378. result:=pass1_normal;
  4379. end;
  4380. finally
  4381. aktcallnode:=oldcallnode;
  4382. end;
  4383. end;
  4384. function tcallnode.pass1_normal : tnode;
  4385. begin
  4386. result:=nil;
  4387. { calculate the parameter info for the procdef }
  4388. procdefinition.init_paraloc_info(callerside);
  4389. { calculate the parameter size needed for this call include varargs if they are available }
  4390. if assigned(varargsparas) then
  4391. pushedparasize:=paramanager.create_varargs_paraloc_info(procdefinition,callerside,varargsparas)
  4392. else
  4393. pushedparasize:=procdefinition.callerargareasize;
  4394. { record maximum parameter size used in this proc }
  4395. current_procinfo.allocate_push_parasize(pushedparasize);
  4396. { check for stacked parameters }
  4397. if assigned(left) and
  4398. (current_settings.optimizerswitches*[cs_opt_stackframe,cs_opt_level1]<>[]) then
  4399. check_stack_parameters;
  4400. if assigned(callinitblock) then
  4401. firstpass(tnode(callinitblock));
  4402. { function result node (tempref or simple load) }
  4403. if assigned(funcretnode) then
  4404. firstpass(funcretnode);
  4405. { parameters }
  4406. if assigned(left) then
  4407. tcallparanode(left).firstcallparan;
  4408. { procedure variable ? }
  4409. if assigned(right) then
  4410. firstpass(right);
  4411. if assigned(methodpointer) and
  4412. (methodpointer.nodetype<>typen) then
  4413. firstpass(methodpointer);
  4414. if assigned(callcleanupblock) then
  4415. firstpass(tnode(callcleanupblock));
  4416. if not (block_type in [bt_const,bt_type,bt_const_type,bt_var_type]) then
  4417. include(current_procinfo.flags,pi_do_call);
  4418. { order parameters }
  4419. order_parameters;
  4420. { get a register for the return value }
  4421. if (not is_void(resultdef)) then
  4422. begin
  4423. if paramanager.ret_in_param(resultdef,procdefinition) then
  4424. begin
  4425. expectloc:=LOC_REFERENCE;
  4426. end
  4427. else
  4428. { ansi/widestrings must be registered, so we can dispose them }
  4429. if is_ansistring(resultdef) or
  4430. is_widestring(resultdef) or
  4431. is_unicodestring(resultdef) then
  4432. begin
  4433. expectloc:=LOC_REFERENCE;
  4434. end
  4435. else
  4436. { we have only to handle the result if it is used }
  4437. if (cnf_return_value_used in callnodeflags) then
  4438. expectloc:=get_expect_loc
  4439. else
  4440. expectloc:=LOC_VOID;
  4441. end
  4442. else
  4443. expectloc:=LOC_VOID;
  4444. { create tree for VMT entry if required }
  4445. gen_vmt_entry_load;
  4446. end;
  4447. {$ifdef state_tracking}
  4448. function Tcallnode.track_state_pass(exec_known:boolean):boolean;
  4449. var hp:Tcallparanode;
  4450. value:Tnode;
  4451. begin
  4452. track_state_pass:=false;
  4453. hp:=Tcallparanode(left);
  4454. while assigned(hp) do
  4455. begin
  4456. if left.track_state_pass(exec_known) then
  4457. begin
  4458. left.resultdef:=nil;
  4459. do_typecheckpass(left);
  4460. end;
  4461. value:=aktstate.find_fact(hp.left);
  4462. if value<>nil then
  4463. begin
  4464. track_state_pass:=true;
  4465. hp.left.destroy;
  4466. hp.left:=value.getcopy;
  4467. do_typecheckpass(hp.left);
  4468. end;
  4469. hp:=Tcallparanode(hp.right);
  4470. end;
  4471. end;
  4472. {$endif}
  4473. {**************************************************************************
  4474. INLINING SUPPORT
  4475. **************************************************************************}
  4476. function tcallnode.replaceparaload(var n: tnode; arg: pointer): foreachnoderesult;
  4477. var
  4478. paras: tcallparanode;
  4479. temp: tnode;
  4480. indexnr : integer;
  4481. begin
  4482. result := fen_false;
  4483. n.fileinfo := pfileposinfo(arg)^;
  4484. if (n.nodetype = loadn) then
  4485. begin
  4486. case tloadnode(n).symtableentry.typ of
  4487. paravarsym :
  4488. begin
  4489. paras := tcallparanode(left);
  4490. while assigned(paras) and
  4491. (paras.parasym <> tloadnode(n).symtableentry) do
  4492. paras := tcallparanode(paras.right);
  4493. if assigned(paras) then
  4494. begin
  4495. temp:=paras.left.getcopy;
  4496. { inherit modification information, this is needed by the dfa/cse }
  4497. temp.flags:=temp.flags+(n.flags*[nf_modify,nf_write,nf_address_taken]);
  4498. n.free;
  4499. n:=temp;
  4500. typecheckpass(n);
  4501. result := fen_true;
  4502. end;
  4503. end;
  4504. localvarsym :
  4505. begin
  4506. { local? }
  4507. if (tloadnode(n).symtableentry.owner <> tprocdef(procdefinition).localst) then
  4508. exit;
  4509. indexnr:=tloadnode(n).symtableentry.owner.SymList.IndexOf(tloadnode(n).symtableentry);
  4510. if (indexnr >= inlinelocals.count) or
  4511. not assigned(inlinelocals[indexnr]) then
  4512. internalerror(20040720);
  4513. temp := tnode(inlinelocals[indexnr]).getcopy;
  4514. { inherit modification information, this is needed by the dfa/cse }
  4515. temp.flags:=temp.flags+(n.flags*[nf_modify,nf_write,nf_address_taken]);
  4516. n.free;
  4517. n:=temp;
  4518. typecheckpass(n);
  4519. result := fen_true;
  4520. end;
  4521. else
  4522. ;
  4523. end;
  4524. end;
  4525. end;
  4526. procedure tcallnode.createlocaltemps(p:TObject;arg:pointer);
  4527. var
  4528. tempnode: ttempcreatenode;
  4529. indexnr : integer;
  4530. begin
  4531. if (TSym(p).typ <> localvarsym) then
  4532. exit;
  4533. indexnr:=TSym(p).Owner.SymList.IndexOf(p);
  4534. if (indexnr >= inlinelocals.count) then
  4535. inlinelocals.count:=indexnr+10;
  4536. if (vo_is_funcret in tabstractvarsym(p).varoptions) then
  4537. begin
  4538. if not assigned(funcretnode) then
  4539. internalerror(200709081);
  4540. inlinelocals[indexnr] := funcretnode.getcopy
  4541. end
  4542. else
  4543. begin
  4544. tempnode :=ctempcreatenode.create(tabstractvarsym(p).vardef,
  4545. tabstractvarsym(p).vardef.size,tt_persistent,tabstractvarsym(p).is_regvar(false));
  4546. addstatement(inlineinitstatement,tempnode);
  4547. if localvartrashing <> -1 then
  4548. cnodeutils.maybe_trash_variable(inlineinitstatement,tabstractnormalvarsym(p),ctemprefnode.create(tempnode));
  4549. addstatement(inlinecleanupstatement,ctempdeletenode.create(tempnode));
  4550. { inherit addr_taken flag }
  4551. if (tabstractvarsym(p).addr_taken) then
  4552. tempnode.includetempflag(ti_addr_taken);
  4553. inlinelocals[indexnr] := ctemprefnode.create(tempnode);
  4554. end;
  4555. end;
  4556. function nonlocalvars(var n: tnode; arg: pointer): foreachnoderesult;
  4557. begin
  4558. result := fen_false;
  4559. { this is just to play it safe, there are more safe situations }
  4560. if (n.nodetype = derefn) or
  4561. ((n.nodetype = loadn) and
  4562. { can be nil in case of internally generated labels like $raiseaddr }
  4563. assigned(tloadnode(n).symtable) and
  4564. { globals and fields of (possibly global) objects could always be changed in the callee }
  4565. ((tloadnode(n).symtable.symtabletype in [globalsymtable,ObjectSymtable]) or
  4566. { statics can only be modified by functions in the same unit }
  4567. ((tloadnode(n).symtable.symtabletype = staticsymtable) and
  4568. (tloadnode(n).symtable = TSymtable(arg))) or
  4569. { if the addr of the symbol is taken somewhere, it can be also non-local }
  4570. ((tloadnode(n).symtableentry.typ in [localvarsym,paravarsym,staticvarsym]) and
  4571. (tabstractvarsym(tloadnode(n).symtableentry).addr_taken))
  4572. )
  4573. ) or
  4574. ((n.nodetype = subscriptn) and
  4575. (tsubscriptnode(n).vs.owner.symtabletype = ObjectSymtable)) then
  4576. result := fen_norecurse_true;
  4577. end;
  4578. function tcallnode.paraneedsinlinetemp(para: tcallparanode; const pushconstaddr, complexpara: boolean): boolean;
  4579. begin
  4580. { if it's an assignable call-by-reference parameter, we cannot pass a
  4581. temp since then the modified valua will be lost }
  4582. if para.parasym.varspez in [vs_var,vs_out] then
  4583. exit(false);
  4584. { We cannot create a formaldef temp and assign something to it }
  4585. if para.parasym.vardef.typ=formaldef then
  4586. exit(false);
  4587. { We don't need temps for parameters that are already temps, except if
  4588. the passed temp could be put in a regvar while the parameter inside
  4589. the routine cannot be (e.g., because its address is taken in the
  4590. routine), or if the temp is a const and the parameter gets modified }
  4591. if (para.left.nodetype=temprefn) and
  4592. (not(ti_may_be_in_reg in ttemprefnode(para.left).tempflags) or
  4593. not(tparavarsym(para.parasym).varregable in [vr_none,vr_addr])) and
  4594. (not(ti_const in ttemprefnode(para.left).tempflags) or
  4595. (tparavarsym(para.parasym).varstate in [vs_initialised,vs_declared,vs_read])) then
  4596. exit(false);
  4597. { We need a temp if the passed value will not be in memory, while
  4598. the parameter inside the routine must be in memory }
  4599. if (tparavarsym(para.parasym).varregable in [vr_none,vr_addr]) and
  4600. not(para.left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  4601. exit(true);
  4602. { We try to handle complex expressions later by taking their address
  4603. and storing this address in a temp (which is then dereferenced when
  4604. the value is used; that doesn't work if we cannot take the address
  4605. of the expression though, in which case we store the result of the
  4606. expression in a temp }
  4607. if (complexpara and not(para.left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE]) or
  4608. (complexpara and
  4609. (not valid_for_addr(para.left,false) or
  4610. (para.left.nodetype=calln) or
  4611. is_constnode(para.left)))) then
  4612. exit(true);
  4613. { Normally, we do not need to create a temp for value parameters that
  4614. are not modified in the inlined function, and neither for const
  4615. parameters that are passed by value.
  4616. However, if we pass a global variable, an object field, a variable
  4617. whose address has been taken, or an expression containing a pointer
  4618. dereference as parameter, this value could be modified in other ways
  4619. as well (even inside the callee) and in such cases we still create a
  4620. temp to be on the safe side.
  4621. We *must not* create a temp for global variables passed by
  4622. reference to a const parameter, because if not inlined then any
  4623. changes to the original value will also be visible in the callee
  4624. (although this is technically undefined behaviour, since with
  4625. "const" the programmer tells the compiler this argument will not
  4626. change). }
  4627. if (((para.parasym.varspez=vs_value) and
  4628. (para.parasym.varstate in [vs_initialised,vs_declared,vs_read])) or
  4629. ((para.parasym.varspez=vs_const) and
  4630. not pushconstaddr)) and
  4631. foreachnodestatic(para.left,@nonlocalvars,pointer(symtableproc)) then
  4632. exit(true);
  4633. { Value parameters of which we know they are modified by definition
  4634. have to be copied to a temp }
  4635. if (para.parasym.varspez=vs_value) and
  4636. not(para.parasym.varstate in [vs_initialised,vs_declared,vs_read]) then
  4637. exit(true);
  4638. { the compiler expects that it can take the address of parameters passed by reference in
  4639. the case of const so we can't replace the node simply by a constant node
  4640. When playing with this code, ensure that
  4641. function f(const a,b : longint) : longint;inline;
  4642. begin
  4643. result:=a*b;
  4644. end;
  4645. [...]
  4646. ...:=f(10,20));
  4647. [...]
  4648. is still folded. (FK)
  4649. }
  4650. if (para.parasym.varspez=vs_const) and
  4651. { const para's can get vs_readwritten if their address is taken ->
  4652. in case they are not passed by reference, to keep the same
  4653. behaviour as without inlining we have to make a copy in case the
  4654. originally passed parameter value gets changed inside the callee
  4655. }
  4656. (not pushconstaddr and
  4657. (para.parasym.varstate=vs_readwritten)
  4658. ) or
  4659. { call-by-reference const's may need to be passed by reference to
  4660. function called in the inlined code }
  4661. (pushconstaddr and
  4662. not valid_for_addr(para.left,false)) then
  4663. exit(true);
  4664. { insert value parameters directly if they are complex instead
  4665. of inserting a reference to the temp.
  4666. - this keeps the node tree simpler
  4667. - alignment is propagated }
  4668. if (para.parasym.varspez=vs_value) and
  4669. complexpara then
  4670. exit(true);
  4671. result:=false;
  4672. end;
  4673. function tcallnode.maybecreateinlineparatemp(para: tcallparanode; out complexpara: boolean): boolean;
  4674. var
  4675. tempnode: ttempcreatenode;
  4676. realtarget: tnode;
  4677. paracomplexity: longint;
  4678. pushconstaddr: boolean;
  4679. begin
  4680. result:=false;
  4681. { determine how a parameter is passed to the inlined body
  4682. There are three options:
  4683. - insert the node tree of the callparanode directly
  4684. If a parameter is used only once, this is the best option if we can do so
  4685. - get the address of the argument, store it in a temp and insert a dereference to this temp
  4686. If the node tree cannot be inserted directly, taking the address of the argument and using it
  4687. is the second best option, but even this is not always possible
  4688. - assign the value of the argument to a newly created temp
  4689. This is the fall back which works always
  4690. Notes:
  4691. - we need to take care that we use the type of the defined parameter and not of the
  4692. passed parameter, because these can be different in case of a formaldef (PFV)
  4693. }
  4694. { pre-compute some values }
  4695. paracomplexity:=node_complexity(para.left);
  4696. if para.parasym.varspez=vs_const then
  4697. pushconstaddr:=paramanager.push_addr_param(vs_const,para.parasym.vardef,procdefinition.proccalloption)
  4698. else
  4699. pushconstaddr:=false;
  4700. realtarget:=actualtargetnode(@para.left)^;
  4701. { if the parameter is "complex", try to take the address of the
  4702. parameter expression, store it in a temp and replace occurrences of
  4703. the parameter with dereferencings of this temp
  4704. }
  4705. complexpara:=
  4706. { don't create a temp. for function results }
  4707. not(nf_is_funcret in realtarget.flags) and
  4708. { this makes only sense if the parameter is reasonably complex,
  4709. otherwise inserting directly is a better solution }
  4710. (
  4711. (paracomplexity>2) or
  4712. { don't create a temp. for the often seen case that p^ is passed to a var parameter }
  4713. ((paracomplexity>1) and
  4714. not((realtarget.nodetype=derefn) and (para.parasym.varspez in [vs_var,vs_out,vs_constref])) and
  4715. not((realtarget.nodetype=loadn) and tloadnode(realtarget).is_addr_param_load) and
  4716. not(realtarget.nodetype=realconstn)
  4717. )
  4718. );
  4719. { check if we have to create a temp, assign the parameter's
  4720. contents to that temp and then substitute the parameter
  4721. with the temp everywhere in the function }
  4722. if paraneedsinlinetemp(para,pushconstaddr,complexpara) then
  4723. begin
  4724. tempnode:=ctempcreatenode.create(para.parasym.vardef,para.parasym.vardef.size,
  4725. tt_persistent,tparavarsym(para.parasym).is_regvar(false));
  4726. addstatement(inlineinitstatement,tempnode);
  4727. addstatement(inlinecleanupstatement,ctempdeletenode.create(tempnode));
  4728. addstatement(inlineinitstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  4729. para.left));
  4730. para.left := ctemprefnode.create(tempnode);
  4731. { inherit addr_taken flag }
  4732. if (tabstractvarsym(para.parasym).addr_taken) then
  4733. tempnode.includetempflag(ti_addr_taken);
  4734. { inherit const }
  4735. if tabstractvarsym(para.parasym).varspez=vs_const then
  4736. begin
  4737. tempnode.includetempflag(ti_const);
  4738. { apply less strict rules for the temp. to be a register than
  4739. ttempcreatenode does
  4740. this way, dyn. array, ansistrings etc. can be put into registers as well }
  4741. if tparavarsym(para.parasym).is_regvar(false) then
  4742. tempnode.includetempflag(ti_may_be_in_reg);
  4743. end;
  4744. result:=true;
  4745. end
  4746. { for formaldefs, we do not need a temp., but it must be inherited if they are not regable }
  4747. else if (para.parasym.vardef.typ=formaldef) and not(tparavarsym(para.parasym).is_regvar(false)) then
  4748. make_not_regable(para.left,[ra_addr_regable]);
  4749. end;
  4750. procedure tcallnode.createinlineparas;
  4751. var
  4752. para: tcallparanode;
  4753. n: tnode;
  4754. complexpara: boolean;
  4755. begin
  4756. { parameters }
  4757. para := tcallparanode(left);
  4758. while assigned(para) do
  4759. begin
  4760. if (para.parasym.typ = paravarsym) and
  4761. ((para.parasym.refs>0) or
  4762. not(cs_opt_dead_values in current_settings.optimizerswitches) or
  4763. might_have_sideeffects(para.left)) then
  4764. begin
  4765. { must take copy of para.left, because if it contains a }
  4766. { temprefn pointing to a copied temp (e.g. methodpointer), }
  4767. { then this parameter must be changed to point to the copy of }
  4768. { that temp (JM) }
  4769. n := para.left.getcopy;
  4770. para.left.free;
  4771. para.left := n;
  4772. firstpass(para.left);
  4773. if not maybecreateinlineparatemp(para,complexpara) and
  4774. complexpara then
  4775. wrapcomplexinlinepara(para);
  4776. end;
  4777. para := tcallparanode(para.right);
  4778. end;
  4779. { local variables }
  4780. if not assigned(tprocdef(procdefinition).localst) or
  4781. (tprocdef(procdefinition).localst.SymList.count = 0) then
  4782. exit;
  4783. inlinelocals.count:=tprocdef(procdefinition).localst.SymList.count;
  4784. tprocdef(procdefinition).localst.SymList.ForEachCall(@createlocaltemps,nil);
  4785. end;
  4786. procedure tcallnode.wrapcomplexinlinepara(para: tcallparanode);
  4787. var
  4788. ptrtype: tdef;
  4789. tempnode: ttempcreatenode;
  4790. paraaddr: taddrnode;
  4791. isfuncretnode : boolean;
  4792. begin
  4793. ptrtype:=cpointerdef.getreusable(para.left.resultdef);
  4794. tempnode:=ctempcreatenode.create(ptrtype,ptrtype.size,tt_persistent,true);
  4795. addstatement(inlineinitstatement,tempnode);
  4796. isfuncretnode:=nf_is_funcret in para.left.flags;
  4797. if isfuncretnode then
  4798. addstatement(inlinecleanupstatement,ctempdeletenode.create_normal_temp(tempnode))
  4799. else
  4800. addstatement(inlinecleanupstatement,ctempdeletenode.create(tempnode));
  4801. { inherit addr_taken flag }
  4802. if (tabstractvarsym(para.parasym).addr_taken) then
  4803. tempnode.includetempflag(ti_addr_taken);
  4804. { inherit read only }
  4805. if tabstractvarsym(para.parasym).varspez=vs_const then
  4806. tempnode.includetempflag(ti_const);
  4807. paraaddr:=caddrnode.create_internal(para.left);
  4808. include(paraaddr.addrnodeflags,anf_typedaddr);
  4809. addstatement(inlineinitstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  4810. paraaddr));
  4811. para.left:=cderefnode.create(ctemprefnode.create(tempnode));
  4812. if isfuncretnode then
  4813. Include(para.left.flags,nf_is_funcret);
  4814. end;
  4815. function UsesTmp(var n: tnode; arg: pointer): foreachnoderesult;
  4816. begin
  4817. Result:=fen_false;
  4818. if (n.nodetype=temprefn) and (ttemprefnode(n).tempinfo=arg) then
  4819. Result:=fen_norecurse_true;
  4820. end;
  4821. function tcallnode.optimize_funcret_assignment(inlineblock: tblocknode): tnode;
  4822. var
  4823. hp : tstatementnode;
  4824. hp2 : tnode;
  4825. resassign : tassignmentnode;
  4826. begin
  4827. result:=nil;
  4828. if not assigned(funcretnode) or
  4829. not(cnf_return_value_used in callnodeflags) then
  4830. exit;
  4831. { block already optimized? }
  4832. if not(inlineblock.nodetype=blockn) then
  4833. exit;
  4834. { tempcreatenode for the function result }
  4835. hp:=tstatementnode(inlineblock.left);
  4836. if not(assigned(hp)) or
  4837. (hp.left.nodetype <> tempcreaten) or
  4838. not(nf_is_funcret in hp.left.flags) then
  4839. exit;
  4840. hp:=tstatementnode(hp.right);
  4841. if not(assigned(hp)) or
  4842. (hp.left.nodetype<>assignn)
  4843. { FK: check commented, original comment was:
  4844. constant assignment? right must be a constant (mainly to avoid trying
  4845. to reuse local temps which may already be freed afterwards once these
  4846. checks are made looser)
  4847. or
  4848. not is_constnode(tassignmentnode(hp.left).right)
  4849. So far I found no example why removing this check might be a problem.
  4850. If this needs to be revert, issue #36279 must be checked/solved again.
  4851. }
  4852. then
  4853. exit;
  4854. { left must be function result }
  4855. resassign:=tassignmentnode(hp.left);
  4856. hp2:=resassign.left;
  4857. { can have extra type conversion due to absolute mapping
  4858. of <fucntionname> on function result var }
  4859. if (hp2.nodetype=typeconvn) and (ttypeconvnode(hp2).convtype=tc_equal) then
  4860. hp2:=ttypeconvnode(hp2).left;
  4861. if (hp2.nodetype<>temprefn) or
  4862. { check if right references the temp. being removed, i.e. using an uninitialized result }
  4863. foreachnodestatic(resassign.right,@UsesTmp,ttemprefnode(hp2).tempinfo) or
  4864. not(nf_is_funcret in hp2.flags) then
  4865. exit;
  4866. { tempdelete to normal of the function result }
  4867. hp:=tstatementnode(hp.right);
  4868. if not(assigned(hp)) or
  4869. (hp.left.nodetype <> tempdeleten) then
  4870. exit;
  4871. { the function result once more }
  4872. hp:=tstatementnode(hp.right);
  4873. if not(assigned(hp)) or
  4874. (hp.left.nodetype<>temprefn) or
  4875. not(nf_is_funcret in hp.left.flags) then
  4876. exit;
  4877. { should be the end }
  4878. if assigned(hp.right) then
  4879. exit;
  4880. { we made it! }
  4881. result:=ctypeconvnode.create_internal(tassignmentnode(resassign).right.getcopy,hp2.resultdef);
  4882. firstpass(result);
  4883. end;
  4884. { this procedure removes the user code flag because it prevents optimizations }
  4885. function removeusercodeflag(var n : tnode; arg : pointer) : foreachnoderesult;
  4886. begin
  4887. result:=fen_false;
  4888. if nf_usercode_entry in n.flags then
  4889. begin
  4890. exclude(n.flags,nf_usercode_entry);
  4891. result:=fen_norecurse_true;
  4892. end;
  4893. end;
  4894. { reference symbols that are imported from another unit }
  4895. function importglobalsyms(var n:tnode; arg:pointer):foreachnoderesult;
  4896. var
  4897. sym : tsym;
  4898. begin
  4899. result:=fen_false;
  4900. if n.nodetype=loadn then
  4901. begin
  4902. sym:=tloadnode(n).symtableentry;
  4903. if sym.typ=staticvarsym then
  4904. begin
  4905. if FindUnitSymtable(tloadnode(n).symtable).moduleid<>current_module.moduleid then
  4906. current_module.addimportedsym(sym);
  4907. end
  4908. else if (sym.typ=constsym) and (tconstsym(sym).consttyp=constresourcestring) then
  4909. begin
  4910. if tloadnode(n).symtableentry.owner.moduleid<>current_module.moduleid then
  4911. current_module.addimportedsym(sym);
  4912. end;
  4913. end
  4914. else if (n.nodetype=calln) then
  4915. begin
  4916. if (assigned(tcallnode(n).procdefinition)) and
  4917. (tcallnode(n).procdefinition.typ=procdef) and
  4918. (findunitsymtable(tcallnode(n).procdefinition.owner).moduleid<>current_module.moduleid) then
  4919. current_module.addimportedsym(tprocdef(tcallnode(n).procdefinition).procsym);
  4920. end;
  4921. end;
  4922. function tcallnode.pass1_inline:tnode;
  4923. var
  4924. n,
  4925. body : tnode;
  4926. para : tcallparanode;
  4927. inlineblock,
  4928. inlinecleanupblock : tblocknode;
  4929. begin
  4930. inc(inlinelevel);
  4931. result:=nil;
  4932. if not(assigned(tprocdef(procdefinition).inlininginfo) and
  4933. assigned(tprocdef(procdefinition).inlininginfo^.code)) then
  4934. internalerror(200412021);
  4935. inlinelocals:=TFPObjectList.create(true);
  4936. { inherit flags }
  4937. current_procinfo.flags:=current_procinfo.flags+
  4938. ((procdefinition as tprocdef).inlininginfo^.flags*inherited_inlining_flags);
  4939. { Create new code block for inlining }
  4940. inlineblock:=internalstatements(inlineinitstatement);
  4941. { make sure that valid_for_assign() returns false for this block
  4942. (otherwise assigning values to the block will result in assigning
  4943. values to the inlined function's result) }
  4944. include(inlineblock.flags,nf_no_lvalue);
  4945. inlinecleanupblock:=internalstatements(inlinecleanupstatement);
  4946. if assigned(callinitblock) then
  4947. addstatement(inlineinitstatement,callinitblock.getcopy);
  4948. { replace complex parameters with temps }
  4949. createinlineparas;
  4950. { create a copy of the body and replace parameter loads with the parameter values }
  4951. body:=tprocdef(procdefinition).inlininginfo^.code.getcopy;
  4952. foreachnodestatic(pm_postprocess,body,@removeusercodeflag,nil);
  4953. foreachnodestatic(pm_postprocess,body,@importglobalsyms,nil);
  4954. foreachnode(pm_preprocess,body,@replaceparaload,@fileinfo);
  4955. { Concat the body and finalization parts }
  4956. addstatement(inlineinitstatement,body);
  4957. addstatement(inlineinitstatement,inlinecleanupblock);
  4958. inlinecleanupblock:=nil;
  4959. if assigned(callcleanupblock) then
  4960. addstatement(inlineinitstatement,callcleanupblock.getcopy);
  4961. { the last statement of the new inline block must return the
  4962. location and type of the function result.
  4963. This is not needed when the result is not used, also the tempnode is then
  4964. already destroyed by a tempdelete in the callcleanupblock tree }
  4965. if not is_void(resultdef) and
  4966. (cnf_return_value_used in callnodeflags) then
  4967. begin
  4968. if assigned(funcretnode) then
  4969. addstatement(inlineinitstatement,funcretnode.getcopy)
  4970. else
  4971. begin
  4972. para:=tcallparanode(left);
  4973. while assigned(para) do
  4974. begin
  4975. if (vo_is_hidden_para in para.parasym.varoptions) and
  4976. (vo_is_funcret in para.parasym.varoptions) then
  4977. begin
  4978. addstatement(inlineinitstatement,para.left.getcopy);
  4979. break;
  4980. end;
  4981. para:=tcallparanode(para.right);
  4982. end;
  4983. end;
  4984. end;
  4985. typecheckpass(tnode(inlineblock));
  4986. doinlinesimplify(tnode(inlineblock));
  4987. node_reset_flags(tnode(inlineblock),[nf_pass1_done]);
  4988. firstpass(tnode(inlineblock));
  4989. result:=inlineblock;
  4990. { if the function result is used then verify that the blocknode
  4991. returns the same result type as the original callnode }
  4992. if (cnf_return_value_used in callnodeflags) and
  4993. (result.resultdef<>resultdef) then
  4994. internalerror(200709171);
  4995. { free the temps for the locals }
  4996. inlinelocals.free;
  4997. inlinelocals:=nil;
  4998. inlineinitstatement:=nil;
  4999. inlinecleanupstatement:=nil;
  5000. n:=optimize_funcret_assignment(inlineblock);
  5001. if assigned(n) then
  5002. begin
  5003. inlineblock.free;
  5004. result:=n;
  5005. end;
  5006. {$ifdef DEBUGINLINE}
  5007. writeln;
  5008. writeln('**************************',tprocdef(procdefinition).mangledname);
  5009. printnode(output,result);
  5010. {$endif DEBUGINLINE}
  5011. dec(inlinelevel);
  5012. end;
  5013. end.