ncal.pas 155 KB

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