types.pas 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717
  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. {$i defines.inc}
  20. interface
  21. uses
  22. cobjects,
  23. cpuinfo,
  24. {$ifdef CG11}
  25. node,
  26. {$endif}
  27. symtable;
  28. type
  29. tmmxtype = (mmxno,mmxu8bit,mmxs8bit,mmxu16bit,mmxs16bit,
  30. mmxu32bit,mmxs32bit,mmxfixed16,mmxsingle);
  31. const
  32. { true if we must never copy this parameter }
  33. never_copy_const_param : boolean = false;
  34. {*****************************************************************************
  35. Basic type functions
  36. *****************************************************************************}
  37. { returns true, if def defines an ordinal type }
  38. function is_ordinal(def : pdef) : boolean;
  39. { returns the min. value of the type }
  40. function get_min_value(def : pdef) : longint;
  41. { returns true, if def defines an ordinal type }
  42. function is_integer(def : pdef) : boolean;
  43. { true if p is a boolean }
  44. function is_boolean(def : pdef) : boolean;
  45. { true if p is a char }
  46. function is_char(def : pdef) : boolean;
  47. { true if p is a void}
  48. function is_void(def : pdef) : boolean;
  49. { true if p is a smallset def }
  50. function is_smallset(p : pdef) : boolean;
  51. { returns true, if def defines a signed data type (only for ordinal types) }
  52. function is_signed(def : pdef) : boolean;
  53. {*****************************************************************************
  54. Array helper functions
  55. *****************************************************************************}
  56. { true, if p points to a zero based (non special like open or
  57. dynamic array def, mainly this is used to see if the array
  58. is convertable to a pointer }
  59. function is_zero_based_array(p : pdef) : boolean;
  60. { true if p points to an open array def }
  61. function is_open_array(p : pdef) : boolean;
  62. { true, if p points to an array of const def }
  63. function is_array_constructor(p : pdef) : boolean;
  64. { true, if p points to a variant array }
  65. function is_variant_array(p : pdef) : boolean;
  66. { true, if p points to an array of const }
  67. function is_array_of_const(p : pdef) : boolean;
  68. { true, if p points any kind of special array }
  69. function is_special_array(p : pdef) : boolean;
  70. { true if p is a char array def }
  71. function is_chararray(p : pdef) : boolean;
  72. {*****************************************************************************
  73. String helper functions
  74. *****************************************************************************}
  75. { true if p points to an open string def }
  76. function is_open_string(p : pdef) : boolean;
  77. { true if p is an ansi string def }
  78. function is_ansistring(p : pdef) : boolean;
  79. { true if p is a long string def }
  80. function is_longstring(p : pdef) : boolean;
  81. { true if p is a wide string def }
  82. function is_widestring(p : pdef) : boolean;
  83. { true if p is a short string def }
  84. function is_shortstring(p : pdef) : boolean;
  85. { true if p is a pchar def }
  86. function is_pchar(p : pdef) : boolean;
  87. { true if p is a voidpointer def }
  88. function is_voidpointer(p : pdef) : boolean;
  89. { returns true, if def uses FPU }
  90. function is_fpu(def : pdef) : boolean;
  91. { true if the return value is in EAX }
  92. function ret_in_acc(def : pdef) : boolean;
  93. { true if uses a parameter as return value }
  94. function ret_in_param(def : pdef) : boolean;
  95. { true, if def is a 64 bit int type }
  96. function is_64bitint(def : pdef) : boolean;
  97. function push_high_param(def : pdef) : boolean;
  98. { true if a parameter is too large to copy and only the address is pushed }
  99. function push_addr_param(def : pdef) : boolean;
  100. { true, if def1 and def2 are semantical the same }
  101. function is_equal(def1,def2 : pdef) : boolean;
  102. { checks for type compatibility (subgroups of type) }
  103. { used for case statements... probably missing stuff }
  104. { to use on other types }
  105. function is_subequal(def1, def2: pdef): boolean;
  106. {$ifdef CG11}
  107. type
  108. tconverttype = (
  109. tc_equal,
  110. tc_not_possible,
  111. tc_string_2_string,
  112. tc_char_2_string,
  113. tc_pchar_2_string,
  114. tc_cchar_2_pchar,
  115. tc_cstring_2_pchar,
  116. tc_ansistring_2_pchar,
  117. tc_string_2_chararray,
  118. tc_chararray_2_string,
  119. tc_array_2_pointer,
  120. tc_pointer_2_array,
  121. tc_int_2_int,
  122. tc_int_2_bool,
  123. tc_bool_2_bool,
  124. tc_bool_2_int,
  125. tc_real_2_real,
  126. tc_int_2_real,
  127. tc_int_2_fix,
  128. tc_real_2_fix,
  129. tc_fix_2_real,
  130. tc_proc_2_procvar,
  131. tc_arrayconstructor_2_set,
  132. tc_load_smallset,
  133. tc_cord_2_pointer
  134. );
  135. function assignment_overloaded(from_def,to_def : pdef) : pprocdef;
  136. { Returns:
  137. 0 - Not convertable
  138. 1 - Convertable
  139. 2 - Convertable, but not first choice }
  140. function isconvertable(def_from,def_to : pdef;
  141. var doconv : tconverttype;fromtreetype : tnodetype;
  142. explicit : boolean) : byte;
  143. {$endif CG11}
  144. { same as is_equal, but with error message if failed }
  145. function CheckTypes(def1,def2 : pdef) : boolean;
  146. function equal_constsym(sym1,sym2:pconstsym):boolean;
  147. { true, if two parameter lists are equal }
  148. { if acp is cp_none, all have to match exactly }
  149. { if acp is cp_value_equal_const call by value }
  150. { and call by const parameter are assumed as }
  151. { equal }
  152. { if acp is cp_all the var const or nothing are considered equal }
  153. type
  154. compare_type = ( cp_none, cp_value_equal_const, cp_all);
  155. function equal_paras(paralist1,paralist2 : plinkedlist; acp : compare_type) : boolean;
  156. { true if a type can be allowed for another one
  157. in a func var }
  158. function convertable_paras(paralist1,paralist2 : plinkedlist; acp : compare_type) : boolean;
  159. { true if a function can be assigned to a procvar }
  160. function proc_to_procvar_equal(def1:pprocdef;def2:pprocvardef) : boolean;
  161. { if l isn't in the range of def a range check error is generated and
  162. the value is placed within the range }
  163. procedure testrange(def : pdef;var l : tconstexprint);
  164. { returns the range of def }
  165. procedure getrange(def : pdef;var l : longint;var h : longint);
  166. { some type helper routines for MMX support }
  167. function is_mmx_able_array(p : pdef) : boolean;
  168. { returns the mmx type }
  169. function mmx_type(p : pdef) : tmmxtype;
  170. { returns true, if sym needs an entry in the proplist of a class rtti }
  171. function needs_prop_entry(sym : psym) : boolean;
  172. { returns true, if p contains data which needs init/final code }
  173. function needs_init_final(p : psymtable) : boolean;
  174. implementation
  175. uses
  176. globtype,globals,
  177. {$ifndef CG11}
  178. htypechk,
  179. tree,
  180. {$endif}
  181. verbose,symconst,tokens;
  182. var
  183. b_needs_init_final : boolean;
  184. procedure _needs_init_final(p : pnamedindexobject);
  185. begin
  186. if (psym(p)^.typ=varsym) and
  187. assigned(pvarsym(p)^.vartype.def) and
  188. not((pvarsym(p)^.vartype.def^.deftype=objectdef) and
  189. pobjectdef(pvarsym(p)^.vartype.def)^.is_class) and
  190. pvarsym(p)^.vartype.def^.needs_inittable then
  191. b_needs_init_final:=true;
  192. end;
  193. { returns true, if p contains data which needs init/final code }
  194. function needs_init_final(p : psymtable) : boolean;
  195. begin
  196. b_needs_init_final:=false;
  197. p^.foreach({$ifdef FPCPROCVAR}@{$endif}_needs_init_final);
  198. needs_init_final:=b_needs_init_final;
  199. end;
  200. function needs_prop_entry(sym : psym) : boolean;
  201. begin
  202. needs_prop_entry:=(sp_published in psym(sym)^.symoptions) and
  203. (sym^.typ in [propertysym,varsym]);
  204. end;
  205. function equal_constsym(sym1,sym2:pconstsym):boolean;
  206. var
  207. p1,p2,pend : pchar;
  208. begin
  209. equal_constsym:=false;
  210. if sym1^.consttyp<>sym2^.consttyp then
  211. exit;
  212. case sym1^.consttyp of
  213. constint,
  214. constbool,
  215. constchar,
  216. constpointer,
  217. constord :
  218. equal_constsym:=(sym1^.value=sym2^.value);
  219. conststring,constresourcestring :
  220. begin
  221. if sym1^.len=sym2^.len then
  222. begin
  223. p1:=pchar(tpointerord(sym1^.value));
  224. p2:=pchar(tpointerord(sym2^.value));
  225. pend:=p1+sym1^.len;
  226. while (p1<pend) do
  227. begin
  228. if p1^<>p2^ then
  229. break;
  230. inc(p1);
  231. inc(p2);
  232. end;
  233. if (p1=pend) then
  234. equal_constsym:=true;
  235. end;
  236. end;
  237. constreal :
  238. equal_constsym:=(pbestreal(tpointerord(sym1^.value))^=pbestreal(tpointerord(sym2^.value))^);
  239. constset :
  240. equal_constsym:=(pnormalset(tpointerord(sym1^.value))^=pnormalset(tpointerord(sym2^.value))^);
  241. constnil :
  242. equal_constsym:=true;
  243. end;
  244. end;
  245. { compare_type = ( cp_none, cp_value_equal_const, cp_all); }
  246. function equal_paras(paralist1,paralist2 : plinkedlist; acp : compare_type) : boolean;
  247. var
  248. def1,def2 : pparaitem;
  249. begin
  250. def1:=pparaitem(paralist1^.first);
  251. def2:=pparaitem(paralist2^.first);
  252. while (assigned(def1)) and (assigned(def2)) do
  253. begin
  254. case acp of
  255. cp_value_equal_const :
  256. begin
  257. if not(is_equal(def1^.paratype.def,def2^.paratype.def)) or
  258. ((def1^.paratyp<>def2^.paratyp) and
  259. ((def1^.paratyp in [vs_var,vs_out]) or
  260. (def2^.paratyp in [vs_var,vs_out])
  261. )
  262. ) then
  263. begin
  264. equal_paras:=false;
  265. exit;
  266. end;
  267. end;
  268. cp_all :
  269. begin
  270. if not(is_equal(def1^.paratype.def,def2^.paratype.def)) or
  271. (def1^.paratyp<>def2^.paratyp) then
  272. begin
  273. equal_paras:=false;
  274. exit;
  275. end;
  276. end;
  277. cp_none :
  278. begin
  279. if not(is_equal(def1^.paratype.def,def2^.paratype.def)) then
  280. begin
  281. equal_paras:=false;
  282. exit;
  283. end;
  284. { also check default value if both have it declared }
  285. if assigned(def1^.defaultvalue) and
  286. assigned(def2^.defaultvalue) then
  287. begin
  288. if not equal_constsym(pconstsym(def1^.defaultvalue),pconstsym(def2^.defaultvalue)) then
  289. begin
  290. equal_paras:=false;
  291. exit;
  292. end;
  293. end;
  294. end;
  295. end;
  296. def1:=pparaitem(def1^.next);
  297. def2:=pparaitem(def2^.next);
  298. end;
  299. if (def1=nil) and (def2=nil) then
  300. equal_paras:=true
  301. else
  302. equal_paras:=false;
  303. end;
  304. function convertable_paras(paralist1,paralist2 : plinkedlist;acp : compare_type) : boolean;
  305. var
  306. def1,def2 : pparaitem;
  307. doconv : tconverttype;
  308. begin
  309. def1:=pparaitem(paralist1^.first);
  310. def2:=pparaitem(paralist2^.first);
  311. while (assigned(def1)) and (assigned(def2)) do
  312. begin
  313. case acp of
  314. cp_value_equal_const :
  315. begin
  316. if (isconvertable(def1^.paratype.def,def2^.paratype.def,doconv,callparan,false)=0) or
  317. ((def1^.paratyp<>def2^.paratyp) and
  318. ((def1^.paratyp in [vs_out,vs_var]) or
  319. (def2^.paratyp in [vs_out,vs_var])
  320. )
  321. ) then
  322. begin
  323. convertable_paras:=false;
  324. exit;
  325. end;
  326. end;
  327. cp_all :
  328. begin
  329. if (isconvertable(def1^.paratype.def,def2^.paratype.def,doconv,callparan,false)=0) or
  330. (def1^.paratyp<>def2^.paratyp) then
  331. begin
  332. convertable_paras:=false;
  333. exit;
  334. end;
  335. end;
  336. cp_none :
  337. begin
  338. if (isconvertable(def1^.paratype.def,def2^.paratype.def,doconv,callparan,false)=0) then
  339. begin
  340. convertable_paras:=false;
  341. exit;
  342. end;
  343. end;
  344. end;
  345. def1:=pparaitem(def1^.next);
  346. def2:=pparaitem(def2^.next);
  347. end;
  348. if (def1=nil) and (def2=nil) then
  349. convertable_paras:=true
  350. else
  351. convertable_paras:=false;
  352. end;
  353. { true if a function can be assigned to a procvar }
  354. function proc_to_procvar_equal(def1:pprocdef;def2:pprocvardef) : boolean;
  355. const
  356. po_comp = po_compatibility_options-[po_methodpointer,po_classmethod];
  357. var
  358. ismethod : boolean;
  359. begin
  360. proc_to_procvar_equal:=false;
  361. if not(assigned(def1)) or not(assigned(def2)) then
  362. exit;
  363. { check for method pointer }
  364. ismethod:=assigned(def1^.owner) and
  365. (def1^.owner^.symtabletype=objectsymtable);
  366. { I think methods of objects are also not compatible }
  367. { with procedure variables! (FK)
  368. and
  369. assigned(def1^.owner^.defowner) and
  370. (pobjectdef(def1^.owner^.defowner)^.is_class); }
  371. if (ismethod and not (po_methodpointer in def2^.procoptions)) or
  372. (not(ismethod) and (po_methodpointer in def2^.procoptions)) then
  373. begin
  374. Message(type_e_no_method_and_procedure_not_compatible);
  375. exit;
  376. end;
  377. { check return value and para's and options, methodpointer is already checked
  378. parameters may also be convertable }
  379. if is_equal(def1^.rettype.def,def2^.rettype.def) and
  380. (equal_paras(def1^.para,def2^.para,cp_all) or
  381. convertable_paras(def1^.para,def2^.para,cp_all)) and
  382. ((po_comp * def1^.procoptions)= (po_comp * def2^.procoptions)) then
  383. proc_to_procvar_equal:=true
  384. else
  385. proc_to_procvar_equal:=false;
  386. end;
  387. { returns true, if def uses FPU }
  388. function is_fpu(def : pdef) : boolean;
  389. begin
  390. is_fpu:=(def^.deftype=floatdef) and (pfloatdef(def)^.typ<>f32bit);
  391. end;
  392. { true if p is an ordinal }
  393. function is_ordinal(def : pdef) : boolean;
  394. var
  395. dt : tbasetype;
  396. begin
  397. case def^.deftype of
  398. orddef :
  399. begin
  400. dt:=porddef(def)^.typ;
  401. is_ordinal:=dt in [uchar,
  402. u8bit,u16bit,u32bit,u64bit,
  403. s8bit,s16bit,s32bit,s64bit,
  404. bool8bit,bool16bit,bool32bit];
  405. end;
  406. enumdef :
  407. is_ordinal:=true;
  408. else
  409. is_ordinal:=false;
  410. end;
  411. end;
  412. { returns the min. value of the type }
  413. function get_min_value(def : pdef) : longint;
  414. begin
  415. case def^.deftype of
  416. orddef:
  417. get_min_value:=porddef(def)^.low;
  418. enumdef:
  419. get_min_value:=penumdef(def)^.min;
  420. else
  421. get_min_value:=0;
  422. end;
  423. end;
  424. { true if p is an integer }
  425. function is_integer(def : pdef) : boolean;
  426. begin
  427. is_integer:=(def^.deftype=orddef) and
  428. (porddef(def)^.typ in [uauto,u8bit,u16bit,u32bit,u64bit,
  429. s8bit,s16bit,s32bit,s64bit]);
  430. end;
  431. { true if p is a boolean }
  432. function is_boolean(def : pdef) : boolean;
  433. begin
  434. is_boolean:=(def^.deftype=orddef) and
  435. (porddef(def)^.typ in [bool8bit,bool16bit,bool32bit]);
  436. end;
  437. { true if p is a void }
  438. function is_void(def : pdef) : boolean;
  439. begin
  440. is_void:=(def^.deftype=orddef) and
  441. (porddef(def)^.typ=uvoid);
  442. end;
  443. { true if p is a char }
  444. function is_char(def : pdef) : boolean;
  445. begin
  446. is_char:=(def^.deftype=orddef) and
  447. (porddef(def)^.typ=uchar);
  448. end;
  449. { true if p is signed (integer) }
  450. function is_signed(def : pdef) : boolean;
  451. var
  452. dt : tbasetype;
  453. begin
  454. case def^.deftype of
  455. orddef :
  456. begin
  457. dt:=porddef(def)^.typ;
  458. is_signed:=(dt in [s8bit,s16bit,s32bit,s64bit]);
  459. end;
  460. enumdef :
  461. is_signed:=false;
  462. else
  463. is_signed:=false;
  464. end;
  465. end;
  466. { true, if p points to an open array def }
  467. function is_open_string(p : pdef) : boolean;
  468. begin
  469. is_open_string:=(p^.deftype=stringdef) and
  470. (pstringdef(p)^.string_typ=st_shortstring) and
  471. (pstringdef(p)^.len=0);
  472. end;
  473. { true, if p points to a zero based array def }
  474. function is_zero_based_array(p : pdef) : boolean;
  475. begin
  476. is_zero_based_array:=(p^.deftype=arraydef) and
  477. (parraydef(p)^.lowrange=0) and
  478. not(is_special_array(p));
  479. end;
  480. { true, if p points to an open array def }
  481. function is_open_array(p : pdef) : boolean;
  482. begin
  483. { check for s32bitdef is needed, because for u32bit the high
  484. range is also -1 ! (PFV) }
  485. is_open_array:=(p^.deftype=arraydef) and
  486. (parraydef(p)^.rangetype.def=pdef(s32bitdef)) and
  487. (parraydef(p)^.lowrange=0) and
  488. (parraydef(p)^.highrange=-1) and
  489. not(parraydef(p)^.IsConstructor) and
  490. not(parraydef(p)^.IsVariant) and
  491. not(parraydef(p)^.IsArrayOfConst);
  492. end;
  493. { true, if p points to an array of const def }
  494. function is_array_constructor(p : pdef) : boolean;
  495. begin
  496. is_array_constructor:=(p^.deftype=arraydef) and
  497. (parraydef(p)^.IsConstructor);
  498. end;
  499. { true, if p points to a variant array }
  500. function is_variant_array(p : pdef) : boolean;
  501. begin
  502. is_variant_array:=(p^.deftype=arraydef) and
  503. (parraydef(p)^.IsVariant);
  504. end;
  505. { true, if p points to an array of const }
  506. function is_array_of_const(p : pdef) : boolean;
  507. begin
  508. is_array_of_const:=(p^.deftype=arraydef) and
  509. (parraydef(p)^.IsArrayOfConst);
  510. end;
  511. { true, if p points to a special array }
  512. function is_special_array(p : pdef) : boolean;
  513. begin
  514. is_special_array:=(p^.deftype=arraydef) and
  515. ((parraydef(p)^.IsVariant) or
  516. (parraydef(p)^.IsArrayOfConst) or
  517. (parraydef(p)^.IsConstructor) or
  518. is_open_array(p)
  519. );
  520. end;
  521. { true if p is an ansi string def }
  522. function is_ansistring(p : pdef) : boolean;
  523. begin
  524. is_ansistring:=(p^.deftype=stringdef) and
  525. (pstringdef(p)^.string_typ=st_ansistring);
  526. end;
  527. { true if p is an long string def }
  528. function is_longstring(p : pdef) : boolean;
  529. begin
  530. is_longstring:=(p^.deftype=stringdef) and
  531. (pstringdef(p)^.string_typ=st_longstring);
  532. end;
  533. { true if p is an wide string def }
  534. function is_widestring(p : pdef) : boolean;
  535. begin
  536. is_widestring:=(p^.deftype=stringdef) and
  537. (pstringdef(p)^.string_typ=st_widestring);
  538. end;
  539. { true if p is an short string def }
  540. function is_shortstring(p : pdef) : boolean;
  541. begin
  542. is_shortstring:=(p^.deftype=stringdef) and
  543. (pstringdef(p)^.string_typ=st_shortstring);
  544. end;
  545. { true if p is a char array def }
  546. function is_chararray(p : pdef) : boolean;
  547. begin
  548. is_chararray:=(p^.deftype=arraydef) and
  549. is_equal(parraydef(p)^.elementtype.def,cchardef) and
  550. not(is_special_array(p));
  551. end;
  552. { true if p is a pchar def }
  553. function is_pchar(p : pdef) : boolean;
  554. begin
  555. is_pchar:=(p^.deftype=pointerdef) and
  556. is_equal(Ppointerdef(p)^.pointertype.def,cchardef);
  557. end;
  558. { true if p is a voidpointer def }
  559. function is_voidpointer(p : pdef) : boolean;
  560. begin
  561. is_voidpointer:=(p^.deftype=pointerdef) and
  562. is_equal(Ppointerdef(p)^.pointertype.def,voiddef);
  563. end;
  564. { true if p is a smallset def }
  565. function is_smallset(p : pdef) : boolean;
  566. begin
  567. is_smallset:=(p^.deftype=setdef) and
  568. (psetdef(p)^.settype=smallset);
  569. end;
  570. { true if the return value is in accumulator (EAX for i386), D0 for 68k }
  571. function ret_in_acc(def : pdef) : boolean;
  572. begin
  573. ret_in_acc:=(def^.deftype in [orddef,pointerdef,enumdef,classrefdef]) or
  574. ((def^.deftype=stringdef) and (pstringdef(def)^.string_typ in [st_ansistring,st_widestring])) or
  575. ((def^.deftype=procvardef) and not(po_methodpointer in pprocvardef(def)^.procoptions)) or
  576. ((def^.deftype=objectdef) and pobjectdef(def)^.is_class) or
  577. ((def^.deftype=setdef) and (psetdef(def)^.settype=smallset)) or
  578. ((def^.deftype=floatdef) and (pfloatdef(def)^.typ=f32bit));
  579. end;
  580. { true, if def is a 64 bit int type }
  581. function is_64bitint(def : pdef) : boolean;
  582. begin
  583. is_64bitint:=(def^.deftype=orddef) and (porddef(def)^.typ in [u64bit,s64bit])
  584. end;
  585. { true if uses a parameter as return value }
  586. function ret_in_param(def : pdef) : boolean;
  587. begin
  588. ret_in_param:=(def^.deftype in [arraydef,recorddef]) or
  589. ((def^.deftype=stringdef) and (pstringdef(def)^.string_typ in [st_shortstring,st_longstring])) or
  590. ((def^.deftype=procvardef) and (po_methodpointer in pprocvardef(def)^.procoptions)) or
  591. ((def^.deftype=objectdef) and not(pobjectdef(def)^.is_class)) or
  592. ((def^.deftype=setdef) and (psetdef(def)^.settype<>smallset));
  593. end;
  594. function push_high_param(def : pdef) : boolean;
  595. begin
  596. push_high_param:=is_open_array(def) or
  597. is_open_string(def) or
  598. is_array_of_const(def);
  599. end;
  600. { true if a parameter is too large to copy and only the address is pushed }
  601. function push_addr_param(def : pdef) : boolean;
  602. begin
  603. push_addr_param:=false;
  604. if never_copy_const_param then
  605. push_addr_param:=true
  606. else
  607. begin
  608. case def^.deftype of
  609. formaldef :
  610. push_addr_param:=true;
  611. recorddef :
  612. push_addr_param:=(def^.size>4);
  613. arraydef :
  614. push_addr_param:=((Parraydef(def)^.highrange>=Parraydef(def)^.lowrange) and (def^.size>4)) or
  615. is_open_array(def) or
  616. is_array_of_const(def) or
  617. is_array_constructor(def);
  618. objectdef :
  619. push_addr_param:=not(pobjectdef(def)^.is_class);
  620. stringdef :
  621. push_addr_param:=pstringdef(def)^.string_typ in [st_shortstring,st_longstring];
  622. procvardef :
  623. push_addr_param:=(po_methodpointer in pprocvardef(def)^.procoptions);
  624. setdef :
  625. push_addr_param:=(psetdef(def)^.settype<>smallset);
  626. end;
  627. end;
  628. end;
  629. { test if l is in the range of def, outputs error if out of range }
  630. procedure testrange(def : pdef;var l : tconstexprint);
  631. var
  632. lv,hv: longint;
  633. begin
  634. { for 64 bit types we need only to check if it is less than }
  635. { zero, if def is a qword node }
  636. if is_64bitint(def) then
  637. begin
  638. if (l<0) and (porddef(def)^.typ=u64bit) then
  639. begin
  640. l:=0;
  641. if (cs_check_range in aktlocalswitches) then
  642. Message(parser_e_range_check_error)
  643. else
  644. Message(parser_w_range_check_error);
  645. end;
  646. end
  647. else
  648. begin
  649. getrange(def,lv,hv);
  650. if (def^.deftype=orddef) and
  651. (porddef(def)^.typ=u32bit) then
  652. begin
  653. if lv<=hv then
  654. begin
  655. if (l<lv) or (l>hv) then
  656. begin
  657. if (cs_check_range in aktlocalswitches) then
  658. Message(parser_e_range_check_error)
  659. else
  660. Message(parser_w_range_check_error);
  661. end;
  662. end
  663. else
  664. { this happens with the wrap around problem }
  665. { if lv is positive and hv is over $7ffffff }
  666. { so it seems negative }
  667. begin
  668. if ((l>=0) and (l<lv)) or
  669. ((l<0) and (l>hv)) then
  670. begin
  671. if (cs_check_range in aktlocalswitches) then
  672. Message(parser_e_range_check_error)
  673. else
  674. Message(parser_w_range_check_error);
  675. end;
  676. end;
  677. end
  678. else if (l<lv) or (l>hv) then
  679. begin
  680. if (def^.deftype=enumdef) or
  681. (cs_check_range in aktlocalswitches) then
  682. Message(parser_e_range_check_error)
  683. else
  684. Message(parser_w_range_check_error);
  685. { Fix the value to fit in the allocated space for this type of variable }
  686. case def^.size of
  687. 1: l := l and $ff;
  688. 2: l := l and $ffff;
  689. end
  690. { l:=lv+(l mod (hv-lv+1));}
  691. end;
  692. end;
  693. end;
  694. { return the range from def in l and h }
  695. procedure getrange(def : pdef;var l : longint;var h : longint);
  696. begin
  697. case def^.deftype of
  698. orddef :
  699. begin
  700. l:=porddef(def)^.low;
  701. h:=porddef(def)^.high;
  702. end;
  703. enumdef :
  704. begin
  705. l:=penumdef(def)^.min;
  706. h:=penumdef(def)^.max;
  707. end;
  708. arraydef :
  709. begin
  710. l:=parraydef(def)^.lowrange;
  711. h:=parraydef(def)^.highrange;
  712. end;
  713. else
  714. internalerror(987);
  715. end;
  716. end;
  717. function mmx_type(p : pdef) : tmmxtype;
  718. begin
  719. mmx_type:=mmxno;
  720. if is_mmx_able_array(p) then
  721. begin
  722. if parraydef(p)^.elementtype.def^.deftype=floatdef then
  723. case pfloatdef(parraydef(p)^.elementtype.def)^.typ of
  724. s32real:
  725. mmx_type:=mmxsingle;
  726. f16bit:
  727. mmx_type:=mmxfixed16
  728. end
  729. else
  730. case porddef(parraydef(p)^.elementtype.def)^.typ of
  731. u8bit:
  732. mmx_type:=mmxu8bit;
  733. s8bit:
  734. mmx_type:=mmxs8bit;
  735. u16bit:
  736. mmx_type:=mmxu16bit;
  737. s16bit:
  738. mmx_type:=mmxs16bit;
  739. u32bit:
  740. mmx_type:=mmxu32bit;
  741. s32bit:
  742. mmx_type:=mmxs32bit;
  743. end;
  744. end;
  745. end;
  746. function is_mmx_able_array(p : pdef) : boolean;
  747. begin
  748. {$ifdef SUPPORT_MMX}
  749. if (cs_mmx_saturation in aktlocalswitches) then
  750. begin
  751. is_mmx_able_array:=(p^.deftype=arraydef) and
  752. not(is_special_array(p)) and
  753. (
  754. (
  755. (parraydef(p)^.elementtype.def^.deftype=orddef) and
  756. (
  757. (
  758. (parraydef(p)^.lowrange=0) and
  759. (parraydef(p)^.highrange=1) and
  760. (porddef(parraydef(p)^.elementtype.def)^.typ in [u32bit,s32bit])
  761. )
  762. or
  763. (
  764. (parraydef(p)^.lowrange=0) and
  765. (parraydef(p)^.highrange=3) and
  766. (porddef(parraydef(p)^.elementtype.def)^.typ in [u16bit,s16bit])
  767. )
  768. )
  769. )
  770. or
  771. (
  772. (
  773. (parraydef(p)^.elementtype.def^.deftype=floatdef) and
  774. (
  775. (parraydef(p)^.lowrange=0) and
  776. (parraydef(p)^.highrange=3) and
  777. (pfloatdef(parraydef(p)^.elementtype.def)^.typ=f16bit)
  778. ) or
  779. (
  780. (parraydef(p)^.lowrange=0) and
  781. (parraydef(p)^.highrange=1) and
  782. (pfloatdef(parraydef(p)^.elementtype.def)^.typ=s32real)
  783. )
  784. )
  785. )
  786. );
  787. end
  788. else
  789. begin
  790. is_mmx_able_array:=(p^.deftype=arraydef) and
  791. (
  792. (
  793. (parraydef(p)^.elementtype.def^.deftype=orddef) and
  794. (
  795. (
  796. (parraydef(p)^.lowrange=0) and
  797. (parraydef(p)^.highrange=1) and
  798. (porddef(parraydef(p)^.elementtype.def)^.typ in [u32bit,s32bit])
  799. )
  800. or
  801. (
  802. (parraydef(p)^.lowrange=0) and
  803. (parraydef(p)^.highrange=3) and
  804. (porddef(parraydef(p)^.elementtype.def)^.typ in [u16bit,s16bit])
  805. )
  806. or
  807. (
  808. (parraydef(p)^.lowrange=0) and
  809. (parraydef(p)^.highrange=7) and
  810. (porddef(parraydef(p)^.elementtype.def)^.typ in [u8bit,s8bit])
  811. )
  812. )
  813. )
  814. or
  815. (
  816. (parraydef(p)^.elementtype.def^.deftype=floatdef) and
  817. (
  818. (
  819. (parraydef(p)^.lowrange=0) and
  820. (parraydef(p)^.highrange=3) and
  821. (pfloatdef(parraydef(p)^.elementtype.def)^.typ=f32bit)
  822. )
  823. or
  824. (
  825. (parraydef(p)^.lowrange=0) and
  826. (parraydef(p)^.highrange=1) and
  827. (pfloatdef(parraydef(p)^.elementtype.def)^.typ=s32real)
  828. )
  829. )
  830. )
  831. );
  832. end;
  833. {$else SUPPORT_MMX}
  834. is_mmx_able_array:=false;
  835. {$endif SUPPORT_MMX}
  836. end;
  837. function is_equal(def1,def2 : pdef) : boolean;
  838. var
  839. b : boolean;
  840. hd : pdef;
  841. begin
  842. { both types must exists }
  843. if not (assigned(def1) and assigned(def2)) then
  844. begin
  845. is_equal:=false;
  846. exit;
  847. end;
  848. { be sure, that if there is a stringdef, that this is def1 }
  849. if def2^.deftype=stringdef then
  850. begin
  851. hd:=def1;
  852. def1:=def2;
  853. def2:=hd;
  854. end;
  855. b:=false;
  856. { both point to the same definition ? }
  857. if def1=def2 then
  858. b:=true
  859. else
  860. { pointer with an equal definition are equal }
  861. if (def1^.deftype=pointerdef) and (def2^.deftype=pointerdef) then
  862. begin
  863. { here a problem detected in tabsolutesym }
  864. { the types can be forward type !! }
  865. if assigned(def1^.typesym) and (ppointerdef(def1)^.pointertype.def^.deftype=forwarddef) then
  866. b:=(def1^.typesym=def2^.typesym)
  867. else
  868. b:=ppointerdef(def1)^.pointertype.def=ppointerdef(def2)^.pointertype.def;
  869. end
  870. else
  871. { ordinals are equal only when the ordinal type is equal }
  872. if (def1^.deftype=orddef) and (def2^.deftype=orddef) then
  873. begin
  874. case porddef(def1)^.typ of
  875. u8bit,u16bit,u32bit,
  876. s8bit,s16bit,s32bit:
  877. b:=((porddef(def1)^.typ=porddef(def2)^.typ) and
  878. (porddef(def1)^.low=porddef(def2)^.low) and
  879. (porddef(def1)^.high=porddef(def2)^.high));
  880. uvoid,uchar,
  881. bool8bit,bool16bit,bool32bit:
  882. b:=(porddef(def1)^.typ=porddef(def2)^.typ);
  883. end;
  884. end
  885. else
  886. if (def1^.deftype=floatdef) and (def2^.deftype=floatdef) then
  887. b:=pfloatdef(def1)^.typ=pfloatdef(def2)^.typ
  888. else
  889. { strings with the same length are equal }
  890. if (def1^.deftype=stringdef) and (def2^.deftype=stringdef) and
  891. (pstringdef(def1)^.string_typ=pstringdef(def2)^.string_typ) then
  892. begin
  893. b:=not(is_shortstring(def1)) or
  894. (pstringdef(def1)^.len=pstringdef(def2)^.len);
  895. end
  896. else
  897. if (def1^.deftype=formaldef) and (def2^.deftype=formaldef) then
  898. b:=true
  899. { file types with the same file element type are equal }
  900. { this is a problem for assign !! }
  901. { changed to allow if one is untyped }
  902. { all typed files are equal to the special }
  903. { typed file that has voiddef as elemnt type }
  904. { but must NOT match for text file !!! }
  905. else
  906. if (def1^.deftype=filedef) and (def2^.deftype=filedef) then
  907. b:=(pfiledef(def1)^.filetyp=pfiledef(def2)^.filetyp) and
  908. ((
  909. ((pfiledef(def1)^.typedfiletype.def=nil) and
  910. (pfiledef(def2)^.typedfiletype.def=nil)) or
  911. (
  912. (pfiledef(def1)^.typedfiletype.def<>nil) and
  913. (pfiledef(def2)^.typedfiletype.def<>nil) and
  914. is_equal(pfiledef(def1)^.typedfiletype.def,pfiledef(def2)^.typedfiletype.def)
  915. ) or
  916. ( (pfiledef(def1)^.typedfiletype.def=pdef(voiddef)) or
  917. (pfiledef(def2)^.typedfiletype.def=pdef(voiddef))
  918. )))
  919. { sets with the same element type are equal }
  920. else
  921. if (def1^.deftype=setdef) and (def2^.deftype=setdef) then
  922. begin
  923. if assigned(psetdef(def1)^.elementtype.def) and
  924. assigned(psetdef(def2)^.elementtype.def) then
  925. b:=(psetdef(def1)^.elementtype.def^.deftype=psetdef(def2)^.elementtype.def^.deftype)
  926. else
  927. b:=true;
  928. end
  929. else
  930. if (def1^.deftype=procvardef) and (def2^.deftype=procvardef) then
  931. begin
  932. { poassembler isn't important for compatibility }
  933. { if a method is assigned to a methodpointer }
  934. { is checked before }
  935. b:=(pprocvardef(def1)^.proctypeoption=pprocvardef(def2)^.proctypeoption) and
  936. (pprocvardef(def1)^.proccalloptions=pprocvardef(def2)^.proccalloptions) and
  937. ((pprocvardef(def1)^.procoptions * po_compatibility_options)=
  938. (pprocvardef(def2)^.procoptions * po_compatibility_options)) and
  939. is_equal(pprocvardef(def1)^.rettype.def,pprocvardef(def2)^.rettype.def) and
  940. equal_paras(pprocvardef(def1)^.para,pprocvardef(def2)^.para,cp_all);
  941. end
  942. else
  943. if (def1^.deftype=arraydef) and (def2^.deftype=arraydef) then
  944. begin
  945. if is_array_of_const(def1) or is_array_of_const(def2) then
  946. begin
  947. b:=(is_array_of_const(def1) and is_array_of_const(def2)) or
  948. (is_array_of_const(def1) and is_array_constructor(def2)) or
  949. (is_array_of_const(def2) and is_array_constructor(def1));
  950. end
  951. else
  952. if is_open_array(def1) or is_open_array(def2) then
  953. begin
  954. b:=is_equal(parraydef(def1)^.elementtype.def,parraydef(def2)^.elementtype.def);
  955. end
  956. else
  957. begin
  958. b:=not(m_tp in aktmodeswitches) and
  959. not(m_delphi in aktmodeswitches) and
  960. (parraydef(def1)^.lowrange=parraydef(def2)^.lowrange) and
  961. (parraydef(def1)^.highrange=parraydef(def2)^.highrange) and
  962. is_equal(parraydef(def1)^.elementtype.def,parraydef(def2)^.elementtype.def) and
  963. is_equal(parraydef(def1)^.rangetype.def,parraydef(def2)^.rangetype.def);
  964. end;
  965. end
  966. else
  967. if (def1^.deftype=classrefdef) and (def2^.deftype=classrefdef) then
  968. begin
  969. { similar to pointerdef: }
  970. if assigned(def1^.typesym) and (pclassrefdef(def1)^.pointertype.def^.deftype=forwarddef) then
  971. b:=(def1^.typesym=def2^.typesym)
  972. else
  973. b:=is_equal(pclassrefdef(def1)^.pointertype.def,pclassrefdef(def2)^.pointertype.def);
  974. end;
  975. is_equal:=b;
  976. end;
  977. function is_subequal(def1, def2: pdef): boolean;
  978. var
  979. basedef1,basedef2 : penumdef;
  980. Begin
  981. is_subequal := false;
  982. if assigned(def1) and assigned(def2) then
  983. Begin
  984. if (def1^.deftype = orddef) and (def2^.deftype = orddef) then
  985. Begin
  986. { see p.47 of Turbo Pascal 7.01 manual for the separation of types }
  987. { range checking for case statements is done with testrange }
  988. case porddef(def1)^.typ of
  989. u8bit,u16bit,u32bit,
  990. s8bit,s16bit,s32bit,s64bit,u64bit :
  991. is_subequal:=(porddef(def2)^.typ in [s64bit,u64bit,s32bit,u32bit,u8bit,s8bit,s16bit,u16bit]);
  992. bool8bit,bool16bit,bool32bit :
  993. is_subequal:=(porddef(def2)^.typ in [bool8bit,bool16bit,bool32bit]);
  994. uchar :
  995. is_subequal:=(porddef(def2)^.typ=uchar);
  996. end;
  997. end
  998. else
  999. Begin
  1000. { I assume that both enumerations are equal when the first }
  1001. { pointers are equal. }
  1002. { I changed this to assume that the enums are equal }
  1003. { if the basedefs are equal (FK) }
  1004. if (def1^.deftype=enumdef) and (def2^.deftype=enumdef) then
  1005. Begin
  1006. { get both basedefs }
  1007. basedef1:=penumdef(def1);
  1008. while assigned(basedef1^.basedef) do
  1009. basedef1:=basedef1^.basedef;
  1010. basedef2:=penumdef(def2);
  1011. while assigned(basedef2^.basedef) do
  1012. basedef2:=basedef2^.basedef;
  1013. is_subequal:=basedef1=basedef2;
  1014. {
  1015. if penumdef(def1)^.firstenum = penumdef(def2)^.firstenum then
  1016. is_subequal := TRUE;
  1017. }
  1018. end;
  1019. end;
  1020. end; { endif assigned ... }
  1021. end;
  1022. {$ifdef CG11}
  1023. function assignment_overloaded(from_def,to_def : pdef) : pprocdef;
  1024. var
  1025. passproc : pprocdef;
  1026. convtyp : tconverttype;
  1027. begin
  1028. assignment_overloaded:=nil;
  1029. if assigned(overloaded_operators[_ASSIGNMENT]) then
  1030. passproc:=overloaded_operators[_ASSIGNMENT]^.definition
  1031. else
  1032. exit;
  1033. while passproc<>nil do
  1034. begin
  1035. if is_equal(passproc^.rettype.def,to_def) and
  1036. (is_equal(pparaitem(passproc^.para^.first)^.paratype.def,from_def) or
  1037. (isconvertable(from_def,pparaitem(passproc^.para^.first)^.paratype.def,convtyp,ordconstn,false)=1)) then
  1038. begin
  1039. assignment_overloaded:=passproc;
  1040. break;
  1041. end;
  1042. passproc:=passproc^.nextoverloaded;
  1043. end;
  1044. end;
  1045. { Returns:
  1046. 0 - Not convertable
  1047. 1 - Convertable
  1048. 2 - Convertable, but not first choice }
  1049. function isconvertable(def_from,def_to : pdef;
  1050. var doconv : tconverttype;fromtreetype : tnodetype;
  1051. explicit : boolean) : byte;
  1052. { Tbasetype: uauto,uvoid,uchar,
  1053. u8bit,u16bit,u32bit,
  1054. s8bit,s16bit,s32,
  1055. bool8bit,bool16bit,bool32bit,
  1056. u64bit,s64bitint }
  1057. type
  1058. tbasedef=(bvoid,bchar,bint,bbool);
  1059. const
  1060. basedeftbl:array[tbasetype] of tbasedef =
  1061. (bvoid,bvoid,bchar,
  1062. bint,bint,bint,
  1063. bint,bint,bint,
  1064. bbool,bbool,bbool,bint,bint,bchar);
  1065. basedefconverts : array[tbasedef,tbasedef] of tconverttype =
  1066. ((tc_not_possible,tc_not_possible,tc_not_possible,tc_not_possible),
  1067. (tc_not_possible,tc_equal,tc_not_possible,tc_not_possible),
  1068. (tc_not_possible,tc_not_possible,tc_int_2_int,tc_int_2_bool),
  1069. (tc_not_possible,tc_not_possible,tc_bool_2_int,tc_bool_2_bool));
  1070. var
  1071. b : byte;
  1072. hd1,hd2 : pdef;
  1073. hct : tconverttype;
  1074. begin
  1075. { safety check }
  1076. if not(assigned(def_from) and assigned(def_to)) then
  1077. begin
  1078. isconvertable:=0;
  1079. exit;
  1080. end;
  1081. { tp7 procvar def support, in tp7 a procvar is always called, if the
  1082. procvar is passed explicit a addrn would be there }
  1083. if (m_tp_procvar in aktmodeswitches) and
  1084. (def_from^.deftype=procvardef) and
  1085. (fromtreetype=loadn) then
  1086. begin
  1087. def_from:=pprocvardef(def_from)^.rettype.def;
  1088. end;
  1089. { we walk the wanted (def_to) types and check then the def_from
  1090. types if there is a conversion possible }
  1091. b:=0;
  1092. case def_to^.deftype of
  1093. orddef :
  1094. begin
  1095. case def_from^.deftype of
  1096. orddef :
  1097. begin
  1098. doconv:=basedefconverts[basedeftbl[porddef(def_from)^.typ],basedeftbl[porddef(def_to)^.typ]];
  1099. b:=1;
  1100. if (doconv=tc_not_possible) or
  1101. ((doconv=tc_int_2_bool) and
  1102. (not explicit) and
  1103. (not is_boolean(def_from))) or
  1104. ((doconv=tc_bool_2_int) and
  1105. (not explicit) and
  1106. (not is_boolean(def_to))) then
  1107. b:=0;
  1108. end;
  1109. enumdef :
  1110. begin
  1111. { needed for char(enum) }
  1112. if explicit then
  1113. begin
  1114. doconv:=tc_int_2_int;
  1115. b:=1;
  1116. end;
  1117. end;
  1118. end;
  1119. end;
  1120. stringdef :
  1121. begin
  1122. case def_from^.deftype of
  1123. stringdef :
  1124. begin
  1125. doconv:=tc_string_2_string;
  1126. b:=1;
  1127. end;
  1128. orddef :
  1129. begin
  1130. { char to string}
  1131. if is_char(def_from) then
  1132. begin
  1133. doconv:=tc_char_2_string;
  1134. b:=1;
  1135. end;
  1136. end;
  1137. arraydef :
  1138. begin
  1139. { array of char to string, the length check is done by the firstpass of this node }
  1140. if is_chararray(def_from) then
  1141. begin
  1142. doconv:=tc_chararray_2_string;
  1143. if (not(cs_ansistrings in aktlocalswitches) and
  1144. is_shortstring(def_to)) or
  1145. ((cs_ansistrings in aktlocalswitches) and
  1146. is_ansistring(def_to)) then
  1147. b:=1
  1148. else
  1149. b:=2;
  1150. end;
  1151. end;
  1152. pointerdef :
  1153. begin
  1154. { pchar can be assigned to short/ansistrings,
  1155. but not in tp7 compatible mode }
  1156. if is_pchar(def_from) and not(m_tp7 in aktmodeswitches) then
  1157. begin
  1158. doconv:=tc_pchar_2_string;
  1159. b:=1;
  1160. end;
  1161. end;
  1162. end;
  1163. end;
  1164. floatdef :
  1165. begin
  1166. case def_from^.deftype of
  1167. orddef :
  1168. begin { ordinal to real }
  1169. if is_integer(def_from) then
  1170. begin
  1171. if pfloatdef(def_to)^.typ=f32bit then
  1172. doconv:=tc_int_2_fix
  1173. else
  1174. doconv:=tc_int_2_real;
  1175. b:=1;
  1176. end;
  1177. end;
  1178. floatdef :
  1179. begin { 2 float types ? }
  1180. if pfloatdef(def_from)^.typ=pfloatdef(def_to)^.typ then
  1181. doconv:=tc_equal
  1182. else
  1183. begin
  1184. if pfloatdef(def_from)^.typ=f32bit then
  1185. doconv:=tc_fix_2_real
  1186. else
  1187. if pfloatdef(def_to)^.typ=f32bit then
  1188. doconv:=tc_real_2_fix
  1189. else
  1190. doconv:=tc_real_2_real;
  1191. end;
  1192. b:=1;
  1193. end;
  1194. end;
  1195. end;
  1196. enumdef :
  1197. begin
  1198. if (def_from^.deftype=enumdef) then
  1199. begin
  1200. hd1:=def_from;
  1201. while assigned(penumdef(hd1)^.basedef) do
  1202. hd1:=penumdef(hd1)^.basedef;
  1203. hd2:=def_to;
  1204. while assigned(penumdef(hd2)^.basedef) do
  1205. hd2:=penumdef(hd2)^.basedef;
  1206. if (hd1=hd2) then
  1207. begin
  1208. b:=1;
  1209. { because of packenum they can have different sizes! (JM) }
  1210. doconv:=tc_int_2_int;
  1211. end;
  1212. end;
  1213. end;
  1214. arraydef :
  1215. begin
  1216. { open array is also compatible with a single element of its base type }
  1217. if is_open_array(def_to) and
  1218. is_equal(parraydef(def_to)^.elementtype.def,def_from) then
  1219. begin
  1220. doconv:=tc_equal;
  1221. b:=1;
  1222. end
  1223. else
  1224. begin
  1225. case def_from^.deftype of
  1226. arraydef :
  1227. begin
  1228. { array constructor -> open array }
  1229. if is_open_array(def_to) and
  1230. is_array_constructor(def_from) then
  1231. begin
  1232. if is_void(parraydef(def_from)^.elementtype.def) or
  1233. is_equal(parraydef(def_to)^.elementtype.def,parraydef(def_from)^.elementtype.def) then
  1234. begin
  1235. doconv:=tc_equal;
  1236. b:=1;
  1237. end
  1238. else
  1239. if isconvertable(parraydef(def_from)^.elementtype.def,
  1240. parraydef(def_to)^.elementtype.def,hct,arrayconstructn,false)<>0 then
  1241. begin
  1242. doconv:=hct;
  1243. b:=2;
  1244. end;
  1245. end;
  1246. end;
  1247. pointerdef :
  1248. begin
  1249. if is_zero_based_array(def_to) and
  1250. is_equal(ppointerdef(def_from)^.pointertype.def,parraydef(def_to)^.elementtype.def) then
  1251. begin
  1252. doconv:=tc_pointer_2_array;
  1253. b:=1;
  1254. end;
  1255. end;
  1256. stringdef :
  1257. begin
  1258. { string to array of char}
  1259. if (not(is_special_array(def_to)) or is_open_array(def_to)) and
  1260. is_equal(parraydef(def_to)^.elementtype.def,cchardef) then
  1261. begin
  1262. doconv:=tc_string_2_chararray;
  1263. b:=1;
  1264. end;
  1265. end;
  1266. end;
  1267. end;
  1268. end;
  1269. pointerdef :
  1270. begin
  1271. case def_from^.deftype of
  1272. stringdef :
  1273. begin
  1274. { string constant (which can be part of array constructor)
  1275. to zero terminated string constant }
  1276. if (fromtreetype in [arrayconstructn,stringconstn]) and
  1277. is_pchar(def_to) then
  1278. begin
  1279. doconv:=tc_cstring_2_pchar;
  1280. b:=1;
  1281. end;
  1282. end;
  1283. orddef :
  1284. begin
  1285. { char constant to zero terminated string constant }
  1286. if (fromtreetype=ordconstn) then
  1287. begin
  1288. if is_equal(def_from,cchardef) and
  1289. is_pchar(def_to) then
  1290. begin
  1291. doconv:=tc_cchar_2_pchar;
  1292. b:=1;
  1293. end
  1294. else
  1295. if is_integer(def_from) then
  1296. begin
  1297. doconv:=tc_cord_2_pointer;
  1298. b:=1;
  1299. end;
  1300. end;
  1301. end;
  1302. arraydef :
  1303. begin
  1304. { chararray to pointer }
  1305. if is_zero_based_array(def_from) and
  1306. is_equal(parraydef(def_from)^.elementtype.def,ppointerdef(def_to)^.pointertype.def) then
  1307. begin
  1308. doconv:=tc_array_2_pointer;
  1309. b:=1;
  1310. end;
  1311. end;
  1312. pointerdef :
  1313. begin
  1314. { child class pointer can be assigned to anchestor pointers }
  1315. if (
  1316. (ppointerdef(def_from)^.pointertype.def^.deftype=objectdef) and
  1317. (ppointerdef(def_to)^.pointertype.def^.deftype=objectdef) and
  1318. pobjectdef(ppointerdef(def_from)^.pointertype.def)^.is_related(
  1319. pobjectdef(ppointerdef(def_to)^.pointertype.def))
  1320. ) or
  1321. { all pointers can be assigned to void-pointer }
  1322. is_equal(ppointerdef(def_to)^.pointertype.def,voiddef) or
  1323. { in my opnion, is this not clean pascal }
  1324. { well, but it's handy to use, it isn't ? (FK) }
  1325. is_equal(ppointerdef(def_from)^.pointertype.def,voiddef) then
  1326. begin
  1327. doconv:=tc_equal;
  1328. b:=1;
  1329. end;
  1330. end;
  1331. procvardef :
  1332. begin
  1333. { procedure variable can be assigned to an void pointer }
  1334. { Not anymore. Use the @ operator now.}
  1335. if not(m_tp_procvar in aktmodeswitches) and
  1336. (ppointerdef(def_to)^.pointertype.def^.deftype=orddef) and
  1337. (porddef(ppointerdef(def_to)^.pointertype.def)^.typ=uvoid) then
  1338. begin
  1339. doconv:=tc_equal;
  1340. b:=1;
  1341. end;
  1342. end;
  1343. classrefdef,
  1344. objectdef :
  1345. begin
  1346. { class types and class reference type
  1347. can be assigned to void pointers }
  1348. if (
  1349. ((def_from^.deftype=objectdef) and pobjectdef(def_from)^.is_class) or
  1350. (def_from^.deftype=classrefdef)
  1351. ) and
  1352. (ppointerdef(def_to)^.pointertype.def^.deftype=orddef) and
  1353. (porddef(ppointerdef(def_to)^.pointertype.def)^.typ=uvoid) then
  1354. begin
  1355. doconv:=tc_equal;
  1356. b:=1;
  1357. end;
  1358. end;
  1359. end;
  1360. end;
  1361. setdef :
  1362. begin
  1363. { automatic arrayconstructor -> set conversion }
  1364. if is_array_constructor(def_from) then
  1365. begin
  1366. doconv:=tc_arrayconstructor_2_set;
  1367. b:=1;
  1368. end;
  1369. end;
  1370. procvardef :
  1371. begin
  1372. { proc -> procvar }
  1373. if (def_from^.deftype=procdef) then
  1374. begin
  1375. doconv:=tc_proc_2_procvar;
  1376. if proc_to_procvar_equal(pprocdef(def_from),pprocvardef(def_to)) then
  1377. b:=1;
  1378. end
  1379. else
  1380. { for example delphi allows the assignement from pointers }
  1381. { to procedure variables }
  1382. if (m_pointer_2_procedure in aktmodeswitches) and
  1383. (def_from^.deftype=pointerdef) and
  1384. (ppointerdef(def_from)^.pointertype.def^.deftype=orddef) and
  1385. (porddef(ppointerdef(def_from)^.pointertype.def)^.typ=uvoid) then
  1386. begin
  1387. doconv:=tc_equal;
  1388. b:=1;
  1389. end
  1390. else
  1391. { nil is compatible with procvars }
  1392. if (fromtreetype=niln) then
  1393. begin
  1394. doconv:=tc_equal;
  1395. b:=1;
  1396. end;
  1397. end;
  1398. objectdef :
  1399. begin
  1400. { object pascal objects }
  1401. if (def_from^.deftype=objectdef) {and
  1402. pobjectdef(def_from)^.isclass and pobjectdef(def_to)^.isclass }then
  1403. begin
  1404. doconv:=tc_equal;
  1405. if pobjectdef(def_from)^.is_related(pobjectdef(def_to)) then
  1406. b:=1;
  1407. end
  1408. else
  1409. { Class specific }
  1410. if (pobjectdef(def_to)^.is_class) then
  1411. begin
  1412. { void pointer also for delphi mode }
  1413. if (m_delphi in aktmodeswitches) and
  1414. is_voidpointer(def_from) then
  1415. begin
  1416. doconv:=tc_equal;
  1417. b:=1;
  1418. end
  1419. else
  1420. { nil is compatible with class instances }
  1421. if (fromtreetype=niln) and (pobjectdef(def_to)^.is_class) then
  1422. begin
  1423. doconv:=tc_equal;
  1424. b:=1;
  1425. end;
  1426. end;
  1427. end;
  1428. classrefdef :
  1429. begin
  1430. { class reference types }
  1431. if (def_from^.deftype=classrefdef) then
  1432. begin
  1433. doconv:=tc_equal;
  1434. if pobjectdef(pclassrefdef(def_from)^.pointertype.def)^.is_related(
  1435. pobjectdef(pclassrefdef(def_to)^.pointertype.def)) then
  1436. b:=1;
  1437. end
  1438. else
  1439. { nil is compatible with class references }
  1440. if (fromtreetype=niln) then
  1441. begin
  1442. doconv:=tc_equal;
  1443. b:=1;
  1444. end;
  1445. end;
  1446. filedef :
  1447. begin
  1448. { typed files are all equal to the abstract file type
  1449. name TYPEDFILE in system.pp in is_equal in types.pas
  1450. the problem is that it sholud be also compatible to FILE
  1451. but this would leed to a problem for ASSIGN RESET and REWRITE
  1452. when trying to find the good overloaded function !!
  1453. so all file function are doubled in system.pp
  1454. this is not very beautiful !!}
  1455. if (def_from^.deftype=filedef) and
  1456. (
  1457. (
  1458. (pfiledef(def_from)^.filetyp = ft_typed) and
  1459. (pfiledef(def_to)^.filetyp = ft_typed) and
  1460. (
  1461. (pfiledef(def_from)^.typedfiletype.def = pdef(voiddef)) or
  1462. (pfiledef(def_to)^.typedfiletype.def = pdef(voiddef))
  1463. )
  1464. ) or
  1465. (
  1466. (
  1467. (pfiledef(def_from)^.filetyp = ft_untyped) and
  1468. (pfiledef(def_to)^.filetyp = ft_typed)
  1469. ) or
  1470. (
  1471. (pfiledef(def_from)^.filetyp = ft_typed) and
  1472. (pfiledef(def_to)^.filetyp = ft_untyped)
  1473. )
  1474. )
  1475. ) then
  1476. begin
  1477. doconv:=tc_equal;
  1478. b:=1;
  1479. end
  1480. end;
  1481. else
  1482. begin
  1483. { assignment overwritten ?? }
  1484. if assignment_overloaded(def_from,def_to)<>nil then
  1485. b:=2;
  1486. end;
  1487. end;
  1488. isconvertable:=b;
  1489. end;
  1490. {$endif CG11}
  1491. function CheckTypes(def1,def2 : pdef) : boolean;
  1492. var
  1493. s1,s2 : string;
  1494. begin
  1495. if not is_equal(def1,def2) then
  1496. begin
  1497. { Crash prevention }
  1498. if (not assigned(def1)) or (not assigned(def2)) then
  1499. Message(type_e_mismatch)
  1500. else
  1501. begin
  1502. s1:=def1^.typename;
  1503. s2:=def2^.typename;
  1504. if (s1<>'<unknown type>') and (s2<>'<unknown type>') then
  1505. Message2(type_e_not_equal_types,def1^.typename,def2^.typename)
  1506. else
  1507. Message(type_e_mismatch);
  1508. end;
  1509. CheckTypes:=false;
  1510. end
  1511. else
  1512. CheckTypes:=true;
  1513. end;
  1514. end.
  1515. {
  1516. $Log$
  1517. Revision 1.13 2000-10-01 19:48:26 peter
  1518. * lot of compile updates for cg11
  1519. Revision 1.12 2000/09/30 16:08:46 peter
  1520. * more cg11 updates
  1521. Revision 1.11 2000/09/24 15:06:32 peter
  1522. * use defines.inc
  1523. Revision 1.10 2000/09/18 12:31:15 jonas
  1524. * fixed bug in push_addr_param for arrays (merged from fixes branch)
  1525. Revision 1.9 2000/09/10 20:16:21 peter
  1526. * array of const isn't equal with array of <type> (merged)
  1527. Revision 1.8 2000/08/19 19:51:03 peter
  1528. * fixed bug with comparing constsym strings
  1529. Revision 1.7 2000/08/16 13:06:07 florian
  1530. + support of 64 bit integer constants
  1531. Revision 1.6 2000/08/13 13:07:18 peter
  1532. * equal_paras now also checks default parameter value
  1533. Revision 1.5 2000/08/12 06:49:22 florian
  1534. + case statement for int64/qword implemented
  1535. Revision 1.4 2000/08/08 19:26:41 peter
  1536. * equal_constsym() needed for default para
  1537. Revision 1.3 2000/07/13 12:08:28 michael
  1538. + patched to 1.1.0 with former 1.09patch from peter
  1539. Revision 1.2 2000/07/13 11:32:53 michael
  1540. + removed logs
  1541. }