defutil.pas 72 KB

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