types.pas 67 KB

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