types.pas 37 KB

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