types.pas 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  1. {
  2. $Id$
  3. Copyright (C) 1993-98 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 be in range }
  534. l:=lv+(l mod (hv-lv+1));
  535. end;
  536. end;
  537. end;
  538. { return the range from def in l and h }
  539. procedure getrange(def : pdef;var l : longint;var h : longint);
  540. begin
  541. case def^.deftype of
  542. orddef :
  543. begin
  544. l:=porddef(def)^.low;
  545. h:=porddef(def)^.high;
  546. end;
  547. enumdef :
  548. begin
  549. l:=penumdef(def)^.min;
  550. h:=penumdef(def)^.max;
  551. end;
  552. arraydef :
  553. begin
  554. l:=parraydef(def)^.lowrange;
  555. h:=parraydef(def)^.highrange;
  556. end;
  557. else
  558. internalerror(987);
  559. end;
  560. end;
  561. function mmx_type(p : pdef) : tmmxtype;
  562. begin
  563. mmx_type:=mmxno;
  564. if is_mmx_able_array(p) then
  565. begin
  566. if parraydef(p)^.elementtype.def^.deftype=floatdef then
  567. case pfloatdef(parraydef(p)^.elementtype.def)^.typ of
  568. s32real:
  569. mmx_type:=mmxsingle;
  570. f16bit:
  571. mmx_type:=mmxfixed16
  572. end
  573. else
  574. case porddef(parraydef(p)^.elementtype.def)^.typ of
  575. u8bit:
  576. mmx_type:=mmxu8bit;
  577. s8bit:
  578. mmx_type:=mmxs8bit;
  579. u16bit:
  580. mmx_type:=mmxu16bit;
  581. s16bit:
  582. mmx_type:=mmxs16bit;
  583. u32bit:
  584. mmx_type:=mmxu32bit;
  585. s32bit:
  586. mmx_type:=mmxs32bit;
  587. end;
  588. end;
  589. end;
  590. function is_mmx_able_array(p : pdef) : boolean;
  591. begin
  592. {$ifdef SUPPORT_MMX}
  593. if (cs_mmx_saturation in aktlocalswitches) then
  594. begin
  595. is_mmx_able_array:=(p^.deftype=arraydef) and
  596. not(is_special_array(p)) and
  597. (
  598. (
  599. (parraydef(p)^.elementtype.def^.deftype=orddef) and
  600. (
  601. (
  602. (parraydef(p)^.lowrange=0) and
  603. (parraydef(p)^.highrange=1) and
  604. (porddef(parraydef(p)^.elementtype.def)^.typ in [u32bit,s32bit])
  605. )
  606. or
  607. (
  608. (parraydef(p)^.lowrange=0) and
  609. (parraydef(p)^.highrange=3) and
  610. (porddef(parraydef(p)^.elementtype.def)^.typ in [u16bit,s16bit])
  611. )
  612. )
  613. )
  614. or
  615. (
  616. (
  617. (parraydef(p)^.elementtype.def^.deftype=floatdef) and
  618. (
  619. (parraydef(p)^.lowrange=0) and
  620. (parraydef(p)^.highrange=3) and
  621. (pfloatdef(parraydef(p)^.elementtype.def)^.typ=f16bit)
  622. ) or
  623. (
  624. (parraydef(p)^.lowrange=0) and
  625. (parraydef(p)^.highrange=1) and
  626. (pfloatdef(parraydef(p)^.elementtype.def)^.typ=s32real)
  627. )
  628. )
  629. )
  630. );
  631. end
  632. else
  633. begin
  634. is_mmx_able_array:=(p^.deftype=arraydef) and
  635. (
  636. (
  637. (parraydef(p)^.elementtype.def^.deftype=orddef) and
  638. (
  639. (
  640. (parraydef(p)^.lowrange=0) and
  641. (parraydef(p)^.highrange=1) and
  642. (porddef(parraydef(p)^.elementtype.def)^.typ in [u32bit,s32bit])
  643. )
  644. or
  645. (
  646. (parraydef(p)^.lowrange=0) and
  647. (parraydef(p)^.highrange=3) and
  648. (porddef(parraydef(p)^.elementtype.def)^.typ in [u16bit,s16bit])
  649. )
  650. or
  651. (
  652. (parraydef(p)^.lowrange=0) and
  653. (parraydef(p)^.highrange=7) and
  654. (porddef(parraydef(p)^.elementtype.def)^.typ in [u8bit,s8bit])
  655. )
  656. )
  657. )
  658. or
  659. (
  660. (parraydef(p)^.elementtype.def^.deftype=floatdef) and
  661. (
  662. (
  663. (parraydef(p)^.lowrange=0) and
  664. (parraydef(p)^.highrange=3) and
  665. (pfloatdef(parraydef(p)^.elementtype.def)^.typ=f32bit)
  666. )
  667. or
  668. (
  669. (parraydef(p)^.lowrange=0) and
  670. (parraydef(p)^.highrange=1) and
  671. (pfloatdef(parraydef(p)^.elementtype.def)^.typ=s32real)
  672. )
  673. )
  674. )
  675. );
  676. end;
  677. {$else SUPPORT_MMX}
  678. is_mmx_able_array:=false;
  679. {$endif SUPPORT_MMX}
  680. end;
  681. function is_equal(def1,def2 : pdef) : boolean;
  682. var
  683. b : boolean;
  684. hd : pdef;
  685. begin
  686. { both types must exists }
  687. if not (assigned(def1) and assigned(def2)) then
  688. begin
  689. is_equal:=false;
  690. exit;
  691. end;
  692. { be sure, that if there is a stringdef, that this is def1 }
  693. if def2^.deftype=stringdef then
  694. begin
  695. hd:=def1;
  696. def1:=def2;
  697. def2:=hd;
  698. end;
  699. b:=false;
  700. { both point to the same definition ? }
  701. if def1=def2 then
  702. b:=true
  703. else
  704. { pointer with an equal definition are equal }
  705. if (def1^.deftype=pointerdef) and (def2^.deftype=pointerdef) then
  706. begin
  707. { here a problem detected in tabsolutesym }
  708. { the types can be forward type !! }
  709. if assigned(def1^.typesym) and (ppointerdef(def1)^.pointertype.def^.deftype=forwarddef) then
  710. b:=(def1^.typesym=def2^.typesym)
  711. else
  712. b:=ppointerdef(def1)^.pointertype.def=ppointerdef(def2)^.pointertype.def;
  713. end
  714. else
  715. { ordinals are equal only when the ordinal type is equal }
  716. if (def1^.deftype=orddef) and (def2^.deftype=orddef) then
  717. begin
  718. case porddef(def1)^.typ of
  719. u8bit,u16bit,u32bit,
  720. s8bit,s16bit,s32bit:
  721. b:=((porddef(def1)^.typ=porddef(def2)^.typ) and
  722. (porddef(def1)^.low=porddef(def2)^.low) and
  723. (porddef(def1)^.high=porddef(def2)^.high));
  724. uvoid,uchar,
  725. bool8bit,bool16bit,bool32bit:
  726. b:=(porddef(def1)^.typ=porddef(def2)^.typ);
  727. end;
  728. end
  729. else
  730. if (def1^.deftype=floatdef) and (def2^.deftype=floatdef) then
  731. b:=pfloatdef(def1)^.typ=pfloatdef(def2)^.typ
  732. else
  733. { strings with the same length are equal }
  734. if (def1^.deftype=stringdef) and (def2^.deftype=stringdef) and
  735. (pstringdef(def1)^.string_typ=pstringdef(def2)^.string_typ) then
  736. begin
  737. b:=not(is_shortstring(def1)) or
  738. (pstringdef(def1)^.len=pstringdef(def2)^.len);
  739. end
  740. else
  741. if (def1^.deftype=formaldef) and (def2^.deftype=formaldef) then
  742. b:=true
  743. { file types with the same file element type are equal }
  744. { this is a problem for assign !! }
  745. { changed to allow if one is untyped }
  746. { all typed files are equal to the special }
  747. { typed file that has voiddef as elemnt type }
  748. { but must NOT match for text file !!! }
  749. else
  750. if (def1^.deftype=filedef) and (def2^.deftype=filedef) then
  751. b:=(pfiledef(def1)^.filetyp=pfiledef(def2)^.filetyp) and
  752. ((
  753. ((pfiledef(def1)^.typedfiletype.def=nil) and
  754. (pfiledef(def2)^.typedfiletype.def=nil)) or
  755. (
  756. (pfiledef(def1)^.typedfiletype.def<>nil) and
  757. (pfiledef(def2)^.typedfiletype.def<>nil) and
  758. is_equal(pfiledef(def1)^.typedfiletype.def,pfiledef(def2)^.typedfiletype.def)
  759. ) or
  760. ( (pfiledef(def1)^.typedfiletype.def=pdef(voiddef)) or
  761. (pfiledef(def2)^.typedfiletype.def=pdef(voiddef))
  762. )))
  763. { sets with the same element type are equal }
  764. else
  765. if (def1^.deftype=setdef) and (def2^.deftype=setdef) then
  766. begin
  767. if assigned(psetdef(def1)^.elementtype.def) and
  768. assigned(psetdef(def2)^.elementtype.def) then
  769. b:=(psetdef(def1)^.elementtype.def^.deftype=psetdef(def2)^.elementtype.def^.deftype)
  770. else
  771. b:=true;
  772. end
  773. else
  774. if (def1^.deftype=procvardef) and (def2^.deftype=procvardef) then
  775. begin
  776. { poassembler isn't important for compatibility }
  777. { if a method is assigned to a methodpointer }
  778. { is checked before }
  779. b:=(pprocvardef(def1)^.proctypeoption=pprocvardef(def2)^.proctypeoption) and
  780. (pprocvardef(def1)^.proccalloptions=pprocvardef(def2)^.proccalloptions) and
  781. ((pprocvardef(def1)^.procoptions * po_compatibility_options)=
  782. (pprocvardef(def2)^.procoptions * po_compatibility_options)) and
  783. is_equal(pprocvardef(def1)^.rettype.def,pprocvardef(def2)^.rettype.def) and
  784. equal_paras(pprocvardef(def1)^.para,pprocvardef(def2)^.para,false);
  785. end
  786. else
  787. if (def1^.deftype=arraydef) and (def2^.deftype=arraydef) then
  788. begin
  789. if is_open_array(def1) or is_open_array(def2) or
  790. is_array_of_const(def1) or is_array_of_const(def2) then
  791. begin
  792. if parraydef(def1)^.IsArrayOfConst or parraydef(def2)^.IsArrayOfConst then
  793. b:=true
  794. else
  795. b:=is_equal(parraydef(def1)^.elementtype.def,parraydef(def2)^.elementtype.def);
  796. end
  797. else
  798. begin
  799. b:=not(m_tp in aktmodeswitches) and
  800. not(m_delphi in aktmodeswitches) and
  801. (parraydef(def1)^.lowrange=parraydef(def2)^.lowrange) and
  802. (parraydef(def1)^.highrange=parraydef(def2)^.highrange) and
  803. is_equal(parraydef(def1)^.elementtype.def,parraydef(def2)^.elementtype.def) and
  804. is_equal(parraydef(def1)^.rangetype.def,parraydef(def2)^.rangetype.def);
  805. end;
  806. end
  807. else
  808. if (def1^.deftype=classrefdef) and (def2^.deftype=classrefdef) then
  809. begin
  810. { similar to pointerdef: }
  811. if assigned(def1^.typesym) and (pclassrefdef(def1)^.pointertype.def^.deftype=forwarddef) then
  812. b:=(def1^.typesym=def2^.typesym)
  813. else
  814. b:=is_equal(pclassrefdef(def1)^.pointertype.def,pclassrefdef(def2)^.pointertype.def);
  815. end;
  816. is_equal:=b;
  817. end;
  818. function is_subequal(def1, def2: pdef): boolean;
  819. Begin
  820. if assigned(def1) and assigned(def2) then
  821. Begin
  822. is_subequal := FALSE;
  823. if (def1^.deftype = orddef) and (def2^.deftype = orddef) then
  824. Begin
  825. { see p.47 of Turbo Pascal 7.01 manual for the separation of types }
  826. { range checking for case statements is done with testrange }
  827. case porddef(def1)^.typ of
  828. u8bit,u16bit,u32bit,
  829. s8bit,s16bit,s32bit :
  830. is_subequal:=(porddef(def2)^.typ in [s32bit,u32bit,u8bit,s8bit,s16bit,u16bit]);
  831. bool8bit,bool16bit,bool32bit :
  832. is_subequal:=(porddef(def2)^.typ in [bool8bit,bool16bit,bool32bit]);
  833. uchar :
  834. is_subequal:=(porddef(def2)^.typ=uchar);
  835. end;
  836. end
  837. else
  838. Begin
  839. { I assume that both enumerations are equal when the first }
  840. { pointers are equal. }
  841. if (def1^.deftype = enumdef) and (def2^.deftype =enumdef) then
  842. Begin
  843. if penumdef(def1)^.firstenum = penumdef(def2)^.firstenum then
  844. is_subequal := TRUE;
  845. end;
  846. end;
  847. end; { endif assigned ... }
  848. end;
  849. function CheckTypes(def1,def2 : pdef) : boolean;
  850. var
  851. s1,s2 : string;
  852. begin
  853. if not is_equal(def1,def2) then
  854. begin
  855. { Crash prevention }
  856. if (not assigned(def1)) or (not assigned(def2)) then
  857. Message(type_e_mismatch)
  858. else
  859. begin
  860. s1:=def1^.typename;
  861. s2:=def2^.typename;
  862. if (s1<>'<unknown type>') and (s2<>'<unknown type>') then
  863. Message2(type_e_not_equal_types,def1^.typename,def2^.typename)
  864. else
  865. Message(type_e_mismatch);
  866. end;
  867. CheckTypes:=false;
  868. end
  869. else
  870. CheckTypes:=true;
  871. end;
  872. end.
  873. {
  874. $Log$
  875. Revision 1.93 1999-12-31 14:26:28 peter
  876. * fixed crash with empty array constructors
  877. Revision 1.92 1999/11/30 10:40:59 peter
  878. + ttype, tsymlist
  879. Revision 1.91 1999/11/06 14:34:31 peter
  880. * truncated log to 20 revs
  881. Revision 1.90 1999/10/26 12:30:46 peter
  882. * const parameter is now checked
  883. * better and generic check if a node can be used for assigning
  884. * export fixes
  885. * procvar equal works now (it never had worked at least from 0.99.8)
  886. * defcoll changed to linkedlist with pparaitem so it can easily be
  887. walked both directions
  888. Revision 1.89 1999/10/01 10:04:07 peter
  889. * fixed is_equal for proc -> procvar which didn't check the
  890. callconvention and type anymore since the splitting of procoptions
  891. Revision 1.88 1999/10/01 08:02:51 peter
  892. * forward type declaration rewritten
  893. Revision 1.87 1999/09/15 22:09:27 florian
  894. + rtti is now automatically generated for published classes, i.e.
  895. they are handled like an implicit property
  896. Revision 1.86 1999/09/11 09:08:35 florian
  897. * fixed bug 596
  898. * fixed some problems with procedure variables and procedures of object,
  899. especially in TP mode. Procedure of object doesn't apply only to classes,
  900. it is also allowed for objects !!
  901. Revision 1.85 1999/08/13 21:27:08 peter
  902. * more fixes for push_addr
  903. Revision 1.84 1999/08/13 15:38:23 peter
  904. * fixed push_addr_param for records < 4, the array high<low range check
  905. broke this code.
  906. Revision 1.83 1999/08/07 14:21:06 florian
  907. * some small problems fixed
  908. Revision 1.82 1999/08/07 13:36:56 daniel
  909. * Recommitted the arraydef overflow bugfix.
  910. Revision 1.80 1999/08/05 22:42:49 daniel
  911. * Fixed potential bug for open arrays (Their size is not known at
  912. compilation time).
  913. Revision 1.79 1999/08/03 22:03:41 peter
  914. * moved bitmask constants to sets
  915. * some other type/const renamings
  916. Revision 1.78 1999/07/30 12:26:42 peter
  917. * array is_equal disabled for tp,delphi mode
  918. Revision 1.77 1999/07/29 11:41:51 peter
  919. * array is_equal extended
  920. Revision 1.76 1999/07/27 23:39:15 peter
  921. * open array checks also for s32bitdef, because u32bit also has a
  922. high range of -1
  923. Revision 1.75 1999/07/06 21:48:29 florian
  924. * a lot bug fixes:
  925. - po_external isn't any longer necessary for procedure compatibility
  926. - m_tp_procvar is in -Sd now available
  927. - error messages of procedure variables improved
  928. - return values with init./finalization fixed
  929. - data types with init./finalization aren't any longer allowed in variant
  930. record
  931. Revision 1.74 1999/07/01 15:49:24 florian
  932. * int64/qword type release
  933. + lo/hi for int64/qword
  934. Revision 1.73 1999/06/28 22:29:22 florian
  935. * qword division fixed
  936. + code for qword/int64 type casting added:
  937. range checking isn't implemented yet
  938. Revision 1.72 1999/06/13 22:41:08 peter
  939. * merged from fixes
  940. Revision 1.71.2.1 1999/06/13 22:37:17 peter
  941. * convertable para's doesn't check for equal, added equal para's to
  942. proc2procvar check
  943. Revision 1.71 1999/06/03 09:34:13 peter
  944. * better methodpointer check for proc->procvar
  945. }