2
0

defutil.pas 37 KB

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