types.pas 67 KB

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