ncal.pas 150 KB

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