defutil.pas 33 KB

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