ncal.pas 136 KB

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