defcmp.pas 71 KB

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