types.pas 39 KB

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