defcmp.pas 133 KB

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