defcmp.pas 117 KB

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