types.pas 62 KB

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