defutil.pas 36 KB

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