types.pas 45 KB

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