defutil.pas 47 KB

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