types.pas 44 KB

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