ncal.pas 178 KB

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