defutil.pas 46 KB

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