defutil.pas 71 KB

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