ncal.pas 136 KB

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