ncal.pas 129 KB

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