defutil.pas 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563
  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. { returns the TTypeKind value of the def }
  238. function get_typekind(def: tdef): byte;
  239. implementation
  240. uses
  241. verbose,cutils;
  242. { returns true, if def uses FPU }
  243. function is_fpu(def : tdef) : boolean;
  244. begin
  245. is_fpu:=(def.typ=floatdef);
  246. end;
  247. { returns true, if def is a currency type }
  248. function is_currency(def : tdef) : boolean;
  249. begin
  250. case s64currencytype.typ of
  251. orddef :
  252. result:=(def.typ=orddef) and
  253. (torddef(s64currencytype).ordtype=torddef(def).ordtype);
  254. floatdef :
  255. result:=(def.typ=floatdef) and
  256. (tfloatdef(s64currencytype).floattype=tfloatdef(def).floattype);
  257. else
  258. internalerror(200304222);
  259. end;
  260. end;
  261. { returns true, if def is a single type }
  262. function is_single(def : tdef) : boolean;
  263. begin
  264. result:=(def.typ=floatdef) and
  265. (tfloatdef(def).floattype=s32real);
  266. end;
  267. { returns true, if def is a double type }
  268. function is_double(def : tdef) : boolean;
  269. begin
  270. result:=(def.typ=floatdef) and
  271. (tfloatdef(def).floattype=s64real);
  272. end;
  273. function is_extended(def : tdef) : boolean;
  274. begin
  275. result:=(def.typ=floatdef) and
  276. (tfloatdef(def).floattype in [s80real,sc80real]);
  277. end;
  278. { returns true, if definition is a "real" real (i.e. single/double/extended) }
  279. function is_real(def : tdef) : boolean;
  280. begin
  281. result:=(def.typ=floatdef) and
  282. (tfloatdef(def).floattype in [s32real,s64real,s80real]);
  283. end;
  284. function is_real_or_cextended(def: tdef): boolean;
  285. begin
  286. result:=(def.typ=floatdef) and
  287. (tfloatdef(def).floattype in [s32real,s64real,s80real,sc80real]);
  288. end;
  289. function range_to_basetype(const l,h:TConstExprInt):tordtype;
  290. begin
  291. { prefer signed over unsigned }
  292. if (l>=int64(-128)) and (h<=127) then
  293. range_to_basetype:=s8bit
  294. else if (l>=0) and (h<=255) then
  295. range_to_basetype:=u8bit
  296. else if (l>=int64(-32768)) and (h<=32767) then
  297. range_to_basetype:=s16bit
  298. else if (l>=0) and (h<=65535) then
  299. range_to_basetype:=u16bit
  300. else if (l>=int64(low(longint))) and (h<=high(longint)) then
  301. range_to_basetype:=s32bit
  302. else if (l>=low(cardinal)) and (h<=high(cardinal)) then
  303. range_to_basetype:=u32bit
  304. else if (l>=low(int64)) and (h<=high(int64)) then
  305. range_to_basetype:=s64bit
  306. else
  307. range_to_basetype:=u64bit;
  308. end;
  309. procedure range_to_type(const l,h:TConstExprInt;var def:tdef);
  310. begin
  311. { prefer signed over unsigned }
  312. if (l>=int64(-128)) and (h<=127) then
  313. def:=s8inttype
  314. else if (l>=0) and (h<=255) then
  315. def:=u8inttype
  316. else if (l>=int64(-32768)) and (h<=32767) then
  317. def:=s16inttype
  318. else if (l>=0) and (h<=65535) then
  319. def:=u16inttype
  320. else if (l>=int64(low(longint))) and (h<=high(longint)) then
  321. def:=s32inttype
  322. else if (l>=low(cardinal)) and (h<=high(cardinal)) then
  323. def:=u32inttype
  324. else if (l>=low(int64)) and (h<=high(int64)) then
  325. def:=s64inttype
  326. else
  327. def:=u64inttype;
  328. end;
  329. procedure int_to_type(const v:TConstExprInt;var def:tdef);
  330. begin
  331. range_to_type(v,v,def);
  332. end;
  333. { true if p is an ordinal }
  334. function is_ordinal(def : tdef) : boolean;
  335. var
  336. dt : tordtype;
  337. begin
  338. case def.typ of
  339. orddef :
  340. begin
  341. dt:=torddef(def).ordtype;
  342. is_ordinal:=dt in [uchar,uwidechar,
  343. u8bit,u16bit,u32bit,u64bit,
  344. s8bit,s16bit,s32bit,s64bit,
  345. pasbool8,pasbool16,pasbool32,pasbool64,
  346. bool8bit,bool16bit,bool32bit,bool64bit];
  347. end;
  348. enumdef :
  349. is_ordinal:=true;
  350. else
  351. is_ordinal:=false;
  352. end;
  353. end;
  354. { true if p is a string }
  355. function is_string(def : tdef) : boolean;
  356. begin
  357. is_string := (assigned(def) and (def.typ = stringdef));
  358. end;
  359. function is_stringlike(def : tdef) : boolean;
  360. begin
  361. result := is_string(def) or
  362. is_anychar(def) or
  363. is_pchar(def) or
  364. is_pwidechar(def) or
  365. is_chararray(def) or
  366. is_widechararray(def) or
  367. is_open_chararray(def) or
  368. is_open_widechararray(def) or
  369. (def=java_jlstring);
  370. end;
  371. function is_enum(def : tdef) : boolean;
  372. begin
  373. result:=def.typ=enumdef;
  374. end;
  375. function is_set(def : tdef) : boolean;
  376. begin
  377. result:=def.typ=setdef;
  378. end;
  379. { returns the min. value of the type }
  380. function get_min_value(def : tdef) : TConstExprInt;
  381. begin
  382. case def.typ of
  383. orddef:
  384. result:=torddef(def).low;
  385. enumdef:
  386. result:=int64(tenumdef(def).min);
  387. else
  388. result:=0;
  389. end;
  390. end;
  391. { returns the max. value of the type }
  392. function get_max_value(def : tdef) : TConstExprInt;
  393. begin
  394. case def.typ of
  395. orddef:
  396. result:=torddef(def).high;
  397. enumdef:
  398. result:=tenumdef(def).max;
  399. else
  400. result:=0;
  401. end;
  402. end;
  403. { true if p is an integer }
  404. function is_integer(def : tdef) : boolean;
  405. begin
  406. result:=(def.typ=orddef) and
  407. (torddef(def).ordtype in [u8bit,u16bit,u32bit,u64bit,
  408. s8bit,s16bit,s32bit,s64bit]);
  409. end;
  410. { true if p is a boolean }
  411. function is_boolean(def : tdef) : boolean;
  412. begin
  413. result:=(def.typ=orddef) and
  414. (torddef(def).ordtype in [pasbool8,pasbool16,pasbool32,pasbool64,bool8bit,bool16bit,bool32bit,bool64bit]);
  415. end;
  416. function is_pasbool(def : tdef) : boolean;
  417. begin
  418. result:=(def.typ=orddef) and
  419. (torddef(def).ordtype in [pasbool8,pasbool16,pasbool32,pasbool64]);
  420. end;
  421. { true if def is a C-style boolean (non-zero value = true, zero = false) }
  422. function is_cbool(def : tdef) : boolean;
  423. begin
  424. result:=(def.typ=orddef) and
  425. (torddef(def).ordtype in [bool8bit,bool16bit,bool32bit,bool64bit]);
  426. end;
  427. { true if p is a void }
  428. function is_void(def : tdef) : boolean;
  429. begin
  430. result:=(def.typ=orddef) and
  431. (torddef(def).ordtype=uvoid);
  432. end;
  433. { true if p is a char }
  434. function is_char(def : tdef) : boolean;
  435. begin
  436. result:=(def.typ=orddef) and
  437. (torddef(def).ordtype=uchar);
  438. end;
  439. { true if p is a wchar }
  440. function is_widechar(def : tdef) : boolean;
  441. begin
  442. result:=(def.typ=orddef) and
  443. (torddef(def).ordtype=uwidechar);
  444. end;
  445. { true if p is a char or wchar }
  446. function is_anychar(def : tdef) : boolean;
  447. begin
  448. result:=(def.typ=orddef) and
  449. (torddef(def).ordtype in [uchar,uwidechar])
  450. end;
  451. { true if p is signed (integer) }
  452. function is_signed(def : tdef) : boolean;
  453. begin
  454. case def.typ of
  455. orddef :
  456. result:=torddef(def).low < 0;
  457. enumdef :
  458. result:=tenumdef(def).min < 0;
  459. arraydef :
  460. result:=is_signed(tarraydef(def).rangedef);
  461. else
  462. result:=false;
  463. end;
  464. end;
  465. function get_unsigned_inttype(def: tdef): torddef;
  466. begin
  467. case def.typ of
  468. orddef,
  469. enumdef:
  470. result:=cgsize_orddef(tcgsize2unsigned[def_cgsize(def)]);
  471. else
  472. internalerror(2016062001);
  473. end;
  474. end;
  475. function is_in_limit(def_from,def_to : tdef) : boolean;
  476. begin
  477. if (def_from.typ<>def_to.typ) or
  478. not(def_from.typ in [orddef,enumdef,setdef]) then
  479. begin
  480. is_in_limit := false;
  481. exit;
  482. end;
  483. case def_from.typ of
  484. orddef:
  485. is_in_limit:=(torddef(def_from).low>=torddef(def_to).low) and
  486. (torddef(def_from).high<=torddef(def_to).high);
  487. enumdef:
  488. is_in_limit:=(tenumdef(def_from).min>=tenumdef(def_to).min) and
  489. (tenumdef(def_from).max<=tenumdef(def_to).max);
  490. setdef:
  491. is_in_limit:=(tsetdef(def_from).setbase>=tsetdef(def_to).setbase) and
  492. (tsetdef(def_from).setmax<=tsetdef(def_to).setmax);
  493. else
  494. is_in_limit:=false;
  495. end;
  496. end;
  497. function is_managed_type(def: tdef): boolean;{$ifdef USEINLINE}inline;{$endif}
  498. begin
  499. result:=def.needs_inittable;
  500. end;
  501. function is_rtti_managed_type(def: tdef): boolean;
  502. begin
  503. result:=def.needs_inittable and not (
  504. is_interfacecom_or_dispinterface(def) or
  505. (def.typ=variantdef) or
  506. (
  507. (def.typ=stringdef) and
  508. (tstringdef(def).stringtype in [st_ansistring,st_widestring,st_unicodestring])
  509. )
  510. );
  511. end;
  512. { true, if p points to an open array def }
  513. function is_open_string(p : tdef) : boolean;
  514. begin
  515. is_open_string:=(p.typ=stringdef) and
  516. (tstringdef(p).stringtype=st_shortstring) and
  517. (tstringdef(p).len=0);
  518. end;
  519. { true, if p points to a zero based array def }
  520. function is_zero_based_array(p : tdef) : boolean;
  521. begin
  522. result:=(p.typ=arraydef) and
  523. (tarraydef(p).lowrange=0) and
  524. not(is_special_array(p));
  525. end;
  526. { true if p points to a dynamic array def }
  527. function is_dynamic_array(p : tdef) : boolean;
  528. begin
  529. result:=(p.typ=arraydef) and
  530. (ado_IsDynamicArray in tarraydef(p).arrayoptions);
  531. end;
  532. { true, if p points to an open array def }
  533. function is_open_array(p : tdef) : boolean;
  534. begin
  535. { check for sizesinttype is needed, because for unsigned the high
  536. range is also -1 ! (PFV) }
  537. result:=(p.typ=arraydef) and
  538. (tarraydef(p).rangedef=sizesinttype) and
  539. (tarraydef(p).lowrange=0) and
  540. (tarraydef(p).highrange=-1) and
  541. ((tarraydef(p).arrayoptions * [ado_IsVariant,ado_IsArrayOfConst,ado_IsConstructor,ado_IsDynamicArray])=[]);
  542. end;
  543. { true, if p points to an array of const def }
  544. function is_array_constructor(p : tdef) : boolean;
  545. begin
  546. result:=(p.typ=arraydef) and
  547. (ado_IsConstructor in tarraydef(p).arrayoptions);
  548. end;
  549. { true, if p points to a variant array }
  550. function is_variant_array(p : tdef) : boolean;
  551. begin
  552. result:=(p.typ=arraydef) and
  553. (ado_IsVariant in tarraydef(p).arrayoptions);
  554. end;
  555. { true, if p points to an array of const }
  556. function is_array_of_const(p : tdef) : boolean;
  557. begin
  558. result:=(p.typ=arraydef) and
  559. (ado_IsArrayOfConst in tarraydef(p).arrayoptions);
  560. end;
  561. { true, if p points to a special array, bitpacked arrays aren't special in this regard though }
  562. function is_special_array(p : tdef) : boolean;
  563. begin
  564. result:=(p.typ=arraydef) and
  565. (
  566. ((tarraydef(p).arrayoptions * [ado_IsVariant,ado_IsArrayOfConst,ado_IsConstructor,ado_IsDynamicArray])<>[]) or
  567. is_open_array(p)
  568. );
  569. end;
  570. { true if p is an ansi string def }
  571. function is_ansistring(p : tdef) : boolean;
  572. begin
  573. is_ansistring:=(p.typ=stringdef) and
  574. (tstringdef(p).stringtype=st_ansistring);
  575. end;
  576. { true if p is an ansi string def with codepage CP_NONE }
  577. function is_rawbytestring(p : tdef) : boolean;
  578. begin
  579. is_rawbytestring:=(p.typ=stringdef) and
  580. (tstringdef(p).stringtype=st_ansistring) and
  581. (tstringdef(p).encoding=globals.CP_NONE);
  582. end;
  583. { true if p is an long string def }
  584. function is_longstring(p : tdef) : boolean;
  585. begin
  586. is_longstring:=(p.typ=stringdef) and
  587. (tstringdef(p).stringtype=st_longstring);
  588. end;
  589. { true if p is an wide string def }
  590. function is_widestring(p : tdef) : boolean;
  591. begin
  592. is_widestring:=(p.typ=stringdef) and
  593. (tstringdef(p).stringtype=st_widestring);
  594. end;
  595. function is_dynamicstring(p: tdef): boolean;
  596. begin
  597. is_dynamicstring:=(p.typ=stringdef) and
  598. (tstringdef(p).stringtype in [st_ansistring,st_widestring,st_unicodestring]);
  599. end;
  600. { true if p is an wide string def }
  601. function is_wide_or_unicode_string(p : tdef) : boolean;
  602. begin
  603. is_wide_or_unicode_string:=(p.typ=stringdef) and
  604. (tstringdef(p).stringtype in [st_widestring,st_unicodestring]);
  605. end;
  606. { true if p is an unicode string def }
  607. function is_unicodestring(p : tdef) : boolean;
  608. begin
  609. is_unicodestring:=(p.typ=stringdef) and
  610. (tstringdef(p).stringtype=st_unicodestring);
  611. end;
  612. { true if p is an short string def }
  613. function is_shortstring(p : tdef) : boolean;
  614. begin
  615. is_shortstring:=(p.typ=stringdef) and
  616. (tstringdef(p).stringtype=st_shortstring);
  617. end;
  618. { true if p is bit packed array def }
  619. function is_packed_array(p: tdef) : boolean;
  620. begin
  621. is_packed_array :=
  622. (p.typ = arraydef) and
  623. (ado_IsBitPacked in tarraydef(p).arrayoptions);
  624. end;
  625. { true if p is bit packed record def }
  626. function is_packed_record_or_object(p: tdef) : boolean;
  627. begin
  628. is_packed_record_or_object :=
  629. (p.typ in [recorddef,objectdef]) and
  630. (tabstractrecorddef(p).is_packed);
  631. end;
  632. { true if p is a char array def }
  633. function is_chararray(p : tdef) : boolean;
  634. begin
  635. is_chararray:=(p.typ=arraydef) and
  636. is_char(tarraydef(p).elementdef) and
  637. not(is_special_array(p));
  638. end;
  639. { true if p is a widechar array def }
  640. function is_widechararray(p : tdef) : boolean;
  641. begin
  642. is_widechararray:=(p.typ=arraydef) and
  643. is_widechar(tarraydef(p).elementdef) and
  644. not(is_special_array(p));
  645. end;
  646. { true if p is a open char array def }
  647. function is_open_chararray(p : tdef) : boolean;
  648. begin
  649. is_open_chararray:= is_open_array(p) and
  650. is_char(tarraydef(p).elementdef);
  651. end;
  652. { true if p is a open wide char array def }
  653. function is_open_widechararray(p : tdef) : boolean;
  654. begin
  655. is_open_widechararray:= is_open_array(p) and
  656. is_widechar(tarraydef(p).elementdef);
  657. end;
  658. { true if p is a pchar def }
  659. function is_pchar(p : tdef) : boolean;
  660. begin
  661. is_pchar:=(p.typ=pointerdef) and
  662. (is_char(tpointerdef(p).pointeddef) or
  663. (is_zero_based_array(tpointerdef(p).pointeddef) and
  664. is_chararray(tpointerdef(p).pointeddef)));
  665. end;
  666. { true if p is a pchar def }
  667. function is_pwidechar(p : tdef) : boolean;
  668. begin
  669. is_pwidechar:=(p.typ=pointerdef) and
  670. (is_widechar(tpointerdef(p).pointeddef) or
  671. (is_zero_based_array(tpointerdef(p).pointeddef) and
  672. is_widechararray(tpointerdef(p).pointeddef)));
  673. end;
  674. { true if p is a voidpointer def }
  675. function is_voidpointer(p : tdef) : boolean;
  676. begin
  677. is_voidpointer:=(p.typ=pointerdef) and
  678. (tpointerdef(p).pointeddef.typ=orddef) and
  679. (torddef(tpointerdef(p).pointeddef).ordtype=uvoid);
  680. end;
  681. { true, if def is a 8 bit int type }
  682. function is_8bitint(def : tdef) : boolean;
  683. begin
  684. result:=(def.typ=orddef) and (torddef(def).ordtype in [u8bit,s8bit])
  685. end;
  686. { true, if def is a 8 bit ordinal type }
  687. function is_8bit(def : tdef) : boolean;
  688. begin
  689. result:=(def.typ=orddef) and (torddef(def).ordtype in [u8bit,s8bit,pasbool8,bool8bit,uchar])
  690. end;
  691. { true, if def is a 16 bit int type }
  692. function is_16bitint(def : tdef) : boolean;
  693. begin
  694. result:=(def.typ=orddef) and (torddef(def).ordtype in [u16bit,s16bit])
  695. end;
  696. { true, if def is a 16 bit ordinal type }
  697. function is_16bit(def : tdef) : boolean;
  698. begin
  699. result:=(def.typ=orddef) and (torddef(def).ordtype in [u16bit,s16bit,pasbool16,bool16bit,uwidechar])
  700. end;
  701. { true, if def is a 32 bit int type }
  702. function is_32bitint(def : tdef) : boolean;
  703. begin
  704. result:=(def.typ=orddef) and (torddef(def).ordtype in [u32bit,s32bit])
  705. end;
  706. { true, if def is a 32 bit ordinal type }
  707. function is_32bit(def: tdef): boolean;
  708. begin
  709. result:=(def.typ=orddef) and (torddef(def).ordtype in [u32bit,s32bit,pasbool32,bool32bit])
  710. end;
  711. { true, if def is a 64 bit int type }
  712. function is_64bitint(def : tdef) : boolean;
  713. begin
  714. is_64bitint:=(def.typ=orddef) and (torddef(def).ordtype in [u64bit,s64bit])
  715. end;
  716. { true, if def is a 64 bit type }
  717. function is_64bit(def : tdef) : boolean;
  718. begin
  719. is_64bit:=(def.typ=orddef) and (torddef(def).ordtype in [u64bit,s64bit,scurrency,pasbool64,bool64bit])
  720. end;
  721. { true, if def1 and def2 are both integers of the same bit size and sign }
  722. function are_equal_ints(def1, def2: tdef): boolean;
  723. begin
  724. result:=(def1.typ=orddef) and (def2.typ=orddef) and
  725. (torddef(def1).ordtype in [u8bit,u16bit,u32bit,u64bit,
  726. s8bit,s16bit,s32bit,s64bit]) and
  727. (torddef(def1).ordtype=torddef(def2).ordtype);
  728. end;
  729. { true, if def is an int type, larger than the processor's native int size }
  730. function is_oversizedint(def : tdef) : boolean;
  731. begin
  732. {$if defined(cpu8bitalu)}
  733. result:=is_64bitint(def) or is_32bitint(def) or is_16bitint(def);
  734. {$elseif defined(cpu16bitalu)}
  735. result:=is_64bitint(def) or is_32bitint(def);
  736. {$elseif defined(cpu32bitaddr)}
  737. result:=is_64bitint(def);
  738. {$elseif defined(cpu64bitaddr)}
  739. result:=false;
  740. {$endif}
  741. end;
  742. { true, if def is an ordinal type, larger than the processor's native int size }
  743. function is_oversizedord(def : tdef) : boolean;
  744. begin
  745. {$if defined(cpu8bitalu)}
  746. result:=is_64bit(def) or is_32bit(def) or is_16bit(def);
  747. {$elseif defined(cpu16bitalu)}
  748. result:=is_64bit(def) or is_32bit(def);
  749. {$elseif defined(cpu32bitaddr)}
  750. result:=is_64bit(def);
  751. {$elseif defined(cpu64bitaddr)}
  752. result:=false;
  753. {$endif}
  754. end;
  755. { true, if def is an int type, equal in size to the processor's native int size }
  756. function is_nativeint(def: tdef): boolean;
  757. begin
  758. {$if defined(cpu8bitalu)}
  759. result:=is_8bitint(def);
  760. {$elseif defined(cpu16bitalu)}
  761. result:=is_16bitint(def);
  762. {$elseif defined(cpu32bitaddr)}
  763. result:=is_32bitint(def);
  764. {$elseif defined(cpu64bitaddr)}
  765. result:=is_64bitint(def);
  766. {$endif}
  767. end;
  768. { true, if def is an ordinal type, equal in size to the processor's native int size }
  769. function is_nativeord(def: tdef): boolean;
  770. begin
  771. {$if defined(cpu8bitalu)}
  772. result:=is_8bit(def);
  773. {$elseif defined(cpu16bitalu)}
  774. result:=is_16bit(def);
  775. {$elseif defined(cpu32bitaddr)}
  776. result:=is_32bit(def);
  777. {$elseif defined(cpu64bitaddr)}
  778. result:=is_64bit(def);
  779. {$endif}
  780. end;
  781. { true, if def is an unsigned int type, equal in size to the processor's native int size }
  782. function is_nativeuint(def: tdef): boolean;
  783. begin
  784. result:=is_nativeint(def) and (def.typ=orddef) and (torddef(def).ordtype in [u64bit,u32bit,u16bit,u8bit]);
  785. end;
  786. { true, if def is a signed int type, equal in size to the processor's native int size }
  787. function is_nativesint(def: tdef): boolean;
  788. begin
  789. result:=is_nativeint(def) and (def.typ=orddef) and (torddef(def).ordtype in [s64bit,s32bit,s16bit,s8bit]);
  790. end;
  791. { if l isn't in the range of todef a range check error (if not explicit) is generated and
  792. the value is placed within the range }
  793. procedure testrange(todef : tdef;var l : tconstexprint;explicit,forcerangecheck:boolean);
  794. var
  795. lv,hv: TConstExprInt;
  796. begin
  797. { for 64 bit types we need only to check if it is less than }
  798. { zero, if def is a qword node }
  799. getrange(todef,lv,hv);
  800. if (l<lv) or (l>hv) then
  801. begin
  802. if not explicit then
  803. begin
  804. if ((todef.typ=enumdef) and
  805. { delphi allows range check errors in
  806. enumeration type casts FK }
  807. not(m_delphi in current_settings.modeswitches)) or
  808. (cs_check_range in current_settings.localswitches) or
  809. forcerangecheck then
  810. Message3(type_e_range_check_error_bounds,tostr(l),tostr(lv),tostr(hv))
  811. else
  812. Message3(type_w_range_check_error_bounds,tostr(l),tostr(lv),tostr(hv));
  813. end;
  814. { Fix the value to fit in the allocated space for this type of variable }
  815. case longint(todef.size) of
  816. 1: l := l and $ff;
  817. 2: l := l and $ffff;
  818. 4: l := l and $ffffffff;
  819. end;
  820. {reset sign, i.e. converting -1 to qword changes the value to high(qword)}
  821. l.signed:=false;
  822. { do sign extension if necessary (JM) }
  823. if is_signed(todef) then
  824. begin
  825. case longint(todef.size) of
  826. 1: l.svalue := shortint(l.svalue);
  827. 2: l.svalue := smallint(l.svalue);
  828. 4: l.svalue := longint(l.svalue);
  829. end;
  830. l.signed:=true;
  831. end;
  832. end;
  833. end;
  834. { return the range from def in l and h }
  835. procedure getrange(def : tdef;out l, h : TConstExprInt);
  836. begin
  837. case def.typ of
  838. orddef :
  839. begin
  840. l:=torddef(def).low;
  841. h:=torddef(def).high;
  842. end;
  843. enumdef :
  844. begin
  845. l:=int64(tenumdef(def).min);
  846. h:=int64(tenumdef(def).max);
  847. end;
  848. arraydef :
  849. begin
  850. l:=int64(tarraydef(def).lowrange);
  851. h:=int64(tarraydef(def).highrange);
  852. end;
  853. undefineddef:
  854. begin
  855. l:=torddef(sizesinttype).low;
  856. h:=torddef(sizesinttype).high;
  857. end;
  858. else
  859. internalerror(200611054);
  860. end;
  861. end;
  862. function mmx_type(p : tdef) : tmmxtype;
  863. begin
  864. mmx_type:=mmxno;
  865. if is_mmx_able_array(p) then
  866. begin
  867. if tarraydef(p).elementdef.typ=floatdef then
  868. case tfloatdef(tarraydef(p).elementdef).floattype of
  869. s32real:
  870. mmx_type:=mmxsingle;
  871. end
  872. else
  873. case torddef(tarraydef(p).elementdef).ordtype of
  874. u8bit:
  875. mmx_type:=mmxu8bit;
  876. s8bit:
  877. mmx_type:=mmxs8bit;
  878. u16bit:
  879. mmx_type:=mmxu16bit;
  880. s16bit:
  881. mmx_type:=mmxs16bit;
  882. u32bit:
  883. mmx_type:=mmxu32bit;
  884. s32bit:
  885. mmx_type:=mmxs32bit;
  886. end;
  887. end;
  888. end;
  889. function is_vector(p : tdef) : boolean;
  890. begin
  891. result:=(p.typ=arraydef) and
  892. not(is_special_array(p)) and
  893. (tarraydef(p).elementdef.typ=floatdef) and
  894. (tfloatdef(tarraydef(p).elementdef).floattype in [s32real,s64real]);
  895. end;
  896. { returns if the passed type (array) fits into an mm register }
  897. function fits_in_mm_register(p : tdef) : boolean;
  898. begin
  899. {$ifdef x86}
  900. result:= is_vector(p) and
  901. (
  902. (tarraydef(p).elementdef.typ=floatdef) and
  903. (
  904. (tarraydef(p).lowrange=0) and
  905. (tarraydef(p).highrange=3) and
  906. (tfloatdef(tarraydef(p).elementdef).floattype=s32real)
  907. )
  908. ) or
  909. (
  910. (tarraydef(p).elementdef.typ=floatdef) and
  911. (
  912. (tarraydef(p).lowrange=0) and
  913. (tarraydef(p).highrange=1) and
  914. (tfloatdef(tarraydef(p).elementdef).floattype=s64real)
  915. )
  916. );
  917. {$else x86}
  918. result:=false;
  919. {$endif x86}
  920. end;
  921. function is_mmx_able_array(p : tdef) : boolean;
  922. begin
  923. {$ifdef SUPPORT_MMX}
  924. if (cs_mmx_saturation in current_settings.localswitches) then
  925. begin
  926. is_mmx_able_array:=(p.typ=arraydef) and
  927. not(is_special_array(p)) and
  928. (
  929. (
  930. (tarraydef(p).elementdef.typ=orddef) and
  931. (
  932. (
  933. (tarraydef(p).lowrange=0) and
  934. (tarraydef(p).highrange=1) and
  935. (torddef(tarraydef(p).elementdef).ordtype in [u32bit,s32bit])
  936. )
  937. or
  938. (
  939. (tarraydef(p).lowrange=0) and
  940. (tarraydef(p).highrange=3) and
  941. (torddef(tarraydef(p).elementdef).ordtype in [u16bit,s16bit])
  942. )
  943. )
  944. )
  945. or
  946. (
  947. (
  948. (tarraydef(p).elementdef.typ=floatdef) and
  949. (
  950. (tarraydef(p).lowrange=0) and
  951. (tarraydef(p).highrange=1) and
  952. (tfloatdef(tarraydef(p).elementdef).floattype=s32real)
  953. )
  954. )
  955. )
  956. );
  957. end
  958. else
  959. begin
  960. is_mmx_able_array:=(p.typ=arraydef) and
  961. (
  962. (
  963. (tarraydef(p).elementdef.typ=orddef) and
  964. (
  965. (
  966. (tarraydef(p).lowrange=0) and
  967. (tarraydef(p).highrange=1) and
  968. (torddef(tarraydef(p).elementdef).ordtype in [u32bit,s32bit])
  969. )
  970. or
  971. (
  972. (tarraydef(p).lowrange=0) and
  973. (tarraydef(p).highrange=3) and
  974. (torddef(tarraydef(p).elementdef).ordtype in [u16bit,s16bit])
  975. )
  976. or
  977. (
  978. (tarraydef(p).lowrange=0) and
  979. (tarraydef(p).highrange=7) and
  980. (torddef(tarraydef(p).elementdef).ordtype in [u8bit,s8bit])
  981. )
  982. )
  983. )
  984. or
  985. (
  986. (tarraydef(p).elementdef.typ=floatdef) and
  987. (
  988. (tarraydef(p).lowrange=0) and
  989. (tarraydef(p).highrange=1) and
  990. (tfloatdef(tarraydef(p).elementdef).floattype=s32real)
  991. )
  992. )
  993. );
  994. end;
  995. {$else SUPPORT_MMX}
  996. is_mmx_able_array:=false;
  997. {$endif SUPPORT_MMX}
  998. end;
  999. function def_cgsize(def: tdef): tcgsize;
  1000. begin
  1001. case def.typ of
  1002. orddef,
  1003. enumdef,
  1004. setdef:
  1005. begin
  1006. result:=int_cgsize(def.size);
  1007. if is_signed(def) then
  1008. result:=tcgsize(ord(result)+(ord(OS_S8)-ord(OS_8)));
  1009. end;
  1010. classrefdef,
  1011. pointerdef:
  1012. begin
  1013. result:=int_cgsize(def.size);
  1014. { can happen for far/huge pointers on non-i8086 }
  1015. if result=OS_NO then
  1016. internalerror(2013052201);
  1017. end;
  1018. formaldef:
  1019. result := int_cgsize(voidpointertype.size);
  1020. procvardef:
  1021. result:=int_cgsize(def.size);
  1022. stringdef :
  1023. result:=int_cgsize(def.size);
  1024. objectdef :
  1025. result:=int_cgsize(def.size);
  1026. floatdef:
  1027. if cs_fp_emulation in current_settings.moduleswitches then
  1028. result:=int_cgsize(def.size)
  1029. else
  1030. result:=tfloat2tcgsize[tfloatdef(def).floattype];
  1031. recorddef :
  1032. result:=int_cgsize(def.size);
  1033. arraydef :
  1034. begin
  1035. if is_dynamic_array(def) or not is_special_array(def) then
  1036. result := int_cgsize(def.size)
  1037. else
  1038. result := OS_NO;
  1039. end;
  1040. else
  1041. begin
  1042. { undefined size }
  1043. result:=OS_NO;
  1044. end;
  1045. end;
  1046. end;
  1047. function cgsize_orddef(size: tcgsize): torddef;
  1048. begin
  1049. case size of
  1050. OS_8:
  1051. result:=torddef(u8inttype);
  1052. OS_S8:
  1053. result:=torddef(s8inttype);
  1054. OS_16:
  1055. result:=torddef(u16inttype);
  1056. OS_S16:
  1057. result:=torddef(s16inttype);
  1058. OS_32:
  1059. result:=torddef(u32inttype);
  1060. OS_S32:
  1061. result:=torddef(s32inttype);
  1062. OS_64:
  1063. result:=torddef(u64inttype);
  1064. OS_S64:
  1065. result:=torddef(s64inttype);
  1066. else
  1067. internalerror(2012050401);
  1068. end;
  1069. end;
  1070. function def_cgmmsize(def: tdef): tcgsize;
  1071. begin
  1072. case def.typ of
  1073. arraydef:
  1074. begin
  1075. if tarraydef(def).elementdef.typ in [orddef,floatdef] then
  1076. begin
  1077. { this is not correct, OS_MX normally mean that the vector
  1078. contains elements of size X. However, vectors themselves
  1079. can also have different sizes (e.g. a vector of 2 singles on
  1080. SSE) and the total size is currently more important }
  1081. case def.size of
  1082. 1: result:=OS_M8;
  1083. 2: result:=OS_M16;
  1084. 4: result:=OS_M32;
  1085. 8: result:=OS_M64;
  1086. 16: result:=OS_M128;
  1087. 32: result:=OS_M256;
  1088. else
  1089. internalerror(2013060103);
  1090. end;
  1091. end
  1092. else
  1093. result:=def_cgsize(def);
  1094. end
  1095. else
  1096. result:=def_cgsize(def);
  1097. end;
  1098. end;
  1099. { In Windows 95 era, ordinals were restricted to [u8bit,s32bit,s16bit,bool16bit]
  1100. As of today, both signed and unsigned types from 8 to 64 bits are supported. }
  1101. function is_automatable(p : tdef) : boolean;
  1102. begin
  1103. result:=false;
  1104. case p.typ of
  1105. orddef:
  1106. result:=torddef(p).ordtype in [u8bit,s8bit,u16bit,s16bit,u32bit,s32bit,
  1107. u64bit,s64bit,bool16bit,scurrency];
  1108. floatdef:
  1109. result:=tfloatdef(p).floattype in [s64currency,s64real,s32real];
  1110. stringdef:
  1111. result:=tstringdef(p).stringtype in [st_ansistring,st_widestring,st_unicodestring];
  1112. variantdef:
  1113. result:=true;
  1114. objectdef:
  1115. result:=tobjectdef(p).objecttype in [odt_interfacecom,odt_dispinterface,odt_interfacecorba];
  1116. end;
  1117. end;
  1118. {# returns true, if the type passed is a varset }
  1119. function is_smallset(p : tdef) : boolean;
  1120. begin
  1121. {$if defined(cpu8bitalu)}
  1122. result:=(p.typ=setdef) and (p.size = 1)
  1123. {$elseif defined(cpu16bitalu)}
  1124. result:=(p.typ=setdef) and (p.size in [1,2])
  1125. {$else}
  1126. result:=(p.typ=setdef) and (p.size in [1,2,4])
  1127. {$endif}
  1128. end;
  1129. function is_bareprocdef(pd : tprocdef): boolean;
  1130. begin
  1131. result:=(pd.maxparacount=0) and
  1132. (is_void(pd.returndef) or
  1133. (pd.proctypeoption = potype_constructor));
  1134. end;
  1135. function get_common_intdef(ld, rd: torddef; keep_sign_if_equal: boolean): torddef;
  1136. var
  1137. llow, lhigh: tconstexprint;
  1138. begin
  1139. llow:=min(ld.low,rd.low);
  1140. lhigh:=max(ld.high,rd.high);
  1141. case range_to_basetype(llow,lhigh) of
  1142. s8bit:
  1143. result:=torddef(s8inttype);
  1144. u8bit:
  1145. result:=torddef(u8inttype);
  1146. s16bit:
  1147. result:=torddef(s16inttype);
  1148. u16bit:
  1149. result:=torddef(u16inttype);
  1150. s32bit:
  1151. result:=torddef(s32inttype);
  1152. u32bit:
  1153. result:=torddef(u32inttype);
  1154. s64bit:
  1155. result:=torddef(s64inttype);
  1156. u64bit:
  1157. result:=torddef(u64inttype);
  1158. else
  1159. begin
  1160. { avoid warning }
  1161. result:=nil;
  1162. internalerror(200802291);
  1163. end;
  1164. end;
  1165. if keep_sign_if_equal and
  1166. (is_signed(ld)=is_signed(rd)) and
  1167. (is_signed(result)<>is_signed(ld)) then
  1168. case result.ordtype of
  1169. s8bit:
  1170. result:=torddef(u8inttype);
  1171. u8bit:
  1172. result:=torddef(s16inttype);
  1173. s16bit:
  1174. result:=torddef(u16inttype);
  1175. u16bit:
  1176. result:=torddef(s32inttype);
  1177. s32bit:
  1178. result:=torddef(u32inttype);
  1179. u32bit:
  1180. result:=torddef(s64inttype);
  1181. s64bit:
  1182. result:=torddef(u64inttype);
  1183. end;
  1184. end;
  1185. function is_valid_univ_para_type(def: tdef): boolean;
  1186. begin
  1187. result:=
  1188. not is_open_array(def) and
  1189. not is_void(def) and
  1190. (def.typ<>formaldef);
  1191. end;
  1192. function is_nested_pd(def: tabstractprocdef): boolean;{$ifdef USEINLINE}inline;{$endif}
  1193. begin
  1194. result:=def.parast.symtablelevel>normal_function_level;
  1195. end;
  1196. function is_typeparam(def : tdef) : boolean;{$ifdef USEINLINE}inline;{$endif}
  1197. begin
  1198. result:=(def.typ=undefineddef);
  1199. end;
  1200. function is_methodpointer(def: tdef): boolean;
  1201. begin
  1202. result:=(def.typ=procvardef) and (po_methodpointer in tprocvardef(def).procoptions);
  1203. end;
  1204. function is_block(def: tdef): boolean;
  1205. begin
  1206. result:=(def.typ=procvardef) and (po_is_block in tprocvardef(def).procoptions)
  1207. end;
  1208. function get_typekind(def:tdef):byte;
  1209. begin
  1210. case def.typ of
  1211. arraydef:
  1212. if ado_IsDynamicArray in tarraydef(def).arrayoptions then
  1213. result:=tkDynArray
  1214. else
  1215. result:=tkArray;
  1216. recorddef:
  1217. result:=tkRecord;
  1218. pointerdef:
  1219. result:=tkPointer;
  1220. orddef:
  1221. case torddef(def).ordtype of
  1222. u8bit,
  1223. u16bit,
  1224. u32bit,
  1225. s8bit,
  1226. s16bit,
  1227. s32bit:
  1228. result:=tkInteger;
  1229. u64bit:
  1230. result:=tkQWord;
  1231. s64bit:
  1232. result:=tkInt64;
  1233. pasbool8,
  1234. pasbool16,
  1235. pasbool32,
  1236. pasbool64,
  1237. bool8bit,
  1238. bool16bit,
  1239. bool32bit,
  1240. bool64bit:
  1241. result:=tkBool;
  1242. uchar:
  1243. result:=tkChar;
  1244. uwidechar:
  1245. result:=tkWChar;
  1246. scurrency:
  1247. result:=tkFloat;
  1248. else
  1249. result:=tkUnknown;
  1250. end;
  1251. stringdef:
  1252. case tstringdef(def).stringtype of
  1253. st_shortstring:
  1254. result:=tkSString;
  1255. st_longstring:
  1256. result:=tkLString;
  1257. st_ansistring:
  1258. result:=tkAString;
  1259. st_widestring:
  1260. result:=tkWString;
  1261. st_unicodestring:
  1262. result:=tkUString;
  1263. else
  1264. result:=tkUnknown;
  1265. end;
  1266. enumdef:
  1267. result:=tkEnumeration;
  1268. objectdef:
  1269. case tobjectdef(def).objecttype of
  1270. odt_class,
  1271. odt_javaclass:
  1272. result:=tkClass;
  1273. odt_object:
  1274. result:=tkObject;
  1275. odt_interfacecom,
  1276. odt_dispinterface,
  1277. odt_interfacejava:
  1278. result:=tkInterface;
  1279. odt_interfacecorba:
  1280. result:=tkInterfaceCorba;
  1281. odt_helper:
  1282. result:=tkHelper;
  1283. else
  1284. result:=tkUnknown;
  1285. end;
  1286. { currently tkFile is not used }
  1287. {filedef:
  1288. result:=tkFile;}
  1289. setdef:
  1290. result:=tkSet;
  1291. procvardef:
  1292. if tprocvardef(def).is_methodpointer then
  1293. result:=tkMethod
  1294. else
  1295. result:=tkProcVar;
  1296. floatdef:
  1297. result:=tkFloat;
  1298. classrefdef:
  1299. result:=tkClassRef;
  1300. variantdef:
  1301. result:=tkVariant;
  1302. else
  1303. result:=tkUnknown;
  1304. end;
  1305. end;
  1306. end.