ncal.pas 170 KB

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