ncal.pas 174 KB

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