types.pas 44 KB

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