defutil.pas 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  1. {
  2. Copyright (c) 1998-2006 by Florian Klaempfl
  3. This unit provides some help routines for type handling
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit defutil;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,
  22. globtype,globals,
  23. symconst,symbase,symtype,symdef,
  24. cgbase,cpubase;
  25. type
  26. tmmxtype = (mmxno,mmxu8bit,mmxs8bit,mmxu16bit,mmxs16bit,
  27. mmxu32bit,mmxs32bit,mmxfixed16,mmxsingle);
  28. {*****************************************************************************
  29. Basic type functions
  30. *****************************************************************************}
  31. {# Returns true, if definition defines an ordinal type }
  32. function is_ordinal(def : tdef) : boolean;
  33. {# Returns the minimal integer value of the type }
  34. function get_min_value(def : tdef) : TConstExprInt;
  35. {# Returns the maximal integer value of the type }
  36. function get_max_value(def : tdef) : TConstExprInt;
  37. {# Returns basetype of the specified integer range }
  38. function range_to_basetype(l,h:TConstExprInt):tbasetype;
  39. procedure range_to_type(l,h:TConstExprInt;var tt:ttype);
  40. procedure int_to_type(v:TConstExprInt;var tt:ttype);
  41. {# Returns true, if definition defines an integer type }
  42. function is_integer(def : tdef) : boolean;
  43. {# Returns true if definition is a boolean }
  44. function is_boolean(def : tdef) : boolean;
  45. {# Returns true if definition is a char
  46. This excludes the unicode char.
  47. }
  48. function is_char(def : tdef) : boolean;
  49. {# Returns true if definition is a widechar }
  50. function is_widechar(def : tdef) : boolean;
  51. {# Returns true if definition is a void}
  52. function is_void(def : tdef) : boolean;
  53. {# Returns true if definition is a smallset}
  54. function is_smallset(p : tdef) : boolean;
  55. {# Returns true, if def defines a signed data type
  56. (only for ordinal types)
  57. }
  58. function is_signed(def : tdef) : boolean;
  59. {# Returns true whether def_from's range is comprised in def_to's if both are
  60. orddefs, false otherwise }
  61. function is_in_limit(def_from,def_to : tdef) : boolean;
  62. function is_in_limit_value(val_from:TConstExprInt;def_from,def_to : tdef) : boolean;
  63. {*****************************************************************************
  64. Array helper functions
  65. *****************************************************************************}
  66. {# Returns true, if p points to a zero based (non special like open or
  67. dynamic array def).
  68. This is mainly used to see if the array
  69. is convertable to a pointer
  70. }
  71. function is_zero_based_array(p : tdef) : boolean;
  72. {# Returns true if p points to an open array definition }
  73. function is_open_array(p : tdef) : boolean;
  74. {# Returns true if p points to a dynamic array definition }
  75. function is_dynamic_array(p : tdef) : boolean;
  76. {# Returns true, if p points to an array of const definition }
  77. function is_array_constructor(p : tdef) : boolean;
  78. {# Returns true, if p points to a variant array }
  79. function is_variant_array(p : tdef) : boolean;
  80. {# Returns true, if p points to an array of const }
  81. function is_array_of_const(p : tdef) : boolean;
  82. {# Returns true, if p points any kind of special array
  83. That is if the array is an open array, a variant
  84. array, an array constants constructor, or an
  85. array of const.
  86. Bitpacked arrays aren't special in this regard though.
  87. }
  88. function is_special_array(p : tdef) : boolean;
  89. {# Returns true if p is a bitpacked array }
  90. function is_packed_array(p: tdef) : boolean;
  91. {# Returns true if p is a bitpacked record }
  92. function is_packed_record_or_object(p: tdef) : boolean;
  93. {# Returns true if p is a char array def }
  94. function is_chararray(p : tdef) : boolean;
  95. {# Returns true if p is a wide char array def }
  96. function is_widechararray(p : tdef) : boolean;
  97. {# Returns true if p is a open char array def }
  98. function is_open_chararray(p : tdef) : boolean;
  99. {# Returns true if p is a open wide char array def }
  100. function is_open_widechararray(p : tdef) : boolean;
  101. {*****************************************************************************
  102. String helper functions
  103. *****************************************************************************}
  104. {# Returns true if p points to an open string type }
  105. function is_open_string(p : tdef) : boolean;
  106. {# Returns true if p is an ansi string type }
  107. function is_ansistring(p : tdef) : boolean;
  108. {# Returns true if p is a long string type }
  109. function is_longstring(p : tdef) : boolean;
  110. {# returns true if p is a wide string type }
  111. function is_widestring(p : tdef) : boolean;
  112. {# Returns true if p is a short string type }
  113. function is_shortstring(p : tdef) : boolean;
  114. {# Returns true if p is a pchar def }
  115. function is_pchar(p : tdef) : boolean;
  116. {# Returns true if p is a pwidechar def }
  117. function is_pwidechar(p : tdef) : boolean;
  118. {# Returns true if p is a voidpointer def }
  119. function is_voidpointer(p : tdef) : boolean;
  120. {# Returns true, if definition is a float }
  121. function is_fpu(def : tdef) : boolean;
  122. {# Returns true, if def is a currency type }
  123. function is_currency(def : tdef) : boolean;
  124. {# Returns true, if def is a single type }
  125. function is_single(def : tdef) : boolean;
  126. {# Returns true, if def is a double type }
  127. function is_double(def : tdef) : boolean;
  128. {# Returns true, if def is an extended type }
  129. function is_extended(def : tdef) : boolean;
  130. {# Returns true, if definition is a "real" real (i.e. single/double/extended) }
  131. function is_real(def : tdef) : boolean;
  132. {# Returns true, if def is a 32 bit integer type }
  133. function is_32bitint(def : tdef) : boolean;
  134. {# Returns true, if def is a 64 bit integer type }
  135. function is_64bitint(def : tdef) : boolean;
  136. {# Returns true, if def is a 64 bit type }
  137. function is_64bit(def : tdef) : boolean;
  138. {# If @var(l) isn't in the range of def a range check error (if not explicit) is generated and
  139. the value is placed within the range
  140. }
  141. procedure testrange(def : tdef;var l : tconstexprint;explicit:boolean);
  142. {# Returns the range of def, where @var(l) is the low-range and @var(h) is
  143. the high-range.
  144. }
  145. procedure getrange(def : tdef;var l : TConstExprInt;var h : TConstExprInt);
  146. { some type helper routines for MMX support }
  147. function is_mmx_able_array(p : tdef) : boolean;
  148. {# returns the mmx type }
  149. function mmx_type(p : tdef) : tmmxtype;
  150. {# From a definition return the abstract code generator size enum. It is
  151. to note that the value returned can be @var(OS_NO) }
  152. function def_cgsize(def: tdef): tcgsize;
  153. {# returns true, if the type passed is can be used with windows automation }
  154. function is_automatable(p : tdef) : boolean;
  155. implementation
  156. uses
  157. systems,verbose;
  158. { returns true, if def uses FPU }
  159. function is_fpu(def : tdef) : boolean;
  160. begin
  161. is_fpu:=(def.deftype=floatdef);
  162. end;
  163. { returns true, if def is a currency type }
  164. function is_currency(def : tdef) : boolean;
  165. begin
  166. case s64currencytype.def.deftype of
  167. orddef :
  168. result:=(def.deftype=orddef) and
  169. (torddef(s64currencytype.def).typ=torddef(def).typ);
  170. floatdef :
  171. result:=(def.deftype=floatdef) and
  172. (tfloatdef(s64currencytype.def).typ=tfloatdef(def).typ);
  173. else
  174. internalerror(200304222);
  175. end;
  176. end;
  177. { returns true, if def is a single type }
  178. function is_single(def : tdef) : boolean;
  179. begin
  180. result:=(def.deftype=floatdef) and
  181. (tfloatdef(def).typ=s32real);
  182. end;
  183. { returns true, if def is a double type }
  184. function is_double(def : tdef) : boolean;
  185. begin
  186. result:=(def.deftype=floatdef) and
  187. (tfloatdef(def).typ=s64real);
  188. end;
  189. function is_extended(def : tdef) : boolean;
  190. begin
  191. result:=(def.deftype=floatdef) and
  192. (tfloatdef(def).typ=s80real);
  193. end;
  194. { returns true, if definition is a "real" real (i.e. single/double/extended) }
  195. function is_real(def : tdef) : boolean;
  196. begin
  197. result:=(def.deftype=floatdef) and
  198. (tfloatdef(def).typ in [s32real,s64real,s80real]);
  199. end;
  200. function range_to_basetype(l,h:TConstExprInt):tbasetype;
  201. begin
  202. { prefer signed over unsigned }
  203. if (l>=-128) and (h<=127) then
  204. range_to_basetype:=s8bit
  205. else if (l>=0) and (h<=255) then
  206. range_to_basetype:=u8bit
  207. else if (l>=-32768) and (h<=32767) then
  208. range_to_basetype:=s16bit
  209. else if (l>=0) and (h<=65535) then
  210. range_to_basetype:=u16bit
  211. else if (l>=low(longint)) and (h<=high(longint)) then
  212. range_to_basetype:=s32bit
  213. else if (l>=low(cardinal)) and (h<=high(cardinal)) then
  214. range_to_basetype:=u32bit
  215. else
  216. range_to_basetype:=s64bit;
  217. end;
  218. procedure range_to_type(l,h:TConstExprInt;var tt:ttype);
  219. begin
  220. { prefer signed over unsigned }
  221. if (l>=-128) and (h<=127) then
  222. tt:=s8inttype
  223. else if (l>=0) and (h<=255) then
  224. tt:=u8inttype
  225. else if (l>=-32768) and (h<=32767) then
  226. tt:=s16inttype
  227. else if (l>=0) and (h<=65535) then
  228. tt:=u16inttype
  229. else if (l>=low(longint)) and (h<=high(longint)) then
  230. tt:=s32inttype
  231. else if (l>=low(cardinal)) and (h<=high(cardinal)) then
  232. tt:=u32inttype
  233. else
  234. tt:=s64inttype;
  235. end;
  236. procedure int_to_type(v:TConstExprInt;var tt:ttype);
  237. begin
  238. range_to_type(v,v,tt);
  239. end;
  240. { true if p is an ordinal }
  241. function is_ordinal(def : tdef) : boolean;
  242. var
  243. dt : tbasetype;
  244. begin
  245. case def.deftype of
  246. orddef :
  247. begin
  248. dt:=torddef(def).typ;
  249. is_ordinal:=dt in [uchar,uwidechar,
  250. u8bit,u16bit,u32bit,u64bit,
  251. s8bit,s16bit,s32bit,s64bit,
  252. bool8bit,bool16bit,bool32bit,bool64bit];
  253. end;
  254. enumdef :
  255. is_ordinal:=true;
  256. else
  257. is_ordinal:=false;
  258. end;
  259. end;
  260. { returns the min. value of the type }
  261. function get_min_value(def : tdef) : TConstExprInt;
  262. begin
  263. case def.deftype of
  264. orddef:
  265. get_min_value:=torddef(def).low;
  266. enumdef:
  267. get_min_value:=tenumdef(def).min;
  268. else
  269. get_min_value:=0;
  270. end;
  271. end;
  272. { returns the max. value of the type }
  273. function get_max_value(def : tdef) : TConstExprInt;
  274. begin
  275. case def.deftype of
  276. orddef:
  277. get_max_value:=torddef(def).high;
  278. enumdef:
  279. get_max_value:=tenumdef(def).max;
  280. else
  281. get_max_value:=0;
  282. end;
  283. end;
  284. { true if p is an integer }
  285. function is_integer(def : tdef) : boolean;
  286. begin
  287. is_integer:=(def.deftype=orddef) and
  288. (torddef(def).typ in [u8bit,u16bit,u32bit,u64bit,
  289. s8bit,s16bit,s32bit,s64bit]);
  290. end;
  291. { true if p is a boolean }
  292. function is_boolean(def : tdef) : boolean;
  293. begin
  294. is_boolean:=(def.deftype=orddef) and
  295. (torddef(def).typ in [bool8bit,bool16bit,bool32bit,bool64bit]);
  296. end;
  297. { true if p is a void }
  298. function is_void(def : tdef) : boolean;
  299. begin
  300. is_void:=(def.deftype=orddef) and
  301. (torddef(def).typ=uvoid);
  302. end;
  303. { true if p is a char }
  304. function is_char(def : tdef) : boolean;
  305. begin
  306. is_char:=(def.deftype=orddef) and
  307. (torddef(def).typ=uchar);
  308. end;
  309. { true if p is a wchar }
  310. function is_widechar(def : tdef) : boolean;
  311. begin
  312. is_widechar:=(def.deftype=orddef) and
  313. (torddef(def).typ=uwidechar);
  314. end;
  315. { true if p is signed (integer) }
  316. function is_signed(def : tdef) : boolean;
  317. var
  318. dt : tbasetype;
  319. begin
  320. case def.deftype of
  321. orddef :
  322. begin
  323. dt:=torddef(def).typ;
  324. is_signed:=(dt in [s8bit,s16bit,s32bit,s64bit,scurrency]);
  325. end;
  326. enumdef :
  327. is_signed:=tenumdef(def).min < 0;
  328. arraydef :
  329. is_signed:=is_signed(tarraydef(def).rangetype.def);
  330. else
  331. is_signed:=false;
  332. end;
  333. end;
  334. function is_in_limit(def_from,def_to : tdef) : boolean;
  335. var
  336. fromqword, toqword: boolean;
  337. begin
  338. if (def_from.deftype <> orddef) or
  339. (def_to.deftype <> orddef) then
  340. begin
  341. is_in_limit := false;
  342. exit;
  343. end;
  344. fromqword := torddef(def_from).typ = u64bit;
  345. toqword := torddef(def_to).typ = u64bit;
  346. is_in_limit:=(toqword and is_signed(def_from)) or
  347. ((not fromqword) and
  348. (torddef(def_from).low>=torddef(def_to).low) and
  349. (torddef(def_from).high<=torddef(def_to).high));
  350. end;
  351. function is_in_limit_value(val_from:TConstExprInt;def_from,def_to : tdef) : boolean;
  352. begin
  353. if (def_from.deftype <> orddef) and
  354. (def_to.deftype <> orddef) then
  355. internalerror(200210062);
  356. if (torddef(def_to).typ = u64bit) then
  357. begin
  358. is_in_limit_value:=((TConstExprUInt(val_from)>=TConstExprUInt(torddef(def_to).low)) and
  359. (TConstExprUInt(val_from)<=TConstExprUInt(torddef(def_to).high)));
  360. end
  361. else
  362. begin;
  363. is_in_limit_value:=((val_from>=torddef(def_to).low) and
  364. (val_from<=torddef(def_to).high));
  365. end;
  366. end;
  367. { true, if p points to an open array def }
  368. function is_open_string(p : tdef) : boolean;
  369. begin
  370. is_open_string:=(p.deftype=stringdef) and
  371. (tstringdef(p).string_typ=st_shortstring) and
  372. (tstringdef(p).len=0);
  373. end;
  374. { true, if p points to a zero based array def }
  375. function is_zero_based_array(p : tdef) : boolean;
  376. begin
  377. result:=(p.deftype=arraydef) and
  378. (tarraydef(p).lowrange=0) and
  379. not(is_special_array(p));
  380. end;
  381. { true if p points to a dynamic array def }
  382. function is_dynamic_array(p : tdef) : boolean;
  383. begin
  384. result:=(p.deftype=arraydef) and
  385. (ado_IsDynamicArray in tarraydef(p).arrayoptions);
  386. end;
  387. { true, if p points to an open array def }
  388. function is_open_array(p : tdef) : boolean;
  389. begin
  390. { check for s32inttype is needed, because for u32bit the high
  391. range is also -1 ! (PFV) }
  392. result:=(p.deftype=arraydef) and
  393. (tarraydef(p).rangetype.def=s32inttype.def) and
  394. (tarraydef(p).lowrange=0) and
  395. (tarraydef(p).highrange=-1) and
  396. ((tarraydef(p).arrayoptions * [ado_IsVariant,ado_IsArrayOfConst,ado_IsConstructor,ado_IsDynamicArray])=[]);
  397. end;
  398. { true, if p points to an array of const def }
  399. function is_array_constructor(p : tdef) : boolean;
  400. begin
  401. result:=(p.deftype=arraydef) and
  402. (ado_IsConstructor in tarraydef(p).arrayoptions);
  403. end;
  404. { true, if p points to a variant array }
  405. function is_variant_array(p : tdef) : boolean;
  406. begin
  407. result:=(p.deftype=arraydef) and
  408. (ado_IsVariant in tarraydef(p).arrayoptions);
  409. end;
  410. { true, if p points to an array of const }
  411. function is_array_of_const(p : tdef) : boolean;
  412. begin
  413. result:=(p.deftype=arraydef) and
  414. (ado_IsArrayOfConst in tarraydef(p).arrayoptions);
  415. end;
  416. { true, if p points to a special array, bitpacked arrays aren't special in this regard though }
  417. function is_special_array(p : tdef) : boolean;
  418. begin
  419. result:=(p.deftype=arraydef) and
  420. (
  421. ((tarraydef(p).arrayoptions * [ado_IsVariant,ado_IsArrayOfConst,ado_IsConstructor,ado_IsDynamicArray])<>[]) or
  422. is_open_array(p)
  423. );
  424. end;
  425. { true if p is an ansi string def }
  426. function is_ansistring(p : tdef) : boolean;
  427. begin
  428. is_ansistring:=(p.deftype=stringdef) and
  429. (tstringdef(p).string_typ=st_ansistring);
  430. end;
  431. { true if p is an long string def }
  432. function is_longstring(p : tdef) : boolean;
  433. begin
  434. is_longstring:=(p.deftype=stringdef) and
  435. (tstringdef(p).string_typ=st_longstring);
  436. end;
  437. { true if p is an wide string def }
  438. function is_widestring(p : tdef) : boolean;
  439. begin
  440. is_widestring:=(p.deftype=stringdef) and
  441. (tstringdef(p).string_typ=st_widestring);
  442. end;
  443. { true if p is an short string def }
  444. function is_shortstring(p : tdef) : boolean;
  445. begin
  446. is_shortstring:=(p.deftype=stringdef) and
  447. (tstringdef(p).string_typ=st_shortstring);
  448. end;
  449. { true if p is bit packed array def }
  450. function is_packed_array(p: tdef) : boolean;
  451. begin
  452. is_packed_array :=
  453. (p.deftype = arraydef) and
  454. (ado_IsBitPacked in tarraydef(p).arrayoptions);
  455. end;
  456. { true if p is bit packed record def }
  457. function is_packed_record_or_object(p: tdef) : boolean;
  458. begin
  459. is_packed_record_or_object :=
  460. (p.deftype in [recorddef,objectdef]) and
  461. (tabstractrecorddef(p).is_packed);
  462. end;
  463. { true if p is a char array def }
  464. function is_chararray(p : tdef) : boolean;
  465. begin
  466. is_chararray:=(p.deftype=arraydef) and
  467. is_char(tarraydef(p).elementtype.def) and
  468. not(is_special_array(p));
  469. end;
  470. { true if p is a widechar array def }
  471. function is_widechararray(p : tdef) : boolean;
  472. begin
  473. is_widechararray:=(p.deftype=arraydef) and
  474. is_widechar(tarraydef(p).elementtype.def) and
  475. not(is_special_array(p));
  476. end;
  477. { true if p is a open char array def }
  478. function is_open_chararray(p : tdef) : boolean;
  479. begin
  480. is_open_chararray:= is_open_array(p) and
  481. is_char(tarraydef(p).elementtype.def);
  482. end;
  483. { true if p is a open wide char array def }
  484. function is_open_widechararray(p : tdef) : boolean;
  485. begin
  486. is_open_widechararray:= is_open_array(p) and
  487. is_widechar(tarraydef(p).elementtype.def);
  488. end;
  489. { true if p is a pchar def }
  490. function is_pchar(p : tdef) : boolean;
  491. begin
  492. is_pchar:=(p.deftype=pointerdef) and
  493. (is_char(tpointerdef(p).pointertype.def) or
  494. (is_zero_based_array(tpointerdef(p).pointertype.def) and
  495. is_chararray(tpointerdef(p).pointertype.def)));
  496. end;
  497. { true if p is a pchar def }
  498. function is_pwidechar(p : tdef) : boolean;
  499. begin
  500. is_pwidechar:=(p.deftype=pointerdef) and
  501. (is_widechar(tpointerdef(p).pointertype.def) or
  502. (is_zero_based_array(tpointerdef(p).pointertype.def) and
  503. is_widechararray(tpointerdef(p).pointertype.def)));
  504. end;
  505. { true if p is a voidpointer def }
  506. function is_voidpointer(p : tdef) : boolean;
  507. begin
  508. is_voidpointer:=(p.deftype=pointerdef) and
  509. (tpointerdef(p).pointertype.def.deftype=orddef) and
  510. (torddef(tpointerdef(p).pointertype.def).typ=uvoid);
  511. end;
  512. { true if p is a smallset def }
  513. function is_smallset(p : tdef) : boolean;
  514. begin
  515. is_smallset:=(p.deftype=setdef) and
  516. (tsetdef(p).settype=smallset);
  517. end;
  518. { true, if def is a 32 bit int type }
  519. function is_32bitint(def : tdef) : boolean;
  520. begin
  521. result:=(def.deftype=orddef) and (torddef(def).typ in [u32bit,s32bit])
  522. end;
  523. { true, if def is a 64 bit int type }
  524. function is_64bitint(def : tdef) : boolean;
  525. begin
  526. is_64bitint:=(def.deftype=orddef) and (torddef(def).typ in [u64bit,s64bit])
  527. end;
  528. { true, if def is a 64 bit type }
  529. function is_64bit(def : tdef) : boolean;
  530. begin
  531. is_64bit:=(def.deftype=orddef) and (torddef(def).typ in [u64bit,s64bit,scurrency])
  532. end;
  533. { if l isn't in the range of def a range check error (if not explicit) is generated and
  534. the value is placed within the range }
  535. procedure testrange(def : tdef;var l : tconstexprint;explicit:boolean);
  536. var
  537. lv,hv: TConstExprInt;
  538. error: boolean;
  539. begin
  540. error := false;
  541. { for 64 bit types we need only to check if it is less than }
  542. { zero, if def is a qword node }
  543. if is_64bitint(def) then
  544. begin
  545. if (l<0) and (torddef(def).typ=u64bit) then
  546. begin
  547. { don't zero the result, because it may come from hex notation
  548. like $ffffffffffffffff! (JM)
  549. l:=0; }
  550. if not explicit then
  551. begin
  552. if (cs_check_range in aktlocalswitches) then
  553. Message(parser_e_range_check_error)
  554. else
  555. Message(parser_w_range_check_error);
  556. end;
  557. error := true;
  558. end;
  559. end
  560. else
  561. begin
  562. getrange(def,lv,hv);
  563. if (l<lv) or (l>hv) then
  564. begin
  565. if not explicit then
  566. begin
  567. if ((def.deftype=enumdef) and
  568. { delphi allows range check errors in
  569. enumeration type casts FK }
  570. not(m_delphi in aktmodeswitches)) or
  571. (cs_check_range in aktlocalswitches) then
  572. Message(parser_e_range_check_error)
  573. else
  574. Message(parser_w_range_check_error);
  575. end;
  576. error := true;
  577. end;
  578. end;
  579. if error then
  580. begin
  581. { Fix the value to fit in the allocated space for this type of variable }
  582. case longint(def.size) of
  583. 1: l := l and $ff;
  584. 2: l := l and $ffff;
  585. { work around sign extension bug (to be fixed) (JM) }
  586. 4: l := l and (int64($fffffff) shl 4 + $f);
  587. end;
  588. { do sign extension if necessary (JM) }
  589. if is_signed(def) then
  590. begin
  591. case longint(def.size) of
  592. 1: l := shortint(l);
  593. 2: l := smallint(l);
  594. 4: l := longint(l);
  595. end;
  596. end;
  597. end;
  598. end;
  599. { return the range from def in l and h }
  600. procedure getrange(def : tdef;var l : TConstExprInt;var h : TConstExprInt);
  601. begin
  602. case def.deftype of
  603. orddef :
  604. begin
  605. l:=torddef(def).low;
  606. h:=torddef(def).high;
  607. end;
  608. enumdef :
  609. begin
  610. l:=tenumdef(def).min;
  611. h:=tenumdef(def).max;
  612. end;
  613. arraydef :
  614. begin
  615. l:=tarraydef(def).lowrange;
  616. h:=tarraydef(def).highrange;
  617. end;
  618. else
  619. internalerror(987);
  620. end;
  621. end;
  622. function mmx_type(p : tdef) : tmmxtype;
  623. begin
  624. mmx_type:=mmxno;
  625. if is_mmx_able_array(p) then
  626. begin
  627. if tarraydef(p).elementtype.def.deftype=floatdef then
  628. case tfloatdef(tarraydef(p).elementtype.def).typ of
  629. s32real:
  630. mmx_type:=mmxsingle;
  631. end
  632. else
  633. case torddef(tarraydef(p).elementtype.def).typ of
  634. u8bit:
  635. mmx_type:=mmxu8bit;
  636. s8bit:
  637. mmx_type:=mmxs8bit;
  638. u16bit:
  639. mmx_type:=mmxu16bit;
  640. s16bit:
  641. mmx_type:=mmxs16bit;
  642. u32bit:
  643. mmx_type:=mmxu32bit;
  644. s32bit:
  645. mmx_type:=mmxs32bit;
  646. end;
  647. end;
  648. end;
  649. function is_mmx_able_array(p : tdef) : boolean;
  650. begin
  651. {$ifdef SUPPORT_MMX}
  652. if (cs_mmx_saturation in aktlocalswitches) then
  653. begin
  654. is_mmx_able_array:=(p.deftype=arraydef) and
  655. not(is_special_array(p)) and
  656. (
  657. (
  658. (tarraydef(p).elementtype.def.deftype=orddef) and
  659. (
  660. (
  661. (tarraydef(p).lowrange=0) and
  662. (tarraydef(p).highrange=1) and
  663. (torddef(tarraydef(p).elementtype.def).typ in [u32bit,s32bit])
  664. )
  665. or
  666. (
  667. (tarraydef(p).lowrange=0) and
  668. (tarraydef(p).highrange=3) and
  669. (torddef(tarraydef(p).elementtype.def).typ in [u16bit,s16bit])
  670. )
  671. )
  672. )
  673. or
  674. (
  675. (
  676. (tarraydef(p).elementtype.def.deftype=floatdef) and
  677. (
  678. (tarraydef(p).lowrange=0) and
  679. (tarraydef(p).highrange=1) and
  680. (tfloatdef(tarraydef(p).elementtype.def).typ=s32real)
  681. )
  682. )
  683. )
  684. );
  685. end
  686. else
  687. begin
  688. is_mmx_able_array:=(p.deftype=arraydef) and
  689. (
  690. (
  691. (tarraydef(p).elementtype.def.deftype=orddef) and
  692. (
  693. (
  694. (tarraydef(p).lowrange=0) and
  695. (tarraydef(p).highrange=1) and
  696. (torddef(tarraydef(p).elementtype.def).typ in [u32bit,s32bit])
  697. )
  698. or
  699. (
  700. (tarraydef(p).lowrange=0) and
  701. (tarraydef(p).highrange=3) and
  702. (torddef(tarraydef(p).elementtype.def).typ in [u16bit,s16bit])
  703. )
  704. or
  705. (
  706. (tarraydef(p).lowrange=0) and
  707. (tarraydef(p).highrange=7) and
  708. (torddef(tarraydef(p).elementtype.def).typ in [u8bit,s8bit])
  709. )
  710. )
  711. )
  712. or
  713. (
  714. (tarraydef(p).elementtype.def.deftype=floatdef) and
  715. (
  716. (tarraydef(p).lowrange=0) and
  717. (tarraydef(p).highrange=1) and
  718. (tfloatdef(tarraydef(p).elementtype.def).typ=s32real)
  719. )
  720. )
  721. );
  722. end;
  723. {$else SUPPORT_MMX}
  724. is_mmx_able_array:=false;
  725. {$endif SUPPORT_MMX}
  726. end;
  727. function def_cgsize(def: tdef): tcgsize;
  728. begin
  729. case def.deftype of
  730. orddef,
  731. enumdef,
  732. setdef:
  733. begin
  734. result:=int_cgsize(def.size);
  735. if is_signed(def) then
  736. result:=tcgsize(ord(result)+(ord(OS_S8)-ord(OS_8)));
  737. end;
  738. classrefdef,
  739. pointerdef:
  740. result := OS_ADDR;
  741. procvardef:
  742. begin
  743. if tprocvardef(def).is_methodpointer and
  744. (not tprocvardef(def).is_addressonly) then
  745. result := OS_64
  746. else
  747. result := OS_ADDR;
  748. end;
  749. stringdef :
  750. begin
  751. if is_ansistring(def) or is_widestring(def) then
  752. result := OS_ADDR
  753. else
  754. result:=int_cgsize(def.size);
  755. end;
  756. objectdef :
  757. begin
  758. if is_class_or_interface(def) then
  759. result := OS_ADDR
  760. else
  761. result:=int_cgsize(def.size);
  762. end;
  763. floatdef:
  764. if cs_fp_emulation in aktmoduleswitches then
  765. result:=int_cgsize(def.size)
  766. else
  767. result:=tfloat2tcgsize[tfloatdef(def).typ];
  768. recorddef :
  769. result:=int_cgsize(def.size);
  770. arraydef :
  771. begin
  772. if not is_special_array(def) then
  773. result := int_cgsize(def.size)
  774. else
  775. begin
  776. if is_dynamic_array(def) then
  777. result := OS_ADDR
  778. else
  779. result := OS_NO;
  780. end;
  781. end;
  782. else
  783. begin
  784. { undefined size }
  785. result:=OS_NO;
  786. end;
  787. end;
  788. end;
  789. function is_automatable(p : tdef) : boolean;
  790. begin
  791. result:=false;
  792. case p.deftype of
  793. orddef:
  794. result:=torddef(p).typ in [u8bit,s32bit,s16bit,bool16bit];
  795. floatdef:
  796. result:=tfloatdef(p).typ in [s64currency,s64real,s32real];
  797. stringdef:
  798. result:=tstringdef(p).string_typ in [st_shortstring,st_ansistring];
  799. variantdef:
  800. result:=true;
  801. end;
  802. end;
  803. end.