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