ncal.pas 212 KB

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