ncal.pas 146 KB

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