ncal.pas 174 KB

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