ncal.pas 190 KB

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