defutil.pas 39 KB

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