defutil.pas 35 KB

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