ncal.pas 209 KB

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