ncal.pas 154 KB

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