defutil.pas 38 KB

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