defutil.pas 69 KB

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