defutil.pas 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194
  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 True, if definition defines a type that behaves like a string,
  36. namely that can be joined and compared with another string-like type }
  37. function is_stringlike(def : tdef) : boolean;
  38. {# Returns True, if definition defines an enumeration type }
  39. function is_enum(def : tdef) : boolean;
  40. {# Returns True, if definition defines a set type }
  41. function is_set(def : tdef) : boolean;
  42. {# Returns the minimal integer value of the type }
  43. function get_min_value(def : tdef) : TConstExprInt;
  44. {# Returns the maximal integer value of the type }
  45. function get_max_value(def : tdef) : TConstExprInt;
  46. {# Returns basetype of the specified integer range }
  47. function range_to_basetype(l,h:TConstExprInt):tordtype;
  48. procedure range_to_type(l,h:TConstExprInt;var def:tdef);
  49. procedure int_to_type(v:TConstExprInt;var def:tdef);
  50. {# Returns true, if definition defines an integer type }
  51. function is_integer(def : tdef) : boolean;
  52. {# Returns true if definition is a boolean }
  53. function is_boolean(def : tdef) : boolean;
  54. {# Returns true if definition is a Pascal-style boolean (1 = true, zero = false) }
  55. function is_pasbool(def : tdef) : boolean;
  56. {# Returns true if definition is a C-style boolean (non-zero value = true, zero = false) }
  57. function is_cbool(def : tdef) : boolean;
  58. {# Returns true if definition is a char
  59. This excludes the unicode char.
  60. }
  61. function is_char(def : tdef) : boolean;
  62. {# Returns true if definition is a widechar }
  63. function is_widechar(def : tdef) : boolean;
  64. {# Returns true if definition is either an AnsiChar or a WideChar }
  65. function is_anychar(def : tdef) : boolean;
  66. {# Returns true if definition is a void}
  67. function is_void(def : tdef) : boolean;
  68. {# Returns true if definition is a smallset}
  69. function is_smallset(p : tdef) : boolean;
  70. {# Returns true, if def defines a signed data type
  71. (only for ordinal types)
  72. }
  73. function is_signed(def : tdef) : boolean;
  74. {# Returns whether def_from's range is comprised in def_to's if both are
  75. orddefs, false otherwise }
  76. function is_in_limit(def_from,def_to : tdef) : boolean;
  77. {# Returns whether def is reference counted }
  78. function is_managed_type(def: tdef) : boolean;{$ifdef USEINLINE}inline;{$endif}
  79. { function is_in_limit_value(val_from:TConstExprInt;def_from,def_to : tdef) : boolean;}
  80. {*****************************************************************************
  81. Array helper functions
  82. *****************************************************************************}
  83. {# Returns true, if p points to a zero based (non special like open or
  84. dynamic array def).
  85. This is mainly used to see if the array
  86. is convertable to a pointer
  87. }
  88. function is_zero_based_array(p : tdef) : boolean;
  89. {# Returns true if p points to an open array definition }
  90. function is_open_array(p : tdef) : boolean;
  91. {# Returns true if p points to a dynamic array definition }
  92. function is_dynamic_array(p : tdef) : boolean;
  93. {# Returns true, if p points to an array of const definition }
  94. function is_array_constructor(p : tdef) : boolean;
  95. {# Returns true, if p points to a variant array }
  96. function is_variant_array(p : tdef) : boolean;
  97. {# Returns true, if p points to an array of const }
  98. function is_array_of_const(p : tdef) : boolean;
  99. {# Returns true, if p points any kind of special array
  100. That is if the array is an open array, a variant
  101. array, an array constants constructor, or an
  102. array of const.
  103. Bitpacked arrays aren't special in this regard though.
  104. }
  105. function is_special_array(p : tdef) : boolean;
  106. {# Returns true if p is a bitpacked array }
  107. function is_packed_array(p: tdef) : boolean;
  108. {# Returns true if p is a bitpacked record }
  109. function is_packed_record_or_object(p: tdef) : boolean;
  110. {# Returns true if p is a char array def }
  111. function is_chararray(p : tdef) : boolean;
  112. {# Returns true if p is a wide char array def }
  113. function is_widechararray(p : tdef) : boolean;
  114. {# Returns true if p is a open char array def }
  115. function is_open_chararray(p : tdef) : boolean;
  116. {# Returns true if p is a open wide char array def }
  117. function is_open_widechararray(p : tdef) : boolean;
  118. {*****************************************************************************
  119. String helper functions
  120. *****************************************************************************}
  121. {# Returns true if p points to an open string type }
  122. function is_open_string(p : tdef) : boolean;
  123. {# Returns true if p is an ansi string type }
  124. function is_ansistring(p : tdef) : boolean;
  125. {# Returns true if p is an ansi string type with codepage 0 }
  126. function is_rawbytestring(p : tdef) : boolean;
  127. {# Returns true if p is a long string type }
  128. function is_longstring(p : tdef) : boolean;
  129. {# returns true if p is a wide string type }
  130. function is_widestring(p : tdef) : boolean;
  131. {# true if p is an unicode string def }
  132. function is_unicodestring(p : tdef) : boolean;
  133. {# returns true if p is a wide or unicode string type }
  134. function is_wide_or_unicode_string(p : tdef) : boolean;
  135. {# Returns true if p is a short string type }
  136. function is_shortstring(p : tdef) : boolean;
  137. {# Returns true if p is a pchar def }
  138. function is_pchar(p : tdef) : boolean;
  139. {# Returns true if p is a pwidechar def }
  140. function is_pwidechar(p : tdef) : boolean;
  141. {# Returns true if p is a voidpointer def }
  142. function is_voidpointer(p : tdef) : boolean;
  143. {# Returns true, if definition is a float }
  144. function is_fpu(def : tdef) : boolean;
  145. {# Returns true, if def is a currency type }
  146. function is_currency(def : tdef) : boolean;
  147. {# Returns true, if def is a single type }
  148. function is_single(def : tdef) : boolean;
  149. {# Returns true, if def is a double type }
  150. function is_double(def : tdef) : boolean;
  151. {# Returns true, if def is an extended type }
  152. function is_extended(def : tdef) : boolean;
  153. {# Returns true, if definition is a "real" real (i.e. single/double/extended) }
  154. function is_real(def : tdef) : boolean;
  155. {# Returns true, if def is a 32 bit integer type }
  156. function is_32bitint(def : tdef) : boolean;
  157. {# Returns true, if def is a 64 bit integer type }
  158. function is_64bitint(def : tdef) : boolean;
  159. {# Returns true, if def is a 64 bit type }
  160. function is_64bit(def : tdef) : boolean;
  161. {# If @var(l) isn't in the range of todef a range check error (if not explicit) is generated and
  162. the value is placed within the range
  163. }
  164. procedure testrange(todef : tdef;var l : tconstexprint;explicit,forcerangecheck:boolean);
  165. {# Returns the range of def, where @var(l) is the low-range and @var(h) is
  166. the high-range.
  167. }
  168. procedure getrange(def : tdef;out l, h : TConstExprInt);
  169. { type being a vector? }
  170. function is_vector(p : tdef) : boolean;
  171. { some type helper routines for MMX support }
  172. function is_mmx_able_array(p : tdef) : boolean;
  173. {# returns the mmx type }
  174. function mmx_type(p : tdef) : tmmxtype;
  175. { returns if the passed type (array) fits into an mm register }
  176. function fits_in_mm_register(p : tdef) : boolean;
  177. {# From a definition return the abstract code generator size enum. It is
  178. to note that the value returned can be @var(OS_NO) }
  179. function def_cgsize(def: tdef): tcgsize;
  180. {# returns true, if the type passed is can be used with windows automation }
  181. function is_automatable(p : tdef) : boolean;
  182. { # returns true if the procdef has no parameters and no specified return type }
  183. function is_bareprocdef(pd : tprocdef): boolean;
  184. { # returns the smallest base integer type whose range encompasses that of
  185. both ld and rd; if keep_sign_if_equal, then if ld and rd have the same
  186. signdness, the result will also get that signdness }
  187. function get_common_intdef(ld, rd: torddef; keep_sign_if_equal: boolean): torddef;
  188. { # returns whether the type is potentially a valid type of/for an "univ" parameter
  189. (basically: it must have a compile-time size) }
  190. function is_valid_univ_para_type(def: tdef): boolean;
  191. { # returns whether the procdef/procvardef represents a nested procedure
  192. or not }
  193. function is_nested_pd(def: tabstractprocdef): boolean;{$ifdef USEINLINE}inline;{$endif}
  194. { # returns whether def is a type parameter of a generic }
  195. function is_typeparam(def : tdef) : boolean;{$ifdef USEINLINE}inline;{$endif}
  196. implementation
  197. uses
  198. systems,verbose;
  199. { returns true, if def uses FPU }
  200. function is_fpu(def : tdef) : boolean;
  201. begin
  202. is_fpu:=(def.typ=floatdef);
  203. end;
  204. { returns true, if def is a currency type }
  205. function is_currency(def : tdef) : boolean;
  206. begin
  207. case s64currencytype.typ of
  208. orddef :
  209. result:=(def.typ=orddef) and
  210. (torddef(s64currencytype).ordtype=torddef(def).ordtype);
  211. floatdef :
  212. result:=(def.typ=floatdef) and
  213. (tfloatdef(s64currencytype).floattype=tfloatdef(def).floattype);
  214. else
  215. internalerror(200304222);
  216. end;
  217. end;
  218. { returns true, if def is a single type }
  219. function is_single(def : tdef) : boolean;
  220. begin
  221. result:=(def.typ=floatdef) and
  222. (tfloatdef(def).floattype=s32real);
  223. end;
  224. { returns true, if def is a double type }
  225. function is_double(def : tdef) : boolean;
  226. begin
  227. result:=(def.typ=floatdef) and
  228. (tfloatdef(def).floattype=s64real);
  229. end;
  230. function is_extended(def : tdef) : boolean;
  231. begin
  232. result:=(def.typ=floatdef) and
  233. (tfloatdef(def).floattype in [s80real,sc80real]);
  234. end;
  235. { returns true, if definition is a "real" real (i.e. single/double/extended) }
  236. function is_real(def : tdef) : boolean;
  237. begin
  238. result:=(def.typ=floatdef) and
  239. (tfloatdef(def).floattype in [s32real,s64real,s80real]);
  240. end;
  241. function range_to_basetype(l,h:TConstExprInt):tordtype;
  242. begin
  243. { prefer signed over unsigned }
  244. if (l>=int64(-128)) and (h<=127) then
  245. range_to_basetype:=s8bit
  246. else if (l>=0) and (h<=255) then
  247. range_to_basetype:=u8bit
  248. else if (l>=int64(-32768)) and (h<=32767) then
  249. range_to_basetype:=s16bit
  250. else if (l>=0) and (h<=65535) then
  251. range_to_basetype:=u16bit
  252. else if (l>=int64(low(longint))) and (h<=high(longint)) then
  253. range_to_basetype:=s32bit
  254. else if (l>=low(cardinal)) and (h<=high(cardinal)) then
  255. range_to_basetype:=u32bit
  256. else
  257. range_to_basetype:=s64bit;
  258. end;
  259. procedure range_to_type(l,h:TConstExprInt;var def:tdef);
  260. begin
  261. { prefer signed over unsigned }
  262. if (l>=int64(-128)) and (h<=127) then
  263. def:=s8inttype
  264. else if (l>=0) and (h<=255) then
  265. def:=u8inttype
  266. else if (l>=int64(-32768)) and (h<=32767) then
  267. def:=s16inttype
  268. else if (l>=0) and (h<=65535) then
  269. def:=u16inttype
  270. else if (l>=int64(low(longint))) and (h<=high(longint)) then
  271. def:=s32inttype
  272. else if (l>=low(cardinal)) and (h<=high(cardinal)) then
  273. def:=u32inttype
  274. else if (l>=low(int64)) and (h<=high(int64)) then
  275. def:=s64inttype
  276. else
  277. def:=u64inttype;
  278. end;
  279. procedure int_to_type(v:TConstExprInt;var def:tdef);
  280. begin
  281. range_to_type(v,v,def);
  282. end;
  283. { true if p is an ordinal }
  284. function is_ordinal(def : tdef) : boolean;
  285. var
  286. dt : tordtype;
  287. begin
  288. case def.typ of
  289. orddef :
  290. begin
  291. dt:=torddef(def).ordtype;
  292. is_ordinal:=dt in [uchar,uwidechar,
  293. u8bit,u16bit,u32bit,u64bit,
  294. s8bit,s16bit,s32bit,s64bit,
  295. pasbool8,pasbool16,pasbool32,pasbool64,
  296. bool8bit,bool16bit,bool32bit,bool64bit];
  297. end;
  298. enumdef :
  299. is_ordinal:=true;
  300. else
  301. is_ordinal:=false;
  302. end;
  303. end;
  304. { true if p is a string }
  305. function is_string(def : tdef) : boolean;
  306. begin
  307. is_string := (assigned(def) and (def.typ = stringdef));
  308. end;
  309. function is_stringlike(def : tdef) : boolean;
  310. begin
  311. result := is_string(def) or
  312. is_anychar(def) or
  313. is_pchar(def) or
  314. is_pwidechar(def) or
  315. is_chararray(def) or
  316. is_widechararray(def) or
  317. is_open_chararray(def) or
  318. is_open_widechararray(def);
  319. end;
  320. function is_enum(def : tdef) : boolean;
  321. begin
  322. result:=def.typ=enumdef;
  323. end;
  324. function is_set(def : tdef) : boolean;
  325. begin
  326. result:=def.typ=setdef;
  327. end;
  328. { returns the min. value of the type }
  329. function get_min_value(def : tdef) : TConstExprInt;
  330. begin
  331. case def.typ of
  332. orddef:
  333. result:=torddef(def).low;
  334. enumdef:
  335. result:=int64(tenumdef(def).min);
  336. else
  337. result:=0;
  338. end;
  339. end;
  340. { returns the max. value of the type }
  341. function get_max_value(def : tdef) : TConstExprInt;
  342. begin
  343. case def.typ of
  344. orddef:
  345. result:=torddef(def).high;
  346. enumdef:
  347. result:=tenumdef(def).max;
  348. else
  349. result:=0;
  350. end;
  351. end;
  352. { true if p is an integer }
  353. function is_integer(def : tdef) : boolean;
  354. begin
  355. result:=(def.typ=orddef) and
  356. (torddef(def).ordtype in [u8bit,u16bit,u32bit,u64bit,
  357. s8bit,s16bit,s32bit,s64bit]);
  358. end;
  359. { true if p is a boolean }
  360. function is_boolean(def : tdef) : boolean;
  361. begin
  362. result:=(def.typ=orddef) and
  363. (torddef(def).ordtype in [pasbool8,pasbool16,pasbool32,pasbool64,bool8bit,bool16bit,bool32bit,bool64bit]);
  364. end;
  365. function is_pasbool(def : tdef) : boolean;
  366. begin
  367. result:=(def.typ=orddef) and
  368. (torddef(def).ordtype in [pasbool8,pasbool16,pasbool32,pasbool64]);
  369. end;
  370. { true if def is a C-style boolean (non-zero value = true, zero = false) }
  371. function is_cbool(def : tdef) : boolean;
  372. begin
  373. result:=(def.typ=orddef) and
  374. (torddef(def).ordtype in [bool8bit,bool16bit,bool32bit,bool64bit]);
  375. end;
  376. { true if p is a void }
  377. function is_void(def : tdef) : boolean;
  378. begin
  379. result:=(def.typ=orddef) and
  380. (torddef(def).ordtype=uvoid);
  381. end;
  382. { true if p is a char }
  383. function is_char(def : tdef) : boolean;
  384. begin
  385. result:=(def.typ=orddef) and
  386. (torddef(def).ordtype=uchar);
  387. end;
  388. { true if p is a wchar }
  389. function is_widechar(def : tdef) : boolean;
  390. begin
  391. result:=(def.typ=orddef) and
  392. (torddef(def).ordtype=uwidechar);
  393. end;
  394. { true if p is a char or wchar }
  395. function is_anychar(def : tdef) : boolean;
  396. begin
  397. result:=(def.typ=orddef) and
  398. (torddef(def).ordtype in [uchar,uwidechar])
  399. end;
  400. { true if p is signed (integer) }
  401. function is_signed(def : tdef) : boolean;
  402. begin
  403. case def.typ of
  404. orddef :
  405. result:=torddef(def).low < 0;
  406. enumdef :
  407. result:=tenumdef(def).min < 0;
  408. arraydef :
  409. result:=is_signed(tarraydef(def).rangedef);
  410. else
  411. result:=false;
  412. end;
  413. end;
  414. function is_in_limit(def_from,def_to : tdef) : boolean;
  415. begin
  416. if (def_from.typ<>def_to.typ) or
  417. not(def_from.typ in [orddef,enumdef,setdef]) then
  418. begin
  419. is_in_limit := false;
  420. exit;
  421. end;
  422. case def_from.typ of
  423. orddef:
  424. is_in_limit:=(torddef(def_from).low>=torddef(def_to).low) and
  425. (torddef(def_from).high<=torddef(def_to).high);
  426. enumdef:
  427. is_in_limit:=(tenumdef(def_from).min>=tenumdef(def_to).min) and
  428. (tenumdef(def_from).max<=tenumdef(def_to).max);
  429. setdef:
  430. is_in_limit:=(tsetdef(def_from).setbase>=tsetdef(def_to).setbase) and
  431. (tsetdef(def_from).setmax<=tsetdef(def_to).setmax);
  432. else
  433. is_in_limit:=false;
  434. end;
  435. end;
  436. function is_managed_type(def: tdef): boolean;{$ifdef USEINLINE}inline;{$endif}
  437. begin
  438. result:=def.needs_inittable;
  439. end;
  440. { true, if p points to an open array def }
  441. function is_open_string(p : tdef) : boolean;
  442. begin
  443. is_open_string:=(p.typ=stringdef) and
  444. (tstringdef(p).stringtype=st_shortstring) and
  445. (tstringdef(p).len=0);
  446. end;
  447. { true, if p points to a zero based array def }
  448. function is_zero_based_array(p : tdef) : boolean;
  449. begin
  450. result:=(p.typ=arraydef) and
  451. (tarraydef(p).lowrange=0) and
  452. not(is_special_array(p));
  453. end;
  454. { true if p points to a dynamic array def }
  455. function is_dynamic_array(p : tdef) : boolean;
  456. begin
  457. result:=(p.typ=arraydef) and
  458. (ado_IsDynamicArray in tarraydef(p).arrayoptions);
  459. end;
  460. { true, if p points to an open array def }
  461. function is_open_array(p : tdef) : boolean;
  462. begin
  463. { check for s32inttype is needed, because for u32bit the high
  464. range is also -1 ! (PFV) }
  465. result:=(p.typ=arraydef) and
  466. (tarraydef(p).rangedef=s32inttype) and
  467. (tarraydef(p).lowrange=0) and
  468. (tarraydef(p).highrange=-1) and
  469. ((tarraydef(p).arrayoptions * [ado_IsVariant,ado_IsArrayOfConst,ado_IsConstructor,ado_IsDynamicArray])=[]);
  470. end;
  471. { true, if p points to an array of const def }
  472. function is_array_constructor(p : tdef) : boolean;
  473. begin
  474. result:=(p.typ=arraydef) and
  475. (ado_IsConstructor in tarraydef(p).arrayoptions);
  476. end;
  477. { true, if p points to a variant array }
  478. function is_variant_array(p : tdef) : boolean;
  479. begin
  480. result:=(p.typ=arraydef) and
  481. (ado_IsVariant in tarraydef(p).arrayoptions);
  482. end;
  483. { true, if p points to an array of const }
  484. function is_array_of_const(p : tdef) : boolean;
  485. begin
  486. result:=(p.typ=arraydef) and
  487. (ado_IsArrayOfConst in tarraydef(p).arrayoptions);
  488. end;
  489. { true, if p points to a special array, bitpacked arrays aren't special in this regard though }
  490. function is_special_array(p : tdef) : boolean;
  491. begin
  492. result:=(p.typ=arraydef) and
  493. (
  494. ((tarraydef(p).arrayoptions * [ado_IsVariant,ado_IsArrayOfConst,ado_IsConstructor,ado_IsDynamicArray])<>[]) or
  495. is_open_array(p)
  496. );
  497. end;
  498. { true if p is an ansi string def }
  499. function is_ansistring(p : tdef) : boolean;
  500. begin
  501. is_ansistring:=(p.typ=stringdef) and
  502. (tstringdef(p).stringtype=st_ansistring);
  503. end;
  504. { true if p is an ansi string def with codepage CP_NONE }
  505. function is_rawbytestring(p : tdef) : boolean;
  506. begin
  507. is_rawbytestring:=(p.typ=stringdef) and
  508. (tstringdef(p).stringtype=st_ansistring) and
  509. (tstringdef(p).encoding=globals.CP_NONE);
  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,pasbool64,bool64bit])
  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. Message3(type_e_range_check_error_bounds,tostr(l),tostr(lv),tostr(hv))
  639. else
  640. Message3(type_w_range_check_error_bounds,tostr(l),tostr(lv),tostr(hv));
  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,scurrency];
  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.