ncal.pas 122 KB

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