ncal.pas 125 KB

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