ncal.pas 223 KB

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