ncal.pas 198 KB

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