defcmp.pas 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Compare definitions and parameter lists
  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 defcmp;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,
  22. globtype,globals,
  23. node,
  24. symconst,symtype,symdef;
  25. type
  26. { if acp is cp_all the var const or nothing are considered equal }
  27. tcompare_paras_type = ( cp_none, cp_value_equal_const, cp_all,cp_procvar);
  28. tcompare_paras_option = (cpo_allowdefaults,cpo_ignorehidden,cpo_allowconvert,cpo_comparedefaultvalue);
  29. tcompare_paras_options = set of tcompare_paras_option;
  30. tcompare_defs_option = (cdo_internal,cdo_explicit,cdo_check_operator,cdo_allow_variant);
  31. tcompare_defs_options = set of tcompare_defs_option;
  32. tconverttype = (tc_none,
  33. tc_equal,
  34. tc_not_possible,
  35. tc_string_2_string,
  36. tc_char_2_string,
  37. tc_char_2_chararray,
  38. tc_pchar_2_string,
  39. tc_cchar_2_pchar,
  40. tc_cstring_2_pchar,
  41. tc_cstring_2_int,
  42. tc_ansistring_2_pchar,
  43. tc_string_2_chararray,
  44. tc_chararray_2_string,
  45. tc_array_2_pointer,
  46. tc_pointer_2_array,
  47. tc_int_2_int,
  48. tc_int_2_bool,
  49. tc_bool_2_bool,
  50. tc_bool_2_int,
  51. tc_real_2_real,
  52. tc_int_2_real,
  53. tc_real_2_currency,
  54. tc_proc_2_procvar,
  55. tc_arrayconstructor_2_set,
  56. tc_load_smallset,
  57. tc_cord_2_pointer,
  58. tc_intf_2_string,
  59. tc_intf_2_guid,
  60. tc_class_2_intf,
  61. tc_char_2_char,
  62. tc_normal_2_smallset,
  63. tc_dynarray_2_openarray,
  64. tc_pwchar_2_string,
  65. tc_variant_2_dynarray,
  66. tc_dynarray_2_variant,
  67. tc_variant_2_enum,
  68. tc_enum_2_variant,
  69. tc_interface_2_variant,
  70. tc_variant_2_interface,
  71. tc_array_2_dynarray
  72. );
  73. function compare_defs_ext(def_from,def_to : tdef;
  74. fromtreetype : tnodetype;
  75. var doconv : tconverttype;
  76. var operatorpd : tprocdef;
  77. cdoptions:tcompare_defs_options):tequaltype;
  78. { Returns if the type def_from can be converted to def_to or if both types are equal }
  79. function compare_defs(def_from,def_to:tdef;fromtreetype:tnodetype):tequaltype;
  80. { Returns true, if def1 and def2 are semantically the same }
  81. function equal_defs(def_from,def_to:tdef):boolean;
  82. { Checks for type compatibility (subgroups of type)
  83. used for case statements... probably missing stuff
  84. to use on other types }
  85. function is_subequal(def1, def2: tdef): boolean;
  86. {# true, if two parameter lists are equal
  87. if acp is cp_none, all have to match exactly
  88. if acp is cp_value_equal_const call by value
  89. and call by const parameter are assumed as
  90. equal
  91. allowdefaults indicates if default value parameters
  92. are allowed (in this case, the search order will first
  93. search for a routine with default parameters, before
  94. searching for the same definition with no parameters)
  95. }
  96. function compare_paras(para1,para2 : tlist; acp : tcompare_paras_type; cpoptions: tcompare_paras_options):tequaltype;
  97. { True if a function can be assigned to a procvar }
  98. { changed first argument type to pabstractprocdef so that it can also be }
  99. { used to test compatibility between two pprocvardefs (JM) }
  100. function proc_to_procvar_equal(def1:tabstractprocdef;def2:tprocvardef;methoderr:boolean):tequaltype;
  101. implementation
  102. uses
  103. verbose,systems,
  104. symtable,symsym,
  105. defutil,symutil;
  106. function compare_defs_ext(def_from,def_to : tdef;
  107. fromtreetype : tnodetype;
  108. var doconv : tconverttype;
  109. var operatorpd : tprocdef;
  110. cdoptions:tcompare_defs_options):tequaltype;
  111. { Tbasetype:
  112. uvoid,
  113. u8bit,u16bit,u32bit,u64bit,
  114. s8bit,s16bit,s32bit,s64bit,
  115. bool8bit,bool16bit,bool32bit,
  116. uchar,uwidechar }
  117. type
  118. tbasedef=(bvoid,bchar,bint,bbool);
  119. const
  120. basedeftbl:array[tbasetype] of tbasedef =
  121. (bvoid,
  122. bint,bint,bint,bint,
  123. bint,bint,bint,bint,
  124. bbool,bbool,bbool,
  125. bchar,bchar,bint);
  126. basedefconvertsimplicit : array[tbasedef,tbasedef] of tconverttype =
  127. { void, char, int, bool }
  128. ((tc_not_possible,tc_not_possible,tc_not_possible,tc_not_possible),
  129. (tc_not_possible,tc_char_2_char,tc_not_possible,tc_not_possible),
  130. (tc_not_possible,tc_not_possible,tc_int_2_int,tc_not_possible),
  131. (tc_not_possible,tc_not_possible,tc_not_possible,tc_bool_2_bool));
  132. basedefconvertsexplicit : array[tbasedef,tbasedef] of tconverttype =
  133. { void, char, int, bool }
  134. ((tc_not_possible,tc_not_possible,tc_not_possible,tc_not_possible),
  135. (tc_not_possible,tc_char_2_char,tc_int_2_int,tc_int_2_bool),
  136. (tc_not_possible,tc_int_2_int,tc_int_2_int,tc_int_2_bool),
  137. (tc_not_possible,tc_bool_2_int,tc_bool_2_int,tc_bool_2_bool));
  138. var
  139. subeq,eq : tequaltype;
  140. hd1,hd2 : tdef;
  141. hct : tconverttype;
  142. hd3 : tobjectdef;
  143. hpd : tprocdef;
  144. begin
  145. eq:=te_incompatible;
  146. doconv:=tc_not_possible;
  147. { safety check }
  148. if not(assigned(def_from) and assigned(def_to)) then
  149. begin
  150. compare_defs_ext:=te_incompatible;
  151. exit;
  152. end;
  153. { same def? then we've an exact match }
  154. if def_from=def_to then
  155. begin
  156. doconv:=tc_equal;
  157. compare_defs_ext:=te_exact;
  158. exit;
  159. end;
  160. { we walk the wanted (def_to) types and check then the def_from
  161. types if there is a conversion possible }
  162. case def_to.deftype of
  163. orddef :
  164. begin
  165. case def_from.deftype of
  166. orddef :
  167. begin
  168. if (torddef(def_from).typ=torddef(def_to).typ) then
  169. begin
  170. case torddef(def_from).typ of
  171. uchar,uwidechar,
  172. u8bit,u16bit,u32bit,u64bit,
  173. s8bit,s16bit,s32bit,s64bit:
  174. begin
  175. if (torddef(def_from).low=torddef(def_to).low) and
  176. (torddef(def_from).high=torddef(def_to).high) then
  177. eq:=te_equal
  178. else
  179. begin
  180. doconv:=tc_int_2_int;
  181. eq:=te_convert_l1;
  182. end;
  183. end;
  184. uvoid,
  185. bool8bit,bool16bit,bool32bit:
  186. eq:=te_equal;
  187. else
  188. internalerror(200210061);
  189. end;
  190. end
  191. else
  192. begin
  193. if cdo_explicit in cdoptions then
  194. doconv:=basedefconvertsexplicit[basedeftbl[torddef(def_from).typ],basedeftbl[torddef(def_to).typ]]
  195. else
  196. doconv:=basedefconvertsimplicit[basedeftbl[torddef(def_from).typ],basedeftbl[torddef(def_to).typ]];
  197. if (doconv=tc_not_possible) then
  198. eq:=te_incompatible
  199. else
  200. { "punish" bad type conversions :) (JM) }
  201. if (not is_in_limit(def_from,def_to)) and
  202. (def_from.size > def_to.size) then
  203. eq:=te_convert_l3
  204. else
  205. eq:=te_convert_l1;
  206. end;
  207. end;
  208. enumdef :
  209. begin
  210. { needed for char(enum) }
  211. if cdo_explicit in cdoptions then
  212. begin
  213. doconv:=tc_int_2_int;
  214. eq:=te_convert_l1;
  215. end;
  216. end;
  217. floatdef :
  218. begin
  219. if is_currency(def_to) then
  220. begin
  221. doconv:=tc_real_2_currency;
  222. eq:=te_convert_l2;
  223. end;
  224. end;
  225. classrefdef,
  226. procvardef,
  227. pointerdef :
  228. begin
  229. if cdo_explicit in cdoptions then
  230. begin
  231. eq:=te_convert_l1;
  232. if (fromtreetype=niln) then
  233. begin
  234. { will be handled by the constant folding }
  235. doconv:=tc_equal;
  236. end
  237. else
  238. doconv:=tc_int_2_int;
  239. end;
  240. end;
  241. arraydef :
  242. begin
  243. if (m_mac in aktmodeswitches) and
  244. (fromtreetype=stringconstn) then
  245. begin
  246. eq:=te_convert_l3;
  247. doconv:=tc_cstring_2_int;
  248. end;
  249. end;
  250. end;
  251. end;
  252. stringdef :
  253. begin
  254. case def_from.deftype of
  255. stringdef :
  256. begin
  257. { Constant string }
  258. if (fromtreetype=stringconstn) then
  259. begin
  260. { we can change the stringconst node }
  261. if (tstringdef(def_from).string_typ=st_conststring) or
  262. (tstringdef(def_from).string_typ=tstringdef(def_to).string_typ) then
  263. eq:=te_equal
  264. else
  265. begin
  266. doconv:=tc_string_2_string;
  267. { Don't prefer conversions from widestring to a
  268. normal string as we can loose information }
  269. if tstringdef(def_from).string_typ=st_widestring then
  270. eq:=te_convert_l3
  271. else if tstringdef(def_to).string_typ=st_widestring then
  272. eq:=te_convert_l2
  273. else
  274. eq:=te_equal;
  275. end;
  276. end
  277. else
  278. { Same string type, for shortstrings also the length must match }
  279. if (tstringdef(def_from).string_typ=tstringdef(def_to).string_typ) and
  280. ((tstringdef(def_from).string_typ<>st_shortstring) or
  281. (tstringdef(def_from).len=tstringdef(def_to).len)) then
  282. eq:=te_equal
  283. else
  284. begin
  285. doconv:=tc_string_2_string;
  286. case tstringdef(def_from).string_typ of
  287. st_widestring :
  288. begin
  289. { Prefer conversions to ansistring }
  290. if tstringdef(def_to).string_typ=st_ansistring then
  291. eq:=te_convert_l2
  292. else
  293. eq:=te_convert_l3;
  294. end;
  295. st_shortstring :
  296. begin
  297. { Prefer shortstrings of different length or conversions
  298. from shortstring to ansistring }
  299. if (tstringdef(def_to).string_typ=st_shortstring) then
  300. eq:=te_convert_l1
  301. else if tstringdef(def_to).string_typ=st_ansistring then
  302. eq:=te_convert_l2
  303. else
  304. eq:=te_convert_l3;
  305. end;
  306. st_ansistring :
  307. begin
  308. { Prefer conversion to widestrings }
  309. if (tstringdef(def_to).string_typ=st_widestring) then
  310. eq:=te_convert_l2
  311. else
  312. eq:=te_convert_l3;
  313. end;
  314. end;
  315. end;
  316. end;
  317. orddef :
  318. begin
  319. { char to string}
  320. if is_char(def_from) or
  321. is_widechar(def_from) then
  322. begin
  323. doconv:=tc_char_2_string;
  324. eq:=te_convert_l1;
  325. end;
  326. end;
  327. arraydef :
  328. begin
  329. { array of char to string, the length check is done by the firstpass of this node }
  330. if is_chararray(def_from) or is_open_chararray(def_from) then
  331. begin
  332. { "Untyped" stringconstn is an array of char }
  333. if fromtreetype=stringconstn then
  334. begin
  335. doconv:=tc_string_2_string;
  336. { prefered string type depends on the $H switch }
  337. if not(cs_ansistrings in aktlocalswitches) and
  338. (tstringdef(def_to).string_typ=st_shortstring) then
  339. eq:=te_equal
  340. else if (cs_ansistrings in aktlocalswitches) and
  341. (tstringdef(def_to).string_typ=st_ansistring) then
  342. eq:=te_equal
  343. else if tstringdef(def_to).string_typ=st_widestring then
  344. eq:=te_convert_l3
  345. else
  346. eq:=te_convert_l1;
  347. end
  348. else
  349. begin
  350. doconv:=tc_chararray_2_string;
  351. if is_open_array(def_from) then
  352. begin
  353. if is_ansistring(def_to) then
  354. eq:=te_convert_l1
  355. else if is_widestring(def_to) then
  356. eq:=te_convert_l3
  357. else
  358. eq:=te_convert_l2;
  359. end
  360. else
  361. begin
  362. if is_shortstring(def_to) then
  363. begin
  364. { Only compatible with arrays that fit
  365. smaller than 255 chars }
  366. if (def_from.size <= 255) then
  367. eq:=te_convert_l1;
  368. end
  369. else if is_ansistring(def_to) then
  370. begin
  371. if (def_from.size > 255) then
  372. eq:=te_convert_l1
  373. else
  374. eq:=te_convert_l2;
  375. end
  376. else if is_widestring(def_to) then
  377. eq:=te_convert_l3
  378. else
  379. eq:=te_convert_l2;
  380. end;
  381. end;
  382. end
  383. else
  384. { array of widechar to string, the length check is done by the firstpass of this node }
  385. if is_widechararray(def_from) or is_open_widechararray(def_from) then
  386. begin
  387. doconv:=tc_chararray_2_string;
  388. if is_widestring(def_to) then
  389. eq:=te_convert_l1
  390. else
  391. { size of widechar array is double due the sizeof a widechar }
  392. if not(is_shortstring(def_to) and (def_from.size>255*sizeof(widechar))) then
  393. eq:=te_convert_l3
  394. else
  395. eq:=te_convert_l2;
  396. end;
  397. end;
  398. pointerdef :
  399. begin
  400. { pchar can be assigned to short/ansistrings,
  401. but not in tp7 compatible mode }
  402. if not(m_tp7 in aktmodeswitches) then
  403. begin
  404. if is_pchar(def_from) then
  405. begin
  406. doconv:=tc_pchar_2_string;
  407. { prefer ansistrings because pchars can overflow shortstrings, }
  408. { but only if ansistrings are the default (JM) }
  409. if (is_shortstring(def_to) and
  410. not(cs_ansistrings in aktlocalswitches)) or
  411. (is_ansistring(def_to) and
  412. (cs_ansistrings in aktlocalswitches)) then
  413. eq:=te_convert_l1
  414. else
  415. eq:=te_convert_l2;
  416. end
  417. else if is_pwidechar(def_from) then
  418. begin
  419. doconv:=tc_pwchar_2_string;
  420. if is_widestring(def_to) then
  421. eq:=te_convert_l1
  422. else
  423. eq:=te_convert_l3;
  424. end;
  425. end;
  426. end;
  427. end;
  428. end;
  429. floatdef :
  430. begin
  431. case def_from.deftype of
  432. orddef :
  433. begin { ordinal to real }
  434. if is_integer(def_from) or
  435. (is_currency(def_from) and
  436. (s64currencytype.def.deftype = floatdef)) then
  437. begin
  438. doconv:=tc_int_2_real;
  439. eq:=te_convert_l1;
  440. end
  441. else if is_currency(def_from)
  442. { and (s64currencytype.def.deftype = orddef)) } then
  443. begin
  444. { prefer conversion to orddef in this case, unless }
  445. { the orddef < currency (then it will get convert l3, }
  446. { and conversion to float is favoured) }
  447. doconv:=tc_int_2_real;
  448. eq:=te_convert_l2;
  449. end;
  450. end;
  451. floatdef :
  452. begin
  453. if tfloatdef(def_from).typ=tfloatdef(def_to).typ then
  454. eq:=te_equal
  455. else
  456. begin
  457. if (fromtreetype=realconstn) or
  458. not((cdo_explicit in cdoptions) and
  459. (m_delphi in aktmodeswitches)) then
  460. begin
  461. doconv:=tc_real_2_real;
  462. { do we loose precision? }
  463. if def_to.size<def_from.size then
  464. eq:=te_convert_l2
  465. else
  466. eq:=te_convert_l1;
  467. end;
  468. end;
  469. end;
  470. end;
  471. end;
  472. enumdef :
  473. begin
  474. case def_from.deftype of
  475. enumdef :
  476. begin
  477. if cdo_explicit in cdoptions then
  478. begin
  479. eq:=te_convert_l1;
  480. doconv:=tc_int_2_int;
  481. end
  482. else
  483. begin
  484. hd1:=def_from;
  485. while assigned(tenumdef(hd1).basedef) do
  486. hd1:=tenumdef(hd1).basedef;
  487. hd2:=def_to;
  488. while assigned(tenumdef(hd2).basedef) do
  489. hd2:=tenumdef(hd2).basedef;
  490. if (hd1=hd2) then
  491. begin
  492. eq:=te_convert_l1;
  493. { because of packenum they can have different sizes! (JM) }
  494. doconv:=tc_int_2_int;
  495. end
  496. else
  497. begin
  498. { assignment of an enum symbol to an unique type? }
  499. if (fromtreetype=ordconstn) and
  500. (tenumsym(tenumdef(hd1).firstenum)=tenumsym(tenumdef(hd2).firstenum)) then
  501. begin
  502. { because of packenum they can have different sizes! (JM) }
  503. eq:=te_convert_l1;
  504. doconv:=tc_int_2_int;
  505. end;
  506. end;
  507. end;
  508. end;
  509. orddef :
  510. begin
  511. if cdo_explicit in cdoptions then
  512. begin
  513. eq:=te_convert_l1;
  514. doconv:=tc_int_2_int;
  515. end;
  516. end;
  517. variantdef :
  518. begin
  519. eq:=te_convert_l1;
  520. doconv:=tc_variant_2_enum;
  521. end;
  522. pointerdef :
  523. begin
  524. { ugly, but delphi allows it }
  525. if (cdo_explicit in cdoptions) and
  526. (m_delphi in aktmodeswitches) and
  527. (eq=te_incompatible) then
  528. begin
  529. doconv:=tc_int_2_int;
  530. eq:=te_convert_l1;
  531. end;
  532. end;
  533. end;
  534. end;
  535. arraydef :
  536. begin
  537. { open array is also compatible with a single element of its base type }
  538. if is_open_array(def_to) and
  539. equal_defs(def_from,tarraydef(def_to).elementtype.def) then
  540. begin
  541. doconv:=tc_equal;
  542. eq:=te_convert_l1;
  543. end
  544. else
  545. begin
  546. case def_from.deftype of
  547. arraydef :
  548. begin
  549. { to dynamic array }
  550. if is_dynamic_array(def_to) then
  551. begin
  552. if equal_defs(tarraydef(def_from).elementtype.def,tarraydef(def_to).elementtype.def) then
  553. begin
  554. { dynamic array -> dynamic array }
  555. if is_dynamic_array(def_from) then
  556. eq:=te_equal
  557. { fpc modes only: array -> dyn. array }
  558. else if (aktmodeswitches*[m_objfpc,m_fpc]<>[]) and
  559. not(is_special_array(def_from)) and
  560. is_zero_based_array(def_from) then
  561. begin
  562. eq:=te_convert_l2;
  563. doconv:=tc_array_2_dynarray;
  564. end;
  565. end
  566. end
  567. else
  568. { to open array }
  569. if is_open_array(def_to) then
  570. begin
  571. { array constructor -> open array }
  572. if is_array_constructor(def_from) then
  573. begin
  574. if is_void(tarraydef(def_from).elementtype.def) then
  575. begin
  576. doconv:=tc_equal;
  577. eq:=te_convert_l1;
  578. end
  579. else
  580. begin
  581. subeq:=compare_defs_ext(tarraydef(def_from).elementtype.def,
  582. tarraydef(def_to).elementtype.def,
  583. arrayconstructorn,hct,hpd,[cdo_check_operator]);
  584. if (subeq>=te_equal) then
  585. begin
  586. doconv:=tc_equal;
  587. eq:=te_convert_l1;
  588. end
  589. else
  590. if (subeq>te_incompatible) then
  591. begin
  592. doconv:=hct;
  593. eq:=te_convert_l2;
  594. end;
  595. end;
  596. end
  597. else
  598. { dynamic array -> open array }
  599. if is_dynamic_array(def_from) and
  600. equal_defs(tarraydef(def_from).elementtype.def,tarraydef(def_to).elementtype.def) then
  601. begin
  602. doconv:=tc_dynarray_2_openarray;
  603. eq:=te_convert_l2;
  604. end
  605. else
  606. { array -> open array }
  607. if equal_defs(tarraydef(def_from).elementtype.def,tarraydef(def_to).elementtype.def) then
  608. eq:=te_equal;
  609. end
  610. else
  611. { to array of const }
  612. if is_array_of_const(def_to) then
  613. begin
  614. if is_array_of_const(def_from) or
  615. is_array_constructor(def_from) then
  616. begin
  617. eq:=te_equal;
  618. end
  619. else
  620. { array of tvarrec -> array of const }
  621. if equal_defs(tarraydef(def_to).elementtype.def,tarraydef(def_from).elementtype.def) then
  622. begin
  623. doconv:=tc_equal;
  624. eq:=te_convert_l1;
  625. end;
  626. end
  627. else
  628. { to array of char, from "Untyped" stringconstn (array of char) }
  629. if (fromtreetype=stringconstn) and
  630. is_chararray(def_to) then
  631. begin
  632. eq:=te_convert_l1;
  633. doconv:=tc_string_2_chararray;
  634. end
  635. else
  636. { other arrays }
  637. begin
  638. { open array -> array }
  639. if is_open_array(def_from) and
  640. equal_defs(tarraydef(def_from).elementtype.def,tarraydef(def_to).elementtype.def) then
  641. begin
  642. eq:=te_equal
  643. end
  644. else
  645. { array -> array }
  646. if not(m_tp7 in aktmodeswitches) and
  647. not(m_delphi in aktmodeswitches) and
  648. (tarraydef(def_from).lowrange=tarraydef(def_to).lowrange) and
  649. (tarraydef(def_from).highrange=tarraydef(def_to).highrange) and
  650. equal_defs(tarraydef(def_from).elementtype.def,tarraydef(def_to).elementtype.def) and
  651. equal_defs(tarraydef(def_from).rangetype.def,tarraydef(def_to).rangetype.def) then
  652. begin
  653. eq:=te_equal
  654. end;
  655. end;
  656. end;
  657. pointerdef :
  658. begin
  659. { nil and voidpointers are compatible with dyn. arrays }
  660. if is_dynamic_array(def_to) and
  661. ((fromtreetype=niln) or
  662. is_voidpointer(def_from)) then
  663. begin
  664. doconv:=tc_equal;
  665. eq:=te_convert_l1;
  666. end
  667. else
  668. if is_zero_based_array(def_to) and
  669. equal_defs(tpointerdef(def_from).pointertype.def,tarraydef(def_to).elementtype.def) then
  670. begin
  671. doconv:=tc_pointer_2_array;
  672. eq:=te_convert_l1;
  673. end;
  674. end;
  675. stringdef :
  676. begin
  677. { string to char array }
  678. if (not is_special_array(def_to)) and
  679. (is_char(tarraydef(def_to).elementtype.def)or
  680. is_widechar(tarraydef(def_to).elementtype.def)) then
  681. begin
  682. doconv:=tc_string_2_chararray;
  683. eq:=te_convert_l1;
  684. end;
  685. end;
  686. orddef:
  687. begin
  688. if is_chararray(def_to) and
  689. is_char(def_from) then
  690. begin
  691. doconv:=tc_char_2_chararray;
  692. eq:=te_convert_l2;
  693. end;
  694. end;
  695. recorddef :
  696. begin
  697. { tvarrec -> array of const }
  698. if is_array_of_const(def_to) and
  699. equal_defs(def_from,tarraydef(def_to).elementtype.def) then
  700. begin
  701. doconv:=tc_equal;
  702. eq:=te_convert_l1;
  703. end;
  704. end;
  705. variantdef :
  706. begin
  707. if is_dynamic_array(def_to) then
  708. begin
  709. doconv:=tc_variant_2_dynarray;
  710. eq:=te_convert_l1;
  711. end;
  712. end;
  713. end;
  714. end;
  715. end;
  716. variantdef :
  717. begin
  718. if (cdo_allow_variant in cdoptions) then
  719. begin
  720. case def_from.deftype of
  721. enumdef :
  722. begin
  723. doconv:=tc_enum_2_variant;
  724. eq:=te_convert_l1;
  725. end;
  726. arraydef :
  727. begin
  728. if is_dynamic_array(def_from) then
  729. begin
  730. doconv:=tc_dynarray_2_variant;
  731. eq:=te_convert_l1;
  732. end;
  733. end;
  734. objectdef :
  735. begin
  736. if is_interface(def_from) then
  737. begin
  738. doconv:=tc_interface_2_variant;
  739. eq:=te_convert_l1;
  740. end;
  741. end;
  742. end;
  743. end;
  744. end;
  745. pointerdef :
  746. begin
  747. case def_from.deftype of
  748. stringdef :
  749. begin
  750. { string constant (which can be part of array constructor)
  751. to zero terminated string constant }
  752. if (fromtreetype in [arrayconstructorn,stringconstn]) and
  753. (is_pchar(def_to) or is_pwidechar(def_to)) then
  754. begin
  755. doconv:=tc_cstring_2_pchar;
  756. eq:=te_convert_l2;
  757. end
  758. else
  759. if cdo_explicit in cdoptions then
  760. begin
  761. { pchar(ansistring) }
  762. if is_pchar(def_to) and
  763. is_ansistring(def_from) then
  764. begin
  765. doconv:=tc_ansistring_2_pchar;
  766. eq:=te_convert_l1;
  767. end
  768. else
  769. { pwidechar(widestring) }
  770. if is_pwidechar(def_to) and
  771. is_widestring(def_from) then
  772. begin
  773. doconv:=tc_ansistring_2_pchar;
  774. eq:=te_convert_l1;
  775. end;
  776. end;
  777. end;
  778. orddef :
  779. begin
  780. { char constant to zero terminated string constant }
  781. if (fromtreetype=ordconstn) then
  782. begin
  783. if (is_char(def_from) or is_widechar(def_from)) and
  784. (is_pchar(def_to) or is_pwidechar(def_to)) then
  785. begin
  786. doconv:=tc_cchar_2_pchar;
  787. eq:=te_convert_l1;
  788. end
  789. else
  790. if (m_delphi in aktmodeswitches) and is_integer(def_from) then
  791. begin
  792. doconv:=tc_cord_2_pointer;
  793. eq:=te_convert_l2;
  794. end;
  795. end;
  796. { delphi compatible, allow explicit typecasts from
  797. ordinals to pointer.
  798. It is also used by the compiler internally for inc(pointer,ordinal) }
  799. if (eq=te_incompatible) and
  800. not is_void(def_from) and
  801. (
  802. (
  803. (m_delphi in aktmodeswitches) and
  804. (cdo_explicit in cdoptions)
  805. ) or
  806. (cdo_internal in cdoptions)
  807. ) then
  808. begin
  809. doconv:=tc_int_2_int;
  810. eq:=te_convert_l1;
  811. end;
  812. end;
  813. arraydef :
  814. begin
  815. { string constant (which can be part of array constructor)
  816. to zero terminated string constant }
  817. if (fromtreetype in [arrayconstructorn,stringconstn]) and
  818. (is_pchar(def_to) or is_pwidechar(def_to)) then
  819. begin
  820. doconv:=tc_cstring_2_pchar;
  821. eq:=te_convert_l2;
  822. end
  823. else
  824. { chararray to pointer }
  825. if (is_zero_based_array(def_from) or
  826. is_open_array(def_from)) and
  827. equal_defs(tarraydef(def_from).elementtype.def,tpointerdef(def_to).pointertype.def) then
  828. begin
  829. doconv:=tc_array_2_pointer;
  830. { don't prefer the pchar overload when a constant
  831. string was passed }
  832. if fromtreetype=stringconstn then
  833. eq:=te_convert_l2
  834. else
  835. eq:=te_convert_l1;
  836. end
  837. else
  838. { dynamic array to pointer, delphi only }
  839. if (m_delphi in aktmodeswitches) and
  840. is_dynamic_array(def_from) then
  841. begin
  842. eq:=te_equal;
  843. end;
  844. end;
  845. pointerdef :
  846. begin
  847. { check for far pointers }
  848. if (tpointerdef(def_from).is_far<>tpointerdef(def_to).is_far) then
  849. begin
  850. eq:=te_incompatible;
  851. end
  852. else
  853. { the types can be forward type, handle before normal type check !! }
  854. if assigned(def_to.typesym) and
  855. (tpointerdef(def_to).pointertype.def.deftype=forwarddef) then
  856. begin
  857. if (def_from.typesym=def_to.typesym) then
  858. eq:=te_equal
  859. end
  860. else
  861. { same types }
  862. if equal_defs(tpointerdef(def_from).pointertype.def,tpointerdef(def_to).pointertype.def) then
  863. begin
  864. eq:=te_equal
  865. end
  866. else
  867. { child class pointer can be assigned to anchestor pointers }
  868. if (
  869. (tpointerdef(def_from).pointertype.def.deftype=objectdef) and
  870. (tpointerdef(def_to).pointertype.def.deftype=objectdef) and
  871. tobjectdef(tpointerdef(def_from).pointertype.def).is_related(
  872. tobjectdef(tpointerdef(def_to).pointertype.def))
  873. ) then
  874. begin
  875. doconv:=tc_equal;
  876. eq:=te_convert_l1;
  877. end
  878. else
  879. { all pointers can be assigned to void-pointer }
  880. if is_void(tpointerdef(def_to).pointertype.def) then
  881. begin
  882. doconv:=tc_equal;
  883. { give pwidechar,pchar a penalty so it prefers
  884. conversion to ansistring }
  885. if is_pchar(def_from) or
  886. is_pwidechar(def_from) then
  887. eq:=te_convert_l2
  888. else
  889. eq:=te_convert_l1;
  890. end
  891. else
  892. { all pointers can be assigned from void-pointer }
  893. if is_void(tpointerdef(def_from).pointertype.def) or
  894. { all pointers can be assigned from void-pointer or formaldef pointer, check
  895. tw3777.pp if you change this }
  896. (tpointerdef(def_from).pointertype.def.deftype=formaldef) then
  897. begin
  898. doconv:=tc_equal;
  899. { give pwidechar a penalty so it prefers
  900. conversion to pchar }
  901. if is_pwidechar(def_to) then
  902. eq:=te_convert_l2
  903. else
  904. eq:=te_convert_l1;
  905. end;
  906. end;
  907. procvardef :
  908. begin
  909. { procedure variable can be assigned to an void pointer,
  910. this not allowed for methodpointers }
  911. if is_void(tpointerdef(def_to).pointertype.def) and
  912. tprocvardef(def_from).is_addressonly then
  913. begin
  914. doconv:=tc_equal;
  915. eq:=te_convert_l1;
  916. end;
  917. end;
  918. classrefdef,
  919. objectdef :
  920. begin
  921. { class types and class reference type
  922. can be assigned to void pointers, but it is less
  923. preferred than assigning to a related objectdef }
  924. if (
  925. is_class_or_interface(def_from) or
  926. (def_from.deftype=classrefdef)
  927. ) and
  928. (tpointerdef(def_to).pointertype.def.deftype=orddef) and
  929. (torddef(tpointerdef(def_to).pointertype.def).typ=uvoid) then
  930. begin
  931. doconv:=tc_equal;
  932. eq:=te_convert_l2;
  933. end;
  934. end;
  935. end;
  936. end;
  937. setdef :
  938. begin
  939. case def_from.deftype of
  940. setdef :
  941. begin
  942. if assigned(tsetdef(def_from).elementtype.def) and
  943. assigned(tsetdef(def_to).elementtype.def) then
  944. begin
  945. { sets with the same element base type are equal }
  946. if is_subequal(tsetdef(def_from).elementtype.def,tsetdef(def_to).elementtype.def) then
  947. eq:=te_equal;
  948. end
  949. else
  950. { empty set is compatible with everything }
  951. eq:=te_equal;
  952. end;
  953. arraydef :
  954. begin
  955. { automatic arrayconstructor -> set conversion }
  956. if is_array_constructor(def_from) then
  957. begin
  958. doconv:=tc_arrayconstructor_2_set;
  959. eq:=te_convert_l1;
  960. end;
  961. end;
  962. end;
  963. end;
  964. procvardef :
  965. begin
  966. case def_from.deftype of
  967. procdef :
  968. begin
  969. { proc -> procvar }
  970. if (m_tp_procvar in aktmodeswitches) then
  971. begin
  972. subeq:=proc_to_procvar_equal(tprocdef(def_from),tprocvardef(def_to),true);
  973. if subeq>te_incompatible then
  974. begin
  975. doconv:=tc_proc_2_procvar;
  976. eq:=te_convert_l1;
  977. end;
  978. end;
  979. end;
  980. procvardef :
  981. begin
  982. { procvar -> procvar }
  983. eq:=proc_to_procvar_equal(tprocvardef(def_from),tprocvardef(def_to),false);
  984. end;
  985. pointerdef :
  986. begin
  987. { nil is compatible with procvars }
  988. if (fromtreetype=niln) then
  989. begin
  990. doconv:=tc_equal;
  991. eq:=te_convert_l1;
  992. end
  993. else
  994. { for example delphi allows the assignement from pointers }
  995. { to procedure variables }
  996. if (m_pointer_2_procedure in aktmodeswitches) and
  997. is_void(tpointerdef(def_from).pointertype.def) and
  998. tprocvardef(def_to).is_addressonly then
  999. begin
  1000. doconv:=tc_equal;
  1001. eq:=te_convert_l1;
  1002. end;
  1003. end;
  1004. end;
  1005. end;
  1006. objectdef :
  1007. begin
  1008. { object pascal objects }
  1009. if (def_from.deftype=objectdef) and
  1010. (tobjectdef(def_from).is_related(tobjectdef(def_to))) then
  1011. begin
  1012. doconv:=tc_equal;
  1013. eq:=te_convert_l1;
  1014. end
  1015. else
  1016. { Class/interface specific }
  1017. if is_class_or_interface(def_to) then
  1018. begin
  1019. { void pointer also for delphi mode }
  1020. if (m_delphi in aktmodeswitches) and
  1021. is_voidpointer(def_from) then
  1022. begin
  1023. doconv:=tc_equal;
  1024. { prefer pointer-pointer assignments }
  1025. eq:=te_convert_l2;
  1026. end
  1027. else
  1028. { nil is compatible with class instances and interfaces }
  1029. if (fromtreetype=niln) then
  1030. begin
  1031. doconv:=tc_equal;
  1032. eq:=te_convert_l1;
  1033. end
  1034. { classes can be assigned to interfaces }
  1035. else if is_interface(def_to) and
  1036. is_class(def_from) and
  1037. assigned(tobjectdef(def_from).implementedinterfaces) then
  1038. begin
  1039. { we've to search in parent classes as well }
  1040. hd3:=tobjectdef(def_from);
  1041. while assigned(hd3) do
  1042. begin
  1043. if hd3.implementedinterfaces.searchintf(def_to)<>-1 then
  1044. begin
  1045. doconv:=tc_class_2_intf;
  1046. eq:=te_convert_l1;
  1047. break;
  1048. end;
  1049. hd3:=hd3.childof;
  1050. end;
  1051. end
  1052. { Interface 2 GUID handling }
  1053. else if (def_to=tdef(rec_tguid)) and
  1054. (fromtreetype=typen) and
  1055. is_interface(def_from) and
  1056. assigned(tobjectdef(def_from).iidguid) then
  1057. begin
  1058. eq:=te_convert_l1;
  1059. doconv:=tc_equal;
  1060. end
  1061. else if (def_from.deftype=variantdef) and is_interface(def_to) then
  1062. begin
  1063. doconv:=tc_variant_2_interface;
  1064. eq:=te_convert_l1;
  1065. end
  1066. { ugly, but delphi allows it }
  1067. else if (eq=te_incompatible) and
  1068. (def_from.deftype=orddef) and
  1069. (m_delphi in aktmodeswitches) and
  1070. (cdo_explicit in cdoptions) then
  1071. begin
  1072. doconv:=tc_int_2_int;
  1073. eq:=te_convert_l1;
  1074. end;
  1075. end;
  1076. end;
  1077. classrefdef :
  1078. begin
  1079. { similar to pointerdef wrt forwards }
  1080. if assigned(def_to.typesym) and
  1081. (tclassrefdef(def_to).pointertype.def.deftype=forwarddef) then
  1082. begin
  1083. if (def_from.typesym=def_to.typesym) then
  1084. eq:=te_equal;
  1085. end
  1086. else
  1087. { class reference types }
  1088. if (def_from.deftype=classrefdef) then
  1089. begin
  1090. if equal_defs(tclassrefdef(def_from).pointertype.def,tclassrefdef(def_to).pointertype.def) then
  1091. begin
  1092. eq:=te_equal;
  1093. end
  1094. else
  1095. begin
  1096. doconv:=tc_equal;
  1097. if (cdo_explicit in cdoptions) or
  1098. tobjectdef(tclassrefdef(def_from).pointertype.def).is_related(
  1099. tobjectdef(tclassrefdef(def_to).pointertype.def)) then
  1100. eq:=te_convert_l1;
  1101. end;
  1102. end
  1103. else
  1104. { nil is compatible with class references }
  1105. if (fromtreetype=niln) then
  1106. begin
  1107. doconv:=tc_equal;
  1108. eq:=te_convert_l1;
  1109. end;
  1110. end;
  1111. filedef :
  1112. begin
  1113. { typed files are all equal to the abstract file type
  1114. name TYPEDFILE in system.pp in is_equal in types.pas
  1115. the problem is that it sholud be also compatible to FILE
  1116. but this would leed to a problem for ASSIGN RESET and REWRITE
  1117. when trying to find the good overloaded function !!
  1118. so all file function are doubled in system.pp
  1119. this is not very beautiful !!}
  1120. if (def_from.deftype=filedef) then
  1121. begin
  1122. if (tfiledef(def_from).filetyp=tfiledef(def_to).filetyp) then
  1123. begin
  1124. if
  1125. (
  1126. (tfiledef(def_from).typedfiletype.def=nil) and
  1127. (tfiledef(def_to).typedfiletype.def=nil)
  1128. ) or
  1129. (
  1130. (tfiledef(def_from).typedfiletype.def<>nil) and
  1131. (tfiledef(def_to).typedfiletype.def<>nil) and
  1132. equal_defs(tfiledef(def_from).typedfiletype.def,tfiledef(def_to).typedfiletype.def)
  1133. ) or
  1134. (
  1135. (tfiledef(def_from).filetyp = ft_typed) and
  1136. (tfiledef(def_to).filetyp = ft_typed) and
  1137. (
  1138. (tfiledef(def_from).typedfiletype.def = tdef(voidtype.def)) or
  1139. (tfiledef(def_to).typedfiletype.def = tdef(voidtype.def))
  1140. )
  1141. ) then
  1142. begin
  1143. eq:=te_equal;
  1144. end;
  1145. end
  1146. else
  1147. if ((tfiledef(def_from).filetyp = ft_untyped) and
  1148. (tfiledef(def_to).filetyp = ft_typed)) or
  1149. ((tfiledef(def_from).filetyp = ft_typed) and
  1150. (tfiledef(def_to).filetyp = ft_untyped)) then
  1151. begin
  1152. doconv:=tc_equal;
  1153. eq:=te_convert_l1;
  1154. end;
  1155. end;
  1156. end;
  1157. recorddef :
  1158. begin
  1159. { interface -> guid }
  1160. if is_interface(def_from) and
  1161. (def_to=rec_tguid) then
  1162. begin
  1163. doconv:=tc_intf_2_guid;
  1164. eq:=te_convert_l1;
  1165. end;
  1166. end;
  1167. formaldef :
  1168. begin
  1169. doconv:=tc_equal;
  1170. if (def_from.deftype=formaldef) then
  1171. eq:=te_equal
  1172. else
  1173. { Just about everything can be converted to a formaldef...}
  1174. if not (def_from.deftype in [abstractdef,errordef]) then
  1175. eq:=te_convert_l1;
  1176. end;
  1177. end;
  1178. { if we didn't find an appropriate type conversion yet
  1179. then we search also the := operator }
  1180. if (eq=te_incompatible) and
  1181. (
  1182. { Check for variants? }
  1183. (
  1184. (cdo_allow_variant in cdoptions) and
  1185. ((def_from.deftype=variantdef) or (def_to.deftype=variantdef))
  1186. ) or
  1187. { Check for operators? }
  1188. (
  1189. (cdo_check_operator in cdoptions) and
  1190. ((def_from.deftype in [objectdef,recorddef,arraydef,stringdef,variantdef]) or
  1191. (def_to.deftype in [objectdef,recorddef,arraydef,stringdef,variantdef]))
  1192. )
  1193. ) then
  1194. begin
  1195. operatorpd:=search_assignment_operator(def_from,def_to);
  1196. if assigned(operatorpd) then
  1197. eq:=te_convert_operator;
  1198. end;
  1199. { update convtype for te_equal when it is not yet set }
  1200. if (eq=te_equal) and
  1201. (doconv=tc_not_possible) then
  1202. doconv:=tc_equal;
  1203. compare_defs_ext:=eq;
  1204. end;
  1205. function equal_defs(def_from,def_to:tdef):boolean;
  1206. var
  1207. convtyp : tconverttype;
  1208. pd : tprocdef;
  1209. begin
  1210. { Compare defs with nothingn and no explicit typecasts and
  1211. searching for overloaded operators is not needed }
  1212. equal_defs:=(compare_defs_ext(def_from,def_to,nothingn,convtyp,pd,[])>=te_equal);
  1213. end;
  1214. function compare_defs(def_from,def_to:tdef;fromtreetype:tnodetype):tequaltype;
  1215. var
  1216. doconv : tconverttype;
  1217. pd : tprocdef;
  1218. begin
  1219. compare_defs:=compare_defs_ext(def_from,def_to,fromtreetype,doconv,pd,[cdo_check_operator,cdo_allow_variant]);
  1220. end;
  1221. function is_subequal(def1, def2: tdef): boolean;
  1222. var
  1223. basedef1,basedef2 : tenumdef;
  1224. Begin
  1225. is_subequal := false;
  1226. if assigned(def1) and assigned(def2) then
  1227. Begin
  1228. if (def1.deftype = orddef) and (def2.deftype = orddef) then
  1229. Begin
  1230. { see p.47 of Turbo Pascal 7.01 manual for the separation of types }
  1231. { range checking for case statements is done with testrange }
  1232. case torddef(def1).typ of
  1233. u8bit,u16bit,u32bit,u64bit,
  1234. s8bit,s16bit,s32bit,s64bit :
  1235. is_subequal:=(torddef(def2).typ in [s64bit,u64bit,s32bit,u32bit,u8bit,s8bit,s16bit,u16bit]);
  1236. bool8bit,bool16bit,bool32bit :
  1237. is_subequal:=(torddef(def2).typ in [bool8bit,bool16bit,bool32bit]);
  1238. uchar :
  1239. is_subequal:=(torddef(def2).typ=uchar);
  1240. uwidechar :
  1241. is_subequal:=(torddef(def2).typ=uwidechar);
  1242. end;
  1243. end
  1244. else
  1245. Begin
  1246. { Check if both basedefs are equal }
  1247. if (def1.deftype=enumdef) and (def2.deftype=enumdef) then
  1248. Begin
  1249. { get both basedefs }
  1250. basedef1:=tenumdef(def1);
  1251. while assigned(basedef1.basedef) do
  1252. basedef1:=basedef1.basedef;
  1253. basedef2:=tenumdef(def2);
  1254. while assigned(basedef2.basedef) do
  1255. basedef2:=basedef2.basedef;
  1256. is_subequal:=(basedef1=basedef2);
  1257. end;
  1258. end;
  1259. end;
  1260. end;
  1261. function compare_paras(para1,para2 : tlist; acp : tcompare_paras_type; cpoptions: tcompare_paras_options):tequaltype;
  1262. var
  1263. currpara1,
  1264. currpara2 : tparavarsym;
  1265. eq,lowesteq : tequaltype;
  1266. hpd : tprocdef;
  1267. convtype : tconverttype;
  1268. cdoptions : tcompare_defs_options;
  1269. i1,i2 : byte;
  1270. begin
  1271. compare_paras:=te_incompatible;
  1272. cdoptions:=[cdo_check_operator,cdo_allow_variant];
  1273. { we need to parse the list from left-right so the
  1274. not-default parameters are checked first }
  1275. lowesteq:=high(tequaltype);
  1276. i1:=0;
  1277. i2:=0;
  1278. if cpo_ignorehidden in cpoptions then
  1279. begin
  1280. while (i1<para1.count) and
  1281. (vo_is_hidden_para in tparavarsym(para1[i1]).varoptions) do
  1282. inc(i1);
  1283. while (i2<para2.count) and
  1284. (vo_is_hidden_para in tparavarsym(para2[i2]).varoptions) do
  1285. inc(i2);
  1286. end;
  1287. while (i1<para1.count) and (i2<para2.count) do
  1288. begin
  1289. eq:=te_incompatible;
  1290. currpara1:=tparavarsym(para1[i1]);
  1291. currpara2:=tparavarsym(para2[i2]);
  1292. { Unique types must match exact }
  1293. if ((df_unique in currpara1.vartype.def.defoptions) or (df_unique in currpara2.vartype.def.defoptions)) and
  1294. (currpara1.vartype.def<>currpara2.vartype.def) then
  1295. exit;
  1296. { Handle hidden parameters separately, because self is
  1297. defined as voidpointer for methodpointers }
  1298. if (vo_is_hidden_para in currpara1.varoptions) or
  1299. (vo_is_hidden_para in currpara2.varoptions) then
  1300. begin
  1301. { both must be hidden }
  1302. if (vo_is_hidden_para in currpara1.varoptions)<>(vo_is_hidden_para in currpara2.varoptions) then
  1303. exit;
  1304. eq:=te_equal;
  1305. if not(vo_is_self in currpara1.varoptions) and
  1306. not(vo_is_self in currpara2.varoptions) then
  1307. begin
  1308. if (currpara1.varspez<>currpara2.varspez) then
  1309. exit;
  1310. eq:=compare_defs_ext(currpara1.vartype.def,currpara2.vartype.def,nothingn,
  1311. convtype,hpd,cdoptions);
  1312. end;
  1313. end
  1314. else
  1315. begin
  1316. case acp of
  1317. cp_value_equal_const :
  1318. begin
  1319. if (
  1320. (currpara1.varspez<>currpara2.varspez) and
  1321. ((currpara1.varspez in [vs_var,vs_out]) or
  1322. (currpara2.varspez in [vs_var,vs_out]))
  1323. ) then
  1324. exit;
  1325. eq:=compare_defs_ext(currpara1.vartype.def,currpara2.vartype.def,nothingn,
  1326. convtype,hpd,cdoptions);
  1327. end;
  1328. cp_all :
  1329. begin
  1330. if (currpara1.varspez<>currpara2.varspez) then
  1331. exit;
  1332. eq:=compare_defs_ext(currpara1.vartype.def,currpara2.vartype.def,nothingn,
  1333. convtype,hpd,cdoptions);
  1334. end;
  1335. cp_procvar :
  1336. begin
  1337. if (currpara1.varspez<>currpara2.varspez) then
  1338. exit;
  1339. eq:=compare_defs_ext(currpara1.vartype.def,currpara2.vartype.def,nothingn,
  1340. convtype,hpd,cdoptions);
  1341. { Parameters must be at least equal otherwise the are incompatible }
  1342. if (eq<te_equal) then
  1343. eq:=te_incompatible;
  1344. end;
  1345. else
  1346. eq:=compare_defs_ext(currpara1.vartype.def,currpara2.vartype.def,nothingn,
  1347. convtype,hpd,cdoptions);
  1348. end;
  1349. end;
  1350. { check type }
  1351. if eq=te_incompatible then
  1352. exit;
  1353. if eq<lowesteq then
  1354. lowesteq:=eq;
  1355. { also check default value if both have it declared }
  1356. if (cpo_comparedefaultvalue in cpoptions) and
  1357. assigned(currpara1.defaultconstsym) and
  1358. assigned(currpara2.defaultconstsym) then
  1359. begin
  1360. if not equal_constsym(tconstsym(currpara1.defaultconstsym),tconstsym(currpara2.defaultconstsym)) then
  1361. exit;
  1362. end;
  1363. inc(i1);
  1364. inc(i2);
  1365. if cpo_ignorehidden in cpoptions then
  1366. begin
  1367. while (i1<para1.count) and
  1368. (vo_is_hidden_para in tparavarsym(para1[i1]).varoptions) do
  1369. inc(i1);
  1370. while (i2<para2.count) and
  1371. (vo_is_hidden_para in tparavarsym(para2[i2]).varoptions) do
  1372. inc(i2);
  1373. end;
  1374. end;
  1375. { when both lists are empty then the parameters are equal. Also
  1376. when one list is empty and the other has a parameter with default
  1377. value assigned then the parameters are also equal }
  1378. if ((i1>=para1.count) and (i2>=para2.count)) or
  1379. ((cpo_allowdefaults in cpoptions) and
  1380. (((i1<para1.count) and assigned(tparavarsym(para1[i1]).defaultconstsym)) or
  1381. ((i2<para2.count) and assigned(tparavarsym(para2[i2]).defaultconstsym)))) then
  1382. compare_paras:=lowesteq;
  1383. end;
  1384. function proc_to_procvar_equal(def1:tabstractprocdef;def2:tprocvardef;methoderr:boolean):tequaltype;
  1385. var
  1386. eq : tequaltype;
  1387. po_comp : tprocoptions;
  1388. begin
  1389. proc_to_procvar_equal:=te_incompatible;
  1390. if not(assigned(def1)) or not(assigned(def2)) then
  1391. exit;
  1392. { check for method pointer }
  1393. if (def1.is_methodpointer xor def2.is_methodpointer) or
  1394. (def1.is_addressonly xor def2.is_addressonly) then
  1395. begin
  1396. if methoderr then
  1397. Message(type_e_no_method_and_procedure_not_compatible);
  1398. exit;
  1399. end;
  1400. { check return value and options, methodpointer is already checked }
  1401. po_comp:=[po_staticmethod,po_interrupt,
  1402. po_iocheck,po_varargs];
  1403. if (m_delphi in aktmodeswitches) then
  1404. exclude(po_comp,po_varargs);
  1405. if (def1.proccalloption=def2.proccalloption) and
  1406. ((po_comp * def1.procoptions)= (po_comp * def2.procoptions)) and
  1407. equal_defs(def1.rettype.def,def2.rettype.def) then
  1408. begin
  1409. { return equal type based on the parameters, but a proc->procvar
  1410. is never exact, so map an exact match of the parameters to
  1411. te_equal }
  1412. eq:=compare_paras(def1.paras,def2.paras,cp_procvar,[]);
  1413. if eq=te_exact then
  1414. eq:=te_equal;
  1415. proc_to_procvar_equal:=eq;
  1416. end;
  1417. end;
  1418. end.