defutil.pas 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  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,constexp,
  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):tordtype;
  39. procedure range_to_type(l,h:TConstExprInt;var def:tdef);
  40. procedure int_to_type(v:TConstExprInt;var def:tdef);
  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 Pascal-style boolean (1 = true, zero = false) }
  46. function is_pasbool(def : tdef) : boolean;
  47. {# Returns true if definition is a C-style boolean (non-zero value = true, zero = false) }
  48. function is_cbool(def : tdef) : boolean;
  49. {# Returns true if definition is a char
  50. This excludes the unicode char.
  51. }
  52. function is_char(def : tdef) : boolean;
  53. {# Returns true if definition is a widechar }
  54. function is_widechar(def : tdef) : boolean;
  55. {# Returns true if definition is a void}
  56. function is_void(def : tdef) : boolean;
  57. {# Returns true if definition is a smallset}
  58. function is_smallset(p : tdef) : boolean;
  59. {# Returns true, if def defines a signed data type
  60. (only for ordinal types)
  61. }
  62. function is_signed(def : tdef) : boolean;
  63. {# Returns true whether def_from's range is comprised in def_to's if both are
  64. orddefs, false otherwise }
  65. function is_in_limit(def_from,def_to : tdef) : boolean;
  66. { function is_in_limit_value(val_from:TConstExprInt;def_from,def_to : tdef) : boolean;}
  67. {*****************************************************************************
  68. Array helper functions
  69. *****************************************************************************}
  70. {# Returns true, if p points to a zero based (non special like open or
  71. dynamic array def).
  72. This is mainly used to see if the array
  73. is convertable to a pointer
  74. }
  75. function is_zero_based_array(p : tdef) : boolean;
  76. {# Returns true if p points to an open array definition }
  77. function is_open_array(p : tdef) : boolean;
  78. {# Returns true if p points to a dynamic array definition }
  79. function is_dynamic_array(p : tdef) : boolean;
  80. {# Returns true, if p points to an array of const definition }
  81. function is_array_constructor(p : tdef) : boolean;
  82. {# Returns true, if p points to a variant array }
  83. function is_variant_array(p : tdef) : boolean;
  84. {# Returns true, if p points to an array of const }
  85. function is_array_of_const(p : tdef) : boolean;
  86. {# Returns true, if p points any kind of special array
  87. That is if the array is an open array, a variant
  88. array, an array constants constructor, or an
  89. array of const.
  90. Bitpacked arrays aren't special in this regard though.
  91. }
  92. function is_special_array(p : tdef) : boolean;
  93. {# Returns true if p is a bitpacked array }
  94. function is_packed_array(p: tdef) : boolean;
  95. {# Returns true if p is a bitpacked record }
  96. function is_packed_record_or_object(p: tdef) : boolean;
  97. {# Returns true if p is a char array def }
  98. function is_chararray(p : tdef) : boolean;
  99. {# Returns true if p is a wide char array def }
  100. function is_widechararray(p : tdef) : boolean;
  101. {# Returns true if p is a open char array def }
  102. function is_open_chararray(p : tdef) : boolean;
  103. {# Returns true if p is a open wide char array def }
  104. function is_open_widechararray(p : tdef) : boolean;
  105. {*****************************************************************************
  106. String helper functions
  107. *****************************************************************************}
  108. {# Returns true if p points to an open string type }
  109. function is_open_string(p : tdef) : boolean;
  110. {# Returns true if p is an ansi string type }
  111. function is_ansistring(p : tdef) : boolean;
  112. {# Returns true if p is a long string type }
  113. function is_longstring(p : tdef) : boolean;
  114. {# returns true if p is a wide string type }
  115. function is_widestring(p : tdef) : boolean;
  116. {# true if p is an unicode string def }
  117. function is_unicodestring(p : tdef) : boolean;
  118. {# returns true if p is a wide or unicode string type }
  119. function is_wide_or_unicode_string(p : tdef) : boolean;
  120. {# Returns true if p is a short string type }
  121. function is_shortstring(p : tdef) : boolean;
  122. {# Returns true if p is a pchar def }
  123. function is_pchar(p : tdef) : boolean;
  124. {# Returns true if p is a pwidechar def }
  125. function is_pwidechar(p : tdef) : boolean;
  126. {# Returns true if p is a voidpointer def }
  127. function is_voidpointer(p : tdef) : boolean;
  128. {# Returns true, if definition is a float }
  129. function is_fpu(def : tdef) : boolean;
  130. {# Returns true, if def is a currency type }
  131. function is_currency(def : tdef) : boolean;
  132. {# Returns true, if def is a single type }
  133. function is_single(def : tdef) : boolean;
  134. {# Returns true, if def is a double type }
  135. function is_double(def : tdef) : boolean;
  136. {# Returns true, if def is an extended type }
  137. function is_extended(def : tdef) : boolean;
  138. {# Returns true, if definition is a "real" real (i.e. single/double/extended) }
  139. function is_real(def : tdef) : boolean;
  140. {# Returns true, if def is a 32 bit integer type }
  141. function is_32bitint(def : tdef) : boolean;
  142. {# Returns true, if def is a 64 bit integer type }
  143. function is_64bitint(def : tdef) : boolean;
  144. {# Returns true, if def is a 64 bit type }
  145. function is_64bit(def : tdef) : boolean;
  146. {# If @var(l) isn't in the range of todef a range check error (if not explicit) is generated and
  147. the value is placed within the range
  148. }
  149. procedure testrange(todef : tdef;var l : tconstexprint;explicit:boolean);
  150. {# Returns the range of def, where @var(l) is the low-range and @var(h) is
  151. the high-range.
  152. }
  153. procedure getrange(def : tdef;out l, h : TConstExprInt);
  154. { type being a vector? }
  155. function is_vector(p : tdef) : boolean;
  156. { some type helper routines for MMX support }
  157. function is_mmx_able_array(p : tdef) : boolean;
  158. {# returns the mmx type }
  159. function mmx_type(p : tdef) : tmmxtype;
  160. { returns if the passed type (array) fits into an mm register }
  161. function fits_in_mm_register(p : tdef) : boolean;
  162. {# From a definition return the abstract code generator size enum. It is
  163. to note that the value returned can be @var(OS_NO) }
  164. function def_cgsize(def: tdef): tcgsize;
  165. {# Return the definition corresponding to an abstract code generator size enum.
  166. The definition will be a base type (integer, float, vector) }
  167. function cgsize_basedef(size: tcgsize): tdef;
  168. {# returns true, if the type passed is can be used with windows automation }
  169. function is_automatable(p : tdef) : boolean;
  170. { # returns true if the procdef has no parameters and no specified return type }
  171. function is_bareprocdef(pd : tprocdef): boolean;
  172. implementation
  173. uses
  174. systems,verbose;
  175. { returns true, if def uses FPU }
  176. function is_fpu(def : tdef) : boolean;
  177. begin
  178. is_fpu:=(def.typ=floatdef);
  179. end;
  180. { returns true, if def is a currency type }
  181. function is_currency(def : tdef) : boolean;
  182. begin
  183. case s64currencytype.typ of
  184. orddef :
  185. result:=(def.typ=orddef) and
  186. (torddef(s64currencytype).ordtype=torddef(def).ordtype);
  187. floatdef :
  188. result:=(def.typ=floatdef) and
  189. (tfloatdef(s64currencytype).floattype=tfloatdef(def).floattype);
  190. else
  191. internalerror(200304222);
  192. end;
  193. end;
  194. { returns true, if def is a single type }
  195. function is_single(def : tdef) : boolean;
  196. begin
  197. result:=(def.typ=floatdef) and
  198. (tfloatdef(def).floattype=s32real);
  199. end;
  200. { returns true, if def is a double type }
  201. function is_double(def : tdef) : boolean;
  202. begin
  203. result:=(def.typ=floatdef) and
  204. (tfloatdef(def).floattype=s64real);
  205. end;
  206. function is_extended(def : tdef) : boolean;
  207. begin
  208. result:=(def.typ=floatdef) and
  209. (tfloatdef(def).floattype=s80real);
  210. end;
  211. { returns true, if definition is a "real" real (i.e. single/double/extended) }
  212. function is_real(def : tdef) : boolean;
  213. begin
  214. result:=(def.typ=floatdef) and
  215. (tfloatdef(def).floattype in [s32real,s64real,s80real]);
  216. end;
  217. function range_to_basetype(l,h:TConstExprInt):tordtype;
  218. begin
  219. { prefer signed over unsigned }
  220. if (l>=int64(-128)) and (h<=127) then
  221. range_to_basetype:=s8bit
  222. else if (l>=0) and (h<=255) then
  223. range_to_basetype:=u8bit
  224. else if (l>=int64(-32768)) and (h<=32767) then
  225. range_to_basetype:=s16bit
  226. else if (l>=0) and (h<=65535) then
  227. range_to_basetype:=u16bit
  228. else if (l>=int64(low(longint))) and (h<=high(longint)) then
  229. range_to_basetype:=s32bit
  230. else if (l>=low(cardinal)) and (h<=high(cardinal)) then
  231. range_to_basetype:=u32bit
  232. else
  233. range_to_basetype:=s64bit;
  234. end;
  235. procedure range_to_type(l,h:TConstExprInt;var def:tdef);
  236. begin
  237. { prefer signed over unsigned }
  238. if (l>=int64(-128)) and (h<=127) then
  239. def:=s8inttype
  240. else if (l>=0) and (h<=255) then
  241. def:=u8inttype
  242. else if (l>=int64(-32768)) and (h<=32767) then
  243. def:=s16inttype
  244. else if (l>=0) and (h<=65535) then
  245. def:=u16inttype
  246. else if (l>=int64(low(longint))) and (h<=high(longint)) then
  247. def:=s32inttype
  248. else if (l>=low(cardinal)) and (h<=high(cardinal)) then
  249. def:=u32inttype
  250. else if (l>=low(int64)) and (h<=high(int64)) then
  251. def:=s64inttype
  252. else
  253. def:=u64inttype;
  254. end;
  255. procedure int_to_type(v:TConstExprInt;var def:tdef);
  256. begin
  257. range_to_type(v,v,def);
  258. end;
  259. { true if p is an ordinal }
  260. function is_ordinal(def : tdef) : boolean;
  261. var
  262. dt : tordtype;
  263. begin
  264. case def.typ of
  265. orddef :
  266. begin
  267. dt:=torddef(def).ordtype;
  268. is_ordinal:=dt in [uchar,uwidechar,
  269. u8bit,u16bit,u32bit,u64bit,
  270. s8bit,s16bit,s32bit,s64bit,
  271. pasbool,bool8bit,bool16bit,bool32bit,bool64bit];
  272. end;
  273. enumdef :
  274. is_ordinal:=true;
  275. else
  276. is_ordinal:=false;
  277. end;
  278. end;
  279. { returns the min. value of the type }
  280. function get_min_value(def : tdef) : TConstExprInt;
  281. begin
  282. case def.typ of
  283. orddef:
  284. result:=torddef(def).low;
  285. enumdef:
  286. result:=int64(tenumdef(def).min);
  287. else
  288. result:=0;
  289. end;
  290. end;
  291. { returns the max. value of the type }
  292. function get_max_value(def : tdef) : TConstExprInt;
  293. begin
  294. case def.typ of
  295. orddef:
  296. result:=torddef(def).high;
  297. enumdef:
  298. result:=tenumdef(def).max;
  299. else
  300. result:=0;
  301. end;
  302. end;
  303. { true if p is an integer }
  304. function is_integer(def : tdef) : boolean;
  305. begin
  306. result:=(def.typ=orddef) and
  307. (torddef(def).ordtype in [u8bit,u16bit,u32bit,u64bit,
  308. s8bit,s16bit,s32bit,s64bit]);
  309. end;
  310. { true if p is a boolean }
  311. function is_boolean(def : tdef) : boolean;
  312. begin
  313. result:=(def.typ=orddef) and
  314. (torddef(def).ordtype in [pasbool,bool8bit,bool16bit,bool32bit,bool64bit]);
  315. end;
  316. function is_pasbool(def : tdef) : boolean;
  317. begin
  318. result:=(def.typ=orddef) and
  319. (torddef(def).ordtype = pasbool);
  320. end;
  321. { true if def is a C-style boolean (non-zero value = true, zero = false) }
  322. function is_cbool(def : tdef) : boolean;
  323. begin
  324. result:=(def.typ=orddef) and
  325. (torddef(def).ordtype in [bool8bit,bool16bit,bool32bit,bool64bit]);
  326. end;
  327. { true if p is a void }
  328. function is_void(def : tdef) : boolean;
  329. begin
  330. result:=(def.typ=orddef) and
  331. (torddef(def).ordtype=uvoid);
  332. end;
  333. { true if p is a char }
  334. function is_char(def : tdef) : boolean;
  335. begin
  336. result:=(def.typ=orddef) and
  337. (torddef(def).ordtype=uchar);
  338. end;
  339. { true if p is a wchar }
  340. function is_widechar(def : tdef) : boolean;
  341. begin
  342. result:=(def.typ=orddef) and
  343. (torddef(def).ordtype=uwidechar);
  344. end;
  345. { true if p is signed (integer) }
  346. function is_signed(def : tdef) : boolean;
  347. begin
  348. case def.typ of
  349. orddef :
  350. result:=torddef(def).low < 0;
  351. enumdef :
  352. result:=tenumdef(def).min < 0;
  353. arraydef :
  354. result:=is_signed(tarraydef(def).rangedef);
  355. else
  356. result:=false;
  357. end;
  358. end;
  359. function is_in_limit(def_from,def_to : tdef) : boolean;
  360. begin
  361. if (def_from.typ<>def_to.typ) or
  362. not(def_from.typ in [orddef,enumdef,setdef]) then
  363. begin
  364. is_in_limit := false;
  365. exit;
  366. end;
  367. case def_from.typ of
  368. orddef:
  369. is_in_limit:=(torddef(def_from).low>=torddef(def_to).low) and
  370. (torddef(def_from).high<=torddef(def_to).high);
  371. enumdef:
  372. is_in_limit:=(tenumdef(def_from).min>=tenumdef(def_to).min) and
  373. (tenumdef(def_from).max<=tenumdef(def_to).max);
  374. setdef:
  375. is_in_limit:=(tsetdef(def_from).setbase>=tsetdef(def_to).setbase) and
  376. (tsetdef(def_from).setmax<=tsetdef(def_to).setmax);
  377. end;
  378. end;
  379. { true, if p points to an open array def }
  380. function is_open_string(p : tdef) : boolean;
  381. begin
  382. is_open_string:=(p.typ=stringdef) and
  383. (tstringdef(p).stringtype=st_shortstring) and
  384. (tstringdef(p).len=0);
  385. end;
  386. { true, if p points to a zero based array def }
  387. function is_zero_based_array(p : tdef) : boolean;
  388. begin
  389. result:=(p.typ=arraydef) and
  390. (tarraydef(p).lowrange=0) and
  391. not(is_special_array(p));
  392. end;
  393. { true if p points to a dynamic array def }
  394. function is_dynamic_array(p : tdef) : boolean;
  395. begin
  396. result:=(p.typ=arraydef) and
  397. (ado_IsDynamicArray in tarraydef(p).arrayoptions);
  398. end;
  399. { true, if p points to an open array def }
  400. function is_open_array(p : tdef) : boolean;
  401. begin
  402. { check for s32inttype is needed, because for u32bit the high
  403. range is also -1 ! (PFV) }
  404. result:=(p.typ=arraydef) and
  405. (tarraydef(p).rangedef=s32inttype) and
  406. (tarraydef(p).lowrange=0) and
  407. (tarraydef(p).highrange=-1) and
  408. ((tarraydef(p).arrayoptions * [ado_IsVariant,ado_IsArrayOfConst,ado_IsConstructor,ado_IsDynamicArray])=[]);
  409. end;
  410. { true, if p points to an array of const def }
  411. function is_array_constructor(p : tdef) : boolean;
  412. begin
  413. result:=(p.typ=arraydef) and
  414. (ado_IsConstructor in tarraydef(p).arrayoptions);
  415. end;
  416. { true, if p points to a variant array }
  417. function is_variant_array(p : tdef) : boolean;
  418. begin
  419. result:=(p.typ=arraydef) and
  420. (ado_IsVariant in tarraydef(p).arrayoptions);
  421. end;
  422. { true, if p points to an array of const }
  423. function is_array_of_const(p : tdef) : boolean;
  424. begin
  425. result:=(p.typ=arraydef) and
  426. (ado_IsArrayOfConst in tarraydef(p).arrayoptions);
  427. end;
  428. { true, if p points to a special array, bitpacked arrays aren't special in this regard though }
  429. function is_special_array(p : tdef) : boolean;
  430. begin
  431. result:=(p.typ=arraydef) and
  432. (
  433. ((tarraydef(p).arrayoptions * [ado_IsVariant,ado_IsArrayOfConst,ado_IsConstructor,ado_IsDynamicArray])<>[]) or
  434. is_open_array(p)
  435. );
  436. end;
  437. { true if p is an ansi string def }
  438. function is_ansistring(p : tdef) : boolean;
  439. begin
  440. is_ansistring:=(p.typ=stringdef) and
  441. (tstringdef(p).stringtype=st_ansistring);
  442. end;
  443. { true if p is an long string def }
  444. function is_longstring(p : tdef) : boolean;
  445. begin
  446. is_longstring:=(p.typ=stringdef) and
  447. (tstringdef(p).stringtype=st_longstring);
  448. end;
  449. { true if p is an wide string def }
  450. function is_widestring(p : tdef) : boolean;
  451. begin
  452. is_widestring:=(p.typ=stringdef) and
  453. (tstringdef(p).stringtype=st_widestring);
  454. end;
  455. { true if p is an wide string def }
  456. function is_wide_or_unicode_string(p : tdef) : boolean;
  457. begin
  458. is_wide_or_unicode_string:=(p.typ=stringdef) and
  459. (tstringdef(p).stringtype in [st_widestring,st_unicodestring]);
  460. end;
  461. { true if p is an unicode string def }
  462. function is_unicodestring(p : tdef) : boolean;
  463. begin
  464. is_unicodestring:=(p.typ=stringdef) and
  465. (tstringdef(p).stringtype=st_unicodestring);
  466. end;
  467. { true if p is an short string def }
  468. function is_shortstring(p : tdef) : boolean;
  469. begin
  470. is_shortstring:=(p.typ=stringdef) and
  471. (tstringdef(p).stringtype=st_shortstring);
  472. end;
  473. { true if p is bit packed array def }
  474. function is_packed_array(p: tdef) : boolean;
  475. begin
  476. is_packed_array :=
  477. (p.typ = arraydef) and
  478. (ado_IsBitPacked in tarraydef(p).arrayoptions);
  479. end;
  480. { true if p is bit packed record def }
  481. function is_packed_record_or_object(p: tdef) : boolean;
  482. begin
  483. is_packed_record_or_object :=
  484. (p.typ in [recorddef,objectdef]) and
  485. (tabstractrecorddef(p).is_packed);
  486. end;
  487. { true if p is a char array def }
  488. function is_chararray(p : tdef) : boolean;
  489. begin
  490. is_chararray:=(p.typ=arraydef) and
  491. is_char(tarraydef(p).elementdef) and
  492. not(is_special_array(p));
  493. end;
  494. { true if p is a widechar array def }
  495. function is_widechararray(p : tdef) : boolean;
  496. begin
  497. is_widechararray:=(p.typ=arraydef) and
  498. is_widechar(tarraydef(p).elementdef) and
  499. not(is_special_array(p));
  500. end;
  501. { true if p is a open char array def }
  502. function is_open_chararray(p : tdef) : boolean;
  503. begin
  504. is_open_chararray:= is_open_array(p) and
  505. is_char(tarraydef(p).elementdef);
  506. end;
  507. { true if p is a open wide char array def }
  508. function is_open_widechararray(p : tdef) : boolean;
  509. begin
  510. is_open_widechararray:= is_open_array(p) and
  511. is_widechar(tarraydef(p).elementdef);
  512. end;
  513. { true if p is a pchar def }
  514. function is_pchar(p : tdef) : boolean;
  515. begin
  516. is_pchar:=(p.typ=pointerdef) and
  517. (is_char(tpointerdef(p).pointeddef) or
  518. (is_zero_based_array(tpointerdef(p).pointeddef) and
  519. is_chararray(tpointerdef(p).pointeddef)));
  520. end;
  521. { true if p is a pchar def }
  522. function is_pwidechar(p : tdef) : boolean;
  523. begin
  524. is_pwidechar:=(p.typ=pointerdef) and
  525. (is_widechar(tpointerdef(p).pointeddef) or
  526. (is_zero_based_array(tpointerdef(p).pointeddef) and
  527. is_widechararray(tpointerdef(p).pointeddef)));
  528. end;
  529. { true if p is a voidpointer def }
  530. function is_voidpointer(p : tdef) : boolean;
  531. begin
  532. is_voidpointer:=(p.typ=pointerdef) and
  533. (tpointerdef(p).pointeddef.typ=orddef) and
  534. (torddef(tpointerdef(p).pointeddef).ordtype=uvoid);
  535. end;
  536. { true, if def is a 32 bit int type }
  537. function is_32bitint(def : tdef) : boolean;
  538. begin
  539. result:=(def.typ=orddef) and (torddef(def).ordtype in [u32bit,s32bit])
  540. end;
  541. { true, if def is a 64 bit int type }
  542. function is_64bitint(def : tdef) : boolean;
  543. begin
  544. is_64bitint:=(def.typ=orddef) and (torddef(def).ordtype in [u64bit,s64bit])
  545. end;
  546. { true, if def is a 64 bit type }
  547. function is_64bit(def : tdef) : boolean;
  548. begin
  549. is_64bit:=(def.typ=orddef) and (torddef(def).ordtype in [u64bit,s64bit,scurrency])
  550. end;
  551. { if l isn't in the range of todef a range check error (if not explicit) is generated and
  552. the value is placed within the range }
  553. procedure testrange(todef : tdef;var l : tconstexprint;explicit:boolean);
  554. var
  555. lv,hv: TConstExprInt;
  556. begin
  557. { for 64 bit types we need only to check if it is less than }
  558. { zero, if def is a qword node }
  559. getrange(todef,lv,hv);
  560. if (l<lv) or (l>hv) then
  561. begin
  562. if not explicit then
  563. begin
  564. if ((todef.typ=enumdef) and
  565. { delphi allows range check errors in
  566. enumeration type casts FK }
  567. not(m_delphi in current_settings.modeswitches)) or
  568. (cs_check_range in current_settings.localswitches) then
  569. Message(parser_e_range_check_error)
  570. else
  571. Message(parser_w_range_check_error);
  572. end;
  573. { Fix the value to fit in the allocated space for this type of variable }
  574. case longint(todef.size) of
  575. 1: l := l and $ff;
  576. 2: l := l and $ffff;
  577. 4: l := l and $ffffffff;
  578. end;
  579. {reset sign, i.e. converting -1 to qword changes the value to high(qword)}
  580. l.signed:=false;
  581. { do sign extension if necessary (JM) }
  582. if is_signed(todef) then
  583. begin
  584. case longint(todef.size) of
  585. 1: l.svalue := shortint(l.svalue);
  586. 2: l.svalue := smallint(l.svalue);
  587. 4: l.svalue := longint(l.svalue);
  588. end;
  589. l.signed:=true;
  590. end;
  591. end;
  592. end;
  593. { return the range from def in l and h }
  594. procedure getrange(def : tdef;out l, h : TConstExprInt);
  595. begin
  596. case def.typ of
  597. orddef :
  598. begin
  599. l:=torddef(def).low;
  600. h:=torddef(def).high;
  601. end;
  602. enumdef :
  603. begin
  604. l:=int64(tenumdef(def).min);
  605. h:=int64(tenumdef(def).max);
  606. end;
  607. arraydef :
  608. begin
  609. l:=int64(tarraydef(def).lowrange);
  610. h:=int64(tarraydef(def).highrange);
  611. end;
  612. else
  613. internalerror(200611054);
  614. end;
  615. end;
  616. function mmx_type(p : tdef) : tmmxtype;
  617. begin
  618. mmx_type:=mmxno;
  619. if is_mmx_able_array(p) then
  620. begin
  621. if tarraydef(p).elementdef.typ=floatdef then
  622. case tfloatdef(tarraydef(p).elementdef).floattype of
  623. s32real:
  624. mmx_type:=mmxsingle;
  625. end
  626. else
  627. case torddef(tarraydef(p).elementdef).ordtype of
  628. u8bit:
  629. mmx_type:=mmxu8bit;
  630. s8bit:
  631. mmx_type:=mmxs8bit;
  632. u16bit:
  633. mmx_type:=mmxu16bit;
  634. s16bit:
  635. mmx_type:=mmxs16bit;
  636. u32bit:
  637. mmx_type:=mmxu32bit;
  638. s32bit:
  639. mmx_type:=mmxs32bit;
  640. end;
  641. end;
  642. end;
  643. function is_vector(p : tdef) : boolean;
  644. begin
  645. result:=(p.typ=arraydef) and
  646. not(is_special_array(p)) and
  647. (tarraydef(p).elementdef.typ=floatdef) and
  648. (tfloatdef(tarraydef(p).elementdef).floattype in [s32real,s64real]);
  649. end;
  650. { returns if the passed type (array) fits into an mm register }
  651. function fits_in_mm_register(p : tdef) : boolean;
  652. begin
  653. {$ifdef x86}
  654. result:= is_vector(p) and
  655. (
  656. (tarraydef(p).elementdef.typ=floatdef) and
  657. (
  658. (tarraydef(p).lowrange=0) and
  659. (tarraydef(p).highrange=3) and
  660. (tfloatdef(tarraydef(p).elementdef).floattype=s32real)
  661. )
  662. ) or
  663. (
  664. (tarraydef(p).elementdef.typ=floatdef) and
  665. (
  666. (tarraydef(p).lowrange=0) and
  667. (tarraydef(p).highrange=1) and
  668. (tfloatdef(tarraydef(p).elementdef).floattype=s64real)
  669. )
  670. );
  671. {$else x86}
  672. result:=false;
  673. {$endif x86}
  674. end;
  675. function is_mmx_able_array(p : tdef) : boolean;
  676. begin
  677. {$ifdef SUPPORT_MMX}
  678. if (cs_mmx_saturation in current_settings.localswitches) then
  679. begin
  680. is_mmx_able_array:=(p.typ=arraydef) and
  681. not(is_special_array(p)) and
  682. (
  683. (
  684. (tarraydef(p).elementdef.typ=orddef) and
  685. (
  686. (
  687. (tarraydef(p).lowrange=0) and
  688. (tarraydef(p).highrange=1) and
  689. (torddef(tarraydef(p).elementdef).ordtype in [u32bit,s32bit])
  690. )
  691. or
  692. (
  693. (tarraydef(p).lowrange=0) and
  694. (tarraydef(p).highrange=3) and
  695. (torddef(tarraydef(p).elementdef).ordtype in [u16bit,s16bit])
  696. )
  697. )
  698. )
  699. or
  700. (
  701. (
  702. (tarraydef(p).elementdef.typ=floatdef) and
  703. (
  704. (tarraydef(p).lowrange=0) and
  705. (tarraydef(p).highrange=1) and
  706. (tfloatdef(tarraydef(p).elementdef).floattype=s32real)
  707. )
  708. )
  709. )
  710. );
  711. end
  712. else
  713. begin
  714. is_mmx_able_array:=(p.typ=arraydef) and
  715. (
  716. (
  717. (tarraydef(p).elementdef.typ=orddef) and
  718. (
  719. (
  720. (tarraydef(p).lowrange=0) and
  721. (tarraydef(p).highrange=1) and
  722. (torddef(tarraydef(p).elementdef).ordtype in [u32bit,s32bit])
  723. )
  724. or
  725. (
  726. (tarraydef(p).lowrange=0) and
  727. (tarraydef(p).highrange=3) and
  728. (torddef(tarraydef(p).elementdef).ordtype in [u16bit,s16bit])
  729. )
  730. or
  731. (
  732. (tarraydef(p).lowrange=0) and
  733. (tarraydef(p).highrange=7) and
  734. (torddef(tarraydef(p).elementdef).ordtype in [u8bit,s8bit])
  735. )
  736. )
  737. )
  738. or
  739. (
  740. (tarraydef(p).elementdef.typ=floatdef) and
  741. (
  742. (tarraydef(p).lowrange=0) and
  743. (tarraydef(p).highrange=1) and
  744. (tfloatdef(tarraydef(p).elementdef).floattype=s32real)
  745. )
  746. )
  747. );
  748. end;
  749. {$else SUPPORT_MMX}
  750. is_mmx_able_array:=false;
  751. {$endif SUPPORT_MMX}
  752. end;
  753. function def_cgsize(def: tdef): tcgsize;
  754. begin
  755. case def.typ of
  756. orddef,
  757. enumdef,
  758. setdef:
  759. begin
  760. result:=int_cgsize(def.size);
  761. if is_signed(def) then
  762. result:=tcgsize(ord(result)+(ord(OS_S8)-ord(OS_8)));
  763. end;
  764. classrefdef,
  765. pointerdef:
  766. result := OS_ADDR;
  767. procvardef:
  768. begin
  769. if tprocvardef(def).is_methodpointer and
  770. (not tprocvardef(def).is_addressonly) then
  771. {$if sizeof(pint) = 4}
  772. result:=OS_64
  773. {$else} {$if sizeof(pint) = 8}
  774. result:=OS_128
  775. {$else}
  776. internalerror(200707141)
  777. {$endif} {$endif}
  778. else
  779. result:=OS_ADDR;
  780. end;
  781. stringdef :
  782. begin
  783. if is_ansistring(def) or is_widestring(def) then
  784. result := OS_ADDR
  785. else
  786. result:=int_cgsize(def.size);
  787. end;
  788. objectdef :
  789. begin
  790. if is_class_or_interface(def) then
  791. result := OS_ADDR
  792. else
  793. result:=int_cgsize(def.size);
  794. end;
  795. floatdef:
  796. if cs_fp_emulation in current_settings.moduleswitches then
  797. result:=int_cgsize(def.size)
  798. else
  799. result:=tfloat2tcgsize[tfloatdef(def).floattype];
  800. recorddef :
  801. result:=int_cgsize(def.size);
  802. arraydef :
  803. begin
  804. if not is_special_array(def) then
  805. result := int_cgsize(def.size)
  806. else
  807. begin
  808. if is_dynamic_array(def) then
  809. result := OS_ADDR
  810. else
  811. result := OS_NO;
  812. end;
  813. end;
  814. else
  815. begin
  816. { undefined size }
  817. result:=OS_NO;
  818. end;
  819. end;
  820. end;
  821. function cgsize_basedef(size: tcgsize): tdef;
  822. begin
  823. case size of
  824. OS_8:
  825. result:=u8inttype;
  826. OS_16:
  827. result:=u16inttype;
  828. OS_32:
  829. result:=u32inttype;
  830. OS_64:
  831. result:=u64inttype;
  832. OS_S8:
  833. result:=s8inttype;
  834. OS_S16:
  835. result:=s16inttype;
  836. OS_S32:
  837. result:=s32inttype;
  838. OS_S64:
  839. result:=s64inttype;
  840. OS_F32:
  841. result:=s32floattype;
  842. OS_F64:
  843. result:=s64floattype;
  844. OS_F80:
  845. result:=s80floattype;
  846. { rest: todo }
  847. else
  848. internalerror(2008071702);
  849. end;
  850. end;
  851. function is_automatable(p : tdef) : boolean;
  852. begin
  853. result:=false;
  854. case p.typ of
  855. orddef:
  856. result:=torddef(p).ordtype in [u8bit,s32bit,s16bit,bool16bit];
  857. floatdef:
  858. result:=tfloatdef(p).floattype in [s64currency,s64real,s32real];
  859. stringdef:
  860. result:=tstringdef(p).stringtype in [st_ansistring,st_widestring];
  861. variantdef:
  862. result:=true;
  863. arraydef:
  864. result:=(ado_IsConstString in tarraydef(p).arrayoptions);
  865. objectdef:
  866. result:=tobjectdef(p).objecttype in [odt_interfacecom,odt_dispinterface,odt_interfacecorba];
  867. end;
  868. end;
  869. {# returns true, if the type passed is a varset }
  870. function is_smallset(p : tdef) : boolean;
  871. begin
  872. result:=(p.typ=setdef) and (p.size in [1,2,4])
  873. end;
  874. function is_bareprocdef(pd : tprocdef): boolean;
  875. begin
  876. result:=(pd.maxparacount=0) and
  877. (is_void(pd.returndef) or
  878. (pd.proctypeoption = potype_constructor));
  879. end;
  880. end.