ncal.pas 166 KB

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