ncal.pas 190 KB

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