defutil.pas 36 KB

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