defcmp.pas 97 KB

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