types.pas 42 KB

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