ncal.pas 117 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999
  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 : TFPHashList;
  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. j : integer;
  1204. begin
  1205. if (tsym(sym).typ<>procsym) then
  1206. exit;
  1207. for i:=0 to tprocsym(sym).ProcdefList.Count-1 do
  1208. begin
  1209. pd:=tprocdef(tprocsym(sym).ProcdefList[i]);
  1210. j:=AbstractMethodsList.FindIndexOf(pd.procsym.name);
  1211. if j<>-1 then
  1212. AbstractMethodsList[j]:=pd
  1213. else
  1214. AbstractMethodsList.Add(pd.procsym.name,pd);
  1215. end;
  1216. end;
  1217. procedure tcallnode.verifyabstractcalls;
  1218. var
  1219. objectdf : tobjectdef;
  1220. parents : tlinkedlist;
  1221. objectinfo : tobjectinfoitem;
  1222. stritem : tstringlistitem;
  1223. pd : tprocdef;
  1224. i : integer;
  1225. first : boolean;
  1226. begin
  1227. objectdf := nil;
  1228. { verify if trying to create an instance of a class which contains
  1229. non-implemented abstract methods }
  1230. { first verify this class type, no class than exit }
  1231. { also, this checking can only be done if the constructor is directly
  1232. called, indirect constructor calls cannot be checked.
  1233. }
  1234. if assigned(methodpointer) and
  1235. not (nf_is_self in methodpointer.flags) then
  1236. begin
  1237. if (methodpointer.resultdef.typ = objectdef) then
  1238. objectdf:=tobjectdef(methodpointer.resultdef)
  1239. else
  1240. if (methodpointer.resultdef.typ = classrefdef) and
  1241. (tclassrefdef(methodpointer.resultdef).pointeddef.typ = objectdef) and
  1242. (methodpointer.nodetype in [typen,loadvmtaddrn]) then
  1243. objectdf:=tobjectdef(tclassrefdef(methodpointer.resultdef).pointeddef);
  1244. end;
  1245. if not assigned(objectdf) then
  1246. exit;
  1247. parents := tlinkedlist.create;
  1248. AbstractMethodsList := TFPHashList.create;
  1249. { insert all parents in this class : the first item in the
  1250. list will be the base parent of the class .
  1251. }
  1252. while assigned(objectdf) do
  1253. begin
  1254. objectinfo:=tobjectinfoitem.create(objectdf);
  1255. parents.insert(objectinfo);
  1256. objectdf := objectdf.childof;
  1257. end;
  1258. { now all parents are in the correct order
  1259. insert all abstract methods in the list, and remove
  1260. those which are overriden by parent classes.
  1261. }
  1262. objectinfo:=tobjectinfoitem(parents.first);
  1263. while assigned(objectinfo) do
  1264. begin
  1265. objectdf := objectinfo.objinfo;
  1266. if assigned(objectdf.symtable) then
  1267. objectdf.symtable.SymList.ForEachCall(@verifyabstract,nil);
  1268. objectinfo:=tobjectinfoitem(objectinfo.next);
  1269. end;
  1270. if assigned(parents) then
  1271. parents.free;
  1272. { Finally give out a warning for each abstract method still in the list }
  1273. first:=true;
  1274. for i:=0 to AbstractMethodsList.Count-1 do
  1275. begin
  1276. pd:=tprocdef(AbstractMethodsList[i]);
  1277. if po_abstractmethod in pd.procoptions then
  1278. begin
  1279. if first then
  1280. begin
  1281. Message1(type_w_instance_with_abstract,objectdf.objrealname^);
  1282. first:=false;
  1283. end;
  1284. MessagePos1(pd.fileinfo,sym_h_abstract_method_list,pd.fullprocname(true));
  1285. end;
  1286. end;
  1287. if assigned(AbstractMethodsList) then
  1288. AbstractMethodsList.Free;
  1289. end;
  1290. function tcallnode.gen_self_tree_methodpointer:tnode;
  1291. var
  1292. hsym : tfieldvarsym;
  1293. begin
  1294. { find self field in methodpointer record }
  1295. hsym:=tfieldvarsym(trecorddef(methodpointertype).symtable.Find('self'));
  1296. if not assigned(hsym) then
  1297. internalerror(200305251);
  1298. { Load tmehodpointer(right).self }
  1299. result:=csubscriptnode.create(
  1300. hsym,
  1301. ctypeconvnode.create_internal(right.getcopy,methodpointertype));
  1302. end;
  1303. function tcallnode.gen_self_tree:tnode;
  1304. var
  1305. selftree : tnode;
  1306. begin
  1307. selftree:=nil;
  1308. { inherited }
  1309. if (cnf_inherited in callnodeflags) then
  1310. selftree:=load_self_node
  1311. else
  1312. { constructors }
  1313. if (procdefinition.proctypeoption=potype_constructor) then
  1314. begin
  1315. { push 0 as self when allocation is needed }
  1316. if (methodpointer.resultdef.typ=classrefdef) or
  1317. (cnf_new_call in callnodeflags) then
  1318. selftree:=cpointerconstnode.create(0,voidpointertype)
  1319. else
  1320. begin
  1321. if methodpointer.nodetype=typen then
  1322. selftree:=load_self_node
  1323. else
  1324. selftree:=methodpointer.getcopy;
  1325. end;
  1326. end
  1327. else
  1328. { Calling a static/class method }
  1329. if (po_classmethod in procdefinition.procoptions) or
  1330. (po_staticmethod in procdefinition.procoptions) then
  1331. begin
  1332. if (procdefinition.typ<>procdef) then
  1333. internalerror(200305062);
  1334. if (oo_has_vmt in tprocdef(procdefinition)._class.objectoptions) then
  1335. begin
  1336. { we only need the vmt, loading self is not required and there is no
  1337. need to check for typen, because that will always get the
  1338. loadvmtaddrnode added }
  1339. selftree:=methodpointer.getcopy;
  1340. if methodpointer.resultdef.typ<>classrefdef then
  1341. selftree:=cloadvmtaddrnode.create(selftree);
  1342. end
  1343. else
  1344. selftree:=cpointerconstnode.create(0,voidpointertype);
  1345. end
  1346. else
  1347. begin
  1348. if methodpointer.nodetype=typen then
  1349. selftree:=load_self_node
  1350. else
  1351. selftree:=methodpointer.getcopy;
  1352. end;
  1353. result:=selftree;
  1354. end;
  1355. function tcallnode.gen_vmt_tree:tnode;
  1356. var
  1357. vmttree : tnode;
  1358. begin
  1359. vmttree:=nil;
  1360. if not(procdefinition.proctypeoption in [potype_constructor,potype_destructor]) then
  1361. internalerror(200305051);
  1362. { Handle classes and legacy objects separate to make it
  1363. more maintainable }
  1364. if (methodpointer.resultdef.typ=classrefdef) then
  1365. begin
  1366. if not is_class(tclassrefdef(methodpointer.resultdef).pointeddef) then
  1367. internalerror(200501041);
  1368. { constructor call via classreference => allocate memory }
  1369. if (procdefinition.proctypeoption=potype_constructor) then
  1370. begin
  1371. vmttree:=methodpointer.getcopy;
  1372. { Only a typenode can be passed when it is called with <class of xx>.create }
  1373. if vmttree.nodetype=typen then
  1374. vmttree:=cloadvmtaddrnode.create(vmttree);
  1375. end
  1376. else
  1377. begin
  1378. { Call afterconstruction }
  1379. vmttree:=cpointerconstnode.create(1,voidpointertype);
  1380. end;
  1381. end
  1382. else
  1383. { Class style objects }
  1384. if is_class(methodpointer.resultdef) then
  1385. begin
  1386. { inherited call, no create/destroy }
  1387. if (cnf_inherited in callnodeflags) then
  1388. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1389. else
  1390. { do not create/destroy when called from member function
  1391. without specifying self explicit }
  1392. if (cnf_member_call in callnodeflags) then
  1393. begin
  1394. { destructor: don't release instance, vmt=0
  1395. constructor:
  1396. if called from a constructor in the same class then
  1397. don't call afterconstruction, vmt=0
  1398. else
  1399. call afterconstrution, vmt=1 }
  1400. if (procdefinition.proctypeoption=potype_destructor) then
  1401. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1402. else
  1403. begin
  1404. if (current_procinfo.procdef.proctypeoption=potype_constructor) and
  1405. (procdefinition.proctypeoption=potype_constructor) then
  1406. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1407. else
  1408. vmttree:=cpointerconstnode.create(1,voidpointertype);
  1409. end;
  1410. end
  1411. else
  1412. { normal call to method like cl1.proc }
  1413. begin
  1414. { destructor: release instance, vmt=1
  1415. constructor:
  1416. if called from a constructor in the same class using self.create then
  1417. don't call afterconstruction, vmt=0
  1418. else
  1419. call afterconstrution, vmt=1 }
  1420. if (procdefinition.proctypeoption=potype_destructor) then
  1421. vmttree:=cpointerconstnode.create(1,voidpointertype)
  1422. else
  1423. begin
  1424. if (current_procinfo.procdef.proctypeoption=potype_constructor) and
  1425. (procdefinition.proctypeoption=potype_constructor) and
  1426. (nf_is_self in methodpointer.flags) then
  1427. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1428. else
  1429. vmttree:=cpointerconstnode.create(1,voidpointertype);
  1430. end;
  1431. end;
  1432. end
  1433. else
  1434. { Old style object }
  1435. begin
  1436. { constructor with extended syntax called from new }
  1437. if (cnf_new_call in callnodeflags) then
  1438. vmttree:=cloadvmtaddrnode.create(ctypenode.create(methodpointer.resultdef))
  1439. else
  1440. { destructor with extended syntax called from dispose }
  1441. if (cnf_dispose_call in callnodeflags) then
  1442. vmttree:=cloadvmtaddrnode.create(methodpointer.getcopy)
  1443. else
  1444. { inherited call, no create/destroy }
  1445. if (cnf_inherited in callnodeflags) then
  1446. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1447. else
  1448. { do not create/destroy when called from member function
  1449. without specifying self explicit }
  1450. if (cnf_member_call in callnodeflags) then
  1451. begin
  1452. { destructor: don't release instance, vmt=0
  1453. constructor: don't initialize instance, vmt=0 }
  1454. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1455. end
  1456. else
  1457. { normal object call like obj.proc }
  1458. begin
  1459. { destructor: direct call, no dispose, vmt=0
  1460. constructor: initialize object, load vmt }
  1461. if (procdefinition.proctypeoption=potype_constructor) then
  1462. begin
  1463. { old styled inherited call? }
  1464. if (methodpointer.nodetype=typen) then
  1465. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1466. else
  1467. vmttree:=cloadvmtaddrnode.create(ctypenode.create(methodpointer.resultdef))
  1468. end
  1469. else
  1470. vmttree:=cpointerconstnode.create(0,voidpointertype);
  1471. end;
  1472. end;
  1473. result:=vmttree;
  1474. end;
  1475. type
  1476. pcallparanode = ^tcallparanode;
  1477. procedure tcallnode.bind_parasym;
  1478. var
  1479. i : integer;
  1480. pt : tcallparanode;
  1481. oldppt : pcallparanode;
  1482. varargspara,
  1483. currpara : tparavarsym;
  1484. used_by_callnode : boolean;
  1485. hiddentree : tnode;
  1486. newstatement : tstatementnode;
  1487. temp : ttempcreatenode;
  1488. begin
  1489. pt:=tcallparanode(left);
  1490. oldppt:=pcallparanode(@left);
  1491. { flag all callparanodes that belong to the varargs }
  1492. i:=paralength;
  1493. while (i>procdefinition.maxparacount) do
  1494. begin
  1495. include(pt.callparaflags,cpf_varargs_para);
  1496. oldppt:=pcallparanode(@pt.right);
  1497. pt:=tcallparanode(pt.right);
  1498. dec(i);
  1499. end;
  1500. { skip varargs that are inserted by array of const }
  1501. while assigned(pt) and
  1502. (cpf_varargs_para in pt.callparaflags) do
  1503. pt:=tcallparanode(pt.right);
  1504. { process normal parameters and insert hidden parameters }
  1505. for i:=procdefinition.paras.count-1 downto 0 do
  1506. begin
  1507. currpara:=tparavarsym(procdefinition.paras[i]);
  1508. if vo_is_hidden_para in currpara.varoptions then
  1509. begin
  1510. { generate hidden tree }
  1511. used_by_callnode:=false;
  1512. hiddentree:=nil;
  1513. if (vo_is_funcret in currpara.varoptions) then
  1514. begin
  1515. { Generate funcretnode if not specified }
  1516. if assigned(funcretnode) then
  1517. begin
  1518. hiddentree:=funcretnode.getcopy;
  1519. end
  1520. else
  1521. begin
  1522. hiddentree:=internalstatements(newstatement);
  1523. { need to use resultdef instead of procdefinition.returndef,
  1524. because they can be different }
  1525. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  1526. addstatement(newstatement,temp);
  1527. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  1528. addstatement(newstatement,ctemprefnode.create(temp));
  1529. end;
  1530. end
  1531. else
  1532. if vo_is_high_para in currpara.varoptions then
  1533. begin
  1534. if not assigned(pt) or
  1535. (i=0) then
  1536. internalerror(200304082);
  1537. { we need the information of the previous parameter }
  1538. hiddentree:=gen_high_tree(pt.left,tparavarsym(procdefinition.paras[i-1]).vardef);
  1539. end
  1540. else
  1541. if vo_is_self in currpara.varoptions then
  1542. begin
  1543. if assigned(right) then
  1544. hiddentree:=gen_self_tree_methodpointer
  1545. else
  1546. hiddentree:=gen_self_tree;
  1547. end
  1548. else
  1549. if vo_is_vmt in currpara.varoptions then
  1550. begin
  1551. hiddentree:=gen_vmt_tree;
  1552. end
  1553. {$if defined(powerpc) or defined(m68k)}
  1554. else
  1555. if vo_is_syscall_lib in currpara.varoptions then
  1556. begin
  1557. { lib parameter has no special type but proccalloptions must be a syscall }
  1558. hiddentree:=cloadnode.create(tprocdef(procdefinition).libsym,tprocdef(procdefinition).libsym.owner);
  1559. end
  1560. {$endif powerpc or m68k}
  1561. else
  1562. if vo_is_parentfp in currpara.varoptions then
  1563. begin
  1564. if not(assigned(procdefinition.owner.defowner)) then
  1565. internalerror(200309287);
  1566. hiddentree:=cloadparentfpnode.create(tprocdef(procdefinition.owner.defowner));
  1567. end;
  1568. { add the hidden parameter }
  1569. if not assigned(hiddentree) then
  1570. internalerror(200304073);
  1571. { Already insert para and let the previous node point to
  1572. this new node }
  1573. pt:=ccallparanode.create(hiddentree,oldppt^);
  1574. pt.used_by_callnode:=used_by_callnode;
  1575. oldppt^:=pt;
  1576. end;
  1577. if not assigned(pt) then
  1578. internalerror(200310052);
  1579. pt.parasym:=currpara;
  1580. oldppt:=pcallparanode(@pt.right);
  1581. pt:=tcallparanode(pt.right);
  1582. end;
  1583. { Create parasyms for varargs, first count the number of varargs paras,
  1584. then insert the parameters with numbering in reverse order. The SortParas
  1585. will set the correct order at the end}
  1586. pt:=tcallparanode(left);
  1587. i:=0;
  1588. while assigned(pt) do
  1589. begin
  1590. if cpf_varargs_para in pt.callparaflags then
  1591. inc(i);
  1592. pt:=tcallparanode(pt.right);
  1593. end;
  1594. if (i>0) then
  1595. begin
  1596. varargsparas:=tvarargsparalist.create;
  1597. pt:=tcallparanode(left);
  1598. while assigned(pt) do
  1599. begin
  1600. if cpf_varargs_para in pt.callparaflags then
  1601. begin
  1602. varargspara:=tparavarsym.create('va'+tostr(i),i,vs_value,pt.resultdef,[]);
  1603. dec(i);
  1604. { varargspara is left-right, use insert
  1605. instead of concat }
  1606. varargsparas.add(varargspara);
  1607. pt.parasym:=varargspara;
  1608. end;
  1609. pt:=tcallparanode(pt.right);
  1610. end;
  1611. varargsparas.sortparas;
  1612. end;
  1613. end;
  1614. function tcallnode.pass_typecheck:tnode;
  1615. var
  1616. candidates : tcallcandidates;
  1617. oldcallnode : tcallnode;
  1618. hpt : tnode;
  1619. pt : tcallparanode;
  1620. lastpara : longint;
  1621. paraidx,
  1622. cand_cnt : integer;
  1623. i : longint;
  1624. is_const : boolean;
  1625. statements : tstatementnode;
  1626. converted_result_data : ttempcreatenode;
  1627. label
  1628. errorexit;
  1629. begin
  1630. result:=nil;
  1631. candidates:=nil;
  1632. oldcallnode:=aktcallnode;
  1633. aktcallnode:=self;
  1634. { determine length of parameter list }
  1635. pt:=tcallparanode(left);
  1636. paralength:=0;
  1637. while assigned(pt) do
  1638. begin
  1639. inc(paralength);
  1640. pt:=tcallparanode(pt.right);
  1641. end;
  1642. { determine the type of the parameters }
  1643. if assigned(left) then
  1644. begin
  1645. tcallparanode(left).get_paratype;
  1646. if codegenerror then
  1647. goto errorexit;
  1648. end;
  1649. if assigned(methodpointer) then
  1650. begin
  1651. typecheckpass(methodpointer);
  1652. maybe_load_para_in_temp(methodpointer);
  1653. end;
  1654. { procedure variable ? }
  1655. if assigned(right) then
  1656. begin
  1657. set_varstate(right,vs_read,[vsf_must_be_valid]);
  1658. typecheckpass(right);
  1659. if codegenerror then
  1660. exit;
  1661. procdefinition:=tabstractprocdef(right.resultdef);
  1662. { Compare parameters from right to left }
  1663. paraidx:=procdefinition.Paras.count-1;
  1664. { Skip default parameters }
  1665. if not(po_varargs in procdefinition.procoptions) then
  1666. begin
  1667. { ignore hidden parameters }
  1668. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) do
  1669. dec(paraidx);
  1670. for i:=1 to procdefinition.maxparacount-paralength do
  1671. begin
  1672. if paraidx<0 then
  1673. internalerror(200402261);
  1674. if not assigned(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym) then
  1675. begin
  1676. CGMessage1(parser_e_wrong_parameter_size,'<Procedure Variable>');
  1677. goto errorexit;
  1678. end;
  1679. dec(paraidx);
  1680. end;
  1681. end;
  1682. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) do
  1683. dec(paraidx);
  1684. pt:=tcallparanode(left);
  1685. lastpara:=paralength;
  1686. while (paraidx>=0) and assigned(pt) do
  1687. begin
  1688. { only goto next para if we're out of the varargs }
  1689. if not(po_varargs in procdefinition.procoptions) or
  1690. (lastpara<=procdefinition.maxparacount) then
  1691. begin
  1692. repeat
  1693. dec(paraidx);
  1694. until (paraidx<0) or not(vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions);
  1695. end;
  1696. pt:=tcallparanode(pt.right);
  1697. dec(lastpara);
  1698. end;
  1699. if assigned(pt) or
  1700. ((paraidx>=0) and
  1701. not assigned(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym)) then
  1702. begin
  1703. if assigned(pt) then
  1704. current_filepos:=pt.fileinfo;
  1705. CGMessage1(parser_e_wrong_parameter_size,symtableprocentry.realname);
  1706. goto errorexit;
  1707. end;
  1708. end
  1709. else
  1710. { not a procedure variable }
  1711. begin
  1712. { do we know the procedure to call ? }
  1713. if not(assigned(procdefinition)) then
  1714. begin
  1715. candidates:=tcallcandidates.create(symtableprocentry,symtableproc,left,(nf_isproperty in flags),
  1716. { ignore possible private in delphi mode for anon. inherited (FK) }
  1717. (m_delphi in current_settings.modeswitches) and (cnf_anon_inherited in callnodeflags));
  1718. { no procedures found? then there is something wrong
  1719. with the parameter size or the procedures are
  1720. not accessible }
  1721. if candidates.count=0 then
  1722. begin
  1723. { when it's an auto inherited call and there
  1724. is no procedure found, but the procedures
  1725. were defined with overload directive and at
  1726. least two procedures are defined then we ignore
  1727. this inherited by inserting a nothingn. Only
  1728. do this ugly hack in Delphi mode as it looks more
  1729. like a bug. It's also not documented }
  1730. if (m_delphi in current_settings.modeswitches) and
  1731. (cnf_anon_inherited in callnodeflags) and
  1732. (symtableprocentry.owner.symtabletype=ObjectSymtable) and
  1733. (po_overload in tprocdef(symtableprocentry.ProcdefList[0]).procoptions) and
  1734. (symtableprocentry.ProcdefList.Count>=2) then
  1735. result:=cnothingnode.create
  1736. else
  1737. begin
  1738. { in tp mode we can try to convert to procvar if
  1739. there are no parameters specified }
  1740. if not(assigned(left)) and
  1741. not(cnf_inherited in callnodeflags) and
  1742. ((m_tp_procvar in current_settings.modeswitches) or
  1743. (m_mac_procvar in current_settings.modeswitches)) and
  1744. (not assigned(methodpointer) or
  1745. (methodpointer.nodetype <> typen)) then
  1746. begin
  1747. hpt:=cloadnode.create(tprocsym(symtableprocentry),symtableproc);
  1748. if assigned(methodpointer) then
  1749. tloadnode(hpt).set_mp(get_load_methodpointer);
  1750. typecheckpass(hpt);
  1751. result:=hpt;
  1752. end
  1753. else
  1754. begin
  1755. if assigned(left) then
  1756. current_filepos:=left.fileinfo;
  1757. CGMessage1(parser_e_wrong_parameter_size,symtableprocentry.realname);
  1758. symtableprocentry.write_parameter_lists(nil);
  1759. end;
  1760. end;
  1761. goto errorexit;
  1762. end;
  1763. { Retrieve information about the candidates }
  1764. candidates.get_information;
  1765. {$ifdef EXTDEBUG}
  1766. { Display info when multiple candidates are found }
  1767. if candidates.count>1 then
  1768. candidates.dump_info(V_Debug);
  1769. {$endif EXTDEBUG}
  1770. { Choose the best candidate and count the number of
  1771. candidates left }
  1772. cand_cnt:=candidates.choose_best(procdefinition,
  1773. assigned(left) and
  1774. not assigned(tcallparanode(left).right) and
  1775. (tcallparanode(left).left.resultdef.typ=variantdef));
  1776. { All parameters are checked, check if there are any
  1777. procedures left }
  1778. if cand_cnt>0 then
  1779. begin
  1780. { Multiple candidates left? }
  1781. if cand_cnt>1 then
  1782. begin
  1783. CGMessage(type_e_cant_choose_overload_function);
  1784. {$ifdef EXTDEBUG}
  1785. candidates.dump_info(V_Hint);
  1786. {$else EXTDEBUG}
  1787. candidates.list(false);
  1788. {$endif EXTDEBUG}
  1789. { we'll just use the first candidate to make the
  1790. call }
  1791. end;
  1792. { assign procdefinition }
  1793. if symtableproc=nil then
  1794. symtableproc:=procdefinition.owner;
  1795. end
  1796. else
  1797. begin
  1798. { No candidates left, this must be a type error,
  1799. because wrong size is already checked. procdefinition
  1800. is filled with the first (random) definition that is
  1801. found. We use this definition to display a nice error
  1802. message that the wrong type is passed }
  1803. candidates.find_wrong_para;
  1804. candidates.list(true);
  1805. {$ifdef EXTDEBUG}
  1806. candidates.dump_info(V_Hint);
  1807. {$endif EXTDEBUG}
  1808. { We can not proceed, release all procs and exit }
  1809. candidates.free;
  1810. goto errorexit;
  1811. end;
  1812. candidates.free;
  1813. end; { end of procedure to call determination }
  1814. end;
  1815. { check for hints (deprecated etc) }
  1816. if (procdefinition.typ = procdef) then
  1817. check_hints(tprocdef(procdefinition).procsym,tprocdef(procdefinition).symoptions);
  1818. { add needed default parameters }
  1819. if assigned(procdefinition) and
  1820. (paralength<procdefinition.maxparacount) then
  1821. begin
  1822. paraidx:=0;
  1823. i:=0;
  1824. while (i<paralength) do
  1825. begin
  1826. if paraidx>=procdefinition.Paras.count then
  1827. internalerror(200306181);
  1828. if not(vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) then
  1829. inc(i);
  1830. inc(paraidx);
  1831. end;
  1832. while (paraidx<procdefinition.paras.count) and (vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) do
  1833. inc(paraidx);
  1834. while (paraidx<procdefinition.paras.count) do
  1835. begin
  1836. if not assigned(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym) then
  1837. internalerror(200212142);
  1838. left:=ccallparanode.create(genconstsymtree(
  1839. tconstsym(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym)),left);
  1840. { Ignore vs_hidden parameters }
  1841. repeat
  1842. inc(paraidx);
  1843. until (paraidx>=procdefinition.paras.count) or not(vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions);
  1844. end;
  1845. end;
  1846. { recursive call? }
  1847. if assigned(current_procinfo) and
  1848. (procdefinition=current_procinfo.procdef) then
  1849. include(current_procinfo.flags,pi_is_recursive);
  1850. { handle predefined procedures }
  1851. is_const:=(po_internconst in procdefinition.procoptions) and
  1852. ((block_type in [bt_const,bt_type]) or
  1853. (assigned(left) and (tcallparanode(left).left.nodetype in [realconstn,ordconstn])));
  1854. if (procdefinition.proccalloption=pocall_internproc) or is_const then
  1855. begin
  1856. if assigned(left) then
  1857. begin
  1858. { ptr and settextbuf needs two args }
  1859. if assigned(tcallparanode(left).right) then
  1860. begin
  1861. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,left);
  1862. left:=nil;
  1863. end
  1864. else
  1865. begin
  1866. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,tcallparanode(left).left);
  1867. tcallparanode(left).left:=nil;
  1868. end;
  1869. end
  1870. else
  1871. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,nil);
  1872. result:=hpt;
  1873. goto errorexit;
  1874. end;
  1875. { ensure that the result type is set }
  1876. if not(cnf_typedefset in callnodeflags) then
  1877. begin
  1878. { constructors return their current class type, not the type where the
  1879. constructor is declared, this can be different because of inheritance }
  1880. if (procdefinition.proctypeoption=potype_constructor) and
  1881. assigned(methodpointer) and
  1882. assigned(methodpointer.resultdef) and
  1883. (methodpointer.resultdef.typ=classrefdef) then
  1884. resultdef:=tclassrefdef(methodpointer.resultdef).pointeddef
  1885. else
  1886. { Member call to a (inherited) constructor from the class, the return
  1887. value is always self, so we change it to voidtype to generate an
  1888. error and to prevent users from generating non-working code
  1889. when they expect to clone the current instance, see bug 3662 (PFV) }
  1890. if (procdefinition.proctypeoption=potype_constructor) and
  1891. is_class(tprocdef(procdefinition)._class) and
  1892. assigned(methodpointer) and
  1893. (nf_is_self in methodpointer.flags) then
  1894. resultdef:=voidtype
  1895. else
  1896. resultdef:=procdefinition.returndef;
  1897. end
  1898. else
  1899. resultdef:=typedef;
  1900. {if resultdef.needs_inittable then
  1901. include(current_procinfo.flags,pi_needs_implicit_finally);}
  1902. if assigned(methodpointer) then
  1903. begin
  1904. { when methodpointer is a callnode we must load it first into a
  1905. temp to prevent the processing callnode twice }
  1906. if (methodpointer.nodetype=calln) then
  1907. internalerror(200405121);
  1908. { direct call to inherited abstract method, then we
  1909. can already give a error in the compiler instead
  1910. of a runtime error }
  1911. if (cnf_inherited in callnodeflags) and
  1912. (po_abstractmethod in procdefinition.procoptions) then
  1913. CGMessage(cg_e_cant_call_abstract_method);
  1914. { if an inherited con- or destructor should be }
  1915. { called in a con- or destructor then a warning }
  1916. { will be made }
  1917. { con- and destructors need a pointer to the vmt }
  1918. if (cnf_inherited in callnodeflags) and
  1919. (procdefinition.proctypeoption in [potype_constructor,potype_destructor]) and
  1920. is_object(methodpointer.resultdef) and
  1921. not(current_procinfo.procdef.proctypeoption in [potype_constructor,potype_destructor]) then
  1922. CGMessage(cg_w_member_cd_call_from_method);
  1923. if methodpointer.nodetype<>typen then
  1924. begin
  1925. { Remove all postfix operators }
  1926. hpt:=methodpointer;
  1927. while assigned(hpt) and (hpt.nodetype in [subscriptn,vecn]) do
  1928. hpt:=tunarynode(hpt).left;
  1929. if (procdefinition.proctypeoption=potype_constructor) and
  1930. assigned(symtableproc) and
  1931. (symtableproc.symtabletype=withsymtable) and
  1932. (tnode(twithsymtable(symtableproc).withrefnode).nodetype=temprefn) then
  1933. CGmessage(cg_e_cannot_call_cons_dest_inside_with);
  1934. { R.Init then R will be initialized by the constructor,
  1935. Also allow it for simple loads }
  1936. if (procdefinition.proctypeoption=potype_constructor) or
  1937. ((hpt.nodetype=loadn) and
  1938. (
  1939. (methodpointer.resultdef.typ=classrefdef) or
  1940. (
  1941. (methodpointer.resultdef.typ=objectdef) and
  1942. not(oo_has_virtual in tobjectdef(methodpointer.resultdef).objectoptions)
  1943. )
  1944. )
  1945. ) then
  1946. set_varstate(methodpointer,vs_read,[])
  1947. else
  1948. set_varstate(methodpointer,vs_read,[vsf_must_be_valid]);
  1949. { The object is already used if it is called once }
  1950. if (hpt.nodetype=loadn) and
  1951. (tloadnode(hpt).symtableentry.typ in [localvarsym,paravarsym,staticvarsym]) then
  1952. set_varstate(hpt,vs_read,[]);
  1953. // tabstractvarsym(tloadnode(hpt).symtableentry).varstate:=vs_readwritten;
  1954. end;
  1955. { if we are calling the constructor check for abstract
  1956. methods. Ignore inherited and member calls, because the
  1957. class is then already created }
  1958. if (procdefinition.proctypeoption=potype_constructor) and
  1959. not(cnf_inherited in callnodeflags) and
  1960. not(cnf_member_call in callnodeflags) then
  1961. verifyabstractcalls;
  1962. end
  1963. else
  1964. begin
  1965. { When this is method the methodpointer must be available }
  1966. if (right=nil) and
  1967. (procdefinition.owner.symtabletype=ObjectSymtable) then
  1968. internalerror(200305061);
  1969. end;
  1970. { Set flag that the procedure uses varargs, also if they are not passed it is still
  1971. needed for x86_64 to pass the number of SSE registers used }
  1972. if po_varargs in procdefinition.procoptions then
  1973. include(callnodeflags,cnf_uses_varargs);
  1974. { Change loading of array of const to varargs }
  1975. if assigned(left) and
  1976. is_array_of_const(tparavarsym(procdefinition.paras[procdefinition.paras.count-1]).vardef) and
  1977. (procdefinition.proccalloption in [pocall_cppdecl,pocall_cdecl]) then
  1978. convert_carg_array_of_const;
  1979. { bind parasyms to the callparanodes and insert hidden parameters }
  1980. bind_parasym;
  1981. { insert type conversions for parameters }
  1982. if assigned(left) then
  1983. tcallparanode(left).insert_typeconv(true);
  1984. { dispinterface methode invoke? }
  1985. if assigned(methodpointer) and is_dispinterface(methodpointer.resultdef) then
  1986. begin
  1987. { if the result is used, we've to insert a call to convert the type to be on the "safe side" }
  1988. if cnf_return_value_used in callnodeflags then
  1989. begin
  1990. result:=internalstatements(statements);
  1991. converted_result_data:=ctempcreatenode.create(procdefinition.returndef,sizeof(procdefinition.returndef),tt_persistent,true);
  1992. addstatement(statements,converted_result_data);
  1993. addstatement(statements,cassignmentnode.create(ctemprefnode.create(converted_result_data),
  1994. ctypeconvnode.create_internal(translate_disp_call(methodpointer,parameters,'',tprocdef(procdefinition).dispid),
  1995. procdefinition.returndef)));
  1996. addstatement(statements,ctempdeletenode.create_normal_temp(converted_result_data));
  1997. addstatement(statements,ctemprefnode.create(converted_result_data));
  1998. end
  1999. else
  2000. result:=translate_disp_call(methodpointer,parameters,'',tprocdef(procdefinition).dispid);
  2001. { don't free reused nodes }
  2002. methodpointer:=nil;
  2003. parameters:=nil;
  2004. end;
  2005. errorexit:
  2006. aktcallnode:=oldcallnode;
  2007. end;
  2008. procedure tcallnode.order_parameters;
  2009. var
  2010. hp,hpcurr,hpnext,hpfirst,hpprev : tcallparanode;
  2011. currloc : tcgloc;
  2012. begin
  2013. hpfirst:=nil;
  2014. hpcurr:=tcallparanode(left);
  2015. while assigned(hpcurr) do
  2016. begin
  2017. { pull out }
  2018. hpnext:=tcallparanode(hpcurr.right);
  2019. { pull in at the correct place.
  2020. Used order:
  2021. 1. LOC_REFERENCE with smallest offset (x86 only)
  2022. 2. LOC_REFERENCE with most registers
  2023. 3. LOC_REGISTER with most registers
  2024. For the moment we only look at the first parameter field. Combining it
  2025. with multiple parameter fields will make things a lot complexer (PFV) }
  2026. if not assigned(hpcurr.parasym.paraloc[callerside].location) then
  2027. internalerror(200412152);
  2028. currloc:=hpcurr.parasym.paraloc[callerside].location^.loc;
  2029. hpprev:=nil;
  2030. hp:=hpfirst;
  2031. while assigned(hp) do
  2032. begin
  2033. case currloc of
  2034. LOC_REFERENCE :
  2035. begin
  2036. case hp.parasym.paraloc[callerside].location^.loc of
  2037. LOC_REFERENCE :
  2038. begin
  2039. { Offset is calculated like:
  2040. sub esp,12
  2041. mov [esp+8],para3
  2042. mov [esp+4],para2
  2043. mov [esp],para1
  2044. call function
  2045. That means the for pushes the para with the
  2046. highest offset (see para3) needs to be pushed first
  2047. }
  2048. if (hpcurr.registersint>hp.registersint)
  2049. {$ifdef x86}
  2050. or (hpcurr.parasym.paraloc[callerside].location^.reference.offset>hp.parasym.paraloc[callerside].location^.reference.offset)
  2051. {$endif x86}
  2052. then
  2053. break;
  2054. end;
  2055. LOC_REGISTER,
  2056. LOC_FPUREGISTER :
  2057. break;
  2058. end;
  2059. end;
  2060. LOC_FPUREGISTER,
  2061. LOC_REGISTER :
  2062. begin
  2063. if (hp.parasym.paraloc[callerside].location^.loc=currloc) and
  2064. (hpcurr.registersint>hp.registersint) then
  2065. break;
  2066. end;
  2067. end;
  2068. hpprev:=hp;
  2069. hp:=tcallparanode(hp.right);
  2070. end;
  2071. hpcurr.right:=hp;
  2072. if assigned(hpprev) then
  2073. hpprev.right:=hpcurr
  2074. else
  2075. hpfirst:=hpcurr;
  2076. { next }
  2077. hpcurr:=hpnext;
  2078. end;
  2079. left:=hpfirst;
  2080. end;
  2081. function tcallnode.replaceparaload(var n: tnode; arg: pointer): foreachnoderesult;
  2082. var
  2083. paras: tcallparanode;
  2084. temp: tnode;
  2085. indexnr : integer;
  2086. begin
  2087. result := fen_false;
  2088. n.fileinfo := pfileposinfo(arg)^;
  2089. if (n.nodetype = loadn) then
  2090. begin
  2091. case tloadnode(n).symtableentry.typ of
  2092. paravarsym :
  2093. begin
  2094. paras := tcallparanode(left);
  2095. while assigned(paras) and
  2096. (paras.parasym <> tloadnode(n).symtableentry) do
  2097. paras := tcallparanode(paras.right);
  2098. if assigned(paras) then
  2099. begin
  2100. n.free;
  2101. n := paras.left.getcopy;
  2102. typecheckpass(n);
  2103. result := fen_true;
  2104. end;
  2105. end;
  2106. localvarsym :
  2107. begin
  2108. { local? }
  2109. if (tloadnode(n).symtableentry.owner <> tprocdef(procdefinition).localst) then
  2110. exit;
  2111. indexnr:=tloadnode(n).symtableentry.owner.SymList.IndexOf(tloadnode(n).symtableentry);
  2112. if (indexnr >= inlinelocals.count) or
  2113. not assigned(inlinelocals[indexnr]) then
  2114. internalerror(20040720);
  2115. temp := tnode(inlinelocals[indexnr]).getcopy;
  2116. n.free;
  2117. n := temp;
  2118. typecheckpass(n);
  2119. result := fen_true;
  2120. end;
  2121. end;
  2122. end;
  2123. end;
  2124. type
  2125. ptempnodes = ^ttempnodes;
  2126. ttempnodes = record
  2127. createstatement, deletestatement: tstatementnode;
  2128. end;
  2129. procedure tcallnode.createlocaltemps(p:TObject;arg:pointer);
  2130. var
  2131. tempinfo: ptempnodes absolute arg;
  2132. tempnode: ttempcreatenode;
  2133. indexnr : integer;
  2134. begin
  2135. if (TSym(p).typ <> localvarsym) then
  2136. exit;
  2137. indexnr:=TSym(p).Owner.SymList.IndexOf(p);
  2138. if (indexnr >= inlinelocals.count) then
  2139. inlinelocals.count:=indexnr+10;
  2140. if (vo_is_funcret in tabstractvarsym(p).varoptions) and
  2141. assigned(funcretnode) then
  2142. begin
  2143. if node_complexity(funcretnode) > 1 then
  2144. begin
  2145. { can this happen? }
  2146. { we may have to replace the funcretnode with the address of funcretnode }
  2147. { loaded in a temp in this case, because the expression may e.g. contain }
  2148. { a global variable that gets changed inside the function }
  2149. internalerror(2004072101);
  2150. end;
  2151. inlinelocals[indexnr] := funcretnode.getcopy
  2152. end
  2153. else
  2154. begin
  2155. tempnode := ctempcreatenode.create(tabstractvarsym(p).vardef,tabstractvarsym(p).vardef.size,tt_persistent,tabstractvarsym(p).is_regvar(false));
  2156. addstatement(tempinfo^.createstatement,tempnode);
  2157. if (vo_is_funcret in tlocalvarsym(p).varoptions) then
  2158. begin
  2159. funcretnode := ctemprefnode.create(tempnode);
  2160. addstatement(tempinfo^.deletestatement,ctempdeletenode.create_normal_temp(tempnode));
  2161. end
  2162. else
  2163. addstatement(tempinfo^.deletestatement,ctempdeletenode.create(tempnode));
  2164. inlinelocals[indexnr] := ctemprefnode.create(tempnode);
  2165. end;
  2166. end;
  2167. function nonlocalvars(var n: tnode; arg: pointer): foreachnoderesult;
  2168. begin
  2169. result := fen_false;
  2170. { this is just to play it safe, there are more safe situations }
  2171. if (n.nodetype = derefn) or
  2172. ((n.nodetype = loadn) and
  2173. { globals and fields of (possibly global) objects could always be changed in the callee }
  2174. ((tloadnode(n).symtable.symtabletype in [globalsymtable,ObjectSymtable]) or
  2175. { statics can only be modified by functions in the same unit }
  2176. ((tloadnode(n).symtable.symtabletype = staticsymtable) and
  2177. (tloadnode(n).symtable = TSymtable(arg))))) or
  2178. ((n.nodetype = subscriptn) and
  2179. (tsubscriptnode(n).vs.owner.symtabletype = ObjectSymtable)) then
  2180. result := fen_norecurse_true;
  2181. end;
  2182. procedure tcallnode.createinlineparas(var createstatement, deletestatement: tstatementnode);
  2183. var
  2184. para: tcallparanode;
  2185. tempnode: ttempcreatenode;
  2186. tempnodes: ttempnodes;
  2187. n: tnode;
  2188. paracomplexity: longint;
  2189. begin
  2190. { parameters }
  2191. para := tcallparanode(left);
  2192. while assigned(para) do
  2193. begin
  2194. if (para.parasym.typ = paravarsym) and
  2195. { para.left will already be the same as funcretnode in the following case, so don't change }
  2196. (not(vo_is_funcret in tparavarsym(para.parasym).varoptions) or
  2197. (not assigned(funcretnode))) then
  2198. begin
  2199. { must take copy of para.left, because if it contains a }
  2200. { temprefn pointing to a copied temp (e.g. methodpointer), }
  2201. { then this parameter must be changed to point to the copy of }
  2202. { that temp (JM) }
  2203. n := para.left.getcopy;
  2204. para.left.free;
  2205. para.left := n;
  2206. firstpass(para.left);
  2207. { create temps for value parameters, function result and also for }
  2208. { const parameters which are passed by value instead of by reference }
  2209. { we need to take care that we use the type of the defined parameter and not of the
  2210. passed parameter, because these can be different in case of a formaldef (PFV) }
  2211. paracomplexity := node_complexity(para.left);
  2212. { check if we have to create a temp, assign the parameter's }
  2213. { contents to that temp and then substitute the paramter }
  2214. { with the temp everywhere in the function }
  2215. if
  2216. ((tparavarsym(para.parasym).varregable in [vr_none,vr_addr]) and
  2217. not(para.left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE])) or
  2218. { we can't assign to formaldef temps }
  2219. ((para.parasym.vardef.typ<>formaldef) and
  2220. (
  2221. { if paracomplexity > 1, we normally take the address of }
  2222. { the parameter expression, store it in a temp and }
  2223. { substitute the dereferenced temp in the inlined function }
  2224. { We can't do this if we can't take the address of the }
  2225. { parameter expression, so in that case assign to a temp }
  2226. not(para.left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CONSTANT]) or
  2227. ((paracomplexity > 1) and
  2228. (not valid_for_addr(para.left,false) or
  2229. (para.left.nodetype = calln) or
  2230. is_constnode(para.left))) or
  2231. { the problem is that we can't take the address of a function result :( }
  2232. (vo_is_funcret in tparavarsym(para.parasym).varoptions) or
  2233. { we do not need to create a temp for value parameters }
  2234. { which are not modified in the inlined function }
  2235. { const parameters can get vs_readwritten if their }
  2236. { address is taken }
  2237. ((((para.parasym.varspez = vs_value) and
  2238. (para.parasym.varstate in [vs_initialised,vs_declared,vs_read])) or
  2239. { in case of const, this is only necessary if the }
  2240. { variable would be passed by value normally, or if }
  2241. { there is such a variable somewhere in an expression }
  2242. ((para.parasym.varspez = vs_const) and
  2243. (not paramanager.push_addr_param(vs_const,para.parasym.vardef,procdefinition.proccalloption) or
  2244. (paracomplexity > 1)))) and
  2245. { however, if we pass a global variable, an object field or}
  2246. { an expression containing a pointer dereference as }
  2247. { parameter, this value could be modified in other ways as }
  2248. { well and in such cases create a temp to be on the safe }
  2249. { side }
  2250. foreachnodestatic(para.left,@nonlocalvars,pointer(symtableproc))) or
  2251. { value parameters of which we know they are modified by }
  2252. { definition have to be copied to a temp }
  2253. ((para.parasym.varspez = vs_value) and
  2254. not(para.parasym.varstate in [vs_initialised,vs_declared,vs_read])) or
  2255. { the compiler expects that it can take the address of parameters passed by reference in
  2256. the case of const so we can't replace the node simply by a constant node
  2257. When playing with this code, ensure that
  2258. function f(const a,b : longint) : longint;inline;
  2259. begin
  2260. result:=a*b;
  2261. end;
  2262. [...]
  2263. ...:=f(10,20));
  2264. [...]
  2265. is still folded. (FK)
  2266. }
  2267. ((para.parasym.varspez = vs_const) and
  2268. { const para's can get vs_readwritten if their address }
  2269. { is taken }
  2270. ((para.parasym.varstate = vs_readwritten) or
  2271. { call-by-reference const's may need to be passed by }
  2272. { reference to function called in the inlined code }
  2273. (paramanager.push_addr_param(vs_const,para.parasym.vardef,procdefinition.proccalloption) and
  2274. (not valid_for_addr(para.left,false) or
  2275. is_constnode(para.left)))))
  2276. )
  2277. ) then
  2278. begin
  2279. tempnode:=nil;
  2280. {$ifdef reuse_existing_para_temp}
  2281. { Try to reuse existing result tempnode from a parameter }
  2282. if para.left.nodetype=blockn then
  2283. begin
  2284. n:=tstatementnode(tblocknode(para.left).left);
  2285. while assigned(n) and assigned(tstatementnode(n).right) do
  2286. begin
  2287. if tstatementnode(n).left.nodetype=tempdeleten then
  2288. break;
  2289. n:=tstatementnode(tstatementnode(n).right);
  2290. end;
  2291. { We expect to find the following statements
  2292. tempdeletenode
  2293. tempref
  2294. nil }
  2295. if assigned(n) and
  2296. assigned(tstatementnode(n).right) and
  2297. (tstatementnode(tstatementnode(n).right).right=nil) and
  2298. (tstatementnode(tstatementnode(n).right).left.nodetype=temprefn) then
  2299. begin
  2300. tempnode:=ttempdeletenode(tstatementnode(n).left).tempinfo^.owner;
  2301. para.left:=tstatementnode(tstatementnode(n).right).left;
  2302. addstatement(deletestatement,tstatementnode(n).left);
  2303. { Replace tempdelete,tempref with dummy statement }
  2304. tstatementnode(n).left:=cnothingnode.create;
  2305. tstatementnode(tstatementnode(n).right).left:=cnothingnode.create;
  2306. end;
  2307. end;
  2308. {$endif reuse_existing_para_temp}
  2309. tempnode := ctempcreatenode.create(para.parasym.vardef,para.parasym.vardef.size,tt_persistent,tparavarsym(para.parasym).is_regvar(false));
  2310. addstatement(createstatement,tempnode);
  2311. { assign the value of the parameter to the temp, except in case of the function result }
  2312. { (in that case, para.left is a block containing the creation of a new temp, while we }
  2313. { only need a temprefnode, so delete the old stuff) }
  2314. if not(vo_is_funcret in tparavarsym(para.parasym).varoptions) then
  2315. begin
  2316. addstatement(createstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  2317. para.left));
  2318. para.left := ctemprefnode.create(tempnode);
  2319. addstatement(deletestatement,ctempdeletenode.create(tempnode));
  2320. end
  2321. else
  2322. begin
  2323. if not(assigned(funcretnode)) then
  2324. funcretnode := ctemprefnode.create(tempnode);
  2325. para.left.free;
  2326. para.left := ctemprefnode.create(tempnode);
  2327. addstatement(deletestatement,ctempdeletenode.create_normal_temp(tempnode));
  2328. end;
  2329. end
  2330. { otherwise if the parameter is "complex", take the address }
  2331. { of the parameter expression, store it in a temp and replace }
  2332. { occurrences of the parameter with dereferencings of this }
  2333. { temp }
  2334. else if (paracomplexity > 1) then
  2335. begin
  2336. tempnode := ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,tparavarsym(para.parasym).is_regvar(true));
  2337. addstatement(createstatement,tempnode);
  2338. addstatement(createstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  2339. caddrnode.create_internal(para.left)));
  2340. para.left := ctypeconvnode.create_internal(cderefnode.create(ctemprefnode.create(tempnode)),para.left.resultdef);
  2341. addstatement(deletestatement,ctempdeletenode.create(tempnode));
  2342. end;
  2343. end;
  2344. para := tcallparanode(para.right);
  2345. end;
  2346. { local variables }
  2347. if not assigned(tprocdef(procdefinition).localst) or
  2348. (tprocdef(procdefinition).localst.SymList.count = 0) then
  2349. exit;
  2350. tempnodes.createstatement := createstatement;
  2351. tempnodes.deletestatement := deletestatement;
  2352. inlinelocals.count:=tprocdef(procdefinition).localst.SymList.count;
  2353. tprocdef(procdefinition).localst.SymList.ForEachCall(@createlocaltemps,@tempnodes);
  2354. createstatement := tempnodes.createstatement;
  2355. deletestatement := tempnodes.deletestatement;
  2356. end;
  2357. function tcallnode.pass1_inline:tnode;
  2358. var
  2359. createstatement,deletestatement: tstatementnode;
  2360. createblock,deleteblock: tblocknode;
  2361. body : tnode;
  2362. begin
  2363. if not(assigned(tprocdef(procdefinition).inlininginfo) and
  2364. assigned(tprocdef(procdefinition).inlininginfo^.code)) then
  2365. internalerror(200412021);
  2366. { inherit flags }
  2367. current_procinfo.flags := current_procinfo.flags + ((procdefinition as tprocdef).inlininginfo^.flags*inherited_inlining_flags);
  2368. { create blocks for loading/deleting of local data }
  2369. createblock:=internalstatements(createstatement);
  2370. deleteblock:=internalstatements(deletestatement);
  2371. { add methodpointer init code to init statement }
  2372. { (fini must be done later, as it will delete the hookoncopy info) }
  2373. if assigned(methodpointerinit) then
  2374. addstatement(createstatement,methodpointerinit.getcopy);
  2375. inlinelocals:=TFPObjectList.create(true);
  2376. { get copy of the procedure body }
  2377. body:=tprocdef(procdefinition).inlininginfo^.code.getcopy;
  2378. { replace complex parameters with temps }
  2379. createinlineparas(createstatement,deletestatement);
  2380. { replace the parameter loads with the parameter values }
  2381. foreachnode(body,@replaceparaload,@fileinfo);
  2382. { copy methodpointer fini code }
  2383. if assigned(methodpointerdone) then
  2384. addstatement(deletestatement,methodpointerdone.getcopy);
  2385. { free the temps for the locals }
  2386. inlinelocals.free;
  2387. inlinelocals:=nil;
  2388. addstatement(createstatement,body);
  2389. addstatement(createstatement,deleteblock);
  2390. { set function result location if necessary }
  2391. if assigned(funcretnode) and
  2392. (cnf_return_value_used in callnodeflags) then
  2393. addstatement(createstatement,funcretnode.getcopy);
  2394. { consider it must not be inlined if called
  2395. again inside the args or itself }
  2396. exclude(procdefinition.procoptions,po_inline);
  2397. dosimplify(createblock);
  2398. firstpass(createblock);
  2399. include(procdefinition.procoptions,po_inline);
  2400. { return inlined block }
  2401. result := createblock;
  2402. {$ifdef DEBUGINLINE}
  2403. writeln;
  2404. writeln('**************************',tprocdef(procdefinition).mangledname);
  2405. printnode(output,result);
  2406. {$endif DEBUGINLINE}
  2407. end;
  2408. procedure tcallnode.check_stack_parameters;
  2409. var
  2410. hp : tcallparanode;
  2411. begin
  2412. hp:=tcallparanode(left);
  2413. while assigned(hp) do
  2414. begin
  2415. if assigned(hp.parasym) and
  2416. assigned(hp.parasym.paraloc[callerside].location) and
  2417. (hp.parasym.paraloc[callerside].location^.loc=LOC_REFERENCE) then
  2418. include(current_procinfo.flags,pi_has_stackparameter);
  2419. hp:=tcallparanode(hp.right);
  2420. end;
  2421. end;
  2422. function tcallnode.pass_1 : tnode;
  2423. var
  2424. st : TSymtable;
  2425. begin
  2426. result:=nil;
  2427. { Can we inline the procedure? }
  2428. if ([po_inline,po_has_inlininginfo] <= procdefinition.procoptions) then
  2429. begin
  2430. { Check if we can inline the procedure when it references proc/var that
  2431. are not in the globally available }
  2432. st:=procdefinition.owner;
  2433. if (st.symtabletype=ObjectSymtable) then
  2434. st:=st.defowner.owner;
  2435. if (pi_uses_static_symtable in tprocdef(procdefinition).inlininginfo^.flags) and
  2436. (st.symtabletype=globalsymtable) and
  2437. (not st.iscurrentunit) then
  2438. begin
  2439. Comment(V_lineinfo+V_Debug,'Not inlining "'+tprocdef(procdefinition).procsym.realname+'", references static symtable');
  2440. end
  2441. else
  2442. begin
  2443. result:=pass1_inline;
  2444. exit;
  2445. end;
  2446. end;
  2447. { calculate the parameter info for the procdef }
  2448. if not procdefinition.has_paraloc_info then
  2449. begin
  2450. procdefinition.requiredargarea:=paramanager.create_paraloc_info(procdefinition,callerside);
  2451. procdefinition.has_paraloc_info:=true;
  2452. end;
  2453. { calculate the parameter size needed for this call include varargs if they are available }
  2454. if assigned(varargsparas) then
  2455. pushedparasize:=paramanager.create_varargs_paraloc_info(procdefinition,varargsparas)
  2456. else
  2457. pushedparasize:=procdefinition.requiredargarea;
  2458. { record maximum parameter size used in this proc }
  2459. current_procinfo.allocate_push_parasize(pushedparasize);
  2460. { work trough all parameters to get the register requirements }
  2461. if assigned(left) then
  2462. begin
  2463. tcallparanode(left).det_registers;
  2464. if cs_opt_level1 in current_settings.optimizerswitches then
  2465. begin
  2466. { check for stacked parameters }
  2467. check_stack_parameters;
  2468. end;
  2469. end;
  2470. { order parameters }
  2471. order_parameters;
  2472. if assigned(methodpointerinit) then
  2473. firstpass(methodpointerinit);
  2474. if assigned(methodpointerdone) then
  2475. firstpass(methodpointerdone);
  2476. { function result node }
  2477. if assigned(_funcretnode) then
  2478. firstpass(_funcretnode);
  2479. { procedure variable ? }
  2480. if assigned(right) then
  2481. firstpass(right);
  2482. if not (block_type in [bt_const,bt_type]) then
  2483. include(current_procinfo.flags,pi_do_call);
  2484. { implicit finally needed ? }
  2485. if resultdef.needs_inittable and
  2486. not paramanager.ret_in_param(resultdef,procdefinition.proccalloption) and
  2487. not assigned(funcretnode) then
  2488. include(current_procinfo.flags,pi_needs_implicit_finally);
  2489. { get a register for the return value }
  2490. if (not is_void(resultdef)) then
  2491. begin
  2492. if paramanager.ret_in_param(resultdef,procdefinition.proccalloption) then
  2493. begin
  2494. expectloc:=LOC_REFERENCE;
  2495. end
  2496. else
  2497. { ansi/widestrings must be registered, so we can dispose them }
  2498. if is_ansistring(resultdef) or
  2499. is_widestring(resultdef) then
  2500. begin
  2501. expectloc:=LOC_REFERENCE;
  2502. registersint:=1;
  2503. end
  2504. else
  2505. { we have only to handle the result if it is used }
  2506. if (cnf_return_value_used in callnodeflags) then
  2507. begin
  2508. case resultdef.typ of
  2509. enumdef,
  2510. orddef :
  2511. begin
  2512. if (procdefinition.proctypeoption=potype_constructor) then
  2513. begin
  2514. expectloc:=LOC_REGISTER;
  2515. registersint:=1;
  2516. end
  2517. else
  2518. begin
  2519. expectloc:=LOC_REGISTER;
  2520. if is_64bit(resultdef) then
  2521. registersint:=2
  2522. else
  2523. registersint:=1;
  2524. end;
  2525. end;
  2526. floatdef :
  2527. begin
  2528. expectloc:=LOC_FPUREGISTER;
  2529. {$ifdef cpufpemu}
  2530. if (cs_fp_emulation in current_settings.moduleswitches) then
  2531. registersint:=1
  2532. else
  2533. {$endif cpufpemu}
  2534. {$ifdef m68k}
  2535. if (tfloatdef(resultdef).floattype=s32real) then
  2536. registersint:=1
  2537. else
  2538. {$endif m68k}
  2539. registersfpu:=1;
  2540. end;
  2541. else
  2542. begin
  2543. expectloc:=procdefinition.funcretloc[callerside].loc;
  2544. if (expectloc = LOC_REGISTER) then
  2545. {$ifndef cpu64bit}
  2546. if (resultdef.size > sizeof(aint)) then
  2547. registersint:=2
  2548. else
  2549. {$endif cpu64bit}
  2550. registersint:=1
  2551. else
  2552. registersint:=0;
  2553. end;
  2554. end;
  2555. end
  2556. else
  2557. expectloc:=LOC_VOID;
  2558. end
  2559. else
  2560. expectloc:=LOC_VOID;
  2561. {$ifdef m68k}
  2562. { we need one more address register for virtual calls on m68k }
  2563. if (po_virtualmethod in procdefinition.procoptions) then
  2564. inc(registersint);
  2565. {$endif m68k}
  2566. { a fpu can be used in any procedure !! }
  2567. {$ifdef i386}
  2568. registersfpu:=procdefinition.fpu_used;
  2569. {$endif i386}
  2570. { if this is a call to a method calc the registers }
  2571. if (methodpointer<>nil) then
  2572. begin
  2573. if methodpointer.nodetype<>typen then
  2574. begin
  2575. firstpass(methodpointer);
  2576. registersfpu:=max(methodpointer.registersfpu,registersfpu);
  2577. registersint:=max(methodpointer.registersint,registersint);
  2578. {$ifdef SUPPORT_MMX }
  2579. registersmmx:=max(methodpointer.registersmmx,registersmmx);
  2580. {$endif SUPPORT_MMX}
  2581. end;
  2582. end;
  2583. { determine the registers of the procedure variable }
  2584. { is this OK for inlined procs also ?? (PM) }
  2585. if assigned(right) then
  2586. begin
  2587. registersfpu:=max(right.registersfpu,registersfpu);
  2588. registersint:=max(right.registersint,registersint);
  2589. {$ifdef SUPPORT_MMX}
  2590. registersmmx:=max(right.registersmmx,registersmmx);
  2591. {$endif SUPPORT_MMX}
  2592. end;
  2593. { determine the registers of the procedure }
  2594. if assigned(left) then
  2595. begin
  2596. registersfpu:=max(left.registersfpu,registersfpu);
  2597. registersint:=max(left.registersint,registersint);
  2598. {$ifdef SUPPORT_MMX}
  2599. registersmmx:=max(left.registersmmx,registersmmx);
  2600. {$endif SUPPORT_MMX}
  2601. end;
  2602. end;
  2603. {$ifdef state_tracking}
  2604. function Tcallnode.track_state_pass(exec_known:boolean):boolean;
  2605. var hp:Tcallparanode;
  2606. value:Tnode;
  2607. begin
  2608. track_state_pass:=false;
  2609. hp:=Tcallparanode(left);
  2610. while assigned(hp) do
  2611. begin
  2612. if left.track_state_pass(exec_known) then
  2613. begin
  2614. left.resultdef:=nil;
  2615. do_typecheckpass(left);
  2616. end;
  2617. value:=aktstate.find_fact(hp.left);
  2618. if value<>nil then
  2619. begin
  2620. track_state_pass:=true;
  2621. hp.left.destroy;
  2622. hp.left:=value.getcopy;
  2623. do_typecheckpass(hp.left);
  2624. end;
  2625. hp:=Tcallparanode(hp.right);
  2626. end;
  2627. end;
  2628. {$endif}
  2629. function tcallnode.para_count:longint;
  2630. var
  2631. ppn : tcallparanode;
  2632. begin
  2633. result:=0;
  2634. ppn:=tcallparanode(left);
  2635. while assigned(ppn) do
  2636. begin
  2637. if not(assigned(ppn.parasym) and
  2638. (vo_is_hidden_para in ppn.parasym.varoptions)) then
  2639. inc(result);
  2640. ppn:=tcallparanode(ppn.right);
  2641. end;
  2642. end;
  2643. function tcallnode.get_load_methodpointer:tnode;
  2644. var
  2645. newstatement : tstatementnode;
  2646. begin
  2647. if assigned(methodpointerinit) then
  2648. begin
  2649. result:=internalstatements(newstatement);
  2650. addstatement(newstatement,methodpointerinit);
  2651. addstatement(newstatement,methodpointer);
  2652. addstatement(newstatement,methodpointerdone);
  2653. methodpointerinit:=nil;
  2654. methodpointer:=nil;
  2655. methodpointerdone:=nil;
  2656. end
  2657. else
  2658. begin
  2659. result:=methodpointer;
  2660. methodpointer:=nil;
  2661. end;
  2662. end;
  2663. function tcallnode.docompare(p: tnode): boolean;
  2664. begin
  2665. docompare :=
  2666. inherited docompare(p) and
  2667. (symtableprocentry = tcallnode(p).symtableprocentry) and
  2668. (procdefinition = tcallnode(p).procdefinition) and
  2669. (methodpointer.isequal(tcallnode(p).methodpointer)) and
  2670. (((cnf_typedefset in callnodeflags) and (cnf_typedefset in tcallnode(p).callnodeflags) and
  2671. (equal_defs(typedef,tcallnode(p).typedef))) or
  2672. (not(cnf_typedefset in callnodeflags) and not(cnf_typedefset in tcallnode(p).callnodeflags)));
  2673. end;
  2674. procedure tcallnode.printnodedata(var t:text);
  2675. begin
  2676. if assigned(procdefinition) and
  2677. (procdefinition.typ=procdef) then
  2678. writeln(t,printnodeindention,'proc = ',tprocdef(procdefinition).fullprocname(true))
  2679. else
  2680. begin
  2681. if assigned(symtableprocentry) then
  2682. writeln(t,printnodeindention,'proc = ',symtableprocentry.name)
  2683. else
  2684. writeln(t,printnodeindention,'proc = <nil>');
  2685. end;
  2686. if assigned(methodpointer) then
  2687. begin
  2688. writeln(t,printnodeindention,'methodpointer =');
  2689. printnode(t,methodpointer);
  2690. end;
  2691. if assigned(methodpointerinit) then
  2692. begin
  2693. writeln(t,printnodeindention,'methodpointerinit =');
  2694. printnode(t,methodpointerinit);
  2695. end;
  2696. if assigned(methodpointerdone) then
  2697. begin
  2698. writeln(t,printnodeindention,'methodpointerdone =');
  2699. printnode(t,methodpointerdone);
  2700. end;
  2701. if assigned(right) then
  2702. begin
  2703. writeln(t,printnodeindention,'right =');
  2704. printnode(t,right);
  2705. end;
  2706. if assigned(left) then
  2707. begin
  2708. writeln(t,printnodeindention,'left =');
  2709. printnode(t,left);
  2710. end;
  2711. end;
  2712. begin
  2713. ccallnode:=tcallnode;
  2714. ccallparanode:=tcallparanode;
  2715. end.