defutil.pas 54 KB

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