ncal.pas 117 KB

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