ncal.pas 172 KB

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