ncal.pas 147 KB

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