defcmp.pas 123 KB

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