defbase.pas 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 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 defbase;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cclasses,
  23. cpuinfo,
  24. globals,
  25. node,
  26. symconst,symbase,symtype,symdef,symsym;
  27. type
  28. tmmxtype = (mmxno,mmxu8bit,mmxs8bit,mmxu16bit,mmxs16bit,
  29. mmxu32bit,mmxs32bit,mmxfixed16,mmxsingle);
  30. const
  31. {# true if we must never copy this parameter }
  32. never_copy_const_param : boolean = false;
  33. {*****************************************************************************
  34. Basic type functions
  35. *****************************************************************************}
  36. {# Returns true, if definition defines an ordinal type }
  37. function is_ordinal(def : tdef) : boolean;
  38. {# Returns the minimal integer value of the type }
  39. function get_min_value(def : tdef) : TConstExprInt;
  40. {# Returns basetype of the specified integer range }
  41. function range_to_basetype(low,high:TConstExprInt):tbasetype;
  42. {# Returns true, if definition defines an integer type }
  43. function is_integer(def : tdef) : boolean;
  44. {# Returns true if definition is a boolean }
  45. function is_boolean(def : tdef) : boolean;
  46. {# Returns true if definition is a char
  47. This excludes the unicode char.
  48. }
  49. function is_char(def : tdef) : boolean;
  50. {# Returns true if definition is a widechar }
  51. function is_widechar(def : tdef) : boolean;
  52. {# Returns true if definition is a void}
  53. function is_void(def : tdef) : boolean;
  54. {# Returns true if definition is a smallset}
  55. function is_smallset(p : tdef) : boolean;
  56. {# Returns true, if def defines a signed data type
  57. (only for ordinal types)
  58. }
  59. function is_signed(def : tdef) : boolean;
  60. {# Returns true whether def_from's range is comprised in def_to's if both are
  61. orddefs, false otherwise }
  62. function is_in_limit(def_from,def_to : tdef) : boolean;
  63. function is_in_limit_value(val_from:TConstExprInt;def_from,def_to : tdef) : boolean;
  64. {*****************************************************************************
  65. Array helper functions
  66. *****************************************************************************}
  67. {# Returns true, if p points to a zero based (non special like open or
  68. dynamic array def).
  69. This is mainly used to see if the array
  70. is convertable to a pointer
  71. }
  72. function is_zero_based_array(p : tdef) : boolean;
  73. {# Returns true if p points to an open array definition }
  74. function is_open_array(p : tdef) : boolean;
  75. {# Returns true if p points to a dynamic array definition }
  76. function is_dynamic_array(p : tdef) : boolean;
  77. {# Returns true, if p points to an array of const definition }
  78. function is_array_constructor(p : tdef) : boolean;
  79. {# Returns true, if p points to a variant array }
  80. function is_variant_array(p : tdef) : boolean;
  81. {# Returns true, if p points to an array of const }
  82. function is_array_of_const(p : tdef) : boolean;
  83. {# Returns true, if p points any kind of special array
  84. That is if the array is an open array, a variant
  85. array, an array constants constructor, or an
  86. array of const.
  87. }
  88. function is_special_array(p : tdef) : boolean;
  89. {# Returns true if p is a char array def }
  90. function is_chararray(p : tdef) : boolean;
  91. {# Returns true if p is a wide char array def }
  92. function is_widechararray(p : tdef) : boolean;
  93. {*****************************************************************************
  94. String helper functions
  95. *****************************************************************************}
  96. {# Returns true if p points to an open string type }
  97. function is_open_string(p : tdef) : boolean;
  98. {# Returns true if p is an ansi string type }
  99. function is_ansistring(p : tdef) : boolean;
  100. {# Returns true if p is a long string type }
  101. function is_longstring(p : tdef) : boolean;
  102. {# returns true if p is a wide string type }
  103. function is_widestring(p : tdef) : boolean;
  104. {# Returns true if p is a short string type }
  105. function is_shortstring(p : tdef) : boolean;
  106. {# Returns true if p is a pchar def }
  107. function is_pchar(p : tdef) : boolean;
  108. {# Returns true if p is a pwidechar def }
  109. function is_pwidechar(p : tdef) : boolean;
  110. {# Returns true if p is a voidpointer def }
  111. function is_voidpointer(p : tdef) : boolean;
  112. {# Returns true, if definition is a float }
  113. function is_fpu(def : tdef) : boolean;
  114. {# Returns true, if def is a currency type }
  115. function is_currency(def : tdef) : boolean;
  116. {# Returns true, if def is a 64 bit integer type }
  117. function is_64bitint(def : tdef) : boolean;
  118. {# Returns true, if def1 and def2 are semantically the same }
  119. function is_equal(def1,def2 : tdef) : boolean;
  120. {# Checks for type compatibility (subgroups of type)
  121. used for case statements... probably missing stuff
  122. to use on other types
  123. }
  124. function is_subequal(def1, def2: tdef): boolean;
  125. type
  126. tconverttype = (
  127. tc_equal,
  128. tc_not_possible,
  129. tc_string_2_string,
  130. tc_char_2_string,
  131. tc_char_2_chararray,
  132. tc_pchar_2_string,
  133. tc_cchar_2_pchar,
  134. tc_cstring_2_pchar,
  135. tc_ansistring_2_pchar,
  136. tc_string_2_chararray,
  137. tc_chararray_2_string,
  138. tc_array_2_pointer,
  139. tc_pointer_2_array,
  140. tc_int_2_int,
  141. tc_int_2_bool,
  142. tc_bool_2_bool,
  143. tc_bool_2_int,
  144. tc_real_2_real,
  145. tc_int_2_real,
  146. tc_proc_2_procvar,
  147. tc_arrayconstructor_2_set,
  148. tc_load_smallset,
  149. tc_cord_2_pointer,
  150. tc_intf_2_string,
  151. tc_intf_2_guid,
  152. tc_class_2_intf,
  153. tc_char_2_char,
  154. tc_normal_2_smallset,
  155. tc_dynarray_2_openarray,
  156. tc_pwchar_2_string
  157. );
  158. function assignment_overloaded(from_def,to_def : tdef) : tprocdef;
  159. { Returns:
  160. 0 - Not convertable
  161. 1 - Convertable
  162. 2 - Convertable, but not first choice }
  163. function isconvertable(def_from,def_to : tdef;
  164. var doconv : tconverttype;
  165. fromtreetype : tnodetype;
  166. explicit : boolean) : byte;
  167. { this routine is recusrive safe, and is used by the
  168. checking of overloaded assignment operators ONLY!
  169. }
  170. function overloaded_assignment_isconvertable(def_from,def_to : tdef;
  171. var doconv : tconverttype;
  172. fromtreetype : tnodetype;
  173. explicit : boolean; var overload_procs : pprocdeflist) : byte;
  174. { Same as is_equal, but with error message if failed }
  175. function CheckTypes(def1,def2 : tdef) : boolean;
  176. function equal_constsym(sym1,sym2:tconstsym):boolean;
  177. { if acp is cp_all the var const or nothing are considered equal }
  178. type
  179. compare_type = ( cp_none, cp_value_equal_const, cp_all,cp_procvar);
  180. {# true, if two parameter lists are equal
  181. if acp is cp_none, all have to match exactly
  182. if acp is cp_value_equal_const call by value
  183. and call by const parameter are assumed as
  184. equal
  185. allowdefaults indicates if default value parameters
  186. are allowed (in this case, the search order will first
  187. search for a routine with default parameters, before
  188. searching for the same definition with no parameters)
  189. }
  190. function equal_paras(paralist1,paralist2 : TLinkedList; acp : compare_type;allowdefaults:boolean) : boolean;
  191. { True if a type can be allowed for another one
  192. in a func var }
  193. function convertable_paras(paralist1,paralist2 : tlinkedlist; acp : compare_type) : boolean;
  194. { True if a function can be assigned to a procvar }
  195. { changed first argument type to pabstractprocdef so that it can also be }
  196. { used to test compatibility between two pprocvardefs (JM) }
  197. function proc_to_procvar_equal(def1:tabstractprocdef;def2:tprocvardef;exact:boolean) : boolean;
  198. { function get_proc_2_procvar_def(p:tprocsym;d:tprocvardef):tprocdef;}
  199. {# If @var(l) isn't in the range of def a range check error (if not explicit) is generated and
  200. the value is placed within the range
  201. }
  202. procedure testrange(def : tdef;var l : tconstexprint;explicit:boolean);
  203. {# Returns the range of def, where @var(l) is the low-range and @var(h) is
  204. the high-range.
  205. }
  206. procedure getrange(def : tdef;var l : TConstExprInt;var h : TConstExprInt);
  207. { some type helper routines for MMX support }
  208. function is_mmx_able_array(p : tdef) : boolean;
  209. {# returns the mmx type }
  210. function mmx_type(p : tdef) : tmmxtype;
  211. {# returns true, if sym needs an entry in the proplist of a class rtti }
  212. function needs_prop_entry(sym : tsym) : boolean;
  213. implementation
  214. uses
  215. globtype,tokens,systems,verbose,
  216. symtable;
  217. function needs_prop_entry(sym : tsym) : boolean;
  218. begin
  219. needs_prop_entry:=(sp_published in tsym(sym).symoptions) and
  220. (sym.typ in [propertysym,varsym]);
  221. end;
  222. function equal_constsym(sym1,sym2:tconstsym):boolean;
  223. var
  224. p1,p2,pend : pchar;
  225. begin
  226. equal_constsym:=false;
  227. if sym1.consttyp<>sym2.consttyp then
  228. exit;
  229. case sym1.consttyp of
  230. constint,
  231. constbool,
  232. constchar,
  233. constord :
  234. equal_constsym:=(sym1.value.valueord=sym2.value.valueord);
  235. constpointer :
  236. equal_constsym:=(sym1.value.valueordptr=sym2.value.valueordptr);
  237. conststring,constresourcestring :
  238. begin
  239. if sym1.value.len=sym2.value.len then
  240. begin
  241. p1:=pchar(sym1.value.valueptr);
  242. p2:=pchar(sym2.value.valueptr);
  243. pend:=p1+sym1.value.len;
  244. while (p1<pend) do
  245. begin
  246. if p1^<>p2^ then
  247. break;
  248. inc(p1);
  249. inc(p2);
  250. end;
  251. if (p1=pend) then
  252. equal_constsym:=true;
  253. end;
  254. end;
  255. constreal :
  256. equal_constsym:=(pbestreal(sym1.value.valueptr)^=pbestreal(sym2.value.valueptr)^);
  257. constset :
  258. equal_constsym:=(pnormalset(sym1.value.valueptr)^=pnormalset(sym2.value.valueptr)^);
  259. constnil :
  260. equal_constsym:=true;
  261. end;
  262. end;
  263. function equal_paras(paralist1,paralist2 : TLinkedList; acp : compare_type;allowdefaults:boolean) : boolean;
  264. var
  265. def1,def2 : TParaItem;
  266. begin
  267. { we need to parse the list from left-right so the
  268. not-default parameters are checked first }
  269. def1:=TParaItem(paralist1.last);
  270. def2:=TParaItem(paralist2.last);
  271. while (assigned(def1)) and (assigned(def2)) do
  272. begin
  273. case acp of
  274. cp_value_equal_const :
  275. begin
  276. if not(is_equal(def1.paratype.def,def2.paratype.def)) or
  277. ((def1.paratyp<>def2.paratyp) and
  278. ((def1.paratyp in [vs_var,vs_out]) or
  279. (def2.paratyp in [vs_var,vs_out])
  280. )
  281. ) then
  282. begin
  283. equal_paras:=false;
  284. exit;
  285. end;
  286. end;
  287. cp_all,cp_procvar :
  288. begin
  289. if not(is_equal(def1.paratype.def,def2.paratype.def)) or
  290. (def1.paratyp<>def2.paratyp) then
  291. begin
  292. equal_paras:=false;
  293. exit;
  294. end;
  295. end;
  296. cp_none :
  297. begin
  298. if not(is_equal(def1.paratype.def,def2.paratype.def)) then
  299. begin
  300. equal_paras:=false;
  301. exit;
  302. end;
  303. { also check default value if both have it declared }
  304. if assigned(def1.defaultvalue) and
  305. assigned(def2.defaultvalue) then
  306. begin
  307. if not equal_constsym(tconstsym(def1.defaultvalue),tconstsym(def2.defaultvalue)) then
  308. begin
  309. equal_paras:=false;
  310. exit;
  311. end;
  312. end;
  313. end;
  314. end;
  315. def1:=TParaItem(def1.previous);
  316. def2:=TParaItem(def2.previous);
  317. end;
  318. { when both lists are empty then the parameters are equal. Also
  319. when one list is empty and the other has a parameter with default
  320. value assigned then the parameters are also equal }
  321. if ((def1=nil) and (def2=nil)) or
  322. (allowdefaults and
  323. ((assigned(def1) and assigned(def1.defaultvalue)) or
  324. (assigned(def2) and assigned(def2.defaultvalue)))) then
  325. equal_paras:=true
  326. else
  327. equal_paras:=false;
  328. end;
  329. function convertable_paras(paralist1,paralist2 : TLinkedList;acp : compare_type) : boolean;
  330. var
  331. def1,def2 : TParaItem;
  332. doconv : tconverttype;
  333. p : pointer;
  334. b : byte;
  335. begin
  336. def1:=TParaItem(paralist1.first);
  337. def2:=TParaItem(paralist2.first);
  338. while (assigned(def1)) and (assigned(def2)) do
  339. begin
  340. case acp of
  341. cp_value_equal_const :
  342. begin
  343. if (isconvertable(def1.paratype.def,def2.paratype.def,doconv,callparan,false)=0) or
  344. ((def1.paratyp<>def2.paratyp) and
  345. ((def1.paratyp in [vs_out,vs_var]) or
  346. (def2.paratyp in [vs_out,vs_var])
  347. )
  348. ) then
  349. begin
  350. convertable_paras:=false;
  351. exit;
  352. end;
  353. end;
  354. cp_all :
  355. begin
  356. if (isconvertable(def1.paratype.def,def2.paratype.def,doconv,callparan,false)=0) or
  357. (def1.paratyp<>def2.paratyp) then
  358. begin
  359. convertable_paras:=false;
  360. exit;
  361. end;
  362. end;
  363. cp_procvar :
  364. begin
  365. b:=isconvertable(def1.paratype.def,def2.paratype.def,doconv,callparan,false);
  366. if (b=0) or
  367. not(doconv in [tc_equal,tc_int_2_int]) or
  368. (def1.paratyp<>def2.paratyp) or
  369. (not is_special_array(def1.paratype.def) and
  370. not is_special_array(def2.paratype.def) and
  371. (def1.paratype.def.size<>def2.paratype.def.size)) then
  372. begin
  373. convertable_paras:=false;
  374. exit;
  375. end;
  376. end;
  377. cp_none :
  378. begin
  379. if (isconvertable(def1.paratype.def,def2.paratype.def,doconv,callparan,false)=0) then
  380. begin
  381. convertable_paras:=false;
  382. exit;
  383. end;
  384. end;
  385. end;
  386. def1:=TParaItem(def1.next);
  387. def2:=TParaItem(def2.next);
  388. end;
  389. if (def1=nil) and (def2=nil) then
  390. convertable_paras:=true
  391. else
  392. convertable_paras:=false;
  393. end;
  394. { true if a function can be assigned to a procvar }
  395. { changed first argument type to pabstractprocdef so that it can also be }
  396. { used to test compatibility between two pprocvardefs (JM) }
  397. function proc_to_procvar_equal(def1:tabstractprocdef;def2:tprocvardef;exact:boolean) : boolean;
  398. const
  399. po_comp = po_compatibility_options-[po_methodpointer,po_classmethod];
  400. var
  401. ismethod : boolean;
  402. begin
  403. proc_to_procvar_equal:=false;
  404. if not(assigned(def1)) or not(assigned(def2)) then
  405. exit;
  406. { check for method pointer }
  407. if def1.deftype=procvardef then
  408. begin
  409. ismethod:=(po_methodpointer in def1.procoptions);
  410. end
  411. else
  412. begin
  413. ismethod:=assigned(def1.owner) and
  414. (def1.owner.symtabletype=objectsymtable);
  415. end;
  416. if (ismethod and not (po_methodpointer in def2.procoptions)) or
  417. (not(ismethod) and (po_methodpointer in def2.procoptions)) then
  418. begin
  419. Message(type_e_no_method_and_procedure_not_compatible);
  420. exit;
  421. end;
  422. { check return value and para's and options, methodpointer is already checked
  423. parameters may also be convertable }
  424. if is_equal(def1.rettype.def,def2.rettype.def) and
  425. (def1.para_size(target_info.alignment.paraalign)=def2.para_size(target_info.alignment.paraalign)) and
  426. (equal_paras(def1.para,def2.para,cp_procvar,false) or
  427. ((not exact) and convertable_paras(def1.para,def2.para,cp_procvar))) and
  428. ((po_comp * def1.procoptions)= (po_comp * def2.procoptions)) then
  429. proc_to_procvar_equal:=true
  430. else
  431. proc_to_procvar_equal:=false;
  432. end;
  433. { returns true, if def uses FPU }
  434. function is_fpu(def : tdef) : boolean;
  435. begin
  436. is_fpu:=(def.deftype=floatdef);
  437. end;
  438. { returns true, if def is a currency type }
  439. function is_currency(def : tdef) : boolean;
  440. begin
  441. is_currency:=(def.deftype=floatdef) and (tfloatdef(def).typ=s64currency);
  442. end;
  443. function range_to_basetype(low,high:TConstExprInt):tbasetype;
  444. begin
  445. { generate a unsigned range if high<0 and low>=0 }
  446. if (low>=0) and (high<0) then
  447. range_to_basetype:=u32bit
  448. else if (low>=0) and (high<=255) then
  449. range_to_basetype:=u8bit
  450. else if (low>=-128) and (high<=127) then
  451. range_to_basetype:=s8bit
  452. else if (low>=0) and (high<=65536) then
  453. range_to_basetype:=u16bit
  454. else if (low>=-32768) and (high<=32767) then
  455. range_to_basetype:=s16bit
  456. else
  457. range_to_basetype:=s32bit;
  458. end;
  459. { true if p is an ordinal }
  460. function is_ordinal(def : tdef) : boolean;
  461. var
  462. dt : tbasetype;
  463. begin
  464. case def.deftype of
  465. orddef :
  466. begin
  467. dt:=torddef(def).typ;
  468. is_ordinal:=dt in [uchar,uwidechar,
  469. u8bit,u16bit,u32bit,u64bit,
  470. s8bit,s16bit,s32bit,s64bit,
  471. bool8bit,bool16bit,bool32bit];
  472. end;
  473. enumdef :
  474. is_ordinal:=true;
  475. else
  476. is_ordinal:=false;
  477. end;
  478. end;
  479. { returns the min. value of the type }
  480. function get_min_value(def : tdef) : TConstExprInt;
  481. begin
  482. case def.deftype of
  483. orddef:
  484. get_min_value:=torddef(def).low;
  485. enumdef:
  486. get_min_value:=tenumdef(def).min;
  487. else
  488. get_min_value:=0;
  489. end;
  490. end;
  491. { true if p is an integer }
  492. function is_integer(def : tdef) : boolean;
  493. begin
  494. is_integer:=(def.deftype=orddef) and
  495. (torddef(def).typ in [u8bit,u16bit,u32bit,u64bit,
  496. s8bit,s16bit,s32bit,s64bit]);
  497. end;
  498. { true if p is a boolean }
  499. function is_boolean(def : tdef) : boolean;
  500. begin
  501. is_boolean:=(def.deftype=orddef) and
  502. (torddef(def).typ in [bool8bit,bool16bit,bool32bit]);
  503. end;
  504. { true if p is a void }
  505. function is_void(def : tdef) : boolean;
  506. begin
  507. is_void:=(def.deftype=orddef) and
  508. (torddef(def).typ=uvoid);
  509. end;
  510. { true if p is a char }
  511. function is_char(def : tdef) : boolean;
  512. begin
  513. is_char:=(def.deftype=orddef) and
  514. (torddef(def).typ=uchar);
  515. end;
  516. { true if p is a wchar }
  517. function is_widechar(def : tdef) : boolean;
  518. begin
  519. is_widechar:=(def.deftype=orddef) and
  520. (torddef(def).typ=uwidechar);
  521. end;
  522. { true if p is signed (integer) }
  523. function is_signed(def : tdef) : boolean;
  524. var
  525. dt : tbasetype;
  526. begin
  527. case def.deftype of
  528. orddef :
  529. begin
  530. dt:=torddef(def).typ;
  531. is_signed:=(dt in [s8bit,s16bit,s32bit,s64bit]);
  532. end;
  533. enumdef :
  534. is_signed:=tenumdef(def).min < 0;
  535. arraydef :
  536. is_signed:=is_signed(tarraydef(def).rangetype.def);
  537. else
  538. is_signed:=false;
  539. end;
  540. end;
  541. function is_in_limit(def_from,def_to : tdef) : boolean;
  542. var
  543. fromqword, toqword: boolean;
  544. begin
  545. if (def_from.deftype <> orddef) or
  546. (def_to.deftype <> orddef) then
  547. begin
  548. is_in_limit := false;
  549. exit;
  550. end;
  551. fromqword := torddef(def_from).typ = u64bit;
  552. toqword := torddef(def_to).typ = u64bit;
  553. is_in_limit:=(toqword and is_signed(def_from)) or
  554. ((not fromqword) and
  555. (torddef(def_from).low>=torddef(def_to).low) and
  556. (torddef(def_from).high<=torddef(def_to).high));
  557. end;
  558. function is_in_limit_value(val_from:TConstExprInt;def_from,def_to : tdef) : boolean;
  559. begin
  560. if (def_from.deftype <> orddef) and
  561. (def_to.deftype <> orddef) then
  562. internalerror(200210062);
  563. if (torddef(def_to).typ = u64bit) then
  564. begin
  565. is_in_limit_value:=((TConstExprUInt(val_from)>=TConstExprUInt(torddef(def_to).low)) and
  566. (TConstExprUInt(val_from)<=TConstExprUInt(torddef(def_to).high)));
  567. end
  568. else
  569. begin;
  570. is_in_limit_value:=((val_from>=torddef(def_to).low) and
  571. (val_from<=torddef(def_to).high));
  572. end;
  573. end;
  574. { true, if p points to an open array def }
  575. function is_open_string(p : tdef) : boolean;
  576. begin
  577. is_open_string:=(p.deftype=stringdef) and
  578. (tstringdef(p).string_typ=st_shortstring) and
  579. (tstringdef(p).len=0);
  580. end;
  581. { true, if p points to a zero based array def }
  582. function is_zero_based_array(p : tdef) : boolean;
  583. begin
  584. is_zero_based_array:=(p.deftype=arraydef) and
  585. (tarraydef(p).lowrange=0) and
  586. not(is_special_array(p));
  587. end;
  588. { true if p points to a dynamic array def }
  589. function is_dynamic_array(p : tdef) : boolean;
  590. begin
  591. is_dynamic_array:=(p.deftype=arraydef) and
  592. tarraydef(p).IsDynamicArray;
  593. end;
  594. { true, if p points to an open array def }
  595. function is_open_array(p : tdef) : boolean;
  596. begin
  597. { check for s32bittype is needed, because for u32bit the high
  598. range is also -1 ! (PFV) }
  599. is_open_array:=(p.deftype=arraydef) and
  600. (tarraydef(p).rangetype.def=s32bittype.def) and
  601. (tarraydef(p).lowrange=0) and
  602. (tarraydef(p).highrange=-1) and
  603. not(tarraydef(p).IsConstructor) and
  604. not(tarraydef(p).IsVariant) and
  605. not(tarraydef(p).IsArrayOfConst) and
  606. not(tarraydef(p).IsDynamicArray);
  607. end;
  608. { true, if p points to an array of const def }
  609. function is_array_constructor(p : tdef) : boolean;
  610. begin
  611. is_array_constructor:=(p.deftype=arraydef) and
  612. (tarraydef(p).IsConstructor);
  613. end;
  614. { true, if p points to a variant array }
  615. function is_variant_array(p : tdef) : boolean;
  616. begin
  617. is_variant_array:=(p.deftype=arraydef) and
  618. (tarraydef(p).IsVariant);
  619. end;
  620. { true, if p points to an array of const }
  621. function is_array_of_const(p : tdef) : boolean;
  622. begin
  623. is_array_of_const:=(p.deftype=arraydef) and
  624. (tarraydef(p).IsArrayOfConst);
  625. end;
  626. { true, if p points to a special array }
  627. function is_special_array(p : tdef) : boolean;
  628. begin
  629. is_special_array:=(p.deftype=arraydef) and
  630. ((tarraydef(p).IsVariant) or
  631. (tarraydef(p).IsArrayOfConst) or
  632. (tarraydef(p).IsConstructor) or
  633. is_open_array(p)
  634. );
  635. end;
  636. { true if p is an ansi string def }
  637. function is_ansistring(p : tdef) : boolean;
  638. begin
  639. is_ansistring:=(p.deftype=stringdef) and
  640. (tstringdef(p).string_typ=st_ansistring);
  641. end;
  642. { true if p is an long string def }
  643. function is_longstring(p : tdef) : boolean;
  644. begin
  645. is_longstring:=(p.deftype=stringdef) and
  646. (tstringdef(p).string_typ=st_longstring);
  647. end;
  648. { true if p is an wide string def }
  649. function is_widestring(p : tdef) : boolean;
  650. begin
  651. is_widestring:=(p.deftype=stringdef) and
  652. (tstringdef(p).string_typ=st_widestring);
  653. end;
  654. { true if p is an short string def }
  655. function is_shortstring(p : tdef) : boolean;
  656. begin
  657. is_shortstring:=(p.deftype=stringdef) and
  658. (tstringdef(p).string_typ=st_shortstring);
  659. end;
  660. { true if p is a char array def }
  661. function is_chararray(p : tdef) : boolean;
  662. begin
  663. is_chararray:=(p.deftype=arraydef) and
  664. is_equal(tarraydef(p).elementtype.def,cchartype.def) and
  665. not(is_special_array(p));
  666. end;
  667. { true if p is a widechar array def }
  668. function is_widechararray(p : tdef) : boolean;
  669. begin
  670. is_widechararray:=(p.deftype=arraydef) and
  671. is_equal(tarraydef(p).elementtype.def,cwidechartype.def) and
  672. not(is_special_array(p));
  673. end;
  674. { true if p is a pchar def }
  675. function is_pchar(p : tdef) : boolean;
  676. begin
  677. is_pchar:=(p.deftype=pointerdef) and
  678. (is_equal(tpointerdef(p).pointertype.def,cchartype.def) or
  679. (is_zero_based_array(tpointerdef(p).pointertype.def) and
  680. is_chararray(tpointerdef(p).pointertype.def)));
  681. end;
  682. { true if p is a pchar def }
  683. function is_pwidechar(p : tdef) : boolean;
  684. begin
  685. is_pwidechar:=(p.deftype=pointerdef) and
  686. (is_equal(tpointerdef(p).pointertype.def,cwidechartype.def) or
  687. (is_zero_based_array(tpointerdef(p).pointertype.def) and
  688. is_widechararray(tpointerdef(p).pointertype.def)));
  689. end;
  690. { true if p is a voidpointer def }
  691. function is_voidpointer(p : tdef) : boolean;
  692. begin
  693. is_voidpointer:=(p.deftype=pointerdef) and
  694. (tpointerdef(p).pointertype.def.deftype=orddef) and
  695. (torddef(tpointerdef(p).pointertype.def).typ=uvoid);
  696. end;
  697. { true if p is a smallset def }
  698. function is_smallset(p : tdef) : boolean;
  699. begin
  700. is_smallset:=(p.deftype=setdef) and
  701. (tsetdef(p).settype=smallset);
  702. end;
  703. { true, if def is a 64 bit int type }
  704. function is_64bitint(def : tdef) : boolean;
  705. begin
  706. is_64bitint:=(def.deftype=orddef) and (torddef(def).typ in [u64bit,s64bit])
  707. end;
  708. { if l isn't in the range of def a range check error (if not explicit) is generated and
  709. the value is placed within the range }
  710. procedure testrange(def : tdef;var l : tconstexprint;explicit:boolean);
  711. var
  712. lv,hv: TConstExprInt;
  713. error: boolean;
  714. begin
  715. error := false;
  716. { for 64 bit types we need only to check if it is less than }
  717. { zero, if def is a qword node }
  718. if is_64bitint(def) then
  719. begin
  720. if (l<0) and (torddef(def).typ=u64bit) then
  721. begin
  722. { don't zero the result, because it may come from hex notation
  723. like $ffffffffffffffff! (JM)
  724. l:=0; }
  725. if not explicit then
  726. begin
  727. if (cs_check_range in aktlocalswitches) then
  728. Message(parser_e_range_check_error)
  729. else
  730. Message(parser_w_range_check_error);
  731. end;
  732. error := true;
  733. end;
  734. end
  735. else
  736. begin
  737. getrange(def,lv,hv);
  738. if (def.deftype=orddef) and
  739. (torddef(def).typ=u32bit) then
  740. begin
  741. if (l < cardinal(lv)) or
  742. (l > cardinal(hv)) then
  743. begin
  744. if not explicit then
  745. begin
  746. if (cs_check_range in aktlocalswitches) then
  747. Message(parser_e_range_check_error)
  748. else
  749. Message(parser_w_range_check_error);
  750. end;
  751. error := true;
  752. end;
  753. end
  754. else if (l<lv) or (l>hv) then
  755. begin
  756. if not explicit then
  757. begin
  758. if ((def.deftype=enumdef) and
  759. { delphi allows range check errors in
  760. enumeration type casts FK }
  761. not(m_delphi in aktmodeswitches)) or
  762. (cs_check_range in aktlocalswitches) then
  763. Message(parser_e_range_check_error)
  764. else
  765. Message(parser_w_range_check_error);
  766. end;
  767. error := true;
  768. end;
  769. end;
  770. if error then
  771. begin
  772. { Fix the value to fit in the allocated space for this type of variable }
  773. case def.size of
  774. 1: l := l and $ff;
  775. 2: l := l and $ffff;
  776. { work around sign extension bug (to be fixed) (JM) }
  777. 4: l := l and (int64($fffffff) shl 4 + $f);
  778. end;
  779. { do sign extension if necessary (JM) }
  780. if is_signed(def) then
  781. begin
  782. case def.size of
  783. 1: l := shortint(l);
  784. 2: l := smallint(l);
  785. 4: l := longint(l);
  786. end;
  787. end;
  788. end;
  789. end;
  790. { return the range from def in l and h }
  791. procedure getrange(def : tdef;var l : TConstExprInt;var h : TConstExprInt);
  792. begin
  793. case def.deftype of
  794. orddef :
  795. begin
  796. l:=torddef(def).low;
  797. h:=torddef(def).high;
  798. end;
  799. enumdef :
  800. begin
  801. l:=tenumdef(def).min;
  802. h:=tenumdef(def).max;
  803. end;
  804. arraydef :
  805. begin
  806. l:=tarraydef(def).lowrange;
  807. h:=tarraydef(def).highrange;
  808. end;
  809. else
  810. internalerror(987);
  811. end;
  812. end;
  813. function mmx_type(p : tdef) : tmmxtype;
  814. begin
  815. mmx_type:=mmxno;
  816. if is_mmx_able_array(p) then
  817. begin
  818. if tarraydef(p).elementtype.def.deftype=floatdef then
  819. case tfloatdef(tarraydef(p).elementtype.def).typ of
  820. s32real:
  821. mmx_type:=mmxsingle;
  822. end
  823. else
  824. case torddef(tarraydef(p).elementtype.def).typ of
  825. u8bit:
  826. mmx_type:=mmxu8bit;
  827. s8bit:
  828. mmx_type:=mmxs8bit;
  829. u16bit:
  830. mmx_type:=mmxu16bit;
  831. s16bit:
  832. mmx_type:=mmxs16bit;
  833. u32bit:
  834. mmx_type:=mmxu32bit;
  835. s32bit:
  836. mmx_type:=mmxs32bit;
  837. end;
  838. end;
  839. end;
  840. function is_mmx_able_array(p : tdef) : boolean;
  841. begin
  842. {$ifdef SUPPORT_MMX}
  843. if (cs_mmx_saturation in aktlocalswitches) then
  844. begin
  845. is_mmx_able_array:=(p.deftype=arraydef) and
  846. not(is_special_array(p)) and
  847. (
  848. (
  849. (tarraydef(p).elementtype.def.deftype=orddef) and
  850. (
  851. (
  852. (tarraydef(p).lowrange=0) and
  853. (tarraydef(p).highrange=1) and
  854. (torddef(tarraydef(p).elementtype.def).typ in [u32bit,s32bit])
  855. )
  856. or
  857. (
  858. (tarraydef(p).lowrange=0) and
  859. (tarraydef(p).highrange=3) and
  860. (torddef(tarraydef(p).elementtype.def).typ in [u16bit,s16bit])
  861. )
  862. )
  863. )
  864. or
  865. (
  866. (
  867. (tarraydef(p).elementtype.def.deftype=floatdef) and
  868. (
  869. (tarraydef(p).lowrange=0) and
  870. (tarraydef(p).highrange=1) and
  871. (tfloatdef(tarraydef(p).elementtype.def).typ=s32real)
  872. )
  873. )
  874. )
  875. );
  876. end
  877. else
  878. begin
  879. is_mmx_able_array:=(p.deftype=arraydef) and
  880. (
  881. (
  882. (tarraydef(p).elementtype.def.deftype=orddef) and
  883. (
  884. (
  885. (tarraydef(p).lowrange=0) and
  886. (tarraydef(p).highrange=1) and
  887. (torddef(tarraydef(p).elementtype.def).typ in [u32bit,s32bit])
  888. )
  889. or
  890. (
  891. (tarraydef(p).lowrange=0) and
  892. (tarraydef(p).highrange=3) and
  893. (torddef(tarraydef(p).elementtype.def).typ in [u16bit,s16bit])
  894. )
  895. or
  896. (
  897. (tarraydef(p).lowrange=0) and
  898. (tarraydef(p).highrange=7) and
  899. (torddef(tarraydef(p).elementtype.def).typ in [u8bit,s8bit])
  900. )
  901. )
  902. )
  903. or
  904. (
  905. (tarraydef(p).elementtype.def.deftype=floatdef) and
  906. (
  907. (tarraydef(p).lowrange=0) and
  908. (tarraydef(p).highrange=1) and
  909. (tfloatdef(tarraydef(p).elementtype.def).typ=s32real)
  910. )
  911. )
  912. );
  913. end;
  914. {$else SUPPORT_MMX}
  915. is_mmx_able_array:=false;
  916. {$endif SUPPORT_MMX}
  917. end;
  918. function is_equal(def1,def2 : tdef) : boolean;
  919. var
  920. b : boolean;
  921. hd : tdef;
  922. begin
  923. { both types must exists }
  924. if not (assigned(def1) and assigned(def2)) then
  925. begin
  926. is_equal:=false;
  927. exit;
  928. end;
  929. { be sure, that if there is a stringdef, that this is def1 }
  930. if def2.deftype=stringdef then
  931. begin
  932. hd:=def1;
  933. def1:=def2;
  934. def2:=hd;
  935. end;
  936. b:=false;
  937. { both point to the same definition ? }
  938. if def1=def2 then
  939. b:=true
  940. else
  941. { pointer with an equal definition are equal }
  942. if (def1.deftype=pointerdef) and (def2.deftype=pointerdef) then
  943. begin
  944. { check if both are farpointer }
  945. if (tpointerdef(def1).is_far=tpointerdef(def2).is_far) then
  946. begin
  947. { here a problem detected in tabsolutesym }
  948. { the types can be forward type !! }
  949. if assigned(def1.typesym) and (tpointerdef(def1).pointertype.def.deftype=forwarddef) then
  950. b:=(def1.typesym=def2.typesym)
  951. else
  952. b:=tpointerdef(def1).pointertype.def=tpointerdef(def2).pointertype.def;
  953. end
  954. else
  955. b:=false;
  956. end
  957. else
  958. { ordinals are equal only when the ordinal type is equal }
  959. if (def1.deftype=orddef) and (def2.deftype=orddef) then
  960. begin
  961. case torddef(def1).typ of
  962. u8bit,u16bit,u32bit,u64bit,
  963. s8bit,s16bit,s32bit,s64bit:
  964. b:=((torddef(def1).typ=torddef(def2).typ) and
  965. (torddef(def1).low=torddef(def2).low) and
  966. (torddef(def1).high=torddef(def2).high));
  967. uvoid,uchar,uwidechar,
  968. bool8bit,bool16bit,bool32bit:
  969. b:=(torddef(def1).typ=torddef(def2).typ);
  970. else
  971. internalerror(200210061);
  972. end;
  973. end
  974. else
  975. if (def1.deftype=floatdef) and (def2.deftype=floatdef) then
  976. b:=tfloatdef(def1).typ=tfloatdef(def2).typ
  977. else
  978. { strings with the same length are equal }
  979. if (def1.deftype=stringdef) and (def2.deftype=stringdef) and
  980. (tstringdef(def1).string_typ=tstringdef(def2).string_typ) then
  981. begin
  982. b:=not(is_shortstring(def1)) or
  983. (tstringdef(def1).len=tstringdef(def2).len);
  984. end
  985. else
  986. if (def1.deftype=formaldef) and (def2.deftype=formaldef) then
  987. b:=true
  988. { file types with the same file element type are equal }
  989. { this is a problem for assign !! }
  990. { changed to allow if one is untyped }
  991. { all typed files are equal to the special }
  992. { typed file that has voiddef as elemnt type }
  993. { but must NOT match for text file !!! }
  994. else
  995. if (def1.deftype=filedef) and (def2.deftype=filedef) then
  996. b:=(tfiledef(def1).filetyp=tfiledef(def2).filetyp) and
  997. ((
  998. ((tfiledef(def1).typedfiletype.def=nil) and
  999. (tfiledef(def2).typedfiletype.def=nil)) or
  1000. (
  1001. (tfiledef(def1).typedfiletype.def<>nil) and
  1002. (tfiledef(def2).typedfiletype.def<>nil) and
  1003. is_equal(tfiledef(def1).typedfiletype.def,tfiledef(def2).typedfiletype.def)
  1004. ) or
  1005. ( (tfiledef(def1).typedfiletype.def=tdef(voidtype.def)) or
  1006. (tfiledef(def2).typedfiletype.def=tdef(voidtype.def))
  1007. )))
  1008. { sets with the same element base type are equal }
  1009. else
  1010. if (def1.deftype=setdef) and (def2.deftype=setdef) then
  1011. begin
  1012. if assigned(tsetdef(def1).elementtype.def) and
  1013. assigned(tsetdef(def2).elementtype.def) then
  1014. b:=is_subequal(tsetdef(def1).elementtype.def,tsetdef(def2).elementtype.def)
  1015. else
  1016. { empty set is compatible with everything }
  1017. b:=true;
  1018. end
  1019. else
  1020. if (def1.deftype=procvardef) and (def2.deftype=procvardef) then
  1021. begin
  1022. { poassembler isn't important for compatibility }
  1023. { if a method is assigned to a methodpointer }
  1024. { is checked before }
  1025. b:=(tprocvardef(def1).proctypeoption=tprocvardef(def2).proctypeoption) and
  1026. (tprocvardef(def1).proccalloption=tprocvardef(def2).proccalloption) and
  1027. ((tprocvardef(def1).procoptions * po_compatibility_options)=
  1028. (tprocvardef(def2).procoptions * po_compatibility_options)) and
  1029. is_equal(tprocvardef(def1).rettype.def,tprocvardef(def2).rettype.def) and
  1030. equal_paras(tprocvardef(def1).para,tprocvardef(def2).para,cp_all,false);
  1031. end
  1032. else
  1033. if (def1.deftype=arraydef) and (def2.deftype=arraydef) then
  1034. begin
  1035. if is_dynamic_array(def1) and is_dynamic_array(def2) then
  1036. b:=is_equal(tarraydef(def1).elementtype.def,tarraydef(def2).elementtype.def)
  1037. else
  1038. if is_array_of_const(def1) or is_array_of_const(def2) then
  1039. begin
  1040. b:=(is_array_of_const(def1) and is_array_of_const(def2)) or
  1041. (is_array_of_const(def1) and is_array_constructor(def2)) or
  1042. (is_array_of_const(def2) and is_array_constructor(def1));
  1043. end
  1044. else
  1045. if (is_dynamic_array(def1) or is_dynamic_array(def2)) then
  1046. begin
  1047. b := is_dynamic_array(def1) and is_dynamic_array(def2) and
  1048. is_equal(tarraydef(def1).elementtype.def,tarraydef(def2).elementtype.def);
  1049. end
  1050. else
  1051. if is_open_array(def1) or is_open_array(def2) then
  1052. begin
  1053. b:=is_equal(tarraydef(def1).elementtype.def,tarraydef(def2).elementtype.def);
  1054. end
  1055. else
  1056. begin
  1057. b:=not(m_tp7 in aktmodeswitches) and
  1058. not(m_delphi in aktmodeswitches) and
  1059. (tarraydef(def1).lowrange=tarraydef(def2).lowrange) and
  1060. (tarraydef(def1).highrange=tarraydef(def2).highrange) and
  1061. is_equal(tarraydef(def1).elementtype.def,tarraydef(def2).elementtype.def) and
  1062. is_equal(tarraydef(def1).rangetype.def,tarraydef(def2).rangetype.def);
  1063. end;
  1064. end
  1065. else
  1066. if (def1.deftype=classrefdef) and (def2.deftype=classrefdef) then
  1067. begin
  1068. { similar to pointerdef: }
  1069. if assigned(def1.typesym) and (tclassrefdef(def1).pointertype.def.deftype=forwarddef) then
  1070. b:=(def1.typesym=def2.typesym)
  1071. else
  1072. b:=is_equal(tclassrefdef(def1).pointertype.def,tclassrefdef(def2).pointertype.def);
  1073. end;
  1074. is_equal:=b;
  1075. end;
  1076. function is_subequal(def1, def2: tdef): boolean;
  1077. var
  1078. basedef1,basedef2 : tenumdef;
  1079. Begin
  1080. is_subequal := false;
  1081. if assigned(def1) and assigned(def2) then
  1082. Begin
  1083. if (def1.deftype = orddef) and (def2.deftype = orddef) then
  1084. Begin
  1085. { see p.47 of Turbo Pascal 7.01 manual for the separation of types }
  1086. { range checking for case statements is done with testrange }
  1087. case torddef(def1).typ of
  1088. u8bit,u16bit,u32bit,
  1089. s8bit,s16bit,s32bit,s64bit,u64bit :
  1090. is_subequal:=(torddef(def2).typ in [s64bit,u64bit,s32bit,u32bit,u8bit,s8bit,s16bit,u16bit]);
  1091. bool8bit,bool16bit,bool32bit :
  1092. is_subequal:=(torddef(def2).typ in [bool8bit,bool16bit,bool32bit]);
  1093. uchar :
  1094. is_subequal:=(torddef(def2).typ=uchar);
  1095. uwidechar :
  1096. is_subequal:=(torddef(def2).typ=uwidechar);
  1097. end;
  1098. end
  1099. else
  1100. Begin
  1101. { I assume that both enumerations are equal when the first }
  1102. { pointers are equal. }
  1103. { I changed this to assume that the enums are equal }
  1104. { if the basedefs are equal (FK) }
  1105. if (def1.deftype=enumdef) and (def2.deftype=enumdef) then
  1106. Begin
  1107. { get both basedefs }
  1108. basedef1:=tenumdef(def1);
  1109. while assigned(basedef1.basedef) do
  1110. basedef1:=basedef1.basedef;
  1111. basedef2:=tenumdef(def2);
  1112. while assigned(basedef2.basedef) do
  1113. basedef2:=basedef2.basedef;
  1114. is_subequal:=basedef1=basedef2;
  1115. {
  1116. if tenumdef(def1).firstenum = tenumdef(def2).firstenum then
  1117. is_subequal := TRUE;
  1118. }
  1119. end;
  1120. end;
  1121. end; { endif assigned ... }
  1122. end;
  1123. (* function assignment_overloaded(from_def,to_def : tdef) : tprocdef;
  1124. var
  1125. passprocs : pprocdeflist;
  1126. convtyp : tconverttype;
  1127. begin
  1128. assignment_overloaded:=nil;
  1129. if not assigned(overloaded_operators[_ASSIGNMENT]) then
  1130. exit;
  1131. { look for an exact match first }
  1132. passprocs:=overloaded_operators[_ASSIGNMENT].defs;
  1133. while assigned(passprocs) do
  1134. begin
  1135. if is_equal(passprocs^.def.rettype.def,to_def) and
  1136. (TParaItem(passprocs^.def.Para.first).paratype.def=from_def) then
  1137. begin
  1138. assignment_overloaded:=passprocs^.def;
  1139. exit;
  1140. end;
  1141. passprocs:=passprocs^.next;
  1142. end;
  1143. { .... then look for an equal match }
  1144. passprocs:=overloaded_operators[_ASSIGNMENT].defs;
  1145. while assigned(passprocs) do
  1146. begin
  1147. if is_equal(passprocs^.def.rettype.def,to_def) and
  1148. is_equal(TParaItem(passprocs^.def.Para.first).paratype.def,from_def) then
  1149. begin
  1150. assignment_overloaded:=passprocs^.def;
  1151. exit;
  1152. end;
  1153. passprocs:=passprocs^.next;
  1154. end;
  1155. { .... then for convert level 1 }
  1156. passprocs:=overloaded_operators[_ASSIGNMENT].defs;
  1157. while assigned(passprocs) do
  1158. begin
  1159. if is_equal(passprocs^.def.rettype.def,to_def) and
  1160. (isconvertable(from_def,TParaItem(passprocs^.def.Para.first).paratype.def,convtyp,ordconstn,false)=1) then
  1161. begin
  1162. assignment_overloaded:=passprocs^.def;
  1163. exit;
  1164. end;
  1165. passprocs:=passprocs^.next;
  1166. end;
  1167. end;
  1168. *)
  1169. { this is an internal routine to take care of recursivity }
  1170. function internal_assignment_overloaded(from_def,to_def : tdef;
  1171. var overload_procs : pprocdeflist) : tprocdef;
  1172. var
  1173. p :pprocdeflist;
  1174. _result : tprocdef;
  1175. begin
  1176. internal_assignment_overloaded:=nil;
  1177. p := nil;
  1178. if not assigned(overloaded_operators[_ASSIGNMENT]) then
  1179. exit;
  1180. { look for an exact match first, from start of list }
  1181. _result:=overloaded_operators[_ASSIGNMENT].
  1182. search_procdef_byretdef_by1paradef(to_def,from_def,dm_exact,
  1183. p);
  1184. if assigned(_result) then
  1185. begin
  1186. internal_assignment_overloaded := _result;
  1187. exit;
  1188. end;
  1189. { .... then look for an equal match, from start of list }
  1190. _result:=overloaded_operators[_ASSIGNMENT].
  1191. search_procdef_byretdef_by1paradef(to_def,from_def,dm_equal,
  1192. p);
  1193. if assigned(_result) then
  1194. begin
  1195. internal_assignment_overloaded := _result;
  1196. exit;
  1197. end;
  1198. { .... then for convert level 1, continue from where we were at }
  1199. internal_assignment_overloaded:=overloaded_operators[_ASSIGNMENT].
  1200. search_procdef_byretdef_by1paradef(to_def,from_def,dm_convertl1,
  1201. overload_procs);
  1202. end;
  1203. function assignment_overloaded(from_def,to_def : tdef) : tprocdef;
  1204. var
  1205. p : pprocdeflist;
  1206. begin
  1207. p:=nil;
  1208. assignment_overloaded:=nil;
  1209. assignment_overloaded:=internal_assignment_overloaded(
  1210. from_def, to_def, p);
  1211. end;
  1212. { Returns:
  1213. 0 - Not convertable
  1214. 1 - Convertable
  1215. 2 - Convertable, but not first choice
  1216. }
  1217. function isconvertable(def_from,def_to : tdef;
  1218. var doconv : tconverttype;
  1219. fromtreetype : tnodetype;
  1220. explicit : boolean) : byte;
  1221. var
  1222. p: pprocdeflist;
  1223. begin
  1224. p:=nil;
  1225. isconvertable:=overloaded_assignment_isconvertable(def_from,def_to,
  1226. doconv, fromtreetype, explicit,p);
  1227. end;
  1228. function overloaded_assignment_isconvertable(def_from,def_to : tdef;
  1229. var doconv : tconverttype;
  1230. fromtreetype : tnodetype;
  1231. explicit : boolean; var overload_procs : pprocdeflist) : byte;
  1232. { Tbasetype:
  1233. uvoid,
  1234. u8bit,u16bit,u32bit,u64bit,
  1235. s8bit,s16bit,s32bit,s64bit,
  1236. bool8bit,bool16bit,bool32bit,
  1237. uchar,uwidechar }
  1238. type
  1239. tbasedef=(bvoid,bchar,bint,bbool);
  1240. const
  1241. basedeftbl:array[tbasetype] of tbasedef =
  1242. (bvoid,
  1243. bint,bint,bint,bint,
  1244. bint,bint,bint,bint,
  1245. bbool,bbool,bbool,
  1246. bchar,bchar);
  1247. basedefconverts : array[tbasedef,tbasedef] of tconverttype =
  1248. ((tc_not_possible,tc_not_possible,tc_not_possible,tc_not_possible),
  1249. (tc_not_possible,tc_char_2_char,tc_not_possible,tc_not_possible),
  1250. (tc_not_possible,tc_not_possible,tc_int_2_int,tc_int_2_bool),
  1251. (tc_not_possible,tc_not_possible,tc_bool_2_int,tc_bool_2_bool));
  1252. var
  1253. b : byte;
  1254. hd1,hd2 : tdef;
  1255. hct : tconverttype;
  1256. hd3 : tobjectdef;
  1257. begin
  1258. { safety check }
  1259. if not(assigned(def_from) and assigned(def_to)) then
  1260. begin
  1261. overloaded_assignment_isconvertable :=0;
  1262. exit;
  1263. end;
  1264. { tp7 procvar def support, in tp7 a procvar is always called, if the
  1265. procvar is passed explicit a addrn would be there }
  1266. if (m_tp_procvar in aktmodeswitches) and
  1267. (def_from.deftype=procvardef) and
  1268. (fromtreetype=loadn) and
  1269. { only if the procvar doesn't require any paramters }
  1270. (tprocvardef(def_from).minparacount = 0) then
  1271. begin
  1272. def_from:=tprocvardef(def_from).rettype.def;
  1273. end;
  1274. { we walk the wanted (def_to) types and check then the def_from
  1275. types if there is a conversion possible }
  1276. b:=0;
  1277. case def_to.deftype of
  1278. orddef :
  1279. begin
  1280. case def_from.deftype of
  1281. orddef :
  1282. begin
  1283. doconv:=basedefconverts[basedeftbl[torddef(def_from).typ],basedeftbl[torddef(def_to).typ]];
  1284. b:=1;
  1285. if (doconv=tc_not_possible) or
  1286. ((doconv=tc_int_2_bool) and
  1287. (not explicit) and
  1288. (not is_boolean(def_from))) or
  1289. ((doconv=tc_bool_2_int) and
  1290. (not explicit) and
  1291. (not is_boolean(def_to))) then
  1292. b:=0
  1293. else
  1294. { "punish" bad type conversions :) (JM) }
  1295. if not is_in_limit(def_from,def_to) and
  1296. (def_from.size > def_to.size) then
  1297. b := 2;
  1298. end;
  1299. enumdef :
  1300. begin
  1301. { needed for char(enum) }
  1302. if explicit then
  1303. begin
  1304. doconv:=tc_int_2_int;
  1305. b:=1;
  1306. end;
  1307. end;
  1308. end;
  1309. end;
  1310. stringdef :
  1311. begin
  1312. case def_from.deftype of
  1313. stringdef :
  1314. begin
  1315. doconv:=tc_string_2_string;
  1316. b:=1;
  1317. end;
  1318. orddef :
  1319. begin
  1320. { char to string}
  1321. if is_char(def_from) or
  1322. is_widechar(def_from) then
  1323. begin
  1324. doconv:=tc_char_2_string;
  1325. b:=1;
  1326. end;
  1327. end;
  1328. arraydef :
  1329. begin
  1330. { array of char to string, the length check is done by the firstpass of this node }
  1331. if is_chararray(def_from) or
  1332. (is_equal(tarraydef(def_from).elementtype.def,cchartype.def) and
  1333. is_open_array(def_from)) then
  1334. begin
  1335. doconv:=tc_chararray_2_string;
  1336. if is_open_array(def_from) or
  1337. (is_shortstring(def_to) and
  1338. (def_from.size <= 255)) or
  1339. (is_ansistring(def_to) and
  1340. (def_from.size > 255)) then
  1341. b:=1
  1342. else
  1343. b:=2;
  1344. end;
  1345. end;
  1346. pointerdef :
  1347. begin
  1348. { pchar can be assigned to short/ansistrings,
  1349. but not in tp7 compatible mode }
  1350. if not(m_tp7 in aktmodeswitches) then
  1351. begin
  1352. if is_pchar(def_from) then
  1353. begin
  1354. doconv:=tc_pchar_2_string;
  1355. { trefer ansistrings because pchars can overflow shortstrings, }
  1356. { but only if ansistrings are the default (JM) }
  1357. if (is_shortstring(def_to) and
  1358. not(cs_ansistrings in aktlocalswitches)) or
  1359. (is_ansistring(def_to) and
  1360. (cs_ansistrings in aktlocalswitches)) then
  1361. b:=1
  1362. else
  1363. b:=2;
  1364. end
  1365. else if is_pwidechar(def_from) then
  1366. begin
  1367. doconv:=tc_pwchar_2_string;
  1368. { trefer ansistrings because pchars can overflow shortstrings, }
  1369. { but only if ansistrings are the default (JM) }
  1370. if is_widestring(def_to) then
  1371. b:=1
  1372. else
  1373. b:=2;
  1374. end;
  1375. end;
  1376. end;
  1377. end;
  1378. end;
  1379. floatdef :
  1380. begin
  1381. case def_from.deftype of
  1382. orddef :
  1383. begin { ordinal to real }
  1384. if is_integer(def_from) then
  1385. begin
  1386. doconv:=tc_int_2_real;
  1387. b:=1;
  1388. end;
  1389. end;
  1390. floatdef :
  1391. begin { 2 float types ? }
  1392. if tfloatdef(def_from).typ=tfloatdef(def_to).typ then
  1393. doconv:=tc_equal
  1394. else
  1395. doconv:=tc_real_2_real;
  1396. b:=1;
  1397. end;
  1398. end;
  1399. end;
  1400. enumdef :
  1401. begin
  1402. if (def_from.deftype=enumdef) then
  1403. begin
  1404. if explicit then
  1405. begin
  1406. b:=1;
  1407. doconv:=tc_int_2_int;
  1408. end
  1409. else
  1410. begin
  1411. hd1:=def_from;
  1412. while assigned(tenumdef(hd1).basedef) do
  1413. hd1:=tenumdef(hd1).basedef;
  1414. hd2:=def_to;
  1415. while assigned(tenumdef(hd2).basedef) do
  1416. hd2:=tenumdef(hd2).basedef;
  1417. if (hd1=hd2) then
  1418. begin
  1419. b:=1;
  1420. { because of packenum they can have different sizes! (JM) }
  1421. doconv:=tc_int_2_int;
  1422. end;
  1423. end;
  1424. end;
  1425. end;
  1426. arraydef :
  1427. begin
  1428. { open array is also compatible with a single element of its base type }
  1429. if is_open_array(def_to) and
  1430. is_equal(tarraydef(def_to).elementtype.def,def_from) then
  1431. begin
  1432. doconv:=tc_equal;
  1433. b:=1;
  1434. end
  1435. else if is_dynamic_array(def_to) and
  1436. { nil is compatible with dyn. arrays }
  1437. (fromtreetype=niln) then
  1438. begin
  1439. doconv:=tc_equal;
  1440. b:=1;
  1441. end
  1442. else
  1443. begin
  1444. case def_from.deftype of
  1445. arraydef :
  1446. begin
  1447. { array constructor -> open array }
  1448. if is_open_array(def_to) and
  1449. is_array_constructor(def_from) then
  1450. begin
  1451. if is_void(tarraydef(def_from).elementtype.def) or
  1452. is_equal(tarraydef(def_to).elementtype.def,tarraydef(def_from).elementtype.def) then
  1453. begin
  1454. doconv:=tc_equal;
  1455. b:=1;
  1456. end
  1457. else
  1458. if isconvertable(tarraydef(def_from).elementtype.def,
  1459. tarraydef(def_to).elementtype.def,hct,arrayconstructorn,false)<>0 then
  1460. begin
  1461. doconv:=hct;
  1462. b:=2;
  1463. end;
  1464. end
  1465. else
  1466. { dynamic array -> open array }
  1467. if is_dynamic_array(def_from) and
  1468. is_open_array(def_to) and
  1469. is_equal(tarraydef(def_to).elementtype.def,tarraydef(def_from).elementtype.def) then
  1470. begin
  1471. doconv := tc_dynarray_2_openarray;
  1472. b := 2;
  1473. end
  1474. else
  1475. { array of tvarrec -> array of const }
  1476. if is_array_of_const(def_to) and
  1477. is_equal(tarraydef(def_to).elementtype.def,tarraydef(def_from).elementtype.def) then
  1478. begin
  1479. doconv:=tc_equal;
  1480. b:=1;
  1481. end;
  1482. end;
  1483. pointerdef :
  1484. begin
  1485. if is_zero_based_array(def_to) and
  1486. is_equal(tpointerdef(def_from).pointertype.def,tarraydef(def_to).elementtype.def) then
  1487. begin
  1488. doconv:=tc_pointer_2_array;
  1489. b:=1;
  1490. end;
  1491. end;
  1492. stringdef :
  1493. begin
  1494. { string to char array }
  1495. if (not is_special_array(def_to)) and
  1496. is_char(tarraydef(def_to).elementtype.def) then
  1497. begin
  1498. doconv:=tc_string_2_chararray;
  1499. b:=1;
  1500. end;
  1501. end;
  1502. orddef:
  1503. begin
  1504. if is_chararray(def_to) and
  1505. is_char(def_from) then
  1506. begin
  1507. doconv:=tc_char_2_chararray;
  1508. b:=2;
  1509. end;
  1510. end;
  1511. recorddef :
  1512. begin
  1513. { tvarrec -> array of const }
  1514. if is_array_of_const(def_to) and
  1515. is_equal(def_from,tarraydef(def_to).elementtype.def) then
  1516. begin
  1517. doconv:=tc_equal;
  1518. b:=1;
  1519. end;
  1520. end;
  1521. end;
  1522. end;
  1523. end;
  1524. pointerdef :
  1525. begin
  1526. case def_from.deftype of
  1527. stringdef :
  1528. begin
  1529. { string constant (which can be part of array constructor)
  1530. to zero terminated string constant }
  1531. if (fromtreetype in [arrayconstructorn,stringconstn]) and
  1532. is_pchar(def_to) or is_pwidechar(def_to) then
  1533. begin
  1534. doconv:=tc_cstring_2_pchar;
  1535. b:=1;
  1536. end;
  1537. end;
  1538. orddef :
  1539. begin
  1540. { char constant to zero terminated string constant }
  1541. if (fromtreetype=ordconstn) then
  1542. begin
  1543. if is_equal(def_from,cchartype.def) and
  1544. is_pchar(def_to) then
  1545. begin
  1546. doconv:=tc_cchar_2_pchar;
  1547. b:=1;
  1548. end
  1549. else
  1550. if is_integer(def_from) then
  1551. begin
  1552. doconv:=tc_cord_2_pointer;
  1553. b:=1;
  1554. end;
  1555. end;
  1556. end;
  1557. arraydef :
  1558. begin
  1559. { chararray to pointer }
  1560. if is_zero_based_array(def_from) and
  1561. is_equal(tarraydef(def_from).elementtype.def,tpointerdef(def_to).pointertype.def) then
  1562. begin
  1563. doconv:=tc_array_2_pointer;
  1564. b:=1;
  1565. end;
  1566. end;
  1567. pointerdef :
  1568. begin
  1569. { child class pointer can be assigned to anchestor pointers }
  1570. if (
  1571. (tpointerdef(def_from).pointertype.def.deftype=objectdef) and
  1572. (tpointerdef(def_to).pointertype.def.deftype=objectdef) and
  1573. tobjectdef(tpointerdef(def_from).pointertype.def).is_related(
  1574. tobjectdef(tpointerdef(def_to).pointertype.def))
  1575. ) or
  1576. { all pointers can be assigned to void-pointer }
  1577. is_equal(tpointerdef(def_to).pointertype.def,voidtype.def) or
  1578. { in my opnion, is this not clean pascal }
  1579. { well, but it's handy to use, it isn't ? (FK) }
  1580. is_equal(tpointerdef(def_from).pointertype.def,voidtype.def) then
  1581. begin
  1582. { but don't allow conversion between farpointer-pointer }
  1583. if (tpointerdef(def_to).is_far=tpointerdef(def_from).is_far) then
  1584. begin
  1585. doconv:=tc_equal;
  1586. b:=1;
  1587. end;
  1588. end;
  1589. end;
  1590. procvardef :
  1591. begin
  1592. { procedure variable can be assigned to an void pointer }
  1593. { Not anymore. Use the @ operator now.}
  1594. if not(m_tp_procvar in aktmodeswitches) and
  1595. (tpointerdef(def_to).pointertype.def.deftype=orddef) and
  1596. (torddef(tpointerdef(def_to).pointertype.def).typ=uvoid) then
  1597. begin
  1598. doconv:=tc_equal;
  1599. b:=1;
  1600. end;
  1601. end;
  1602. classrefdef,
  1603. objectdef :
  1604. begin
  1605. { class types and class reference type
  1606. can be assigned to void pointers }
  1607. if (
  1608. is_class_or_interface(def_from) or
  1609. (def_from.deftype=classrefdef)
  1610. ) and
  1611. (tpointerdef(def_to).pointertype.def.deftype=orddef) and
  1612. (torddef(tpointerdef(def_to).pointertype.def).typ=uvoid) then
  1613. begin
  1614. doconv:=tc_equal;
  1615. b:=1;
  1616. end;
  1617. end;
  1618. end;
  1619. end;
  1620. setdef :
  1621. begin
  1622. { automatic arrayconstructor -> set conversion }
  1623. if is_array_constructor(def_from) then
  1624. begin
  1625. doconv:=tc_arrayconstructor_2_set;
  1626. b:=1;
  1627. end;
  1628. end;
  1629. procvardef :
  1630. begin
  1631. { proc -> procvar }
  1632. if (def_from.deftype=procdef) and
  1633. (m_tp_procvar in aktmodeswitches) then
  1634. begin
  1635. doconv:=tc_proc_2_procvar;
  1636. if proc_to_procvar_equal(tprocdef(def_from),tprocvardef(def_to),false) then
  1637. b:=1;
  1638. end
  1639. { procvar -> procvar }
  1640. else
  1641. if (def_from.deftype=procvardef) and
  1642. (proc_to_procvar_equal(tprocvardef(def_from),tprocvardef(def_to),false)) then
  1643. begin
  1644. doconv:=tc_equal;
  1645. b := 2;
  1646. end
  1647. else
  1648. { for example delphi allows the assignement from pointers }
  1649. { to procedure variables }
  1650. if (m_pointer_2_procedure in aktmodeswitches) and
  1651. (def_from.deftype=pointerdef) and
  1652. (tpointerdef(def_from).pointertype.def.deftype=orddef) and
  1653. (torddef(tpointerdef(def_from).pointertype.def).typ=uvoid) then
  1654. begin
  1655. doconv:=tc_equal;
  1656. b:=1;
  1657. end
  1658. else
  1659. { nil is compatible with procvars }
  1660. if (fromtreetype=niln) then
  1661. begin
  1662. doconv:=tc_equal;
  1663. b:=1;
  1664. end;
  1665. end;
  1666. objectdef :
  1667. begin
  1668. { object pascal objects }
  1669. if (def_from.deftype=objectdef) and
  1670. tobjectdef(def_from).is_related(tobjectdef(def_to)) then
  1671. begin
  1672. doconv:=tc_equal;
  1673. b:=1;
  1674. end
  1675. else
  1676. { Class/interface specific }
  1677. if is_class_or_interface(def_to) then
  1678. begin
  1679. { void pointer also for delphi mode }
  1680. if (m_delphi in aktmodeswitches) and
  1681. is_voidpointer(def_from) then
  1682. begin
  1683. doconv:=tc_equal;
  1684. b:=1;
  1685. end
  1686. else
  1687. { nil is compatible with class instances and interfaces }
  1688. if (fromtreetype=niln) then
  1689. begin
  1690. doconv:=tc_equal;
  1691. b:=1;
  1692. end
  1693. { classes can be assigned to interfaces }
  1694. else if is_interface(def_to) and
  1695. is_class(def_from) and
  1696. assigned(tobjectdef(def_from).implementedinterfaces) then
  1697. begin
  1698. { we've to search in parent classes as well }
  1699. hd3:=tobjectdef(def_from);
  1700. while assigned(hd3) do
  1701. begin
  1702. if hd3.implementedinterfaces.searchintf(def_to)<>-1 then
  1703. begin
  1704. doconv:=tc_class_2_intf;
  1705. b:=1;
  1706. break;
  1707. end;
  1708. hd3:=hd3.childof;
  1709. end;
  1710. end
  1711. { Interface 2 GUID handling }
  1712. else if (def_to=tdef(rec_tguid)) and
  1713. (fromtreetype=typen) and
  1714. is_interface(def_from) and
  1715. assigned(tobjectdef(def_from).iidguid) then
  1716. begin
  1717. b:=1;
  1718. doconv:=tc_equal;
  1719. end;
  1720. end;
  1721. end;
  1722. classrefdef :
  1723. begin
  1724. { class reference types }
  1725. if (def_from.deftype=classrefdef) then
  1726. begin
  1727. doconv:=tc_equal;
  1728. if tobjectdef(tclassrefdef(def_from).pointertype.def).is_related(
  1729. tobjectdef(tclassrefdef(def_to).pointertype.def)) then
  1730. b:=1;
  1731. end
  1732. else
  1733. { nil is compatible with class references }
  1734. if (fromtreetype=niln) then
  1735. begin
  1736. doconv:=tc_equal;
  1737. b:=1;
  1738. end;
  1739. end;
  1740. filedef :
  1741. begin
  1742. { typed files are all equal to the abstract file type
  1743. name TYPEDFILE in system.pp in is_equal in types.pas
  1744. the problem is that it sholud be also compatible to FILE
  1745. but this would leed to a problem for ASSIGN RESET and REWRITE
  1746. when trying to find the good overloaded function !!
  1747. so all file function are doubled in system.pp
  1748. this is not very beautiful !!}
  1749. if (def_from.deftype=filedef) and
  1750. (
  1751. (
  1752. (tfiledef(def_from).filetyp = ft_typed) and
  1753. (tfiledef(def_to).filetyp = ft_typed) and
  1754. (
  1755. (tfiledef(def_from).typedfiletype.def = tdef(voidtype.def)) or
  1756. (tfiledef(def_to).typedfiletype.def = tdef(voidtype.def))
  1757. )
  1758. ) or
  1759. (
  1760. (
  1761. (tfiledef(def_from).filetyp = ft_untyped) and
  1762. (tfiledef(def_to).filetyp = ft_typed)
  1763. ) or
  1764. (
  1765. (tfiledef(def_from).filetyp = ft_typed) and
  1766. (tfiledef(def_to).filetyp = ft_untyped)
  1767. )
  1768. )
  1769. ) then
  1770. begin
  1771. doconv:=tc_equal;
  1772. b:=1;
  1773. end
  1774. end;
  1775. recorddef :
  1776. begin
  1777. { interface -> guid }
  1778. if is_interface(def_from) and
  1779. (def_to=rec_tguid) then
  1780. begin
  1781. doconv:=tc_intf_2_guid;
  1782. b:=1;
  1783. end
  1784. else
  1785. begin
  1786. { assignment overwritten ?? }
  1787. if internal_assignment_overloaded(def_from,def_to,overload_procs)<>nil then
  1788. b:=2;
  1789. end;
  1790. end;
  1791. { a variant isn't compatible to nil (FK)
  1792. variantdef :
  1793. begin
  1794. if (fromtreetype=niln) then
  1795. begin
  1796. doconv:=tc_equal;
  1797. b:=1;
  1798. end;
  1799. end;
  1800. }
  1801. formaldef :
  1802. begin
  1803. { Just about everything can be converted to a formaldef...}
  1804. if not (def_from.deftype in [abstractdef,errordef]) then
  1805. b:=1
  1806. else
  1807. begin
  1808. { assignment overwritten ?? }
  1809. if internal_assignment_overloaded(def_from,def_to,overload_procs)<>nil then
  1810. b:=2;
  1811. end;
  1812. end;
  1813. end;
  1814. { if we didn't find an appropriate type conversion yet, we try the overloaded := operator }
  1815. { This is done for variants only yet, maybe we should do this for other types as well (FK) }
  1816. if (b=0) and ((def_from.deftype in [variantdef]) or (def_to.deftype in [variantdef])) then
  1817. begin
  1818. if internal_assignment_overloaded(def_from,def_to,overload_procs)<>nil then
  1819. b:=2;
  1820. end;
  1821. overloaded_assignment_isconvertable :=b;
  1822. end;
  1823. function CheckTypes(def1,def2 : tdef) : boolean;
  1824. var
  1825. s1,s2 : string;
  1826. begin
  1827. CheckTypes:=False;
  1828. if not is_equal(def1,def2) then
  1829. begin
  1830. { Crash prevention }
  1831. if (not assigned(def1)) or (not assigned(def2)) then
  1832. Message(type_e_mismatch)
  1833. else
  1834. begin
  1835. if not is_subequal(def1,def2) then
  1836. begin
  1837. s1:=def1.typename;
  1838. s2:=def2.typename;
  1839. Message2(type_e_not_equal_types,def1.typename,def2.typename);
  1840. end
  1841. else
  1842. CheckTypes := true;
  1843. end;
  1844. end
  1845. else
  1846. CheckTypes := True;
  1847. end;
  1848. end.
  1849. {
  1850. $Log$
  1851. Revision 1.27 2002-11-22 22:48:10 carl
  1852. * memory optimization with tconstsym (1.5%)
  1853. Revision 1.26 2002/11/17 16:31:55 carl
  1854. * memory optimization (3-4%) : cleanup of tai fields,
  1855. cleanup of tdef and tsym fields.
  1856. * make it work for m68k
  1857. Revision 1.25 2002/11/16 18:00:53 peter
  1858. * fix merged proc-procvar check
  1859. Revision 1.24 2002/11/15 01:58:46 peter
  1860. * merged changes from 1.0.7 up to 04-11
  1861. - -V option for generating bug report tracing
  1862. - more tracing for option parsing
  1863. - errors for cdecl and high()
  1864. - win32 import stabs
  1865. - win32 records<=8 are returned in eax:edx (turned off by default)
  1866. - heaptrc update
  1867. - more info for temp management in .s file with EXTDEBUG
  1868. Revision 1.23 2002/10/20 15:34:16 peter
  1869. * removed df_unique flag. It breaks code. For a good type=type <id>
  1870. a def copy is required
  1871. Revision 1.22 2002/10/10 16:07:57 florian
  1872. + several widestring/pwidechar related stuff added
  1873. Revision 1.21 2002/10/09 21:01:41 florian
  1874. * variants aren't compatible with nil
  1875. Revision 1.20 2002/10/07 09:49:42 florian
  1876. * overloaded :=-operator is now searched when looking for possible
  1877. variant type conversions
  1878. Revision 1.19 2002/10/06 21:02:17 peter
  1879. * fixed limit checking for qword
  1880. Revision 1.18 2002/10/06 15:08:59 peter
  1881. * only check for forwarddefs the definitions that really belong to
  1882. the current procsym
  1883. Revision 1.17 2002/10/06 12:25:04 florian
  1884. + proper support of type <id> = type <another id>;
  1885. Revision 1.16 2002/10/05 12:43:24 carl
  1886. * fixes for Delphi 6 compilation
  1887. (warning : Some features do not work under Delphi)
  1888. Revision 1.15 2002/10/05 00:50:01 peter
  1889. * check parameters from left to right in equal_paras, so default
  1890. parameters are checked at the end
  1891. Revision 1.14 2002/09/30 07:00:44 florian
  1892. * fixes to common code to get the alpha compiler compiled applied
  1893. Revision 1.13 2002/09/22 14:02:34 carl
  1894. * stack checking cannot be called before system unit is initialized
  1895. * MC68020 define
  1896. Revision 1.12 2002/09/16 14:11:12 peter
  1897. * add argument to equal_paras() to support default values or not
  1898. Revision 1.11 2002/09/15 17:54:46 peter
  1899. * allow default parameters in equal_paras
  1900. Revision 1.10 2002/09/08 11:10:17 carl
  1901. * bugfix 2109 (bad imho, but only way)
  1902. Revision 1.9 2002/09/07 15:25:02 peter
  1903. * old logs removed and tabs fixed
  1904. Revision 1.8 2002/09/07 09:16:55 carl
  1905. * fix my stupid copy and paste bug
  1906. Revision 1.7 2002/09/06 19:58:31 carl
  1907. * start bugfix 1996
  1908. * 64-bit typed constant now work correctly and fully (bugfix 2001)
  1909. Revision 1.6 2002/08/20 10:31:26 daniel
  1910. * Tcallnode.det_resulttype rewritten
  1911. Revision 1.5 2002/08/12 20:39:17 florian
  1912. * casting of classes to interface fixed when the interface was
  1913. implemented by a parent class
  1914. Revision 1.4 2002/08/12 14:17:56 florian
  1915. * nil is now recognized as being compatible with a dynamic array
  1916. Revision 1.3 2002/08/05 18:27:48 carl
  1917. + more more more documentation
  1918. + first version include/exclude (can't test though, not enough scratch for i386 :()...
  1919. Revision 1.2 2002/07/23 09:51:22 daniel
  1920. * Tried to make Tprocsym.defs protected. I didn't succeed but the cleanups
  1921. are worth comitting.
  1922. Revision 1.1 2002/07/20 11:57:53 florian
  1923. * types.pas renamed to defbase.pas because D6 contains a types
  1924. unit so this would conflicts if D6 programms are compiled
  1925. + Willamette/SSE2 instructions to assembler added
  1926. Revision 1.75 2002/07/11 14:41:32 florian
  1927. * start of the new generic parameter handling
  1928. Revision 1.74 2002/07/01 16:23:54 peter
  1929. * cg64 patch
  1930. * basics for currency
  1931. * asnode updates for class and interface (not finished)
  1932. Revision 1.73 2002/05/18 13:34:21 peter
  1933. * readded missing revisions
  1934. Revision 1.72 2002/05/16 19:46:47 carl
  1935. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1936. + try to fix temp allocation (still in ifdef)
  1937. + generic constructor calls
  1938. + start of tassembler / tmodulebase class cleanup
  1939. Revision 1.70 2002/05/12 16:53:16 peter
  1940. * moved entry and exitcode to ncgutil and cgobj
  1941. * foreach gets extra argument for passing local data to the
  1942. iterator function
  1943. * -CR checks also class typecasts at runtime by changing them
  1944. into as
  1945. * fixed compiler to cycle with the -CR option
  1946. * fixed stabs with elf writer, finally the global variables can
  1947. be watched
  1948. * removed a lot of routines from cga unit and replaced them by
  1949. calls to cgobj
  1950. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  1951. u32bit then the other is typecasted also to u32bit without giving
  1952. a rangecheck warning/error.
  1953. * fixed pascal calling method with reversing also the high tree in
  1954. the parast, detected by tcalcst3 test
  1955. Revision 1.69 2002/04/25 20:16:39 peter
  1956. * moved more routines from cga/n386util
  1957. Revision 1.68 2002/04/15 19:08:22 carl
  1958. + target_info.size_of_pointer -> pointer_size
  1959. + some cleanup of unused types/variables
  1960. Revision 1.67 2002/04/07 13:40:29 carl
  1961. + update documentation
  1962. Revision 1.66 2002/04/02 17:11:32 peter
  1963. * tlocation,treference update
  1964. * LOC_CONSTANT added for better constant handling
  1965. * secondadd splitted in multiple routines
  1966. * location_force_reg added for loading a location to a register
  1967. of a specified size
  1968. * secondassignment parses now first the right and then the left node
  1969. (this is compatible with Kylix). This saves a lot of push/pop especially
  1970. with string operations
  1971. * adapted some routines to use the new cg methods
  1972. Revision 1.65 2002/04/01 20:57:14 jonas
  1973. * fixed web bug 1907
  1974. * fixed some other procvar related bugs (all related to accepting procvar
  1975. constructs with either too many or too little parameters)
  1976. (both merged, includes second typo fix of pexpr.pas)
  1977. Revision 1.64 2002/01/24 18:25:53 peter
  1978. * implicit result variable generation for assembler routines
  1979. * removed m_tp modeswitch, use m_tp7 or not(m_fpc) instead
  1980. Revision 1.63 2002/01/24 12:33:53 jonas
  1981. * adapted ranges of native types to int64 (e.g. high cardinal is no
  1982. longer longint($ffffffff), but just $fffffff in psystem)
  1983. * small additional fix in 64bit rangecheck code generation for 32 bit
  1984. processors
  1985. * adaption of ranges required the matching talgorithm used for selecting
  1986. which overloaded procedure to call to be adapted. It should now always
  1987. select the closest match for ordinal parameters.
  1988. + inttostr(qword) in sysstr.inc/sysstrh.inc
  1989. + abs(int64), sqr(int64), sqr(qword) in systemh.inc/generic.inc (previous
  1990. fixes were required to be able to add them)
  1991. * is_in_limit() moved from ncal to types unit, should always be used
  1992. instead of direct comparisons of low/high values of orddefs because
  1993. qword is a special case
  1994. }