defutil.pas 34 KB

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