types.pas 39 KB

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