defutil.pas 42 KB

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