ncal.pas 179 KB

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