ncal.pas 210 KB

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