ncal.pas 145 KB

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