defutil.pas 47 KB

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