ncal.pas 113 KB

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