ncal.pas 212 KB

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