ncal.pas 215 KB

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