ncal.pas 117 KB

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