types.pas 45 KB

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