defutil.pas 33 KB

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