ncal.pas 213 KB

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