ncal.pas 213 KB

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