ncal.pas 138 KB

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