ncal.pas 129 KB

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