ncal.pas 111 KB

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