defutil.pas 46 KB

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