types.pas 38 KB

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