types.pas 41 KB

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