ncal.pas 147 KB

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