defcmp.pas 114 KB

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