types.pas 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794
  1. {
  2. $Id$
  3. Copyright (C) 1998-2000 by Florian Klaempfl
  4. This unit provides some help routines for type handling
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit types;
  19. {$i defines.inc}
  20. interface
  21. uses
  22. cobjects,
  23. cpuinfo,
  24. node,
  25. symbase,symtype,symdef,symsym;
  26. type
  27. tmmxtype = (mmxno,mmxu8bit,mmxs8bit,mmxu16bit,mmxs16bit,
  28. mmxu32bit,mmxs32bit,mmxfixed16,mmxsingle);
  29. const
  30. { true if we must never copy this parameter }
  31. never_copy_const_param : boolean = false;
  32. {*****************************************************************************
  33. Basic type functions
  34. *****************************************************************************}
  35. { returns true, if def defines an ordinal type }
  36. function is_ordinal(def : pdef) : boolean;
  37. { returns the min. value of the type }
  38. function get_min_value(def : pdef) : longint;
  39. { returns true, if def defines an ordinal type }
  40. function is_integer(def : pdef) : boolean;
  41. { true if p is a boolean }
  42. function is_boolean(def : pdef) : boolean;
  43. { true if p is a char }
  44. function is_char(def : pdef) : boolean;
  45. { true if p is a void}
  46. function is_void(def : pdef) : boolean;
  47. { true if p is a smallset def }
  48. function is_smallset(p : pdef) : boolean;
  49. { returns true, if def defines a signed data type (only for ordinal types) }
  50. function is_signed(def : pdef) : boolean;
  51. {*****************************************************************************
  52. Array helper functions
  53. *****************************************************************************}
  54. { true, if p points to a zero based (non special like open or
  55. dynamic array def, mainly this is used to see if the array
  56. is convertable to a pointer }
  57. function is_zero_based_array(p : pdef) : boolean;
  58. { true if p points to an open array def }
  59. function is_open_array(p : pdef) : boolean;
  60. { true if p points to a dynamic array def }
  61. function is_dynamic_array(p : pdef) : boolean;
  62. { true, if p points to an array of const def }
  63. function is_array_constructor(p : pdef) : boolean;
  64. { true, if p points to a variant array }
  65. function is_variant_array(p : pdef) : boolean;
  66. { true, if p points to an array of const }
  67. function is_array_of_const(p : pdef) : boolean;
  68. { true, if p points any kind of special array }
  69. function is_special_array(p : pdef) : boolean;
  70. { true if p is a char array def }
  71. function is_chararray(p : pdef) : boolean;
  72. {*****************************************************************************
  73. String helper functions
  74. *****************************************************************************}
  75. { true if p points to an open string def }
  76. function is_open_string(p : pdef) : boolean;
  77. { true if p is an ansi string def }
  78. function is_ansistring(p : pdef) : boolean;
  79. { true if p is a long string def }
  80. function is_longstring(p : pdef) : boolean;
  81. { true if p is a wide string def }
  82. function is_widestring(p : pdef) : boolean;
  83. { true if p is a short string def }
  84. function is_shortstring(p : pdef) : boolean;
  85. { true if p is a pchar def }
  86. function is_pchar(p : pdef) : boolean;
  87. { true if p is a voidpointer def }
  88. function is_voidpointer(p : pdef) : boolean;
  89. { returns true, if def uses FPU }
  90. function is_fpu(def : pdef) : boolean;
  91. { true if the return value is in EAX }
  92. function ret_in_acc(def : pdef) : boolean;
  93. { true if uses a parameter as return value }
  94. function ret_in_param(def : pdef) : boolean;
  95. { true, if def is a 64 bit int type }
  96. function is_64bitint(def : pdef) : boolean;
  97. function push_high_param(def : pdef) : boolean;
  98. { true if a parameter is too large to copy and only the address is pushed }
  99. function push_addr_param(def : pdef) : boolean;
  100. { true, if def1 and def2 are semantical the same }
  101. function is_equal(def1,def2 : pdef) : boolean;
  102. { checks for type compatibility (subgroups of type) }
  103. { used for case statements... probably missing stuff }
  104. { to use on other types }
  105. function is_subequal(def1, def2: pdef): boolean;
  106. type
  107. tconverttype = (
  108. tc_equal,
  109. tc_not_possible,
  110. tc_string_2_string,
  111. tc_char_2_string,
  112. tc_pchar_2_string,
  113. tc_cchar_2_pchar,
  114. tc_cstring_2_pchar,
  115. tc_ansistring_2_pchar,
  116. tc_string_2_chararray,
  117. tc_chararray_2_string,
  118. tc_array_2_pointer,
  119. tc_pointer_2_array,
  120. tc_int_2_int,
  121. tc_int_2_bool,
  122. tc_bool_2_bool,
  123. tc_bool_2_int,
  124. tc_real_2_real,
  125. tc_int_2_real,
  126. tc_int_2_fix,
  127. tc_real_2_fix,
  128. tc_fix_2_real,
  129. tc_proc_2_procvar,
  130. tc_arrayconstructor_2_set,
  131. tc_load_smallset,
  132. tc_cord_2_pointer,
  133. tc_intf_2_string,
  134. tc_intf_2_guid,
  135. 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 : plinkedlist; 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 : plinkedlist; 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 : plinkedlist; acp : compare_type) : boolean;
  244. var
  245. def1,def2 : pparaitem;
  246. begin
  247. def1:=pparaitem(paralist1^.first);
  248. def2:=pparaitem(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:=pparaitem(def1^.next);
  294. def2:=pparaitem(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 : plinkedlist;acp : compare_type) : boolean;
  302. var
  303. def1,def2 : pparaitem;
  304. doconv : tconverttype;
  305. begin
  306. def1:=pparaitem(paralist1^.first);
  307. def2:=pparaitem(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:=pparaitem(def1^.next);
  343. def2:=pparaitem(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:=false;
  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);
  563. end;
  564. { true if p is a voidpointer def }
  565. function is_voidpointer(p : pdef) : boolean;
  566. begin
  567. is_voidpointer:=(p^.deftype=pointerdef) and
  568. is_equal(Ppointerdef(p)^.pointertype.def,voiddef);
  569. end;
  570. { true if p is a smallset def }
  571. function is_smallset(p : pdef) : boolean;
  572. begin
  573. is_smallset:=(p^.deftype=setdef) and
  574. (psetdef(p)^.settype=smallset);
  575. end;
  576. { true if the return value is in accumulator (EAX for i386), D0 for 68k }
  577. function ret_in_acc(def : pdef) : boolean;
  578. begin
  579. ret_in_acc:=(def^.deftype in [orddef,pointerdef,enumdef,classrefdef]) or
  580. ((def^.deftype=stringdef) and (pstringdef(def)^.string_typ in [st_ansistring,st_widestring])) or
  581. ((def^.deftype=procvardef) and not(po_methodpointer in pprocvardef(def)^.procoptions)) or
  582. ((def^.deftype=objectdef) and not is_object(def)) or
  583. ((def^.deftype=setdef) and (psetdef(def)^.settype=smallset)) or
  584. ((def^.deftype=floatdef) and (pfloatdef(def)^.typ=f32bit));
  585. end;
  586. { true, if def is a 64 bit int type }
  587. function is_64bitint(def : pdef) : boolean;
  588. begin
  589. is_64bitint:=(def^.deftype=orddef) and (porddef(def)^.typ in [u64bit,s64bit])
  590. end;
  591. { true if uses a parameter as return value }
  592. function ret_in_param(def : pdef) : boolean;
  593. begin
  594. ret_in_param:=(def^.deftype in [arraydef,recorddef]) or
  595. ((def^.deftype=stringdef) and (pstringdef(def)^.string_typ in [st_shortstring,st_longstring])) or
  596. ((def^.deftype=procvardef) and (po_methodpointer in pprocvardef(def)^.procoptions)) or
  597. ((def^.deftype=objectdef) and is_object(def)) or
  598. ((def^.deftype=setdef) and (psetdef(def)^.settype<>smallset));
  599. end;
  600. function push_high_param(def : pdef) : boolean;
  601. begin
  602. push_high_param:=is_open_array(def) or
  603. is_open_string(def) or
  604. is_array_of_const(def);
  605. end;
  606. { true if a parameter is too large to copy and only the address is pushed }
  607. function push_addr_param(def : pdef) : boolean;
  608. begin
  609. push_addr_param:=false;
  610. if never_copy_const_param then
  611. push_addr_param:=true
  612. else
  613. begin
  614. case def^.deftype of
  615. formaldef :
  616. push_addr_param:=true;
  617. recorddef :
  618. push_addr_param:=(def^.size>4);
  619. arraydef :
  620. push_addr_param:=((Parraydef(def)^.highrange>=Parraydef(def)^.lowrange) and (def^.size>4)) or
  621. is_open_array(def) or
  622. is_array_of_const(def) or
  623. is_array_constructor(def);
  624. objectdef :
  625. push_addr_param:=is_object(def);
  626. stringdef :
  627. push_addr_param:=pstringdef(def)^.string_typ in [st_shortstring,st_longstring];
  628. procvardef :
  629. push_addr_param:=(po_methodpointer in pprocvardef(def)^.procoptions);
  630. setdef :
  631. push_addr_param:=(psetdef(def)^.settype<>smallset);
  632. end;
  633. end;
  634. end;
  635. { test if l is in the range of def, outputs error if out of range }
  636. procedure testrange(def : pdef;var l : tconstexprint);
  637. var
  638. lv,hv: longint;
  639. begin
  640. { for 64 bit types we need only to check if it is less than }
  641. { zero, if def is a qword node }
  642. if is_64bitint(def) then
  643. begin
  644. if (l<0) and (porddef(def)^.typ=u64bit) then
  645. begin
  646. l:=0;
  647. if (cs_check_range in aktlocalswitches) then
  648. Message(parser_e_range_check_error)
  649. else
  650. Message(parser_w_range_check_error);
  651. end;
  652. end
  653. else
  654. begin
  655. getrange(def,lv,hv);
  656. if (def^.deftype=orddef) and
  657. (porddef(def)^.typ=u32bit) then
  658. begin
  659. if lv<=hv then
  660. begin
  661. if (l<lv) or (l>hv) 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. end
  669. else
  670. { this happens with the wrap around problem }
  671. { if lv is positive and hv is over $7ffffff }
  672. { so it seems negative }
  673. begin
  674. if ((l>=0) and (l<lv)) or
  675. ((l<0) and (l>hv)) then
  676. begin
  677. if (cs_check_range in aktlocalswitches) then
  678. Message(parser_e_range_check_error)
  679. else
  680. Message(parser_w_range_check_error);
  681. end;
  682. end;
  683. end
  684. else if (l<lv) or (l>hv) then
  685. begin
  686. if (def^.deftype=enumdef) or
  687. (cs_check_range in aktlocalswitches) then
  688. Message(parser_e_range_check_error)
  689. else
  690. Message(parser_w_range_check_error);
  691. { Fix the value to fit in the allocated space for this type of variable }
  692. case def^.size of
  693. 1: l := l and $ff;
  694. 2: l := l and $ffff;
  695. 4: l := l and $ffffffff;
  696. end
  697. { l:=lv+(l mod (hv-lv+1));}
  698. end;
  699. end;
  700. end;
  701. { return the range from def in l and h }
  702. procedure getrange(def : pdef;var l : longint;var h : longint);
  703. begin
  704. case def^.deftype of
  705. orddef :
  706. begin
  707. l:=porddef(def)^.low;
  708. h:=porddef(def)^.high;
  709. end;
  710. enumdef :
  711. begin
  712. l:=penumdef(def)^.min;
  713. h:=penumdef(def)^.max;
  714. end;
  715. arraydef :
  716. begin
  717. l:=parraydef(def)^.lowrange;
  718. h:=parraydef(def)^.highrange;
  719. end;
  720. else
  721. internalerror(987);
  722. end;
  723. end;
  724. function mmx_type(p : pdef) : tmmxtype;
  725. begin
  726. mmx_type:=mmxno;
  727. if is_mmx_able_array(p) then
  728. begin
  729. if parraydef(p)^.elementtype.def^.deftype=floatdef then
  730. case pfloatdef(parraydef(p)^.elementtype.def)^.typ of
  731. s32real:
  732. mmx_type:=mmxsingle;
  733. f16bit:
  734. mmx_type:=mmxfixed16
  735. end
  736. else
  737. case porddef(parraydef(p)^.elementtype.def)^.typ of
  738. u8bit:
  739. mmx_type:=mmxu8bit;
  740. s8bit:
  741. mmx_type:=mmxs8bit;
  742. u16bit:
  743. mmx_type:=mmxu16bit;
  744. s16bit:
  745. mmx_type:=mmxs16bit;
  746. u32bit:
  747. mmx_type:=mmxu32bit;
  748. s32bit:
  749. mmx_type:=mmxs32bit;
  750. end;
  751. end;
  752. end;
  753. function is_mmx_able_array(p : pdef) : boolean;
  754. begin
  755. {$ifdef SUPPORT_MMX}
  756. if (cs_mmx_saturation in aktlocalswitches) then
  757. begin
  758. is_mmx_able_array:=(p^.deftype=arraydef) and
  759. not(is_special_array(p)) and
  760. (
  761. (
  762. (parraydef(p)^.elementtype.def^.deftype=orddef) and
  763. (
  764. (
  765. (parraydef(p)^.lowrange=0) and
  766. (parraydef(p)^.highrange=1) and
  767. (porddef(parraydef(p)^.elementtype.def)^.typ in [u32bit,s32bit])
  768. )
  769. or
  770. (
  771. (parraydef(p)^.lowrange=0) and
  772. (parraydef(p)^.highrange=3) and
  773. (porddef(parraydef(p)^.elementtype.def)^.typ in [u16bit,s16bit])
  774. )
  775. )
  776. )
  777. or
  778. (
  779. (
  780. (parraydef(p)^.elementtype.def^.deftype=floatdef) and
  781. (
  782. (parraydef(p)^.lowrange=0) and
  783. (parraydef(p)^.highrange=3) and
  784. (pfloatdef(parraydef(p)^.elementtype.def)^.typ=f16bit)
  785. ) or
  786. (
  787. (parraydef(p)^.lowrange=0) and
  788. (parraydef(p)^.highrange=1) and
  789. (pfloatdef(parraydef(p)^.elementtype.def)^.typ=s32real)
  790. )
  791. )
  792. )
  793. );
  794. end
  795. else
  796. begin
  797. is_mmx_able_array:=(p^.deftype=arraydef) and
  798. (
  799. (
  800. (parraydef(p)^.elementtype.def^.deftype=orddef) and
  801. (
  802. (
  803. (parraydef(p)^.lowrange=0) and
  804. (parraydef(p)^.highrange=1) and
  805. (porddef(parraydef(p)^.elementtype.def)^.typ in [u32bit,s32bit])
  806. )
  807. or
  808. (
  809. (parraydef(p)^.lowrange=0) and
  810. (parraydef(p)^.highrange=3) and
  811. (porddef(parraydef(p)^.elementtype.def)^.typ in [u16bit,s16bit])
  812. )
  813. or
  814. (
  815. (parraydef(p)^.lowrange=0) and
  816. (parraydef(p)^.highrange=7) and
  817. (porddef(parraydef(p)^.elementtype.def)^.typ in [u8bit,s8bit])
  818. )
  819. )
  820. )
  821. or
  822. (
  823. (parraydef(p)^.elementtype.def^.deftype=floatdef) and
  824. (
  825. (
  826. (parraydef(p)^.lowrange=0) and
  827. (parraydef(p)^.highrange=3) and
  828. (pfloatdef(parraydef(p)^.elementtype.def)^.typ=f32bit)
  829. )
  830. or
  831. (
  832. (parraydef(p)^.lowrange=0) and
  833. (parraydef(p)^.highrange=1) and
  834. (pfloatdef(parraydef(p)^.elementtype.def)^.typ=s32real)
  835. )
  836. )
  837. )
  838. );
  839. end;
  840. {$else SUPPORT_MMX}
  841. is_mmx_able_array:=false;
  842. {$endif SUPPORT_MMX}
  843. end;
  844. function is_equal(def1,def2 : pdef) : boolean;
  845. var
  846. b : boolean;
  847. hd : pdef;
  848. begin
  849. { both types must exists }
  850. if not (assigned(def1) and assigned(def2)) then
  851. begin
  852. is_equal:=false;
  853. exit;
  854. end;
  855. { be sure, that if there is a stringdef, that this is def1 }
  856. if def2^.deftype=stringdef then
  857. begin
  858. hd:=def1;
  859. def1:=def2;
  860. def2:=hd;
  861. end;
  862. b:=false;
  863. { both point to the same definition ? }
  864. if def1=def2 then
  865. b:=true
  866. else
  867. { pointer with an equal definition are equal }
  868. if (def1^.deftype=pointerdef) and (def2^.deftype=pointerdef) then
  869. begin
  870. { check if both are farpointer }
  871. if (ppointerdef(def1)^.is_far=ppointerdef(def2)^.is_far) then
  872. begin
  873. { here a problem detected in tabsolutesym }
  874. { the types can be forward type !! }
  875. if assigned(def1^.typesym) and (ppointerdef(def1)^.pointertype.def^.deftype=forwarddef) then
  876. b:=(def1^.typesym=def2^.typesym)
  877. else
  878. b:=ppointerdef(def1)^.pointertype.def=ppointerdef(def2)^.pointertype.def;
  879. end
  880. else
  881. b:=false;
  882. end
  883. else
  884. { ordinals are equal only when the ordinal type is equal }
  885. if (def1^.deftype=orddef) and (def2^.deftype=orddef) then
  886. begin
  887. case porddef(def1)^.typ of
  888. u8bit,u16bit,u32bit,
  889. s8bit,s16bit,s32bit:
  890. b:=((porddef(def1)^.typ=porddef(def2)^.typ) and
  891. (porddef(def1)^.low=porddef(def2)^.low) and
  892. (porddef(def1)^.high=porddef(def2)^.high));
  893. uvoid,uchar,
  894. bool8bit,bool16bit,bool32bit:
  895. b:=(porddef(def1)^.typ=porddef(def2)^.typ);
  896. end;
  897. end
  898. else
  899. if (def1^.deftype=floatdef) and (def2^.deftype=floatdef) then
  900. b:=pfloatdef(def1)^.typ=pfloatdef(def2)^.typ
  901. else
  902. { strings with the same length are equal }
  903. if (def1^.deftype=stringdef) and (def2^.deftype=stringdef) and
  904. (pstringdef(def1)^.string_typ=pstringdef(def2)^.string_typ) then
  905. begin
  906. b:=not(is_shortstring(def1)) or
  907. (pstringdef(def1)^.len=pstringdef(def2)^.len);
  908. end
  909. else
  910. if (def1^.deftype=formaldef) and (def2^.deftype=formaldef) then
  911. b:=true
  912. { file types with the same file element type are equal }
  913. { this is a problem for assign !! }
  914. { changed to allow if one is untyped }
  915. { all typed files are equal to the special }
  916. { typed file that has voiddef as elemnt type }
  917. { but must NOT match for text file !!! }
  918. else
  919. if (def1^.deftype=filedef) and (def2^.deftype=filedef) then
  920. b:=(pfiledef(def1)^.filetyp=pfiledef(def2)^.filetyp) and
  921. ((
  922. ((pfiledef(def1)^.typedfiletype.def=nil) and
  923. (pfiledef(def2)^.typedfiletype.def=nil)) or
  924. (
  925. (pfiledef(def1)^.typedfiletype.def<>nil) and
  926. (pfiledef(def2)^.typedfiletype.def<>nil) and
  927. is_equal(pfiledef(def1)^.typedfiletype.def,pfiledef(def2)^.typedfiletype.def)
  928. ) or
  929. ( (pfiledef(def1)^.typedfiletype.def=pdef(voiddef)) or
  930. (pfiledef(def2)^.typedfiletype.def=pdef(voiddef))
  931. )))
  932. { sets with the same element type are equal }
  933. else
  934. if (def1^.deftype=setdef) and (def2^.deftype=setdef) then
  935. begin
  936. if assigned(psetdef(def1)^.elementtype.def) and
  937. assigned(psetdef(def2)^.elementtype.def) then
  938. b:=(psetdef(def1)^.elementtype.def^.deftype=psetdef(def2)^.elementtype.def^.deftype)
  939. else
  940. b:=true;
  941. end
  942. else
  943. if (def1^.deftype=procvardef) and (def2^.deftype=procvardef) then
  944. begin
  945. { poassembler isn't important for compatibility }
  946. { if a method is assigned to a methodpointer }
  947. { is checked before }
  948. b:=(pprocvardef(def1)^.proctypeoption=pprocvardef(def2)^.proctypeoption) and
  949. (pprocvardef(def1)^.proccalloptions=pprocvardef(def2)^.proccalloptions) and
  950. ((pprocvardef(def1)^.procoptions * po_compatibility_options)=
  951. (pprocvardef(def2)^.procoptions * po_compatibility_options)) and
  952. is_equal(pprocvardef(def1)^.rettype.def,pprocvardef(def2)^.rettype.def) and
  953. equal_paras(pprocvardef(def1)^.para,pprocvardef(def2)^.para,cp_all);
  954. end
  955. else
  956. if (def1^.deftype=arraydef) and (def2^.deftype=arraydef) then
  957. begin
  958. if is_dynamic_array(def1) and is_dynamic_array(def2) then
  959. b:=is_equal(parraydef(def1)^.elementtype.def,parraydef(def2)^.elementtype.def)
  960. else
  961. if is_array_of_const(def1) or is_array_of_const(def2) then
  962. begin
  963. b:=(is_array_of_const(def1) and is_array_of_const(def2)) or
  964. (is_array_of_const(def1) and is_array_constructor(def2)) or
  965. (is_array_of_const(def2) and is_array_constructor(def1));
  966. end
  967. else
  968. if is_open_array(def1) or is_open_array(def2) then
  969. begin
  970. b:=is_equal(parraydef(def1)^.elementtype.def,parraydef(def2)^.elementtype.def);
  971. end
  972. else
  973. begin
  974. b:=not(m_tp in aktmodeswitches) and
  975. not(m_delphi in aktmodeswitches) and
  976. (parraydef(def1)^.lowrange=parraydef(def2)^.lowrange) and
  977. (parraydef(def1)^.highrange=parraydef(def2)^.highrange) and
  978. is_equal(parraydef(def1)^.elementtype.def,parraydef(def2)^.elementtype.def) and
  979. is_equal(parraydef(def1)^.rangetype.def,parraydef(def2)^.rangetype.def);
  980. end;
  981. end
  982. else
  983. if (def1^.deftype=classrefdef) and (def2^.deftype=classrefdef) then
  984. begin
  985. { similar to pointerdef: }
  986. if assigned(def1^.typesym) and (pclassrefdef(def1)^.pointertype.def^.deftype=forwarddef) then
  987. b:=(def1^.typesym=def2^.typesym)
  988. else
  989. b:=is_equal(pclassrefdef(def1)^.pointertype.def,pclassrefdef(def2)^.pointertype.def);
  990. end;
  991. is_equal:=b;
  992. end;
  993. function is_subequal(def1, def2: pdef): boolean;
  994. var
  995. basedef1,basedef2 : penumdef;
  996. Begin
  997. is_subequal := false;
  998. if assigned(def1) and assigned(def2) then
  999. Begin
  1000. if (def1^.deftype = orddef) and (def2^.deftype = orddef) then
  1001. Begin
  1002. { see p.47 of Turbo Pascal 7.01 manual for the separation of types }
  1003. { range checking for case statements is done with testrange }
  1004. case porddef(def1)^.typ of
  1005. u8bit,u16bit,u32bit,
  1006. s8bit,s16bit,s32bit,s64bit,u64bit :
  1007. is_subequal:=(porddef(def2)^.typ in [s64bit,u64bit,s32bit,u32bit,u8bit,s8bit,s16bit,u16bit]);
  1008. bool8bit,bool16bit,bool32bit :
  1009. is_subequal:=(porddef(def2)^.typ in [bool8bit,bool16bit,bool32bit]);
  1010. uchar :
  1011. is_subequal:=(porddef(def2)^.typ=uchar);
  1012. end;
  1013. end
  1014. else
  1015. Begin
  1016. { I assume that both enumerations are equal when the first }
  1017. { pointers are equal. }
  1018. { I changed this to assume that the enums are equal }
  1019. { if the basedefs are equal (FK) }
  1020. if (def1^.deftype=enumdef) and (def2^.deftype=enumdef) then
  1021. Begin
  1022. { get both basedefs }
  1023. basedef1:=penumdef(def1);
  1024. while assigned(basedef1^.basedef) do
  1025. basedef1:=basedef1^.basedef;
  1026. basedef2:=penumdef(def2);
  1027. while assigned(basedef2^.basedef) do
  1028. basedef2:=basedef2^.basedef;
  1029. is_subequal:=basedef1=basedef2;
  1030. {
  1031. if penumdef(def1)^.firstenum = penumdef(def2)^.firstenum then
  1032. is_subequal := TRUE;
  1033. }
  1034. end;
  1035. end;
  1036. end; { endif assigned ... }
  1037. end;
  1038. function assignment_overloaded(from_def,to_def : pdef) : pprocdef;
  1039. var
  1040. passproc : pprocdef;
  1041. convtyp : tconverttype;
  1042. begin
  1043. assignment_overloaded:=nil;
  1044. if assigned(overloaded_operators[_ASSIGNMENT]) then
  1045. passproc:=overloaded_operators[_ASSIGNMENT]^.definition
  1046. else
  1047. exit;
  1048. while passproc<>nil do
  1049. begin
  1050. if is_equal(passproc^.rettype.def,to_def) and
  1051. (is_equal(pparaitem(passproc^.para^.first)^.paratype.def,from_def) or
  1052. (isconvertable(from_def,pparaitem(passproc^.para^.first)^.paratype.def,convtyp,nil,ordconstn,false)=1)) then
  1053. begin
  1054. assignment_overloaded:=passproc;
  1055. break;
  1056. end;
  1057. passproc:=passproc^.nextoverloaded;
  1058. end;
  1059. end;
  1060. { Returns:
  1061. 0 - Not convertable
  1062. 1 - Convertable
  1063. 2 - Convertable, but not first choice }
  1064. function isconvertable(def_from,def_to : pdef;
  1065. var doconv : tconverttype;
  1066. fromtree: tnode; fromtreetype : tnodetype;
  1067. explicit : boolean) : byte;
  1068. { Tbasetype: uauto,uvoid,uchar,
  1069. u8bit,u16bit,u32bit,
  1070. s8bit,s16bit,s32,
  1071. bool8bit,bool16bit,bool32bit,
  1072. u64bit,s64bitint }
  1073. type
  1074. tbasedef=(bvoid,bchar,bint,bbool);
  1075. const
  1076. basedeftbl:array[tbasetype] of tbasedef =
  1077. (bvoid,bvoid,bchar,
  1078. bint,bint,bint,
  1079. bint,bint,bint,
  1080. bbool,bbool,bbool,bint,bint,bchar);
  1081. basedefconverts : array[tbasedef,tbasedef] of tconverttype =
  1082. ((tc_not_possible,tc_not_possible,tc_not_possible,tc_not_possible),
  1083. (tc_not_possible,tc_equal,tc_not_possible,tc_not_possible),
  1084. (tc_not_possible,tc_not_possible,tc_int_2_int,tc_int_2_bool),
  1085. (tc_not_possible,tc_not_possible,tc_bool_2_int,tc_bool_2_bool));
  1086. var
  1087. b : byte;
  1088. hd1,hd2 : pdef;
  1089. hct : tconverttype;
  1090. begin
  1091. { safety check }
  1092. if not(assigned(def_from) and assigned(def_to)) then
  1093. begin
  1094. isconvertable:=0;
  1095. exit;
  1096. end;
  1097. { tp7 procvar def support, in tp7 a procvar is always called, if the
  1098. procvar is passed explicit a addrn would be there }
  1099. if (m_tp_procvar in aktmodeswitches) and
  1100. (def_from^.deftype=procvardef) and
  1101. (fromtreetype=loadn) then
  1102. begin
  1103. def_from:=pprocvardef(def_from)^.rettype.def;
  1104. end;
  1105. { we walk the wanted (def_to) types and check then the def_from
  1106. types if there is a conversion possible }
  1107. b:=0;
  1108. case def_to^.deftype of
  1109. orddef :
  1110. begin
  1111. case def_from^.deftype of
  1112. orddef :
  1113. begin
  1114. doconv:=basedefconverts[basedeftbl[porddef(def_from)^.typ],basedeftbl[porddef(def_to)^.typ]];
  1115. b:=1;
  1116. if (doconv=tc_not_possible) or
  1117. ((doconv=tc_int_2_bool) and
  1118. (not explicit) and
  1119. (not is_boolean(def_from))) or
  1120. ((doconv=tc_bool_2_int) and
  1121. (not explicit) and
  1122. (not is_boolean(def_to))) then
  1123. b:=0;
  1124. end;
  1125. enumdef :
  1126. begin
  1127. { needed for char(enum) }
  1128. if explicit then
  1129. begin
  1130. doconv:=tc_int_2_int;
  1131. b:=1;
  1132. end;
  1133. end;
  1134. end;
  1135. end;
  1136. stringdef :
  1137. begin
  1138. case def_from^.deftype of
  1139. stringdef :
  1140. begin
  1141. doconv:=tc_string_2_string;
  1142. b:=1;
  1143. end;
  1144. orddef :
  1145. begin
  1146. { char to string}
  1147. if is_char(def_from) then
  1148. begin
  1149. doconv:=tc_char_2_string;
  1150. b:=1;
  1151. end;
  1152. end;
  1153. arraydef :
  1154. begin
  1155. { array of char to string, the length check is done by the firstpass of this node }
  1156. if is_chararray(def_from) then
  1157. begin
  1158. doconv:=tc_chararray_2_string;
  1159. if (not(cs_ansistrings in aktlocalswitches) and
  1160. is_shortstring(def_to)) or
  1161. ((cs_ansistrings in aktlocalswitches) and
  1162. is_ansistring(def_to)) then
  1163. b:=1
  1164. else
  1165. b:=2;
  1166. end;
  1167. end;
  1168. pointerdef :
  1169. begin
  1170. { pchar can be assigned to short/ansistrings,
  1171. but not in tp7 compatible mode }
  1172. if is_pchar(def_from) and not(m_tp7 in aktmodeswitches) then
  1173. begin
  1174. doconv:=tc_pchar_2_string;
  1175. b:=1;
  1176. end;
  1177. end;
  1178. end;
  1179. end;
  1180. floatdef :
  1181. begin
  1182. case def_from^.deftype of
  1183. orddef :
  1184. begin { ordinal to real }
  1185. if is_integer(def_from) then
  1186. begin
  1187. if pfloatdef(def_to)^.typ=f32bit then
  1188. doconv:=tc_int_2_fix
  1189. else
  1190. doconv:=tc_int_2_real;
  1191. b:=1;
  1192. end;
  1193. end;
  1194. floatdef :
  1195. begin { 2 float types ? }
  1196. if pfloatdef(def_from)^.typ=pfloatdef(def_to)^.typ then
  1197. doconv:=tc_equal
  1198. else
  1199. begin
  1200. if pfloatdef(def_from)^.typ=f32bit then
  1201. doconv:=tc_fix_2_real
  1202. else
  1203. if pfloatdef(def_to)^.typ=f32bit then
  1204. doconv:=tc_real_2_fix
  1205. else
  1206. doconv:=tc_real_2_real;
  1207. end;
  1208. b:=1;
  1209. end;
  1210. end;
  1211. end;
  1212. enumdef :
  1213. begin
  1214. if (def_from^.deftype=enumdef) then
  1215. begin
  1216. hd1:=def_from;
  1217. while assigned(penumdef(hd1)^.basedef) do
  1218. hd1:=penumdef(hd1)^.basedef;
  1219. hd2:=def_to;
  1220. while assigned(penumdef(hd2)^.basedef) do
  1221. hd2:=penumdef(hd2)^.basedef;
  1222. if (hd1=hd2) then
  1223. begin
  1224. b:=1;
  1225. { because of packenum they can have different sizes! (JM) }
  1226. doconv:=tc_int_2_int;
  1227. end;
  1228. end;
  1229. end;
  1230. arraydef :
  1231. begin
  1232. { open array is also compatible with a single element of its base type }
  1233. if is_open_array(def_to) and
  1234. is_equal(parraydef(def_to)^.elementtype.def,def_from) then
  1235. begin
  1236. doconv:=tc_equal;
  1237. b:=1;
  1238. end
  1239. else
  1240. begin
  1241. case def_from^.deftype of
  1242. arraydef :
  1243. begin
  1244. { array constructor -> open array }
  1245. if is_open_array(def_to) and
  1246. is_array_constructor(def_from) then
  1247. begin
  1248. if is_void(parraydef(def_from)^.elementtype.def) or
  1249. is_equal(parraydef(def_to)^.elementtype.def,parraydef(def_from)^.elementtype.def) then
  1250. begin
  1251. doconv:=tc_equal;
  1252. b:=1;
  1253. end
  1254. else
  1255. if isconvertable(parraydef(def_from)^.elementtype.def,
  1256. parraydef(def_to)^.elementtype.def,hct,nil,arrayconstructorn,false)<>0 then
  1257. begin
  1258. doconv:=hct;
  1259. b:=2;
  1260. end;
  1261. end;
  1262. end;
  1263. pointerdef :
  1264. begin
  1265. if is_zero_based_array(def_to) and
  1266. is_equal(ppointerdef(def_from)^.pointertype.def,parraydef(def_to)^.elementtype.def) then
  1267. begin
  1268. doconv:=tc_pointer_2_array;
  1269. b:=1;
  1270. end;
  1271. end;
  1272. stringdef :
  1273. begin
  1274. { string to array of char}
  1275. if (not(is_special_array(def_to)) or is_open_array(def_to)) and
  1276. is_equal(parraydef(def_to)^.elementtype.def,cchardef) then
  1277. begin
  1278. doconv:=tc_string_2_chararray;
  1279. b:=1;
  1280. end;
  1281. end;
  1282. end;
  1283. end;
  1284. end;
  1285. pointerdef :
  1286. begin
  1287. case def_from^.deftype of
  1288. stringdef :
  1289. begin
  1290. { string constant (which can be part of array constructor)
  1291. to zero terminated string constant }
  1292. if (fromtreetype in [arrayconstructorn,stringconstn]) and
  1293. is_pchar(def_to) then
  1294. begin
  1295. doconv:=tc_cstring_2_pchar;
  1296. b:=1;
  1297. end;
  1298. end;
  1299. orddef :
  1300. begin
  1301. { char constant to zero terminated string constant }
  1302. if (fromtreetype=ordconstn) then
  1303. begin
  1304. if is_equal(def_from,cchardef) and
  1305. is_pchar(def_to) then
  1306. begin
  1307. doconv:=tc_cchar_2_pchar;
  1308. b:=1;
  1309. end
  1310. else
  1311. if is_integer(def_from) then
  1312. begin
  1313. doconv:=tc_cord_2_pointer;
  1314. b:=1;
  1315. end;
  1316. end;
  1317. end;
  1318. arraydef :
  1319. begin
  1320. { chararray to pointer }
  1321. if is_zero_based_array(def_from) and
  1322. is_equal(parraydef(def_from)^.elementtype.def,ppointerdef(def_to)^.pointertype.def) then
  1323. begin
  1324. doconv:=tc_array_2_pointer;
  1325. b:=1;
  1326. end;
  1327. end;
  1328. pointerdef :
  1329. begin
  1330. { child class pointer can be assigned to anchestor pointers }
  1331. if (
  1332. (ppointerdef(def_from)^.pointertype.def^.deftype=objectdef) and
  1333. (ppointerdef(def_to)^.pointertype.def^.deftype=objectdef) and
  1334. pobjectdef(ppointerdef(def_from)^.pointertype.def)^.is_related(
  1335. pobjectdef(ppointerdef(def_to)^.pointertype.def))
  1336. ) or
  1337. { all pointers can be assigned to void-pointer }
  1338. is_equal(ppointerdef(def_to)^.pointertype.def,voiddef) or
  1339. { in my opnion, is this not clean pascal }
  1340. { well, but it's handy to use, it isn't ? (FK) }
  1341. is_equal(ppointerdef(def_from)^.pointertype.def,voiddef) then
  1342. begin
  1343. { but don't allow conversion between farpointer-pointer }
  1344. if (ppointerdef(def_to)^.is_far=ppointerdef(def_from)^.is_far) then
  1345. begin
  1346. doconv:=tc_equal;
  1347. b:=1;
  1348. end;
  1349. end;
  1350. end;
  1351. procvardef :
  1352. begin
  1353. { procedure variable can be assigned to an void pointer }
  1354. { Not anymore. Use the @ operator now.}
  1355. if not(m_tp_procvar in aktmodeswitches) and
  1356. (ppointerdef(def_to)^.pointertype.def^.deftype=orddef) and
  1357. (porddef(ppointerdef(def_to)^.pointertype.def)^.typ=uvoid) then
  1358. begin
  1359. doconv:=tc_equal;
  1360. b:=1;
  1361. end;
  1362. end;
  1363. classrefdef,
  1364. objectdef :
  1365. begin
  1366. { class types and class reference type
  1367. can be assigned to void pointers }
  1368. if (
  1369. is_class_or_interface(def_from) or
  1370. (def_from^.deftype=classrefdef)
  1371. ) and
  1372. (ppointerdef(def_to)^.pointertype.def^.deftype=orddef) and
  1373. (porddef(ppointerdef(def_to)^.pointertype.def)^.typ=uvoid) then
  1374. begin
  1375. doconv:=tc_equal;
  1376. b:=1;
  1377. end;
  1378. end;
  1379. end;
  1380. end;
  1381. setdef :
  1382. begin
  1383. { automatic arrayconstructor -> set conversion }
  1384. if is_array_constructor(def_from) then
  1385. begin
  1386. doconv:=tc_arrayconstructor_2_set;
  1387. b:=1;
  1388. end;
  1389. end;
  1390. procvardef :
  1391. begin
  1392. { proc -> procvar }
  1393. if (def_from^.deftype=procdef) then
  1394. begin
  1395. doconv:=tc_proc_2_procvar;
  1396. if proc_to_procvar_equal(pprocdef(def_from),pprocvardef(def_to)) then
  1397. b:=1;
  1398. end
  1399. else
  1400. { for example delphi allows the assignement from pointers }
  1401. { to procedure variables }
  1402. if (m_pointer_2_procedure in aktmodeswitches) and
  1403. (def_from^.deftype=pointerdef) and
  1404. (ppointerdef(def_from)^.pointertype.def^.deftype=orddef) and
  1405. (porddef(ppointerdef(def_from)^.pointertype.def)^.typ=uvoid) then
  1406. begin
  1407. doconv:=tc_equal;
  1408. b:=1;
  1409. end
  1410. else
  1411. { nil is compatible with procvars }
  1412. if (fromtreetype=niln) then
  1413. begin
  1414. doconv:=tc_equal;
  1415. b:=1;
  1416. end;
  1417. end;
  1418. objectdef :
  1419. begin
  1420. { object pascal objects }
  1421. if (def_from^.deftype=objectdef) and
  1422. pobjectdef(def_from)^.is_related(pobjectdef(def_to)) then
  1423. begin
  1424. doconv:=tc_equal;
  1425. b:=1;
  1426. end
  1427. else
  1428. { Class/interface specific }
  1429. if is_class_or_interface(def_to) then
  1430. begin
  1431. { void pointer also for delphi mode }
  1432. if (m_delphi in aktmodeswitches) and
  1433. is_voidpointer(def_from) then
  1434. begin
  1435. doconv:=tc_equal;
  1436. b:=1;
  1437. end
  1438. else
  1439. { nil is compatible with class instances and interfaces }
  1440. if (fromtreetype=niln) then
  1441. begin
  1442. doconv:=tc_equal;
  1443. b:=1;
  1444. end
  1445. { classes can be assigned to interfaces }
  1446. else if is_interface(def_to) and
  1447. is_class(def_from) and
  1448. assigned(pobjectdef(def_from)^.implementedinterfaces) and
  1449. (pobjectdef(def_from)^.implementedinterfaces^.searchintf(def_to)<>-1) then
  1450. begin
  1451. doconv:=tc_class_2_intf;
  1452. b:=1;
  1453. end;
  1454. end;
  1455. end;
  1456. classrefdef :
  1457. begin
  1458. { class reference types }
  1459. if (def_from^.deftype=classrefdef) then
  1460. begin
  1461. doconv:=tc_equal;
  1462. if pobjectdef(pclassrefdef(def_from)^.pointertype.def)^.is_related(
  1463. pobjectdef(pclassrefdef(def_to)^.pointertype.def)) then
  1464. b:=1;
  1465. end
  1466. else
  1467. { nil is compatible with class references }
  1468. if (fromtreetype=niln) then
  1469. begin
  1470. doconv:=tc_equal;
  1471. b:=1;
  1472. end;
  1473. end;
  1474. filedef :
  1475. begin
  1476. { typed files are all equal to the abstract file type
  1477. name TYPEDFILE in system.pp in is_equal in types.pas
  1478. the problem is that it sholud be also compatible to FILE
  1479. but this would leed to a problem for ASSIGN RESET and REWRITE
  1480. when trying to find the good overloaded function !!
  1481. so all file function are doubled in system.pp
  1482. this is not very beautiful !!}
  1483. if (def_from^.deftype=filedef) and
  1484. (
  1485. (
  1486. (pfiledef(def_from)^.filetyp = ft_typed) and
  1487. (pfiledef(def_to)^.filetyp = ft_typed) and
  1488. (
  1489. (pfiledef(def_from)^.typedfiletype.def = pdef(voiddef)) or
  1490. (pfiledef(def_to)^.typedfiletype.def = pdef(voiddef))
  1491. )
  1492. ) or
  1493. (
  1494. (
  1495. (pfiledef(def_from)^.filetyp = ft_untyped) and
  1496. (pfiledef(def_to)^.filetyp = ft_typed)
  1497. ) or
  1498. (
  1499. (pfiledef(def_from)^.filetyp = ft_typed) and
  1500. (pfiledef(def_to)^.filetyp = ft_untyped)
  1501. )
  1502. )
  1503. ) then
  1504. begin
  1505. doconv:=tc_equal;
  1506. b:=1;
  1507. end
  1508. end;
  1509. else
  1510. begin
  1511. { Interface 2 GUID handling }
  1512. if (def_from^.deftype=errordef) and (def_to=pdef(rec_tguid)) and
  1513. assigned(fromtree) and (fromtree.nodetype=typen) and
  1514. assigned(ttypenode(fromtree).typenodetype) and
  1515. is_interface(ttypenode(fromtree).typenodetype) and
  1516. pobjectdef(ttypenode(fromtree).typenodetype)^.isiidguidvalid then
  1517. begin
  1518. b:=1;
  1519. doconv:=tc_equal;
  1520. end
  1521. else
  1522. { assignment overwritten ?? }
  1523. if assignment_overloaded(def_from,def_to)<>nil then
  1524. b:=2;
  1525. end;
  1526. end;
  1527. isconvertable:=b;
  1528. end;
  1529. function CheckTypes(def1,def2 : pdef) : boolean;
  1530. var
  1531. s1,s2 : string;
  1532. begin
  1533. if not is_equal(def1,def2) then
  1534. begin
  1535. { Crash prevention }
  1536. if (not assigned(def1)) or (not assigned(def2)) then
  1537. Message(type_e_mismatch)
  1538. else
  1539. begin
  1540. s1:=def1^.typename;
  1541. s2:=def2^.typename;
  1542. if (s1<>'<unknown type>') and (s2<>'<unknown type>') then
  1543. Message2(type_e_not_equal_types,def1^.typename,def2^.typename)
  1544. else
  1545. Message(type_e_mismatch);
  1546. end;
  1547. CheckTypes:=false;
  1548. end
  1549. else
  1550. CheckTypes:=true;
  1551. end;
  1552. end.
  1553. {
  1554. $Log$
  1555. Revision 1.23 2000-11-13 14:42:41 jonas
  1556. * fix in testrange so that 64bit constants are properly truncated when
  1557. assigned to 32bit vars
  1558. Revision 1.22 2000/11/13 11:30:55 florian
  1559. * some bugs with interfaces and NIL fixed
  1560. Revision 1.21 2000/11/12 23:24:12 florian
  1561. * interfaces are basically running
  1562. Revision 1.20 2000/11/11 16:13:31 peter
  1563. * farpointer and normal pointer aren't compatible
  1564. Revision 1.19 2000/11/06 22:30:30 peter
  1565. * more fixes
  1566. Revision 1.18 2000/11/04 14:25:22 florian
  1567. + merged Attila's changes for interfaces, not tested yet
  1568. Revision 1.17 2000/10/31 22:30:13 peter
  1569. * merged asm result patch part 2
  1570. Revision 1.16 2000/10/31 22:02:55 peter
  1571. * symtable splitted, no real code changes
  1572. Revision 1.15 2000/10/21 18:16:12 florian
  1573. * a lot of changes:
  1574. - basic dyn. array support
  1575. - basic C++ support
  1576. - some work for interfaces done
  1577. ....
  1578. Revision 1.14 2000/10/14 10:14:56 peter
  1579. * moehrendorf oct 2000 rewrite
  1580. Revision 1.13 2000/10/01 19:48:26 peter
  1581. * lot of compile updates for cg11
  1582. Revision 1.12 2000/09/30 16:08:46 peter
  1583. * more cg11 updates
  1584. Revision 1.11 2000/09/24 15:06:32 peter
  1585. * use defines.inc
  1586. Revision 1.10 2000/09/18 12:31:15 jonas
  1587. * fixed bug in push_addr_param for arrays (merged from fixes branch)
  1588. Revision 1.9 2000/09/10 20:16:21 peter
  1589. * array of const isn't equal with array of <type> (merged)
  1590. Revision 1.8 2000/08/19 19:51:03 peter
  1591. * fixed bug with comparing constsym strings
  1592. Revision 1.7 2000/08/16 13:06:07 florian
  1593. + support of 64 bit integer constants
  1594. Revision 1.6 2000/08/13 13:07:18 peter
  1595. * equal_paras now also checks default parameter value
  1596. Revision 1.5 2000/08/12 06:49:22 florian
  1597. + case statement for int64/qword implemented
  1598. Revision 1.4 2000/08/08 19:26:41 peter
  1599. * equal_constsym() needed for default para
  1600. Revision 1.3 2000/07/13 12:08:28 michael
  1601. + patched to 1.1.0 with former 1.09patch from peter
  1602. Revision 1.2 2000/07/13 11:32:53 michael
  1603. + removed logs
  1604. }