types.pas 66 KB

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