defutil.pas 33 KB

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