ncal.pas 191 KB

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