defutil.pas 38 KB

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