ncal.pas 219 KB

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