defutil.pas 51 KB

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