ncal.pas 111 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This file implements the node for sub procedure calling.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit ncal;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cutils,cclasses,
  23. globtype,cpuinfo,
  24. node,nbas,
  25. {$ifdef state_tracking}
  26. nstate,
  27. {$endif state_tracking}
  28. symbase,symtype,symppu,symsym,symdef,symtable;
  29. type
  30. pcandidate = ^tcandidate;
  31. tcandidate = record
  32. next : pcandidate;
  33. data : tprocdef;
  34. wrongpara,
  35. firstpara : tparaitem;
  36. exact_count,
  37. equal_count,
  38. cl1_count,
  39. cl2_count,
  40. cl3_count,
  41. coper_count : integer; { should be signed }
  42. ordinal_distance : bestreal;
  43. invalid : boolean;
  44. wrongparanr : byte;
  45. end;
  46. tcallnode = class(tbinarynode)
  47. private
  48. paralength : smallint;
  49. function candidates_find:pcandidate;
  50. procedure candidates_free(procs:pcandidate);
  51. procedure candidates_list(procs:pcandidate;all:boolean);
  52. procedure candidates_get_information(procs:pcandidate);
  53. function candidates_choose_best(procs:pcandidate;var bestpd:tprocdef):integer;
  54. procedure candidates_find_wrong_para(procs:pcandidate);
  55. {$ifdef EXTDEBUG}
  56. procedure candidates_dump_info(lvl:longint;procs:pcandidate);
  57. {$endif EXTDEBUG}
  58. function gen_self_tree:tnode;
  59. function gen_vmt_tree:tnode;
  60. procedure bind_paraitem;
  61. public
  62. { the symbol containing the definition of the procedure }
  63. { to call }
  64. symtableprocentry : tprocsym;
  65. { symtable where the entry was found, needed for with support }
  66. symtableproc : tsymtable;
  67. { the definition of the procedure to call }
  68. procdefinition : tabstractprocdef;
  69. { tree that contains the pointer to the object for this method }
  70. methodpointer : tnode;
  71. { function return node, this is used to pass the data for a
  72. ret_in_param return value }
  73. funcretnode : tnode;
  74. { separately specified resulttype for some compilerprocs (e.g. }
  75. { you can't have a function with an "array of char" resulttype }
  76. { the RTL) (JM) }
  77. restype: ttype;
  78. restypeset: boolean;
  79. { only the processor specific nodes need to override this }
  80. { constructor }
  81. constructor create(l:tnode; v : tprocsym;st : tsymtable; mp : tnode);virtual;
  82. constructor create_def(l:tnode;def:tprocdef;mp:tnode);virtual;
  83. constructor create_procvar(l,r:tnode);
  84. constructor createintern(const name: string; params: tnode);
  85. constructor createinternres(const name: string; params: tnode; const res: ttype);
  86. constructor createinternreturn(const name: string; params: tnode; returnnode : tnode);
  87. destructor destroy;override;
  88. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  89. procedure ppuwrite(ppufile:tcompilerppufile);override;
  90. procedure derefimpl;override;
  91. function getcopy : tnode;override;
  92. { Goes through all symbols in a class and subclasses and calls
  93. verify abstract for each .
  94. }
  95. procedure verifyabstractcalls;
  96. { called for each definition in a class and verifies if a method
  97. is abstract or not, if it is abstract, give out a warning
  98. }
  99. procedure verifyabstract(p : tnamedindexitem;arg:pointer);
  100. procedure insertintolist(l : tnodelist);override;
  101. function pass_1 : tnode;override;
  102. function det_resulttype:tnode;override;
  103. {$ifdef state_tracking}
  104. function track_state_pass(exec_known:boolean):boolean;override;
  105. {$endif state_tracking}
  106. function docompare(p: tnode): boolean; override;
  107. procedure printnodedata(var t:text);override;
  108. private
  109. {$ifdef callparatemp}
  110. function extract_functioncall_paras: tblocknode;
  111. {$endif callparatemp}
  112. AbstractMethodsList : TStringList;
  113. end;
  114. tcallnodeclass = class of tcallnode;
  115. tcallparaflags = (
  116. { flags used by tcallparanode }
  117. cpf_exact_match_found,
  118. cpf_convlevel1found,
  119. cpf_convlevel2found,
  120. cpf_is_colon_para
  121. );
  122. tcallparanode = class(tbinarynode)
  123. callparaflags : set of tcallparaflags;
  124. paraitem : tparaitem;
  125. used_by_callnode : boolean;
  126. { only the processor specific nodes need to override this }
  127. { constructor }
  128. constructor create(expr,next : tnode);virtual;
  129. destructor destroy;override;
  130. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  131. procedure ppuwrite(ppufile:tcompilerppufile);override;
  132. procedure derefimpl;override;
  133. function getcopy : tnode;override;
  134. procedure insertintolist(l : tnodelist);override;
  135. procedure get_paratype;
  136. procedure insert_typeconv(do_count : boolean);
  137. procedure det_registers;
  138. procedure firstcallparan(do_count : boolean);
  139. procedure secondcallparan(push_from_left_to_right:boolean;calloption:tproccalloption;
  140. para_alignment,para_offset : longint);virtual;abstract;
  141. function docompare(p: tnode): boolean; override;
  142. procedure printnodetree(var t:text);override;
  143. end;
  144. tcallparanodeclass = class of tcallparanode;
  145. tprocinlinenode = class(tnode)
  146. inlinetree : tnode;
  147. inlineprocdef : tprocdef;
  148. retoffset,para_offset,para_size : longint;
  149. constructor create(p:tprocdef);virtual;
  150. destructor destroy;override;
  151. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  152. procedure ppuwrite(ppufile:tcompilerppufile);override;
  153. procedure derefimpl;override;
  154. function getcopy : tnode;override;
  155. function det_resulttype : tnode;override;
  156. procedure insertintolist(l : tnodelist);override;
  157. function pass_1 : tnode;override;
  158. function docompare(p: tnode): boolean; override;
  159. end;
  160. tprocinlinenodeclass = class of tprocinlinenode;
  161. function reverseparameters(p: tcallparanode): tcallparanode;
  162. var
  163. ccallnode : tcallnodeclass;
  164. ccallparanode : tcallparanodeclass;
  165. cprocinlinenode : tprocinlinenodeclass;
  166. implementation
  167. uses
  168. systems,
  169. verbose,globals,
  170. symconst,paramgr,defutil,defcmp,
  171. htypechk,pass_1,cpubase,
  172. ncnv,nld,ninl,nadd,ncon,nmem,
  173. nutils,
  174. tgobj,rgobj,cginfo,cgbase
  175. ;
  176. type
  177. tobjectinfoitem = class(tlinkedlistitem)
  178. objinfo : tobjectdef;
  179. constructor create(def : tobjectdef);
  180. end;
  181. {****************************************************************************
  182. HELPERS
  183. ****************************************************************************}
  184. function reverseparameters(p: tcallparanode): tcallparanode;
  185. var
  186. hp1, hp2: tcallparanode;
  187. begin
  188. hp1:=nil;
  189. while assigned(p) do
  190. begin
  191. { pull out }
  192. hp2:=p;
  193. p:=tcallparanode(p.right);
  194. { pull in }
  195. hp2.right:=hp1;
  196. hp1:=hp2;
  197. end;
  198. reverseparameters:=hp1;
  199. end;
  200. function gen_high_tree(p:tnode;openstring:boolean):tnode;
  201. var
  202. temp: tnode;
  203. len : integer;
  204. loadconst : boolean;
  205. hightree : tnode;
  206. begin
  207. len:=-1;
  208. loadconst:=true;
  209. hightree:=nil;
  210. case p.resulttype.def.deftype of
  211. arraydef :
  212. begin
  213. { handle via a normal inline in_high_x node }
  214. loadconst := false;
  215. hightree := geninlinenode(in_high_x,false,p.getcopy);
  216. { only substract low(array) if it's <> 0 }
  217. temp := geninlinenode(in_low_x,false,p.getcopy);
  218. resulttypepass(temp);
  219. if (temp.nodetype <> ordconstn) or
  220. (tordconstnode(temp).value <> 0) then
  221. hightree := caddnode.create(subn,hightree,temp)
  222. else
  223. temp.free;
  224. end;
  225. stringdef :
  226. begin
  227. if openstring then
  228. begin
  229. { handle via a normal inline in_high_x node }
  230. loadconst := false;
  231. hightree := geninlinenode(in_high_x,false,p.getcopy);
  232. end
  233. else
  234. begin
  235. { passing a string to an array of char }
  236. if (p.nodetype=stringconstn) then
  237. begin
  238. len:=str_length(p);
  239. if len>0 then
  240. dec(len);
  241. end
  242. else
  243. begin
  244. hightree:=caddnode.create(subn,geninlinenode(in_length_x,false,p.getcopy),
  245. cordconstnode.create(1,s32bittype,false));
  246. loadconst:=false;
  247. end;
  248. end;
  249. end;
  250. else
  251. len:=0;
  252. end;
  253. if loadconst then
  254. hightree:=cordconstnode.create(len,s32bittype,true)
  255. else
  256. begin
  257. if not assigned(hightree) then
  258. internalerror(200304071);
  259. hightree:=ctypeconvnode.create(hightree,s32bittype);
  260. end;
  261. result:=hightree;
  262. end;
  263. procedure search_class_overloads(aprocsym : tprocsym);
  264. { searches n in symtable of pd and all anchestors }
  265. var
  266. speedvalue : cardinal;
  267. srsym : tprocsym;
  268. s : string;
  269. objdef : tobjectdef;
  270. begin
  271. if aprocsym.overloadchecked then
  272. exit;
  273. aprocsym.overloadchecked:=true;
  274. if (aprocsym.owner.symtabletype<>objectsymtable) then
  275. internalerror(200111021);
  276. objdef:=tobjectdef(aprocsym.owner.defowner);
  277. { we start in the parent }
  278. if not assigned(objdef.childof) then
  279. exit;
  280. objdef:=objdef.childof;
  281. s:=aprocsym.name;
  282. speedvalue:=getspeedvalue(s);
  283. while assigned(objdef) do
  284. begin
  285. srsym:=tprocsym(objdef.symtable.speedsearch(s,speedvalue));
  286. if assigned(srsym) then
  287. begin
  288. if (srsym.typ<>procsym) then
  289. internalerror(200111022);
  290. if srsym.is_visible_for_proc(current_procdef) then
  291. begin
  292. srsym.add_para_match_to(Aprocsym);
  293. { we can stop if the overloads were already added
  294. for the found symbol }
  295. if srsym.overloadchecked then
  296. break;
  297. end;
  298. end;
  299. { next parent }
  300. objdef:=objdef.childof;
  301. end;
  302. end;
  303. function is_better_candidate(currpd,bestpd:pcandidate):integer;
  304. var
  305. res : integer;
  306. begin
  307. {
  308. Return values:
  309. > 0 when currpd is better than bestpd
  310. < 0 when bestpd is better than currpd
  311. = 0 when both are equal
  312. To choose the best candidate we use the following order:
  313. - Incompatible flag
  314. - (Smaller) Number of convert operator parameters.
  315. - (Smaller) Number of convertlevel 2 parameters.
  316. - (Smaller) Number of convertlevel 1 parameters.
  317. - (Bigger) Number of exact parameters.
  318. - (Smaller) Number of equal parameters.
  319. - (Smaller) Total of ordinal distance. For example, the distance of a word
  320. to a byte is 65535-255=65280.
  321. }
  322. if bestpd^.invalid then
  323. begin
  324. if currpd^.invalid then
  325. res:=0
  326. else
  327. res:=1;
  328. end
  329. else
  330. if currpd^.invalid then
  331. res:=-1
  332. else
  333. begin
  334. { less operator parameters? }
  335. res:=(bestpd^.coper_count-currpd^.coper_count);
  336. if (res=0) then
  337. begin
  338. { less cl3 parameters? }
  339. res:=(bestpd^.cl3_count-currpd^.cl3_count);
  340. if (res=0) then
  341. begin
  342. { less cl2 parameters? }
  343. res:=(bestpd^.cl2_count-currpd^.cl2_count);
  344. if (res=0) then
  345. begin
  346. { less cl1 parameters? }
  347. res:=(bestpd^.cl1_count-currpd^.cl1_count);
  348. if (res=0) then
  349. begin
  350. { more exact parameters? }
  351. res:=(currpd^.exact_count-bestpd^.exact_count);
  352. if (res=0) then
  353. begin
  354. { less equal parameters? }
  355. res:=(bestpd^.equal_count-currpd^.equal_count);
  356. if (res=0) then
  357. begin
  358. { smaller ordinal distance? }
  359. if (currpd^.ordinal_distance<bestpd^.ordinal_distance) then
  360. res:=1
  361. else
  362. if (currpd^.ordinal_distance>bestpd^.ordinal_distance) then
  363. res:=-1
  364. else
  365. res:=0;
  366. end;
  367. end;
  368. end;
  369. end;
  370. end;
  371. end;
  372. end;
  373. is_better_candidate:=res;
  374. end;
  375. procedure var_para_allowed(var eq:tequaltype;def_from,def_to:Tdef);
  376. begin
  377. { Note: eq must be already valid, it will only be updated! }
  378. case def_to.deftype of
  379. formaldef :
  380. begin
  381. { all types can be passed to a formaldef }
  382. eq:=te_equal;
  383. end;
  384. orddef :
  385. begin
  386. { allows conversion from word to integer and
  387. byte to shortint, but only for TP7 compatibility }
  388. if (m_tp7 in aktmodeswitches) and
  389. (def_from.deftype=orddef) and
  390. (def_from.size=def_to.size) then
  391. eq:=te_convert_l1;
  392. end;
  393. pointerdef :
  394. begin
  395. { an implicit pointer conversion is allowed }
  396. if (def_from.deftype=pointerdef) then
  397. eq:=te_convert_l1;
  398. end;
  399. stringdef :
  400. begin
  401. { all shortstrings are allowed, size is not important }
  402. if is_shortstring(def_from) and
  403. is_shortstring(def_to) then
  404. eq:=te_equal;
  405. end;
  406. objectdef :
  407. begin
  408. { child objects can be also passed }
  409. { in non-delphi mode, otherwise }
  410. { they must match exactly, except }
  411. { if they are objects }
  412. if (def_from.deftype=objectdef) and
  413. (
  414. not(m_delphi in aktmodeswitches) or
  415. (
  416. (tobjectdef(def_from).objecttype=odt_object) and
  417. (tobjectdef(def_to).objecttype=odt_object)
  418. )
  419. ) and
  420. (tobjectdef(def_from).is_related(tobjectdef(def_to))) then
  421. eq:=te_convert_l1;
  422. end;
  423. filedef :
  424. begin
  425. { an implicit file conversion is also allowed }
  426. { from a typed file to an untyped one }
  427. if (def_from.deftype=filedef) and
  428. (tfiledef(def_from).filetyp = ft_typed) and
  429. (tfiledef(def_to).filetyp = ft_untyped) then
  430. eq:=te_convert_l1;
  431. end;
  432. end;
  433. end;
  434. procedure para_allowed(var eq:tequaltype;p:tcallparanode;def_to:tdef);
  435. begin
  436. { Note: eq must be already valid, it will only be updated! }
  437. case def_to.deftype of
  438. formaldef :
  439. begin
  440. { all types can be passed to a formaldef }
  441. eq:=te_equal;
  442. end;
  443. stringdef :
  444. begin
  445. { to support ansi/long/wide strings in a proper way }
  446. { string and string[10] are assumed as equal }
  447. { when searching the correct overloaded procedure }
  448. if (p.resulttype.def.deftype=stringdef) and
  449. (tstringdef(def_to).string_typ=tstringdef(p.resulttype.def).string_typ) then
  450. eq:=te_equal
  451. else
  452. { Passing a constant char to ansistring or shortstring or
  453. a widechar to widestring then handle it as equal. }
  454. if (p.left.nodetype=ordconstn) and
  455. (
  456. is_char(p.resulttype.def) and
  457. (is_shortstring(def_to) or is_ansistring(def_to))
  458. ) or
  459. (
  460. is_widechar(p.resulttype.def) and
  461. is_widestring(def_to)
  462. ) then
  463. eq:=te_equal
  464. end;
  465. setdef :
  466. begin
  467. { set can also be a not yet converted array constructor }
  468. if (p.resulttype.def.deftype=arraydef) and
  469. (tarraydef(p.resulttype.def).IsConstructor) and
  470. not(tarraydef(p.resulttype.def).IsVariant) then
  471. eq:=te_equal;
  472. end;
  473. procvardef :
  474. begin
  475. { in tp7 mode proc -> procvar is allowed }
  476. if (m_tp_procvar in aktmodeswitches) and
  477. (p.left.nodetype=calln) and
  478. (proc_to_procvar_equal(tprocdef(tcallnode(p.left).procdefinition),tprocvardef(def_to),true)>=te_equal) then
  479. eq:=te_equal;
  480. end;
  481. end;
  482. end;
  483. {****************************************************************************
  484. TOBJECTINFOITEM
  485. ****************************************************************************}
  486. constructor tobjectinfoitem.create(def : tobjectdef);
  487. begin
  488. inherited create;
  489. objinfo := def;
  490. end;
  491. {****************************************************************************
  492. TCALLPARANODE
  493. ****************************************************************************}
  494. constructor tcallparanode.create(expr,next : tnode);
  495. begin
  496. inherited create(callparan,expr,next);
  497. if not assigned(expr) then
  498. internalerror(200305091);
  499. expr.set_file_line(self);
  500. callparaflags:=[];
  501. end;
  502. destructor tcallparanode.destroy;
  503. begin
  504. { When the node is used by callnode then
  505. we don't destroy left, the callnode takes care of it }
  506. if used_by_callnode then
  507. left:=nil;
  508. inherited destroy;
  509. end;
  510. constructor tcallparanode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  511. begin
  512. inherited ppuload(t,ppufile);
  513. ppufile.getsmallset(callparaflags);
  514. end;
  515. procedure tcallparanode.ppuwrite(ppufile:tcompilerppufile);
  516. begin
  517. inherited ppuwrite(ppufile);
  518. ppufile.putsmallset(callparaflags);
  519. end;
  520. procedure tcallparanode.derefimpl;
  521. begin
  522. inherited derefimpl;
  523. end;
  524. function tcallparanode.getcopy : tnode;
  525. var
  526. n : tcallparanode;
  527. begin
  528. n:=tcallparanode(inherited getcopy);
  529. n.callparaflags:=callparaflags;
  530. n.paraitem:=paraitem;
  531. result:=n;
  532. end;
  533. procedure tcallparanode.insertintolist(l : tnodelist);
  534. begin
  535. end;
  536. procedure tcallparanode.get_paratype;
  537. var
  538. old_get_para_resulttype : boolean;
  539. old_array_constructor : boolean;
  540. begin
  541. inc(parsing_para_level);
  542. if assigned(right) then
  543. tcallparanode(right).get_paratype;
  544. old_array_constructor:=allow_array_constructor;
  545. old_get_para_resulttype:=get_para_resulttype;
  546. get_para_resulttype:=true;
  547. allow_array_constructor:=true;
  548. resulttypepass(left);
  549. get_para_resulttype:=old_get_para_resulttype;
  550. allow_array_constructor:=old_array_constructor;
  551. if codegenerror then
  552. resulttype:=generrortype
  553. else
  554. resulttype:=left.resulttype;
  555. dec(parsing_para_level);
  556. end;
  557. procedure tcallparanode.insert_typeconv(do_count : boolean);
  558. var
  559. oldtype : ttype;
  560. {$ifdef extdebug}
  561. store_count_ref : boolean;
  562. {$endif def extdebug}
  563. begin
  564. inc(parsing_para_level);
  565. {$ifdef extdebug}
  566. if do_count then
  567. begin
  568. store_count_ref:=count_ref;
  569. count_ref:=true;
  570. end;
  571. {$endif def extdebug}
  572. { Be sure to have the resulttype }
  573. if not assigned(left.resulttype.def) then
  574. resulttypepass(left);
  575. { Handle varargs and hidden paras directly, no typeconvs or }
  576. { typechecking needed }
  577. if (nf_varargs_para in flags) then
  578. begin
  579. { convert pascal to C types }
  580. case left.resulttype.def.deftype of
  581. stringdef :
  582. inserttypeconv(left,charpointertype);
  583. floatdef :
  584. inserttypeconv(left,s64floattype);
  585. end;
  586. set_varstate(left,true);
  587. resulttype:=left.resulttype;
  588. end
  589. else
  590. if (paraitem.is_hidden) then
  591. begin
  592. set_varstate(left,true);
  593. resulttype:=left.resulttype;
  594. end
  595. else
  596. begin
  597. { Do we need arrayconstructor -> set conversion, then insert
  598. it here before the arrayconstructor node breaks the tree
  599. with its conversions of enum->ord }
  600. if (left.nodetype=arrayconstructorn) and
  601. (paraitem.paratype.def.deftype=setdef) then
  602. inserttypeconv(left,paraitem.paratype);
  603. { set some settings needed for arrayconstructor }
  604. if is_array_constructor(left.resulttype.def) then
  605. begin
  606. if is_array_of_const(paraitem.paratype.def) then
  607. begin
  608. if assigned(aktcallprocdef) and
  609. (aktcallprocdef.proccalloption in [pocall_cppdecl,pocall_cdecl]) then
  610. include(left.flags,nf_cargs);
  611. { force variant array }
  612. include(left.flags,nf_forcevaria);
  613. end
  614. else
  615. begin
  616. include(left.flags,nf_novariaallowed);
  617. { now that the resultting type is know we can insert the required
  618. typeconvs for the array constructor }
  619. tarrayconstructornode(left).force_type(tarraydef(paraitem.paratype.def).elementtype);
  620. end;
  621. end;
  622. { check if local proc/func is assigned to procvar }
  623. if left.resulttype.def.deftype=procvardef then
  624. test_local_to_procvar(tprocvardef(left.resulttype.def),paraitem.paratype.def);
  625. { test conversions }
  626. if not(is_shortstring(left.resulttype.def) and
  627. is_shortstring(paraitem.paratype.def)) and
  628. (paraitem.paratype.def.deftype<>formaldef) then
  629. begin
  630. { Process open parameters }
  631. if paramanager.push_high_param(paraitem.paratype.def,aktcallprocdef.proccalloption) then
  632. begin
  633. { insert type conv but hold the ranges of the array }
  634. oldtype:=left.resulttype;
  635. inserttypeconv(left,paraitem.paratype);
  636. left.resulttype:=oldtype;
  637. end
  638. else
  639. begin
  640. { for ordinals, floats and enums, verify if we might cause
  641. some range-check errors. }
  642. if (left.resulttype.def.deftype in [enumdef,orddef,floatdef]) and
  643. (left.nodetype in [vecn,loadn,calln]) then
  644. begin
  645. if (left.resulttype.def.size>paraitem.paratype.def.size) then
  646. begin
  647. if (cs_check_range in aktlocalswitches) then
  648. Message(type_w_smaller_possible_range_check)
  649. else
  650. Message(type_h_smaller_possible_range_check);
  651. end;
  652. end;
  653. inserttypeconv(left,paraitem.paratype);
  654. end;
  655. if codegenerror then
  656. begin
  657. dec(parsing_para_level);
  658. exit;
  659. end;
  660. end;
  661. { check var strings }
  662. if (cs_strict_var_strings in aktlocalswitches) and
  663. is_shortstring(left.resulttype.def) and
  664. is_shortstring(paraitem.paratype.def) and
  665. (paraitem.paratyp in [vs_out,vs_var]) and
  666. not(is_open_string(paraitem.paratype.def)) and
  667. not(equal_defs(left.resulttype.def,paraitem.paratype.def)) then
  668. begin
  669. aktfilepos:=left.fileinfo;
  670. CGMessage(type_e_strict_var_string_violation);
  671. end;
  672. { File types are only allowed for var parameters }
  673. if (paraitem.paratype.def.deftype=filedef) and
  674. (paraitem.paratyp<>vs_var) then
  675. CGMessage(cg_e_file_must_call_by_reference);
  676. { Handle formal parameters separate }
  677. if (paraitem.paratype.def.deftype=formaldef) then
  678. begin
  679. { load procvar if a procedure is passed }
  680. if (m_tp_procvar in aktmodeswitches) and
  681. (left.nodetype=calln) and
  682. (is_void(left.resulttype.def)) then
  683. load_procvar_from_calln(left);
  684. case paraitem.paratyp of
  685. vs_var,
  686. vs_out :
  687. begin
  688. if not valid_for_formal_var(left) then
  689. CGMessagePos(left.fileinfo,parser_e_illegal_parameter_list);
  690. end;
  691. vs_const :
  692. begin
  693. if not valid_for_formal_const(left) then
  694. CGMessagePos(left.fileinfo,parser_e_illegal_parameter_list);
  695. end;
  696. end;
  697. end
  698. else
  699. begin
  700. { check if the argument is allowed }
  701. if (paraitem.paratyp in [vs_out,vs_var]) then
  702. valid_for_var(left);
  703. end;
  704. if paraitem.paratyp in [vs_var,vs_const] then
  705. begin
  706. { Causes problems with const ansistrings if also }
  707. { done for vs_const (JM) }
  708. if paraitem.paratyp = vs_var then
  709. set_unique(left);
  710. make_not_regable(left);
  711. end;
  712. { ansistrings out paramaters doesn't need to be }
  713. { unique, they are finalized }
  714. if paraitem.paratyp=vs_out then
  715. make_not_regable(left);
  716. if do_count then
  717. begin
  718. { not completly proper, but avoids some warnings }
  719. {if (paraitem.paratyp in [vs_var,vs_out]) then
  720. set_funcret_is_valid(left); }
  721. set_varstate(left,not(paraitem.paratyp in [vs_var,vs_out]));
  722. end;
  723. { must only be done after typeconv PM }
  724. resulttype:=paraitem.paratype;
  725. end;
  726. { process next node }
  727. if assigned(right) then
  728. tcallparanode(right).insert_typeconv(do_count);
  729. dec(parsing_para_level);
  730. {$ifdef extdebug}
  731. if do_count then
  732. count_ref:=store_count_ref;
  733. {$endif def extdebug}
  734. end;
  735. procedure tcallparanode.det_registers;
  736. var
  737. old_get_para_resulttype : boolean;
  738. old_array_constructor : boolean;
  739. begin
  740. if assigned(right) then
  741. begin
  742. tcallparanode(right).det_registers;
  743. registers32:=right.registers32;
  744. registersfpu:=right.registersfpu;
  745. {$ifdef SUPPORT_MMX}
  746. registersmmx:=right.registersmmx;
  747. {$endif}
  748. end;
  749. old_array_constructor:=allow_array_constructor;
  750. old_get_para_resulttype:=get_para_resulttype;
  751. get_para_resulttype:=true;
  752. allow_array_constructor:=true;
  753. firstpass(left);
  754. get_para_resulttype:=old_get_para_resulttype;
  755. allow_array_constructor:=old_array_constructor;
  756. if left.registers32>registers32 then
  757. registers32:=left.registers32;
  758. if left.registersfpu>registersfpu then
  759. registersfpu:=left.registersfpu;
  760. {$ifdef SUPPORT_MMX}
  761. if left.registersmmx>registersmmx then
  762. registersmmx:=left.registersmmx;
  763. {$endif SUPPORT_MMX}
  764. end;
  765. procedure tcallparanode.firstcallparan(do_count : boolean);
  766. begin
  767. if not assigned(left.resulttype.def) then
  768. begin
  769. get_paratype;
  770. {
  771. if assigned(defcoll) then
  772. insert_typeconv(defcoll,do_count);
  773. }
  774. end;
  775. det_registers;
  776. end;
  777. function tcallparanode.docompare(p: tnode): boolean;
  778. begin
  779. docompare :=
  780. inherited docompare(p) and
  781. (callparaflags = tcallparanode(p).callparaflags)
  782. ;
  783. end;
  784. procedure tcallparanode.printnodetree(var t:text);
  785. begin
  786. printnodelist(t);
  787. end;
  788. {****************************************************************************
  789. TCALLNODE
  790. ****************************************************************************}
  791. constructor tcallnode.create(l:tnode;v : tprocsym;st : tsymtable; mp : tnode);
  792. begin
  793. inherited create(calln,l,nil);
  794. symtableprocentry:=v;
  795. symtableproc:=st;
  796. include(flags,nf_return_value_used);
  797. methodpointer:=mp;
  798. procdefinition:=nil;
  799. restypeset:=false;
  800. funcretnode:=nil;
  801. paralength:=-1;
  802. end;
  803. constructor tcallnode.create_def(l:tnode;def:tprocdef;mp:tnode);
  804. begin
  805. inherited create(calln,l,nil);
  806. symtableprocentry:=nil;
  807. symtableproc:=nil;
  808. include(flags,nf_return_value_used);
  809. methodpointer:=mp;
  810. procdefinition:=def;
  811. restypeset:=false;
  812. funcretnode:=nil;
  813. paralength:=-1;
  814. end;
  815. constructor tcallnode.create_procvar(l,r:tnode);
  816. begin
  817. inherited create(calln,l,r);
  818. symtableprocentry:=nil;
  819. symtableproc:=nil;
  820. include(flags,nf_return_value_used);
  821. methodpointer:=nil;
  822. procdefinition:=nil;
  823. restypeset:=false;
  824. funcretnode:=nil;
  825. paralength:=-1;
  826. end;
  827. constructor tcallnode.createintern(const name: string; params: tnode);
  828. var
  829. srsym: tsym;
  830. symowner: tsymtable;
  831. begin
  832. if not (cs_compilesystem in aktmoduleswitches) then
  833. begin
  834. srsym := searchsymonlyin(systemunit,name);
  835. symowner := systemunit;
  836. end
  837. else
  838. begin
  839. searchsym(name,srsym,symowner);
  840. if not assigned(srsym) then
  841. searchsym(upper(name),srsym,symowner);
  842. end;
  843. if not assigned(srsym) or
  844. (srsym.typ <> procsym) then
  845. begin
  846. {$ifdef EXTDEBUG}
  847. Comment(V_Error,'unknown compilerproc '+name);
  848. {$endif EXTDEBUG}
  849. internalerror(200107271);
  850. end;
  851. self.create(params,tprocsym(srsym),symowner,nil);
  852. end;
  853. constructor tcallnode.createinternres(const name: string; params: tnode; const res: ttype);
  854. begin
  855. self.createintern(name,params);
  856. restype := res;
  857. restypeset := true;
  858. { both the normal and specified resulttype either have to be returned via a }
  859. { parameter or not, but no mixing (JM) }
  860. if paramanager.ret_in_param(restype.def,pocall_compilerproc) xor
  861. paramanager.ret_in_param(symtableprocentry.first_procdef.rettype.def,symtableprocentry.first_procdef.proccalloption) then
  862. internalerror(200108291);
  863. end;
  864. constructor tcallnode.createinternreturn(const name: string; params: tnode; returnnode : tnode);
  865. begin
  866. self.createintern(name,params);
  867. funcretnode:=returnnode;
  868. if not paramanager.ret_in_param(symtableprocentry.first_procdef.rettype.def,symtableprocentry.first_procdef.proccalloption) then
  869. internalerror(200204247);
  870. end;
  871. destructor tcallnode.destroy;
  872. begin
  873. methodpointer.free;
  874. funcretnode.free;
  875. inherited destroy;
  876. end;
  877. constructor tcallnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  878. begin
  879. inherited ppuload(t,ppufile);
  880. symtableprocentry:=tprocsym(ppufile.getderef);
  881. {$ifdef fpc}
  882. {$warning FIXME: No withsymtable support}
  883. {$endif}
  884. symtableproc:=nil;
  885. procdefinition:=tprocdef(ppufile.getderef);
  886. restypeset:=boolean(ppufile.getbyte);
  887. methodpointer:=ppuloadnode(ppufile);
  888. funcretnode:=ppuloadnode(ppufile);
  889. end;
  890. procedure tcallnode.ppuwrite(ppufile:tcompilerppufile);
  891. begin
  892. inherited ppuwrite(ppufile);
  893. ppufile.putderef(symtableprocentry);
  894. ppufile.putderef(procdefinition);
  895. ppufile.putbyte(byte(restypeset));
  896. ppuwritenode(ppufile,methodpointer);
  897. ppuwritenode(ppufile,funcretnode);
  898. end;
  899. procedure tcallnode.derefimpl;
  900. begin
  901. inherited derefimpl;
  902. resolvesym(pointer(symtableprocentry));
  903. symtableproc:=symtableprocentry.owner;
  904. resolvedef(pointer(procdefinition));
  905. if assigned(methodpointer) then
  906. methodpointer.derefimpl;
  907. if assigned(funcretnode) then
  908. funcretnode.derefimpl;
  909. end;
  910. function tcallnode.getcopy : tnode;
  911. var
  912. n : tcallnode;
  913. begin
  914. n:=tcallnode(inherited getcopy);
  915. n.symtableprocentry:=symtableprocentry;
  916. n.symtableproc:=symtableproc;
  917. n.procdefinition:=procdefinition;
  918. n.restype := restype;
  919. n.restypeset := restypeset;
  920. if assigned(methodpointer) then
  921. n.methodpointer:=methodpointer.getcopy
  922. else
  923. n.methodpointer:=nil;
  924. if assigned(funcretnode) then
  925. n.funcretnode:=funcretnode.getcopy
  926. else
  927. n.funcretnode:=nil;
  928. result:=n;
  929. end;
  930. procedure tcallnode.insertintolist(l : tnodelist);
  931. begin
  932. end;
  933. procedure tcallnode.verifyabstract(p : tnamedindexitem;arg:pointer);
  934. var
  935. hp : tprocdef;
  936. j: integer;
  937. begin
  938. if (tsym(p).typ=procsym) then
  939. begin
  940. for j:=1 to tprocsym(p).procdef_count do
  941. begin
  942. { index starts at 1 }
  943. hp:=tprocsym(p).procdef[j];
  944. { If this is an abstract method insert into the list }
  945. if (po_abstractmethod in hp.procoptions) then
  946. AbstractMethodsList.Insert(hp.procsym.name)
  947. else
  948. { If this symbol is already in the list, and it is
  949. an overriding method or dynamic, then remove it from the list
  950. }
  951. begin
  952. { symbol was found }
  953. if AbstractMethodsList.Find(hp.procsym.name) <> nil then
  954. begin
  955. if po_overridingmethod in hp.procoptions then
  956. AbstractMethodsList.Remove(hp.procsym.name);
  957. end;
  958. end;
  959. end;
  960. end;
  961. end;
  962. procedure tcallnode.verifyabstractcalls;
  963. var
  964. objectdf : tobjectdef;
  965. parents : tlinkedlist;
  966. objectinfo : tobjectinfoitem;
  967. stritem : tstringlistitem;
  968. _classname : string;
  969. begin
  970. objectdf := nil;
  971. { verify if trying to create an instance of a class which contains
  972. non-implemented abstract methods }
  973. { first verify this class type, no class than exit }
  974. { also, this checking can only be done if the constructor is directly
  975. called, indirect constructor calls cannot be checked.
  976. }
  977. if assigned(methodpointer) and assigned(methodpointer.resulttype.def) then
  978. if (methodpointer.resulttype.def.deftype = classrefdef) and
  979. (methodpointer.nodetype in [typen,loadvmtaddrn]) then
  980. begin
  981. if (tclassrefdef(methodpointer.resulttype.def).pointertype.def.deftype = objectdef) then
  982. objectdf := tobjectdef(tclassrefdef(methodpointer.resulttype.def).pointertype.def);
  983. end;
  984. if not assigned(objectdf) then exit;
  985. if assigned(objectdf.symtable.name) then
  986. _classname := objectdf.symtable.name^
  987. else
  988. _classname := '';
  989. parents := tlinkedlist.create;
  990. AbstractMethodsList := tstringlist.create;
  991. { insert all parents in this class : the first item in the
  992. list will be the base parent of the class .
  993. }
  994. while assigned(objectdf) do
  995. begin
  996. objectinfo:=tobjectinfoitem.create(objectdf);
  997. parents.insert(objectinfo);
  998. objectdf := objectdf.childof;
  999. end;
  1000. { now all parents are in the correct order
  1001. insert all abstract methods in the list, and remove
  1002. those which are overriden by parent classes.
  1003. }
  1004. objectinfo:=tobjectinfoitem(parents.first);
  1005. while assigned(objectinfo) do
  1006. begin
  1007. objectdf := objectinfo.objinfo;
  1008. if assigned(objectdf.symtable) then
  1009. objectdf.symtable.foreach({$ifdef FPCPROCVAR}@{$endif}verifyabstract,nil);
  1010. objectinfo:=tobjectinfoitem(objectinfo.next);
  1011. end;
  1012. if assigned(parents) then
  1013. parents.free;
  1014. { Finally give out a warning for each abstract method still in the list }
  1015. stritem := tstringlistitem(AbstractMethodsList.first);
  1016. while assigned(stritem) do
  1017. begin
  1018. if assigned(stritem.fpstr) then
  1019. Message2(type_w_instance_with_abstract,lower(_classname),lower(stritem.fpstr^));
  1020. stritem := tstringlistitem(stritem.next);
  1021. end;
  1022. if assigned(AbstractMethodsList) then
  1023. AbstractMethodsList.Free;
  1024. end;
  1025. function Tcallnode.candidates_find:pcandidate;
  1026. var
  1027. j : integer;
  1028. pd : tprocdef;
  1029. procs,hp : pcandidate;
  1030. found,
  1031. has_overload_directive : boolean;
  1032. srsymtable : tsymtable;
  1033. srprocsym : tprocsym;
  1034. procedure proc_add(pd:tprocdef);
  1035. var
  1036. i : integer;
  1037. begin
  1038. { generate new candidate entry }
  1039. new(hp);
  1040. fillchar(hp^,sizeof(tcandidate),0);
  1041. hp^.data:=pd;
  1042. hp^.next:=procs;
  1043. procs:=hp;
  1044. { Find last parameter, skip all default parameters
  1045. that are not passed. Ignore this skipping for varargs }
  1046. hp^.firstpara:=tparaitem(pd.Para.last);
  1047. if not(po_varargs in pd.procoptions) then
  1048. begin
  1049. for i:=1 to pd.maxparacount-paralength do
  1050. hp^.firstpara:=tparaitem(hp^.firstPara.previous);
  1051. end;
  1052. end;
  1053. begin
  1054. procs:=nil;
  1055. { when the definition has overload directive set, we search for
  1056. overloaded definitions in the class, this only needs to be done once
  1057. for class entries as the tree keeps always the same }
  1058. if (not symtableprocentry.overloadchecked) and
  1059. (po_overload in symtableprocentry.first_procdef.procoptions) and
  1060. (symtableprocentry.owner.symtabletype=objectsymtable) then
  1061. search_class_overloads(symtableprocentry);
  1062. { link all procedures which have the same # of parameters }
  1063. for j:=1 to symtableprocentry.procdef_count do
  1064. begin
  1065. pd:=symtableprocentry.procdef[j];
  1066. { Is the procdef visible? This needs to be checked on
  1067. procdef level since a symbol can contain both private and
  1068. public declarations. But the check should not be done
  1069. when the callnode is generated by a property }
  1070. if (nf_isproperty in flags) or
  1071. (pd.owner.symtabletype<>objectsymtable) or
  1072. pd.is_visible_for_proc(current_procdef) then
  1073. begin
  1074. { only when the # of parameter are supported by the
  1075. procedure }
  1076. if (paralength>=pd.minparacount) and
  1077. ((po_varargs in pd.procoptions) or { varargs }
  1078. (paralength<=pd.maxparacount)) then
  1079. proc_add(pd);
  1080. end;
  1081. end;
  1082. { remember if the procedure is declared with the overload directive,
  1083. it's information is still needed also after all procs are removed }
  1084. has_overload_directive:=(po_overload in symtableprocentry.first_procdef.procoptions);
  1085. { when the definition has overload directive set, we search for
  1086. overloaded definitions in the symtablestack. The found
  1087. entries are only added to the procs list and not the procsym, because
  1088. the list can change in every situation }
  1089. if has_overload_directive and
  1090. (symtableprocentry.owner.symtabletype<>objectsymtable) then
  1091. begin
  1092. srsymtable:=symtableprocentry.owner.next;
  1093. while assigned(srsymtable) do
  1094. begin
  1095. if srsymtable.symtabletype in [localsymtable,staticsymtable,globalsymtable] then
  1096. begin
  1097. srprocsym:=tprocsym(srsymtable.speedsearch(symtableprocentry.name,symtableprocentry.speedvalue));
  1098. { process only visible procsyms }
  1099. if assigned(srprocsym) and
  1100. (srprocsym.typ=procsym) and
  1101. srprocsym.is_visible_for_proc(current_procdef) then
  1102. begin
  1103. { if this procedure doesn't have overload we can stop
  1104. searching }
  1105. if not(po_overload in srprocsym.first_procdef.procoptions) then
  1106. break;
  1107. { process all overloaded definitions }
  1108. for j:=1 to srprocsym.procdef_count do
  1109. begin
  1110. pd:=srprocsym.procdef[j];
  1111. { only when the # of parameter are supported by the
  1112. procedure }
  1113. if (paralength>=pd.minparacount) and
  1114. ((po_varargs in pd.procoptions) or { varargs }
  1115. (paralength<=pd.maxparacount)) then
  1116. begin
  1117. found:=false;
  1118. hp:=procs;
  1119. while assigned(hp) do
  1120. begin
  1121. if compare_paras(hp^.data.para,pd.para,cp_value_equal_const,false)>=te_equal then
  1122. begin
  1123. found:=true;
  1124. break;
  1125. end;
  1126. hp:=hp^.next;
  1127. end;
  1128. if not found then
  1129. proc_add(pd);
  1130. end;
  1131. end;
  1132. end;
  1133. end;
  1134. srsymtable:=srsymtable.next;
  1135. end;
  1136. end;
  1137. candidates_find:=procs;
  1138. end;
  1139. procedure tcallnode.candidates_free(procs:pcandidate);
  1140. var
  1141. hpnext,
  1142. hp : pcandidate;
  1143. begin
  1144. hp:=procs;
  1145. while assigned(hp) do
  1146. begin
  1147. hpnext:=hp^.next;
  1148. dispose(hp);
  1149. hp:=hpnext;
  1150. end;
  1151. end;
  1152. procedure tcallnode.candidates_list(procs:pcandidate;all:boolean);
  1153. var
  1154. hp : pcandidate;
  1155. begin
  1156. hp:=procs;
  1157. while assigned(hp) do
  1158. begin
  1159. if all or
  1160. (not hp^.invalid) then
  1161. MessagePos1(hp^.data.fileinfo,sym_h_param_list,hp^.data.fullprocname(false));
  1162. hp:=hp^.next;
  1163. end;
  1164. end;
  1165. {$ifdef EXTDEBUG}
  1166. procedure Tcallnode.candidates_dump_info(lvl:longint;procs:pcandidate);
  1167. function ParaTreeStr(p:tcallparanode):string;
  1168. begin
  1169. result:='';
  1170. while assigned(p) do
  1171. begin
  1172. if result<>'' then
  1173. result:=result+',';
  1174. result:=result+p.resulttype.def.typename;
  1175. p:=tcallparanode(p.right);
  1176. end;
  1177. end;
  1178. var
  1179. hp : pcandidate;
  1180. currpara : tparaitem;
  1181. begin
  1182. if not CheckVerbosity(lvl) then
  1183. exit;
  1184. Comment(lvl+V_LineInfo,'Overloaded callnode: '+symtableprocentry.name+'('+ParaTreeStr(tcallparanode(left))+')');
  1185. hp:=procs;
  1186. while assigned(hp) do
  1187. begin
  1188. Comment(lvl,' '+hp^.data.fullprocname(false));
  1189. if (hp^.invalid) then
  1190. Comment(lvl,' invalid')
  1191. else
  1192. begin
  1193. Comment(lvl,' ex: '+tostr(hp^.exact_count)+
  1194. ' eq: '+tostr(hp^.equal_count)+
  1195. ' l1: '+tostr(hp^.cl1_count)+
  1196. ' l2: '+tostr(hp^.cl2_count)+
  1197. ' l3: '+tostr(hp^.cl3_count)+
  1198. ' oper: '+tostr(hp^.coper_count)+
  1199. ' ord: '+realtostr(hp^.exact_count));
  1200. { Print parameters in left-right order }
  1201. currpara:=hp^.firstpara;
  1202. if assigned(currpara) then
  1203. begin
  1204. while assigned(currpara.next) do
  1205. currpara:=tparaitem(currpara.next);
  1206. end;
  1207. while assigned(currpara) do
  1208. begin
  1209. if (not currpara.is_hidden) then
  1210. Comment(lvl,' - '+currpara.paratype.def.typename+' : '+EqualTypeName[currpara.eqval]);
  1211. currpara:=tparaitem(currpara.previous);
  1212. end;
  1213. end;
  1214. hp:=hp^.next;
  1215. end;
  1216. end;
  1217. {$endif EXTDEBUG}
  1218. procedure Tcallnode.candidates_get_information(procs:pcandidate);
  1219. var
  1220. hp : pcandidate;
  1221. currpara : tparaitem;
  1222. currparanr : byte;
  1223. def_from,
  1224. def_to : tdef;
  1225. pt : tcallparanode;
  1226. eq : tequaltype;
  1227. convtype : tconverttype;
  1228. pdoper : tprocdef;
  1229. begin
  1230. { process all procs }
  1231. hp:=procs;
  1232. while assigned(hp) do
  1233. begin
  1234. { We compare parameters in reverse order (right to left),
  1235. the firstpara is already pointing to the last parameter
  1236. were we need to start comparing }
  1237. currparanr:=paralength;
  1238. currpara:=hp^.firstpara;
  1239. while assigned(currpara) and (currpara.is_hidden) do
  1240. currpara:=tparaitem(currpara.previous);
  1241. pt:=tcallparanode(left);
  1242. while assigned(pt) and assigned(currpara) do
  1243. begin
  1244. { retrieve current parameter definitions to compares }
  1245. eq:=te_incompatible;
  1246. def_from:=pt.resulttype.def;
  1247. def_to:=currpara.paratype.def;
  1248. if not(assigned(def_from)) then
  1249. internalerror(200212091);
  1250. if not(
  1251. assigned(def_to) or
  1252. ((po_varargs in hp^.data.procoptions) and
  1253. (currparanr>hp^.data.minparacount))
  1254. ) then
  1255. internalerror(200212092);
  1256. { varargs are always equal, but not exact }
  1257. if (po_varargs in hp^.data.procoptions) and
  1258. (currparanr>hp^.data.minparacount) then
  1259. begin
  1260. inc(hp^.equal_count);
  1261. eq:=te_equal;
  1262. end
  1263. else
  1264. { same definition -> exact }
  1265. if (def_from=def_to) then
  1266. begin
  1267. inc(hp^.exact_count);
  1268. eq:=te_exact;
  1269. end
  1270. else
  1271. { for value and const parameters check if a integer is constant or
  1272. included in other integer -> equal and calc ordinal_distance }
  1273. if not(currpara.paratyp in [vs_var,vs_out]) and
  1274. is_integer(def_from) and
  1275. is_integer(def_to) and
  1276. is_in_limit(def_from,def_to) then
  1277. begin
  1278. inc(hp^.equal_count);
  1279. eq:=te_equal;
  1280. hp^.ordinal_distance:=hp^.ordinal_distance+
  1281. abs(bestreal(torddef(def_from).low)-bestreal(torddef(def_to).low));
  1282. hp^.ordinal_distance:=hp^.ordinal_distance+
  1283. abs(bestreal(torddef(def_to).high)-bestreal(torddef(def_from).high));
  1284. { Give wrong sign a small penalty, this is need to get a diffrence
  1285. from word->[longword,longint] }
  1286. if is_signed(def_from)<>is_signed(def_to) then
  1287. hp^.ordinal_distance:=hp^.ordinal_distance+1.0;
  1288. end
  1289. else
  1290. { generic type comparision }
  1291. begin
  1292. eq:=compare_defs_ext(def_from,def_to,pt.left.nodetype,
  1293. false,true,convtype,pdoper);
  1294. { when the types are not equal we need to check
  1295. some special case for parameter passing }
  1296. if (eq<te_equal) then
  1297. begin
  1298. if currpara.paratyp in [vs_var,vs_out] then
  1299. begin
  1300. { para requires an equal type so the previous found
  1301. match was not good enough, reset to incompatible }
  1302. eq:=te_incompatible;
  1303. { var_para_allowed will return te_equal and te_convert_l1 to
  1304. make a difference for best matching }
  1305. var_para_allowed(eq,pt.resulttype.def,currpara.paratype.def)
  1306. end
  1307. else
  1308. para_allowed(eq,pt,def_to);
  1309. end;
  1310. case eq of
  1311. te_exact :
  1312. internalerror(200212071); { already checked }
  1313. te_equal :
  1314. inc(hp^.equal_count);
  1315. te_convert_l1 :
  1316. inc(hp^.cl1_count);
  1317. te_convert_l2 :
  1318. inc(hp^.cl2_count);
  1319. te_convert_l3 :
  1320. inc(hp^.cl3_count);
  1321. te_convert_operator :
  1322. inc(hp^.coper_count);
  1323. te_incompatible :
  1324. hp^.invalid:=true;
  1325. else
  1326. internalerror(200212072);
  1327. end;
  1328. end;
  1329. { stop checking when an incompatible parameter is found }
  1330. if hp^.invalid then
  1331. begin
  1332. { store the current parameter info for
  1333. a nice error message when no procedure is found }
  1334. hp^.wrongpara:=currpara;
  1335. hp^.wrongparanr:=currparanr;
  1336. break;
  1337. end;
  1338. {$ifdef EXTDEBUG}
  1339. { store equal in node tree for dump }
  1340. currpara.eqval:=eq;
  1341. {$endif EXTDEBUG}
  1342. { next parameter in the call tree }
  1343. pt:=tcallparanode(pt.right);
  1344. { next parameter for definition, only goto next para
  1345. if we're out of the varargs }
  1346. if not(po_varargs in hp^.data.procoptions) or
  1347. (currparanr<=hp^.data.maxparacount) then
  1348. begin
  1349. { Ignore vs_hidden parameters }
  1350. repeat
  1351. currpara:=tparaitem(currpara.previous);
  1352. until (not assigned(currpara)) or (not currpara.is_hidden);
  1353. end;
  1354. dec(currparanr);
  1355. end;
  1356. if not(hp^.invalid) and
  1357. (assigned(pt) or assigned(currpara) or (currparanr<>0)) then
  1358. internalerror(200212141);
  1359. { next candidate }
  1360. hp:=hp^.next;
  1361. end;
  1362. end;
  1363. function Tcallnode.candidates_choose_best(procs:pcandidate;var bestpd:tprocdef):integer;
  1364. var
  1365. besthpstart,
  1366. hp : pcandidate;
  1367. cntpd,
  1368. res : integer;
  1369. begin
  1370. {
  1371. Returns the number of candidates left and the
  1372. first candidate is returned in pdbest
  1373. }
  1374. { Setup the first procdef as best, only count it as a result
  1375. when it is valid }
  1376. bestpd:=procs^.data;
  1377. if procs^.invalid then
  1378. cntpd:=0
  1379. else
  1380. cntpd:=1;
  1381. if assigned(procs^.next) then
  1382. begin
  1383. besthpstart:=procs;
  1384. hp:=procs^.next;
  1385. while assigned(hp) do
  1386. begin
  1387. res:=is_better_candidate(hp,besthpstart);
  1388. if (res>0) then
  1389. begin
  1390. { hp is better, flag all procs to be incompatible }
  1391. while (besthpstart<>hp) do
  1392. begin
  1393. besthpstart^.invalid:=true;
  1394. besthpstart:=besthpstart^.next;
  1395. end;
  1396. { besthpstart is already set to hp }
  1397. bestpd:=besthpstart^.data;
  1398. cntpd:=1;
  1399. end
  1400. else
  1401. if (res<0) then
  1402. begin
  1403. { besthpstart is better, flag current hp to be incompatible }
  1404. hp^.invalid:=true;
  1405. end
  1406. else
  1407. begin
  1408. { res=0, both are valid }
  1409. if not hp^.invalid then
  1410. inc(cntpd);
  1411. end;
  1412. hp:=hp^.next;
  1413. end;
  1414. end;
  1415. candidates_choose_best:=cntpd;
  1416. end;
  1417. procedure tcallnode.candidates_find_wrong_para(procs:pcandidate);
  1418. var
  1419. currparanr : smallint;
  1420. hp : pcandidate;
  1421. pt : tcallparanode;
  1422. begin
  1423. { Only process the first overloaded procdef }
  1424. hp:=procs;
  1425. { Find callparanode corresponding to the argument }
  1426. pt:=tcallparanode(left);
  1427. currparanr:=paralength;
  1428. while assigned(pt) and
  1429. (currparanr>hp^.wrongparanr) do
  1430. begin
  1431. pt:=tcallparanode(pt.right);
  1432. dec(currparanr);
  1433. end;
  1434. if (currparanr<>hp^.wrongparanr) or
  1435. not assigned(pt) then
  1436. internalerror(200212094);
  1437. { Show error message, when it was a var or out parameter
  1438. guess that it is a missing typeconv }
  1439. if hp^.wrongpara.paratyp in [vs_var,vs_out] then
  1440. CGMessagePos2(left.fileinfo,parser_e_call_by_ref_without_typeconv,
  1441. pt.resulttype.def.typename,hp^.wrongpara.paratype.def.typename)
  1442. else
  1443. CGMessagePos3(pt.fileinfo,type_e_wrong_parameter_type,
  1444. tostr(hp^.wrongparanr),pt.resulttype.def.typename,hp^.wrongpara.paratype.def.typename);
  1445. end;
  1446. function tcallnode.gen_self_tree:tnode;
  1447. var
  1448. selftree : tnode;
  1449. begin
  1450. selftree:=nil;
  1451. { constructors }
  1452. if (procdefinition.proctypeoption=potype_constructor) then
  1453. begin
  1454. if not(nf_inherited in flags) then
  1455. begin
  1456. { push 0 as self when allocation is needed }
  1457. if (methodpointer.resulttype.def.deftype=classrefdef) or
  1458. (nf_new_call in flags) then
  1459. selftree:=cpointerconstnode.create(0,voidpointertype)
  1460. else
  1461. begin
  1462. if methodpointer.nodetype=typen then
  1463. selftree:=load_self_node
  1464. else
  1465. selftree:=methodpointer.getcopy;
  1466. end;
  1467. end
  1468. else
  1469. selftree:=load_self_node;
  1470. end
  1471. else
  1472. begin
  1473. { Calling a static/class method from a non-static/class method,
  1474. then we need to load self with the VMT }
  1475. if (
  1476. (po_classmethod in procdefinition.procoptions) and
  1477. not(assigned(current_procdef) and
  1478. (po_classmethod in current_procdef.procoptions))
  1479. ) or
  1480. (
  1481. (po_staticmethod in procdefinition.procoptions) and
  1482. not(assigned(current_procdef) and
  1483. (po_staticmethod in current_procdef.procoptions))
  1484. ) then
  1485. begin
  1486. if (procdefinition.deftype<>procdef) then
  1487. internalerror(200305062);
  1488. if (oo_has_vmt in tprocdef(procdefinition)._class.objectoptions) then
  1489. begin
  1490. if methodpointer.resulttype.def.deftype=classrefdef then
  1491. selftree:=methodpointer.getcopy
  1492. else
  1493. selftree:=cloadvmtaddrnode.create(methodpointer.getcopy);
  1494. end
  1495. else
  1496. selftree:=cpointerconstnode.create(0,voidpointertype);
  1497. end
  1498. else
  1499. begin
  1500. if methodpointer.nodetype=typen then
  1501. selftree:=load_self_node
  1502. else
  1503. selftree:=methodpointer.getcopy;
  1504. end;
  1505. end;
  1506. result:=selftree;
  1507. end;
  1508. function tcallnode.gen_vmt_tree:tnode;
  1509. var
  1510. vmttree : tnode;
  1511. begin
  1512. vmttree:=nil;
  1513. if not(procdefinition.proctypeoption in [potype_constructor,potype_destructor]) then
  1514. internalerror(200305051);
  1515. { inherited call, no create/destroy }
  1516. if (nf_inherited in flags) then
  1517. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1518. else
  1519. { constructor with extended syntax called from new }
  1520. if (nf_new_call in flags) then
  1521. vmttree:=cloadvmtaddrnode.create(ctypenode.create(methodpointer.resulttype))
  1522. else
  1523. { destructor with extended syntax called from dispose }
  1524. if (nf_dispose_call in flags) then
  1525. vmttree:=cloadvmtaddrnode.create(methodpointer.getcopy)
  1526. else
  1527. if (methodpointer.resulttype.def.deftype=classrefdef) then
  1528. begin
  1529. { constructor call via classreference => allocate memory }
  1530. if (procdefinition.proctypeoption=potype_constructor) and
  1531. is_class(tclassrefdef(methodpointer.resulttype.def).pointertype.def) then
  1532. vmttree:=methodpointer.getcopy
  1533. else
  1534. vmttree:=cpointerconstnode.create(0,voidpointertype);
  1535. end
  1536. else
  1537. { class }
  1538. if is_class(methodpointer.resulttype.def) then
  1539. begin
  1540. { destructor: release instance, flag(vmt)=1
  1541. constructor: direct call, do nothing, leave vmt=0 }
  1542. if (procdefinition.proctypeoption=potype_destructor) then
  1543. begin
  1544. { do not release when called from member function
  1545. without specifying self explicit }
  1546. if (nf_member_call in flags) then
  1547. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1548. else
  1549. vmttree:=cpointerconstnode.create(1,voidpointertype);
  1550. end
  1551. else
  1552. vmttree:=cpointerconstnode.create(0,voidpointertype);
  1553. end
  1554. else
  1555. { object }
  1556. begin
  1557. { destructor: direct call, no dispose, vmt=0
  1558. constructor: initialize object, load vmt }
  1559. if (procdefinition.proctypeoption=potype_constructor) then
  1560. vmttree:=cloadvmtaddrnode.create(ctypenode.create(methodpointer.resulttype))
  1561. else
  1562. vmttree:=cpointerconstnode.create(0,voidpointertype);
  1563. end;
  1564. result:=vmttree;
  1565. end;
  1566. procedure tcallnode.bind_paraitem;
  1567. var
  1568. i : integer;
  1569. pt : tcallparanode;
  1570. oldppt : ^tcallparanode;
  1571. currpara : tparaitem;
  1572. used_by_callnode : boolean;
  1573. hiddentree : tnode;
  1574. newstatement : tstatementnode;
  1575. temp : ttempcreatenode;
  1576. begin
  1577. pt:=tcallparanode(left);
  1578. oldppt:=@left;
  1579. { flag all callparanodes that belong to the varargs }
  1580. if (po_varargs in procdefinition.procoptions) then
  1581. begin
  1582. i:=paralength;
  1583. while (i>procdefinition.maxparacount) do
  1584. begin
  1585. include(tcallparanode(pt).flags,nf_varargs_para);
  1586. oldppt:[email protected];
  1587. pt:=tcallparanode(pt.right);
  1588. dec(i);
  1589. end;
  1590. end;
  1591. { insert hidden parameters }
  1592. currpara:=tparaitem(procdefinition.Para.last);
  1593. while assigned(currpara) do
  1594. begin
  1595. if currpara.is_hidden then
  1596. begin
  1597. { generate hidden tree }
  1598. used_by_callnode:=false;
  1599. hiddentree:=nil;
  1600. if (vo_is_funcret in tvarsym(currpara.parasym).varoptions) then
  1601. begin
  1602. { Generate funcretnode if not specified }
  1603. if assigned(funcretnode) then
  1604. begin
  1605. hiddentree:=funcretnode;
  1606. funcretnode:=nil;
  1607. end
  1608. else
  1609. begin
  1610. hiddentree:=internalstatements(newstatement,false);
  1611. { need to use resulttype instead of procdefinition.rettype,
  1612. because they can be different }
  1613. temp:=ctempcreatenode.create(resulttype,resulttype.def.size,tt_persistent);
  1614. addstatement(newstatement,temp);
  1615. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  1616. addstatement(newstatement,ctemprefnode.create(temp));
  1617. end;
  1618. end
  1619. else
  1620. if vo_is_high_value in tvarsym(currpara.parasym).varoptions then
  1621. begin
  1622. if not assigned(pt) then
  1623. internalerror(200304082);
  1624. { we need the information of the next parameter }
  1625. hiddentree:=gen_high_tree(pt.left,is_open_string(tparaitem(currpara.previous).paratype.def));
  1626. end
  1627. else
  1628. if vo_is_self in tvarsym(currpara.parasym).varoptions then
  1629. begin
  1630. {$warning todo methodpointer}
  1631. if (right=nil) then
  1632. hiddentree:=gen_self_tree
  1633. else
  1634. hiddentree:=cnothingnode.create;
  1635. end
  1636. else
  1637. if vo_is_vmt in tvarsym(currpara.parasym).varoptions then
  1638. begin
  1639. hiddentree:=gen_vmt_tree;
  1640. end;
  1641. { add the hidden parameter }
  1642. if not assigned(hiddentree) then
  1643. internalerror(200304073);
  1644. { Already insert para and let the previous node point to
  1645. this new node }
  1646. pt:=ccallparanode.create(hiddentree,oldppt^);
  1647. pt.used_by_callnode:=used_by_callnode;
  1648. oldppt^:=pt;
  1649. end;
  1650. { Bind paraitem to this node }
  1651. pt.paraitem:=currpara;
  1652. { Next node and paraitem }
  1653. oldppt:[email protected];
  1654. pt:=tcallparanode(pt.right);
  1655. currpara:=tparaitem(currpara.previous);
  1656. end;
  1657. end;
  1658. function tcallnode.det_resulttype:tnode;
  1659. var
  1660. procs : pcandidate;
  1661. oldcallprocdef : tabstractprocdef;
  1662. hpt : tnode;
  1663. pt : tcallparanode;
  1664. lastpara : longint;
  1665. currpara : tparaitem;
  1666. cand_cnt : integer;
  1667. i : longint;
  1668. method_must_be_valid,
  1669. is_const : boolean;
  1670. label
  1671. errorexit;
  1672. begin
  1673. result:=nil;
  1674. procs:=nil;
  1675. oldcallprocdef:=aktcallprocdef;
  1676. aktcallprocdef:=nil;
  1677. { determine length of parameter list }
  1678. pt:=tcallparanode(left);
  1679. paralength:=0;
  1680. while assigned(pt) do
  1681. begin
  1682. inc(paralength);
  1683. pt:=tcallparanode(pt.right);
  1684. end;
  1685. { determine the type of the parameters }
  1686. if assigned(left) then
  1687. begin
  1688. tcallparanode(left).get_paratype;
  1689. if codegenerror then
  1690. goto errorexit;
  1691. end;
  1692. { procedure variable ? }
  1693. if assigned(right) then
  1694. begin
  1695. set_varstate(right,true);
  1696. resulttypepass(right);
  1697. if codegenerror then
  1698. exit;
  1699. procdefinition:=tabstractprocdef(right.resulttype.def);
  1700. { Compare parameters from right to left }
  1701. currpara:=tparaitem(procdefinition.Para.last);
  1702. while assigned(currpara) and (currpara.is_hidden) do
  1703. currpara:=tparaitem(currpara.previous);
  1704. pt:=tcallparanode(left);
  1705. lastpara:=paralength;
  1706. while assigned(currpara) and assigned(pt) do
  1707. begin
  1708. { only goto next para if we're out of the varargs }
  1709. if not(po_varargs in procdefinition.procoptions) or
  1710. (lastpara<=procdefinition.maxparacount) then
  1711. begin
  1712. repeat
  1713. currpara:=tparaitem(currpara.previous);
  1714. until (not assigned(currpara)) or (not currpara.is_hidden);
  1715. end;
  1716. pt:=tcallparanode(pt.right);
  1717. dec(lastpara);
  1718. end;
  1719. if assigned(pt) or assigned(currpara) then
  1720. begin
  1721. if assigned(pt) then
  1722. aktfilepos:=pt.fileinfo;
  1723. CGMessage(parser_e_wrong_parameter_size);
  1724. goto errorexit;
  1725. end;
  1726. end
  1727. else
  1728. { not a procedure variable }
  1729. begin
  1730. { do we know the procedure to call ? }
  1731. if not(assigned(procdefinition)) then
  1732. begin
  1733. procs:=candidates_find;
  1734. { no procedures found? then there is something wrong
  1735. with the parameter size }
  1736. if not assigned(procs) then
  1737. begin
  1738. { when it's an auto inherited call and there
  1739. is no procedure found, but the procedures
  1740. were defined with overload directive and at
  1741. least two procedures are defined then we ignore
  1742. this inherited by inserting a nothingn. Only
  1743. do this ugly hack in Delphi mode as it looks more
  1744. like a bug. It's also not documented }
  1745. if (m_delphi in aktmodeswitches) and
  1746. (nf_anon_inherited in flags) and
  1747. (symtableprocentry.owner.symtabletype=objectsymtable) and
  1748. (po_overload in symtableprocentry.first_procdef.procoptions) and
  1749. (symtableprocentry.procdef_count>=2) then
  1750. result:=cnothingnode.create
  1751. else
  1752. begin
  1753. { in tp mode we can try to convert to procvar if
  1754. there are no parameters specified. Only try it
  1755. when there is only one proc definition, else the
  1756. loadnode will give a strange error }
  1757. if not(assigned(left)) and
  1758. not(nf_inherited in flags) and
  1759. (m_tp_procvar in aktmodeswitches) and
  1760. (symtableprocentry.procdef_count=1) then
  1761. begin
  1762. hpt:=cloadnode.create(tprocsym(symtableprocentry),symtableproc);
  1763. if assigned(methodpointer) then
  1764. tloadnode(hpt).set_mp(methodpointer.getcopy);
  1765. resulttypepass(hpt);
  1766. result:=hpt;
  1767. end
  1768. else
  1769. begin
  1770. if assigned(left) then
  1771. aktfilepos:=left.fileinfo;
  1772. CGMessage(parser_e_wrong_parameter_size);
  1773. symtableprocentry.write_parameter_lists(nil);
  1774. end;
  1775. end;
  1776. goto errorexit;
  1777. end;
  1778. { Retrieve information about the candidates }
  1779. candidates_get_information(procs);
  1780. {$ifdef EXTDEBUG}
  1781. { Display info when multiple candidates are found }
  1782. if assigned(procs^.next) then
  1783. candidates_dump_info(V_Debug,procs);
  1784. {$endif EXTDEBUG}
  1785. { Choose the best candidate and count the number of
  1786. candidates left }
  1787. cand_cnt:=candidates_choose_best(procs,tprocdef(procdefinition));
  1788. { All parameters are checked, check if there are any
  1789. procedures left }
  1790. if cand_cnt>0 then
  1791. begin
  1792. { Multiple candidates left? }
  1793. if cand_cnt>1 then
  1794. begin
  1795. CGMessage(cg_e_cant_choose_overload_function);
  1796. {$ifdef EXTDEBUG}
  1797. candidates_dump_info(V_Hint,procs);
  1798. {$else}
  1799. candidates_list(procs,false);
  1800. {$endif EXTDEBUG}
  1801. { we'll just use the first candidate to make the
  1802. call }
  1803. end;
  1804. { assign procdefinition }
  1805. if symtableproc=nil then
  1806. symtableproc:=procdefinition.owner;
  1807. { update browser information }
  1808. if make_ref then
  1809. begin
  1810. tprocdef(procdefinition).lastref:=tref.create(tprocdef(procdefinition).lastref,@fileinfo);
  1811. inc(tprocdef(procdefinition).refcount);
  1812. if tprocdef(procdefinition).defref=nil then
  1813. tprocdef(procdefinition).defref:=tprocdef(procdefinition).lastref;
  1814. end;
  1815. end
  1816. else
  1817. begin
  1818. { No candidates left, this must be a type error,
  1819. because wrong size is already checked. procdefinition
  1820. is filled with the first (random) definition that is
  1821. found. We use this definition to display a nice error
  1822. message that the wrong type is passed }
  1823. candidates_find_wrong_para(procs);
  1824. candidates_list(procs,true);
  1825. {$ifdef EXTDEBUG}
  1826. candidates_dump_info(V_Hint,procs);
  1827. {$endif EXTDEBUG}
  1828. { We can not proceed, release all procs and exit }
  1829. candidates_free(procs);
  1830. goto errorexit;
  1831. end;
  1832. candidates_free(procs);
  1833. end; { end of procedure to call determination }
  1834. { add needed default parameters }
  1835. if assigned(procdefinition) and
  1836. (paralength<procdefinition.maxparacount) then
  1837. begin
  1838. currpara:=tparaitem(procdefinition.Para.first);
  1839. for i:=1 to paralength do
  1840. currpara:=tparaitem(currpara.next);
  1841. while assigned(currpara) do
  1842. begin
  1843. if not assigned(currpara.defaultvalue) then
  1844. internalerror(200212142);
  1845. left:=ccallparanode.create(genconstsymtree(tconstsym(currpara.defaultvalue)),left);
  1846. currpara:=tparaitem(currpara.next);
  1847. end;
  1848. end;
  1849. end;
  1850. { handle predefined procedures }
  1851. is_const:=(po_internconst in procdefinition.procoptions) and
  1852. ((block_type in [bt_const,bt_type]) or
  1853. (assigned(left) and (tcallparanode(left).left.nodetype in [realconstn,ordconstn])));
  1854. if (procdefinition.proccalloption=pocall_internproc) or is_const then
  1855. begin
  1856. if assigned(left) then
  1857. begin
  1858. { ptr and settextbuf needs two args }
  1859. if assigned(tcallparanode(left).right) then
  1860. begin
  1861. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,left);
  1862. left:=nil;
  1863. end
  1864. else
  1865. begin
  1866. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,tcallparanode(left).left);
  1867. tcallparanode(left).left:=nil;
  1868. end;
  1869. end
  1870. else
  1871. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,nil);
  1872. result:=hpt;
  1873. goto errorexit;
  1874. end;
  1875. { ensure that the result type is set }
  1876. if not restypeset then
  1877. begin
  1878. { constructors return their current class type, not the type where the
  1879. constructor is declared, this can be different because of inheritance }
  1880. if (procdefinition.proctypeoption=potype_constructor) and
  1881. assigned(methodpointer) and
  1882. assigned(methodpointer.resulttype.def) and
  1883. (methodpointer.resulttype.def.deftype=classrefdef) then
  1884. resulttype:=tclassrefdef(methodpointer.resulttype.def).pointertype
  1885. else
  1886. resulttype:=procdefinition.rettype;
  1887. end
  1888. else
  1889. resulttype:=restype;
  1890. if resulttype.def.needs_inittable then
  1891. include(current_procinfo.flags,pi_needs_implicit_finally);
  1892. if assigned(methodpointer) then
  1893. begin
  1894. resulttypepass(methodpointer);
  1895. { direct call to inherited abstract method, then we
  1896. can already give a error in the compiler instead
  1897. of a runtime error }
  1898. if (nf_inherited in flags) and
  1899. (po_abstractmethod in procdefinition.procoptions) then
  1900. CGMessage(cg_e_cant_call_abstract_method);
  1901. { if an inherited con- or destructor should be }
  1902. { called in a con- or destructor then a warning }
  1903. { will be made }
  1904. { con- and destructors need a pointer to the vmt }
  1905. if (nf_inherited in flags) and
  1906. (procdefinition.proctypeoption in [potype_constructor,potype_destructor]) and
  1907. is_object(methodpointer.resulttype.def) and
  1908. not(current_procdef.proctypeoption in [potype_constructor,potype_destructor]) then
  1909. CGMessage(cg_w_member_cd_call_from_method);
  1910. if methodpointer.nodetype<>typen then
  1911. begin
  1912. hpt:=methodpointer;
  1913. while assigned(hpt) and (hpt.nodetype in [subscriptn,vecn]) do
  1914. hpt:=tunarynode(hpt).left;
  1915. if (procdefinition.proctypeoption in [potype_constructor,potype_destructor]) and
  1916. assigned(symtableproc) and
  1917. (symtableproc.symtabletype=withsymtable) and
  1918. (tnode(twithsymtable(symtableproc).withrefnode).nodetype=temprefn) then
  1919. CGmessage(cg_e_cannot_call_cons_dest_inside_with);
  1920. { R.Init then R will be initialized by the constructor,
  1921. Also allow it for simple loads }
  1922. if (procdefinition.proctypeoption=potype_constructor) or
  1923. ((hpt.nodetype=loadn) and
  1924. (
  1925. (methodpointer.resulttype.def.deftype=classrefdef) or
  1926. (
  1927. (methodpointer.resulttype.def.deftype=objectdef) and
  1928. not(oo_has_virtual in tobjectdef(methodpointer.resulttype.def).objectoptions)
  1929. )
  1930. )
  1931. ) then
  1932. method_must_be_valid:=false
  1933. else
  1934. method_must_be_valid:=true;
  1935. set_varstate(methodpointer,method_must_be_valid);
  1936. { The object is already used if it is called once }
  1937. if (hpt.nodetype=loadn) and
  1938. (tloadnode(hpt).symtableentry.typ=varsym) then
  1939. tvarsym(tloadnode(hpt).symtableentry).varstate:=vs_used;
  1940. end;
  1941. end
  1942. else
  1943. begin
  1944. { When this is method the methodpointer must be available }
  1945. if (right=nil) and
  1946. (procdefinition.owner.symtabletype=objectsymtable) then
  1947. internalerror(200305061);
  1948. end;
  1949. { bind paraitems to the callparanodes and insert hidden parameters }
  1950. aktcallprocdef:=procdefinition;
  1951. bind_paraitem;
  1952. { methodpointer is only needed for virtual calls, and
  1953. it should then be loaded with the VMT }
  1954. if (po_virtualmethod in procdefinition.procoptions) and
  1955. not(assigned(methodpointer) and
  1956. (methodpointer.nodetype=typen)) then
  1957. begin
  1958. if not assigned(methodpointer) then
  1959. internalerror(200305063);
  1960. if (methodpointer.resulttype.def.deftype<>classrefdef) then
  1961. begin
  1962. methodpointer:=cloadvmtaddrnode.create(methodpointer);
  1963. resulttypepass(methodpointer);
  1964. end;
  1965. end
  1966. else
  1967. begin
  1968. { not needed anymore }
  1969. methodpointer.free;
  1970. methodpointer:=nil;
  1971. end;
  1972. { insert type conversions for parameters }
  1973. if assigned(left) then
  1974. tcallparanode(left).insert_typeconv(true);
  1975. errorexit:
  1976. aktcallprocdef:=oldcallprocdef;
  1977. end;
  1978. {$ifdef callparatemp}
  1979. function tree_contains_function_call(var n: tnode): foreachnoderesult;
  1980. begin
  1981. result := fen_false;
  1982. if n.nodetype = calln then
  1983. { stop when we encounter a call node }
  1984. result := fen_norecurse_true;
  1985. end;
  1986. function tcallnode.extract_functioncall_paras: tblocknode;
  1987. var
  1988. curpara: tcallparanode;
  1989. newblock: tblocknode;
  1990. newstatement: tstatementnode;
  1991. temp: ttempcreatenode;
  1992. curparaitem: tparaitem;
  1993. orgtype: ^ttype;
  1994. temptype: ttemptype;
  1995. foundcall: boolean;
  1996. take_addr: boolean;
  1997. begin
  1998. foundcall := false;
  1999. curpara := tcallparanode(left);
  2000. curparaitem:=tparaitem(procdefinition.Para.last);
  2001. if assigned(curpara) then
  2002. begin
  2003. curpara := tcallparanode(curpara.right);
  2004. curparaitem:=tparaitem(curparaitem.previous);
  2005. end;
  2006. newblock := nil;
  2007. while assigned(curpara) do
  2008. begin
  2009. if foreachnodestatic(curpara.left,@tree_contains_function_call) then
  2010. begin
  2011. if (not foundcall) then
  2012. begin
  2013. foundcall := true;
  2014. newblock := internalstatements(newstatement,false);
  2015. end;
  2016. take_addr := (curparaitem.paratyp in [vs_var,vs_out]) or
  2017. ((curparaitem.paratype.def.deftype = formaldef));
  2018. if not(take_addr) then
  2019. begin
  2020. if is_ansistring(curpara.left.resulttype.def) then
  2021. temptype := tt_ansistring
  2022. else if is_widestring(curpara.left.resulttype.def) then
  2023. temptype := tt_widestring
  2024. else if is_interfacecom(curpara.left.resulttype.def) then
  2025. temptype := tt_interfacecom
  2026. else
  2027. temptype := tt_persistent;
  2028. temp := ctempcreatenode.create(curpara.left.resulttype,curpara.left.resulttype.def.size,temptype)
  2029. end
  2030. else
  2031. begin
  2032. temptype := tt_persistent;
  2033. temp := ctempcreatenode.create(voidpointertype,pointer_size,tt_persistent);
  2034. orgtype := @curpara.left.resulttype;
  2035. end;
  2036. addstatement(newstatement,temp);
  2037. if take_addr then
  2038. curpara.left := caddrnode.create(curpara.left);
  2039. addstatement(newstatement,
  2040. cassignmentnode.create(ctemprefnode.create(temp),curpara.left));
  2041. { after the assignment, turn the temp into a non-persistent one, so }
  2042. { that it will be freed once it's used as parameter }
  2043. if (temptype = tt_persistent) then
  2044. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2045. curpara.left := ctemprefnode.create(temp);
  2046. if take_addr then
  2047. curpara.left := ctypeconvnode.create_explicit(cderefnode.create(curpara.left),orgtype^);
  2048. end;
  2049. curpara := tcallparanode(curpara.right);
  2050. curparaitem := tparaitem(curparaitem.previous);
  2051. end;
  2052. if assigned(newblock) then
  2053. firstpass(newblock);
  2054. result := newblock;
  2055. end;
  2056. {$endif callparatemp}
  2057. function tcallnode.pass_1 : tnode;
  2058. var
  2059. inlinecode : tnode;
  2060. inlined : boolean;
  2061. {$ifdef m68k}
  2062. regi : tregister;
  2063. {$endif}
  2064. {$ifdef callparatemp}
  2065. callparatemps, newblock: tblocknode;
  2066. statement: tstatementnode;
  2067. paras, oldright, newcall: tnode;
  2068. {$endif callparatemp}
  2069. label
  2070. errorexit;
  2071. begin
  2072. result:=nil;
  2073. inlined:=false;
  2074. inlinecode := nil;
  2075. { work trough all parameters to get the register requirements }
  2076. if assigned(left) then
  2077. tcallparanode(left).det_registers;
  2078. {$ifdef callparatemp}
  2079. callparatemps := extract_functioncall_paras;
  2080. {$endif callparatemp}
  2081. { function result node }
  2082. if assigned(funcretnode) then
  2083. firstpass(funcretnode);
  2084. if assigned(procdefinition) and
  2085. (procdefinition.proccalloption=pocall_inline) then
  2086. begin
  2087. inlinecode:=right;
  2088. if assigned(inlinecode) then
  2089. inlined:=true;
  2090. right:=nil;
  2091. end;
  2092. { procedure variable ? }
  2093. if assigned(right) then
  2094. begin
  2095. firstpass(right);
  2096. { procedure does a call }
  2097. if not (block_type in [bt_const,bt_type]) then
  2098. include(current_procinfo.flags,pi_do_call);
  2099. rg.incrementintregisterpushed(all_intregisters);
  2100. rg.incrementotherregisterpushed(all_registers);
  2101. end
  2102. else
  2103. { not a procedure variable }
  2104. begin
  2105. { calc the correture value for the register }
  2106. { handle predefined procedures }
  2107. if (procdefinition.proccalloption=pocall_inline) then
  2108. begin
  2109. if assigned(methodpointer) then
  2110. CGMessage(cg_e_unable_inline_object_methods);
  2111. if assigned(right) and (right.nodetype<>procinlinen) then
  2112. CGMessage(cg_e_unable_inline_procvar);
  2113. if not assigned(inlinecode) then
  2114. begin
  2115. if assigned(tprocdef(procdefinition).code) then
  2116. inlinecode:=cprocinlinenode.create(tprocdef(procdefinition))
  2117. else
  2118. CGMessage(cg_e_no_code_for_inline_stored);
  2119. if assigned(inlinecode) then
  2120. begin
  2121. { consider it has not inlined if called
  2122. again inside the args }
  2123. procdefinition.proccalloption:=pocall_fpccall;
  2124. firstpass(inlinecode);
  2125. inlined:=true;
  2126. end;
  2127. end;
  2128. end
  2129. else
  2130. begin
  2131. if not (block_type in [bt_const,bt_type]) then
  2132. include(current_procinfo.flags,pi_do_call);
  2133. end;
  2134. { It doesn't hurt to calculate it already though :) (JM) }
  2135. rg.incrementintregisterpushed(tprocdef(procdefinition).usedintregisters);
  2136. rg.incrementotherregisterpushed(tprocdef(procdefinition).usedotherregisters);
  2137. end;
  2138. { get a register for the return value }
  2139. if (not is_void(resulttype.def)) then
  2140. begin
  2141. if paramanager.ret_in_param(resulttype.def,procdefinition.proccalloption) then
  2142. begin
  2143. expectloc:=LOC_REFERENCE;
  2144. end
  2145. else
  2146. { for win32 records returned in EDX:EAX, we
  2147. move them to memory after ... }
  2148. if (resulttype.def.deftype=recorddef) then
  2149. begin
  2150. expectloc:=LOC_CREFERENCE;
  2151. end
  2152. else
  2153. { ansi/widestrings must be registered, so we can dispose them }
  2154. if is_ansistring(resulttype.def) or
  2155. is_widestring(resulttype.def) then
  2156. begin
  2157. expectloc:=LOC_CREFERENCE;
  2158. registers32:=1;
  2159. end
  2160. else
  2161. { we have only to handle the result if it is used }
  2162. if (nf_return_value_used in flags) then
  2163. begin
  2164. case resulttype.def.deftype of
  2165. enumdef,
  2166. orddef :
  2167. begin
  2168. if (procdefinition.proctypeoption=potype_constructor) then
  2169. begin
  2170. expectloc:=LOC_REGISTER;
  2171. registers32:=1;
  2172. end
  2173. else
  2174. begin
  2175. expectloc:=LOC_REGISTER;
  2176. if is_64bit(resulttype.def) then
  2177. registers32:=2
  2178. else
  2179. registers32:=1;
  2180. end;
  2181. end;
  2182. floatdef :
  2183. begin
  2184. expectloc:=LOC_FPUREGISTER;
  2185. {$ifdef cpufpemu}
  2186. if (cs_fp_emulation in aktmoduleswitches) then
  2187. registers32:=1
  2188. else
  2189. {$endif cpufpemu}
  2190. {$ifdef m68k}
  2191. if (tfloatdef(resulttype.def).typ=s32real) then
  2192. registers32:=1
  2193. else
  2194. {$endif m68k}
  2195. registersfpu:=1;
  2196. end;
  2197. else
  2198. begin
  2199. expectloc:=LOC_REGISTER;
  2200. registers32:=1;
  2201. end;
  2202. end;
  2203. end
  2204. else
  2205. expectloc:=LOC_VOID;
  2206. end
  2207. else
  2208. expectloc:=LOC_VOID;
  2209. {$ifdef m68k}
  2210. { we need one more address register for virtual calls on m68k }
  2211. if (po_virtualmethod in procdefinition.procoptions) then
  2212. inc(registers32);
  2213. {$endif m68k}
  2214. { a fpu can be used in any procedure !! }
  2215. {$ifdef i386}
  2216. registersfpu:=procdefinition.fpu_used;
  2217. {$endif i386}
  2218. { if this is a call to a method calc the registers }
  2219. if (methodpointer<>nil) then
  2220. begin
  2221. if methodpointer.nodetype<>typen then
  2222. begin
  2223. firstpass(methodpointer);
  2224. registersfpu:=max(methodpointer.registersfpu,registersfpu);
  2225. registers32:=max(methodpointer.registers32,registers32);
  2226. {$ifdef SUPPORT_MMX }
  2227. registersmmx:=max(methodpointer.registersmmx,registersmmx);
  2228. {$endif SUPPORT_MMX}
  2229. end;
  2230. { if we are calling the constructor }
  2231. if procdefinition.proctypeoption=potype_constructor then
  2232. verifyabstractcalls;
  2233. end;
  2234. if inlined then
  2235. right:=inlinecode;
  2236. { determine the registers of the procedure variable }
  2237. { is this OK for inlined procs also ?? (PM) }
  2238. if assigned(right) then
  2239. begin
  2240. registersfpu:=max(right.registersfpu,registersfpu);
  2241. registers32:=max(right.registers32,registers32);
  2242. {$ifdef SUPPORT_MMX}
  2243. registersmmx:=max(right.registersmmx,registersmmx);
  2244. {$endif SUPPORT_MMX}
  2245. end;
  2246. { determine the registers of the procedure }
  2247. if assigned(left) then
  2248. begin
  2249. registersfpu:=max(left.registersfpu,registersfpu);
  2250. registers32:=max(left.registers32,registers32);
  2251. {$ifdef SUPPORT_MMX}
  2252. registersmmx:=max(left.registersmmx,registersmmx);
  2253. {$endif SUPPORT_MMX}
  2254. end;
  2255. {$ifdef callparatemp}
  2256. if (callparatemps <> nil) then
  2257. begin
  2258. { we have to replace the callnode with a blocknode. firstpass will }
  2259. { free the original call node. Avoid copying all subnodes though }
  2260. paras := left;
  2261. oldright := right;
  2262. left := nil;
  2263. right := nil;
  2264. newcall := self.getcopy;
  2265. tcallnode(newcall).left := paras;
  2266. tcallnode(newcall).right := oldright;
  2267. newblock := internalstatements(statement,false);
  2268. addstatement(statement,callparatemps);
  2269. { add the copy of the call node after the callparatemps block }
  2270. { and return that. The last statement of a bocknode determines }
  2271. { the resulttype & location of the block -> ok. Working with a }
  2272. { new block is easier than going to the end of the callparatemps }
  2273. { block (JM) }
  2274. addstatement(statement,newcall);
  2275. result := newblock;
  2276. { set to nil so we can free this one in case of an errorexit }
  2277. callparatemps := nil;
  2278. end;
  2279. {$endif callparatemp}
  2280. errorexit:
  2281. if inlined then
  2282. procdefinition.proccalloption:=pocall_inline;
  2283. {$ifdef callparatemp}
  2284. if assigned(callparatemps) then
  2285. callparatemps.free;
  2286. {$endif callparatemp}
  2287. end;
  2288. {$ifdef state_tracking}
  2289. function Tcallnode.track_state_pass(exec_known:boolean):boolean;
  2290. var hp:Tcallparanode;
  2291. value:Tnode;
  2292. begin
  2293. track_state_pass:=false;
  2294. hp:=Tcallparanode(left);
  2295. while assigned(hp) do
  2296. begin
  2297. if left.track_state_pass(exec_known) then
  2298. begin
  2299. left.resulttype.def:=nil;
  2300. do_resulttypepass(left);
  2301. end;
  2302. value:=aktstate.find_fact(hp.left);
  2303. if value<>nil then
  2304. begin
  2305. track_state_pass:=true;
  2306. hp.left.destroy;
  2307. hp.left:=value.getcopy;
  2308. do_resulttypepass(hp.left);
  2309. end;
  2310. hp:=Tcallparanode(hp.right);
  2311. end;
  2312. end;
  2313. {$endif}
  2314. function tcallnode.docompare(p: tnode): boolean;
  2315. begin
  2316. docompare :=
  2317. inherited docompare(p) and
  2318. (symtableprocentry = tcallnode(p).symtableprocentry) and
  2319. (procdefinition = tcallnode(p).procdefinition) and
  2320. (methodpointer.isequal(tcallnode(p).methodpointer)) and
  2321. ((restypeset and tcallnode(p).restypeset and
  2322. (equal_defs(restype.def,tcallnode(p).restype.def))) or
  2323. (not restypeset and not tcallnode(p).restypeset));
  2324. end;
  2325. procedure tcallnode.printnodedata(var t:text);
  2326. begin
  2327. if assigned(procdefinition) and
  2328. (procdefinition.deftype=procdef) then
  2329. writeln(t,printnodeindention,'proc = ',tprocdef(procdefinition).fullprocname(true))
  2330. else
  2331. begin
  2332. if assigned(symtableprocentry) then
  2333. writeln(t,printnodeindention,'proc = ',symtableprocentry.name)
  2334. else
  2335. writeln(t,printnodeindention,'proc = <nil>');
  2336. end;
  2337. printnode(t,methodpointer);
  2338. printnode(t,right);
  2339. printnode(t,left);
  2340. end;
  2341. {****************************************************************************
  2342. TPROCINLINENODE
  2343. ****************************************************************************}
  2344. constructor tprocinlinenode.create(p:tprocdef);
  2345. begin
  2346. inherited create(procinlinen);
  2347. inlineprocdef:=p;
  2348. retoffset:=-POINTER_SIZE; { less dangerous as zero (PM) }
  2349. para_offset:=0;
  2350. para_size:=0;
  2351. { copy inlinetree }
  2352. if assigned(p.code) then
  2353. inlinetree:=p.code.getcopy
  2354. else
  2355. inlinetree:=nil;
  2356. end;
  2357. destructor tprocinlinenode.destroy;
  2358. begin
  2359. if assigned(inlinetree) then
  2360. inlinetree.free;
  2361. inherited destroy;
  2362. end;
  2363. constructor tprocinlinenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  2364. begin
  2365. inherited ppuload(t,ppufile);
  2366. inlineprocdef:=tprocdef(ppufile.getderef);
  2367. inlinetree:=ppuloadnode(ppufile);
  2368. retoffset:=-POINTER_SIZE; { less dangerous as zero (PM) }
  2369. para_offset:=0;
  2370. para_size:=0;
  2371. end;
  2372. procedure tprocinlinenode.ppuwrite(ppufile:tcompilerppufile);
  2373. begin
  2374. inherited ppuwrite(ppufile);
  2375. ppufile.putderef(inlineprocdef);
  2376. ppuwritenode(ppufile,inlinetree);
  2377. end;
  2378. procedure tprocinlinenode.derefimpl;
  2379. begin
  2380. inherited derefimpl;
  2381. if assigned(inlinetree) then
  2382. inlinetree.derefimpl;
  2383. resolvedef(pointer(inlineprocdef));
  2384. end;
  2385. function tprocinlinenode.getcopy : tnode;
  2386. var
  2387. n : tprocinlinenode;
  2388. begin
  2389. n:=tprocinlinenode(inherited getcopy);
  2390. n.inlineprocdef:=inlineprocdef;
  2391. if assigned(inlinetree) then
  2392. n.inlinetree:=inlinetree.getcopy
  2393. else
  2394. n.inlinetree:=nil;
  2395. n.retoffset:=retoffset;
  2396. n.para_offset:=para_offset;
  2397. n.para_size:=para_size;
  2398. getcopy:=n;
  2399. end;
  2400. procedure tprocinlinenode.insertintolist(l : tnodelist);
  2401. begin
  2402. end;
  2403. function tprocinlinenode.det_resulttype : tnode;
  2404. var
  2405. storesymtablelevel : longint;
  2406. storeparasymtable,
  2407. storelocalsymtable : tsymtabletype;
  2408. oldprocdef : tprocdef;
  2409. old_current_procinfo : tprocinfo;
  2410. oldinlining_procedure : boolean;
  2411. begin
  2412. result:=nil;
  2413. oldinlining_procedure:=inlining_procedure;
  2414. oldprocdef:=current_procdef;
  2415. old_current_procinfo:=current_procinfo;
  2416. { we're inlining a procedure }
  2417. inlining_procedure:=true;
  2418. current_procdef:=inlineprocdef;
  2419. { clone current_procinfo, but not the asmlists }
  2420. current_procinfo:=tprocinfo(cprocinfo.newinstance);
  2421. move(pointer(old_current_procinfo)^,pointer(current_procinfo)^,cprocinfo.InstanceSize);
  2422. current_procinfo.aktentrycode:=nil;
  2423. current_procinfo.aktexitcode:=nil;
  2424. current_procinfo.aktproccode:=nil;
  2425. current_procinfo.aktlocaldata:=nil;
  2426. { set new current_procinfo }
  2427. current_procinfo.return_offset:=retoffset;
  2428. { set it to the same lexical level }
  2429. storesymtablelevel:=current_procdef.localst.symtablelevel;
  2430. storelocalsymtable:=current_procdef.localst.symtabletype;
  2431. storeparasymtable:=current_procdef.parast.symtabletype;
  2432. current_procdef.localst.symtablelevel:=oldprocdef.localst.symtablelevel;
  2433. current_procdef.localst.symtabletype:=inlinelocalsymtable;
  2434. current_procdef.parast.symtabletype:=inlineparasymtable;
  2435. { pass inlinetree }
  2436. resulttypepass(inlinetree);
  2437. resulttype:=inlineprocdef.rettype;
  2438. { retrieve info from inlineprocdef }
  2439. retoffset:=-POINTER_SIZE; { less dangerous as zero (PM) }
  2440. para_offset:=0;
  2441. para_size:=inlineprocdef.para_size(target_info.alignment.paraalign);
  2442. if paramanager.ret_in_param(inlineprocdef.rettype.def,inlineprocdef.proccalloption) then
  2443. inc(para_size,POINTER_SIZE);
  2444. { restore current_procinfo }
  2445. current_procinfo.free;
  2446. current_procinfo:=old_current_procinfo;
  2447. { restore symtable }
  2448. current_procdef.localst.symtablelevel:=storesymtablelevel;
  2449. current_procdef.localst.symtabletype:=storelocalsymtable;
  2450. current_procdef.parast.symtabletype:=storeparasymtable;
  2451. { restore }
  2452. current_procdef:=oldprocdef;
  2453. inlining_procedure:=oldinlining_procedure;
  2454. end;
  2455. function tprocinlinenode.pass_1 : tnode;
  2456. begin
  2457. firstpass(inlinetree);
  2458. registers32:=inlinetree.registers32;
  2459. registersfpu:=inlinetree.registersfpu;
  2460. {$ifdef SUPPORT_MMX}
  2461. registersmmx:=inlinetree.registersmmx;
  2462. {$endif SUPPORT_MMX}
  2463. result:=nil;
  2464. end;
  2465. function tprocinlinenode.docompare(p: tnode): boolean;
  2466. begin
  2467. docompare :=
  2468. inherited docompare(p) and
  2469. inlinetree.isequal(tprocinlinenode(p).inlinetree) and
  2470. (inlineprocdef = tprocinlinenode(p).inlineprocdef);
  2471. end;
  2472. begin
  2473. ccallnode:=tcallnode;
  2474. ccallparanode:=tcallparanode;
  2475. cprocinlinenode:=tprocinlinenode;
  2476. end.
  2477. {
  2478. $Log$
  2479. Revision 1.158 2003-05-23 14:27:35 peter
  2480. * remove some unit dependencies
  2481. * current_procinfo changes to store more info
  2482. Revision 1.157 2003/05/17 14:05:58 jonas
  2483. * fixed callparatemp for ansi/widestring and interfacecoms
  2484. Revision 1.156 2003/05/17 13:30:08 jonas
  2485. * changed tt_persistant to tt_persistent :)
  2486. * tempcreatenode now doesn't accept a boolean anymore for persistent
  2487. temps, but a ttemptype, so you can also create ansistring temps etc
  2488. Revision 1.155 2003/05/16 14:33:31 peter
  2489. * regvar fixes
  2490. Revision 1.154 2003/05/14 19:35:50 jonas
  2491. * fixed callparatemp so it works with vs_var, vs_out and formal const
  2492. parameters
  2493. Revision 1.153 2003/05/13 20:53:41 peter
  2494. * constructors return in register
  2495. Revision 1.152 2003/05/13 15:18:49 peter
  2496. * fixed various crashes
  2497. Revision 1.151 2003/05/11 21:37:03 peter
  2498. * moved implicit exception frame from ncgutil to psub
  2499. * constructor/destructor helpers moved from cobj/ncgutil to psub
  2500. Revision 1.150 2003/05/11 14:45:12 peter
  2501. * tloadnode does not support objectsymtable,withsymtable anymore
  2502. * withnode cleanup
  2503. * direct with rewritten to use temprefnode
  2504. Revision 1.149 2003/05/09 17:47:02 peter
  2505. * self moved to hidden parameter
  2506. * removed hdisposen,hnewn,selfn
  2507. Revision 1.148 2003/05/05 14:53:16 peter
  2508. * vs_hidden replaced by is_hidden boolean
  2509. Revision 1.147 2003/04/27 11:21:33 peter
  2510. * aktprocdef renamed to current_procdef
  2511. * procinfo renamed to current_procinfo
  2512. * procinfo will now be stored in current_module so it can be
  2513. cleaned up properly
  2514. * gen_main_procsym changed to create_main_proc and release_main_proc
  2515. to also generate a tprocinfo structure
  2516. * fixed unit implicit initfinal
  2517. Revision 1.146 2003/04/27 09:08:44 jonas
  2518. * do callparatemp stuff only after the parameters have been firstpassed,
  2519. because some nodes are turned into calls during the firstpass
  2520. Revision 1.145 2003/04/27 07:29:50 peter
  2521. * current_procdef cleanup, current_procdef is now always nil when parsing
  2522. a new procdef declaration
  2523. * aktprocsym removed
  2524. * lexlevel removed, use symtable.symtablelevel instead
  2525. * implicit init/final code uses the normal genentry/genexit
  2526. * funcret state checking updated for new funcret handling
  2527. Revision 1.144 2003/04/25 20:59:33 peter
  2528. * removed funcretn,funcretsym, function result is now in varsym
  2529. and aliases for result and function name are added using absolutesym
  2530. * vs_hidden parameter for funcret passed in parameter
  2531. * vs_hidden fixes
  2532. * writenode changed to printnode and released from extdebug
  2533. * -vp option added to generate a tree.log with the nodetree
  2534. * nicer printnode for statements, callnode
  2535. Revision 1.143 2002/04/25 20:15:39 florian
  2536. * block nodes within expressions shouldn't release the used registers,
  2537. fixed using a flag till the new rg is ready
  2538. Revision 1.142 2003/04/23 20:16:04 peter
  2539. + added currency support based on int64
  2540. + is_64bit for use in cg units instead of is_64bitint
  2541. * removed cgmessage from n386add, replace with internalerrors
  2542. Revision 1.141 2003/04/23 13:21:06 peter
  2543. * fix warning for calling constructor inside constructor
  2544. Revision 1.140 2003/04/23 12:35:34 florian
  2545. * fixed several issues with powerpc
  2546. + applied a patch from Jonas for nested function calls (PowerPC only)
  2547. * ...
  2548. Revision 1.139 2003/04/22 23:50:22 peter
  2549. * firstpass uses expectloc
  2550. * checks if there are differences between the expectloc and
  2551. location.loc from secondpass in EXTDEBUG
  2552. Revision 1.138 2003/04/22 09:53:33 peter
  2553. * fix insert_typeconv to handle new varargs which don't have a
  2554. paraitem set
  2555. Revision 1.137 2003/04/11 16:02:05 peter
  2556. * don't firstpass typen
  2557. Revision 1.136 2003/04/11 15:51:04 peter
  2558. * support subscript,vec for setting methodpointer varstate
  2559. Revision 1.135 2003/04/10 17:57:52 peter
  2560. * vs_hidden released
  2561. Revision 1.134 2003/04/07 11:58:22 jonas
  2562. * more vs_invisible fixes
  2563. Revision 1.133 2003/04/07 10:40:21 jonas
  2564. * fixed VS_HIDDEN for high parameter so it works again
  2565. Revision 1.132 2003/04/04 15:38:56 peter
  2566. * moved generic code from n386cal to ncgcal, i386 now also
  2567. uses the generic ncgcal
  2568. Revision 1.131 2003/03/17 18:54:23 peter
  2569. * fix missing self setting for method to procvar conversion in
  2570. tp_procvar mode
  2571. Revision 1.130 2003/03/17 16:54:41 peter
  2572. * support DefaultHandler and anonymous inheritance fixed
  2573. for message methods
  2574. Revision 1.129 2003/03/17 15:54:22 peter
  2575. * store symoptions also for procdef
  2576. * check symoptions (private,public) when calculating possible
  2577. overload candidates
  2578. Revision 1.128 2003/02/19 22:00:14 daniel
  2579. * Code generator converted to new register notation
  2580. - Horribily outdated todo.txt removed
  2581. Revision 1.127 2003/01/16 22:13:52 peter
  2582. * convert_l3 convertlevel added. This level is used for conversions
  2583. where information can be lost like converting widestring->ansistring
  2584. or dword->byte
  2585. Revision 1.126 2003/01/15 01:44:32 peter
  2586. * merged methodpointer fixes from 1.0.x
  2587. Revision 1.125 2003/01/12 17:52:07 peter
  2588. * only check for auto inherited in objectsymtable
  2589. Revision 1.124 2003/01/09 21:45:46 peter
  2590. * extended information about overloaded candidates when compiled
  2591. with EXTDEBUG
  2592. Revision 1.123 2002/12/26 18:24:33 jonas
  2593. * fixed check for whether or not a high parameter was already generated
  2594. * no type checking/conversions for invisible parameters
  2595. Revision 1.122 2002/12/15 22:50:00 florian
  2596. + some stuff for the new hidden parameter handling added
  2597. Revision 1.121 2002/12/15 21:34:15 peter
  2598. * give sign difference between ordinals a small penalty. This is
  2599. needed to get word->[longword|longint] working
  2600. Revision 1.120 2002/12/15 21:30:12 florian
  2601. * tcallnode.paraitem introduced, all references to defcoll removed
  2602. Revision 1.119 2002/12/15 20:59:58 peter
  2603. * fix crash with default parameters
  2604. Revision 1.118 2002/12/15 11:26:02 peter
  2605. * ignore vs_hidden parameters when choosing overloaded proc
  2606. Revision 1.117 2002/12/11 22:42:28 peter
  2607. * tcallnode.det_resulttype rewrite, merged code from nice_ncal and
  2608. the old code. The new code collects the information about possible
  2609. candidates only once resultting in much less calls to type compare
  2610. routines
  2611. Revision 1.116 2002/12/07 14:27:07 carl
  2612. * 3% memory optimization
  2613. * changed some types
  2614. + added type checking with different size for call node and for
  2615. parameters
  2616. Revision 1.115 2002/12/06 17:51:10 peter
  2617. * merged cdecl and array fixes
  2618. Revision 1.114 2002/12/06 16:56:58 peter
  2619. * only compile cs_fp_emulation support when cpufpuemu is defined
  2620. * define cpufpuemu for m68k only
  2621. Revision 1.113 2002/11/27 20:04:38 peter
  2622. * cdecl array of const fixes
  2623. Revision 1.112 2002/11/27 15:33:46 peter
  2624. * the never ending story of tp procvar hacks
  2625. Revision 1.111 2002/11/27 02:31:17 peter
  2626. * fixed inlinetree parsing in det_resulttype
  2627. Revision 1.110 2002/11/25 18:43:32 carl
  2628. - removed the invalid if <> checking (Delphi is strange on this)
  2629. + implemented abstract warning on instance creation of class with
  2630. abstract methods.
  2631. * some error message cleanups
  2632. Revision 1.109 2002/11/25 17:43:17 peter
  2633. * splitted defbase in defutil,symutil,defcmp
  2634. * merged isconvertable and is_equal into compare_defs(_ext)
  2635. * made operator search faster by walking the list only once
  2636. Revision 1.108 2002/11/18 17:31:54 peter
  2637. * pass proccalloption to ret_in_xxx and push_xxx functions
  2638. Revision 1.107 2002/11/15 01:58:50 peter
  2639. * merged changes from 1.0.7 up to 04-11
  2640. - -V option for generating bug report tracing
  2641. - more tracing for option parsing
  2642. - errors for cdecl and high()
  2643. - win32 import stabs
  2644. - win32 records<=8 are returned in eax:edx (turned off by default)
  2645. - heaptrc update
  2646. - more info for temp management in .s file with EXTDEBUG
  2647. Revision 1.106 2002/10/14 18:20:30 carl
  2648. * var parameter checking for classes and interfaces in Delphi mode
  2649. Revision 1.105 2002/10/06 21:02:17 peter
  2650. * fixed limit checking for qword
  2651. Revision 1.104 2002/10/05 15:15:45 peter
  2652. * Write unknwon compiler proc using Comment and only in Extdebug
  2653. Revision 1.103 2002/10/05 12:43:25 carl
  2654. * fixes for Delphi 6 compilation
  2655. (warning : Some features do not work under Delphi)
  2656. Revision 1.102 2002/10/05 00:48:57 peter
  2657. * support inherited; support for overload as it is handled by
  2658. delphi. This is only for delphi mode as it is working is
  2659. undocumented and hard to predict what is done
  2660. Revision 1.101 2002/09/16 14:11:12 peter
  2661. * add argument to equal_paras() to support default values or not
  2662. Revision 1.100 2002/09/15 17:49:59 peter
  2663. * don't have strict var parameter checking for procedures in the
  2664. system unit
  2665. Revision 1.99 2002/09/09 19:30:34 peter
  2666. * don't allow convertable parameters for var and out parameters in
  2667. delphi and tp mode
  2668. Revision 1.98 2002/09/07 15:25:02 peter
  2669. * old logs removed and tabs fixed
  2670. Revision 1.97 2002/09/07 12:16:05 carl
  2671. * second part bug report 1996 fix, testrange in cordconstnode
  2672. only called if option is set (also make parsing a tiny faster)
  2673. Revision 1.96 2002/09/05 14:53:41 peter
  2674. * fixed old callnode.det_resulttype code
  2675. * old ncal code is default again
  2676. Revision 1.95 2002/09/03 21:32:49 daniel
  2677. * Small bugfix for procdef selection
  2678. Revision 1.94 2002/09/03 19:27:22 daniel
  2679. * Activated new ncal code
  2680. Revision 1.93 2002/09/03 16:26:26 daniel
  2681. * Make Tprocdef.defs protected
  2682. Revision 1.92 2002/09/01 13:28:37 daniel
  2683. - write_access fields removed in favor of a flag
  2684. Revision 1.91 2002/09/01 12:14:15 peter
  2685. * remove debug line
  2686. * containself methods can be called directly
  2687. Revision 1.90 2002/09/01 08:01:16 daniel
  2688. * Removed sets from Tcallnode.det_resulttype
  2689. + Added read/write notifications of variables. These will be usefull
  2690. for providing information for several optimizations. For example
  2691. the value of the loop variable of a for loop does matter is the
  2692. variable is read after the for loop, but if it's no longer used
  2693. or written, it doesn't matter and this can be used to optimize
  2694. the loop code generation.
  2695. Revision 1.89 2002/08/23 16:13:16 peter
  2696. * also firstpass funcretrefnode if available. This was breaking the
  2697. asnode compilerproc code
  2698. Revision 1.88 2002/08/20 10:31:26 daniel
  2699. * Tcallnode.det_resulttype rewritten
  2700. Revision 1.87 2002/08/19 19:36:42 peter
  2701. * More fixes for cross unit inlining, all tnodes are now implemented
  2702. * Moved pocall_internconst to po_internconst because it is not a
  2703. calling type at all and it conflicted when inlining of these small
  2704. functions was requested
  2705. Revision 1.86 2002/08/17 22:09:44 florian
  2706. * result type handling in tcgcal.pass_2 overhauled
  2707. * better tnode.dowrite
  2708. * some ppc stuff fixed
  2709. Revision 1.85 2002/08/17 09:23:34 florian
  2710. * first part of current_procinfo rewrite
  2711. Revision 1.84 2002/08/16 14:24:57 carl
  2712. * issameref() to test if two references are the same (then emit no opcodes)
  2713. + ret_in_reg to replace ret_in_acc
  2714. (fix some register allocation bugs at the same time)
  2715. + save_std_register now has an extra parameter which is the
  2716. usedinproc registers
  2717. Revision 1.83 2002/07/20 11:57:53 florian
  2718. * types.pas renamed to defbase.pas because D6 contains a types
  2719. unit so this would conflicts if D6 programms are compiled
  2720. + Willamette/SSE2 instructions to assembler added
  2721. Revision 1.82 2002/07/19 11:41:35 daniel
  2722. * State tracker work
  2723. * The whilen and repeatn are now completely unified into whilerepeatn. This
  2724. allows the state tracker to change while nodes automatically into
  2725. repeat nodes.
  2726. * Resulttypepass improvements to the notn. 'not not a' is optimized away and
  2727. 'not(a>b)' is optimized into 'a<=b'.
  2728. * Resulttypepass improvements to the whilerepeatn. 'while not a' is optimized
  2729. by removing the notn and later switchting the true and falselabels. The
  2730. same is done with 'repeat until not a'.
  2731. Revision 1.81 2002/07/15 18:03:14 florian
  2732. * readded removed changes
  2733. Revision 1.79 2002/07/11 14:41:27 florian
  2734. * start of the new generic parameter handling
  2735. Revision 1.80 2002/07/14 18:00:43 daniel
  2736. + Added the beginning of a state tracker. This will track the values of
  2737. variables through procedures and optimize things away.
  2738. Revision 1.78 2002/07/04 20:43:00 florian
  2739. * first x86-64 patches
  2740. }