defutil.pas 41 KB

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