ncal.pas 127 KB

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