ncal.pas 164 KB

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