ncal.pas 166 KB

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