ncal.pas 117 KB

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