types.pas 38 KB

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