2
0

types.pas 44 KB

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