defutil.pas 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806
  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,mmxs64bit,mmxu64bit);
  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. {# Return true if the type (orddef or enumdef) spans its entire bitrange }
  50. function spans_entire_range(def: tdef): boolean;
  51. {# Returns true, if definition defines an integer type }
  52. function is_integer(def : tdef) : boolean;
  53. {# Returns true if definition is a boolean }
  54. function is_boolean(def : tdef) : boolean;
  55. {# Returns true if definition is a Pascal-style boolean (1 = true, zero = false) }
  56. function is_pasbool(def : tdef) : boolean;
  57. {# Returns true if definition is a C-style boolean (non-zero value = true, zero = false) }
  58. function is_cbool(def : tdef) : boolean;
  59. {# Returns true if definition is a char
  60. This excludes the unicode char.
  61. }
  62. function is_char(def : tdef) : boolean;
  63. {# Returns true if definition is a widechar }
  64. function is_widechar(def : tdef) : boolean;
  65. {# Returns true if definition is either an AnsiChar or a WideChar }
  66. function is_anychar(def : tdef) : boolean;
  67. {# Returns true if definition is a void}
  68. function is_void(def : tdef) : boolean;
  69. {# Returns true if definition is a smallset}
  70. function is_smallset(p : tdef) : boolean;
  71. {# Returns true, if def defines a signed data type
  72. (only for ordinal types)
  73. }
  74. function is_signed(def : tdef) : boolean;
  75. {# Returns an unsigned integer type of the same size as def; def must be
  76. an ordinal or enum }
  77. function get_unsigned_inttype(def: tdef): torddef;
  78. {# Returns whether def_from's range is comprised in def_to's if both are
  79. orddefs, false otherwise }
  80. function is_in_limit(def_from,def_to : tdef) : boolean;
  81. {# Returns whether def is reference counted }
  82. function is_managed_type(def: tdef) : boolean;{$ifdef USEINLINE}inline;{$endif}
  83. { # Returns whether def is needs to load RTTI for reference counting }
  84. function is_rtti_managed_type(def: tdef) : boolean;
  85. { function is_in_limit_value(val_from:TConstExprInt;def_from,def_to : tdef) : boolean;}
  86. {*****************************************************************************
  87. Array helper functions
  88. *****************************************************************************}
  89. {# Returns true, if p points to a zero based (non special like open or
  90. dynamic array def).
  91. This is mainly used to see if the array
  92. is convertable to a pointer
  93. }
  94. function is_zero_based_array(p : tdef) : boolean;
  95. {# Returns true if p points to an open array definition }
  96. function is_open_array(p : tdef) : boolean;
  97. {# Returns true if p points to a dynamic array definition }
  98. function is_dynamic_array(p : tdef) : boolean;
  99. {# Returns true, if p points to an array of const definition }
  100. function is_array_constructor(p : tdef) : boolean;
  101. {# Returns true, if p points to a variant array }
  102. function is_variant_array(p : tdef) : boolean;
  103. {# Returns true, if p points to an array of const }
  104. function is_array_of_const(p : tdef) : boolean;
  105. {# Returns true, if p points any kind of special array
  106. That is if the array is an open array, a variant
  107. array, an array constants constructor, or an
  108. array of const.
  109. Bitpacked arrays aren't special in this regard though.
  110. }
  111. function is_special_array(p : tdef) : boolean;
  112. {# Returns true if p is a bitpacked array }
  113. function is_packed_array(p: tdef) : boolean;
  114. {# Returns true if p is a bitpacked record }
  115. function is_packed_record_or_object(p: tdef) : boolean;
  116. {# Returns true if p is a char array def }
  117. function is_chararray(p : tdef) : boolean;
  118. {# Returns true if p is a wide char array def }
  119. function is_widechararray(p : tdef) : boolean;
  120. {# Returns true if p is a open char array def }
  121. function is_open_chararray(p : tdef) : boolean;
  122. {# Returns true if p is a open wide char array def }
  123. function is_open_widechararray(p : tdef) : boolean;
  124. {*****************************************************************************
  125. String helper functions
  126. *****************************************************************************}
  127. {# Returns true if p points to an open string type }
  128. function is_open_string(p : tdef) : boolean;
  129. {# Returns true if p is an ansi string type }
  130. function is_ansistring(p : tdef) : boolean;
  131. {# Returns true if p is an ansi string type with codepage 0 }
  132. function is_rawbytestring(p : tdef) : boolean;
  133. {# Returns true if p is a long string type }
  134. function is_longstring(p : tdef) : boolean;
  135. {# returns true if p is a wide string type }
  136. function is_widestring(p : tdef) : boolean;
  137. {# true if p is an unicode string def }
  138. function is_unicodestring(p : tdef) : boolean;
  139. {# true if p is an unicode/wide/ansistring string def }
  140. function is_dynamicstring(p : tdef) : boolean;
  141. {# returns true if p is a wide or unicode string type }
  142. function is_wide_or_unicode_string(p : tdef) : boolean;
  143. {# Returns true if p is a short string type }
  144. function is_shortstring(p : tdef) : boolean;
  145. {# Returns true if p is any pointer def }
  146. function is_pointer(p : tdef) : boolean;
  147. {# Returns true if p is a pchar def }
  148. function is_pchar(p : tdef) : boolean;
  149. {# Returns true if p is a pwidechar def }
  150. function is_pwidechar(p : tdef) : boolean;
  151. {# Returns true if p is a voidpointer def }
  152. function is_voidpointer(p : tdef) : boolean;
  153. {# Returns true, if definition is a float }
  154. function is_fpu(def : tdef) : boolean;
  155. {# Returns true, if def is a currency type }
  156. function is_currency(def : tdef) : boolean;
  157. {# Returns true, if def is a single type }
  158. function is_single(def : tdef) : boolean;
  159. {# Returns true, if def is a double type }
  160. function is_double(def : tdef) : boolean;
  161. {# Returns true, if def is an extended type }
  162. function is_extended(def : tdef) : boolean;
  163. {# Returns true, if definition is a "real" real (i.e. single/double/extended) }
  164. function is_real(def : tdef) : boolean;
  165. {# Returns true for single,double,extended and cextended }
  166. function is_real_or_cextended(def : tdef) : boolean;
  167. { true, if def is a 8 bit int type }
  168. function is_8bitint(def : tdef) : boolean;
  169. { true, if def is a 8 bit ordinal type }
  170. function is_8bit(def : tdef) : boolean;
  171. { true, if def is a 16 bit int type }
  172. function is_16bitint(def : tdef) : boolean;
  173. { true, if def is a 16 bit ordinal type }
  174. function is_16bit(def : tdef) : boolean;
  175. {# Returns true, if def is a 32 bit integer type }
  176. function is_32bitint(def : tdef) : boolean;
  177. {# Returns true, if def is a 32 bit ordinal type }
  178. function is_32bit(def : tdef) : boolean;
  179. {# Returns true, if def is a 64 bit integer type }
  180. function is_64bitint(def : tdef) : boolean;
  181. {# Returns true, if def is a 64 bit type }
  182. function is_64bit(def : tdef) : boolean;
  183. { true, if def1 and def2 are both integers of the same bit size and sign }
  184. function are_equal_ints(def1, def2: tdef): boolean;
  185. { true, if def is an int type, larger than the processor's native int size }
  186. function is_oversizedint(def : tdef) : boolean;
  187. { true, if def is an ordinal type, larger than the processor's native int size }
  188. function is_oversizedord(def : tdef) : boolean;
  189. { true, if def is an int type, equal in size to the processor's native int size }
  190. function is_nativeint(def : tdef) : boolean;
  191. { true, if def is an ordinal type, equal in size to the processor's native int size }
  192. function is_nativeord(def : tdef) : boolean;
  193. { true, if def is an unsigned int type, equal in size to the processor's native int size }
  194. function is_nativeuint(def : tdef) : boolean;
  195. { true, if def is a signed int type, equal in size to the processor's native int size }
  196. function is_nativesint(def : tdef) : boolean;
  197. type
  198. tperformrangecheck = (
  199. rc_internal, { nothing, internal conversion }
  200. rc_explicit, { no, but this is an explcit user conversion and hence can still give warnings in some cases (or errors in case of enums) }
  201. rc_implicit, { no, but this is an implicit conversion and hence can still give warnings/errors in some cases }
  202. rc_yes { yes }
  203. );
  204. {# If @var(l) isn't in the range of todef a range check error (if not explicit) is generated and
  205. the value is placed within the range
  206. }
  207. procedure adaptrange(todef : tdef;var l : tconstexprint; rangecheck: tperformrangecheck);
  208. { for when used with nf_explicit/nf_internal/cs_check_range nodeflags }
  209. procedure adaptrange(todef : tdef;var l : tconstexprint; internal, explicit, rangecheckstate: boolean);
  210. {# Returns the range of def, where @var(l) is the low-range and @var(h) is
  211. the high-range.
  212. }
  213. procedure getrange(def : tdef;out l, h : TConstExprInt);
  214. procedure getrangedefmasksize(def: tdef; out rangedef: tdef; out mask: TConstExprInt; out size: longint);
  215. { Returns the range type of an ordinal type in the sense of ISO-10206 }
  216. function get_iso_range_type(def: tdef): tdef;
  217. { type being a vector? }
  218. function is_vector(p : tdef) : boolean;
  219. { some type helper routines for MMX support }
  220. function is_mmx_able_array(p : tdef) : boolean;
  221. {# returns the mmx type }
  222. function mmx_type(p : tdef) : tmmxtype;
  223. { returns if the passed type (array) fits into an mm register }
  224. function fits_in_mm_register(p : tdef) : boolean;
  225. {# From a definition return the abstract code generator size enum. It is
  226. to note that the value returned can be @var(OS_NO) }
  227. function def_cgsize(def: tdef): tcgsize;
  228. { #Return an orddef (integer) correspondig to a tcgsize }
  229. function cgsize_orddef(size: tcgsize): torddef;
  230. {# Same as def_cgsize, except that it will interpret certain arrays as
  231. vectors and return OS_M* sizes for them }
  232. function def_cgmmsize(def: tdef): tcgsize;
  233. {# returns true, if the type passed is can be used with windows automation }
  234. function is_automatable(p : tdef) : boolean;
  235. { # returns true if the procdef has no parameters and no specified return type }
  236. function is_bareprocdef(pd : tprocdef): boolean;
  237. { # returns the smallest base integer type whose range encompasses that of
  238. both ld and rd; if keep_sign_if_equal, then if ld and rd have the same
  239. signdness, the result will also get that signdness }
  240. function get_common_intdef(ld, rd: torddef; keep_sign_if_equal: boolean): torddef;
  241. { # returns whether the type is potentially a valid type of/for an "univ" parameter
  242. (basically: it must have a compile-time size) }
  243. function is_valid_univ_para_type(def: tdef): boolean;
  244. { # returns whether the procdef/procvardef represents a nested procedure
  245. or not }
  246. function is_nested_pd(def: tabstractprocdef): boolean;{$ifdef USEINLINE}inline;{$endif}
  247. { # returns whether def is a type parameter of a generic }
  248. function is_typeparam(def : tdef) : boolean;{$ifdef USEINLINE}inline;{$endif}
  249. { returns true of def is a methodpointer }
  250. function is_methodpointer(def : tdef) : boolean;
  251. { returns true if def is a C "block" }
  252. function is_block(def: tdef): boolean;
  253. { returns the TTypeKind value of the def }
  254. function get_typekind(def: tdef): byte;
  255. implementation
  256. uses
  257. verbose,cutils;
  258. { returns true, if def uses FPU }
  259. function is_fpu(def : tdef) : boolean;
  260. begin
  261. is_fpu:=(def.typ=floatdef);
  262. end;
  263. { returns true, if def is a currency type }
  264. function is_currency(def : tdef) : boolean;
  265. begin
  266. case s64currencytype.typ of
  267. orddef :
  268. result:=(def.typ=orddef) and
  269. (torddef(s64currencytype).ordtype=torddef(def).ordtype);
  270. floatdef :
  271. result:=(def.typ=floatdef) and
  272. (tfloatdef(s64currencytype).floattype=tfloatdef(def).floattype);
  273. else
  274. internalerror(200304222);
  275. end;
  276. end;
  277. { returns true, if def is a single type }
  278. function is_single(def : tdef) : boolean;
  279. begin
  280. result:=(def.typ=floatdef) and
  281. (tfloatdef(def).floattype=s32real);
  282. end;
  283. { returns true, if def is a double type }
  284. function is_double(def : tdef) : boolean;
  285. begin
  286. result:=(def.typ=floatdef) and
  287. (tfloatdef(def).floattype=s64real);
  288. end;
  289. function is_extended(def : tdef) : boolean;
  290. begin
  291. result:=(def.typ=floatdef) and
  292. (tfloatdef(def).floattype in [s80real,sc80real]);
  293. end;
  294. { returns true, if definition is a "real" real (i.e. single/double/extended) }
  295. function is_real(def : tdef) : boolean;
  296. begin
  297. result:=(def.typ=floatdef) and
  298. (tfloatdef(def).floattype in [s32real,s64real,s80real]);
  299. end;
  300. function is_real_or_cextended(def: tdef): boolean;
  301. begin
  302. result:=(def.typ=floatdef) and
  303. (tfloatdef(def).floattype in [s32real,s64real,s80real,sc80real]);
  304. end;
  305. function range_to_basetype(const l,h:TConstExprInt):tordtype;
  306. begin
  307. { prefer signed over unsigned }
  308. if (l>=int64(-128)) and (h<=127) then
  309. range_to_basetype:=s8bit
  310. else if (l>=0) and (h<=255) then
  311. range_to_basetype:=u8bit
  312. else if (l>=int64(-32768)) and (h<=32767) then
  313. range_to_basetype:=s16bit
  314. else if (l>=0) and (h<=65535) then
  315. range_to_basetype:=u16bit
  316. else if (l>=int64(low(longint))) and (h<=high(longint)) then
  317. range_to_basetype:=s32bit
  318. else if (l>=low(cardinal)) and (h<=high(cardinal)) then
  319. range_to_basetype:=u32bit
  320. else if (l>=low(int64)) and (h<=high(int64)) then
  321. range_to_basetype:=s64bit
  322. else
  323. range_to_basetype:=u64bit;
  324. end;
  325. procedure range_to_type(const l,h:TConstExprInt;var def:tdef);
  326. begin
  327. { prefer signed over unsigned }
  328. if (l>=int64(-128)) and (h<=127) then
  329. def:=s8inttype
  330. else if (l>=0) and (h<=255) then
  331. def:=u8inttype
  332. else if (l>=int64(-32768)) and (h<=32767) then
  333. def:=s16inttype
  334. else if (l>=0) and (h<=65535) then
  335. def:=u16inttype
  336. else if (l>=int64(low(longint))) and (h<=high(longint)) then
  337. def:=s32inttype
  338. else if (l>=low(cardinal)) and (h<=high(cardinal)) then
  339. def:=u32inttype
  340. else if (l>=low(int64)) and (h<=high(int64)) then
  341. def:=s64inttype
  342. else
  343. def:=u64inttype;
  344. end;
  345. procedure int_to_type(const v:TConstExprInt;var def:tdef);
  346. begin
  347. range_to_type(v,v,def);
  348. end;
  349. { true if p is an ordinal }
  350. function is_ordinal(def : tdef) : boolean;
  351. var
  352. dt : tordtype;
  353. begin
  354. case def.typ of
  355. orddef :
  356. begin
  357. dt:=torddef(def).ordtype;
  358. is_ordinal:=dt in [uchar,uwidechar,
  359. u8bit,u16bit,u32bit,u64bit,
  360. s8bit,s16bit,s32bit,s64bit,
  361. pasbool1,pasbool8,pasbool16,pasbool32,pasbool64,
  362. bool8bit,bool16bit,bool32bit,bool64bit];
  363. end;
  364. enumdef :
  365. is_ordinal:=true;
  366. else
  367. is_ordinal:=false;
  368. end;
  369. end;
  370. { true if p is a string }
  371. function is_string(def : tdef) : boolean;
  372. begin
  373. is_string := (assigned(def) and (def.typ = stringdef));
  374. end;
  375. function is_stringlike(def : tdef) : boolean;
  376. begin
  377. result := is_string(def) or
  378. is_anychar(def) or
  379. is_pchar(def) or
  380. is_pwidechar(def) or
  381. is_chararray(def) or
  382. is_widechararray(def) or
  383. is_open_chararray(def) or
  384. is_open_widechararray(def) or
  385. (def=java_jlstring);
  386. end;
  387. function is_enum(def : tdef) : boolean;
  388. begin
  389. result:=def.typ=enumdef;
  390. end;
  391. function is_set(def : tdef) : boolean;
  392. begin
  393. result:=def.typ=setdef;
  394. end;
  395. { returns the min. value of the type }
  396. function get_min_value(def : tdef) : TConstExprInt;
  397. begin
  398. case def.typ of
  399. orddef:
  400. result:=torddef(def).low;
  401. enumdef:
  402. result:=int64(tenumdef(def).min);
  403. else
  404. result:=0;
  405. end;
  406. end;
  407. { returns the max. value of the type }
  408. function get_max_value(def : tdef) : TConstExprInt;
  409. begin
  410. case def.typ of
  411. orddef:
  412. result:=torddef(def).high;
  413. enumdef:
  414. result:=tenumdef(def).max;
  415. else
  416. result:=0;
  417. end;
  418. end;
  419. function spans_entire_range(def: tdef): boolean;
  420. var
  421. lv, hv: Tconstexprint;
  422. mask: qword;
  423. size: longint;
  424. begin
  425. case def.typ of
  426. orddef,
  427. enumdef:
  428. getrange(def,lv,hv);
  429. else
  430. internalerror(2019062203);
  431. end;
  432. size:=def.size;
  433. case size of
  434. 1: mask:=$ff;
  435. 2: mask:=$ffff;
  436. 4: mask:=$ffffffff;
  437. 8: mask:=qword(-1);
  438. else
  439. internalerror(2019062204);
  440. end;
  441. result:=false;
  442. if is_signed(def) then
  443. begin
  444. if (lv.uvalue and mask)<>(qword(1) shl (size*8-1)) then
  445. exit;
  446. if (hv.uvalue and mask)<>(mask shr 1) then
  447. exit;
  448. end
  449. else
  450. begin
  451. if lv<>0 then
  452. exit;
  453. if hv.uvalue<>mask then
  454. exit;
  455. end;
  456. result:=true;
  457. end;
  458. { true if p is an integer }
  459. function is_integer(def : tdef) : boolean;
  460. begin
  461. result:=(def.typ=orddef) and
  462. (torddef(def).ordtype in [u8bit,u16bit,u32bit,u64bit,
  463. s8bit,s16bit,s32bit,s64bit]);
  464. end;
  465. { true if p is a boolean }
  466. function is_boolean(def : tdef) : boolean;
  467. begin
  468. result:=(def.typ=orddef) and
  469. (torddef(def).ordtype in [pasbool1,pasbool8,pasbool16,pasbool32,pasbool64,bool8bit,bool16bit,bool32bit,bool64bit]);
  470. end;
  471. function is_pasbool(def : tdef) : boolean;
  472. begin
  473. result:=(def.typ=orddef) and
  474. (torddef(def).ordtype in [pasbool1,pasbool8,pasbool16,pasbool32,pasbool64]);
  475. end;
  476. { true if def is a C-style boolean (non-zero value = true, zero = false) }
  477. function is_cbool(def : tdef) : boolean;
  478. begin
  479. result:=(def.typ=orddef) and
  480. (torddef(def).ordtype in [bool8bit,bool16bit,bool32bit,bool64bit]);
  481. end;
  482. { true if p is a void }
  483. function is_void(def : tdef) : boolean;
  484. begin
  485. result:=(def.typ=orddef) and
  486. (torddef(def).ordtype=uvoid);
  487. end;
  488. { true if p is a char }
  489. function is_char(def : tdef) : boolean;
  490. begin
  491. result:=(def.typ=orddef) and
  492. (torddef(def).ordtype=uchar);
  493. end;
  494. { true if p is a wchar }
  495. function is_widechar(def : tdef) : boolean;
  496. begin
  497. result:=(def.typ=orddef) and
  498. (torddef(def).ordtype=uwidechar);
  499. end;
  500. { true if p is a char or wchar }
  501. function is_anychar(def : tdef) : boolean;
  502. begin
  503. result:=(def.typ=orddef) and
  504. (torddef(def).ordtype in [uchar,uwidechar])
  505. end;
  506. { true if p is signed (integer) }
  507. function is_signed(def : tdef) : boolean;
  508. begin
  509. case def.typ of
  510. orddef :
  511. result:=torddef(def).low < 0;
  512. enumdef :
  513. result:=tenumdef(def).min < 0;
  514. arraydef :
  515. result:=is_signed(tarraydef(def).rangedef);
  516. else
  517. result:=false;
  518. end;
  519. end;
  520. function get_unsigned_inttype(def: tdef): torddef;
  521. begin
  522. case def.typ of
  523. orddef,
  524. enumdef:
  525. result:=cgsize_orddef(tcgsize2unsigned[def_cgsize(def)]);
  526. else
  527. internalerror(2016062001);
  528. end;
  529. end;
  530. function is_in_limit(def_from,def_to : tdef) : boolean;
  531. begin
  532. if (def_from.typ<>def_to.typ) or
  533. not(def_from.typ in [orddef,enumdef,setdef]) then
  534. begin
  535. is_in_limit := false;
  536. exit;
  537. end;
  538. case def_from.typ of
  539. orddef:
  540. is_in_limit:=(torddef(def_from).low>=torddef(def_to).low) and
  541. (torddef(def_from).high<=torddef(def_to).high);
  542. enumdef:
  543. is_in_limit:=(tenumdef(def_from).min>=tenumdef(def_to).min) and
  544. (tenumdef(def_from).max<=tenumdef(def_to).max);
  545. setdef:
  546. is_in_limit:=(tsetdef(def_from).setbase>=tsetdef(def_to).setbase) and
  547. (tsetdef(def_from).setmax<=tsetdef(def_to).setmax);
  548. else
  549. is_in_limit:=false;
  550. end;
  551. end;
  552. function is_managed_type(def: tdef): boolean;{$ifdef USEINLINE}inline;{$endif}
  553. begin
  554. result:=def.needs_inittable;
  555. end;
  556. function is_rtti_managed_type(def: tdef): boolean;
  557. begin
  558. result:=def.needs_inittable and not (
  559. is_interfacecom_or_dispinterface(def) or
  560. (def.typ=variantdef) or
  561. (
  562. (def.typ=stringdef) and
  563. (tstringdef(def).stringtype in [st_ansistring,st_widestring,st_unicodestring])
  564. )
  565. );
  566. end;
  567. { true, if p points to an open array def }
  568. function is_open_string(p : tdef) : boolean;
  569. begin
  570. is_open_string:=(p.typ=stringdef) and
  571. (tstringdef(p).stringtype=st_shortstring) and
  572. (tstringdef(p).len=0);
  573. end;
  574. { true, if p points to a zero based array def }
  575. function is_zero_based_array(p : tdef) : boolean;
  576. begin
  577. result:=(p.typ=arraydef) and
  578. (tarraydef(p).lowrange=0) and
  579. not(is_special_array(p));
  580. end;
  581. { true if p points to a dynamic array def }
  582. function is_dynamic_array(p : tdef) : boolean;
  583. begin
  584. result:=(p.typ=arraydef) and
  585. (ado_IsDynamicArray in tarraydef(p).arrayoptions);
  586. end;
  587. { true, if p points to an open array def }
  588. function is_open_array(p : tdef) : boolean;
  589. begin
  590. { check for sizesinttype is needed, because for unsigned the high
  591. range is also -1 ! (PFV) }
  592. result:=(p.typ=arraydef) and
  593. (tarraydef(p).rangedef=sizesinttype) and
  594. (tarraydef(p).lowrange=0) and
  595. (tarraydef(p).highrange=-1) and
  596. ((tarraydef(p).arrayoptions * [ado_IsVariant,ado_IsArrayOfConst,ado_IsConstructor,ado_IsDynamicArray])=[]);
  597. end;
  598. { true, if p points to an array of const def }
  599. function is_array_constructor(p : tdef) : boolean;
  600. begin
  601. result:=(p.typ=arraydef) and
  602. (ado_IsConstructor in tarraydef(p).arrayoptions);
  603. end;
  604. { true, if p points to a variant array }
  605. function is_variant_array(p : tdef) : boolean;
  606. begin
  607. result:=(p.typ=arraydef) and
  608. (ado_IsVariant in tarraydef(p).arrayoptions);
  609. end;
  610. { true, if p points to an array of const }
  611. function is_array_of_const(p : tdef) : boolean;
  612. begin
  613. result:=(p.typ=arraydef) and
  614. (ado_IsArrayOfConst in tarraydef(p).arrayoptions);
  615. end;
  616. { true, if p points to a special array, bitpacked arrays aren't special in this regard though }
  617. function is_special_array(p : tdef) : boolean;
  618. begin
  619. result:=(p.typ=arraydef) and
  620. (
  621. ((tarraydef(p).arrayoptions * [ado_IsVariant,ado_IsArrayOfConst,ado_IsConstructor,ado_IsDynamicArray])<>[]) or
  622. is_open_array(p)
  623. );
  624. end;
  625. { true if p is an ansi string def }
  626. function is_ansistring(p : tdef) : boolean;
  627. begin
  628. is_ansistring:=(p.typ=stringdef) and
  629. (tstringdef(p).stringtype=st_ansistring);
  630. end;
  631. { true if p is an ansi string def with codepage CP_NONE }
  632. function is_rawbytestring(p : tdef) : boolean;
  633. begin
  634. is_rawbytestring:=(p.typ=stringdef) and
  635. (tstringdef(p).stringtype=st_ansistring) and
  636. (tstringdef(p).encoding=globals.CP_NONE);
  637. end;
  638. { true if p is an long string def }
  639. function is_longstring(p : tdef) : boolean;
  640. begin
  641. is_longstring:=(p.typ=stringdef) and
  642. (tstringdef(p).stringtype=st_longstring);
  643. end;
  644. { true if p is an wide string def }
  645. function is_widestring(p : tdef) : boolean;
  646. begin
  647. is_widestring:=(p.typ=stringdef) and
  648. (tstringdef(p).stringtype=st_widestring);
  649. end;
  650. function is_dynamicstring(p: tdef): boolean;
  651. begin
  652. is_dynamicstring:=(p.typ=stringdef) and
  653. (tstringdef(p).stringtype in [st_ansistring,st_widestring,st_unicodestring]);
  654. end;
  655. { true if p is an wide string def }
  656. function is_wide_or_unicode_string(p : tdef) : boolean;
  657. begin
  658. is_wide_or_unicode_string:=(p.typ=stringdef) and
  659. (tstringdef(p).stringtype in [st_widestring,st_unicodestring]);
  660. end;
  661. { true if p is an unicode string def }
  662. function is_unicodestring(p : tdef) : boolean;
  663. begin
  664. is_unicodestring:=(p.typ=stringdef) and
  665. (tstringdef(p).stringtype=st_unicodestring);
  666. end;
  667. { true if p is an short string def }
  668. function is_shortstring(p : tdef) : boolean;
  669. begin
  670. is_shortstring:=(p.typ=stringdef) and
  671. (tstringdef(p).stringtype=st_shortstring);
  672. end;
  673. { true if p is bit packed array def }
  674. function is_packed_array(p: tdef) : boolean;
  675. begin
  676. is_packed_array :=
  677. (p.typ = arraydef) and
  678. (ado_IsBitPacked in tarraydef(p).arrayoptions);
  679. end;
  680. { true if p is bit packed record def }
  681. function is_packed_record_or_object(p: tdef) : boolean;
  682. begin
  683. is_packed_record_or_object :=
  684. (p.typ in [recorddef,objectdef]) and
  685. (tabstractrecorddef(p).is_packed);
  686. end;
  687. { true if p is a char array def }
  688. function is_chararray(p : tdef) : boolean;
  689. begin
  690. is_chararray:=(p.typ=arraydef) and
  691. is_char(tarraydef(p).elementdef) and
  692. not(is_special_array(p));
  693. end;
  694. { true if p is a widechar array def }
  695. function is_widechararray(p : tdef) : boolean;
  696. begin
  697. is_widechararray:=(p.typ=arraydef) and
  698. is_widechar(tarraydef(p).elementdef) and
  699. not(is_special_array(p));
  700. end;
  701. { true if p is a open char array def }
  702. function is_open_chararray(p : tdef) : boolean;
  703. begin
  704. is_open_chararray:= is_open_array(p) and
  705. is_char(tarraydef(p).elementdef);
  706. end;
  707. { true if p is a open wide char array def }
  708. function is_open_widechararray(p : tdef) : boolean;
  709. begin
  710. is_open_widechararray:= is_open_array(p) and
  711. is_widechar(tarraydef(p).elementdef);
  712. end;
  713. { true if p is any pointer def }
  714. function is_pointer(p : tdef) : boolean;
  715. begin
  716. is_pointer:=(p.typ=pointerdef);
  717. end;
  718. { true if p is a pchar def }
  719. function is_pchar(p : tdef) : boolean;
  720. begin
  721. is_pchar:=(p.typ=pointerdef) and
  722. (is_char(tpointerdef(p).pointeddef) or
  723. (is_zero_based_array(tpointerdef(p).pointeddef) and
  724. is_chararray(tpointerdef(p).pointeddef)));
  725. end;
  726. { true if p is a pchar def }
  727. function is_pwidechar(p : tdef) : boolean;
  728. begin
  729. is_pwidechar:=(p.typ=pointerdef) and
  730. (is_widechar(tpointerdef(p).pointeddef) or
  731. (is_zero_based_array(tpointerdef(p).pointeddef) and
  732. is_widechararray(tpointerdef(p).pointeddef)));
  733. end;
  734. { true if p is a voidpointer def }
  735. function is_voidpointer(p : tdef) : boolean;
  736. begin
  737. is_voidpointer:=(p.typ=pointerdef) and
  738. (tpointerdef(p).pointeddef.typ=orddef) and
  739. (torddef(tpointerdef(p).pointeddef).ordtype=uvoid);
  740. end;
  741. { true, if def is a 8 bit int type }
  742. function is_8bitint(def : tdef) : boolean;
  743. begin
  744. result:=(def.typ=orddef) and (torddef(def).ordtype in [u8bit,s8bit])
  745. end;
  746. { true, if def is a 8 bit ordinal type }
  747. function is_8bit(def : tdef) : boolean;
  748. begin
  749. result:=(def.typ=orddef) and (torddef(def).ordtype in [u8bit,s8bit,pasbool1,pasbool8,bool8bit,uchar])
  750. end;
  751. { true, if def is a 16 bit int type }
  752. function is_16bitint(def : tdef) : boolean;
  753. begin
  754. result:=(def.typ=orddef) and (torddef(def).ordtype in [u16bit,s16bit])
  755. end;
  756. { true, if def is a 16 bit ordinal type }
  757. function is_16bit(def : tdef) : boolean;
  758. begin
  759. result:=(def.typ=orddef) and (torddef(def).ordtype in [u16bit,s16bit,pasbool16,bool16bit,uwidechar])
  760. end;
  761. { true, if def is a 32 bit int type }
  762. function is_32bitint(def : tdef) : boolean;
  763. begin
  764. result:=(def.typ=orddef) and (torddef(def).ordtype in [u32bit,s32bit])
  765. end;
  766. { true, if def is a 32 bit ordinal type }
  767. function is_32bit(def: tdef): boolean;
  768. begin
  769. result:=(def.typ=orddef) and (torddef(def).ordtype in [u32bit,s32bit,pasbool32,bool32bit])
  770. end;
  771. { true, if def is a 64 bit int type }
  772. function is_64bitint(def : tdef) : boolean;
  773. begin
  774. is_64bitint:=(def.typ=orddef) and (torddef(def).ordtype in [u64bit,s64bit])
  775. end;
  776. { true, if def is a 64 bit type }
  777. function is_64bit(def : tdef) : boolean;
  778. begin
  779. is_64bit:=(def.typ=orddef) and (torddef(def).ordtype in [u64bit,s64bit,scurrency,pasbool64,bool64bit])
  780. end;
  781. { true, if def1 and def2 are both integers of the same bit size and sign }
  782. function are_equal_ints(def1, def2: tdef): boolean;
  783. begin
  784. result:=(def1.typ=orddef) and (def2.typ=orddef) and
  785. (torddef(def1).ordtype in [u8bit,u16bit,u32bit,u64bit,
  786. s8bit,s16bit,s32bit,s64bit]) and
  787. (torddef(def1).ordtype=torddef(def2).ordtype);
  788. end;
  789. { true, if def is an int type, larger than the processor's native int size }
  790. function is_oversizedint(def : tdef) : boolean;
  791. begin
  792. {$if defined(cpu8bitalu)}
  793. result:=is_64bitint(def) or is_32bitint(def) or is_16bitint(def);
  794. {$elseif defined(cpu16bitalu)}
  795. result:=is_64bitint(def) or is_32bitint(def);
  796. {$elseif defined(cpu32bitaddr)}
  797. result:=is_64bitint(def);
  798. {$elseif defined(cpu64bitaddr)}
  799. result:=false;
  800. {$endif}
  801. end;
  802. { true, if def is an ordinal type, larger than the processor's native int size }
  803. function is_oversizedord(def : tdef) : boolean;
  804. begin
  805. {$if defined(cpu8bitalu)}
  806. result:=is_64bit(def) or is_32bit(def) or is_16bit(def);
  807. {$elseif defined(cpu16bitalu)}
  808. result:=is_64bit(def) or is_32bit(def);
  809. {$elseif defined(cpu32bitaddr)}
  810. result:=is_64bit(def);
  811. {$elseif defined(cpu64bitaddr)}
  812. result:=false;
  813. {$endif}
  814. end;
  815. { true, if def is an int type, equal in size to the processor's native int size }
  816. function is_nativeint(def: tdef): boolean;
  817. begin
  818. {$if defined(cpu8bitalu)}
  819. result:=is_8bitint(def);
  820. {$elseif defined(cpu16bitalu)}
  821. result:=is_16bitint(def);
  822. {$elseif defined(cpu32bitaddr)}
  823. result:=is_32bitint(def);
  824. {$elseif defined(cpu64bitaddr)}
  825. result:=is_64bitint(def);
  826. {$endif}
  827. end;
  828. { true, if def is an ordinal type, equal in size to the processor's native int size }
  829. function is_nativeord(def: tdef): boolean;
  830. begin
  831. {$if defined(cpu8bitalu)}
  832. result:=is_8bit(def);
  833. {$elseif defined(cpu16bitalu)}
  834. result:=is_16bit(def);
  835. {$elseif defined(cpu32bitaddr)}
  836. result:=is_32bit(def);
  837. {$elseif defined(cpu64bitaddr)}
  838. result:=is_64bit(def);
  839. {$endif}
  840. end;
  841. { true, if def is an unsigned int type, equal in size to the processor's native int size }
  842. function is_nativeuint(def: tdef): boolean;
  843. begin
  844. result:=is_nativeint(def) and (def.typ=orddef) and (torddef(def).ordtype in [u64bit,u32bit,u16bit,u8bit]);
  845. end;
  846. { true, if def is a signed int type, equal in size to the processor's native int size }
  847. function is_nativesint(def: tdef): boolean;
  848. begin
  849. result:=is_nativeint(def) and (def.typ=orddef) and (torddef(def).ordtype in [s64bit,s32bit,s16bit,s8bit]);
  850. end;
  851. { if l isn't in the range of todef a range check error (if not explicit) is generated and
  852. the value is placed within the range }
  853. procedure adaptrange(todef : tdef;var l : tconstexprint; rangecheck: tperformrangecheck);
  854. var
  855. lv,hv,oldval,sextval,mask: TConstExprInt;
  856. rangedef: tdef;
  857. rangedefsize: longint;
  858. warned: boolean;
  859. begin
  860. getrange(todef,lv,hv);
  861. if (l<lv) or (l>hv) then
  862. begin
  863. warned:=false;
  864. if rangecheck in [rc_implicit,rc_yes] then
  865. begin
  866. if (rangecheck=rc_yes) or
  867. (todef.typ=enumdef) then
  868. Message3(type_e_range_check_error_bounds,tostr(l),tostr(lv),tostr(hv))
  869. else
  870. Message3(type_w_range_check_error_bounds,tostr(l),tostr(lv),tostr(hv));
  871. warned:=true;
  872. end
  873. { give warnings about range errors with explicit typeconversions if the target
  874. type does not span the entire range that can be represented by its bits
  875. (subrange type or enum), because then the result is undefined }
  876. else if (rangecheck<>rc_internal) and
  877. (not is_pasbool(todef) and
  878. not spans_entire_range(todef)) then
  879. begin
  880. Message3(type_w_range_check_error_bounds,tostr(l),tostr(lv),tostr(hv));
  881. warned:=true;
  882. end;
  883. { Fix the value to fit in the allocated space for this type of variable }
  884. oldval:=l;
  885. getrangedefmasksize(todef,rangedef,mask,rangedefsize);
  886. l:=l and mask;
  887. {reset sign, i.e. converting -1 to qword changes the value to high(qword)}
  888. l.signed:=false;
  889. sextval:=0;
  890. { do sign extension if necessary (JM) }
  891. case rangedefsize of
  892. 1: sextval.svalue:=shortint(l.svalue);
  893. 2: sextval.svalue:=smallint(l.svalue);
  894. 4: sextval.svalue:=longint(l.svalue);
  895. 8: sextval.svalue:=l.svalue;
  896. else
  897. internalerror(201906230);
  898. end;
  899. sextval.signed:=true;
  900. { Detect if the type spans the entire range, but more bits were specified than
  901. the type can contain, e.g. shortint($fff).
  902. However, none of the following should result in a warning:
  903. 1) shortint($ff) (-> $ff -> $ff -> $ffff ffff ffff ffff)
  904. 2) shortint(longint(-1)) ($ffff ffff ffff ffff ffff -> $ff -> $ffff ffff ffff ffff
  905. 3) cardinal(-1) (-> $ffff ffff ffff ffff -> $ffff ffff)
  906. }
  907. if not warned and
  908. (rangecheck<>rc_internal) and
  909. (oldval.uvalue<>l.uvalue) and
  910. (oldval.uvalue<>sextval.uvalue) then
  911. begin
  912. Message3(type_w_range_check_error_bounds,tostr(oldval),tostr(lv),tostr(hv));
  913. end;
  914. if is_signed(rangedef) then
  915. l:=sextval;
  916. end;
  917. end;
  918. procedure adaptrange(todef: tdef; var l: tconstexprint; internal, explicit, rangecheckstate: boolean);
  919. begin
  920. if internal then
  921. adaptrange(todef, l, rc_internal)
  922. else if explicit then
  923. adaptrange(todef, l, rc_explicit)
  924. else if not rangecheckstate then
  925. adaptrange(todef, l, rc_implicit)
  926. else
  927. adaptrange(todef, l, rc_yes)
  928. end;
  929. { return the range from def in l and h }
  930. procedure getrange(def : tdef;out l, h : TConstExprInt);
  931. begin
  932. case def.typ of
  933. orddef :
  934. begin
  935. l:=torddef(def).low;
  936. h:=torddef(def).high;
  937. end;
  938. enumdef :
  939. begin
  940. l:=int64(tenumdef(def).min);
  941. h:=int64(tenumdef(def).max);
  942. end;
  943. arraydef :
  944. begin
  945. l:=int64(tarraydef(def).lowrange);
  946. h:=int64(tarraydef(def).highrange);
  947. end;
  948. undefineddef:
  949. begin
  950. l:=torddef(sizesinttype).low;
  951. h:=torddef(sizesinttype).high;
  952. end;
  953. else
  954. internalerror(200611054);
  955. end;
  956. end;
  957. procedure getrangedefmasksize(def: tdef; out rangedef: tdef; out mask: TConstExprInt; out size: longint);
  958. begin
  959. case def.typ of
  960. orddef, enumdef:
  961. begin
  962. rangedef:=def;
  963. size:=def.size;
  964. case size of
  965. 1: mask:=$ff;
  966. 2: mask:=$ffff;
  967. 4: mask:=$ffffffff;
  968. 8: mask:=$ffffffffffffffff;
  969. else
  970. internalerror(2019062305);
  971. end;
  972. end;
  973. arraydef:
  974. begin
  975. rangedef:=tarraydef(def).rangedef;
  976. getrangedefmasksize(rangedef,rangedef,mask,size);
  977. end;
  978. undefineddef:
  979. begin
  980. rangedef:=sizesinttype;
  981. size:=rangedef.size;
  982. mask:=-1;
  983. end;
  984. else
  985. internalerror(2019062306);
  986. end;
  987. end;
  988. function mmx_type(p : tdef) : tmmxtype;
  989. begin
  990. mmx_type:=mmxno;
  991. if is_mmx_able_array(p) then
  992. begin
  993. if tarraydef(p).elementdef.typ=floatdef then
  994. case tfloatdef(tarraydef(p).elementdef).floattype of
  995. s32real:
  996. mmx_type:=mmxsingle;
  997. end
  998. else
  999. case torddef(tarraydef(p).elementdef).ordtype of
  1000. u8bit:
  1001. mmx_type:=mmxu8bit;
  1002. s8bit:
  1003. mmx_type:=mmxs8bit;
  1004. u16bit:
  1005. mmx_type:=mmxu16bit;
  1006. s16bit:
  1007. mmx_type:=mmxs16bit;
  1008. u32bit:
  1009. mmx_type:=mmxu32bit;
  1010. s32bit:
  1011. mmx_type:=mmxs32bit;
  1012. end;
  1013. end;
  1014. end;
  1015. { The range-type of an ordinal-type that is a subrange-type shall be the host-type (see 6.4.2.4) of the subrange-type.
  1016. The range-type of an ordinal-type that is not a subrange-type shall be the ordinal-type.
  1017. The subrange-bounds shall be of compatible ordinal-types, and the range-type (see 6.4.2.1) of the ordinal-types shall
  1018. be designated the host-type of the subrange-type. }
  1019. function get_iso_range_type(def: tdef): tdef;
  1020. begin
  1021. result:=nil;
  1022. case def.typ of
  1023. orddef:
  1024. begin
  1025. if is_integer(def) then
  1026. begin
  1027. if (torddef(def).low>=torddef(sinttype).low) and
  1028. (torddef(def).high<=torddef(sinttype).high) then
  1029. result:=sinttype
  1030. else
  1031. range_to_type(torddef(def).low,torddef(def).high,result);
  1032. end
  1033. else case torddef(def).ordtype of
  1034. pasbool1:
  1035. result:=pasbool1type;
  1036. pasbool8:
  1037. result:=pasbool8type;
  1038. pasbool16:
  1039. result:=pasbool16type;
  1040. pasbool32:
  1041. result:=pasbool32type;
  1042. pasbool64:
  1043. result:=pasbool64type;
  1044. bool8bit:
  1045. result:=bool8type;
  1046. bool16bit:
  1047. result:=bool16type;
  1048. bool32bit:
  1049. result:=bool32type;
  1050. bool64bit:
  1051. result:=bool64type;
  1052. uchar:
  1053. result:=cansichartype;
  1054. uwidechar:
  1055. result:=cwidechartype;
  1056. scurrency:
  1057. result:=s64currencytype;
  1058. else
  1059. internalerror(2018010901);
  1060. end;
  1061. end;
  1062. enumdef:
  1063. begin
  1064. while assigned(tenumdef(def).basedef) do
  1065. def:=tenumdef(def).basedef;
  1066. result:=def;
  1067. end
  1068. else
  1069. internalerror(2018010701);
  1070. end;
  1071. end;
  1072. function is_vector(p : tdef) : boolean;
  1073. begin
  1074. result:=(p.typ=arraydef) and
  1075. not(is_special_array(p)) and
  1076. (tarraydef(p).elementdef.typ=floatdef) and
  1077. (tfloatdef(tarraydef(p).elementdef).floattype in [s32real,s64real]);
  1078. end;
  1079. { returns if the passed type (array) fits into an mm register }
  1080. function fits_in_mm_register(p : tdef) : boolean;
  1081. begin
  1082. {$ifdef x86}
  1083. result:= is_vector(p) and
  1084. (
  1085. (tarraydef(p).elementdef.typ=floatdef) and
  1086. (
  1087. (tarraydef(p).lowrange=0) and
  1088. (tarraydef(p).highrange=3) and
  1089. (tfloatdef(tarraydef(p).elementdef).floattype=s32real)
  1090. )
  1091. ) or
  1092. (
  1093. (tarraydef(p).elementdef.typ=floatdef) and
  1094. (
  1095. (tarraydef(p).lowrange=0) and
  1096. (tarraydef(p).highrange=1) and
  1097. (tfloatdef(tarraydef(p).elementdef).floattype=s64real)
  1098. )
  1099. );
  1100. {$else x86}
  1101. result:=false;
  1102. {$endif x86}
  1103. end;
  1104. function is_mmx_able_array(p : tdef) : boolean;
  1105. begin
  1106. {$ifdef SUPPORT_MMX}
  1107. if (cs_mmx_saturation in current_settings.localswitches) then
  1108. begin
  1109. is_mmx_able_array:=(p.typ=arraydef) and
  1110. not(is_special_array(p)) and
  1111. (
  1112. (
  1113. (tarraydef(p).elementdef.typ=orddef) and
  1114. (
  1115. (
  1116. (tarraydef(p).lowrange=0) and
  1117. (tarraydef(p).highrange=1) and
  1118. (torddef(tarraydef(p).elementdef).ordtype in [u32bit,s32bit])
  1119. )
  1120. or
  1121. (
  1122. (tarraydef(p).lowrange=0) and
  1123. (tarraydef(p).highrange=3) and
  1124. (torddef(tarraydef(p).elementdef).ordtype in [u16bit,s16bit])
  1125. )
  1126. )
  1127. )
  1128. or
  1129. (
  1130. (
  1131. (tarraydef(p).elementdef.typ=floatdef) and
  1132. (
  1133. (tarraydef(p).lowrange=0) and
  1134. (tarraydef(p).highrange=1) and
  1135. (tfloatdef(tarraydef(p).elementdef).floattype=s32real)
  1136. )
  1137. )
  1138. )
  1139. );
  1140. end
  1141. else
  1142. begin
  1143. is_mmx_able_array:=(p.typ=arraydef) and
  1144. (
  1145. (
  1146. (tarraydef(p).elementdef.typ=orddef) and
  1147. (
  1148. (
  1149. (tarraydef(p).lowrange=0) and
  1150. (tarraydef(p).highrange=1) and
  1151. (torddef(tarraydef(p).elementdef).ordtype in [u32bit,s32bit])
  1152. )
  1153. or
  1154. (
  1155. (tarraydef(p).lowrange=0) and
  1156. (tarraydef(p).highrange=3) and
  1157. (torddef(tarraydef(p).elementdef).ordtype in [u16bit,s16bit])
  1158. )
  1159. or
  1160. (
  1161. (tarraydef(p).lowrange=0) and
  1162. (tarraydef(p).highrange=7) and
  1163. (torddef(tarraydef(p).elementdef).ordtype in [u8bit,s8bit])
  1164. )
  1165. )
  1166. )
  1167. or
  1168. (
  1169. (tarraydef(p).elementdef.typ=floatdef) and
  1170. (
  1171. (tarraydef(p).lowrange=0) and
  1172. (tarraydef(p).highrange=1) and
  1173. (tfloatdef(tarraydef(p).elementdef).floattype=s32real)
  1174. )
  1175. )
  1176. );
  1177. end;
  1178. {$else SUPPORT_MMX}
  1179. is_mmx_able_array:=false;
  1180. {$endif SUPPORT_MMX}
  1181. end;
  1182. function def_cgsize(def: tdef): tcgsize;
  1183. begin
  1184. case def.typ of
  1185. orddef,
  1186. enumdef,
  1187. setdef:
  1188. begin
  1189. result:=int_cgsize(def.size);
  1190. if is_signed(def) then
  1191. result:=tcgsize(ord(result)+(ord(OS_S8)-ord(OS_8)));
  1192. end;
  1193. classrefdef,
  1194. pointerdef:
  1195. begin
  1196. result:=int_cgsize(def.size);
  1197. { can happen for far/huge pointers on non-i8086 }
  1198. if result=OS_NO then
  1199. internalerror(2013052201);
  1200. end;
  1201. formaldef:
  1202. result := int_cgsize(voidpointertype.size);
  1203. procvardef:
  1204. result:=int_cgsize(def.size);
  1205. stringdef :
  1206. result:=int_cgsize(def.size);
  1207. objectdef :
  1208. result:=int_cgsize(def.size);
  1209. floatdef:
  1210. if cs_fp_emulation in current_settings.moduleswitches then
  1211. result:=int_cgsize(def.size)
  1212. else
  1213. result:=tfloat2tcgsize[tfloatdef(def).floattype];
  1214. recorddef :
  1215. result:=int_cgsize(def.size);
  1216. arraydef :
  1217. begin
  1218. if is_dynamic_array(def) or not is_special_array(def) then
  1219. begin
  1220. if (cs_support_vectors in current_settings.globalswitches) and is_vector(def) and ((TArrayDef(def).elementdef.typ = floatdef) and not (cs_fp_emulation in current_settings.moduleswitches)) then
  1221. begin
  1222. { Determine if, based on the floating-point type and the size
  1223. of the array, if it can be made into a vector }
  1224. case TFloatDef(def).floattype of
  1225. s32real:
  1226. result := float_array_cgsize(def.size);
  1227. s64real:
  1228. result := double_array_cgsize(def.size);
  1229. else
  1230. { If not, fall back }
  1231. result := int_cgsize(def.size);
  1232. end;
  1233. end
  1234. else
  1235. result := int_cgsize(def.size);
  1236. end
  1237. else
  1238. result := OS_NO;
  1239. end;
  1240. else
  1241. begin
  1242. { undefined size }
  1243. result:=OS_NO;
  1244. end;
  1245. end;
  1246. end;
  1247. function cgsize_orddef(size: tcgsize): torddef;
  1248. begin
  1249. case size of
  1250. OS_8:
  1251. result:=torddef(u8inttype);
  1252. OS_S8:
  1253. result:=torddef(s8inttype);
  1254. OS_16:
  1255. result:=torddef(u16inttype);
  1256. OS_S16:
  1257. result:=torddef(s16inttype);
  1258. OS_32:
  1259. result:=torddef(u32inttype);
  1260. OS_S32:
  1261. result:=torddef(s32inttype);
  1262. OS_64:
  1263. result:=torddef(u64inttype);
  1264. OS_S64:
  1265. result:=torddef(s64inttype);
  1266. else
  1267. internalerror(2012050401);
  1268. end;
  1269. end;
  1270. function def_cgmmsize(def: tdef): tcgsize;
  1271. begin
  1272. case def.typ of
  1273. arraydef:
  1274. begin
  1275. case tarraydef(def).elementdef.typ of
  1276. orddef:
  1277. begin
  1278. { this is not correct, OS_MX normally mean that the vector
  1279. contains elements of size X. However, vectors themselves
  1280. can also have different sizes (e.g. a vector of 2 singles on
  1281. SSE) and the total size is currently more important }
  1282. case def.size of
  1283. 1: result:=OS_M8;
  1284. 2: result:=OS_M16;
  1285. 4: result:=OS_M32;
  1286. 8: result:=OS_M64;
  1287. 16: result:=OS_M128;
  1288. 32: result:=OS_M256;
  1289. 64: result:=OS_M512;
  1290. else
  1291. internalerror(2013060103);
  1292. end;
  1293. end;
  1294. floatdef:
  1295. begin
  1296. case TFloatDef(tarraydef(def).elementdef).floattype of
  1297. s32real:
  1298. case def.size of
  1299. 4: result:=OS_MF32;
  1300. 16: result:=OS_MF128;
  1301. 32: result:=OS_MF256;
  1302. 64: result:=OS_MF512;
  1303. else
  1304. internalerror(2017121400);
  1305. end;
  1306. s64real:
  1307. case def.size of
  1308. 8: result:=OS_MD64;
  1309. 16: result:=OS_MD128;
  1310. 32: result:=OS_MD256;
  1311. 64: result:=OS_MD512;
  1312. else
  1313. internalerror(2017121401);
  1314. end;
  1315. else
  1316. internalerror(2017121402);
  1317. end;
  1318. end;
  1319. else
  1320. result:=def_cgsize(def);
  1321. end;
  1322. end
  1323. else
  1324. result:=def_cgsize(def);
  1325. end;
  1326. end;
  1327. { In Windows 95 era, ordinals were restricted to [u8bit,s32bit,s16bit,bool16bit]
  1328. As of today, both signed and unsigned types from 8 to 64 bits are supported. }
  1329. function is_automatable(p : tdef) : boolean;
  1330. begin
  1331. result:=false;
  1332. case p.typ of
  1333. orddef:
  1334. result:=torddef(p).ordtype in [u8bit,s8bit,u16bit,s16bit,u32bit,s32bit,
  1335. u64bit,s64bit,bool16bit,scurrency];
  1336. floatdef:
  1337. result:=tfloatdef(p).floattype in [s64currency,s64real,s32real];
  1338. stringdef:
  1339. result:=tstringdef(p).stringtype in [st_ansistring,st_widestring,st_unicodestring];
  1340. variantdef:
  1341. result:=true;
  1342. objectdef:
  1343. result:=tobjectdef(p).objecttype in [odt_interfacecom,odt_dispinterface,odt_interfacecorba];
  1344. end;
  1345. end;
  1346. {# returns true, if the type passed is a varset }
  1347. function is_smallset(p : tdef) : boolean;
  1348. begin
  1349. {$if defined(cpu8bitalu)}
  1350. result:=(p.typ=setdef) and (p.size = 1)
  1351. {$elseif defined(cpu16bitalu)}
  1352. result:=(p.typ=setdef) and (p.size in [1,2])
  1353. {$else}
  1354. result:=(p.typ=setdef) and (p.size in [1,2,4])
  1355. {$endif}
  1356. end;
  1357. function is_bareprocdef(pd : tprocdef): boolean;
  1358. begin
  1359. result:=(pd.maxparacount=0) and
  1360. (is_void(pd.returndef) or
  1361. (pd.proctypeoption = potype_constructor));
  1362. end;
  1363. function get_common_intdef(ld, rd: torddef; keep_sign_if_equal: boolean): torddef;
  1364. var
  1365. llow, lhigh: tconstexprint;
  1366. begin
  1367. llow:=min(ld.low,rd.low);
  1368. lhigh:=max(ld.high,rd.high);
  1369. case range_to_basetype(llow,lhigh) of
  1370. s8bit:
  1371. result:=torddef(s8inttype);
  1372. u8bit:
  1373. result:=torddef(u8inttype);
  1374. s16bit:
  1375. result:=torddef(s16inttype);
  1376. u16bit:
  1377. result:=torddef(u16inttype);
  1378. s32bit:
  1379. result:=torddef(s32inttype);
  1380. u32bit:
  1381. result:=torddef(u32inttype);
  1382. s64bit:
  1383. result:=torddef(s64inttype);
  1384. u64bit:
  1385. result:=torddef(u64inttype);
  1386. else
  1387. begin
  1388. { avoid warning }
  1389. result:=nil;
  1390. internalerror(200802291);
  1391. end;
  1392. end;
  1393. if keep_sign_if_equal and
  1394. (is_signed(ld)=is_signed(rd)) and
  1395. (is_signed(result)<>is_signed(ld)) then
  1396. case result.ordtype of
  1397. s8bit:
  1398. result:=torddef(u8inttype);
  1399. u8bit:
  1400. result:=torddef(s16inttype);
  1401. s16bit:
  1402. result:=torddef(u16inttype);
  1403. u16bit:
  1404. result:=torddef(s32inttype);
  1405. s32bit:
  1406. result:=torddef(u32inttype);
  1407. u32bit:
  1408. result:=torddef(s64inttype);
  1409. s64bit:
  1410. result:=torddef(u64inttype);
  1411. end;
  1412. end;
  1413. function is_valid_univ_para_type(def: tdef): boolean;
  1414. begin
  1415. result:=
  1416. not is_open_array(def) and
  1417. not is_void(def) and
  1418. (def.typ<>formaldef);
  1419. end;
  1420. function is_nested_pd(def: tabstractprocdef): boolean;{$ifdef USEINLINE}inline;{$endif}
  1421. begin
  1422. result:=def.parast.symtablelevel>normal_function_level;
  1423. end;
  1424. function is_typeparam(def : tdef) : boolean;{$ifdef USEINLINE}inline;{$endif}
  1425. begin
  1426. result:=(def.typ=undefineddef);
  1427. end;
  1428. function is_methodpointer(def: tdef): boolean;
  1429. begin
  1430. result:=(def.typ=procvardef) and (po_methodpointer in tprocvardef(def).procoptions);
  1431. end;
  1432. function is_block(def: tdef): boolean;
  1433. begin
  1434. result:=(def.typ=procvardef) and (po_is_block in tprocvardef(def).procoptions)
  1435. end;
  1436. function get_typekind(def:tdef):byte;
  1437. begin
  1438. case def.typ of
  1439. arraydef:
  1440. if ado_IsDynamicArray in tarraydef(def).arrayoptions then
  1441. result:=tkDynArray
  1442. else
  1443. result:=tkArray;
  1444. recorddef:
  1445. result:=tkRecord;
  1446. pointerdef:
  1447. result:=tkPointer;
  1448. orddef:
  1449. case torddef(def).ordtype of
  1450. u8bit,
  1451. u16bit,
  1452. u32bit,
  1453. s8bit,
  1454. s16bit,
  1455. s32bit:
  1456. result:=tkInteger;
  1457. u64bit:
  1458. result:=tkQWord;
  1459. s64bit:
  1460. result:=tkInt64;
  1461. pasbool1,
  1462. pasbool8,
  1463. pasbool16,
  1464. pasbool32,
  1465. pasbool64,
  1466. bool8bit,
  1467. bool16bit,
  1468. bool32bit,
  1469. bool64bit:
  1470. result:=tkBool;
  1471. uchar:
  1472. result:=tkChar;
  1473. uwidechar:
  1474. result:=tkWChar;
  1475. scurrency:
  1476. result:=tkFloat;
  1477. else
  1478. result:=tkUnknown;
  1479. end;
  1480. stringdef:
  1481. case tstringdef(def).stringtype of
  1482. st_shortstring:
  1483. result:=tkSString;
  1484. st_longstring:
  1485. result:=tkLString;
  1486. st_ansistring:
  1487. result:=tkAString;
  1488. st_widestring:
  1489. result:=tkWString;
  1490. st_unicodestring:
  1491. result:=tkUString;
  1492. else
  1493. result:=tkUnknown;
  1494. end;
  1495. enumdef:
  1496. result:=tkEnumeration;
  1497. objectdef:
  1498. case tobjectdef(def).objecttype of
  1499. odt_class,
  1500. odt_javaclass:
  1501. result:=tkClass;
  1502. odt_object:
  1503. result:=tkObject;
  1504. odt_interfacecom,
  1505. odt_dispinterface,
  1506. odt_interfacejava:
  1507. result:=tkInterface;
  1508. odt_interfacecorba:
  1509. result:=tkInterfaceCorba;
  1510. odt_helper:
  1511. result:=tkHelper;
  1512. else
  1513. result:=tkUnknown;
  1514. end;
  1515. { currently tkFile is not used }
  1516. {filedef:
  1517. result:=tkFile;}
  1518. setdef:
  1519. result:=tkSet;
  1520. procvardef:
  1521. if tprocvardef(def).is_methodpointer then
  1522. result:=tkMethod
  1523. else
  1524. result:=tkProcVar;
  1525. floatdef:
  1526. result:=tkFloat;
  1527. classrefdef:
  1528. result:=tkClassRef;
  1529. variantdef:
  1530. result:=tkVariant;
  1531. else
  1532. result:=tkUnknown;
  1533. end;
  1534. end;
  1535. end.