defcmp.pas 94 KB

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