types.pas 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  1. {
  2. $Id$
  3. Copyright (C) 1993-98 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;
  22. type
  23. tmmxtype = (mmxno,mmxu8bit,mmxs8bit,mmxu16bit,mmxs16bit,
  24. mmxu32bit,mmxs32bit,mmxfixed16,mmxsingle);
  25. const
  26. { true if we must never copy this parameter }
  27. never_copy_const_param : boolean = false;
  28. {*****************************************************************************
  29. Basic type functions
  30. *****************************************************************************}
  31. { returns true, if def defines an ordinal type }
  32. function is_ordinal(def : pdef) : boolean;
  33. { returns the min. value of the type }
  34. function get_min_value(def : pdef) : longint;
  35. { returns true, if def defines an ordinal type }
  36. function is_integer(def : pdef) : boolean;
  37. { true if p is a boolean }
  38. function is_boolean(def : pdef) : boolean;
  39. { true if p is a char }
  40. function is_char(def : pdef) : boolean;
  41. { true if p is a smallset def }
  42. function is_smallset(p : pdef) : boolean;
  43. { returns true, if def defines a signed data type (only for ordinal types) }
  44. function is_signed(def : pdef) : boolean;
  45. {*****************************************************************************
  46. Array helper functions
  47. *****************************************************************************}
  48. { true, if p points to a zero based (non special like open or
  49. dynamic array def, mainly this is used to see if the array
  50. is convertable to a pointer }
  51. function is_zero_based_array(p : pdef) : boolean;
  52. { true if p points to an open array def }
  53. function is_open_array(p : pdef) : boolean;
  54. { true, if p points to an array of const def }
  55. function is_array_constructor(p : pdef) : boolean;
  56. { true, if p points to a variant array }
  57. function is_variant_array(p : pdef) : boolean;
  58. { true, if p points to an array of const }
  59. function is_array_of_const(p : pdef) : boolean;
  60. { true, if p points any kind of special array }
  61. function is_special_array(p : pdef) : boolean;
  62. { true if p is a char array def }
  63. function is_chararray(p : pdef) : boolean;
  64. {*****************************************************************************
  65. String helper functions
  66. *****************************************************************************}
  67. { true if p points to an open string def }
  68. function is_open_string(p : pdef) : boolean;
  69. { true if p is an ansi string def }
  70. function is_ansistring(p : pdef) : boolean;
  71. { true if p is a long string def }
  72. function is_longstring(p : pdef) : boolean;
  73. { true if p is a wide string def }
  74. function is_widestring(p : pdef) : boolean;
  75. { true if p is a short string def }
  76. function is_shortstring(p : pdef) : boolean;
  77. { true if p is a pchar def }
  78. function is_pchar(p : pdef) : boolean;
  79. { returns true, if def uses FPU }
  80. function is_fpu(def : pdef) : boolean;
  81. { true if the return value is in EAX }
  82. function ret_in_acc(def : pdef) : boolean;
  83. { true if uses a parameter as return value }
  84. function ret_in_param(def : pdef) : boolean;
  85. { true, if def is a 64 bit int type }
  86. function is_64bitint(def : pdef) : boolean;
  87. function push_high_param(def : pdef) : boolean;
  88. { true if a parameter is too large to copy and only the address is pushed }
  89. function push_addr_param(def : pdef) : boolean;
  90. { true, if def1 and def2 are semantical the same }
  91. function is_equal(def1,def2 : pdef) : boolean;
  92. { checks for type compatibility (subgroups of type) }
  93. { used for case statements... probably missing stuff }
  94. { to use on other types }
  95. function is_subequal(def1, def2: pdef): boolean;
  96. { same as is_equal, but with error message if failed }
  97. function CheckTypes(def1,def2 : pdef) : boolean;
  98. { true, if two parameter lists are equal }
  99. { if value_equal_const is true, call by value }
  100. { and call by const parameter are assumed as }
  101. { equal }
  102. function equal_paras(def1,def2 : pdefcoll;value_equal_const : boolean) : boolean;
  103. { true if a function can be assigned to a procvar }
  104. function proc_to_procvar_equal(def1,def2 : pabstractprocdef) : boolean;
  105. { if l isn't in the range of def a range check error is generated }
  106. procedure testrange(def : pdef;l : longint);
  107. { returns the range of def }
  108. procedure getrange(def : pdef;var l : longint;var h : longint);
  109. { some type helper routines for MMX support }
  110. function is_mmx_able_array(p : pdef) : boolean;
  111. { returns the mmx type }
  112. function mmx_type(p : pdef) : tmmxtype;
  113. { should we try to unget a temp of this type
  114. or is it done by temptoremove list (PM) }
  115. function ungettempoftype(p : pdef) : boolean;
  116. implementation
  117. uses
  118. strings,
  119. globtype,globals,verbose;
  120. function equal_paras(def1,def2 : pdefcoll;value_equal_const : boolean) : boolean;
  121. begin
  122. while (assigned(def1)) and (assigned(def2)) do
  123. begin
  124. if value_equal_const then
  125. begin
  126. if not(is_equal(def1^.data,def2^.data)) or
  127. ((def1^.paratyp<>def2^.paratyp) and
  128. ((def1^.paratyp=vs_var) or
  129. (def1^.paratyp=vs_var)
  130. )
  131. ) then
  132. begin
  133. equal_paras:=false;
  134. exit;
  135. end;
  136. end
  137. else
  138. begin
  139. if not(is_equal(def1^.data,def2^.data)) or
  140. (def1^.paratyp<>def2^.paratyp) then
  141. begin
  142. equal_paras:=false;
  143. exit;
  144. end;
  145. end;
  146. def1:=def1^.next;
  147. def2:=def2^.next;
  148. end;
  149. if (def1=nil) and (def2=nil) then
  150. equal_paras:=true
  151. else
  152. equal_paras:=false;
  153. end;
  154. { true if a function can be assigned to a procvar }
  155. function proc_to_procvar_equal(def1,def2 : pabstractprocdef) : boolean;
  156. begin
  157. if is_equal(def1^.retdef,def2^.retdef) and
  158. equal_paras(def1^.para1,def2^.para1,false) and
  159. ((def1^.options and po_compatibility_options)=
  160. (def2^.options and po_compatibility_options)) then
  161. proc_to_procvar_equal:=true
  162. else
  163. proc_to_procvar_equal:=false;
  164. end;
  165. { returns true, if def uses FPU }
  166. function is_fpu(def : pdef) : boolean;
  167. begin
  168. is_fpu:=(def^.deftype=floatdef) and (pfloatdef(def)^.typ<>f32bit);
  169. end;
  170. { true if p is an ordinal }
  171. function is_ordinal(def : pdef) : boolean;
  172. var
  173. dt : tbasetype;
  174. begin
  175. case def^.deftype of
  176. orddef : begin
  177. dt:=porddef(def)^.typ;
  178. is_ordinal:=dt in [uchar,u8bit,u16bit,u32bit,u64bit,s8bit,s16bit,s32bit,
  179. s64bitint,bool8bit,bool16bit,bool32bit];
  180. end;
  181. enumdef : is_ordinal:=true;
  182. else
  183. is_ordinal:=false;
  184. end;
  185. end;
  186. { returns the min. value of the type }
  187. function get_min_value(def : pdef) : longint;
  188. begin
  189. case def^.deftype of
  190. orddef:
  191. get_min_value:=porddef(def)^.low;
  192. enumdef:
  193. get_min_value:=penumdef(def)^.min;
  194. else
  195. get_min_value:=0;
  196. end;
  197. end;
  198. { true if p is an integer }
  199. function is_integer(def : pdef) : boolean;
  200. begin
  201. is_integer:=(def^.deftype=orddef) and
  202. (porddef(def)^.typ in [uauto,u8bit,u16bit,u32bit,s8bit,s16bit,s32bit]);
  203. end;
  204. { true if p is a boolean }
  205. function is_boolean(def : pdef) : boolean;
  206. begin
  207. is_boolean:=(def^.deftype=orddef) and
  208. (porddef(def)^.typ in [bool8bit,bool16bit,bool32bit]);
  209. end;
  210. { true if p is a char }
  211. function is_char(def : pdef) : boolean;
  212. begin
  213. is_char:=(def^.deftype=orddef) and
  214. (porddef(def)^.typ=uchar);
  215. end;
  216. { true if p is signed (integer) }
  217. function is_signed(def : pdef) : boolean;
  218. var
  219. dt : tbasetype;
  220. begin
  221. case def^.deftype of
  222. orddef : begin
  223. dt:=porddef(def)^.typ;
  224. is_signed:=(dt in [s8bit,s16bit,s32bit]);
  225. end;
  226. enumdef : is_signed:=false;
  227. else
  228. is_signed:=false;
  229. end;
  230. end;
  231. { true, if p points to an open array def }
  232. function is_open_string(p : pdef) : boolean;
  233. begin
  234. is_open_string:=(p^.deftype=stringdef) and
  235. (pstringdef(p)^.string_typ=st_shortstring) and
  236. (pstringdef(p)^.len=0);
  237. end;
  238. { true, if p points to a zero based array def }
  239. function is_zero_based_array(p : pdef) : boolean;
  240. begin
  241. is_zero_based_array:=(p^.deftype=arraydef) and
  242. (parraydef(p)^.lowrange=0) and
  243. not(is_special_array(p));
  244. end;
  245. { true, if p points to an open array def }
  246. function is_open_array(p : pdef) : boolean;
  247. begin
  248. is_open_array:=(p^.deftype=arraydef) and
  249. (parraydef(p)^.lowrange=0) and
  250. (parraydef(p)^.highrange=-1) and
  251. not(parraydef(p)^.IsConstructor) and
  252. not(parraydef(p)^.IsVariant) and
  253. not(parraydef(p)^.IsArrayOfConst);
  254. end;
  255. { true, if p points to an array of const def }
  256. function is_array_constructor(p : pdef) : boolean;
  257. begin
  258. is_array_constructor:=(p^.deftype=arraydef) and
  259. (parraydef(p)^.IsConstructor);
  260. end;
  261. { true, if p points to a variant array }
  262. function is_variant_array(p : pdef) : boolean;
  263. begin
  264. is_variant_array:=(p^.deftype=arraydef) and
  265. (parraydef(p)^.IsVariant);
  266. end;
  267. { true, if p points to an array of const }
  268. function is_array_of_const(p : pdef) : boolean;
  269. begin
  270. is_array_of_const:=(p^.deftype=arraydef) and
  271. (parraydef(p)^.IsArrayOfConst);
  272. end;
  273. { true, if p points to a special array }
  274. function is_special_array(p : pdef) : boolean;
  275. begin
  276. is_special_array:=(p^.deftype=arraydef) and
  277. ((parraydef(p)^.IsVariant) or
  278. (parraydef(p)^.IsArrayOfConst) or
  279. (parraydef(p)^.IsConstructor) or
  280. is_open_array(p)
  281. );
  282. end;
  283. { true if p is an ansi string def }
  284. function is_ansistring(p : pdef) : boolean;
  285. begin
  286. is_ansistring:=(p^.deftype=stringdef) and
  287. (pstringdef(p)^.string_typ=st_ansistring);
  288. end;
  289. { true if p is an long string def }
  290. function is_longstring(p : pdef) : boolean;
  291. begin
  292. is_longstring:=(p^.deftype=stringdef) and
  293. (pstringdef(p)^.string_typ=st_longstring);
  294. end;
  295. { true if p is an wide string def }
  296. function is_widestring(p : pdef) : boolean;
  297. begin
  298. is_widestring:=(p^.deftype=stringdef) and
  299. (pstringdef(p)^.string_typ=st_widestring);
  300. end;
  301. { true if p is an short string def }
  302. function is_shortstring(p : pdef) : boolean;
  303. begin
  304. is_shortstring:=(p^.deftype=stringdef) and
  305. (pstringdef(p)^.string_typ=st_shortstring);
  306. end;
  307. { true if p is a char array def }
  308. function is_chararray(p : pdef) : boolean;
  309. begin
  310. is_chararray:=(p^.deftype=arraydef) and
  311. is_equal(parraydef(p)^.definition,cchardef) and
  312. not(is_special_array(p));
  313. end;
  314. { true if p is a pchar def }
  315. function is_pchar(p : pdef) : boolean;
  316. begin
  317. is_pchar:=(p^.deftype=pointerdef) and
  318. is_equal(Ppointerdef(p)^.definition,cchardef);
  319. end;
  320. { true if p is a smallset def }
  321. function is_smallset(p : pdef) : boolean;
  322. begin
  323. is_smallset:=(p^.deftype=setdef) and
  324. (psetdef(p)^.settype=smallset);
  325. end;
  326. { true if the return value is in accumulator (EAX for i386), D0 for 68k }
  327. function ret_in_acc(def : pdef) : boolean;
  328. begin
  329. ret_in_acc:=(def^.deftype in [orddef,pointerdef,enumdef,classrefdef]) or
  330. ((def^.deftype=stringdef) and (pstringdef(def)^.string_typ in [st_ansistring,st_widestring])) or
  331. ((def^.deftype=procvardef) and ((pprocvardef(def)^.options and pomethodpointer)=0)) or
  332. ((def^.deftype=objectdef) and pobjectdef(def)^.isclass) or
  333. ((def^.deftype=setdef) and (psetdef(def)^.settype=smallset)) or
  334. ((def^.deftype=floatdef) and (pfloatdef(def)^.typ=f32bit));
  335. end;
  336. { true, if def is a 64 bit int type }
  337. function is_64bitint(def : pdef) : boolean;
  338. begin
  339. is_64bitint:=(def^.deftype=orddef) and (porddef(def)^.typ in [u64bit,s64bitint])
  340. end;
  341. { true if uses a parameter as return value }
  342. function ret_in_param(def : pdef) : boolean;
  343. begin
  344. ret_in_param:=(def^.deftype in [arraydef,recorddef]) or
  345. ((def^.deftype=stringdef) and (pstringdef(def)^.string_typ in [st_shortstring,st_longstring])) or
  346. ((def^.deftype=procvardef) and ((pprocvardef(def)^.options and pomethodpointer)<>0)) or
  347. ((def^.deftype=objectdef) and not(pobjectdef(def)^.isclass)) or
  348. ((def^.deftype=setdef) and (psetdef(def)^.settype<>smallset));
  349. end;
  350. function push_high_param(def : pdef) : boolean;
  351. begin
  352. push_high_param:=is_open_array(def) or is_open_string(def) or
  353. is_array_of_const(def);
  354. end;
  355. { true if a parameter is too large to copy and only the address is pushed }
  356. function push_addr_param(def : pdef) : boolean;
  357. begin
  358. push_addr_param:=never_copy_const_param or
  359. (def^.deftype = formaldef) or
  360. { copy directly small records or arrays unless array of const ! PM }
  361. ((def^.deftype in [arraydef,recorddef]) and
  362. ((def^.size>4) or
  363. ((def^.deftype=arraydef) and
  364. (parraydef(def)^.IsConstructor or
  365. parraydef(def)^.isArrayOfConst or
  366. is_open_array(def)
  367. )
  368. )
  369. )
  370. ) or
  371. ((def^.deftype=objectdef) and not(pobjectdef(def)^.isclass)) or
  372. ((def^.deftype=stringdef) and (pstringdef(def)^.string_typ in [st_shortstring,st_longstring])) or
  373. ((def^.deftype=procvardef) and ((pprocvardef(def)^.options and pomethodpointer)<>0)) or
  374. ((def^.deftype=setdef) and (psetdef(def)^.settype<>smallset));
  375. end;
  376. { test if l is in the range of def, outputs error if out of range }
  377. procedure testrange(def : pdef;l : longint);
  378. var
  379. lv,hv: longint;
  380. begin
  381. getrange(def,lv,hv);
  382. if (def^.deftype=orddef) and
  383. (porddef(def)^.typ=u32bit) then
  384. begin
  385. if lv<=hv then
  386. begin
  387. if (l<lv) or (l>hv) then
  388. begin
  389. if (cs_check_range in aktlocalswitches) then
  390. Message(parser_e_range_check_error)
  391. else
  392. Message(parser_w_range_check_error);
  393. end;
  394. end
  395. else
  396. { this happens with the wrap around problem }
  397. { if lv is positive and hv is over $7ffffff }
  398. { so it seems negative }
  399. begin
  400. if ((l>=0) and (l<lv)) or
  401. ((l<0) and (l>hv)) then
  402. begin
  403. if (cs_check_range in aktlocalswitches) then
  404. Message(parser_e_range_check_error)
  405. else
  406. Message(parser_w_range_check_error);
  407. end;
  408. end;
  409. end
  410. else if (l<lv) or (l>hv) then
  411. begin
  412. if (cs_check_range in aktlocalswitches) then
  413. Message(parser_e_range_check_error)
  414. else
  415. Message(parser_w_range_check_error);
  416. end;
  417. end;
  418. { return the range from def in l and h }
  419. procedure getrange(def : pdef;var l : longint;var h : longint);
  420. begin
  421. case def^.deftype of
  422. orddef :
  423. begin
  424. l:=porddef(def)^.low;
  425. h:=porddef(def)^.high;
  426. end;
  427. enumdef :
  428. begin
  429. l:=penumdef(def)^.min;
  430. h:=penumdef(def)^.max;
  431. end;
  432. arraydef :
  433. begin
  434. l:=parraydef(def)^.lowrange;
  435. h:=parraydef(def)^.highrange;
  436. end;
  437. else
  438. internalerror(987);
  439. end;
  440. end;
  441. { should we try to unget a temp of this type
  442. or is it done by temptoremove list (PM) }
  443. function ungettempoftype(p : pdef) : boolean;
  444. begin
  445. ungettempoftype:=true;
  446. if assigned(p) and (p^.deftype=stringdef) and
  447. (pstringdef(p)^.string_typ in [st_ansistring,st_widestring]) then
  448. ungettempoftype:=false;
  449. end;
  450. function mmx_type(p : pdef) : tmmxtype;
  451. begin
  452. mmx_type:=mmxno;
  453. if is_mmx_able_array(p) then
  454. begin
  455. if parraydef(p)^.definition^.deftype=floatdef then
  456. case pfloatdef(parraydef(p)^.definition)^.typ of
  457. s32real:
  458. mmx_type:=mmxsingle;
  459. f16bit:
  460. mmx_type:=mmxfixed16
  461. end
  462. else
  463. case porddef(parraydef(p)^.definition)^.typ of
  464. u8bit:
  465. mmx_type:=mmxu8bit;
  466. s8bit:
  467. mmx_type:=mmxs8bit;
  468. u16bit:
  469. mmx_type:=mmxu16bit;
  470. s16bit:
  471. mmx_type:=mmxs16bit;
  472. u32bit:
  473. mmx_type:=mmxu32bit;
  474. s32bit:
  475. mmx_type:=mmxs32bit;
  476. end;
  477. end;
  478. end;
  479. function is_mmx_able_array(p : pdef) : boolean;
  480. begin
  481. {$ifdef SUPPORT_MMX}
  482. if (cs_mmx_saturation in aktlocalswitches) then
  483. begin
  484. is_mmx_able_array:=(p^.deftype=arraydef) and
  485. not(is_special_array(p)) and
  486. (
  487. (
  488. (parraydef(p)^.definition^.deftype=orddef) and
  489. (
  490. (
  491. (parraydef(p)^.lowrange=0) and
  492. (parraydef(p)^.highrange=1) and
  493. (porddef(parraydef(p)^.definition)^.typ in [u32bit,s32bit])
  494. )
  495. or
  496. (
  497. (parraydef(p)^.lowrange=0) and
  498. (parraydef(p)^.highrange=3) and
  499. (porddef(parraydef(p)^.definition)^.typ in [u16bit,s16bit])
  500. )
  501. )
  502. )
  503. or
  504. (
  505. (
  506. (parraydef(p)^.definition^.deftype=floatdef) and
  507. (
  508. (parraydef(p)^.lowrange=0) and
  509. (parraydef(p)^.highrange=3) and
  510. (pfloatdef(parraydef(p)^.definition)^.typ=f16bit)
  511. ) or
  512. (
  513. (parraydef(p)^.lowrange=0) and
  514. (parraydef(p)^.highrange=1) and
  515. (pfloatdef(parraydef(p)^.definition)^.typ=s32real)
  516. )
  517. )
  518. )
  519. );
  520. end
  521. else
  522. begin
  523. is_mmx_able_array:=(p^.deftype=arraydef) and
  524. (
  525. (
  526. (parraydef(p)^.definition^.deftype=orddef) and
  527. (
  528. (
  529. (parraydef(p)^.lowrange=0) and
  530. (parraydef(p)^.highrange=1) and
  531. (porddef(parraydef(p)^.definition)^.typ in [u32bit,s32bit])
  532. )
  533. or
  534. (
  535. (parraydef(p)^.lowrange=0) and
  536. (parraydef(p)^.highrange=3) and
  537. (porddef(parraydef(p)^.definition)^.typ in [u16bit,s16bit])
  538. )
  539. or
  540. (
  541. (parraydef(p)^.lowrange=0) and
  542. (parraydef(p)^.highrange=7) and
  543. (porddef(parraydef(p)^.definition)^.typ in [u8bit,s8bit])
  544. )
  545. )
  546. )
  547. or
  548. (
  549. (parraydef(p)^.definition^.deftype=floatdef) and
  550. (
  551. (
  552. (parraydef(p)^.lowrange=0) and
  553. (parraydef(p)^.highrange=3) and
  554. (pfloatdef(parraydef(p)^.definition)^.typ=f32bit)
  555. )
  556. or
  557. (
  558. (parraydef(p)^.lowrange=0) and
  559. (parraydef(p)^.highrange=1) and
  560. (pfloatdef(parraydef(p)^.definition)^.typ=s32real)
  561. )
  562. )
  563. )
  564. );
  565. end;
  566. {$else SUPPORT_MMX}
  567. is_mmx_able_array:=false;
  568. {$endif SUPPORT_MMX}
  569. end;
  570. function is_equal(def1,def2 : pdef) : boolean;
  571. const
  572. procvarmask = not(poassembler or pomethodpointer or povirtualmethod or
  573. pooverridingmethod or pomsgint or pomsgstr);
  574. var
  575. b : boolean;
  576. hd : pdef;
  577. hp1,hp2 : pdefcoll;
  578. begin
  579. { both types must exists }
  580. if not (assigned(def1) and assigned(def2)) then
  581. begin
  582. is_equal:=false;
  583. exit;
  584. end;
  585. { be sure, that if there is a stringdef, that this is def1 }
  586. if def2^.deftype=stringdef then
  587. begin
  588. hd:=def1;
  589. def1:=def2;
  590. def2:=hd;
  591. end;
  592. b:=false;
  593. { both point to the same definition ? }
  594. if def1=def2 then
  595. b:=true
  596. else
  597. { pointer with an equal definition are equal }
  598. if (def1^.deftype=pointerdef) and (def2^.deftype=pointerdef) then
  599. begin
  600. { here a problem detected in tabsolutesym }
  601. { the types can be forward type !! }
  602. if assigned(def1^.sym) and ((def1^.sym^.properties and sp_forwarddef)<>0) then
  603. b:=(def1^.sym=def2^.sym)
  604. else
  605. b:=ppointerdef(def1)^.definition=ppointerdef(def2)^.definition;
  606. end
  607. else
  608. { ordinals are equal only when the ordinal type is equal }
  609. if (def1^.deftype=orddef) and (def2^.deftype=orddef) then
  610. begin
  611. case porddef(def1)^.typ of
  612. u8bit,u16bit,u32bit,
  613. s8bit,s16bit,s32bit:
  614. b:=((porddef(def1)^.typ=porddef(def2)^.typ) and
  615. (porddef(def1)^.low=porddef(def2)^.low) and
  616. (porddef(def1)^.high=porddef(def2)^.high));
  617. uvoid,uchar,
  618. bool8bit,bool16bit,bool32bit:
  619. b:=(porddef(def1)^.typ=porddef(def2)^.typ);
  620. end;
  621. end
  622. else
  623. if (def1^.deftype=floatdef) and (def2^.deftype=floatdef) then
  624. b:=pfloatdef(def1)^.typ=pfloatdef(def2)^.typ
  625. else
  626. { strings with the same length are equal }
  627. if (def1^.deftype=stringdef) and (def2^.deftype=stringdef) and
  628. (pstringdef(def1)^.string_typ=pstringdef(def2)^.string_typ) then
  629. begin
  630. b:=not(is_shortstring(def1)) or
  631. (pstringdef(def1)^.len=pstringdef(def2)^.len);
  632. end
  633. else
  634. if (def1^.deftype=formaldef) and (def2^.deftype=formaldef) then
  635. b:=true
  636. { file types with the same file element type are equal }
  637. { this is a problem for assign !! }
  638. { changed to allow if one is untyped }
  639. { all typed files are equal to the special }
  640. { typed file that has voiddef as elemnt type }
  641. { but must NOT match for text file !!! }
  642. else
  643. if (def1^.deftype=filedef) and (def2^.deftype=filedef) then
  644. b:=(pfiledef(def1)^.filetype=pfiledef(def2)^.filetype) and
  645. ((
  646. ((pfiledef(def1)^.typed_as=nil) and
  647. (pfiledef(def2)^.typed_as=nil)) or
  648. (
  649. (pfiledef(def1)^.typed_as<>nil) and
  650. (pfiledef(def2)^.typed_as<>nil) and
  651. is_equal(pfiledef(def1)^.typed_as,pfiledef(def2)^.typed_as)
  652. ) or
  653. ( (pfiledef(def1)^.typed_as=pdef(voiddef)) or
  654. (pfiledef(def2)^.typed_as=pdef(voiddef))
  655. )))
  656. { sets with the same element type are equal }
  657. else
  658. if (def1^.deftype=setdef) and (def2^.deftype=setdef) then
  659. begin
  660. if assigned(psetdef(def1)^.setof) and
  661. assigned(psetdef(def2)^.setof) then
  662. b:=(psetdef(def1)^.setof^.deftype=psetdef(def2)^.setof^.deftype)
  663. else
  664. b:=true;
  665. end
  666. else
  667. if (def1^.deftype=procvardef) and (def2^.deftype=procvardef) then
  668. begin
  669. { poassembler isn't important for compatibility }
  670. { if a method is assigned to a methodpointer }
  671. { is checked before }
  672. b:=((pprocvardef(def1)^.options and procvarmask)=(pprocvardef(def2)^.options and procvarmask)) and
  673. is_equal(pprocvardef(def1)^.retdef,pprocvardef(def2)^.retdef);
  674. { now evalute the parameters }
  675. if b then
  676. begin
  677. hp1:=pprocvardef(def1)^.para1;
  678. hp2:=pprocvardef(def1)^.para1;
  679. while assigned(hp1) and assigned(hp2) do
  680. begin
  681. if not(is_equal(hp1^.data,hp2^.data)) or
  682. not(hp1^.paratyp=hp2^.paratyp) then
  683. begin
  684. b:=false;
  685. break;
  686. end;
  687. hp1:=hp1^.next;
  688. hp2:=hp2^.next;
  689. end;
  690. b:=(hp1=nil) and (hp2=nil);
  691. end;
  692. end
  693. else
  694. if (def1^.deftype=arraydef) and (def2^.deftype=arraydef) and
  695. (is_open_array(def1) or is_open_array(def2) or
  696. is_array_of_const(def1) or is_array_of_const(def2)) then
  697. begin
  698. if parraydef(def1)^.IsArrayOfConst or parraydef(def2)^.IsArrayOfConst then
  699. b:=true
  700. else
  701. b:=is_equal(parraydef(def1)^.definition,parraydef(def2)^.definition);
  702. end
  703. else
  704. if (def1^.deftype=classrefdef) and (def2^.deftype=classrefdef) then
  705. begin
  706. { similar to pointerdef: }
  707. if assigned(def1^.sym) and ((def1^.sym^.properties and sp_forwarddef)<>0) then
  708. b:=(def1^.sym=def2^.sym)
  709. else
  710. b:=is_equal(pclassrefdef(def1)^.definition,pclassrefdef(def2)^.definition);
  711. end;
  712. is_equal:=b;
  713. end;
  714. function is_subequal(def1, def2: pdef): boolean;
  715. Begin
  716. if assigned(def1) and assigned(def2) then
  717. Begin
  718. is_subequal := FALSE;
  719. if (def1^.deftype = orddef) and (def2^.deftype = orddef) then
  720. Begin
  721. { see p.47 of Turbo Pascal 7.01 manual for the separation of types }
  722. { range checking for case statements is done with testrange }
  723. case porddef(def1)^.typ of
  724. u8bit,u16bit,u32bit,
  725. s8bit,s16bit,s32bit :
  726. is_subequal:=(porddef(def2)^.typ in [s32bit,u32bit,u8bit,s8bit,s16bit,u16bit]);
  727. bool8bit,bool16bit,bool32bit :
  728. is_subequal:=(porddef(def2)^.typ in [bool8bit,bool16bit,bool32bit]);
  729. uchar :
  730. is_subequal:=(porddef(def2)^.typ=uchar);
  731. end;
  732. end
  733. else
  734. Begin
  735. { I assume that both enumerations are equal when the first }
  736. { pointers are equal. }
  737. if (def1^.deftype = enumdef) and (def2^.deftype =enumdef) then
  738. Begin
  739. if penumdef(def1)^.firstenum = penumdef(def2)^.firstenum then
  740. is_subequal := TRUE;
  741. end;
  742. end;
  743. end; { endif assigned ... }
  744. end;
  745. function CheckTypes(def1,def2 : pdef) : boolean;
  746. var
  747. s1,s2 : string;
  748. begin
  749. if not is_equal(def1,def2) then
  750. begin
  751. { Crash prevention }
  752. if (not assigned(def1)) or (not assigned(def2)) then
  753. Message(type_e_mismatch)
  754. else
  755. begin
  756. s1:=def1^.typename;
  757. s2:=def2^.typename;
  758. if (s1<>'<unknown type>') and (s2<>'<unknown type>') then
  759. Message2(type_e_not_equal_types,def1^.typename,def2^.typename)
  760. else
  761. Message(type_e_mismatch);
  762. end;
  763. CheckTypes:=false;
  764. end
  765. else
  766. CheckTypes:=true;
  767. end;
  768. end.
  769. {
  770. $Log$
  771. Revision 1.65 1999-05-23 18:42:23 florian
  772. * better error recovering in typed constants
  773. * some problems with arrays of const fixed, some problems
  774. due my previous
  775. - the location type of array constructor is now LOC_MEM
  776. - the pushing of high fixed
  777. - parameter copying fixed
  778. - zero temp. allocation removed
  779. * small problem in the assembler writers fixed:
  780. ref to nil wasn't written correctly
  781. Revision 1.64 1999/05/19 20:55:08 florian
  782. * fix of my previous commit
  783. Revision 1.63 1999/05/19 20:40:15 florian
  784. * fixed a couple of array related bugs:
  785. - var a : array[0..1] of char; p : pchar; p:=a+123; works now
  786. - open arrays with an odd size doesn't work: movsb wasn't generated
  787. - introduced some new array type helper routines (is_special_array) etc.
  788. - made the array type checking in isconvertable more strict, often
  789. open array can be used where is wasn't allowed etc...
  790. Revision 1.62 1999/05/19 16:48:29 florian
  791. * tdef.typename: returns a now a proper type name for the most types
  792. Revision 1.61 1999/05/19 10:31:56 florian
  793. * two bugs reported by Romio (bugs 13) are fixed:
  794. - empty array constructors are now handled correctly (e.g. for sysutils.format)
  795. - comparsion of ansistrings was sometimes coded wrong
  796. Revision 1.60 1999/05/18 14:16:01 peter
  797. * containsself fixes
  798. * checktypes()
  799. Revision 1.59 1999/05/18 09:52:24 peter
  800. * procedure of object and addrn fixes
  801. Revision 1.58 1999/04/19 09:29:51 pierre
  802. + ungettempoftype(pdef) boolean function
  803. returns true (can call ungetiftemp )
  804. unless the temp should be "unget" with temptoremove
  805. (currently ansistring or widestring !)
  806. Revision 1.57 1999/04/14 09:15:08 peter
  807. * first things to store the symbol/def number in the ppu
  808. Revision 1.56 1999/03/24 23:17:42 peter
  809. * fixed bugs 212,222,225,227,229,231,233
  810. Revision 1.55 1999/03/09 11:45:42 pierre
  811. * small arrays and records (size <=4) are copied directly
  812. Revision 1.54 1999/03/02 22:52:20 peter
  813. * fixed char array, which can start with all possible values
  814. Revision 1.53 1999/02/25 21:02:57 peter
  815. * ag386bin updates
  816. + coff writer
  817. Revision 1.52 1999/02/24 09:51:44 florian
  818. * wrong warning fixed, if a non-virtual method was hidden by a virtual
  819. method (repoerted by Matthias Koeppe)
  820. Revision 1.51 1999/02/22 23:33:31 florian
  821. + message directive for integers added
  822. Revision 1.50 1999/02/22 20:13:42 florian
  823. + first implementation of message keyword
  824. Revision 1.49 1999/02/16 00:45:30 peter
  825. * fixed crashes by forgotten strpnew() for init_symbol
  826. Revision 1.48 1999/02/09 23:03:08 florian
  827. * check for duplicate field names in inherited classes/objects
  828. * bug with self from the mailing list solved (the problem
  829. was that classes were sometimes pushed wrong)
  830. Revision 1.47 1999/01/27 00:14:01 florian
  831. * "procedure of object"-stuff fixed
  832. Revision 1.46 1999/01/21 22:10:54 peter
  833. * fixed array of const
  834. * generic platform independent high() support
  835. Revision 1.45 1999/01/20 12:34:22 peter
  836. * fixed typed file read/write
  837. Revision 1.44 1999/01/15 11:33:03 pierre
  838. * bug in mmx code removed
  839. Revision 1.43 1998/12/30 13:41:20 peter
  840. * released valuepara
  841. Revision 1.42 1998/12/11 00:04:03 peter
  842. + globtype,tokens,version unit splitted from globals
  843. Revision 1.41 1998/12/10 09:47:33 florian
  844. + basic operations with int64/qord (compiler with -dint64)
  845. + rtti of enumerations extended: names are now written
  846. Revision 1.40 1998/12/04 10:18:14 florian
  847. * some stuff for procedures of object added
  848. * bug with overridden virtual constructors fixed (reported by Italo Gomes)
  849. Revision 1.39 1998/11/27 14:50:55 peter
  850. + open strings, $P switch support
  851. Revision 1.38 1998/11/18 15:44:24 peter
  852. * VALUEPARA for tp7 compatible value parameters
  853. Revision 1.37 1998/11/13 10:15:50 peter
  854. * fixed ptr() with constants
  855. Revision 1.36 1998/11/10 10:09:21 peter
  856. * va_list -> array of const
  857. Revision 1.35 1998/10/19 08:55:13 pierre
  858. * wrong stabs info corrected once again !!
  859. + variable vmt offset with vmt field only if required
  860. implemented now !!!
  861. Revision 1.34 1998/10/12 09:50:06 florian
  862. + support of <procedure var type>:=<pointer> in delphi mode added
  863. Revision 1.33 1998/10/06 20:43:30 peter
  864. * fixed set of bugs. like set of false..true set of #1..#255 and
  865. set of #1..true which was allowed
  866. Revision 1.32 1998/10/05 21:33:35 peter
  867. * fixed 161,165,166,167,168
  868. Revision 1.31 1998/09/23 09:58:56 peter
  869. * first working array of const things
  870. Revision 1.30 1998/09/22 15:40:58 peter
  871. * some extra ifdef GDB
  872. Revision 1.29 1998/09/16 12:37:31 michael
  873. Added FPC_ prefix to abstracterror
  874. Revision 1.28 1998/09/09 16:44:23 florian
  875. * I hope, the case bug is fixed now
  876. Revision 1.27 1998/09/07 17:37:07 florian
  877. * first fixes for published properties
  878. Revision 1.26 1998/09/04 12:24:31 florian
  879. * bug0159 fixed
  880. Revision 1.25 1998/09/04 09:06:36 florian
  881. * bug0132 fixed
  882. Revision 1.24 1998/09/04 08:36:49 peter
  883. * fixed boolean:=integer which is not explicit
  884. Revision 1.23 1998/09/01 17:39:55 peter
  885. + internal constant functions
  886. Revision 1.22 1998/09/01 12:53:28 peter
  887. + aktpackenum
  888. Revision 1.21 1998/08/19 00:42:45 peter
  889. + subrange types for enums
  890. + checking for bounds type with ranges
  891. Revision 1.20 1998/08/18 14:17:14 pierre
  892. * bug about assigning the return value of a function to
  893. a procvar fixed : warning
  894. assigning a proc to a procvar need @ in FPC mode !!
  895. * missing file/line info restored
  896. Revision 1.19 1998/08/18 09:24:48 pierre
  897. * small warning position bug fixed
  898. * support_mmx switches splitting was missing
  899. * rhide error and warning output corrected
  900. Revision 1.18 1998/08/14 18:18:49 peter
  901. + dynamic set contruction
  902. * smallsets are now working (always longint size)
  903. Revision 1.17 1998/08/05 16:00:17 florian
  904. * some fixes for ansi strings
  905. Revision 1.16 1998/07/20 23:35:50 michael
  906. Const ansistrings are not copied.
  907. Revision 1.15 1998/07/18 22:54:32 florian
  908. * some ansi/wide/longstring support fixed:
  909. o parameter passing
  910. o returning as result from functions
  911. Revision 1.14 1998/06/12 14:50:50 peter
  912. * removed the tree dependency to types.pas
  913. * long_fil.pas support (not fully tested yet)
  914. Revision 1.13 1998/06/03 22:49:07 peter
  915. + wordbool,longbool
  916. * rename bis,von -> high,low
  917. * moved some systemunit loading/creating to psystem.pas
  918. Revision 1.12 1998/05/12 10:47:00 peter
  919. * moved printstatus to verb_def
  920. + V_Normal which is between V_Error and V_Warning and doesn't have a
  921. prefix like error: warning: and is included in V_Default
  922. * fixed some messages
  923. * first time parameter scan is only for -v and -T
  924. - removed old style messages
  925. Revision 1.11 1998/05/01 16:38:46 florian
  926. * handling of private and protected fixed
  927. + change_keywords_to_tp implemented to remove
  928. keywords which aren't supported by tp
  929. * break and continue are now symbols of the system unit
  930. + widestring, longstring and ansistring type released
  931. Revision 1.10 1998/04/29 10:34:08 pierre
  932. + added some code for ansistring (not complete nor working yet)
  933. * corrected operator overloading
  934. * corrected nasm output
  935. + started inline procedures
  936. + added starstarn : use ** for exponentiation (^ gave problems)
  937. + started UseTokenInfo cond to get accurate positions
  938. Revision 1.9 1998/04/21 10:16:49 peter
  939. * patches from strasbourg
  940. * objects is not used anymore in the fpc compiled version
  941. Revision 1.8 1998/04/12 22:39:44 florian
  942. * problem with read access to properties solved
  943. * correct handling of hidding methods via virtual (COM)
  944. * correct result type of constructor calls (COM), the resulttype
  945. depends now on the type of the class reference
  946. Revision 1.7 1998/04/10 21:36:56 florian
  947. + some stuff to support method pointers (procedure of object) added
  948. (declaration, parameter handling)
  949. Revision 1.6 1998/04/10 15:39:49 florian
  950. * more fixes to get classes.pas compiled
  951. Revision 1.5 1998/04/09 23:02:16 florian
  952. * small problems solved to get remake3 work
  953. Revision 1.4 1998/04/08 16:58:09 pierre
  954. * several bugfixes
  955. ADD ADC and AND are also sign extended
  956. nasm output OK (program still crashes at end
  957. and creates wrong assembler files !!)
  958. procsym types sym in tdef removed !!
  959. Revision 1.3 1998/04/08 11:34:22 peter
  960. * nasm works (linux only tested)
  961. }