types.pas 41 KB

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