types.pas 41 KB

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