ncal.pas 150 KB

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