types.pas 40 KB

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