ncal.pas 171 KB

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