ncal.pas 151 KB

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