defutil.pas 71 KB

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