defcmp.pas 59 KB

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