defutil.pas 47 KB

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