types.pas 66 KB

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