defutil.pas 47 KB

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