ncal.pas 175 KB

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