types.pas 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958
  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. doconv:=tc_int_2_real;
  1234. b:=1;
  1235. end;
  1236. end;
  1237. floatdef :
  1238. begin { 2 float types ? }
  1239. if tfloatdef(def_from).typ=tfloatdef(def_to).typ then
  1240. doconv:=tc_equal
  1241. else
  1242. doconv:=tc_real_2_real;
  1243. b:=1;
  1244. end;
  1245. end;
  1246. end;
  1247. enumdef :
  1248. begin
  1249. if (def_from.deftype=enumdef) then
  1250. begin
  1251. hd1:=def_from;
  1252. while assigned(tenumdef(hd1).basedef) do
  1253. hd1:=tenumdef(hd1).basedef;
  1254. hd2:=def_to;
  1255. while assigned(tenumdef(hd2).basedef) do
  1256. hd2:=tenumdef(hd2).basedef;
  1257. if (hd1=hd2) then
  1258. begin
  1259. b:=1;
  1260. { because of packenum they can have different sizes! (JM) }
  1261. doconv:=tc_int_2_int;
  1262. end;
  1263. end;
  1264. end;
  1265. arraydef :
  1266. begin
  1267. { open array is also compatible with a single element of its base type }
  1268. if is_open_array(def_to) and
  1269. is_equal(tarraydef(def_to).elementtype.def,def_from) then
  1270. begin
  1271. doconv:=tc_equal;
  1272. b:=1;
  1273. end
  1274. else
  1275. begin
  1276. case def_from.deftype of
  1277. arraydef :
  1278. begin
  1279. { array constructor -> open array }
  1280. if is_open_array(def_to) and
  1281. is_array_constructor(def_from) then
  1282. begin
  1283. if is_void(tarraydef(def_from).elementtype.def) or
  1284. is_equal(tarraydef(def_to).elementtype.def,tarraydef(def_from).elementtype.def) then
  1285. begin
  1286. doconv:=tc_equal;
  1287. b:=1;
  1288. end
  1289. else
  1290. if isconvertable(tarraydef(def_from).elementtype.def,
  1291. tarraydef(def_to).elementtype.def,hct,arrayconstructorn,false)<>0 then
  1292. begin
  1293. doconv:=hct;
  1294. b:=2;
  1295. end;
  1296. end
  1297. else
  1298. { array of tvarrec -> array of const }
  1299. if is_array_of_const(def_to) and
  1300. is_equal(tarraydef(def_to).elementtype.def,tarraydef(def_from).elementtype.def) then
  1301. begin
  1302. doconv:=tc_equal;
  1303. b:=1;
  1304. end;
  1305. end;
  1306. pointerdef :
  1307. begin
  1308. if is_zero_based_array(def_to) and
  1309. is_equal(tpointerdef(def_from).pointertype.def,tarraydef(def_to).elementtype.def) then
  1310. begin
  1311. doconv:=tc_pointer_2_array;
  1312. b:=1;
  1313. end;
  1314. end;
  1315. stringdef :
  1316. begin
  1317. { string to char array }
  1318. if (not is_special_array(def_to)) and
  1319. is_char(tarraydef(def_to).elementtype.def) then
  1320. begin
  1321. doconv:=tc_string_2_chararray;
  1322. b:=1;
  1323. end;
  1324. end;
  1325. recorddef :
  1326. begin
  1327. { tvarrec -> array of constconst }
  1328. if is_array_of_const(def_to) and
  1329. is_equal(def_from,tarraydef(def_to).elementtype.def) then
  1330. begin
  1331. doconv:=tc_equal;
  1332. b:=1;
  1333. end;
  1334. end;
  1335. end;
  1336. end;
  1337. end;
  1338. pointerdef :
  1339. begin
  1340. case def_from.deftype of
  1341. stringdef :
  1342. begin
  1343. { string constant (which can be part of array constructor)
  1344. to zero terminated string constant }
  1345. if (fromtreetype in [arrayconstructorn,stringconstn]) and
  1346. is_pchar(def_to) or is_pwidechar(def_to) then
  1347. begin
  1348. doconv:=tc_cstring_2_pchar;
  1349. b:=1;
  1350. end;
  1351. end;
  1352. orddef :
  1353. begin
  1354. { char constant to zero terminated string constant }
  1355. if (fromtreetype=ordconstn) then
  1356. begin
  1357. if is_equal(def_from,cchartype.def) and
  1358. is_pchar(def_to) then
  1359. begin
  1360. doconv:=tc_cchar_2_pchar;
  1361. b:=1;
  1362. end
  1363. else
  1364. if is_integer(def_from) then
  1365. begin
  1366. doconv:=tc_cord_2_pointer;
  1367. b:=1;
  1368. end;
  1369. end;
  1370. end;
  1371. arraydef :
  1372. begin
  1373. { chararray to pointer }
  1374. if is_zero_based_array(def_from) and
  1375. is_equal(tarraydef(def_from).elementtype.def,tpointerdef(def_to).pointertype.def) then
  1376. begin
  1377. doconv:=tc_array_2_pointer;
  1378. b:=1;
  1379. end;
  1380. end;
  1381. pointerdef :
  1382. begin
  1383. { child class pointer can be assigned to anchestor pointers }
  1384. if (
  1385. (tpointerdef(def_from).pointertype.def.deftype=objectdef) and
  1386. (tpointerdef(def_to).pointertype.def.deftype=objectdef) and
  1387. tobjectdef(tpointerdef(def_from).pointertype.def).is_related(
  1388. tobjectdef(tpointerdef(def_to).pointertype.def))
  1389. ) or
  1390. { all pointers can be assigned to void-pointer }
  1391. is_equal(tpointerdef(def_to).pointertype.def,voidtype.def) or
  1392. { in my opnion, is this not clean pascal }
  1393. { well, but it's handy to use, it isn't ? (FK) }
  1394. is_equal(tpointerdef(def_from).pointertype.def,voidtype.def) then
  1395. begin
  1396. { but don't allow conversion between farpointer-pointer }
  1397. if (tpointerdef(def_to).is_far=tpointerdef(def_from).is_far) then
  1398. begin
  1399. doconv:=tc_equal;
  1400. b:=1;
  1401. end;
  1402. end;
  1403. end;
  1404. procvardef :
  1405. begin
  1406. { procedure variable can be assigned to an void pointer }
  1407. { Not anymore. Use the @ operator now.}
  1408. if not(m_tp_procvar in aktmodeswitches) and
  1409. (tpointerdef(def_to).pointertype.def.deftype=orddef) and
  1410. (torddef(tpointerdef(def_to).pointertype.def).typ=uvoid) then
  1411. begin
  1412. doconv:=tc_equal;
  1413. b:=1;
  1414. end;
  1415. end;
  1416. classrefdef,
  1417. objectdef :
  1418. begin
  1419. { class types and class reference type
  1420. can be assigned to void pointers }
  1421. if (
  1422. is_class_or_interface(def_from) or
  1423. (def_from.deftype=classrefdef)
  1424. ) and
  1425. (tpointerdef(def_to).pointertype.def.deftype=orddef) and
  1426. (torddef(tpointerdef(def_to).pointertype.def).typ=uvoid) then
  1427. begin
  1428. doconv:=tc_equal;
  1429. b:=1;
  1430. end;
  1431. end;
  1432. end;
  1433. end;
  1434. setdef :
  1435. begin
  1436. { automatic arrayconstructor -> set conversion }
  1437. if is_array_constructor(def_from) then
  1438. begin
  1439. doconv:=tc_arrayconstructor_2_set;
  1440. b:=1;
  1441. end;
  1442. end;
  1443. procvardef :
  1444. begin
  1445. { proc -> procvar }
  1446. if (def_from.deftype=procdef) and
  1447. (m_tp_procvar in aktmodeswitches) then
  1448. begin
  1449. doconv:=tc_proc_2_procvar;
  1450. if proc_to_procvar_equal(tprocdef(def_from),tprocvardef(def_to)) then
  1451. b:=1;
  1452. end
  1453. else
  1454. { for example delphi allows the assignement from pointers }
  1455. { to procedure variables }
  1456. if (m_pointer_2_procedure in aktmodeswitches) and
  1457. (def_from.deftype=pointerdef) and
  1458. (tpointerdef(def_from).pointertype.def.deftype=orddef) and
  1459. (torddef(tpointerdef(def_from).pointertype.def).typ=uvoid) then
  1460. begin
  1461. doconv:=tc_equal;
  1462. b:=1;
  1463. end
  1464. else
  1465. { nil is compatible with procvars }
  1466. if (fromtreetype=niln) then
  1467. begin
  1468. doconv:=tc_equal;
  1469. b:=1;
  1470. end;
  1471. end;
  1472. objectdef :
  1473. begin
  1474. { object pascal objects }
  1475. if (def_from.deftype=objectdef) and
  1476. tobjectdef(def_from).is_related(tobjectdef(def_to)) then
  1477. begin
  1478. doconv:=tc_equal;
  1479. b:=1;
  1480. end
  1481. else
  1482. { Class/interface specific }
  1483. if is_class_or_interface(def_to) then
  1484. begin
  1485. { void pointer also for delphi mode }
  1486. if (m_delphi in aktmodeswitches) and
  1487. is_voidpointer(def_from) then
  1488. begin
  1489. doconv:=tc_equal;
  1490. b:=1;
  1491. end
  1492. else
  1493. { nil is compatible with class instances and interfaces }
  1494. if (fromtreetype=niln) then
  1495. begin
  1496. doconv:=tc_equal;
  1497. b:=1;
  1498. end
  1499. { classes can be assigned to interfaces }
  1500. else if is_interface(def_to) and
  1501. is_class(def_from) and
  1502. assigned(tobjectdef(def_from).implementedinterfaces) and
  1503. (tobjectdef(def_from).implementedinterfaces.searchintf(def_to)<>-1) then
  1504. begin
  1505. doconv:=tc_class_2_intf;
  1506. b:=1;
  1507. end
  1508. { Interface 2 GUID handling }
  1509. else if (def_to=tdef(rec_tguid)) and
  1510. (fromtreetype=typen) and
  1511. is_interface(def_from) and
  1512. tobjectdef(def_from).isiidguidvalid then
  1513. begin
  1514. b:=1;
  1515. doconv:=tc_equal;
  1516. end;
  1517. end;
  1518. end;
  1519. classrefdef :
  1520. begin
  1521. { class reference types }
  1522. if (def_from.deftype=classrefdef) then
  1523. begin
  1524. doconv:=tc_equal;
  1525. if tobjectdef(tclassrefdef(def_from).pointertype.def).is_related(
  1526. tobjectdef(tclassrefdef(def_to).pointertype.def)) then
  1527. b:=1;
  1528. end
  1529. else
  1530. { nil is compatible with class references }
  1531. if (fromtreetype=niln) then
  1532. begin
  1533. doconv:=tc_equal;
  1534. b:=1;
  1535. end;
  1536. end;
  1537. filedef :
  1538. begin
  1539. { typed files are all equal to the abstract file type
  1540. name TYPEDFILE in system.pp in is_equal in types.pas
  1541. the problem is that it sholud be also compatible to FILE
  1542. but this would leed to a problem for ASSIGN RESET and REWRITE
  1543. when trying to find the good overloaded function !!
  1544. so all file function are doubled in system.pp
  1545. this is not very beautiful !!}
  1546. if (def_from.deftype=filedef) and
  1547. (
  1548. (
  1549. (tfiledef(def_from).filetyp = ft_typed) and
  1550. (tfiledef(def_to).filetyp = ft_typed) and
  1551. (
  1552. (tfiledef(def_from).typedfiletype.def = tdef(voidtype.def)) or
  1553. (tfiledef(def_to).typedfiletype.def = tdef(voidtype.def))
  1554. )
  1555. ) or
  1556. (
  1557. (
  1558. (tfiledef(def_from).filetyp = ft_untyped) and
  1559. (tfiledef(def_to).filetyp = ft_typed)
  1560. ) or
  1561. (
  1562. (tfiledef(def_from).filetyp = ft_typed) and
  1563. (tfiledef(def_to).filetyp = ft_untyped)
  1564. )
  1565. )
  1566. ) then
  1567. begin
  1568. doconv:=tc_equal;
  1569. b:=1;
  1570. end
  1571. end;
  1572. else
  1573. begin
  1574. { assignment overwritten ?? }
  1575. if assignment_overloaded(def_from,def_to)<>nil then
  1576. b:=2;
  1577. end;
  1578. end;
  1579. isconvertable:=b;
  1580. end;
  1581. function CheckTypes(def1,def2 : tdef) : boolean;
  1582. var
  1583. s1,s2 : string;
  1584. begin
  1585. if not is_equal(def1,def2) then
  1586. begin
  1587. { Crash prevention }
  1588. if (not assigned(def1)) or (not assigned(def2)) then
  1589. Message(type_e_mismatch)
  1590. else
  1591. begin
  1592. s1:=def1.typename;
  1593. s2:=def2.typename;
  1594. if (s1<>'<unknown type>') and (s2<>'<unknown type>') then
  1595. Message2(type_e_not_equal_types,def1.typename,def2.typename)
  1596. else
  1597. Message(type_e_mismatch);
  1598. end;
  1599. CheckTypes:=false;
  1600. end
  1601. else
  1602. CheckTypes:=true;
  1603. end;
  1604. end.
  1605. {
  1606. $Log$
  1607. Revision 1.48 2001-10-16 17:15:44 jonas
  1608. * auto-converting from int64 to real is again allowed for all modes
  1609. (it's allowed in Delphi too)
  1610. Revision 1.47 2001/09/03 13:27:41 jonas
  1611. * compilerproc implementation of set addition/substraction/...
  1612. * changed the declaration of some set helpers somewhat to accomodate the
  1613. above change
  1614. * i386 still uses the old code for comparisons of sets, because its
  1615. helpers return the results in the flags
  1616. * dummy tc_normal_2_small_set type conversion because I need the original
  1617. resulttype of the set add nodes
  1618. NOTE: you have to start a cycle with 1.0.5!
  1619. Revision 1.46 2001/09/02 21:15:34 peter
  1620. * don't allow int64->real for delphi mode
  1621. Revision 1.45 2001/08/19 21:11:21 florian
  1622. * some bugs fix:
  1623. - overload; with external procedures fixed
  1624. - better selection of routine to do an overloaded
  1625. type case
  1626. - ... some more
  1627. Revision 1.44 2001/07/08 21:00:16 peter
  1628. * various widestring updates, it works now mostly without charset
  1629. mapping supported
  1630. Revision 1.43 2001/06/29 14:16:57 jonas
  1631. * fixed inconsistent handling of procvars in FPC mode (sometimes @ was
  1632. required to assign the address of a procedure to a procvar, sometimes
  1633. not. Now it is always required) (merged)
  1634. Revision 1.42 2001/05/08 21:06:33 florian
  1635. * some more support for widechars commited especially
  1636. regarding type casting and constants
  1637. Revision 1.41 2001/04/22 22:46:49 florian
  1638. * more variant support
  1639. Revision 1.40 2001/04/18 22:02:00 peter
  1640. * registration of targets and assemblers
  1641. Revision 1.39 2001/04/13 01:22:17 peter
  1642. * symtable change to classes
  1643. * range check generation and errors fixed, make cycle DEBUG=1 works
  1644. * memory leaks fixed
  1645. Revision 1.38 2001/04/04 21:30:47 florian
  1646. * applied several fixes to get the DD8 Delphi Unit compiled
  1647. e.g. "forward"-interfaces are working now
  1648. Revision 1.37 2001/04/02 21:20:35 peter
  1649. * resulttype rewrite
  1650. Revision 1.36 2001/03/23 00:16:07 florian
  1651. + some stuff to compile FreeCLX added
  1652. Revision 1.35 2001/03/03 12:38:33 jonas
  1653. + support for arraydefs in is_signed (for their rangetype, used in rangechecks)
  1654. Revision 1.34 2001/02/26 19:44:55 peter
  1655. * merged generic m68k updates from fixes branch
  1656. Revision 1.33 2001/02/26 12:47:46 jonas
  1657. * fixed bug in type checking for compatibility of set elements (merged)
  1658. * released fix in options.pas from Carl also for FPC (merged)
  1659. Revision 1.32 2001/02/20 21:44:25 peter
  1660. * tvarrec -> array of const fixed
  1661. Revision 1.31 2001/01/22 11:20:15 jonas
  1662. * fixed web bug 1363 (merged)
  1663. Revision 1.30 2001/01/08 21:43:38 peter
  1664. * string isn't compatible with array of char
  1665. Revision 1.29 2000/12/25 00:07:30 peter
  1666. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  1667. tlinkedlist objects)
  1668. Revision 1.28 2000/12/22 22:38:12 peter
  1669. * fixed bug #1286
  1670. Revision 1.27 2000/12/20 15:59:40 jonas
  1671. - removed obsolete special case for range checking of cardinal constants
  1672. at compile time
  1673. Revision 1.26 2000/12/11 19:13:54 jonas
  1674. * fixed range checking of cardinal constants
  1675. * fixed range checking of "qword constants" (they don't really exist,
  1676. but values > high(int64) were set to zero if assigned to qword)
  1677. Revision 1.25 2000/12/08 14:06:11 jonas
  1678. * fix for web bug 1245: arrays of char with size >255 are now passed to
  1679. overloaded procedures which expect ansistrings instead of shortstrings
  1680. if possible
  1681. * pointer to array of chars (when using $t+) are now also considered
  1682. pchars
  1683. Revision 1.24 2000/11/20 15:52:47 jonas
  1684. * testrange now always cuts a constant to the size of the destination
  1685. if a rangeerror occurred
  1686. * changed an "and $ffffffff" to "and (int64($fffffff) shl 4 + $f" to
  1687. work around the constant evaluation problem we currently have
  1688. Revision 1.23 2000/11/13 14:42:41 jonas
  1689. * fix in testrange so that 64bit constants are properly truncated when
  1690. assigned to 32bit vars
  1691. Revision 1.22 2000/11/13 11:30:55 florian
  1692. * some bugs with interfaces and NIL fixed
  1693. Revision 1.21 2000/11/12 23:24:12 florian
  1694. * interfaces are basically running
  1695. Revision 1.20 2000/11/11 16:13:31 peter
  1696. * farpointer and normal pointer aren't compatible
  1697. Revision 1.19 2000/11/06 22:30:30 peter
  1698. * more fixes
  1699. Revision 1.18 2000/11/04 14:25:22 florian
  1700. + merged Attila's changes for interfaces, not tested yet
  1701. Revision 1.17 2000/10/31 22:30:13 peter
  1702. * merged asm result patch part 2
  1703. Revision 1.16 2000/10/31 22:02:55 peter
  1704. * symtable splitted, no real code changes
  1705. Revision 1.15 2000/10/21 18:16:12 florian
  1706. * a lot of changes:
  1707. - basic dyn. array support
  1708. - basic C++ support
  1709. - some work for interfaces done
  1710. ....
  1711. Revision 1.14 2000/10/14 10:14:56 peter
  1712. * moehrendorf oct 2000 rewrite
  1713. Revision 1.13 2000/10/01 19:48:26 peter
  1714. * lot of compile updates for cg11
  1715. Revision 1.12 2000/09/30 16:08:46 peter
  1716. * more cg11 updates
  1717. Revision 1.11 2000/09/24 15:06:32 peter
  1718. * use defines.inc
  1719. Revision 1.10 2000/09/18 12:31:15 jonas
  1720. * fixed bug in push_addr_param for arrays (merged from fixes branch)
  1721. Revision 1.9 2000/09/10 20:16:21 peter
  1722. * array of const isn't equal with array of <type> (merged)
  1723. Revision 1.8 2000/08/19 19:51:03 peter
  1724. * fixed bug with comparing constsym strings
  1725. Revision 1.7 2000/08/16 13:06:07 florian
  1726. + support of 64 bit integer constants
  1727. Revision 1.6 2000/08/13 13:07:18 peter
  1728. * equal_paras now also checks default parameter value
  1729. Revision 1.5 2000/08/12 06:49:22 florian
  1730. + case statement for int64/qword implemented
  1731. Revision 1.4 2000/08/08 19:26:41 peter
  1732. * equal_constsym() needed for default para
  1733. Revision 1.3 2000/07/13 12:08:28 michael
  1734. + patched to 1.1.0 with former 1.09patch from peter
  1735. Revision 1.2 2000/07/13 11:32:53 michael
  1736. + removed logs
  1737. }