types.pas 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865
  1. {
  2. $Id$
  3. Copyright (C) 1998-2000 by Florian Klaempfl
  4. This unit provides some help routines for type handling
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit types;
  19. {$i defines.inc}
  20. interface
  21. uses
  22. cobjects,cclasses,
  23. cpuinfo,
  24. node,
  25. symbase,symtype,symdef,symsym;
  26. type
  27. tmmxtype = (mmxno,mmxu8bit,mmxs8bit,mmxu16bit,mmxs16bit,
  28. mmxu32bit,mmxs32bit,mmxfixed16,mmxsingle);
  29. const
  30. { true if we must never copy this parameter }
  31. never_copy_const_param : boolean = false;
  32. {*****************************************************************************
  33. Basic type functions
  34. *****************************************************************************}
  35. { returns true, if def defines an ordinal type }
  36. function is_ordinal(def : pdef) : boolean;
  37. { returns the min. value of the type }
  38. function get_min_value(def : pdef) : longint;
  39. { returns true, if def defines an ordinal type }
  40. function is_integer(def : pdef) : boolean;
  41. { true if p is a boolean }
  42. function is_boolean(def : pdef) : boolean;
  43. { true if p is a char }
  44. function is_char(def : pdef) : boolean;
  45. { true if p is a void}
  46. function is_void(def : pdef) : boolean;
  47. { true if p is a smallset def }
  48. function is_smallset(p : pdef) : boolean;
  49. { returns true, if def defines a signed data type (only for ordinal types) }
  50. function is_signed(def : pdef) : boolean;
  51. {*****************************************************************************
  52. Array helper functions
  53. *****************************************************************************}
  54. { true, if p points to a zero based (non special like open or
  55. dynamic array def, mainly this is used to see if the array
  56. is convertable to a pointer }
  57. function is_zero_based_array(p : pdef) : boolean;
  58. { true if p points to an open array def }
  59. function is_open_array(p : pdef) : boolean;
  60. { true if p points to a dynamic array def }
  61. function is_dynamic_array(p : pdef) : boolean;
  62. { true, if p points to an array of const def }
  63. function is_array_constructor(p : pdef) : boolean;
  64. { true, if p points to a variant array }
  65. function is_variant_array(p : pdef) : boolean;
  66. { true, if p points to an array of const }
  67. function is_array_of_const(p : pdef) : boolean;
  68. { true, if p points any kind of special array }
  69. function is_special_array(p : pdef) : boolean;
  70. { true if p is a char array def }
  71. function is_chararray(p : pdef) : boolean;
  72. {*****************************************************************************
  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 : tlinkedlist; 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 : tlinkedlist; 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,systems,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 : TLinkedList; acp : compare_type) : boolean;
  244. var
  245. def1,def2 : TParaItem;
  246. begin
  247. def1:=TParaItem(paralist1.first);
  248. def2:=TParaItem(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:=TParaItem(def1.next);
  294. def2:=TParaItem(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 : TLinkedList;acp : compare_type) : boolean;
  302. var
  303. def1,def2 : TParaItem;
  304. doconv : tconverttype;
  305. begin
  306. def1:=TParaItem(paralist1.first);
  307. def2:=TParaItem(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:=TParaItem(def1.next);
  343. def2:=TParaItem(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:=penumdef(def)^.min < 0;
  461. arraydef :
  462. is_signed:=is_signed(parraydef(def)^.rangetype.def);
  463. else
  464. is_signed:=false;
  465. end;
  466. end;
  467. { true, if p points to an open array def }
  468. function is_open_string(p : pdef) : boolean;
  469. begin
  470. is_open_string:=(p^.deftype=stringdef) and
  471. (pstringdef(p)^.string_typ=st_shortstring) and
  472. (pstringdef(p)^.len=0);
  473. end;
  474. { true, if p points to a zero based array def }
  475. function is_zero_based_array(p : pdef) : boolean;
  476. begin
  477. is_zero_based_array:=(p^.deftype=arraydef) and
  478. (parraydef(p)^.lowrange=0) and
  479. not(is_special_array(p));
  480. end;
  481. { true if p points to a dynamic array def }
  482. function is_dynamic_array(p : pdef) : boolean;
  483. begin
  484. is_dynamic_array:=(p^.deftype=arraydef) and
  485. parraydef(p)^.IsDynamicArray;
  486. end;
  487. { true, if p points to an open array def }
  488. function is_open_array(p : pdef) : boolean;
  489. begin
  490. { check for s32bitdef is needed, because for u32bit the high
  491. range is also -1 ! (PFV) }
  492. is_open_array:=(p^.deftype=arraydef) and
  493. (parraydef(p)^.rangetype.def=pdef(s32bitdef)) and
  494. (parraydef(p)^.lowrange=0) and
  495. (parraydef(p)^.highrange=-1) and
  496. not(parraydef(p)^.IsConstructor) and
  497. not(parraydef(p)^.IsVariant) and
  498. not(parraydef(p)^.IsArrayOfConst) and
  499. not(parraydef(p)^.IsDynamicArray);
  500. end;
  501. { true, if p points to an array of const def }
  502. function is_array_constructor(p : pdef) : boolean;
  503. begin
  504. is_array_constructor:=(p^.deftype=arraydef) and
  505. (parraydef(p)^.IsConstructor);
  506. end;
  507. { true, if p points to a variant array }
  508. function is_variant_array(p : pdef) : boolean;
  509. begin
  510. is_variant_array:=(p^.deftype=arraydef) and
  511. (parraydef(p)^.IsVariant);
  512. end;
  513. { true, if p points to an array of const }
  514. function is_array_of_const(p : pdef) : boolean;
  515. begin
  516. is_array_of_const:=(p^.deftype=arraydef) and
  517. (parraydef(p)^.IsArrayOfConst);
  518. end;
  519. { true, if p points to a special array }
  520. function is_special_array(p : pdef) : boolean;
  521. begin
  522. is_special_array:=(p^.deftype=arraydef) and
  523. ((parraydef(p)^.IsVariant) or
  524. (parraydef(p)^.IsArrayOfConst) or
  525. (parraydef(p)^.IsConstructor) or
  526. is_open_array(p)
  527. );
  528. end;
  529. { true if p is an ansi string def }
  530. function is_ansistring(p : pdef) : boolean;
  531. begin
  532. is_ansistring:=(p^.deftype=stringdef) and
  533. (pstringdef(p)^.string_typ=st_ansistring);
  534. end;
  535. { true if p is an long string def }
  536. function is_longstring(p : pdef) : boolean;
  537. begin
  538. is_longstring:=(p^.deftype=stringdef) and
  539. (pstringdef(p)^.string_typ=st_longstring);
  540. end;
  541. { true if p is an wide string def }
  542. function is_widestring(p : pdef) : boolean;
  543. begin
  544. is_widestring:=(p^.deftype=stringdef) and
  545. (pstringdef(p)^.string_typ=st_widestring);
  546. end;
  547. { true if p is an short string def }
  548. function is_shortstring(p : pdef) : boolean;
  549. begin
  550. is_shortstring:=(p^.deftype=stringdef) and
  551. (pstringdef(p)^.string_typ=st_shortstring);
  552. end;
  553. { true if p is a char array def }
  554. function is_chararray(p : pdef) : boolean;
  555. begin
  556. is_chararray:=(p^.deftype=arraydef) and
  557. is_equal(parraydef(p)^.elementtype.def,cchardef) and
  558. not(is_special_array(p));
  559. end;
  560. { true if p is a pchar def }
  561. function is_pchar(p : pdef) : boolean;
  562. begin
  563. is_pchar:=(p^.deftype=pointerdef) and
  564. (is_equal(ppointerdef(p)^.pointertype.def,cchardef) or
  565. (is_zero_based_array(ppointerdef(p)^.pointertype.def) and
  566. is_chararray(ppointerdef(p)^.pointertype.def)));
  567. end;
  568. { true if p is a voidpointer def }
  569. function is_voidpointer(p : pdef) : boolean;
  570. begin
  571. is_voidpointer:=(p^.deftype=pointerdef) and
  572. is_equal(Ppointerdef(p)^.pointertype.def,voiddef);
  573. end;
  574. { true if p is a smallset def }
  575. function is_smallset(p : pdef) : boolean;
  576. begin
  577. is_smallset:=(p^.deftype=setdef) and
  578. (psetdef(p)^.settype=smallset);
  579. end;
  580. { true if the return value is in accumulator (EAX for i386), D0 for 68k }
  581. function ret_in_acc(def : pdef) : boolean;
  582. begin
  583. ret_in_acc:=(def^.deftype in [orddef,pointerdef,enumdef,classrefdef]) or
  584. ((def^.deftype=stringdef) and (pstringdef(def)^.string_typ in [st_ansistring,st_widestring])) or
  585. ((def^.deftype=procvardef) and not(po_methodpointer in pprocvardef(def)^.procoptions)) or
  586. ((def^.deftype=objectdef) and not is_object(def)) or
  587. ((def^.deftype=setdef) and (psetdef(def)^.settype=smallset)) or
  588. ((def^.deftype=floatdef) and (pfloatdef(def)^.typ=f32bit));
  589. end;
  590. { true, if def is a 64 bit int type }
  591. function is_64bitint(def : pdef) : boolean;
  592. begin
  593. is_64bitint:=(def^.deftype=orddef) and (porddef(def)^.typ in [u64bit,s64bit])
  594. end;
  595. { true if uses a parameter as return value }
  596. function ret_in_param(def : pdef) : boolean;
  597. begin
  598. ret_in_param:=(def^.deftype in [arraydef,recorddef]) or
  599. ((def^.deftype=stringdef) and (pstringdef(def)^.string_typ in [st_shortstring,st_longstring])) or
  600. ((def^.deftype=procvardef) and (po_methodpointer in pprocvardef(def)^.procoptions)) or
  601. ((def^.deftype=objectdef) and is_object(def)) or
  602. ((def^.deftype=setdef) and (psetdef(def)^.settype<>smallset));
  603. end;
  604. function push_high_param(def : pdef) : boolean;
  605. begin
  606. push_high_param:=is_open_array(def) or
  607. is_open_string(def) or
  608. is_array_of_const(def);
  609. end;
  610. { true if a parameter is too large to copy and only the address is pushed }
  611. function push_addr_param(def : pdef) : boolean;
  612. begin
  613. push_addr_param:=false;
  614. if never_copy_const_param then
  615. push_addr_param:=true
  616. else
  617. begin
  618. case def^.deftype of
  619. formaldef :
  620. push_addr_param:=true;
  621. recorddef :
  622. push_addr_param:=(def^.size>target_os.size_of_pointer);
  623. arraydef :
  624. push_addr_param:=((Parraydef(def)^.highrange>=Parraydef(def)^.lowrange) and (def^.size>target_os.size_of_pointer)) or
  625. is_open_array(def) or
  626. is_array_of_const(def) or
  627. is_array_constructor(def);
  628. objectdef :
  629. push_addr_param:=is_object(def);
  630. stringdef :
  631. push_addr_param:=pstringdef(def)^.string_typ in [st_shortstring,st_longstring];
  632. procvardef :
  633. push_addr_param:=(po_methodpointer in pprocvardef(def)^.procoptions);
  634. setdef :
  635. push_addr_param:=(psetdef(def)^.settype<>smallset);
  636. end;
  637. end;
  638. end;
  639. { test if l is in the range of def, outputs error if out of range }
  640. procedure testrange(def : pdef;var l : tconstexprint);
  641. var
  642. lv,hv: longint;
  643. error: boolean;
  644. begin
  645. error := false;
  646. { for 64 bit types we need only to check if it is less than }
  647. { zero, if def is a qword node }
  648. if is_64bitint(def) then
  649. begin
  650. if (l<0) and (porddef(def)^.typ=u64bit) then
  651. begin
  652. { don't zero the result, because it may come from hex notation
  653. like $ffffffffffffffff! (JM)
  654. l:=0; }
  655. if (cs_check_range in aktlocalswitches) then
  656. Message(parser_e_range_check_error)
  657. else
  658. Message(parser_w_range_check_error);
  659. error := true;
  660. end;
  661. end
  662. else
  663. begin
  664. getrange(def,lv,hv);
  665. if (def^.deftype=orddef) and
  666. (porddef(def)^.typ=u32bit) then
  667. begin
  668. if (l < cardinal(lv)) or
  669. (l > cardinal(hv)) then
  670. begin
  671. if (cs_check_range in aktlocalswitches) then
  672. Message(parser_e_range_check_error)
  673. else
  674. Message(parser_w_range_check_error);
  675. error := true;
  676. end;
  677. end
  678. else if (l<lv) or (l>hv) then
  679. begin
  680. if (def^.deftype=enumdef) or
  681. (cs_check_range in aktlocalswitches) then
  682. Message(parser_e_range_check_error)
  683. else
  684. Message(parser_w_range_check_error);
  685. error := true;
  686. end;
  687. end;
  688. if error then
  689. { Fix the value to fit in the allocated space for this type of variable }
  690. case def^.size of
  691. 1: l := l and $ff;
  692. 2: l := l and $ffff;
  693. { work around sign extension bug (to be fixed) (JM) }
  694. 4: l := l and (int64($fffffff) shl 4 + $f);
  695. end
  696. end;
  697. { return the range from def in l and h }
  698. procedure getrange(def : pdef;var l : longint;var h : longint);
  699. begin
  700. case def^.deftype of
  701. orddef :
  702. begin
  703. l:=porddef(def)^.low;
  704. h:=porddef(def)^.high;
  705. end;
  706. enumdef :
  707. begin
  708. l:=penumdef(def)^.min;
  709. h:=penumdef(def)^.max;
  710. end;
  711. arraydef :
  712. begin
  713. l:=parraydef(def)^.lowrange;
  714. h:=parraydef(def)^.highrange;
  715. end;
  716. else
  717. internalerror(987);
  718. end;
  719. end;
  720. function mmx_type(p : pdef) : tmmxtype;
  721. begin
  722. mmx_type:=mmxno;
  723. if is_mmx_able_array(p) then
  724. begin
  725. if parraydef(p)^.elementtype.def^.deftype=floatdef then
  726. case pfloatdef(parraydef(p)^.elementtype.def)^.typ of
  727. s32real:
  728. mmx_type:=mmxsingle;
  729. f16bit:
  730. mmx_type:=mmxfixed16
  731. end
  732. else
  733. case porddef(parraydef(p)^.elementtype.def)^.typ of
  734. u8bit:
  735. mmx_type:=mmxu8bit;
  736. s8bit:
  737. mmx_type:=mmxs8bit;
  738. u16bit:
  739. mmx_type:=mmxu16bit;
  740. s16bit:
  741. mmx_type:=mmxs16bit;
  742. u32bit:
  743. mmx_type:=mmxu32bit;
  744. s32bit:
  745. mmx_type:=mmxs32bit;
  746. end;
  747. end;
  748. end;
  749. function is_mmx_able_array(p : pdef) : boolean;
  750. begin
  751. {$ifdef SUPPORT_MMX}
  752. if (cs_mmx_saturation in aktlocalswitches) then
  753. begin
  754. is_mmx_able_array:=(p^.deftype=arraydef) and
  755. not(is_special_array(p)) and
  756. (
  757. (
  758. (parraydef(p)^.elementtype.def^.deftype=orddef) and
  759. (
  760. (
  761. (parraydef(p)^.lowrange=0) and
  762. (parraydef(p)^.highrange=1) and
  763. (porddef(parraydef(p)^.elementtype.def)^.typ in [u32bit,s32bit])
  764. )
  765. or
  766. (
  767. (parraydef(p)^.lowrange=0) and
  768. (parraydef(p)^.highrange=3) and
  769. (porddef(parraydef(p)^.elementtype.def)^.typ in [u16bit,s16bit])
  770. )
  771. )
  772. )
  773. or
  774. (
  775. (
  776. (parraydef(p)^.elementtype.def^.deftype=floatdef) and
  777. (
  778. (parraydef(p)^.lowrange=0) and
  779. (parraydef(p)^.highrange=3) and
  780. (pfloatdef(parraydef(p)^.elementtype.def)^.typ=f16bit)
  781. ) or
  782. (
  783. (parraydef(p)^.lowrange=0) and
  784. (parraydef(p)^.highrange=1) and
  785. (pfloatdef(parraydef(p)^.elementtype.def)^.typ=s32real)
  786. )
  787. )
  788. )
  789. );
  790. end
  791. else
  792. begin
  793. is_mmx_able_array:=(p^.deftype=arraydef) and
  794. (
  795. (
  796. (parraydef(p)^.elementtype.def^.deftype=orddef) and
  797. (
  798. (
  799. (parraydef(p)^.lowrange=0) and
  800. (parraydef(p)^.highrange=1) and
  801. (porddef(parraydef(p)^.elementtype.def)^.typ in [u32bit,s32bit])
  802. )
  803. or
  804. (
  805. (parraydef(p)^.lowrange=0) and
  806. (parraydef(p)^.highrange=3) and
  807. (porddef(parraydef(p)^.elementtype.def)^.typ in [u16bit,s16bit])
  808. )
  809. or
  810. (
  811. (parraydef(p)^.lowrange=0) and
  812. (parraydef(p)^.highrange=7) and
  813. (porddef(parraydef(p)^.elementtype.def)^.typ in [u8bit,s8bit])
  814. )
  815. )
  816. )
  817. or
  818. (
  819. (parraydef(p)^.elementtype.def^.deftype=floatdef) and
  820. (
  821. (
  822. (parraydef(p)^.lowrange=0) and
  823. (parraydef(p)^.highrange=3) and
  824. (pfloatdef(parraydef(p)^.elementtype.def)^.typ=f32bit)
  825. )
  826. or
  827. (
  828. (parraydef(p)^.lowrange=0) and
  829. (parraydef(p)^.highrange=1) and
  830. (pfloatdef(parraydef(p)^.elementtype.def)^.typ=s32real)
  831. )
  832. )
  833. )
  834. );
  835. end;
  836. {$else SUPPORT_MMX}
  837. is_mmx_able_array:=false;
  838. {$endif SUPPORT_MMX}
  839. end;
  840. function is_equal(def1,def2 : pdef) : boolean;
  841. var
  842. b : boolean;
  843. hd : pdef;
  844. begin
  845. { both types must exists }
  846. if not (assigned(def1) and assigned(def2)) then
  847. begin
  848. is_equal:=false;
  849. exit;
  850. end;
  851. { be sure, that if there is a stringdef, that this is def1 }
  852. if def2^.deftype=stringdef then
  853. begin
  854. hd:=def1;
  855. def1:=def2;
  856. def2:=hd;
  857. end;
  858. b:=false;
  859. { both point to the same definition ? }
  860. if def1=def2 then
  861. b:=true
  862. else
  863. { pointer with an equal definition are equal }
  864. if (def1^.deftype=pointerdef) and (def2^.deftype=pointerdef) then
  865. begin
  866. { check if both are farpointer }
  867. if (ppointerdef(def1)^.is_far=ppointerdef(def2)^.is_far) then
  868. begin
  869. { here a problem detected in tabsolutesym }
  870. { the types can be forward type !! }
  871. if assigned(def1^.typesym) and (ppointerdef(def1)^.pointertype.def^.deftype=forwarddef) then
  872. b:=(def1^.typesym=def2^.typesym)
  873. else
  874. b:=ppointerdef(def1)^.pointertype.def=ppointerdef(def2)^.pointertype.def;
  875. end
  876. else
  877. b:=false;
  878. end
  879. else
  880. { ordinals are equal only when the ordinal type is equal }
  881. if (def1^.deftype=orddef) and (def2^.deftype=orddef) then
  882. begin
  883. case porddef(def1)^.typ of
  884. u8bit,u16bit,u32bit,
  885. s8bit,s16bit,s32bit:
  886. b:=((porddef(def1)^.typ=porddef(def2)^.typ) and
  887. (porddef(def1)^.low=porddef(def2)^.low) and
  888. (porddef(def1)^.high=porddef(def2)^.high));
  889. uvoid,uchar,
  890. bool8bit,bool16bit,bool32bit:
  891. b:=(porddef(def1)^.typ=porddef(def2)^.typ);
  892. end;
  893. end
  894. else
  895. if (def1^.deftype=floatdef) and (def2^.deftype=floatdef) then
  896. b:=pfloatdef(def1)^.typ=pfloatdef(def2)^.typ
  897. else
  898. { strings with the same length are equal }
  899. if (def1^.deftype=stringdef) and (def2^.deftype=stringdef) and
  900. (pstringdef(def1)^.string_typ=pstringdef(def2)^.string_typ) then
  901. begin
  902. b:=not(is_shortstring(def1)) or
  903. (pstringdef(def1)^.len=pstringdef(def2)^.len);
  904. end
  905. else
  906. if (def1^.deftype=formaldef) and (def2^.deftype=formaldef) then
  907. b:=true
  908. { file types with the same file element type are equal }
  909. { this is a problem for assign !! }
  910. { changed to allow if one is untyped }
  911. { all typed files are equal to the special }
  912. { typed file that has voiddef as elemnt type }
  913. { but must NOT match for text file !!! }
  914. else
  915. if (def1^.deftype=filedef) and (def2^.deftype=filedef) then
  916. b:=(pfiledef(def1)^.filetyp=pfiledef(def2)^.filetyp) and
  917. ((
  918. ((pfiledef(def1)^.typedfiletype.def=nil) and
  919. (pfiledef(def2)^.typedfiletype.def=nil)) or
  920. (
  921. (pfiledef(def1)^.typedfiletype.def<>nil) and
  922. (pfiledef(def2)^.typedfiletype.def<>nil) and
  923. is_equal(pfiledef(def1)^.typedfiletype.def,pfiledef(def2)^.typedfiletype.def)
  924. ) or
  925. ( (pfiledef(def1)^.typedfiletype.def=pdef(voiddef)) or
  926. (pfiledef(def2)^.typedfiletype.def=pdef(voiddef))
  927. )))
  928. { sets with the same element base type are equal }
  929. else
  930. if (def1^.deftype=setdef) and (def2^.deftype=setdef) then
  931. begin
  932. if assigned(psetdef(def1)^.elementtype.def) and
  933. assigned(psetdef(def2)^.elementtype.def) then
  934. b:=is_subequal(psetdef(def1)^.elementtype.def,psetdef(def2)^.elementtype.def)
  935. else
  936. { empty set is compatible with everything }
  937. b:=true;
  938. end
  939. else
  940. if (def1^.deftype=procvardef) and (def2^.deftype=procvardef) then
  941. begin
  942. { poassembler isn't important for compatibility }
  943. { if a method is assigned to a methodpointer }
  944. { is checked before }
  945. b:=(pprocvardef(def1)^.proctypeoption=pprocvardef(def2)^.proctypeoption) and
  946. (pprocvardef(def1)^.proccalloptions=pprocvardef(def2)^.proccalloptions) and
  947. ((pprocvardef(def1)^.procoptions * po_compatibility_options)=
  948. (pprocvardef(def2)^.procoptions * po_compatibility_options)) and
  949. is_equal(pprocvardef(def1)^.rettype.def,pprocvardef(def2)^.rettype.def) and
  950. equal_paras(pprocvardef(def1)^.para,pprocvardef(def2)^.para,cp_all);
  951. end
  952. else
  953. if (def1^.deftype=arraydef) and (def2^.deftype=arraydef) then
  954. begin
  955. if is_dynamic_array(def1) and is_dynamic_array(def2) then
  956. b:=is_equal(parraydef(def1)^.elementtype.def,parraydef(def2)^.elementtype.def)
  957. else
  958. if is_array_of_const(def1) or is_array_of_const(def2) then
  959. begin
  960. b:=(is_array_of_const(def1) and is_array_of_const(def2)) or
  961. (is_array_of_const(def1) and is_array_constructor(def2)) or
  962. (is_array_of_const(def2) and is_array_constructor(def1));
  963. end
  964. else
  965. if is_open_array(def1) or is_open_array(def2) then
  966. begin
  967. b:=is_equal(parraydef(def1)^.elementtype.def,parraydef(def2)^.elementtype.def);
  968. end
  969. else
  970. begin
  971. b:=not(m_tp in aktmodeswitches) and
  972. not(m_delphi in aktmodeswitches) and
  973. (parraydef(def1)^.lowrange=parraydef(def2)^.lowrange) and
  974. (parraydef(def1)^.highrange=parraydef(def2)^.highrange) and
  975. is_equal(parraydef(def1)^.elementtype.def,parraydef(def2)^.elementtype.def) and
  976. is_equal(parraydef(def1)^.rangetype.def,parraydef(def2)^.rangetype.def);
  977. end;
  978. end
  979. else
  980. if (def1^.deftype=classrefdef) and (def2^.deftype=classrefdef) then
  981. begin
  982. { similar to pointerdef: }
  983. if assigned(def1^.typesym) and (pclassrefdef(def1)^.pointertype.def^.deftype=forwarddef) then
  984. b:=(def1^.typesym=def2^.typesym)
  985. else
  986. b:=is_equal(pclassrefdef(def1)^.pointertype.def,pclassrefdef(def2)^.pointertype.def);
  987. end;
  988. is_equal:=b;
  989. end;
  990. function is_subequal(def1, def2: pdef): boolean;
  991. var
  992. basedef1,basedef2 : penumdef;
  993. Begin
  994. is_subequal := false;
  995. if assigned(def1) and assigned(def2) then
  996. Begin
  997. if (def1^.deftype = orddef) and (def2^.deftype = orddef) then
  998. Begin
  999. { see p.47 of Turbo Pascal 7.01 manual for the separation of types }
  1000. { range checking for case statements is done with testrange }
  1001. case porddef(def1)^.typ of
  1002. u8bit,u16bit,u32bit,
  1003. s8bit,s16bit,s32bit,s64bit,u64bit :
  1004. is_subequal:=(porddef(def2)^.typ in [s64bit,u64bit,s32bit,u32bit,u8bit,s8bit,s16bit,u16bit]);
  1005. bool8bit,bool16bit,bool32bit :
  1006. is_subequal:=(porddef(def2)^.typ in [bool8bit,bool16bit,bool32bit]);
  1007. uchar :
  1008. is_subequal:=(porddef(def2)^.typ=uchar);
  1009. end;
  1010. end
  1011. else
  1012. Begin
  1013. { I assume that both enumerations are equal when the first }
  1014. { pointers are equal. }
  1015. { I changed this to assume that the enums are equal }
  1016. { if the basedefs are equal (FK) }
  1017. if (def1^.deftype=enumdef) and (def2^.deftype=enumdef) then
  1018. Begin
  1019. { get both basedefs }
  1020. basedef1:=penumdef(def1);
  1021. while assigned(basedef1^.basedef) do
  1022. basedef1:=basedef1^.basedef;
  1023. basedef2:=penumdef(def2);
  1024. while assigned(basedef2^.basedef) do
  1025. basedef2:=basedef2^.basedef;
  1026. is_subequal:=basedef1=basedef2;
  1027. {
  1028. if penumdef(def1)^.firstenum = penumdef(def2)^.firstenum then
  1029. is_subequal := TRUE;
  1030. }
  1031. end;
  1032. end;
  1033. end; { endif assigned ... }
  1034. end;
  1035. function assignment_overloaded(from_def,to_def : pdef) : pprocdef;
  1036. var
  1037. passproc : pprocdef;
  1038. convtyp : tconverttype;
  1039. begin
  1040. assignment_overloaded:=nil;
  1041. if assigned(overloaded_operators[_ASSIGNMENT]) then
  1042. passproc:=overloaded_operators[_ASSIGNMENT]^.definition
  1043. else
  1044. exit;
  1045. while passproc<>nil do
  1046. begin
  1047. if is_equal(passproc^.rettype.def,to_def) and
  1048. (is_equal(TParaItem(passproc^.Para.first).paratype.def,from_def) or
  1049. (isconvertable(from_def,TParaItem(passproc^.Para.first).paratype.def,convtyp,nil,ordconstn,false)=1)) then
  1050. begin
  1051. assignment_overloaded:=passproc;
  1052. break;
  1053. end;
  1054. passproc:=passproc^.nextoverloaded;
  1055. end;
  1056. end;
  1057. { Returns:
  1058. 0 - Not convertable
  1059. 1 - Convertable
  1060. 2 - Convertable, but not first choice }
  1061. function isconvertable(def_from,def_to : pdef;
  1062. var doconv : tconverttype;
  1063. fromtree: tnode; fromtreetype : tnodetype;
  1064. explicit : boolean) : byte;
  1065. { Tbasetype: uauto,uvoid,uchar,
  1066. u8bit,u16bit,u32bit,
  1067. s8bit,s16bit,s32,
  1068. bool8bit,bool16bit,bool32bit,
  1069. u64bit,s64bitint }
  1070. type
  1071. tbasedef=(bvoid,bchar,bint,bbool);
  1072. const
  1073. basedeftbl:array[tbasetype] of tbasedef =
  1074. (bvoid,bvoid,bchar,
  1075. bint,bint,bint,
  1076. bint,bint,bint,
  1077. bbool,bbool,bbool,bint,bint,bchar);
  1078. basedefconverts : array[tbasedef,tbasedef] of tconverttype =
  1079. ((tc_not_possible,tc_not_possible,tc_not_possible,tc_not_possible),
  1080. (tc_not_possible,tc_equal,tc_not_possible,tc_not_possible),
  1081. (tc_not_possible,tc_not_possible,tc_int_2_int,tc_int_2_bool),
  1082. (tc_not_possible,tc_not_possible,tc_bool_2_int,tc_bool_2_bool));
  1083. var
  1084. b : byte;
  1085. hd1,hd2 : pdef;
  1086. hct : tconverttype;
  1087. begin
  1088. { safety check }
  1089. if not(assigned(def_from) and assigned(def_to)) then
  1090. begin
  1091. isconvertable:=0;
  1092. exit;
  1093. end;
  1094. { tp7 procvar def support, in tp7 a procvar is always called, if the
  1095. procvar is passed explicit a addrn would be there }
  1096. if (m_tp_procvar in aktmodeswitches) and
  1097. (def_from^.deftype=procvardef) and
  1098. (fromtreetype=loadn) then
  1099. begin
  1100. def_from:=pprocvardef(def_from)^.rettype.def;
  1101. end;
  1102. { we walk the wanted (def_to) types and check then the def_from
  1103. types if there is a conversion possible }
  1104. b:=0;
  1105. case def_to^.deftype of
  1106. orddef :
  1107. begin
  1108. case def_from^.deftype of
  1109. orddef :
  1110. begin
  1111. doconv:=basedefconverts[basedeftbl[porddef(def_from)^.typ],basedeftbl[porddef(def_to)^.typ]];
  1112. b:=1;
  1113. if (doconv=tc_not_possible) or
  1114. ((doconv=tc_int_2_bool) and
  1115. (not explicit) and
  1116. (not is_boolean(def_from))) or
  1117. ((doconv=tc_bool_2_int) and
  1118. (not explicit) and
  1119. (not is_boolean(def_to))) then
  1120. b:=0;
  1121. end;
  1122. enumdef :
  1123. begin
  1124. { needed for char(enum) }
  1125. if explicit then
  1126. begin
  1127. doconv:=tc_int_2_int;
  1128. b:=1;
  1129. end;
  1130. end;
  1131. end;
  1132. end;
  1133. stringdef :
  1134. begin
  1135. case def_from^.deftype of
  1136. stringdef :
  1137. begin
  1138. doconv:=tc_string_2_string;
  1139. b:=1;
  1140. end;
  1141. orddef :
  1142. begin
  1143. { char to string}
  1144. if is_char(def_from) then
  1145. begin
  1146. doconv:=tc_char_2_string;
  1147. b:=1;
  1148. end;
  1149. end;
  1150. arraydef :
  1151. begin
  1152. { array of char to string, the length check is done by the firstpass of this node }
  1153. if is_chararray(def_from) then
  1154. begin
  1155. doconv:=tc_chararray_2_string;
  1156. if (is_shortstring(def_to) and
  1157. (def_from^.size <= 255)) or
  1158. (is_ansistring(def_to) and
  1159. (def_from^.size > 255)) then
  1160. b:=1
  1161. else
  1162. b:=2;
  1163. end;
  1164. end;
  1165. pointerdef :
  1166. begin
  1167. { pchar can be assigned to short/ansistrings,
  1168. but not in tp7 compatible mode }
  1169. if is_pchar(def_from) and not(m_tp7 in aktmodeswitches) then
  1170. begin
  1171. doconv:=tc_pchar_2_string;
  1172. { prefer ansistrings because pchars can overflow shortstrings, }
  1173. { but only if ansistrings are the default (JM) }
  1174. if (is_shortstring(def_to) and
  1175. not(cs_ansistrings in aktlocalswitches)) or
  1176. (is_ansistring(def_to) and
  1177. (cs_ansistrings in aktlocalswitches)) then
  1178. b:=1
  1179. else
  1180. b:=2;
  1181. end;
  1182. end;
  1183. end;
  1184. end;
  1185. floatdef :
  1186. begin
  1187. case def_from^.deftype of
  1188. orddef :
  1189. begin { ordinal to real }
  1190. if is_integer(def_from) then
  1191. begin
  1192. if pfloatdef(def_to)^.typ=f32bit then
  1193. doconv:=tc_int_2_fix
  1194. else
  1195. doconv:=tc_int_2_real;
  1196. b:=1;
  1197. end;
  1198. end;
  1199. floatdef :
  1200. begin { 2 float types ? }
  1201. if pfloatdef(def_from)^.typ=pfloatdef(def_to)^.typ then
  1202. doconv:=tc_equal
  1203. else
  1204. begin
  1205. if pfloatdef(def_from)^.typ=f32bit then
  1206. doconv:=tc_fix_2_real
  1207. else
  1208. if pfloatdef(def_to)^.typ=f32bit then
  1209. doconv:=tc_real_2_fix
  1210. else
  1211. doconv:=tc_real_2_real;
  1212. end;
  1213. b:=1;
  1214. end;
  1215. end;
  1216. end;
  1217. enumdef :
  1218. begin
  1219. if (def_from^.deftype=enumdef) then
  1220. begin
  1221. hd1:=def_from;
  1222. while assigned(penumdef(hd1)^.basedef) do
  1223. hd1:=penumdef(hd1)^.basedef;
  1224. hd2:=def_to;
  1225. while assigned(penumdef(hd2)^.basedef) do
  1226. hd2:=penumdef(hd2)^.basedef;
  1227. if (hd1=hd2) then
  1228. begin
  1229. b:=1;
  1230. { because of packenum they can have different sizes! (JM) }
  1231. doconv:=tc_int_2_int;
  1232. end;
  1233. end;
  1234. end;
  1235. arraydef :
  1236. begin
  1237. { open array is also compatible with a single element of its base type }
  1238. if is_open_array(def_to) and
  1239. is_equal(parraydef(def_to)^.elementtype.def,def_from) then
  1240. begin
  1241. doconv:=tc_equal;
  1242. b:=1;
  1243. end
  1244. else
  1245. begin
  1246. case def_from^.deftype of
  1247. arraydef :
  1248. begin
  1249. { array constructor -> open array }
  1250. if is_open_array(def_to) and
  1251. is_array_constructor(def_from) then
  1252. begin
  1253. if is_void(parraydef(def_from)^.elementtype.def) or
  1254. is_equal(parraydef(def_to)^.elementtype.def,parraydef(def_from)^.elementtype.def) then
  1255. begin
  1256. doconv:=tc_equal;
  1257. b:=1;
  1258. end
  1259. else
  1260. if isconvertable(parraydef(def_from)^.elementtype.def,
  1261. parraydef(def_to)^.elementtype.def,hct,nil,arrayconstructorn,false)<>0 then
  1262. begin
  1263. doconv:=hct;
  1264. b:=2;
  1265. end;
  1266. end
  1267. else
  1268. { array of tvarrec -> array of const }
  1269. if is_array_of_const(def_to) and
  1270. is_equal(parraydef(def_to)^.elementtype.def,parraydef(def_from)^.elementtype.def) then
  1271. begin
  1272. doconv:=tc_equal;
  1273. b:=1;
  1274. end;
  1275. end;
  1276. pointerdef :
  1277. begin
  1278. if is_zero_based_array(def_to) and
  1279. is_equal(ppointerdef(def_from)^.pointertype.def,parraydef(def_to)^.elementtype.def) then
  1280. begin
  1281. doconv:=tc_pointer_2_array;
  1282. b:=1;
  1283. end;
  1284. end;
  1285. stringdef :
  1286. begin
  1287. { string to char array }
  1288. if (not is_special_array(def_to)) and
  1289. is_char(parraydef(def_to)^.elementtype.def) then
  1290. begin
  1291. doconv:=tc_string_2_chararray;
  1292. b:=1;
  1293. end;
  1294. end;
  1295. recorddef :
  1296. begin
  1297. { tvarrec -> array of constconst }
  1298. if is_array_of_const(def_to) and
  1299. is_equal(def_from,parraydef(def_to)^.elementtype.def) then
  1300. begin
  1301. doconv:=tc_equal;
  1302. b:=1;
  1303. end;
  1304. end;
  1305. end;
  1306. end;
  1307. end;
  1308. pointerdef :
  1309. begin
  1310. case def_from^.deftype of
  1311. stringdef :
  1312. begin
  1313. { string constant (which can be part of array constructor)
  1314. to zero terminated string constant }
  1315. if (fromtreetype in [arrayconstructorn,stringconstn]) and
  1316. is_pchar(def_to) then
  1317. begin
  1318. doconv:=tc_cstring_2_pchar;
  1319. b:=1;
  1320. end;
  1321. end;
  1322. orddef :
  1323. begin
  1324. { char constant to zero terminated string constant }
  1325. if (fromtreetype=ordconstn) then
  1326. begin
  1327. if is_equal(def_from,cchardef) and
  1328. is_pchar(def_to) then
  1329. begin
  1330. doconv:=tc_cchar_2_pchar;
  1331. b:=1;
  1332. end
  1333. else
  1334. if is_integer(def_from) then
  1335. begin
  1336. doconv:=tc_cord_2_pointer;
  1337. b:=1;
  1338. end;
  1339. end;
  1340. end;
  1341. arraydef :
  1342. begin
  1343. { chararray to pointer }
  1344. if is_zero_based_array(def_from) and
  1345. is_equal(parraydef(def_from)^.elementtype.def,ppointerdef(def_to)^.pointertype.def) then
  1346. begin
  1347. doconv:=tc_array_2_pointer;
  1348. b:=1;
  1349. end;
  1350. end;
  1351. pointerdef :
  1352. begin
  1353. { child class pointer can be assigned to anchestor pointers }
  1354. if (
  1355. (ppointerdef(def_from)^.pointertype.def^.deftype=objectdef) and
  1356. (ppointerdef(def_to)^.pointertype.def^.deftype=objectdef) and
  1357. pobjectdef(ppointerdef(def_from)^.pointertype.def)^.is_related(
  1358. pobjectdef(ppointerdef(def_to)^.pointertype.def))
  1359. ) or
  1360. { all pointers can be assigned to void-pointer }
  1361. is_equal(ppointerdef(def_to)^.pointertype.def,voiddef) or
  1362. { in my opnion, is this not clean pascal }
  1363. { well, but it's handy to use, it isn't ? (FK) }
  1364. is_equal(ppointerdef(def_from)^.pointertype.def,voiddef) then
  1365. begin
  1366. { but don't allow conversion between farpointer-pointer }
  1367. if (ppointerdef(def_to)^.is_far=ppointerdef(def_from)^.is_far) then
  1368. begin
  1369. doconv:=tc_equal;
  1370. b:=1;
  1371. end;
  1372. end;
  1373. end;
  1374. procvardef :
  1375. begin
  1376. { procedure variable can be assigned to an void pointer }
  1377. { Not anymore. Use the @ operator now.}
  1378. if not(m_tp_procvar in aktmodeswitches) and
  1379. (ppointerdef(def_to)^.pointertype.def^.deftype=orddef) and
  1380. (porddef(ppointerdef(def_to)^.pointertype.def)^.typ=uvoid) then
  1381. begin
  1382. doconv:=tc_equal;
  1383. b:=1;
  1384. end;
  1385. end;
  1386. classrefdef,
  1387. objectdef :
  1388. begin
  1389. { class types and class reference type
  1390. can be assigned to void pointers }
  1391. if (
  1392. is_class_or_interface(def_from) or
  1393. (def_from^.deftype=classrefdef)
  1394. ) and
  1395. (ppointerdef(def_to)^.pointertype.def^.deftype=orddef) and
  1396. (porddef(ppointerdef(def_to)^.pointertype.def)^.typ=uvoid) then
  1397. begin
  1398. doconv:=tc_equal;
  1399. b:=1;
  1400. end;
  1401. end;
  1402. end;
  1403. end;
  1404. setdef :
  1405. begin
  1406. { automatic arrayconstructor -> set conversion }
  1407. if is_array_constructor(def_from) then
  1408. begin
  1409. doconv:=tc_arrayconstructor_2_set;
  1410. b:=1;
  1411. end;
  1412. end;
  1413. procvardef :
  1414. begin
  1415. { proc -> procvar }
  1416. if (def_from^.deftype=procdef) then
  1417. begin
  1418. doconv:=tc_proc_2_procvar;
  1419. if proc_to_procvar_equal(pprocdef(def_from),pprocvardef(def_to)) then
  1420. b:=1;
  1421. end
  1422. else
  1423. { for example delphi allows the assignement from pointers }
  1424. { to procedure variables }
  1425. if (m_pointer_2_procedure in aktmodeswitches) and
  1426. (def_from^.deftype=pointerdef) and
  1427. (ppointerdef(def_from)^.pointertype.def^.deftype=orddef) and
  1428. (porddef(ppointerdef(def_from)^.pointertype.def)^.typ=uvoid) then
  1429. begin
  1430. doconv:=tc_equal;
  1431. b:=1;
  1432. end
  1433. else
  1434. { nil is compatible with procvars }
  1435. if (fromtreetype=niln) then
  1436. begin
  1437. doconv:=tc_equal;
  1438. b:=1;
  1439. end;
  1440. end;
  1441. objectdef :
  1442. begin
  1443. { object pascal objects }
  1444. if (def_from^.deftype=objectdef) and
  1445. pobjectdef(def_from)^.is_related(pobjectdef(def_to)) then
  1446. begin
  1447. doconv:=tc_equal;
  1448. b:=1;
  1449. end
  1450. else
  1451. { Class/interface specific }
  1452. if is_class_or_interface(def_to) then
  1453. begin
  1454. { void pointer also for delphi mode }
  1455. if (m_delphi in aktmodeswitches) and
  1456. is_voidpointer(def_from) then
  1457. begin
  1458. doconv:=tc_equal;
  1459. b:=1;
  1460. end
  1461. else
  1462. { nil is compatible with class instances and interfaces }
  1463. if (fromtreetype=niln) then
  1464. begin
  1465. doconv:=tc_equal;
  1466. b:=1;
  1467. end
  1468. { classes can be assigned to interfaces }
  1469. else if is_interface(def_to) and
  1470. is_class(def_from) and
  1471. assigned(pobjectdef(def_from)^.implementedinterfaces) and
  1472. (pobjectdef(def_from)^.implementedinterfaces^.searchintf(def_to)<>-1) then
  1473. begin
  1474. doconv:=tc_class_2_intf;
  1475. b:=1;
  1476. end;
  1477. end;
  1478. end;
  1479. classrefdef :
  1480. begin
  1481. { class reference types }
  1482. if (def_from^.deftype=classrefdef) then
  1483. begin
  1484. doconv:=tc_equal;
  1485. if pobjectdef(pclassrefdef(def_from)^.pointertype.def)^.is_related(
  1486. pobjectdef(pclassrefdef(def_to)^.pointertype.def)) then
  1487. b:=1;
  1488. end
  1489. else
  1490. { nil is compatible with class references }
  1491. if (fromtreetype=niln) then
  1492. begin
  1493. doconv:=tc_equal;
  1494. b:=1;
  1495. end;
  1496. end;
  1497. filedef :
  1498. begin
  1499. { typed files are all equal to the abstract file type
  1500. name TYPEDFILE in system.pp in is_equal in types.pas
  1501. the problem is that it sholud be also compatible to FILE
  1502. but this would leed to a problem for ASSIGN RESET and REWRITE
  1503. when trying to find the good overloaded function !!
  1504. so all file function are doubled in system.pp
  1505. this is not very beautiful !!}
  1506. if (def_from^.deftype=filedef) and
  1507. (
  1508. (
  1509. (pfiledef(def_from)^.filetyp = ft_typed) and
  1510. (pfiledef(def_to)^.filetyp = ft_typed) and
  1511. (
  1512. (pfiledef(def_from)^.typedfiletype.def = pdef(voiddef)) or
  1513. (pfiledef(def_to)^.typedfiletype.def = pdef(voiddef))
  1514. )
  1515. ) or
  1516. (
  1517. (
  1518. (pfiledef(def_from)^.filetyp = ft_untyped) and
  1519. (pfiledef(def_to)^.filetyp = ft_typed)
  1520. ) or
  1521. (
  1522. (pfiledef(def_from)^.filetyp = ft_typed) and
  1523. (pfiledef(def_to)^.filetyp = ft_untyped)
  1524. )
  1525. )
  1526. ) then
  1527. begin
  1528. doconv:=tc_equal;
  1529. b:=1;
  1530. end
  1531. end;
  1532. else
  1533. begin
  1534. { Interface 2 GUID handling }
  1535. if (def_from^.deftype=errordef) and (def_to=pdef(rec_tguid)) and
  1536. assigned(fromtree) and (fromtree.nodetype=typen) and
  1537. assigned(ttypenode(fromtree).typenodetype) and
  1538. is_interface(ttypenode(fromtree).typenodetype) and
  1539. pobjectdef(ttypenode(fromtree).typenodetype)^.isiidguidvalid then
  1540. begin
  1541. b:=1;
  1542. doconv:=tc_equal;
  1543. end
  1544. else
  1545. { assignment overwritten ?? }
  1546. if assignment_overloaded(def_from,def_to)<>nil then
  1547. b:=2;
  1548. end;
  1549. end;
  1550. isconvertable:=b;
  1551. end;
  1552. function CheckTypes(def1,def2 : pdef) : boolean;
  1553. var
  1554. s1,s2 : string;
  1555. begin
  1556. if not is_equal(def1,def2) then
  1557. begin
  1558. { Crash prevention }
  1559. if (not assigned(def1)) or (not assigned(def2)) then
  1560. Message(type_e_mismatch)
  1561. else
  1562. begin
  1563. s1:=def1^.typename;
  1564. s2:=def2^.typename;
  1565. if (s1<>'<unknown type>') and (s2<>'<unknown type>') then
  1566. Message2(type_e_not_equal_types,def1^.typename,def2^.typename)
  1567. else
  1568. Message(type_e_mismatch);
  1569. end;
  1570. CheckTypes:=false;
  1571. end
  1572. else
  1573. CheckTypes:=true;
  1574. end;
  1575. end.
  1576. {
  1577. $Log$
  1578. Revision 1.35 2001-03-03 12:38:33 jonas
  1579. + support for arraydefs in is_signed (for their rangetype, used in rangechecks)
  1580. Revision 1.34 2001/02/26 19:44:55 peter
  1581. * merged generic m68k updates from fixes branch
  1582. Revision 1.33 2001/02/26 12:47:46 jonas
  1583. * fixed bug in type checking for compatibility of set elements (merged)
  1584. * released fix in options.pas from Carl also for FPC (merged)
  1585. Revision 1.32 2001/02/20 21:44:25 peter
  1586. * tvarrec -> array of const fixed
  1587. Revision 1.31 2001/01/22 11:20:15 jonas
  1588. * fixed web bug 1363 (merged)
  1589. Revision 1.30 2001/01/08 21:43:38 peter
  1590. * string isn't compatible with array of char
  1591. Revision 1.29 2000/12/25 00:07:30 peter
  1592. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  1593. tlinkedlist objects)
  1594. Revision 1.28 2000/12/22 22:38:12 peter
  1595. * fixed bug #1286
  1596. Revision 1.27 2000/12/20 15:59:40 jonas
  1597. - removed obsolete special case for range checking of cardinal constants
  1598. at compile time
  1599. Revision 1.26 2000/12/11 19:13:54 jonas
  1600. * fixed range checking of cardinal constants
  1601. * fixed range checking of "qword constants" (they don't really exist,
  1602. but values > high(int64) were set to zero if assigned to qword)
  1603. Revision 1.25 2000/12/08 14:06:11 jonas
  1604. * fix for web bug 1245: arrays of char with size >255 are now passed to
  1605. overloaded procedures which expect ansistrings instead of shortstrings
  1606. if possible
  1607. * pointer to array of chars (when using $t+) are now also considered
  1608. pchars
  1609. Revision 1.24 2000/11/20 15:52:47 jonas
  1610. * testrange now always cuts a constant to the size of the destination
  1611. if a rangeerror occurred
  1612. * changed an "and $ffffffff" to "and (int64($fffffff) shl 4 + $f" to
  1613. work around the constant evaluation problem we currently have
  1614. Revision 1.23 2000/11/13 14:42:41 jonas
  1615. * fix in testrange so that 64bit constants are properly truncated when
  1616. assigned to 32bit vars
  1617. Revision 1.22 2000/11/13 11:30:55 florian
  1618. * some bugs with interfaces and NIL fixed
  1619. Revision 1.21 2000/11/12 23:24:12 florian
  1620. * interfaces are basically running
  1621. Revision 1.20 2000/11/11 16:13:31 peter
  1622. * farpointer and normal pointer aren't compatible
  1623. Revision 1.19 2000/11/06 22:30:30 peter
  1624. * more fixes
  1625. Revision 1.18 2000/11/04 14:25:22 florian
  1626. + merged Attila's changes for interfaces, not tested yet
  1627. Revision 1.17 2000/10/31 22:30:13 peter
  1628. * merged asm result patch part 2
  1629. Revision 1.16 2000/10/31 22:02:55 peter
  1630. * symtable splitted, no real code changes
  1631. Revision 1.15 2000/10/21 18:16:12 florian
  1632. * a lot of changes:
  1633. - basic dyn. array support
  1634. - basic C++ support
  1635. - some work for interfaces done
  1636. ....
  1637. Revision 1.14 2000/10/14 10:14:56 peter
  1638. * moehrendorf oct 2000 rewrite
  1639. Revision 1.13 2000/10/01 19:48:26 peter
  1640. * lot of compile updates for cg11
  1641. Revision 1.12 2000/09/30 16:08:46 peter
  1642. * more cg11 updates
  1643. Revision 1.11 2000/09/24 15:06:32 peter
  1644. * use defines.inc
  1645. Revision 1.10 2000/09/18 12:31:15 jonas
  1646. * fixed bug in push_addr_param for arrays (merged from fixes branch)
  1647. Revision 1.9 2000/09/10 20:16:21 peter
  1648. * array of const isn't equal with array of <type> (merged)
  1649. Revision 1.8 2000/08/19 19:51:03 peter
  1650. * fixed bug with comparing constsym strings
  1651. Revision 1.7 2000/08/16 13:06:07 florian
  1652. + support of 64 bit integer constants
  1653. Revision 1.6 2000/08/13 13:07:18 peter
  1654. * equal_paras now also checks default parameter value
  1655. Revision 1.5 2000/08/12 06:49:22 florian
  1656. + case statement for int64/qword implemented
  1657. Revision 1.4 2000/08/08 19:26:41 peter
  1658. * equal_constsym() needed for default para
  1659. Revision 1.3 2000/07/13 12:08:28 michael
  1660. + patched to 1.1.0 with former 1.09patch from peter
  1661. Revision 1.2 2000/07/13 11:32:53 michael
  1662. + removed logs
  1663. }