ncal.pas 160 KB

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