ncal.pas 221 KB

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