types.pas 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  1. {
  2. $Id$
  3. Copyright (C) 1998-2000 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 types;
  19. interface
  20. uses
  21. cobjects,symtable;
  22. type
  23. tmmxtype = (mmxno,mmxu8bit,mmxs8bit,mmxu16bit,mmxs16bit,
  24. mmxu32bit,mmxs32bit,mmxfixed16,mmxsingle);
  25. const
  26. { true if we must never copy this parameter }
  27. never_copy_const_param : boolean = false;
  28. {*****************************************************************************
  29. Basic type functions
  30. *****************************************************************************}
  31. { returns true, if def defines an ordinal type }
  32. function is_ordinal(def : pdef) : boolean;
  33. { returns the min. value of the type }
  34. function get_min_value(def : pdef) : longint;
  35. { returns true, if def defines an ordinal type }
  36. function is_integer(def : pdef) : boolean;
  37. { true if p is a boolean }
  38. function is_boolean(def : pdef) : boolean;
  39. { true if p is a char }
  40. function is_char(def : pdef) : boolean;
  41. { true if p is a void}
  42. function is_void(def : pdef) : boolean;
  43. { true if p is a smallset def }
  44. function is_smallset(p : pdef) : boolean;
  45. { returns true, if def defines a signed data type (only for ordinal types) }
  46. function is_signed(def : pdef) : boolean;
  47. {*****************************************************************************
  48. Array helper functions
  49. *****************************************************************************}
  50. { true, if p points to a zero based (non special like open or
  51. dynamic array def, mainly this is used to see if the array
  52. is convertable to a pointer }
  53. function is_zero_based_array(p : pdef) : boolean;
  54. { true if p points to an open array def }
  55. function is_open_array(p : pdef) : boolean;
  56. { true, if p points to an array of const def }
  57. function is_array_constructor(p : pdef) : boolean;
  58. { true, if p points to a variant array }
  59. function is_variant_array(p : pdef) : boolean;
  60. { true, if p points to an array of const }
  61. function is_array_of_const(p : pdef) : boolean;
  62. { true, if p points any kind of special array }
  63. function is_special_array(p : pdef) : boolean;
  64. { true if p is a char array def }
  65. function is_chararray(p : pdef) : boolean;
  66. {*****************************************************************************
  67. String helper functions
  68. *****************************************************************************}
  69. { true if p points to an open string def }
  70. function is_open_string(p : pdef) : boolean;
  71. { true if p is an ansi string def }
  72. function is_ansistring(p : pdef) : boolean;
  73. { true if p is a long string def }
  74. function is_longstring(p : pdef) : boolean;
  75. { true if p is a wide string def }
  76. function is_widestring(p : pdef) : boolean;
  77. { true if p is a short string def }
  78. function is_shortstring(p : pdef) : boolean;
  79. { true if p is a pchar def }
  80. function is_pchar(p : pdef) : boolean;
  81. { true if p is a voidpointer def }
  82. function is_voidpointer(p : pdef) : boolean;
  83. { returns true, if def uses FPU }
  84. function is_fpu(def : pdef) : boolean;
  85. { true if the return value is in EAX }
  86. function ret_in_acc(def : pdef) : boolean;
  87. { true if uses a parameter as return value }
  88. function ret_in_param(def : pdef) : boolean;
  89. { true, if def is a 64 bit int type }
  90. function is_64bitint(def : pdef) : boolean;
  91. function push_high_param(def : pdef) : boolean;
  92. { true if a parameter is too large to copy and only the address is pushed }
  93. function push_addr_param(def : pdef) : boolean;
  94. { true, if def1 and def2 are semantical the same }
  95. function is_equal(def1,def2 : pdef) : boolean;
  96. { checks for type compatibility (subgroups of type) }
  97. { used for case statements... probably missing stuff }
  98. { to use on other types }
  99. function is_subequal(def1, def2: pdef): boolean;
  100. { same as is_equal, but with error message if failed }
  101. function CheckTypes(def1,def2 : pdef) : boolean;
  102. { true, if two parameter lists are equal }
  103. { if value_equal_const is true, call by value }
  104. { and call by const parameter are assumed as }
  105. { equal }
  106. function equal_paras(paralist1,paralist2 : plinkedlist;value_equal_const : boolean) : boolean;
  107. { true if a type can be allowed for another one
  108. in a func var }
  109. function convertable_paras(paralist1,paralist2 : plinkedlist;value_equal_const : boolean) : boolean;
  110. { true if a function can be assigned to a procvar }
  111. function proc_to_procvar_equal(def1:pprocdef;def2:pprocvardef) : boolean;
  112. { if l isn't in the range of def a range check error is generated and
  113. the value is placed within the range }
  114. procedure testrange(def : pdef;var l : longint);
  115. { returns the range of def }
  116. procedure getrange(def : pdef;var l : longint;var h : longint);
  117. { some type helper routines for MMX support }
  118. function is_mmx_able_array(p : pdef) : boolean;
  119. { returns the mmx type }
  120. function mmx_type(p : pdef) : tmmxtype;
  121. { returns true, if sym needs an entry in the proplist of a class rtti }
  122. function needs_prop_entry(sym : psym) : boolean;
  123. implementation
  124. uses
  125. strings,globtype,globals,htypechk,
  126. tree,verbose,symconst;
  127. function needs_prop_entry(sym : psym) : boolean;
  128. begin
  129. needs_prop_entry:=(sp_published in psym(sym)^.symoptions) and
  130. (sym^.typ in [propertysym,varsym]);
  131. end;
  132. function equal_paras(paralist1,paralist2 : plinkedlist;value_equal_const : boolean) : boolean;
  133. var
  134. def1,def2 : pparaitem;
  135. begin
  136. def1:=pparaitem(paralist1^.first);
  137. def2:=pparaitem(paralist2^.first);
  138. while (assigned(def1)) and (assigned(def2)) do
  139. begin
  140. if value_equal_const then
  141. begin
  142. if not(is_equal(def1^.paratype.def,def2^.paratype.def)) or
  143. ((def1^.paratyp<>def2^.paratyp) and
  144. ((def1^.paratyp=vs_var) or
  145. (def1^.paratyp=vs_var)
  146. )
  147. ) then
  148. begin
  149. equal_paras:=false;
  150. exit;
  151. end;
  152. end
  153. else
  154. begin
  155. if not(is_equal(def1^.paratype.def,def2^.paratype.def)) or
  156. (def1^.paratyp<>def2^.paratyp) then
  157. begin
  158. equal_paras:=false;
  159. exit;
  160. end;
  161. end;
  162. def1:=pparaitem(def1^.next);
  163. def2:=pparaitem(def2^.next);
  164. end;
  165. if (def1=nil) and (def2=nil) then
  166. equal_paras:=true
  167. else
  168. equal_paras:=false;
  169. end;
  170. function convertable_paras(paralist1,paralist2 : plinkedlist;value_equal_const : boolean) : boolean;
  171. var
  172. def1,def2 : pparaitem;
  173. doconv : tconverttype;
  174. begin
  175. def1:=pparaitem(paralist1^.first);
  176. def2:=pparaitem(paralist2^.first);
  177. while (assigned(def1)) and (assigned(def2)) do
  178. begin
  179. if value_equal_const then
  180. begin
  181. if (isconvertable(def1^.paratype.def,def2^.paratype.def,doconv,callparan,false)=0) or
  182. ((def1^.paratyp<>def2^.paratyp) and
  183. ((def1^.paratyp=vs_var) or
  184. (def1^.paratyp=vs_var)
  185. )
  186. ) then
  187. begin
  188. convertable_paras:=false;
  189. exit;
  190. end;
  191. end
  192. else
  193. begin
  194. if (isconvertable(def1^.paratype.def,def2^.paratype.def,doconv,callparan,false)=0) or
  195. (def1^.paratyp<>def2^.paratyp) then
  196. begin
  197. convertable_paras:=false;
  198. exit;
  199. end;
  200. end;
  201. def1:=pparaitem(def1^.next);
  202. def2:=pparaitem(def2^.next);
  203. end;
  204. if (def1=nil) and (def2=nil) then
  205. convertable_paras:=true
  206. else
  207. convertable_paras:=false;
  208. end;
  209. { true if a function can be assigned to a procvar }
  210. function proc_to_procvar_equal(def1:pprocdef;def2:pprocvardef) : boolean;
  211. const
  212. po_comp = po_compatibility_options-[po_methodpointer];
  213. var
  214. ismethod : boolean;
  215. begin
  216. proc_to_procvar_equal:=false;
  217. if not(assigned(def1)) or not(assigned(def2)) then
  218. exit;
  219. { check for method pointer }
  220. ismethod:=assigned(def1^.owner) and
  221. (def1^.owner^.symtabletype=objectsymtable);
  222. { I think methods of objects are also not compatible }
  223. { with procedure variables! (FK)
  224. and
  225. assigned(def1^.owner^.defowner) and
  226. (pobjectdef(def1^.owner^.defowner)^.is_class); }
  227. if (ismethod and not (po_methodpointer in def2^.procoptions)) or
  228. (not(ismethod) and (po_methodpointer in def2^.procoptions)) then
  229. begin
  230. Message(type_e_no_method_and_procedure_not_compatible);
  231. exit;
  232. end;
  233. { check return value and para's and options, methodpointer is already checked
  234. parameters may also be convertable }
  235. if is_equal(def1^.rettype.def,def2^.rettype.def) and
  236. (equal_paras(def1^.para,def2^.para,false) or
  237. convertable_paras(def1^.para,def2^.para,false)) and
  238. ((po_comp * def1^.procoptions)= (po_comp * def2^.procoptions)) then
  239. proc_to_procvar_equal:=true
  240. else
  241. proc_to_procvar_equal:=false;
  242. end;
  243. { returns true, if def uses FPU }
  244. function is_fpu(def : pdef) : boolean;
  245. begin
  246. is_fpu:=(def^.deftype=floatdef) and (pfloatdef(def)^.typ<>f32bit);
  247. end;
  248. { true if p is an ordinal }
  249. function is_ordinal(def : pdef) : boolean;
  250. var
  251. dt : tbasetype;
  252. begin
  253. case def^.deftype of
  254. orddef :
  255. begin
  256. dt:=porddef(def)^.typ;
  257. is_ordinal:=dt in [uchar,
  258. u8bit,u16bit,u32bit,u64bit,
  259. s8bit,s16bit,s32bit,s64bit,
  260. bool8bit,bool16bit,bool32bit];
  261. end;
  262. enumdef :
  263. is_ordinal:=true;
  264. else
  265. is_ordinal:=false;
  266. end;
  267. end;
  268. { returns the min. value of the type }
  269. function get_min_value(def : pdef) : longint;
  270. begin
  271. case def^.deftype of
  272. orddef:
  273. get_min_value:=porddef(def)^.low;
  274. enumdef:
  275. get_min_value:=penumdef(def)^.min;
  276. else
  277. get_min_value:=0;
  278. end;
  279. end;
  280. { true if p is an integer }
  281. function is_integer(def : pdef) : boolean;
  282. begin
  283. is_integer:=(def^.deftype=orddef) and
  284. (porddef(def)^.typ in [uauto,u8bit,u16bit,u32bit,u64bit,
  285. s8bit,s16bit,s32bit,s64bit]);
  286. end;
  287. { true if p is a boolean }
  288. function is_boolean(def : pdef) : boolean;
  289. begin
  290. is_boolean:=(def^.deftype=orddef) and
  291. (porddef(def)^.typ in [bool8bit,bool16bit,bool32bit]);
  292. end;
  293. { true if p is a void }
  294. function is_void(def : pdef) : boolean;
  295. begin
  296. is_void:=(def^.deftype=orddef) and
  297. (porddef(def)^.typ=uvoid);
  298. end;
  299. { true if p is a char }
  300. function is_char(def : pdef) : boolean;
  301. begin
  302. is_char:=(def^.deftype=orddef) and
  303. (porddef(def)^.typ=uchar);
  304. end;
  305. { true if p is signed (integer) }
  306. function is_signed(def : pdef) : boolean;
  307. var
  308. dt : tbasetype;
  309. begin
  310. case def^.deftype of
  311. orddef :
  312. begin
  313. dt:=porddef(def)^.typ;
  314. is_signed:=(dt in [s8bit,s16bit,s32bit,s64bit]);
  315. end;
  316. enumdef :
  317. is_signed:=false;
  318. else
  319. is_signed:=false;
  320. end;
  321. end;
  322. { true, if p points to an open array def }
  323. function is_open_string(p : pdef) : boolean;
  324. begin
  325. is_open_string:=(p^.deftype=stringdef) and
  326. (pstringdef(p)^.string_typ=st_shortstring) and
  327. (pstringdef(p)^.len=0);
  328. end;
  329. { true, if p points to a zero based array def }
  330. function is_zero_based_array(p : pdef) : boolean;
  331. begin
  332. is_zero_based_array:=(p^.deftype=arraydef) and
  333. (parraydef(p)^.lowrange=0) and
  334. not(is_special_array(p));
  335. end;
  336. { true, if p points to an open array def }
  337. function is_open_array(p : pdef) : boolean;
  338. begin
  339. { check for s32bitdef is needed, because for u32bit the high
  340. range is also -1 ! (PFV) }
  341. is_open_array:=(p^.deftype=arraydef) and
  342. (parraydef(p)^.rangetype.def=pdef(s32bitdef)) and
  343. (parraydef(p)^.lowrange=0) and
  344. (parraydef(p)^.highrange=-1) and
  345. not(parraydef(p)^.IsConstructor) and
  346. not(parraydef(p)^.IsVariant) and
  347. not(parraydef(p)^.IsArrayOfConst);
  348. end;
  349. { true, if p points to an array of const def }
  350. function is_array_constructor(p : pdef) : boolean;
  351. begin
  352. is_array_constructor:=(p^.deftype=arraydef) and
  353. (parraydef(p)^.IsConstructor);
  354. end;
  355. { true, if p points to a variant array }
  356. function is_variant_array(p : pdef) : boolean;
  357. begin
  358. is_variant_array:=(p^.deftype=arraydef) and
  359. (parraydef(p)^.IsVariant);
  360. end;
  361. { true, if p points to an array of const }
  362. function is_array_of_const(p : pdef) : boolean;
  363. begin
  364. is_array_of_const:=(p^.deftype=arraydef) and
  365. (parraydef(p)^.IsArrayOfConst);
  366. end;
  367. { true, if p points to a special array }
  368. function is_special_array(p : pdef) : boolean;
  369. begin
  370. is_special_array:=(p^.deftype=arraydef) and
  371. ((parraydef(p)^.IsVariant) or
  372. (parraydef(p)^.IsArrayOfConst) or
  373. (parraydef(p)^.IsConstructor) or
  374. is_open_array(p)
  375. );
  376. end;
  377. { true if p is an ansi string def }
  378. function is_ansistring(p : pdef) : boolean;
  379. begin
  380. is_ansistring:=(p^.deftype=stringdef) and
  381. (pstringdef(p)^.string_typ=st_ansistring);
  382. end;
  383. { true if p is an long string def }
  384. function is_longstring(p : pdef) : boolean;
  385. begin
  386. is_longstring:=(p^.deftype=stringdef) and
  387. (pstringdef(p)^.string_typ=st_longstring);
  388. end;
  389. { true if p is an wide string def }
  390. function is_widestring(p : pdef) : boolean;
  391. begin
  392. is_widestring:=(p^.deftype=stringdef) and
  393. (pstringdef(p)^.string_typ=st_widestring);
  394. end;
  395. { true if p is an short string def }
  396. function is_shortstring(p : pdef) : boolean;
  397. begin
  398. is_shortstring:=(p^.deftype=stringdef) and
  399. (pstringdef(p)^.string_typ=st_shortstring);
  400. end;
  401. { true if p is a char array def }
  402. function is_chararray(p : pdef) : boolean;
  403. begin
  404. is_chararray:=(p^.deftype=arraydef) and
  405. is_equal(parraydef(p)^.elementtype.def,cchardef) and
  406. not(is_special_array(p));
  407. end;
  408. { true if p is a pchar def }
  409. function is_pchar(p : pdef) : boolean;
  410. begin
  411. is_pchar:=(p^.deftype=pointerdef) and
  412. is_equal(Ppointerdef(p)^.pointertype.def,cchardef);
  413. end;
  414. { true if p is a voidpointer def }
  415. function is_voidpointer(p : pdef) : boolean;
  416. begin
  417. is_voidpointer:=(p^.deftype=pointerdef) and
  418. is_equal(Ppointerdef(p)^.pointertype.def,voiddef);
  419. end;
  420. { true if p is a smallset def }
  421. function is_smallset(p : pdef) : boolean;
  422. begin
  423. is_smallset:=(p^.deftype=setdef) and
  424. (psetdef(p)^.settype=smallset);
  425. end;
  426. { true if the return value is in accumulator (EAX for i386), D0 for 68k }
  427. function ret_in_acc(def : pdef) : boolean;
  428. begin
  429. ret_in_acc:=(def^.deftype in [orddef,pointerdef,enumdef,classrefdef]) or
  430. ((def^.deftype=stringdef) and (pstringdef(def)^.string_typ in [st_ansistring,st_widestring])) or
  431. ((def^.deftype=procvardef) and not(po_methodpointer in pprocvardef(def)^.procoptions)) or
  432. ((def^.deftype=objectdef) and pobjectdef(def)^.is_class) or
  433. ((def^.deftype=setdef) and (psetdef(def)^.settype=smallset)) or
  434. ((def^.deftype=floatdef) and (pfloatdef(def)^.typ=f32bit));
  435. end;
  436. { true, if def is a 64 bit int type }
  437. function is_64bitint(def : pdef) : boolean;
  438. begin
  439. is_64bitint:=(def^.deftype=orddef) and (porddef(def)^.typ in [u64bit,s64bit])
  440. end;
  441. { true if uses a parameter as return value }
  442. function ret_in_param(def : pdef) : boolean;
  443. begin
  444. ret_in_param:=(def^.deftype in [arraydef,recorddef]) or
  445. ((def^.deftype=stringdef) and (pstringdef(def)^.string_typ in [st_shortstring,st_longstring])) or
  446. ((def^.deftype=procvardef) and (po_methodpointer in pprocvardef(def)^.procoptions)) or
  447. ((def^.deftype=objectdef) and not(pobjectdef(def)^.is_class)) or
  448. ((def^.deftype=setdef) and (psetdef(def)^.settype<>smallset));
  449. end;
  450. function push_high_param(def : pdef) : boolean;
  451. begin
  452. push_high_param:=is_open_array(def) or
  453. is_open_string(def) or
  454. is_array_of_const(def);
  455. end;
  456. { true if a parameter is too large to copy and only the address is pushed }
  457. function push_addr_param(def : pdef) : boolean;
  458. begin
  459. push_addr_param:=false;
  460. if never_copy_const_param then
  461. push_addr_param:=true
  462. else
  463. begin
  464. case def^.deftype of
  465. formaldef :
  466. push_addr_param:=true;
  467. recorddef :
  468. push_addr_param:=(def^.size>4);
  469. arraydef :
  470. push_addr_param:=((Parraydef(def)^.highrange>Parraydef(def)^.lowrange) and (def^.size>4)) or
  471. is_open_array(def) or
  472. is_array_of_const(def) or
  473. is_array_constructor(def);
  474. objectdef :
  475. push_addr_param:=not(pobjectdef(def)^.is_class);
  476. stringdef :
  477. push_addr_param:=pstringdef(def)^.string_typ in [st_shortstring,st_longstring];
  478. procvardef :
  479. push_addr_param:=(po_methodpointer in pprocvardef(def)^.procoptions);
  480. setdef :
  481. push_addr_param:=(psetdef(def)^.settype<>smallset);
  482. end;
  483. end;
  484. end;
  485. { test if l is in the range of def, outputs error if out of range }
  486. procedure testrange(def : pdef;var l : longint);
  487. var
  488. lv,hv: longint;
  489. begin
  490. { for 64 bit types we need only to check if it is less than }
  491. { zero, if def is a qword node }
  492. if is_64bitint(def) then
  493. begin
  494. if (l<0) and (porddef(def)^.typ=u64bit) then
  495. begin
  496. l:=0;
  497. if (cs_check_range in aktlocalswitches) then
  498. Message(parser_e_range_check_error)
  499. else
  500. Message(parser_w_range_check_error);
  501. end;
  502. end
  503. else
  504. begin
  505. getrange(def,lv,hv);
  506. if (def^.deftype=orddef) and
  507. (porddef(def)^.typ=u32bit) then
  508. begin
  509. if lv<=hv then
  510. begin
  511. if (l<lv) or (l>hv) then
  512. begin
  513. if (cs_check_range in aktlocalswitches) then
  514. Message(parser_e_range_check_error)
  515. else
  516. Message(parser_w_range_check_error);
  517. end;
  518. end
  519. else
  520. { this happens with the wrap around problem }
  521. { if lv is positive and hv is over $7ffffff }
  522. { so it seems negative }
  523. begin
  524. if ((l>=0) and (l<lv)) or
  525. ((l<0) and (l>hv)) then
  526. begin
  527. if (cs_check_range in aktlocalswitches) then
  528. Message(parser_e_range_check_error)
  529. else
  530. Message(parser_w_range_check_error);
  531. end;
  532. end;
  533. end
  534. else if (l<lv) or (l>hv) then
  535. begin
  536. if (def^.deftype=enumdef) or
  537. (cs_check_range in aktlocalswitches) then
  538. Message(parser_e_range_check_error)
  539. else
  540. Message(parser_w_range_check_error);
  541. { Fix the value to fit in the allocated space for this type of variable }
  542. case def^.size of
  543. 1: l := l and $ff;
  544. 2: l := l and $ffff;
  545. end
  546. { l:=lv+(l mod (hv-lv+1));}
  547. end;
  548. end;
  549. end;
  550. { return the range from def in l and h }
  551. procedure getrange(def : pdef;var l : longint;var h : longint);
  552. begin
  553. case def^.deftype of
  554. orddef :
  555. begin
  556. l:=porddef(def)^.low;
  557. h:=porddef(def)^.high;
  558. end;
  559. enumdef :
  560. begin
  561. l:=penumdef(def)^.min;
  562. h:=penumdef(def)^.max;
  563. end;
  564. arraydef :
  565. begin
  566. l:=parraydef(def)^.lowrange;
  567. h:=parraydef(def)^.highrange;
  568. end;
  569. else
  570. internalerror(987);
  571. end;
  572. end;
  573. function mmx_type(p : pdef) : tmmxtype;
  574. begin
  575. mmx_type:=mmxno;
  576. if is_mmx_able_array(p) then
  577. begin
  578. if parraydef(p)^.elementtype.def^.deftype=floatdef then
  579. case pfloatdef(parraydef(p)^.elementtype.def)^.typ of
  580. s32real:
  581. mmx_type:=mmxsingle;
  582. f16bit:
  583. mmx_type:=mmxfixed16
  584. end
  585. else
  586. case porddef(parraydef(p)^.elementtype.def)^.typ of
  587. u8bit:
  588. mmx_type:=mmxu8bit;
  589. s8bit:
  590. mmx_type:=mmxs8bit;
  591. u16bit:
  592. mmx_type:=mmxu16bit;
  593. s16bit:
  594. mmx_type:=mmxs16bit;
  595. u32bit:
  596. mmx_type:=mmxu32bit;
  597. s32bit:
  598. mmx_type:=mmxs32bit;
  599. end;
  600. end;
  601. end;
  602. function is_mmx_able_array(p : pdef) : boolean;
  603. begin
  604. {$ifdef SUPPORT_MMX}
  605. if (cs_mmx_saturation in aktlocalswitches) then
  606. begin
  607. is_mmx_able_array:=(p^.deftype=arraydef) and
  608. not(is_special_array(p)) and
  609. (
  610. (
  611. (parraydef(p)^.elementtype.def^.deftype=orddef) and
  612. (
  613. (
  614. (parraydef(p)^.lowrange=0) and
  615. (parraydef(p)^.highrange=1) and
  616. (porddef(parraydef(p)^.elementtype.def)^.typ in [u32bit,s32bit])
  617. )
  618. or
  619. (
  620. (parraydef(p)^.lowrange=0) and
  621. (parraydef(p)^.highrange=3) and
  622. (porddef(parraydef(p)^.elementtype.def)^.typ in [u16bit,s16bit])
  623. )
  624. )
  625. )
  626. or
  627. (
  628. (
  629. (parraydef(p)^.elementtype.def^.deftype=floatdef) and
  630. (
  631. (parraydef(p)^.lowrange=0) and
  632. (parraydef(p)^.highrange=3) and
  633. (pfloatdef(parraydef(p)^.elementtype.def)^.typ=f16bit)
  634. ) or
  635. (
  636. (parraydef(p)^.lowrange=0) and
  637. (parraydef(p)^.highrange=1) and
  638. (pfloatdef(parraydef(p)^.elementtype.def)^.typ=s32real)
  639. )
  640. )
  641. )
  642. );
  643. end
  644. else
  645. begin
  646. is_mmx_able_array:=(p^.deftype=arraydef) and
  647. (
  648. (
  649. (parraydef(p)^.elementtype.def^.deftype=orddef) and
  650. (
  651. (
  652. (parraydef(p)^.lowrange=0) and
  653. (parraydef(p)^.highrange=1) and
  654. (porddef(parraydef(p)^.elementtype.def)^.typ in [u32bit,s32bit])
  655. )
  656. or
  657. (
  658. (parraydef(p)^.lowrange=0) and
  659. (parraydef(p)^.highrange=3) and
  660. (porddef(parraydef(p)^.elementtype.def)^.typ in [u16bit,s16bit])
  661. )
  662. or
  663. (
  664. (parraydef(p)^.lowrange=0) and
  665. (parraydef(p)^.highrange=7) and
  666. (porddef(parraydef(p)^.elementtype.def)^.typ in [u8bit,s8bit])
  667. )
  668. )
  669. )
  670. or
  671. (
  672. (parraydef(p)^.elementtype.def^.deftype=floatdef) and
  673. (
  674. (
  675. (parraydef(p)^.lowrange=0) and
  676. (parraydef(p)^.highrange=3) and
  677. (pfloatdef(parraydef(p)^.elementtype.def)^.typ=f32bit)
  678. )
  679. or
  680. (
  681. (parraydef(p)^.lowrange=0) and
  682. (parraydef(p)^.highrange=1) and
  683. (pfloatdef(parraydef(p)^.elementtype.def)^.typ=s32real)
  684. )
  685. )
  686. )
  687. );
  688. end;
  689. {$else SUPPORT_MMX}
  690. is_mmx_able_array:=false;
  691. {$endif SUPPORT_MMX}
  692. end;
  693. function is_equal(def1,def2 : pdef) : boolean;
  694. var
  695. b : boolean;
  696. hd : pdef;
  697. begin
  698. { both types must exists }
  699. if not (assigned(def1) and assigned(def2)) then
  700. begin
  701. is_equal:=false;
  702. exit;
  703. end;
  704. { be sure, that if there is a stringdef, that this is def1 }
  705. if def2^.deftype=stringdef then
  706. begin
  707. hd:=def1;
  708. def1:=def2;
  709. def2:=hd;
  710. end;
  711. b:=false;
  712. { both point to the same definition ? }
  713. if def1=def2 then
  714. b:=true
  715. else
  716. { pointer with an equal definition are equal }
  717. if (def1^.deftype=pointerdef) and (def2^.deftype=pointerdef) then
  718. begin
  719. { here a problem detected in tabsolutesym }
  720. { the types can be forward type !! }
  721. if assigned(def1^.typesym) and (ppointerdef(def1)^.pointertype.def^.deftype=forwarddef) then
  722. b:=(def1^.typesym=def2^.typesym)
  723. else
  724. b:=ppointerdef(def1)^.pointertype.def=ppointerdef(def2)^.pointertype.def;
  725. end
  726. else
  727. { ordinals are equal only when the ordinal type is equal }
  728. if (def1^.deftype=orddef) and (def2^.deftype=orddef) then
  729. begin
  730. case porddef(def1)^.typ of
  731. u8bit,u16bit,u32bit,
  732. s8bit,s16bit,s32bit:
  733. b:=((porddef(def1)^.typ=porddef(def2)^.typ) and
  734. (porddef(def1)^.low=porddef(def2)^.low) and
  735. (porddef(def1)^.high=porddef(def2)^.high));
  736. uvoid,uchar,
  737. bool8bit,bool16bit,bool32bit:
  738. b:=(porddef(def1)^.typ=porddef(def2)^.typ);
  739. end;
  740. end
  741. else
  742. if (def1^.deftype=floatdef) and (def2^.deftype=floatdef) then
  743. b:=pfloatdef(def1)^.typ=pfloatdef(def2)^.typ
  744. else
  745. { strings with the same length are equal }
  746. if (def1^.deftype=stringdef) and (def2^.deftype=stringdef) and
  747. (pstringdef(def1)^.string_typ=pstringdef(def2)^.string_typ) then
  748. begin
  749. b:=not(is_shortstring(def1)) or
  750. (pstringdef(def1)^.len=pstringdef(def2)^.len);
  751. end
  752. else
  753. if (def1^.deftype=formaldef) and (def2^.deftype=formaldef) then
  754. b:=true
  755. { file types with the same file element type are equal }
  756. { this is a problem for assign !! }
  757. { changed to allow if one is untyped }
  758. { all typed files are equal to the special }
  759. { typed file that has voiddef as elemnt type }
  760. { but must NOT match for text file !!! }
  761. else
  762. if (def1^.deftype=filedef) and (def2^.deftype=filedef) then
  763. b:=(pfiledef(def1)^.filetyp=pfiledef(def2)^.filetyp) and
  764. ((
  765. ((pfiledef(def1)^.typedfiletype.def=nil) and
  766. (pfiledef(def2)^.typedfiletype.def=nil)) or
  767. (
  768. (pfiledef(def1)^.typedfiletype.def<>nil) and
  769. (pfiledef(def2)^.typedfiletype.def<>nil) and
  770. is_equal(pfiledef(def1)^.typedfiletype.def,pfiledef(def2)^.typedfiletype.def)
  771. ) or
  772. ( (pfiledef(def1)^.typedfiletype.def=pdef(voiddef)) or
  773. (pfiledef(def2)^.typedfiletype.def=pdef(voiddef))
  774. )))
  775. { sets with the same element type are equal }
  776. else
  777. if (def1^.deftype=setdef) and (def2^.deftype=setdef) then
  778. begin
  779. if assigned(psetdef(def1)^.elementtype.def) and
  780. assigned(psetdef(def2)^.elementtype.def) then
  781. b:=(psetdef(def1)^.elementtype.def^.deftype=psetdef(def2)^.elementtype.def^.deftype)
  782. else
  783. b:=true;
  784. end
  785. else
  786. if (def1^.deftype=procvardef) and (def2^.deftype=procvardef) then
  787. begin
  788. { poassembler isn't important for compatibility }
  789. { if a method is assigned to a methodpointer }
  790. { is checked before }
  791. b:=(pprocvardef(def1)^.proctypeoption=pprocvardef(def2)^.proctypeoption) and
  792. (pprocvardef(def1)^.proccalloptions=pprocvardef(def2)^.proccalloptions) and
  793. ((pprocvardef(def1)^.procoptions * po_compatibility_options)=
  794. (pprocvardef(def2)^.procoptions * po_compatibility_options)) and
  795. is_equal(pprocvardef(def1)^.rettype.def,pprocvardef(def2)^.rettype.def) and
  796. equal_paras(pprocvardef(def1)^.para,pprocvardef(def2)^.para,false);
  797. end
  798. else
  799. if (def1^.deftype=arraydef) and (def2^.deftype=arraydef) then
  800. begin
  801. if is_open_array(def1) or is_open_array(def2) or
  802. is_array_of_const(def1) or is_array_of_const(def2) then
  803. begin
  804. if parraydef(def1)^.IsArrayOfConst or parraydef(def2)^.IsArrayOfConst then
  805. b:=true
  806. else
  807. b:=is_equal(parraydef(def1)^.elementtype.def,parraydef(def2)^.elementtype.def);
  808. end
  809. else
  810. begin
  811. b:=not(m_tp in aktmodeswitches) and
  812. not(m_delphi in aktmodeswitches) and
  813. (parraydef(def1)^.lowrange=parraydef(def2)^.lowrange) and
  814. (parraydef(def1)^.highrange=parraydef(def2)^.highrange) and
  815. is_equal(parraydef(def1)^.elementtype.def,parraydef(def2)^.elementtype.def) and
  816. is_equal(parraydef(def1)^.rangetype.def,parraydef(def2)^.rangetype.def);
  817. end;
  818. end
  819. else
  820. if (def1^.deftype=classrefdef) and (def2^.deftype=classrefdef) then
  821. begin
  822. { similar to pointerdef: }
  823. if assigned(def1^.typesym) and (pclassrefdef(def1)^.pointertype.def^.deftype=forwarddef) then
  824. b:=(def1^.typesym=def2^.typesym)
  825. else
  826. b:=is_equal(pclassrefdef(def1)^.pointertype.def,pclassrefdef(def2)^.pointertype.def);
  827. end;
  828. is_equal:=b;
  829. end;
  830. function is_subequal(def1, def2: pdef): boolean;
  831. Begin
  832. is_subequal := false;
  833. if assigned(def1) and assigned(def2) then
  834. Begin
  835. if (def1^.deftype = orddef) and (def2^.deftype = orddef) then
  836. Begin
  837. { see p.47 of Turbo Pascal 7.01 manual for the separation of types }
  838. { range checking for case statements is done with testrange }
  839. case porddef(def1)^.typ of
  840. u8bit,u16bit,u32bit,
  841. s8bit,s16bit,s32bit :
  842. is_subequal:=(porddef(def2)^.typ in [s32bit,u32bit,u8bit,s8bit,s16bit,u16bit]);
  843. bool8bit,bool16bit,bool32bit :
  844. is_subequal:=(porddef(def2)^.typ in [bool8bit,bool16bit,bool32bit]);
  845. uchar :
  846. is_subequal:=(porddef(def2)^.typ=uchar);
  847. end;
  848. end
  849. else
  850. Begin
  851. { I assume that both enumerations are equal when the first }
  852. { pointers are equal. }
  853. if (def1^.deftype = enumdef) and (def2^.deftype =enumdef) then
  854. Begin
  855. if penumdef(def1)^.firstenum = penumdef(def2)^.firstenum then
  856. is_subequal := TRUE;
  857. end;
  858. end;
  859. end; { endif assigned ... }
  860. end;
  861. function CheckTypes(def1,def2 : pdef) : boolean;
  862. var
  863. s1,s2 : string;
  864. begin
  865. if not is_equal(def1,def2) then
  866. begin
  867. { Crash prevention }
  868. if (not assigned(def1)) or (not assigned(def2)) then
  869. Message(type_e_mismatch)
  870. else
  871. begin
  872. s1:=def1^.typename;
  873. s2:=def2^.typename;
  874. if (s1<>'<unknown type>') and (s2<>'<unknown type>') then
  875. Message2(type_e_not_equal_types,def1^.typename,def2^.typename)
  876. else
  877. Message(type_e_mismatch);
  878. end;
  879. CheckTypes:=false;
  880. end
  881. else
  882. CheckTypes:=true;
  883. end;
  884. end.
  885. {
  886. $Log$
  887. Revision 1.97 2000-02-09 13:23:09 peter
  888. * log truncated
  889. Revision 1.96 2000/02/01 09:44:03 peter
  890. * is_voidpointer
  891. Revision 1.95 2000/01/07 01:14:49 peter
  892. * updated copyright to 2000
  893. Revision 1.94 2000/01/04 16:35:58 jonas
  894. * when range checking is off, constants that are out of bound are no longer
  895. truncated to their max/min legal value but left alone (jsut an "and" is done to
  896. make sure they fit in the allocated space if necessary)
  897. Revision 1.93 1999/12/31 14:26:28 peter
  898. * fixed crash with empty array constructors
  899. Revision 1.92 1999/11/30 10:40:59 peter
  900. + ttype, tsymlist
  901. Revision 1.91 1999/11/06 14:34:31 peter
  902. * truncated log to 20 revs
  903. Revision 1.90 1999/10/26 12:30:46 peter
  904. * const parameter is now checked
  905. * better and generic check if a node can be used for assigning
  906. * export fixes
  907. * procvar equal works now (it never had worked at least from 0.99.8)
  908. * defcoll changed to linkedlist with pparaitem so it can easily be
  909. walked both directions
  910. Revision 1.89 1999/10/01 10:04:07 peter
  911. * fixed is_equal for proc -> procvar which didn't check the
  912. callconvention and type anymore since the splitting of procoptions
  913. Revision 1.88 1999/10/01 08:02:51 peter
  914. * forward type declaration rewritten
  915. Revision 1.87 1999/09/15 22:09:27 florian
  916. + rtti is now automatically generated for published classes, i.e.
  917. they are handled like an implicit property
  918. Revision 1.86 1999/09/11 09:08:35 florian
  919. * fixed bug 596
  920. * fixed some problems with procedure variables and procedures of object,
  921. especially in TP mode. Procedure of object doesn't apply only to classes,
  922. it is also allowed for objects !!
  923. Revision 1.85 1999/08/13 21:27:08 peter
  924. * more fixes for push_addr
  925. Revision 1.84 1999/08/13 15:38:23 peter
  926. * fixed push_addr_param for records < 4, the array high<low range check
  927. broke this code.
  928. Revision 1.83 1999/08/07 14:21:06 florian
  929. * some small problems fixed
  930. Revision 1.82 1999/08/07 13:36:56 daniel
  931. * Recommitted the arraydef overflow bugfix.
  932. Revision 1.80 1999/08/05 22:42:49 daniel
  933. * Fixed potential bug for open arrays (Their size is not known at
  934. compilation time).
  935. Revision 1.79 1999/08/03 22:03:41 peter
  936. * moved bitmask constants to sets
  937. * some other type/const renamings
  938. Revision 1.78 1999/07/30 12:26:42 peter
  939. * array is_equal disabled for tp,delphi mode
  940. Revision 1.77 1999/07/29 11:41:51 peter
  941. * array is_equal extended
  942. Revision 1.76 1999/07/27 23:39:15 peter
  943. * open array checks also for s32bitdef, because u32bit also has a
  944. high range of -1
  945. }