defcmp.pas 96 KB

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