types.pas 41 KB

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