types.pas 61 KB

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