defbase.pas 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit provides some help routines for type handling
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit defbase;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cclasses,
  23. cpuinfo,
  24. globals,
  25. node,
  26. symconst,symbase,symtype,symdef,symsym;
  27. type
  28. tmmxtype = (mmxno,mmxu8bit,mmxs8bit,mmxu16bit,mmxs16bit,
  29. mmxu32bit,mmxs32bit,mmxfixed16,mmxsingle);
  30. const
  31. {# true if we must never copy this parameter }
  32. never_copy_const_param : boolean = false;
  33. {*****************************************************************************
  34. Basic type functions
  35. *****************************************************************************}
  36. {# Returns true, if definition defines an ordinal type }
  37. function is_ordinal(def : tdef) : boolean;
  38. {# Returns the minimal integer value of the type }
  39. function get_min_value(def : tdef) : TConstExprInt;
  40. {# Returns basetype of the specified integer range }
  41. function range_to_basetype(low,high:TConstExprInt):tbasetype;
  42. {# Returns true, if definition defines an integer type }
  43. function is_integer(def : tdef) : boolean;
  44. {# Returns true if definition is a boolean }
  45. function is_boolean(def : tdef) : boolean;
  46. {# Returns true if definition is a char
  47. This excludes the unicode char.
  48. }
  49. function is_char(def : tdef) : boolean;
  50. {# Returns true if definition is a widechar }
  51. function is_widechar(def : tdef) : boolean;
  52. {# Returns true if definition is a void}
  53. function is_void(def : tdef) : boolean;
  54. {# Returns true if definition is a smallset}
  55. function is_smallset(p : tdef) : boolean;
  56. {# Returns true, if def defines a signed data type
  57. (only for ordinal types)
  58. }
  59. function is_signed(def : tdef) : boolean;
  60. {# Returns true whether def_from's range is comprised in def_to's if both are
  61. orddefs, false otherwise }
  62. function is_in_limit(def_from,def_to : tdef) : boolean;
  63. function is_in_limit_value(val_from:TConstExprInt;def_from,def_to : tdef) : boolean;
  64. {*****************************************************************************
  65. Array helper functions
  66. *****************************************************************************}
  67. {# Returns true, if p points to a zero based (non special like open or
  68. dynamic array def).
  69. This is mainly used to see if the array
  70. is convertable to a pointer
  71. }
  72. function is_zero_based_array(p : tdef) : boolean;
  73. {# Returns true if p points to an open array definition }
  74. function is_open_array(p : tdef) : boolean;
  75. {# Returns true if p points to a dynamic array definition }
  76. function is_dynamic_array(p : tdef) : boolean;
  77. {# Returns true, if p points to an array of const definition }
  78. function is_array_constructor(p : tdef) : boolean;
  79. {# Returns true, if p points to a variant array }
  80. function is_variant_array(p : tdef) : boolean;
  81. {# Returns true, if p points to an array of const }
  82. function is_array_of_const(p : tdef) : boolean;
  83. {# Returns true, if p points any kind of special array
  84. That is if the array is an open array, a variant
  85. array, an array constants constructor, or an
  86. array of const.
  87. }
  88. function is_special_array(p : tdef) : boolean;
  89. {# Returns true if p is a char array def }
  90. function is_chararray(p : tdef) : boolean;
  91. {# Returns true if p is a wide char array def }
  92. function is_widechararray(p : tdef) : boolean;
  93. {*****************************************************************************
  94. String helper functions
  95. *****************************************************************************}
  96. {# Returns true if p points to an open string type }
  97. function is_open_string(p : tdef) : boolean;
  98. {# Returns true if p is an ansi string type }
  99. function is_ansistring(p : tdef) : boolean;
  100. {# Returns true if p is a long string type }
  101. function is_longstring(p : tdef) : boolean;
  102. {# returns true if p is a wide string type }
  103. function is_widestring(p : tdef) : boolean;
  104. {# Returns true if p is a short string type }
  105. function is_shortstring(p : tdef) : boolean;
  106. {# Returns true if p is a pchar def }
  107. function is_pchar(p : tdef) : boolean;
  108. {# Returns true if p is a pwidechar def }
  109. function is_pwidechar(p : tdef) : boolean;
  110. {# Returns true if p is a voidpointer def }
  111. function is_voidpointer(p : tdef) : boolean;
  112. {# Returns true, if definition is a float }
  113. function is_fpu(def : tdef) : boolean;
  114. {# Returns true, if def is a currency type }
  115. function is_currency(def : tdef) : boolean;
  116. {# Returns true, if def is a 64 bit integer type }
  117. function is_64bitint(def : tdef) : boolean;
  118. {# Returns true, if def1 and def2 are semantically the same }
  119. function is_equal(def1,def2 : tdef) : boolean;
  120. {# Checks for type compatibility (subgroups of type)
  121. used for case statements... probably missing stuff
  122. to use on other types
  123. }
  124. function is_subequal(def1, def2: tdef): boolean;
  125. type
  126. tconverttype = (
  127. tc_equal,
  128. tc_not_possible,
  129. tc_string_2_string,
  130. tc_char_2_string,
  131. tc_char_2_chararray,
  132. tc_pchar_2_string,
  133. tc_cchar_2_pchar,
  134. tc_cstring_2_pchar,
  135. tc_ansistring_2_pchar,
  136. tc_string_2_chararray,
  137. tc_chararray_2_string,
  138. tc_array_2_pointer,
  139. tc_pointer_2_array,
  140. tc_int_2_int,
  141. tc_int_2_bool,
  142. tc_bool_2_bool,
  143. tc_bool_2_int,
  144. tc_real_2_real,
  145. tc_int_2_real,
  146. tc_proc_2_procvar,
  147. tc_arrayconstructor_2_set,
  148. tc_load_smallset,
  149. tc_cord_2_pointer,
  150. tc_intf_2_string,
  151. tc_intf_2_guid,
  152. tc_class_2_intf,
  153. tc_char_2_char,
  154. tc_normal_2_smallset,
  155. tc_dynarray_2_openarray,
  156. tc_pwchar_2_string
  157. );
  158. function assignment_overloaded(from_def,to_def : tdef) : tprocdef;
  159. { Returns:
  160. 0 - Not convertable
  161. 1 - Convertable
  162. 2 - Convertable, but not first choice }
  163. function isconvertable(def_from,def_to : tdef;
  164. var doconv : tconverttype;
  165. fromtreetype : tnodetype;
  166. explicit : boolean) : byte;
  167. { this routine is recusrive safe, and is used by the
  168. checking of overloaded assignment operators ONLY!
  169. }
  170. function overloaded_assignment_isconvertable(def_from,def_to : tdef;
  171. var doconv : tconverttype;
  172. fromtreetype : tnodetype;
  173. explicit : boolean; var overload_procs : pprocdeflist) : byte;
  174. { Same as is_equal, but with error message if failed }
  175. function CheckTypes(def1,def2 : tdef) : boolean;
  176. function equal_constsym(sym1,sym2:tconstsym):boolean;
  177. { if acp is cp_all the var const or nothing are considered equal }
  178. type
  179. compare_type = ( cp_none, cp_value_equal_const, cp_all);
  180. {# true, if two parameter lists are equal
  181. if acp is cp_none, all have to match exactly
  182. if acp is cp_value_equal_const call by value
  183. and call by const parameter are assumed as
  184. equal
  185. allowdefaults indicates if default value parameters
  186. are allowed (in this case, the search order will first
  187. search for a routine with default parameters, before
  188. searching for the same definition with no parameters)
  189. }
  190. function equal_paras(paralist1,paralist2 : TLinkedList; acp : compare_type;allowdefaults:boolean) : boolean;
  191. { True if a type can be allowed for another one
  192. in a func var }
  193. function convertable_paras(paralist1,paralist2 : tlinkedlist; acp : compare_type) : boolean;
  194. { True if a function can be assigned to a procvar }
  195. { changed first argument type to pabstractprocdef so that it can also be }
  196. { used to test compatibility between two pprocvardefs (JM) }
  197. function proc_to_procvar_equal(def1:tabstractprocdef;def2:tprocvardef;exact:boolean) : boolean;
  198. { function get_proc_2_procvar_def(p:tprocsym;d:tprocvardef):tprocdef;}
  199. {# If @var(l) isn't in the range of def a range check error (if not explicit) is generated and
  200. the value is placed within the range
  201. }
  202. procedure testrange(def : tdef;var l : tconstexprint;explicit:boolean);
  203. {# Returns the range of def, where @var(l) is the low-range and @var(h) is
  204. the high-range.
  205. }
  206. procedure getrange(def : tdef;var l : TConstExprInt;var h : TConstExprInt);
  207. { some type helper routines for MMX support }
  208. function is_mmx_able_array(p : tdef) : boolean;
  209. {# returns the mmx type }
  210. function mmx_type(p : tdef) : tmmxtype;
  211. {# returns true, if sym needs an entry in the proplist of a class rtti }
  212. function needs_prop_entry(sym : tsym) : boolean;
  213. implementation
  214. uses
  215. globtype,tokens,verbose,
  216. symtable;
  217. function needs_prop_entry(sym : tsym) : boolean;
  218. begin
  219. needs_prop_entry:=(sp_published in tsym(sym).symoptions) and
  220. (sym.typ in [propertysym,varsym]);
  221. end;
  222. function equal_constsym(sym1,sym2:tconstsym):boolean;
  223. var
  224. p1,p2,pend : pchar;
  225. begin
  226. equal_constsym:=false;
  227. if sym1.consttyp<>sym2.consttyp then
  228. exit;
  229. case sym1.consttyp of
  230. constint,
  231. constbool,
  232. constchar,
  233. constord :
  234. equal_constsym:=(sym1.valueord=sym2.valueord);
  235. constpointer :
  236. equal_constsym:=(sym1.valueordptr=sym2.valueordptr);
  237. conststring,constresourcestring :
  238. begin
  239. if sym1.len=sym2.len then
  240. begin
  241. p1:=pchar(sym1.valueptr);
  242. p2:=pchar(sym2.valueptr);
  243. pend:=p1+sym1.len;
  244. while (p1<pend) do
  245. begin
  246. if p1^<>p2^ then
  247. break;
  248. inc(p1);
  249. inc(p2);
  250. end;
  251. if (p1=pend) then
  252. equal_constsym:=true;
  253. end;
  254. end;
  255. constreal :
  256. equal_constsym:=(pbestreal(sym1.valueptr)^=pbestreal(sym2.valueptr)^);
  257. constset :
  258. equal_constsym:=(pnormalset(sym1.valueptr)^=pnormalset(sym2.valueptr)^);
  259. constnil :
  260. equal_constsym:=true;
  261. end;
  262. end;
  263. { compare_type = ( cp_none, cp_value_equal_const, cp_all); }
  264. function equal_paras(paralist1,paralist2 : TLinkedList; acp : compare_type;allowdefaults:boolean) : boolean;
  265. var
  266. def1,def2 : TParaItem;
  267. begin
  268. { we need to parse the list from left-right so the
  269. not-default parameters are checked first }
  270. def1:=TParaItem(paralist1.last);
  271. def2:=TParaItem(paralist2.last);
  272. while (assigned(def1)) and (assigned(def2)) do
  273. begin
  274. case acp of
  275. cp_value_equal_const :
  276. begin
  277. if not(is_equal(def1.paratype.def,def2.paratype.def)) or
  278. ((def1.paratyp<>def2.paratyp) and
  279. ((def1.paratyp in [vs_var,vs_out]) or
  280. (def2.paratyp in [vs_var,vs_out])
  281. )
  282. ) then
  283. begin
  284. equal_paras:=false;
  285. exit;
  286. end;
  287. end;
  288. cp_all :
  289. begin
  290. if not(is_equal(def1.paratype.def,def2.paratype.def)) or
  291. (def1.paratyp<>def2.paratyp) then
  292. begin
  293. equal_paras:=false;
  294. exit;
  295. end;
  296. end;
  297. cp_none :
  298. begin
  299. if not(is_equal(def1.paratype.def,def2.paratype.def)) then
  300. begin
  301. equal_paras:=false;
  302. exit;
  303. end;
  304. { also check default value if both have it declared }
  305. if assigned(def1.defaultvalue) and
  306. assigned(def2.defaultvalue) then
  307. begin
  308. if not equal_constsym(tconstsym(def1.defaultvalue),tconstsym(def2.defaultvalue)) then
  309. begin
  310. equal_paras:=false;
  311. exit;
  312. end;
  313. end;
  314. end;
  315. end;
  316. def1:=TParaItem(def1.previous);
  317. def2:=TParaItem(def2.previous);
  318. end;
  319. { when both lists are empty then the parameters are equal. Also
  320. when one list is empty and the other has a parameter with default
  321. value assigned then the parameters are also equal }
  322. if ((def1=nil) and (def2=nil)) or
  323. (allowdefaults and
  324. ((assigned(def1) and assigned(def1.defaultvalue)) or
  325. (assigned(def2) and assigned(def2.defaultvalue)))) then
  326. equal_paras:=true
  327. else
  328. equal_paras:=false;
  329. end;
  330. function convertable_paras(paralist1,paralist2 : TLinkedList;acp : compare_type) : boolean;
  331. var
  332. def1,def2 : TParaItem;
  333. doconv : tconverttype;
  334. p : pointer;
  335. begin
  336. def1:=TParaItem(paralist1.first);
  337. def2:=TParaItem(paralist2.first);
  338. while (assigned(def1)) and (assigned(def2)) do
  339. begin
  340. case acp of
  341. cp_value_equal_const :
  342. begin
  343. if (isconvertable(def1.paratype.def,def2.paratype.def,doconv,callparan,false)=0) or
  344. ((def1.paratyp<>def2.paratyp) and
  345. ((def1.paratyp in [vs_out,vs_var]) or
  346. (def2.paratyp in [vs_out,vs_var])
  347. )
  348. ) then
  349. begin
  350. convertable_paras:=false;
  351. exit;
  352. end;
  353. end;
  354. cp_all :
  355. begin
  356. if (isconvertable(def1.paratype.def,def2.paratype.def,doconv,callparan,false)=0) or
  357. (def1.paratyp<>def2.paratyp) then
  358. begin
  359. convertable_paras:=false;
  360. exit;
  361. end;
  362. end;
  363. cp_none :
  364. begin
  365. if (isconvertable(def1.paratype.def,def2.paratype.def,doconv,callparan,false)=0) then
  366. begin
  367. convertable_paras:=false;
  368. exit;
  369. end;
  370. end;
  371. end;
  372. def1:=TParaItem(def1.next);
  373. def2:=TParaItem(def2.next);
  374. end;
  375. if (def1=nil) and (def2=nil) then
  376. convertable_paras:=true
  377. else
  378. convertable_paras:=false;
  379. end;
  380. { true if a function can be assigned to a procvar }
  381. { changed first argument type to pabstractprocdef so that it can also be }
  382. { used to test compatibility between two pprocvardefs (JM) }
  383. function proc_to_procvar_equal(def1:tabstractprocdef;def2:tprocvardef;exact:boolean) : boolean;
  384. const
  385. po_comp = po_compatibility_options-[po_methodpointer,po_classmethod];
  386. var
  387. ismethod : boolean;
  388. begin
  389. proc_to_procvar_equal:=false;
  390. if not(assigned(def1)) or not(assigned(def2)) then
  391. exit;
  392. { check for method pointer }
  393. if def1.deftype=procvardef then
  394. begin
  395. ismethod:=(po_methodpointer in def1.procoptions);
  396. end
  397. else
  398. begin
  399. ismethod:=assigned(def1.owner) and
  400. (def1.owner.symtabletype=objectsymtable);
  401. end;
  402. if (ismethod and not (po_methodpointer in def2.procoptions)) or
  403. (not(ismethod) and (po_methodpointer in def2.procoptions)) then
  404. begin
  405. Message(type_e_no_method_and_procedure_not_compatible);
  406. exit;
  407. end;
  408. { check return value and para's and options, methodpointer is already checked
  409. parameters may also be convertable }
  410. if is_equal(def1.rettype.def,def2.rettype.def) and
  411. (equal_paras(def1.para,def2.para,cp_all,false) or
  412. ((not exact) and convertable_paras(def1.para,def2.para,cp_all))) and
  413. ((po_comp * def1.procoptions)= (po_comp * def2.procoptions)) then
  414. proc_to_procvar_equal:=true
  415. else
  416. proc_to_procvar_equal:=false;
  417. end;
  418. { returns true, if def uses FPU }
  419. function is_fpu(def : tdef) : boolean;
  420. begin
  421. is_fpu:=(def.deftype=floatdef);
  422. end;
  423. { returns true, if def is a currency type }
  424. function is_currency(def : tdef) : boolean;
  425. begin
  426. is_currency:=(def.deftype=floatdef) and (tfloatdef(def).typ=s64currency);
  427. end;
  428. function range_to_basetype(low,high:TConstExprInt):tbasetype;
  429. begin
  430. { generate a unsigned range if high<0 and low>=0 }
  431. if (low>=0) and (high<0) then
  432. range_to_basetype:=u32bit
  433. else if (low>=0) and (high<=255) then
  434. range_to_basetype:=u8bit
  435. else if (low>=-128) and (high<=127) then
  436. range_to_basetype:=s8bit
  437. else if (low>=0) and (high<=65536) then
  438. range_to_basetype:=u16bit
  439. else if (low>=-32768) and (high<=32767) then
  440. range_to_basetype:=s16bit
  441. else
  442. range_to_basetype:=s32bit;
  443. end;
  444. { true if p is an ordinal }
  445. function is_ordinal(def : tdef) : boolean;
  446. var
  447. dt : tbasetype;
  448. begin
  449. case def.deftype of
  450. orddef :
  451. begin
  452. dt:=torddef(def).typ;
  453. is_ordinal:=dt in [uchar,uwidechar,
  454. u8bit,u16bit,u32bit,u64bit,
  455. s8bit,s16bit,s32bit,s64bit,
  456. bool8bit,bool16bit,bool32bit];
  457. end;
  458. enumdef :
  459. is_ordinal:=true;
  460. else
  461. is_ordinal:=false;
  462. end;
  463. end;
  464. { returns the min. value of the type }
  465. function get_min_value(def : tdef) : TConstExprInt;
  466. begin
  467. case def.deftype of
  468. orddef:
  469. get_min_value:=torddef(def).low;
  470. enumdef:
  471. get_min_value:=tenumdef(def).min;
  472. else
  473. get_min_value:=0;
  474. end;
  475. end;
  476. { true if p is an integer }
  477. function is_integer(def : tdef) : boolean;
  478. begin
  479. is_integer:=(def.deftype=orddef) and
  480. (torddef(def).typ in [u8bit,u16bit,u32bit,u64bit,
  481. s8bit,s16bit,s32bit,s64bit]);
  482. end;
  483. { true if p is a boolean }
  484. function is_boolean(def : tdef) : boolean;
  485. begin
  486. is_boolean:=(def.deftype=orddef) and
  487. (torddef(def).typ in [bool8bit,bool16bit,bool32bit]);
  488. end;
  489. { true if p is a void }
  490. function is_void(def : tdef) : boolean;
  491. begin
  492. is_void:=(def.deftype=orddef) and
  493. (torddef(def).typ=uvoid);
  494. end;
  495. { true if p is a char }
  496. function is_char(def : tdef) : boolean;
  497. begin
  498. is_char:=(def.deftype=orddef) and
  499. (torddef(def).typ=uchar);
  500. end;
  501. { true if p is a wchar }
  502. function is_widechar(def : tdef) : boolean;
  503. begin
  504. is_widechar:=(def.deftype=orddef) and
  505. (torddef(def).typ=uwidechar);
  506. end;
  507. { true if p is signed (integer) }
  508. function is_signed(def : tdef) : boolean;
  509. var
  510. dt : tbasetype;
  511. begin
  512. case def.deftype of
  513. orddef :
  514. begin
  515. dt:=torddef(def).typ;
  516. is_signed:=(dt in [s8bit,s16bit,s32bit,s64bit]);
  517. end;
  518. enumdef :
  519. is_signed:=tenumdef(def).min < 0;
  520. arraydef :
  521. is_signed:=is_signed(tarraydef(def).rangetype.def);
  522. else
  523. is_signed:=false;
  524. end;
  525. end;
  526. function is_in_limit(def_from,def_to : tdef) : boolean;
  527. var
  528. fromqword, toqword: boolean;
  529. begin
  530. if (def_from.deftype <> orddef) or
  531. (def_to.deftype <> orddef) then
  532. begin
  533. is_in_limit := false;
  534. exit;
  535. end;
  536. fromqword := torddef(def_from).typ = u64bit;
  537. toqword := torddef(def_to).typ = u64bit;
  538. is_in_limit:=(toqword and is_signed(def_from)) or
  539. ((not fromqword) and
  540. (torddef(def_from).low>=torddef(def_to).low) and
  541. (torddef(def_from).high<=torddef(def_to).high));
  542. end;
  543. function is_in_limit_value(val_from:TConstExprInt;def_from,def_to : tdef) : boolean;
  544. begin
  545. if (def_from.deftype <> orddef) and
  546. (def_to.deftype <> orddef) then
  547. internalerror(200210062);
  548. if (torddef(def_to).typ = u64bit) then
  549. begin
  550. is_in_limit_value:=((TConstExprUInt(val_from)>=TConstExprUInt(torddef(def_to).low)) and
  551. (TConstExprUInt(val_from)<=TConstExprUInt(torddef(def_to).high)));
  552. end
  553. else
  554. begin;
  555. is_in_limit_value:=((val_from>=torddef(def_to).low) and
  556. (val_from<=torddef(def_to).high));
  557. end;
  558. end;
  559. { true, if p points to an open array def }
  560. function is_open_string(p : tdef) : boolean;
  561. begin
  562. is_open_string:=(p.deftype=stringdef) and
  563. (tstringdef(p).string_typ=st_shortstring) and
  564. (tstringdef(p).len=0);
  565. end;
  566. { true, if p points to a zero based array def }
  567. function is_zero_based_array(p : tdef) : boolean;
  568. begin
  569. is_zero_based_array:=(p.deftype=arraydef) and
  570. (tarraydef(p).lowrange=0) and
  571. not(is_special_array(p));
  572. end;
  573. { true if p points to a dynamic array def }
  574. function is_dynamic_array(p : tdef) : boolean;
  575. begin
  576. is_dynamic_array:=(p.deftype=arraydef) and
  577. tarraydef(p).IsDynamicArray;
  578. end;
  579. { true, if p points to an open array def }
  580. function is_open_array(p : tdef) : boolean;
  581. begin
  582. { check for s32bittype is needed, because for u32bit the high
  583. range is also -1 ! (PFV) }
  584. is_open_array:=(p.deftype=arraydef) and
  585. (tarraydef(p).rangetype.def=s32bittype.def) and
  586. (tarraydef(p).lowrange=0) and
  587. (tarraydef(p).highrange=-1) and
  588. not(tarraydef(p).IsConstructor) and
  589. not(tarraydef(p).IsVariant) and
  590. not(tarraydef(p).IsArrayOfConst) and
  591. not(tarraydef(p).IsDynamicArray);
  592. end;
  593. { true, if p points to an array of const def }
  594. function is_array_constructor(p : tdef) : boolean;
  595. begin
  596. is_array_constructor:=(p.deftype=arraydef) and
  597. (tarraydef(p).IsConstructor);
  598. end;
  599. { true, if p points to a variant array }
  600. function is_variant_array(p : tdef) : boolean;
  601. begin
  602. is_variant_array:=(p.deftype=arraydef) and
  603. (tarraydef(p).IsVariant);
  604. end;
  605. { true, if p points to an array of const }
  606. function is_array_of_const(p : tdef) : boolean;
  607. begin
  608. is_array_of_const:=(p.deftype=arraydef) and
  609. (tarraydef(p).IsArrayOfConst);
  610. end;
  611. { true, if p points to a special array }
  612. function is_special_array(p : tdef) : boolean;
  613. begin
  614. is_special_array:=(p.deftype=arraydef) and
  615. ((tarraydef(p).IsVariant) or
  616. (tarraydef(p).IsArrayOfConst) or
  617. (tarraydef(p).IsConstructor) or
  618. is_open_array(p)
  619. );
  620. end;
  621. { true if p is an ansi string def }
  622. function is_ansistring(p : tdef) : boolean;
  623. begin
  624. is_ansistring:=(p.deftype=stringdef) and
  625. (tstringdef(p).string_typ=st_ansistring);
  626. end;
  627. { true if p is an long string def }
  628. function is_longstring(p : tdef) : boolean;
  629. begin
  630. is_longstring:=(p.deftype=stringdef) and
  631. (tstringdef(p).string_typ=st_longstring);
  632. end;
  633. { true if p is an wide string def }
  634. function is_widestring(p : tdef) : boolean;
  635. begin
  636. is_widestring:=(p.deftype=stringdef) and
  637. (tstringdef(p).string_typ=st_widestring);
  638. end;
  639. { true if p is an short string def }
  640. function is_shortstring(p : tdef) : boolean;
  641. begin
  642. is_shortstring:=(p.deftype=stringdef) and
  643. (tstringdef(p).string_typ=st_shortstring);
  644. end;
  645. { true if p is a char array def }
  646. function is_chararray(p : tdef) : boolean;
  647. begin
  648. is_chararray:=(p.deftype=arraydef) and
  649. is_equal(tarraydef(p).elementtype.def,cchartype.def) and
  650. not(is_special_array(p));
  651. end;
  652. { true if p is a widechar array def }
  653. function is_widechararray(p : tdef) : boolean;
  654. begin
  655. is_widechararray:=(p.deftype=arraydef) and
  656. is_equal(tarraydef(p).elementtype.def,cwidechartype.def) and
  657. not(is_special_array(p));
  658. end;
  659. { true if p is a pchar def }
  660. function is_pchar(p : tdef) : boolean;
  661. begin
  662. is_pchar:=(p.deftype=pointerdef) and
  663. (is_equal(tpointerdef(p).pointertype.def,cchartype.def) or
  664. (is_zero_based_array(tpointerdef(p).pointertype.def) and
  665. is_chararray(tpointerdef(p).pointertype.def)));
  666. end;
  667. { true if p is a pchar def }
  668. function is_pwidechar(p : tdef) : boolean;
  669. begin
  670. is_pwidechar:=(p.deftype=pointerdef) and
  671. (is_equal(tpointerdef(p).pointertype.def,cwidechartype.def) or
  672. (is_zero_based_array(tpointerdef(p).pointertype.def) and
  673. is_widechararray(tpointerdef(p).pointertype.def)));
  674. end;
  675. { true if p is a voidpointer def }
  676. function is_voidpointer(p : tdef) : boolean;
  677. begin
  678. is_voidpointer:=(p.deftype=pointerdef) and
  679. (tpointerdef(p).pointertype.def.deftype=orddef) and
  680. (torddef(tpointerdef(p).pointertype.def).typ=uvoid);
  681. end;
  682. { true if p is a smallset def }
  683. function is_smallset(p : tdef) : boolean;
  684. begin
  685. is_smallset:=(p.deftype=setdef) and
  686. (tsetdef(p).settype=smallset);
  687. end;
  688. { true, if def is a 64 bit int type }
  689. function is_64bitint(def : tdef) : boolean;
  690. begin
  691. is_64bitint:=(def.deftype=orddef) and (torddef(def).typ in [u64bit,s64bit])
  692. end;
  693. { if l isn't in the range of def a range check error (if not explicit) is generated and
  694. the value is placed within the range }
  695. procedure testrange(def : tdef;var l : tconstexprint;explicit:boolean);
  696. var
  697. lv,hv: TConstExprInt;
  698. error: boolean;
  699. begin
  700. error := false;
  701. { for 64 bit types we need only to check if it is less than }
  702. { zero, if def is a qword node }
  703. if is_64bitint(def) then
  704. begin
  705. if (l<0) and (torddef(def).typ=u64bit) then
  706. begin
  707. { don't zero the result, because it may come from hex notation
  708. like $ffffffffffffffff! (JM)
  709. l:=0; }
  710. if not explicit then
  711. begin
  712. if (cs_check_range in aktlocalswitches) then
  713. Message(parser_e_range_check_error)
  714. else
  715. Message(parser_w_range_check_error);
  716. end;
  717. error := true;
  718. end;
  719. end
  720. else
  721. begin
  722. getrange(def,lv,hv);
  723. if (def.deftype=orddef) and
  724. (torddef(def).typ=u32bit) then
  725. begin
  726. if (l < cardinal(lv)) or
  727. (l > cardinal(hv)) then
  728. begin
  729. if not explicit then
  730. begin
  731. if (cs_check_range in aktlocalswitches) then
  732. Message(parser_e_range_check_error)
  733. else
  734. Message(parser_w_range_check_error);
  735. end;
  736. error := true;
  737. end;
  738. end
  739. else if (l<lv) or (l>hv) then
  740. begin
  741. if not explicit then
  742. begin
  743. if ((def.deftype=enumdef) and
  744. { delphi allows range check errors in
  745. enumeration type casts FK }
  746. not(m_delphi in aktmodeswitches)) or
  747. (cs_check_range in aktlocalswitches) then
  748. Message(parser_e_range_check_error)
  749. else
  750. Message(parser_w_range_check_error);
  751. end;
  752. error := true;
  753. end;
  754. end;
  755. if error then
  756. begin
  757. { Fix the value to fit in the allocated space for this type of variable }
  758. case def.size of
  759. 1: l := l and $ff;
  760. 2: l := l and $ffff;
  761. { work around sign extension bug (to be fixed) (JM) }
  762. 4: l := l and (int64($fffffff) shl 4 + $f);
  763. end;
  764. { do sign extension if necessary (JM) }
  765. if is_signed(def) then
  766. begin
  767. case def.size of
  768. 1: l := shortint(l);
  769. 2: l := smallint(l);
  770. 4: l := longint(l);
  771. end;
  772. end;
  773. end;
  774. end;
  775. { return the range from def in l and h }
  776. procedure getrange(def : tdef;var l : TConstExprInt;var h : TConstExprInt);
  777. begin
  778. case def.deftype of
  779. orddef :
  780. begin
  781. l:=torddef(def).low;
  782. h:=torddef(def).high;
  783. end;
  784. enumdef :
  785. begin
  786. l:=tenumdef(def).min;
  787. h:=tenumdef(def).max;
  788. end;
  789. arraydef :
  790. begin
  791. l:=tarraydef(def).lowrange;
  792. h:=tarraydef(def).highrange;
  793. end;
  794. else
  795. internalerror(987);
  796. end;
  797. end;
  798. function mmx_type(p : tdef) : tmmxtype;
  799. begin
  800. mmx_type:=mmxno;
  801. if is_mmx_able_array(p) then
  802. begin
  803. if tarraydef(p).elementtype.def.deftype=floatdef then
  804. case tfloatdef(tarraydef(p).elementtype.def).typ of
  805. s32real:
  806. mmx_type:=mmxsingle;
  807. end
  808. else
  809. case torddef(tarraydef(p).elementtype.def).typ of
  810. u8bit:
  811. mmx_type:=mmxu8bit;
  812. s8bit:
  813. mmx_type:=mmxs8bit;
  814. u16bit:
  815. mmx_type:=mmxu16bit;
  816. s16bit:
  817. mmx_type:=mmxs16bit;
  818. u32bit:
  819. mmx_type:=mmxu32bit;
  820. s32bit:
  821. mmx_type:=mmxs32bit;
  822. end;
  823. end;
  824. end;
  825. function is_mmx_able_array(p : tdef) : boolean;
  826. begin
  827. {$ifdef SUPPORT_MMX}
  828. if (cs_mmx_saturation in aktlocalswitches) then
  829. begin
  830. is_mmx_able_array:=(p.deftype=arraydef) and
  831. not(is_special_array(p)) and
  832. (
  833. (
  834. (tarraydef(p).elementtype.def.deftype=orddef) and
  835. (
  836. (
  837. (tarraydef(p).lowrange=0) and
  838. (tarraydef(p).highrange=1) and
  839. (torddef(tarraydef(p).elementtype.def).typ in [u32bit,s32bit])
  840. )
  841. or
  842. (
  843. (tarraydef(p).lowrange=0) and
  844. (tarraydef(p).highrange=3) and
  845. (torddef(tarraydef(p).elementtype.def).typ in [u16bit,s16bit])
  846. )
  847. )
  848. )
  849. or
  850. (
  851. (
  852. (tarraydef(p).elementtype.def.deftype=floatdef) and
  853. (
  854. (tarraydef(p).lowrange=0) and
  855. (tarraydef(p).highrange=1) and
  856. (tfloatdef(tarraydef(p).elementtype.def).typ=s32real)
  857. )
  858. )
  859. )
  860. );
  861. end
  862. else
  863. begin
  864. is_mmx_able_array:=(p.deftype=arraydef) and
  865. (
  866. (
  867. (tarraydef(p).elementtype.def.deftype=orddef) and
  868. (
  869. (
  870. (tarraydef(p).lowrange=0) and
  871. (tarraydef(p).highrange=1) and
  872. (torddef(tarraydef(p).elementtype.def).typ in [u32bit,s32bit])
  873. )
  874. or
  875. (
  876. (tarraydef(p).lowrange=0) and
  877. (tarraydef(p).highrange=3) and
  878. (torddef(tarraydef(p).elementtype.def).typ in [u16bit,s16bit])
  879. )
  880. or
  881. (
  882. (tarraydef(p).lowrange=0) and
  883. (tarraydef(p).highrange=7) and
  884. (torddef(tarraydef(p).elementtype.def).typ in [u8bit,s8bit])
  885. )
  886. )
  887. )
  888. or
  889. (
  890. (tarraydef(p).elementtype.def.deftype=floatdef) and
  891. (
  892. (tarraydef(p).lowrange=0) and
  893. (tarraydef(p).highrange=1) and
  894. (tfloatdef(tarraydef(p).elementtype.def).typ=s32real)
  895. )
  896. )
  897. );
  898. end;
  899. {$else SUPPORT_MMX}
  900. is_mmx_able_array:=false;
  901. {$endif SUPPORT_MMX}
  902. end;
  903. function is_equal(def1,def2 : tdef) : boolean;
  904. var
  905. b : boolean;
  906. hd : tdef;
  907. begin
  908. { both types must exists }
  909. if not (assigned(def1) and assigned(def2)) then
  910. begin
  911. is_equal:=false;
  912. exit;
  913. end;
  914. { be sure, that if there is a stringdef, that this is def1 }
  915. if def2.deftype=stringdef then
  916. begin
  917. hd:=def1;
  918. def1:=def2;
  919. def2:=hd;
  920. end;
  921. b:=false;
  922. { both point to the same definition ? }
  923. if def1=def2 then
  924. b:=true
  925. else
  926. { pointer with an equal definition are equal }
  927. if (def1.deftype=pointerdef) and (def2.deftype=pointerdef) then
  928. begin
  929. { check if both are farpointer }
  930. if (tpointerdef(def1).is_far=tpointerdef(def2).is_far) then
  931. begin
  932. { here a problem detected in tabsolutesym }
  933. { the types can be forward type !! }
  934. if assigned(def1.typesym) and (tpointerdef(def1).pointertype.def.deftype=forwarddef) then
  935. b:=(def1.typesym=def2.typesym)
  936. else
  937. b:=tpointerdef(def1).pointertype.def=tpointerdef(def2).pointertype.def;
  938. end
  939. else
  940. b:=false;
  941. end
  942. else
  943. { ordinals are equal only when the ordinal type is equal }
  944. if (def1.deftype=orddef) and (def2.deftype=orddef) then
  945. begin
  946. case torddef(def1).typ of
  947. u8bit,u16bit,u32bit,u64bit,
  948. s8bit,s16bit,s32bit,s64bit:
  949. b:=((torddef(def1).typ=torddef(def2).typ) and
  950. (torddef(def1).low=torddef(def2).low) and
  951. (torddef(def1).high=torddef(def2).high));
  952. uvoid,uchar,uwidechar,
  953. bool8bit,bool16bit,bool32bit:
  954. b:=(torddef(def1).typ=torddef(def2).typ);
  955. else
  956. internalerror(200210061);
  957. end;
  958. end
  959. else
  960. if (def1.deftype=floatdef) and (def2.deftype=floatdef) then
  961. b:=tfloatdef(def1).typ=tfloatdef(def2).typ
  962. else
  963. { strings with the same length are equal }
  964. if (def1.deftype=stringdef) and (def2.deftype=stringdef) and
  965. (tstringdef(def1).string_typ=tstringdef(def2).string_typ) then
  966. begin
  967. b:=not(is_shortstring(def1)) or
  968. (tstringdef(def1).len=tstringdef(def2).len);
  969. end
  970. else
  971. if (def1.deftype=formaldef) and (def2.deftype=formaldef) then
  972. b:=true
  973. { file types with the same file element type are equal }
  974. { this is a problem for assign !! }
  975. { changed to allow if one is untyped }
  976. { all typed files are equal to the special }
  977. { typed file that has voiddef as elemnt type }
  978. { but must NOT match for text file !!! }
  979. else
  980. if (def1.deftype=filedef) and (def2.deftype=filedef) then
  981. b:=(tfiledef(def1).filetyp=tfiledef(def2).filetyp) and
  982. ((
  983. ((tfiledef(def1).typedfiletype.def=nil) and
  984. (tfiledef(def2).typedfiletype.def=nil)) or
  985. (
  986. (tfiledef(def1).typedfiletype.def<>nil) and
  987. (tfiledef(def2).typedfiletype.def<>nil) and
  988. is_equal(tfiledef(def1).typedfiletype.def,tfiledef(def2).typedfiletype.def)
  989. ) or
  990. ( (tfiledef(def1).typedfiletype.def=tdef(voidtype.def)) or
  991. (tfiledef(def2).typedfiletype.def=tdef(voidtype.def))
  992. )))
  993. { sets with the same element base type are equal }
  994. else
  995. if (def1.deftype=setdef) and (def2.deftype=setdef) then
  996. begin
  997. if assigned(tsetdef(def1).elementtype.def) and
  998. assigned(tsetdef(def2).elementtype.def) then
  999. b:=is_subequal(tsetdef(def1).elementtype.def,tsetdef(def2).elementtype.def)
  1000. else
  1001. { empty set is compatible with everything }
  1002. b:=true;
  1003. end
  1004. else
  1005. if (def1.deftype=procvardef) and (def2.deftype=procvardef) then
  1006. begin
  1007. { poassembler isn't important for compatibility }
  1008. { if a method is assigned to a methodpointer }
  1009. { is checked before }
  1010. b:=(tprocvardef(def1).proctypeoption=tprocvardef(def2).proctypeoption) and
  1011. (tprocvardef(def1).proccalloption=tprocvardef(def2).proccalloption) and
  1012. ((tprocvardef(def1).procoptions * po_compatibility_options)=
  1013. (tprocvardef(def2).procoptions * po_compatibility_options)) and
  1014. is_equal(tprocvardef(def1).rettype.def,tprocvardef(def2).rettype.def) and
  1015. equal_paras(tprocvardef(def1).para,tprocvardef(def2).para,cp_all,false);
  1016. end
  1017. else
  1018. if (def1.deftype=arraydef) and (def2.deftype=arraydef) then
  1019. begin
  1020. if is_dynamic_array(def1) and is_dynamic_array(def2) then
  1021. b:=is_equal(tarraydef(def1).elementtype.def,tarraydef(def2).elementtype.def)
  1022. else
  1023. if is_array_of_const(def1) or is_array_of_const(def2) then
  1024. begin
  1025. b:=(is_array_of_const(def1) and is_array_of_const(def2)) or
  1026. (is_array_of_const(def1) and is_array_constructor(def2)) or
  1027. (is_array_of_const(def2) and is_array_constructor(def1));
  1028. end
  1029. else
  1030. if (is_dynamic_array(def1) or is_dynamic_array(def2)) then
  1031. begin
  1032. b := is_dynamic_array(def1) and is_dynamic_array(def2) and
  1033. is_equal(tarraydef(def1).elementtype.def,tarraydef(def2).elementtype.def);
  1034. end
  1035. else
  1036. if is_open_array(def1) or is_open_array(def2) then
  1037. begin
  1038. b:=is_equal(tarraydef(def1).elementtype.def,tarraydef(def2).elementtype.def);
  1039. end
  1040. else
  1041. begin
  1042. b:=not(m_tp7 in aktmodeswitches) and
  1043. not(m_delphi in aktmodeswitches) and
  1044. (tarraydef(def1).lowrange=tarraydef(def2).lowrange) and
  1045. (tarraydef(def1).highrange=tarraydef(def2).highrange) and
  1046. is_equal(tarraydef(def1).elementtype.def,tarraydef(def2).elementtype.def) and
  1047. is_equal(tarraydef(def1).rangetype.def,tarraydef(def2).rangetype.def);
  1048. end;
  1049. end
  1050. else
  1051. if (def1.deftype=classrefdef) and (def2.deftype=classrefdef) then
  1052. begin
  1053. { similar to pointerdef: }
  1054. if assigned(def1.typesym) and (tclassrefdef(def1).pointertype.def.deftype=forwarddef) then
  1055. b:=(def1.typesym=def2.typesym)
  1056. else
  1057. b:=is_equal(tclassrefdef(def1).pointertype.def,tclassrefdef(def2).pointertype.def);
  1058. end;
  1059. is_equal:=b;
  1060. end;
  1061. function is_subequal(def1, def2: tdef): boolean;
  1062. var
  1063. basedef1,basedef2 : tenumdef;
  1064. Begin
  1065. is_subequal := false;
  1066. if assigned(def1) and assigned(def2) then
  1067. Begin
  1068. if (def1.deftype = orddef) and (def2.deftype = orddef) then
  1069. Begin
  1070. { see p.47 of Turbo Pascal 7.01 manual for the separation of types }
  1071. { range checking for case statements is done with testrange }
  1072. case torddef(def1).typ of
  1073. u8bit,u16bit,u32bit,
  1074. s8bit,s16bit,s32bit,s64bit,u64bit :
  1075. is_subequal:=(torddef(def2).typ in [s64bit,u64bit,s32bit,u32bit,u8bit,s8bit,s16bit,u16bit]);
  1076. bool8bit,bool16bit,bool32bit :
  1077. is_subequal:=(torddef(def2).typ in [bool8bit,bool16bit,bool32bit]);
  1078. uchar :
  1079. is_subequal:=(torddef(def2).typ=uchar);
  1080. uwidechar :
  1081. is_subequal:=(torddef(def2).typ=uwidechar);
  1082. end;
  1083. end
  1084. else
  1085. Begin
  1086. { I assume that both enumerations are equal when the first }
  1087. { pointers are equal. }
  1088. { I changed this to assume that the enums are equal }
  1089. { if the basedefs are equal (FK) }
  1090. if (def1.deftype=enumdef) and (def2.deftype=enumdef) then
  1091. Begin
  1092. { get both basedefs }
  1093. basedef1:=tenumdef(def1);
  1094. while assigned(basedef1.basedef) do
  1095. basedef1:=basedef1.basedef;
  1096. basedef2:=tenumdef(def2);
  1097. while assigned(basedef2.basedef) do
  1098. basedef2:=basedef2.basedef;
  1099. is_subequal:=basedef1=basedef2;
  1100. {
  1101. if tenumdef(def1).firstenum = tenumdef(def2).firstenum then
  1102. is_subequal := TRUE;
  1103. }
  1104. end;
  1105. end;
  1106. end; { endif assigned ... }
  1107. end;
  1108. (* function assignment_overloaded(from_def,to_def : tdef) : tprocdef;
  1109. var
  1110. passprocs : pprocdeflist;
  1111. convtyp : tconverttype;
  1112. begin
  1113. assignment_overloaded:=nil;
  1114. if not assigned(overloaded_operators[_ASSIGNMENT]) then
  1115. exit;
  1116. { look for an exact match first }
  1117. passprocs:=overloaded_operators[_ASSIGNMENT].defs;
  1118. while assigned(passprocs) do
  1119. begin
  1120. if is_equal(passprocs^.def.rettype.def,to_def) and
  1121. (TParaItem(passprocs^.def.Para.first).paratype.def=from_def) then
  1122. begin
  1123. assignment_overloaded:=passprocs^.def;
  1124. exit;
  1125. end;
  1126. passprocs:=passprocs^.next;
  1127. end;
  1128. { .... then look for an equal match }
  1129. passprocs:=overloaded_operators[_ASSIGNMENT].defs;
  1130. while assigned(passprocs) do
  1131. begin
  1132. if is_equal(passprocs^.def.rettype.def,to_def) and
  1133. is_equal(TParaItem(passprocs^.def.Para.first).paratype.def,from_def) then
  1134. begin
  1135. assignment_overloaded:=passprocs^.def;
  1136. exit;
  1137. end;
  1138. passprocs:=passprocs^.next;
  1139. end;
  1140. { .... then for convert level 1 }
  1141. passprocs:=overloaded_operators[_ASSIGNMENT].defs;
  1142. while assigned(passprocs) do
  1143. begin
  1144. if is_equal(passprocs^.def.rettype.def,to_def) and
  1145. (isconvertable(from_def,TParaItem(passprocs^.def.Para.first).paratype.def,convtyp,ordconstn,false)=1) then
  1146. begin
  1147. assignment_overloaded:=passprocs^.def;
  1148. exit;
  1149. end;
  1150. passprocs:=passprocs^.next;
  1151. end;
  1152. end;
  1153. *)
  1154. { this is an internal routine to take care of recursivity }
  1155. function internal_assignment_overloaded(from_def,to_def : tdef;
  1156. var overload_procs : pprocdeflist) : tprocdef;
  1157. var
  1158. p :pprocdeflist;
  1159. _result : tprocdef;
  1160. begin
  1161. internal_assignment_overloaded:=nil;
  1162. p := nil;
  1163. if not assigned(overloaded_operators[_ASSIGNMENT]) then
  1164. exit;
  1165. { look for an exact match first, from start of list }
  1166. _result:=overloaded_operators[_ASSIGNMENT].
  1167. search_procdef_byretdef_by1paradef(to_def,from_def,dm_exact,
  1168. p);
  1169. if assigned(_result) then
  1170. begin
  1171. internal_assignment_overloaded := _result;
  1172. exit;
  1173. end;
  1174. { .... then look for an equal match, from start of list }
  1175. _result:=overloaded_operators[_ASSIGNMENT].
  1176. search_procdef_byretdef_by1paradef(to_def,from_def,dm_equal,
  1177. p);
  1178. if assigned(_result) then
  1179. begin
  1180. internal_assignment_overloaded := _result;
  1181. exit;
  1182. end;
  1183. { .... then for convert level 1, continue from where we were at }
  1184. internal_assignment_overloaded:=overloaded_operators[_ASSIGNMENT].
  1185. search_procdef_byretdef_by1paradef(to_def,from_def,dm_convertl1,
  1186. overload_procs);
  1187. end;
  1188. function assignment_overloaded(from_def,to_def : tdef) : tprocdef;
  1189. var
  1190. p : pprocdeflist;
  1191. begin
  1192. p:=nil;
  1193. assignment_overloaded:=nil;
  1194. assignment_overloaded:=internal_assignment_overloaded(
  1195. from_def, to_def, p);
  1196. end;
  1197. { Returns:
  1198. 0 - Not convertable
  1199. 1 - Convertable
  1200. 2 - Convertable, but not first choice
  1201. }
  1202. function isconvertable(def_from,def_to : tdef;
  1203. var doconv : tconverttype;
  1204. fromtreetype : tnodetype;
  1205. explicit : boolean) : byte;
  1206. var
  1207. p: pprocdeflist;
  1208. begin
  1209. p:=nil;
  1210. isconvertable:=overloaded_assignment_isconvertable(def_from,def_to,
  1211. doconv, fromtreetype, explicit,p);
  1212. end;
  1213. function overloaded_assignment_isconvertable(def_from,def_to : tdef;
  1214. var doconv : tconverttype;
  1215. fromtreetype : tnodetype;
  1216. explicit : boolean; var overload_procs : pprocdeflist) : byte;
  1217. { Tbasetype:
  1218. uvoid,
  1219. u8bit,u16bit,u32bit,u64bit,
  1220. s8bit,s16bit,s32bit,s64bit,
  1221. bool8bit,bool16bit,bool32bit,
  1222. uchar,uwidechar }
  1223. type
  1224. tbasedef=(bvoid,bchar,bint,bbool);
  1225. const
  1226. basedeftbl:array[tbasetype] of tbasedef =
  1227. (bvoid,
  1228. bint,bint,bint,bint,
  1229. bint,bint,bint,bint,
  1230. bbool,bbool,bbool,
  1231. bchar,bchar);
  1232. basedefconverts : array[tbasedef,tbasedef] of tconverttype =
  1233. ((tc_not_possible,tc_not_possible,tc_not_possible,tc_not_possible),
  1234. (tc_not_possible,tc_char_2_char,tc_not_possible,tc_not_possible),
  1235. (tc_not_possible,tc_not_possible,tc_int_2_int,tc_int_2_bool),
  1236. (tc_not_possible,tc_not_possible,tc_bool_2_int,tc_bool_2_bool));
  1237. var
  1238. b : byte;
  1239. hd1,hd2 : tdef;
  1240. hct : tconverttype;
  1241. hd3 : tobjectdef;
  1242. begin
  1243. { safety check }
  1244. if not(assigned(def_from) and assigned(def_to)) then
  1245. begin
  1246. overloaded_assignment_isconvertable :=0;
  1247. exit;
  1248. end;
  1249. { tp7 procvar def support, in tp7 a procvar is always called, if the
  1250. procvar is passed explicit a addrn would be there }
  1251. if (m_tp_procvar in aktmodeswitches) and
  1252. (def_from.deftype=procvardef) and
  1253. (fromtreetype=loadn) and
  1254. { only if the procvar doesn't require any paramters }
  1255. (tprocvardef(def_from).minparacount = 0) then
  1256. begin
  1257. def_from:=tprocvardef(def_from).rettype.def;
  1258. end;
  1259. { we walk the wanted (def_to) types and check then the def_from
  1260. types if there is a conversion possible }
  1261. b:=0;
  1262. case def_to.deftype of
  1263. orddef :
  1264. begin
  1265. case def_from.deftype of
  1266. orddef :
  1267. begin
  1268. doconv:=basedefconverts[basedeftbl[torddef(def_from).typ],basedeftbl[torddef(def_to).typ]];
  1269. b:=1;
  1270. if (doconv=tc_not_possible) or
  1271. ((doconv=tc_int_2_bool) and
  1272. (not explicit) and
  1273. (not is_boolean(def_from))) or
  1274. ((doconv=tc_bool_2_int) and
  1275. (not explicit) and
  1276. (not is_boolean(def_to))) then
  1277. b:=0
  1278. else
  1279. { "punish" bad type conversions :) (JM) }
  1280. if not is_in_limit(def_from,def_to) and
  1281. (def_from.size > def_to.size) then
  1282. b := 2;
  1283. end;
  1284. enumdef :
  1285. begin
  1286. { needed for char(enum) }
  1287. if explicit then
  1288. begin
  1289. doconv:=tc_int_2_int;
  1290. b:=1;
  1291. end;
  1292. end;
  1293. end;
  1294. end;
  1295. stringdef :
  1296. begin
  1297. case def_from.deftype of
  1298. stringdef :
  1299. begin
  1300. doconv:=tc_string_2_string;
  1301. b:=1;
  1302. end;
  1303. orddef :
  1304. begin
  1305. { char to string}
  1306. if is_char(def_from) or
  1307. is_widechar(def_from) then
  1308. begin
  1309. doconv:=tc_char_2_string;
  1310. b:=1;
  1311. end;
  1312. end;
  1313. arraydef :
  1314. begin
  1315. { array of char to string, the length check is done by the firstpass of this node }
  1316. if is_chararray(def_from) or
  1317. (is_equal(tarraydef(def_from).elementtype.def,cchartype.def) and
  1318. is_open_array(def_from)) then
  1319. begin
  1320. doconv:=tc_chararray_2_string;
  1321. if is_open_array(def_from) or
  1322. (is_shortstring(def_to) and
  1323. (def_from.size <= 255)) or
  1324. (is_ansistring(def_to) and
  1325. (def_from.size > 255)) then
  1326. b:=1
  1327. else
  1328. b:=2;
  1329. end;
  1330. end;
  1331. pointerdef :
  1332. begin
  1333. { pchar can be assigned to short/ansistrings,
  1334. but not in tp7 compatible mode }
  1335. if not(m_tp7 in aktmodeswitches) then
  1336. begin
  1337. if is_pchar(def_from) then
  1338. begin
  1339. doconv:=tc_pchar_2_string;
  1340. { trefer ansistrings because pchars can overflow shortstrings, }
  1341. { but only if ansistrings are the default (JM) }
  1342. if (is_shortstring(def_to) and
  1343. not(cs_ansistrings in aktlocalswitches)) or
  1344. (is_ansistring(def_to) and
  1345. (cs_ansistrings in aktlocalswitches)) then
  1346. b:=1
  1347. else
  1348. b:=2;
  1349. end
  1350. else if is_pwidechar(def_from) then
  1351. begin
  1352. doconv:=tc_pwchar_2_string;
  1353. { trefer ansistrings because pchars can overflow shortstrings, }
  1354. { but only if ansistrings are the default (JM) }
  1355. if is_widestring(def_to) then
  1356. b:=1
  1357. else
  1358. b:=2;
  1359. end;
  1360. end;
  1361. end;
  1362. end;
  1363. end;
  1364. floatdef :
  1365. begin
  1366. case def_from.deftype of
  1367. orddef :
  1368. begin { ordinal to real }
  1369. if is_integer(def_from) then
  1370. begin
  1371. doconv:=tc_int_2_real;
  1372. b:=1;
  1373. end;
  1374. end;
  1375. floatdef :
  1376. begin { 2 float types ? }
  1377. if tfloatdef(def_from).typ=tfloatdef(def_to).typ then
  1378. doconv:=tc_equal
  1379. else
  1380. doconv:=tc_real_2_real;
  1381. b:=1;
  1382. end;
  1383. end;
  1384. end;
  1385. enumdef :
  1386. begin
  1387. if (def_from.deftype=enumdef) then
  1388. begin
  1389. if explicit then
  1390. begin
  1391. b:=1;
  1392. doconv:=tc_int_2_int;
  1393. end
  1394. else
  1395. begin
  1396. hd1:=def_from;
  1397. while assigned(tenumdef(hd1).basedef) do
  1398. hd1:=tenumdef(hd1).basedef;
  1399. hd2:=def_to;
  1400. while assigned(tenumdef(hd2).basedef) do
  1401. hd2:=tenumdef(hd2).basedef;
  1402. if (hd1=hd2) then
  1403. begin
  1404. b:=1;
  1405. { because of packenum they can have different sizes! (JM) }
  1406. doconv:=tc_int_2_int;
  1407. end;
  1408. end;
  1409. end;
  1410. end;
  1411. arraydef :
  1412. begin
  1413. { open array is also compatible with a single element of its base type }
  1414. if is_open_array(def_to) and
  1415. is_equal(tarraydef(def_to).elementtype.def,def_from) then
  1416. begin
  1417. doconv:=tc_equal;
  1418. b:=1;
  1419. end
  1420. else if is_dynamic_array(def_to) and
  1421. { nil is compatible with dyn. arrays }
  1422. (fromtreetype=niln) then
  1423. begin
  1424. doconv:=tc_equal;
  1425. b:=1;
  1426. end
  1427. else
  1428. begin
  1429. case def_from.deftype of
  1430. arraydef :
  1431. begin
  1432. { array constructor -> open array }
  1433. if is_open_array(def_to) and
  1434. is_array_constructor(def_from) then
  1435. begin
  1436. if is_void(tarraydef(def_from).elementtype.def) or
  1437. is_equal(tarraydef(def_to).elementtype.def,tarraydef(def_from).elementtype.def) then
  1438. begin
  1439. doconv:=tc_equal;
  1440. b:=1;
  1441. end
  1442. else
  1443. if isconvertable(tarraydef(def_from).elementtype.def,
  1444. tarraydef(def_to).elementtype.def,hct,arrayconstructorn,false)<>0 then
  1445. begin
  1446. doconv:=hct;
  1447. b:=2;
  1448. end;
  1449. end
  1450. else
  1451. { dynamic array -> open array }
  1452. if is_dynamic_array(def_from) and
  1453. is_open_array(def_to) and
  1454. is_equal(tarraydef(def_to).elementtype.def,tarraydef(def_from).elementtype.def) then
  1455. begin
  1456. doconv := tc_dynarray_2_openarray;
  1457. b := 2;
  1458. end
  1459. else
  1460. { array of tvarrec -> array of const }
  1461. if is_array_of_const(def_to) and
  1462. is_equal(tarraydef(def_to).elementtype.def,tarraydef(def_from).elementtype.def) then
  1463. begin
  1464. doconv:=tc_equal;
  1465. b:=1;
  1466. end;
  1467. end;
  1468. pointerdef :
  1469. begin
  1470. if is_zero_based_array(def_to) and
  1471. is_equal(tpointerdef(def_from).pointertype.def,tarraydef(def_to).elementtype.def) then
  1472. begin
  1473. doconv:=tc_pointer_2_array;
  1474. b:=1;
  1475. end;
  1476. end;
  1477. stringdef :
  1478. begin
  1479. { string to char array }
  1480. if (not is_special_array(def_to)) and
  1481. is_char(tarraydef(def_to).elementtype.def) then
  1482. begin
  1483. doconv:=tc_string_2_chararray;
  1484. b:=1;
  1485. end;
  1486. end;
  1487. orddef:
  1488. begin
  1489. if is_chararray(def_to) and
  1490. is_char(def_from) then
  1491. begin
  1492. doconv:=tc_char_2_chararray;
  1493. b:=2;
  1494. end;
  1495. end;
  1496. recorddef :
  1497. begin
  1498. { tvarrec -> array of const }
  1499. if is_array_of_const(def_to) and
  1500. is_equal(def_from,tarraydef(def_to).elementtype.def) then
  1501. begin
  1502. doconv:=tc_equal;
  1503. b:=1;
  1504. end;
  1505. end;
  1506. end;
  1507. end;
  1508. end;
  1509. pointerdef :
  1510. begin
  1511. case def_from.deftype of
  1512. stringdef :
  1513. begin
  1514. { string constant (which can be part of array constructor)
  1515. to zero terminated string constant }
  1516. if (fromtreetype in [arrayconstructorn,stringconstn]) and
  1517. is_pchar(def_to) or is_pwidechar(def_to) then
  1518. begin
  1519. doconv:=tc_cstring_2_pchar;
  1520. b:=1;
  1521. end;
  1522. end;
  1523. orddef :
  1524. begin
  1525. { char constant to zero terminated string constant }
  1526. if (fromtreetype=ordconstn) then
  1527. begin
  1528. if is_equal(def_from,cchartype.def) and
  1529. is_pchar(def_to) then
  1530. begin
  1531. doconv:=tc_cchar_2_pchar;
  1532. b:=1;
  1533. end
  1534. else
  1535. if is_integer(def_from) then
  1536. begin
  1537. doconv:=tc_cord_2_pointer;
  1538. b:=1;
  1539. end;
  1540. end;
  1541. end;
  1542. arraydef :
  1543. begin
  1544. { chararray to pointer }
  1545. if is_zero_based_array(def_from) and
  1546. is_equal(tarraydef(def_from).elementtype.def,tpointerdef(def_to).pointertype.def) then
  1547. begin
  1548. doconv:=tc_array_2_pointer;
  1549. b:=1;
  1550. end;
  1551. end;
  1552. pointerdef :
  1553. begin
  1554. { child class pointer can be assigned to anchestor pointers }
  1555. if (
  1556. (tpointerdef(def_from).pointertype.def.deftype=objectdef) and
  1557. (tpointerdef(def_to).pointertype.def.deftype=objectdef) and
  1558. tobjectdef(tpointerdef(def_from).pointertype.def).is_related(
  1559. tobjectdef(tpointerdef(def_to).pointertype.def))
  1560. ) or
  1561. { all pointers can be assigned to void-pointer }
  1562. is_equal(tpointerdef(def_to).pointertype.def,voidtype.def) or
  1563. { in my opnion, is this not clean pascal }
  1564. { well, but it's handy to use, it isn't ? (FK) }
  1565. is_equal(tpointerdef(def_from).pointertype.def,voidtype.def) then
  1566. begin
  1567. { but don't allow conversion between farpointer-pointer }
  1568. if (tpointerdef(def_to).is_far=tpointerdef(def_from).is_far) then
  1569. begin
  1570. doconv:=tc_equal;
  1571. b:=1;
  1572. end;
  1573. end;
  1574. end;
  1575. procvardef :
  1576. begin
  1577. { procedure variable can be assigned to an void pointer }
  1578. { Not anymore. Use the @ operator now.}
  1579. if not(m_tp_procvar in aktmodeswitches) and
  1580. (tpointerdef(def_to).pointertype.def.deftype=orddef) and
  1581. (torddef(tpointerdef(def_to).pointertype.def).typ=uvoid) then
  1582. begin
  1583. doconv:=tc_equal;
  1584. b:=1;
  1585. end;
  1586. end;
  1587. classrefdef,
  1588. objectdef :
  1589. begin
  1590. { class types and class reference type
  1591. can be assigned to void pointers }
  1592. if (
  1593. is_class_or_interface(def_from) or
  1594. (def_from.deftype=classrefdef)
  1595. ) and
  1596. (tpointerdef(def_to).pointertype.def.deftype=orddef) and
  1597. (torddef(tpointerdef(def_to).pointertype.def).typ=uvoid) then
  1598. begin
  1599. doconv:=tc_equal;
  1600. b:=1;
  1601. end;
  1602. end;
  1603. end;
  1604. end;
  1605. setdef :
  1606. begin
  1607. { automatic arrayconstructor -> set conversion }
  1608. if is_array_constructor(def_from) then
  1609. begin
  1610. doconv:=tc_arrayconstructor_2_set;
  1611. b:=1;
  1612. end;
  1613. end;
  1614. procvardef :
  1615. begin
  1616. { proc -> procvar }
  1617. if (def_from.deftype=procdef) and
  1618. (m_tp_procvar in aktmodeswitches) then
  1619. begin
  1620. doconv:=tc_proc_2_procvar;
  1621. if proc_to_procvar_equal(tprocdef(def_from),tprocvardef(def_to),false) then
  1622. b:=1;
  1623. end
  1624. { procvar -> procvar }
  1625. else
  1626. if (def_from.deftype=procvardef) and
  1627. (proc_to_procvar_equal(tprocvardef(def_from),tprocvardef(def_to),false)) then
  1628. begin
  1629. doconv:=tc_equal;
  1630. b := 2;
  1631. end
  1632. else
  1633. { for example delphi allows the assignement from pointers }
  1634. { to procedure variables }
  1635. if (m_pointer_2_procedure in aktmodeswitches) and
  1636. (def_from.deftype=pointerdef) and
  1637. (tpointerdef(def_from).pointertype.def.deftype=orddef) and
  1638. (torddef(tpointerdef(def_from).pointertype.def).typ=uvoid) then
  1639. begin
  1640. doconv:=tc_equal;
  1641. b:=1;
  1642. end
  1643. else
  1644. { nil is compatible with procvars }
  1645. if (fromtreetype=niln) then
  1646. begin
  1647. doconv:=tc_equal;
  1648. b:=1;
  1649. end;
  1650. end;
  1651. objectdef :
  1652. begin
  1653. { object pascal objects }
  1654. if (def_from.deftype=objectdef) and
  1655. tobjectdef(def_from).is_related(tobjectdef(def_to)) then
  1656. begin
  1657. doconv:=tc_equal;
  1658. b:=1;
  1659. end
  1660. else
  1661. { Class/interface specific }
  1662. if is_class_or_interface(def_to) then
  1663. begin
  1664. { void pointer also for delphi mode }
  1665. if (m_delphi in aktmodeswitches) and
  1666. is_voidpointer(def_from) then
  1667. begin
  1668. doconv:=tc_equal;
  1669. b:=1;
  1670. end
  1671. else
  1672. { nil is compatible with class instances and interfaces }
  1673. if (fromtreetype=niln) then
  1674. begin
  1675. doconv:=tc_equal;
  1676. b:=1;
  1677. end
  1678. { classes can be assigned to interfaces }
  1679. else if is_interface(def_to) and
  1680. is_class(def_from) and
  1681. assigned(tobjectdef(def_from).implementedinterfaces) then
  1682. begin
  1683. { we've to search in parent classes as well }
  1684. hd3:=tobjectdef(def_from);
  1685. while assigned(hd3) do
  1686. begin
  1687. if hd3.implementedinterfaces.searchintf(def_to)<>-1 then
  1688. begin
  1689. doconv:=tc_class_2_intf;
  1690. b:=1;
  1691. break;
  1692. end;
  1693. hd3:=hd3.childof;
  1694. end;
  1695. end
  1696. { Interface 2 GUID handling }
  1697. else if (def_to=tdef(rec_tguid)) and
  1698. (fromtreetype=typen) and
  1699. is_interface(def_from) and
  1700. tobjectdef(def_from).isiidguidvalid then
  1701. begin
  1702. b:=1;
  1703. doconv:=tc_equal;
  1704. end;
  1705. end;
  1706. end;
  1707. classrefdef :
  1708. begin
  1709. { class reference types }
  1710. if (def_from.deftype=classrefdef) then
  1711. begin
  1712. doconv:=tc_equal;
  1713. if tobjectdef(tclassrefdef(def_from).pointertype.def).is_related(
  1714. tobjectdef(tclassrefdef(def_to).pointertype.def)) then
  1715. b:=1;
  1716. end
  1717. else
  1718. { nil is compatible with class references }
  1719. if (fromtreetype=niln) then
  1720. begin
  1721. doconv:=tc_equal;
  1722. b:=1;
  1723. end;
  1724. end;
  1725. filedef :
  1726. begin
  1727. { typed files are all equal to the abstract file type
  1728. name TYPEDFILE in system.pp in is_equal in types.pas
  1729. the problem is that it sholud be also compatible to FILE
  1730. but this would leed to a problem for ASSIGN RESET and REWRITE
  1731. when trying to find the good overloaded function !!
  1732. so all file function are doubled in system.pp
  1733. this is not very beautiful !!}
  1734. if (def_from.deftype=filedef) and
  1735. (
  1736. (
  1737. (tfiledef(def_from).filetyp = ft_typed) and
  1738. (tfiledef(def_to).filetyp = ft_typed) and
  1739. (
  1740. (tfiledef(def_from).typedfiletype.def = tdef(voidtype.def)) or
  1741. (tfiledef(def_to).typedfiletype.def = tdef(voidtype.def))
  1742. )
  1743. ) or
  1744. (
  1745. (
  1746. (tfiledef(def_from).filetyp = ft_untyped) and
  1747. (tfiledef(def_to).filetyp = ft_typed)
  1748. ) or
  1749. (
  1750. (tfiledef(def_from).filetyp = ft_typed) and
  1751. (tfiledef(def_to).filetyp = ft_untyped)
  1752. )
  1753. )
  1754. ) then
  1755. begin
  1756. doconv:=tc_equal;
  1757. b:=1;
  1758. end
  1759. end;
  1760. recorddef :
  1761. begin
  1762. { interface -> guid }
  1763. if is_interface(def_from) and
  1764. (def_to=rec_tguid) then
  1765. begin
  1766. doconv:=tc_intf_2_guid;
  1767. b:=1;
  1768. end
  1769. else
  1770. begin
  1771. { assignment overwritten ?? }
  1772. if internal_assignment_overloaded(def_from,def_to,overload_procs)<>nil then
  1773. b:=2;
  1774. end;
  1775. end;
  1776. { a variant isn't compatible to nil (FK)
  1777. variantdef :
  1778. begin
  1779. if (fromtreetype=niln) then
  1780. begin
  1781. doconv:=tc_equal;
  1782. b:=1;
  1783. end;
  1784. end;
  1785. }
  1786. formaldef :
  1787. begin
  1788. { Just about everything can be converted to a formaldef...}
  1789. if not (def_from.deftype in [abstractdef,errordef]) then
  1790. b:=1
  1791. else
  1792. begin
  1793. { assignment overwritten ?? }
  1794. if internal_assignment_overloaded(def_from,def_to,overload_procs)<>nil then
  1795. b:=2;
  1796. end;
  1797. end;
  1798. end;
  1799. { if we didn't find an appropriate type conversion yet, we try the overloaded := operator }
  1800. { This is done for variants only yet, maybe we should do this for other types as well (FK) }
  1801. if (b=0) and ((def_from.deftype in [variantdef]) or (def_to.deftype in [variantdef])) then
  1802. begin
  1803. if internal_assignment_overloaded(def_from,def_to,overload_procs)<>nil then
  1804. b:=2;
  1805. end;
  1806. overloaded_assignment_isconvertable :=b;
  1807. end;
  1808. function CheckTypes(def1,def2 : tdef) : boolean;
  1809. var
  1810. s1,s2 : string;
  1811. begin
  1812. CheckTypes:=False;
  1813. if not is_equal(def1,def2) then
  1814. begin
  1815. { Crash prevention }
  1816. if (not assigned(def1)) or (not assigned(def2)) then
  1817. Message(type_e_mismatch)
  1818. else
  1819. begin
  1820. if not is_subequal(def1,def2) then
  1821. begin
  1822. s1:=def1.typename;
  1823. s2:=def2.typename;
  1824. Message2(type_e_not_equal_types,def1.typename,def2.typename);
  1825. end
  1826. else
  1827. CheckTypes := true;
  1828. end;
  1829. end
  1830. else
  1831. CheckTypes := True;
  1832. end;
  1833. end.
  1834. {
  1835. $Log$
  1836. Revision 1.23 2002-10-20 15:34:16 peter
  1837. * removed df_unique flag. It breaks code. For a good type=type <id>
  1838. a def copy is required
  1839. Revision 1.22 2002/10/10 16:07:57 florian
  1840. + several widestring/pwidechar related stuff added
  1841. Revision 1.21 2002/10/09 21:01:41 florian
  1842. * variants aren't compatible with nil
  1843. Revision 1.20 2002/10/07 09:49:42 florian
  1844. * overloaded :=-operator is now searched when looking for possible
  1845. variant type conversions
  1846. Revision 1.19 2002/10/06 21:02:17 peter
  1847. * fixed limit checking for qword
  1848. Revision 1.18 2002/10/06 15:08:59 peter
  1849. * only check for forwarddefs the definitions that really belong to
  1850. the current procsym
  1851. Revision 1.17 2002/10/06 12:25:04 florian
  1852. + proper support of type <id> = type <another id>;
  1853. Revision 1.16 2002/10/05 12:43:24 carl
  1854. * fixes for Delphi 6 compilation
  1855. (warning : Some features do not work under Delphi)
  1856. Revision 1.15 2002/10/05 00:50:01 peter
  1857. * check parameters from left to right in equal_paras, so default
  1858. parameters are checked at the end
  1859. Revision 1.14 2002/09/30 07:00:44 florian
  1860. * fixes to common code to get the alpha compiler compiled applied
  1861. Revision 1.13 2002/09/22 14:02:34 carl
  1862. * stack checking cannot be called before system unit is initialized
  1863. * MC68020 define
  1864. Revision 1.12 2002/09/16 14:11:12 peter
  1865. * add argument to equal_paras() to support default values or not
  1866. Revision 1.11 2002/09/15 17:54:46 peter
  1867. * allow default parameters in equal_paras
  1868. Revision 1.10 2002/09/08 11:10:17 carl
  1869. * bugfix 2109 (bad imho, but only way)
  1870. Revision 1.9 2002/09/07 15:25:02 peter
  1871. * old logs removed and tabs fixed
  1872. Revision 1.8 2002/09/07 09:16:55 carl
  1873. * fix my stupid copy and paste bug
  1874. Revision 1.7 2002/09/06 19:58:31 carl
  1875. * start bugfix 1996
  1876. * 64-bit typed constant now work correctly and fully (bugfix 2001)
  1877. Revision 1.6 2002/08/20 10:31:26 daniel
  1878. * Tcallnode.det_resulttype rewritten
  1879. Revision 1.5 2002/08/12 20:39:17 florian
  1880. * casting of classes to interface fixed when the interface was
  1881. implemented by a parent class
  1882. Revision 1.4 2002/08/12 14:17:56 florian
  1883. * nil is now recognized as being compatible with a dynamic array
  1884. Revision 1.3 2002/08/05 18:27:48 carl
  1885. + more more more documentation
  1886. + first version include/exclude (can't test though, not enough scratch for i386 :()...
  1887. Revision 1.2 2002/07/23 09:51:22 daniel
  1888. * Tried to make Tprocsym.defs protected. I didn't succeed but the cleanups
  1889. are worth comitting.
  1890. Revision 1.1 2002/07/20 11:57:53 florian
  1891. * types.pas renamed to defbase.pas because D6 contains a types
  1892. unit so this would conflicts if D6 programms are compiled
  1893. + Willamette/SSE2 instructions to assembler added
  1894. Revision 1.75 2002/07/11 14:41:32 florian
  1895. * start of the new generic parameter handling
  1896. Revision 1.74 2002/07/01 16:23:54 peter
  1897. * cg64 patch
  1898. * basics for currency
  1899. * asnode updates for class and interface (not finished)
  1900. Revision 1.73 2002/05/18 13:34:21 peter
  1901. * readded missing revisions
  1902. Revision 1.72 2002/05/16 19:46:47 carl
  1903. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1904. + try to fix temp allocation (still in ifdef)
  1905. + generic constructor calls
  1906. + start of tassembler / tmodulebase class cleanup
  1907. Revision 1.70 2002/05/12 16:53:16 peter
  1908. * moved entry and exitcode to ncgutil and cgobj
  1909. * foreach gets extra argument for passing local data to the
  1910. iterator function
  1911. * -CR checks also class typecasts at runtime by changing them
  1912. into as
  1913. * fixed compiler to cycle with the -CR option
  1914. * fixed stabs with elf writer, finally the global variables can
  1915. be watched
  1916. * removed a lot of routines from cga unit and replaced them by
  1917. calls to cgobj
  1918. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  1919. u32bit then the other is typecasted also to u32bit without giving
  1920. a rangecheck warning/error.
  1921. * fixed pascal calling method with reversing also the high tree in
  1922. the parast, detected by tcalcst3 test
  1923. Revision 1.69 2002/04/25 20:16:39 peter
  1924. * moved more routines from cga/n386util
  1925. Revision 1.68 2002/04/15 19:08:22 carl
  1926. + target_info.size_of_pointer -> pointer_size
  1927. + some cleanup of unused types/variables
  1928. Revision 1.67 2002/04/07 13:40:29 carl
  1929. + update documentation
  1930. Revision 1.66 2002/04/02 17:11:32 peter
  1931. * tlocation,treference update
  1932. * LOC_CONSTANT added for better constant handling
  1933. * secondadd splitted in multiple routines
  1934. * location_force_reg added for loading a location to a register
  1935. of a specified size
  1936. * secondassignment parses now first the right and then the left node
  1937. (this is compatible with Kylix). This saves a lot of push/pop especially
  1938. with string operations
  1939. * adapted some routines to use the new cg methods
  1940. Revision 1.65 2002/04/01 20:57:14 jonas
  1941. * fixed web bug 1907
  1942. * fixed some other procvar related bugs (all related to accepting procvar
  1943. constructs with either too many or too little parameters)
  1944. (both merged, includes second typo fix of pexpr.pas)
  1945. Revision 1.64 2002/01/24 18:25:53 peter
  1946. * implicit result variable generation for assembler routines
  1947. * removed m_tp modeswitch, use m_tp7 or not(m_fpc) instead
  1948. Revision 1.63 2002/01/24 12:33:53 jonas
  1949. * adapted ranges of native types to int64 (e.g. high cardinal is no
  1950. longer longint($ffffffff), but just $fffffff in psystem)
  1951. * small additional fix in 64bit rangecheck code generation for 32 bit
  1952. processors
  1953. * adaption of ranges required the matching talgorithm used for selecting
  1954. which overloaded procedure to call to be adapted. It should now always
  1955. select the closest match for ordinal parameters.
  1956. + inttostr(qword) in sysstr.inc/sysstrh.inc
  1957. + abs(int64), sqr(int64), sqr(qword) in systemh.inc/generic.inc (previous
  1958. fixes were required to be able to add them)
  1959. * is_in_limit() moved from ncal to types unit, should always be used
  1960. instead of direct comparisons of low/high values of orddefs because
  1961. qword is a special case
  1962. }