ncal.pas 171 KB

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