types.pas 39 KB

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