defutil.pas 36 KB

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