ncal.pas 202 KB

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