ncal.pas 163 KB

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