ncal.pas 123 KB

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