ncal.pas 170 KB

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