defutil.pas 35 KB

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