types.pas 45 KB

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