defutil.pas 38 KB

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