defutil.pas 47 KB

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