types.pas 43 KB

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