ncal.pas 146 KB

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