ncal.pas 118 KB

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