defcmp.pas 65 KB

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