types.pas 41 KB

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