ncal.pas 218 KB

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