defcmp.pas 114 KB

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