ncal.pas 212 KB

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