types.pas 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  1. {
  2. $Id$
  3. Copyright (C) 1998-2000 by Florian Klaempfl
  4. This unit provides some help routines for type handling
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit types;
  19. interface
  20. uses
  21. cobjects,symtable,cpuinfo
  22. {$IFDEF NEWST}
  23. ,defs
  24. {$ENDIF NEWST};
  25. type
  26. tmmxtype = (mmxno,mmxu8bit,mmxs8bit,mmxu16bit,mmxs16bit,
  27. mmxu32bit,mmxs32bit,mmxfixed16,mmxsingle);
  28. const
  29. { true if we must never copy this parameter }
  30. never_copy_const_param : boolean = false;
  31. {*****************************************************************************
  32. Basic type functions
  33. *****************************************************************************}
  34. { returns true, if def defines an ordinal type }
  35. function is_ordinal(def : pdef) : boolean;
  36. { returns the min. value of the type }
  37. function get_min_value(def : pdef) : longint;
  38. { returns true, if def defines an ordinal type }
  39. function is_integer(def : pdef) : boolean;
  40. { true if p is a boolean }
  41. function is_boolean(def : pdef) : boolean;
  42. { true if p is a char }
  43. function is_char(def : pdef) : boolean;
  44. { true if p is a void}
  45. function is_void(def : pdef) : boolean;
  46. { true if p is a smallset def }
  47. function is_smallset(p : pdef) : boolean;
  48. { returns true, if def defines a signed data type (only for ordinal types) }
  49. function is_signed(def : pdef) : boolean;
  50. {*****************************************************************************
  51. Array helper functions
  52. *****************************************************************************}
  53. { true, if p points to a zero based (non special like open or
  54. dynamic array def, mainly this is used to see if the array
  55. is convertable to a pointer }
  56. function is_zero_based_array(p : pdef) : boolean;
  57. { true if p points to an open array def }
  58. function is_open_array(p : pdef) : boolean;
  59. { true, if p points to an array of const def }
  60. function is_array_constructor(p : pdef) : boolean;
  61. { true, if p points to a variant array }
  62. function is_variant_array(p : pdef) : boolean;
  63. { true, if p points to an array of const }
  64. function is_array_of_const(p : pdef) : boolean;
  65. { true, if p points any kind of special array }
  66. function is_special_array(p : pdef) : boolean;
  67. { true if p is a char array def }
  68. function is_chararray(p : pdef) : boolean;
  69. {*****************************************************************************
  70. String helper functions
  71. *****************************************************************************}
  72. { true if p points to an open string def }
  73. function is_open_string(p : pdef) : boolean;
  74. { true if p is an ansi string def }
  75. function is_ansistring(p : pdef) : boolean;
  76. { true if p is a long string def }
  77. function is_longstring(p : pdef) : boolean;
  78. { true if p is a wide string def }
  79. function is_widestring(p : pdef) : boolean;
  80. { true if p is a short string def }
  81. function is_shortstring(p : pdef) : boolean;
  82. { true if p is a pchar def }
  83. function is_pchar(p : pdef) : boolean;
  84. { true if p is a voidpointer def }
  85. function is_voidpointer(p : pdef) : boolean;
  86. { returns true, if def uses FPU }
  87. function is_fpu(def : pdef) : boolean;
  88. { true if the return value is in EAX }
  89. function ret_in_acc(def : pdef) : boolean;
  90. { true if uses a parameter as return value }
  91. function ret_in_param(def : pdef) : boolean;
  92. { true, if def is a 64 bit int type }
  93. function is_64bitint(def : pdef) : boolean;
  94. function push_high_param(def : pdef) : boolean;
  95. { true if a parameter is too large to copy and only the address is pushed }
  96. function push_addr_param(def : pdef) : boolean;
  97. { true, if def1 and def2 are semantical the same }
  98. function is_equal(def1,def2 : pdef) : boolean;
  99. { checks for type compatibility (subgroups of type) }
  100. { used for case statements... probably missing stuff }
  101. { to use on other types }
  102. function is_subequal(def1, def2: pdef): boolean;
  103. { same as is_equal, but with error message if failed }
  104. function CheckTypes(def1,def2 : pdef) : boolean;
  105. function equal_constsym(sym1,sym2:pconstsym):boolean;
  106. { true, if two parameter lists are equal }
  107. { if acp is cp_none, all have to match exactly }
  108. { if acp is cp_value_equal_const call by value }
  109. { and call by const parameter are assumed as }
  110. { equal }
  111. { if acp is cp_all the var const or nothing are considered equal }
  112. type
  113. compare_type = ( cp_none, cp_value_equal_const, cp_all);
  114. function equal_paras(paralist1,paralist2 : plinkedlist; acp : compare_type) : boolean;
  115. { true if a type can be allowed for another one
  116. in a func var }
  117. function convertable_paras(paralist1,paralist2 : plinkedlist; acp : compare_type) : boolean;
  118. { true if a function can be assigned to a procvar }
  119. function proc_to_procvar_equal(def1:pprocdef;def2:pprocvardef) : boolean;
  120. { if l isn't in the range of def a range check error is generated and
  121. the value is placed within the range }
  122. procedure testrange(def : pdef;var l : tconstexprint);
  123. { returns the range of def }
  124. procedure getrange(def : pdef;var l : longint;var h : longint);
  125. { some type helper routines for MMX support }
  126. function is_mmx_able_array(p : pdef) : boolean;
  127. { returns the mmx type }
  128. function mmx_type(p : pdef) : tmmxtype;
  129. { returns true, if sym needs an entry in the proplist of a class rtti }
  130. function needs_prop_entry(sym : psym) : boolean;
  131. { returns true, if p contains data which needs init/final code }
  132. function needs_init_final(p : psymtable) : boolean;
  133. implementation
  134. uses
  135. strings,globtype,globals,htypechk,
  136. tree,verbose,symconst;
  137. var
  138. b_needs_init_final : boolean;
  139. procedure _needs_init_final(p : pnamedindexobject);{$ifndef FPC}far;{$endif}
  140. begin
  141. if (psym(p)^.typ=varsym) and
  142. assigned(pvarsym(p)^.vartype.def) and
  143. not((pvarsym(p)^.vartype.def^.deftype=objectdef) and
  144. pobjectdef(pvarsym(p)^.vartype.def)^.is_class) and
  145. pvarsym(p)^.vartype.def^.needs_inittable then
  146. b_needs_init_final:=true;
  147. end;
  148. { returns true, if p contains data which needs init/final code }
  149. function needs_init_final(p : psymtable) : boolean;
  150. begin
  151. b_needs_init_final:=false;
  152. p^.foreach({$ifndef TP}@{$endif}_needs_init_final);
  153. needs_init_final:=b_needs_init_final;
  154. end;
  155. function needs_prop_entry(sym : psym) : boolean;
  156. begin
  157. needs_prop_entry:=(sp_published in psym(sym)^.symoptions) and
  158. (sym^.typ in [propertysym,varsym]);
  159. end;
  160. function equal_constsym(sym1,sym2:pconstsym):boolean;
  161. var
  162. p1,p2,pend : pchar;
  163. begin
  164. equal_constsym:=false;
  165. if sym1^.consttyp<>sym2^.consttyp then
  166. exit;
  167. case sym1^.consttyp of
  168. constint,
  169. constbool,
  170. constchar,
  171. constpointer,
  172. constord :
  173. equal_constsym:=(sym1^.value=sym2^.value);
  174. conststring,constresourcestring :
  175. begin
  176. if sym1^.len=sym2^.len then
  177. begin
  178. p1:=pchar(tpointerord(sym1^.value));
  179. p2:=pchar(tpointerord(sym2^.value));
  180. pend:=p1+sym1^.len;
  181. while (p1<pend) do
  182. begin
  183. if p1^<>p2^ then
  184. break;
  185. inc(p1);
  186. inc(p2);
  187. end;
  188. if (p1=pend) then
  189. equal_constsym:=true;
  190. end;
  191. end;
  192. constreal :
  193. equal_constsym:=(pbestreal(tpointerord(sym1^.value))^=pbestreal(tpointerord(sym2^.value))^);
  194. constset :
  195. equal_constsym:=(pnormalset(tpointerord(sym1^.value))^=pnormalset(tpointerord(sym2^.value))^);
  196. constnil :
  197. equal_constsym:=true;
  198. end;
  199. end;
  200. { compare_type = ( cp_none, cp_value_equal_const, cp_all); }
  201. function equal_paras(paralist1,paralist2 : plinkedlist; acp : compare_type) : boolean;
  202. var
  203. def1,def2 : pparaitem;
  204. begin
  205. def1:=pparaitem(paralist1^.first);
  206. def2:=pparaitem(paralist2^.first);
  207. while (assigned(def1)) and (assigned(def2)) do
  208. begin
  209. case acp of
  210. cp_value_equal_const :
  211. begin
  212. if not(is_equal(def1^.paratype.def,def2^.paratype.def)) or
  213. ((def1^.paratyp<>def2^.paratyp) and
  214. ((def1^.paratyp in [vs_var,vs_out]) or
  215. (def2^.paratyp in [vs_var,vs_out])
  216. )
  217. ) then
  218. begin
  219. equal_paras:=false;
  220. exit;
  221. end;
  222. end;
  223. cp_all :
  224. begin
  225. if not(is_equal(def1^.paratype.def,def2^.paratype.def)) or
  226. (def1^.paratyp<>def2^.paratyp) then
  227. begin
  228. equal_paras:=false;
  229. exit;
  230. end;
  231. end;
  232. cp_none :
  233. begin
  234. if not(is_equal(def1^.paratype.def,def2^.paratype.def)) then
  235. begin
  236. equal_paras:=false;
  237. exit;
  238. end;
  239. { also check default value if both have it declared }
  240. if assigned(def1^.defaultvalue) and
  241. assigned(def2^.defaultvalue) then
  242. begin
  243. if not equal_constsym(pconstsym(def1^.defaultvalue),pconstsym(def2^.defaultvalue)) then
  244. begin
  245. equal_paras:=false;
  246. exit;
  247. end;
  248. end;
  249. end;
  250. end;
  251. def1:=pparaitem(def1^.next);
  252. def2:=pparaitem(def2^.next);
  253. end;
  254. if (def1=nil) and (def2=nil) then
  255. equal_paras:=true
  256. else
  257. equal_paras:=false;
  258. end;
  259. function convertable_paras(paralist1,paralist2 : plinkedlist;acp : compare_type) : boolean;
  260. var
  261. def1,def2 : pparaitem;
  262. doconv : tconverttype;
  263. begin
  264. def1:=pparaitem(paralist1^.first);
  265. def2:=pparaitem(paralist2^.first);
  266. while (assigned(def1)) and (assigned(def2)) do
  267. begin
  268. case acp of
  269. cp_value_equal_const :
  270. begin
  271. if (isconvertable(def1^.paratype.def,def2^.paratype.def,doconv,callparan,false)=0) or
  272. ((def1^.paratyp<>def2^.paratyp) and
  273. ((def1^.paratyp in [vs_out,vs_var]) or
  274. (def2^.paratyp in [vs_out,vs_var])
  275. )
  276. ) then
  277. begin
  278. convertable_paras:=false;
  279. exit;
  280. end;
  281. end;
  282. cp_all :
  283. begin
  284. if (isconvertable(def1^.paratype.def,def2^.paratype.def,doconv,callparan,false)=0) or
  285. (def1^.paratyp<>def2^.paratyp) then
  286. begin
  287. convertable_paras:=false;
  288. exit;
  289. end;
  290. end;
  291. cp_none :
  292. begin
  293. if (isconvertable(def1^.paratype.def,def2^.paratype.def,doconv,callparan,false)=0) then
  294. begin
  295. convertable_paras:=false;
  296. exit;
  297. end;
  298. end;
  299. end;
  300. def1:=pparaitem(def1^.next);
  301. def2:=pparaitem(def2^.next);
  302. end;
  303. if (def1=nil) and (def2=nil) then
  304. convertable_paras:=true
  305. else
  306. convertable_paras:=false;
  307. end;
  308. { true if a function can be assigned to a procvar }
  309. function proc_to_procvar_equal(def1:pprocdef;def2:pprocvardef) : boolean;
  310. const
  311. po_comp = po_compatibility_options-[po_methodpointer,po_classmethod];
  312. var
  313. ismethod : boolean;
  314. begin
  315. proc_to_procvar_equal:=false;
  316. if not(assigned(def1)) or not(assigned(def2)) then
  317. exit;
  318. { check for method pointer }
  319. ismethod:=assigned(def1^.owner) and
  320. (def1^.owner^.symtabletype=objectsymtable);
  321. { I think methods of objects are also not compatible }
  322. { with procedure variables! (FK)
  323. and
  324. assigned(def1^.owner^.defowner) and
  325. (pobjectdef(def1^.owner^.defowner)^.is_class); }
  326. if (ismethod and not (po_methodpointer in def2^.procoptions)) or
  327. (not(ismethod) and (po_methodpointer in def2^.procoptions)) then
  328. begin
  329. Message(type_e_no_method_and_procedure_not_compatible);
  330. exit;
  331. end;
  332. { check return value and para's and options, methodpointer is already checked
  333. parameters may also be convertable }
  334. if is_equal(def1^.rettype.def,def2^.rettype.def) and
  335. (equal_paras(def1^.para,def2^.para,cp_all) or
  336. convertable_paras(def1^.para,def2^.para,cp_all)) and
  337. ((po_comp * def1^.procoptions)= (po_comp * def2^.procoptions)) then
  338. proc_to_procvar_equal:=true
  339. else
  340. proc_to_procvar_equal:=false;
  341. end;
  342. { returns true, if def uses FPU }
  343. function is_fpu(def : pdef) : boolean;
  344. begin
  345. is_fpu:=(def^.deftype=floatdef) and (pfloatdef(def)^.typ<>f32bit);
  346. end;
  347. { true if p is an ordinal }
  348. function is_ordinal(def : pdef) : boolean;
  349. var
  350. dt : tbasetype;
  351. begin
  352. case def^.deftype of
  353. orddef :
  354. begin
  355. dt:=porddef(def)^.typ;
  356. is_ordinal:=dt in [uchar,
  357. u8bit,u16bit,u32bit,u64bit,
  358. s8bit,s16bit,s32bit,s64bit,
  359. bool8bit,bool16bit,bool32bit];
  360. end;
  361. enumdef :
  362. is_ordinal:=true;
  363. else
  364. is_ordinal:=false;
  365. end;
  366. end;
  367. { returns the min. value of the type }
  368. function get_min_value(def : pdef) : longint;
  369. begin
  370. case def^.deftype of
  371. orddef:
  372. get_min_value:=porddef(def)^.low;
  373. enumdef:
  374. get_min_value:=penumdef(def)^.min;
  375. else
  376. get_min_value:=0;
  377. end;
  378. end;
  379. { true if p is an integer }
  380. function is_integer(def : pdef) : boolean;
  381. begin
  382. is_integer:=(def^.deftype=orddef) and
  383. (porddef(def)^.typ in [uauto,u8bit,u16bit,u32bit,u64bit,
  384. s8bit,s16bit,s32bit,s64bit]);
  385. end;
  386. { true if p is a boolean }
  387. function is_boolean(def : pdef) : boolean;
  388. begin
  389. is_boolean:=(def^.deftype=orddef) and
  390. (porddef(def)^.typ in [bool8bit,bool16bit,bool32bit]);
  391. end;
  392. { true if p is a void }
  393. function is_void(def : pdef) : boolean;
  394. begin
  395. is_void:=(def^.deftype=orddef) and
  396. (porddef(def)^.typ=uvoid);
  397. end;
  398. { true if p is a char }
  399. function is_char(def : pdef) : boolean;
  400. begin
  401. is_char:=(def^.deftype=orddef) and
  402. (porddef(def)^.typ=uchar);
  403. end;
  404. { true if p is signed (integer) }
  405. function is_signed(def : pdef) : boolean;
  406. var
  407. dt : tbasetype;
  408. begin
  409. case def^.deftype of
  410. orddef :
  411. begin
  412. dt:=porddef(def)^.typ;
  413. is_signed:=(dt in [s8bit,s16bit,s32bit,s64bit]);
  414. end;
  415. enumdef :
  416. is_signed:=false;
  417. else
  418. is_signed:=false;
  419. end;
  420. end;
  421. { true, if p points to an open array def }
  422. function is_open_string(p : pdef) : boolean;
  423. begin
  424. is_open_string:=(p^.deftype=stringdef) and
  425. (pstringdef(p)^.string_typ=st_shortstring) and
  426. (pstringdef(p)^.len=0);
  427. end;
  428. { true, if p points to a zero based array def }
  429. function is_zero_based_array(p : pdef) : boolean;
  430. begin
  431. is_zero_based_array:=(p^.deftype=arraydef) and
  432. (parraydef(p)^.lowrange=0) and
  433. not(is_special_array(p));
  434. end;
  435. { true, if p points to an open array def }
  436. function is_open_array(p : pdef) : boolean;
  437. begin
  438. { check for s32bitdef is needed, because for u32bit the high
  439. range is also -1 ! (PFV) }
  440. is_open_array:=(p^.deftype=arraydef) and
  441. (parraydef(p)^.rangetype.def=pdef(s32bitdef)) and
  442. (parraydef(p)^.lowrange=0) and
  443. (parraydef(p)^.highrange=-1) and
  444. not(parraydef(p)^.IsConstructor) and
  445. not(parraydef(p)^.IsVariant) and
  446. not(parraydef(p)^.IsArrayOfConst);
  447. end;
  448. { true, if p points to an array of const def }
  449. function is_array_constructor(p : pdef) : boolean;
  450. begin
  451. is_array_constructor:=(p^.deftype=arraydef) and
  452. (parraydef(p)^.IsConstructor);
  453. end;
  454. { true, if p points to a variant array }
  455. function is_variant_array(p : pdef) : boolean;
  456. begin
  457. is_variant_array:=(p^.deftype=arraydef) and
  458. (parraydef(p)^.IsVariant);
  459. end;
  460. { true, if p points to an array of const }
  461. function is_array_of_const(p : pdef) : boolean;
  462. begin
  463. is_array_of_const:=(p^.deftype=arraydef) and
  464. (parraydef(p)^.IsArrayOfConst);
  465. end;
  466. { true, if p points to a special array }
  467. function is_special_array(p : pdef) : boolean;
  468. begin
  469. is_special_array:=(p^.deftype=arraydef) and
  470. ((parraydef(p)^.IsVariant) or
  471. (parraydef(p)^.IsArrayOfConst) or
  472. (parraydef(p)^.IsConstructor) or
  473. is_open_array(p)
  474. );
  475. end;
  476. { true if p is an ansi string def }
  477. function is_ansistring(p : pdef) : boolean;
  478. begin
  479. is_ansistring:=(p^.deftype=stringdef) and
  480. (pstringdef(p)^.string_typ=st_ansistring);
  481. end;
  482. { true if p is an long string def }
  483. function is_longstring(p : pdef) : boolean;
  484. begin
  485. is_longstring:=(p^.deftype=stringdef) and
  486. (pstringdef(p)^.string_typ=st_longstring);
  487. end;
  488. { true if p is an wide string def }
  489. function is_widestring(p : pdef) : boolean;
  490. begin
  491. is_widestring:=(p^.deftype=stringdef) and
  492. (pstringdef(p)^.string_typ=st_widestring);
  493. end;
  494. { true if p is an short string def }
  495. function is_shortstring(p : pdef) : boolean;
  496. begin
  497. is_shortstring:=(p^.deftype=stringdef) and
  498. (pstringdef(p)^.string_typ=st_shortstring);
  499. end;
  500. { true if p is a char array def }
  501. function is_chararray(p : pdef) : boolean;
  502. begin
  503. is_chararray:=(p^.deftype=arraydef) and
  504. is_equal(parraydef(p)^.elementtype.def,cchardef) and
  505. not(is_special_array(p));
  506. end;
  507. { true if p is a pchar def }
  508. function is_pchar(p : pdef) : boolean;
  509. begin
  510. is_pchar:=(p^.deftype=pointerdef) and
  511. is_equal(Ppointerdef(p)^.pointertype.def,cchardef);
  512. end;
  513. { true if p is a voidpointer def }
  514. function is_voidpointer(p : pdef) : boolean;
  515. begin
  516. is_voidpointer:=(p^.deftype=pointerdef) and
  517. is_equal(Ppointerdef(p)^.pointertype.def,voiddef);
  518. end;
  519. { true if p is a smallset def }
  520. function is_smallset(p : pdef) : boolean;
  521. begin
  522. is_smallset:=(p^.deftype=setdef) and
  523. (psetdef(p)^.settype=smallset);
  524. end;
  525. { true if the return value is in accumulator (EAX for i386), D0 for 68k }
  526. function ret_in_acc(def : pdef) : boolean;
  527. begin
  528. ret_in_acc:=(def^.deftype in [orddef,pointerdef,enumdef,classrefdef]) or
  529. ((def^.deftype=stringdef) and (pstringdef(def)^.string_typ in [st_ansistring,st_widestring])) or
  530. ((def^.deftype=procvardef) and not(po_methodpointer in pprocvardef(def)^.procoptions)) or
  531. ((def^.deftype=objectdef) and pobjectdef(def)^.is_class) or
  532. ((def^.deftype=setdef) and (psetdef(def)^.settype=smallset)) or
  533. ((def^.deftype=floatdef) and (pfloatdef(def)^.typ=f32bit));
  534. end;
  535. { true, if def is a 64 bit int type }
  536. function is_64bitint(def : pdef) : boolean;
  537. begin
  538. is_64bitint:=(def^.deftype=orddef) and (porddef(def)^.typ in [u64bit,s64bit])
  539. end;
  540. { true if uses a parameter as return value }
  541. function ret_in_param(def : pdef) : boolean;
  542. begin
  543. ret_in_param:=(def^.deftype in [arraydef,recorddef]) or
  544. ((def^.deftype=stringdef) and (pstringdef(def)^.string_typ in [st_shortstring,st_longstring])) or
  545. ((def^.deftype=procvardef) and (po_methodpointer in pprocvardef(def)^.procoptions)) or
  546. ((def^.deftype=objectdef) and not(pobjectdef(def)^.is_class)) or
  547. ((def^.deftype=setdef) and (psetdef(def)^.settype<>smallset));
  548. end;
  549. function push_high_param(def : pdef) : boolean;
  550. begin
  551. push_high_param:=is_open_array(def) or
  552. is_open_string(def) or
  553. is_array_of_const(def);
  554. end;
  555. { true if a parameter is too large to copy and only the address is pushed }
  556. function push_addr_param(def : pdef) : boolean;
  557. begin
  558. push_addr_param:=false;
  559. if never_copy_const_param then
  560. push_addr_param:=true
  561. else
  562. begin
  563. case def^.deftype of
  564. formaldef :
  565. push_addr_param:=true;
  566. recorddef :
  567. push_addr_param:=(def^.size>4);
  568. arraydef :
  569. push_addr_param:=((Parraydef(def)^.highrange>Parraydef(def)^.lowrange) and (def^.size>4)) or
  570. is_open_array(def) or
  571. is_array_of_const(def) or
  572. is_array_constructor(def);
  573. objectdef :
  574. push_addr_param:=not(pobjectdef(def)^.is_class);
  575. stringdef :
  576. push_addr_param:=pstringdef(def)^.string_typ in [st_shortstring,st_longstring];
  577. procvardef :
  578. push_addr_param:=(po_methodpointer in pprocvardef(def)^.procoptions);
  579. setdef :
  580. push_addr_param:=(psetdef(def)^.settype<>smallset);
  581. end;
  582. end;
  583. end;
  584. { test if l is in the range of def, outputs error if out of range }
  585. procedure testrange(def : pdef;var l : tconstexprint);
  586. var
  587. lv,hv: longint;
  588. begin
  589. { for 64 bit types we need only to check if it is less than }
  590. { zero, if def is a qword node }
  591. if is_64bitint(def) then
  592. begin
  593. if (l<0) and (porddef(def)^.typ=u64bit) then
  594. begin
  595. l:=0;
  596. if (cs_check_range in aktlocalswitches) then
  597. Message(parser_e_range_check_error)
  598. else
  599. Message(parser_w_range_check_error);
  600. end;
  601. end
  602. else
  603. begin
  604. getrange(def,lv,hv);
  605. if (def^.deftype=orddef) and
  606. (porddef(def)^.typ=u32bit) then
  607. begin
  608. if lv<=hv then
  609. begin
  610. if (l<lv) or (l>hv) then
  611. begin
  612. if (cs_check_range in aktlocalswitches) then
  613. Message(parser_e_range_check_error)
  614. else
  615. Message(parser_w_range_check_error);
  616. end;
  617. end
  618. else
  619. { this happens with the wrap around problem }
  620. { if lv is positive and hv is over $7ffffff }
  621. { so it seems negative }
  622. begin
  623. if ((l>=0) and (l<lv)) or
  624. ((l<0) and (l>hv)) then
  625. begin
  626. if (cs_check_range in aktlocalswitches) then
  627. Message(parser_e_range_check_error)
  628. else
  629. Message(parser_w_range_check_error);
  630. end;
  631. end;
  632. end
  633. else if (l<lv) or (l>hv) then
  634. begin
  635. if (def^.deftype=enumdef) or
  636. (cs_check_range in aktlocalswitches) then
  637. Message(parser_e_range_check_error)
  638. else
  639. Message(parser_w_range_check_error);
  640. { Fix the value to fit in the allocated space for this type of variable }
  641. case def^.size of
  642. 1: l := l and $ff;
  643. 2: l := l and $ffff;
  644. end
  645. { l:=lv+(l mod (hv-lv+1));}
  646. end;
  647. end;
  648. end;
  649. { return the range from def in l and h }
  650. procedure getrange(def : pdef;var l : longint;var h : longint);
  651. begin
  652. case def^.deftype of
  653. orddef :
  654. begin
  655. l:=porddef(def)^.low;
  656. h:=porddef(def)^.high;
  657. end;
  658. enumdef :
  659. begin
  660. l:=penumdef(def)^.min;
  661. h:=penumdef(def)^.max;
  662. end;
  663. arraydef :
  664. begin
  665. l:=parraydef(def)^.lowrange;
  666. h:=parraydef(def)^.highrange;
  667. end;
  668. else
  669. internalerror(987);
  670. end;
  671. end;
  672. function mmx_type(p : pdef) : tmmxtype;
  673. begin
  674. mmx_type:=mmxno;
  675. if is_mmx_able_array(p) then
  676. begin
  677. if parraydef(p)^.elementtype.def^.deftype=floatdef then
  678. case pfloatdef(parraydef(p)^.elementtype.def)^.typ of
  679. s32real:
  680. mmx_type:=mmxsingle;
  681. f16bit:
  682. mmx_type:=mmxfixed16
  683. end
  684. else
  685. case porddef(parraydef(p)^.elementtype.def)^.typ of
  686. u8bit:
  687. mmx_type:=mmxu8bit;
  688. s8bit:
  689. mmx_type:=mmxs8bit;
  690. u16bit:
  691. mmx_type:=mmxu16bit;
  692. s16bit:
  693. mmx_type:=mmxs16bit;
  694. u32bit:
  695. mmx_type:=mmxu32bit;
  696. s32bit:
  697. mmx_type:=mmxs32bit;
  698. end;
  699. end;
  700. end;
  701. function is_mmx_able_array(p : pdef) : boolean;
  702. begin
  703. {$ifdef SUPPORT_MMX}
  704. if (cs_mmx_saturation in aktlocalswitches) then
  705. begin
  706. is_mmx_able_array:=(p^.deftype=arraydef) and
  707. not(is_special_array(p)) and
  708. (
  709. (
  710. (parraydef(p)^.elementtype.def^.deftype=orddef) and
  711. (
  712. (
  713. (parraydef(p)^.lowrange=0) and
  714. (parraydef(p)^.highrange=1) and
  715. (porddef(parraydef(p)^.elementtype.def)^.typ in [u32bit,s32bit])
  716. )
  717. or
  718. (
  719. (parraydef(p)^.lowrange=0) and
  720. (parraydef(p)^.highrange=3) and
  721. (porddef(parraydef(p)^.elementtype.def)^.typ in [u16bit,s16bit])
  722. )
  723. )
  724. )
  725. or
  726. (
  727. (
  728. (parraydef(p)^.elementtype.def^.deftype=floatdef) and
  729. (
  730. (parraydef(p)^.lowrange=0) and
  731. (parraydef(p)^.highrange=3) and
  732. (pfloatdef(parraydef(p)^.elementtype.def)^.typ=f16bit)
  733. ) or
  734. (
  735. (parraydef(p)^.lowrange=0) and
  736. (parraydef(p)^.highrange=1) and
  737. (pfloatdef(parraydef(p)^.elementtype.def)^.typ=s32real)
  738. )
  739. )
  740. )
  741. );
  742. end
  743. else
  744. begin
  745. is_mmx_able_array:=(p^.deftype=arraydef) and
  746. (
  747. (
  748. (parraydef(p)^.elementtype.def^.deftype=orddef) and
  749. (
  750. (
  751. (parraydef(p)^.lowrange=0) and
  752. (parraydef(p)^.highrange=1) and
  753. (porddef(parraydef(p)^.elementtype.def)^.typ in [u32bit,s32bit])
  754. )
  755. or
  756. (
  757. (parraydef(p)^.lowrange=0) and
  758. (parraydef(p)^.highrange=3) and
  759. (porddef(parraydef(p)^.elementtype.def)^.typ in [u16bit,s16bit])
  760. )
  761. or
  762. (
  763. (parraydef(p)^.lowrange=0) and
  764. (parraydef(p)^.highrange=7) and
  765. (porddef(parraydef(p)^.elementtype.def)^.typ in [u8bit,s8bit])
  766. )
  767. )
  768. )
  769. or
  770. (
  771. (parraydef(p)^.elementtype.def^.deftype=floatdef) and
  772. (
  773. (
  774. (parraydef(p)^.lowrange=0) and
  775. (parraydef(p)^.highrange=3) and
  776. (pfloatdef(parraydef(p)^.elementtype.def)^.typ=f32bit)
  777. )
  778. or
  779. (
  780. (parraydef(p)^.lowrange=0) and
  781. (parraydef(p)^.highrange=1) and
  782. (pfloatdef(parraydef(p)^.elementtype.def)^.typ=s32real)
  783. )
  784. )
  785. )
  786. );
  787. end;
  788. {$else SUPPORT_MMX}
  789. is_mmx_able_array:=false;
  790. {$endif SUPPORT_MMX}
  791. end;
  792. function is_equal(def1,def2 : pdef) : boolean;
  793. var
  794. b : boolean;
  795. hd : pdef;
  796. begin
  797. { both types must exists }
  798. if not (assigned(def1) and assigned(def2)) then
  799. begin
  800. is_equal:=false;
  801. exit;
  802. end;
  803. { be sure, that if there is a stringdef, that this is def1 }
  804. if def2^.deftype=stringdef then
  805. begin
  806. hd:=def1;
  807. def1:=def2;
  808. def2:=hd;
  809. end;
  810. b:=false;
  811. { both point to the same definition ? }
  812. if def1=def2 then
  813. b:=true
  814. else
  815. { pointer with an equal definition are equal }
  816. if (def1^.deftype=pointerdef) and (def2^.deftype=pointerdef) then
  817. begin
  818. { here a problem detected in tabsolutesym }
  819. { the types can be forward type !! }
  820. if assigned(def1^.typesym) and (ppointerdef(def1)^.pointertype.def^.deftype=forwarddef) then
  821. b:=(def1^.typesym=def2^.typesym)
  822. else
  823. b:=ppointerdef(def1)^.pointertype.def=ppointerdef(def2)^.pointertype.def;
  824. end
  825. else
  826. { ordinals are equal only when the ordinal type is equal }
  827. if (def1^.deftype=orddef) and (def2^.deftype=orddef) then
  828. begin
  829. case porddef(def1)^.typ of
  830. u8bit,u16bit,u32bit,
  831. s8bit,s16bit,s32bit:
  832. b:=((porddef(def1)^.typ=porddef(def2)^.typ) and
  833. (porddef(def1)^.low=porddef(def2)^.low) and
  834. (porddef(def1)^.high=porddef(def2)^.high));
  835. uvoid,uchar,
  836. bool8bit,bool16bit,bool32bit:
  837. b:=(porddef(def1)^.typ=porddef(def2)^.typ);
  838. end;
  839. end
  840. else
  841. if (def1^.deftype=floatdef) and (def2^.deftype=floatdef) then
  842. b:=pfloatdef(def1)^.typ=pfloatdef(def2)^.typ
  843. else
  844. { strings with the same length are equal }
  845. if (def1^.deftype=stringdef) and (def2^.deftype=stringdef) and
  846. (pstringdef(def1)^.string_typ=pstringdef(def2)^.string_typ) then
  847. begin
  848. b:=not(is_shortstring(def1)) or
  849. (pstringdef(def1)^.len=pstringdef(def2)^.len);
  850. end
  851. else
  852. if (def1^.deftype=formaldef) and (def2^.deftype=formaldef) then
  853. b:=true
  854. { file types with the same file element type are equal }
  855. { this is a problem for assign !! }
  856. { changed to allow if one is untyped }
  857. { all typed files are equal to the special }
  858. { typed file that has voiddef as elemnt type }
  859. { but must NOT match for text file !!! }
  860. else
  861. if (def1^.deftype=filedef) and (def2^.deftype=filedef) then
  862. b:=(pfiledef(def1)^.filetyp=pfiledef(def2)^.filetyp) and
  863. ((
  864. ((pfiledef(def1)^.typedfiletype.def=nil) and
  865. (pfiledef(def2)^.typedfiletype.def=nil)) or
  866. (
  867. (pfiledef(def1)^.typedfiletype.def<>nil) and
  868. (pfiledef(def2)^.typedfiletype.def<>nil) and
  869. is_equal(pfiledef(def1)^.typedfiletype.def,pfiledef(def2)^.typedfiletype.def)
  870. ) or
  871. ( (pfiledef(def1)^.typedfiletype.def=pdef(voiddef)) or
  872. (pfiledef(def2)^.typedfiletype.def=pdef(voiddef))
  873. )))
  874. { sets with the same element type are equal }
  875. else
  876. if (def1^.deftype=setdef) and (def2^.deftype=setdef) then
  877. begin
  878. if assigned(psetdef(def1)^.elementtype.def) and
  879. assigned(psetdef(def2)^.elementtype.def) then
  880. b:=(psetdef(def1)^.elementtype.def^.deftype=psetdef(def2)^.elementtype.def^.deftype)
  881. else
  882. b:=true;
  883. end
  884. else
  885. if (def1^.deftype=procvardef) and (def2^.deftype=procvardef) then
  886. begin
  887. { poassembler isn't important for compatibility }
  888. { if a method is assigned to a methodpointer }
  889. { is checked before }
  890. b:=(pprocvardef(def1)^.proctypeoption=pprocvardef(def2)^.proctypeoption) and
  891. (pprocvardef(def1)^.proccalloptions=pprocvardef(def2)^.proccalloptions) and
  892. ((pprocvardef(def1)^.procoptions * po_compatibility_options)=
  893. (pprocvardef(def2)^.procoptions * po_compatibility_options)) and
  894. is_equal(pprocvardef(def1)^.rettype.def,pprocvardef(def2)^.rettype.def) and
  895. equal_paras(pprocvardef(def1)^.para,pprocvardef(def2)^.para,cp_all);
  896. end
  897. else
  898. if (def1^.deftype=arraydef) and (def2^.deftype=arraydef) then
  899. begin
  900. if is_array_of_const(def1) or is_array_of_const(def2) then
  901. begin
  902. b:=(is_array_of_const(def1) and is_array_of_const(def2)) or
  903. (is_array_of_const(def1) and is_array_constructor(def2)) or
  904. (is_array_of_const(def2) and is_array_constructor(def1));
  905. end
  906. else
  907. if is_open_array(def1) or is_open_array(def2) then
  908. begin
  909. b:=is_equal(parraydef(def1)^.elementtype.def,parraydef(def2)^.elementtype.def);
  910. end
  911. else
  912. begin
  913. b:=not(m_tp in aktmodeswitches) and
  914. not(m_delphi in aktmodeswitches) and
  915. (parraydef(def1)^.lowrange=parraydef(def2)^.lowrange) and
  916. (parraydef(def1)^.highrange=parraydef(def2)^.highrange) and
  917. is_equal(parraydef(def1)^.elementtype.def,parraydef(def2)^.elementtype.def) and
  918. is_equal(parraydef(def1)^.rangetype.def,parraydef(def2)^.rangetype.def);
  919. end;
  920. end
  921. else
  922. if (def1^.deftype=classrefdef) and (def2^.deftype=classrefdef) then
  923. begin
  924. { similar to pointerdef: }
  925. if assigned(def1^.typesym) and (pclassrefdef(def1)^.pointertype.def^.deftype=forwarddef) then
  926. b:=(def1^.typesym=def2^.typesym)
  927. else
  928. b:=is_equal(pclassrefdef(def1)^.pointertype.def,pclassrefdef(def2)^.pointertype.def);
  929. end;
  930. is_equal:=b;
  931. end;
  932. function is_subequal(def1, def2: pdef): boolean;
  933. var
  934. basedef1,basedef2 : penumdef;
  935. Begin
  936. is_subequal := false;
  937. if assigned(def1) and assigned(def2) then
  938. Begin
  939. if (def1^.deftype = orddef) and (def2^.deftype = orddef) then
  940. Begin
  941. { see p.47 of Turbo Pascal 7.01 manual for the separation of types }
  942. { range checking for case statements is done with testrange }
  943. case porddef(def1)^.typ of
  944. u8bit,u16bit,u32bit,
  945. s8bit,s16bit,s32bit,s64bit,u64bit :
  946. is_subequal:=(porddef(def2)^.typ in [s64bit,u64bit,s32bit,u32bit,u8bit,s8bit,s16bit,u16bit]);
  947. bool8bit,bool16bit,bool32bit :
  948. is_subequal:=(porddef(def2)^.typ in [bool8bit,bool16bit,bool32bit]);
  949. uchar :
  950. is_subequal:=(porddef(def2)^.typ=uchar);
  951. end;
  952. end
  953. else
  954. Begin
  955. { I assume that both enumerations are equal when the first }
  956. { pointers are equal. }
  957. { I changed this to assume that the enums are equal }
  958. { if the basedefs are equal (FK) }
  959. if (def1^.deftype=enumdef) and (def2^.deftype=enumdef) then
  960. Begin
  961. { get both basedefs }
  962. basedef1:=penumdef(def1);
  963. while assigned(basedef1^.basedef) do
  964. basedef1:=basedef1^.basedef;
  965. basedef2:=penumdef(def2);
  966. while assigned(basedef2^.basedef) do
  967. basedef2:=basedef2^.basedef;
  968. is_subequal:=basedef1=basedef2;
  969. {
  970. if penumdef(def1)^.firstenum = penumdef(def2)^.firstenum then
  971. is_subequal := TRUE;
  972. }
  973. end;
  974. end;
  975. end; { endif assigned ... }
  976. end;
  977. function CheckTypes(def1,def2 : pdef) : boolean;
  978. var
  979. s1,s2 : string;
  980. begin
  981. if not is_equal(def1,def2) then
  982. begin
  983. { Crash prevention }
  984. if (not assigned(def1)) or (not assigned(def2)) then
  985. Message(type_e_mismatch)
  986. else
  987. begin
  988. s1:=def1^.typename;
  989. s2:=def2^.typename;
  990. if (s1<>'<unknown type>') and (s2<>'<unknown type>') then
  991. Message2(type_e_not_equal_types,def1^.typename,def2^.typename)
  992. else
  993. Message(type_e_mismatch);
  994. end;
  995. CheckTypes:=false;
  996. end
  997. else
  998. CheckTypes:=true;
  999. end;
  1000. end.
  1001. {
  1002. $Log$
  1003. Revision 1.9 2000-09-10 20:16:21 peter
  1004. * array of const isn't equal with array of <type> (merged)
  1005. Revision 1.8 2000/08/19 19:51:03 peter
  1006. * fixed bug with comparing constsym strings
  1007. Revision 1.7 2000/08/16 13:06:07 florian
  1008. + support of 64 bit integer constants
  1009. Revision 1.6 2000/08/13 13:07:18 peter
  1010. * equal_paras now also checks default parameter value
  1011. Revision 1.5 2000/08/12 06:49:22 florian
  1012. + case statement for int64/qword implemented
  1013. Revision 1.4 2000/08/08 19:26:41 peter
  1014. * equal_constsym() needed for default para
  1015. Revision 1.3 2000/07/13 12:08:28 michael
  1016. + patched to 1.1.0 with former 1.09patch from peter
  1017. Revision 1.2 2000/07/13 11:32:53 michael
  1018. + removed logs
  1019. }