defutil.pas 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205
  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. cclasses,
  22. globtype,globals,constexp,node,
  23. symconst,symbase,symtype,symdef,
  24. cgbase,cpubase;
  25. type
  26. tmmxtype = (mmxno,mmxu8bit,mmxs8bit,mmxu16bit,mmxs16bit,
  27. mmxu32bit,mmxs32bit,mmxfixed16,mmxsingle);
  28. {*****************************************************************************
  29. Basic type functions
  30. *****************************************************************************}
  31. {# Returns true, if definition defines an ordinal type }
  32. function is_ordinal(def : tdef) : boolean;
  33. {# Returns true, if definition defines a string type }
  34. function is_string(def : tdef): boolean;
  35. {# Returns True, if definition defines a type that behaves like a string,
  36. namely that can be joined and compared with another string-like type }
  37. function is_stringlike(def : tdef) : boolean;
  38. {# Returns True, if definition defines an enumeration type }
  39. function is_enum(def : tdef) : boolean;
  40. {# Returns True, if definition defines a set type }
  41. function is_set(def : tdef) : boolean;
  42. {# Returns the minimal integer value of the type }
  43. function get_min_value(def : tdef) : TConstExprInt;
  44. {# Returns the maximal integer value of the type }
  45. function get_max_value(def : tdef) : TConstExprInt;
  46. {# Returns basetype of the specified integer range }
  47. function range_to_basetype(l,h:TConstExprInt):tordtype;
  48. procedure range_to_type(l,h:TConstExprInt;var def:tdef);
  49. procedure int_to_type(v:TConstExprInt;var def:tdef);
  50. {# Returns true, if definition defines an integer type }
  51. function is_integer(def : tdef) : boolean;
  52. {# Returns true if definition is a boolean }
  53. function is_boolean(def : tdef) : boolean;
  54. {# Returns true if definition is a Pascal-style boolean (1 = true, zero = false) }
  55. function is_pasbool(def : tdef) : boolean;
  56. {# Returns true if definition is a C-style boolean (non-zero value = true, zero = false) }
  57. function is_cbool(def : tdef) : boolean;
  58. {# Returns true if definition is a char
  59. This excludes the unicode char.
  60. }
  61. function is_char(def : tdef) : boolean;
  62. {# Returns true if definition is a widechar }
  63. function is_widechar(def : tdef) : boolean;
  64. {# Returns true if definition is either an AnsiChar or a WideChar }
  65. function is_anychar(def : tdef) : boolean;
  66. {# Returns true if definition is a void}
  67. function is_void(def : tdef) : boolean;
  68. {# Returns true if definition is a smallset}
  69. function is_smallset(p : tdef) : boolean;
  70. {# Returns true, if def defines a signed data type
  71. (only for ordinal types)
  72. }
  73. function is_signed(def : tdef) : boolean;
  74. {# Returns whether def_from's range is comprised in def_to's if both are
  75. orddefs, false otherwise }
  76. function is_in_limit(def_from,def_to : tdef) : boolean;
  77. {# Returns whether def is reference counted }
  78. function is_managed_type(def: tdef) : boolean;{$ifdef USEINLINE}inline;{$endif}
  79. { function is_in_limit_value(val_from:TConstExprInt;def_from,def_to : tdef) : boolean;}
  80. {*****************************************************************************
  81. Array helper functions
  82. *****************************************************************************}
  83. {# Returns true, if p points to a zero based (non special like open or
  84. dynamic array def).
  85. This is mainly used to see if the array
  86. is convertable to a pointer
  87. }
  88. function is_zero_based_array(p : tdef) : boolean;
  89. {# Returns true if p points to an open array definition }
  90. function is_open_array(p : tdef) : boolean;
  91. {# Returns true if p points to a dynamic array definition }
  92. function is_dynamic_array(p : tdef) : boolean;
  93. {# Returns true, if p points to an array of const definition }
  94. function is_array_constructor(p : tdef) : boolean;
  95. {# Returns true, if p points to a variant array }
  96. function is_variant_array(p : tdef) : boolean;
  97. {# Returns true, if p points to an array of const }
  98. function is_array_of_const(p : tdef) : boolean;
  99. {# Returns true, if p points any kind of special array
  100. That is if the array is an open array, a variant
  101. array, an array constants constructor, or an
  102. array of const.
  103. Bitpacked arrays aren't special in this regard though.
  104. }
  105. function is_special_array(p : tdef) : boolean;
  106. {# Returns true if p is a bitpacked array }
  107. function is_packed_array(p: tdef) : boolean;
  108. {# Returns true if p is a bitpacked record }
  109. function is_packed_record_or_object(p: tdef) : boolean;
  110. {# Returns true if p is a char array def }
  111. function is_chararray(p : tdef) : boolean;
  112. {# Returns true if p is a wide char array def }
  113. function is_widechararray(p : tdef) : boolean;
  114. {# Returns true if p is a open char array def }
  115. function is_open_chararray(p : tdef) : boolean;
  116. {# Returns true if p is a open wide char array def }
  117. function is_open_widechararray(p : tdef) : boolean;
  118. {*****************************************************************************
  119. String helper functions
  120. *****************************************************************************}
  121. {# Returns true if p points to an open string type }
  122. function is_open_string(p : tdef) : boolean;
  123. {# Returns true if p is an ansi string type }
  124. function is_ansistring(p : tdef) : boolean;
  125. {# Returns true if p is an ansi string type with codepage 0 }
  126. function is_rawbytestring(p : tdef) : boolean;
  127. {# Returns true if p is a long string type }
  128. function is_longstring(p : tdef) : boolean;
  129. {# returns true if p is a wide string type }
  130. function is_widestring(p : tdef) : boolean;
  131. {# true if p is an unicode string def }
  132. function is_unicodestring(p : tdef) : boolean;
  133. {# returns true if p is a wide or unicode string type }
  134. function is_wide_or_unicode_string(p : tdef) : boolean;
  135. {# Returns true if p is a short string type }
  136. function is_shortstring(p : tdef) : boolean;
  137. {# Returns true if p is a pchar def }
  138. function is_pchar(p : tdef) : boolean;
  139. {# Returns true if p is a pwidechar def }
  140. function is_pwidechar(p : tdef) : boolean;
  141. {# Returns true if p is a voidpointer def }
  142. function is_voidpointer(p : tdef) : boolean;
  143. {# Returns true, if definition is a float }
  144. function is_fpu(def : tdef) : boolean;
  145. {# Returns true, if def is a currency type }
  146. function is_currency(def : tdef) : boolean;
  147. {# Returns true, if def is a single type }
  148. function is_single(def : tdef) : boolean;
  149. {# Returns true, if def is a double type }
  150. function is_double(def : tdef) : boolean;
  151. {# Returns true, if def is an extended type }
  152. function is_extended(def : tdef) : boolean;
  153. {# Returns true, if definition is a "real" real (i.e. single/double/extended) }
  154. function is_real(def : tdef) : boolean;
  155. {# Returns true, if def is a 32 bit integer type }
  156. function is_32bitint(def : tdef) : boolean;
  157. {# Returns true, if def is a 64 bit integer type }
  158. function is_64bitint(def : tdef) : boolean;
  159. {# Returns true, if def is a 64 bit type }
  160. function is_64bit(def : tdef) : boolean;
  161. {# If @var(l) isn't in the range of todef a range check error (if not explicit) is generated and
  162. the value is placed within the range
  163. }
  164. procedure testrange(todef : tdef;var l : tconstexprint;explicit,forcerangecheck:boolean);
  165. {# Returns the range of def, where @var(l) is the low-range and @var(h) is
  166. the high-range.
  167. }
  168. procedure getrange(def : tdef;out l, h : TConstExprInt);
  169. { type being a vector? }
  170. function is_vector(p : tdef) : boolean;
  171. { some type helper routines for MMX support }
  172. function is_mmx_able_array(p : tdef) : boolean;
  173. {# returns the mmx type }
  174. function mmx_type(p : tdef) : tmmxtype;
  175. { returns if the passed type (array) fits into an mm register }
  176. function fits_in_mm_register(p : tdef) : boolean;
  177. {# From a definition return the abstract code generator size enum. It is
  178. to note that the value returned can be @var(OS_NO) }
  179. function def_cgsize(def: tdef): tcgsize;
  180. {# returns true, if the type passed is can be used with windows automation }
  181. function is_automatable(p : tdef) : boolean;
  182. { # returns true if the procdef has no parameters and no specified return type }
  183. function is_bareprocdef(pd : tprocdef): boolean;
  184. { # returns the smallest base integer type whose range encompasses that of
  185. both ld and rd; if keep_sign_if_equal, then if ld and rd have the same
  186. signdness, the result will also get that signdness }
  187. function get_common_intdef(ld, rd: torddef; keep_sign_if_equal: boolean): torddef;
  188. { # returns whether the type is potentially a valid type of/for an "univ" parameter
  189. (basically: it must have a compile-time size) }
  190. function is_valid_univ_para_type(def: tdef): boolean;
  191. { # returns whether the procdef/procvardef represents a nested procedure
  192. or not }
  193. function is_nested_pd(def: tabstractprocdef): boolean;{$ifdef USEINLINE}inline;{$endif}
  194. { # returns whether def is a type parameter of a generic }
  195. function is_typeparam(def : tdef) : boolean;{$ifdef USEINLINE}inline;{$endif}
  196. { returns true of def is a methodpointer }
  197. function is_methodpointer(def : tdef) : boolean;
  198. implementation
  199. uses
  200. systems,verbose;
  201. { returns true, if def uses FPU }
  202. function is_fpu(def : tdef) : boolean;
  203. begin
  204. is_fpu:=(def.typ=floatdef);
  205. end;
  206. { returns true, if def is a currency type }
  207. function is_currency(def : tdef) : boolean;
  208. begin
  209. case s64currencytype.typ of
  210. orddef :
  211. result:=(def.typ=orddef) and
  212. (torddef(s64currencytype).ordtype=torddef(def).ordtype);
  213. floatdef :
  214. result:=(def.typ=floatdef) and
  215. (tfloatdef(s64currencytype).floattype=tfloatdef(def).floattype);
  216. else
  217. internalerror(200304222);
  218. end;
  219. end;
  220. { returns true, if def is a single type }
  221. function is_single(def : tdef) : boolean;
  222. begin
  223. result:=(def.typ=floatdef) and
  224. (tfloatdef(def).floattype=s32real);
  225. end;
  226. { returns true, if def is a double type }
  227. function is_double(def : tdef) : boolean;
  228. begin
  229. result:=(def.typ=floatdef) and
  230. (tfloatdef(def).floattype=s64real);
  231. end;
  232. function is_extended(def : tdef) : boolean;
  233. begin
  234. result:=(def.typ=floatdef) and
  235. (tfloatdef(def).floattype in [s80real,sc80real]);
  236. end;
  237. { returns true, if definition is a "real" real (i.e. single/double/extended) }
  238. function is_real(def : tdef) : boolean;
  239. begin
  240. result:=(def.typ=floatdef) and
  241. (tfloatdef(def).floattype in [s32real,s64real,s80real]);
  242. end;
  243. function range_to_basetype(l,h:TConstExprInt):tordtype;
  244. begin
  245. { prefer signed over unsigned }
  246. if (l>=int64(-128)) and (h<=127) then
  247. range_to_basetype:=s8bit
  248. else if (l>=0) and (h<=255) then
  249. range_to_basetype:=u8bit
  250. else if (l>=int64(-32768)) and (h<=32767) then
  251. range_to_basetype:=s16bit
  252. else if (l>=0) and (h<=65535) then
  253. range_to_basetype:=u16bit
  254. else if (l>=int64(low(longint))) and (h<=high(longint)) then
  255. range_to_basetype:=s32bit
  256. else if (l>=low(cardinal)) and (h<=high(cardinal)) then
  257. range_to_basetype:=u32bit
  258. else
  259. range_to_basetype:=s64bit;
  260. end;
  261. procedure range_to_type(l,h:TConstExprInt;var def:tdef);
  262. begin
  263. { prefer signed over unsigned }
  264. if (l>=int64(-128)) and (h<=127) then
  265. def:=s8inttype
  266. else if (l>=0) and (h<=255) then
  267. def:=u8inttype
  268. else if (l>=int64(-32768)) and (h<=32767) then
  269. def:=s16inttype
  270. else if (l>=0) and (h<=65535) then
  271. def:=u16inttype
  272. else if (l>=int64(low(longint))) and (h<=high(longint)) then
  273. def:=s32inttype
  274. else if (l>=low(cardinal)) and (h<=high(cardinal)) then
  275. def:=u32inttype
  276. else if (l>=low(int64)) and (h<=high(int64)) then
  277. def:=s64inttype
  278. else
  279. def:=u64inttype;
  280. end;
  281. procedure int_to_type(v:TConstExprInt;var def:tdef);
  282. begin
  283. range_to_type(v,v,def);
  284. end;
  285. { true if p is an ordinal }
  286. function is_ordinal(def : tdef) : boolean;
  287. var
  288. dt : tordtype;
  289. begin
  290. case def.typ of
  291. orddef :
  292. begin
  293. dt:=torddef(def).ordtype;
  294. is_ordinal:=dt in [uchar,uwidechar,
  295. u8bit,u16bit,u32bit,u64bit,
  296. s8bit,s16bit,s32bit,s64bit,
  297. pasbool8,pasbool16,pasbool32,pasbool64,
  298. bool8bit,bool16bit,bool32bit,bool64bit];
  299. end;
  300. enumdef :
  301. is_ordinal:=true;
  302. else
  303. is_ordinal:=false;
  304. end;
  305. end;
  306. { true if p is a string }
  307. function is_string(def : tdef) : boolean;
  308. begin
  309. is_string := (assigned(def) and (def.typ = stringdef));
  310. end;
  311. function is_stringlike(def : tdef) : boolean;
  312. begin
  313. result := is_string(def) or
  314. is_anychar(def) or
  315. is_pchar(def) or
  316. is_pwidechar(def) or
  317. is_chararray(def) or
  318. is_widechararray(def) or
  319. is_open_chararray(def) or
  320. is_open_widechararray(def) or
  321. (def=java_jlstring);
  322. end;
  323. function is_enum(def : tdef) : boolean;
  324. begin
  325. result:=def.typ=enumdef;
  326. end;
  327. function is_set(def : tdef) : boolean;
  328. begin
  329. result:=def.typ=setdef;
  330. end;
  331. { returns the min. value of the type }
  332. function get_min_value(def : tdef) : TConstExprInt;
  333. begin
  334. case def.typ of
  335. orddef:
  336. result:=torddef(def).low;
  337. enumdef:
  338. result:=int64(tenumdef(def).min);
  339. else
  340. result:=0;
  341. end;
  342. end;
  343. { returns the max. value of the type }
  344. function get_max_value(def : tdef) : TConstExprInt;
  345. begin
  346. case def.typ of
  347. orddef:
  348. result:=torddef(def).high;
  349. enumdef:
  350. result:=tenumdef(def).max;
  351. else
  352. result:=0;
  353. end;
  354. end;
  355. { true if p is an integer }
  356. function is_integer(def : tdef) : boolean;
  357. begin
  358. result:=(def.typ=orddef) and
  359. (torddef(def).ordtype in [u8bit,u16bit,u32bit,u64bit,
  360. s8bit,s16bit,s32bit,s64bit]);
  361. end;
  362. { true if p is a boolean }
  363. function is_boolean(def : tdef) : boolean;
  364. begin
  365. result:=(def.typ=orddef) and
  366. (torddef(def).ordtype in [pasbool8,pasbool16,pasbool32,pasbool64,bool8bit,bool16bit,bool32bit,bool64bit]);
  367. end;
  368. function is_pasbool(def : tdef) : boolean;
  369. begin
  370. result:=(def.typ=orddef) and
  371. (torddef(def).ordtype in [pasbool8,pasbool16,pasbool32,pasbool64]);
  372. end;
  373. { true if def is a C-style boolean (non-zero value = true, zero = false) }
  374. function is_cbool(def : tdef) : boolean;
  375. begin
  376. result:=(def.typ=orddef) and
  377. (torddef(def).ordtype in [bool8bit,bool16bit,bool32bit,bool64bit]);
  378. end;
  379. { true if p is a void }
  380. function is_void(def : tdef) : boolean;
  381. begin
  382. result:=(def.typ=orddef) and
  383. (torddef(def).ordtype=uvoid);
  384. end;
  385. { true if p is a char }
  386. function is_char(def : tdef) : boolean;
  387. begin
  388. result:=(def.typ=orddef) and
  389. (torddef(def).ordtype=uchar);
  390. end;
  391. { true if p is a wchar }
  392. function is_widechar(def : tdef) : boolean;
  393. begin
  394. result:=(def.typ=orddef) and
  395. (torddef(def).ordtype=uwidechar);
  396. end;
  397. { true if p is a char or wchar }
  398. function is_anychar(def : tdef) : boolean;
  399. begin
  400. result:=(def.typ=orddef) and
  401. (torddef(def).ordtype in [uchar,uwidechar])
  402. end;
  403. { true if p is signed (integer) }
  404. function is_signed(def : tdef) : boolean;
  405. begin
  406. case def.typ of
  407. orddef :
  408. result:=torddef(def).low < 0;
  409. enumdef :
  410. result:=tenumdef(def).min < 0;
  411. arraydef :
  412. result:=is_signed(tarraydef(def).rangedef);
  413. else
  414. result:=false;
  415. end;
  416. end;
  417. function is_in_limit(def_from,def_to : tdef) : boolean;
  418. begin
  419. if (def_from.typ<>def_to.typ) or
  420. not(def_from.typ in [orddef,enumdef,setdef]) then
  421. begin
  422. is_in_limit := false;
  423. exit;
  424. end;
  425. case def_from.typ of
  426. orddef:
  427. is_in_limit:=(torddef(def_from).low>=torddef(def_to).low) and
  428. (torddef(def_from).high<=torddef(def_to).high);
  429. enumdef:
  430. is_in_limit:=(tenumdef(def_from).min>=tenumdef(def_to).min) and
  431. (tenumdef(def_from).max<=tenumdef(def_to).max);
  432. setdef:
  433. is_in_limit:=(tsetdef(def_from).setbase>=tsetdef(def_to).setbase) and
  434. (tsetdef(def_from).setmax<=tsetdef(def_to).setmax);
  435. else
  436. is_in_limit:=false;
  437. end;
  438. end;
  439. function is_managed_type(def: tdef): boolean;{$ifdef USEINLINE}inline;{$endif}
  440. begin
  441. result:=def.needs_inittable;
  442. end;
  443. { true, if p points to an open array def }
  444. function is_open_string(p : tdef) : boolean;
  445. begin
  446. is_open_string:=(p.typ=stringdef) and
  447. (tstringdef(p).stringtype=st_shortstring) and
  448. (tstringdef(p).len=0);
  449. end;
  450. { true, if p points to a zero based array def }
  451. function is_zero_based_array(p : tdef) : boolean;
  452. begin
  453. result:=(p.typ=arraydef) and
  454. (tarraydef(p).lowrange=0) and
  455. not(is_special_array(p));
  456. end;
  457. { true if p points to a dynamic array def }
  458. function is_dynamic_array(p : tdef) : boolean;
  459. begin
  460. result:=(p.typ=arraydef) and
  461. (ado_IsDynamicArray in tarraydef(p).arrayoptions);
  462. end;
  463. { true, if p points to an open array def }
  464. function is_open_array(p : tdef) : boolean;
  465. begin
  466. { check for s32inttype is needed, because for u32bit the high
  467. range is also -1 ! (PFV) }
  468. result:=(p.typ=arraydef) and
  469. (tarraydef(p).rangedef=s32inttype) and
  470. (tarraydef(p).lowrange=0) and
  471. (tarraydef(p).highrange=-1) and
  472. ((tarraydef(p).arrayoptions * [ado_IsVariant,ado_IsArrayOfConst,ado_IsConstructor,ado_IsDynamicArray])=[]);
  473. end;
  474. { true, if p points to an array of const def }
  475. function is_array_constructor(p : tdef) : boolean;
  476. begin
  477. result:=(p.typ=arraydef) and
  478. (ado_IsConstructor in tarraydef(p).arrayoptions);
  479. end;
  480. { true, if p points to a variant array }
  481. function is_variant_array(p : tdef) : boolean;
  482. begin
  483. result:=(p.typ=arraydef) and
  484. (ado_IsVariant in tarraydef(p).arrayoptions);
  485. end;
  486. { true, if p points to an array of const }
  487. function is_array_of_const(p : tdef) : boolean;
  488. begin
  489. result:=(p.typ=arraydef) and
  490. (ado_IsArrayOfConst in tarraydef(p).arrayoptions);
  491. end;
  492. { true, if p points to a special array, bitpacked arrays aren't special in this regard though }
  493. function is_special_array(p : tdef) : boolean;
  494. begin
  495. result:=(p.typ=arraydef) and
  496. (
  497. ((tarraydef(p).arrayoptions * [ado_IsVariant,ado_IsArrayOfConst,ado_IsConstructor,ado_IsDynamicArray])<>[]) or
  498. is_open_array(p)
  499. );
  500. end;
  501. { true if p is an ansi string def }
  502. function is_ansistring(p : tdef) : boolean;
  503. begin
  504. is_ansistring:=(p.typ=stringdef) and
  505. (tstringdef(p).stringtype=st_ansistring);
  506. end;
  507. { true if p is an ansi string def with codepage CP_NONE }
  508. function is_rawbytestring(p : tdef) : boolean;
  509. begin
  510. is_rawbytestring:=(p.typ=stringdef) and
  511. (tstringdef(p).stringtype=st_ansistring) and
  512. (tstringdef(p).encoding=globals.CP_NONE);
  513. end;
  514. { true if p is an long string def }
  515. function is_longstring(p : tdef) : boolean;
  516. begin
  517. is_longstring:=(p.typ=stringdef) and
  518. (tstringdef(p).stringtype=st_longstring);
  519. end;
  520. { true if p is an wide string def }
  521. function is_widestring(p : tdef) : boolean;
  522. begin
  523. is_widestring:=(p.typ=stringdef) and
  524. (tstringdef(p).stringtype=st_widestring);
  525. end;
  526. { true if p is an wide string def }
  527. function is_wide_or_unicode_string(p : tdef) : boolean;
  528. begin
  529. is_wide_or_unicode_string:=(p.typ=stringdef) and
  530. (tstringdef(p).stringtype in [st_widestring,st_unicodestring]);
  531. end;
  532. { true if p is an unicode string def }
  533. function is_unicodestring(p : tdef) : boolean;
  534. begin
  535. is_unicodestring:=(p.typ=stringdef) and
  536. (tstringdef(p).stringtype=st_unicodestring);
  537. end;
  538. { true if p is an short string def }
  539. function is_shortstring(p : tdef) : boolean;
  540. begin
  541. is_shortstring:=(p.typ=stringdef) and
  542. (tstringdef(p).stringtype=st_shortstring);
  543. end;
  544. { true if p is bit packed array def }
  545. function is_packed_array(p: tdef) : boolean;
  546. begin
  547. is_packed_array :=
  548. (p.typ = arraydef) and
  549. (ado_IsBitPacked in tarraydef(p).arrayoptions);
  550. end;
  551. { true if p is bit packed record def }
  552. function is_packed_record_or_object(p: tdef) : boolean;
  553. begin
  554. is_packed_record_or_object :=
  555. (p.typ in [recorddef,objectdef]) and
  556. (tabstractrecorddef(p).is_packed);
  557. end;
  558. { true if p is a char array def }
  559. function is_chararray(p : tdef) : boolean;
  560. begin
  561. is_chararray:=(p.typ=arraydef) and
  562. is_char(tarraydef(p).elementdef) and
  563. not(is_special_array(p));
  564. end;
  565. { true if p is a widechar array def }
  566. function is_widechararray(p : tdef) : boolean;
  567. begin
  568. is_widechararray:=(p.typ=arraydef) and
  569. is_widechar(tarraydef(p).elementdef) and
  570. not(is_special_array(p));
  571. end;
  572. { true if p is a open char array def }
  573. function is_open_chararray(p : tdef) : boolean;
  574. begin
  575. is_open_chararray:= is_open_array(p) and
  576. is_char(tarraydef(p).elementdef);
  577. end;
  578. { true if p is a open wide char array def }
  579. function is_open_widechararray(p : tdef) : boolean;
  580. begin
  581. is_open_widechararray:= is_open_array(p) and
  582. is_widechar(tarraydef(p).elementdef);
  583. end;
  584. { true if p is a pchar def }
  585. function is_pchar(p : tdef) : boolean;
  586. begin
  587. is_pchar:=(p.typ=pointerdef) and
  588. (is_char(tpointerdef(p).pointeddef) or
  589. (is_zero_based_array(tpointerdef(p).pointeddef) and
  590. is_chararray(tpointerdef(p).pointeddef)));
  591. end;
  592. { true if p is a pchar def }
  593. function is_pwidechar(p : tdef) : boolean;
  594. begin
  595. is_pwidechar:=(p.typ=pointerdef) and
  596. (is_widechar(tpointerdef(p).pointeddef) or
  597. (is_zero_based_array(tpointerdef(p).pointeddef) and
  598. is_widechararray(tpointerdef(p).pointeddef)));
  599. end;
  600. { true if p is a voidpointer def }
  601. function is_voidpointer(p : tdef) : boolean;
  602. begin
  603. is_voidpointer:=(p.typ=pointerdef) and
  604. (tpointerdef(p).pointeddef.typ=orddef) and
  605. (torddef(tpointerdef(p).pointeddef).ordtype=uvoid);
  606. end;
  607. { true, if def is a 32 bit int type }
  608. function is_32bitint(def : tdef) : boolean;
  609. begin
  610. result:=(def.typ=orddef) and (torddef(def).ordtype in [u32bit,s32bit])
  611. end;
  612. { true, if def is a 64 bit int type }
  613. function is_64bitint(def : tdef) : boolean;
  614. begin
  615. is_64bitint:=(def.typ=orddef) and (torddef(def).ordtype in [u64bit,s64bit])
  616. end;
  617. { true, if def is a 64 bit type }
  618. function is_64bit(def : tdef) : boolean;
  619. begin
  620. is_64bit:=(def.typ=orddef) and (torddef(def).ordtype in [u64bit,s64bit,scurrency,pasbool64,bool64bit])
  621. end;
  622. { if l isn't in the range of todef a range check error (if not explicit) is generated and
  623. the value is placed within the range }
  624. procedure testrange(todef : tdef;var l : tconstexprint;explicit,forcerangecheck:boolean);
  625. var
  626. lv,hv: TConstExprInt;
  627. begin
  628. { for 64 bit types we need only to check if it is less than }
  629. { zero, if def is a qword node }
  630. getrange(todef,lv,hv);
  631. if (l<lv) or (l>hv) then
  632. begin
  633. if not explicit then
  634. begin
  635. if ((todef.typ=enumdef) and
  636. { delphi allows range check errors in
  637. enumeration type casts FK }
  638. not(m_delphi in current_settings.modeswitches)) or
  639. (cs_check_range in current_settings.localswitches) or
  640. forcerangecheck then
  641. Message3(type_e_range_check_error_bounds,tostr(l),tostr(lv),tostr(hv))
  642. else
  643. Message3(type_w_range_check_error_bounds,tostr(l),tostr(lv),tostr(hv));
  644. end;
  645. { Fix the value to fit in the allocated space for this type of variable }
  646. case longint(todef.size) of
  647. 1: l := l and $ff;
  648. 2: l := l and $ffff;
  649. 4: l := l and $ffffffff;
  650. end;
  651. {reset sign, i.e. converting -1 to qword changes the value to high(qword)}
  652. l.signed:=false;
  653. { do sign extension if necessary (JM) }
  654. if is_signed(todef) then
  655. begin
  656. case longint(todef.size) of
  657. 1: l.svalue := shortint(l.svalue);
  658. 2: l.svalue := smallint(l.svalue);
  659. 4: l.svalue := longint(l.svalue);
  660. end;
  661. l.signed:=true;
  662. end;
  663. end;
  664. end;
  665. { return the range from def in l and h }
  666. procedure getrange(def : tdef;out l, h : TConstExprInt);
  667. begin
  668. case def.typ of
  669. orddef :
  670. begin
  671. l:=torddef(def).low;
  672. h:=torddef(def).high;
  673. end;
  674. enumdef :
  675. begin
  676. l:=int64(tenumdef(def).min);
  677. h:=int64(tenumdef(def).max);
  678. end;
  679. arraydef :
  680. begin
  681. l:=int64(tarraydef(def).lowrange);
  682. h:=int64(tarraydef(def).highrange);
  683. end;
  684. else
  685. internalerror(200611054);
  686. end;
  687. end;
  688. function mmx_type(p : tdef) : tmmxtype;
  689. begin
  690. mmx_type:=mmxno;
  691. if is_mmx_able_array(p) then
  692. begin
  693. if tarraydef(p).elementdef.typ=floatdef then
  694. case tfloatdef(tarraydef(p).elementdef).floattype of
  695. s32real:
  696. mmx_type:=mmxsingle;
  697. end
  698. else
  699. case torddef(tarraydef(p).elementdef).ordtype of
  700. u8bit:
  701. mmx_type:=mmxu8bit;
  702. s8bit:
  703. mmx_type:=mmxs8bit;
  704. u16bit:
  705. mmx_type:=mmxu16bit;
  706. s16bit:
  707. mmx_type:=mmxs16bit;
  708. u32bit:
  709. mmx_type:=mmxu32bit;
  710. s32bit:
  711. mmx_type:=mmxs32bit;
  712. end;
  713. end;
  714. end;
  715. function is_vector(p : tdef) : boolean;
  716. begin
  717. result:=(p.typ=arraydef) and
  718. not(is_special_array(p)) and
  719. (tarraydef(p).elementdef.typ=floatdef) and
  720. (tfloatdef(tarraydef(p).elementdef).floattype in [s32real,s64real]);
  721. end;
  722. { returns if the passed type (array) fits into an mm register }
  723. function fits_in_mm_register(p : tdef) : boolean;
  724. begin
  725. {$ifdef x86}
  726. result:= is_vector(p) and
  727. (
  728. (tarraydef(p).elementdef.typ=floatdef) and
  729. (
  730. (tarraydef(p).lowrange=0) and
  731. (tarraydef(p).highrange=3) and
  732. (tfloatdef(tarraydef(p).elementdef).floattype=s32real)
  733. )
  734. ) or
  735. (
  736. (tarraydef(p).elementdef.typ=floatdef) and
  737. (
  738. (tarraydef(p).lowrange=0) and
  739. (tarraydef(p).highrange=1) and
  740. (tfloatdef(tarraydef(p).elementdef).floattype=s64real)
  741. )
  742. );
  743. {$else x86}
  744. result:=false;
  745. {$endif x86}
  746. end;
  747. function is_mmx_able_array(p : tdef) : boolean;
  748. begin
  749. {$ifdef SUPPORT_MMX}
  750. if (cs_mmx_saturation in current_settings.localswitches) then
  751. begin
  752. is_mmx_able_array:=(p.typ=arraydef) and
  753. not(is_special_array(p)) and
  754. (
  755. (
  756. (tarraydef(p).elementdef.typ=orddef) and
  757. (
  758. (
  759. (tarraydef(p).lowrange=0) and
  760. (tarraydef(p).highrange=1) and
  761. (torddef(tarraydef(p).elementdef).ordtype in [u32bit,s32bit])
  762. )
  763. or
  764. (
  765. (tarraydef(p).lowrange=0) and
  766. (tarraydef(p).highrange=3) and
  767. (torddef(tarraydef(p).elementdef).ordtype in [u16bit,s16bit])
  768. )
  769. )
  770. )
  771. or
  772. (
  773. (
  774. (tarraydef(p).elementdef.typ=floatdef) and
  775. (
  776. (tarraydef(p).lowrange=0) and
  777. (tarraydef(p).highrange=1) and
  778. (tfloatdef(tarraydef(p).elementdef).floattype=s32real)
  779. )
  780. )
  781. )
  782. );
  783. end
  784. else
  785. begin
  786. is_mmx_able_array:=(p.typ=arraydef) and
  787. (
  788. (
  789. (tarraydef(p).elementdef.typ=orddef) and
  790. (
  791. (
  792. (tarraydef(p).lowrange=0) and
  793. (tarraydef(p).highrange=1) and
  794. (torddef(tarraydef(p).elementdef).ordtype in [u32bit,s32bit])
  795. )
  796. or
  797. (
  798. (tarraydef(p).lowrange=0) and
  799. (tarraydef(p).highrange=3) and
  800. (torddef(tarraydef(p).elementdef).ordtype in [u16bit,s16bit])
  801. )
  802. or
  803. (
  804. (tarraydef(p).lowrange=0) and
  805. (tarraydef(p).highrange=7) and
  806. (torddef(tarraydef(p).elementdef).ordtype in [u8bit,s8bit])
  807. )
  808. )
  809. )
  810. or
  811. (
  812. (tarraydef(p).elementdef.typ=floatdef) and
  813. (
  814. (tarraydef(p).lowrange=0) and
  815. (tarraydef(p).highrange=1) and
  816. (tfloatdef(tarraydef(p).elementdef).floattype=s32real)
  817. )
  818. )
  819. );
  820. end;
  821. {$else SUPPORT_MMX}
  822. is_mmx_able_array:=false;
  823. {$endif SUPPORT_MMX}
  824. end;
  825. function def_cgsize(def: tdef): tcgsize;
  826. begin
  827. case def.typ of
  828. orddef,
  829. enumdef,
  830. setdef:
  831. begin
  832. result:=int_cgsize(def.size);
  833. if is_signed(def) then
  834. result:=tcgsize(ord(result)+(ord(OS_S8)-ord(OS_8)));
  835. end;
  836. classrefdef,
  837. pointerdef:
  838. result := OS_ADDR;
  839. procvardef:
  840. begin
  841. if not tprocvardef(def).is_addressonly then
  842. {$if sizeof(pint) = 4}
  843. result:=OS_64
  844. {$else} {$if sizeof(pint) = 8}
  845. result:=OS_128
  846. {$else}
  847. internalerror(200707141)
  848. {$endif} {$endif}
  849. else
  850. result:=OS_ADDR;
  851. end;
  852. stringdef :
  853. begin
  854. if is_ansistring(def) or is_wide_or_unicode_string(def) then
  855. result := OS_ADDR
  856. else
  857. result:=int_cgsize(def.size);
  858. end;
  859. objectdef :
  860. begin
  861. if is_implicit_pointer_object_type(def) then
  862. result := OS_ADDR
  863. else
  864. result:=int_cgsize(def.size);
  865. end;
  866. floatdef:
  867. if cs_fp_emulation in current_settings.moduleswitches then
  868. result:=int_cgsize(def.size)
  869. else
  870. result:=tfloat2tcgsize[tfloatdef(def).floattype];
  871. recorddef :
  872. result:=int_cgsize(def.size);
  873. arraydef :
  874. begin
  875. if not is_special_array(def) then
  876. result := int_cgsize(def.size)
  877. else
  878. begin
  879. if is_dynamic_array(def) then
  880. result := OS_ADDR
  881. else
  882. result := OS_NO;
  883. end;
  884. end;
  885. else
  886. begin
  887. { undefined size }
  888. result:=OS_NO;
  889. end;
  890. end;
  891. end;
  892. { In Windows 95 era, ordinals were restricted to [u8bit,s32bit,s16bit,bool16bit]
  893. As of today, both signed and unsigned types from 8 to 64 bits are supported. }
  894. function is_automatable(p : tdef) : boolean;
  895. begin
  896. result:=false;
  897. case p.typ of
  898. orddef:
  899. result:=torddef(p).ordtype in [u8bit,s8bit,u16bit,s16bit,u32bit,s32bit,
  900. u64bit,s64bit,bool16bit,scurrency];
  901. floatdef:
  902. result:=tfloatdef(p).floattype in [s64currency,s64real,s32real];
  903. stringdef:
  904. result:=tstringdef(p).stringtype in [st_ansistring,st_widestring,st_unicodestring];
  905. variantdef:
  906. result:=true;
  907. objectdef:
  908. result:=tobjectdef(p).objecttype in [odt_interfacecom,odt_dispinterface,odt_interfacecorba];
  909. end;
  910. end;
  911. {# returns true, if the type passed is a varset }
  912. function is_smallset(p : tdef) : boolean;
  913. begin
  914. result:=(p.typ=setdef) and (p.size in [1,2,4])
  915. end;
  916. function is_bareprocdef(pd : tprocdef): boolean;
  917. begin
  918. result:=(pd.maxparacount=0) and
  919. (is_void(pd.returndef) or
  920. (pd.proctypeoption = potype_constructor));
  921. end;
  922. function get_common_intdef(ld, rd: torddef; keep_sign_if_equal: boolean): torddef;
  923. var
  924. llow, lhigh: tconstexprint;
  925. begin
  926. llow:=rd.low;
  927. if llow<ld.low then
  928. llow:=ld.low;
  929. lhigh:=rd.high;
  930. if lhigh<ld.high then
  931. lhigh:=ld.high;
  932. case range_to_basetype(llow,lhigh) of
  933. s8bit:
  934. result:=torddef(s8inttype);
  935. u8bit:
  936. result:=torddef(u8inttype);
  937. s16bit:
  938. result:=torddef(s16inttype);
  939. u16bit:
  940. result:=torddef(u16inttype);
  941. s32bit:
  942. result:=torddef(s32inttype);
  943. u32bit:
  944. result:=torddef(u32inttype);
  945. s64bit:
  946. result:=torddef(s64inttype);
  947. u64bit:
  948. result:=torddef(u64inttype);
  949. else
  950. begin
  951. { avoid warning }
  952. result:=nil;
  953. internalerror(200802291);
  954. end;
  955. end;
  956. if keep_sign_if_equal and
  957. (is_signed(ld)=is_signed(rd)) and
  958. (is_signed(result)<>is_signed(ld)) then
  959. case result.ordtype of
  960. s8bit:
  961. result:=torddef(u8inttype);
  962. u8bit:
  963. result:=torddef(s16inttype);
  964. s16bit:
  965. result:=torddef(u16inttype);
  966. u16bit:
  967. result:=torddef(s32inttype);
  968. s32bit:
  969. result:=torddef(u32inttype);
  970. u32bit:
  971. result:=torddef(s64inttype);
  972. s64bit:
  973. result:=torddef(u64inttype);
  974. end;
  975. end;
  976. function is_valid_univ_para_type(def: tdef): boolean;
  977. begin
  978. result:=
  979. not is_open_array(def) and
  980. not is_void(def) and
  981. (def.typ<>formaldef);
  982. end;
  983. function is_nested_pd(def: tabstractprocdef): boolean;{$ifdef USEINLINE}inline;{$endif}
  984. begin
  985. result:=def.parast.symtablelevel>normal_function_level;
  986. end;
  987. function is_typeparam(def : tdef) : boolean;{$ifdef USEINLINE}inline;{$endif}
  988. begin
  989. result:=(def.typ=undefineddef);
  990. end;
  991. function is_methodpointer(def: tdef): boolean;
  992. begin
  993. result:=(def.typ=procvardef) and (po_methodpointer in tprocvardef(def).procoptions);
  994. end;
  995. end.