ncal.pas 124 KB

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