ncal.pas 222 KB

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