ncal.pas 225 KB

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