ncal.pas 156 KB

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