ncal.pas 123 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145
  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. pd : tprocdef;
  1260. i : integer;
  1261. first : boolean;
  1262. begin
  1263. objectdf := nil;
  1264. { verify if trying to create an instance of a class which contains
  1265. non-implemented abstract methods }
  1266. { first verify this class type, no class than exit }
  1267. { also, this checking can only be done if the constructor is directly
  1268. called, indirect constructor calls cannot be checked.
  1269. }
  1270. if assigned(methodpointer) and
  1271. not (nf_is_self in methodpointer.flags) then
  1272. begin
  1273. if (methodpointer.resultdef.typ = objectdef) then
  1274. objectdf:=tobjectdef(methodpointer.resultdef)
  1275. else
  1276. if (methodpointer.resultdef.typ = classrefdef) and
  1277. (tclassrefdef(methodpointer.resultdef).pointeddef.typ = objectdef) and
  1278. (methodpointer.nodetype in [typen,loadvmtaddrn]) then
  1279. objectdf:=tobjectdef(tclassrefdef(methodpointer.resultdef).pointeddef);
  1280. end;
  1281. if not assigned(objectdf) then
  1282. exit;
  1283. parents := tlinkedlist.create;
  1284. AbstractMethodsList := TFPHashList.create;
  1285. { insert all parents in this class : the first item in the
  1286. list will be the base parent of the class .
  1287. }
  1288. while assigned(objectdf) do
  1289. begin
  1290. objectinfo:=tobjectinfoitem.create(objectdf);
  1291. parents.insert(objectinfo);
  1292. objectdf := objectdf.childof;
  1293. end;
  1294. { now all parents are in the correct order
  1295. insert all abstract methods in the list, and remove
  1296. those which are overriden by parent classes.
  1297. }
  1298. objectinfo:=tobjectinfoitem(parents.first);
  1299. while assigned(objectinfo) do
  1300. begin
  1301. objectdf := objectinfo.objinfo;
  1302. if assigned(objectdf.symtable) then
  1303. objectdf.symtable.SymList.ForEachCall(@verifyabstract,nil);
  1304. objectinfo:=tobjectinfoitem(objectinfo.next);
  1305. end;
  1306. if assigned(parents) then
  1307. parents.free;
  1308. { Finally give out a warning for each abstract method still in the list }
  1309. first:=true;
  1310. for i:=0 to AbstractMethodsList.Count-1 do
  1311. begin
  1312. pd:=tprocdef(AbstractMethodsList[i]);
  1313. if po_abstractmethod in pd.procoptions then
  1314. begin
  1315. if first then
  1316. begin
  1317. Message1(type_w_instance_with_abstract,objectdf.objrealname^);
  1318. first:=false;
  1319. end;
  1320. MessagePos1(pd.fileinfo,sym_h_abstract_method_list,pd.fullprocname(true));
  1321. end;
  1322. end;
  1323. if assigned(AbstractMethodsList) then
  1324. AbstractMethodsList.Free;
  1325. end;
  1326. function tcallnode.gen_self_tree_methodpointer:tnode;
  1327. var
  1328. hsym : tfieldvarsym;
  1329. begin
  1330. { find self field in methodpointer record }
  1331. hsym:=tfieldvarsym(trecorddef(methodpointertype).symtable.Find('self'));
  1332. if not assigned(hsym) then
  1333. internalerror(200305251);
  1334. { Load tmehodpointer(right).self }
  1335. result:=csubscriptnode.create(
  1336. hsym,
  1337. ctypeconvnode.create_internal(right.getcopy,methodpointertype));
  1338. end;
  1339. function tcallnode.gen_self_tree:tnode;
  1340. var
  1341. selftree : tnode;
  1342. begin
  1343. selftree:=nil;
  1344. { inherited }
  1345. if (cnf_inherited in callnodeflags) then
  1346. selftree:=load_self_node
  1347. else
  1348. { constructors }
  1349. if (procdefinition.proctypeoption=potype_constructor) then
  1350. begin
  1351. { push 0 as self when allocation is needed }
  1352. if (methodpointer.resultdef.typ=classrefdef) or
  1353. (cnf_new_call in callnodeflags) then
  1354. selftree:=cpointerconstnode.create(0,voidpointertype)
  1355. else
  1356. begin
  1357. if methodpointer.nodetype=typen then
  1358. selftree:=load_self_node
  1359. else
  1360. selftree:=methodpointer.getcopy;
  1361. end;
  1362. end
  1363. else
  1364. { Calling a static/class method }
  1365. if (po_classmethod in procdefinition.procoptions) or
  1366. (po_staticmethod in procdefinition.procoptions) then
  1367. begin
  1368. if (procdefinition.typ<>procdef) then
  1369. internalerror(200305062);
  1370. if (oo_has_vmt in tprocdef(procdefinition)._class.objectoptions) then
  1371. begin
  1372. { we only need the vmt, loading self is not required and there is no
  1373. need to check for typen, because that will always get the
  1374. loadvmtaddrnode added }
  1375. selftree:=methodpointer.getcopy;
  1376. if (methodpointer.resultdef.typ<>classrefdef) or
  1377. (methodpointer.nodetype = typen) then
  1378. selftree:=cloadvmtaddrnode.create(selftree);
  1379. end
  1380. else
  1381. selftree:=cpointerconstnode.create(0,voidpointertype);
  1382. end
  1383. else
  1384. begin
  1385. if methodpointer.nodetype=typen then
  1386. selftree:=load_self_node
  1387. else
  1388. selftree:=methodpointer.getcopy;
  1389. end;
  1390. result:=selftree;
  1391. end;
  1392. function tcallnode.gen_vmt_tree:tnode;
  1393. var
  1394. vmttree : tnode;
  1395. begin
  1396. vmttree:=nil;
  1397. if not(procdefinition.proctypeoption in [potype_constructor,potype_destructor]) then
  1398. internalerror(200305051);
  1399. { Handle classes and legacy objects separate to make it
  1400. more maintainable }
  1401. if (methodpointer.resultdef.typ=classrefdef) then
  1402. begin
  1403. if not is_class(tclassrefdef(methodpointer.resultdef).pointeddef) then
  1404. internalerror(200501041);
  1405. { constructor call via classreference => allocate memory }
  1406. if (procdefinition.proctypeoption=potype_constructor) then
  1407. begin
  1408. vmttree:=methodpointer.getcopy;
  1409. { Only a typenode can be passed when it is called with <class of xx>.create }
  1410. if vmttree.nodetype=typen then
  1411. vmttree:=cloadvmtaddrnode.create(vmttree);
  1412. end
  1413. else
  1414. begin
  1415. { Call afterconstruction }
  1416. vmttree:=cpointerconstnode.create(1,voidpointertype);
  1417. end;
  1418. end
  1419. else
  1420. { Class style objects }
  1421. if is_class(methodpointer.resultdef) then
  1422. begin
  1423. { inherited call, no create/destroy }
  1424. if (cnf_inherited in callnodeflags) then
  1425. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1426. else
  1427. { do not create/destroy when called from member function
  1428. without specifying self explicit }
  1429. if (cnf_member_call in callnodeflags) then
  1430. begin
  1431. { destructor: don't release instance, vmt=0
  1432. constructor:
  1433. if called from a constructor in the same class then
  1434. don't call afterconstruction, vmt=0
  1435. else
  1436. call afterconstrution, vmt=1 }
  1437. if (procdefinition.proctypeoption=potype_destructor) then
  1438. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1439. else if (current_procinfo.procdef.proctypeoption=potype_constructor) and
  1440. (procdefinition.proctypeoption=potype_constructor) then
  1441. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1442. else
  1443. vmttree:=cpointerconstnode.create(1,voidpointertype);
  1444. end
  1445. else
  1446. { normal call to method like cl1.proc }
  1447. begin
  1448. { destructor:
  1449. if not called from exception block in constructor
  1450. call beforedestruction and release instance, vmt=1
  1451. else
  1452. don't call beforedestruction and release instance, vmt=-1
  1453. constructor:
  1454. if called from a constructor in the same class using self.create then
  1455. don't call afterconstruction, vmt=0
  1456. else
  1457. call afterconstrution, vmt=1 }
  1458. if (procdefinition.proctypeoption=potype_destructor) then
  1459. if not(cnf_create_failed in callnodeflags) then
  1460. vmttree:=cpointerconstnode.create(1,voidpointertype)
  1461. else
  1462. vmttree:=cpointerconstnode.create(TConstPtrUInt(-1),voidpointertype)
  1463. else
  1464. begin
  1465. if (current_procinfo.procdef.proctypeoption=potype_constructor) and
  1466. (procdefinition.proctypeoption=potype_constructor) and
  1467. (nf_is_self in methodpointer.flags) then
  1468. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1469. else
  1470. vmttree:=cpointerconstnode.create(1,voidpointertype);
  1471. end;
  1472. end;
  1473. end
  1474. else
  1475. { Old style object }
  1476. begin
  1477. { constructor with extended syntax called from new }
  1478. if (cnf_new_call in callnodeflags) then
  1479. vmttree:=cloadvmtaddrnode.create(ctypenode.create(methodpointer.resultdef))
  1480. else
  1481. { destructor with extended syntax called from dispose }
  1482. if (cnf_dispose_call in callnodeflags) then
  1483. vmttree:=cloadvmtaddrnode.create(methodpointer.getcopy)
  1484. else
  1485. { inherited call, no create/destroy }
  1486. if (cnf_inherited in callnodeflags) then
  1487. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1488. else
  1489. { do not create/destroy when called from member function
  1490. without specifying self explicit }
  1491. if (cnf_member_call in callnodeflags) then
  1492. begin
  1493. { destructor: don't release instance, vmt=0
  1494. constructor: don't initialize instance, vmt=0 }
  1495. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1496. end
  1497. else
  1498. { normal object call like obj.proc }
  1499. begin
  1500. { destructor: direct call, no dispose, vmt=0
  1501. constructor: initialize object, load vmt }
  1502. if (procdefinition.proctypeoption=potype_constructor) then
  1503. begin
  1504. { old styled inherited call? }
  1505. if (methodpointer.nodetype=typen) then
  1506. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1507. else
  1508. vmttree:=cloadvmtaddrnode.create(ctypenode.create(methodpointer.resultdef))
  1509. end
  1510. else
  1511. vmttree:=cpointerconstnode.create(0,voidpointertype);
  1512. end;
  1513. end;
  1514. result:=vmttree;
  1515. end;
  1516. type
  1517. pcallparanode = ^tcallparanode;
  1518. procedure tcallnode.bind_parasym;
  1519. var
  1520. i : integer;
  1521. pt : tcallparanode;
  1522. oldppt : pcallparanode;
  1523. varargspara,
  1524. currpara : tparavarsym;
  1525. used_by_callnode : boolean;
  1526. hiddentree : tnode;
  1527. newstatement : tstatementnode;
  1528. temp : ttempcreatenode;
  1529. begin
  1530. pt:=tcallparanode(left);
  1531. oldppt:=pcallparanode(@left);
  1532. { flag all callparanodes that belong to the varargs }
  1533. i:=paralength;
  1534. while (i>procdefinition.maxparacount) do
  1535. begin
  1536. include(pt.callparaflags,cpf_varargs_para);
  1537. oldppt:=pcallparanode(@pt.right);
  1538. pt:=tcallparanode(pt.right);
  1539. dec(i);
  1540. end;
  1541. { skip varargs that are inserted by array of const }
  1542. while assigned(pt) and
  1543. (cpf_varargs_para in pt.callparaflags) do
  1544. pt:=tcallparanode(pt.right);
  1545. { process normal parameters and insert hidden parameters }
  1546. for i:=procdefinition.paras.count-1 downto 0 do
  1547. begin
  1548. currpara:=tparavarsym(procdefinition.paras[i]);
  1549. if vo_is_hidden_para in currpara.varoptions then
  1550. begin
  1551. { generate hidden tree }
  1552. used_by_callnode:=false;
  1553. hiddentree:=nil;
  1554. if (vo_is_funcret in currpara.varoptions) then
  1555. begin
  1556. { Generate funcretnode if not specified }
  1557. if assigned(funcretnode) then
  1558. begin
  1559. hiddentree:=funcretnode.getcopy;
  1560. end
  1561. else
  1562. begin
  1563. hiddentree:=internalstatements(newstatement);
  1564. { need to use resultdef instead of procdefinition.returndef,
  1565. because they can be different }
  1566. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  1567. addstatement(newstatement,temp);
  1568. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  1569. addstatement(newstatement,ctemprefnode.create(temp));
  1570. end;
  1571. end
  1572. else
  1573. if vo_is_high_para in currpara.varoptions then
  1574. begin
  1575. if not assigned(pt) or (i=0) then
  1576. internalerror(200304082);
  1577. { we need the information of the previous parameter }
  1578. hiddentree:=gen_high_tree(pt.left,tparavarsym(procdefinition.paras[i-1]).vardef);
  1579. end
  1580. else
  1581. if vo_is_self in currpara.varoptions then
  1582. begin
  1583. if assigned(right) then
  1584. hiddentree:=gen_self_tree_methodpointer
  1585. else
  1586. hiddentree:=gen_self_tree;
  1587. end
  1588. else
  1589. if vo_is_vmt in currpara.varoptions then
  1590. begin
  1591. hiddentree:=gen_vmt_tree;
  1592. end
  1593. {$if defined(powerpc) or defined(m68k)}
  1594. else
  1595. if vo_is_syscall_lib in currpara.varoptions then
  1596. begin
  1597. { lib parameter has no special type but proccalloptions must be a syscall }
  1598. hiddentree:=cloadnode.create(tprocdef(procdefinition).libsym,tprocdef(procdefinition).libsym.owner);
  1599. end
  1600. {$endif powerpc or m68k}
  1601. else
  1602. if vo_is_parentfp in currpara.varoptions then
  1603. begin
  1604. if not(assigned(procdefinition.owner.defowner)) then
  1605. internalerror(200309287);
  1606. hiddentree:=cloadparentfpnode.create(tprocdef(procdefinition.owner.defowner));
  1607. end
  1608. else
  1609. if vo_is_range_check in currpara.varoptions then
  1610. begin
  1611. hiddentree:=cordconstnode.create(Ord(cs_check_range in current_settings.localswitches),booltype,false);
  1612. end
  1613. else
  1614. if vo_is_overflow_check in currpara.varoptions then
  1615. begin
  1616. hiddentree:=cordconstnode.create(Ord(cs_check_overflow in current_settings.localswitches),booltype,false);
  1617. end
  1618. else
  1619. if vo_is_typinfo_para in currpara.varoptions then
  1620. begin
  1621. if not assigned(pt) or (i=0) then
  1622. internalerror(200304082);
  1623. hiddentree:=caddrnode.create_internal(
  1624. crttinode.create(Tstoreddef(pt.resultdef),fullrtti,rdt_normal)
  1625. );
  1626. end;
  1627. { add the hidden parameter }
  1628. if not assigned(hiddentree) then
  1629. internalerror(200304073);
  1630. { Already insert para and let the previous node point to
  1631. this new node }
  1632. pt:=ccallparanode.create(hiddentree,oldppt^);
  1633. pt.used_by_callnode:=used_by_callnode;
  1634. oldppt^:=pt;
  1635. end;
  1636. if not assigned(pt) then
  1637. internalerror(200310052);
  1638. pt.parasym:=currpara;
  1639. oldppt:=pcallparanode(@pt.right);
  1640. pt:=tcallparanode(pt.right);
  1641. end;
  1642. { Create parasyms for varargs, first count the number of varargs paras,
  1643. then insert the parameters with numbering in reverse order. The SortParas
  1644. will set the correct order at the end}
  1645. pt:=tcallparanode(left);
  1646. i:=0;
  1647. while assigned(pt) do
  1648. begin
  1649. if cpf_varargs_para in pt.callparaflags then
  1650. inc(i);
  1651. pt:=tcallparanode(pt.right);
  1652. end;
  1653. if (i>0) then
  1654. begin
  1655. varargsparas:=tvarargsparalist.create;
  1656. pt:=tcallparanode(left);
  1657. while assigned(pt) do
  1658. begin
  1659. if cpf_varargs_para in pt.callparaflags then
  1660. begin
  1661. varargspara:=tparavarsym.create('va'+tostr(i),i,vs_value,pt.resultdef,[]);
  1662. dec(i);
  1663. { varargspara is left-right, use insert
  1664. instead of concat }
  1665. varargsparas.add(varargspara);
  1666. pt.parasym:=varargspara;
  1667. end;
  1668. pt:=tcallparanode(pt.right);
  1669. end;
  1670. varargsparas.sortparas;
  1671. end;
  1672. end;
  1673. function tcallnode.pass_typecheck:tnode;
  1674. var
  1675. candidates : tcallcandidates;
  1676. oldcallnode : tcallnode;
  1677. hpt : tnode;
  1678. pt : tcallparanode;
  1679. lastpara : longint;
  1680. paraidx,
  1681. cand_cnt : integer;
  1682. i : longint;
  1683. is_const : boolean;
  1684. statements : tstatementnode;
  1685. converted_result_data : ttempcreatenode;
  1686. label
  1687. errorexit;
  1688. begin
  1689. result:=nil;
  1690. candidates:=nil;
  1691. oldcallnode:=aktcallnode;
  1692. aktcallnode:=self;
  1693. { determine length of parameter list }
  1694. pt:=tcallparanode(left);
  1695. paralength:=0;
  1696. while assigned(pt) do
  1697. begin
  1698. inc(paralength);
  1699. pt:=tcallparanode(pt.right);
  1700. end;
  1701. { determine the type of the parameters }
  1702. if assigned(left) then
  1703. begin
  1704. tcallparanode(left).get_paratype;
  1705. if codegenerror then
  1706. goto errorexit;
  1707. end;
  1708. if assigned(methodpointer) then
  1709. begin
  1710. typecheckpass(methodpointer);
  1711. maybe_load_para_in_temp(methodpointer);
  1712. end;
  1713. { procedure variable ? }
  1714. if assigned(right) then
  1715. begin
  1716. set_varstate(right,vs_read,[vsf_must_be_valid]);
  1717. typecheckpass(right);
  1718. if codegenerror then
  1719. exit;
  1720. procdefinition:=tabstractprocdef(right.resultdef);
  1721. { Compare parameters from right to left }
  1722. paraidx:=procdefinition.Paras.count-1;
  1723. { Skip default parameters }
  1724. if not(po_varargs in procdefinition.procoptions) then
  1725. begin
  1726. { ignore hidden parameters }
  1727. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) do
  1728. dec(paraidx);
  1729. for i:=1 to procdefinition.maxparacount-paralength do
  1730. begin
  1731. if paraidx<0 then
  1732. internalerror(200402261);
  1733. if not assigned(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym) then
  1734. begin
  1735. CGMessage1(parser_e_wrong_parameter_size,'<Procedure Variable>');
  1736. goto errorexit;
  1737. end;
  1738. dec(paraidx);
  1739. end;
  1740. end;
  1741. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) do
  1742. dec(paraidx);
  1743. pt:=tcallparanode(left);
  1744. lastpara:=paralength;
  1745. while (paraidx>=0) and assigned(pt) do
  1746. begin
  1747. { only goto next para if we're out of the varargs }
  1748. if not(po_varargs in procdefinition.procoptions) or
  1749. (lastpara<=procdefinition.maxparacount) then
  1750. begin
  1751. repeat
  1752. dec(paraidx);
  1753. until (paraidx<0) or not(vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions);
  1754. end;
  1755. pt:=tcallparanode(pt.right);
  1756. dec(lastpara);
  1757. end;
  1758. if assigned(pt) or
  1759. ((paraidx>=0) and
  1760. not assigned(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym)) then
  1761. begin
  1762. if assigned(pt) then
  1763. current_filepos:=pt.fileinfo;
  1764. CGMessage1(parser_e_wrong_parameter_size,'<Procedure Variable>');
  1765. goto errorexit;
  1766. end;
  1767. end
  1768. else
  1769. { not a procedure variable }
  1770. begin
  1771. { do we know the procedure to call ? }
  1772. if not(assigned(procdefinition)) then
  1773. begin
  1774. candidates:=tcallcandidates.create(symtableprocentry,symtableproc,left,(nf_isproperty in flags),
  1775. { ignore possible private in delphi mode for anon. inherited (FK) }
  1776. (m_delphi in current_settings.modeswitches) and (cnf_anon_inherited in callnodeflags));
  1777. { no procedures found? then there is something wrong
  1778. with the parameter size or the procedures are
  1779. not accessible }
  1780. if candidates.count=0 then
  1781. begin
  1782. { when it's an auto inherited call and there
  1783. is no procedure found, but the procedures
  1784. were defined with overload directive and at
  1785. least two procedures are defined then we ignore
  1786. this inherited by inserting a nothingn. Only
  1787. do this ugly hack in Delphi mode as it looks more
  1788. like a bug. It's also not documented }
  1789. if (m_delphi in current_settings.modeswitches) and
  1790. (cnf_anon_inherited in callnodeflags) and
  1791. (symtableprocentry.owner.symtabletype=ObjectSymtable) and
  1792. (po_overload in tprocdef(symtableprocentry.ProcdefList[0]).procoptions) and
  1793. (symtableprocentry.ProcdefList.Count>=2) then
  1794. result:=cnothingnode.create
  1795. else
  1796. begin
  1797. { in tp mode we can try to convert to procvar if
  1798. there are no parameters specified }
  1799. if not(assigned(left)) and
  1800. not(cnf_inherited in callnodeflags) and
  1801. ((m_tp_procvar in current_settings.modeswitches) or
  1802. (m_mac_procvar in current_settings.modeswitches)) and
  1803. (not assigned(methodpointer) or
  1804. (methodpointer.nodetype <> typen)) then
  1805. begin
  1806. hpt:=cloadnode.create(tprocsym(symtableprocentry),symtableproc);
  1807. if assigned(methodpointer) then
  1808. tloadnode(hpt).set_mp(get_load_methodpointer);
  1809. typecheckpass(hpt);
  1810. result:=hpt;
  1811. end
  1812. else
  1813. begin
  1814. if assigned(left) then
  1815. current_filepos:=left.fileinfo;
  1816. CGMessage1(parser_e_wrong_parameter_size,symtableprocentry.realname);
  1817. symtableprocentry.write_parameter_lists(nil);
  1818. end;
  1819. end;
  1820. goto errorexit;
  1821. end;
  1822. { Retrieve information about the candidates }
  1823. candidates.get_information;
  1824. {$ifdef EXTDEBUG}
  1825. { Display info when multiple candidates are found }
  1826. if candidates.count>1 then
  1827. candidates.dump_info(V_Debug);
  1828. {$endif EXTDEBUG}
  1829. { Choose the best candidate and count the number of
  1830. candidates left }
  1831. cand_cnt:=candidates.choose_best(procdefinition,
  1832. assigned(left) and
  1833. not assigned(tcallparanode(left).right) and
  1834. (tcallparanode(left).left.resultdef.typ=variantdef));
  1835. { All parameters are checked, check if there are any
  1836. procedures left }
  1837. if cand_cnt>0 then
  1838. begin
  1839. { Multiple candidates left? }
  1840. if cand_cnt>1 then
  1841. begin
  1842. CGMessage(type_e_cant_choose_overload_function);
  1843. {$ifdef EXTDEBUG}
  1844. candidates.dump_info(V_Hint);
  1845. {$else EXTDEBUG}
  1846. candidates.list(false);
  1847. {$endif EXTDEBUG}
  1848. { we'll just use the first candidate to make the
  1849. call }
  1850. end;
  1851. { assign procdefinition }
  1852. if symtableproc=nil then
  1853. symtableproc:=procdefinition.owner;
  1854. end
  1855. else
  1856. begin
  1857. { No candidates left, this must be a type error,
  1858. because wrong size is already checked. procdefinition
  1859. is filled with the first (random) definition that is
  1860. found. We use this definition to display a nice error
  1861. message that the wrong type is passed }
  1862. candidates.find_wrong_para;
  1863. candidates.list(true);
  1864. {$ifdef EXTDEBUG}
  1865. candidates.dump_info(V_Hint);
  1866. {$endif EXTDEBUG}
  1867. { We can not proceed, release all procs and exit }
  1868. candidates.free;
  1869. goto errorexit;
  1870. end;
  1871. candidates.free;
  1872. end; { end of procedure to call determination }
  1873. end;
  1874. { check for hints (deprecated etc) }
  1875. if (procdefinition.typ = procdef) then
  1876. check_hints(tprocdef(procdefinition).procsym,tprocdef(procdefinition).symoptions);
  1877. { add needed default parameters }
  1878. if assigned(procdefinition) and
  1879. (paralength<procdefinition.maxparacount) then
  1880. begin
  1881. paraidx:=0;
  1882. i:=0;
  1883. while (i<paralength) do
  1884. begin
  1885. if paraidx>=procdefinition.Paras.count then
  1886. internalerror(200306181);
  1887. if not(vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) then
  1888. inc(i);
  1889. inc(paraidx);
  1890. end;
  1891. while (paraidx<procdefinition.paras.count) and (vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) do
  1892. inc(paraidx);
  1893. while (paraidx<procdefinition.paras.count) do
  1894. begin
  1895. if not assigned(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym) then
  1896. internalerror(200212142);
  1897. left:=ccallparanode.create(genconstsymtree(
  1898. tconstsym(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym)),left);
  1899. { Ignore vs_hidden parameters }
  1900. repeat
  1901. inc(paraidx);
  1902. until (paraidx>=procdefinition.paras.count) or not(vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions);
  1903. end;
  1904. end;
  1905. { recursive call? }
  1906. if assigned(current_procinfo) and
  1907. (procdefinition=current_procinfo.procdef) then
  1908. include(current_procinfo.flags,pi_is_recursive);
  1909. { handle predefined procedures }
  1910. is_const:=(po_internconst in procdefinition.procoptions) and
  1911. ((block_type in [bt_const,bt_type]) or
  1912. (assigned(left) and (tcallparanode(left).left.nodetype in [realconstn,ordconstn])));
  1913. if (procdefinition.proccalloption=pocall_internproc) or is_const then
  1914. begin
  1915. if assigned(left) then
  1916. begin
  1917. { ptr and settextbuf needs two args }
  1918. if assigned(tcallparanode(left).right) then
  1919. begin
  1920. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,left);
  1921. left:=nil;
  1922. end
  1923. else
  1924. begin
  1925. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,tcallparanode(left).left);
  1926. tcallparanode(left).left:=nil;
  1927. end;
  1928. end
  1929. else
  1930. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,nil);
  1931. result:=hpt;
  1932. goto errorexit;
  1933. end;
  1934. { ensure that the result type is set }
  1935. if not(cnf_typedefset in callnodeflags) then
  1936. begin
  1937. { constructors return their current class type, not the type where the
  1938. constructor is declared, this can be different because of inheritance }
  1939. if (procdefinition.proctypeoption=potype_constructor) and
  1940. assigned(methodpointer) and
  1941. assigned(methodpointer.resultdef) and
  1942. (methodpointer.resultdef.typ=classrefdef) then
  1943. resultdef:=tclassrefdef(methodpointer.resultdef).pointeddef
  1944. else
  1945. { Member call to a (inherited) constructor from the class, the return
  1946. value is always self, so we change it to voidtype to generate an
  1947. error and to prevent users from generating non-working code
  1948. when they expect to clone the current instance, see bug 3662 (PFV) }
  1949. if (procdefinition.proctypeoption=potype_constructor) and
  1950. is_class(tprocdef(procdefinition)._class) and
  1951. assigned(methodpointer) and
  1952. (nf_is_self in methodpointer.flags) then
  1953. resultdef:=voidtype
  1954. else
  1955. resultdef:=procdefinition.returndef;
  1956. end
  1957. else
  1958. resultdef:=typedef;
  1959. {if resultdef.needs_inittable then
  1960. include(current_procinfo.flags,pi_needs_implicit_finally);}
  1961. if assigned(methodpointer) then
  1962. begin
  1963. { when methodpointer is a callnode we must load it first into a
  1964. temp to prevent the processing callnode twice }
  1965. if (methodpointer.nodetype=calln) then
  1966. internalerror(200405121);
  1967. { direct call to inherited abstract method, then we
  1968. can already give a error in the compiler instead
  1969. of a runtime error }
  1970. if (cnf_inherited in callnodeflags) and
  1971. (po_abstractmethod in procdefinition.procoptions) then
  1972. begin
  1973. if (m_delphi in current_settings.modeswitches) and
  1974. (cnf_anon_inherited in callnodeflags) then
  1975. begin
  1976. CGMessage(cg_h_inherited_ignored);
  1977. result:=cnothingnode.create;
  1978. exit;
  1979. end
  1980. else
  1981. CGMessage(cg_e_cant_call_abstract_method);
  1982. end;
  1983. { if an inherited con- or destructor should be }
  1984. { called in a con- or destructor then a warning }
  1985. { will be made }
  1986. { con- and destructors need a pointer to the vmt }
  1987. if (cnf_inherited in callnodeflags) and
  1988. (procdefinition.proctypeoption in [potype_constructor,potype_destructor]) and
  1989. is_object(methodpointer.resultdef) and
  1990. not(current_procinfo.procdef.proctypeoption in [potype_constructor,potype_destructor]) then
  1991. CGMessage(cg_w_member_cd_call_from_method);
  1992. if methodpointer.nodetype<>typen then
  1993. begin
  1994. { Remove all postfix operators }
  1995. hpt:=methodpointer;
  1996. while assigned(hpt) and (hpt.nodetype in [subscriptn,vecn]) do
  1997. hpt:=tunarynode(hpt).left;
  1998. if (procdefinition.proctypeoption=potype_constructor) and
  1999. assigned(symtableproc) and
  2000. (symtableproc.symtabletype=withsymtable) and
  2001. (tnode(twithsymtable(symtableproc).withrefnode).nodetype=temprefn) then
  2002. CGmessage(cg_e_cannot_call_cons_dest_inside_with);
  2003. { R.Init then R will be initialized by the constructor,
  2004. Also allow it for simple loads }
  2005. if (procdefinition.proctypeoption=potype_constructor) or
  2006. ((hpt.nodetype=loadn) and
  2007. (methodpointer.resultdef.typ=objectdef) and
  2008. not(oo_has_virtual in tobjectdef(methodpointer.resultdef).objectoptions)
  2009. ) then
  2010. { a constructor will and a method may write something to }
  2011. { the fields }
  2012. set_varstate(methodpointer,vs_readwritten,[])
  2013. else if ((hpt.nodetype=loadn) and
  2014. (methodpointer.resultdef.typ=classrefdef)) then
  2015. set_varstate(methodpointer,vs_read,[])
  2016. else
  2017. set_varstate(methodpointer,vs_read,[vsf_must_be_valid]);
  2018. { The object is already used if it is called once }
  2019. if (hpt.nodetype=loadn) and
  2020. (tloadnode(hpt).symtableentry.typ in [localvarsym,paravarsym,staticvarsym]) then
  2021. set_varstate(hpt,vs_read,[]);
  2022. // tabstractvarsym(tloadnode(hpt).symtableentry).varstate:=vs_readwritten;
  2023. end;
  2024. { if we are calling the constructor check for abstract
  2025. methods. Ignore inherited and member calls, because the
  2026. class is then already created }
  2027. if (procdefinition.proctypeoption=potype_constructor) and
  2028. not(cnf_inherited in callnodeflags) and
  2029. not(cnf_member_call in callnodeflags) then
  2030. verifyabstractcalls;
  2031. end
  2032. else
  2033. begin
  2034. { When this is method the methodpointer must be available }
  2035. if (right=nil) and
  2036. (procdefinition.owner.symtabletype=ObjectSymtable) then
  2037. internalerror(200305061);
  2038. end;
  2039. { Set flag that the procedure uses varargs, also if they are not passed it is still
  2040. needed for x86_64 to pass the number of SSE registers used }
  2041. if po_varargs in procdefinition.procoptions then
  2042. include(callnodeflags,cnf_uses_varargs);
  2043. { Change loading of array of const to varargs }
  2044. if assigned(left) and
  2045. is_array_of_const(tparavarsym(procdefinition.paras[procdefinition.paras.count-1]).vardef) and
  2046. (procdefinition.proccalloption in [pocall_cppdecl,pocall_cdecl]) then
  2047. convert_carg_array_of_const;
  2048. { bind parasyms to the callparanodes and insert hidden parameters }
  2049. bind_parasym;
  2050. { insert type conversions for parameters }
  2051. if assigned(left) then
  2052. tcallparanode(left).insert_typeconv(true);
  2053. { dispinterface methode invoke? }
  2054. if assigned(methodpointer) and is_dispinterface(methodpointer.resultdef) then
  2055. begin
  2056. { if the result is used, we've to insert a call to convert the type to be on the "safe side" }
  2057. if cnf_return_value_used in callnodeflags then
  2058. begin
  2059. result:=internalstatements(statements);
  2060. converted_result_data:=ctempcreatenode.create(procdefinition.returndef,sizeof(procdefinition.returndef),tt_persistent,true);
  2061. addstatement(statements,converted_result_data);
  2062. addstatement(statements,cassignmentnode.create(ctemprefnode.create(converted_result_data),
  2063. ctypeconvnode.create_internal(translate_disp_call(methodpointer,parameters,'',tprocdef(procdefinition).dispid,true),
  2064. procdefinition.returndef)));
  2065. addstatement(statements,ctempdeletenode.create_normal_temp(converted_result_data));
  2066. addstatement(statements,ctemprefnode.create(converted_result_data));
  2067. end
  2068. else
  2069. result:=translate_disp_call(methodpointer,parameters,'',tprocdef(procdefinition).dispid,false);
  2070. { don't free reused nodes }
  2071. methodpointer:=nil;
  2072. parameters:=nil;
  2073. end;
  2074. errorexit:
  2075. aktcallnode:=oldcallnode;
  2076. end;
  2077. procedure tcallnode.order_parameters;
  2078. var
  2079. hp,hpcurr,hpnext,hpfirst,hpprev : tcallparanode;
  2080. currloc : tcgloc;
  2081. begin
  2082. hpfirst:=nil;
  2083. hpcurr:=tcallparanode(left);
  2084. while assigned(hpcurr) do
  2085. begin
  2086. { pull out }
  2087. hpnext:=tcallparanode(hpcurr.right);
  2088. { pull in at the correct place.
  2089. Used order:
  2090. 1. LOC_REFERENCE with smallest offset (x86 only)
  2091. 2. LOC_REFERENCE with most registers
  2092. 3. LOC_REGISTER with most registers
  2093. For the moment we only look at the first parameter field. Combining it
  2094. with multiple parameter fields will make things a lot complexer (PFV) }
  2095. if not assigned(hpcurr.parasym.paraloc[callerside].location) then
  2096. internalerror(200412152);
  2097. currloc:=hpcurr.parasym.paraloc[callerside].location^.loc;
  2098. hpprev:=nil;
  2099. hp:=hpfirst;
  2100. while assigned(hp) do
  2101. begin
  2102. case currloc of
  2103. LOC_REFERENCE :
  2104. begin
  2105. case hp.parasym.paraloc[callerside].location^.loc of
  2106. LOC_REFERENCE :
  2107. begin
  2108. { Offset is calculated like:
  2109. sub esp,12
  2110. mov [esp+8],para3
  2111. mov [esp+4],para2
  2112. mov [esp],para1
  2113. call function
  2114. That means the for pushes the para with the
  2115. highest offset (see para3) needs to be pushed first
  2116. }
  2117. if (hpcurr.registersint>hp.registersint)
  2118. {$ifdef x86}
  2119. or (hpcurr.parasym.paraloc[callerside].location^.reference.offset>hp.parasym.paraloc[callerside].location^.reference.offset)
  2120. {$endif x86}
  2121. then
  2122. break;
  2123. end;
  2124. LOC_REGISTER,
  2125. LOC_FPUREGISTER :
  2126. break;
  2127. end;
  2128. end;
  2129. LOC_FPUREGISTER,
  2130. LOC_REGISTER :
  2131. begin
  2132. if (hp.parasym.paraloc[callerside].location^.loc=currloc) and
  2133. (hpcurr.registersint>hp.registersint) then
  2134. break;
  2135. end;
  2136. end;
  2137. hpprev:=hp;
  2138. hp:=tcallparanode(hp.right);
  2139. end;
  2140. hpcurr.right:=hp;
  2141. if assigned(hpprev) then
  2142. hpprev.right:=hpcurr
  2143. else
  2144. hpfirst:=hpcurr;
  2145. { next }
  2146. hpcurr:=hpnext;
  2147. end;
  2148. left:=hpfirst;
  2149. end;
  2150. function tcallnode.replaceparaload(var n: tnode; arg: pointer): foreachnoderesult;
  2151. var
  2152. paras: tcallparanode;
  2153. temp: tnode;
  2154. indexnr : integer;
  2155. begin
  2156. result := fen_false;
  2157. n.fileinfo := pfileposinfo(arg)^;
  2158. if (n.nodetype = loadn) then
  2159. begin
  2160. case tloadnode(n).symtableentry.typ of
  2161. paravarsym :
  2162. begin
  2163. paras := tcallparanode(left);
  2164. while assigned(paras) and
  2165. (paras.parasym <> tloadnode(n).symtableentry) do
  2166. paras := tcallparanode(paras.right);
  2167. if assigned(paras) then
  2168. begin
  2169. n.free;
  2170. n := paras.left.getcopy;
  2171. typecheckpass(n);
  2172. result := fen_true;
  2173. end;
  2174. end;
  2175. localvarsym :
  2176. begin
  2177. { local? }
  2178. if (tloadnode(n).symtableentry.owner <> tprocdef(procdefinition).localst) then
  2179. exit;
  2180. indexnr:=tloadnode(n).symtableentry.owner.SymList.IndexOf(tloadnode(n).symtableentry);
  2181. if (indexnr >= inlinelocals.count) or
  2182. not assigned(inlinelocals[indexnr]) then
  2183. internalerror(20040720);
  2184. temp := tnode(inlinelocals[indexnr]).getcopy;
  2185. n.free;
  2186. n := temp;
  2187. typecheckpass(n);
  2188. result := fen_true;
  2189. end;
  2190. end;
  2191. end;
  2192. end;
  2193. type
  2194. ptempnodes = ^ttempnodes;
  2195. ttempnodes = record
  2196. createstatement, deletestatement: tstatementnode;
  2197. end;
  2198. procedure tcallnode.createlocaltemps(p:TObject;arg:pointer);
  2199. var
  2200. tempinfo: ptempnodes absolute arg;
  2201. tempnode: ttempcreatenode;
  2202. indexnr : integer;
  2203. begin
  2204. if (TSym(p).typ <> localvarsym) then
  2205. exit;
  2206. indexnr:=TSym(p).Owner.SymList.IndexOf(p);
  2207. if (indexnr >= inlinelocals.count) then
  2208. inlinelocals.count:=indexnr+10;
  2209. if (vo_is_funcret in tabstractvarsym(p).varoptions) and
  2210. assigned(funcretnode) then
  2211. begin
  2212. if node_complexity(funcretnode) > 1 then
  2213. begin
  2214. { can this happen? }
  2215. { we may have to replace the funcretnode with the address of funcretnode }
  2216. { loaded in a temp in this case, because the expression may e.g. contain }
  2217. { a global variable that gets changed inside the function }
  2218. internalerror(2004072101);
  2219. end;
  2220. inlinelocals[indexnr] := funcretnode.getcopy
  2221. end
  2222. else
  2223. begin
  2224. if (vo_is_funcret in tlocalvarsym(p).varoptions) then
  2225. begin
  2226. tempnode := ctempcreatenode.create_inlined_result(tabstractvarsym(p).vardef,tabstractvarsym(p).vardef.size,tt_persistent,tabstractvarsym(p).is_regvar(false));
  2227. addstatement(tempinfo^.createstatement,tempnode);
  2228. funcretnode := ctemprefnode.create(tempnode);
  2229. addstatement(tempinfo^.deletestatement,ctempdeletenode.create_normal_temp(tempnode));
  2230. end
  2231. else
  2232. begin
  2233. tempnode := ctempcreatenode.create(tabstractvarsym(p).vardef,tabstractvarsym(p).vardef.size,tt_persistent,tabstractvarsym(p).is_regvar(false));
  2234. addstatement(tempinfo^.createstatement,tempnode);
  2235. addstatement(tempinfo^.deletestatement,ctempdeletenode.create(tempnode));
  2236. end;
  2237. inlinelocals[indexnr] := ctemprefnode.create(tempnode);
  2238. end;
  2239. end;
  2240. function nonlocalvars(var n: tnode; arg: pointer): foreachnoderesult;
  2241. begin
  2242. result := fen_false;
  2243. { this is just to play it safe, there are more safe situations }
  2244. if (n.nodetype = derefn) or
  2245. ((n.nodetype = loadn) and
  2246. { globals and fields of (possibly global) objects could always be changed in the callee }
  2247. ((tloadnode(n).symtable.symtabletype in [globalsymtable,ObjectSymtable]) or
  2248. { statics can only be modified by functions in the same unit }
  2249. ((tloadnode(n).symtable.symtabletype = staticsymtable) and
  2250. (tloadnode(n).symtable = TSymtable(arg))))) or
  2251. ((n.nodetype = subscriptn) and
  2252. (tsubscriptnode(n).vs.owner.symtabletype = ObjectSymtable)) then
  2253. result := fen_norecurse_true;
  2254. end;
  2255. procedure tcallnode.createinlineparas(var createstatement, deletestatement: tstatementnode);
  2256. var
  2257. para: tcallparanode;
  2258. tempnode: ttempcreatenode;
  2259. tempnodes: ttempnodes;
  2260. n: tnode;
  2261. paracomplexity: longint;
  2262. begin
  2263. { parameters }
  2264. para := tcallparanode(left);
  2265. while assigned(para) do
  2266. begin
  2267. if (para.parasym.typ = paravarsym) and
  2268. { para.left will already be the same as funcretnode in the following case, so don't change }
  2269. (not(vo_is_funcret in tparavarsym(para.parasym).varoptions) or
  2270. (not assigned(funcretnode))) then
  2271. begin
  2272. { must take copy of para.left, because if it contains a }
  2273. { temprefn pointing to a copied temp (e.g. methodpointer), }
  2274. { then this parameter must be changed to point to the copy of }
  2275. { that temp (JM) }
  2276. n := para.left.getcopy;
  2277. para.left.free;
  2278. para.left := n;
  2279. firstpass(para.left);
  2280. { create temps for value parameters, function result and also for }
  2281. { const parameters which are passed by value instead of by reference }
  2282. { we need to take care that we use the type of the defined parameter and not of the
  2283. passed parameter, because these can be different in case of a formaldef (PFV) }
  2284. paracomplexity := node_complexity(para.left);
  2285. { check if we have to create a temp, assign the parameter's }
  2286. { contents to that temp and then substitute the paramter }
  2287. { with the temp everywhere in the function }
  2288. if
  2289. ((tparavarsym(para.parasym).varregable in [vr_none,vr_addr]) and
  2290. not(para.left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE])) or
  2291. { we can't assign to formaldef temps }
  2292. ((para.parasym.vardef.typ<>formaldef) and
  2293. (
  2294. { if paracomplexity > 1, we normally take the address of }
  2295. { the parameter expression, store it in a temp and }
  2296. { substitute the dereferenced temp in the inlined function }
  2297. { We can't do this if we can't take the address of the }
  2298. { parameter expression, so in that case assign to a temp }
  2299. not(para.left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CONSTANT]) or
  2300. ((paracomplexity > 1) and
  2301. (not valid_for_addr(para.left,false) or
  2302. (para.left.nodetype = calln) or
  2303. is_constnode(para.left))) or
  2304. { the problem is that we can't take the address of a function result :( }
  2305. (vo_is_funcret in tparavarsym(para.parasym).varoptions) or
  2306. { we do not need to create a temp for value parameters }
  2307. { which are not modified in the inlined function }
  2308. { const parameters can get vs_readwritten if their }
  2309. { address is taken }
  2310. ((((para.parasym.varspez = vs_value) and
  2311. (para.parasym.varstate in [vs_initialised,vs_declared,vs_read])) or
  2312. { in case of const, this is only necessary if the }
  2313. { variable would be passed by value normally, or if }
  2314. { there is such a variable somewhere in an expression }
  2315. ((para.parasym.varspez = vs_const) and
  2316. (not paramanager.push_addr_param(vs_const,para.parasym.vardef,procdefinition.proccalloption) or
  2317. (paracomplexity > 1)))) and
  2318. { however, if we pass a global variable, an object field or}
  2319. { an expression containing a pointer dereference as }
  2320. { parameter, this value could be modified in other ways as }
  2321. { well and in such cases create a temp to be on the safe }
  2322. { side }
  2323. foreachnodestatic(para.left,@nonlocalvars,pointer(symtableproc))) or
  2324. { value parameters of which we know they are modified by }
  2325. { definition have to be copied to a temp }
  2326. ((para.parasym.varspez = vs_value) and
  2327. not(para.parasym.varstate in [vs_initialised,vs_declared,vs_read])) or
  2328. { the compiler expects that it can take the address of parameters passed by reference in
  2329. the case of const so we can't replace the node simply by a constant node
  2330. When playing with this code, ensure that
  2331. function f(const a,b : longint) : longint;inline;
  2332. begin
  2333. result:=a*b;
  2334. end;
  2335. [...]
  2336. ...:=f(10,20));
  2337. [...]
  2338. is still folded. (FK)
  2339. }
  2340. ((para.parasym.varspez = vs_const) and
  2341. { const para's can get vs_readwritten if their address }
  2342. { is taken }
  2343. ((para.parasym.varstate = vs_readwritten) or
  2344. { call-by-reference const's may need to be passed by }
  2345. { reference to function called in the inlined code }
  2346. (paramanager.push_addr_param(vs_const,para.parasym.vardef,procdefinition.proccalloption) and
  2347. (not valid_for_addr(para.left,false) or
  2348. is_constnode(para.left)))))
  2349. )
  2350. ) then
  2351. begin
  2352. tempnode:=nil;
  2353. {$ifdef reuse_existing_para_temp}
  2354. { Try to reuse existing result tempnode from a parameter }
  2355. if para.left.nodetype=blockn then
  2356. begin
  2357. n:=tstatementnode(tblocknode(para.left).left);
  2358. while assigned(n) and assigned(tstatementnode(n).right) do
  2359. begin
  2360. if tstatementnode(n).left.nodetype=tempdeleten then
  2361. break;
  2362. n:=tstatementnode(tstatementnode(n).right);
  2363. end;
  2364. { We expect to find the following statements
  2365. tempdeletenode
  2366. tempref
  2367. nil }
  2368. if assigned(n) and
  2369. assigned(tstatementnode(n).right) and
  2370. (tstatementnode(tstatementnode(n).right).right=nil) and
  2371. (tstatementnode(tstatementnode(n).right).left.nodetype=temprefn) then
  2372. begin
  2373. tempnode:=ttempdeletenode(tstatementnode(n).left).tempinfo^.owner;
  2374. para.left:=tstatementnode(tstatementnode(n).right).left;
  2375. addstatement(deletestatement,tstatementnode(n).left);
  2376. { Replace tempdelete,tempref with dummy statement }
  2377. tstatementnode(n).left:=cnothingnode.create;
  2378. tstatementnode(tstatementnode(n).right).left:=cnothingnode.create;
  2379. end;
  2380. end;
  2381. {$endif reuse_existing_para_temp}
  2382. tempnode := ctempcreatenode.create(para.parasym.vardef,para.parasym.vardef.size,tt_persistent,tparavarsym(para.parasym).is_regvar(false));
  2383. addstatement(createstatement,tempnode);
  2384. { assign the value of the parameter to the temp, except in case of the function result }
  2385. { (in that case, para.left is a block containing the creation of a new temp, while we }
  2386. { only need a temprefnode, so delete the old stuff) }
  2387. if not(vo_is_funcret in tparavarsym(para.parasym).varoptions) then
  2388. begin
  2389. addstatement(createstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  2390. para.left));
  2391. para.left := ctemprefnode.create(tempnode);
  2392. addstatement(deletestatement,ctempdeletenode.create(tempnode));
  2393. end
  2394. else
  2395. begin
  2396. if not(assigned(funcretnode)) then
  2397. funcretnode := ctemprefnode.create(tempnode);
  2398. para.left.free;
  2399. para.left := ctemprefnode.create(tempnode);
  2400. addstatement(deletestatement,ctempdeletenode.create_normal_temp(tempnode));
  2401. end;
  2402. end
  2403. { otherwise if the parameter is "complex", take the address }
  2404. { of the parameter expression, store it in a temp and replace }
  2405. { occurrences of the parameter with dereferencings of this }
  2406. { temp }
  2407. else if (paracomplexity > 1) then
  2408. begin
  2409. tempnode := ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,tparavarsym(para.parasym).is_regvar(true));
  2410. addstatement(createstatement,tempnode);
  2411. addstatement(createstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  2412. caddrnode.create_internal(para.left)));
  2413. para.left := ctypeconvnode.create_internal(cderefnode.create(ctemprefnode.create(tempnode)),para.left.resultdef);
  2414. addstatement(deletestatement,ctempdeletenode.create(tempnode));
  2415. end;
  2416. end;
  2417. para := tcallparanode(para.right);
  2418. end;
  2419. { local variables }
  2420. if not assigned(tprocdef(procdefinition).localst) or
  2421. (tprocdef(procdefinition).localst.SymList.count = 0) then
  2422. exit;
  2423. tempnodes.createstatement := createstatement;
  2424. tempnodes.deletestatement := deletestatement;
  2425. inlinelocals.count:=tprocdef(procdefinition).localst.SymList.count;
  2426. tprocdef(procdefinition).localst.SymList.ForEachCall(@createlocaltemps,@tempnodes);
  2427. createstatement := tempnodes.createstatement;
  2428. deletestatement := tempnodes.deletestatement;
  2429. end;
  2430. function tcallnode.getfuncretassignment(inlineblock: tblocknode): tnode;
  2431. var
  2432. hp: tstatementnode;
  2433. resassign: tnode;
  2434. begin
  2435. result:=nil;
  2436. if not assigned(funcretnode) or
  2437. not(cnf_return_value_used in callnodeflags) then
  2438. exit;
  2439. { tempcreatenode for the function result }
  2440. hp:=tstatementnode(inlineblock.left);
  2441. if not(assigned(hp)) or
  2442. (hp.left.nodetype <> tempcreaten) then
  2443. exit;
  2444. { assignment to the result }
  2445. hp:=tstatementnode(hp.right);
  2446. if not(assigned(hp)) or
  2447. (hp.left.nodetype<>assignn) or
  2448. { left must be function result }
  2449. (not(tassignmentnode(hp.left).left.isequal(funcretnode)) and
  2450. { can have extra type conversion due to absolute mapping }
  2451. { of <fucntionname> on function result var }
  2452. not((tassignmentnode(hp.left).left.nodetype = typeconvn) and
  2453. (ttypeconvnode(tassignmentnode(hp.left).left).convtype = tc_equal) and
  2454. (ttypeconvnode(tassignmentnode(hp.left).left).left.isequal(funcretnode)))) or
  2455. { right must be a constant (mainly to avoid trying to reuse }
  2456. { local temps which may already be freed afterwards once these }
  2457. { checks are made looser) }
  2458. not is_constnode(tassignmentnode(hp.left).right) then
  2459. exit
  2460. else
  2461. resassign:=hp.left;
  2462. { tempdelete to normal of the function result }
  2463. hp:=tstatementnode(hp.right);
  2464. if not(assigned(hp)) or
  2465. (hp.left.nodetype <> tempdeleten) then
  2466. exit;
  2467. { the function result once more }
  2468. hp:=tstatementnode(hp.right);
  2469. if not(assigned(hp)) or
  2470. not(hp.left.isequal(funcretnode)) then
  2471. exit;
  2472. { should be the end }
  2473. if assigned(hp.right) then
  2474. exit;
  2475. { we made it! }
  2476. result:=tassignmentnode(resassign).right.getcopy;
  2477. firstpass(result);
  2478. end;
  2479. function tcallnode.pass1_inline:tnode;
  2480. var
  2481. createstatement,deletestatement: tstatementnode;
  2482. createblock,deleteblock: tblocknode;
  2483. body : tnode;
  2484. begin
  2485. if not(assigned(tprocdef(procdefinition).inlininginfo) and
  2486. assigned(tprocdef(procdefinition).inlininginfo^.code)) then
  2487. internalerror(200412021);
  2488. { inherit flags }
  2489. current_procinfo.flags := current_procinfo.flags + ((procdefinition as tprocdef).inlininginfo^.flags*inherited_inlining_flags);
  2490. { create blocks for loading/deleting of local data }
  2491. createblock:=internalstatements(createstatement);
  2492. deleteblock:=internalstatements(deletestatement);
  2493. { add methodpointer init code to init statement }
  2494. { (fini must be done later, as it will delete the hookoncopy info) }
  2495. if assigned(methodpointerinit) then
  2496. addstatement(createstatement,methodpointerinit.getcopy);
  2497. inlinelocals:=TFPObjectList.create(true);
  2498. { get copy of the procedure body }
  2499. body:=tprocdef(procdefinition).inlininginfo^.code.getcopy;
  2500. { replace complex parameters with temps }
  2501. createinlineparas(createstatement,deletestatement);
  2502. { replace the parameter loads with the parameter values }
  2503. foreachnode(body,@replaceparaload,@fileinfo);
  2504. { copy methodpointer fini code }
  2505. if assigned(methodpointerdone) then
  2506. addstatement(deletestatement,methodpointerdone.getcopy);
  2507. { free the temps for the locals }
  2508. inlinelocals.free;
  2509. inlinelocals:=nil;
  2510. addstatement(createstatement,body);
  2511. addstatement(createstatement,deleteblock);
  2512. { set function result location if necessary }
  2513. if assigned(funcretnode) and
  2514. (cnf_return_value_used in callnodeflags) then
  2515. addstatement(createstatement,funcretnode.getcopy);
  2516. { consider it must not be inlined if called
  2517. again inside the args or itself }
  2518. exclude(procdefinition.procoptions,po_inline);
  2519. dosimplify(createblock);
  2520. firstpass(createblock);
  2521. include(procdefinition.procoptions,po_inline);
  2522. { if all that's left of the inlined function is an constant }
  2523. { assignment to the result, replace the whole block with what's }
  2524. { assigned to the result. There will also be a tempcreatenode for }
  2525. { the function result itself though, so ignore it. The statement/ }
  2526. { blocknode simplification code will have removed all nothingn- }
  2527. { statements empty nested blocks, so we don't have to care about }
  2528. { those }
  2529. result := getfuncretassignment(createblock);
  2530. if assigned(result) then
  2531. createblock.free
  2532. else
  2533. { return inlined block }
  2534. result := createblock;
  2535. {$ifdef DEBUGINLINE}
  2536. writeln;
  2537. writeln('**************************',tprocdef(procdefinition).mangledname);
  2538. printnode(output,result);
  2539. {$endif DEBUGINLINE}
  2540. end;
  2541. procedure tcallnode.check_stack_parameters;
  2542. var
  2543. hp : tcallparanode;
  2544. begin
  2545. hp:=tcallparanode(left);
  2546. while assigned(hp) do
  2547. begin
  2548. if assigned(hp.parasym) and
  2549. assigned(hp.parasym.paraloc[callerside].location) and
  2550. (hp.parasym.paraloc[callerside].location^.loc=LOC_REFERENCE) then
  2551. include(current_procinfo.flags,pi_has_stackparameter);
  2552. hp:=tcallparanode(hp.right);
  2553. end;
  2554. end;
  2555. function tcallnode.pass_1 : tnode;
  2556. var
  2557. st : TSymtable;
  2558. begin
  2559. result:=nil;
  2560. { Can we inline the procedure? }
  2561. if ([po_inline,po_has_inlininginfo] <= procdefinition.procoptions) then
  2562. begin
  2563. { Check if we can inline the procedure when it references proc/var that
  2564. are not in the globally available }
  2565. st:=procdefinition.owner;
  2566. if (st.symtabletype=ObjectSymtable) then
  2567. st:=st.defowner.owner;
  2568. if (pi_uses_static_symtable in tprocdef(procdefinition).inlininginfo^.flags) and
  2569. (st.symtabletype=globalsymtable) and
  2570. (not st.iscurrentunit) then
  2571. begin
  2572. Comment(V_lineinfo+V_Debug,'Not inlining "'+tprocdef(procdefinition).procsym.realname+'", references static symtable');
  2573. end
  2574. else
  2575. begin
  2576. result:=pass1_inline;
  2577. exit;
  2578. end;
  2579. end;
  2580. { calculate the parameter info for the procdef }
  2581. if not procdefinition.has_paraloc_info then
  2582. begin
  2583. procdefinition.requiredargarea:=paramanager.create_paraloc_info(procdefinition,callerside);
  2584. procdefinition.has_paraloc_info:=true;
  2585. end;
  2586. { calculate the parameter size needed for this call include varargs if they are available }
  2587. if assigned(varargsparas) then
  2588. pushedparasize:=paramanager.create_varargs_paraloc_info(procdefinition,varargsparas)
  2589. else
  2590. pushedparasize:=procdefinition.requiredargarea;
  2591. { record maximum parameter size used in this proc }
  2592. current_procinfo.allocate_push_parasize(pushedparasize);
  2593. { work trough all parameters to get the register requirements }
  2594. if assigned(left) then
  2595. begin
  2596. tcallparanode(left).det_registers;
  2597. if (current_settings.optimizerswitches*[cs_opt_stackframe,cs_opt_level1]<>[]) then
  2598. begin
  2599. { check for stacked parameters }
  2600. check_stack_parameters;
  2601. end;
  2602. end;
  2603. { order parameters }
  2604. order_parameters;
  2605. if assigned(methodpointerinit) then
  2606. firstpass(methodpointerinit);
  2607. if assigned(methodpointerdone) then
  2608. firstpass(methodpointerdone);
  2609. { function result node }
  2610. if assigned(_funcretnode) then
  2611. firstpass(_funcretnode);
  2612. { procedure variable ? }
  2613. if assigned(right) then
  2614. firstpass(right);
  2615. if not (block_type in [bt_const,bt_type]) then
  2616. include(current_procinfo.flags,pi_do_call);
  2617. { implicit finally needed ? }
  2618. if resultdef.needs_inittable and
  2619. not paramanager.ret_in_param(resultdef,procdefinition.proccalloption) and
  2620. not assigned(funcretnode) then
  2621. include(current_procinfo.flags,pi_needs_implicit_finally);
  2622. { get a register for the return value }
  2623. if (not is_void(resultdef)) then
  2624. begin
  2625. if paramanager.ret_in_param(resultdef,procdefinition.proccalloption) then
  2626. begin
  2627. expectloc:=LOC_REFERENCE;
  2628. end
  2629. else
  2630. { ansi/widestrings must be registered, so we can dispose them }
  2631. if is_ansistring(resultdef) or
  2632. is_widestring(resultdef) then
  2633. begin
  2634. expectloc:=LOC_REFERENCE;
  2635. registersint:=1;
  2636. end
  2637. else
  2638. { we have only to handle the result if it is used }
  2639. if (cnf_return_value_used in callnodeflags) then
  2640. begin
  2641. case resultdef.typ of
  2642. enumdef,
  2643. orddef :
  2644. begin
  2645. if (procdefinition.proctypeoption=potype_constructor) then
  2646. begin
  2647. expectloc:=LOC_REGISTER;
  2648. registersint:=1;
  2649. end
  2650. else
  2651. begin
  2652. expectloc:=LOC_REGISTER;
  2653. if is_64bit(resultdef) then
  2654. registersint:=2
  2655. else
  2656. registersint:=1;
  2657. end;
  2658. end;
  2659. floatdef :
  2660. begin
  2661. expectloc:=LOC_FPUREGISTER;
  2662. {$ifdef cpufpemu}
  2663. if (cs_fp_emulation in current_settings.moduleswitches) then
  2664. registersint:=1
  2665. else
  2666. {$endif cpufpemu}
  2667. {$ifdef m68k}
  2668. if (tfloatdef(resultdef).floattype=s32real) then
  2669. registersint:=1
  2670. else
  2671. {$endif m68k}
  2672. registersfpu:=1;
  2673. end;
  2674. else
  2675. begin
  2676. expectloc:=procdefinition.funcretloc[callerside].loc;
  2677. if (expectloc = LOC_REGISTER) then
  2678. {$ifndef cpu64bit}
  2679. if (resultdef.size > sizeof(aint)) then
  2680. registersint:=2
  2681. else
  2682. {$endif cpu64bit}
  2683. registersint:=1
  2684. else
  2685. registersint:=0;
  2686. end;
  2687. end;
  2688. end
  2689. else
  2690. expectloc:=LOC_VOID;
  2691. end
  2692. else
  2693. expectloc:=LOC_VOID;
  2694. {$ifdef m68k}
  2695. { we need one more address register for virtual calls on m68k }
  2696. if (po_virtualmethod in procdefinition.procoptions) then
  2697. inc(registersint);
  2698. {$endif m68k}
  2699. { a fpu can be used in any procedure !! }
  2700. {$ifdef i386}
  2701. registersfpu:=procdefinition.fpu_used;
  2702. {$endif i386}
  2703. { if this is a call to a method calc the registers }
  2704. if (methodpointer<>nil) then
  2705. begin
  2706. if methodpointer.nodetype<>typen then
  2707. begin
  2708. firstpass(methodpointer);
  2709. registersfpu:=max(methodpointer.registersfpu,registersfpu);
  2710. registersint:=max(methodpointer.registersint,registersint);
  2711. {$ifdef SUPPORT_MMX }
  2712. registersmmx:=max(methodpointer.registersmmx,registersmmx);
  2713. {$endif SUPPORT_MMX}
  2714. end;
  2715. end;
  2716. { determine the registers of the procedure variable }
  2717. { is this OK for inlined procs also ?? (PM) }
  2718. if assigned(right) then
  2719. begin
  2720. registersfpu:=max(right.registersfpu,registersfpu);
  2721. registersint:=max(right.registersint,registersint);
  2722. {$ifdef SUPPORT_MMX}
  2723. registersmmx:=max(right.registersmmx,registersmmx);
  2724. {$endif SUPPORT_MMX}
  2725. end;
  2726. { determine the registers of the procedure }
  2727. if assigned(left) then
  2728. begin
  2729. registersfpu:=max(left.registersfpu,registersfpu);
  2730. registersint:=max(left.registersint,registersint);
  2731. {$ifdef SUPPORT_MMX}
  2732. registersmmx:=max(left.registersmmx,registersmmx);
  2733. {$endif SUPPORT_MMX}
  2734. end;
  2735. end;
  2736. {$ifdef state_tracking}
  2737. function Tcallnode.track_state_pass(exec_known:boolean):boolean;
  2738. var hp:Tcallparanode;
  2739. value:Tnode;
  2740. begin
  2741. track_state_pass:=false;
  2742. hp:=Tcallparanode(left);
  2743. while assigned(hp) do
  2744. begin
  2745. if left.track_state_pass(exec_known) then
  2746. begin
  2747. left.resultdef:=nil;
  2748. do_typecheckpass(left);
  2749. end;
  2750. value:=aktstate.find_fact(hp.left);
  2751. if value<>nil then
  2752. begin
  2753. track_state_pass:=true;
  2754. hp.left.destroy;
  2755. hp.left:=value.getcopy;
  2756. do_typecheckpass(hp.left);
  2757. end;
  2758. hp:=Tcallparanode(hp.right);
  2759. end;
  2760. end;
  2761. {$endif}
  2762. function tcallnode.para_count:longint;
  2763. var
  2764. ppn : tcallparanode;
  2765. begin
  2766. result:=0;
  2767. ppn:=tcallparanode(left);
  2768. while assigned(ppn) do
  2769. begin
  2770. if not(assigned(ppn.parasym) and
  2771. (vo_is_hidden_para in ppn.parasym.varoptions)) then
  2772. inc(result);
  2773. ppn:=tcallparanode(ppn.right);
  2774. end;
  2775. end;
  2776. function tcallnode.get_load_methodpointer:tnode;
  2777. var
  2778. newstatement : tstatementnode;
  2779. begin
  2780. if assigned(methodpointerinit) then
  2781. begin
  2782. result:=internalstatements(newstatement);
  2783. addstatement(newstatement,methodpointerinit);
  2784. addstatement(newstatement,methodpointer);
  2785. addstatement(newstatement,methodpointerdone);
  2786. methodpointerinit:=nil;
  2787. methodpointer:=nil;
  2788. methodpointerdone:=nil;
  2789. end
  2790. else
  2791. begin
  2792. result:=methodpointer;
  2793. methodpointer:=nil;
  2794. end;
  2795. end;
  2796. function tcallnode.docompare(p: tnode): boolean;
  2797. begin
  2798. docompare :=
  2799. inherited docompare(p) and
  2800. (symtableprocentry = tcallnode(p).symtableprocentry) and
  2801. (procdefinition = tcallnode(p).procdefinition) and
  2802. (methodpointer.isequal(tcallnode(p).methodpointer)) and
  2803. (((cnf_typedefset in callnodeflags) and (cnf_typedefset in tcallnode(p).callnodeflags) and
  2804. (equal_defs(typedef,tcallnode(p).typedef))) or
  2805. (not(cnf_typedefset in callnodeflags) and not(cnf_typedefset in tcallnode(p).callnodeflags)));
  2806. end;
  2807. procedure tcallnode.printnodedata(var t:text);
  2808. begin
  2809. if assigned(procdefinition) and
  2810. (procdefinition.typ=procdef) then
  2811. writeln(t,printnodeindention,'proc = ',tprocdef(procdefinition).fullprocname(true))
  2812. else
  2813. begin
  2814. if assigned(symtableprocentry) then
  2815. writeln(t,printnodeindention,'proc = ',symtableprocentry.name)
  2816. else
  2817. writeln(t,printnodeindention,'proc = <nil>');
  2818. end;
  2819. if assigned(methodpointer) then
  2820. begin
  2821. writeln(t,printnodeindention,'methodpointer =');
  2822. printnode(t,methodpointer);
  2823. end;
  2824. if assigned(methodpointerinit) then
  2825. begin
  2826. writeln(t,printnodeindention,'methodpointerinit =');
  2827. printnode(t,methodpointerinit);
  2828. end;
  2829. if assigned(methodpointerdone) then
  2830. begin
  2831. writeln(t,printnodeindention,'methodpointerdone =');
  2832. printnode(t,methodpointerdone);
  2833. end;
  2834. if assigned(right) then
  2835. begin
  2836. writeln(t,printnodeindention,'right =');
  2837. printnode(t,right);
  2838. end;
  2839. if assigned(left) then
  2840. begin
  2841. writeln(t,printnodeindention,'left =');
  2842. printnode(t,left);
  2843. end;
  2844. end;
  2845. begin
  2846. ccallnode:=tcallnode;
  2847. ccallparanode:=tcallparanode;
  2848. end.