types.pas 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184
  1. {
  2. $Id$
  3. Copyright (C) 1998-2000 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,symtable
  22. {$IFDEF NEWST}
  23. ,defs
  24. {$ENDIF NEWST};
  25. type
  26. tmmxtype = (mmxno,mmxu8bit,mmxs8bit,mmxu16bit,mmxs16bit,
  27. mmxu32bit,mmxs32bit,mmxfixed16,mmxsingle);
  28. const
  29. { true if we must never copy this parameter }
  30. never_copy_const_param : boolean = false;
  31. {*****************************************************************************
  32. Basic type functions
  33. *****************************************************************************}
  34. { returns true, if def defines an ordinal type }
  35. function is_ordinal(def : pdef) : boolean;
  36. { returns the min. value of the type }
  37. function get_min_value(def : pdef) : longint;
  38. { returns true, if def defines an ordinal type }
  39. function is_integer(def : pdef) : boolean;
  40. { true if p is a boolean }
  41. function is_boolean(def : pdef) : boolean;
  42. { true if p is a char }
  43. function is_char(def : pdef) : boolean;
  44. { true if p is a void}
  45. function is_void(def : pdef) : boolean;
  46. { true if p is a smallset def }
  47. function is_smallset(p : pdef) : boolean;
  48. { returns true, if def defines a signed data type (only for ordinal types) }
  49. function is_signed(def : pdef) : boolean;
  50. {*****************************************************************************
  51. Array helper functions
  52. *****************************************************************************}
  53. { true, if p points to a zero based (non special like open or
  54. dynamic array def, mainly this is used to see if the array
  55. is convertable to a pointer }
  56. function is_zero_based_array(p : pdef) : boolean;
  57. { true if p points to an open array def }
  58. function is_open_array(p : pdef) : boolean;
  59. { true, if p points to an array of const def }
  60. function is_array_constructor(p : pdef) : boolean;
  61. { true, if p points to a variant array }
  62. function is_variant_array(p : pdef) : boolean;
  63. { true, if p points to an array of const }
  64. function is_array_of_const(p : pdef) : boolean;
  65. { true, if p points any kind of special array }
  66. function is_special_array(p : pdef) : boolean;
  67. { true if p is a char array def }
  68. function is_chararray(p : pdef) : boolean;
  69. {*****************************************************************************
  70. String helper functions
  71. *****************************************************************************}
  72. { true if p points to an open string def }
  73. function is_open_string(p : pdef) : boolean;
  74. { true if p is an ansi string def }
  75. function is_ansistring(p : pdef) : boolean;
  76. { true if p is a long string def }
  77. function is_longstring(p : pdef) : boolean;
  78. { true if p is a wide string def }
  79. function is_widestring(p : pdef) : boolean;
  80. { true if p is a short string def }
  81. function is_shortstring(p : pdef) : boolean;
  82. { true if p is a pchar def }
  83. function is_pchar(p : pdef) : boolean;
  84. { true if p is a voidpointer def }
  85. function is_voidpointer(p : pdef) : boolean;
  86. { returns true, if def uses FPU }
  87. function is_fpu(def : pdef) : boolean;
  88. { true if the return value is in EAX }
  89. function ret_in_acc(def : pdef) : boolean;
  90. { true if uses a parameter as return value }
  91. function ret_in_param(def : pdef) : boolean;
  92. { true, if def is a 64 bit int type }
  93. function is_64bitint(def : pdef) : boolean;
  94. function push_high_param(def : pdef) : boolean;
  95. { true if a parameter is too large to copy and only the address is pushed }
  96. function push_addr_param(def : pdef) : boolean;
  97. { true, if def1 and def2 are semantical the same }
  98. function is_equal(def1,def2 : pdef) : boolean;
  99. { checks for type compatibility (subgroups of type) }
  100. { used for case statements... probably missing stuff }
  101. { to use on other types }
  102. function is_subequal(def1, def2: pdef): boolean;
  103. { same as is_equal, but with error message if failed }
  104. function CheckTypes(def1,def2 : pdef) : boolean;
  105. { true, if two parameter lists are equal }
  106. { if acp is cp_none, all have to match exactly }
  107. { if acp is cp_value_equal_const call by value }
  108. { and call by const parameter are assumed as }
  109. { equal }
  110. { if acp is cp_all the var const or nothing are considered equal }
  111. type
  112. compare_type = ( cp_none, cp_value_equal_const, cp_all);
  113. function equal_paras(paralist1,paralist2 : plinkedlist; acp : compare_type) : boolean;
  114. { true if a type can be allowed for another one
  115. in a func var }
  116. function convertable_paras(paralist1,paralist2 : plinkedlist; acp : compare_type) : boolean;
  117. { true if a function can be assigned to a procvar }
  118. function proc_to_procvar_equal(def1:pprocdef;def2:pprocvardef) : boolean;
  119. { if l isn't in the range of def a range check error is generated and
  120. the value is placed within the range }
  121. procedure testrange(def : pdef;var l : longint);
  122. { returns the range of def }
  123. procedure getrange(def : pdef;var l : longint;var h : longint);
  124. { some type helper routines for MMX support }
  125. function is_mmx_able_array(p : pdef) : boolean;
  126. { returns the mmx type }
  127. function mmx_type(p : pdef) : tmmxtype;
  128. { returns true, if sym needs an entry in the proplist of a class rtti }
  129. function needs_prop_entry(sym : psym) : boolean;
  130. { returns true, if p contains data which needs init/final code }
  131. function needs_init_final(p : psymtable) : boolean;
  132. implementation
  133. uses
  134. strings,globtype,globals,htypechk,
  135. tree,verbose,symconst;
  136. var
  137. b_needs_init_final : boolean;
  138. procedure _needs_init_final(p : pnamedindexobject);{$ifndef FPC}far;{$endif}
  139. begin
  140. if (psym(p)^.typ=varsym) and
  141. assigned(pvarsym(p)^.vartype.def) and
  142. not((pvarsym(p)^.vartype.def^.deftype=objectdef) and
  143. pobjectdef(pvarsym(p)^.vartype.def)^.is_class) and
  144. pvarsym(p)^.vartype.def^.needs_inittable then
  145. b_needs_init_final:=true;
  146. end;
  147. { returns true, if p contains data which needs init/final code }
  148. function needs_init_final(p : psymtable) : boolean;
  149. begin
  150. b_needs_init_final:=false;
  151. p^.foreach({$ifndef TP}@{$endif}_needs_init_final);
  152. needs_init_final:=b_needs_init_final;
  153. end;
  154. function needs_prop_entry(sym : psym) : boolean;
  155. begin
  156. needs_prop_entry:=(sp_published in psym(sym)^.symoptions) and
  157. (sym^.typ in [propertysym,varsym]);
  158. end;
  159. { compare_type = ( cp_none, cp_value_equal_const, cp_all); }
  160. function equal_paras(paralist1,paralist2 : plinkedlist; acp : compare_type) : boolean;
  161. var
  162. def1,def2 : pparaitem;
  163. begin
  164. def1:=pparaitem(paralist1^.first);
  165. def2:=pparaitem(paralist2^.first);
  166. while (assigned(def1)) and (assigned(def2)) do
  167. begin
  168. case acp of
  169. cp_value_equal_const :
  170. begin
  171. if not(is_equal(def1^.paratype.def,def2^.paratype.def)) or
  172. ((def1^.paratyp<>def2^.paratyp) and
  173. ((def1^.paratyp=vs_var) or
  174. (def1^.paratyp=vs_var)
  175. )
  176. ) then
  177. begin
  178. equal_paras:=false;
  179. exit;
  180. end;
  181. end;
  182. cp_all :
  183. begin
  184. if not(is_equal(def1^.paratype.def,def2^.paratype.def)) or
  185. (def1^.paratyp<>def2^.paratyp) then
  186. begin
  187. equal_paras:=false;
  188. exit;
  189. end;
  190. end;
  191. cp_none :
  192. begin
  193. if not(is_equal(def1^.paratype.def,def2^.paratype.def)) then
  194. begin
  195. equal_paras:=false;
  196. exit;
  197. end;
  198. end;
  199. end;
  200. def1:=pparaitem(def1^.next);
  201. def2:=pparaitem(def2^.next);
  202. end;
  203. if (def1=nil) and (def2=nil) then
  204. equal_paras:=true
  205. else
  206. equal_paras:=false;
  207. end;
  208. function convertable_paras(paralist1,paralist2 : plinkedlist;acp : compare_type) : boolean;
  209. var
  210. def1,def2 : pparaitem;
  211. doconv : tconverttype;
  212. begin
  213. def1:=pparaitem(paralist1^.first);
  214. def2:=pparaitem(paralist2^.first);
  215. while (assigned(def1)) and (assigned(def2)) do
  216. begin
  217. case acp of
  218. cp_value_equal_const :
  219. begin
  220. if (isconvertable(def1^.paratype.def,def2^.paratype.def,doconv,callparan,false)=0) or
  221. ((def1^.paratyp<>def2^.paratyp) and
  222. ((def1^.paratyp=vs_var) or
  223. (def1^.paratyp=vs_var)
  224. )
  225. ) then
  226. begin
  227. convertable_paras:=false;
  228. exit;
  229. end;
  230. end;
  231. cp_all :
  232. begin
  233. if (isconvertable(def1^.paratype.def,def2^.paratype.def,doconv,callparan,false)=0) or
  234. (def1^.paratyp<>def2^.paratyp) then
  235. begin
  236. convertable_paras:=false;
  237. exit;
  238. end;
  239. end;
  240. cp_none :
  241. begin
  242. if (isconvertable(def1^.paratype.def,def2^.paratype.def,doconv,callparan,false)=0) then
  243. begin
  244. convertable_paras:=false;
  245. exit;
  246. end;
  247. end;
  248. end;
  249. def1:=pparaitem(def1^.next);
  250. def2:=pparaitem(def2^.next);
  251. end;
  252. if (def1=nil) and (def2=nil) then
  253. convertable_paras:=true
  254. else
  255. convertable_paras:=false;
  256. end;
  257. { true if a function can be assigned to a procvar }
  258. function proc_to_procvar_equal(def1:pprocdef;def2:pprocvardef) : boolean;
  259. const
  260. po_comp = po_compatibility_options-[po_methodpointer];
  261. var
  262. ismethod : boolean;
  263. begin
  264. proc_to_procvar_equal:=false;
  265. if not(assigned(def1)) or not(assigned(def2)) then
  266. exit;
  267. { check for method pointer }
  268. ismethod:=assigned(def1^.owner) and
  269. (def1^.owner^.symtabletype=objectsymtable);
  270. { I think methods of objects are also not compatible }
  271. { with procedure variables! (FK)
  272. and
  273. assigned(def1^.owner^.defowner) and
  274. (pobjectdef(def1^.owner^.defowner)^.is_class); }
  275. if (ismethod and not (po_methodpointer in def2^.procoptions)) or
  276. (not(ismethod) and (po_methodpointer in def2^.procoptions)) then
  277. begin
  278. Message(type_e_no_method_and_procedure_not_compatible);
  279. exit;
  280. end;
  281. { check return value and para's and options, methodpointer is already checked
  282. parameters may also be convertable }
  283. if is_equal(def1^.rettype.def,def2^.rettype.def) and
  284. (equal_paras(def1^.para,def2^.para,cp_all) or
  285. convertable_paras(def1^.para,def2^.para,cp_all)) and
  286. ((po_comp * def1^.procoptions)= (po_comp * def2^.procoptions)) then
  287. proc_to_procvar_equal:=true
  288. else
  289. proc_to_procvar_equal:=false;
  290. end;
  291. { returns true, if def uses FPU }
  292. function is_fpu(def : pdef) : boolean;
  293. begin
  294. is_fpu:=(def^.deftype=floatdef) and (pfloatdef(def)^.typ<>f32bit);
  295. end;
  296. { true if p is an ordinal }
  297. function is_ordinal(def : pdef) : boolean;
  298. var
  299. dt : tbasetype;
  300. begin
  301. case def^.deftype of
  302. orddef :
  303. begin
  304. dt:=porddef(def)^.typ;
  305. is_ordinal:=dt in [uchar,
  306. u8bit,u16bit,u32bit,u64bit,
  307. s8bit,s16bit,s32bit,s64bit,
  308. bool8bit,bool16bit,bool32bit];
  309. end;
  310. enumdef :
  311. is_ordinal:=true;
  312. else
  313. is_ordinal:=false;
  314. end;
  315. end;
  316. { returns the min. value of the type }
  317. function get_min_value(def : pdef) : longint;
  318. begin
  319. case def^.deftype of
  320. orddef:
  321. get_min_value:=porddef(def)^.low;
  322. enumdef:
  323. get_min_value:=penumdef(def)^.min;
  324. else
  325. get_min_value:=0;
  326. end;
  327. end;
  328. { true if p is an integer }
  329. function is_integer(def : pdef) : boolean;
  330. begin
  331. is_integer:=(def^.deftype=orddef) and
  332. (porddef(def)^.typ in [uauto,u8bit,u16bit,u32bit,u64bit,
  333. s8bit,s16bit,s32bit,s64bit]);
  334. end;
  335. { true if p is a boolean }
  336. function is_boolean(def : pdef) : boolean;
  337. begin
  338. is_boolean:=(def^.deftype=orddef) and
  339. (porddef(def)^.typ in [bool8bit,bool16bit,bool32bit]);
  340. end;
  341. { true if p is a void }
  342. function is_void(def : pdef) : boolean;
  343. begin
  344. is_void:=(def^.deftype=orddef) and
  345. (porddef(def)^.typ=uvoid);
  346. end;
  347. { true if p is a char }
  348. function is_char(def : pdef) : boolean;
  349. begin
  350. is_char:=(def^.deftype=orddef) and
  351. (porddef(def)^.typ=uchar);
  352. end;
  353. { true if p is signed (integer) }
  354. function is_signed(def : pdef) : boolean;
  355. var
  356. dt : tbasetype;
  357. begin
  358. case def^.deftype of
  359. orddef :
  360. begin
  361. dt:=porddef(def)^.typ;
  362. is_signed:=(dt in [s8bit,s16bit,s32bit,s64bit]);
  363. end;
  364. enumdef :
  365. is_signed:=false;
  366. else
  367. is_signed:=false;
  368. end;
  369. end;
  370. { true, if p points to an open array def }
  371. function is_open_string(p : pdef) : boolean;
  372. begin
  373. is_open_string:=(p^.deftype=stringdef) and
  374. (pstringdef(p)^.string_typ=st_shortstring) and
  375. (pstringdef(p)^.len=0);
  376. end;
  377. { true, if p points to a zero based array def }
  378. function is_zero_based_array(p : pdef) : boolean;
  379. begin
  380. is_zero_based_array:=(p^.deftype=arraydef) and
  381. (parraydef(p)^.lowrange=0) and
  382. not(is_special_array(p));
  383. end;
  384. { true, if p points to an open array def }
  385. function is_open_array(p : pdef) : boolean;
  386. begin
  387. { check for s32bitdef is needed, because for u32bit the high
  388. range is also -1 ! (PFV) }
  389. is_open_array:=(p^.deftype=arraydef) and
  390. (parraydef(p)^.rangetype.def=pdef(s32bitdef)) and
  391. (parraydef(p)^.lowrange=0) and
  392. (parraydef(p)^.highrange=-1) and
  393. not(parraydef(p)^.IsConstructor) and
  394. not(parraydef(p)^.IsVariant) and
  395. not(parraydef(p)^.IsArrayOfConst);
  396. end;
  397. { true, if p points to an array of const def }
  398. function is_array_constructor(p : pdef) : boolean;
  399. begin
  400. is_array_constructor:=(p^.deftype=arraydef) and
  401. (parraydef(p)^.IsConstructor);
  402. end;
  403. { true, if p points to a variant array }
  404. function is_variant_array(p : pdef) : boolean;
  405. begin
  406. is_variant_array:=(p^.deftype=arraydef) and
  407. (parraydef(p)^.IsVariant);
  408. end;
  409. { true, if p points to an array of const }
  410. function is_array_of_const(p : pdef) : boolean;
  411. begin
  412. is_array_of_const:=(p^.deftype=arraydef) and
  413. (parraydef(p)^.IsArrayOfConst);
  414. end;
  415. { true, if p points to a special array }
  416. function is_special_array(p : pdef) : boolean;
  417. begin
  418. is_special_array:=(p^.deftype=arraydef) and
  419. ((parraydef(p)^.IsVariant) or
  420. (parraydef(p)^.IsArrayOfConst) or
  421. (parraydef(p)^.IsConstructor) or
  422. is_open_array(p)
  423. );
  424. end;
  425. { true if p is an ansi string def }
  426. function is_ansistring(p : pdef) : boolean;
  427. begin
  428. is_ansistring:=(p^.deftype=stringdef) and
  429. (pstringdef(p)^.string_typ=st_ansistring);
  430. end;
  431. { true if p is an long string def }
  432. function is_longstring(p : pdef) : boolean;
  433. begin
  434. is_longstring:=(p^.deftype=stringdef) and
  435. (pstringdef(p)^.string_typ=st_longstring);
  436. end;
  437. { true if p is an wide string def }
  438. function is_widestring(p : pdef) : boolean;
  439. begin
  440. is_widestring:=(p^.deftype=stringdef) and
  441. (pstringdef(p)^.string_typ=st_widestring);
  442. end;
  443. { true if p is an short string def }
  444. function is_shortstring(p : pdef) : boolean;
  445. begin
  446. is_shortstring:=(p^.deftype=stringdef) and
  447. (pstringdef(p)^.string_typ=st_shortstring);
  448. end;
  449. { true if p is a char array def }
  450. function is_chararray(p : pdef) : boolean;
  451. begin
  452. is_chararray:=(p^.deftype=arraydef) and
  453. is_equal(parraydef(p)^.elementtype.def,cchardef) and
  454. not(is_special_array(p));
  455. end;
  456. { true if p is a pchar def }
  457. function is_pchar(p : pdef) : boolean;
  458. begin
  459. is_pchar:=(p^.deftype=pointerdef) and
  460. is_equal(Ppointerdef(p)^.pointertype.def,cchardef);
  461. end;
  462. { true if p is a voidpointer def }
  463. function is_voidpointer(p : pdef) : boolean;
  464. begin
  465. is_voidpointer:=(p^.deftype=pointerdef) and
  466. is_equal(Ppointerdef(p)^.pointertype.def,voiddef);
  467. end;
  468. { true if p is a smallset def }
  469. function is_smallset(p : pdef) : boolean;
  470. begin
  471. is_smallset:=(p^.deftype=setdef) and
  472. (psetdef(p)^.settype=smallset);
  473. end;
  474. { true if the return value is in accumulator (EAX for i386), D0 for 68k }
  475. function ret_in_acc(def : pdef) : boolean;
  476. begin
  477. ret_in_acc:=(def^.deftype in [orddef,pointerdef,enumdef,classrefdef]) or
  478. ((def^.deftype=stringdef) and (pstringdef(def)^.string_typ in [st_ansistring,st_widestring])) or
  479. ((def^.deftype=procvardef) and not(po_methodpointer in pprocvardef(def)^.procoptions)) or
  480. ((def^.deftype=objectdef) and pobjectdef(def)^.is_class) or
  481. ((def^.deftype=setdef) and (psetdef(def)^.settype=smallset)) or
  482. ((def^.deftype=floatdef) and (pfloatdef(def)^.typ=f32bit));
  483. end;
  484. { true, if def is a 64 bit int type }
  485. function is_64bitint(def : pdef) : boolean;
  486. begin
  487. is_64bitint:=(def^.deftype=orddef) and (porddef(def)^.typ in [u64bit,s64bit])
  488. end;
  489. { true if uses a parameter as return value }
  490. function ret_in_param(def : pdef) : boolean;
  491. begin
  492. ret_in_param:=(def^.deftype in [arraydef,recorddef]) or
  493. ((def^.deftype=stringdef) and (pstringdef(def)^.string_typ in [st_shortstring,st_longstring])) or
  494. ((def^.deftype=procvardef) and (po_methodpointer in pprocvardef(def)^.procoptions)) or
  495. ((def^.deftype=objectdef) and not(pobjectdef(def)^.is_class)) or
  496. ((def^.deftype=setdef) and (psetdef(def)^.settype<>smallset));
  497. end;
  498. function push_high_param(def : pdef) : boolean;
  499. begin
  500. push_high_param:=is_open_array(def) or
  501. is_open_string(def) or
  502. is_array_of_const(def);
  503. end;
  504. { true if a parameter is too large to copy and only the address is pushed }
  505. function push_addr_param(def : pdef) : boolean;
  506. begin
  507. push_addr_param:=false;
  508. if never_copy_const_param then
  509. push_addr_param:=true
  510. else
  511. begin
  512. case def^.deftype of
  513. formaldef :
  514. push_addr_param:=true;
  515. recorddef :
  516. push_addr_param:=(def^.size>4);
  517. arraydef :
  518. push_addr_param:=((Parraydef(def)^.highrange>Parraydef(def)^.lowrange) and (def^.size>4)) or
  519. is_open_array(def) or
  520. is_array_of_const(def) or
  521. is_array_constructor(def);
  522. objectdef :
  523. push_addr_param:=not(pobjectdef(def)^.is_class);
  524. stringdef :
  525. push_addr_param:=pstringdef(def)^.string_typ in [st_shortstring,st_longstring];
  526. procvardef :
  527. push_addr_param:=(po_methodpointer in pprocvardef(def)^.procoptions);
  528. setdef :
  529. push_addr_param:=(psetdef(def)^.settype<>smallset);
  530. end;
  531. end;
  532. end;
  533. { test if l is in the range of def, outputs error if out of range }
  534. procedure testrange(def : pdef;var l : longint);
  535. var
  536. lv,hv: longint;
  537. begin
  538. { for 64 bit types we need only to check if it is less than }
  539. { zero, if def is a qword node }
  540. if is_64bitint(def) then
  541. begin
  542. if (l<0) and (porddef(def)^.typ=u64bit) then
  543. begin
  544. l:=0;
  545. if (cs_check_range in aktlocalswitches) then
  546. Message(parser_e_range_check_error)
  547. else
  548. Message(parser_w_range_check_error);
  549. end;
  550. end
  551. else
  552. begin
  553. getrange(def,lv,hv);
  554. if (def^.deftype=orddef) and
  555. (porddef(def)^.typ=u32bit) then
  556. begin
  557. if lv<=hv then
  558. begin
  559. if (l<lv) or (l>hv) then
  560. begin
  561. if (cs_check_range in aktlocalswitches) then
  562. Message(parser_e_range_check_error)
  563. else
  564. Message(parser_w_range_check_error);
  565. end;
  566. end
  567. else
  568. { this happens with the wrap around problem }
  569. { if lv is positive and hv is over $7ffffff }
  570. { so it seems negative }
  571. begin
  572. if ((l>=0) and (l<lv)) or
  573. ((l<0) and (l>hv)) then
  574. begin
  575. if (cs_check_range in aktlocalswitches) then
  576. Message(parser_e_range_check_error)
  577. else
  578. Message(parser_w_range_check_error);
  579. end;
  580. end;
  581. end
  582. else if (l<lv) or (l>hv) then
  583. begin
  584. if (def^.deftype=enumdef) or
  585. (cs_check_range in aktlocalswitches) then
  586. Message(parser_e_range_check_error)
  587. else
  588. Message(parser_w_range_check_error);
  589. { Fix the value to fit in the allocated space for this type of variable }
  590. case def^.size of
  591. 1: l := l and $ff;
  592. 2: l := l and $ffff;
  593. end
  594. { l:=lv+(l mod (hv-lv+1));}
  595. end;
  596. end;
  597. end;
  598. { return the range from def in l and h }
  599. procedure getrange(def : pdef;var l : longint;var h : longint);
  600. begin
  601. case def^.deftype of
  602. orddef :
  603. begin
  604. l:=porddef(def)^.low;
  605. h:=porddef(def)^.high;
  606. end;
  607. enumdef :
  608. begin
  609. l:=penumdef(def)^.min;
  610. h:=penumdef(def)^.max;
  611. end;
  612. arraydef :
  613. begin
  614. l:=parraydef(def)^.lowrange;
  615. h:=parraydef(def)^.highrange;
  616. end;
  617. else
  618. internalerror(987);
  619. end;
  620. end;
  621. function mmx_type(p : pdef) : tmmxtype;
  622. begin
  623. mmx_type:=mmxno;
  624. if is_mmx_able_array(p) then
  625. begin
  626. if parraydef(p)^.elementtype.def^.deftype=floatdef then
  627. case pfloatdef(parraydef(p)^.elementtype.def)^.typ of
  628. s32real:
  629. mmx_type:=mmxsingle;
  630. f16bit:
  631. mmx_type:=mmxfixed16
  632. end
  633. else
  634. case porddef(parraydef(p)^.elementtype.def)^.typ of
  635. u8bit:
  636. mmx_type:=mmxu8bit;
  637. s8bit:
  638. mmx_type:=mmxs8bit;
  639. u16bit:
  640. mmx_type:=mmxu16bit;
  641. s16bit:
  642. mmx_type:=mmxs16bit;
  643. u32bit:
  644. mmx_type:=mmxu32bit;
  645. s32bit:
  646. mmx_type:=mmxs32bit;
  647. end;
  648. end;
  649. end;
  650. function is_mmx_able_array(p : pdef) : boolean;
  651. begin
  652. {$ifdef SUPPORT_MMX}
  653. if (cs_mmx_saturation in aktlocalswitches) then
  654. begin
  655. is_mmx_able_array:=(p^.deftype=arraydef) and
  656. not(is_special_array(p)) and
  657. (
  658. (
  659. (parraydef(p)^.elementtype.def^.deftype=orddef) and
  660. (
  661. (
  662. (parraydef(p)^.lowrange=0) and
  663. (parraydef(p)^.highrange=1) and
  664. (porddef(parraydef(p)^.elementtype.def)^.typ in [u32bit,s32bit])
  665. )
  666. or
  667. (
  668. (parraydef(p)^.lowrange=0) and
  669. (parraydef(p)^.highrange=3) and
  670. (porddef(parraydef(p)^.elementtype.def)^.typ in [u16bit,s16bit])
  671. )
  672. )
  673. )
  674. or
  675. (
  676. (
  677. (parraydef(p)^.elementtype.def^.deftype=floatdef) and
  678. (
  679. (parraydef(p)^.lowrange=0) and
  680. (parraydef(p)^.highrange=3) and
  681. (pfloatdef(parraydef(p)^.elementtype.def)^.typ=f16bit)
  682. ) or
  683. (
  684. (parraydef(p)^.lowrange=0) and
  685. (parraydef(p)^.highrange=1) and
  686. (pfloatdef(parraydef(p)^.elementtype.def)^.typ=s32real)
  687. )
  688. )
  689. )
  690. );
  691. end
  692. else
  693. begin
  694. is_mmx_able_array:=(p^.deftype=arraydef) and
  695. (
  696. (
  697. (parraydef(p)^.elementtype.def^.deftype=orddef) and
  698. (
  699. (
  700. (parraydef(p)^.lowrange=0) and
  701. (parraydef(p)^.highrange=1) and
  702. (porddef(parraydef(p)^.elementtype.def)^.typ in [u32bit,s32bit])
  703. )
  704. or
  705. (
  706. (parraydef(p)^.lowrange=0) and
  707. (parraydef(p)^.highrange=3) and
  708. (porddef(parraydef(p)^.elementtype.def)^.typ in [u16bit,s16bit])
  709. )
  710. or
  711. (
  712. (parraydef(p)^.lowrange=0) and
  713. (parraydef(p)^.highrange=7) and
  714. (porddef(parraydef(p)^.elementtype.def)^.typ in [u8bit,s8bit])
  715. )
  716. )
  717. )
  718. or
  719. (
  720. (parraydef(p)^.elementtype.def^.deftype=floatdef) and
  721. (
  722. (
  723. (parraydef(p)^.lowrange=0) and
  724. (parraydef(p)^.highrange=3) and
  725. (pfloatdef(parraydef(p)^.elementtype.def)^.typ=f32bit)
  726. )
  727. or
  728. (
  729. (parraydef(p)^.lowrange=0) and
  730. (parraydef(p)^.highrange=1) and
  731. (pfloatdef(parraydef(p)^.elementtype.def)^.typ=s32real)
  732. )
  733. )
  734. )
  735. );
  736. end;
  737. {$else SUPPORT_MMX}
  738. is_mmx_able_array:=false;
  739. {$endif SUPPORT_MMX}
  740. end;
  741. function is_equal(def1,def2 : pdef) : boolean;
  742. var
  743. b : boolean;
  744. hd : pdef;
  745. begin
  746. { both types must exists }
  747. if not (assigned(def1) and assigned(def2)) then
  748. begin
  749. is_equal:=false;
  750. exit;
  751. end;
  752. { be sure, that if there is a stringdef, that this is def1 }
  753. if def2^.deftype=stringdef then
  754. begin
  755. hd:=def1;
  756. def1:=def2;
  757. def2:=hd;
  758. end;
  759. b:=false;
  760. { both point to the same definition ? }
  761. if def1=def2 then
  762. b:=true
  763. else
  764. { pointer with an equal definition are equal }
  765. if (def1^.deftype=pointerdef) and (def2^.deftype=pointerdef) then
  766. begin
  767. { here a problem detected in tabsolutesym }
  768. { the types can be forward type !! }
  769. if assigned(def1^.typesym) and (ppointerdef(def1)^.pointertype.def^.deftype=forwarddef) then
  770. b:=(def1^.typesym=def2^.typesym)
  771. else
  772. b:=ppointerdef(def1)^.pointertype.def=ppointerdef(def2)^.pointertype.def;
  773. end
  774. else
  775. { ordinals are equal only when the ordinal type is equal }
  776. if (def1^.deftype=orddef) and (def2^.deftype=orddef) then
  777. begin
  778. case porddef(def1)^.typ of
  779. u8bit,u16bit,u32bit,
  780. s8bit,s16bit,s32bit:
  781. b:=((porddef(def1)^.typ=porddef(def2)^.typ) and
  782. (porddef(def1)^.low=porddef(def2)^.low) and
  783. (porddef(def1)^.high=porddef(def2)^.high));
  784. uvoid,uchar,
  785. bool8bit,bool16bit,bool32bit:
  786. b:=(porddef(def1)^.typ=porddef(def2)^.typ);
  787. end;
  788. end
  789. else
  790. if (def1^.deftype=floatdef) and (def2^.deftype=floatdef) then
  791. b:=pfloatdef(def1)^.typ=pfloatdef(def2)^.typ
  792. else
  793. { strings with the same length are equal }
  794. if (def1^.deftype=stringdef) and (def2^.deftype=stringdef) and
  795. (pstringdef(def1)^.string_typ=pstringdef(def2)^.string_typ) then
  796. begin
  797. b:=not(is_shortstring(def1)) or
  798. (pstringdef(def1)^.len=pstringdef(def2)^.len);
  799. end
  800. else
  801. if (def1^.deftype=formaldef) and (def2^.deftype=formaldef) then
  802. b:=true
  803. { file types with the same file element type are equal }
  804. { this is a problem for assign !! }
  805. { changed to allow if one is untyped }
  806. { all typed files are equal to the special }
  807. { typed file that has voiddef as elemnt type }
  808. { but must NOT match for text file !!! }
  809. else
  810. if (def1^.deftype=filedef) and (def2^.deftype=filedef) then
  811. b:=(pfiledef(def1)^.filetyp=pfiledef(def2)^.filetyp) and
  812. ((
  813. ((pfiledef(def1)^.typedfiletype.def=nil) and
  814. (pfiledef(def2)^.typedfiletype.def=nil)) or
  815. (
  816. (pfiledef(def1)^.typedfiletype.def<>nil) and
  817. (pfiledef(def2)^.typedfiletype.def<>nil) and
  818. is_equal(pfiledef(def1)^.typedfiletype.def,pfiledef(def2)^.typedfiletype.def)
  819. ) or
  820. ( (pfiledef(def1)^.typedfiletype.def=pdef(voiddef)) or
  821. (pfiledef(def2)^.typedfiletype.def=pdef(voiddef))
  822. )))
  823. { sets with the same element type are equal }
  824. else
  825. if (def1^.deftype=setdef) and (def2^.deftype=setdef) then
  826. begin
  827. if assigned(psetdef(def1)^.elementtype.def) and
  828. assigned(psetdef(def2)^.elementtype.def) then
  829. b:=(psetdef(def1)^.elementtype.def^.deftype=psetdef(def2)^.elementtype.def^.deftype)
  830. else
  831. b:=true;
  832. end
  833. else
  834. if (def1^.deftype=procvardef) and (def2^.deftype=procvardef) then
  835. begin
  836. { poassembler isn't important for compatibility }
  837. { if a method is assigned to a methodpointer }
  838. { is checked before }
  839. b:=(pprocvardef(def1)^.proctypeoption=pprocvardef(def2)^.proctypeoption) and
  840. (pprocvardef(def1)^.proccalloptions=pprocvardef(def2)^.proccalloptions) and
  841. ((pprocvardef(def1)^.procoptions * po_compatibility_options)=
  842. (pprocvardef(def2)^.procoptions * po_compatibility_options)) and
  843. is_equal(pprocvardef(def1)^.rettype.def,pprocvardef(def2)^.rettype.def) and
  844. equal_paras(pprocvardef(def1)^.para,pprocvardef(def2)^.para,cp_all);
  845. end
  846. else
  847. if (def1^.deftype=arraydef) and (def2^.deftype=arraydef) then
  848. begin
  849. if is_open_array(def1) or is_open_array(def2) or
  850. is_array_of_const(def1) or is_array_of_const(def2) then
  851. begin
  852. if parraydef(def1)^.IsArrayOfConst or parraydef(def2)^.IsArrayOfConst then
  853. b:=true
  854. else
  855. b:=is_equal(parraydef(def1)^.elementtype.def,parraydef(def2)^.elementtype.def);
  856. end
  857. else
  858. begin
  859. b:=not(m_tp in aktmodeswitches) and
  860. not(m_delphi in aktmodeswitches) and
  861. (parraydef(def1)^.lowrange=parraydef(def2)^.lowrange) and
  862. (parraydef(def1)^.highrange=parraydef(def2)^.highrange) and
  863. is_equal(parraydef(def1)^.elementtype.def,parraydef(def2)^.elementtype.def) and
  864. is_equal(parraydef(def1)^.rangetype.def,parraydef(def2)^.rangetype.def);
  865. end;
  866. end
  867. else
  868. if (def1^.deftype=classrefdef) and (def2^.deftype=classrefdef) then
  869. begin
  870. { similar to pointerdef: }
  871. if assigned(def1^.typesym) and (pclassrefdef(def1)^.pointertype.def^.deftype=forwarddef) then
  872. b:=(def1^.typesym=def2^.typesym)
  873. else
  874. b:=is_equal(pclassrefdef(def1)^.pointertype.def,pclassrefdef(def2)^.pointertype.def);
  875. end;
  876. is_equal:=b;
  877. end;
  878. function is_subequal(def1, def2: pdef): boolean;
  879. var
  880. basedef1,basedef2 : penumdef;
  881. Begin
  882. is_subequal := false;
  883. if assigned(def1) and assigned(def2) then
  884. Begin
  885. if (def1^.deftype = orddef) and (def2^.deftype = orddef) then
  886. Begin
  887. { see p.47 of Turbo Pascal 7.01 manual for the separation of types }
  888. { range checking for case statements is done with testrange }
  889. case porddef(def1)^.typ of
  890. u8bit,u16bit,u32bit,
  891. s8bit,s16bit,s32bit :
  892. is_subequal:=(porddef(def2)^.typ in [s32bit,u32bit,u8bit,s8bit,s16bit,u16bit]);
  893. bool8bit,bool16bit,bool32bit :
  894. is_subequal:=(porddef(def2)^.typ in [bool8bit,bool16bit,bool32bit]);
  895. uchar :
  896. is_subequal:=(porddef(def2)^.typ=uchar);
  897. end;
  898. end
  899. else
  900. Begin
  901. { I assume that both enumerations are equal when the first }
  902. { pointers are equal. }
  903. { I changed this to assume that the enums are equal }
  904. { if the basedefs are equal (FK) }
  905. if (def1^.deftype=enumdef) and (def2^.deftype=enumdef) then
  906. Begin
  907. { get both basedefs }
  908. basedef1:=penumdef(def1);
  909. while assigned(basedef1^.basedef) do
  910. basedef1:=basedef1^.basedef;
  911. basedef2:=penumdef(def2);
  912. while assigned(basedef2^.basedef) do
  913. basedef2:=basedef2^.basedef;
  914. is_subequal:=basedef1=basedef2;
  915. {
  916. if penumdef(def1)^.firstenum = penumdef(def2)^.firstenum then
  917. is_subequal := TRUE;
  918. }
  919. end;
  920. end;
  921. end; { endif assigned ... }
  922. end;
  923. function CheckTypes(def1,def2 : pdef) : boolean;
  924. var
  925. s1,s2 : string;
  926. begin
  927. if not is_equal(def1,def2) then
  928. begin
  929. { Crash prevention }
  930. if (not assigned(def1)) or (not assigned(def2)) then
  931. Message(type_e_mismatch)
  932. else
  933. begin
  934. s1:=def1^.typename;
  935. s2:=def2^.typename;
  936. if (s1<>'<unknown type>') and (s2<>'<unknown type>') then
  937. Message2(type_e_not_equal_types,def1^.typename,def2^.typename)
  938. else
  939. Message(type_e_mismatch);
  940. end;
  941. CheckTypes:=false;
  942. end
  943. else
  944. CheckTypes:=true;
  945. end;
  946. end.
  947. {
  948. $Log$
  949. Revision 1.101 2000-06-20 12:47:53 pierre
  950. * equal_paras and convertable_paras changed by transforming third parameter
  951. into an enum with three possible values:
  952. cp_none, cp_value_equal_const and cp_all.
  953. Revision 1.100 2000/05/28 15:22:54 florian
  954. * fixed a problem with subrange enumerations in case statements
  955. Revision 1.99 2000/03/01 15:36:12 florian
  956. * some new stuff for the new cg
  957. Revision 1.98 2000/02/28 17:23:57 daniel
  958. * Current work of symtable integration committed. The symtable can be
  959. activated by defining 'newst', but doesn't compile yet. Changes in type
  960. checking and oop are completed. What is left is to write a new
  961. symtablestack and adapt the parser to use it.
  962. Revision 1.97 2000/02/09 13:23:09 peter
  963. * log truncated
  964. Revision 1.96 2000/02/01 09:44:03 peter
  965. * is_voidpointer
  966. Revision 1.95 2000/01/07 01:14:49 peter
  967. * updated copyright to 2000
  968. Revision 1.94 2000/01/04 16:35:58 jonas
  969. * when range checking is off, constants that are out of bound are no longer
  970. truncated to their max/min legal value but left alone (jsut an "and" is done to
  971. make sure they fit in the allocated space if necessary)
  972. Revision 1.93 1999/12/31 14:26:28 peter
  973. * fixed crash with empty array constructors
  974. Revision 1.92 1999/11/30 10:40:59 peter
  975. + ttype, tsymlist
  976. Revision 1.91 1999/11/06 14:34:31 peter
  977. * truncated log to 20 revs
  978. Revision 1.90 1999/10/26 12:30:46 peter
  979. * const parameter is now checked
  980. * better and generic check if a node can be used for assigning
  981. * export fixes
  982. * procvar equal works now (it never had worked at least from 0.99.8)
  983. * defcoll changed to linkedlist with pparaitem so it can easily be
  984. walked both directions
  985. Revision 1.89 1999/10/01 10:04:07 peter
  986. * fixed is_equal for proc -> procvar which didn't check the
  987. callconvention and type anymore since the splitting of procoptions
  988. Revision 1.88 1999/10/01 08:02:51 peter
  989. * forward type declaration rewritten
  990. Revision 1.87 1999/09/15 22:09:27 florian
  991. + rtti is now automatically generated for published classes, i.e.
  992. they are handled like an implicit property
  993. Revision 1.86 1999/09/11 09:08:35 florian
  994. * fixed bug 596
  995. * fixed some problems with procedure variables and procedures of object,
  996. especially in TP mode. Procedure of object doesn't apply only to classes,
  997. it is also allowed for objects !!
  998. Revision 1.85 1999/08/13 21:27:08 peter
  999. * more fixes for push_addr
  1000. Revision 1.84 1999/08/13 15:38:23 peter
  1001. * fixed push_addr_param for records < 4, the array high<low range check
  1002. broke this code.
  1003. Revision 1.83 1999/08/07 14:21:06 florian
  1004. * some small problems fixed
  1005. Revision 1.82 1999/08/07 13:36:56 daniel
  1006. * Recommitted the arraydef overflow bugfix.
  1007. Revision 1.80 1999/08/05 22:42:49 daniel
  1008. * Fixed potential bug for open arrays (Their size is not known at
  1009. compilation time).
  1010. Revision 1.79 1999/08/03 22:03:41 peter
  1011. * moved bitmask constants to sets
  1012. * some other type/const renamings
  1013. Revision 1.78 1999/07/30 12:26:42 peter
  1014. * array is_equal disabled for tp,delphi mode
  1015. Revision 1.77 1999/07/29 11:41:51 peter
  1016. * array is_equal extended
  1017. Revision 1.76 1999/07/27 23:39:15 peter
  1018. * open array checks also for s32bitdef, because u32bit also has a
  1019. high range of -1
  1020. }