ncal.pas 118 KB

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