defcmp.pas 70 KB

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