defutil.pas 64 KB

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