ncal.pas 160 KB

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