ncal.pas 210 KB

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