types.pas 65 KB

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