ncal.pas 124 KB

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