types.pas 39 KB

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