defutil.pas 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442
  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);
  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(l,h:TConstExprInt):tordtype;
  47. procedure range_to_type(l,h:TConstExprInt;var def:tdef);
  48. procedure int_to_type(v:TConstExprInt;var def:tdef);
  49. {# Returns true, if definition defines an integer type }
  50. function is_integer(def : tdef) : boolean;
  51. {# Returns true if definition is a boolean }
  52. function is_boolean(def : tdef) : boolean;
  53. {# Returns true if definition is a Pascal-style boolean (1 = true, zero = false) }
  54. function is_pasbool(def : tdef) : boolean;
  55. {# Returns true if definition is a C-style boolean (non-zero value = true, zero = false) }
  56. function is_cbool(def : tdef) : boolean;
  57. {# Returns true if definition is a char
  58. This excludes the unicode char.
  59. }
  60. function is_char(def : tdef) : boolean;
  61. {# Returns true if definition is a widechar }
  62. function is_widechar(def : tdef) : boolean;
  63. {# Returns true if definition is either an AnsiChar or a WideChar }
  64. function is_anychar(def : tdef) : boolean;
  65. {# Returns true if definition is a void}
  66. function is_void(def : tdef) : boolean;
  67. {# Returns true if definition is a smallset}
  68. function is_smallset(p : tdef) : boolean;
  69. {# Returns true, if def defines a signed data type
  70. (only for ordinal types)
  71. }
  72. function is_signed(def : tdef) : boolean;
  73. {# Returns an unsigned integer type of the same size as def; def must be
  74. an ordinal or enum }
  75. function get_unsigned_inttype(def: tdef): torddef;
  76. {# Returns whether def_from's range is comprised in def_to's if both are
  77. orddefs, false otherwise }
  78. function is_in_limit(def_from,def_to : tdef) : boolean;
  79. {# Returns whether def is reference counted }
  80. function is_managed_type(def: tdef) : boolean;{$ifdef USEINLINE}inline;{$endif}
  81. { # Returns whether def is needs to load RTTI for reference counting }
  82. function is_rtti_managed_type(def: tdef) : boolean;
  83. { function is_in_limit_value(val_from:TConstExprInt;def_from,def_to : tdef) : boolean;}
  84. {*****************************************************************************
  85. Array helper functions
  86. *****************************************************************************}
  87. {# Returns true, if p points to a zero based (non special like open or
  88. dynamic array def).
  89. This is mainly used to see if the array
  90. is convertable to a pointer
  91. }
  92. function is_zero_based_array(p : tdef) : boolean;
  93. {# Returns true if p points to an open array definition }
  94. function is_open_array(p : tdef) : boolean;
  95. {# Returns true if p points to a dynamic array definition }
  96. function is_dynamic_array(p : tdef) : boolean;
  97. {# Returns true, if p points to an array of const definition }
  98. function is_array_constructor(p : tdef) : boolean;
  99. {# Returns true, if p points to a variant array }
  100. function is_variant_array(p : tdef) : boolean;
  101. {# Returns true, if p points to an array of const }
  102. function is_array_of_const(p : tdef) : boolean;
  103. {# Returns true, if p points any kind of special array
  104. That is if the array is an open array, a variant
  105. array, an array constants constructor, or an
  106. array of const.
  107. Bitpacked arrays aren't special in this regard though.
  108. }
  109. function is_special_array(p : tdef) : boolean;
  110. {# Returns true if p is a bitpacked array }
  111. function is_packed_array(p: tdef) : boolean;
  112. {# Returns true if p is a bitpacked record }
  113. function is_packed_record_or_object(p: tdef) : boolean;
  114. {# Returns true if p is a char array def }
  115. function is_chararray(p : tdef) : boolean;
  116. {# Returns true if p is a wide char array def }
  117. function is_widechararray(p : tdef) : boolean;
  118. {# Returns true if p is a open char array def }
  119. function is_open_chararray(p : tdef) : boolean;
  120. {# Returns true if p is a open wide char array def }
  121. function is_open_widechararray(p : tdef) : boolean;
  122. {*****************************************************************************
  123. String helper functions
  124. *****************************************************************************}
  125. {# Returns true if p points to an open string type }
  126. function is_open_string(p : tdef) : boolean;
  127. {# Returns true if p is an ansi string type }
  128. function is_ansistring(p : tdef) : boolean;
  129. {# Returns true if p is an ansi string type with codepage 0 }
  130. function is_rawbytestring(p : tdef) : boolean;
  131. {# Returns true if p is a long string type }
  132. function is_longstring(p : tdef) : boolean;
  133. {# returns true if p is a wide string type }
  134. function is_widestring(p : tdef) : boolean;
  135. {# true if p is an unicode string def }
  136. function is_unicodestring(p : tdef) : boolean;
  137. {# true if p is an unicode/wide/ansistring string def }
  138. function is_dynamicstring(p : tdef) : boolean;
  139. {# returns true if p is a wide or unicode string type }
  140. function is_wide_or_unicode_string(p : tdef) : boolean;
  141. {# Returns true if p is a short string type }
  142. function is_shortstring(p : tdef) : boolean;
  143. {# Returns true if p is a pchar def }
  144. function is_pchar(p : tdef) : boolean;
  145. {# Returns true if p is a pwidechar def }
  146. function is_pwidechar(p : tdef) : boolean;
  147. {# Returns true if p is a voidpointer def }
  148. function is_voidpointer(p : tdef) : boolean;
  149. {# Returns true, if definition is a float }
  150. function is_fpu(def : tdef) : boolean;
  151. {# Returns true, if def is a currency type }
  152. function is_currency(def : tdef) : boolean;
  153. {# Returns true, if def is a single type }
  154. function is_single(def : tdef) : boolean;
  155. {# Returns true, if def is a double type }
  156. function is_double(def : tdef) : boolean;
  157. {# Returns true, if def is an extended type }
  158. function is_extended(def : tdef) : boolean;
  159. {# Returns true, if definition is a "real" real (i.e. single/double/extended) }
  160. function is_real(def : tdef) : boolean;
  161. {# Returns true for single,double,extended and cextended }
  162. function is_real_or_cextended(def : tdef) : boolean;
  163. { true, if def is a 8 bit int type }
  164. function is_8bitint(def : tdef) : boolean;
  165. { true, if def is a 8 bit ordinal type }
  166. function is_8bit(def : tdef) : boolean;
  167. { true, if def is a 16 bit int type }
  168. function is_16bitint(def : tdef) : boolean;
  169. { true, if def is a 16 bit ordinal type }
  170. function is_16bit(def : tdef) : boolean;
  171. {# Returns true, if def is a 32 bit integer type }
  172. function is_32bitint(def : tdef) : boolean;
  173. {# Returns true, if def is a 32 bit ordinal type }
  174. function is_32bit(def : tdef) : boolean;
  175. {# Returns true, if def is a 64 bit integer type }
  176. function is_64bitint(def : tdef) : boolean;
  177. {# Returns true, if def is a 64 bit type }
  178. function is_64bit(def : tdef) : boolean;
  179. { true, if def is an int type, larger than the processor's native int size }
  180. function is_oversizedint(def : tdef) : boolean;
  181. { true, if def is an ordinal type, larger than the processor's native int size }
  182. function is_oversizedord(def : tdef) : boolean;
  183. { true, if def is an int type, equal in size to the processor's native int size }
  184. function is_nativeint(def : tdef) : boolean;
  185. { true, if def is an ordinal type, equal in size to the processor's native int size }
  186. function is_nativeord(def : tdef) : boolean;
  187. { true, if def is an unsigned int type, equal in size to the processor's native int size }
  188. function is_nativeuint(def : tdef) : boolean;
  189. { true, if def is a signed int type, equal in size to the processor's native int size }
  190. function is_nativesint(def : tdef) : boolean;
  191. {# If @var(l) isn't in the range of todef a range check error (if not explicit) is generated and
  192. the value is placed within the range
  193. }
  194. procedure testrange(todef : tdef;var l : tconstexprint;explicit,forcerangecheck:boolean);
  195. {# Returns the range of def, where @var(l) is the low-range and @var(h) is
  196. the high-range.
  197. }
  198. procedure getrange(def : tdef;out l, h : TConstExprInt);
  199. { type being a vector? }
  200. function is_vector(p : tdef) : boolean;
  201. { some type helper routines for MMX support }
  202. function is_mmx_able_array(p : tdef) : boolean;
  203. {# returns the mmx type }
  204. function mmx_type(p : tdef) : tmmxtype;
  205. { returns if the passed type (array) fits into an mm register }
  206. function fits_in_mm_register(p : tdef) : boolean;
  207. {# From a definition return the abstract code generator size enum. It is
  208. to note that the value returned can be @var(OS_NO) }
  209. function def_cgsize(def: tdef): tcgsize;
  210. { #Return an orddef (integer) correspondig to a tcgsize }
  211. function cgsize_orddef(size: tcgsize): torddef;
  212. {# Same as def_cgsize, except that it will interpret certain arrays as
  213. vectors and return OS_M* sizes for them }
  214. function def_cgmmsize(def: tdef): tcgsize;
  215. {# returns true, if the type passed is can be used with windows automation }
  216. function is_automatable(p : tdef) : boolean;
  217. { # returns true if the procdef has no parameters and no specified return type }
  218. function is_bareprocdef(pd : tprocdef): boolean;
  219. { # returns the smallest base integer type whose range encompasses that of
  220. both ld and rd; if keep_sign_if_equal, then if ld and rd have the same
  221. signdness, the result will also get that signdness }
  222. function get_common_intdef(ld, rd: torddef; keep_sign_if_equal: boolean): torddef;
  223. { # returns whether the type is potentially a valid type of/for an "univ" parameter
  224. (basically: it must have a compile-time size) }
  225. function is_valid_univ_para_type(def: tdef): boolean;
  226. { # returns whether the procdef/procvardef represents a nested procedure
  227. or not }
  228. function is_nested_pd(def: tabstractprocdef): boolean;{$ifdef USEINLINE}inline;{$endif}
  229. { # returns whether def is a type parameter of a generic }
  230. function is_typeparam(def : tdef) : boolean;{$ifdef USEINLINE}inline;{$endif}
  231. { returns true of def is a methodpointer }
  232. function is_methodpointer(def : tdef) : boolean;
  233. { returns true if def is a C "block" }
  234. function is_block(def: tdef): boolean;
  235. implementation
  236. uses
  237. verbose,cutils;
  238. { returns true, if def uses FPU }
  239. function is_fpu(def : tdef) : boolean;
  240. begin
  241. is_fpu:=(def.typ=floatdef);
  242. end;
  243. { returns true, if def is a currency type }
  244. function is_currency(def : tdef) : boolean;
  245. begin
  246. case s64currencytype.typ of
  247. orddef :
  248. result:=(def.typ=orddef) and
  249. (torddef(s64currencytype).ordtype=torddef(def).ordtype);
  250. floatdef :
  251. result:=(def.typ=floatdef) and
  252. (tfloatdef(s64currencytype).floattype=tfloatdef(def).floattype);
  253. else
  254. internalerror(200304222);
  255. end;
  256. end;
  257. { returns true, if def is a single type }
  258. function is_single(def : tdef) : boolean;
  259. begin
  260. result:=(def.typ=floatdef) and
  261. (tfloatdef(def).floattype=s32real);
  262. end;
  263. { returns true, if def is a double type }
  264. function is_double(def : tdef) : boolean;
  265. begin
  266. result:=(def.typ=floatdef) and
  267. (tfloatdef(def).floattype=s64real);
  268. end;
  269. function is_extended(def : tdef) : boolean;
  270. begin
  271. result:=(def.typ=floatdef) and
  272. (tfloatdef(def).floattype in [s80real,sc80real]);
  273. end;
  274. { returns true, if definition is a "real" real (i.e. single/double/extended) }
  275. function is_real(def : tdef) : boolean;
  276. begin
  277. result:=(def.typ=floatdef) and
  278. (tfloatdef(def).floattype in [s32real,s64real,s80real]);
  279. end;
  280. function is_real_or_cextended(def: tdef): boolean;
  281. begin
  282. result:=(def.typ=floatdef) and
  283. (tfloatdef(def).floattype in [s32real,s64real,s80real,sc80real]);
  284. end;
  285. function range_to_basetype(l,h:TConstExprInt):tordtype;
  286. begin
  287. { prefer signed over unsigned }
  288. if (l>=int64(-128)) and (h<=127) then
  289. range_to_basetype:=s8bit
  290. else if (l>=0) and (h<=255) then
  291. range_to_basetype:=u8bit
  292. else if (l>=int64(-32768)) and (h<=32767) then
  293. range_to_basetype:=s16bit
  294. else if (l>=0) and (h<=65535) then
  295. range_to_basetype:=u16bit
  296. else if (l>=int64(low(longint))) and (h<=high(longint)) then
  297. range_to_basetype:=s32bit
  298. else if (l>=low(cardinal)) and (h<=high(cardinal)) then
  299. range_to_basetype:=u32bit
  300. else if (l>=low(int64)) and (h<=high(int64)) then
  301. range_to_basetype:=s64bit
  302. else
  303. range_to_basetype:=u64bit;
  304. end;
  305. procedure range_to_type(l,h:TConstExprInt;var def:tdef);
  306. begin
  307. { prefer signed over unsigned }
  308. if (l>=int64(-128)) and (h<=127) then
  309. def:=s8inttype
  310. else if (l>=0) and (h<=255) then
  311. def:=u8inttype
  312. else if (l>=int64(-32768)) and (h<=32767) then
  313. def:=s16inttype
  314. else if (l>=0) and (h<=65535) then
  315. def:=u16inttype
  316. else if (l>=int64(low(longint))) and (h<=high(longint)) then
  317. def:=s32inttype
  318. else if (l>=low(cardinal)) and (h<=high(cardinal)) then
  319. def:=u32inttype
  320. else if (l>=low(int64)) and (h<=high(int64)) then
  321. def:=s64inttype
  322. else
  323. def:=u64inttype;
  324. end;
  325. procedure int_to_type(v:TConstExprInt;var def:tdef);
  326. begin
  327. range_to_type(v,v,def);
  328. end;
  329. { true if p is an ordinal }
  330. function is_ordinal(def : tdef) : boolean;
  331. var
  332. dt : tordtype;
  333. begin
  334. case def.typ of
  335. orddef :
  336. begin
  337. dt:=torddef(def).ordtype;
  338. is_ordinal:=dt in [uchar,uwidechar,
  339. u8bit,u16bit,u32bit,u64bit,
  340. s8bit,s16bit,s32bit,s64bit,
  341. pasbool8,pasbool16,pasbool32,pasbool64,
  342. bool8bit,bool16bit,bool32bit,bool64bit];
  343. end;
  344. enumdef :
  345. is_ordinal:=true;
  346. else
  347. is_ordinal:=false;
  348. end;
  349. end;
  350. { true if p is a string }
  351. function is_string(def : tdef) : boolean;
  352. begin
  353. is_string := (assigned(def) and (def.typ = stringdef));
  354. end;
  355. function is_stringlike(def : tdef) : boolean;
  356. begin
  357. result := is_string(def) or
  358. is_anychar(def) or
  359. is_pchar(def) or
  360. is_pwidechar(def) or
  361. is_chararray(def) or
  362. is_widechararray(def) or
  363. is_open_chararray(def) or
  364. is_open_widechararray(def) or
  365. (def=java_jlstring);
  366. end;
  367. function is_enum(def : tdef) : boolean;
  368. begin
  369. result:=def.typ=enumdef;
  370. end;
  371. function is_set(def : tdef) : boolean;
  372. begin
  373. result:=def.typ=setdef;
  374. end;
  375. { returns the min. value of the type }
  376. function get_min_value(def : tdef) : TConstExprInt;
  377. begin
  378. case def.typ of
  379. orddef:
  380. result:=torddef(def).low;
  381. enumdef:
  382. result:=int64(tenumdef(def).min);
  383. else
  384. result:=0;
  385. end;
  386. end;
  387. { returns the max. value of the type }
  388. function get_max_value(def : tdef) : TConstExprInt;
  389. begin
  390. case def.typ of
  391. orddef:
  392. result:=torddef(def).high;
  393. enumdef:
  394. result:=tenumdef(def).max;
  395. else
  396. result:=0;
  397. end;
  398. end;
  399. { true if p is an integer }
  400. function is_integer(def : tdef) : boolean;
  401. begin
  402. result:=(def.typ=orddef) and
  403. (torddef(def).ordtype in [u8bit,u16bit,u32bit,u64bit,
  404. s8bit,s16bit,s32bit,s64bit]);
  405. end;
  406. { true if p is a boolean }
  407. function is_boolean(def : tdef) : boolean;
  408. begin
  409. result:=(def.typ=orddef) and
  410. (torddef(def).ordtype in [pasbool8,pasbool16,pasbool32,pasbool64,bool8bit,bool16bit,bool32bit,bool64bit]);
  411. end;
  412. function is_pasbool(def : tdef) : boolean;
  413. begin
  414. result:=(def.typ=orddef) and
  415. (torddef(def).ordtype in [pasbool8,pasbool16,pasbool32,pasbool64]);
  416. end;
  417. { true if def is a C-style boolean (non-zero value = true, zero = false) }
  418. function is_cbool(def : tdef) : boolean;
  419. begin
  420. result:=(def.typ=orddef) and
  421. (torddef(def).ordtype in [bool8bit,bool16bit,bool32bit,bool64bit]);
  422. end;
  423. { true if p is a void }
  424. function is_void(def : tdef) : boolean;
  425. begin
  426. result:=(def.typ=orddef) and
  427. (torddef(def).ordtype=uvoid);
  428. end;
  429. { true if p is a char }
  430. function is_char(def : tdef) : boolean;
  431. begin
  432. result:=(def.typ=orddef) and
  433. (torddef(def).ordtype=uchar);
  434. end;
  435. { true if p is a wchar }
  436. function is_widechar(def : tdef) : boolean;
  437. begin
  438. result:=(def.typ=orddef) and
  439. (torddef(def).ordtype=uwidechar);
  440. end;
  441. { true if p is a char or wchar }
  442. function is_anychar(def : tdef) : boolean;
  443. begin
  444. result:=(def.typ=orddef) and
  445. (torddef(def).ordtype in [uchar,uwidechar])
  446. end;
  447. { true if p is signed (integer) }
  448. function is_signed(def : tdef) : boolean;
  449. begin
  450. case def.typ of
  451. orddef :
  452. result:=torddef(def).low < 0;
  453. enumdef :
  454. result:=tenumdef(def).min < 0;
  455. arraydef :
  456. result:=is_signed(tarraydef(def).rangedef);
  457. else
  458. result:=false;
  459. end;
  460. end;
  461. function get_unsigned_inttype(def: tdef): torddef;
  462. begin
  463. case def.typ of
  464. orddef,
  465. enumdef:
  466. result:=cgsize_orddef(tcgsize2unsigned[def_cgsize(def)]);
  467. else
  468. internalerror(2016062001);
  469. end;
  470. end;
  471. function is_in_limit(def_from,def_to : tdef) : boolean;
  472. begin
  473. if (def_from.typ<>def_to.typ) or
  474. not(def_from.typ in [orddef,enumdef,setdef]) then
  475. begin
  476. is_in_limit := false;
  477. exit;
  478. end;
  479. case def_from.typ of
  480. orddef:
  481. is_in_limit:=(torddef(def_from).low>=torddef(def_to).low) and
  482. (torddef(def_from).high<=torddef(def_to).high);
  483. enumdef:
  484. is_in_limit:=(tenumdef(def_from).min>=tenumdef(def_to).min) and
  485. (tenumdef(def_from).max<=tenumdef(def_to).max);
  486. setdef:
  487. is_in_limit:=(tsetdef(def_from).setbase>=tsetdef(def_to).setbase) and
  488. (tsetdef(def_from).setmax<=tsetdef(def_to).setmax);
  489. else
  490. is_in_limit:=false;
  491. end;
  492. end;
  493. function is_managed_type(def: tdef): boolean;{$ifdef USEINLINE}inline;{$endif}
  494. begin
  495. result:=def.needs_inittable;
  496. end;
  497. function is_rtti_managed_type(def: tdef): boolean;
  498. begin
  499. result:=def.needs_inittable and not (
  500. is_interfacecom_or_dispinterface(def) or
  501. (def.typ=variantdef) or
  502. (
  503. (def.typ=stringdef) and
  504. (tstringdef(def).stringtype in [st_ansistring,st_widestring,st_unicodestring])
  505. )
  506. );
  507. end;
  508. { true, if p points to an open array def }
  509. function is_open_string(p : tdef) : boolean;
  510. begin
  511. is_open_string:=(p.typ=stringdef) and
  512. (tstringdef(p).stringtype=st_shortstring) and
  513. (tstringdef(p).len=0);
  514. end;
  515. { true, if p points to a zero based array def }
  516. function is_zero_based_array(p : tdef) : boolean;
  517. begin
  518. result:=(p.typ=arraydef) and
  519. (tarraydef(p).lowrange=0) and
  520. not(is_special_array(p));
  521. end;
  522. { true if p points to a dynamic array def }
  523. function is_dynamic_array(p : tdef) : boolean;
  524. begin
  525. result:=(p.typ=arraydef) and
  526. (ado_IsDynamicArray in tarraydef(p).arrayoptions);
  527. end;
  528. { true, if p points to an open array def }
  529. function is_open_array(p : tdef) : boolean;
  530. begin
  531. { check for sizesinttype is needed, because for unsigned the high
  532. range is also -1 ! (PFV) }
  533. result:=(p.typ=arraydef) and
  534. (tarraydef(p).rangedef=sizesinttype) and
  535. (tarraydef(p).lowrange=0) and
  536. (tarraydef(p).highrange=-1) and
  537. ((tarraydef(p).arrayoptions * [ado_IsVariant,ado_IsArrayOfConst,ado_IsConstructor,ado_IsDynamicArray])=[]);
  538. end;
  539. { true, if p points to an array of const def }
  540. function is_array_constructor(p : tdef) : boolean;
  541. begin
  542. result:=(p.typ=arraydef) and
  543. (ado_IsConstructor in tarraydef(p).arrayoptions);
  544. end;
  545. { true, if p points to a variant array }
  546. function is_variant_array(p : tdef) : boolean;
  547. begin
  548. result:=(p.typ=arraydef) and
  549. (ado_IsVariant in tarraydef(p).arrayoptions);
  550. end;
  551. { true, if p points to an array of const }
  552. function is_array_of_const(p : tdef) : boolean;
  553. begin
  554. result:=(p.typ=arraydef) and
  555. (ado_IsArrayOfConst in tarraydef(p).arrayoptions);
  556. end;
  557. { true, if p points to a special array, bitpacked arrays aren't special in this regard though }
  558. function is_special_array(p : tdef) : boolean;
  559. begin
  560. result:=(p.typ=arraydef) and
  561. (
  562. ((tarraydef(p).arrayoptions * [ado_IsVariant,ado_IsArrayOfConst,ado_IsConstructor,ado_IsDynamicArray])<>[]) or
  563. is_open_array(p)
  564. );
  565. end;
  566. { true if p is an ansi string def }
  567. function is_ansistring(p : tdef) : boolean;
  568. begin
  569. is_ansistring:=(p.typ=stringdef) and
  570. (tstringdef(p).stringtype=st_ansistring);
  571. end;
  572. { true if p is an ansi string def with codepage CP_NONE }
  573. function is_rawbytestring(p : tdef) : boolean;
  574. begin
  575. is_rawbytestring:=(p.typ=stringdef) and
  576. (tstringdef(p).stringtype=st_ansistring) and
  577. (tstringdef(p).encoding=globals.CP_NONE);
  578. end;
  579. { true if p is an long string def }
  580. function is_longstring(p : tdef) : boolean;
  581. begin
  582. is_longstring:=(p.typ=stringdef) and
  583. (tstringdef(p).stringtype=st_longstring);
  584. end;
  585. { true if p is an wide string def }
  586. function is_widestring(p : tdef) : boolean;
  587. begin
  588. is_widestring:=(p.typ=stringdef) and
  589. (tstringdef(p).stringtype=st_widestring);
  590. end;
  591. function is_dynamicstring(p: tdef): boolean;
  592. begin
  593. is_dynamicstring:=(p.typ=stringdef) and
  594. (tstringdef(p).stringtype in [st_ansistring,st_widestring,st_unicodestring]);
  595. end;
  596. { true if p is an wide string def }
  597. function is_wide_or_unicode_string(p : tdef) : boolean;
  598. begin
  599. is_wide_or_unicode_string:=(p.typ=stringdef) and
  600. (tstringdef(p).stringtype in [st_widestring,st_unicodestring]);
  601. end;
  602. { true if p is an unicode string def }
  603. function is_unicodestring(p : tdef) : boolean;
  604. begin
  605. is_unicodestring:=(p.typ=stringdef) and
  606. (tstringdef(p).stringtype=st_unicodestring);
  607. end;
  608. { true if p is an short string def }
  609. function is_shortstring(p : tdef) : boolean;
  610. begin
  611. is_shortstring:=(p.typ=stringdef) and
  612. (tstringdef(p).stringtype=st_shortstring);
  613. end;
  614. { true if p is bit packed array def }
  615. function is_packed_array(p: tdef) : boolean;
  616. begin
  617. is_packed_array :=
  618. (p.typ = arraydef) and
  619. (ado_IsBitPacked in tarraydef(p).arrayoptions);
  620. end;
  621. { true if p is bit packed record def }
  622. function is_packed_record_or_object(p: tdef) : boolean;
  623. begin
  624. is_packed_record_or_object :=
  625. (p.typ in [recorddef,objectdef]) and
  626. (tabstractrecorddef(p).is_packed);
  627. end;
  628. { true if p is a char array def }
  629. function is_chararray(p : tdef) : boolean;
  630. begin
  631. is_chararray:=(p.typ=arraydef) and
  632. is_char(tarraydef(p).elementdef) and
  633. not(is_special_array(p));
  634. end;
  635. { true if p is a widechar array def }
  636. function is_widechararray(p : tdef) : boolean;
  637. begin
  638. is_widechararray:=(p.typ=arraydef) and
  639. is_widechar(tarraydef(p).elementdef) and
  640. not(is_special_array(p));
  641. end;
  642. { true if p is a open char array def }
  643. function is_open_chararray(p : tdef) : boolean;
  644. begin
  645. is_open_chararray:= is_open_array(p) and
  646. is_char(tarraydef(p).elementdef);
  647. end;
  648. { true if p is a open wide char array def }
  649. function is_open_widechararray(p : tdef) : boolean;
  650. begin
  651. is_open_widechararray:= is_open_array(p) and
  652. is_widechar(tarraydef(p).elementdef);
  653. end;
  654. { true if p is a pchar def }
  655. function is_pchar(p : tdef) : boolean;
  656. begin
  657. is_pchar:=(p.typ=pointerdef) and
  658. (is_char(tpointerdef(p).pointeddef) or
  659. (is_zero_based_array(tpointerdef(p).pointeddef) and
  660. is_chararray(tpointerdef(p).pointeddef)));
  661. end;
  662. { true if p is a pchar def }
  663. function is_pwidechar(p : tdef) : boolean;
  664. begin
  665. is_pwidechar:=(p.typ=pointerdef) and
  666. (is_widechar(tpointerdef(p).pointeddef) or
  667. (is_zero_based_array(tpointerdef(p).pointeddef) and
  668. is_widechararray(tpointerdef(p).pointeddef)));
  669. end;
  670. { true if p is a voidpointer def }
  671. function is_voidpointer(p : tdef) : boolean;
  672. begin
  673. is_voidpointer:=(p.typ=pointerdef) and
  674. (tpointerdef(p).pointeddef.typ=orddef) and
  675. (torddef(tpointerdef(p).pointeddef).ordtype=uvoid);
  676. end;
  677. { true, if def is a 8 bit int type }
  678. function is_8bitint(def : tdef) : boolean;
  679. begin
  680. result:=(def.typ=orddef) and (torddef(def).ordtype in [u8bit,s8bit])
  681. end;
  682. { true, if def is a 8 bit ordinal type }
  683. function is_8bit(def : tdef) : boolean;
  684. begin
  685. result:=(def.typ=orddef) and (torddef(def).ordtype in [u8bit,s8bit,pasbool8,bool8bit,uchar])
  686. end;
  687. { true, if def is a 16 bit int type }
  688. function is_16bitint(def : tdef) : boolean;
  689. begin
  690. result:=(def.typ=orddef) and (torddef(def).ordtype in [u16bit,s16bit])
  691. end;
  692. { true, if def is a 16 bit ordinal type }
  693. function is_16bit(def : tdef) : boolean;
  694. begin
  695. result:=(def.typ=orddef) and (torddef(def).ordtype in [u16bit,s16bit,pasbool16,bool16bit,uwidechar])
  696. end;
  697. { true, if def is a 32 bit int type }
  698. function is_32bitint(def : tdef) : boolean;
  699. begin
  700. result:=(def.typ=orddef) and (torddef(def).ordtype in [u32bit,s32bit])
  701. end;
  702. { true, if def is a 32 bit ordinal type }
  703. function is_32bit(def: tdef): boolean;
  704. begin
  705. result:=(def.typ=orddef) and (torddef(def).ordtype in [u32bit,s32bit,pasbool32,bool32bit])
  706. end;
  707. { true, if def is a 64 bit int type }
  708. function is_64bitint(def : tdef) : boolean;
  709. begin
  710. is_64bitint:=(def.typ=orddef) and (torddef(def).ordtype in [u64bit,s64bit])
  711. end;
  712. { true, if def is a 64 bit type }
  713. function is_64bit(def : tdef) : boolean;
  714. begin
  715. is_64bit:=(def.typ=orddef) and (torddef(def).ordtype in [u64bit,s64bit,scurrency,pasbool64,bool64bit])
  716. end;
  717. { true, if def is an int type, larger than the processor's native int size }
  718. function is_oversizedint(def : tdef) : boolean;
  719. begin
  720. {$if defined(cpu8bitalu)}
  721. result:=is_64bitint(def) or is_32bitint(def) or is_16bitint(def);
  722. {$elseif defined(cpu16bitalu)}
  723. result:=is_64bitint(def) or is_32bitint(def);
  724. {$elseif defined(cpu32bitaddr)}
  725. result:=is_64bitint(def);
  726. {$elseif defined(cpu64bitaddr)}
  727. result:=false;
  728. {$endif}
  729. end;
  730. { true, if def is an ordinal type, larger than the processor's native int size }
  731. function is_oversizedord(def : tdef) : boolean;
  732. begin
  733. {$if defined(cpu8bitalu)}
  734. result:=is_64bit(def) or is_32bit(def) or is_16bit(def);
  735. {$elseif defined(cpu16bitalu)}
  736. result:=is_64bit(def) or is_32bit(def);
  737. {$elseif defined(cpu32bitaddr)}
  738. result:=is_64bit(def);
  739. {$elseif defined(cpu64bitaddr)}
  740. result:=false;
  741. {$endif}
  742. end;
  743. { true, if def is an int type, equal in size to the processor's native int size }
  744. function is_nativeint(def: tdef): boolean;
  745. begin
  746. {$if defined(cpu8bitalu)}
  747. result:=is_8bitint(def);
  748. {$elseif defined(cpu16bitalu)}
  749. result:=is_16bitint(def);
  750. {$elseif defined(cpu32bitaddr)}
  751. result:=is_32bitint(def);
  752. {$elseif defined(cpu64bitaddr)}
  753. result:=is_64bitint(def);
  754. {$endif}
  755. end;
  756. { true, if def is an ordinal type, equal in size to the processor's native int size }
  757. function is_nativeord(def: tdef): boolean;
  758. begin
  759. {$if defined(cpu8bitalu)}
  760. result:=is_8bit(def);
  761. {$elseif defined(cpu16bitalu)}
  762. result:=is_16bit(def);
  763. {$elseif defined(cpu32bitaddr)}
  764. result:=is_32bit(def);
  765. {$elseif defined(cpu64bitaddr)}
  766. result:=is_64bit(def);
  767. {$endif}
  768. end;
  769. { true, if def is an unsigned int type, equal in size to the processor's native int size }
  770. function is_nativeuint(def: tdef): boolean;
  771. begin
  772. result:=is_nativeint(def) and (def.typ=orddef) and (torddef(def).ordtype in [u64bit,u32bit,u16bit,u8bit]);
  773. end;
  774. { true, if def is a signed int type, equal in size to the processor's native int size }
  775. function is_nativesint(def: tdef): boolean;
  776. begin
  777. result:=is_nativeint(def) and (def.typ=orddef) and (torddef(def).ordtype in [s64bit,s32bit,s16bit,s8bit]);
  778. end;
  779. { if l isn't in the range of todef a range check error (if not explicit) is generated and
  780. the value is placed within the range }
  781. procedure testrange(todef : tdef;var l : tconstexprint;explicit,forcerangecheck:boolean);
  782. var
  783. lv,hv: TConstExprInt;
  784. begin
  785. { for 64 bit types we need only to check if it is less than }
  786. { zero, if def is a qword node }
  787. getrange(todef,lv,hv);
  788. if (l<lv) or (l>hv) then
  789. begin
  790. if not explicit then
  791. begin
  792. if ((todef.typ=enumdef) and
  793. { delphi allows range check errors in
  794. enumeration type casts FK }
  795. not(m_delphi in current_settings.modeswitches)) or
  796. (cs_check_range in current_settings.localswitches) or
  797. forcerangecheck then
  798. Message3(type_e_range_check_error_bounds,tostr(l),tostr(lv),tostr(hv))
  799. else
  800. Message3(type_w_range_check_error_bounds,tostr(l),tostr(lv),tostr(hv));
  801. end;
  802. { Fix the value to fit in the allocated space for this type of variable }
  803. case longint(todef.size) of
  804. 1: l := l and $ff;
  805. 2: l := l and $ffff;
  806. 4: l := l and $ffffffff;
  807. end;
  808. {reset sign, i.e. converting -1 to qword changes the value to high(qword)}
  809. l.signed:=false;
  810. { do sign extension if necessary (JM) }
  811. if is_signed(todef) then
  812. begin
  813. case longint(todef.size) of
  814. 1: l.svalue := shortint(l.svalue);
  815. 2: l.svalue := smallint(l.svalue);
  816. 4: l.svalue := longint(l.svalue);
  817. end;
  818. l.signed:=true;
  819. end;
  820. end;
  821. end;
  822. { return the range from def in l and h }
  823. procedure getrange(def : tdef;out l, h : TConstExprInt);
  824. begin
  825. case def.typ of
  826. orddef :
  827. begin
  828. l:=torddef(def).low;
  829. h:=torddef(def).high;
  830. end;
  831. enumdef :
  832. begin
  833. l:=int64(tenumdef(def).min);
  834. h:=int64(tenumdef(def).max);
  835. end;
  836. arraydef :
  837. begin
  838. l:=int64(tarraydef(def).lowrange);
  839. h:=int64(tarraydef(def).highrange);
  840. end;
  841. else
  842. internalerror(200611054);
  843. end;
  844. end;
  845. function mmx_type(p : tdef) : tmmxtype;
  846. begin
  847. mmx_type:=mmxno;
  848. if is_mmx_able_array(p) then
  849. begin
  850. if tarraydef(p).elementdef.typ=floatdef then
  851. case tfloatdef(tarraydef(p).elementdef).floattype of
  852. s32real:
  853. mmx_type:=mmxsingle;
  854. end
  855. else
  856. case torddef(tarraydef(p).elementdef).ordtype of
  857. u8bit:
  858. mmx_type:=mmxu8bit;
  859. s8bit:
  860. mmx_type:=mmxs8bit;
  861. u16bit:
  862. mmx_type:=mmxu16bit;
  863. s16bit:
  864. mmx_type:=mmxs16bit;
  865. u32bit:
  866. mmx_type:=mmxu32bit;
  867. s32bit:
  868. mmx_type:=mmxs32bit;
  869. end;
  870. end;
  871. end;
  872. function is_vector(p : tdef) : boolean;
  873. begin
  874. result:=(p.typ=arraydef) and
  875. not(is_special_array(p)) and
  876. (tarraydef(p).elementdef.typ=floatdef) and
  877. (tfloatdef(tarraydef(p).elementdef).floattype in [s32real,s64real]);
  878. end;
  879. { returns if the passed type (array) fits into an mm register }
  880. function fits_in_mm_register(p : tdef) : boolean;
  881. begin
  882. {$ifdef x86}
  883. result:= is_vector(p) and
  884. (
  885. (tarraydef(p).elementdef.typ=floatdef) and
  886. (
  887. (tarraydef(p).lowrange=0) and
  888. (tarraydef(p).highrange=3) and
  889. (tfloatdef(tarraydef(p).elementdef).floattype=s32real)
  890. )
  891. ) or
  892. (
  893. (tarraydef(p).elementdef.typ=floatdef) and
  894. (
  895. (tarraydef(p).lowrange=0) and
  896. (tarraydef(p).highrange=1) and
  897. (tfloatdef(tarraydef(p).elementdef).floattype=s64real)
  898. )
  899. );
  900. {$else x86}
  901. result:=false;
  902. {$endif x86}
  903. end;
  904. function is_mmx_able_array(p : tdef) : boolean;
  905. begin
  906. {$ifdef SUPPORT_MMX}
  907. if (cs_mmx_saturation in current_settings.localswitches) then
  908. begin
  909. is_mmx_able_array:=(p.typ=arraydef) and
  910. not(is_special_array(p)) and
  911. (
  912. (
  913. (tarraydef(p).elementdef.typ=orddef) and
  914. (
  915. (
  916. (tarraydef(p).lowrange=0) and
  917. (tarraydef(p).highrange=1) and
  918. (torddef(tarraydef(p).elementdef).ordtype in [u32bit,s32bit])
  919. )
  920. or
  921. (
  922. (tarraydef(p).lowrange=0) and
  923. (tarraydef(p).highrange=3) and
  924. (torddef(tarraydef(p).elementdef).ordtype in [u16bit,s16bit])
  925. )
  926. )
  927. )
  928. or
  929. (
  930. (
  931. (tarraydef(p).elementdef.typ=floatdef) and
  932. (
  933. (tarraydef(p).lowrange=0) and
  934. (tarraydef(p).highrange=1) and
  935. (tfloatdef(tarraydef(p).elementdef).floattype=s32real)
  936. )
  937. )
  938. )
  939. );
  940. end
  941. else
  942. begin
  943. is_mmx_able_array:=(p.typ=arraydef) and
  944. (
  945. (
  946. (tarraydef(p).elementdef.typ=orddef) and
  947. (
  948. (
  949. (tarraydef(p).lowrange=0) and
  950. (tarraydef(p).highrange=1) and
  951. (torddef(tarraydef(p).elementdef).ordtype in [u32bit,s32bit])
  952. )
  953. or
  954. (
  955. (tarraydef(p).lowrange=0) and
  956. (tarraydef(p).highrange=3) and
  957. (torddef(tarraydef(p).elementdef).ordtype in [u16bit,s16bit])
  958. )
  959. or
  960. (
  961. (tarraydef(p).lowrange=0) and
  962. (tarraydef(p).highrange=7) and
  963. (torddef(tarraydef(p).elementdef).ordtype in [u8bit,s8bit])
  964. )
  965. )
  966. )
  967. or
  968. (
  969. (tarraydef(p).elementdef.typ=floatdef) and
  970. (
  971. (tarraydef(p).lowrange=0) and
  972. (tarraydef(p).highrange=1) and
  973. (tfloatdef(tarraydef(p).elementdef).floattype=s32real)
  974. )
  975. )
  976. );
  977. end;
  978. {$else SUPPORT_MMX}
  979. is_mmx_able_array:=false;
  980. {$endif SUPPORT_MMX}
  981. end;
  982. function def_cgsize(def: tdef): tcgsize;
  983. begin
  984. case def.typ of
  985. orddef,
  986. enumdef,
  987. setdef:
  988. begin
  989. result:=int_cgsize(def.size);
  990. if is_signed(def) then
  991. result:=tcgsize(ord(result)+(ord(OS_S8)-ord(OS_8)));
  992. end;
  993. classrefdef,
  994. pointerdef:
  995. begin
  996. result:=int_cgsize(def.size);
  997. { can happen for far/huge pointers on non-i8086 }
  998. if result=OS_NO then
  999. internalerror(2013052201);
  1000. end;
  1001. formaldef:
  1002. result := int_cgsize(voidpointertype.size);
  1003. procvardef:
  1004. result:=int_cgsize(def.size);
  1005. stringdef :
  1006. result:=int_cgsize(def.size);
  1007. objectdef :
  1008. result:=int_cgsize(def.size);
  1009. floatdef:
  1010. if cs_fp_emulation in current_settings.moduleswitches then
  1011. result:=int_cgsize(def.size)
  1012. else
  1013. result:=tfloat2tcgsize[tfloatdef(def).floattype];
  1014. recorddef :
  1015. result:=int_cgsize(def.size);
  1016. arraydef :
  1017. begin
  1018. if is_dynamic_array(def) or not is_special_array(def) then
  1019. result := int_cgsize(def.size)
  1020. else
  1021. result := OS_NO;
  1022. end;
  1023. else
  1024. begin
  1025. { undefined size }
  1026. result:=OS_NO;
  1027. end;
  1028. end;
  1029. end;
  1030. function cgsize_orddef(size: tcgsize): torddef;
  1031. begin
  1032. case size of
  1033. OS_8:
  1034. result:=torddef(u8inttype);
  1035. OS_S8:
  1036. result:=torddef(s8inttype);
  1037. OS_16:
  1038. result:=torddef(u16inttype);
  1039. OS_S16:
  1040. result:=torddef(s16inttype);
  1041. OS_32:
  1042. result:=torddef(u32inttype);
  1043. OS_S32:
  1044. result:=torddef(s32inttype);
  1045. OS_64:
  1046. result:=torddef(u64inttype);
  1047. OS_S64:
  1048. result:=torddef(s64inttype);
  1049. else
  1050. internalerror(2012050401);
  1051. end;
  1052. end;
  1053. function def_cgmmsize(def: tdef): tcgsize;
  1054. begin
  1055. case def.typ of
  1056. arraydef:
  1057. begin
  1058. if tarraydef(def).elementdef.typ in [orddef,floatdef] then
  1059. begin
  1060. { this is not correct, OS_MX normally mean that the vector
  1061. contains elements of size X. However, vectors themselves
  1062. can also have different sizes (e.g. a vector of 2 singles on
  1063. SSE) and the total size is currently more important }
  1064. case def.size of
  1065. 1: result:=OS_M8;
  1066. 2: result:=OS_M16;
  1067. 4: result:=OS_M32;
  1068. 8: result:=OS_M64;
  1069. 16: result:=OS_M128;
  1070. 32: result:=OS_M256;
  1071. else
  1072. internalerror(2013060103);
  1073. end;
  1074. end
  1075. else
  1076. result:=def_cgsize(def);
  1077. end
  1078. else
  1079. result:=def_cgsize(def);
  1080. end;
  1081. end;
  1082. { In Windows 95 era, ordinals were restricted to [u8bit,s32bit,s16bit,bool16bit]
  1083. As of today, both signed and unsigned types from 8 to 64 bits are supported. }
  1084. function is_automatable(p : tdef) : boolean;
  1085. begin
  1086. result:=false;
  1087. case p.typ of
  1088. orddef:
  1089. result:=torddef(p).ordtype in [u8bit,s8bit,u16bit,s16bit,u32bit,s32bit,
  1090. u64bit,s64bit,bool16bit,scurrency];
  1091. floatdef:
  1092. result:=tfloatdef(p).floattype in [s64currency,s64real,s32real];
  1093. stringdef:
  1094. result:=tstringdef(p).stringtype in [st_ansistring,st_widestring,st_unicodestring];
  1095. variantdef:
  1096. result:=true;
  1097. objectdef:
  1098. result:=tobjectdef(p).objecttype in [odt_interfacecom,odt_dispinterface,odt_interfacecorba];
  1099. end;
  1100. end;
  1101. {# returns true, if the type passed is a varset }
  1102. function is_smallset(p : tdef) : boolean;
  1103. begin
  1104. {$if defined(cpu8bitalu)}
  1105. result:=(p.typ=setdef) and (p.size = 1)
  1106. {$elseif defined(cpu16bitalu)}
  1107. result:=(p.typ=setdef) and (p.size in [1,2])
  1108. {$else}
  1109. result:=(p.typ=setdef) and (p.size in [1,2,4])
  1110. {$endif}
  1111. end;
  1112. function is_bareprocdef(pd : tprocdef): boolean;
  1113. begin
  1114. result:=(pd.maxparacount=0) and
  1115. (is_void(pd.returndef) or
  1116. (pd.proctypeoption = potype_constructor));
  1117. end;
  1118. function get_common_intdef(ld, rd: torddef; keep_sign_if_equal: boolean): torddef;
  1119. var
  1120. llow, lhigh: tconstexprint;
  1121. begin
  1122. llow:=min(ld.low,rd.low);
  1123. lhigh:=max(ld.high,rd.high);
  1124. case range_to_basetype(llow,lhigh) of
  1125. s8bit:
  1126. result:=torddef(s8inttype);
  1127. u8bit:
  1128. result:=torddef(u8inttype);
  1129. s16bit:
  1130. result:=torddef(s16inttype);
  1131. u16bit:
  1132. result:=torddef(u16inttype);
  1133. s32bit:
  1134. result:=torddef(s32inttype);
  1135. u32bit:
  1136. result:=torddef(u32inttype);
  1137. s64bit:
  1138. result:=torddef(s64inttype);
  1139. u64bit:
  1140. result:=torddef(u64inttype);
  1141. else
  1142. begin
  1143. { avoid warning }
  1144. result:=nil;
  1145. internalerror(200802291);
  1146. end;
  1147. end;
  1148. if keep_sign_if_equal and
  1149. (is_signed(ld)=is_signed(rd)) and
  1150. (is_signed(result)<>is_signed(ld)) then
  1151. case result.ordtype of
  1152. s8bit:
  1153. result:=torddef(u8inttype);
  1154. u8bit:
  1155. result:=torddef(s16inttype);
  1156. s16bit:
  1157. result:=torddef(u16inttype);
  1158. u16bit:
  1159. result:=torddef(s32inttype);
  1160. s32bit:
  1161. result:=torddef(u32inttype);
  1162. u32bit:
  1163. result:=torddef(s64inttype);
  1164. s64bit:
  1165. result:=torddef(u64inttype);
  1166. end;
  1167. end;
  1168. function is_valid_univ_para_type(def: tdef): boolean;
  1169. begin
  1170. result:=
  1171. not is_open_array(def) and
  1172. not is_void(def) and
  1173. (def.typ<>formaldef);
  1174. end;
  1175. function is_nested_pd(def: tabstractprocdef): boolean;{$ifdef USEINLINE}inline;{$endif}
  1176. begin
  1177. result:=def.parast.symtablelevel>normal_function_level;
  1178. end;
  1179. function is_typeparam(def : tdef) : boolean;{$ifdef USEINLINE}inline;{$endif}
  1180. begin
  1181. result:=(def.typ=undefineddef);
  1182. end;
  1183. function is_methodpointer(def: tdef): boolean;
  1184. begin
  1185. result:=(def.typ=procvardef) and (po_methodpointer in tprocvardef(def).procoptions);
  1186. end;
  1187. function is_block(def: tdef): boolean;
  1188. begin
  1189. result:=(def.typ=procvardef) and (po_is_block in tprocvardef(def).procoptions)
  1190. end;
  1191. end.