types.pas 40 KB

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