ncal.pas 155 KB

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