ncal.pas 174 KB

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