defutil.pas 37 KB

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