ncal.pas 123 KB

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