types.pas 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  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. { wenn beide auf die gleiche Definition zeigen sind sie wohl gleich...}
  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. b:=is_equal(parraydef(def1)^.definition,parraydef(def2)^.definition);
  515. end
  516. else
  517. if (def1^.deftype=classrefdef) and (def2^.deftype=classrefdef) then
  518. begin
  519. { similar to pointerdef: }
  520. if assigned(def1^.sym) and ((def1^.sym^.properties and sp_forwarddef)<>0) then
  521. b:=(def1^.sym=def2^.sym)
  522. else
  523. b:=is_equal(pclassrefdef(def1)^.definition,pclassrefdef(def2)^.definition);
  524. end;
  525. is_equal:=b;
  526. end;
  527. function is_subequal(def1, def2: pdef): boolean;
  528. Begin
  529. if assigned(def1) and assigned(def2) then
  530. Begin
  531. is_subequal := FALSE;
  532. if (def1^.deftype = orddef) and (def2^.deftype = orddef) then
  533. Begin
  534. { see p.47 of Turbo Pascal 7.01 manual for the separation of types }
  535. { range checking for case statements is done with testrange }
  536. case porddef(def1)^.typ of
  537. u8bit,u16bit,u32bit,
  538. s8bit,s16bit,s32bit : is_subequal:=(porddef(def2)^.typ in [s32bit,u32bit,u8bit,s8bit,s16bit,u16bit]);
  539. bool8bit,bool16bit,bool32bit : is_subequal:=(porddef(def2)^.typ in [bool8bit,bool16bit,bool32bit]);
  540. uchar : is_subequal:=(porddef(def2)^.typ=uchar);
  541. end;
  542. end
  543. else
  544. Begin
  545. { I assume that both enumerations are equal when the first }
  546. { pointers are equal. }
  547. if (def1^.deftype = enumdef) and (def2^.deftype =enumdef) then
  548. Begin
  549. if penumdef(def1)^.first = penumdef(def2)^.first then
  550. is_subequal := TRUE;
  551. end;
  552. end;
  553. end; { endif assigned ... }
  554. end;
  555. type
  556. pprocdefcoll = ^tprocdefcoll;
  557. tprocdefcoll = record
  558. next : pprocdefcoll;
  559. data : pprocdef;
  560. end;
  561. psymcoll = ^tsymcoll;
  562. tsymcoll = record
  563. next : psymcoll;
  564. name : pstring;
  565. data : pprocdefcoll;
  566. end;
  567. var
  568. wurzel : psymcoll;
  569. nextvirtnumber : longint;
  570. _c : pobjectdef;
  571. has_constructor,has_virtual_method : boolean;
  572. procedure eachsym(sym : psym);{$ifndef FPC}far;{$endif}
  573. var
  574. procdefcoll : pprocdefcoll;
  575. hp : pprocdef;
  576. symcoll : psymcoll;
  577. _name : string;
  578. stored : boolean;
  579. { creates a new entry in the procsym list }
  580. procedure newentry;
  581. begin
  582. { if not, generate a new symbol item }
  583. new(symcoll);
  584. symcoll^.name:=stringdup(sym^.name);
  585. symcoll^.next:=wurzel;
  586. symcoll^.data:=nil;
  587. wurzel:=symcoll;
  588. hp:=pprocsym(sym)^.definition;
  589. { inserts all definitions }
  590. while assigned(hp) do
  591. begin
  592. new(procdefcoll);
  593. procdefcoll^.data:=hp;
  594. procdefcoll^.next:=symcoll^.data;
  595. symcoll^.data:=procdefcoll;
  596. { if it's a virtual method }
  597. if (hp^.options and povirtualmethod)<>0 then
  598. begin
  599. { then it gets a number ... }
  600. hp^.extnumber:=nextvirtnumber;
  601. { and we inc the number }
  602. inc(nextvirtnumber);
  603. has_virtual_method:=true;
  604. end;
  605. if (hp^.options and poconstructor)<>0 then
  606. has_constructor:=true;
  607. { check, if a method should be overridden }
  608. if (hp^.options and pooverridingmethod)<>0 then
  609. Message1(parser_e_nothing_to_be_overridden,_c^.name^+'.'+_name);
  610. { next overloaded method }
  611. hp:=hp^.nextoverloaded;
  612. end;
  613. end;
  614. begin
  615. { put only sub routines into the VMT }
  616. if sym^.typ=procsym then
  617. begin
  618. _name:=sym^.name;
  619. symcoll:=wurzel;
  620. while assigned(symcoll) do
  621. begin
  622. { does the symbol already exist in the list ? }
  623. if _name=symcoll^.name^ then
  624. begin
  625. { walk through all defs of the symbol }
  626. hp:=pprocsym(sym)^.definition;
  627. while assigned(hp) do
  628. begin
  629. { compare with all stored definitions }
  630. procdefcoll:=symcoll^.data;
  631. stored:=false;
  632. while assigned(procdefcoll) do
  633. begin
  634. { compare parameters }
  635. if equal_paras(procdefcoll^.data^.para1,hp^.para1,false) and
  636. (
  637. ((procdefcoll^.data^.options and povirtualmethod)<>0) or
  638. ((hp^.options and povirtualmethod)<>0)
  639. ) then
  640. begin
  641. { wenn sie gleich sind }
  642. { und eine davon virtual deklariert ist }
  643. { Fehler falls nur eine VIRTUAL }
  644. if (procdefcoll^.data^.options and povirtualmethod)<>
  645. (hp^.options and povirtualmethod) then
  646. begin
  647. { in classes, we hide the old method }
  648. if _c^.isclass then
  649. begin
  650. { warn only if it is the first time,
  651. we hide the method }
  652. if _c=hp^._class then
  653. Message1(parser_w_should_use_override,_c^.name^+'.'+_name);
  654. newentry;
  655. exit;
  656. end
  657. else
  658. if _c=hp^._class then
  659. begin
  660. Message1(parser_w_overloaded_are_not_both_virtual,_c^.name^+'.'+_name);
  661. newentry;
  662. exit;
  663. end;
  664. end;
  665. { check, if the overridden directive is set }
  666. { (povirtualmethod is set! }
  667. { class ? }
  668. if _c^.isclass and
  669. ((hp^.options and pooverridingmethod)=0) then
  670. begin
  671. { warn only if it is the first time,
  672. we hide the method }
  673. if _c=hp^._class then
  674. Message1(parser_w_should_use_override,_c^.name^+'.'+_name);
  675. newentry;
  676. exit;
  677. end;
  678. { error, if the return types aren't equal }
  679. if not(is_equal(procdefcoll^.data^.retdef,hp^.retdef)) then
  680. Message1(parser_e_overloaded_methodes_not_same_ret,_c^.name^+'.'+_name);
  681. { the flags have to match }
  682. { except abstract and override }
  683. if (procdefcoll^.data^.options and not(poabstractmethod or pooverridingmethod))<>
  684. (hp^.options and not(poabstractmethod or pooverridingmethod)) then
  685. Message1(parser_e_header_dont_match_forward,_c^.name^+'.'+_name);
  686. { now set the number }
  687. hp^.extnumber:=procdefcoll^.data^.extnumber;
  688. { and exchange }
  689. procdefcoll^.data:=hp;
  690. stored:=true;
  691. end;
  692. procdefcoll:=procdefcoll^.next;
  693. end;
  694. { if it isn't saved in the list }
  695. { we create a new entry }
  696. if not(stored) then
  697. begin
  698. new(procdefcoll);
  699. procdefcoll^.data:=hp;
  700. procdefcoll^.next:=symcoll^.data;
  701. symcoll^.data:=procdefcoll;
  702. { if the method is virtual ... }
  703. if (hp^.options and povirtualmethod)<>0 then
  704. begin
  705. { ... it will get a number }
  706. hp^.extnumber:=nextvirtnumber;
  707. inc(nextvirtnumber);
  708. end;
  709. { check, if a method should be overridden }
  710. if (hp^.options and pooverridingmethod)<>0 then
  711. Message1(parser_e_nothing_to_be_overridden,_c^.name^+'.'+_name);
  712. end;
  713. hp:=hp^.nextoverloaded;
  714. end;
  715. exit;
  716. end;
  717. symcoll:=symcoll^.next;
  718. end;
  719. newentry;
  720. end;
  721. end;
  722. procedure genvmt(_class : pobjectdef);
  723. procedure do_genvmt(p : pobjectdef);
  724. begin
  725. { start with the base class }
  726. if assigned(p^.childof) then
  727. do_genvmt(p^.childof);
  728. { walk through all public syms }
  729. _c:=_class;
  730. {$ifdef tp}
  731. p^.publicsyms^.foreach(eachsym);
  732. {$else}
  733. p^.publicsyms^.foreach(@eachsym);
  734. {$endif}
  735. end;
  736. var
  737. symcoll : psymcoll;
  738. procdefcoll : pprocdefcoll;
  739. i : longint;
  740. begin
  741. wurzel:=nil;
  742. nextvirtnumber:=0;
  743. has_constructor:=false;
  744. has_virtual_method:=false;
  745. { generates a tree of all used methods }
  746. do_genvmt(_class);
  747. if has_virtual_method and not(has_constructor) then
  748. Message1(parser_w_virtual_without_constructor,_class^.name^);
  749. { generates the VMT }
  750. { walk trough all numbers for virtual methods and search }
  751. { the method }
  752. for i:=0 to nextvirtnumber-1 do
  753. begin
  754. symcoll:=wurzel;
  755. { walk trough all symbols }
  756. while assigned(symcoll) do
  757. begin
  758. { walk trough all methods }
  759. procdefcoll:=symcoll^.data;
  760. while assigned(procdefcoll) do
  761. begin
  762. { writes the addresses to the VMT }
  763. { but only this which are declared as virtual }
  764. if procdefcoll^.data^.extnumber=i then
  765. begin
  766. if (procdefcoll^.data^.options and povirtualmethod)<>0 then
  767. begin
  768. { if a method is abstract, then is also the }
  769. { class abstract and it's not allow to }
  770. { generates an instance }
  771. if (procdefcoll^.data^.options and poabstractmethod)<>0 then
  772. begin
  773. _class^.options:=_class^.options or oois_abstract;
  774. datasegment^.concat(new(pai_const,init_symbol('ABSTRACTERROR')));
  775. end
  776. else
  777. begin
  778. datasegment^.concat(new(pai_const,init_symbol(
  779. strpnew(procdefcoll^.data^.mangledname))));
  780. maybe_concat_external(procdefcoll^.data^.owner,
  781. procdefcoll^.data^.mangledname);
  782. end;
  783. end;
  784. end;
  785. procdefcoll:=procdefcoll^.next;
  786. end;
  787. symcoll:=symcoll^.next;
  788. end;
  789. end;
  790. { disposes the above generated tree }
  791. symcoll:=wurzel;
  792. while assigned(symcoll) do
  793. begin
  794. wurzel:=symcoll^.next;
  795. stringdispose(symcoll^.name);
  796. procdefcoll:=symcoll^.data;
  797. while assigned(procdefcoll) do
  798. begin
  799. symcoll^.data:=procdefcoll^.next;
  800. dispose(procdefcoll);
  801. procdefcoll:=symcoll^.data;
  802. end;
  803. dispose(symcoll);
  804. symcoll:=wurzel;
  805. end;
  806. end;
  807. end.
  808. {
  809. $Log$
  810. Revision 1.28 1998-09-09 16:44:23 florian
  811. * I hope, the case bug is fixed now
  812. Revision 1.27 1998/09/07 17:37:07 florian
  813. * first fixes for published properties
  814. Revision 1.26 1998/09/04 12:24:31 florian
  815. * bug0159 fixed
  816. Revision 1.25 1998/09/04 09:06:36 florian
  817. * bug0132 fixed
  818. Revision 1.24 1998/09/04 08:36:49 peter
  819. * fixed boolean:=integer which is not explicit
  820. Revision 1.23 1998/09/01 17:39:55 peter
  821. + internal constant functions
  822. Revision 1.22 1998/09/01 12:53:28 peter
  823. + aktpackenum
  824. Revision 1.21 1998/08/19 00:42:45 peter
  825. + subrange types for enums
  826. + checking for bounds type with ranges
  827. Revision 1.20 1998/08/18 14:17:14 pierre
  828. * bug about assigning the return value of a function to
  829. a procvar fixed : warning
  830. assigning a proc to a procvar need @ in FPC mode !!
  831. * missing file/line info restored
  832. Revision 1.19 1998/08/18 09:24:48 pierre
  833. * small warning position bug fixed
  834. * support_mmx switches splitting was missing
  835. * rhide error and warning output corrected
  836. Revision 1.18 1998/08/14 18:18:49 peter
  837. + dynamic set contruction
  838. * smallsets are now working (always longint size)
  839. Revision 1.17 1998/08/05 16:00:17 florian
  840. * some fixes for ansi strings
  841. Revision 1.16 1998/07/20 23:35:50 michael
  842. Const ansistrings are not copied.
  843. Revision 1.15 1998/07/18 22:54:32 florian
  844. * some ansi/wide/longstring support fixed:
  845. o parameter passing
  846. o returning as result from functions
  847. Revision 1.14 1998/06/12 14:50:50 peter
  848. * removed the tree dependency to types.pas
  849. * long_fil.pas support (not fully tested yet)
  850. Revision 1.13 1998/06/03 22:49:07 peter
  851. + wordbool,longbool
  852. * rename bis,von -> high,low
  853. * moved some systemunit loading/creating to psystem.pas
  854. Revision 1.12 1998/05/12 10:47:00 peter
  855. * moved printstatus to verb_def
  856. + V_Normal which is between V_Error and V_Warning and doesn't have a
  857. prefix like error: warning: and is included in V_Default
  858. * fixed some messages
  859. * first time parameter scan is only for -v and -T
  860. - removed old style messages
  861. Revision 1.11 1998/05/01 16:38:46 florian
  862. * handling of private and protected fixed
  863. + change_keywords_to_tp implemented to remove
  864. keywords which aren't supported by tp
  865. * break and continue are now symbols of the system unit
  866. + widestring, longstring and ansistring type released
  867. Revision 1.10 1998/04/29 10:34:08 pierre
  868. + added some code for ansistring (not complete nor working yet)
  869. * corrected operator overloading
  870. * corrected nasm output
  871. + started inline procedures
  872. + added starstarn : use ** for exponentiation (^ gave problems)
  873. + started UseTokenInfo cond to get accurate positions
  874. Revision 1.9 1998/04/21 10:16:49 peter
  875. * patches from strasbourg
  876. * objects is not used anymore in the fpc compiled version
  877. Revision 1.8 1998/04/12 22:39:44 florian
  878. * problem with read access to properties solved
  879. * correct handling of hidding methods via virtual (COM)
  880. * correct result type of constructor calls (COM), the resulttype
  881. depends now on the type of the class reference
  882. Revision 1.7 1998/04/10 21:36:56 florian
  883. + some stuff to support method pointers (procedure of object) added
  884. (declaration, parameter handling)
  885. Revision 1.6 1998/04/10 15:39:49 florian
  886. * more fixes to get classes.pas compiled
  887. Revision 1.5 1998/04/09 23:02:16 florian
  888. * small problems solved to get remake3 work
  889. Revision 1.4 1998/04/08 16:58:09 pierre
  890. * several bugfixes
  891. ADD ADC and AND are also sign extended
  892. nasm output OK (program still crashes at end
  893. and creates wrong assembler files !!)
  894. procsym types sym in tdef removed !!
  895. Revision 1.3 1998/04/08 11:34:22 peter
  896. * nasm works (linux only tested)
  897. }