defutil.pas 52 KB

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