ncal.pas 154 KB

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