ncal.pas 118 KB

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