types.pas 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  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. objects,cobjects,symtable,defs;
  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 void}
  42. function is_void(def : pdef) : boolean;
  43. { true if p is a smallset def }
  44. function is_smallset(p : pdef) : boolean;
  45. { returns true, if def defines a signed data type (only for ordinal types) }
  46. function is_signed(def : pdef) : boolean;
  47. {*****************************************************************************
  48. Array helper functions
  49. *****************************************************************************}
  50. { true, if p points to a zero based (non special like open or
  51. dynamic array def, mainly this is used to see if the array
  52. is convertable to a pointer }
  53. function is_zero_based_array(p : pdef) : boolean;
  54. { true if p points to an open array def }
  55. function is_open_array(p : pdef) : boolean;
  56. { true, if p points to an array of const def }
  57. function is_array_constructor(p : pdef) : boolean;
  58. { true, if p points to a variant array }
  59. function is_variant_array(p : pdef) : boolean;
  60. { true, if p points to an array of const }
  61. function is_array_of_const(p : pdef) : boolean;
  62. { true, if p points any kind of special array }
  63. function is_special_array(p : pdef) : boolean;
  64. { true if p is a char array def }
  65. function is_chararray(p : pdef) : boolean;
  66. {*****************************************************************************
  67. String helper functions
  68. *****************************************************************************}
  69. { true if p points to an open string def }
  70. function is_open_string(p : pdef) : boolean;
  71. { true if p is an ansi string def }
  72. function is_ansistring(p : pdef) : boolean;
  73. { true if p is a long string def }
  74. function is_longstring(p : pdef) : boolean;
  75. { true if p is a wide string def }
  76. function is_widestring(p : pdef) : boolean;
  77. { true if p is a short string def }
  78. function is_shortstring(p : pdef) : boolean;
  79. { true if p is a pchar def }
  80. function is_pchar(p : pdef) : boolean;
  81. { true if p is a voidpointer def }
  82. function is_voidpointer(p : pdef) : boolean;
  83. { returns true, if def uses FPU }
  84. function is_fpu(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(paralist1,paralist2:Pcollection;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(paralist1,paralist2:Pcollection;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. { returns true, if sym needs an entry in the proplist of a class rtti }
  118. function needs_prop_entry(sym : psym) : boolean;
  119. implementation
  120. uses strings,globtype,globals,htypechk,tree,verbose,symbols,symtablt;
  121. function needs_prop_entry(sym : psym) : boolean;
  122. begin
  123. needs_prop_entry:=(((typeof(sym^)=typeof(Tpropertysym)) and
  124. (sp_published in Ppropertysym(sym)^.objprop)) or
  125. (((typeof(sym^)=typeof(Tvarsym)) and
  126. (sp_published in Pvarsym(sym)^.objprop))));
  127. end;
  128. function equal_paras(paralist1,paralist2:Pcollection;
  129. value_equal_const:boolean):boolean;
  130. var def1,def2:Pparameter;
  131. i:word;
  132. begin
  133. equal_paras:=true;
  134. if paralist1^.count=paralist2^.count then
  135. for i:=1 to paralist1^.count do
  136. begin
  137. if (not is_equal(Pvarsym(def1^.data)^.definition,
  138. Pvarsym(def2^.data)^.definition)) or
  139. (def1^.paratyp<>def2^.paratyp) then
  140. begin
  141. if (not value_equal_const) or
  142. ((def1^.paratyp<>vs_var) and
  143. (def2^.paratyp<>vs_var)) then
  144. equal_paras:=false;
  145. break;
  146. end;
  147. end
  148. else
  149. equal_paras:=false;
  150. end;
  151. function convertable_paras(paralist1,paralist2:Pcollection;
  152. value_equal_const : boolean):boolean;
  153. var def1,def2:Pparameter;
  154. doconv:Tconverttype;
  155. i:word;
  156. begin
  157. convertable_paras:=true;
  158. if paralist1^.count=paralist2^.count then
  159. for i:=1 to paralist1^.count do
  160. begin
  161. if (isconvertable(Pvarsym(def1^.data)^.definition,
  162. Pvarsym(def2^.data)^.definition,
  163. doconv,callparan,false)=0) or
  164. (def1^.paratyp<>def2^.paratyp) then
  165. begin
  166. if (not value_equal_const) or
  167. ((def1^.paratyp<>vs_var) and
  168. (def2^.paratyp<>vs_var)) then
  169. convertable_paras:=false;
  170. break;
  171. end;
  172. end
  173. else
  174. convertable_paras:=false;
  175. end;
  176. { true if a function can be assigned to a procvar }
  177. function proc_to_procvar_equal(def1:pprocdef;def2:pprocvardef):boolean;
  178. const po_comp=po_compatibility_options-[po_methodpointer];
  179. var ismethod:boolean;
  180. begin
  181. proc_to_procvar_equal:=false;
  182. {!!!! This code should never be called with nil parameters. If you really
  183. want to check this, make it an internalerror instead of an exit!! (DM)
  184. if not(assigned(def1)) or not(assigned(def2)) then
  185. exit;}
  186. {Check for method pointer.}
  187. ismethod:=(def1^.owner<>nil) and
  188. (typeof(def1^.owner^)=typeof(Tobjectsymtable));
  189. if (ismethod and not (po_methodpointer in def2^.options)) or
  190. (not(ismethod) and (po_methodpointer in def2^.options)) then
  191. begin
  192. message(type_e_no_method_and_procedure_not_compatible);
  193. exit;
  194. end;
  195. { check return value and para's and options, methodpointer is already checked
  196. parameters may also be convertable }
  197. proc_to_procvar_equal:=is_equal(def1^.retdef,def2^.retdef) and
  198. (equal_paras(def1^.parameters,def2^.parameters,false) or
  199. convertable_paras(def1^.parameters,def2^.parameters,false)) and
  200. ((po_comp*def1^.options)=(po_comp*def2^.options));
  201. end;
  202. { returns true, if def uses FPU }
  203. function is_fpu(def : pdef) : boolean;
  204. begin
  205. is_fpu:=(typeof(def^)=typeof(Tfloatdef)) and (Pfloatdef(def)^.typ<>f32bit);
  206. end;
  207. { true if p is an ordinal }
  208. function is_ordinal(def : pdef) : boolean;
  209. var dt : tbasetype;
  210. begin
  211. if typeof(def^)=typeof(Torddef) then
  212. begin
  213. dt:=porddef(def)^.typ;
  214. is_ordinal:=dt in [uchar,
  215. u8bit,u16bit,u32bit,u64bit,
  216. s8bit,s16bit,s32bit,s64bit,
  217. bool8bit,bool16bit,bool32bit];
  218. end
  219. else
  220. is_ordinal:=typeof(def^)=typeof(Tenumdef);
  221. end;
  222. { returns the min. value of the type }
  223. function get_min_value(def:pdef) : longint;
  224. begin
  225. if typeof(def^)=typeof(Torddef) then
  226. get_min_value:=porddef(def)^.low.values
  227. else if typeof(def^)=typeof(Tenumdef) then
  228. get_min_value:=penumdef(def)^.minval
  229. else
  230. internalerror($00022701);
  231. end;
  232. { true if p is an integer }
  233. function is_integer(def : pdef) : boolean;
  234. begin
  235. is_integer:=(typeof(Tdef)=typeof(Torddef)) and
  236. (Porddef(def)^.typ in [uauto,u8bit,u16bit,u32bit,u64bit,
  237. s8bit,s16bit,s32bit,s64bit]);
  238. end;
  239. { true if p is a boolean }
  240. function is_boolean(def : pdef) : boolean;
  241. begin
  242. is_boolean:=(typeof(def^)=typeof(Torddef)) and
  243. (porddef(def)^.typ in [bool8bit,bool16bit,bool32bit]);
  244. end;
  245. { true if p is a void }
  246. function is_void(def : pdef) : boolean;
  247. begin
  248. is_void:=(typeof(def^)=typeof(Torddef)) and
  249. (porddef(def)^.typ=uvoid);
  250. end;
  251. { true if p is a char }
  252. function is_char(def : pdef):boolean;
  253. begin
  254. is_char:=(typeof(def^)=typeof(Torddef)) and
  255. (porddef(def)^.typ=uchar);
  256. end;
  257. { true if p is signed (integer) }
  258. function is_signed(def : pdef) : boolean;
  259. var dt:Tbasetype;
  260. begin
  261. if typeof(def^)=typeof(Torddef) then
  262. begin
  263. dt:=porddef(def)^.typ;
  264. is_signed:=(dt in [s8bit,s16bit,s32bit,s64bit]);
  265. end
  266. else
  267. is_signed:=false;
  268. end;
  269. { true, if p points to an open string def }
  270. function is_open_string(p:Pdef):boolean;
  271. begin
  272. is_open_string:=(typeof(p^)=typeof(Tstringdef)) and
  273. (pstringdef(p)^.string_typ=st_shortstring) and
  274. (pstringdef(p)^.len=0);
  275. end;
  276. { true, if p points to a zero based array def }
  277. function is_zero_based_array(p : pdef) : boolean;
  278. begin
  279. is_zero_based_array:=(typeof(p^)=typeof(Tarraydef)) and
  280. (parraydef(p)^.lowrange.values=0) and
  281. not(is_special_array(p));
  282. end;
  283. { true, if p points to an open array def }
  284. function is_open_array(p : pdef) : boolean;
  285. begin
  286. is_open_array:=(typeof(p^)=typeof(Tarraydef)) and
  287. (parraydef(p)^.lowrange.values=0) and
  288. (Parraydef(p)^.highrange.signed) and
  289. (parraydef(p)^.highrange.values=-1) and
  290. not(ap_constructor in Parraydef(p)^.options) and
  291. not(ap_variant in Parraydef(p)^.options) and
  292. not(ap_arrayofconst in Parraydef(p)^.options);
  293. end;
  294. { true, if p points to an array of const def }
  295. function is_array_constructor(p : pdef) : boolean;
  296. begin
  297. is_array_constructor:=(typeof(p^)=typeof(Tarraydef)) and
  298. (ap_constructor in Parraydef(p)^.options);
  299. end;
  300. { true, if p points to a variant array }
  301. function is_variant_array(p : pdef) : boolean;
  302. begin
  303. is_variant_array:=(typeof(p^)=typeof(Tarraydef)) and
  304. (ap_variant in Parraydef(p)^.options);
  305. end;
  306. { true, if p points to an array of const }
  307. function is_array_of_const(p : pdef) : boolean;
  308. begin
  309. is_array_of_const:=(typeof(p^)=typeof(Tarraydef)) and
  310. (ap_arrayofconst in Parraydef(p)^.options);
  311. end;
  312. { true, if p points to a special array }
  313. function is_special_array(p : pdef) : boolean;
  314. begin
  315. is_special_array:=(typeof(p^)=typeof(Tarraydef)) and
  316. ((ap_variant in Parraydef(p)^.options) or
  317. (ap_arrayofconst in Parraydef(p)^.options) or
  318. (ap_constructor in Parraydef(p)^.options) or
  319. is_open_array(p)
  320. );
  321. end;
  322. { true if p is an ansi string def }
  323. function is_ansistring(p : pdef) : boolean;
  324. begin
  325. is_ansistring:=(typeof(p^)=typeof(Tstringdef)) and
  326. (pstringdef(p)^.string_typ=st_ansistring);
  327. end;
  328. { true if p is an long string def }
  329. function is_longstring(p : pdef) : boolean;
  330. begin
  331. is_longstring:=(typeof(p^)=typeof(Tstringdef)) and
  332. (pstringdef(p)^.string_typ=st_longstring);
  333. end;
  334. { true if p is an wide string def }
  335. function is_widestring(p : pdef) : boolean;
  336. begin
  337. is_widestring:=(typeof(p^)=typeof(Tstringdef)) and
  338. (pstringdef(p)^.string_typ=st_widestring);
  339. end;
  340. { true if p is an short string def }
  341. function is_shortstring(p : pdef) : boolean;
  342. begin
  343. is_shortstring:=(typeof(p^)=typeof(Tstringdef)) and
  344. (pstringdef(p)^.string_typ=st_shortstring);
  345. end;
  346. { true if p is a char array def }
  347. function is_chararray(p : pdef) : boolean;
  348. begin
  349. is_chararray:=(typeof(p^)=typeof(Tarraydef)) and
  350. is_equal(parraydef(p)^.definition,cchardef) and
  351. not(is_special_array(p));
  352. end;
  353. { true if p is a pchar def }
  354. function is_pchar(p : pdef) : boolean;
  355. begin
  356. is_pchar:=(typeof(p^)=typeof(Tpointerdef)) and
  357. is_equal(Ppointerdef(p)^.definition,cchardef);
  358. end;
  359. { true if p is a voidpointer def }
  360. function is_voidpointer(p : pdef) : boolean;
  361. begin
  362. is_voidpointer:=(typeof(p^)=typeof(Tpointerdef)) and
  363. is_equal(Ppointerdef(p)^.definition,voiddef);
  364. end;
  365. { true if p is a smallset def }
  366. function is_smallset(p : pdef) : boolean;
  367. begin
  368. is_smallset:=(typeof(p^)=typeof(Tsetdef)) and
  369. (psetdef(p)^.settype=smallset);
  370. end;
  371. { true, if def is a 64 bit int type }
  372. function is_64bitint(def : pdef) : boolean;
  373. begin
  374. is_64bitint:=(typeof(def^)=typeof(Torddef)) and
  375. (porddef(def)^.typ in [u64bit,s64bit])
  376. end;
  377. function push_high_param(def : pdef) : boolean;
  378. begin
  379. push_high_param:=is_open_array(def) or
  380. is_open_string(def) or
  381. is_array_of_const(def);
  382. end;
  383. { true if a parameter is too large to copy and only the address is pushed }
  384. function push_addr_param(def : pdef) : boolean;
  385. var r:boolean;
  386. begin
  387. push_addr_param:=false;
  388. if never_copy_const_param then
  389. push_addr_param:=true
  390. else
  391. begin
  392. if typeof(def^)=typeof(Tformaldef) then
  393. push_addr_param:=true
  394. else if typeof(def^)=typeof(Trecorddef) then
  395. push_addr_param:=(def^.size>4)
  396. else if typeof(def^)=typeof(Tarraydef) then
  397. begin
  398. r:=is_open_array(def) or is_array_of_const(def) or
  399. is_array_constructor(def);
  400. if Parraydef(def)^.highrange.signed then
  401. r:=r or ((Parraydef(def)^.highrange.values>
  402. Parraydef(def)^.lowrange.values) and (def^.size>4))
  403. else
  404. r:=r or ((Parraydef(def)^.highrange.valueu>
  405. Parraydef(def)^.lowrange.valueu) and (def^.size>4));
  406. end
  407. else if typeof(def^)=typeof(Tobjectdef) then
  408. push_addr_param:=not (oo_is_class in Pobjectdef(def)^.options)
  409. else if typeof(def^)=typeof(Tstringdef) then
  410. push_addr_param:=pstringdef(def)^.string_typ in [st_shortstring,st_longstring]
  411. else if typeof(def^)=typeof(Tprocvardef) then
  412. push_addr_param:=(po_methodpointer in pprocvardef(def)^.options)
  413. else if typeof(def^)=typeof(Tsetdef) then
  414. push_addr_param:=(psetdef(def)^.settype<>smallset);
  415. end;
  416. end;
  417. { test if l is in the range of def, outputs error if out of range }
  418. procedure testrange(def : pdef;var l:longint);
  419. var lsv,hsv:longint;
  420. {$IFDEF TP}
  421. luv:longint absolute lsv;
  422. huv:longint absolute hsv;
  423. {$ELSE}
  424. luv:cardinal absolute lsv;
  425. huv:cardinal absolute hsv;
  426. {$ENDIF TP}
  427. begin
  428. { for 64 bit types we need only to check if it is less than }
  429. { zero, if def is a qword node }
  430. if is_64bitint(def) then
  431. begin
  432. if (l<0) and (porddef(def)^.typ=u64bit) then
  433. begin
  434. l:=0;
  435. if (cs_check_range in aktlocalswitches) then
  436. Message(parser_e_range_check_error)
  437. else
  438. Message(parser_w_range_check_error);
  439. end;
  440. end
  441. else
  442. begin
  443. getrange(def,lsv,hsv);
  444. if (typeof(def^)=typeof(Torddef)) and
  445. (porddef(def)^.typ=u32bit) then
  446. begin
  447. if (l<luv) or (l>huv) then
  448. begin
  449. if (cs_check_range in aktlocalswitches) then
  450. Message(parser_e_range_check_error)
  451. else
  452. Message(parser_w_range_check_error);
  453. end;
  454. end
  455. else if (l<lsv) or (l>hsv) then
  456. begin
  457. if (typeof(def^)=typeof(Tenumdef)) or
  458. (cs_check_range in aktlocalswitches) then
  459. Message(parser_e_range_check_error)
  460. else
  461. Message(parser_w_range_check_error);
  462. { Fix the value to fit in the allocated space for this type of variable }
  463. case def^.size of
  464. 1: l := l and $ff;
  465. 2: l := l and $ffff;
  466. end
  467. end;
  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. {Needs fixing for u32bit; low.signed etc....}
  473. begin
  474. if typeof(def^)=typeof(Torddef) then
  475. begin
  476. l:=porddef(def)^.low.values;
  477. h:=porddef(def)^.high.values;
  478. end
  479. else if typeof(def^)=typeof(Tenumdef) then
  480. begin
  481. l:=penumdef(def)^.minval;
  482. h:=penumdef(def)^.maxval;
  483. end
  484. else if typeof(def^)=typeof(Tarraydef) then
  485. begin
  486. l:=parraydef(def)^.lowrange.values;
  487. h:=parraydef(def)^.highrange.values;
  488. end
  489. else
  490. internalerror(987);
  491. end;
  492. function mmx_type(p : pdef) : tmmxtype;
  493. begin
  494. mmx_type:=mmxno;
  495. if is_mmx_able_array(p) then
  496. begin
  497. if typeof((Parraydef(p)^.definition^))=typeof(Tfloatdef) then
  498. case pfloatdef(parraydef(p)^.definition)^.typ of
  499. s32real:
  500. mmx_type:=mmxsingle;
  501. f16bit:
  502. mmx_type:=mmxfixed16
  503. end
  504. else
  505. case porddef(parraydef(p)^.definition)^.typ of
  506. u8bit:
  507. mmx_type:=mmxu8bit;
  508. s8bit:
  509. mmx_type:=mmxs8bit;
  510. u16bit:
  511. mmx_type:=mmxu16bit;
  512. s16bit:
  513. mmx_type:=mmxs16bit;
  514. u32bit:
  515. mmx_type:=mmxu32bit;
  516. s32bit:
  517. mmx_type:=mmxs32bit;
  518. end;
  519. end;
  520. end;
  521. function is_mmx_able_array(p : pdef) : boolean;
  522. begin
  523. {$ifdef SUPPORT_MMX}
  524. if (cs_mmx_saturation in aktlocalswitches) then
  525. begin
  526. is_mmx_able_array:=(p^.deftype=arraydef) and
  527. not(is_special_array(p)) and
  528. (
  529. (
  530. (parraydef(p)^.elementtype.def^.deftype=orddef) and
  531. (
  532. (
  533. (parraydef(p)^.lowrange=0) and
  534. (parraydef(p)^.highrange=1) and
  535. (porddef(parraydef(p)^.elementtype.def)^.typ in [u32bit,s32bit])
  536. )
  537. or
  538. (
  539. (parraydef(p)^.lowrange=0) and
  540. (parraydef(p)^.highrange=3) and
  541. (porddef(parraydef(p)^.elementtype.def)^.typ in [u16bit,s16bit])
  542. )
  543. )
  544. )
  545. or
  546. (
  547. (
  548. (parraydef(p)^.elementtype.def^.deftype=floatdef) and
  549. (
  550. (parraydef(p)^.lowrange=0) and
  551. (parraydef(p)^.highrange=3) and
  552. (pfloatdef(parraydef(p)^.elementtype.def)^.typ=f16bit)
  553. ) or
  554. (
  555. (parraydef(p)^.lowrange=0) and
  556. (parraydef(p)^.highrange=1) and
  557. (pfloatdef(parraydef(p)^.elementtype.def)^.typ=s32real)
  558. )
  559. )
  560. )
  561. );
  562. end
  563. else
  564. begin
  565. is_mmx_able_array:=(p^.deftype=arraydef) and
  566. (
  567. (
  568. (parraydef(p)^.elementtype.def^.deftype=orddef) and
  569. (
  570. (
  571. (parraydef(p)^.lowrange=0) and
  572. (parraydef(p)^.highrange=1) and
  573. (porddef(parraydef(p)^.elementtype.def)^.typ in [u32bit,s32bit])
  574. )
  575. or
  576. (
  577. (parraydef(p)^.lowrange=0) and
  578. (parraydef(p)^.highrange=3) and
  579. (porddef(parraydef(p)^.elementtype.def)^.typ in [u16bit,s16bit])
  580. )
  581. or
  582. (
  583. (parraydef(p)^.lowrange=0) and
  584. (parraydef(p)^.highrange=7) and
  585. (porddef(parraydef(p)^.elementtype.def)^.typ in [u8bit,s8bit])
  586. )
  587. )
  588. )
  589. or
  590. (
  591. (parraydef(p)^.elementtype.def^.deftype=floatdef) and
  592. (
  593. (
  594. (parraydef(p)^.lowrange=0) and
  595. (parraydef(p)^.highrange=3) and
  596. (pfloatdef(parraydef(p)^.elementtype.def)^.typ=f32bit)
  597. )
  598. or
  599. (
  600. (parraydef(p)^.lowrange=0) and
  601. (parraydef(p)^.highrange=1) and
  602. (pfloatdef(parraydef(p)^.elementtype.def)^.typ=s32real)
  603. )
  604. )
  605. )
  606. );
  607. end;
  608. {$else SUPPORT_MMX}
  609. is_mmx_able_array:=false;
  610. {$endif SUPPORT_MMX}
  611. end;
  612. function is_equal(def1,def2 : pdef) : boolean;
  613. var b : boolean;
  614. hd : pdef;
  615. d1type,d2type:pointer;
  616. begin
  617. {!!!! This code should never be called with nil parameters. If you really
  618. want to check this, make it an internalerror instead of an exit!! (DM)
  619. if not (assigned(def1) and assigned(def2)) then
  620. begin
  621. is_equal:=false;
  622. exit;
  623. end;}
  624. { be sure, that if there is a stringdef, that this is def1 }
  625. if typeof(def2^)=typeof(Tstringdef) then
  626. begin
  627. hd:=def1;
  628. def1:=def2;
  629. def2:=hd;
  630. end;
  631. b:=false;
  632. d1type:=typeof(def1^);
  633. d2type:=typeof(def2^);
  634. { both point to the same definition ? }
  635. if def1=def2 then
  636. b:=true
  637. else
  638. { pointer with an equal definition are equal }
  639. if (d1type=typeof(Tpointerdef)) and (d1type=d2type) then
  640. begin
  641. { here a problem detected in tabsolutesym }
  642. { the types can be forward type !! }
  643. if assigned(def1^.sym) and
  644. (typeof((Ppointerdef(def1)^.definition^))=typeof(Tforwarddef)) then
  645. b:=(def1^.sym=def2^.sym)
  646. else
  647. b:=ppointerdef(def1)^.definition=ppointerdef(def2)^.definition;
  648. end
  649. else
  650. { ordinals are equal only when the ordinal type is equal }
  651. if (d1type=typeof(Torddef)) and (d1type=d2type) then
  652. begin
  653. case porddef(def1)^.typ of
  654. u8bit,u16bit,u32bit,
  655. s8bit,s16bit,s32bit:
  656. b:=((porddef(def1)^.typ=porddef(def2)^.typ) and
  657. (porddef(def1)^.low.values=porddef(def2)^.low.values) and
  658. (porddef(def1)^.high.values=porddef(def2)^.high.values));
  659. uvoid,uchar,
  660. bool8bit,bool16bit,bool32bit:
  661. b:=(porddef(def1)^.typ=porddef(def2)^.typ);
  662. end;
  663. end
  664. else
  665. if (d1type=typeof(Tfloatdef)) and (d1type=d2type) then
  666. b:=pfloatdef(def1)^.typ=pfloatdef(def2)^.typ
  667. else
  668. { strings with the same length are equal }
  669. if (d1type=typeof(Tstringdef)) and (d1type=d2type) and
  670. (pstringdef(def1)^.string_typ=pstringdef(def2)^.string_typ) then
  671. begin
  672. b:=not(is_shortstring(def1)) or
  673. (pstringdef(def1)^.len=pstringdef(def2)^.len);
  674. end
  675. else
  676. if (d1type=typeof(Tformaldef)) and (d1type=d2type) then
  677. b:=true
  678. { file types with the same file element type are equal }
  679. { this is a problem for assign !! }
  680. { changed to allow if one is untyped }
  681. { all typed files are equal to the special }
  682. { typed file that has voiddef as elemnt type }
  683. { but must NOT match for text file !!! }
  684. else
  685. if (d1type=typeof(Tfiledef)) and (d1type=d2type) then
  686. b:=(pfiledef(def1)^.filetype=pfiledef(def2)^.filetype) and
  687. ((
  688. ((pfiledef(def1)^.definition=nil) and
  689. (pfiledef(def2)^.definition=nil)) or
  690. (
  691. (pfiledef(def1)^.definition<>nil) and
  692. (pfiledef(def2)^.definition<>nil) and
  693. is_equal(pfiledef(def1)^.definition,pfiledef(def2)^.definition)
  694. ) or
  695. ( (pfiledef(def1)^.definition=pdef(voiddef)) or
  696. (pfiledef(def2)^.definition=pdef(voiddef))
  697. )))
  698. { sets with the same element type are equal }
  699. else
  700. if (d1type=typeof(Tsetdef)) and (d1type=d2type) then
  701. begin
  702. if assigned(psetdef(def1)^.definition) and
  703. assigned(psetdef(def2)^.definition) then
  704. b:=(typeof((psetdef(def1)^.definition^))=
  705. typeof((psetdef(def2)^.definition^)))
  706. else
  707. b:=true;
  708. end
  709. else
  710. if (d1type=typeof(Tprocvardef)) and (d1type=d2type) then
  711. begin
  712. { poassembler isn't important for compatibility }
  713. { if a method is assigned to a methodpointer }
  714. { is checked before }
  715. b:=(pprocvardef(def1)^.options=pprocvardef(def2)^.options) and
  716. (pprocvardef(def1)^.calloptions=pprocvardef(def2)^.calloptions) and
  717. ((pprocvardef(def1)^.options*po_compatibility_options)=
  718. (pprocvardef(def2)^.options*po_compatibility_options)) and
  719. is_equal(pprocvardef(def1)^.retdef,pprocvardef(def2)^.retdef) and
  720. equal_paras(pprocvardef(def1)^.parameters,pprocvardef(def2)^.parameters,false);
  721. end
  722. else
  723. if (d1type=typeof(Tarraydef)) and (d1type=d2type) then
  724. begin
  725. if is_open_array(def1) or is_open_array(def2) or
  726. is_array_of_const(def1) or is_array_of_const(def2) then
  727. begin
  728. if (ap_arrayofconst in parraydef(def1)^.options) or
  729. (ap_arrayofconst in parraydef(def2)^.options) then
  730. b:=true
  731. else
  732. b:=is_equal(parraydef(def1)^.definition,parraydef(def2)^.definition);
  733. end
  734. else
  735. begin
  736. b:=not(m_tp in aktmodeswitches) and
  737. not(m_delphi in aktmodeswitches) and
  738. (parraydef(def1)^.lowrange.values=parraydef(def2)^.lowrange.values) and
  739. (parraydef(def1)^.highrange.values=parraydef(def2)^.highrange.values) and
  740. is_equal(parraydef(def1)^.definition,parraydef(def2)^.definition) and
  741. is_equal(parraydef(def1)^.rangedef,parraydef(def2)^.rangedef);
  742. end;
  743. end
  744. else
  745. if (d1type=typeof(Tclassrefdef)) and (d1type=d2type) then
  746. begin
  747. {Similar to pointerdef:}
  748. if (def1^.sym<>nil) and (typeof((pclassrefdef(def1)^.definition^))=
  749. typeof(Tforwarddef)) then
  750. b:=(def1^.sym=def2^.sym)
  751. else
  752. b:=is_equal(pclassrefdef(def1)^.definition,pclassrefdef(def2)^.definition);
  753. end;
  754. is_equal:=b;
  755. end;
  756. function is_subequal(def1, def2: pdef): boolean;
  757. begin
  758. is_subequal := false;
  759. if (typeof(def1^)=typeof(Torddef)) and (typeof(def2^)=typeof(Torddef)) then
  760. { see p.47 of Turbo Pascal 7.01 manual for the separation of types }
  761. { range checking for case statements is done with testrange }
  762. case porddef(def1)^.typ of
  763. u8bit,u16bit,u32bit,
  764. s8bit,s16bit,s32bit:
  765. is_subequal:=(porddef(def2)^.typ in
  766. [s32bit,u32bit,u8bit,s8bit,s16bit,u16bit]);
  767. bool8bit,bool16bit,bool32bit :
  768. is_subequal:=(porddef(def2)^.typ in
  769. [bool8bit,bool16bit,bool32bit]);
  770. uchar:
  771. is_subequal:=(porddef(def2)^.typ=uchar);
  772. end
  773. else
  774. { I assume that both enumerations are equal when the first }
  775. { pointers are equal. }
  776. if (typeof(def1^)=typeof(Tenumdef)) and (typeof(def2^)=typeof(Tenumdef)) then
  777. Begin
  778. if penumdef(def1)^.symbols=penumdef(def2)^.symbols then
  779. is_subequal := TRUE;
  780. end;
  781. end;
  782. function CheckTypes(def1,def2 : pdef) : boolean;
  783. var
  784. s1,s2 : string;
  785. begin
  786. if not is_equal(def1,def2) then
  787. begin
  788. s1:=def1^.typename;
  789. s2:=def2^.typename;
  790. if (s1<>'<unknown type>') and (s2<>'<unknown type>') then
  791. Message2(type_e_not_equal_types,s1,s2)
  792. else
  793. Message(type_e_mismatch);
  794. CheckTypes:=false;
  795. end
  796. else
  797. CheckTypes:=true;
  798. end;
  799. end.
  800. {
  801. $Log$
  802. Revision 1.1 2000-07-13 06:30:14 michael
  803. + Initial import
  804. Revision 1.2 2000/03/16 12:52:48 daniel
  805. * Changed names of procedures flags
  806. * Changed VMT generation
  807. Revision 1.1 2000/02/28 17:23:58 daniel
  808. * Current work of symtable integration committed. The symtable can be
  809. activated by defining 'newst', but doesn't compile yet. Changes in type
  810. checking and oop are completed. What is left is to write a new
  811. symtablestack and adapt the parser to use it.
  812. Revision 1.97 2000/02/09 13:23:09 peter
  813. * log truncated
  814. Revision 1.96 2000/02/01 09:44:03 peter
  815. * is_voidpointer
  816. Revision 1.95 2000/01/07 01:14:49 peter
  817. * updated copyright to 2000
  818. Revision 1.94 2000/01/04 16:35:58 jonas
  819. * when range checking is off, constants that are out of bound are no longer
  820. truncated to their max/min legal value but left alone (jsut an "and" is done to
  821. make sure they fit in the allocated space if necessary)
  822. Revision 1.93 1999/12/31 14:26:28 peter
  823. * fixed crash with empty array constructors
  824. Revision 1.92 1999/11/30 10:40:59 peter
  825. + ttype, tsymlist
  826. Revision 1.91 1999/11/06 14:34:31 peter
  827. * truncated log to 20 revs
  828. Revision 1.90 1999/10/26 12:30:46 peter
  829. * const parameter is now checked
  830. * better and generic check if a node can be used for assigning
  831. * export fixes
  832. * procvar equal works now (it never had worked at least from 0.99.8)
  833. * defcoll changed to linkedlist with pparaitem so it can easily be
  834. walked both directions
  835. Revision 1.89 1999/10/01 10:04:07 peter
  836. * fixed is_equal for proc -> procvar which didn't check the
  837. callconvention and type anymore since the splitting of procoptions
  838. Revision 1.88 1999/10/01 08:02:51 peter
  839. * forward type declaration rewritten
  840. Revision 1.87 1999/09/15 22:09:27 florian
  841. + rtti is now automatically generated for published classes, i.e.
  842. they are handled like an implicit property
  843. Revision 1.86 1999/09/11 09:08:35 florian
  844. * fixed bug 596
  845. * fixed some problems with procedure variables and procedures of object,
  846. especially in TP mode. Procedure of object doesn't apply only to classes,
  847. it is also allowed for objects !!
  848. Revision 1.85 1999/08/13 21:27:08 peter
  849. * more fixes for push_addr
  850. Revision 1.84 1999/08/13 15:38:23 peter
  851. * fixed push_addr_param for records < 4, the array high<low range check
  852. broke this code.
  853. Revision 1.83 1999/08/07 14:21:06 florian
  854. * some small problems fixed
  855. Revision 1.82 1999/08/07 13:36:56 daniel
  856. * Recommitted the arraydef overflow bugfix.
  857. Revision 1.80 1999/08/05 22:42:49 daniel
  858. * Fixed potential bug for open arrays (Their size is not known at
  859. compilation time).
  860. Revision 1.79 1999/08/03 22:03:41 peter
  861. * moved bitmask constants to sets
  862. * some other type/const renamings
  863. Revision 1.78 1999/07/30 12:26:42 peter
  864. * array is_equal disabled for tp,delphi mode
  865. Revision 1.77 1999/07/29 11:41:51 peter
  866. * array is_equal extended
  867. Revision 1.76 1999/07/27 23:39:15 peter
  868. * open array checks also for s32bitdef, because u32bit also has a
  869. high range of -1
  870. }