defcmp.pas 105 KB

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