ncal.pas 209 KB

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