defutil.pas 35 KB

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