ncal.pas 188 KB

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