ncal.pas 193 KB

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