ncal.pas 202 KB

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