ncal.pas 149 KB

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