ncal.pas 164 KB

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