defcmp.pas 88 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976
  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 = (
  29. cpo_allowdefaults,
  30. cpo_ignorehidden, // ignore hidden parameters
  31. cpo_allowconvert,
  32. cpo_comparedefaultvalue,
  33. cpo_openequalisexact,
  34. cpo_ignoreuniv,
  35. cpo_warn_incompatible_univ,
  36. cpo_ignorevarspez, // ignore parameter access type
  37. cpo_ignoreframepointer, // ignore frame pointer parameter (for assignment-compatibility of global procedures to nested procvars)
  38. cpo_compilerproc,
  39. cpo_rtlproc
  40. );
  41. tcompare_paras_options = set of tcompare_paras_option;
  42. tcompare_defs_option = (
  43. cdo_internal,
  44. cdo_explicit,
  45. cdo_check_operator,
  46. cdo_allow_variant,
  47. cdo_parameter,
  48. cdo_warn_incompatible_univ,
  49. cdo_strict_undefined_check // undefined defs are incompatible to everything except other undefined defs
  50. );
  51. tcompare_defs_options = set of tcompare_defs_option;
  52. tconverttype = (tc_none,
  53. tc_equal,
  54. tc_not_possible,
  55. tc_string_2_string,
  56. tc_char_2_string,
  57. tc_char_2_chararray,
  58. tc_pchar_2_string,
  59. tc_cchar_2_pchar,
  60. tc_cstring_2_pchar,
  61. tc_cstring_2_int,
  62. tc_ansistring_2_pchar,
  63. tc_string_2_chararray,
  64. tc_chararray_2_string,
  65. tc_array_2_pointer,
  66. tc_pointer_2_array,
  67. tc_int_2_int,
  68. tc_int_2_bool,
  69. tc_bool_2_bool,
  70. tc_bool_2_int,
  71. tc_real_2_real,
  72. tc_int_2_real,
  73. tc_real_2_currency,
  74. tc_proc_2_procvar,
  75. tc_nil_2_methodprocvar,
  76. tc_arrayconstructor_2_set,
  77. tc_set_to_set,
  78. tc_cord_2_pointer,
  79. tc_intf_2_string,
  80. tc_intf_2_guid,
  81. tc_class_2_intf,
  82. tc_char_2_char,
  83. tc_dynarray_2_openarray,
  84. tc_pwchar_2_string,
  85. tc_variant_2_dynarray,
  86. tc_dynarray_2_variant,
  87. tc_variant_2_enum,
  88. tc_enum_2_variant,
  89. tc_interface_2_variant,
  90. tc_variant_2_interface,
  91. tc_array_2_dynarray
  92. );
  93. function compare_defs_ext(def_from,def_to : tdef;
  94. fromtreetype : tnodetype;
  95. var doconv : tconverttype;
  96. var operatorpd : tprocdef;
  97. cdoptions:tcompare_defs_options):tequaltype;
  98. { Returns if the type def_from can be converted to def_to or if both types are equal }
  99. function compare_defs(def_from,def_to:tdef;fromtreetype:tnodetype):tequaltype;
  100. { Returns true, if def1 and def2 are semantically the same }
  101. function equal_defs(def_from,def_to:tdef):boolean;
  102. { Checks for type compatibility (subgroups of type)
  103. used for case statements... probably missing stuff
  104. to use on other types }
  105. function is_subequal(def1, def2: tdef): boolean;
  106. {# true, if two parameter lists are equal
  107. if acp is cp_all, all have to match exactly
  108. if acp is cp_value_equal_const call by value
  109. and call by const parameter are assumed as
  110. equal
  111. if acp is cp_procvar then the varspez have to match,
  112. and all parameter types must be at least te_equal
  113. if acp is cp_none, then we don't check the varspez at all
  114. allowdefaults indicates if default value parameters
  115. are allowed (in this case, the search order will first
  116. search for a routine with default parameters, before
  117. searching for the same definition with no parameters)
  118. }
  119. function compare_paras(para1,para2 : TFPObjectList; acp : tcompare_paras_type; cpoptions: tcompare_paras_options):tequaltype;
  120. { True if a function can be assigned to a procvar }
  121. { changed first argument type to pabstractprocdef so that it can also be }
  122. { used to test compatibility between two pprocvardefs (JM) }
  123. function proc_to_procvar_equal(def1:tabstractprocdef;def2:tprocvardef;checkincompatibleuniv: boolean):tequaltype;
  124. { Parentdef is the definition of a method defined in a parent class or interface }
  125. { Childdef is the definition of a method defined in a child class, interface or }
  126. { a class implementing an interface with parentdef. }
  127. { Returns true if the resultdef of childdef can be used to implement/override }
  128. { parentdef's resultdef }
  129. function compatible_childmethod_resultdef(parentretdef, childretdef: tdef): boolean;
  130. implementation
  131. uses
  132. verbose,systems,constexp,
  133. symtable,symsym,
  134. defutil,symutil;
  135. function compare_defs_ext(def_from,def_to : tdef;
  136. fromtreetype : tnodetype;
  137. var doconv : tconverttype;
  138. var operatorpd : tprocdef;
  139. cdoptions:tcompare_defs_options):tequaltype;
  140. { tordtype:
  141. uvoid,
  142. u8bit,u16bit,u32bit,u64bit,
  143. s8bit,s16bit,s32bit,s64bit,
  144. pasbool, bool8bit,bool16bit,bool32bit,bool64bit,
  145. uchar,uwidechar,scurrency }
  146. type
  147. tbasedef=(bvoid,bchar,bint,bbool);
  148. const
  149. basedeftbl:array[tordtype] of tbasedef =
  150. (bvoid,
  151. bint,bint,bint,bint,
  152. bint,bint,bint,bint,
  153. bbool,bbool,bbool,bbool,
  154. bbool,bbool,bbool,bbool,
  155. bchar,bchar,bint);
  156. basedefconvertsimplicit : array[tbasedef,tbasedef] of tconverttype =
  157. { void, char, int, bool }
  158. ((tc_not_possible,tc_not_possible,tc_not_possible,tc_not_possible),
  159. (tc_not_possible,tc_char_2_char,tc_not_possible,tc_not_possible),
  160. (tc_not_possible,tc_not_possible,tc_int_2_int,tc_not_possible),
  161. (tc_not_possible,tc_not_possible,tc_not_possible,tc_bool_2_bool));
  162. basedefconvertsexplicit : array[tbasedef,tbasedef] of tconverttype =
  163. { void, char, int, bool }
  164. ((tc_not_possible,tc_not_possible,tc_not_possible,tc_not_possible),
  165. (tc_not_possible,tc_char_2_char,tc_int_2_int,tc_int_2_bool),
  166. (tc_not_possible,tc_int_2_int,tc_int_2_int,tc_int_2_bool),
  167. (tc_not_possible,tc_bool_2_int,tc_bool_2_int,tc_bool_2_bool));
  168. var
  169. subeq,eq : tequaltype;
  170. hd1,hd2 : tdef;
  171. hct : tconverttype;
  172. hobjdef : tobjectdef;
  173. hpd : tprocdef;
  174. begin
  175. eq:=te_incompatible;
  176. doconv:=tc_not_possible;
  177. { safety check }
  178. if not(assigned(def_from) and assigned(def_to)) then
  179. begin
  180. compare_defs_ext:=te_incompatible;
  181. exit;
  182. end;
  183. { same def? then we've an exact match }
  184. if def_from=def_to then
  185. begin
  186. doconv:=tc_equal;
  187. compare_defs_ext:=te_exact;
  188. exit;
  189. end;
  190. if cdo_strict_undefined_check in cdoptions then
  191. begin
  192. { undefined defs are considered equal if both are undefined defs }
  193. if (def_from.typ=undefineddef) and
  194. (def_to.typ=undefineddef) then
  195. begin
  196. doconv:=tc_equal;
  197. compare_defs_ext:=te_exact;
  198. exit;
  199. end;
  200. { if only one def is a undefined def then they are not considered as
  201. equal}
  202. if (def_from.typ=undefineddef) or
  203. (def_to.typ=undefineddef) then
  204. begin
  205. doconv:=tc_not_possible;
  206. compare_defs_ext:=te_incompatible;
  207. exit;
  208. end;
  209. end
  210. else
  211. begin
  212. { undefined defs are considered equal }
  213. if (def_from.typ=undefineddef) or
  214. (def_to.typ=undefineddef) then
  215. begin
  216. doconv:=tc_equal;
  217. compare_defs_ext:=te_exact;
  218. exit;
  219. end;
  220. end;
  221. { we walk the wanted (def_to) types and check then the def_from
  222. types if there is a conversion possible }
  223. case def_to.typ of
  224. orddef :
  225. begin
  226. case def_from.typ of
  227. orddef :
  228. begin
  229. if (torddef(def_from).ordtype=torddef(def_to).ordtype) then
  230. begin
  231. case torddef(def_from).ordtype of
  232. uchar,uwidechar,
  233. u8bit,u16bit,u32bit,u64bit,
  234. s8bit,s16bit,s32bit,s64bit:
  235. begin
  236. if (torddef(def_from).low>=torddef(def_to).low) and
  237. (torddef(def_from).high<=torddef(def_to).high) then
  238. eq:=te_equal
  239. else
  240. begin
  241. doconv:=tc_int_2_int;
  242. eq:=te_convert_l1;
  243. end;
  244. end;
  245. uvoid,
  246. pasbool8,pasbool16,pasbool32,pasbool64,
  247. bool8bit,bool16bit,bool32bit,bool64bit:
  248. eq:=te_equal;
  249. else
  250. internalerror(200210061);
  251. end;
  252. end
  253. else
  254. begin
  255. if cdo_explicit in cdoptions then
  256. doconv:=basedefconvertsexplicit[basedeftbl[torddef(def_from).ordtype],basedeftbl[torddef(def_to).ordtype]]
  257. else
  258. doconv:=basedefconvertsimplicit[basedeftbl[torddef(def_from).ordtype],basedeftbl[torddef(def_to).ordtype]];
  259. if (doconv=tc_not_possible) then
  260. eq:=te_incompatible
  261. else if (not is_in_limit(def_from,def_to)) then
  262. { "punish" bad type conversions :) (JM) }
  263. eq:=te_convert_l3
  264. else
  265. eq:=te_convert_l1;
  266. end;
  267. end;
  268. enumdef :
  269. begin
  270. { needed for char(enum) }
  271. if cdo_explicit in cdoptions then
  272. begin
  273. doconv:=tc_int_2_int;
  274. eq:=te_convert_l1;
  275. end;
  276. end;
  277. floatdef :
  278. begin
  279. if is_currency(def_to) then
  280. begin
  281. doconv:=tc_real_2_currency;
  282. eq:=te_convert_l2;
  283. end;
  284. end;
  285. objectdef:
  286. begin
  287. if (m_delphi in current_settings.modeswitches) and
  288. is_implicit_pointer_object_type(def_from) and
  289. (cdo_explicit in cdoptions) then
  290. begin
  291. eq:=te_convert_l1;
  292. if (fromtreetype=niln) then
  293. begin
  294. { will be handled by the constant folding }
  295. doconv:=tc_equal;
  296. end
  297. else
  298. doconv:=tc_int_2_int;
  299. end;
  300. end;
  301. classrefdef,
  302. procvardef,
  303. pointerdef :
  304. begin
  305. if cdo_explicit in cdoptions then
  306. begin
  307. eq:=te_convert_l1;
  308. if (fromtreetype=niln) then
  309. begin
  310. { will be handled by the constant folding }
  311. doconv:=tc_equal;
  312. end
  313. else
  314. doconv:=tc_int_2_int;
  315. end;
  316. end;
  317. arraydef :
  318. begin
  319. if (m_mac in current_settings.modeswitches) and
  320. (fromtreetype=stringconstn) then
  321. begin
  322. eq:=te_convert_l3;
  323. doconv:=tc_cstring_2_int;
  324. end;
  325. end;
  326. end;
  327. end;
  328. stringdef :
  329. begin
  330. case def_from.typ of
  331. stringdef :
  332. begin
  333. { Constant string }
  334. if (fromtreetype=stringconstn) then
  335. begin
  336. if (tstringdef(def_from).stringtype=tstringdef(def_to).stringtype) and
  337. ((tstringdef(def_from).stringtype<>st_ansistring) or
  338. (tstringdef(def_from).encoding=tstringdef(def_to).encoding)
  339. ) then
  340. eq:=te_equal
  341. else
  342. begin
  343. doconv:=tc_string_2_string;
  344. if (tstringdef(def_from).stringtype = st_ansistring) and
  345. (tstringdef(def_to).stringtype = st_ansistring) then
  346. if (tstringdef(def_to).encoding=globals.CP_UTF8) then
  347. eq:=te_convert_l1
  348. else
  349. eq:=te_convert_l2
  350. else
  351. begin
  352. { Don't prefer conversions from widestring to a
  353. normal string as we can lose information }
  354. if (tstringdef(def_from).stringtype in [st_widestring,st_unicodestring]) and
  355. not (tstringdef(def_to).stringtype in [st_widestring,st_unicodestring]) then
  356. eq:=te_convert_l3
  357. else if tstringdef(def_to).stringtype in [st_widestring,st_unicodestring] then
  358. eq:=te_convert_l2
  359. else
  360. eq:=te_convert_l1;
  361. end;
  362. end;
  363. end
  364. else if (tstringdef(def_to).stringtype=st_ansistring) and
  365. (tstringdef(def_from).stringtype=st_ansistring) then
  366. begin
  367. { don't convert ansistrings if any condition is true:
  368. 1) same encoding
  369. 2) from explicit codepage ansistring to ansistring and vice versa
  370. 3) from any ansistring to rawbytestring
  371. 4) from rawbytestring to any ansistring }
  372. if (tstringdef(def_from).encoding=tstringdef(def_to).encoding) or
  373. ((tstringdef(def_to).encoding=0) and (tstringdef(def_from).encoding=getansistringcodepage)) or
  374. ((tstringdef(def_to).encoding=getansistringcodepage) and (tstringdef(def_from).encoding=0)) or
  375. (tstringdef(def_to).encoding=globals.CP_NONE) or
  376. (tstringdef(def_from).encoding=globals.CP_NONE) then
  377. begin
  378. eq:=te_equal;
  379. end
  380. else
  381. begin
  382. doconv := tc_string_2_string;
  383. if (tstringdef(def_to).encoding=globals.CP_UTF8) then
  384. eq:=te_convert_l1
  385. else
  386. eq:=te_convert_l2;
  387. end
  388. end
  389. else
  390. { same string type ? }
  391. if (tstringdef(def_from).stringtype=tstringdef(def_to).stringtype) and
  392. { for shortstrings also the length must match }
  393. ((tstringdef(def_from).stringtype<>st_shortstring) or
  394. (tstringdef(def_from).len=tstringdef(def_to).len)) and
  395. { for ansi- and unicodestrings also the encoding must match }
  396. (not(tstringdef(def_from).stringtype in [st_ansistring,st_unicodestring]) or
  397. (tstringdef(def_from).encoding=tstringdef(def_to).encoding)) then
  398. eq:=te_equal
  399. else
  400. begin
  401. doconv:=tc_string_2_string;
  402. case tstringdef(def_from).stringtype of
  403. st_widestring :
  404. begin
  405. { Prefer conversions to unicodestring }
  406. if tstringdef(def_to).stringtype=st_unicodestring then
  407. eq:=te_convert_l1
  408. { else prefer conversions to ansistring }
  409. else if tstringdef(def_to).stringtype=st_ansistring then
  410. eq:=te_convert_l2
  411. else
  412. eq:=te_convert_l3;
  413. end;
  414. st_unicodestring :
  415. begin
  416. { Prefer conversions to widestring }
  417. if tstringdef(def_to).stringtype=st_widestring then
  418. eq:=te_convert_l1
  419. { else prefer conversions to ansistring }
  420. else if tstringdef(def_to).stringtype=st_ansistring then
  421. eq:=te_convert_l2
  422. else
  423. eq:=te_convert_l3;
  424. end;
  425. st_shortstring :
  426. begin
  427. { Prefer shortstrings of different length or conversions
  428. from shortstring to ansistring }
  429. if (tstringdef(def_to).stringtype=st_shortstring) then
  430. eq:=te_convert_l1
  431. else if tstringdef(def_to).stringtype=st_ansistring then
  432. eq:=te_convert_l2
  433. else
  434. eq:=te_convert_l3;
  435. end;
  436. st_ansistring :
  437. begin
  438. { Prefer conversion to widestrings }
  439. if (tstringdef(def_to).stringtype in [st_widestring,st_unicodestring]) then
  440. eq:=te_convert_l2
  441. else
  442. eq:=te_convert_l3;
  443. end;
  444. end;
  445. end;
  446. end;
  447. orddef :
  448. begin
  449. { char to string}
  450. if is_char(def_from) or
  451. is_widechar(def_from) then
  452. begin
  453. doconv:=tc_char_2_string;
  454. eq:=te_convert_l1;
  455. end;
  456. end;
  457. arraydef :
  458. begin
  459. { array of char to string, the length check is done by the firstpass of this node }
  460. if is_chararray(def_from) or is_open_chararray(def_from) then
  461. begin
  462. { "Untyped" stringconstn is an array of char }
  463. if fromtreetype=stringconstn then
  464. begin
  465. doconv:=tc_string_2_string;
  466. { prefered string type depends on the $H switch }
  467. if not(cs_ansistrings in current_settings.localswitches) and
  468. (tstringdef(def_to).stringtype=st_shortstring) then
  469. eq:=te_equal
  470. else if (cs_ansistrings in current_settings.localswitches) and
  471. (tstringdef(def_to).stringtype=st_ansistring) then
  472. eq:=te_equal
  473. else if tstringdef(def_to).stringtype in [st_widestring,st_unicodestring] then
  474. eq:=te_convert_l3
  475. else
  476. eq:=te_convert_l1;
  477. end
  478. else
  479. begin
  480. doconv:=tc_chararray_2_string;
  481. if is_open_array(def_from) then
  482. begin
  483. if is_ansistring(def_to) then
  484. eq:=te_convert_l1
  485. else if is_widestring(def_to) or is_unicodestring(def_to) then
  486. eq:=te_convert_l3
  487. else
  488. eq:=te_convert_l2;
  489. end
  490. else
  491. begin
  492. if is_shortstring(def_to) then
  493. begin
  494. { Only compatible with arrays that fit
  495. smaller than 255 chars }
  496. if (def_from.size <= 255) then
  497. eq:=te_convert_l1;
  498. end
  499. else if is_ansistring(def_to) then
  500. begin
  501. if (def_from.size > 255) then
  502. eq:=te_convert_l1
  503. else
  504. eq:=te_convert_l2;
  505. end
  506. else if is_widestring(def_to) or is_unicodestring(def_to) then
  507. eq:=te_convert_l3
  508. else
  509. eq:=te_convert_l2;
  510. end;
  511. end;
  512. end
  513. else
  514. { array of widechar to string, the length check is done by the firstpass of this node }
  515. if is_widechararray(def_from) or is_open_widechararray(def_from) then
  516. begin
  517. doconv:=tc_chararray_2_string;
  518. if is_widestring(def_to) or is_unicodestring(def_to) then
  519. eq:=te_convert_l1
  520. else
  521. { size of widechar array is double due the sizeof a widechar }
  522. if not(is_shortstring(def_to) and (is_open_widechararray(def_from) or (def_from.size>255*sizeof(widechar)))) then
  523. eq:=te_convert_l3
  524. else
  525. eq:=te_convert_l2;
  526. end;
  527. end;
  528. pointerdef :
  529. begin
  530. { pchar can be assigned to short/ansistrings,
  531. but not in tp7 compatible mode }
  532. if not(m_tp7 in current_settings.modeswitches) then
  533. begin
  534. if is_pchar(def_from) then
  535. begin
  536. doconv:=tc_pchar_2_string;
  537. { prefer ansistrings because pchars can overflow shortstrings, }
  538. { but only if ansistrings are the default (JM) }
  539. if (is_shortstring(def_to) and
  540. not(cs_ansistrings in current_settings.localswitches)) or
  541. (is_ansistring(def_to) and
  542. (cs_ansistrings in current_settings.localswitches)) then
  543. eq:=te_convert_l1
  544. else
  545. eq:=te_convert_l2;
  546. end
  547. else if is_pwidechar(def_from) then
  548. begin
  549. doconv:=tc_pwchar_2_string;
  550. if is_widestring(def_to) or is_unicodestring(def_to) then
  551. eq:=te_convert_l1
  552. else
  553. eq:=te_convert_l3;
  554. end;
  555. end;
  556. end;
  557. objectdef :
  558. begin
  559. { corba interface -> id string }
  560. if is_interfacecorba(def_from) then
  561. begin
  562. doconv:=tc_intf_2_string;
  563. eq:=te_convert_l1;
  564. end;
  565. end;
  566. end;
  567. end;
  568. floatdef :
  569. begin
  570. case def_from.typ of
  571. orddef :
  572. begin { ordinal to real }
  573. { only for implicit and internal typecasts in tp/delphi }
  574. if (([cdo_explicit,cdo_internal] * cdoptions <> [cdo_explicit]) or
  575. ([m_tp7,m_delphi] * current_settings.modeswitches = [])) and
  576. (is_integer(def_from) or
  577. (is_currency(def_from) and
  578. (s64currencytype.typ = floatdef))) then
  579. begin
  580. doconv:=tc_int_2_real;
  581. { prefer single over others }
  582. if is_single(def_to) then
  583. eq:=te_convert_l3
  584. else
  585. eq:=te_convert_l4;
  586. end
  587. else if is_currency(def_from)
  588. { and (s64currencytype.typ = orddef)) } then
  589. begin
  590. { prefer conversion to orddef in this case, unless }
  591. { the orddef < currency (then it will get convert l3, }
  592. { and conversion to float is favoured) }
  593. doconv:=tc_int_2_real;
  594. eq:=te_convert_l2;
  595. end;
  596. end;
  597. floatdef :
  598. begin
  599. if tfloatdef(def_from).floattype=tfloatdef(def_to).floattype then
  600. eq:=te_equal
  601. else
  602. begin
  603. { Delphi does not allow explicit type conversions for float types like:
  604. single_var:=single(double_var);
  605. But if such conversion is inserted by compiler (internal) for some purpose,
  606. it should be allowed even in Delphi mode. }
  607. if (fromtreetype=realconstn) or
  608. not((cdoptions*[cdo_explicit,cdo_internal]=[cdo_explicit]) and
  609. (m_delphi in current_settings.modeswitches)) then
  610. begin
  611. doconv:=tc_real_2_real;
  612. { do we lose precision? }
  613. if (def_to.size<def_from.size) or
  614. (is_currency(def_from) and (tfloatdef(def_to).floattype in [s32real,s64real])) then
  615. eq:=te_convert_l2
  616. else
  617. eq:=te_convert_l1;
  618. end;
  619. end;
  620. end;
  621. end;
  622. end;
  623. enumdef :
  624. begin
  625. case def_from.typ of
  626. enumdef :
  627. begin
  628. if cdo_explicit in cdoptions then
  629. begin
  630. eq:=te_convert_l1;
  631. doconv:=tc_int_2_int;
  632. end
  633. else
  634. begin
  635. hd1:=def_from;
  636. while assigned(tenumdef(hd1).basedef) do
  637. hd1:=tenumdef(hd1).basedef;
  638. hd2:=def_to;
  639. while assigned(tenumdef(hd2).basedef) do
  640. hd2:=tenumdef(hd2).basedef;
  641. if (hd1=hd2) then
  642. begin
  643. eq:=te_convert_l1;
  644. { because of packenum they can have different sizes! (JM) }
  645. doconv:=tc_int_2_int;
  646. end
  647. else
  648. begin
  649. { assignment of an enum symbol to an unique type? }
  650. if (fromtreetype=ordconstn) and
  651. (tenumsym(tenumdef(hd1).getfirstsym)=tenumsym(tenumdef(hd2).getfirstsym)) then
  652. begin
  653. { because of packenum they can have different sizes! (JM) }
  654. eq:=te_convert_l1;
  655. doconv:=tc_int_2_int;
  656. end;
  657. end;
  658. end;
  659. end;
  660. orddef :
  661. begin
  662. if cdo_explicit in cdoptions then
  663. begin
  664. eq:=te_convert_l1;
  665. doconv:=tc_int_2_int;
  666. end;
  667. end;
  668. variantdef :
  669. begin
  670. eq:=te_convert_l1;
  671. doconv:=tc_variant_2_enum;
  672. end;
  673. pointerdef :
  674. begin
  675. { ugly, but delphi allows it }
  676. if (cdo_explicit in cdoptions) and
  677. (m_delphi in current_settings.modeswitches) then
  678. begin
  679. doconv:=tc_int_2_int;
  680. eq:=te_convert_l1;
  681. end;
  682. end;
  683. objectdef:
  684. begin
  685. { ugly, but delphi allows it }
  686. if (m_delphi in current_settings.modeswitches) and
  687. is_class_or_interface_or_dispinterface(def_from) and
  688. (cdo_explicit in cdoptions) then
  689. begin
  690. doconv:=tc_int_2_int;
  691. eq:=te_convert_l1;
  692. end;
  693. end;
  694. end;
  695. end;
  696. arraydef :
  697. begin
  698. { open array is also compatible with a single element of its base type.
  699. the extra check for deftyp is needed because equal defs can also return
  700. true if the def types are not the same, for example with dynarray to pointer. }
  701. if is_open_array(def_to) and
  702. (def_from.typ=tarraydef(def_to).elementdef.typ) and
  703. equal_defs(def_from,tarraydef(def_to).elementdef) then
  704. begin
  705. doconv:=tc_equal;
  706. { also update in htypechk.pas/var_para_allowed if changed
  707. here }
  708. eq:=te_convert_l3;
  709. end
  710. else
  711. begin
  712. case def_from.typ of
  713. arraydef :
  714. begin
  715. { from/to packed array -- packed chararrays are }
  716. { strings in ISO Pascal (at least if the lower bound }
  717. { is 1, but GPC makes all equal-length chararrays }
  718. { compatible), so treat those the same as regular }
  719. { char arrays }
  720. if (is_packed_array(def_from) and
  721. not is_chararray(def_from) and
  722. not is_widechararray(def_from)) xor
  723. (is_packed_array(def_to) and
  724. not is_chararray(def_to) and
  725. not is_widechararray(def_to)) then
  726. { both must be packed }
  727. begin
  728. compare_defs_ext:=te_incompatible;
  729. exit;
  730. end
  731. { to dynamic array }
  732. else if is_dynamic_array(def_to) then
  733. begin
  734. if equal_defs(tarraydef(def_from).elementdef,tarraydef(def_to).elementdef) then
  735. begin
  736. { dynamic array -> dynamic array }
  737. if is_dynamic_array(def_from) then
  738. eq:=te_equal
  739. { fpc modes only: array -> dyn. array }
  740. else if (current_settings.modeswitches*[m_objfpc,m_fpc]<>[]) and
  741. not(is_special_array(def_from)) and
  742. is_zero_based_array(def_from) then
  743. begin
  744. eq:=te_convert_l2;
  745. doconv:=tc_array_2_dynarray;
  746. end;
  747. end
  748. end
  749. else
  750. { to open array }
  751. if is_open_array(def_to) then
  752. begin
  753. { array constructor -> open array }
  754. if is_array_constructor(def_from) then
  755. begin
  756. if is_void(tarraydef(def_from).elementdef) then
  757. begin
  758. doconv:=tc_equal;
  759. eq:=te_convert_l1;
  760. end
  761. else
  762. begin
  763. subeq:=compare_defs_ext(tarraydef(def_from).elementdef,
  764. tarraydef(def_to).elementdef,
  765. { reason for cdo_allow_variant: see webtbs/tw7070a and webtbs/tw7070b }
  766. arrayconstructorn,hct,hpd,[cdo_check_operator,cdo_allow_variant]);
  767. if (subeq>=te_equal) then
  768. begin
  769. doconv:=tc_equal;
  770. eq:=te_convert_l1;
  771. end
  772. else
  773. if (subeq>te_incompatible) then
  774. begin
  775. doconv:=hct;
  776. eq:=te_convert_l2;
  777. end;
  778. end;
  779. end
  780. else
  781. { dynamic array -> open array }
  782. if is_dynamic_array(def_from) and
  783. equal_defs(tarraydef(def_from).elementdef,tarraydef(def_to).elementdef) then
  784. begin
  785. doconv:=tc_dynarray_2_openarray;
  786. eq:=te_convert_l2;
  787. end
  788. else
  789. { open array -> open array }
  790. if is_open_array(def_from) and
  791. equal_defs(tarraydef(def_from).elementdef,tarraydef(def_to).elementdef) then
  792. if tarraydef(def_from).elementdef=tarraydef(def_to).elementdef then
  793. eq:=te_exact
  794. else
  795. eq:=te_equal
  796. else
  797. { array -> open array }
  798. if not(cdo_parameter in cdoptions) and
  799. equal_defs(tarraydef(def_from).elementdef,tarraydef(def_to).elementdef) then
  800. begin
  801. if fromtreetype=stringconstn then
  802. eq:=te_convert_l1
  803. else
  804. eq:=te_equal;
  805. end;
  806. end
  807. else
  808. { to array of const }
  809. if is_array_of_const(def_to) then
  810. begin
  811. if is_array_of_const(def_from) or
  812. is_array_constructor(def_from) then
  813. begin
  814. eq:=te_equal;
  815. end
  816. else
  817. { array of tvarrec -> array of const }
  818. if equal_defs(tarraydef(def_to).elementdef,tarraydef(def_from).elementdef) then
  819. begin
  820. doconv:=tc_equal;
  821. eq:=te_convert_l1;
  822. end;
  823. end
  824. else
  825. { to array of char, from "Untyped" stringconstn (array of char) }
  826. if (fromtreetype=stringconstn) and
  827. (is_chararray(def_to) or
  828. is_widechararray(def_to)) then
  829. begin
  830. eq:=te_convert_l1;
  831. doconv:=tc_string_2_chararray;
  832. end
  833. else
  834. { other arrays }
  835. begin
  836. { open array -> array }
  837. if not(cdo_parameter in cdoptions) and
  838. is_open_array(def_from) and
  839. equal_defs(tarraydef(def_from).elementdef,tarraydef(def_to).elementdef) then
  840. begin
  841. eq:=te_equal
  842. end
  843. else
  844. { array -> array }
  845. if not(m_tp7 in current_settings.modeswitches) and
  846. not(m_delphi in current_settings.modeswitches) and
  847. (tarraydef(def_from).lowrange=tarraydef(def_to).lowrange) and
  848. (tarraydef(def_from).highrange=tarraydef(def_to).highrange) and
  849. equal_defs(tarraydef(def_from).elementdef,tarraydef(def_to).elementdef) and
  850. equal_defs(tarraydef(def_from).rangedef,tarraydef(def_to).rangedef) then
  851. begin
  852. eq:=te_equal
  853. end;
  854. end;
  855. end;
  856. pointerdef :
  857. begin
  858. { nil and voidpointers are compatible with dyn. arrays }
  859. if is_dynamic_array(def_to) and
  860. ((fromtreetype=niln) or
  861. is_voidpointer(def_from)) then
  862. begin
  863. doconv:=tc_equal;
  864. eq:=te_convert_l1;
  865. end
  866. else
  867. if is_zero_based_array(def_to) and
  868. equal_defs(tpointerdef(def_from).pointeddef,tarraydef(def_to).elementdef) then
  869. begin
  870. doconv:=tc_pointer_2_array;
  871. eq:=te_convert_l1;
  872. end;
  873. end;
  874. stringdef :
  875. begin
  876. { string to char array }
  877. if (not is_special_array(def_to)) and
  878. (is_char(tarraydef(def_to).elementdef)or
  879. is_widechar(tarraydef(def_to).elementdef)) then
  880. begin
  881. doconv:=tc_string_2_chararray;
  882. eq:=te_convert_l1;
  883. end;
  884. end;
  885. orddef:
  886. begin
  887. if is_chararray(def_to) and
  888. is_char(def_from) then
  889. begin
  890. doconv:=tc_char_2_chararray;
  891. eq:=te_convert_l2;
  892. end;
  893. end;
  894. recorddef :
  895. begin
  896. { tvarrec -> array of const }
  897. if is_array_of_const(def_to) and
  898. equal_defs(def_from,tarraydef(def_to).elementdef) then
  899. begin
  900. doconv:=tc_equal;
  901. eq:=te_convert_l1;
  902. end;
  903. end;
  904. variantdef :
  905. begin
  906. if is_dynamic_array(def_to) then
  907. begin
  908. doconv:=tc_variant_2_dynarray;
  909. eq:=te_convert_l1;
  910. end;
  911. end;
  912. end;
  913. end;
  914. end;
  915. variantdef :
  916. begin
  917. if (cdo_allow_variant in cdoptions) then
  918. begin
  919. case def_from.typ of
  920. enumdef :
  921. begin
  922. doconv:=tc_enum_2_variant;
  923. eq:=te_convert_l1;
  924. end;
  925. arraydef :
  926. begin
  927. if is_dynamic_array(def_from) then
  928. begin
  929. doconv:=tc_dynarray_2_variant;
  930. eq:=te_convert_l1;
  931. end;
  932. end;
  933. objectdef :
  934. begin
  935. { corbainterfaces not accepted, until we have
  936. runtime support for them in Variants (sergei) }
  937. if is_interfacecom_or_dispinterface(def_from) then
  938. begin
  939. doconv:=tc_interface_2_variant;
  940. eq:=te_convert_l1;
  941. end;
  942. end;
  943. variantdef :
  944. begin
  945. { doing this in the compiler avoids a lot of unncessary
  946. copying }
  947. if (tvariantdef(def_from).varianttype=vt_olevariant) and
  948. (tvariantdef(def_to).varianttype=vt_normalvariant) then
  949. begin
  950. doconv:=tc_equal;
  951. eq:=te_convert_l1;
  952. end;
  953. end;
  954. end;
  955. end;
  956. end;
  957. pointerdef :
  958. begin
  959. case def_from.typ of
  960. stringdef :
  961. begin
  962. { string constant (which can be part of array constructor)
  963. to zero terminated string constant }
  964. if (fromtreetype = stringconstn) and
  965. (is_pchar(def_to) or is_pwidechar(def_to)) then
  966. begin
  967. doconv:=tc_cstring_2_pchar;
  968. eq:=te_convert_l2;
  969. end
  970. else
  971. if (cdo_explicit in cdoptions) or (fromtreetype = arrayconstructorn) then
  972. begin
  973. { pchar(ansistring) }
  974. if is_pchar(def_to) and
  975. is_ansistring(def_from) then
  976. begin
  977. doconv:=tc_ansistring_2_pchar;
  978. eq:=te_convert_l1;
  979. end
  980. else
  981. { pwidechar(widestring) }
  982. if is_pwidechar(def_to) and
  983. is_wide_or_unicode_string(def_from) then
  984. begin
  985. doconv:=tc_ansistring_2_pchar;
  986. eq:=te_convert_l1;
  987. end;
  988. end;
  989. end;
  990. orddef :
  991. begin
  992. { char constant to zero terminated string constant }
  993. if (fromtreetype in [ordconstn,arrayconstructorn]) then
  994. begin
  995. if (is_char(def_from) or is_widechar(def_from)) and
  996. (is_pchar(def_to) or is_pwidechar(def_to)) then
  997. begin
  998. doconv:=tc_cchar_2_pchar;
  999. eq:=te_convert_l1;
  1000. end
  1001. else
  1002. if (m_delphi in current_settings.modeswitches) and is_integer(def_from) then
  1003. begin
  1004. doconv:=tc_cord_2_pointer;
  1005. eq:=te_convert_l5;
  1006. end;
  1007. end;
  1008. { allow explicit typecasts from ordinals to pointer.
  1009. Support for delphi compatibility
  1010. Support constructs like pointer(cardinal-cardinal) or pointer(longint+cardinal) where
  1011. the result of the ordinal operation is int64 also on 32 bit platforms.
  1012. It is also used by the compiler internally for inc(pointer,ordinal) }
  1013. if (eq=te_incompatible) and
  1014. not is_void(def_from) and
  1015. (
  1016. (
  1017. (cdo_explicit in cdoptions) and
  1018. (
  1019. (m_delphi in current_settings.modeswitches) or
  1020. { Don't allow pchar(char) in fpc modes }
  1021. is_integer(def_from)
  1022. )
  1023. ) or
  1024. (cdo_internal in cdoptions)
  1025. ) then
  1026. begin
  1027. doconv:=tc_int_2_int;
  1028. eq:=te_convert_l1;
  1029. end;
  1030. end;
  1031. enumdef :
  1032. begin
  1033. { allow explicit typecasts from enums to pointer.
  1034. Support for delphi compatibility
  1035. }
  1036. if (((cdo_explicit in cdoptions) and
  1037. (m_delphi in current_settings.modeswitches)
  1038. ) or
  1039. (cdo_internal in cdoptions)
  1040. ) then
  1041. begin
  1042. doconv:=tc_int_2_int;
  1043. eq:=te_convert_l1;
  1044. end;
  1045. end;
  1046. arraydef :
  1047. begin
  1048. { string constant (which can be part of array constructor)
  1049. to zero terminated string constant }
  1050. if (((fromtreetype = arrayconstructorn) and
  1051. { can't use is_chararray, because returns false for }
  1052. { array constructors }
  1053. is_char(tarraydef(def_from).elementdef)) or
  1054. (fromtreetype = stringconstn)) and
  1055. (is_pchar(def_to) or is_pwidechar(def_to)) then
  1056. begin
  1057. doconv:=tc_cstring_2_pchar;
  1058. eq:=te_convert_l2;
  1059. end
  1060. else
  1061. { chararray to pointer }
  1062. if (is_zero_based_array(def_from) or
  1063. is_open_array(def_from)) and
  1064. equal_defs(tarraydef(def_from).elementdef,tpointerdef(def_to).pointeddef) then
  1065. begin
  1066. doconv:=tc_array_2_pointer;
  1067. { don't prefer the pchar overload when a constant
  1068. string was passed }
  1069. if fromtreetype=stringconstn then
  1070. eq:=te_convert_l2
  1071. else
  1072. eq:=te_convert_l1;
  1073. end
  1074. else
  1075. { dynamic array to pointer, delphi only }
  1076. if (m_delphi in current_settings.modeswitches) and
  1077. is_dynamic_array(def_from) and
  1078. is_voidpointer(def_to) then
  1079. begin
  1080. eq:=te_equal;
  1081. end;
  1082. end;
  1083. pointerdef :
  1084. begin
  1085. { check for far pointers }
  1086. if (tpointerdef(def_from).is_far<>tpointerdef(def_to).is_far) then
  1087. begin
  1088. eq:=te_incompatible;
  1089. end
  1090. else
  1091. { the types can be forward type, handle before normal type check !! }
  1092. if assigned(def_to.typesym) and
  1093. (tpointerdef(def_to).pointeddef.typ=forwarddef) then
  1094. begin
  1095. if (def_from.typesym=def_to.typesym) then
  1096. eq:=te_equal
  1097. end
  1098. else
  1099. { same types }
  1100. if equal_defs(tpointerdef(def_from).pointeddef,tpointerdef(def_to).pointeddef) then
  1101. begin
  1102. eq:=te_equal
  1103. end
  1104. else
  1105. { child class pointer can be assigned to anchestor pointers }
  1106. if (
  1107. (tpointerdef(def_from).pointeddef.typ=objectdef) and
  1108. (tpointerdef(def_to).pointeddef.typ=objectdef) and
  1109. tobjectdef(tpointerdef(def_from).pointeddef).is_related(
  1110. tobjectdef(tpointerdef(def_to).pointeddef))
  1111. ) then
  1112. begin
  1113. doconv:=tc_equal;
  1114. eq:=te_convert_l1;
  1115. end
  1116. else
  1117. { all pointers can be assigned to void-pointer }
  1118. if is_void(tpointerdef(def_to).pointeddef) then
  1119. begin
  1120. doconv:=tc_equal;
  1121. { give pwidechar,pchar a penalty so it prefers
  1122. conversion to ansistring }
  1123. if is_pchar(def_from) or
  1124. is_pwidechar(def_from) then
  1125. eq:=te_convert_l2
  1126. else
  1127. eq:=te_convert_l1;
  1128. end
  1129. else
  1130. { all pointers can be assigned from void-pointer }
  1131. if is_void(tpointerdef(def_from).pointeddef) or
  1132. { all pointers can be assigned from void-pointer or formaldef pointer, check
  1133. tw3777.pp if you change this }
  1134. (tpointerdef(def_from).pointeddef.typ=formaldef) then
  1135. begin
  1136. doconv:=tc_equal;
  1137. { give pwidechar a penalty so it prefers
  1138. conversion to pchar }
  1139. if is_pwidechar(def_to) then
  1140. eq:=te_convert_l2
  1141. else
  1142. eq:=te_convert_l1;
  1143. end
  1144. { id = generic class instance. metaclasses are also
  1145. class instances themselves. }
  1146. else if ((def_from=objc_idtype) and
  1147. (def_to=objc_metaclasstype)) or
  1148. ((def_to=objc_idtype) and
  1149. (def_from=objc_metaclasstype)) then
  1150. begin
  1151. doconv:=tc_equal;
  1152. eq:=te_convert_l2;
  1153. end;
  1154. end;
  1155. procvardef :
  1156. begin
  1157. { procedure variable can be assigned to an void pointer,
  1158. this is not allowed for complex procvars }
  1159. if (is_void(tpointerdef(def_to).pointeddef) or
  1160. (m_mac_procvar in current_settings.modeswitches)) and
  1161. tprocvardef(def_from).is_addressonly then
  1162. begin
  1163. doconv:=tc_equal;
  1164. eq:=te_convert_l1;
  1165. end;
  1166. end;
  1167. procdef :
  1168. begin
  1169. { procedure variable can be assigned to an void pointer,
  1170. this not allowed for methodpointers }
  1171. if (m_mac_procvar in current_settings.modeswitches) and
  1172. tprocdef(def_from).is_addressonly then
  1173. begin
  1174. doconv:=tc_proc_2_procvar;
  1175. eq:=te_convert_l2;
  1176. end;
  1177. end;
  1178. classrefdef,
  1179. objectdef :
  1180. begin
  1181. { implicit pointer object and class reference types
  1182. can be assigned to void pointers, but it is less
  1183. preferred than assigning to a related objectdef }
  1184. if (
  1185. is_implicit_pointer_object_type(def_from) or
  1186. (def_from.typ=classrefdef)
  1187. ) and
  1188. (tpointerdef(def_to).pointeddef.typ=orddef) and
  1189. (torddef(tpointerdef(def_to).pointeddef).ordtype=uvoid) then
  1190. begin
  1191. doconv:=tc_equal;
  1192. eq:=te_convert_l2;
  1193. end
  1194. else if (is_objc_class_or_protocol(def_from) and
  1195. (def_to=objc_idtype)) or
  1196. { classrefs are also instances in Objective-C,
  1197. hence they're also assignment-cpmpatible with
  1198. id }
  1199. (is_objcclassref(def_from) and
  1200. ((def_to=objc_metaclasstype) or
  1201. (def_to=objc_idtype))) then
  1202. begin
  1203. doconv:=tc_equal;
  1204. eq:=te_convert_l2;
  1205. end;
  1206. end;
  1207. end;
  1208. end;
  1209. setdef :
  1210. begin
  1211. case def_from.typ of
  1212. setdef :
  1213. begin
  1214. if assigned(tsetdef(def_from).elementdef) and
  1215. assigned(tsetdef(def_to).elementdef) then
  1216. begin
  1217. { sets with the same element base type and the same range are equal }
  1218. if equal_defs(tsetdef(def_from).elementdef,tsetdef(def_to).elementdef) and
  1219. (tsetdef(def_from).setbase=tsetdef(def_to).setbase) and
  1220. (tsetdef(def_from).setmax=tsetdef(def_to).setmax) then
  1221. eq:=te_equal
  1222. else if is_subequal(tsetdef(def_from).elementdef,tsetdef(def_to).elementdef) then
  1223. begin
  1224. eq:=te_convert_l1;
  1225. doconv:=tc_set_to_set;
  1226. end;
  1227. end
  1228. else
  1229. begin
  1230. { empty set is compatible with everything }
  1231. eq:=te_convert_l1;
  1232. doconv:=tc_set_to_set;
  1233. end;
  1234. end;
  1235. arraydef :
  1236. begin
  1237. { automatic arrayconstructor -> set conversion }
  1238. if is_array_constructor(def_from) then
  1239. begin
  1240. doconv:=tc_arrayconstructor_2_set;
  1241. eq:=te_convert_l1;
  1242. end;
  1243. end;
  1244. end;
  1245. end;
  1246. procvardef :
  1247. begin
  1248. case def_from.typ of
  1249. procdef :
  1250. begin
  1251. { proc -> procvar }
  1252. if (m_tp_procvar in current_settings.modeswitches) or
  1253. (m_mac_procvar in current_settings.modeswitches) then
  1254. begin
  1255. subeq:=proc_to_procvar_equal(tprocdef(def_from),tprocvardef(def_to),cdo_warn_incompatible_univ in cdoptions);
  1256. if subeq>te_incompatible then
  1257. begin
  1258. doconv:=tc_proc_2_procvar;
  1259. if subeq>te_convert_l5 then
  1260. eq:=pred(subeq)
  1261. else
  1262. eq:=subeq;
  1263. end;
  1264. end;
  1265. end;
  1266. procvardef :
  1267. begin
  1268. { procvar -> procvar }
  1269. eq:=proc_to_procvar_equal(tprocvardef(def_from),tprocvardef(def_to),cdo_warn_incompatible_univ in cdoptions);
  1270. end;
  1271. pointerdef :
  1272. begin
  1273. { nil is compatible with procvars }
  1274. if (fromtreetype=niln) then
  1275. begin
  1276. if not Tprocvardef(def_to).is_addressonly then
  1277. {Nil to method pointers requires to convert a single
  1278. pointer nil value to a two pointer procvardef.}
  1279. doconv:=tc_nil_2_methodprocvar
  1280. else
  1281. doconv:=tc_equal;
  1282. eq:=te_convert_l1;
  1283. end
  1284. else
  1285. { for example delphi allows the assignement from pointers }
  1286. { to procedure variables }
  1287. if (m_pointer_2_procedure in current_settings.modeswitches) and
  1288. is_void(tpointerdef(def_from).pointeddef) and
  1289. tprocvardef(def_to).is_addressonly then
  1290. begin
  1291. doconv:=tc_equal;
  1292. eq:=te_convert_l1;
  1293. end;
  1294. end;
  1295. end;
  1296. end;
  1297. objectdef :
  1298. begin
  1299. { Objective-C classes (handle anonymous externals) }
  1300. if (def_from.typ=objectdef) and
  1301. (find_real_objcclass_definition(tobjectdef(def_from),false) =
  1302. find_real_objcclass_definition(tobjectdef(def_to),false)) then
  1303. begin
  1304. doconv:=tc_equal;
  1305. { exact, not equal, because can change between interface
  1306. and implementation }
  1307. eq:=te_exact;
  1308. end
  1309. { object pascal objects }
  1310. else if (def_from.typ=objectdef) and
  1311. (tobjectdef(def_from).is_related(tobjectdef(def_to))) then
  1312. begin
  1313. doconv:=tc_equal;
  1314. eq:=te_convert_l1;
  1315. end
  1316. else
  1317. { specific to implicit pointer object types }
  1318. if is_implicit_pointer_object_type(def_to) then
  1319. begin
  1320. { void pointer also for delphi mode }
  1321. if (m_delphi in current_settings.modeswitches) and
  1322. is_voidpointer(def_from) then
  1323. begin
  1324. doconv:=tc_equal;
  1325. { prefer pointer-pointer assignments }
  1326. eq:=te_convert_l2;
  1327. end
  1328. else
  1329. { nil is compatible with class instances and interfaces }
  1330. if (fromtreetype=niln) then
  1331. begin
  1332. doconv:=tc_equal;
  1333. eq:=te_convert_l1;
  1334. end
  1335. { All Objective-C classes are compatible with ID }
  1336. else if is_objc_class_or_protocol(def_to) and
  1337. (def_from=objc_idtype) then
  1338. begin
  1339. doconv:=tc_equal;
  1340. eq:=te_convert_l2;
  1341. end
  1342. { classes can be assigned to interfaces
  1343. (same with objcclass and objcprotocol) }
  1344. else if ((is_interface(def_to) and
  1345. is_class(def_from)) or
  1346. (is_objcprotocol(def_to) and
  1347. is_objcclass(def_from))) and
  1348. assigned(tobjectdef(def_from).ImplementedInterfaces) then
  1349. begin
  1350. { we've to search in parent classes as well }
  1351. hobjdef:=tobjectdef(def_from);
  1352. while assigned(hobjdef) do
  1353. begin
  1354. if hobjdef.find_implemented_interface(tobjectdef(def_to))<>nil then
  1355. begin
  1356. if is_interface(def_to) then
  1357. doconv:=tc_class_2_intf
  1358. else
  1359. { for Objective-C, we don't have to do anything special }
  1360. doconv:=tc_equal;
  1361. { don't prefer this over objectdef->objectdef }
  1362. eq:=te_convert_l2;
  1363. break;
  1364. end;
  1365. hobjdef:=hobjdef.childof;
  1366. end;
  1367. end
  1368. { Interface 2 GUID handling }
  1369. else if (def_to=tdef(rec_tguid)) and
  1370. (fromtreetype=typen) and
  1371. is_interface(def_from) and
  1372. assigned(tobjectdef(def_from).iidguid) then
  1373. begin
  1374. eq:=te_convert_l1;
  1375. doconv:=tc_equal;
  1376. end
  1377. else if (def_from.typ=variantdef) and is_interfacecom_or_dispinterface(def_to) then
  1378. begin
  1379. { corbainterfaces not accepted, until we have
  1380. runtime support for them in Variants (sergei) }
  1381. doconv:=tc_variant_2_interface;
  1382. eq:=te_convert_l2;
  1383. end
  1384. { ugly, but delphi allows it }
  1385. else if (def_from.typ in [orddef,enumdef]) and
  1386. (m_delphi in current_settings.modeswitches) and
  1387. (cdo_explicit in cdoptions) then
  1388. begin
  1389. doconv:=tc_int_2_int;
  1390. eq:=te_convert_l1;
  1391. end;
  1392. end;
  1393. end;
  1394. classrefdef :
  1395. begin
  1396. { similar to pointerdef wrt forwards }
  1397. if assigned(def_to.typesym) and
  1398. (tclassrefdef(def_to).pointeddef.typ=forwarddef) then
  1399. begin
  1400. if (def_from.typesym=def_to.typesym) then
  1401. eq:=te_equal;
  1402. end
  1403. else
  1404. { class reference types }
  1405. if (def_from.typ=classrefdef) then
  1406. begin
  1407. if equal_defs(tclassrefdef(def_from).pointeddef,tclassrefdef(def_to).pointeddef) then
  1408. begin
  1409. eq:=te_equal;
  1410. end
  1411. else
  1412. begin
  1413. doconv:=tc_equal;
  1414. if (cdo_explicit in cdoptions) or
  1415. tobjectdef(tclassrefdef(def_from).pointeddef).is_related(
  1416. tobjectdef(tclassrefdef(def_to).pointeddef)) then
  1417. eq:=te_convert_l1;
  1418. end;
  1419. end
  1420. else
  1421. if (m_delphi in current_settings.modeswitches) and
  1422. is_voidpointer(def_from) then
  1423. begin
  1424. doconv:=tc_equal;
  1425. { prefer pointer-pointer assignments }
  1426. eq:=te_convert_l2;
  1427. end
  1428. else
  1429. { nil is compatible with class references }
  1430. if (fromtreetype=niln) then
  1431. begin
  1432. doconv:=tc_equal;
  1433. eq:=te_convert_l1;
  1434. end
  1435. else
  1436. { id is compatible with all classref types }
  1437. if (def_from=objc_idtype) then
  1438. begin
  1439. doconv:=tc_equal;
  1440. eq:=te_convert_l1;
  1441. end;
  1442. end;
  1443. filedef :
  1444. begin
  1445. { typed files are all equal to the abstract file type
  1446. name TYPEDFILE in system.pp in is_equal in types.pas
  1447. the problem is that it sholud be also compatible to FILE
  1448. but this would leed to a problem for ASSIGN RESET and REWRITE
  1449. when trying to find the good overloaded function !!
  1450. so all file function are doubled in system.pp
  1451. this is not very beautiful !!}
  1452. if (def_from.typ=filedef) then
  1453. begin
  1454. if (tfiledef(def_from).filetyp=tfiledef(def_to).filetyp) then
  1455. begin
  1456. if
  1457. (
  1458. (tfiledef(def_from).typedfiledef=nil) and
  1459. (tfiledef(def_to).typedfiledef=nil)
  1460. ) or
  1461. (
  1462. (tfiledef(def_from).typedfiledef<>nil) and
  1463. (tfiledef(def_to).typedfiledef<>nil) and
  1464. equal_defs(tfiledef(def_from).typedfiledef,tfiledef(def_to).typedfiledef)
  1465. ) or
  1466. (
  1467. (tfiledef(def_from).filetyp = ft_typed) and
  1468. (tfiledef(def_to).filetyp = ft_typed) and
  1469. (
  1470. (tfiledef(def_from).typedfiledef = tdef(voidtype)) or
  1471. (tfiledef(def_to).typedfiledef = tdef(voidtype))
  1472. )
  1473. ) then
  1474. begin
  1475. eq:=te_equal;
  1476. end;
  1477. end
  1478. else
  1479. if ((tfiledef(def_from).filetyp = ft_untyped) and
  1480. (tfiledef(def_to).filetyp = ft_typed)) or
  1481. ((tfiledef(def_from).filetyp = ft_typed) and
  1482. (tfiledef(def_to).filetyp = ft_untyped)) then
  1483. begin
  1484. doconv:=tc_equal;
  1485. eq:=te_convert_l1;
  1486. end;
  1487. end;
  1488. end;
  1489. recorddef :
  1490. begin
  1491. { interface -> guid }
  1492. if (def_to=rec_tguid) and
  1493. (is_interfacecom_or_dispinterface(def_from)) then
  1494. begin
  1495. doconv:=tc_intf_2_guid;
  1496. eq:=te_convert_l1;
  1497. end;
  1498. end;
  1499. formaldef :
  1500. begin
  1501. doconv:=tc_equal;
  1502. if (def_from.typ=formaldef) then
  1503. eq:=te_equal
  1504. else
  1505. { Just about everything can be converted to a formaldef...}
  1506. if not (def_from.typ in [abstractdef,errordef]) then
  1507. eq:=te_convert_l2;
  1508. end;
  1509. end;
  1510. { if we didn't find an appropriate type conversion yet
  1511. then we search also the := operator }
  1512. if (eq=te_incompatible) and
  1513. { make sure there is not a single variant if variants }
  1514. { are not allowed (otherwise if only cdo_check_operator }
  1515. { and e.g. fromdef=stringdef and todef=variantdef, then }
  1516. { the test will still succeed }
  1517. ((cdo_allow_variant in cdoptions) or
  1518. ((def_from.typ<>variantdef) and (def_to.typ<>variantdef))
  1519. ) and
  1520. (
  1521. { Check for variants? }
  1522. (
  1523. (cdo_allow_variant in cdoptions) and
  1524. ((def_from.typ=variantdef) or (def_to.typ=variantdef))
  1525. ) or
  1526. { Check for operators? }
  1527. (
  1528. (cdo_check_operator in cdoptions) and
  1529. ((def_from.typ<>variantdef) or (def_to.typ<>variantdef))
  1530. )
  1531. ) then
  1532. begin
  1533. operatorpd:=search_assignment_operator(def_from,def_to,cdo_explicit in cdoptions);
  1534. if assigned(operatorpd) then
  1535. eq:=te_convert_operator;
  1536. end;
  1537. { update convtype for te_equal when it is not yet set }
  1538. if (eq=te_equal) and
  1539. (doconv=tc_not_possible) then
  1540. doconv:=tc_equal;
  1541. compare_defs_ext:=eq;
  1542. end;
  1543. function equal_defs(def_from,def_to:tdef):boolean;
  1544. var
  1545. convtyp : tconverttype;
  1546. pd : tprocdef;
  1547. begin
  1548. { Compare defs with nothingn and no explicit typecasts and
  1549. searching for overloaded operators is not needed }
  1550. equal_defs:=(compare_defs_ext(def_from,def_to,nothingn,convtyp,pd,[])>=te_equal);
  1551. end;
  1552. function compare_defs(def_from,def_to:tdef;fromtreetype:tnodetype):tequaltype;
  1553. var
  1554. doconv : tconverttype;
  1555. pd : tprocdef;
  1556. begin
  1557. compare_defs:=compare_defs_ext(def_from,def_to,fromtreetype,doconv,pd,[cdo_check_operator,cdo_allow_variant]);
  1558. end;
  1559. function is_subequal(def1, def2: tdef): boolean;
  1560. var
  1561. basedef1,basedef2 : tenumdef;
  1562. Begin
  1563. is_subequal := false;
  1564. if assigned(def1) and assigned(def2) then
  1565. Begin
  1566. if (def1.typ = orddef) and (def2.typ = orddef) then
  1567. Begin
  1568. { see p.47 of Turbo Pascal 7.01 manual for the separation of types }
  1569. { range checking for case statements is done with testrange }
  1570. case torddef(def1).ordtype of
  1571. u8bit,u16bit,u32bit,u64bit,
  1572. s8bit,s16bit,s32bit,s64bit :
  1573. is_subequal:=(torddef(def2).ordtype in [s64bit,u64bit,s32bit,u32bit,u8bit,s8bit,s16bit,u16bit]);
  1574. pasbool8,pasbool16,pasbool32,pasbool64,
  1575. bool8bit,bool16bit,bool32bit,bool64bit :
  1576. is_subequal:=(torddef(def2).ordtype in [pasbool8,pasbool16,pasbool32,pasbool64,bool8bit,bool16bit,bool32bit,bool64bit]);
  1577. uchar :
  1578. is_subequal:=(torddef(def2).ordtype=uchar);
  1579. uwidechar :
  1580. is_subequal:=(torddef(def2).ordtype=uwidechar);
  1581. end;
  1582. end
  1583. else
  1584. Begin
  1585. { Check if both basedefs are equal }
  1586. if (def1.typ=enumdef) and (def2.typ=enumdef) then
  1587. Begin
  1588. { get both basedefs }
  1589. basedef1:=tenumdef(def1);
  1590. while assigned(basedef1.basedef) do
  1591. basedef1:=basedef1.basedef;
  1592. basedef2:=tenumdef(def2);
  1593. while assigned(basedef2.basedef) do
  1594. basedef2:=basedef2.basedef;
  1595. is_subequal:=(basedef1=basedef2);
  1596. end;
  1597. end;
  1598. end;
  1599. end;
  1600. function potentially_incompatible_univ_paras(def1, def2: tdef): boolean;
  1601. begin
  1602. result :=
  1603. { not entirely safe: different records can be passed differently
  1604. depending on the types of their fields, but they're hard to compare
  1605. (variant records, bitpacked vs non-bitpacked) }
  1606. ((def1.typ in [floatdef,recorddef,arraydef,filedef,variantdef]) and
  1607. (def1.typ<>def2.typ)) or
  1608. { pointers, ordinals and small sets are all passed the same}
  1609. (((def1.typ in [orddef,enumdef,pointerdef,procvardef,classrefdef]) or
  1610. (is_class_or_interface_or_objc(def1)) or
  1611. is_dynamic_array(def1) or
  1612. is_smallset(def1) or
  1613. is_ansistring(def1) or
  1614. is_unicodestring(def1)) <>
  1615. (def2.typ in [orddef,enumdef,pointerdef,procvardef,classrefdef]) or
  1616. (is_class_or_interface_or_objc(def2)) or
  1617. is_dynamic_array(def2) or
  1618. is_smallset(def2) or
  1619. is_ansistring(def2) or
  1620. is_unicodestring(def2)) or
  1621. { shortstrings }
  1622. (is_shortstring(def1)<>
  1623. is_shortstring(def2)) or
  1624. { winlike widestrings }
  1625. (is_widestring(def1)<>
  1626. is_widestring(def2)) or
  1627. { TP-style objects }
  1628. (is_object(def1) <>
  1629. is_object(def2));
  1630. end;
  1631. function compare_paras(para1,para2 : TFPObjectList; acp : tcompare_paras_type; cpoptions: tcompare_paras_options):tequaltype;
  1632. var
  1633. currpara1,
  1634. currpara2 : tparavarsym;
  1635. eq,lowesteq : tequaltype;
  1636. hpd : tprocdef;
  1637. convtype : tconverttype;
  1638. cdoptions : tcompare_defs_options;
  1639. i1,i2 : byte;
  1640. begin
  1641. compare_paras:=te_incompatible;
  1642. cdoptions:=[cdo_parameter,cdo_check_operator,cdo_allow_variant,cdo_strict_undefined_check];
  1643. { we need to parse the list from left-right so the
  1644. not-default parameters are checked first }
  1645. lowesteq:=high(tequaltype);
  1646. i1:=0;
  1647. i2:=0;
  1648. if cpo_ignorehidden in cpoptions then
  1649. begin
  1650. while (i1<para1.count) and
  1651. (vo_is_hidden_para in tparavarsym(para1[i1]).varoptions) do
  1652. inc(i1);
  1653. while (i2<para2.count) and
  1654. (vo_is_hidden_para in tparavarsym(para2[i2]).varoptions) do
  1655. inc(i2);
  1656. end;
  1657. if cpo_ignoreframepointer in cpoptions then
  1658. begin
  1659. if (i1<para1.count) and
  1660. (vo_is_parentfp in tparavarsym(para1[i1]).varoptions) then
  1661. inc(i1);
  1662. if (i2<para2.count) and
  1663. (vo_is_parentfp in tparavarsym(para2[i2]).varoptions) then
  1664. inc(i2);
  1665. end;
  1666. while (i1<para1.count) and (i2<para2.count) do
  1667. begin
  1668. eq:=te_incompatible;
  1669. currpara1:=tparavarsym(para1[i1]);
  1670. currpara2:=tparavarsym(para2[i2]);
  1671. { Unique types must match exact }
  1672. if ((df_unique in currpara1.vardef.defoptions) or (df_unique in currpara2.vardef.defoptions)) and
  1673. (currpara1.vardef<>currpara2.vardef) then
  1674. exit;
  1675. { Handle hidden parameters separately, because self is
  1676. defined as voidpointer for methodpointers }
  1677. if (vo_is_hidden_para in currpara1.varoptions) or
  1678. (vo_is_hidden_para in currpara2.varoptions) then
  1679. begin
  1680. { both must be hidden }
  1681. if (vo_is_hidden_para in currpara1.varoptions)<>(vo_is_hidden_para in currpara2.varoptions) then
  1682. exit;
  1683. eq:=te_exact;
  1684. if not(vo_is_self in currpara1.varoptions) and
  1685. not(vo_is_self in currpara2.varoptions) then
  1686. begin
  1687. if not(cpo_ignorevarspez in cpoptions) and
  1688. (currpara1.varspez<>currpara2.varspez) then
  1689. exit;
  1690. eq:=compare_defs_ext(currpara1.vardef,currpara2.vardef,nothingn,
  1691. convtype,hpd,cdoptions);
  1692. end;
  1693. end
  1694. else
  1695. begin
  1696. case acp of
  1697. cp_value_equal_const :
  1698. begin
  1699. { this one is used for matching parameters from a call
  1700. statement to a procdef -> univ state can't be equal
  1701. in any case since the call statement does not contain
  1702. any information about that }
  1703. if (
  1704. not(cpo_ignorevarspez in cpoptions) and
  1705. (currpara1.varspez<>currpara2.varspez) and
  1706. ((currpara1.varspez in [vs_var,vs_out,vs_constref]) or
  1707. (currpara2.varspez in [vs_var,vs_out,vs_constref]))
  1708. ) then
  1709. exit;
  1710. eq:=compare_defs_ext(currpara1.vardef,currpara2.vardef,nothingn,
  1711. convtype,hpd,cdoptions);
  1712. end;
  1713. cp_all :
  1714. begin
  1715. { used to resolve forward definitions -> headers must
  1716. match exactly, including the "univ" specifier }
  1717. if (not(cpo_ignorevarspez in cpoptions) and
  1718. (currpara1.varspez<>currpara2.varspez)) or
  1719. (currpara1.univpara<>currpara2.univpara) then
  1720. exit;
  1721. eq:=compare_defs_ext(currpara1.vardef,currpara2.vardef,nothingn,
  1722. convtype,hpd,cdoptions);
  1723. end;
  1724. cp_procvar :
  1725. begin
  1726. if not(cpo_ignorevarspez in cpoptions) and
  1727. (currpara1.varspez<>currpara2.varspez) then
  1728. exit;
  1729. { "univ" state doesn't matter here: from univ to non-univ
  1730. matches if the types are compatible (i.e., as usual),
  1731. from from non-univ to univ also matches if the types
  1732. have the same size (checked below) }
  1733. eq:=compare_defs_ext(currpara1.vardef,currpara2.vardef,nothingn,
  1734. convtype,hpd,cdoptions);
  1735. { Parameters must be at least equal otherwise the are incompatible }
  1736. if (eq<te_equal) then
  1737. eq:=te_incompatible;
  1738. end;
  1739. else
  1740. eq:=compare_defs_ext(currpara1.vardef,currpara2.vardef,nothingn,
  1741. convtype,hpd,cdoptions);
  1742. end;
  1743. end;
  1744. { check type }
  1745. if eq=te_incompatible then
  1746. begin
  1747. { special case: "univ" parameters match if their size is equal }
  1748. if not(cpo_ignoreuniv in cpoptions) and
  1749. currpara2.univpara and
  1750. is_valid_univ_para_type(currpara1.vardef) and
  1751. (currpara1.vardef.size=currpara2.vardef.size) then
  1752. begin
  1753. { only pick as last choice }
  1754. eq:=te_convert_l5;
  1755. if (acp=cp_procvar) and
  1756. (cpo_warn_incompatible_univ in cpoptions) then
  1757. begin
  1758. { if the types may be passed in different ways by the
  1759. calling convention then this can lead to crashes
  1760. (note: not an exhaustive check, and failing this
  1761. this check does not mean things will crash on all
  1762. platforms) }
  1763. if potentially_incompatible_univ_paras(currpara1.vardef,currpara2.vardef) then
  1764. Message2(type_w_procvar_univ_conflicting_para,currpara1.vardef.typename,currpara2.vardef.typename)
  1765. end;
  1766. end
  1767. else
  1768. exit;
  1769. end;
  1770. { open strings can never match exactly, since you cannot define }
  1771. { a separate "open string" type -> we have to be able to }
  1772. { consider those as exact when resolving forward definitions. }
  1773. { The same goes for array of const. Open arrays are handled }
  1774. { already (if their element types match exactly, they are }
  1775. { considered to be an exact match) }
  1776. { And also for "inline defined" function parameter definitions }
  1777. { (i.e., function types directly declared in a parameter list) }
  1778. if (is_array_of_const(currpara1.vardef) or
  1779. is_open_string(currpara1.vardef) or
  1780. ((currpara1.vardef.typ = procvardef) and
  1781. not(assigned(currpara1.vardef.typesym)))) and
  1782. (eq=te_equal) and
  1783. (cpo_openequalisexact in cpoptions) then
  1784. eq:=te_exact;
  1785. if eq<lowesteq then
  1786. lowesteq:=eq;
  1787. { also check default value if both have it declared }
  1788. if (cpo_comparedefaultvalue in cpoptions) and
  1789. assigned(currpara1.defaultconstsym) and
  1790. assigned(currpara2.defaultconstsym) then
  1791. begin
  1792. if not equal_constsym(tconstsym(currpara1.defaultconstsym),tconstsym(currpara2.defaultconstsym)) then
  1793. exit;
  1794. end;
  1795. if not(cpo_compilerproc in cpoptions) and
  1796. not(cpo_rtlproc in cpoptions) and
  1797. is_ansistring(currpara1.vardef) and
  1798. is_ansistring(currpara2.vardef) and
  1799. (tstringdef(currpara1.vardef).encoding<>tstringdef(currpara2.vardef).encoding) and
  1800. ((tstringdef(currpara1.vardef).encoding=globals.CP_NONE) or
  1801. (tstringdef(currpara2.vardef).encoding=globals.CP_NONE)
  1802. ) then
  1803. eq:=te_convert_l1;
  1804. if eq<lowesteq then
  1805. lowesteq:=eq;
  1806. inc(i1);
  1807. inc(i2);
  1808. if cpo_ignorehidden in cpoptions then
  1809. begin
  1810. while (i1<para1.count) and
  1811. (vo_is_hidden_para in tparavarsym(para1[i1]).varoptions) do
  1812. inc(i1);
  1813. while (i2<para2.count) and
  1814. (vo_is_hidden_para in tparavarsym(para2[i2]).varoptions) do
  1815. inc(i2);
  1816. end;
  1817. if cpo_ignoreframepointer in cpoptions then
  1818. begin
  1819. if (i1<para1.count) and
  1820. (vo_is_parentfp in tparavarsym(para1[i1]).varoptions) then
  1821. inc(i1);
  1822. if (i2<para2.count) and
  1823. (vo_is_parentfp in tparavarsym(para2[i2]).varoptions) then
  1824. inc(i2);
  1825. end;
  1826. end;
  1827. { when both lists are empty then the parameters are equal. Also
  1828. when one list is empty and the other has a parameter with default
  1829. value assigned then the parameters are also equal }
  1830. if ((i1>=para1.count) and (i2>=para2.count)) or
  1831. ((cpo_allowdefaults in cpoptions) and
  1832. (((i1<para1.count) and assigned(tparavarsym(para1[i1]).defaultconstsym)) or
  1833. ((i2<para2.count) and assigned(tparavarsym(para2[i2]).defaultconstsym)))) then
  1834. compare_paras:=lowesteq;
  1835. end;
  1836. function proc_to_procvar_equal(def1:tabstractprocdef;def2:tprocvardef;checkincompatibleuniv: boolean):tequaltype;
  1837. var
  1838. eq : tequaltype;
  1839. po_comp : tprocoptions;
  1840. pa_comp: tcompare_paras_options;
  1841. begin
  1842. proc_to_procvar_equal:=te_incompatible;
  1843. if not(assigned(def1)) or not(assigned(def2)) then
  1844. exit;
  1845. { check for method pointer and local procedure pointer:
  1846. a) if one is a procedure of object, the other also has to be one
  1847. b) if one is a pure address, the other also has to be one
  1848. except if def1 is a global proc and def2 is a nested procdef
  1849. (global procedures can be converted into nested procvars)
  1850. c) if def1 is a nested procedure, then def2 has to be a nested
  1851. procvar and def1 has to have the po_delphi_nested_cc option
  1852. d) if def1 is a procvar, def1 and def2 both have to be nested or
  1853. non-nested (we don't allow assignments from non-nested to
  1854. nested procvars to make sure that we can still implement
  1855. nested procvars using trampolines -- e.g., this would be
  1856. necessary for LLVM or CIL as long as they do not have support
  1857. for Delphi-style frame pointer parameter passing) }
  1858. if (def1.is_methodpointer<>def2.is_methodpointer) or { a) }
  1859. ((def1.is_addressonly<>def2.is_addressonly) and { b) }
  1860. (is_nested_pd(def1) or
  1861. not is_nested_pd(def2))) or
  1862. ((def1.typ=procdef) and { c) }
  1863. is_nested_pd(def1) and
  1864. (not(po_delphi_nested_cc in def1.procoptions) or
  1865. not is_nested_pd(def2))) or
  1866. ((def1.typ=procvardef) and { d) }
  1867. (is_nested_pd(def1)<>is_nested_pd(def2))) then
  1868. exit;
  1869. pa_comp:=[cpo_ignoreframepointer];
  1870. if checkincompatibleuniv then
  1871. include(pa_comp,cpo_warn_incompatible_univ);
  1872. { check return value and options, methodpointer is already checked }
  1873. po_comp:=[po_staticmethod,po_interrupt,
  1874. po_iocheck,po_varargs];
  1875. if (m_delphi in current_settings.modeswitches) then
  1876. exclude(po_comp,po_varargs);
  1877. if (def1.proccalloption=def2.proccalloption) and
  1878. ((po_comp * def1.procoptions)= (po_comp * def2.procoptions)) and
  1879. equal_defs(def1.returndef,def2.returndef) then
  1880. begin
  1881. { return equal type based on the parameters, but a proc->procvar
  1882. is never exact, so map an exact match of the parameters to
  1883. te_equal }
  1884. eq:=compare_paras(def1.paras,def2.paras,cp_procvar,pa_comp);
  1885. if eq=te_exact then
  1886. eq:=te_equal;
  1887. if (eq=te_equal) then
  1888. begin
  1889. { prefer non-nested to non-nested over non-nested to nested }
  1890. if (is_nested_pd(def1)<>is_nested_pd(def2)) then
  1891. eq:=te_convert_l1;
  1892. end;
  1893. proc_to_procvar_equal:=eq;
  1894. end;
  1895. end;
  1896. function compatible_childmethod_resultdef(parentretdef, childretdef: tdef): boolean;
  1897. begin
  1898. compatible_childmethod_resultdef :=
  1899. (equal_defs(parentretdef,childretdef)) or
  1900. ((parentretdef.typ=objectdef) and
  1901. (childretdef.typ=objectdef) and
  1902. is_class_or_interface_or_objc(parentretdef) and
  1903. is_class_or_interface_or_objc(childretdef) and
  1904. (tobjectdef(childretdef).is_related(tobjectdef(parentretdef))))
  1905. end;
  1906. end.