defbase.pas 82 KB

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