types.pas 70 KB

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