types.pas 62 KB

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