defutil.pas 46 KB

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