defutil.pas 36 KB

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