defutil.pas 64 KB

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