types.pas 63 KB

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