defutil.pas 47 KB

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