defutil.pas 60 KB

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