ncal.pas 113 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807
  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. { define nice_ncal}
  21. interface
  22. uses
  23. globtype,
  24. node,
  25. {$ifdef state_tracking}
  26. nstate,
  27. {$endif state_tracking}
  28. symbase,symtype,symppu,symsym,symdef,symtable;
  29. type
  30. tcallnode = class(tbinarynode)
  31. { the symbol containing the definition of the procedure }
  32. { to call }
  33. symtableprocentry : tprocsym;
  34. { the symtable containing symtableprocentry }
  35. symtableproc : tsymtable;
  36. { the definition of the procedure to call }
  37. procdefinition : tabstractprocdef;
  38. methodpointer : tnode;
  39. { separately specified resulttype for some compilerprocs (e.g. }
  40. { you can't have a function with an "array of char" resulttype }
  41. { the RTL) (JM) }
  42. restype: ttype;
  43. restypeset: boolean;
  44. { function return reference node, this is used to pass an already
  45. allocated reference for a ret_in_param return value }
  46. funcretrefnode : tnode;
  47. { only the processor specific nodes need to override this }
  48. { constructor }
  49. constructor create(l:tnode; v : tprocsym;st : tsymtable; mp : tnode);virtual;
  50. constructor createintern(const name: string; params: tnode);
  51. constructor createinternres(const name: string; params: tnode; const res: ttype);
  52. constructor createinternreturn(const name: string; params: tnode; returnnode : tnode);
  53. destructor destroy;override;
  54. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  55. procedure ppuwrite(ppufile:tcompilerppufile);override;
  56. procedure derefimpl;override;
  57. function getcopy : tnode;override;
  58. procedure insertintolist(l : tnodelist);override;
  59. function pass_1 : tnode;override;
  60. {$ifdef nice_ncal}
  61. function choose_definition_to_call(paralength:byte;var errorexit:boolean):Tnode;
  62. {$endif}
  63. function det_resulttype:tnode;override;
  64. {$ifdef state_tracking}
  65. function track_state_pass(exec_known:boolean):boolean;override;
  66. {$endif state_tracking}
  67. function docompare(p: tnode): boolean; override;
  68. procedure set_procvar(procvar:tnode);
  69. end;
  70. tcallnodeclass = class of tcallnode;
  71. tcallparaflags = (
  72. { flags used by tcallparanode }
  73. cpf_exact_match_found,
  74. cpf_convlevel1found,
  75. cpf_convlevel2found,
  76. cpf_is_colon_para
  77. {$ifdef nice_ncal}
  78. ,cpf_nomatchfound
  79. {$endif}
  80. );
  81. tcallparanode = class(tbinarynode)
  82. callparaflags : set of tcallparaflags;
  83. hightree : tnode;
  84. { only the processor specific nodes need to override this }
  85. { constructor }
  86. constructor create(expr,next : tnode);virtual;
  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. procedure insertintolist(l : tnodelist);override;
  93. procedure gen_high_tree(openstring:boolean);
  94. procedure get_paratype;
  95. procedure insert_typeconv(defcoll : tparaitem;do_count : boolean);
  96. procedure det_registers;
  97. procedure firstcallparan(defcoll : tparaitem;do_count : boolean);
  98. procedure secondcallparan(defcoll : TParaItem;
  99. push_from_left_to_right:boolean;calloption:tproccalloption;
  100. para_alignment,para_offset : longint);virtual;abstract;
  101. function docompare(p: tnode): boolean; override;
  102. end;
  103. tcallparanodeclass = class of tcallparanode;
  104. tprocinlinenode = class(tnode)
  105. inlinetree : tnode;
  106. inlineprocdef : tprocdef;
  107. retoffset,para_offset,para_size : longint;
  108. constructor create(p:tprocdef);virtual;
  109. destructor destroy;override;
  110. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  111. procedure ppuwrite(ppufile:tcompilerppufile);override;
  112. procedure derefimpl;override;
  113. function getcopy : tnode;override;
  114. function det_resulttype : tnode;override;
  115. procedure insertintolist(l : tnodelist);override;
  116. function pass_1 : tnode;override;
  117. function docompare(p: tnode): boolean; override;
  118. end;
  119. tprocinlinenodeclass = class of tprocinlinenode;
  120. function reverseparameters(p: tcallparanode): tcallparanode;
  121. var
  122. ccallnode : tcallnodeclass;
  123. ccallparanode : tcallparanodeclass;
  124. cprocinlinenode : tprocinlinenodeclass;
  125. implementation
  126. uses
  127. cutils,systems,
  128. verbose,globals,
  129. symconst,paramgr,defutil,defcmp,
  130. htypechk,pass_1,cpuinfo,cpubase,
  131. nbas,ncnv,nld,ninl,nadd,ncon,
  132. rgobj,cgbase
  133. ;
  134. {****************************************************************************
  135. HELPERS
  136. ****************************************************************************}
  137. function reverseparameters(p: tcallparanode): tcallparanode;
  138. var
  139. hp1, hp2: tcallparanode;
  140. begin
  141. hp1:=nil;
  142. while assigned(p) do
  143. begin
  144. { pull out }
  145. hp2:=p;
  146. p:=tcallparanode(p.right);
  147. { pull in }
  148. hp2.right:=hp1;
  149. hp1:=hp2;
  150. end;
  151. reverseparameters:=hp1;
  152. end;
  153. procedure search_class_overloads(aprocsym : tprocsym);
  154. { searches n in symtable of pd and all anchestors }
  155. var
  156. speedvalue : cardinal;
  157. srsym : tprocsym;
  158. s : string;
  159. srpdl : pprocdeflist;
  160. objdef : tobjectdef;
  161. begin
  162. if aprocsym.overloadchecked then
  163. exit;
  164. aprocsym.overloadchecked:=true;
  165. if (aprocsym.owner.symtabletype<>objectsymtable) then
  166. internalerror(200111021);
  167. objdef:=tobjectdef(aprocsym.owner.defowner);
  168. { we start in the parent }
  169. if not assigned(objdef.childof) then
  170. exit;
  171. objdef:=objdef.childof;
  172. s:=aprocsym.name;
  173. speedvalue:=getspeedvalue(s);
  174. while assigned(objdef) do
  175. begin
  176. srsym:=tprocsym(objdef.symtable.speedsearch(s,speedvalue));
  177. if assigned(srsym) then
  178. begin
  179. if (srsym.typ<>procsym) then
  180. internalerror(200111022);
  181. if srsym.is_visible_for_proc(aktprocdef) then
  182. begin
  183. srsym.add_para_match_to(Aprocsym);
  184. { we can stop if the overloads were already added
  185. for the found symbol }
  186. if srsym.overloadchecked then
  187. break;
  188. end;
  189. end;
  190. { next parent }
  191. objdef:=objdef.childof;
  192. end;
  193. end;
  194. {****************************************************************************
  195. TCALLPARANODE
  196. ****************************************************************************}
  197. constructor tcallparanode.create(expr,next : tnode);
  198. begin
  199. inherited create(callparan,expr,next);
  200. hightree:=nil;
  201. if assigned(expr) then
  202. expr.set_file_line(self);
  203. callparaflags:=[];
  204. end;
  205. destructor tcallparanode.destroy;
  206. begin
  207. hightree.free;
  208. inherited destroy;
  209. end;
  210. constructor tcallparanode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  211. begin
  212. inherited ppuload(t,ppufile);
  213. ppufile.getsmallset(callparaflags);
  214. hightree:=ppuloadnode(ppufile);
  215. end;
  216. procedure tcallparanode.ppuwrite(ppufile:tcompilerppufile);
  217. begin
  218. inherited ppuwrite(ppufile);
  219. ppufile.putsmallset(callparaflags);
  220. ppuwritenode(ppufile,hightree);
  221. end;
  222. procedure tcallparanode.derefimpl;
  223. begin
  224. inherited derefimpl;
  225. if assigned(hightree) then
  226. hightree.derefimpl;
  227. end;
  228. function tcallparanode.getcopy : tnode;
  229. var
  230. n : tcallparanode;
  231. begin
  232. n:=tcallparanode(inherited getcopy);
  233. n.callparaflags:=callparaflags;
  234. if assigned(hightree) then
  235. n.hightree:=hightree.getcopy
  236. else
  237. n.hightree:=nil;
  238. result:=n;
  239. end;
  240. procedure tcallparanode.insertintolist(l : tnodelist);
  241. begin
  242. end;
  243. procedure tcallparanode.get_paratype;
  244. var
  245. old_get_para_resulttype : boolean;
  246. old_array_constructor : boolean;
  247. begin
  248. inc(parsing_para_level);
  249. if assigned(right) then
  250. tcallparanode(right).get_paratype;
  251. old_array_constructor:=allow_array_constructor;
  252. old_get_para_resulttype:=get_para_resulttype;
  253. get_para_resulttype:=true;
  254. allow_array_constructor:=true;
  255. resulttypepass(left);
  256. get_para_resulttype:=old_get_para_resulttype;
  257. allow_array_constructor:=old_array_constructor;
  258. if codegenerror then
  259. resulttype:=generrortype
  260. else
  261. resulttype:=left.resulttype;
  262. dec(parsing_para_level);
  263. end;
  264. function is_var_para_incompatible(from_def,to_def:Tdef):boolean;
  265. {Might be an idea to move this to defbase...}
  266. begin
  267. is_var_para_incompatible:=
  268. { allows conversion from word to integer and
  269. byte to shortint, but only for TP7 compatibility }
  270. (not(
  271. (m_tp7 in aktmodeswitches) and
  272. (from_def.deftype=orddef) and
  273. (to_def.deftype=orddef) and
  274. (from_def.size=to_def.size)
  275. ) and
  276. { an implicit pointer conversion is allowed }
  277. not(
  278. (from_def.deftype=pointerdef) and
  279. (to_def.deftype=pointerdef)
  280. ) and
  281. { child objects can be also passed }
  282. { in non-delphi mode, otherwise }
  283. { they must match exactly, except }
  284. { if they are objects }
  285. not(
  286. (from_def.deftype=objectdef) and
  287. (to_def.deftype=objectdef) and
  288. ((
  289. (tobjectdef(from_def).is_related(tobjectdef(to_def))) and
  290. (m_delphi in aktmodeswitches) and
  291. (tobjectdef(from_def).objecttype=odt_object) and
  292. (tobjectdef(to_def).objecttype=odt_object)
  293. ) or
  294. (
  295. (tobjectdef(from_def).is_related(tobjectdef(to_def))) and
  296. (not (m_delphi in aktmodeswitches))
  297. ))
  298. ) and
  299. { passing a single element to a openarray of the same type }
  300. not(
  301. (is_open_array(to_def) and
  302. equal_defs(tarraydef(to_def).elementtype.def,from_def))
  303. ) and
  304. { an implicit file conversion is also allowed }
  305. { from a typed file to an untyped one }
  306. not(
  307. (from_def.deftype=filedef) and
  308. (to_def.deftype=filedef) and
  309. (tfiledef(to_def).filetyp = ft_untyped) and
  310. (tfiledef(from_def).filetyp = ft_typed)
  311. ) and
  312. not(equal_defs(from_def,to_def)));
  313. end;
  314. procedure tcallparanode.insert_typeconv(defcoll : tparaitem;do_count : boolean);
  315. var
  316. oldtype : ttype;
  317. {$ifdef extdebug}
  318. store_count_ref : boolean;
  319. {$endif def extdebug}
  320. p1 : tnode;
  321. begin
  322. inc(parsing_para_level);
  323. if not assigned(defcoll) then
  324. internalerror(200104261);
  325. {$ifdef extdebug}
  326. if do_count then
  327. begin
  328. store_count_ref:=count_ref;
  329. count_ref:=true;
  330. end;
  331. {$endif def extdebug}
  332. if assigned(right) then
  333. begin
  334. { if we are a para that belongs to varargs then keep
  335. the current defcoll }
  336. if (nf_varargs_para in flags) then
  337. tcallparanode(right).insert_typeconv(defcoll,do_count)
  338. else
  339. tcallparanode(right).insert_typeconv(tparaitem(defcoll.next),do_count);
  340. end;
  341. { Be sure to have the resulttype }
  342. if not assigned(left.resulttype.def) then
  343. resulttypepass(left);
  344. { Handle varargs directly, no typeconvs or typechecking needed }
  345. if (nf_varargs_para in flags) then
  346. begin
  347. { convert pascal to C types }
  348. case left.resulttype.def.deftype of
  349. stringdef :
  350. inserttypeconv(left,charpointertype);
  351. floatdef :
  352. inserttypeconv(left,s64floattype);
  353. end;
  354. set_varstate(left,true);
  355. resulttype:=left.resulttype;
  356. dec(parsing_para_level);
  357. exit;
  358. end;
  359. { Do we need arrayconstructor -> set conversion, then insert
  360. it here before the arrayconstructor node breaks the tree
  361. with its conversions of enum->ord }
  362. if (left.nodetype=arrayconstructorn) and
  363. (defcoll.paratype.def.deftype=setdef) then
  364. inserttypeconv(left,defcoll.paratype);
  365. { set some settings needed for arrayconstructor }
  366. if is_array_constructor(left.resulttype.def) then
  367. begin
  368. if is_array_of_const(defcoll.paratype.def) then
  369. begin
  370. if assigned(aktcallprocdef) and
  371. (aktcallprocdef.proccalloption in [pocall_cppdecl,pocall_cdecl]) and
  372. (po_external in aktcallprocdef.procoptions) then
  373. include(left.flags,nf_cargs);
  374. { force variant array }
  375. include(left.flags,nf_forcevaria);
  376. end
  377. else
  378. begin
  379. include(left.flags,nf_novariaallowed);
  380. { now that the resultting type is know we can insert the required
  381. typeconvs for the array constructor }
  382. tarrayconstructornode(left).force_type(tarraydef(defcoll.paratype.def).elementtype);
  383. end;
  384. end;
  385. { check if local proc/func is assigned to procvar }
  386. if left.resulttype.def.deftype=procvardef then
  387. test_local_to_procvar(tprocvardef(left.resulttype.def),defcoll.paratype.def);
  388. { generate the high() value tree }
  389. if not(assigned(aktcallprocdef) and
  390. (aktcallprocdef.proccalloption in [pocall_cppdecl,pocall_cdecl]) and
  391. (po_external in aktcallprocdef.procoptions)) and
  392. paramanager.push_high_param(defcoll.paratype.def,aktcallprocdef.proccalloption) then
  393. gen_high_tree(is_open_string(defcoll.paratype.def));
  394. { test conversions }
  395. if not(is_shortstring(left.resulttype.def) and
  396. is_shortstring(defcoll.paratype.def)) and
  397. (defcoll.paratype.def.deftype<>formaldef) then
  398. begin
  399. if (defcoll.paratyp in [vs_var,vs_out]) and
  400. is_var_para_incompatible(left.resulttype.def,defcoll.paratype.def) then
  401. begin
  402. CGMessagePos2(left.fileinfo,parser_e_call_by_ref_without_typeconv,
  403. left.resulttype.def.typename,defcoll.paratype.def.typename);
  404. end;
  405. { Process open parameters }
  406. if paramanager.push_high_param(defcoll.paratype.def,aktcallprocdef.proccalloption) then
  407. begin
  408. { insert type conv but hold the ranges of the array }
  409. oldtype:=left.resulttype;
  410. inserttypeconv(left,defcoll.paratype);
  411. left.resulttype:=oldtype;
  412. end
  413. else
  414. begin
  415. inserttypeconv(left,defcoll.paratype);
  416. end;
  417. if codegenerror then
  418. begin
  419. dec(parsing_para_level);
  420. exit;
  421. end;
  422. end;
  423. { check var strings }
  424. if (cs_strict_var_strings in aktlocalswitches) and
  425. is_shortstring(left.resulttype.def) and
  426. is_shortstring(defcoll.paratype.def) and
  427. (defcoll.paratyp in [vs_out,vs_var]) and
  428. not(is_open_string(defcoll.paratype.def)) and
  429. not(equal_defs(left.resulttype.def,defcoll.paratype.def)) then
  430. begin
  431. aktfilepos:=left.fileinfo;
  432. CGMessage(type_e_strict_var_string_violation);
  433. end;
  434. { Handle formal parameters separate }
  435. if (defcoll.paratype.def.deftype=formaldef) then
  436. begin
  437. { load procvar if a procedure is passed }
  438. if (m_tp_procvar in aktmodeswitches) and
  439. (left.nodetype=calln) and
  440. (is_void(left.resulttype.def)) then
  441. begin
  442. p1:=cloadnode.create_procvar(tcallnode(left).symtableprocentry,
  443. tprocdef(tcallnode(left).procdefinition),tcallnode(left).symtableproc);
  444. if assigned(tcallnode(left).right) then
  445. tloadnode(p1).set_mp(tcallnode(left).right);
  446. left.free;
  447. left:=p1;
  448. resulttypepass(left);
  449. end;
  450. case defcoll.paratyp of
  451. vs_var,
  452. vs_out :
  453. begin
  454. if not valid_for_formal_var(left) then
  455. CGMessagePos(left.fileinfo,parser_e_illegal_parameter_list);
  456. end;
  457. vs_const :
  458. begin
  459. if not valid_for_formal_const(left) then
  460. CGMessagePos(left.fileinfo,parser_e_illegal_parameter_list);
  461. end;
  462. end;
  463. end
  464. else
  465. begin
  466. { check if the argument is allowed }
  467. if (defcoll.paratyp in [vs_out,vs_var]) then
  468. valid_for_var(left);
  469. end;
  470. if defcoll.paratyp in [vs_var,vs_const] then
  471. begin
  472. { Causes problems with const ansistrings if also }
  473. { done for vs_const (JM) }
  474. if defcoll.paratyp = vs_var then
  475. set_unique(left);
  476. make_not_regable(left);
  477. end;
  478. { ansistrings out paramaters doesn't need to be }
  479. { unique, they are finalized }
  480. if defcoll.paratyp=vs_out then
  481. make_not_regable(left);
  482. if do_count then
  483. begin
  484. { not completly proper, but avoids some warnings }
  485. if (defcoll.paratyp in [vs_var,vs_out]) then
  486. set_funcret_is_valid(left);
  487. set_varstate(left,not(defcoll.paratyp in [vs_var,vs_out]));
  488. end;
  489. { must only be done after typeconv PM }
  490. resulttype:=defcoll.paratype;
  491. dec(parsing_para_level);
  492. {$ifdef extdebug}
  493. if do_count then
  494. count_ref:=store_count_ref;
  495. {$endif def extdebug}
  496. end;
  497. procedure tcallparanode.det_registers;
  498. var
  499. old_get_para_resulttype : boolean;
  500. old_array_constructor : boolean;
  501. begin
  502. if assigned(right) then
  503. begin
  504. tcallparanode(right).det_registers;
  505. registers32:=right.registers32;
  506. registersfpu:=right.registersfpu;
  507. {$ifdef SUPPORT_MMX}
  508. registersmmx:=right.registersmmx;
  509. {$endif}
  510. end;
  511. old_array_constructor:=allow_array_constructor;
  512. old_get_para_resulttype:=get_para_resulttype;
  513. get_para_resulttype:=true;
  514. allow_array_constructor:=true;
  515. firstpass(left);
  516. get_para_resulttype:=old_get_para_resulttype;
  517. allow_array_constructor:=old_array_constructor;
  518. if left.registers32>registers32 then
  519. registers32:=left.registers32;
  520. if left.registersfpu>registersfpu then
  521. registersfpu:=left.registersfpu;
  522. {$ifdef SUPPORT_MMX}
  523. if left.registersmmx>registersmmx then
  524. registersmmx:=left.registersmmx;
  525. {$endif SUPPORT_MMX}
  526. end;
  527. procedure tcallparanode.firstcallparan(defcoll : tparaitem;do_count : boolean);
  528. begin
  529. if not assigned(left.resulttype.def) then
  530. begin
  531. get_paratype;
  532. if assigned(defcoll) then
  533. insert_typeconv(defcoll,do_count);
  534. end;
  535. det_registers;
  536. end;
  537. procedure tcallparanode.gen_high_tree(openstring:boolean);
  538. var
  539. temp: tnode;
  540. len : integer;
  541. loadconst : boolean;
  542. begin
  543. if assigned(hightree) then
  544. exit;
  545. len:=-1;
  546. loadconst:=true;
  547. case left.resulttype.def.deftype of
  548. arraydef :
  549. begin
  550. { handle via a normal inline in_high_x node }
  551. loadconst := false;
  552. hightree := geninlinenode(in_high_x,false,left.getcopy);
  553. { only substract low(array) if it's <> 0 }
  554. temp := geninlinenode(in_low_x,false,left.getcopy);
  555. firstpass(temp);
  556. if (temp.nodetype <> ordconstn) or
  557. (tordconstnode(temp).value <> 0) then
  558. hightree := caddnode.create(subn,hightree,temp)
  559. else
  560. temp.free;
  561. end;
  562. stringdef :
  563. begin
  564. if openstring then
  565. begin
  566. { handle via a normal inline in_high_x node }
  567. loadconst := false;
  568. hightree := geninlinenode(in_high_x,false,left.getcopy);
  569. end
  570. else
  571. { passing a string to an array of char }
  572. begin
  573. if (left.nodetype=stringconstn) then
  574. begin
  575. len:=str_length(left);
  576. if len>0 then
  577. dec(len);
  578. end
  579. else
  580. begin
  581. hightree:=caddnode.create(subn,geninlinenode(in_length_x,false,left.getcopy),
  582. cordconstnode.create(1,s32bittype,false));
  583. loadconst:=false;
  584. end;
  585. end;
  586. end;
  587. else
  588. len:=0;
  589. end;
  590. if loadconst then
  591. hightree:=cordconstnode.create(len,s32bittype,true)
  592. else
  593. hightree:=ctypeconvnode.create(hightree,s32bittype);
  594. firstpass(hightree);
  595. end;
  596. function tcallparanode.docompare(p: tnode): boolean;
  597. begin
  598. docompare :=
  599. inherited docompare(p) and
  600. (callparaflags = tcallparanode(p).callparaflags) and
  601. hightree.isequal(tcallparanode(p).hightree);
  602. end;
  603. {****************************************************************************
  604. TCALLNODE
  605. ****************************************************************************}
  606. constructor tcallnode.create(l:tnode;v : tprocsym;st : tsymtable; mp : tnode);
  607. begin
  608. inherited create(calln,l,nil);
  609. symtableprocentry:=v;
  610. symtableproc:=st;
  611. include(flags,nf_return_value_used);
  612. methodpointer:=mp;
  613. procdefinition:=nil;
  614. restypeset := false;
  615. funcretrefnode:=nil;
  616. end;
  617. constructor tcallnode.createintern(const name: string; params: tnode);
  618. var
  619. srsym: tsym;
  620. symowner: tsymtable;
  621. begin
  622. if not (cs_compilesystem in aktmoduleswitches) then
  623. begin
  624. srsym := searchsymonlyin(systemunit,name);
  625. symowner := systemunit;
  626. end
  627. else
  628. begin
  629. searchsym(name,srsym,symowner);
  630. if not assigned(srsym) then
  631. searchsym(upper(name),srsym,symowner);
  632. end;
  633. if not assigned(srsym) or
  634. (srsym.typ <> procsym) then
  635. begin
  636. {$ifdef EXTDEBUG}
  637. Comment(V_Error,'unknown compilerproc '+name);
  638. {$endif EXTDEBUG}
  639. internalerror(200107271);
  640. end;
  641. self.create(params,tprocsym(srsym),symowner,nil);
  642. end;
  643. constructor tcallnode.createinternres(const name: string; params: tnode; const res: ttype);
  644. begin
  645. self.createintern(name,params);
  646. restype := res;
  647. restypeset := true;
  648. { both the normal and specified resulttype either have to be returned via a }
  649. { parameter or not, but no mixing (JM) }
  650. if paramanager.ret_in_param(restype.def,pocall_compilerproc) xor
  651. paramanager.ret_in_param(symtableprocentry.first_procdef.rettype.def,symtableprocentry.first_procdef.proccalloption) then
  652. internalerror(200108291);
  653. end;
  654. constructor tcallnode.createinternreturn(const name: string; params: tnode; returnnode : tnode);
  655. begin
  656. self.createintern(name,params);
  657. funcretrefnode:=returnnode;
  658. if not paramanager.ret_in_param(symtableprocentry.first_procdef.rettype.def,symtableprocentry.first_procdef.proccalloption) then
  659. internalerror(200204247);
  660. end;
  661. destructor tcallnode.destroy;
  662. begin
  663. methodpointer.free;
  664. funcretrefnode.free;
  665. inherited destroy;
  666. end;
  667. constructor tcallnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  668. begin
  669. inherited ppuload(t,ppufile);
  670. symtableprocentry:=tprocsym(ppufile.getderef);
  671. {$ifdef fpc}
  672. {$warning FIXME: No withsymtable support}
  673. {$endif}
  674. symtableproc:=nil;
  675. procdefinition:=tprocdef(ppufile.getderef);
  676. restypeset:=boolean(ppufile.getbyte);
  677. methodpointer:=ppuloadnode(ppufile);
  678. funcretrefnode:=ppuloadnode(ppufile);
  679. end;
  680. procedure tcallnode.ppuwrite(ppufile:tcompilerppufile);
  681. begin
  682. inherited ppuwrite(ppufile);
  683. ppufile.putderef(symtableprocentry);
  684. ppufile.putderef(procdefinition);
  685. ppufile.putbyte(byte(restypeset));
  686. ppuwritenode(ppufile,methodpointer);
  687. ppuwritenode(ppufile,funcretrefnode);
  688. end;
  689. procedure tcallnode.derefimpl;
  690. begin
  691. inherited derefimpl;
  692. resolvesym(pointer(symtableprocentry));
  693. symtableproc:=symtableprocentry.owner;
  694. resolvedef(pointer(procdefinition));
  695. if assigned(methodpointer) then
  696. methodpointer.derefimpl;
  697. if assigned(funcretrefnode) then
  698. funcretrefnode.derefimpl;
  699. end;
  700. procedure tcallnode.set_procvar(procvar:tnode);
  701. begin
  702. right:=procvar;
  703. end;
  704. function tcallnode.getcopy : tnode;
  705. var
  706. n : tcallnode;
  707. begin
  708. n:=tcallnode(inherited getcopy);
  709. n.symtableprocentry:=symtableprocentry;
  710. n.symtableproc:=symtableproc;
  711. n.procdefinition:=procdefinition;
  712. n.restype := restype;
  713. n.restypeset := restypeset;
  714. if assigned(methodpointer) then
  715. n.methodpointer:=methodpointer.getcopy
  716. else
  717. n.methodpointer:=nil;
  718. if assigned(funcretrefnode) then
  719. n.funcretrefnode:=funcretrefnode.getcopy
  720. else
  721. n.funcretrefnode:=nil;
  722. result:=n;
  723. end;
  724. procedure tcallnode.insertintolist(l : tnodelist);
  725. begin
  726. end;
  727. {$ifdef nice_ncal}
  728. function Tcallnode.choose_definition_to_call(paralength:byte;var errorexit:boolean):Tnode;
  729. { check if the resulttype.def from tree p is equal with def, needed
  730. for stringconstn and formaldef }
  731. function is_equal(p:tcallparanode;def:tdef) : boolean;
  732. begin
  733. { safety check }
  734. if not (assigned(def) or assigned(p.resulttype.def)) then
  735. begin
  736. is_equal:=false;
  737. exit;
  738. end;
  739. { all types can be passed to a formaldef }
  740. is_equal:=(def.deftype=formaldef) or
  741. (defbase.is_equal(p.resulttype.def,def))
  742. { integer constants are compatible with all integer parameters if
  743. the specified value matches the range }
  744. or
  745. (
  746. (tbinarynode(p).left.nodetype=ordconstn) and
  747. is_integer(p.resulttype.def) and
  748. is_integer(def) and
  749. (tordconstnode(p.left).value>=torddef(def).low) and
  750. (tordconstnode(p.left).value<=torddef(def).high)
  751. )
  752. { to support ansi/long/wide strings in a proper way }
  753. { string and string[10] are assumed as equal }
  754. { when searching the correct overloaded procedure }
  755. or
  756. (
  757. (def.deftype=stringdef) and (p.resulttype.def.deftype=stringdef) and
  758. (tstringdef(def).string_typ=tstringdef(p.resulttype.def).string_typ)
  759. )
  760. or
  761. (
  762. (p.left.nodetype=stringconstn) and
  763. (is_ansistring(p.resulttype.def) and is_pchar(def))
  764. )
  765. or
  766. (
  767. (p.left.nodetype=ordconstn) and
  768. (is_char(p.resulttype.def) and (is_shortstring(def) or is_ansistring(def)))
  769. )
  770. { set can also be a not yet converted array constructor }
  771. or
  772. (
  773. (def.deftype=setdef) and (p.resulttype.def.deftype=arraydef) and
  774. (tarraydef(p.resulttype.def).IsConstructor) and not(tarraydef(p.resulttype.def).IsVariant)
  775. )
  776. { in tp7 mode proc -> procvar is allowed }
  777. or
  778. (
  779. (m_tp_procvar in aktmodeswitches) and
  780. (def.deftype=procvardef) and (p.left.nodetype=calln) and
  781. (proc_to_procvar_equal(tprocdef(tcallnode(p.left).procdefinition),tprocvardef(def),false))
  782. )
  783. ;
  784. end;
  785. procedure get_candidate_information(var cl2_count,cl1_count,equal_count,exact_count:byte;
  786. var ordspace:double;
  787. treeparas:Tcallparanode;candparas:Tparaitem);
  788. {Gets information how the parameters would be converted to the candidate.}
  789. var hcvt:Tconverttype;
  790. from_def,to_def:Tdef;
  791. begin
  792. cl2_count:=0;
  793. cl1_count:=0;
  794. equal_count:=0;
  795. exact_count:=0;
  796. ordspace:=0;
  797. while candparas<>nil do
  798. begin
  799. from_def:=treeparas.resulttype.def;
  800. to_def:=candparas.paratype.def;
  801. if to_def=from_def then
  802. inc(exact_count)
  803. { if a type is totally included in the other }
  804. { we don't fear an overflow , }
  805. { so we can do as if it is an equal match }
  806. else if (treeparas.left.nodetype=ordconstn) and is_integer(to_def) then
  807. begin
  808. inc(equal_count);
  809. ordspace:=ordspace+(double(Torddef(from_def).low)-Torddef(to_def).low)+
  810. (double(Torddef(to_def).high)-Torddef(from_def).high);
  811. end
  812. else if ((from_def.deftype=orddef) and (to_def.deftype=orddef)) and
  813. (is_in_limit(from_def,to_def) or
  814. ((candparas.paratyp in [vs_var,vs_out]) and (from_def.size=to_def.size))
  815. ) then
  816. begin
  817. ordspace:=ordspace+Torddef(to_def).high;
  818. ordspace:=ordspace-Torddef(to_def).low;
  819. inc(equal_count);
  820. end
  821. else if is_equal(treeparas,to_def) then
  822. inc(equal_count)
  823. else
  824. case isconvertable(from_def,to_def,
  825. hcvt,treeparas.left.nodetype,false) of
  826. 0:
  827. internalerror(200208021);
  828. 1:
  829. inc(cl1_count);
  830. 2:
  831. inc(cl2_count);
  832. end;
  833. treeparas:=Tcallparanode(treeparas.right);
  834. candparas:=Tparaitem(candparas.next);
  835. end;
  836. end;
  837. type Tcandidate_array=array[1..$ffff] of Tprocdef;
  838. Pcandidate_array=^Tcandidate_array;
  839. var candidate_alloc,candidates_left,candidate_count:cardinal;
  840. c1,c2,delete_start:cardinal;
  841. cl2_count1,cl1_count1,equal_count1,exact_count1:byte;
  842. ordspace1:double;
  843. cl2_count2,cl1_count2,equal_count2,exact_count2:byte;
  844. ordspace2:double;
  845. i,n:cardinal;
  846. pt:Tcallparanode;
  847. def:Tprocdef;
  848. hcvt:Tconverttype;
  849. pdc:Tparaitem;
  850. hpt:Tnode;
  851. srprocsym:Tprocsym;
  852. srsymtable:Tsymtable;
  853. candidate_defs:Pcandidate_array;
  854. begin
  855. if fileinfo.line=398 then
  856. i:=0;
  857. choose_definition_to_call:=nil;
  858. errorexit:=true;
  859. { when the definition has overload directive set, we search for
  860. overloaded definitions in the class, this only needs to be done once
  861. for class entries as the tree keeps always the same }
  862. if (not symtableprocentry.overloadchecked) and
  863. (po_overload in symtableprocentry.first_procdef.procoptions) and
  864. (symtableprocentry.owner.symtabletype=objectsymtable) then
  865. search_class_overloads(symtableprocentry);
  866. {Collect all procedures which have the same # of parameters }
  867. candidates_left:=0;
  868. candidate_count:=0;
  869. candidate_alloc:=32;
  870. getmem(candidate_defs,candidate_alloc*sizeof(Tprocdef));
  871. srprocsym:=symtableprocentry;
  872. srsymtable:=symtableprocentry.owner;
  873. repeat
  874. for i:=1 to srprocsym.procdef_count do
  875. begin
  876. def:=srprocsym.procdef[i];
  877. { only when the # of parameters are supported by the procedure }
  878. if (paralength>=def.minparacount) and
  879. ((po_varargs in def.procoptions) or (paralength<=def.maxparacount)) then
  880. begin
  881. candidate_defs^[i]:=def;
  882. inc(candidates_left);
  883. end
  884. else
  885. candidate_defs^[i]:=nil;
  886. inc(candidate_count);
  887. if candidate_alloc=candidate_count then
  888. begin
  889. candidate_alloc:=candidate_alloc*2;
  890. reallocmem(candidate_defs,candidate_alloc*sizeof(Tprocdef));
  891. end;
  892. end;
  893. if po_overload in srprocsym.first_procdef.procoptions then
  894. begin
  895. repeat
  896. srprocsym:=nil;
  897. repeat
  898. srsymtable:=srsymtable.next;
  899. until (srsymtable=nil) or (srsymtable.symtabletype in [localsymtable,staticsymtable,globalsymtable]);
  900. if assigned(srsymtable) then
  901. srprocsym:=Tprocsym(srsymtable.speedsearch(symtableprocentry.name,symtableprocentry.speedvalue));
  902. until (srsymtable=nil) or (srprocsym<>nil);
  903. if not assigned(srprocsym) then
  904. break;
  905. end
  906. else
  907. break;
  908. until false;
  909. { no procedures found? then there is something wrong
  910. with the parameter size }
  911. if candidates_left=0 then
  912. begin
  913. { in tp mode we can try to convert to procvar if
  914. there are no parameters specified }
  915. if not(assigned(left)) and
  916. (m_tp_procvar in aktmodeswitches) then
  917. begin
  918. hpt:=cloadnode.create(tprocsym(symtableprocentry),symtableproc);
  919. if (symtableprocentry.owner.symtabletype=objectsymtable) and
  920. assigned(methodpointer) then
  921. tloadnode(hpt).set_mp(methodpointer.getcopy);
  922. resulttypepass(hpt);
  923. choose_definition_to_call:=hpt;
  924. end
  925. else
  926. begin
  927. if assigned(left) then
  928. aktfilepos:=left.fileinfo;
  929. cgmessage(parser_e_wrong_parameter_size);
  930. symtableprocentry.write_parameter_lists(nil);
  931. end;
  932. exit;
  933. end;
  934. {Walk through all candidates and remove the ones
  935. that have incompatible parameters.}
  936. for i:=1 to candidate_count do
  937. if assigned(candidate_defs^[i]) then
  938. begin
  939. def:=candidate_defs^[i];
  940. {Walk through all parameters.}
  941. pdc:=Tparaitem(def.para.first);
  942. pt:=Tcallparanode(left);
  943. while assigned(pdc) do
  944. begin
  945. if pdc.paratyp in [vs_var,vs_out] then
  946. if is_var_para_incompatible(pt.resulttype.def,pdc.paratype.def) and
  947. not(is_shortstring(pt.resulttype.def) and is_shortstring(pdc.paratype.def)) and
  948. (pdc.paratype.def.deftype<>formaldef) then
  949. begin
  950. {Not convertable, def is no longer a candidate.}
  951. candidate_defs^[i]:=nil;
  952. dec(candidates_left);
  953. break;
  954. end
  955. else
  956. exclude(pt.callparaflags,cpf_nomatchfound)
  957. else
  958. if (pt.resulttype.def<>pdc.paratype.def) and
  959. ((isconvertable(pt.resulttype.def,pdc.paratype.def,
  960. hcvt,pt.left.nodetype,false)=0) and
  961. not is_equal(pt,pdc.paratype.def)) then
  962. begin
  963. {Not convertable, def is no longer a candidate.}
  964. candidate_defs^[i]:=nil;
  965. dec(candidates_left);
  966. break;
  967. end
  968. else
  969. exclude(pt.callparaflags,cpf_nomatchfound);
  970. pdc:=Tparaitem(pdc.next);
  971. pt:=Tcallparanode(pt.right);
  972. end;
  973. end;
  974. {Are there any candidates left?}
  975. if candidates_left=0 then
  976. begin
  977. {There is an error, must be wrong type, because
  978. wrong size is already checked (PFV) }
  979. pt:=Tcallparanode(left);
  980. n:=0;
  981. while assigned(pt) do
  982. if cpf_nomatchfound in pt.callparaflags then
  983. break
  984. else
  985. begin
  986. pt:=tcallparanode(pt.right);
  987. inc(n);
  988. end;
  989. if not(assigned(pt) and assigned(pt.resulttype.def)) then
  990. internalerror(39393);
  991. {Def contains the last candidate tested.}
  992. pdc:=Tparaitem(def.para.first);
  993. for i:=1 to n do
  994. pdc:=Tparaitem(pdc.next);
  995. aktfilepos:=pt.fileinfo;
  996. cgmessage3(type_e_wrong_parameter_type,tostr(n+1),
  997. pt.resulttype.def.typename,pdc.paratype.def.typename);
  998. symtableprocentry.write_parameter_lists(nil);
  999. exit;
  1000. end;
  1001. {If there is more candidate that can be called, we have to
  1002. find the most suitable one. We collect the following
  1003. information:
  1004. - Amount of convertlevel 2 parameters.
  1005. - Amount of convertlevel 1 parameters.
  1006. - Amount of equal parameters.
  1007. - Amount of exact parameters.
  1008. - Amount of ordinal space the destination parameters
  1009. provide. For exampe, a word provides 65535-255=65280
  1010. of ordinal space above a byte.
  1011. The first criterium is the candidate that has the least
  1012. convertlevel 2 parameters. The next criterium is
  1013. the candidate that has the most exact parameters, next
  1014. criterium is the least ordinal space and
  1015. the last criterium is the most equal parameters. (DM)}
  1016. if candidates_left>1 then
  1017. begin
  1018. {Find the first candidate.}
  1019. c1:=1;
  1020. while c1<=candidate_count do
  1021. if assigned(candidate_defs^[c1]) then
  1022. break
  1023. else
  1024. inc(c1);
  1025. delete_start:=c1;
  1026. {Get information about candidate c1.}
  1027. get_candidate_information(cl2_count1,cl1_count1,equal_count1,
  1028. exact_count1,ordspace1,Tcallparanode(left),
  1029. Tparaitem(candidate_defs^[c1].para.first));
  1030. {Find the other candidates and eliminate the lesser ones.}
  1031. c2:=c1+1;
  1032. while c2<=candidate_count do
  1033. if assigned(candidate_defs^[c2]) then
  1034. begin
  1035. {Candidate found, get information on it.}
  1036. get_candidate_information(cl2_count2,cl1_count2,equal_count2,
  1037. exact_count2,ordspace2,Tcallparanode(left),
  1038. Tparaitem(candidate_defs^[c2].para.first));
  1039. {Is c1 the better candidate?}
  1040. if (cl2_count1<cl2_count2) or
  1041. ((cl2_count1=cl2_count2) and (exact_count1>exact_count2)) or
  1042. ((cl2_count1=cl2_count2) and (exact_count1=exact_count2) and (equal_count1>equal_count2)) or
  1043. ((cl2_count1=cl2_count2) and (exact_count1=exact_count2) and (equal_count1=equal_count2) and (ordspace1<ordspace2)) then
  1044. {C1 is better, drop c2.}
  1045. candidate_defs^[c2]:=nil
  1046. {Is c2 the better candidate?}
  1047. else if (cl2_count2<cl2_count1) or
  1048. ((cl2_count2=cl2_count1) and (exact_count2>exact_count1)) or
  1049. ((cl2_count2=cl2_count1) and (exact_count2=exact_count1) and (equal_count2>equal_count1)) or
  1050. ((cl2_count2=cl2_count1) and (exact_count2=exact_count1) and (equal_count2=equal_count1) and (ordspace2<ordspace1)) then
  1051. begin
  1052. {C2 is better, drop all previous
  1053. candidates.}
  1054. for i:=delete_start to c2-1 do
  1055. candidate_defs^[i]:=nil;
  1056. delete_start:=c2;
  1057. c1:=c2;
  1058. cl2_count1:=cl2_count2;
  1059. cl1_count1:=cl1_count2;
  1060. equal_count1:=equal_count2;
  1061. exact_count1:=exact_count2;
  1062. ordspace1:=ordspace2;
  1063. end;
  1064. {else the candidates have no advantage over each other,
  1065. do nothing}
  1066. inc(c2);
  1067. end
  1068. else
  1069. inc(c2);
  1070. end;
  1071. {Count the candidates that are left.}
  1072. candidates_left:=0;
  1073. for i:=1 to candidate_count do
  1074. if assigned(candidate_defs^[i]) then
  1075. begin
  1076. inc(candidates_left);
  1077. procdefinition:=candidate_defs^[i];
  1078. end;
  1079. if candidates_left>1 then
  1080. begin
  1081. cgmessage(cg_e_cant_choose_overload_function);
  1082. symtableprocentry.write_parameter_lists(nil);
  1083. exit;
  1084. end;
  1085. freemem(candidate_defs,candidate_alloc*sizeof(Tprocdef));
  1086. if make_ref then
  1087. begin
  1088. Tprocdef(procdefinition).lastref:=Tref.create(Tprocdef(procdefinition).lastref,@fileinfo);
  1089. inc(Tprocdef(procdefinition).refcount);
  1090. if Tprocdef(procdefinition).defref=nil then
  1091. Tprocdef(procdefinition).defref:=Tprocdef(procdefinition).lastref;
  1092. end;
  1093. { big error for with statements
  1094. symtableproc:=procdefinition.owner;
  1095. but neede for overloaded operators !! }
  1096. if symtableproc=nil then
  1097. symtableproc:=procdefinition.owner;
  1098. errorexit:=false;
  1099. end;
  1100. function tcallnode.det_resulttype:tnode;
  1101. var lastpara,paralength:byte;
  1102. oldcallprocdef:Tabstractprocdef;
  1103. pt:Tcallparanode;
  1104. i,n:byte;
  1105. e,is_const:boolean;
  1106. pdc:Tparaitem;
  1107. hpt:Tnode;
  1108. label errorexit;
  1109. begin
  1110. result:=nil;
  1111. oldcallprocdef:=aktcallprocdef;
  1112. aktcallprocdef:=nil;
  1113. { determine length of parameter list }
  1114. pt:=tcallparanode(left);
  1115. paralength:=0;
  1116. while assigned(pt) do
  1117. begin
  1118. include(pt.callparaflags,cpf_nomatchfound);
  1119. inc(paralength);
  1120. pt:=tcallparanode(pt.right);
  1121. end;
  1122. { determine the type of the parameters }
  1123. if assigned(left) then
  1124. begin
  1125. tcallparanode(left).get_paratype;
  1126. if codegenerror then
  1127. goto errorexit;
  1128. end;
  1129. { procedure variable ? }
  1130. if assigned(right) then
  1131. begin
  1132. set_varstate(right,true);
  1133. resulttypepass(right);
  1134. if codegenerror then
  1135. exit;
  1136. procdefinition:=tabstractprocdef(right.resulttype.def);
  1137. { check the amount of parameters }
  1138. pdc:=tparaitem(procdefinition.Para.first);
  1139. pt:=tcallparanode(left);
  1140. lastpara:=paralength;
  1141. while assigned(pdc) and assigned(pt) do
  1142. begin
  1143. { only goto next para if we're out of the varargs }
  1144. if not(po_varargs in procdefinition.procoptions) or
  1145. (lastpara<=procdefinition.maxparacount) then
  1146. pdc:=tparaitem(pdc.next);
  1147. pt:=tcallparanode(pt.right);
  1148. dec(lastpara);
  1149. end;
  1150. if assigned(pt) or assigned(pdc) then
  1151. begin
  1152. if assigned(pt) then
  1153. aktfilepos:=pt.fileinfo;
  1154. CGMessage(parser_e_wrong_parameter_size);
  1155. end;
  1156. end
  1157. else
  1158. { not a procedure variable }
  1159. begin
  1160. { do we know the procedure to call ? }
  1161. if not(assigned(procdefinition)) then
  1162. begin
  1163. result:=choose_definition_to_call(paralength,e);
  1164. if e then
  1165. goto errorexit;
  1166. end;
  1167. (* To do!!!
  1168. { add needed default parameters }
  1169. if assigned(procdefinition) and
  1170. (paralength<procdefinition.maxparacount) then
  1171. begin
  1172. { add default parameters, just read back the skipped
  1173. paras starting from firstPara.previous, when not available
  1174. (all parameters are default) then start with the last
  1175. parameter and read backward (PFV) }
  1176. if not assigned(procs^.firstpara) then
  1177. pdc:=tparaitem(procs^.data.Para.last)
  1178. else
  1179. pdc:=tparaitem(procs^.firstPara.previous);
  1180. while assigned(pdc) do
  1181. begin
  1182. if not assigned(pdc.defaultvalue) then
  1183. internalerror(751349858);
  1184. left:=ccallparanode.create(genconstsymtree(tconstsym(pdc.defaultvalue)),left);
  1185. pdc:=tparaitem(pdc.previous);
  1186. end;
  1187. end;
  1188. *)
  1189. end;
  1190. { handle predefined procedures }
  1191. is_const:=(po_internconst in procdefinition.procoptions) and
  1192. ((block_type in [bt_const,bt_type]) or
  1193. (assigned(left) and (tcallparanode(left).left.nodetype in [realconstn,ordconstn])));
  1194. if (procdefinition.proccalloption=pocall_internproc) or is_const then
  1195. begin
  1196. if assigned(left) then
  1197. begin
  1198. { ptr and settextbuf needs two args }
  1199. if assigned(tcallparanode(left).right) then
  1200. begin
  1201. hpt:=geninlinenode(Tprocdef(procdefinition).extnumber,is_const,left);
  1202. left:=nil;
  1203. end
  1204. else
  1205. begin
  1206. hpt:=geninlinenode(Tprocdef(procdefinition).extnumber,is_const,Tcallparanode(left).left);
  1207. Tcallparanode(left).left:=nil;
  1208. end;
  1209. end
  1210. else
  1211. hpt:=geninlinenode(Tprocdef(procdefinition).extnumber,is_const,nil);
  1212. result:=hpt;
  1213. goto errorexit;
  1214. end;
  1215. {$ifdef dummy}
  1216. { Calling a message method directly ? }
  1217. if assigned(procdefinition) and
  1218. (po_containsself in procdefinition.procoptions) then
  1219. message(cg_e_cannot_call_message_direct);
  1220. {$endif}
  1221. { ensure that the result type is set }
  1222. if not restypeset then
  1223. resulttype:=procdefinition.rettype
  1224. else
  1225. resulttype:=restype;
  1226. { modify the exit code, in case of special cases }
  1227. if (not is_void(resulttype.def)) then
  1228. begin
  1229. if paramanager.ret_in_acc(resulttype.def) then
  1230. begin
  1231. { wide- and ansistrings are returned in EAX }
  1232. { but they are imm. moved to a memory location }
  1233. if is_widestring(resulttype.def) or
  1234. is_ansistring(resulttype.def) then
  1235. begin
  1236. { we use ansistrings so no fast exit here }
  1237. if assigned(procinfo) then
  1238. procinfo.no_fast_exit:=true;
  1239. end;
  1240. end;
  1241. end;
  1242. { constructors return their current class type, not the type where the
  1243. constructor is declared, this can be different because of inheritance }
  1244. if (procdefinition.proctypeoption=potype_constructor) then
  1245. begin
  1246. if assigned(methodpointer) and
  1247. assigned(methodpointer.resulttype.def) and
  1248. (methodpointer.resulttype.def.deftype=classrefdef) then
  1249. resulttype:=tclassrefdef(methodpointer.resulttype.def).pointertype;
  1250. end;
  1251. { flag all callparanodes that belong to the varargs }
  1252. if (po_varargs in procdefinition.procoptions) then
  1253. begin
  1254. pt:=tcallparanode(left);
  1255. i:=paralength;
  1256. while (i>procdefinition.maxparacount) do
  1257. begin
  1258. include(tcallparanode(pt).flags,nf_varargs_para);
  1259. pt:=tcallparanode(pt.right);
  1260. dec(i);
  1261. end;
  1262. end;
  1263. { insert type conversions }
  1264. if assigned(left) then
  1265. begin
  1266. aktcallprocdef:=procdefinition;
  1267. tcallparanode(left).insert_typeconv(tparaitem(procdefinition.Para.first),true);
  1268. end;
  1269. errorexit:
  1270. { Reset some settings back }
  1271. aktcallprocdef:=oldcallprocdef;
  1272. end;
  1273. {$else}
  1274. function tcallnode.det_resulttype:tnode;
  1275. type
  1276. pprocdefcoll = ^tprocdefcoll;
  1277. tprocdefcoll = record
  1278. data : tprocdef;
  1279. nextpara : tparaitem;
  1280. firstpara : tparaitem;
  1281. next : pprocdefcoll;
  1282. end;
  1283. var
  1284. hp,procs,hp2 : pprocdefcoll;
  1285. pd : tprocdef;
  1286. oldcallprocdef : tabstractprocdef;
  1287. def_from,def_to,conv_to : tdef;
  1288. hpt : tnode;
  1289. pt : tcallparanode;
  1290. exactmatch : boolean;
  1291. paralength,lastpara : longint;
  1292. lastparatype : tdef;
  1293. pdc : tparaitem;
  1294. { only Dummy }
  1295. hcvt : tconverttype;
  1296. label
  1297. errorexit;
  1298. { check if the resulttype.def from tree p is equal with def, needed
  1299. for stringconstn and formaldef }
  1300. function is_equal(p:tcallparanode;def:tdef) : boolean;
  1301. begin
  1302. { safety check }
  1303. if not (assigned(def) or assigned(p.resulttype.def)) then
  1304. begin
  1305. is_equal:=false;
  1306. exit;
  1307. end;
  1308. { all types can be passed to a formaldef }
  1309. is_equal:=(def.deftype=formaldef) or
  1310. (defcmp.equal_defs(p.resulttype.def,def))
  1311. { integer constants are compatible with all integer parameters if
  1312. the specified value matches the range }
  1313. or
  1314. (
  1315. (tbinarynode(p).left.nodetype=ordconstn) and
  1316. is_integer(p.resulttype.def) and
  1317. is_integer(def) and
  1318. is_in_limit_value(tordconstnode(p.left).value,p.resulttype.def,def)
  1319. )
  1320. { to support ansi/long/wide strings in a proper way }
  1321. { string and string[10] are assumed as equal }
  1322. { when searching the correct overloaded procedure }
  1323. or
  1324. (
  1325. (def.deftype=stringdef) and (p.resulttype.def.deftype=stringdef) and
  1326. (tstringdef(def).string_typ=tstringdef(p.resulttype.def).string_typ)
  1327. )
  1328. or
  1329. (
  1330. (p.left.nodetype=stringconstn) and
  1331. (is_ansistring(p.resulttype.def) and is_pchar(def))
  1332. )
  1333. or
  1334. (
  1335. (p.left.nodetype=ordconstn) and
  1336. (is_char(p.resulttype.def) and (is_shortstring(def) or is_ansistring(def)))
  1337. )
  1338. { set can also be a not yet converted array constructor }
  1339. or
  1340. (
  1341. (def.deftype=setdef) and (p.resulttype.def.deftype=arraydef) and
  1342. (tarraydef(p.resulttype.def).IsConstructor) and not(tarraydef(p.resulttype.def).IsVariant)
  1343. )
  1344. { in tp7 mode proc -> procvar is allowed }
  1345. or
  1346. (
  1347. (m_tp_procvar in aktmodeswitches) and
  1348. (def.deftype=procvardef) and (p.left.nodetype=calln) and
  1349. (proc_to_procvar_equal(tprocdef(tcallnode(p.left).procdefinition),tprocvardef(def))>=te_equal)
  1350. )
  1351. ;
  1352. end;
  1353. var
  1354. i,j : longint;
  1355. has_overload_directive,
  1356. found,
  1357. is_const : boolean;
  1358. bestord : torddef;
  1359. srprocsym : tprocsym;
  1360. srsymtable : tsymtable;
  1361. begin
  1362. result:=nil;
  1363. procs:=nil;
  1364. has_overload_directive:=false;
  1365. oldcallprocdef:=aktcallprocdef;
  1366. aktcallprocdef:=nil;
  1367. { determine length of parameter list }
  1368. pt:=tcallparanode(left);
  1369. paralength:=0;
  1370. while assigned(pt) do
  1371. begin
  1372. inc(paralength);
  1373. pt:=tcallparanode(pt.right);
  1374. end;
  1375. { determine the type of the parameters }
  1376. if assigned(left) then
  1377. begin
  1378. tcallparanode(left).get_paratype;
  1379. if codegenerror then
  1380. goto errorexit;
  1381. end;
  1382. { procedure variable ? }
  1383. if assigned(right) then
  1384. begin
  1385. set_varstate(right,true);
  1386. resulttypepass(right);
  1387. if codegenerror then
  1388. exit;
  1389. procdefinition:=tabstractprocdef(right.resulttype.def);
  1390. { check the amount of parameters }
  1391. pdc:=tparaitem(procdefinition.Para.first);
  1392. pt:=tcallparanode(left);
  1393. lastpara:=paralength;
  1394. while assigned(pdc) and assigned(pt) do
  1395. begin
  1396. { only goto next para if we're out of the varargs }
  1397. if not(po_varargs in procdefinition.procoptions) or
  1398. (lastpara<=procdefinition.maxparacount) then
  1399. pdc:=tparaitem(pdc.next);
  1400. pt:=tcallparanode(pt.right);
  1401. dec(lastpara);
  1402. end;
  1403. if assigned(pt) or assigned(pdc) then
  1404. begin
  1405. if assigned(pt) then
  1406. aktfilepos:=pt.fileinfo;
  1407. CGMessage(parser_e_wrong_parameter_size);
  1408. end;
  1409. end
  1410. else
  1411. { not a procedure variable }
  1412. begin
  1413. { do we know the procedure to call ? }
  1414. if not(assigned(procdefinition)) then
  1415. begin
  1416. { when the definition has overload directive set, we search for
  1417. overloaded definitions in the class, this only needs to be done once
  1418. for class entries as the tree keeps always the same }
  1419. if (not symtableprocentry.overloadchecked) and
  1420. (po_overload in symtableprocentry.first_procdef.procoptions) and
  1421. (symtableprocentry.owner.symtabletype=objectsymtable) then
  1422. search_class_overloads(symtableprocentry);
  1423. { link all procedures which have the same # of parameters }
  1424. for j:=1 to symtableprocentry.procdef_count do
  1425. begin
  1426. pd:=symtableprocentry.procdef[j];
  1427. { only when the # of parameter are supported by the
  1428. procedure }
  1429. if (paralength>=pd.minparacount) and
  1430. ((po_varargs in pd.procoptions) or { varargs }
  1431. (paralength<=pd.maxparacount)) then
  1432. begin
  1433. new(hp);
  1434. hp^.data:=pd;
  1435. hp^.next:=procs;
  1436. hp^.firstpara:=tparaitem(pd.Para.first);
  1437. if not(po_varargs in pd.procoptions) then
  1438. begin
  1439. { if not all parameters are given, then skip the
  1440. default parameters }
  1441. for i:=1 to pd.maxparacount-paralength do
  1442. hp^.firstpara:=tparaitem(hp^.firstPara.next);
  1443. end;
  1444. hp^.nextpara:=hp^.firstpara;
  1445. procs:=hp;
  1446. end;
  1447. end;
  1448. { remember if the procedure is declared with the overload directive,
  1449. it's information is still needed also after all procs are removed }
  1450. has_overload_directive:=(po_overload in symtableprocentry.first_procdef.procoptions);
  1451. { when the definition has overload directive set, we search for
  1452. overloaded definitions in the symtablestack. The found
  1453. entries are only added to the procs list and not the procsym, because
  1454. the list can change in every situation }
  1455. if has_overload_directive and
  1456. (symtableprocentry.owner.symtabletype<>objectsymtable) then
  1457. begin
  1458. srsymtable:=symtableprocentry.owner.next;
  1459. while assigned(srsymtable) do
  1460. begin
  1461. if srsymtable.symtabletype in [localsymtable,staticsymtable,globalsymtable] then
  1462. begin
  1463. srprocsym:=tprocsym(srsymtable.speedsearch(symtableprocentry.name,symtableprocentry.speedvalue));
  1464. { process only visible procsyms }
  1465. if assigned(srprocsym) and
  1466. (srprocsym.typ=procsym) and
  1467. srprocsym.is_visible_for_proc(aktprocdef) then
  1468. begin
  1469. { if this procedure doesn't have overload we can stop
  1470. searching }
  1471. if not(po_overload in srprocsym.first_procdef.procoptions) then
  1472. break;
  1473. { process all overloaded definitions }
  1474. for j:=1 to srprocsym.procdef_count do
  1475. begin
  1476. pd:=srprocsym.procdef[j];
  1477. { only when the # of parameter are supported by the
  1478. procedure }
  1479. if (paralength>=pd.minparacount) and
  1480. ((po_varargs in pd.procoptions) or { varargs }
  1481. (paralength<=pd.maxparacount)) then
  1482. begin
  1483. found:=false;
  1484. hp:=procs;
  1485. while assigned(hp) do
  1486. begin
  1487. if compare_paras(hp^.data.para,pd.para,cp_value_equal_const,false)>=te_equal then
  1488. begin
  1489. found:=true;
  1490. break;
  1491. end;
  1492. hp:=hp^.next;
  1493. end;
  1494. if not found then
  1495. begin
  1496. new(hp);
  1497. hp^.data:=pd;
  1498. hp^.next:=procs;
  1499. hp^.firstpara:=tparaitem(pd.Para.first);
  1500. if not(po_varargs in pd.procoptions) then
  1501. begin
  1502. { if not all parameters are given, then skip the
  1503. default parameters }
  1504. for i:=1 to pd.maxparacount-paralength do
  1505. hp^.firstpara:=tparaitem(hp^.firstPara.next);
  1506. end;
  1507. hp^.nextpara:=hp^.firstpara;
  1508. procs:=hp;
  1509. end;
  1510. end;
  1511. end;
  1512. end;
  1513. end;
  1514. srsymtable:=srsymtable.next;
  1515. end;
  1516. end;
  1517. { no procedures found? then there is something wrong
  1518. with the parameter size }
  1519. if not assigned(procs) then
  1520. begin
  1521. { when it's an auto inherited call and there
  1522. is no procedure found, but the procedures
  1523. were defined with overload directive and at
  1524. least two procedures are defined then we ignore
  1525. this inherited by inserting a nothingn. Only
  1526. do this ugly hack in Delphi mode as it looks more
  1527. like a bug. It's also not documented }
  1528. if (m_delphi in aktmodeswitches) and
  1529. (nf_auto_inherited in flags) and
  1530. (has_overload_directive) and
  1531. (symtableprocentry.procdef_count>=2) then
  1532. result:=cnothingnode.create
  1533. else
  1534. begin
  1535. { in tp mode we can try to convert to procvar if
  1536. there are no parameters specified. Only try it
  1537. when there is only one proc definition, else the
  1538. loadnode will give a strange error }
  1539. if not(assigned(left)) and
  1540. (m_tp_procvar in aktmodeswitches) and
  1541. (symtableprocentry.procdef_count=1) then
  1542. begin
  1543. hpt:=cloadnode.create(tprocsym(symtableprocentry),symtableproc);
  1544. if (symtableprocentry.owner.symtabletype=objectsymtable) and
  1545. assigned(methodpointer) then
  1546. tloadnode(hpt).set_mp(methodpointer.getcopy);
  1547. resulttypepass(hpt);
  1548. result:=hpt;
  1549. end
  1550. else
  1551. begin
  1552. if assigned(left) then
  1553. aktfilepos:=left.fileinfo;
  1554. CGMessage(parser_e_wrong_parameter_size);
  1555. symtableprocentry.write_parameter_lists(nil);
  1556. end;
  1557. end;
  1558. goto errorexit;
  1559. end;
  1560. { now we can compare parameter after parameter }
  1561. pt:=tcallparanode(left);
  1562. { we start with the last parameter }
  1563. lastpara:=paralength+1;
  1564. lastparatype:=nil;
  1565. while assigned(pt) do
  1566. begin
  1567. dec(lastpara);
  1568. { walk all procedures and determine how this parameter matches and set:
  1569. 1. pt.exact_match_found if one parameter has an exact match
  1570. 2. exactmatch if an equal or exact match is found
  1571. 3. Para.argconvtyp to exact,equal or convertable
  1572. (when convertable then also convertlevel is set)
  1573. 4. pt.convlevel1found if there is a convertlevel=1
  1574. 5. pt.convlevel2found if there is a convertlevel=2
  1575. }
  1576. exactmatch:=false;
  1577. hp:=procs;
  1578. while assigned(hp) do
  1579. begin
  1580. { varargs are always equal, but not exact }
  1581. if (po_varargs in hp^.data.procoptions) and
  1582. (lastpara>hp^.data.minparacount) then
  1583. begin
  1584. hp^.nextPara.argconvtyp:=act_equal;
  1585. exactmatch:=true;
  1586. end
  1587. else
  1588. begin
  1589. if is_equal(pt,hp^.nextPara.paratype.def) then
  1590. begin
  1591. if hp^.nextPara.paratype.def=pt.resulttype.def then
  1592. begin
  1593. include(pt.callparaflags,cpf_exact_match_found);
  1594. hp^.nextPara.argconvtyp:=act_exact;
  1595. end
  1596. else
  1597. hp^.nextPara.argconvtyp:=act_equal;
  1598. exactmatch:=true;
  1599. end
  1600. else
  1601. begin
  1602. hp^.nextPara.argconvtyp:=act_convertable;
  1603. { var and out parameters are not be convertable
  1604. in Delphi/tp mode. The only exception is when the
  1605. procedure is defined in the system unit }
  1606. if (hp^.nextPara.paratyp in [vs_var,vs_out]) and
  1607. (procs^.data.owner.unitid<>1) and
  1608. ((m_delphi in aktmodeswitches) or
  1609. (m_tp7 in aktmodeswitches)) then
  1610. hp^.nextPara.convertlevel:=0
  1611. else
  1612. begin
  1613. case compare_defs(pt.resulttype.def,hp^.nextPara.paratype.def,pt.left.nodetype) of
  1614. te_convert_l1 :
  1615. hp^.nextPara.convertlevel:=1;
  1616. te_convert_operator,
  1617. te_convert_l2 :
  1618. hp^.nextPara.convertlevel:=2;
  1619. else
  1620. hp^.nextPara.convertlevel:=0;
  1621. end;
  1622. end;
  1623. case hp^.nextPara.convertlevel of
  1624. 1 : include(pt.callparaflags,cpf_convlevel1found);
  1625. 2 : include(pt.callparaflags,cpf_convlevel2found);
  1626. end;
  1627. end;
  1628. end;
  1629. hp:=hp^.next;
  1630. end;
  1631. { If there was an exactmatch then delete all convertables }
  1632. if exactmatch then
  1633. begin
  1634. hp:=procs;
  1635. procs:=nil;
  1636. while assigned(hp) do
  1637. begin
  1638. hp2:=hp^.next;
  1639. { keep if not convertable }
  1640. if (hp^.nextPara.argconvtyp<>act_convertable) then
  1641. begin
  1642. hp^.next:=procs;
  1643. procs:=hp;
  1644. end
  1645. else
  1646. dispose(hp);
  1647. hp:=hp2;
  1648. end;
  1649. end
  1650. else
  1651. { No exact match was found, remove all procedures that are
  1652. not convertable (convertlevel=0) }
  1653. begin
  1654. hp:=procs;
  1655. procs:=nil;
  1656. while assigned(hp) do
  1657. begin
  1658. hp2:=hp^.next;
  1659. { keep if not convertable }
  1660. if (hp^.nextPara.convertlevel<>0) then
  1661. begin
  1662. hp^.next:=procs;
  1663. procs:=hp;
  1664. end
  1665. else
  1666. begin
  1667. { save the type for nice error message }
  1668. lastparatype:=hp^.nextPara.paratype.def;
  1669. dispose(hp);
  1670. end;
  1671. hp:=hp2;
  1672. end;
  1673. end;
  1674. { update nextpara for all procedures }
  1675. hp:=procs;
  1676. while assigned(hp) do
  1677. begin
  1678. { only goto next para if we're out of the varargs }
  1679. if not(po_varargs in hp^.data.procoptions) or
  1680. (lastpara<=hp^.data.maxparacount) then
  1681. hp^.nextpara:=tparaitem(hp^.nextPara.next);
  1682. hp:=hp^.next;
  1683. end;
  1684. { load next parameter or quit loop if no procs left }
  1685. if assigned(procs) then
  1686. pt:=tcallparanode(pt.right)
  1687. else
  1688. break;
  1689. end;
  1690. { All parameters are checked, check if there are any
  1691. procedures left }
  1692. if not assigned(procs) then
  1693. begin
  1694. { there is an error, must be wrong type, because
  1695. wrong size is already checked (PFV) }
  1696. if (not assigned(lastparatype)) or
  1697. (not assigned(pt)) or
  1698. (not assigned(pt.resulttype.def)) then
  1699. internalerror(39393)
  1700. else
  1701. begin
  1702. aktfilepos:=pt.fileinfo;
  1703. CGMessage3(type_e_wrong_parameter_type,tostr(lastpara),
  1704. pt.resulttype.def.typename,lastparatype.typename);
  1705. end;
  1706. symtableprocentry.write_parameter_lists(nil);
  1707. goto errorexit;
  1708. end;
  1709. { if there are several choices left then for orddef }
  1710. { if a type is totally included in the other }
  1711. { we don't fear an overflow , }
  1712. { so we can do as if it is an exact match }
  1713. { this will convert integer to longint }
  1714. { rather than to words }
  1715. { conversion of byte to integer or longint }
  1716. { would still not be solved }
  1717. if assigned(procs) and assigned(procs^.next) then
  1718. begin
  1719. hp:=procs;
  1720. while assigned(hp) do
  1721. begin
  1722. hp^.nextpara:=hp^.firstpara;
  1723. hp:=hp^.next;
  1724. end;
  1725. pt:=tcallparanode(left);
  1726. while assigned(pt) do
  1727. begin
  1728. { matches a parameter of one procedure exact ? }
  1729. exactmatch:=false;
  1730. def_from:=pt.resulttype.def;
  1731. hp:=procs;
  1732. while assigned(hp) do
  1733. begin
  1734. if not is_equal(pt,hp^.nextPara.paratype.def) then
  1735. begin
  1736. def_to:=hp^.nextPara.paratype.def;
  1737. if ((def_from.deftype=orddef) and (def_to.deftype=orddef)) and
  1738. (is_in_limit(def_from,def_to) or
  1739. ((hp^.nextPara.paratyp in [vs_var,vs_out]) and
  1740. (def_from.size=def_to.size))) then
  1741. begin
  1742. exactmatch:=true;
  1743. conv_to:=def_to;
  1744. { there's no use in continuing the search, it will }
  1745. { only result in conv_to being overwritten }
  1746. break;
  1747. end;
  1748. end;
  1749. hp:=hp^.next;
  1750. end;
  1751. { .... if yes, del all the other procedures }
  1752. if exactmatch then
  1753. begin
  1754. { the first .... }
  1755. while (assigned(procs)) and not(is_in_limit(def_from,procs^.nextPara.paratype.def)) do
  1756. begin
  1757. hp:=procs^.next;
  1758. dispose(procs);
  1759. procs:=hp;
  1760. end;
  1761. { and the others }
  1762. hp:=procs;
  1763. while (assigned(hp)) and assigned(hp^.next) do
  1764. begin
  1765. def_to:=hp^.next^.nextPara.paratype.def;
  1766. if not(is_in_limit(def_from,def_to)) then
  1767. begin
  1768. hp2:=hp^.next^.next;
  1769. dispose(hp^.next);
  1770. hp^.next:=hp2;
  1771. end
  1772. else
  1773. begin
  1774. { did we possibly find a better match? }
  1775. if (conv_to.size>def_to.size) or
  1776. is_in_limit(def_to,conv_to) then
  1777. begin
  1778. { is it the same as the previous best? }
  1779. if not defcmp.equal_defs(def_to,conv_to) then
  1780. begin
  1781. { no -> remove all previous best matches }
  1782. hp := hp^.next;
  1783. while procs <> hp do
  1784. begin
  1785. hp2 := procs;
  1786. procs := procs^.next;
  1787. dispose(hp2);
  1788. end;
  1789. { set new match type }
  1790. conv_to:=def_to;
  1791. end
  1792. { the new one matches just as well as the }
  1793. { old one -> keep both }
  1794. else
  1795. hp := hp^.next;
  1796. end
  1797. { not a better match -> remove }
  1798. else
  1799. begin
  1800. hp2 := hp^.next^.next;
  1801. dispose(hp^.next);
  1802. hp^.next:=hp2;
  1803. end;
  1804. end;
  1805. end;
  1806. end;
  1807. { update nextpara for all procedures }
  1808. hp:=procs;
  1809. while assigned(hp) do
  1810. begin
  1811. hp^.nextpara:=tparaitem(hp^.nextPara.next);
  1812. hp:=hp^.next;
  1813. end;
  1814. pt:=tcallparanode(pt.right);
  1815. end;
  1816. end;
  1817. { let's try to eliminate equal if there is an exact match
  1818. is there }
  1819. if assigned(procs) and assigned(procs^.next) then
  1820. begin
  1821. { reset nextpara for all procs left }
  1822. hp:=procs;
  1823. while assigned(hp) do
  1824. begin
  1825. hp^.nextpara:=hp^.firstpara;
  1826. hp:=hp^.next;
  1827. end;
  1828. pt:=tcallparanode(left);
  1829. while assigned(pt) do
  1830. begin
  1831. if cpf_exact_match_found in pt.callparaflags then
  1832. begin
  1833. hp:=procs;
  1834. procs:=nil;
  1835. while assigned(hp) do
  1836. begin
  1837. hp2:=hp^.next;
  1838. { keep the exact matches, dispose the others }
  1839. if (hp^.nextPara.argconvtyp=act_exact) then
  1840. begin
  1841. hp^.next:=procs;
  1842. procs:=hp;
  1843. end
  1844. else
  1845. dispose(hp);
  1846. hp:=hp2;
  1847. end;
  1848. end;
  1849. { update nextpara for all procedures }
  1850. hp:=procs;
  1851. while assigned(hp) do
  1852. begin
  1853. hp^.nextpara:=tparaitem(hp^.nextPara.next);
  1854. hp:=hp^.next;
  1855. end;
  1856. pt:=tcallparanode(pt.right);
  1857. end;
  1858. end;
  1859. { Check if there are integer constant to integer
  1860. parameters then choose the best matching integer
  1861. parameter and remove the others, this is Delphi
  1862. compatible. 1 = byte, 256 = word, etc. }
  1863. if assigned(procs) and assigned(procs^.next) then
  1864. begin
  1865. { reset nextpara for all procs left }
  1866. hp:=procs;
  1867. while assigned(hp) do
  1868. begin
  1869. hp^.nextpara:=hp^.firstpara;
  1870. hp:=hp^.next;
  1871. end;
  1872. pt:=tcallparanode(left);
  1873. while assigned(pt) do
  1874. begin
  1875. bestord:=nil;
  1876. if (pt.left.nodetype=ordconstn) and
  1877. is_integer(pt.resulttype.def) then
  1878. begin
  1879. hp:=procs;
  1880. while assigned(hp) do
  1881. begin
  1882. def_to:=hp^.nextPara.paratype.def;
  1883. { to be sure, it couldn't be something else,
  1884. also the defs here are all in the range
  1885. so now find the closest range }
  1886. if not is_integer(def_to) then
  1887. internalerror(43297815);
  1888. if (not assigned(bestord)) or
  1889. ((torddef(def_to).low>bestord.low) or
  1890. (torddef(def_to).high<bestord.high)) then
  1891. bestord:=torddef(def_to);
  1892. hp:=hp^.next;
  1893. end;
  1894. end;
  1895. { if a bestmatch is found then remove the other
  1896. procs which don't match the bestord }
  1897. if assigned(bestord) then
  1898. begin
  1899. hp:=procs;
  1900. procs:=nil;
  1901. while assigned(hp) do
  1902. begin
  1903. hp2:=hp^.next;
  1904. { keep matching bestord, dispose the others }
  1905. if (torddef(hp^.nextPara.paratype.def)=bestord) then
  1906. begin
  1907. hp^.next:=procs;
  1908. procs:=hp;
  1909. end
  1910. else
  1911. dispose(hp);
  1912. hp:=hp2;
  1913. end;
  1914. end;
  1915. { update nextpara for all procedures }
  1916. hp:=procs;
  1917. while assigned(hp) do
  1918. begin
  1919. hp^.nextpara:=tparaitem(hp^.nextPara.next);
  1920. hp:=hp^.next;
  1921. end;
  1922. pt:=tcallparanode(pt.right);
  1923. end;
  1924. end;
  1925. { Check if there are convertlevel 1 and 2 differences
  1926. left for the parameters, then discard all convertlevel
  1927. 2 procedures. The value of convlevelXfound can still
  1928. be used, because all convertables are still here or
  1929. not }
  1930. if assigned(procs) and assigned(procs^.next) then
  1931. begin
  1932. { reset nextpara for all procs left }
  1933. hp:=procs;
  1934. while assigned(hp) do
  1935. begin
  1936. hp^.nextpara:=hp^.firstpara;
  1937. hp:=hp^.next;
  1938. end;
  1939. pt:=tcallparanode(left);
  1940. while assigned(pt) do
  1941. begin
  1942. if (cpf_convlevel1found in pt.callparaflags) and
  1943. (cpf_convlevel2found in pt.callparaflags) then
  1944. begin
  1945. hp:=procs;
  1946. procs:=nil;
  1947. while assigned(hp) do
  1948. begin
  1949. hp2:=hp^.next;
  1950. { keep all not act_convertable and all convertlevels=1 }
  1951. if (hp^.nextPara.argconvtyp<>act_convertable) or
  1952. (hp^.nextPara.convertlevel=1) then
  1953. begin
  1954. hp^.next:=procs;
  1955. procs:=hp;
  1956. end
  1957. else
  1958. dispose(hp);
  1959. hp:=hp2;
  1960. end;
  1961. end;
  1962. { update nextpara for all procedures }
  1963. hp:=procs;
  1964. while assigned(hp) do
  1965. begin
  1966. hp^.nextpara:=tparaitem(hp^.nextPara.next);
  1967. hp:=hp^.next;
  1968. end;
  1969. pt:=tcallparanode(pt.right);
  1970. end;
  1971. end;
  1972. if not(assigned(procs)) or assigned(procs^.next) then
  1973. begin
  1974. CGMessage(cg_e_cant_choose_overload_function);
  1975. symtableprocentry.write_parameter_lists(nil);
  1976. goto errorexit;
  1977. end;
  1978. if make_ref then
  1979. begin
  1980. procs^.data.lastref:=tref.create(procs^.data.lastref,@fileinfo);
  1981. inc(procs^.data.refcount);
  1982. if procs^.data.defref=nil then
  1983. procs^.data.defref:=procs^.data.lastref;
  1984. end;
  1985. procdefinition:=procs^.data;
  1986. { big error for with statements
  1987. symtableproc:=procdefinition.owner;
  1988. but neede for overloaded operators !! }
  1989. if symtableproc=nil then
  1990. symtableproc:=procdefinition.owner;
  1991. end; { end of procedure to call determination }
  1992. { add needed default parameters }
  1993. if assigned(procs) and
  1994. (paralength<procdefinition.maxparacount) then
  1995. begin
  1996. { add default parameters, just read back the skipped
  1997. paras starting from firstPara.previous, when not available
  1998. (all parameters are default) then start with the last
  1999. parameter and read backward (PFV) }
  2000. if not assigned(procs^.firstpara) then
  2001. pdc:=tparaitem(procs^.data.Para.last)
  2002. else
  2003. pdc:=tparaitem(procs^.firstPara.previous);
  2004. while assigned(pdc) do
  2005. begin
  2006. if not assigned(pdc.defaultvalue) then
  2007. internalerror(751349858);
  2008. left:=ccallparanode.create(genconstsymtree(tconstsym(pdc.defaultvalue)),left);
  2009. pdc:=tparaitem(pdc.previous);
  2010. end;
  2011. end;
  2012. end;
  2013. { handle predefined procedures }
  2014. is_const:=(po_internconst in procdefinition.procoptions) and
  2015. ((block_type in [bt_const,bt_type]) or
  2016. (assigned(left) and (tcallparanode(left).left.nodetype in [realconstn,ordconstn])));
  2017. if (procdefinition.proccalloption=pocall_internproc) or is_const then
  2018. begin
  2019. if assigned(left) then
  2020. begin
  2021. { ptr and settextbuf needs two args }
  2022. if assigned(tcallparanode(left).right) then
  2023. begin
  2024. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,left);
  2025. left:=nil;
  2026. end
  2027. else
  2028. begin
  2029. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,tcallparanode(left).left);
  2030. tcallparanode(left).left:=nil;
  2031. end;
  2032. end
  2033. else
  2034. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,nil);
  2035. result:=hpt;
  2036. goto errorexit;
  2037. end;
  2038. {$ifdef dummy}
  2039. { Calling a message method directly ? }
  2040. if assigned(procdefinition) and
  2041. (po_containsself in procdefinition.procoptions) then
  2042. message(cg_e_cannot_call_message_direct);
  2043. {$endif}
  2044. { ensure that the result type is set }
  2045. if not restypeset then
  2046. resulttype:=procdefinition.rettype
  2047. else
  2048. resulttype:=restype;
  2049. { modify the exit code, in case of special cases }
  2050. if (not is_void(resulttype.def)) then
  2051. begin
  2052. if paramanager.ret_in_reg(resulttype.def,procdefinition.proccalloption) then
  2053. begin
  2054. { wide- and ansistrings are returned in EAX }
  2055. { but they are imm. moved to a memory location }
  2056. if is_widestring(resulttype.def) or
  2057. is_ansistring(resulttype.def) then
  2058. begin
  2059. { we use ansistrings so no fast exit here }
  2060. if assigned(procinfo) then
  2061. procinfo.no_fast_exit:=true;
  2062. end;
  2063. end;
  2064. end;
  2065. { constructors return their current class type, not the type where the
  2066. constructor is declared, this can be different because of inheritance }
  2067. if (procdefinition.proctypeoption=potype_constructor) then
  2068. begin
  2069. if assigned(methodpointer) and
  2070. assigned(methodpointer.resulttype.def) and
  2071. (methodpointer.resulttype.def.deftype=classrefdef) then
  2072. resulttype:=tclassrefdef(methodpointer.resulttype.def).pointertype;
  2073. end;
  2074. { flag all callparanodes that belong to the varargs }
  2075. if (po_varargs in procdefinition.procoptions) then
  2076. begin
  2077. pt:=tcallparanode(left);
  2078. i:=paralength;
  2079. while (i>procdefinition.maxparacount) do
  2080. begin
  2081. include(tcallparanode(pt).flags,nf_varargs_para);
  2082. pt:=tcallparanode(pt.right);
  2083. dec(i);
  2084. end;
  2085. end;
  2086. { insert type conversions }
  2087. if assigned(left) then
  2088. begin
  2089. aktcallprocdef:=procdefinition;
  2090. tcallparanode(left).insert_typeconv(tparaitem(procdefinition.Para.first),true);
  2091. end;
  2092. errorexit:
  2093. { Reset some settings back }
  2094. if assigned(procs) then
  2095. dispose(procs);
  2096. aktcallprocdef:=oldcallprocdef;
  2097. end;
  2098. {$endif}
  2099. function tcallnode.pass_1 : tnode;
  2100. var
  2101. inlinecode : tnode;
  2102. inlined : boolean;
  2103. {$ifdef m68k}
  2104. regi : tregister;
  2105. {$endif}
  2106. method_must_be_valid : boolean;
  2107. label
  2108. errorexit;
  2109. begin
  2110. { the default is nothing to return }
  2111. location.loc:=LOC_INVALID;
  2112. result:=nil;
  2113. inlined:=false;
  2114. inlinecode := nil;
  2115. { work trough all parameters to get the register requirements }
  2116. if assigned(left) then
  2117. tcallparanode(left).det_registers;
  2118. { return node }
  2119. if assigned(funcretrefnode) then
  2120. firstpass(funcretrefnode);
  2121. if assigned(procdefinition) and
  2122. (procdefinition.proccalloption=pocall_inline) then
  2123. begin
  2124. inlinecode:=right;
  2125. if assigned(inlinecode) then
  2126. inlined:=true;
  2127. right:=nil;
  2128. end;
  2129. { procedure variable ? }
  2130. if assigned(right) then
  2131. begin
  2132. firstpass(right);
  2133. { procedure does a call }
  2134. if not (block_type in [bt_const,bt_type]) then
  2135. procinfo.flags:=procinfo.flags or pi_do_call;
  2136. rg.incrementregisterpushed(all_registers);
  2137. end
  2138. else
  2139. { not a procedure variable }
  2140. begin
  2141. { calc the correture value for the register }
  2142. { handle predefined procedures }
  2143. if (procdefinition.proccalloption=pocall_inline) then
  2144. begin
  2145. if assigned(methodpointer) then
  2146. CGMessage(cg_e_unable_inline_object_methods);
  2147. if assigned(right) and (right.nodetype<>procinlinen) then
  2148. CGMessage(cg_e_unable_inline_procvar);
  2149. if not assigned(inlinecode) then
  2150. begin
  2151. if assigned(tprocdef(procdefinition).code) then
  2152. inlinecode:=cprocinlinenode.create(tprocdef(procdefinition))
  2153. else
  2154. CGMessage(cg_e_no_code_for_inline_stored);
  2155. if assigned(inlinecode) then
  2156. begin
  2157. { consider it has not inlined if called
  2158. again inside the args }
  2159. procdefinition.proccalloption:=pocall_fpccall;
  2160. firstpass(inlinecode);
  2161. inlined:=true;
  2162. end;
  2163. end;
  2164. end
  2165. else
  2166. begin
  2167. if not (block_type in [bt_const,bt_type]) then
  2168. procinfo.flags:=procinfo.flags or pi_do_call;
  2169. end;
  2170. { It doesn't hurt to calculate it already though :) (JM) }
  2171. rg.incrementregisterpushed(tprocdef(procdefinition).usedregisters);
  2172. end;
  2173. { get a register for the return value }
  2174. if (not is_void(resulttype.def)) then
  2175. begin
  2176. { for win32 records returned in EDX:EAX, we
  2177. move them to memory after ... }
  2178. if (resulttype.def.deftype=recorddef) then
  2179. begin
  2180. location.loc:=LOC_CREFERENCE;
  2181. end
  2182. else
  2183. if paramanager.ret_in_param(resulttype.def,procdefinition.proccalloption) then
  2184. begin
  2185. location.loc:=LOC_CREFERENCE;
  2186. end
  2187. else
  2188. { ansi/widestrings must be registered, so we can dispose them }
  2189. if is_ansistring(resulttype.def) or
  2190. is_widestring(resulttype.def) then
  2191. begin
  2192. location.loc:=LOC_CREFERENCE;
  2193. registers32:=1;
  2194. end
  2195. else
  2196. { we have only to handle the result if it is used }
  2197. if (nf_return_value_used in flags) then
  2198. begin
  2199. case resulttype.def.deftype of
  2200. enumdef,
  2201. orddef :
  2202. begin
  2203. if (procdefinition.proctypeoption=potype_constructor) then
  2204. begin
  2205. if assigned(methodpointer) and
  2206. (methodpointer.resulttype.def.deftype=classrefdef) then
  2207. begin
  2208. location.loc:=LOC_REGISTER;
  2209. registers32:=1;
  2210. end
  2211. else
  2212. location.loc:=LOC_FLAGS;
  2213. end
  2214. else
  2215. begin
  2216. location.loc:=LOC_REGISTER;
  2217. if is_64bitint(resulttype.def) then
  2218. registers32:=2
  2219. else
  2220. registers32:=1;
  2221. end;
  2222. end;
  2223. floatdef :
  2224. begin
  2225. location.loc:=LOC_FPUREGISTER;
  2226. {$ifdef m68k}
  2227. if (cs_fp_emulation in aktmoduleswitches) or
  2228. (tfloatdef(resulttype.def).typ=s32real) then
  2229. registers32:=1
  2230. else
  2231. registersfpu:=1;
  2232. {$else not m68k}
  2233. registersfpu:=1;
  2234. {$endif not m68k}
  2235. end;
  2236. else
  2237. begin
  2238. location.loc:=LOC_REGISTER;
  2239. registers32:=1;
  2240. end;
  2241. end;
  2242. end;
  2243. end;
  2244. { a fpu can be used in any procedure !! }
  2245. {$ifdef i386}
  2246. registersfpu:=procdefinition.fpu_used;
  2247. {$endif i386}
  2248. { if this is a call to a method calc the registers }
  2249. if (methodpointer<>nil) then
  2250. begin
  2251. case methodpointer.nodetype of
  2252. { but only, if this is not a supporting node }
  2253. typen: ;
  2254. { we need one register for new return value PM }
  2255. hnewn : if registers32=0 then
  2256. registers32:=1;
  2257. else
  2258. begin
  2259. if (procdefinition.proctypeoption in [potype_constructor,potype_destructor]) and
  2260. assigned(symtableproc) and (symtableproc.symtabletype=withsymtable) and
  2261. not twithsymtable(symtableproc).direct_with then
  2262. begin
  2263. CGmessage(cg_e_cannot_call_cons_dest_inside_with);
  2264. end; { Is accepted by Delphi !! }
  2265. { this is not a good reason to accept it in FPC if we produce
  2266. wrong code for it !!! (PM) }
  2267. { R.Assign is not a constructor !!! }
  2268. { but for R^.Assign, R must be valid !! }
  2269. if (procdefinition.proctypeoption=potype_constructor) or
  2270. ((methodpointer.nodetype=loadn) and
  2271. ((methodpointer.resulttype.def.deftype=classrefdef) or
  2272. ((methodpointer.resulttype.def.deftype=objectdef) and
  2273. not(oo_has_virtual in tobjectdef(methodpointer.resulttype.def).objectoptions)
  2274. )
  2275. )
  2276. ) then
  2277. method_must_be_valid:=false
  2278. else
  2279. method_must_be_valid:=true;
  2280. firstpass(methodpointer);
  2281. set_varstate(methodpointer,method_must_be_valid);
  2282. { The object is already used ven if it is called once }
  2283. if (methodpointer.nodetype=loadn) and
  2284. (tloadnode(methodpointer).symtableentry.typ=varsym) then
  2285. tvarsym(tloadnode(methodpointer).symtableentry).varstate:=vs_used;
  2286. registersfpu:=max(methodpointer.registersfpu,registersfpu);
  2287. registers32:=max(methodpointer.registers32,registers32);
  2288. {$ifdef SUPPORT_MMX }
  2289. registersmmx:=max(methodpointer.registersmmx,registersmmx);
  2290. {$endif SUPPORT_MMX}
  2291. end;
  2292. end;
  2293. end;
  2294. if inlined then
  2295. right:=inlinecode;
  2296. { determine the registers of the procedure variable }
  2297. { is this OK for inlined procs also ?? (PM) }
  2298. if assigned(right) then
  2299. begin
  2300. registersfpu:=max(right.registersfpu,registersfpu);
  2301. registers32:=max(right.registers32,registers32);
  2302. {$ifdef SUPPORT_MMX}
  2303. registersmmx:=max(right.registersmmx,registersmmx);
  2304. {$endif SUPPORT_MMX}
  2305. end;
  2306. { determine the registers of the procedure }
  2307. if assigned(left) then
  2308. begin
  2309. registersfpu:=max(left.registersfpu,registersfpu);
  2310. registers32:=max(left.registers32,registers32);
  2311. {$ifdef SUPPORT_MMX}
  2312. registersmmx:=max(left.registersmmx,registersmmx);
  2313. {$endif SUPPORT_MMX}
  2314. end;
  2315. errorexit:
  2316. if inlined then
  2317. procdefinition.proccalloption:=pocall_inline;
  2318. end;
  2319. {$ifdef state_tracking}
  2320. function Tcallnode.track_state_pass(exec_known:boolean):boolean;
  2321. var hp:Tcallparanode;
  2322. value:Tnode;
  2323. begin
  2324. track_state_pass:=false;
  2325. hp:=Tcallparanode(left);
  2326. while assigned(hp) do
  2327. begin
  2328. if left.track_state_pass(exec_known) then
  2329. begin
  2330. left.resulttype.def:=nil;
  2331. do_resulttypepass(left);
  2332. end;
  2333. value:=aktstate.find_fact(hp.left);
  2334. if value<>nil then
  2335. begin
  2336. track_state_pass:=true;
  2337. hp.left.destroy;
  2338. hp.left:=value.getcopy;
  2339. do_resulttypepass(hp.left);
  2340. end;
  2341. hp:=Tcallparanode(hp.right);
  2342. end;
  2343. end;
  2344. {$endif}
  2345. function tcallnode.docompare(p: tnode): boolean;
  2346. begin
  2347. docompare :=
  2348. inherited docompare(p) and
  2349. (symtableprocentry = tcallnode(p).symtableprocentry) and
  2350. (symtableproc = tcallnode(p).symtableproc) and
  2351. (procdefinition = tcallnode(p).procdefinition) and
  2352. (methodpointer.isequal(tcallnode(p).methodpointer)) and
  2353. ((restypeset and tcallnode(p).restypeset and
  2354. (equal_defs(restype.def,tcallnode(p).restype.def))) or
  2355. (not restypeset and not tcallnode(p).restypeset));
  2356. end;
  2357. {****************************************************************************
  2358. TPROCINLINENODE
  2359. ****************************************************************************}
  2360. constructor tprocinlinenode.create(p:tprocdef);
  2361. begin
  2362. inherited create(procinlinen);
  2363. inlineprocdef:=p;
  2364. retoffset:=-POINTER_SIZE; { less dangerous as zero (PM) }
  2365. para_offset:=0;
  2366. para_size:=0;
  2367. { copy inlinetree }
  2368. if assigned(p.code) then
  2369. inlinetree:=p.code.getcopy
  2370. else
  2371. inlinetree:=nil;
  2372. end;
  2373. destructor tprocinlinenode.destroy;
  2374. begin
  2375. if assigned(inlinetree) then
  2376. inlinetree.free;
  2377. inherited destroy;
  2378. end;
  2379. constructor tprocinlinenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  2380. begin
  2381. inherited ppuload(t,ppufile);
  2382. inlineprocdef:=tprocdef(ppufile.getderef);
  2383. inlinetree:=ppuloadnode(ppufile);
  2384. retoffset:=-POINTER_SIZE; { less dangerous as zero (PM) }
  2385. para_offset:=0;
  2386. para_size:=0;
  2387. end;
  2388. procedure tprocinlinenode.ppuwrite(ppufile:tcompilerppufile);
  2389. begin
  2390. inherited ppuwrite(ppufile);
  2391. ppufile.putderef(inlineprocdef);
  2392. ppuwritenode(ppufile,inlinetree);
  2393. end;
  2394. procedure tprocinlinenode.derefimpl;
  2395. begin
  2396. inherited derefimpl;
  2397. if assigned(inlinetree) then
  2398. inlinetree.derefimpl;
  2399. resolvedef(pointer(inlineprocdef));
  2400. end;
  2401. function tprocinlinenode.getcopy : tnode;
  2402. var
  2403. n : tprocinlinenode;
  2404. begin
  2405. n:=tprocinlinenode(inherited getcopy);
  2406. n.inlineprocdef:=inlineprocdef;
  2407. if assigned(inlinetree) then
  2408. n.inlinetree:=inlinetree.getcopy
  2409. else
  2410. n.inlinetree:=nil;
  2411. n.retoffset:=retoffset;
  2412. n.para_offset:=para_offset;
  2413. n.para_size:=para_size;
  2414. getcopy:=n;
  2415. end;
  2416. procedure tprocinlinenode.insertintolist(l : tnodelist);
  2417. begin
  2418. end;
  2419. function tprocinlinenode.det_resulttype : tnode;
  2420. begin
  2421. resulttype:=inlineprocdef.rettype;
  2422. { retrieve info from inlineprocdef }
  2423. retoffset:=-POINTER_SIZE; { less dangerous as zero (PM) }
  2424. para_offset:=0;
  2425. para_size:=inlineprocdef.para_size(target_info.alignment.paraalign);
  2426. if paramanager.ret_in_param(inlineprocdef.rettype.def,inlineprocdef.proccalloption) then
  2427. inc(para_size,POINTER_SIZE);
  2428. result:=nil;
  2429. end;
  2430. function tprocinlinenode.pass_1 : tnode;
  2431. begin
  2432. firstpass(inlinetree);
  2433. registers32:=inlinetree.registers32;
  2434. registersfpu:=inlinetree.registersfpu;
  2435. {$ifdef SUPPORT_MMX}
  2436. registersmmx:=inlinetree.registersmmx;
  2437. {$endif SUPPORT_MMX}
  2438. result:=nil;
  2439. end;
  2440. function tprocinlinenode.docompare(p: tnode): boolean;
  2441. begin
  2442. docompare :=
  2443. inherited docompare(p) and
  2444. inlinetree.isequal(tprocinlinenode(p).inlinetree) and
  2445. (inlineprocdef = tprocinlinenode(p).inlineprocdef);
  2446. end;
  2447. begin
  2448. ccallnode:=tcallnode;
  2449. ccallparanode:=tcallparanode;
  2450. cprocinlinenode:=tprocinlinenode;
  2451. end.
  2452. {
  2453. $Log$
  2454. Revision 1.109 2002-11-25 17:43:17 peter
  2455. * splitted defbase in defutil,symutil,defcmp
  2456. * merged isconvertable and is_equal into compare_defs(_ext)
  2457. * made operator search faster by walking the list only once
  2458. Revision 1.108 2002/11/18 17:31:54 peter
  2459. * pass proccalloption to ret_in_xxx and push_xxx functions
  2460. Revision 1.107 2002/11/15 01:58:50 peter
  2461. * merged changes from 1.0.7 up to 04-11
  2462. - -V option for generating bug report tracing
  2463. - more tracing for option parsing
  2464. - errors for cdecl and high()
  2465. - win32 import stabs
  2466. - win32 records<=8 are returned in eax:edx (turned off by default)
  2467. - heaptrc update
  2468. - more info for temp management in .s file with EXTDEBUG
  2469. Revision 1.106 2002/10/14 18:20:30 carl
  2470. * var parameter checking for classes and interfaces in Delphi mode
  2471. Revision 1.105 2002/10/06 21:02:17 peter
  2472. * fixed limit checking for qword
  2473. Revision 1.104 2002/10/05 15:15:45 peter
  2474. * Write unknwon compiler proc using Comment and only in Extdebug
  2475. Revision 1.103 2002/10/05 12:43:25 carl
  2476. * fixes for Delphi 6 compilation
  2477. (warning : Some features do not work under Delphi)
  2478. Revision 1.102 2002/10/05 00:48:57 peter
  2479. * support inherited; support for overload as it is handled by
  2480. delphi. This is only for delphi mode as it is working is
  2481. undocumented and hard to predict what is done
  2482. Revision 1.101 2002/09/16 14:11:12 peter
  2483. * add argument to equal_paras() to support default values or not
  2484. Revision 1.100 2002/09/15 17:49:59 peter
  2485. * don't have strict var parameter checking for procedures in the
  2486. system unit
  2487. Revision 1.99 2002/09/09 19:30:34 peter
  2488. * don't allow convertable parameters for var and out parameters in
  2489. delphi and tp mode
  2490. Revision 1.98 2002/09/07 15:25:02 peter
  2491. * old logs removed and tabs fixed
  2492. Revision 1.97 2002/09/07 12:16:05 carl
  2493. * second part bug report 1996 fix, testrange in cordconstnode
  2494. only called if option is set (also make parsing a tiny faster)
  2495. Revision 1.96 2002/09/05 14:53:41 peter
  2496. * fixed old callnode.det_resulttype code
  2497. * old ncal code is default again
  2498. Revision 1.95 2002/09/03 21:32:49 daniel
  2499. * Small bugfix for procdef selection
  2500. Revision 1.94 2002/09/03 19:27:22 daniel
  2501. * Activated new ncal code
  2502. Revision 1.93 2002/09/03 16:26:26 daniel
  2503. * Make Tprocdef.defs protected
  2504. Revision 1.92 2002/09/01 13:28:37 daniel
  2505. - write_access fields removed in favor of a flag
  2506. Revision 1.91 2002/09/01 12:14:15 peter
  2507. * remove debug line
  2508. * containself methods can be called directly
  2509. Revision 1.90 2002/09/01 08:01:16 daniel
  2510. * Removed sets from Tcallnode.det_resulttype
  2511. + Added read/write notifications of variables. These will be usefull
  2512. for providing information for several optimizations. For example
  2513. the value of the loop variable of a for loop does matter is the
  2514. variable is read after the for loop, but if it's no longer used
  2515. or written, it doesn't matter and this can be used to optimize
  2516. the loop code generation.
  2517. Revision 1.89 2002/08/23 16:13:16 peter
  2518. * also firstpass funcretrefnode if available. This was breaking the
  2519. asnode compilerproc code
  2520. Revision 1.88 2002/08/20 10:31:26 daniel
  2521. * Tcallnode.det_resulttype rewritten
  2522. Revision 1.87 2002/08/19 19:36:42 peter
  2523. * More fixes for cross unit inlining, all tnodes are now implemented
  2524. * Moved pocall_internconst to po_internconst because it is not a
  2525. calling type at all and it conflicted when inlining of these small
  2526. functions was requested
  2527. Revision 1.86 2002/08/17 22:09:44 florian
  2528. * result type handling in tcgcal.pass_2 overhauled
  2529. * better tnode.dowrite
  2530. * some ppc stuff fixed
  2531. Revision 1.85 2002/08/17 09:23:34 florian
  2532. * first part of procinfo rewrite
  2533. Revision 1.84 2002/08/16 14:24:57 carl
  2534. * issameref() to test if two references are the same (then emit no opcodes)
  2535. + ret_in_reg to replace ret_in_acc
  2536. (fix some register allocation bugs at the same time)
  2537. + save_std_register now has an extra parameter which is the
  2538. usedinproc registers
  2539. Revision 1.83 2002/07/20 11:57:53 florian
  2540. * types.pas renamed to defbase.pas because D6 contains a types
  2541. unit so this would conflicts if D6 programms are compiled
  2542. + Willamette/SSE2 instructions to assembler added
  2543. Revision 1.82 2002/07/19 11:41:35 daniel
  2544. * State tracker work
  2545. * The whilen and repeatn are now completely unified into whilerepeatn. This
  2546. allows the state tracker to change while nodes automatically into
  2547. repeat nodes.
  2548. * Resulttypepass improvements to the notn. 'not not a' is optimized away and
  2549. 'not(a>b)' is optimized into 'a<=b'.
  2550. * Resulttypepass improvements to the whilerepeatn. 'while not a' is optimized
  2551. by removing the notn and later switchting the true and falselabels. The
  2552. same is done with 'repeat until not a'.
  2553. Revision 1.81 2002/07/15 18:03:14 florian
  2554. * readded removed changes
  2555. Revision 1.79 2002/07/11 14:41:27 florian
  2556. * start of the new generic parameter handling
  2557. Revision 1.80 2002/07/14 18:00:43 daniel
  2558. + Added the beginning of a state tracker. This will track the values of
  2559. variables through procedures and optimize things away.
  2560. Revision 1.78 2002/07/04 20:43:00 florian
  2561. * first x86-64 patches
  2562. }