htypechk.pas 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit exports some help routines for the type checking
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit htypechk;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. tokens,cpuinfo,
  22. node,
  23. symconst,symtype,symdef,symsym,symbase;
  24. type
  25. Ttok2nodeRec=record
  26. tok : ttoken;
  27. nod : tnodetype;
  28. op_overloading_supported : boolean;
  29. end;
  30. pcandidate = ^tcandidate;
  31. tcandidate = record
  32. next : pcandidate;
  33. data : tprocdef;
  34. wrongparaidx,
  35. firstparaidx : integer;
  36. exact_count,
  37. equal_count,
  38. cl1_count,
  39. cl2_count,
  40. cl3_count,
  41. coper_count : integer; { should be signed }
  42. ordinal_distance : bestreal;
  43. invalid : boolean;
  44. wrongparanr : byte;
  45. end;
  46. tcallcandidates = class
  47. private
  48. FProcSym : tprocsym;
  49. FProcs : pcandidate;
  50. FProcVisibleCnt,
  51. FProcCnt : integer;
  52. FParaNode : tnode;
  53. FParaLength : smallint;
  54. FAllowVariant : boolean;
  55. function proc_add(pd:tprocdef):pcandidate;
  56. public
  57. constructor create(sym:tprocsym;st:tsymtable;ppn:tnode;isprop,ignorevis : boolean);
  58. constructor create_operator(op:ttoken;ppn:tnode);
  59. destructor destroy;override;
  60. procedure list(all:boolean);
  61. {$ifdef EXTDEBUG}
  62. procedure dump_info(lvl:longint);
  63. {$endif EXTDEBUG}
  64. procedure get_information;
  65. function choose_best(var bestpd:tabstractprocdef):integer;
  66. procedure find_wrong_para;
  67. property Count:integer read FProcCnt;
  68. property VisibleCount:integer read FProcVisibleCnt;
  69. end;
  70. const
  71. tok2nodes=25;
  72. tok2node:array[1..tok2nodes] of ttok2noderec=(
  73. (tok:_PLUS ;nod:addn;op_overloading_supported:true), { binary overloading supported }
  74. (tok:_MINUS ;nod:subn;op_overloading_supported:true), { binary and unary overloading supported }
  75. (tok:_STAR ;nod:muln;op_overloading_supported:true), { binary overloading supported }
  76. (tok:_SLASH ;nod:slashn;op_overloading_supported:true), { binary overloading supported }
  77. (tok:_EQUAL ;nod:equaln;op_overloading_supported:true), { binary overloading supported }
  78. (tok:_GT ;nod:gtn;op_overloading_supported:true), { binary overloading supported }
  79. (tok:_LT ;nod:ltn;op_overloading_supported:true), { binary overloading supported }
  80. (tok:_GTE ;nod:gten;op_overloading_supported:true), { binary overloading supported }
  81. (tok:_LTE ;nod:lten;op_overloading_supported:true), { binary overloading supported }
  82. (tok:_SYMDIF ;nod:symdifn;op_overloading_supported:true), { binary overloading supported }
  83. (tok:_STARSTAR;nod:starstarn;op_overloading_supported:true), { binary overloading supported }
  84. (tok:_OP_AS ;nod:asn;op_overloading_supported:false), { binary overloading NOT supported }
  85. (tok:_OP_IN ;nod:inn;op_overloading_supported:false), { binary overloading NOT supported }
  86. (tok:_OP_IS ;nod:isn;op_overloading_supported:false), { binary overloading NOT supported }
  87. (tok:_OP_OR ;nod:orn;op_overloading_supported:true), { binary overloading supported }
  88. (tok:_OP_AND ;nod:andn;op_overloading_supported:true), { binary overloading supported }
  89. (tok:_OP_DIV ;nod:divn;op_overloading_supported:true), { binary overloading supported }
  90. (tok:_OP_NOT ;nod:notn;op_overloading_supported:true), { unary overloading supported }
  91. (tok:_OP_MOD ;nod:modn;op_overloading_supported:true), { binary overloading supported }
  92. (tok:_OP_SHL ;nod:shln;op_overloading_supported:true), { binary overloading supported }
  93. (tok:_OP_SHR ;nod:shrn;op_overloading_supported:true), { binary overloading supported }
  94. (tok:_OP_XOR ;nod:xorn;op_overloading_supported:true), { binary overloading supported }
  95. (tok:_ASSIGNMENT;nod:assignn;op_overloading_supported:true), { unary overloading supported }
  96. (tok:_CARET ;nod:caretn;op_overloading_supported:false), { binary overloading NOT supported }
  97. (tok:_UNEQUAL ;nod:unequaln;op_overloading_supported:false) { binary overloading NOT supported overload = instead }
  98. );
  99. const
  100. { firstcallparan without varspez we don't count the ref }
  101. {$ifdef extdebug}
  102. count_ref : boolean = true;
  103. {$endif def extdebug}
  104. allow_array_constructor : boolean = false;
  105. function node2opstr(nt:tnodetype):string;
  106. { check operator args and result type }
  107. function isbinaryoperatoroverloadable(treetyp:tnodetype;ld:tdef;lt:tnodetype;rd:tdef;rt:tnodetype) : boolean;
  108. function isoperatoracceptable(pf : tprocdef; optoken : ttoken) : boolean;
  109. function isunaryoverloaded(var t : tnode) : boolean;
  110. function isbinaryoverloaded(var t : tnode) : boolean;
  111. { Register Allocation }
  112. procedure make_not_regable(p : tnode);
  113. procedure calcregisters(p : tbinarynode;r32,fpu,mmx : word);
  114. { procvar handling }
  115. function is_procvar_load(p:tnode):boolean;
  116. procedure test_local_to_procvar(from_def:tprocvardef;to_def:tdef);
  117. { sets varsym varstate field correctly }
  118. type
  119. tvarstateflag = (vsf_must_be_valid,vsf_use_hints);
  120. tvarstateflags = set of tvarstateflag;
  121. procedure set_varstate(p:tnode;newstate:tvarstate;varstateflags:tvarstateflags);
  122. { sets the callunique flag, if the node is a vecn, }
  123. { takes care of type casts etc. }
  124. procedure set_unique(p : tnode);
  125. function valid_for_formal_var(p : tnode) : boolean;
  126. function valid_for_formal_const(p : tnode) : boolean;
  127. function valid_for_var(p:tnode):boolean;
  128. function valid_for_assignment(p:tnode):boolean;
  129. function valid_for_addr(p : tnode) : boolean;
  130. function allowenumop(nt:tnodetype):boolean;
  131. implementation
  132. uses
  133. globtype,systems,
  134. cutils,verbose,globals,
  135. symtable,
  136. defutil,defcmp,
  137. nbas,ncnv,nld,nmem,ncal,nmat,ninl,nutils,
  138. cgbase,procinfo
  139. ;
  140. type
  141. TValidAssign=(Valid_Property,Valid_Void,Valid_Const,Valid_Addr);
  142. TValidAssigns=set of TValidAssign;
  143. function node2opstr(nt:tnodetype):string;
  144. var
  145. i : integer;
  146. begin
  147. result:='<unknown>';
  148. for i:=1 to tok2nodes do
  149. if tok2node[i].nod=nt then
  150. begin
  151. result:=tokeninfo^[tok2node[i].tok].str;
  152. break;
  153. end;
  154. end;
  155. function isbinaryoperatoroverloadable(treetyp:tnodetype;ld:tdef;lt:tnodetype;rd:tdef;rt:tnodetype) : boolean;
  156. function internal_check(treetyp:tnodetype;ld:tdef;lt:tnodetype;rd:tdef;rt:tnodetype;var allowed:boolean):boolean;
  157. begin
  158. internal_check:=true;
  159. case ld.deftype of
  160. formaldef,
  161. recorddef,
  162. variantdef :
  163. begin
  164. allowed:=true;
  165. end;
  166. procvardef :
  167. begin
  168. if (rd.deftype in [pointerdef,procdef,procvardef]) then
  169. begin
  170. allowed:=false;
  171. exit;
  172. end;
  173. allowed:=true;
  174. end;
  175. pointerdef :
  176. begin
  177. if ((rd.deftype in [orddef,enumdef,pointerdef,classrefdef,procvardef]) or
  178. is_class_or_interface(rd)) then
  179. begin
  180. allowed:=false;
  181. exit;
  182. end;
  183. { don't allow pchar+string }
  184. if (is_pchar(ld) or is_pwidechar(ld)) and
  185. ((rd.deftype=stringdef) or
  186. is_pchar(rd) or
  187. is_pwidechar(rd) or
  188. is_chararray(rd) or
  189. is_widechararray(rd)) then
  190. begin
  191. allowed:=false;
  192. exit;
  193. end;
  194. allowed:=true;
  195. end;
  196. arraydef :
  197. begin
  198. { not mmx }
  199. if (cs_mmx in aktlocalswitches) and
  200. is_mmx_able_array(ld) then
  201. begin
  202. allowed:=false;
  203. exit;
  204. end;
  205. { not chararray+[(wide)char,(wide)string,(wide)chararray] }
  206. if (is_chararray(ld) or is_widechararray(ld) or
  207. is_open_chararray(ld) or is_open_widechararray(ld))
  208. and
  209. ((rd.deftype in [stringdef,orddef,enumdef]) or
  210. is_pchar(rd) or
  211. is_pwidechar(rd) or
  212. is_chararray(rd) or
  213. is_widechararray(rd) or
  214. is_open_chararray(rd) or
  215. is_open_widechararray(rd) or
  216. (rt=niln)) then
  217. begin
  218. allowed:=false;
  219. exit;
  220. end;
  221. { dynamic array compare with niln }
  222. if ((is_dynamic_array(ld) and
  223. (rt=niln)) or
  224. (is_dynamic_array(ld) and is_dynamic_array(rd)))
  225. and
  226. (treetyp in [equaln,unequaln]) then
  227. begin
  228. allowed:=false;
  229. exit;
  230. end;
  231. allowed:=true;
  232. end;
  233. objectdef :
  234. begin
  235. { <> and = are defined for classes }
  236. if (treetyp in [equaln,unequaln]) and
  237. is_class_or_interface(ld) then
  238. begin
  239. allowed:=false;
  240. exit;
  241. end;
  242. allowed:=true;
  243. end;
  244. stringdef :
  245. begin
  246. if (rd.deftype in [orddef,enumdef,stringdef]) or
  247. is_pchar(rd) or
  248. is_pwidechar(rd) or
  249. is_chararray(rd) or
  250. is_widechararray(rd) or
  251. is_open_chararray(rd) or
  252. is_open_widechararray(rd) then
  253. begin
  254. allowed:=false;
  255. exit;
  256. end;
  257. allowed:=true;
  258. end;
  259. else
  260. internal_check:=false;
  261. end;
  262. end;
  263. var
  264. allowed : boolean;
  265. begin
  266. { power ** is always possible }
  267. if (treetyp=starstarn) then
  268. begin
  269. isbinaryoperatoroverloadable:=true;
  270. exit;
  271. end;
  272. { order of arguments does not matter so we have to check also
  273. the reversed order }
  274. allowed:=false;
  275. if not internal_check(treetyp,ld,lt,rd,rt,allowed) then
  276. internal_check(treetyp,rd,rt,ld,lt,allowed);
  277. isbinaryoperatoroverloadable:=allowed;
  278. end;
  279. function isunaryoperatoroverloadable(treetyp : tnodetype;ld : tdef) : boolean;
  280. begin
  281. result:=false;
  282. case treetyp of
  283. subn,
  284. unaryminusn :
  285. begin
  286. if (ld.deftype in [orddef,enumdef,floatdef]) then
  287. exit;
  288. {$ifdef SUPPORT_MMX}
  289. if (cs_mmx in aktlocalswitches) and
  290. is_mmx_able_array(ld) then
  291. exit;
  292. {$endif SUPPORT_MMX}
  293. result:=true;
  294. end;
  295. notn :
  296. begin
  297. if (ld.deftype in [orddef,enumdef,floatdef]) then
  298. exit;
  299. {$ifdef SUPPORT_MMX}
  300. if (cs_mmx in aktlocalswitches) and
  301. is_mmx_able_array(ld) then
  302. exit;
  303. {$endif SUPPORT_MMX}
  304. result:=true;
  305. end;
  306. end;
  307. end;
  308. function isoperatoracceptable(pf : tprocdef; optoken : ttoken) : boolean;
  309. var
  310. ld,rd : tdef;
  311. i : longint;
  312. eq : tequaltype;
  313. conv : tconverttype;
  314. pd : tprocdef;
  315. begin
  316. result:=false;
  317. case pf.parast.symindex.count of
  318. 1 : begin
  319. ld:=tparavarsym(pf.parast.symindex.first).vartype.def;
  320. { assignment is a special case }
  321. if optoken=_ASSIGNMENT then
  322. begin
  323. eq:=compare_defs_ext(ld,pf.rettype.def,nothingn,conv,pd,[cdo_explicit]);
  324. result:=(eq=te_incompatible);
  325. end
  326. else
  327. begin
  328. for i:=1 to tok2nodes do
  329. if tok2node[i].tok=optoken then
  330. begin
  331. result:=
  332. tok2node[i].op_overloading_supported and
  333. isunaryoperatoroverloadable(tok2node[i].nod,ld);
  334. break;
  335. end;
  336. end;
  337. end;
  338. 2 : begin
  339. for i:=1 to tok2nodes do
  340. if tok2node[i].tok=optoken then
  341. begin
  342. ld:=tparavarsym(pf.parast.symindex.first).vartype.def;
  343. rd:=tparavarsym(pf.parast.symindex.first.indexnext).vartype.def;
  344. result:=
  345. tok2node[i].op_overloading_supported and
  346. isbinaryoperatoroverloadable(tok2node[i].nod,ld,nothingn,rd,nothingn);
  347. break;
  348. end;
  349. end;
  350. end;
  351. end;
  352. function isunaryoverloaded(var t : tnode) : boolean;
  353. var
  354. ld : tdef;
  355. optoken : ttoken;
  356. operpd : tprocdef;
  357. ppn : tcallparanode;
  358. candidates : tcallcandidates;
  359. cand_cnt : integer;
  360. begin
  361. result:=false;
  362. operpd:=nil;
  363. { load easier access variables }
  364. ld:=tunarynode(t).left.resulttype.def;
  365. if not isunaryoperatoroverloadable(t.nodetype,ld) then
  366. exit;
  367. { operator overload is possible }
  368. result:=true;
  369. case t.nodetype of
  370. notn:
  371. optoken:=_OP_NOT;
  372. unaryminusn:
  373. optoken:=_MINUS;
  374. else
  375. begin
  376. CGMessage(parser_e_operator_not_overloaded);
  377. t:=cnothingnode.create;
  378. exit;
  379. end;
  380. end;
  381. { generate parameter nodes }
  382. ppn:=ccallparanode.create(tunarynode(t).left.getcopy,nil);
  383. ppn.get_paratype;
  384. candidates:=tcallcandidates.create_operator(optoken,ppn);
  385. { stop when there are no operators found }
  386. if candidates.count=0 then
  387. begin
  388. CGMessage(parser_e_operator_not_overloaded);
  389. candidates.free;
  390. ppn.free;
  391. t:=cnothingnode.create;
  392. exit;
  393. end;
  394. { Retrieve information about the candidates }
  395. candidates.get_information;
  396. {$ifdef EXTDEBUG}
  397. { Display info when multiple candidates are found }
  398. candidates.dump_info(V_Debug);
  399. {$endif EXTDEBUG}
  400. cand_cnt:=candidates.choose_best(operpd);
  401. { exit when no overloads are found }
  402. if cand_cnt=0 then
  403. begin
  404. CGMessage(parser_e_operator_not_overloaded);
  405. candidates.free;
  406. ppn.free;
  407. t:=cnothingnode.create;
  408. exit;
  409. end;
  410. { Multiple candidates left? }
  411. if cand_cnt>1 then
  412. begin
  413. CGMessage(type_e_cant_choose_overload_function);
  414. {$ifdef EXTDEBUG}
  415. candidates.dump_info(V_Hint);
  416. {$else EXTDEBUG}
  417. candidates.list(false);
  418. {$endif EXTDEBUG}
  419. { we'll just use the first candidate to make the
  420. call }
  421. end;
  422. candidates.free;
  423. inc(operpd.procsym.refs);
  424. { the nil as symtable signs firstcalln that this is
  425. an overloaded operator }
  426. t:=ccallnode.create(ppn,Tprocsym(operpd.procsym),nil,nil,[]);
  427. { we already know the procdef to use, so it can
  428. skip the overload choosing in callnode.det_resulttype }
  429. tcallnode(t).procdefinition:=operpd;
  430. end;
  431. function isbinaryoverloaded(var t : tnode) : boolean;
  432. var
  433. rd,ld : tdef;
  434. optoken : ttoken;
  435. operpd : tprocdef;
  436. ht : tnode;
  437. ppn : tcallparanode;
  438. candidates : tcallcandidates;
  439. cand_cnt : integer;
  440. begin
  441. isbinaryoverloaded:=false;
  442. operpd:=nil;
  443. { load easier access variables }
  444. ld:=tbinarynode(t).left.resulttype.def;
  445. rd:=tbinarynode(t).right.resulttype.def;
  446. if not isbinaryoperatoroverloadable(t.nodetype,ld,tbinarynode(t).left.nodetype,rd,tbinarynode(t).right.nodetype) then
  447. exit;
  448. { operator overload is possible }
  449. result:=true;
  450. case t.nodetype of
  451. equaln,
  452. unequaln :
  453. optoken:=_EQUAL;
  454. addn:
  455. optoken:=_PLUS;
  456. subn:
  457. optoken:=_MINUS;
  458. muln:
  459. optoken:=_STAR;
  460. starstarn:
  461. optoken:=_STARSTAR;
  462. slashn:
  463. optoken:=_SLASH;
  464. ltn:
  465. optoken:=_LT;
  466. gtn:
  467. optoken:=_GT;
  468. lten:
  469. optoken:=_LTE;
  470. gten:
  471. optoken:=_GTE;
  472. symdifn :
  473. optoken:=_SYMDIF;
  474. modn :
  475. optoken:=_OP_MOD;
  476. orn :
  477. optoken:=_OP_OR;
  478. xorn :
  479. optoken:=_OP_XOR;
  480. andn :
  481. optoken:=_OP_AND;
  482. divn :
  483. optoken:=_OP_DIV;
  484. shln :
  485. optoken:=_OP_SHL;
  486. shrn :
  487. optoken:=_OP_SHR;
  488. else
  489. begin
  490. CGMessage(parser_e_operator_not_overloaded);
  491. t:=cnothingnode.create;
  492. exit;
  493. end;
  494. end;
  495. { generate parameter nodes }
  496. ppn:=ccallparanode.create(tbinarynode(t).right.getcopy,ccallparanode.create(tbinarynode(t).left.getcopy,nil));
  497. ppn.get_paratype;
  498. candidates:=tcallcandidates.create_operator(optoken,ppn);
  499. { for commutative operators we can swap arguments and try again }
  500. if (candidates.count=0) and
  501. not(optoken in [_OP_SHL,_OP_SHR,_OP_DIV,_OP_MOD,_STARSTAR,_SLASH,_MINUS]) then
  502. begin
  503. candidates.free;
  504. reverseparameters(ppn);
  505. { reverse compare operators }
  506. case optoken of
  507. _LT:
  508. optoken:=_GTE;
  509. _GT:
  510. optoken:=_LTE;
  511. _LTE:
  512. optoken:=_GT;
  513. _GTE:
  514. optoken:=_LT;
  515. end;
  516. candidates:=tcallcandidates.create_operator(optoken,ppn);
  517. end;
  518. { stop when there are no operators found }
  519. if candidates.count=0 then
  520. begin
  521. CGMessage(parser_e_operator_not_overloaded);
  522. candidates.free;
  523. ppn.free;
  524. t:=cnothingnode.create;
  525. exit;
  526. end;
  527. { Retrieve information about the candidates }
  528. candidates.get_information;
  529. {$ifdef EXTDEBUG}
  530. { Display info when multiple candidates are found }
  531. candidates.dump_info(V_Debug);
  532. {$endif EXTDEBUG}
  533. cand_cnt:=candidates.choose_best(operpd);
  534. { exit when no overloads are found }
  535. if cand_cnt=0 then
  536. begin
  537. CGMessage(parser_e_operator_not_overloaded);
  538. candidates.free;
  539. ppn.free;
  540. t:=cnothingnode.create;
  541. exit;
  542. end;
  543. { Multiple candidates left? }
  544. if cand_cnt>1 then
  545. begin
  546. CGMessage(type_e_cant_choose_overload_function);
  547. {$ifdef EXTDEBUG}
  548. candidates.dump_info(V_Hint);
  549. {$else EXTDEBUG}
  550. candidates.list(false);
  551. {$endif EXTDEBUG}
  552. { we'll just use the first candidate to make the
  553. call }
  554. end;
  555. candidates.free;
  556. inc(operpd.procsym.refs);
  557. { the nil as symtable signs firstcalln that this is
  558. an overloaded operator }
  559. ht:=ccallnode.create(ppn,Tprocsym(operpd.procsym),nil,nil,[]);
  560. { we already know the procdef to use, so it can
  561. skip the overload choosing in callnode.det_resulttype }
  562. tcallnode(ht).procdefinition:=operpd;
  563. if t.nodetype=unequaln then
  564. ht:=cnotnode.create(ht);
  565. t:=ht;
  566. end;
  567. {****************************************************************************
  568. Register Calculation
  569. ****************************************************************************}
  570. { marks an lvalue as "unregable" }
  571. procedure make_not_regable(p : tnode);
  572. begin
  573. case p.nodetype of
  574. typeconvn :
  575. make_not_regable(ttypeconvnode(p).left);
  576. loadn :
  577. if tloadnode(p).symtableentry.typ in [globalvarsym,localvarsym,paravarsym] then
  578. tabstractvarsym(tloadnode(p).symtableentry).varregable:=vr_none;
  579. end;
  580. end;
  581. { calculates the needed registers for a binary operator }
  582. procedure calcregisters(p : tbinarynode;r32,fpu,mmx : word);
  583. begin
  584. p.left_right_max;
  585. { Only when the difference between the left and right registers < the
  586. wanted registers allocate the amount of registers }
  587. if assigned(p.left) then
  588. begin
  589. if assigned(p.right) then
  590. begin
  591. { the location must be already filled in because we need it to }
  592. { calculate the necessary number of registers (JM) }
  593. if p.expectloc = LOC_INVALID then
  594. internalerror(200110101);
  595. if (abs(p.left.registersint-p.right.registersint)<r32) or
  596. ((p.expectloc = LOC_FPUREGISTER) and
  597. (p.right.registersfpu <= p.left.registersfpu) and
  598. ((p.right.registersfpu <> 0) or (p.left.registersfpu <> 0)) and
  599. (p.left.registersint < p.right.registersint)) then
  600. inc(p.registersint,r32);
  601. if (abs(p.left.registersfpu-p.right.registersfpu)<fpu) then
  602. inc(p.registersfpu,fpu);
  603. {$ifdef SUPPORT_MMX}
  604. if (abs(p.left.registersmmx-p.right.registersmmx)<mmx) then
  605. inc(p.registersmmx,mmx);
  606. {$endif SUPPORT_MMX}
  607. { the following is a little bit guessing but I think }
  608. { it's the only way to solve same internalerrors: }
  609. { if the left and right node both uses registers }
  610. { and return a mem location, but the current node }
  611. { doesn't use an integer register we get probably }
  612. { trouble when restoring a node }
  613. if (p.left.registersint=p.right.registersint) and
  614. (p.registersint=p.left.registersint) and
  615. (p.registersint>0) and
  616. (p.left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE]) and
  617. (p.right.expectloc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  618. inc(p.registersint);
  619. end
  620. else
  621. begin
  622. if (p.left.registersint<r32) then
  623. inc(p.registersint,r32);
  624. if (p.left.registersfpu<fpu) then
  625. inc(p.registersfpu,fpu);
  626. {$ifdef SUPPORT_MMX}
  627. if (p.left.registersmmx<mmx) then
  628. inc(p.registersmmx,mmx);
  629. {$endif SUPPORT_MMX}
  630. end;
  631. end;
  632. end;
  633. {****************************************************************************
  634. Subroutine Handling
  635. ****************************************************************************}
  636. function is_procvar_load(p:tnode):boolean;
  637. begin
  638. result:=false;
  639. { remove voidpointer typecast for tp procvars }
  640. if ((m_tp_procvar in aktmodeswitches) or
  641. (m_mac_procvar in aktmodeswitches)) and
  642. (p.nodetype=typeconvn) and
  643. is_voidpointer(p.resulttype.def) then
  644. p:=tunarynode(p).left;
  645. result:=(p.nodetype=typeconvn) and
  646. (ttypeconvnode(p).convtype=tc_proc_2_procvar);
  647. end;
  648. { local routines can't be assigned to procvars }
  649. procedure test_local_to_procvar(from_def:tprocvardef;to_def:tdef);
  650. begin
  651. if (from_def.parast.symtablelevel>normal_function_level) and
  652. (to_def.deftype=procvardef) then
  653. CGMessage(type_e_cannot_local_proc_to_procvar);
  654. end;
  655. procedure set_varstate(p:tnode;newstate:tvarstate;varstateflags:tvarstateflags);
  656. var
  657. hsym : tabstractvarsym;
  658. begin
  659. while assigned(p) do
  660. begin
  661. case p.nodetype of
  662. typeconvn :
  663. begin
  664. case ttypeconvnode(p).convtype of
  665. tc_cchar_2_pchar,
  666. tc_cstring_2_pchar,
  667. tc_array_2_pointer :
  668. exclude(varstateflags,vsf_must_be_valid);
  669. tc_pchar_2_string,
  670. tc_pointer_2_array :
  671. include(varstateflags,vsf_must_be_valid);
  672. end;
  673. p:=tunarynode(p).left;
  674. end;
  675. subscriptn :
  676. p:=tunarynode(p).left;
  677. vecn:
  678. begin
  679. set_varstate(tbinarynode(p).right,vs_used,[vsf_must_be_valid]);
  680. if not(tunarynode(p).left.resulttype.def.deftype in [stringdef,arraydef]) then
  681. include(varstateflags,vsf_must_be_valid);
  682. p:=tunarynode(p).left;
  683. end;
  684. { do not parse calln }
  685. calln :
  686. break;
  687. loadn :
  688. begin
  689. if (tloadnode(p).symtableentry.typ in [localvarsym,paravarsym,globalvarsym]) then
  690. begin
  691. hsym:=tabstractvarsym(tloadnode(p).symtableentry);
  692. if (vsf_must_be_valid in varstateflags) and (hsym.varstate=vs_declared) then
  693. begin
  694. { Give warning/note for uninitialized locals }
  695. if assigned(hsym.owner) and
  696. not(vo_is_external in hsym.varoptions) and
  697. (hsym.owner.symtabletype in [localsymtable,staticsymtable]) and
  698. (hsym.owner=current_procinfo.procdef.localst) then
  699. begin
  700. if (vo_is_funcret in hsym.varoptions) then
  701. CGMessage(sym_w_function_result_not_set)
  702. else
  703. begin
  704. if tloadnode(p).symtable.symtabletype=localsymtable then
  705. begin
  706. if (vsf_use_hints in varstateflags) then
  707. CGMessage1(sym_h_uninitialized_local_variable,hsym.realname)
  708. else
  709. CGMessage1(sym_w_uninitialized_local_variable,hsym.realname);
  710. end
  711. else
  712. begin
  713. if (vsf_use_hints in varstateflags) then
  714. CGMessage1(sym_h_uninitialized_variable,hsym.realname)
  715. else
  716. CGMessage1(sym_w_uninitialized_variable,hsym.realname);
  717. end;
  718. end;
  719. end;
  720. end;
  721. { don't override vs_used with vs_assigned }
  722. if hsym.varstate<>vs_used then
  723. hsym.varstate:=newstate;
  724. end;
  725. break;
  726. end;
  727. callparan :
  728. internalerror(200310081);
  729. else
  730. break;
  731. end;{case }
  732. end;
  733. end;
  734. procedure set_unique(p : tnode);
  735. begin
  736. while assigned(p) do
  737. begin
  738. case p.nodetype of
  739. vecn:
  740. begin
  741. include(p.flags,nf_callunique);
  742. break;
  743. end;
  744. typeconvn,
  745. subscriptn,
  746. derefn:
  747. p:=tunarynode(p).left;
  748. else
  749. break;
  750. end;
  751. end;
  752. end;
  753. function valid_for_assign(p:tnode;opts:TValidAssigns):boolean;
  754. var
  755. hp : tnode;
  756. gotstring,
  757. gotwith,
  758. gotsubscript,
  759. gotrecord,
  760. gotpointer,
  761. gotvec,
  762. gotclass,
  763. gotdynarray,
  764. gotderef : boolean;
  765. fromdef,
  766. todef : tdef;
  767. errmsg : longint;
  768. begin
  769. if valid_const in opts then
  770. errmsg:=type_e_variable_id_expected
  771. else
  772. errmsg:=type_e_argument_cant_be_assigned;
  773. result:=false;
  774. gotsubscript:=false;
  775. gotvec:=false;
  776. gotderef:=false;
  777. gotrecord:=false;
  778. gotclass:=false;
  779. gotpointer:=false;
  780. gotwith:=false;
  781. gotdynarray:=false;
  782. gotstring:=false;
  783. hp:=p;
  784. if not(valid_void in opts) and
  785. is_void(hp.resulttype.def) then
  786. begin
  787. CGMessagePos(hp.fileinfo,errmsg);
  788. exit;
  789. end;
  790. while assigned(hp) do
  791. begin
  792. { property allowed? calln has a property check itself }
  793. if (nf_isproperty in hp.flags) then
  794. begin
  795. if (hp.nodetype=calln) then
  796. begin
  797. { check return type }
  798. case hp.resulttype.def.deftype of
  799. pointerdef :
  800. gotpointer:=true;
  801. objectdef :
  802. gotclass:=is_class_or_interface(hp.resulttype.def);
  803. recorddef :
  804. gotrecord:=true;
  805. classrefdef :
  806. gotclass:=true;
  807. stringdef :
  808. gotstring:=true;
  809. end;
  810. if (valid_property in opts) then
  811. begin
  812. { don't allow writing to calls that will create
  813. temps like calls that return a structure and we
  814. are assigning to a member }
  815. if (valid_const in opts) or
  816. not(
  817. (gotsubscript and gotrecord) or
  818. (gotstring and gotvec)
  819. ) then
  820. result:=true
  821. else
  822. CGMessagePos(hp.fileinfo,errmsg);
  823. end
  824. else
  825. begin
  826. { 1. if it returns a pointer and we've found a deref,
  827. 2. if it returns a class or record and a subscription or with is found
  828. 3. if the address is needed of a field (subscriptn) }
  829. if (gotpointer and gotderef) or
  830. (gotstring and gotvec) or
  831. (
  832. (gotclass or gotrecord) and
  833. (gotsubscript or gotwith)
  834. ) or
  835. (
  836. (gotvec and gotdynarray)
  837. ) or
  838. (
  839. (Valid_Addr in opts) and
  840. (hp.nodetype=subscriptn)
  841. ) then
  842. result:=true
  843. else
  844. CGMessagePos(hp.fileinfo,errmsg);
  845. end;
  846. end
  847. else
  848. result:=true;
  849. exit;
  850. end;
  851. if (Valid_Const in opts) and is_constnode(hp) then
  852. begin
  853. result:=true;
  854. exit;
  855. end;
  856. case hp.nodetype of
  857. temprefn :
  858. begin
  859. valid_for_assign := true;
  860. exit;
  861. end;
  862. derefn :
  863. begin
  864. gotderef:=true;
  865. hp:=tderefnode(hp).left;
  866. end;
  867. typeconvn :
  868. begin
  869. { typecast sizes must match, exceptions:
  870. - implicit typecast made by absolute
  871. - from formaldef
  872. - from void
  873. - from/to open array
  874. - typecast from pointer to array }
  875. fromdef:=ttypeconvnode(hp).left.resulttype.def;
  876. todef:=hp.resulttype.def;
  877. if not((nf_absolute in ttypeconvnode(hp).flags) or
  878. (fromdef.deftype=formaldef) or
  879. is_void(fromdef) or
  880. is_open_array(fromdef) or
  881. is_open_array(todef) or
  882. ((fromdef.deftype=pointerdef) and (todef.deftype=arraydef)) or
  883. ((fromdef.deftype = objectdef) and (todef.deftype = objectdef) and
  884. (tobjectdef(fromdef).is_related(tobjectdef(todef))))) and
  885. (fromdef.size<>todef.size) then
  886. begin
  887. { in TP it is allowed to typecast to smaller types. But the variable can't
  888. be in a register }
  889. if (m_tp7 in aktmodeswitches) or
  890. (todef.size<fromdef.size) then
  891. make_not_regable(hp)
  892. else
  893. CGMessagePos2(hp.fileinfo,type_e_typecast_wrong_size_for_assignment,tostr(fromdef.size),tostr(todef.size));
  894. end;
  895. { don't allow assignments to typeconvs that need special code }
  896. if not(gotsubscript or gotvec or gotderef) and
  897. not(ttypeconvnode(hp).assign_allowed) then
  898. begin
  899. CGMessagePos(hp.fileinfo,errmsg);
  900. exit;
  901. end;
  902. case hp.resulttype.def.deftype of
  903. pointerdef :
  904. gotpointer:=true;
  905. objectdef :
  906. gotclass:=is_class_or_interface(hp.resulttype.def);
  907. classrefdef :
  908. gotclass:=true;
  909. arraydef :
  910. begin
  911. { pointer -> array conversion is done then we need to see it
  912. as a deref, because a ^ is then not required anymore }
  913. if (ttypeconvnode(hp).left.resulttype.def.deftype=pointerdef) then
  914. gotderef:=true;
  915. end;
  916. end;
  917. hp:=ttypeconvnode(hp).left;
  918. end;
  919. vecn :
  920. begin
  921. gotvec:=true;
  922. { accesses to dyn. arrays override read only access in delphi }
  923. if (m_delphi in aktmodeswitches) and is_dynamic_array(tunarynode(hp).left.resulttype.def) then
  924. gotdynarray:=true;
  925. hp:=tunarynode(hp).left;
  926. end;
  927. asn :
  928. begin
  929. { asn can't be assigned directly, it returns the value in a register instead
  930. of reference. }
  931. if not(gotsubscript or gotderef or gotvec) then
  932. begin
  933. CGMessagePos(hp.fileinfo,errmsg);
  934. exit;
  935. end;
  936. hp:=tunarynode(hp).left;
  937. end;
  938. subscriptn :
  939. begin
  940. gotsubscript:=true;
  941. { loop counter? }
  942. if not(Valid_Const in opts) and
  943. (vo_is_loop_counter in tsubscriptnode(hp).vs.varoptions) then
  944. CGMessage1(parser_e_illegal_assignment_to_count_var,tsubscriptnode(hp).vs.realname);
  945. { a class/interface access is an implicit }
  946. { dereferencing }
  947. hp:=tsubscriptnode(hp).left;
  948. if is_class_or_interface(hp.resulttype.def) then
  949. gotderef:=true;
  950. end;
  951. muln,
  952. divn,
  953. andn,
  954. xorn,
  955. orn,
  956. notn,
  957. subn,
  958. addn :
  959. begin
  960. { Allow operators on a pointer, or an integer
  961. and a pointer typecast and deref has been found }
  962. if ((hp.resulttype.def.deftype=pointerdef) or
  963. (is_integer(hp.resulttype.def) and gotpointer)) and
  964. gotderef then
  965. result:=true
  966. else
  967. { Temp strings are stored in memory, for compatibility with
  968. delphi only }
  969. if (m_delphi in aktmodeswitches) and
  970. ((valid_addr in opts) or
  971. (valid_const in opts)) and
  972. (hp.resulttype.def.deftype=stringdef) then
  973. result:=true
  974. else
  975. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  976. exit;
  977. end;
  978. niln,
  979. pointerconstn :
  980. begin
  981. { to support e.g. @tmypointer(0)^.data; see tests/tbs/tb0481 }
  982. if gotderef then
  983. result:=true
  984. else
  985. CGMessagePos(hp.fileinfo,type_e_no_assign_to_addr);
  986. exit;
  987. end;
  988. addrn :
  989. begin
  990. if gotderef then
  991. result:=true
  992. else
  993. CGMessagePos(hp.fileinfo,type_e_no_assign_to_addr);
  994. exit;
  995. end;
  996. calln :
  997. begin
  998. { check return type }
  999. case hp.resulttype.def.deftype of
  1000. arraydef :
  1001. begin
  1002. { dynamic arrays are allowed when there is also a
  1003. vec node }
  1004. if is_dynamic_array(hp.resulttype.def) and
  1005. gotvec then
  1006. begin
  1007. gotderef:=true;
  1008. gotpointer:=true;
  1009. end;
  1010. end;
  1011. pointerdef :
  1012. gotpointer:=true;
  1013. objectdef :
  1014. gotclass:=is_class_or_interface(hp.resulttype.def);
  1015. recorddef, { handle record like class it needs a subscription }
  1016. classrefdef :
  1017. gotclass:=true;
  1018. stringdef :
  1019. gotstring:=true;
  1020. end;
  1021. { 1. if it returns a pointer and we've found a deref,
  1022. 2. if it returns a class or record and a subscription or with is found
  1023. 3. string is returned }
  1024. if (gotstring and gotvec) or
  1025. (gotpointer and gotderef) or
  1026. (gotclass and (gotsubscript or gotwith)) then
  1027. result:=true
  1028. else
  1029. { Temp strings are stored in memory, for compatibility with
  1030. delphi only }
  1031. if (m_delphi in aktmodeswitches) and
  1032. (valid_addr in opts) and
  1033. (hp.resulttype.def.deftype=stringdef) then
  1034. result:=true
  1035. else
  1036. if ([valid_const,valid_addr] * opts = [valid_const]) then
  1037. result:=true
  1038. else
  1039. CGMessagePos(hp.fileinfo,errmsg);
  1040. exit;
  1041. end;
  1042. inlinen :
  1043. begin
  1044. if (valid_const in opts) and
  1045. (tinlinenode(hp).inlinenumber in [in_typeof_x]) then
  1046. result:=true
  1047. else
  1048. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1049. exit;
  1050. end;
  1051. loadn :
  1052. begin
  1053. case tloadnode(hp).symtableentry.typ of
  1054. absolutevarsym,
  1055. globalvarsym,
  1056. localvarsym,
  1057. paravarsym :
  1058. begin
  1059. { loop counter? }
  1060. if not(Valid_Const in opts) and
  1061. not gotderef and
  1062. (vo_is_loop_counter in tabstractvarsym(tloadnode(hp).symtableentry).varoptions) then
  1063. CGMessage1(parser_e_illegal_assignment_to_count_var,tloadnode(hp).symtableentry.realname);
  1064. { derefed pointer }
  1065. if (tabstractvarsym(tloadnode(hp).symtableentry).varspez=vs_const) then
  1066. begin
  1067. { allow p^:= constructions with p is const parameter }
  1068. if gotderef or gotdynarray or (Valid_Const in opts) then
  1069. result:=true
  1070. else
  1071. CGMessagePos(tloadnode(hp).fileinfo,type_e_no_assign_to_const);
  1072. exit;
  1073. end;
  1074. { Are we at a with symtable, then we need to process the
  1075. withrefnode also to check for maybe a const load }
  1076. if (tloadnode(hp).symtable.symtabletype=withsymtable) then
  1077. begin
  1078. { continue with processing the withref node }
  1079. hp:=tnode(twithsymtable(tloadnode(hp).symtable).withrefnode);
  1080. gotwith:=true;
  1081. end
  1082. else
  1083. begin
  1084. result:=true;
  1085. exit;
  1086. end;
  1087. end;
  1088. typedconstsym :
  1089. begin
  1090. if ttypedconstsym(tloadnode(hp).symtableentry).is_writable or
  1091. (valid_addr in opts) or
  1092. (valid_const in opts) then
  1093. result:=true
  1094. else
  1095. CGMessagePos(hp.fileinfo,type_e_no_assign_to_const);
  1096. exit;
  1097. end;
  1098. procsym :
  1099. begin
  1100. if (Valid_Const in opts) then
  1101. result:=true
  1102. else
  1103. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1104. exit;
  1105. end;
  1106. labelsym :
  1107. begin
  1108. if (Valid_Addr in opts) then
  1109. result:=true
  1110. else
  1111. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1112. exit;
  1113. end;
  1114. constsym:
  1115. begin
  1116. if (tconstsym(tloadnode(hp).symtableentry).consttyp=constresourcestring) and
  1117. (valid_addr in opts) then
  1118. result:=true
  1119. else
  1120. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1121. exit;
  1122. end;
  1123. else
  1124. begin
  1125. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1126. exit;
  1127. end;
  1128. end;
  1129. end;
  1130. else
  1131. begin
  1132. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  1133. exit;
  1134. end;
  1135. end;
  1136. end;
  1137. end;
  1138. function valid_for_var(p:tnode):boolean;
  1139. begin
  1140. valid_for_var:=valid_for_assign(p,[]);
  1141. end;
  1142. function valid_for_formal_var(p : tnode) : boolean;
  1143. begin
  1144. valid_for_formal_var:=valid_for_assign(p,[valid_void]);
  1145. end;
  1146. function valid_for_formal_const(p : tnode) : boolean;
  1147. begin
  1148. valid_for_formal_const:=(p.resulttype.def.deftype=formaldef) or
  1149. valid_for_assign(p,[valid_void,valid_const]);
  1150. end;
  1151. function valid_for_assignment(p:tnode):boolean;
  1152. begin
  1153. valid_for_assignment:=valid_for_assign(p,[valid_property]);
  1154. end;
  1155. function valid_for_addr(p : tnode) : boolean;
  1156. begin
  1157. result:=valid_for_assign(p,[valid_const,valid_addr,valid_void]);
  1158. end;
  1159. procedure var_para_allowed(var eq:tequaltype;def_from,def_to:Tdef);
  1160. begin
  1161. { Note: eq must be already valid, it will only be updated! }
  1162. case def_to.deftype of
  1163. formaldef :
  1164. begin
  1165. { all types can be passed to a formaldef }
  1166. eq:=te_equal;
  1167. end;
  1168. orddef :
  1169. begin
  1170. { allows conversion from word to integer and
  1171. byte to shortint, but only for TP7 compatibility }
  1172. if (m_tp7 in aktmodeswitches) and
  1173. (def_from.deftype=orddef) and
  1174. (def_from.size=def_to.size) then
  1175. eq:=te_convert_l1;
  1176. end;
  1177. arraydef :
  1178. begin
  1179. if is_open_array(def_to) and
  1180. is_dynamic_array(def_from) and
  1181. equal_defs(tarraydef(def_from).elementtype.def,tarraydef(def_to).elementtype.def) then
  1182. eq:=te_convert_l2;
  1183. end;
  1184. pointerdef :
  1185. begin
  1186. { an implicit pointer conversion is allowed }
  1187. if (def_from.deftype=pointerdef) then
  1188. eq:=te_convert_l1;
  1189. end;
  1190. stringdef :
  1191. begin
  1192. { all shortstrings are allowed, size is not important }
  1193. if is_shortstring(def_from) and
  1194. is_shortstring(def_to) then
  1195. eq:=te_equal;
  1196. end;
  1197. objectdef :
  1198. begin
  1199. { child objects can be also passed }
  1200. { in non-delphi mode, otherwise }
  1201. { they must match exactly, except }
  1202. { if they are objects }
  1203. if (def_from.deftype=objectdef) and
  1204. (
  1205. not(m_delphi in aktmodeswitches) or
  1206. (
  1207. (tobjectdef(def_from).objecttype=odt_object) and
  1208. (tobjectdef(def_to).objecttype=odt_object)
  1209. )
  1210. ) and
  1211. (tobjectdef(def_from).is_related(tobjectdef(def_to))) then
  1212. eq:=te_convert_l1;
  1213. end;
  1214. filedef :
  1215. begin
  1216. { an implicit file conversion is also allowed }
  1217. { from a typed file to an untyped one }
  1218. if (def_from.deftype=filedef) and
  1219. (tfiledef(def_from).filetyp = ft_typed) and
  1220. (tfiledef(def_to).filetyp = ft_untyped) then
  1221. eq:=te_convert_l1;
  1222. end;
  1223. end;
  1224. end;
  1225. procedure para_allowed(var eq:tequaltype;p:tcallparanode;def_to:tdef);
  1226. begin
  1227. { Note: eq must be already valid, it will only be updated! }
  1228. case def_to.deftype of
  1229. formaldef :
  1230. begin
  1231. { all types can be passed to a formaldef }
  1232. eq:=te_equal;
  1233. end;
  1234. stringdef :
  1235. begin
  1236. { to support ansi/long/wide strings in a proper way }
  1237. { string and string[10] are assumed as equal }
  1238. { when searching the correct overloaded procedure }
  1239. if (p.resulttype.def.deftype=stringdef) and
  1240. (tstringdef(def_to).string_typ=tstringdef(p.resulttype.def).string_typ) then
  1241. eq:=te_equal
  1242. else
  1243. { Passing a constant char to ansistring or shortstring or
  1244. a widechar to widestring then handle it as equal. }
  1245. if (p.left.nodetype=ordconstn) and
  1246. (
  1247. is_char(p.resulttype.def) and
  1248. (is_shortstring(def_to) or is_ansistring(def_to))
  1249. ) or
  1250. (
  1251. is_widechar(p.resulttype.def) and
  1252. is_widestring(def_to)
  1253. ) then
  1254. eq:=te_equal
  1255. end;
  1256. setdef :
  1257. begin
  1258. { set can also be a not yet converted array constructor }
  1259. if (p.resulttype.def.deftype=arraydef) and
  1260. (tarraydef(p.resulttype.def).IsConstructor) and
  1261. not(tarraydef(p.resulttype.def).IsVariant) then
  1262. eq:=te_equal;
  1263. end;
  1264. procvardef :
  1265. begin
  1266. { in tp7 mode proc -> procvar is allowed }
  1267. if ((m_tp_procvar in aktmodeswitches) or
  1268. (m_mac_procvar in aktmodeswitches)) and
  1269. (p.left.nodetype=calln) and
  1270. (proc_to_procvar_equal(tprocdef(tcallnode(p.left).procdefinition),tprocvardef(def_to))>=te_equal) then
  1271. eq:=te_equal
  1272. else
  1273. if (m_mac_procvar in aktmodeswitches) and
  1274. is_procvar_load(p.left) then
  1275. eq:=te_convert_l2;
  1276. end;
  1277. end;
  1278. end;
  1279. function allowenumop(nt:tnodetype):boolean;
  1280. begin
  1281. result:=(nt in [equaln,unequaln,ltn,lten,gtn,gten]) or
  1282. ((cs_allow_enum_calc in aktlocalswitches) and
  1283. (nt in [addn,subn]));
  1284. end;
  1285. {****************************************************************************
  1286. TCallCandidates
  1287. ****************************************************************************}
  1288. constructor tcallcandidates.create(sym:tprocsym;st:tsymtable;ppn:tnode;isprop,ignorevis : boolean);
  1289. var
  1290. j : integer;
  1291. pd : tprocdef;
  1292. hp : pcandidate;
  1293. found,
  1294. has_overload_directive : boolean;
  1295. topclassh : tobjectdef;
  1296. srsymtable : tsymtable;
  1297. srprocsym : tprocsym;
  1298. pt : tcallparanode;
  1299. begin
  1300. if not assigned(sym) then
  1301. internalerror(200411015);
  1302. FProcSym:=sym;
  1303. FProcs:=nil;
  1304. FProccnt:=0;
  1305. FProcvisiblecnt:=0;
  1306. FParanode:=ppn;
  1307. FAllowVariant:=true;
  1308. { determine length of parameter list }
  1309. pt:=tcallparanode(ppn);
  1310. FParalength:=0;
  1311. while assigned(pt) do
  1312. begin
  1313. inc(FParalength);
  1314. pt:=tcallparanode(pt.right);
  1315. end;
  1316. { when the definition has overload directive set, we search for
  1317. overloaded definitions in the class, this only needs to be done once
  1318. for class entries as the tree keeps always the same }
  1319. if (not sym.overloadchecked) and
  1320. (sym.owner.symtabletype=objectsymtable) and
  1321. (po_overload in sym.first_procdef.procoptions) then
  1322. search_class_overloads(sym);
  1323. { when the class passed is defined in this unit we
  1324. need to use the scope of that class. This is a trick
  1325. that can be used to access protected members in other
  1326. units. At least kylix supports it this way (PFV) }
  1327. if assigned(st) and
  1328. (
  1329. (st.symtabletype=objectsymtable) or
  1330. ((st.symtabletype=withsymtable) and
  1331. (st.defowner.deftype=objectdef))
  1332. ) and
  1333. (st.defowner.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1334. st.defowner.owner.iscurrentunit then
  1335. topclassh:=tobjectdef(st.defowner)
  1336. else
  1337. begin
  1338. if assigned(current_procinfo) then
  1339. topclassh:=current_procinfo.procdef._class
  1340. else
  1341. topclassh:=nil;
  1342. end;
  1343. { link all procedures which have the same # of parameters }
  1344. for j:=1 to sym.procdef_count do
  1345. begin
  1346. pd:=sym.procdef[j];
  1347. { Is the procdef visible? This needs to be checked on
  1348. procdef level since a symbol can contain both private and
  1349. public declarations. But the check should not be done
  1350. when the callnode is generated by a property
  1351. inherited overrides invisible anonymous inherited (FK) }
  1352. if isprop or ignorevis or
  1353. (pd.owner.symtabletype<>objectsymtable) or
  1354. pd.is_visible_for_object(topclassh) then
  1355. begin
  1356. { we have at least one procedure that is visible }
  1357. inc(FProcvisiblecnt);
  1358. { only when the # of parameter are supported by the
  1359. procedure }
  1360. if (FParalength>=pd.minparacount) and
  1361. ((po_varargs in pd.procoptions) or { varargs }
  1362. (FParalength<=pd.maxparacount)) then
  1363. proc_add(pd);
  1364. end;
  1365. end;
  1366. { remember if the procedure is declared with the overload directive,
  1367. it's information is still needed also after all procs are removed }
  1368. has_overload_directive:=(po_overload in sym.first_procdef.procoptions);
  1369. { when the definition has overload directive set, we search for
  1370. overloaded definitions in the symtablestack. The found
  1371. entries are only added to the procs list and not the procsym, because
  1372. the list can change in every situation }
  1373. if has_overload_directive and
  1374. (sym.owner.symtabletype<>objectsymtable) then
  1375. begin
  1376. srsymtable:=sym.owner.next;
  1377. while assigned(srsymtable) do
  1378. begin
  1379. if srsymtable.symtabletype in [localsymtable,staticsymtable,globalsymtable] then
  1380. begin
  1381. srprocsym:=tprocsym(srsymtable.speedsearch(sym.name,sym.speedvalue));
  1382. if assigned(srprocsym) and
  1383. (srprocsym.typ=procsym) then
  1384. begin
  1385. { if this visible procedure doesn't have overload we can stop
  1386. searching }
  1387. if not(po_overload in srprocsym.first_procdef.procoptions) and
  1388. srprocsym.first_procdef.is_visible_for_object(topclassh) then
  1389. break;
  1390. { process all overloaded definitions }
  1391. for j:=1 to srprocsym.procdef_count do
  1392. begin
  1393. pd:=srprocsym.procdef[j];
  1394. { only visible procedures need to be added }
  1395. if pd.is_visible_for_object(topclassh) then
  1396. begin
  1397. { only when the # of parameter are supported by the
  1398. procedure }
  1399. if (FParalength>=pd.minparacount) and
  1400. ((po_varargs in pd.procoptions) or { varargs }
  1401. (FParalength<=pd.maxparacount)) then
  1402. begin
  1403. found:=false;
  1404. hp:=FProcs;
  1405. while assigned(hp) do
  1406. begin
  1407. { Only compare visible parameters for the user }
  1408. if compare_paras(hp^.data.paras,pd.paras,cp_value_equal_const,[cpo_ignorehidden])>=te_equal then
  1409. begin
  1410. found:=true;
  1411. break;
  1412. end;
  1413. hp:=hp^.next;
  1414. end;
  1415. if not found then
  1416. proc_add(pd);
  1417. end;
  1418. end;
  1419. end;
  1420. end;
  1421. end;
  1422. srsymtable:=srsymtable.next;
  1423. end;
  1424. end;
  1425. end;
  1426. constructor tcallcandidates.create_operator(op:ttoken;ppn:tnode);
  1427. var
  1428. j : integer;
  1429. pd : tprocdef;
  1430. hp : pcandidate;
  1431. found : boolean;
  1432. srsymtable : tsymtable;
  1433. srprocsym : tprocsym;
  1434. pt : tcallparanode;
  1435. sv : cardinal;
  1436. begin
  1437. FProcSym:=nil;
  1438. FProcs:=nil;
  1439. FProccnt:=0;
  1440. FProcvisiblecnt:=0;
  1441. FParanode:=ppn;
  1442. FAllowVariant:=false;
  1443. { determine length of parameter list }
  1444. pt:=tcallparanode(ppn);
  1445. FParalength:=0;
  1446. while assigned(pt) do
  1447. begin
  1448. if pt.resulttype.def.deftype=variantdef then
  1449. FAllowVariant:=true;
  1450. inc(FParalength);
  1451. pt:=tcallparanode(pt.right);
  1452. end;
  1453. { we search all overloaded operator definitions in the symtablestack. The found
  1454. entries are only added to the procs list and not the procsym, because
  1455. the list can change in every situation }
  1456. sv:=getspeedvalue(overloaded_names[op]);
  1457. srsymtable:=symtablestack;
  1458. while assigned(srsymtable) do
  1459. begin
  1460. if srsymtable.symtabletype in [localsymtable,staticsymtable,globalsymtable] then
  1461. begin
  1462. srprocsym:=tprocsym(srsymtable.speedsearch(overloaded_names[op],sv));
  1463. if assigned(srprocsym) and
  1464. (srprocsym.typ=procsym) then
  1465. begin
  1466. { Store first procsym found }
  1467. if not assigned(FProcsym) then
  1468. FProcsym:=srprocsym;
  1469. { process all overloaded definitions }
  1470. for j:=1 to srprocsym.procdef_count do
  1471. begin
  1472. pd:=srprocsym.procdef[j];
  1473. { only when the # of parameter are supported by the
  1474. procedure }
  1475. if (FParalength>=pd.minparacount) and
  1476. (FParalength<=pd.maxparacount) then
  1477. begin
  1478. found:=false;
  1479. hp:=FProcs;
  1480. while assigned(hp) do
  1481. begin
  1482. { Only compare visible parameters for the user }
  1483. if compare_paras(hp^.data.paras,pd.paras,cp_value_equal_const,[cpo_ignorehidden])>=te_equal then
  1484. begin
  1485. found:=true;
  1486. break;
  1487. end;
  1488. hp:=hp^.next;
  1489. end;
  1490. if not found then
  1491. proc_add(pd);
  1492. end;
  1493. end;
  1494. end;
  1495. end;
  1496. srsymtable:=srsymtable.next;
  1497. end;
  1498. end;
  1499. destructor tcallcandidates.destroy;
  1500. var
  1501. hpnext,
  1502. hp : pcandidate;
  1503. begin
  1504. hp:=FProcs;
  1505. while assigned(hp) do
  1506. begin
  1507. hpnext:=hp^.next;
  1508. dispose(hp);
  1509. hp:=hpnext;
  1510. end;
  1511. end;
  1512. function tcallcandidates.proc_add(pd:tprocdef):pcandidate;
  1513. var
  1514. defaultparacnt : integer;
  1515. begin
  1516. { generate new candidate entry }
  1517. new(result);
  1518. fillchar(result^,sizeof(tcandidate),0);
  1519. result^.data:=pd;
  1520. result^.next:=FProcs;
  1521. FProcs:=result;
  1522. inc(FProccnt);
  1523. { Find last parameter, skip all default parameters
  1524. that are not passed. Ignore this skipping for varargs }
  1525. result^.firstparaidx:=pd.paras.count-1;
  1526. if not(po_varargs in pd.procoptions) then
  1527. begin
  1528. { ignore hidden parameters }
  1529. while (result^.firstparaidx>=0) and (vo_is_hidden_para in tparavarsym(pd.paras[result^.firstparaidx]).varoptions) do
  1530. dec(result^.firstparaidx);
  1531. defaultparacnt:=pd.maxparacount-FParalength;
  1532. if defaultparacnt>0 then
  1533. begin
  1534. if defaultparacnt>result^.firstparaidx+1 then
  1535. internalerror(200401141);
  1536. dec(result^.firstparaidx,defaultparacnt);
  1537. end;
  1538. end;
  1539. end;
  1540. procedure tcallcandidates.list(all:boolean);
  1541. var
  1542. hp : pcandidate;
  1543. begin
  1544. hp:=FProcs;
  1545. while assigned(hp) do
  1546. begin
  1547. if all or
  1548. (not hp^.invalid) then
  1549. MessagePos1(hp^.data.fileinfo,sym_h_param_list,hp^.data.fullprocname(false));
  1550. hp:=hp^.next;
  1551. end;
  1552. end;
  1553. {$ifdef EXTDEBUG}
  1554. procedure tcallcandidates.dump_info(lvl:longint);
  1555. function ParaTreeStr(p:tcallparanode):string;
  1556. begin
  1557. result:='';
  1558. while assigned(p) do
  1559. begin
  1560. if result<>'' then
  1561. result:=','+result;
  1562. result:=p.resulttype.def.typename+result;
  1563. p:=tcallparanode(p.right);
  1564. end;
  1565. end;
  1566. var
  1567. hp : pcandidate;
  1568. i : integer;
  1569. currpara : tparavarsym;
  1570. begin
  1571. if not CheckVerbosity(lvl) then
  1572. exit;
  1573. Comment(lvl+V_LineInfo,'Overloaded callnode: '+FProcSym.name+'('+ParaTreeStr(tcallparanode(FParaNode))+')');
  1574. hp:=FProcs;
  1575. while assigned(hp) do
  1576. begin
  1577. Comment(lvl,' '+hp^.data.fullprocname(false));
  1578. if (hp^.invalid) then
  1579. Comment(lvl,' invalid')
  1580. else
  1581. begin
  1582. Comment(lvl,' ex: '+tostr(hp^.exact_count)+
  1583. ' eq: '+tostr(hp^.equal_count)+
  1584. ' l1: '+tostr(hp^.cl1_count)+
  1585. ' l2: '+tostr(hp^.cl2_count)+
  1586. ' l3: '+tostr(hp^.cl3_count)+
  1587. ' oper: '+tostr(hp^.coper_count)+
  1588. ' ord: '+realtostr(hp^.ordinal_distance));
  1589. { Print parameters in left-right order }
  1590. for i:=0 to hp^.data.paras.count-1 do
  1591. begin
  1592. currpara:=tparavarsym(hp^.data.paras[i]);
  1593. if (vo_is_hidden_para in currpara.varoptions) then
  1594. Comment(lvl,' - '+currpara.vartype.def.typename+' : '+EqualTypeName[currpara.eqval]);
  1595. end;
  1596. end;
  1597. hp:=hp^.next;
  1598. end;
  1599. end;
  1600. {$endif EXTDEBUG}
  1601. procedure tcallcandidates.get_information;
  1602. var
  1603. hp : pcandidate;
  1604. currpara : tparavarsym;
  1605. paraidx : integer;
  1606. currparanr : byte;
  1607. rfh,rth : bestreal;
  1608. objdef : tobjectdef;
  1609. def_from,
  1610. def_to : tdef;
  1611. currpt,
  1612. pt : tcallparanode;
  1613. eq : tequaltype;
  1614. convtype : tconverttype;
  1615. pdoper : tprocdef;
  1616. releasecurrpt : boolean;
  1617. cdoptions : tcompare_defs_options;
  1618. begin
  1619. cdoptions:=[cdo_check_operator];
  1620. if FAllowVariant then
  1621. include(cdoptions,cdo_allow_variant);
  1622. { process all procs }
  1623. hp:=FProcs;
  1624. while assigned(hp) do
  1625. begin
  1626. { We compare parameters in reverse order (right to left),
  1627. the firstpara is already pointing to the last parameter
  1628. were we need to start comparing }
  1629. currparanr:=FParalength;
  1630. paraidx:=hp^.firstparaidx;
  1631. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(hp^.data.paras[paraidx]).varoptions) do
  1632. dec(paraidx);
  1633. pt:=tcallparanode(FParaNode);
  1634. while assigned(pt) and (paraidx>=0) do
  1635. begin
  1636. currpara:=tparavarsym(hp^.data.paras[paraidx]);
  1637. { currpt can be changed from loadn to calln when a procvar
  1638. is passed. This is to prevent that the change is permanent }
  1639. currpt:=pt;
  1640. releasecurrpt:=false;
  1641. { retrieve current parameter definitions to compares }
  1642. eq:=te_incompatible;
  1643. def_from:=currpt.resulttype.def;
  1644. def_to:=currpara.vartype.def;
  1645. if not(assigned(def_from)) then
  1646. internalerror(200212091);
  1647. if not(
  1648. assigned(def_to) or
  1649. ((po_varargs in hp^.data.procoptions) and
  1650. (currparanr>hp^.data.minparacount))
  1651. ) then
  1652. internalerror(200212092);
  1653. { Convert tp procvars when not expecting a procvar }
  1654. if (def_to.deftype<>procvardef) and
  1655. (currpt.left.resulttype.def.deftype=procvardef) then
  1656. begin
  1657. releasecurrpt:=true;
  1658. currpt:=tcallparanode(pt.getcopy);
  1659. if maybe_call_procvar(currpt.left,true) then
  1660. begin
  1661. currpt.resulttype:=currpt.left.resulttype;
  1662. def_from:=currpt.left.resulttype.def;
  1663. end;
  1664. end;
  1665. { varargs are always equal, but not exact }
  1666. if (po_varargs in hp^.data.procoptions) and
  1667. (currparanr>hp^.data.minparacount) then
  1668. begin
  1669. eq:=te_equal;
  1670. end
  1671. else
  1672. { same definition -> exact }
  1673. if (def_from=def_to) then
  1674. begin
  1675. eq:=te_exact;
  1676. end
  1677. else
  1678. { for value and const parameters check if a integer is constant or
  1679. included in other integer -> equal and calc ordinal_distance }
  1680. if not(currpara.varspez in [vs_var,vs_out]) and
  1681. is_integer(def_from) and
  1682. is_integer(def_to) and
  1683. is_in_limit(def_from,def_to) then
  1684. begin
  1685. eq:=te_equal;
  1686. hp^.ordinal_distance:=hp^.ordinal_distance+
  1687. abs(bestreal(torddef(def_from).low)-bestreal(torddef(def_to).low));
  1688. if (torddef(def_to).typ=u64bit) then
  1689. rth:=bestreal(qword(torddef(def_to).high))
  1690. else
  1691. rth:=bestreal(torddef(def_to).high);
  1692. if (torddef(def_from).typ=u64bit) then
  1693. rfh:=bestreal(qword(torddef(def_from).high))
  1694. else
  1695. rfh:=bestreal(torddef(def_from).high);
  1696. hp^.ordinal_distance:=hp^.ordinal_distance+abs(rth-rfh);
  1697. { Give wrong sign a small penalty, this is need to get a diffrence
  1698. from word->[longword,longint] }
  1699. if is_signed(def_from)<>is_signed(def_to) then
  1700. hp^.ordinal_distance:=hp^.ordinal_distance+1.0;
  1701. end
  1702. else
  1703. { for value and const parameters check precision of real, give
  1704. penalty for loosing of precision. var and out parameters must match exactly }
  1705. if not(currpara.varspez in [vs_var,vs_out]) and
  1706. is_real(def_from) and
  1707. is_real(def_to) then
  1708. begin
  1709. eq:=te_equal;
  1710. if is_extended(def_to) then
  1711. rth:=bestreal(4)
  1712. else
  1713. if is_double (def_to) then
  1714. rth:=bestreal(2)
  1715. else
  1716. rth:=bestreal(1);
  1717. if is_extended(def_from) then
  1718. rfh:=bestreal(4)
  1719. else
  1720. if is_double (def_from) then
  1721. rfh:=bestreal(2)
  1722. else
  1723. rfh:=bestreal(1);
  1724. { penalty for shrinking of precision }
  1725. if rth<rfh then
  1726. rfh:=(rfh-rth)*16
  1727. else
  1728. rfh:=rth-rfh;
  1729. hp^.ordinal_distance:=hp^.ordinal_distance+rfh;
  1730. end
  1731. else
  1732. { related object parameters also need to determine the distance between the current
  1733. object and the object we are comparing with. var and out parameters must match exactly }
  1734. if not(currpara.varspez in [vs_var,vs_out]) and
  1735. (def_from.deftype=objectdef) and
  1736. (def_to.deftype=objectdef) and
  1737. (tobjectdef(def_from).objecttype=tobjectdef(def_to).objecttype) and
  1738. tobjectdef(def_from).is_related(tobjectdef(def_to)) then
  1739. begin
  1740. eq:=te_convert_l1;
  1741. objdef:=tobjectdef(def_from);
  1742. while assigned(objdef) do
  1743. begin
  1744. if objdef=def_to then
  1745. break;
  1746. hp^.ordinal_distance:=hp^.ordinal_distance+1;
  1747. objdef:=objdef.childof;
  1748. end;
  1749. end
  1750. else
  1751. { generic type comparision }
  1752. begin
  1753. eq:=compare_defs_ext(def_from,def_to,currpt.left.nodetype,convtype,pdoper,cdoptions);
  1754. { when the types are not equal we need to check
  1755. some special case for parameter passing }
  1756. if (eq<te_equal) then
  1757. begin
  1758. if currpara.varspez in [vs_var,vs_out] then
  1759. begin
  1760. { para requires an equal type so the previous found
  1761. match was not good enough, reset to incompatible }
  1762. eq:=te_incompatible;
  1763. { var_para_allowed will return te_equal and te_convert_l1 to
  1764. make a difference for best matching }
  1765. var_para_allowed(eq,currpt.resulttype.def,currpara.vartype.def)
  1766. end
  1767. else
  1768. para_allowed(eq,currpt,def_to);
  1769. end;
  1770. end;
  1771. { when a procvar was changed to a call an exact much is
  1772. downgraded to equal. This way an overload call with the
  1773. procvar is choosen. See tb0471 (PFV) }
  1774. if (pt<>currpt) and (eq=te_exact) then
  1775. eq:=te_equal;
  1776. { increase correct counter }
  1777. case eq of
  1778. te_exact :
  1779. inc(hp^.exact_count);
  1780. te_equal :
  1781. inc(hp^.equal_count);
  1782. te_convert_l1 :
  1783. inc(hp^.cl1_count);
  1784. te_convert_l2 :
  1785. inc(hp^.cl2_count);
  1786. te_convert_l3 :
  1787. inc(hp^.cl3_count);
  1788. te_convert_operator :
  1789. inc(hp^.coper_count);
  1790. te_incompatible :
  1791. hp^.invalid:=true;
  1792. else
  1793. internalerror(200212072);
  1794. end;
  1795. { stop checking when an incompatible parameter is found }
  1796. if hp^.invalid then
  1797. begin
  1798. { store the current parameter info for
  1799. a nice error message when no procedure is found }
  1800. hp^.wrongparaidx:=paraidx;
  1801. hp^.wrongparanr:=currparanr;
  1802. break;
  1803. end;
  1804. {$ifdef EXTDEBUG}
  1805. { store equal in node tree for dump }
  1806. currpara.eqval:=eq;
  1807. {$endif EXTDEBUG}
  1808. { maybe release temp currpt }
  1809. if releasecurrpt then
  1810. currpt.free;
  1811. { next parameter in the call tree }
  1812. pt:=tcallparanode(pt.right);
  1813. { next parameter for definition, only goto next para
  1814. if we're out of the varargs }
  1815. if not(po_varargs in hp^.data.procoptions) or
  1816. (currparanr<=hp^.data.maxparacount) then
  1817. begin
  1818. { Ignore vs_hidden parameters }
  1819. repeat
  1820. dec(paraidx);
  1821. until (paraidx<0) or not(vo_is_hidden_para in tparavarsym(hp^.data.paras[paraidx]).varoptions);
  1822. end;
  1823. dec(currparanr);
  1824. end;
  1825. if not(hp^.invalid) and
  1826. (assigned(pt) or (paraidx>=0) or (currparanr<>0)) then
  1827. internalerror(200212141);
  1828. { next candidate }
  1829. hp:=hp^.next;
  1830. end;
  1831. end;
  1832. function is_better_candidate(currpd,bestpd:pcandidate):integer;
  1833. var
  1834. res : integer;
  1835. begin
  1836. {
  1837. Return values:
  1838. > 0 when currpd is better than bestpd
  1839. < 0 when bestpd is better than currpd
  1840. = 0 when both are equal
  1841. To choose the best candidate we use the following order:
  1842. - Incompatible flag
  1843. - (Smaller) Number of convert operator parameters.
  1844. - (Smaller) Number of convertlevel 2 parameters.
  1845. - (Smaller) Number of convertlevel 1 parameters.
  1846. - (Bigger) Number of exact parameters.
  1847. - (Smaller) Number of equal parameters.
  1848. - (Smaller) Total of ordinal distance. For example, the distance of a word
  1849. to a byte is 65535-255=65280.
  1850. }
  1851. if bestpd^.invalid then
  1852. begin
  1853. if currpd^.invalid then
  1854. res:=0
  1855. else
  1856. res:=1;
  1857. end
  1858. else
  1859. if currpd^.invalid then
  1860. res:=-1
  1861. else
  1862. begin
  1863. { less operator parameters? }
  1864. res:=(bestpd^.coper_count-currpd^.coper_count);
  1865. if (res=0) then
  1866. begin
  1867. { less cl3 parameters? }
  1868. res:=(bestpd^.cl3_count-currpd^.cl3_count);
  1869. if (res=0) then
  1870. begin
  1871. { less cl2 parameters? }
  1872. res:=(bestpd^.cl2_count-currpd^.cl2_count);
  1873. if (res=0) then
  1874. begin
  1875. { less cl1 parameters? }
  1876. res:=(bestpd^.cl1_count-currpd^.cl1_count);
  1877. if (res=0) then
  1878. begin
  1879. { more exact parameters? }
  1880. res:=(currpd^.exact_count-bestpd^.exact_count);
  1881. if (res=0) then
  1882. begin
  1883. { less equal parameters? }
  1884. res:=(bestpd^.equal_count-currpd^.equal_count);
  1885. if (res=0) then
  1886. begin
  1887. { smaller ordinal distance? }
  1888. if (currpd^.ordinal_distance<bestpd^.ordinal_distance) then
  1889. res:=1
  1890. else
  1891. if (currpd^.ordinal_distance>bestpd^.ordinal_distance) then
  1892. res:=-1
  1893. else
  1894. res:=0;
  1895. end;
  1896. end;
  1897. end;
  1898. end;
  1899. end;
  1900. end;
  1901. end;
  1902. is_better_candidate:=res;
  1903. end;
  1904. function tcallcandidates.choose_best(var bestpd:tabstractprocdef):integer;
  1905. var
  1906. besthpstart,
  1907. hp : pcandidate;
  1908. cntpd,
  1909. res : integer;
  1910. begin
  1911. {
  1912. Returns the number of candidates left and the
  1913. first candidate is returned in pdbest
  1914. }
  1915. { Setup the first procdef as best, only count it as a result
  1916. when it is valid }
  1917. bestpd:=FProcs^.data;
  1918. if FProcs^.invalid then
  1919. cntpd:=0
  1920. else
  1921. cntpd:=1;
  1922. if assigned(FProcs^.next) then
  1923. begin
  1924. besthpstart:=FProcs;
  1925. hp:=FProcs^.next;
  1926. while assigned(hp) do
  1927. begin
  1928. res:=is_better_candidate(hp,besthpstart);
  1929. if (res>0) then
  1930. begin
  1931. { hp is better, flag all procs to be incompatible }
  1932. while (besthpstart<>hp) do
  1933. begin
  1934. besthpstart^.invalid:=true;
  1935. besthpstart:=besthpstart^.next;
  1936. end;
  1937. { besthpstart is already set to hp }
  1938. bestpd:=besthpstart^.data;
  1939. cntpd:=1;
  1940. end
  1941. else
  1942. if (res<0) then
  1943. begin
  1944. { besthpstart is better, flag current hp to be incompatible }
  1945. hp^.invalid:=true;
  1946. end
  1947. else
  1948. begin
  1949. { res=0, both are valid }
  1950. if not hp^.invalid then
  1951. inc(cntpd);
  1952. end;
  1953. hp:=hp^.next;
  1954. end;
  1955. end;
  1956. result:=cntpd;
  1957. end;
  1958. procedure tcallcandidates.find_wrong_para;
  1959. var
  1960. currparanr : smallint;
  1961. hp : pcandidate;
  1962. pt : tcallparanode;
  1963. wrongpara : tparavarsym;
  1964. begin
  1965. { Only process the first overloaded procdef }
  1966. hp:=FProcs;
  1967. { Find callparanode corresponding to the argument }
  1968. pt:=tcallparanode(FParanode);
  1969. currparanr:=FParalength;
  1970. while assigned(pt) and
  1971. (currparanr>hp^.wrongparanr) do
  1972. begin
  1973. pt:=tcallparanode(pt.right);
  1974. dec(currparanr);
  1975. end;
  1976. if (currparanr<>hp^.wrongparanr) or
  1977. not assigned(pt) then
  1978. internalerror(200212094);
  1979. { Show error message, when it was a var or out parameter
  1980. guess that it is a missing typeconv }
  1981. wrongpara:=tparavarsym(hp^.data.paras[hp^.wrongparaidx]);
  1982. if wrongpara.varspez in [vs_var,vs_out] then
  1983. begin
  1984. { Maybe passing the correct type but passing a const to var parameter }
  1985. if (compare_defs(pt.resulttype.def,wrongpara.vartype.def,pt.nodetype)<>te_incompatible) and
  1986. not valid_for_var(pt.left) then
  1987. CGMessagePos(pt.left.fileinfo,type_e_variable_id_expected)
  1988. else
  1989. CGMessagePos2(pt.left.fileinfo,parser_e_call_by_ref_without_typeconv,
  1990. FullTypeName(pt.left.resulttype.def,wrongpara.vartype.def),
  1991. FullTypeName(wrongpara.vartype.def,pt.left.resulttype.def))
  1992. end
  1993. else
  1994. CGMessagePos3(pt.left.fileinfo,type_e_wrong_parameter_type,tostr(hp^.wrongparanr),
  1995. FullTypeName(pt.left.resulttype.def,wrongpara.vartype.def),
  1996. FullTypeName(wrongpara.vartype.def,pt.left.resulttype.def));
  1997. end;
  1998. end.