types.pas 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836
  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:=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) 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 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:=(psetdef(def1)^.elementtype.def^.deftype=psetdef(def2)^.elementtype.def^.deftype)
  933. else
  934. b:=true;
  935. end
  936. else
  937. if (def1^.deftype=procvardef) and (def2^.deftype=procvardef) then
  938. begin
  939. { poassembler isn't important for compatibility }
  940. { if a method is assigned to a methodpointer }
  941. { is checked before }
  942. b:=(pprocvardef(def1)^.proctypeoption=pprocvardef(def2)^.proctypeoption) and
  943. (pprocvardef(def1)^.proccalloptions=pprocvardef(def2)^.proccalloptions) and
  944. ((pprocvardef(def1)^.procoptions * po_compatibility_options)=
  945. (pprocvardef(def2)^.procoptions * po_compatibility_options)) and
  946. is_equal(pprocvardef(def1)^.rettype.def,pprocvardef(def2)^.rettype.def) and
  947. equal_paras(pprocvardef(def1)^.para,pprocvardef(def2)^.para,cp_all);
  948. end
  949. else
  950. if (def1^.deftype=arraydef) and (def2^.deftype=arraydef) then
  951. begin
  952. if is_dynamic_array(def1) and is_dynamic_array(def2) then
  953. b:=is_equal(parraydef(def1)^.elementtype.def,parraydef(def2)^.elementtype.def)
  954. else
  955. if is_array_of_const(def1) or is_array_of_const(def2) then
  956. begin
  957. b:=(is_array_of_const(def1) and is_array_of_const(def2)) or
  958. (is_array_of_const(def1) and is_array_constructor(def2)) or
  959. (is_array_of_const(def2) and is_array_constructor(def1));
  960. end
  961. else
  962. if is_open_array(def1) or is_open_array(def2) then
  963. begin
  964. b:=is_equal(parraydef(def1)^.elementtype.def,parraydef(def2)^.elementtype.def);
  965. end
  966. else
  967. begin
  968. b:=not(m_tp in aktmodeswitches) and
  969. not(m_delphi in aktmodeswitches) and
  970. (parraydef(def1)^.lowrange=parraydef(def2)^.lowrange) and
  971. (parraydef(def1)^.highrange=parraydef(def2)^.highrange) and
  972. is_equal(parraydef(def1)^.elementtype.def,parraydef(def2)^.elementtype.def) and
  973. is_equal(parraydef(def1)^.rangetype.def,parraydef(def2)^.rangetype.def);
  974. end;
  975. end
  976. else
  977. if (def1^.deftype=classrefdef) and (def2^.deftype=classrefdef) then
  978. begin
  979. { similar to pointerdef: }
  980. if assigned(def1^.typesym) and (pclassrefdef(def1)^.pointertype.def^.deftype=forwarddef) then
  981. b:=(def1^.typesym=def2^.typesym)
  982. else
  983. b:=is_equal(pclassrefdef(def1)^.pointertype.def,pclassrefdef(def2)^.pointertype.def);
  984. end;
  985. is_equal:=b;
  986. end;
  987. function is_subequal(def1, def2: pdef): boolean;
  988. var
  989. basedef1,basedef2 : penumdef;
  990. Begin
  991. is_subequal := false;
  992. if assigned(def1) and assigned(def2) then
  993. Begin
  994. if (def1^.deftype = orddef) and (def2^.deftype = orddef) then
  995. Begin
  996. { see p.47 of Turbo Pascal 7.01 manual for the separation of types }
  997. { range checking for case statements is done with testrange }
  998. case porddef(def1)^.typ of
  999. u8bit,u16bit,u32bit,
  1000. s8bit,s16bit,s32bit,s64bit,u64bit :
  1001. is_subequal:=(porddef(def2)^.typ in [s64bit,u64bit,s32bit,u32bit,u8bit,s8bit,s16bit,u16bit]);
  1002. bool8bit,bool16bit,bool32bit :
  1003. is_subequal:=(porddef(def2)^.typ in [bool8bit,bool16bit,bool32bit]);
  1004. uchar :
  1005. is_subequal:=(porddef(def2)^.typ=uchar);
  1006. end;
  1007. end
  1008. else
  1009. Begin
  1010. { I assume that both enumerations are equal when the first }
  1011. { pointers are equal. }
  1012. { I changed this to assume that the enums are equal }
  1013. { if the basedefs are equal (FK) }
  1014. if (def1^.deftype=enumdef) and (def2^.deftype=enumdef) then
  1015. Begin
  1016. { get both basedefs }
  1017. basedef1:=penumdef(def1);
  1018. while assigned(basedef1^.basedef) do
  1019. basedef1:=basedef1^.basedef;
  1020. basedef2:=penumdef(def2);
  1021. while assigned(basedef2^.basedef) do
  1022. basedef2:=basedef2^.basedef;
  1023. is_subequal:=basedef1=basedef2;
  1024. {
  1025. if penumdef(def1)^.firstenum = penumdef(def2)^.firstenum then
  1026. is_subequal := TRUE;
  1027. }
  1028. end;
  1029. end;
  1030. end; { endif assigned ... }
  1031. end;
  1032. function assignment_overloaded(from_def,to_def : pdef) : pprocdef;
  1033. var
  1034. passproc : pprocdef;
  1035. convtyp : tconverttype;
  1036. begin
  1037. assignment_overloaded:=nil;
  1038. if assigned(overloaded_operators[_ASSIGNMENT]) then
  1039. passproc:=overloaded_operators[_ASSIGNMENT]^.definition
  1040. else
  1041. exit;
  1042. while passproc<>nil do
  1043. begin
  1044. if is_equal(passproc^.rettype.def,to_def) and
  1045. (is_equal(TParaItem(passproc^.Para.first).paratype.def,from_def) or
  1046. (isconvertable(from_def,TParaItem(passproc^.Para.first).paratype.def,convtyp,nil,ordconstn,false)=1)) then
  1047. begin
  1048. assignment_overloaded:=passproc;
  1049. break;
  1050. end;
  1051. passproc:=passproc^.nextoverloaded;
  1052. end;
  1053. end;
  1054. { Returns:
  1055. 0 - Not convertable
  1056. 1 - Convertable
  1057. 2 - Convertable, but not first choice }
  1058. function isconvertable(def_from,def_to : pdef;
  1059. var doconv : tconverttype;
  1060. fromtree: tnode; fromtreetype : tnodetype;
  1061. explicit : boolean) : byte;
  1062. { Tbasetype: uauto,uvoid,uchar,
  1063. u8bit,u16bit,u32bit,
  1064. s8bit,s16bit,s32,
  1065. bool8bit,bool16bit,bool32bit,
  1066. u64bit,s64bitint }
  1067. type
  1068. tbasedef=(bvoid,bchar,bint,bbool);
  1069. const
  1070. basedeftbl:array[tbasetype] of tbasedef =
  1071. (bvoid,bvoid,bchar,
  1072. bint,bint,bint,
  1073. bint,bint,bint,
  1074. bbool,bbool,bbool,bint,bint,bchar);
  1075. basedefconverts : array[tbasedef,tbasedef] of tconverttype =
  1076. ((tc_not_possible,tc_not_possible,tc_not_possible,tc_not_possible),
  1077. (tc_not_possible,tc_equal,tc_not_possible,tc_not_possible),
  1078. (tc_not_possible,tc_not_possible,tc_int_2_int,tc_int_2_bool),
  1079. (tc_not_possible,tc_not_possible,tc_bool_2_int,tc_bool_2_bool));
  1080. var
  1081. b : byte;
  1082. hd1,hd2 : pdef;
  1083. hct : tconverttype;
  1084. begin
  1085. { safety check }
  1086. if not(assigned(def_from) and assigned(def_to)) then
  1087. begin
  1088. isconvertable:=0;
  1089. exit;
  1090. end;
  1091. { tp7 procvar def support, in tp7 a procvar is always called, if the
  1092. procvar is passed explicit a addrn would be there }
  1093. if (m_tp_procvar in aktmodeswitches) and
  1094. (def_from^.deftype=procvardef) and
  1095. (fromtreetype=loadn) then
  1096. begin
  1097. def_from:=pprocvardef(def_from)^.rettype.def;
  1098. end;
  1099. { we walk the wanted (def_to) types and check then the def_from
  1100. types if there is a conversion possible }
  1101. b:=0;
  1102. case def_to^.deftype of
  1103. orddef :
  1104. begin
  1105. case def_from^.deftype of
  1106. orddef :
  1107. begin
  1108. doconv:=basedefconverts[basedeftbl[porddef(def_from)^.typ],basedeftbl[porddef(def_to)^.typ]];
  1109. b:=1;
  1110. if (doconv=tc_not_possible) or
  1111. ((doconv=tc_int_2_bool) and
  1112. (not explicit) and
  1113. (not is_boolean(def_from))) or
  1114. ((doconv=tc_bool_2_int) and
  1115. (not explicit) and
  1116. (not is_boolean(def_to))) then
  1117. b:=0;
  1118. end;
  1119. enumdef :
  1120. begin
  1121. { needed for char(enum) }
  1122. if explicit then
  1123. begin
  1124. doconv:=tc_int_2_int;
  1125. b:=1;
  1126. end;
  1127. end;
  1128. end;
  1129. end;
  1130. stringdef :
  1131. begin
  1132. case def_from^.deftype of
  1133. stringdef :
  1134. begin
  1135. doconv:=tc_string_2_string;
  1136. b:=1;
  1137. end;
  1138. orddef :
  1139. begin
  1140. { char to string}
  1141. if is_char(def_from) then
  1142. begin
  1143. doconv:=tc_char_2_string;
  1144. b:=1;
  1145. end;
  1146. end;
  1147. arraydef :
  1148. begin
  1149. { array of char to string, the length check is done by the firstpass of this node }
  1150. if is_chararray(def_from) then
  1151. begin
  1152. doconv:=tc_chararray_2_string;
  1153. if (is_shortstring(def_to) and
  1154. (def_from^.size <= 255)) or
  1155. (is_ansistring(def_to) and
  1156. (def_from^.size > 255)) then
  1157. b:=1
  1158. else
  1159. b:=2;
  1160. end;
  1161. end;
  1162. pointerdef :
  1163. begin
  1164. { pchar can be assigned to short/ansistrings,
  1165. but not in tp7 compatible mode }
  1166. if is_pchar(def_from) and not(m_tp7 in aktmodeswitches) then
  1167. begin
  1168. doconv:=tc_pchar_2_string;
  1169. { prefer ansistrings because pchars can overflow shortstrings, }
  1170. { but only if ansistrings are the default (JM) }
  1171. if (is_shortstring(def_to) and
  1172. not(cs_ansistrings in aktlocalswitches)) or
  1173. (is_ansistring(def_to) and
  1174. (cs_ansistrings in aktlocalswitches)) then
  1175. b:=1
  1176. else
  1177. b:=2;
  1178. end;
  1179. end;
  1180. end;
  1181. end;
  1182. floatdef :
  1183. begin
  1184. case def_from^.deftype of
  1185. orddef :
  1186. begin { ordinal to real }
  1187. if is_integer(def_from) then
  1188. begin
  1189. if pfloatdef(def_to)^.typ=f32bit then
  1190. doconv:=tc_int_2_fix
  1191. else
  1192. doconv:=tc_int_2_real;
  1193. b:=1;
  1194. end;
  1195. end;
  1196. floatdef :
  1197. begin { 2 float types ? }
  1198. if pfloatdef(def_from)^.typ=pfloatdef(def_to)^.typ then
  1199. doconv:=tc_equal
  1200. else
  1201. begin
  1202. if pfloatdef(def_from)^.typ=f32bit then
  1203. doconv:=tc_fix_2_real
  1204. else
  1205. if pfloatdef(def_to)^.typ=f32bit then
  1206. doconv:=tc_real_2_fix
  1207. else
  1208. doconv:=tc_real_2_real;
  1209. end;
  1210. b:=1;
  1211. end;
  1212. end;
  1213. end;
  1214. enumdef :
  1215. begin
  1216. if (def_from^.deftype=enumdef) then
  1217. begin
  1218. hd1:=def_from;
  1219. while assigned(penumdef(hd1)^.basedef) do
  1220. hd1:=penumdef(hd1)^.basedef;
  1221. hd2:=def_to;
  1222. while assigned(penumdef(hd2)^.basedef) do
  1223. hd2:=penumdef(hd2)^.basedef;
  1224. if (hd1=hd2) then
  1225. begin
  1226. b:=1;
  1227. { because of packenum they can have different sizes! (JM) }
  1228. doconv:=tc_int_2_int;
  1229. end;
  1230. end;
  1231. end;
  1232. arraydef :
  1233. begin
  1234. { open array is also compatible with a single element of its base type }
  1235. if is_open_array(def_to) and
  1236. is_equal(parraydef(def_to)^.elementtype.def,def_from) then
  1237. begin
  1238. doconv:=tc_equal;
  1239. b:=1;
  1240. end
  1241. else
  1242. begin
  1243. case def_from^.deftype of
  1244. arraydef :
  1245. begin
  1246. { array constructor -> open array }
  1247. if is_open_array(def_to) and
  1248. is_array_constructor(def_from) then
  1249. begin
  1250. if is_void(parraydef(def_from)^.elementtype.def) or
  1251. is_equal(parraydef(def_to)^.elementtype.def,parraydef(def_from)^.elementtype.def) then
  1252. begin
  1253. doconv:=tc_equal;
  1254. b:=1;
  1255. end
  1256. else
  1257. if isconvertable(parraydef(def_from)^.elementtype.def,
  1258. parraydef(def_to)^.elementtype.def,hct,nil,arrayconstructorn,false)<>0 then
  1259. begin
  1260. doconv:=hct;
  1261. b:=2;
  1262. end;
  1263. end
  1264. else
  1265. { tvarrec -> array of constconst }
  1266. if is_array_of_const(def_to) and
  1267. is_equal(parraydef(def_to)^.elementtype.def,parraydef(def_from)^.elementtype.def) then
  1268. begin
  1269. doconv:=tc_equal;
  1270. b:=1;
  1271. end;
  1272. end;
  1273. pointerdef :
  1274. begin
  1275. if is_zero_based_array(def_to) and
  1276. is_equal(ppointerdef(def_from)^.pointertype.def,parraydef(def_to)^.elementtype.def) then
  1277. begin
  1278. doconv:=tc_pointer_2_array;
  1279. b:=1;
  1280. end;
  1281. end;
  1282. stringdef :
  1283. begin
  1284. { string to char array }
  1285. if (not is_special_array(def_to)) and
  1286. is_char(parraydef(def_to)^.elementtype.def) then
  1287. begin
  1288. doconv:=tc_string_2_chararray;
  1289. b:=1;
  1290. end;
  1291. end;
  1292. end;
  1293. end;
  1294. end;
  1295. pointerdef :
  1296. begin
  1297. case def_from^.deftype of
  1298. stringdef :
  1299. begin
  1300. { string constant (which can be part of array constructor)
  1301. to zero terminated string constant }
  1302. if (fromtreetype in [arrayconstructorn,stringconstn]) and
  1303. is_pchar(def_to) then
  1304. begin
  1305. doconv:=tc_cstring_2_pchar;
  1306. b:=1;
  1307. end;
  1308. end;
  1309. orddef :
  1310. begin
  1311. { char constant to zero terminated string constant }
  1312. if (fromtreetype=ordconstn) then
  1313. begin
  1314. if is_equal(def_from,cchardef) and
  1315. is_pchar(def_to) then
  1316. begin
  1317. doconv:=tc_cchar_2_pchar;
  1318. b:=1;
  1319. end
  1320. else
  1321. if is_integer(def_from) then
  1322. begin
  1323. doconv:=tc_cord_2_pointer;
  1324. b:=1;
  1325. end;
  1326. end;
  1327. end;
  1328. arraydef :
  1329. begin
  1330. { chararray to pointer }
  1331. if is_zero_based_array(def_from) and
  1332. is_equal(parraydef(def_from)^.elementtype.def,ppointerdef(def_to)^.pointertype.def) then
  1333. begin
  1334. doconv:=tc_array_2_pointer;
  1335. b:=1;
  1336. end;
  1337. end;
  1338. pointerdef :
  1339. begin
  1340. { child class pointer can be assigned to anchestor pointers }
  1341. if (
  1342. (ppointerdef(def_from)^.pointertype.def^.deftype=objectdef) and
  1343. (ppointerdef(def_to)^.pointertype.def^.deftype=objectdef) and
  1344. pobjectdef(ppointerdef(def_from)^.pointertype.def)^.is_related(
  1345. pobjectdef(ppointerdef(def_to)^.pointertype.def))
  1346. ) or
  1347. { all pointers can be assigned to void-pointer }
  1348. is_equal(ppointerdef(def_to)^.pointertype.def,voiddef) or
  1349. { in my opnion, is this not clean pascal }
  1350. { well, but it's handy to use, it isn't ? (FK) }
  1351. is_equal(ppointerdef(def_from)^.pointertype.def,voiddef) then
  1352. begin
  1353. { but don't allow conversion between farpointer-pointer }
  1354. if (ppointerdef(def_to)^.is_far=ppointerdef(def_from)^.is_far) then
  1355. begin
  1356. doconv:=tc_equal;
  1357. b:=1;
  1358. end;
  1359. end;
  1360. end;
  1361. procvardef :
  1362. begin
  1363. { procedure variable can be assigned to an void pointer }
  1364. { Not anymore. Use the @ operator now.}
  1365. if not(m_tp_procvar in aktmodeswitches) and
  1366. (ppointerdef(def_to)^.pointertype.def^.deftype=orddef) and
  1367. (porddef(ppointerdef(def_to)^.pointertype.def)^.typ=uvoid) then
  1368. begin
  1369. doconv:=tc_equal;
  1370. b:=1;
  1371. end;
  1372. end;
  1373. classrefdef,
  1374. objectdef :
  1375. begin
  1376. { class types and class reference type
  1377. can be assigned to void pointers }
  1378. if (
  1379. is_class_or_interface(def_from) or
  1380. (def_from^.deftype=classrefdef)
  1381. ) and
  1382. (ppointerdef(def_to)^.pointertype.def^.deftype=orddef) and
  1383. (porddef(ppointerdef(def_to)^.pointertype.def)^.typ=uvoid) then
  1384. begin
  1385. doconv:=tc_equal;
  1386. b:=1;
  1387. end;
  1388. end;
  1389. end;
  1390. end;
  1391. setdef :
  1392. begin
  1393. { automatic arrayconstructor -> set conversion }
  1394. if is_array_constructor(def_from) then
  1395. begin
  1396. doconv:=tc_arrayconstructor_2_set;
  1397. b:=1;
  1398. end;
  1399. end;
  1400. procvardef :
  1401. begin
  1402. { proc -> procvar }
  1403. if (def_from^.deftype=procdef) then
  1404. begin
  1405. doconv:=tc_proc_2_procvar;
  1406. if proc_to_procvar_equal(pprocdef(def_from),pprocvardef(def_to)) then
  1407. b:=1;
  1408. end
  1409. else
  1410. { for example delphi allows the assignement from pointers }
  1411. { to procedure variables }
  1412. if (m_pointer_2_procedure in aktmodeswitches) and
  1413. (def_from^.deftype=pointerdef) and
  1414. (ppointerdef(def_from)^.pointertype.def^.deftype=orddef) and
  1415. (porddef(ppointerdef(def_from)^.pointertype.def)^.typ=uvoid) then
  1416. begin
  1417. doconv:=tc_equal;
  1418. b:=1;
  1419. end
  1420. else
  1421. { nil is compatible with procvars }
  1422. if (fromtreetype=niln) then
  1423. begin
  1424. doconv:=tc_equal;
  1425. b:=1;
  1426. end;
  1427. end;
  1428. objectdef :
  1429. begin
  1430. { object pascal objects }
  1431. if (def_from^.deftype=objectdef) and
  1432. pobjectdef(def_from)^.is_related(pobjectdef(def_to)) then
  1433. begin
  1434. doconv:=tc_equal;
  1435. b:=1;
  1436. end
  1437. else
  1438. { Class/interface specific }
  1439. if is_class_or_interface(def_to) then
  1440. begin
  1441. { void pointer also for delphi mode }
  1442. if (m_delphi in aktmodeswitches) and
  1443. is_voidpointer(def_from) then
  1444. begin
  1445. doconv:=tc_equal;
  1446. b:=1;
  1447. end
  1448. else
  1449. { nil is compatible with class instances and interfaces }
  1450. if (fromtreetype=niln) then
  1451. begin
  1452. doconv:=tc_equal;
  1453. b:=1;
  1454. end
  1455. { classes can be assigned to interfaces }
  1456. else if is_interface(def_to) and
  1457. is_class(def_from) and
  1458. assigned(pobjectdef(def_from)^.implementedinterfaces) and
  1459. (pobjectdef(def_from)^.implementedinterfaces^.searchintf(def_to)<>-1) then
  1460. begin
  1461. doconv:=tc_class_2_intf;
  1462. b:=1;
  1463. end;
  1464. end;
  1465. end;
  1466. classrefdef :
  1467. begin
  1468. { class reference types }
  1469. if (def_from^.deftype=classrefdef) then
  1470. begin
  1471. doconv:=tc_equal;
  1472. if pobjectdef(pclassrefdef(def_from)^.pointertype.def)^.is_related(
  1473. pobjectdef(pclassrefdef(def_to)^.pointertype.def)) then
  1474. b:=1;
  1475. end
  1476. else
  1477. { nil is compatible with class references }
  1478. if (fromtreetype=niln) then
  1479. begin
  1480. doconv:=tc_equal;
  1481. b:=1;
  1482. end;
  1483. end;
  1484. filedef :
  1485. begin
  1486. { typed files are all equal to the abstract file type
  1487. name TYPEDFILE in system.pp in is_equal in types.pas
  1488. the problem is that it sholud be also compatible to FILE
  1489. but this would leed to a problem for ASSIGN RESET and REWRITE
  1490. when trying to find the good overloaded function !!
  1491. so all file function are doubled in system.pp
  1492. this is not very beautiful !!}
  1493. if (def_from^.deftype=filedef) and
  1494. (
  1495. (
  1496. (pfiledef(def_from)^.filetyp = ft_typed) and
  1497. (pfiledef(def_to)^.filetyp = ft_typed) and
  1498. (
  1499. (pfiledef(def_from)^.typedfiletype.def = pdef(voiddef)) or
  1500. (pfiledef(def_to)^.typedfiletype.def = pdef(voiddef))
  1501. )
  1502. ) or
  1503. (
  1504. (
  1505. (pfiledef(def_from)^.filetyp = ft_untyped) and
  1506. (pfiledef(def_to)^.filetyp = ft_typed)
  1507. ) or
  1508. (
  1509. (pfiledef(def_from)^.filetyp = ft_typed) and
  1510. (pfiledef(def_to)^.filetyp = ft_untyped)
  1511. )
  1512. )
  1513. ) then
  1514. begin
  1515. doconv:=tc_equal;
  1516. b:=1;
  1517. end
  1518. end;
  1519. else
  1520. begin
  1521. { Interface 2 GUID handling }
  1522. if (def_from^.deftype=errordef) and (def_to=pdef(rec_tguid)) and
  1523. assigned(fromtree) and (fromtree.nodetype=typen) and
  1524. assigned(ttypenode(fromtree).typenodetype) and
  1525. is_interface(ttypenode(fromtree).typenodetype) and
  1526. pobjectdef(ttypenode(fromtree).typenodetype)^.isiidguidvalid then
  1527. begin
  1528. b:=1;
  1529. doconv:=tc_equal;
  1530. end
  1531. else
  1532. { assignment overwritten ?? }
  1533. if assignment_overloaded(def_from,def_to)<>nil then
  1534. b:=2;
  1535. end;
  1536. end;
  1537. isconvertable:=b;
  1538. end;
  1539. function CheckTypes(def1,def2 : pdef) : boolean;
  1540. var
  1541. s1,s2 : string;
  1542. begin
  1543. if not is_equal(def1,def2) then
  1544. begin
  1545. { Crash prevention }
  1546. if (not assigned(def1)) or (not assigned(def2)) then
  1547. Message(type_e_mismatch)
  1548. else
  1549. begin
  1550. s1:=def1^.typename;
  1551. s2:=def2^.typename;
  1552. if (s1<>'<unknown type>') and (s2<>'<unknown type>') then
  1553. Message2(type_e_not_equal_types,def1^.typename,def2^.typename)
  1554. else
  1555. Message(type_e_mismatch);
  1556. end;
  1557. CheckTypes:=false;
  1558. end
  1559. else
  1560. CheckTypes:=true;
  1561. end;
  1562. end.
  1563. {
  1564. $Log$
  1565. Revision 1.30 2001-01-08 21:43:38 peter
  1566. * string isn't compatible with array of char
  1567. Revision 1.29 2000/12/25 00:07:30 peter
  1568. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  1569. tlinkedlist objects)
  1570. Revision 1.28 2000/12/22 22:38:12 peter
  1571. * fixed bug #1286
  1572. Revision 1.27 2000/12/20 15:59:40 jonas
  1573. - removed obsolete special case for range checking of cardinal constants
  1574. at compile time
  1575. Revision 1.26 2000/12/11 19:13:54 jonas
  1576. * fixed range checking of cardinal constants
  1577. * fixed range checking of "qword constants" (they don't really exist,
  1578. but values > high(int64) were set to zero if assigned to qword)
  1579. Revision 1.25 2000/12/08 14:06:11 jonas
  1580. * fix for web bug 1245: arrays of char with size >255 are now passed to
  1581. overloaded procedures which expect ansistrings instead of shortstrings
  1582. if possible
  1583. * pointer to array of chars (when using $t+) are now also considered
  1584. pchars
  1585. Revision 1.24 2000/11/20 15:52:47 jonas
  1586. * testrange now always cuts a constant to the size of the destination
  1587. if a rangeerror occurred
  1588. * changed an "and $ffffffff" to "and (int64($fffffff) shl 4 + $f" to
  1589. work around the constant evaluation problem we currently have
  1590. Revision 1.23 2000/11/13 14:42:41 jonas
  1591. * fix in testrange so that 64bit constants are properly truncated when
  1592. assigned to 32bit vars
  1593. Revision 1.22 2000/11/13 11:30:55 florian
  1594. * some bugs with interfaces and NIL fixed
  1595. Revision 1.21 2000/11/12 23:24:12 florian
  1596. * interfaces are basically running
  1597. Revision 1.20 2000/11/11 16:13:31 peter
  1598. * farpointer and normal pointer aren't compatible
  1599. Revision 1.19 2000/11/06 22:30:30 peter
  1600. * more fixes
  1601. Revision 1.18 2000/11/04 14:25:22 florian
  1602. + merged Attila's changes for interfaces, not tested yet
  1603. Revision 1.17 2000/10/31 22:30:13 peter
  1604. * merged asm result patch part 2
  1605. Revision 1.16 2000/10/31 22:02:55 peter
  1606. * symtable splitted, no real code changes
  1607. Revision 1.15 2000/10/21 18:16:12 florian
  1608. * a lot of changes:
  1609. - basic dyn. array support
  1610. - basic C++ support
  1611. - some work for interfaces done
  1612. ....
  1613. Revision 1.14 2000/10/14 10:14:56 peter
  1614. * moehrendorf oct 2000 rewrite
  1615. Revision 1.13 2000/10/01 19:48:26 peter
  1616. * lot of compile updates for cg11
  1617. Revision 1.12 2000/09/30 16:08:46 peter
  1618. * more cg11 updates
  1619. Revision 1.11 2000/09/24 15:06:32 peter
  1620. * use defines.inc
  1621. Revision 1.10 2000/09/18 12:31:15 jonas
  1622. * fixed bug in push_addr_param for arrays (merged from fixes branch)
  1623. Revision 1.9 2000/09/10 20:16:21 peter
  1624. * array of const isn't equal with array of <type> (merged)
  1625. Revision 1.8 2000/08/19 19:51:03 peter
  1626. * fixed bug with comparing constsym strings
  1627. Revision 1.7 2000/08/16 13:06:07 florian
  1628. + support of 64 bit integer constants
  1629. Revision 1.6 2000/08/13 13:07:18 peter
  1630. * equal_paras now also checks default parameter value
  1631. Revision 1.5 2000/08/12 06:49:22 florian
  1632. + case statement for int64/qword implemented
  1633. Revision 1.4 2000/08/08 19:26:41 peter
  1634. * equal_constsym() needed for default para
  1635. Revision 1.3 2000/07/13 12:08:28 michael
  1636. + patched to 1.1.0 with former 1.09patch from peter
  1637. Revision 1.2 2000/07/13 11:32:53 michael
  1638. + removed logs
  1639. }