ncal.pas 174 KB

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