types.pas 64 KB

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