types.pas 44 KB

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