types.pas 44 KB

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