types.pas 68 KB

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