defutil.pas 35 KB

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