defutil.pas 68 KB

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