defutil.pas 46 KB

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