defcmp.pas 114 KB

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