ncal.pas 176 KB

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