types.pas 67 KB

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