types.pas 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789
  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. begin
  640. { for 64 bit types we need only to check if it is less than }
  641. { zero, if def is a qword node }
  642. if is_64bitint(def) then
  643. begin
  644. if (l<0) and (porddef(def)^.typ=u64bit) then
  645. begin
  646. l:=0;
  647. if (cs_check_range in aktlocalswitches) then
  648. Message(parser_e_range_check_error)
  649. else
  650. Message(parser_w_range_check_error);
  651. end;
  652. end
  653. else
  654. begin
  655. getrange(def,lv,hv);
  656. if (def^.deftype=orddef) and
  657. (porddef(def)^.typ=u32bit) then
  658. begin
  659. if lv<=hv then
  660. begin
  661. if (l<lv) or (l>hv) then
  662. begin
  663. if (cs_check_range in aktlocalswitches) then
  664. Message(parser_e_range_check_error)
  665. else
  666. Message(parser_w_range_check_error);
  667. end;
  668. end
  669. else
  670. { this happens with the wrap around problem }
  671. { if lv is positive and hv is over $7ffffff }
  672. { so it seems negative }
  673. begin
  674. if ((l>=0) and (l<lv)) or
  675. ((l<0) and (l>hv)) then
  676. begin
  677. if (cs_check_range in aktlocalswitches) then
  678. Message(parser_e_range_check_error)
  679. else
  680. Message(parser_w_range_check_error);
  681. end;
  682. end;
  683. end
  684. else if (l<lv) or (l>hv) then
  685. begin
  686. if (def^.deftype=enumdef) or
  687. (cs_check_range in aktlocalswitches) then
  688. Message(parser_e_range_check_error)
  689. else
  690. Message(parser_w_range_check_error);
  691. { Fix the value to fit in the allocated space for this type of variable }
  692. case def^.size of
  693. 1: l := l and $ff;
  694. 2: l := l and $ffff;
  695. end
  696. { l:=lv+(l mod (hv-lv+1));}
  697. end;
  698. end;
  699. end;
  700. { return the range from def in l and h }
  701. procedure getrange(def : pdef;var l : longint;var h : longint);
  702. begin
  703. case def^.deftype of
  704. orddef :
  705. begin
  706. l:=porddef(def)^.low;
  707. h:=porddef(def)^.high;
  708. end;
  709. enumdef :
  710. begin
  711. l:=penumdef(def)^.min;
  712. h:=penumdef(def)^.max;
  713. end;
  714. arraydef :
  715. begin
  716. l:=parraydef(def)^.lowrange;
  717. h:=parraydef(def)^.highrange;
  718. end;
  719. else
  720. internalerror(987);
  721. end;
  722. end;
  723. function mmx_type(p : pdef) : tmmxtype;
  724. begin
  725. mmx_type:=mmxno;
  726. if is_mmx_able_array(p) then
  727. begin
  728. if parraydef(p)^.elementtype.def^.deftype=floatdef then
  729. case pfloatdef(parraydef(p)^.elementtype.def)^.typ of
  730. s32real:
  731. mmx_type:=mmxsingle;
  732. f16bit:
  733. mmx_type:=mmxfixed16
  734. end
  735. else
  736. case porddef(parraydef(p)^.elementtype.def)^.typ of
  737. u8bit:
  738. mmx_type:=mmxu8bit;
  739. s8bit:
  740. mmx_type:=mmxs8bit;
  741. u16bit:
  742. mmx_type:=mmxu16bit;
  743. s16bit:
  744. mmx_type:=mmxs16bit;
  745. u32bit:
  746. mmx_type:=mmxu32bit;
  747. s32bit:
  748. mmx_type:=mmxs32bit;
  749. end;
  750. end;
  751. end;
  752. function is_mmx_able_array(p : pdef) : boolean;
  753. begin
  754. {$ifdef SUPPORT_MMX}
  755. if (cs_mmx_saturation in aktlocalswitches) then
  756. begin
  757. is_mmx_able_array:=(p^.deftype=arraydef) and
  758. not(is_special_array(p)) and
  759. (
  760. (
  761. (parraydef(p)^.elementtype.def^.deftype=orddef) and
  762. (
  763. (
  764. (parraydef(p)^.lowrange=0) and
  765. (parraydef(p)^.highrange=1) and
  766. (porddef(parraydef(p)^.elementtype.def)^.typ in [u32bit,s32bit])
  767. )
  768. or
  769. (
  770. (parraydef(p)^.lowrange=0) and
  771. (parraydef(p)^.highrange=3) and
  772. (porddef(parraydef(p)^.elementtype.def)^.typ in [u16bit,s16bit])
  773. )
  774. )
  775. )
  776. or
  777. (
  778. (
  779. (parraydef(p)^.elementtype.def^.deftype=floatdef) and
  780. (
  781. (parraydef(p)^.lowrange=0) and
  782. (parraydef(p)^.highrange=3) and
  783. (pfloatdef(parraydef(p)^.elementtype.def)^.typ=f16bit)
  784. ) or
  785. (
  786. (parraydef(p)^.lowrange=0) and
  787. (parraydef(p)^.highrange=1) and
  788. (pfloatdef(parraydef(p)^.elementtype.def)^.typ=s32real)
  789. )
  790. )
  791. )
  792. );
  793. end
  794. else
  795. begin
  796. is_mmx_able_array:=(p^.deftype=arraydef) and
  797. (
  798. (
  799. (parraydef(p)^.elementtype.def^.deftype=orddef) and
  800. (
  801. (
  802. (parraydef(p)^.lowrange=0) and
  803. (parraydef(p)^.highrange=1) and
  804. (porddef(parraydef(p)^.elementtype.def)^.typ in [u32bit,s32bit])
  805. )
  806. or
  807. (
  808. (parraydef(p)^.lowrange=0) and
  809. (parraydef(p)^.highrange=3) and
  810. (porddef(parraydef(p)^.elementtype.def)^.typ in [u16bit,s16bit])
  811. )
  812. or
  813. (
  814. (parraydef(p)^.lowrange=0) and
  815. (parraydef(p)^.highrange=7) and
  816. (porddef(parraydef(p)^.elementtype.def)^.typ in [u8bit,s8bit])
  817. )
  818. )
  819. )
  820. or
  821. (
  822. (parraydef(p)^.elementtype.def^.deftype=floatdef) and
  823. (
  824. (
  825. (parraydef(p)^.lowrange=0) and
  826. (parraydef(p)^.highrange=3) and
  827. (pfloatdef(parraydef(p)^.elementtype.def)^.typ=f32bit)
  828. )
  829. or
  830. (
  831. (parraydef(p)^.lowrange=0) and
  832. (parraydef(p)^.highrange=1) and
  833. (pfloatdef(parraydef(p)^.elementtype.def)^.typ=s32real)
  834. )
  835. )
  836. )
  837. );
  838. end;
  839. {$else SUPPORT_MMX}
  840. is_mmx_able_array:=false;
  841. {$endif SUPPORT_MMX}
  842. end;
  843. function is_equal(def1,def2 : pdef) : boolean;
  844. var
  845. b : boolean;
  846. hd : pdef;
  847. begin
  848. { both types must exists }
  849. if not (assigned(def1) and assigned(def2)) then
  850. begin
  851. is_equal:=false;
  852. exit;
  853. end;
  854. { be sure, that if there is a stringdef, that this is def1 }
  855. if def2^.deftype=stringdef then
  856. begin
  857. hd:=def1;
  858. def1:=def2;
  859. def2:=hd;
  860. end;
  861. b:=false;
  862. { both point to the same definition ? }
  863. if def1=def2 then
  864. b:=true
  865. else
  866. { pointer with an equal definition are equal }
  867. if (def1^.deftype=pointerdef) and (def2^.deftype=pointerdef) then
  868. begin
  869. { check if both are farpointer }
  870. if (ppointerdef(def1)^.is_far=ppointerdef(def2)^.is_far) then
  871. begin
  872. { here a problem detected in tabsolutesym }
  873. { the types can be forward type !! }
  874. if assigned(def1^.typesym) and (ppointerdef(def1)^.pointertype.def^.deftype=forwarddef) then
  875. b:=(def1^.typesym=def2^.typesym)
  876. else
  877. b:=ppointerdef(def1)^.pointertype.def=ppointerdef(def2)^.pointertype.def;
  878. end
  879. else
  880. b:=false;
  881. end
  882. else
  883. { ordinals are equal only when the ordinal type is equal }
  884. if (def1^.deftype=orddef) and (def2^.deftype=orddef) then
  885. begin
  886. case porddef(def1)^.typ of
  887. u8bit,u16bit,u32bit,
  888. s8bit,s16bit,s32bit:
  889. b:=((porddef(def1)^.typ=porddef(def2)^.typ) and
  890. (porddef(def1)^.low=porddef(def2)^.low) and
  891. (porddef(def1)^.high=porddef(def2)^.high));
  892. uvoid,uchar,
  893. bool8bit,bool16bit,bool32bit:
  894. b:=(porddef(def1)^.typ=porddef(def2)^.typ);
  895. end;
  896. end
  897. else
  898. if (def1^.deftype=floatdef) and (def2^.deftype=floatdef) then
  899. b:=pfloatdef(def1)^.typ=pfloatdef(def2)^.typ
  900. else
  901. { strings with the same length are equal }
  902. if (def1^.deftype=stringdef) and (def2^.deftype=stringdef) and
  903. (pstringdef(def1)^.string_typ=pstringdef(def2)^.string_typ) then
  904. begin
  905. b:=not(is_shortstring(def1)) or
  906. (pstringdef(def1)^.len=pstringdef(def2)^.len);
  907. end
  908. else
  909. if (def1^.deftype=formaldef) and (def2^.deftype=formaldef) then
  910. b:=true
  911. { file types with the same file element type are equal }
  912. { this is a problem for assign !! }
  913. { changed to allow if one is untyped }
  914. { all typed files are equal to the special }
  915. { typed file that has voiddef as elemnt type }
  916. { but must NOT match for text file !!! }
  917. else
  918. if (def1^.deftype=filedef) and (def2^.deftype=filedef) then
  919. b:=(pfiledef(def1)^.filetyp=pfiledef(def2)^.filetyp) and
  920. ((
  921. ((pfiledef(def1)^.typedfiletype.def=nil) and
  922. (pfiledef(def2)^.typedfiletype.def=nil)) or
  923. (
  924. (pfiledef(def1)^.typedfiletype.def<>nil) and
  925. (pfiledef(def2)^.typedfiletype.def<>nil) and
  926. is_equal(pfiledef(def1)^.typedfiletype.def,pfiledef(def2)^.typedfiletype.def)
  927. ) or
  928. ( (pfiledef(def1)^.typedfiletype.def=pdef(voiddef)) or
  929. (pfiledef(def2)^.typedfiletype.def=pdef(voiddef))
  930. )))
  931. { sets with the same element type are equal }
  932. else
  933. if (def1^.deftype=setdef) and (def2^.deftype=setdef) then
  934. begin
  935. if assigned(psetdef(def1)^.elementtype.def) and
  936. assigned(psetdef(def2)^.elementtype.def) then
  937. b:=(psetdef(def1)^.elementtype.def^.deftype=psetdef(def2)^.elementtype.def^.deftype)
  938. else
  939. b:=true;
  940. end
  941. else
  942. if (def1^.deftype=procvardef) and (def2^.deftype=procvardef) then
  943. begin
  944. { poassembler isn't important for compatibility }
  945. { if a method is assigned to a methodpointer }
  946. { is checked before }
  947. b:=(pprocvardef(def1)^.proctypeoption=pprocvardef(def2)^.proctypeoption) and
  948. (pprocvardef(def1)^.proccalloptions=pprocvardef(def2)^.proccalloptions) and
  949. ((pprocvardef(def1)^.procoptions * po_compatibility_options)=
  950. (pprocvardef(def2)^.procoptions * po_compatibility_options)) and
  951. is_equal(pprocvardef(def1)^.rettype.def,pprocvardef(def2)^.rettype.def) and
  952. equal_paras(pprocvardef(def1)^.para,pprocvardef(def2)^.para,cp_all);
  953. end
  954. else
  955. if (def1^.deftype=arraydef) and (def2^.deftype=arraydef) then
  956. begin
  957. if is_dynamic_array(def1) and is_dynamic_array(def2) then
  958. b:=is_equal(parraydef(def1)^.elementtype.def,parraydef(def2)^.elementtype.def)
  959. else
  960. if is_array_of_const(def1) or is_array_of_const(def2) then
  961. begin
  962. b:=(is_array_of_const(def1) and is_array_of_const(def2)) or
  963. (is_array_of_const(def1) and is_array_constructor(def2)) or
  964. (is_array_of_const(def2) and is_array_constructor(def1));
  965. end
  966. else
  967. if is_open_array(def1) or is_open_array(def2) then
  968. begin
  969. b:=is_equal(parraydef(def1)^.elementtype.def,parraydef(def2)^.elementtype.def);
  970. end
  971. else
  972. begin
  973. b:=not(m_tp in aktmodeswitches) and
  974. not(m_delphi in aktmodeswitches) and
  975. (parraydef(def1)^.lowrange=parraydef(def2)^.lowrange) and
  976. (parraydef(def1)^.highrange=parraydef(def2)^.highrange) and
  977. is_equal(parraydef(def1)^.elementtype.def,parraydef(def2)^.elementtype.def) and
  978. is_equal(parraydef(def1)^.rangetype.def,parraydef(def2)^.rangetype.def);
  979. end;
  980. end
  981. else
  982. if (def1^.deftype=classrefdef) and (def2^.deftype=classrefdef) then
  983. begin
  984. { similar to pointerdef: }
  985. if assigned(def1^.typesym) and (pclassrefdef(def1)^.pointertype.def^.deftype=forwarddef) then
  986. b:=(def1^.typesym=def2^.typesym)
  987. else
  988. b:=is_equal(pclassrefdef(def1)^.pointertype.def,pclassrefdef(def2)^.pointertype.def);
  989. end;
  990. is_equal:=b;
  991. end;
  992. function is_subequal(def1, def2: pdef): boolean;
  993. var
  994. basedef1,basedef2 : penumdef;
  995. Begin
  996. is_subequal := false;
  997. if assigned(def1) and assigned(def2) then
  998. Begin
  999. if (def1^.deftype = orddef) and (def2^.deftype = orddef) then
  1000. Begin
  1001. { see p.47 of Turbo Pascal 7.01 manual for the separation of types }
  1002. { range checking for case statements is done with testrange }
  1003. case porddef(def1)^.typ of
  1004. u8bit,u16bit,u32bit,
  1005. s8bit,s16bit,s32bit,s64bit,u64bit :
  1006. is_subequal:=(porddef(def2)^.typ in [s64bit,u64bit,s32bit,u32bit,u8bit,s8bit,s16bit,u16bit]);
  1007. bool8bit,bool16bit,bool32bit :
  1008. is_subequal:=(porddef(def2)^.typ in [bool8bit,bool16bit,bool32bit]);
  1009. uchar :
  1010. is_subequal:=(porddef(def2)^.typ=uchar);
  1011. end;
  1012. end
  1013. else
  1014. Begin
  1015. { I assume that both enumerations are equal when the first }
  1016. { pointers are equal. }
  1017. { I changed this to assume that the enums are equal }
  1018. { if the basedefs are equal (FK) }
  1019. if (def1^.deftype=enumdef) and (def2^.deftype=enumdef) then
  1020. Begin
  1021. { get both basedefs }
  1022. basedef1:=penumdef(def1);
  1023. while assigned(basedef1^.basedef) do
  1024. basedef1:=basedef1^.basedef;
  1025. basedef2:=penumdef(def2);
  1026. while assigned(basedef2^.basedef) do
  1027. basedef2:=basedef2^.basedef;
  1028. is_subequal:=basedef1=basedef2;
  1029. {
  1030. if penumdef(def1)^.firstenum = penumdef(def2)^.firstenum then
  1031. is_subequal := TRUE;
  1032. }
  1033. end;
  1034. end;
  1035. end; { endif assigned ... }
  1036. end;
  1037. function assignment_overloaded(from_def,to_def : pdef) : pprocdef;
  1038. var
  1039. passproc : pprocdef;
  1040. convtyp : tconverttype;
  1041. begin
  1042. assignment_overloaded:=nil;
  1043. if assigned(overloaded_operators[_ASSIGNMENT]) then
  1044. passproc:=overloaded_operators[_ASSIGNMENT]^.definition
  1045. else
  1046. exit;
  1047. while passproc<>nil do
  1048. begin
  1049. if is_equal(passproc^.rettype.def,to_def) and
  1050. (is_equal(pparaitem(passproc^.para^.first)^.paratype.def,from_def) or
  1051. (isconvertable(from_def,pparaitem(passproc^.para^.first)^.paratype.def,convtyp,nil,ordconstn,false)=1)) then
  1052. begin
  1053. assignment_overloaded:=passproc;
  1054. break;
  1055. end;
  1056. passproc:=passproc^.nextoverloaded;
  1057. end;
  1058. end;
  1059. { Returns:
  1060. 0 - Not convertable
  1061. 1 - Convertable
  1062. 2 - Convertable, but not first choice }
  1063. function isconvertable(def_from,def_to : pdef;
  1064. var doconv : tconverttype;
  1065. fromtree: tnode; fromtreetype : tnodetype;
  1066. explicit : boolean) : byte;
  1067. { Tbasetype: uauto,uvoid,uchar,
  1068. u8bit,u16bit,u32bit,
  1069. s8bit,s16bit,s32,
  1070. bool8bit,bool16bit,bool32bit,
  1071. u64bit,s64bitint }
  1072. type
  1073. tbasedef=(bvoid,bchar,bint,bbool);
  1074. const
  1075. basedeftbl:array[tbasetype] of tbasedef =
  1076. (bvoid,bvoid,bchar,
  1077. bint,bint,bint,
  1078. bint,bint,bint,
  1079. bbool,bbool,bbool,bint,bint,bchar);
  1080. basedefconverts : array[tbasedef,tbasedef] of tconverttype =
  1081. ((tc_not_possible,tc_not_possible,tc_not_possible,tc_not_possible),
  1082. (tc_not_possible,tc_equal,tc_not_possible,tc_not_possible),
  1083. (tc_not_possible,tc_not_possible,tc_int_2_int,tc_int_2_bool),
  1084. (tc_not_possible,tc_not_possible,tc_bool_2_int,tc_bool_2_bool));
  1085. var
  1086. b : byte;
  1087. hd1,hd2 : pdef;
  1088. hct : tconverttype;
  1089. begin
  1090. { safety check }
  1091. if not(assigned(def_from) and assigned(def_to)) then
  1092. begin
  1093. isconvertable:=0;
  1094. exit;
  1095. end;
  1096. { tp7 procvar def support, in tp7 a procvar is always called, if the
  1097. procvar is passed explicit a addrn would be there }
  1098. if (m_tp_procvar in aktmodeswitches) and
  1099. (def_from^.deftype=procvardef) and
  1100. (fromtreetype=loadn) then
  1101. begin
  1102. def_from:=pprocvardef(def_from)^.rettype.def;
  1103. end;
  1104. { we walk the wanted (def_to) types and check then the def_from
  1105. types if there is a conversion possible }
  1106. b:=0;
  1107. case def_to^.deftype of
  1108. orddef :
  1109. begin
  1110. case def_from^.deftype of
  1111. orddef :
  1112. begin
  1113. doconv:=basedefconverts[basedeftbl[porddef(def_from)^.typ],basedeftbl[porddef(def_to)^.typ]];
  1114. b:=1;
  1115. if (doconv=tc_not_possible) or
  1116. ((doconv=tc_int_2_bool) and
  1117. (not explicit) and
  1118. (not is_boolean(def_from))) or
  1119. ((doconv=tc_bool_2_int) and
  1120. (not explicit) and
  1121. (not is_boolean(def_to))) then
  1122. b:=0;
  1123. end;
  1124. enumdef :
  1125. begin
  1126. { needed for char(enum) }
  1127. if explicit then
  1128. begin
  1129. doconv:=tc_int_2_int;
  1130. b:=1;
  1131. end;
  1132. end;
  1133. end;
  1134. end;
  1135. stringdef :
  1136. begin
  1137. case def_from^.deftype of
  1138. stringdef :
  1139. begin
  1140. doconv:=tc_string_2_string;
  1141. b:=1;
  1142. end;
  1143. orddef :
  1144. begin
  1145. { char to string}
  1146. if is_char(def_from) then
  1147. begin
  1148. doconv:=tc_char_2_string;
  1149. b:=1;
  1150. end;
  1151. end;
  1152. arraydef :
  1153. begin
  1154. { array of char to string, the length check is done by the firstpass of this node }
  1155. if is_chararray(def_from) then
  1156. begin
  1157. doconv:=tc_chararray_2_string;
  1158. if (not(cs_ansistrings in aktlocalswitches) and
  1159. is_shortstring(def_to)) or
  1160. ((cs_ansistrings in aktlocalswitches) and
  1161. is_ansistring(def_to)) then
  1162. b:=1
  1163. else
  1164. b:=2;
  1165. end;
  1166. end;
  1167. pointerdef :
  1168. begin
  1169. { pchar can be assigned to short/ansistrings,
  1170. but not in tp7 compatible mode }
  1171. if is_pchar(def_from) and not(m_tp7 in aktmodeswitches) then
  1172. begin
  1173. doconv:=tc_pchar_2_string;
  1174. b:=1;
  1175. end;
  1176. end;
  1177. end;
  1178. end;
  1179. floatdef :
  1180. begin
  1181. case def_from^.deftype of
  1182. orddef :
  1183. begin { ordinal to real }
  1184. if is_integer(def_from) then
  1185. begin
  1186. if pfloatdef(def_to)^.typ=f32bit then
  1187. doconv:=tc_int_2_fix
  1188. else
  1189. doconv:=tc_int_2_real;
  1190. b:=1;
  1191. end;
  1192. end;
  1193. floatdef :
  1194. begin { 2 float types ? }
  1195. if pfloatdef(def_from)^.typ=pfloatdef(def_to)^.typ then
  1196. doconv:=tc_equal
  1197. else
  1198. begin
  1199. if pfloatdef(def_from)^.typ=f32bit then
  1200. doconv:=tc_fix_2_real
  1201. else
  1202. if pfloatdef(def_to)^.typ=f32bit then
  1203. doconv:=tc_real_2_fix
  1204. else
  1205. doconv:=tc_real_2_real;
  1206. end;
  1207. b:=1;
  1208. end;
  1209. end;
  1210. end;
  1211. enumdef :
  1212. begin
  1213. if (def_from^.deftype=enumdef) then
  1214. begin
  1215. hd1:=def_from;
  1216. while assigned(penumdef(hd1)^.basedef) do
  1217. hd1:=penumdef(hd1)^.basedef;
  1218. hd2:=def_to;
  1219. while assigned(penumdef(hd2)^.basedef) do
  1220. hd2:=penumdef(hd2)^.basedef;
  1221. if (hd1=hd2) then
  1222. begin
  1223. b:=1;
  1224. { because of packenum they can have different sizes! (JM) }
  1225. doconv:=tc_int_2_int;
  1226. end;
  1227. end;
  1228. end;
  1229. arraydef :
  1230. begin
  1231. { open array is also compatible with a single element of its base type }
  1232. if is_open_array(def_to) and
  1233. is_equal(parraydef(def_to)^.elementtype.def,def_from) then
  1234. begin
  1235. doconv:=tc_equal;
  1236. b:=1;
  1237. end
  1238. else
  1239. begin
  1240. case def_from^.deftype of
  1241. arraydef :
  1242. begin
  1243. { array constructor -> open array }
  1244. if is_open_array(def_to) and
  1245. is_array_constructor(def_from) then
  1246. begin
  1247. if is_void(parraydef(def_from)^.elementtype.def) or
  1248. is_equal(parraydef(def_to)^.elementtype.def,parraydef(def_from)^.elementtype.def) then
  1249. begin
  1250. doconv:=tc_equal;
  1251. b:=1;
  1252. end
  1253. else
  1254. if isconvertable(parraydef(def_from)^.elementtype.def,
  1255. parraydef(def_to)^.elementtype.def,hct,nil,arrayconstructorn,false)<>0 then
  1256. begin
  1257. doconv:=hct;
  1258. b:=2;
  1259. end;
  1260. end;
  1261. end;
  1262. pointerdef :
  1263. begin
  1264. if is_zero_based_array(def_to) and
  1265. is_equal(ppointerdef(def_from)^.pointertype.def,parraydef(def_to)^.elementtype.def) then
  1266. begin
  1267. doconv:=tc_pointer_2_array;
  1268. b:=1;
  1269. end;
  1270. end;
  1271. stringdef :
  1272. begin
  1273. { string to array of char}
  1274. if (not(is_special_array(def_to)) or is_open_array(def_to)) and
  1275. is_equal(parraydef(def_to)^.elementtype.def,cchardef) then
  1276. begin
  1277. doconv:=tc_string_2_chararray;
  1278. b:=1;
  1279. end;
  1280. end;
  1281. end;
  1282. end;
  1283. end;
  1284. pointerdef :
  1285. begin
  1286. case def_from^.deftype of
  1287. stringdef :
  1288. begin
  1289. { string constant (which can be part of array constructor)
  1290. to zero terminated string constant }
  1291. if (fromtreetype in [arrayconstructorn,stringconstn]) and
  1292. is_pchar(def_to) then
  1293. begin
  1294. doconv:=tc_cstring_2_pchar;
  1295. b:=1;
  1296. end;
  1297. end;
  1298. orddef :
  1299. begin
  1300. { char constant to zero terminated string constant }
  1301. if (fromtreetype=ordconstn) then
  1302. begin
  1303. if is_equal(def_from,cchardef) and
  1304. is_pchar(def_to) then
  1305. begin
  1306. doconv:=tc_cchar_2_pchar;
  1307. b:=1;
  1308. end
  1309. else
  1310. if is_integer(def_from) then
  1311. begin
  1312. doconv:=tc_cord_2_pointer;
  1313. b:=1;
  1314. end;
  1315. end;
  1316. end;
  1317. arraydef :
  1318. begin
  1319. { chararray to pointer }
  1320. if is_zero_based_array(def_from) and
  1321. is_equal(parraydef(def_from)^.elementtype.def,ppointerdef(def_to)^.pointertype.def) then
  1322. begin
  1323. doconv:=tc_array_2_pointer;
  1324. b:=1;
  1325. end;
  1326. end;
  1327. pointerdef :
  1328. begin
  1329. { child class pointer can be assigned to anchestor pointers }
  1330. if (
  1331. (ppointerdef(def_from)^.pointertype.def^.deftype=objectdef) and
  1332. (ppointerdef(def_to)^.pointertype.def^.deftype=objectdef) and
  1333. pobjectdef(ppointerdef(def_from)^.pointertype.def)^.is_related(
  1334. pobjectdef(ppointerdef(def_to)^.pointertype.def))
  1335. ) or
  1336. { all pointers can be assigned to void-pointer }
  1337. is_equal(ppointerdef(def_to)^.pointertype.def,voiddef) or
  1338. { in my opnion, is this not clean pascal }
  1339. { well, but it's handy to use, it isn't ? (FK) }
  1340. is_equal(ppointerdef(def_from)^.pointertype.def,voiddef) then
  1341. begin
  1342. { but don't allow conversion between farpointer-pointer }
  1343. if (ppointerdef(def_to)^.is_far=ppointerdef(def_from)^.is_far) then
  1344. begin
  1345. doconv:=tc_equal;
  1346. b:=1;
  1347. end;
  1348. end;
  1349. end;
  1350. procvardef :
  1351. begin
  1352. { procedure variable can be assigned to an void pointer }
  1353. { Not anymore. Use the @ operator now.}
  1354. if not(m_tp_procvar in aktmodeswitches) and
  1355. (ppointerdef(def_to)^.pointertype.def^.deftype=orddef) and
  1356. (porddef(ppointerdef(def_to)^.pointertype.def)^.typ=uvoid) then
  1357. begin
  1358. doconv:=tc_equal;
  1359. b:=1;
  1360. end;
  1361. end;
  1362. classrefdef,
  1363. objectdef :
  1364. begin
  1365. { class types and class reference type
  1366. can be assigned to void pointers }
  1367. if (
  1368. is_class_or_interface(def_from) or
  1369. (def_from^.deftype=classrefdef)
  1370. ) and
  1371. (ppointerdef(def_to)^.pointertype.def^.deftype=orddef) and
  1372. (porddef(ppointerdef(def_to)^.pointertype.def)^.typ=uvoid) then
  1373. begin
  1374. doconv:=tc_equal;
  1375. b:=1;
  1376. end;
  1377. end;
  1378. end;
  1379. end;
  1380. setdef :
  1381. begin
  1382. { automatic arrayconstructor -> set conversion }
  1383. if is_array_constructor(def_from) then
  1384. begin
  1385. doconv:=tc_arrayconstructor_2_set;
  1386. b:=1;
  1387. end;
  1388. end;
  1389. procvardef :
  1390. begin
  1391. { proc -> procvar }
  1392. if (def_from^.deftype=procdef) then
  1393. begin
  1394. doconv:=tc_proc_2_procvar;
  1395. if proc_to_procvar_equal(pprocdef(def_from),pprocvardef(def_to)) then
  1396. b:=1;
  1397. end
  1398. else
  1399. { for example delphi allows the assignement from pointers }
  1400. { to procedure variables }
  1401. if (m_pointer_2_procedure in aktmodeswitches) and
  1402. (def_from^.deftype=pointerdef) and
  1403. (ppointerdef(def_from)^.pointertype.def^.deftype=orddef) and
  1404. (porddef(ppointerdef(def_from)^.pointertype.def)^.typ=uvoid) then
  1405. begin
  1406. doconv:=tc_equal;
  1407. b:=1;
  1408. end
  1409. else
  1410. { nil is compatible with procvars }
  1411. if (fromtreetype=niln) then
  1412. begin
  1413. doconv:=tc_equal;
  1414. b:=1;
  1415. end;
  1416. end;
  1417. objectdef :
  1418. begin
  1419. { object pascal objects }
  1420. if (def_from^.deftype=objectdef) and
  1421. pobjectdef(def_from)^.is_related(pobjectdef(def_to)) then
  1422. begin
  1423. doconv:=tc_equal;
  1424. b:=1;
  1425. end
  1426. else
  1427. { Class/interface specific }
  1428. if is_class_or_interface(def_to) then
  1429. begin
  1430. { void pointer also for delphi mode }
  1431. if (m_delphi in aktmodeswitches) and
  1432. is_voidpointer(def_from) then
  1433. begin
  1434. doconv:=tc_equal;
  1435. b:=1;
  1436. end
  1437. else
  1438. { nil is compatible with class instances and interfaces }
  1439. if (fromtreetype=niln) then
  1440. begin
  1441. doconv:=tc_equal;
  1442. b:=1;
  1443. end
  1444. { classes can be assigned to interfaces }
  1445. else if is_interface(def_to) and
  1446. is_class(def_from) and
  1447. assigned(pobjectdef(def_from)^.implementedinterfaces) and
  1448. (pobjectdef(def_from)^.implementedinterfaces^.searchintf(def_to)<>-1) then
  1449. begin
  1450. doconv:=tc_class_2_intf;
  1451. b:=1;
  1452. end;
  1453. end;
  1454. end;
  1455. classrefdef :
  1456. begin
  1457. { class reference types }
  1458. if (def_from^.deftype=classrefdef) then
  1459. begin
  1460. doconv:=tc_equal;
  1461. if pobjectdef(pclassrefdef(def_from)^.pointertype.def)^.is_related(
  1462. pobjectdef(pclassrefdef(def_to)^.pointertype.def)) then
  1463. b:=1;
  1464. end
  1465. else
  1466. { nil is compatible with class references }
  1467. if (fromtreetype=niln) then
  1468. begin
  1469. doconv:=tc_equal;
  1470. b:=1;
  1471. end;
  1472. end;
  1473. filedef :
  1474. begin
  1475. { typed files are all equal to the abstract file type
  1476. name TYPEDFILE in system.pp in is_equal in types.pas
  1477. the problem is that it sholud be also compatible to FILE
  1478. but this would leed to a problem for ASSIGN RESET and REWRITE
  1479. when trying to find the good overloaded function !!
  1480. so all file function are doubled in system.pp
  1481. this is not very beautiful !!}
  1482. if (def_from^.deftype=filedef) and
  1483. (
  1484. (
  1485. (pfiledef(def_from)^.filetyp = ft_typed) and
  1486. (pfiledef(def_to)^.filetyp = ft_typed) and
  1487. (
  1488. (pfiledef(def_from)^.typedfiletype.def = pdef(voiddef)) or
  1489. (pfiledef(def_to)^.typedfiletype.def = pdef(voiddef))
  1490. )
  1491. ) or
  1492. (
  1493. (
  1494. (pfiledef(def_from)^.filetyp = ft_untyped) and
  1495. (pfiledef(def_to)^.filetyp = ft_typed)
  1496. ) or
  1497. (
  1498. (pfiledef(def_from)^.filetyp = ft_typed) and
  1499. (pfiledef(def_to)^.filetyp = ft_untyped)
  1500. )
  1501. )
  1502. ) then
  1503. begin
  1504. doconv:=tc_equal;
  1505. b:=1;
  1506. end
  1507. end;
  1508. else
  1509. begin
  1510. { Interface 2 GUID handling }
  1511. if (def_from^.deftype=errordef) and (def_to=pdef(rec_tguid)) and
  1512. assigned(fromtree) and (fromtree.nodetype=typen) and
  1513. assigned(ttypenode(fromtree).typenodetype) and
  1514. is_interface(ttypenode(fromtree).typenodetype) and
  1515. pobjectdef(ttypenode(fromtree).typenodetype)^.isiidguidvalid then
  1516. begin
  1517. b:=1;
  1518. doconv:=tc_equal;
  1519. end
  1520. else
  1521. { assignment overwritten ?? }
  1522. if assignment_overloaded(def_from,def_to)<>nil then
  1523. b:=2;
  1524. end;
  1525. end;
  1526. isconvertable:=b;
  1527. end;
  1528. function CheckTypes(def1,def2 : pdef) : boolean;
  1529. var
  1530. s1,s2 : string;
  1531. begin
  1532. if not is_equal(def1,def2) then
  1533. begin
  1534. { Crash prevention }
  1535. if (not assigned(def1)) or (not assigned(def2)) then
  1536. Message(type_e_mismatch)
  1537. else
  1538. begin
  1539. s1:=def1^.typename;
  1540. s2:=def2^.typename;
  1541. if (s1<>'<unknown type>') and (s2<>'<unknown type>') then
  1542. Message2(type_e_not_equal_types,def1^.typename,def2^.typename)
  1543. else
  1544. Message(type_e_mismatch);
  1545. end;
  1546. CheckTypes:=false;
  1547. end
  1548. else
  1549. CheckTypes:=true;
  1550. end;
  1551. end.
  1552. {
  1553. $Log$
  1554. Revision 1.22 2000-11-13 11:30:55 florian
  1555. * some bugs with interfaces and NIL fixed
  1556. Revision 1.21 2000/11/12 23:24:12 florian
  1557. * interfaces are basically running
  1558. Revision 1.20 2000/11/11 16:13:31 peter
  1559. * farpointer and normal pointer aren't compatible
  1560. Revision 1.19 2000/11/06 22:30:30 peter
  1561. * more fixes
  1562. Revision 1.18 2000/11/04 14:25:22 florian
  1563. + merged Attila's changes for interfaces, not tested yet
  1564. Revision 1.17 2000/10/31 22:30:13 peter
  1565. * merged asm result patch part 2
  1566. Revision 1.16 2000/10/31 22:02:55 peter
  1567. * symtable splitted, no real code changes
  1568. Revision 1.15 2000/10/21 18:16:12 florian
  1569. * a lot of changes:
  1570. - basic dyn. array support
  1571. - basic C++ support
  1572. - some work for interfaces done
  1573. ....
  1574. Revision 1.14 2000/10/14 10:14:56 peter
  1575. * moehrendorf oct 2000 rewrite
  1576. Revision 1.13 2000/10/01 19:48:26 peter
  1577. * lot of compile updates for cg11
  1578. Revision 1.12 2000/09/30 16:08:46 peter
  1579. * more cg11 updates
  1580. Revision 1.11 2000/09/24 15:06:32 peter
  1581. * use defines.inc
  1582. Revision 1.10 2000/09/18 12:31:15 jonas
  1583. * fixed bug in push_addr_param for arrays (merged from fixes branch)
  1584. Revision 1.9 2000/09/10 20:16:21 peter
  1585. * array of const isn't equal with array of <type> (merged)
  1586. Revision 1.8 2000/08/19 19:51:03 peter
  1587. * fixed bug with comparing constsym strings
  1588. Revision 1.7 2000/08/16 13:06:07 florian
  1589. + support of 64 bit integer constants
  1590. Revision 1.6 2000/08/13 13:07:18 peter
  1591. * equal_paras now also checks default parameter value
  1592. Revision 1.5 2000/08/12 06:49:22 florian
  1593. + case statement for int64/qword implemented
  1594. Revision 1.4 2000/08/08 19:26:41 peter
  1595. * equal_constsym() needed for default para
  1596. Revision 1.3 2000/07/13 12:08:28 michael
  1597. + patched to 1.1.0 with former 1.09patch from peter
  1598. Revision 1.2 2000/07/13 11:32:53 michael
  1599. + removed logs
  1600. }