types.pas 67 KB

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