ncal.pas 163 KB

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