defcmp.pas 94 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108
  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) then
  1181. begin
  1182. if (def_from.typesym=def_to.typesym) or
  1183. (fromtreetype=niln) then
  1184. eq:=te_equal
  1185. end
  1186. else
  1187. { same types }
  1188. if equal_defs(tpointerdef(def_from).pointeddef,tpointerdef(def_to).pointeddef) then
  1189. begin
  1190. eq:=te_equal
  1191. end
  1192. else
  1193. { child class pointer can be assigned to anchestor pointers }
  1194. if (
  1195. (tpointerdef(def_from).pointeddef.typ=objectdef) and
  1196. (tpointerdef(def_to).pointeddef.typ=objectdef) and
  1197. tobjectdef(tpointerdef(def_from).pointeddef).is_related(
  1198. tobjectdef(tpointerdef(def_to).pointeddef))
  1199. ) then
  1200. begin
  1201. doconv:=tc_equal;
  1202. eq:=te_convert_l1;
  1203. end
  1204. else
  1205. { all pointers can be assigned to void-pointer }
  1206. if is_void(tpointerdef(def_to).pointeddef) then
  1207. begin
  1208. doconv:=tc_equal;
  1209. { give pwidechar,pchar a penalty so it prefers
  1210. conversion to ansistring }
  1211. if is_pchar(def_from) or
  1212. is_pwidechar(def_from) then
  1213. eq:=te_convert_l2
  1214. else
  1215. eq:=te_convert_l1;
  1216. end
  1217. else
  1218. { all pointers can be assigned from void-pointer }
  1219. if is_void(tpointerdef(def_from).pointeddef) or
  1220. { all pointers can be assigned from void-pointer or formaldef pointer, check
  1221. tw3777.pp if you change this }
  1222. (tpointerdef(def_from).pointeddef.typ=formaldef) then
  1223. begin
  1224. doconv:=tc_equal;
  1225. { give pwidechar a penalty so it prefers
  1226. conversion to pchar }
  1227. if is_pwidechar(def_to) then
  1228. eq:=te_convert_l2
  1229. else
  1230. eq:=te_convert_l1;
  1231. end
  1232. { id = generic class instance. metaclasses are also
  1233. class instances themselves. }
  1234. else if ((def_from=objc_idtype) and
  1235. (def_to=objc_metaclasstype)) or
  1236. ((def_to=objc_idtype) and
  1237. (def_from=objc_metaclasstype)) then
  1238. begin
  1239. doconv:=tc_equal;
  1240. eq:=te_convert_l2;
  1241. end;
  1242. end;
  1243. procvardef :
  1244. begin
  1245. { procedure variable can be assigned to an void pointer,
  1246. this is not allowed for complex procvars }
  1247. if (is_void(tpointerdef(def_to).pointeddef) or
  1248. (m_mac_procvar in current_settings.modeswitches)) and
  1249. tprocvardef(def_from).is_addressonly then
  1250. begin
  1251. doconv:=tc_equal;
  1252. eq:=te_convert_l1;
  1253. end;
  1254. end;
  1255. procdef :
  1256. begin
  1257. { procedure variable can be assigned to an void pointer,
  1258. this not allowed for methodpointers }
  1259. if (m_mac_procvar in current_settings.modeswitches) and
  1260. tprocdef(def_from).is_addressonly then
  1261. begin
  1262. doconv:=tc_proc_2_procvar;
  1263. eq:=te_convert_l2;
  1264. end;
  1265. end;
  1266. classrefdef,
  1267. objectdef :
  1268. begin
  1269. { implicit pointer object and class reference types
  1270. can be assigned to void pointers, but it is less
  1271. preferred than assigning to a related objectdef }
  1272. if (
  1273. is_implicit_pointer_object_type(def_from) or
  1274. (def_from.typ=classrefdef)
  1275. ) and
  1276. (tpointerdef(def_to).pointeddef.typ=orddef) and
  1277. (torddef(tpointerdef(def_to).pointeddef).ordtype=uvoid) then
  1278. begin
  1279. doconv:=tc_equal;
  1280. eq:=te_convert_l2;
  1281. end
  1282. else if (is_objc_class_or_protocol(def_from) and
  1283. (def_to=objc_idtype)) or
  1284. { classrefs are also instances in Objective-C,
  1285. hence they're also assignment-cpmpatible with
  1286. id }
  1287. (is_objcclassref(def_from) and
  1288. ((def_to=objc_metaclasstype) or
  1289. (def_to=objc_idtype))) then
  1290. begin
  1291. doconv:=tc_equal;
  1292. eq:=te_convert_l2;
  1293. end;
  1294. end;
  1295. end;
  1296. end;
  1297. setdef :
  1298. begin
  1299. case def_from.typ of
  1300. setdef :
  1301. begin
  1302. if assigned(tsetdef(def_from).elementdef) and
  1303. assigned(tsetdef(def_to).elementdef) then
  1304. begin
  1305. { sets with the same element base type and the same range are equal }
  1306. if equal_defs(tsetdef(def_from).elementdef,tsetdef(def_to).elementdef) and
  1307. (tsetdef(def_from).setbase=tsetdef(def_to).setbase) and
  1308. (tsetdef(def_from).setmax=tsetdef(def_to).setmax) then
  1309. eq:=te_equal
  1310. else if is_subequal(tsetdef(def_from).elementdef,tsetdef(def_to).elementdef) then
  1311. begin
  1312. eq:=te_convert_l1;
  1313. doconv:=tc_set_to_set;
  1314. end;
  1315. end
  1316. else
  1317. begin
  1318. { empty set is compatible with everything }
  1319. eq:=te_convert_l1;
  1320. doconv:=tc_set_to_set;
  1321. end;
  1322. end;
  1323. arraydef :
  1324. begin
  1325. { automatic arrayconstructor -> set conversion }
  1326. if is_array_constructor(def_from) then
  1327. begin
  1328. doconv:=tc_arrayconstructor_2_set;
  1329. eq:=te_convert_l1;
  1330. end;
  1331. end;
  1332. end;
  1333. end;
  1334. procvardef :
  1335. begin
  1336. case def_from.typ of
  1337. procdef :
  1338. begin
  1339. { proc -> procvar }
  1340. if (m_tp_procvar in current_settings.modeswitches) or
  1341. (m_mac_procvar in current_settings.modeswitches) then
  1342. begin
  1343. subeq:=proc_to_procvar_equal(tprocdef(def_from),tprocvardef(def_to),cdo_warn_incompatible_univ in cdoptions);
  1344. if subeq>te_incompatible then
  1345. begin
  1346. doconv:=tc_proc_2_procvar;
  1347. if subeq>te_convert_l5 then
  1348. eq:=pred(subeq)
  1349. else
  1350. eq:=subeq;
  1351. end;
  1352. end;
  1353. end;
  1354. procvardef :
  1355. begin
  1356. { procvar -> procvar }
  1357. eq:=proc_to_procvar_equal(tprocvardef(def_from),tprocvardef(def_to),cdo_warn_incompatible_univ in cdoptions);
  1358. end;
  1359. pointerdef :
  1360. begin
  1361. { nil is compatible with procvars }
  1362. if (fromtreetype=niln) then
  1363. begin
  1364. if not Tprocvardef(def_to).is_addressonly then
  1365. {Nil to method pointers requires to convert a single
  1366. pointer nil value to a two pointer procvardef.}
  1367. doconv:=tc_nil_2_methodprocvar
  1368. else
  1369. doconv:=tc_equal;
  1370. eq:=te_convert_l1;
  1371. end
  1372. else
  1373. { for example delphi allows the assignement from pointers }
  1374. { to procedure variables }
  1375. if (m_pointer_2_procedure in current_settings.modeswitches) and
  1376. is_void(tpointerdef(def_from).pointeddef) and
  1377. tprocvardef(def_to).is_addressonly then
  1378. begin
  1379. doconv:=tc_equal;
  1380. eq:=te_convert_l1;
  1381. end;
  1382. end;
  1383. end;
  1384. end;
  1385. objectdef :
  1386. begin
  1387. { object pascal objects }
  1388. if (def_from.typ=objectdef) and
  1389. (tobjectdef(def_from).is_related(tobjectdef(def_to))) then
  1390. begin
  1391. doconv:=tc_equal;
  1392. { also update in htypechk.pas/var_para_allowed if changed
  1393. here }
  1394. eq:=te_convert_l3;
  1395. end
  1396. { string -> java.lang.string }
  1397. else if (def_to=java_jlstring) and
  1398. ((def_from.typ=stringdef) or
  1399. (fromtreetype=stringconstn)) then
  1400. begin
  1401. if is_wide_or_unicode_string(def_from) or
  1402. ((fromtreetype=stringconstn) and
  1403. (cs_refcountedstrings in current_settings.localswitches) and
  1404. (m_default_unicodestring in current_settings.modeswitches)) then
  1405. begin
  1406. doconv:=tc_equal;
  1407. eq:=te_equal
  1408. end
  1409. else
  1410. begin
  1411. doconv:=tc_string_2_string;
  1412. eq:=te_convert_l2;
  1413. end;
  1414. end
  1415. else if (def_to=java_jlstring) and
  1416. is_anychar(def_from) then
  1417. begin
  1418. doconv:=tc_char_2_string;
  1419. eq:=te_convert_l2
  1420. end
  1421. else
  1422. { specific to implicit pointer object types }
  1423. if is_implicit_pointer_object_type(def_to) then
  1424. begin
  1425. { void pointer also for delphi mode }
  1426. if (m_delphi in current_settings.modeswitches) and
  1427. is_voidpointer(def_from) then
  1428. begin
  1429. doconv:=tc_equal;
  1430. { prefer pointer-pointer assignments }
  1431. eq:=te_convert_l2;
  1432. end
  1433. else
  1434. { nil is compatible with class instances and interfaces }
  1435. if (fromtreetype=niln) then
  1436. begin
  1437. doconv:=tc_equal;
  1438. eq:=te_convert_l1;
  1439. end
  1440. { All Objective-C classes are compatible with ID }
  1441. else if is_objc_class_or_protocol(def_to) and
  1442. (def_from=objc_idtype) then
  1443. begin
  1444. doconv:=tc_equal;
  1445. eq:=te_convert_l2;
  1446. end
  1447. { classes can be assigned to interfaces
  1448. (same with objcclass and objcprotocol) }
  1449. else if ((is_interface(def_to) and
  1450. is_class(def_from)) or
  1451. (is_objcprotocol(def_to) and
  1452. is_objcclass(def_from)) or
  1453. (is_javainterface(def_to) and
  1454. is_javaclass(def_from))) and
  1455. assigned(tobjectdef(def_from).ImplementedInterfaces) then
  1456. begin
  1457. { we've to search in parent classes as well }
  1458. hobjdef:=tobjectdef(def_from);
  1459. while assigned(hobjdef) do
  1460. begin
  1461. if hobjdef.find_implemented_interface(tobjectdef(def_to))<>nil then
  1462. begin
  1463. if is_interface(def_to) then
  1464. doconv:=tc_class_2_intf
  1465. else
  1466. { for Objective-C, we don't have to do anything special }
  1467. doconv:=tc_equal;
  1468. { don't prefer this over objectdef->objectdef }
  1469. eq:=te_convert_l2;
  1470. break;
  1471. end;
  1472. hobjdef:=hobjdef.childof;
  1473. end;
  1474. end
  1475. { Interface 2 GUID handling }
  1476. else if (def_to=tdef(rec_tguid)) and
  1477. (fromtreetype=typen) and
  1478. is_interface(def_from) and
  1479. assigned(tobjectdef(def_from).iidguid) then
  1480. begin
  1481. eq:=te_convert_l1;
  1482. doconv:=tc_equal;
  1483. end
  1484. else if (def_from.typ=variantdef) and is_interfacecom_or_dispinterface(def_to) then
  1485. begin
  1486. { corbainterfaces not accepted, until we have
  1487. runtime support for them in Variants (sergei) }
  1488. doconv:=tc_variant_2_interface;
  1489. eq:=te_convert_l2;
  1490. end
  1491. { ugly, but delphi allows it (enables typecasting ordinals/
  1492. enums of any size to pointer-based object defs) }
  1493. { in Java enums /are/ class instances, and hence such
  1494. typecasts must not be treated as integer-like conversions;
  1495. arbitrary constants cannot be converted into classes/
  1496. pointer-based values either on the JVM -> always return
  1497. false and let it be handled by the regular explicit type
  1498. casting code
  1499. }
  1500. else if (not(target_info.system in systems_jvm) and
  1501. ((def_from.typ=enumdef) or
  1502. (def_from.typ=orddef))) and
  1503. (m_delphi in current_settings.modeswitches) and
  1504. (cdo_explicit in cdoptions) then
  1505. begin
  1506. doconv:=tc_int_2_int;
  1507. eq:=te_convert_l1;
  1508. end;
  1509. end;
  1510. end;
  1511. classrefdef :
  1512. begin
  1513. { similar to pointerdef wrt forwards }
  1514. if assigned(def_to.typesym) and
  1515. (tclassrefdef(def_to).pointeddef.typ=forwarddef) then
  1516. begin
  1517. if (def_from.typesym=def_to.typesym) or
  1518. (fromtreetype=niln) then
  1519. eq:=te_equal;
  1520. end
  1521. else
  1522. { class reference types }
  1523. if (def_from.typ=classrefdef) then
  1524. begin
  1525. if equal_defs(tclassrefdef(def_from).pointeddef,tclassrefdef(def_to).pointeddef) then
  1526. begin
  1527. eq:=te_equal;
  1528. end
  1529. else
  1530. begin
  1531. doconv:=tc_equal;
  1532. if (cdo_explicit in cdoptions) or
  1533. tobjectdef(tclassrefdef(def_from).pointeddef).is_related(
  1534. tobjectdef(tclassrefdef(def_to).pointeddef)) then
  1535. eq:=te_convert_l1;
  1536. end;
  1537. end
  1538. else
  1539. if (m_delphi in current_settings.modeswitches) and
  1540. is_voidpointer(def_from) then
  1541. begin
  1542. doconv:=tc_equal;
  1543. { prefer pointer-pointer assignments }
  1544. eq:=te_convert_l2;
  1545. end
  1546. else
  1547. { nil is compatible with class references }
  1548. if (fromtreetype=niln) then
  1549. begin
  1550. doconv:=tc_equal;
  1551. eq:=te_convert_l1;
  1552. end
  1553. else
  1554. { id is compatible with all classref types }
  1555. if (def_from=objc_idtype) then
  1556. begin
  1557. doconv:=tc_equal;
  1558. eq:=te_convert_l1;
  1559. end;
  1560. end;
  1561. filedef :
  1562. begin
  1563. { typed files are all equal to the abstract file type
  1564. name TYPEDFILE in system.pp in is_equal in types.pas
  1565. the problem is that it sholud be also compatible to FILE
  1566. but this would leed to a problem for ASSIGN RESET and REWRITE
  1567. when trying to find the good overloaded function !!
  1568. so all file function are doubled in system.pp
  1569. this is not very beautiful !!}
  1570. if (def_from.typ=filedef) then
  1571. begin
  1572. if (tfiledef(def_from).filetyp=tfiledef(def_to).filetyp) then
  1573. begin
  1574. if
  1575. (
  1576. (tfiledef(def_from).typedfiledef=nil) and
  1577. (tfiledef(def_to).typedfiledef=nil)
  1578. ) or
  1579. (
  1580. (tfiledef(def_from).typedfiledef<>nil) and
  1581. (tfiledef(def_to).typedfiledef<>nil) and
  1582. equal_defs(tfiledef(def_from).typedfiledef,tfiledef(def_to).typedfiledef)
  1583. ) or
  1584. (
  1585. (tfiledef(def_from).filetyp = ft_typed) and
  1586. (tfiledef(def_to).filetyp = ft_typed) and
  1587. (
  1588. (tfiledef(def_from).typedfiledef = tdef(voidtype)) or
  1589. (tfiledef(def_to).typedfiledef = tdef(voidtype))
  1590. )
  1591. ) then
  1592. begin
  1593. eq:=te_equal;
  1594. end;
  1595. end
  1596. else
  1597. if ((tfiledef(def_from).filetyp = ft_untyped) and
  1598. (tfiledef(def_to).filetyp = ft_typed)) or
  1599. ((tfiledef(def_from).filetyp = ft_typed) and
  1600. (tfiledef(def_to).filetyp = ft_untyped)) then
  1601. begin
  1602. doconv:=tc_equal;
  1603. eq:=te_convert_l1;
  1604. end;
  1605. end;
  1606. end;
  1607. recorddef :
  1608. begin
  1609. { interface -> guid }
  1610. if (def_to=rec_tguid) and
  1611. (is_interfacecom_or_dispinterface(def_from)) then
  1612. begin
  1613. doconv:=tc_intf_2_guid;
  1614. eq:=te_convert_l1;
  1615. end;
  1616. end;
  1617. formaldef :
  1618. begin
  1619. doconv:=tc_equal;
  1620. if (def_from.typ=formaldef) then
  1621. eq:=te_equal
  1622. else
  1623. { Just about everything can be converted to a formaldef...}
  1624. if not (def_from.typ in [abstractdef,errordef]) then
  1625. eq:=te_convert_l2;
  1626. end;
  1627. end;
  1628. { if we didn't find an appropriate type conversion yet
  1629. then we search also the := operator }
  1630. if (eq=te_incompatible) and
  1631. { make sure there is not a single variant if variants }
  1632. { are not allowed (otherwise if only cdo_check_operator }
  1633. { and e.g. fromdef=stringdef and todef=variantdef, then }
  1634. { the test will still succeed }
  1635. ((cdo_allow_variant in cdoptions) or
  1636. ((def_from.typ<>variantdef) and
  1637. (def_to.typ<>variantdef) and
  1638. { internal typeconversions always have to be bitcasts (except for
  1639. variants) }
  1640. not(cdo_internal in cdoptions)
  1641. )
  1642. ) and
  1643. (
  1644. { Check for variants? }
  1645. (
  1646. (cdo_allow_variant in cdoptions) and
  1647. ((def_from.typ=variantdef) or (def_to.typ=variantdef))
  1648. ) or
  1649. { Check for operators? }
  1650. (
  1651. (cdo_check_operator in cdoptions) and
  1652. ((def_from.typ<>variantdef) or (def_to.typ<>variantdef))
  1653. )
  1654. ) then
  1655. begin
  1656. operatorpd:=search_assignment_operator(def_from,def_to,cdo_explicit in cdoptions);
  1657. if assigned(operatorpd) then
  1658. eq:=te_convert_operator;
  1659. end;
  1660. { update convtype for te_equal when it is not yet set }
  1661. if (eq=te_equal) and
  1662. (doconv=tc_not_possible) then
  1663. doconv:=tc_equal;
  1664. compare_defs_ext:=eq;
  1665. end;
  1666. function equal_defs(def_from,def_to:tdef):boolean;
  1667. var
  1668. convtyp : tconverttype;
  1669. pd : tprocdef;
  1670. begin
  1671. { Compare defs with nothingn and no explicit typecasts and
  1672. searching for overloaded operators is not needed }
  1673. equal_defs:=(compare_defs_ext(def_from,def_to,nothingn,convtyp,pd,[])>=te_equal);
  1674. end;
  1675. function compare_defs(def_from,def_to:tdef;fromtreetype:tnodetype):tequaltype;
  1676. var
  1677. doconv : tconverttype;
  1678. pd : tprocdef;
  1679. begin
  1680. compare_defs:=compare_defs_ext(def_from,def_to,fromtreetype,doconv,pd,[cdo_check_operator,cdo_allow_variant]);
  1681. end;
  1682. function is_subequal(def1, def2: tdef): boolean;
  1683. var
  1684. basedef1,basedef2 : tenumdef;
  1685. Begin
  1686. is_subequal := false;
  1687. if assigned(def1) and assigned(def2) then
  1688. Begin
  1689. if (def1.typ = orddef) and (def2.typ = orddef) then
  1690. Begin
  1691. { see p.47 of Turbo Pascal 7.01 manual for the separation of types }
  1692. { range checking for case statements is done with testrange }
  1693. case torddef(def1).ordtype of
  1694. u8bit,u16bit,u32bit,u64bit,
  1695. s8bit,s16bit,s32bit,s64bit :
  1696. is_subequal:=(torddef(def2).ordtype in [s64bit,u64bit,s32bit,u32bit,u8bit,s8bit,s16bit,u16bit]);
  1697. pasbool8,pasbool16,pasbool32,pasbool64,
  1698. bool8bit,bool16bit,bool32bit,bool64bit :
  1699. is_subequal:=(torddef(def2).ordtype in [pasbool8,pasbool16,pasbool32,pasbool64,bool8bit,bool16bit,bool32bit,bool64bit]);
  1700. uchar :
  1701. is_subequal:=(torddef(def2).ordtype=uchar);
  1702. uwidechar :
  1703. is_subequal:=(torddef(def2).ordtype=uwidechar);
  1704. end;
  1705. end
  1706. else
  1707. Begin
  1708. { Check if both basedefs are equal }
  1709. if (def1.typ=enumdef) and (def2.typ=enumdef) then
  1710. Begin
  1711. { get both basedefs }
  1712. basedef1:=tenumdef(def1);
  1713. while assigned(basedef1.basedef) do
  1714. basedef1:=basedef1.basedef;
  1715. basedef2:=tenumdef(def2);
  1716. while assigned(basedef2.basedef) do
  1717. basedef2:=basedef2.basedef;
  1718. is_subequal:=(basedef1=basedef2);
  1719. end;
  1720. end;
  1721. end;
  1722. end;
  1723. function potentially_incompatible_univ_paras(def1, def2: tdef): boolean;
  1724. begin
  1725. result :=
  1726. { not entirely safe: different records can be passed differently
  1727. depending on the types of their fields, but they're hard to compare
  1728. (variant records, bitpacked vs non-bitpacked) }
  1729. ((def1.typ in [floatdef,recorddef,arraydef,filedef,variantdef]) and
  1730. (def1.typ<>def2.typ)) or
  1731. { pointers, ordinals and small sets are all passed the same}
  1732. (((def1.typ in [orddef,enumdef,pointerdef,procvardef,classrefdef]) or
  1733. (is_class_or_interface_or_objc(def1)) or
  1734. is_dynamic_array(def1) or
  1735. is_smallset(def1) or
  1736. is_ansistring(def1) or
  1737. is_unicodestring(def1)) <>
  1738. (def2.typ in [orddef,enumdef,pointerdef,procvardef,classrefdef]) or
  1739. (is_class_or_interface_or_objc(def2)) or
  1740. is_dynamic_array(def2) or
  1741. is_smallset(def2) or
  1742. is_ansistring(def2) or
  1743. is_unicodestring(def2)) or
  1744. { shortstrings }
  1745. (is_shortstring(def1)<>
  1746. is_shortstring(def2)) or
  1747. { winlike widestrings }
  1748. (is_widestring(def1)<>
  1749. is_widestring(def2)) or
  1750. { TP-style objects }
  1751. (is_object(def1) <>
  1752. is_object(def2));
  1753. end;
  1754. function compare_paras(para1,para2 : TFPObjectList; acp : tcompare_paras_type; cpoptions: tcompare_paras_options):tequaltype;
  1755. var
  1756. currpara1,
  1757. currpara2 : tparavarsym;
  1758. eq,lowesteq : tequaltype;
  1759. hpd : tprocdef;
  1760. convtype : tconverttype;
  1761. cdoptions : tcompare_defs_options;
  1762. i1,i2 : byte;
  1763. begin
  1764. compare_paras:=te_incompatible;
  1765. cdoptions:=[cdo_parameter,cdo_check_operator,cdo_allow_variant,cdo_strict_undefined_check];
  1766. { we need to parse the list from left-right so the
  1767. not-default parameters are checked first }
  1768. lowesteq:=high(tequaltype);
  1769. i1:=0;
  1770. i2:=0;
  1771. if cpo_ignorehidden in cpoptions then
  1772. begin
  1773. while (i1<para1.count) and
  1774. (vo_is_hidden_para in tparavarsym(para1[i1]).varoptions) do
  1775. inc(i1);
  1776. while (i2<para2.count) and
  1777. (vo_is_hidden_para in tparavarsym(para2[i2]).varoptions) do
  1778. inc(i2);
  1779. end;
  1780. if cpo_ignoreframepointer in cpoptions then
  1781. begin
  1782. if (i1<para1.count) and
  1783. (vo_is_parentfp in tparavarsym(para1[i1]).varoptions) then
  1784. inc(i1);
  1785. if (i2<para2.count) and
  1786. (vo_is_parentfp in tparavarsym(para2[i2]).varoptions) then
  1787. inc(i2);
  1788. end;
  1789. while (i1<para1.count) and (i2<para2.count) do
  1790. begin
  1791. eq:=te_incompatible;
  1792. currpara1:=tparavarsym(para1[i1]);
  1793. currpara2:=tparavarsym(para2[i2]);
  1794. { Unique types must match exact }
  1795. if ((df_unique in currpara1.vardef.defoptions) or (df_unique in currpara2.vardef.defoptions)) and
  1796. (currpara1.vardef<>currpara2.vardef) then
  1797. exit;
  1798. { Handle hidden parameters separately, because self is
  1799. defined as voidpointer for methodpointers }
  1800. if (vo_is_hidden_para in currpara1.varoptions) or
  1801. (vo_is_hidden_para in currpara2.varoptions) then
  1802. begin
  1803. { both must be hidden }
  1804. if (vo_is_hidden_para in currpara1.varoptions)<>(vo_is_hidden_para in currpara2.varoptions) then
  1805. exit;
  1806. eq:=te_exact;
  1807. if not(vo_is_self in currpara1.varoptions) and
  1808. not(vo_is_self in currpara2.varoptions) then
  1809. begin
  1810. if not(cpo_ignorevarspez in cpoptions) and
  1811. (currpara1.varspez<>currpara2.varspez) then
  1812. exit;
  1813. eq:=compare_defs_ext(currpara1.vardef,currpara2.vardef,nothingn,
  1814. convtype,hpd,cdoptions);
  1815. end;
  1816. end
  1817. else
  1818. begin
  1819. case acp of
  1820. cp_value_equal_const :
  1821. begin
  1822. { this one is used for matching parameters from a call
  1823. statement to a procdef -> univ state can't be equal
  1824. in any case since the call statement does not contain
  1825. any information about that }
  1826. if (
  1827. not(cpo_ignorevarspez in cpoptions) and
  1828. (currpara1.varspez<>currpara2.varspez) and
  1829. ((currpara1.varspez in [vs_var,vs_out,vs_constref]) or
  1830. (currpara2.varspez in [vs_var,vs_out,vs_constref]))
  1831. ) then
  1832. exit;
  1833. eq:=compare_defs_ext(currpara1.vardef,currpara2.vardef,nothingn,
  1834. convtype,hpd,cdoptions);
  1835. end;
  1836. cp_all :
  1837. begin
  1838. { used to resolve forward definitions -> headers must
  1839. match exactly, including the "univ" specifier }
  1840. if (not(cpo_ignorevarspez in cpoptions) and
  1841. (currpara1.varspez<>currpara2.varspez)) or
  1842. (currpara1.univpara<>currpara2.univpara) then
  1843. exit;
  1844. eq:=compare_defs_ext(currpara1.vardef,currpara2.vardef,nothingn,
  1845. convtype,hpd,cdoptions);
  1846. end;
  1847. cp_procvar :
  1848. begin
  1849. if not(cpo_ignorevarspez in cpoptions) and
  1850. (currpara1.varspez<>currpara2.varspez) then
  1851. exit;
  1852. { "univ" state doesn't matter here: from univ to non-univ
  1853. matches if the types are compatible (i.e., as usual),
  1854. from from non-univ to univ also matches if the types
  1855. have the same size (checked below) }
  1856. eq:=compare_defs_ext(currpara1.vardef,currpara2.vardef,nothingn,
  1857. convtype,hpd,cdoptions);
  1858. { Parameters must be at least equal otherwise the are incompatible }
  1859. if (eq<te_equal) then
  1860. eq:=te_incompatible;
  1861. end;
  1862. else
  1863. eq:=compare_defs_ext(currpara1.vardef,currpara2.vardef,nothingn,
  1864. convtype,hpd,cdoptions);
  1865. end;
  1866. end;
  1867. { check type }
  1868. if eq=te_incompatible then
  1869. begin
  1870. { special case: "univ" parameters match if their size is equal }
  1871. if not(cpo_ignoreuniv in cpoptions) and
  1872. currpara2.univpara and
  1873. is_valid_univ_para_type(currpara1.vardef) and
  1874. (currpara1.vardef.size=currpara2.vardef.size) then
  1875. begin
  1876. { only pick as last choice }
  1877. eq:=te_convert_l5;
  1878. if (acp=cp_procvar) and
  1879. (cpo_warn_incompatible_univ in cpoptions) then
  1880. begin
  1881. { if the types may be passed in different ways by the
  1882. calling convention then this can lead to crashes
  1883. (note: not an exhaustive check, and failing this
  1884. this check does not mean things will crash on all
  1885. platforms) }
  1886. if potentially_incompatible_univ_paras(currpara1.vardef,currpara2.vardef) then
  1887. Message2(type_w_procvar_univ_conflicting_para,currpara1.vardef.typename,currpara2.vardef.typename)
  1888. end;
  1889. end
  1890. else
  1891. exit;
  1892. end;
  1893. { open strings can never match exactly, since you cannot define }
  1894. { a separate "open string" type -> we have to be able to }
  1895. { consider those as exact when resolving forward definitions. }
  1896. { The same goes for array of const. Open arrays are handled }
  1897. { already (if their element types match exactly, they are }
  1898. { considered to be an exact match) }
  1899. { And also for "inline defined" function parameter definitions }
  1900. { (i.e., function types directly declared in a parameter list) }
  1901. if (is_array_of_const(currpara1.vardef) or
  1902. is_open_string(currpara1.vardef) or
  1903. ((currpara1.vardef.typ = procvardef) and
  1904. not(assigned(currpara1.vardef.typesym)))) and
  1905. (eq=te_equal) and
  1906. (cpo_openequalisexact in cpoptions) then
  1907. eq:=te_exact;
  1908. if eq<lowesteq then
  1909. lowesteq:=eq;
  1910. { also check default value if both have it declared }
  1911. if (cpo_comparedefaultvalue in cpoptions) then
  1912. begin
  1913. if assigned(currpara1.defaultconstsym) and
  1914. assigned(currpara2.defaultconstsym) then
  1915. begin
  1916. if not equal_constsym(tconstsym(currpara1.defaultconstsym),tconstsym(currpara2.defaultconstsym)) then
  1917. exit;
  1918. end
  1919. { cannot have that the second (= implementation) has a default value declared and the
  1920. other (interface) doesn't }
  1921. else if not assigned(currpara1.defaultconstsym) and assigned(currpara2.defaultconstsym) then
  1922. exit;
  1923. end;
  1924. if not(cpo_compilerproc in cpoptions) and
  1925. not(cpo_rtlproc in cpoptions) and
  1926. is_ansistring(currpara1.vardef) and
  1927. is_ansistring(currpara2.vardef) and
  1928. (tstringdef(currpara1.vardef).encoding<>tstringdef(currpara2.vardef).encoding) and
  1929. ((tstringdef(currpara1.vardef).encoding=globals.CP_NONE) or
  1930. (tstringdef(currpara2.vardef).encoding=globals.CP_NONE)
  1931. ) then
  1932. eq:=te_convert_l1;
  1933. if eq<lowesteq then
  1934. lowesteq:=eq;
  1935. inc(i1);
  1936. inc(i2);
  1937. if cpo_ignorehidden in cpoptions then
  1938. begin
  1939. while (i1<para1.count) and
  1940. (vo_is_hidden_para in tparavarsym(para1[i1]).varoptions) do
  1941. inc(i1);
  1942. while (i2<para2.count) and
  1943. (vo_is_hidden_para in tparavarsym(para2[i2]).varoptions) do
  1944. inc(i2);
  1945. end;
  1946. if cpo_ignoreframepointer in cpoptions then
  1947. begin
  1948. if (i1<para1.count) and
  1949. (vo_is_parentfp in tparavarsym(para1[i1]).varoptions) then
  1950. inc(i1);
  1951. if (i2<para2.count) and
  1952. (vo_is_parentfp in tparavarsym(para2[i2]).varoptions) then
  1953. inc(i2);
  1954. end;
  1955. end;
  1956. { when both lists are empty then the parameters are equal. Also
  1957. when one list is empty and the other has a parameter with default
  1958. value assigned then the parameters are also equal }
  1959. if ((i1>=para1.count) and (i2>=para2.count)) or
  1960. ((cpo_allowdefaults in cpoptions) and
  1961. (((i1<para1.count) and assigned(tparavarsym(para1[i1]).defaultconstsym)) or
  1962. ((i2<para2.count) and assigned(tparavarsym(para2[i2]).defaultconstsym)))) then
  1963. compare_paras:=lowesteq;
  1964. end;
  1965. function proc_to_procvar_equal(def1:tabstractprocdef;def2:tprocvardef;checkincompatibleuniv: boolean):tequaltype;
  1966. var
  1967. eq : tequaltype;
  1968. po_comp : tprocoptions;
  1969. pa_comp: tcompare_paras_options;
  1970. begin
  1971. proc_to_procvar_equal:=te_incompatible;
  1972. if not(assigned(def1)) or not(assigned(def2)) then
  1973. exit;
  1974. { check for method pointer and local procedure pointer:
  1975. a) if one is a procedure of object, the other also has to be one
  1976. b) if one is a pure address, the other also has to be one
  1977. except if def1 is a global proc and def2 is a nested procdef
  1978. (global procedures can be converted into nested procvars)
  1979. c) if def1 is a nested procedure, then def2 has to be a nested
  1980. procvar and def1 has to have the po_delphi_nested_cc option
  1981. d) if def1 is a procvar, def1 and def2 both have to be nested or
  1982. non-nested (we don't allow assignments from non-nested to
  1983. nested procvars to make sure that we can still implement
  1984. nested procvars using trampolines -- e.g., this would be
  1985. necessary for LLVM or CIL as long as they do not have support
  1986. for Delphi-style frame pointer parameter passing) }
  1987. if (def1.is_methodpointer<>def2.is_methodpointer) or { a) }
  1988. ((def1.is_addressonly<>def2.is_addressonly) and { b) }
  1989. (is_nested_pd(def1) or
  1990. not is_nested_pd(def2))) or
  1991. ((def1.typ=procdef) and { c) }
  1992. is_nested_pd(def1) and
  1993. (not(po_delphi_nested_cc in def1.procoptions) or
  1994. not is_nested_pd(def2))) or
  1995. ((def1.typ=procvardef) and { d) }
  1996. (is_nested_pd(def1)<>is_nested_pd(def2))) then
  1997. exit;
  1998. pa_comp:=[cpo_ignoreframepointer];
  1999. if checkincompatibleuniv then
  2000. include(pa_comp,cpo_warn_incompatible_univ);
  2001. { check return value and options, methodpointer is already checked }
  2002. po_comp:=[po_staticmethod,po_interrupt,
  2003. po_iocheck,po_varargs];
  2004. if (m_delphi in current_settings.modeswitches) then
  2005. exclude(po_comp,po_varargs);
  2006. if (def1.proccalloption=def2.proccalloption) and
  2007. ((po_comp * def1.procoptions)= (po_comp * def2.procoptions)) and
  2008. equal_defs(def1.returndef,def2.returndef) then
  2009. begin
  2010. { return equal type based on the parameters, but a proc->procvar
  2011. is never exact, so map an exact match of the parameters to
  2012. te_equal }
  2013. eq:=compare_paras(def1.paras,def2.paras,cp_procvar,pa_comp);
  2014. if eq=te_exact then
  2015. eq:=te_equal;
  2016. if (eq=te_equal) then
  2017. begin
  2018. { prefer non-nested to non-nested over non-nested to nested }
  2019. if (is_nested_pd(def1)<>is_nested_pd(def2)) then
  2020. eq:=te_convert_l1;
  2021. end;
  2022. proc_to_procvar_equal:=eq;
  2023. end;
  2024. end;
  2025. function compatible_childmethod_resultdef(parentretdef, childretdef: tdef): boolean;
  2026. begin
  2027. compatible_childmethod_resultdef :=
  2028. (equal_defs(parentretdef,childretdef)) or
  2029. ((parentretdef.typ=objectdef) and
  2030. (childretdef.typ=objectdef) and
  2031. is_class_or_interface_or_objc_or_java(parentretdef) and
  2032. is_class_or_interface_or_objc_or_java(childretdef) and
  2033. (tobjectdef(childretdef).is_related(tobjectdef(parentretdef))))
  2034. end;
  2035. end.