ncal.pas 154 KB

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