ncnv.pas 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198
  1. {
  2. $Id$
  3. Copyright (c) 2000-2002 by Florian Klaempfl
  4. Type checking and register allocation for type converting nodes
  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 ncnv;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. node,
  23. symtype,symppu,defbase,
  24. nld
  25. {$ifdef Delphi}
  26. ,dmisc
  27. {$endif}
  28. ;
  29. type
  30. ttypeconvnode = class(tunarynode)
  31. totype : ttype;
  32. convtype : tconverttype;
  33. constructor create(node : tnode;const t : ttype);virtual;
  34. constructor create_explicit(node : tnode;const t : ttype);
  35. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  36. procedure ppuwrite(ppufile:tcompilerppufile);override;
  37. procedure derefimpl;override;
  38. function getcopy : tnode;override;
  39. function pass_1 : tnode;override;
  40. function det_resulttype:tnode;override;
  41. {$ifdef var_notification}
  42. procedure mark_write;override;
  43. {$endif}
  44. function docompare(p: tnode) : boolean; override;
  45. private
  46. function resulttype_cord_to_pointer : tnode;
  47. function resulttype_chararray_to_string : tnode;
  48. function resulttype_string_to_chararray : tnode;
  49. function resulttype_string_to_string : tnode;
  50. function resulttype_char_to_string : tnode;
  51. function resulttype_char_to_chararray : tnode;
  52. function resulttype_int_to_real : tnode;
  53. function resulttype_real_to_real : tnode;
  54. function resulttype_cchar_to_pchar : tnode;
  55. function resulttype_cstring_to_pchar : tnode;
  56. function resulttype_char_to_char : tnode;
  57. function resulttype_arrayconstructor_to_set : tnode;
  58. function resulttype_pchar_to_string : tnode;
  59. function resulttype_interface_to_guid : tnode;
  60. function resulttype_dynarray_to_openarray : tnode;
  61. function resulttype_call_helper(c : tconverttype) : tnode;
  62. protected
  63. function first_int_to_int : tnode;virtual;
  64. function first_cstring_to_pchar : tnode;virtual;
  65. function first_string_to_chararray : tnode;virtual;
  66. function first_char_to_string : tnode;virtual;
  67. function first_nothing : tnode;virtual;
  68. function first_array_to_pointer : tnode;virtual;
  69. function first_int_to_real : tnode;virtual;
  70. function first_real_to_real : tnode;virtual;
  71. function first_pointer_to_array : tnode;virtual;
  72. function first_cchar_to_pchar : tnode;virtual;
  73. function first_bool_to_int : tnode;virtual;
  74. function first_int_to_bool : tnode;virtual;
  75. function first_bool_to_bool : tnode;virtual;
  76. function first_proc_to_procvar : tnode;virtual;
  77. function first_load_smallset : tnode;virtual;
  78. function first_cord_to_pointer : tnode;virtual;
  79. function first_ansistring_to_pchar : tnode;virtual;
  80. function first_arrayconstructor_to_set : tnode;virtual;
  81. function first_class_to_intf : tnode;virtual;
  82. function first_char_to_char : tnode;virtual;
  83. function first_call_helper(c : tconverttype) : tnode;
  84. { these wrapper are necessary, because the first_* stuff is called }
  85. { through a table. Without the wrappers override wouldn't have }
  86. { any effect }
  87. function _first_int_to_int : tnode;
  88. function _first_cstring_to_pchar : tnode;
  89. function _first_string_to_chararray : tnode;
  90. function _first_char_to_string : tnode;
  91. function _first_nothing : tnode;
  92. function _first_array_to_pointer : tnode;
  93. function _first_int_to_real : tnode;
  94. function _first_real_to_real : tnode;
  95. function _first_pointer_to_array : tnode;
  96. function _first_cchar_to_pchar : tnode;
  97. function _first_bool_to_int : tnode;
  98. function _first_int_to_bool : tnode;
  99. function _first_bool_to_bool : tnode;
  100. function _first_proc_to_procvar : tnode;
  101. function _first_load_smallset : tnode;
  102. function _first_cord_to_pointer : tnode;
  103. function _first_ansistring_to_pchar : tnode;
  104. function _first_arrayconstructor_to_set : tnode;
  105. function _first_class_to_intf : tnode;
  106. function _first_char_to_char : tnode;
  107. procedure second_int_to_int;virtual;abstract;
  108. procedure second_string_to_string;virtual;abstract;
  109. procedure second_cstring_to_pchar;virtual;abstract;
  110. procedure second_string_to_chararray;virtual;abstract;
  111. procedure second_array_to_pointer;virtual;abstract;
  112. procedure second_pointer_to_array;virtual;abstract;
  113. procedure second_chararray_to_string;virtual;abstract;
  114. procedure second_char_to_string;virtual;abstract;
  115. procedure second_int_to_real;virtual;abstract;
  116. procedure second_real_to_real;virtual;abstract;
  117. procedure second_cord_to_pointer;virtual;abstract;
  118. procedure second_proc_to_procvar;virtual;abstract;
  119. procedure second_bool_to_int;virtual;abstract;
  120. procedure second_int_to_bool;virtual;abstract;
  121. procedure second_bool_to_bool;virtual;abstract;
  122. procedure second_load_smallset;virtual;abstract;
  123. procedure second_ansistring_to_pchar;virtual;abstract;
  124. procedure second_pchar_to_string;virtual;abstract;
  125. procedure second_class_to_intf;virtual;abstract;
  126. procedure second_char_to_char;virtual;abstract;
  127. procedure second_nothing; virtual;abstract;
  128. end;
  129. ttypeconvnodeclass = class of ttypeconvnode;
  130. tasnode = class(tbinarynode)
  131. constructor create(l,r : tnode);virtual;
  132. function pass_1 : tnode;override;
  133. function det_resulttype:tnode;override;
  134. function getcopy: tnode;override;
  135. destructor destroy; override;
  136. protected
  137. call: tnode;
  138. end;
  139. tasnodeclass = class of tasnode;
  140. tisnode = class(tbinarynode)
  141. constructor create(l,r : tnode);virtual;
  142. function pass_1 : tnode;override;
  143. function det_resulttype:tnode;override;
  144. procedure pass_2;override;
  145. end;
  146. tisnodeclass = class of tisnode;
  147. var
  148. ctypeconvnode : ttypeconvnodeclass;
  149. casnode : tasnodeclass;
  150. cisnode : tisnodeclass;
  151. procedure inserttypeconv(var p:tnode;const t:ttype);
  152. procedure inserttypeconv_explicit(var p:tnode;const t:ttype);
  153. procedure arrayconstructor_to_set(var p : tnode);
  154. implementation
  155. uses
  156. globtype,systems,tokens,
  157. cutils,verbose,globals,widestr,
  158. symconst,symdef,symsym,symtable,
  159. ncon,ncal,nset,nadd,ninl,nmem,nmat,
  160. cgbase,
  161. htypechk,pass_1,cpubase,cpuinfo;
  162. {*****************************************************************************
  163. Helpers
  164. *****************************************************************************}
  165. procedure inserttypeconv(var p:tnode;const t:ttype);
  166. begin
  167. if not assigned(p.resulttype.def) then
  168. begin
  169. resulttypepass(p);
  170. if codegenerror then
  171. exit;
  172. end;
  173. { don't insert obsolete type conversions }
  174. if is_equal(p.resulttype.def,t.def) and
  175. not ((p.resulttype.def.deftype=setdef) and
  176. (tsetdef(p.resulttype.def).settype <>
  177. tsetdef(t.def).settype)) then
  178. begin
  179. p.resulttype:=t;
  180. end
  181. else
  182. begin
  183. p:=ctypeconvnode.create(p,t);
  184. resulttypepass(p);
  185. end;
  186. end;
  187. procedure inserttypeconv_explicit(var p:tnode;const t:ttype);
  188. begin
  189. if not assigned(p.resulttype.def) then
  190. begin
  191. resulttypepass(p);
  192. if codegenerror then
  193. exit;
  194. end;
  195. { don't insert obsolete type conversions }
  196. if is_equal(p.resulttype.def,t.def) and
  197. not ((p.resulttype.def.deftype=setdef) and
  198. (tsetdef(p.resulttype.def).settype <>
  199. tsetdef(t.def).settype)) then
  200. begin
  201. p.resulttype:=t;
  202. end
  203. else
  204. begin
  205. p:=ctypeconvnode.create_explicit(p,t);
  206. resulttypepass(p);
  207. end;
  208. end;
  209. {*****************************************************************************
  210. Array constructor to Set Conversion
  211. *****************************************************************************}
  212. procedure arrayconstructor_to_set(var p : tnode);
  213. var
  214. constp : tsetconstnode;
  215. buildp,
  216. p2,p3,p4 : tnode;
  217. htype : ttype;
  218. constset : Pconstset;
  219. constsetlo,
  220. constsethi : longint;
  221. procedure update_constsethi(t:ttype);
  222. begin
  223. if ((t.def.deftype=orddef) and
  224. (torddef(t.def).high>=constsethi)) then
  225. begin
  226. constsethi:=torddef(t.def).high;
  227. if htype.def=nil then
  228. begin
  229. if (constsethi>255) or
  230. (torddef(t.def).low<0) then
  231. htype:=u8bittype
  232. else
  233. htype:=t;
  234. end;
  235. if constsethi>255 then
  236. constsethi:=255;
  237. end
  238. else if ((t.def.deftype=enumdef) and
  239. (tenumdef(t.def).max>=constsethi)) then
  240. begin
  241. if htype.def=nil then
  242. htype:=t;
  243. constsethi:=tenumdef(t.def).max;
  244. end;
  245. end;
  246. procedure do_set(pos : longint);
  247. {$ifdef oldset}
  248. var
  249. mask,l : longint;
  250. {$endif}
  251. begin
  252. if (pos and not $ff)<>0 then
  253. Message(parser_e_illegal_set_expr);
  254. if pos>constsethi then
  255. constsethi:=pos;
  256. if pos<constsetlo then
  257. constsetlo:=pos;
  258. {$ifdef oldset}
  259. { to do this correctly we use the 32bit array }
  260. l:=pos shr 5;
  261. mask:=1 shl (pos mod 32);
  262. { do we allow the same twice }
  263. if (pconst32bitset(constset)^[l] and mask)<>0 then
  264. Message(parser_e_illegal_set_expr);
  265. pconst32bitset(constset)^[l]:=pconst32bitset(constset)^[l] or mask;
  266. {$else}
  267. include(constset^,pos);
  268. {$endif}
  269. end;
  270. var
  271. l : Longint;
  272. lr,hr : TConstExprInt;
  273. hp : tarrayconstructornode;
  274. begin
  275. if p.nodetype<>arrayconstructorn then
  276. internalerror(200205105);
  277. new(constset);
  278. {$ifdef oldset}
  279. FillChar(constset^,sizeof(constset^),0);
  280. {$else}
  281. constset^:=[];
  282. {$endif}
  283. htype.reset;
  284. constsetlo:=0;
  285. constsethi:=0;
  286. constp:=csetconstnode.create(nil,htype);
  287. constp.value_set:=constset;
  288. buildp:=constp;
  289. hp:=tarrayconstructornode(p);
  290. if assigned(hp.left) then
  291. begin
  292. while assigned(hp) do
  293. begin
  294. p4:=nil; { will contain the tree to create the set }
  295. {split a range into p2 and p3 }
  296. if hp.left.nodetype=arrayconstructorrangen then
  297. begin
  298. p2:=tarrayconstructorrangenode(hp.left).left;
  299. p3:=tarrayconstructorrangenode(hp.left).right;
  300. tarrayconstructorrangenode(hp.left).left:=nil;
  301. tarrayconstructorrangenode(hp.left).right:=nil;
  302. end
  303. else
  304. begin
  305. p2:=hp.left;
  306. hp.left:=nil;
  307. p3:=nil;
  308. end;
  309. resulttypepass(p2);
  310. if assigned(p3) then
  311. resulttypepass(p3);
  312. if codegenerror then
  313. break;
  314. case p2.resulttype.def.deftype of
  315. enumdef,
  316. orddef:
  317. begin
  318. getrange(p2.resulttype.def,lr,hr);
  319. if assigned(p3) then
  320. begin
  321. { this isn't good, you'll get problems with
  322. type t010 = 0..10;
  323. ts = set of t010;
  324. var s : ts;b : t010
  325. begin s:=[1,2,b]; end.
  326. if is_integer(p3^.resulttype.def) then
  327. begin
  328. inserttypeconv(p3,u8bitdef);
  329. end;
  330. }
  331. if assigned(htype.def) and not(is_equal(htype.def,p3.resulttype.def)) then
  332. begin
  333. aktfilepos:=p3.fileinfo;
  334. CGMessage(type_e_typeconflict_in_set);
  335. end
  336. else
  337. begin
  338. if (p2.nodetype=ordconstn) and (p3.nodetype=ordconstn) then
  339. begin
  340. if not(is_integer(p3.resulttype.def)) then
  341. htype:=p3.resulttype
  342. else
  343. begin
  344. inserttypeconv(p3,u8bittype);
  345. inserttypeconv(p2,u8bittype);
  346. end;
  347. for l:=tordconstnode(p2).value to tordconstnode(p3).value do
  348. do_set(l);
  349. p2.free;
  350. p3.free;
  351. end
  352. else
  353. begin
  354. update_constsethi(p2.resulttype);
  355. inserttypeconv(p2,htype);
  356. update_constsethi(p3.resulttype);
  357. inserttypeconv(p3,htype);
  358. if assigned(htype.def) then
  359. inserttypeconv(p3,htype)
  360. else
  361. inserttypeconv(p3,u8bittype);
  362. p4:=csetelementnode.create(p2,p3);
  363. end;
  364. end;
  365. end
  366. else
  367. begin
  368. { Single value }
  369. if p2.nodetype=ordconstn then
  370. begin
  371. if not(is_integer(p2.resulttype.def)) then
  372. update_constsethi(p2.resulttype)
  373. else
  374. inserttypeconv(p2,u8bittype);
  375. do_set(tordconstnode(p2).value);
  376. p2.free;
  377. end
  378. else
  379. begin
  380. update_constsethi(p2.resulttype);
  381. if assigned(htype.def) then
  382. inserttypeconv(p2,htype)
  383. else
  384. inserttypeconv(p2,u8bittype);
  385. p4:=csetelementnode.create(p2,nil);
  386. end;
  387. end;
  388. end;
  389. stringdef :
  390. begin
  391. { if we've already set elements which are constants }
  392. { throw an error }
  393. if ((htype.def=nil) and assigned(buildp)) or
  394. not(is_char(htype.def)) then
  395. CGMessage(type_e_typeconflict_in_set)
  396. else
  397. for l:=1 to length(pstring(tstringconstnode(p2).value_str)^) do
  398. do_set(ord(pstring(tstringconstnode(p2).value_str)^[l]));
  399. if htype.def=nil then
  400. htype:=cchartype;
  401. p2.free;
  402. end;
  403. else
  404. CGMessage(type_e_ordinal_expr_expected);
  405. end;
  406. { insert the set creation tree }
  407. if assigned(p4) then
  408. buildp:=caddnode.create(addn,buildp,p4);
  409. { load next and dispose current node }
  410. p2:=hp;
  411. hp:=tarrayconstructornode(tarrayconstructornode(p2).right);
  412. tarrayconstructornode(p2).right:=nil;
  413. p2.free;
  414. end;
  415. if (htype.def=nil) then
  416. htype:=u8bittype;
  417. end
  418. else
  419. begin
  420. { empty set [], only remove node }
  421. p.free;
  422. end;
  423. { set the initial set type }
  424. constp.resulttype.setdef(tsetdef.create(htype,constsethi));
  425. { determine the resulttype for the tree }
  426. resulttypepass(buildp);
  427. { set the new tree }
  428. p:=buildp;
  429. end;
  430. {*****************************************************************************
  431. TTYPECONVNODE
  432. *****************************************************************************}
  433. constructor ttypeconvnode.create(node : tnode;const t:ttype);
  434. begin
  435. inherited create(typeconvn,node);
  436. convtype:=tc_not_possible;
  437. totype:=t;
  438. if t.def=nil then
  439. internalerror(200103281);
  440. set_file_line(node);
  441. end;
  442. constructor ttypeconvnode.create_explicit(node : tnode;const t:ttype);
  443. begin
  444. self.create(node,t);
  445. toggleflag(nf_explizit);
  446. end;
  447. constructor ttypeconvnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  448. begin
  449. inherited ppuload(t,ppufile);
  450. ppufile.gettype(totype);
  451. convtype:=tconverttype(ppufile.getbyte);
  452. end;
  453. procedure ttypeconvnode.ppuwrite(ppufile:tcompilerppufile);
  454. begin
  455. inherited ppuwrite(ppufile);
  456. ppufile.puttype(totype);
  457. ppufile.putbyte(byte(convtype));
  458. end;
  459. procedure ttypeconvnode.derefimpl;
  460. begin
  461. inherited derefimpl;
  462. totype.resolve;
  463. end;
  464. function ttypeconvnode.getcopy : tnode;
  465. var
  466. n : ttypeconvnode;
  467. begin
  468. n:=ttypeconvnode(inherited getcopy);
  469. n.convtype:=convtype;
  470. getcopy:=n;
  471. end;
  472. function ttypeconvnode.resulttype_cord_to_pointer : tnode;
  473. var
  474. t : tnode;
  475. begin
  476. result:=nil;
  477. if left.nodetype=ordconstn then
  478. begin
  479. { check if we have a valid pointer constant (JM) }
  480. if (sizeof(pointer) > sizeof(TConstPtrUInt)) then
  481. if (sizeof(TConstPtrUInt) = 4) then
  482. begin
  483. if (tordconstnode(left).value < low(longint)) or
  484. (tordconstnode(left).value > high(cardinal)) then
  485. CGMessage(parser_e_range_check_error);
  486. end
  487. else if (sizeof(TConstPtrUInt) = 8) then
  488. begin
  489. if (tordconstnode(left).value < low(int64)) or
  490. (tordconstnode(left).value > high(qword)) then
  491. CGMessage(parser_e_range_check_error);
  492. end
  493. else
  494. internalerror(2001020801);
  495. t:=cpointerconstnode.create(TConstPtrUInt(tordconstnode(left).value),resulttype);
  496. result:=t;
  497. end
  498. else
  499. internalerror(200104023);
  500. end;
  501. function ttypeconvnode.resulttype_chararray_to_string : tnode;
  502. begin
  503. result := ccallnode.createinternres(
  504. 'fpc_chararray_to_'+tstringdef(resulttype.def).stringtypname,
  505. ccallparanode.create(left,nil),resulttype);
  506. left := nil;
  507. end;
  508. function ttypeconvnode.resulttype_string_to_chararray : tnode;
  509. var
  510. arrsize: longint;
  511. begin
  512. with tarraydef(resulttype.def) do
  513. begin
  514. if highrange<lowrange then
  515. internalerror(75432653);
  516. arrsize := highrange-lowrange+1;
  517. end;
  518. if (left.nodetype = stringconstn) and
  519. { left.length+1 since there's always a terminating #0 character (JM) }
  520. (tstringconstnode(left).len+1 >= arrsize) and
  521. (tstringdef(left.resulttype.def).string_typ=st_shortstring) then
  522. begin
  523. { handled separately }
  524. result := nil;
  525. exit;
  526. end;
  527. result := ccallnode.createinternres(
  528. 'fpc_'+tstringdef(left.resulttype.def).stringtypname+
  529. '_to_chararray',ccallparanode.create(left,ccallparanode.create(
  530. cordconstnode.create(arrsize,s32bittype,true),nil)),resulttype);
  531. left := nil;
  532. end;
  533. function ttypeconvnode.resulttype_string_to_string : tnode;
  534. var
  535. procname: string[31];
  536. stringpara : tcallparanode;
  537. pw : pcompilerwidestring;
  538. pc : pchar;
  539. begin
  540. result:=nil;
  541. if left.nodetype=stringconstn then
  542. begin
  543. { convert ascii 2 unicode }
  544. if (tstringdef(resulttype.def).string_typ=st_widestring) and
  545. (tstringconstnode(left).st_type in [st_ansistring,st_shortstring,st_longstring]) then
  546. begin
  547. initwidestring(pw);
  548. ascii2unicode(tstringconstnode(left).value_str,tstringconstnode(left).len,pw);
  549. ansistringdispose(tstringconstnode(left).value_str,tstringconstnode(left).len);
  550. pcompilerwidestring(tstringconstnode(left).value_str):=pw;
  551. end
  552. else
  553. { convert unicode 2 ascii }
  554. if (tstringconstnode(left).st_type=st_widestring) and
  555. (tstringdef(resulttype.def).string_typ in [st_ansistring,st_shortstring,st_longstring]) then
  556. begin
  557. pw:=pcompilerwidestring(tstringconstnode(left).value_str);
  558. getmem(pc,getlengthwidestring(pw)+1);
  559. unicode2ascii(pw,pc);
  560. donewidestring(pw);
  561. tstringconstnode(left).value_str:=pc;
  562. end;
  563. tstringconstnode(left).st_type:=tstringdef(resulttype.def).string_typ;
  564. tstringconstnode(left).resulttype:=resulttype;
  565. result:=left;
  566. left:=nil;
  567. end
  568. else
  569. begin
  570. { get the correct procedure name }
  571. procname := 'fpc_'+tstringdef(left.resulttype.def).stringtypname+
  572. '_to_'+tstringdef(resulttype.def).stringtypname;
  573. { create parameter (and remove left node from typeconvnode }
  574. { since it's reused as parameter) }
  575. stringpara := ccallparanode.create(left,nil);
  576. left := nil;
  577. { when converting to shortstrings, we have to pass high(destination) too }
  578. if (tstringdef(resulttype.def).string_typ = st_shortstring) then
  579. stringpara.right := ccallparanode.create(cinlinenode.create(
  580. in_high_x,false,self.getcopy),nil);
  581. { and create the callnode }
  582. result := ccallnode.createinternres(procname,stringpara,resulttype);
  583. end;
  584. end;
  585. function ttypeconvnode.resulttype_char_to_string : tnode;
  586. var
  587. procname: string[31];
  588. para : tcallparanode;
  589. hp : tstringconstnode;
  590. ws : pcompilerwidestring;
  591. begin
  592. result:=nil;
  593. if left.nodetype=ordconstn then
  594. begin
  595. if tstringdef(resulttype.def).string_typ=st_widestring then
  596. begin
  597. initwidestring(ws);
  598. concatwidestringchar(ws,tcompilerwidechar(chr(tordconstnode(left).value)));
  599. hp:=cstringconstnode.createwstr(ws);
  600. donewidestring(ws);
  601. end
  602. else
  603. hp:=cstringconstnode.createstr(chr(tordconstnode(left).value),tstringdef(resulttype.def).string_typ);
  604. result:=hp;
  605. end
  606. else
  607. { shortstrings are handled 'inline' }
  608. if tstringdef(resulttype.def).string_typ <> st_shortstring then
  609. begin
  610. { create the parameter }
  611. para := ccallparanode.create(left,nil);
  612. left := nil;
  613. { and the procname }
  614. procname := 'fpc_char_to_' +tstringdef(resulttype.def).stringtypname;
  615. { and finally the call }
  616. result := ccallnode.createinternres(procname,para,resulttype);
  617. end
  618. else
  619. begin
  620. { create word(byte(char) shl 8 or 1) for litte endian machines }
  621. { and word(byte(char) or 256) for big endian machines }
  622. left := ctypeconvnode.create(left,u8bittype);
  623. left.toggleflag(nf_explizit);
  624. if (target_info.endian = endian_little) then
  625. left := caddnode.create(orn,
  626. cshlshrnode.create(shln,left,cordconstnode.create(8,s32bittype,false)),
  627. cordconstnode.create(1,s32bittype,false))
  628. else
  629. left := caddnode.create(orn,left,
  630. cordconstnode.create(1 shl 8,s32bittype,false));
  631. left := ctypeconvnode.create(left,u16bittype);
  632. left.toggleflag(nf_explizit);
  633. resulttypepass(left);
  634. end;
  635. end;
  636. function ttypeconvnode.resulttype_char_to_chararray : tnode;
  637. begin
  638. if resulttype.def.size <> 1 then
  639. begin
  640. { convert first to string, then to chararray }
  641. inserttypeconv(left,cshortstringtype);
  642. inserttypeconv(left,resulttype);
  643. result:=left;
  644. left := nil;
  645. exit;
  646. end;
  647. result := nil;
  648. end;
  649. function ttypeconvnode.resulttype_char_to_char : tnode;
  650. var
  651. hp : tordconstnode;
  652. begin
  653. result:=nil;
  654. if left.nodetype=ordconstn then
  655. begin
  656. if (torddef(resulttype.def).typ=uchar) and
  657. (torddef(left.resulttype.def).typ=uwidechar) then
  658. begin
  659. hp:=cordconstnode.create(
  660. ord(unicode2asciichar(tcompilerwidechar(tordconstnode(left).value))),
  661. cchartype,true);
  662. result:=hp;
  663. end
  664. else if (torddef(resulttype.def).typ=uwidechar) and
  665. (torddef(left.resulttype.def).typ=uchar) then
  666. begin
  667. hp:=cordconstnode.create(
  668. asciichar2unicode(chr(tordconstnode(left).value)),
  669. cwidechartype,true);
  670. result:=hp;
  671. end
  672. else
  673. internalerror(200105131);
  674. exit;
  675. end;
  676. end;
  677. function ttypeconvnode.resulttype_int_to_real : tnode;
  678. var
  679. t : trealconstnode;
  680. begin
  681. result:=nil;
  682. if left.nodetype=ordconstn then
  683. begin
  684. t:=crealconstnode.create(tordconstnode(left).value,resulttype);
  685. result:=t;
  686. end;
  687. end;
  688. function ttypeconvnode.resulttype_real_to_real : tnode;
  689. var
  690. t : tnode;
  691. begin
  692. result:=nil;
  693. if is_currency(left.resulttype.def) and not(is_currency(resulttype.def)) then
  694. begin
  695. end
  696. else
  697. if is_currency(resulttype.def) then
  698. begin
  699. end;
  700. if left.nodetype=realconstn then
  701. begin
  702. t:=crealconstnode.create(trealconstnode(left).value_real,resulttype);
  703. result:=t;
  704. end;
  705. end;
  706. function ttypeconvnode.resulttype_cchar_to_pchar : tnode;
  707. begin
  708. result:=nil;
  709. if is_pwidechar(resulttype.def) then
  710. inserttypeconv(left,cwidestringtype)
  711. else
  712. inserttypeconv(left,cshortstringtype);
  713. { evaluate again, reset resulttype so the convert_typ
  714. will be calculated again and cstring_to_pchar will
  715. be used for futher conversion }
  716. result:=det_resulttype;
  717. end;
  718. function ttypeconvnode.resulttype_cstring_to_pchar : tnode;
  719. begin
  720. result:=nil;
  721. if is_pwidechar(resulttype.def) then
  722. inserttypeconv(left,cwidestringtype);
  723. end;
  724. function ttypeconvnode.resulttype_arrayconstructor_to_set : tnode;
  725. var
  726. hp : tnode;
  727. begin
  728. result:=nil;
  729. if left.nodetype<>arrayconstructorn then
  730. internalerror(5546);
  731. { remove typeconv node }
  732. hp:=left;
  733. left:=nil;
  734. { create a set constructor tree }
  735. arrayconstructor_to_set(hp);
  736. result:=hp;
  737. end;
  738. function ttypeconvnode.resulttype_pchar_to_string : tnode;
  739. begin
  740. result := ccallnode.createinternres(
  741. 'fpc_pchar_to_'+tstringdef(resulttype.def).stringtypname,
  742. ccallparanode.create(left,nil),resulttype);
  743. left := nil;
  744. end;
  745. function ttypeconvnode.resulttype_interface_to_guid : tnode;
  746. begin
  747. if tobjectdef(left.resulttype.def).isiidguidvalid then
  748. result:=cguidconstnode.create(tobjectdef(left.resulttype.def).iidguid);
  749. end;
  750. function ttypeconvnode.resulttype_dynarray_to_openarray : tnode;
  751. begin
  752. { a dynamic array is a pointer to an array, so to convert it to }
  753. { an open array, we have to dereference it (JM) }
  754. result := ctypeconvnode.create(left,voidpointertype);
  755. { left is reused }
  756. left := nil;
  757. result.toggleflag(nf_explizit);
  758. result := cderefnode.create(result);
  759. result.resulttype := resulttype;
  760. end;
  761. function ttypeconvnode.resulttype_call_helper(c : tconverttype) : tnode;
  762. {$ifdef fpc}
  763. const
  764. resulttypeconvert : array[tconverttype] of pointer = (
  765. {equal} nil,
  766. {not_possible} nil,
  767. { string_2_string } @ttypeconvnode.resulttype_string_to_string,
  768. { char_2_string } @ttypeconvnode.resulttype_char_to_string,
  769. { char_2_chararray } @ttypeconvnode.resulttype_char_to_chararray,
  770. { pchar_2_string } @ttypeconvnode.resulttype_pchar_to_string,
  771. { cchar_2_pchar } @ttypeconvnode.resulttype_cchar_to_pchar,
  772. { cstring_2_pchar } @ttypeconvnode.resulttype_cstring_to_pchar,
  773. { ansistring_2_pchar } nil,
  774. { string_2_chararray } @ttypeconvnode.resulttype_string_to_chararray,
  775. { chararray_2_string } @ttypeconvnode.resulttype_chararray_to_string,
  776. { array_2_pointer } nil,
  777. { pointer_2_array } nil,
  778. { int_2_int } nil,
  779. { int_2_bool } nil,
  780. { bool_2_bool } nil,
  781. { bool_2_int } nil,
  782. { real_2_real } @ttypeconvnode.resulttype_real_to_real,
  783. { int_2_real } @ttypeconvnode.resulttype_int_to_real,
  784. { proc_2_procvar } nil,
  785. { arrayconstructor_2_set } @ttypeconvnode.resulttype_arrayconstructor_to_set,
  786. { load_smallset } nil,
  787. { cord_2_pointer } @ttypeconvnode.resulttype_cord_to_pointer,
  788. { intf_2_string } nil,
  789. { intf_2_guid } @ttypeconvnode.resulttype_interface_to_guid,
  790. { class_2_intf } nil,
  791. { char_2_char } @ttypeconvnode.resulttype_char_to_char,
  792. { normal_2_smallset} nil,
  793. { dynarray_2_openarray} @resulttype_dynarray_to_openarray
  794. );
  795. type
  796. tprocedureofobject = function : tnode of object;
  797. var
  798. r : packed record
  799. proc : pointer;
  800. obj : pointer;
  801. end;
  802. begin
  803. result:=nil;
  804. { this is a little bit dirty but it works }
  805. { and should be quite portable too }
  806. r.proc:=resulttypeconvert[c];
  807. r.obj:=self;
  808. if assigned(r.proc) then
  809. result:=tprocedureofobject(r){$ifdef FPC}();{$endif FPC}
  810. end;
  811. {$else}
  812. begin
  813. case c of
  814. tc_string_2_string: resulttype_string_to_string;
  815. tc_char_2_string : resulttype_char_to_string;
  816. tc_char_2_chararray: resulttype_char_to_chararray;
  817. tc_pchar_2_string : resulttype_pchar_to_string;
  818. tc_cchar_2_pchar : resulttype_cchar_to_pchar;
  819. tc_cstring_2_pchar : resulttype_cstring_to_pchar;
  820. tc_string_2_chararray : resulttype_string_to_chararray;
  821. tc_chararray_2_string : resulttype_chararray_to_string;
  822. tc_real_2_real : resulttype_real_to_real;
  823. tc_int_2_real : resulttype_int_to_real;
  824. tc_arrayconstructor_2_set : resulttype_arrayconstructor_to_set;
  825. tc_cord_2_pointer : resulttype_cord_to_pointer;
  826. tc_intf_2_guid : resulttype_interface_to_guid;
  827. tc_char_2_char : resulttype_char_to_char;
  828. tc_dynarray_2_openarray : resulttype_dynarray_to_openarray;
  829. end;
  830. end;
  831. {$Endif fpc}
  832. function ttypeconvnode.det_resulttype:tnode;
  833. var
  834. hp : tnode;
  835. currprocdef,
  836. aprocdef : tprocdef;
  837. begin
  838. result:=nil;
  839. resulttype:=totype;
  840. resulttypepass(left);
  841. if codegenerror then
  842. exit;
  843. { remove obsolete type conversions }
  844. if is_equal(left.resulttype.def,resulttype.def) then
  845. begin
  846. { because is_equal only checks the basetype for sets we need to
  847. check here if we are loading a smallset into a normalset }
  848. if (resulttype.def.deftype=setdef) and
  849. (left.resulttype.def.deftype=setdef) and
  850. ((tsetdef(resulttype.def).settype = smallset) xor
  851. (tsetdef(left.resulttype.def).settype = smallset)) then
  852. begin
  853. { constant sets can be converted by changing the type only }
  854. if (left.nodetype=setconstn) then
  855. begin
  856. tsetdef(left.resulttype.def).changesettype(tsetdef(resulttype.def).settype);
  857. result:=left;
  858. left:=nil;
  859. exit;
  860. end;
  861. if (tsetdef(resulttype.def).settype <> smallset) then
  862. convtype:=tc_load_smallset
  863. else
  864. convtype := tc_normal_2_smallset;
  865. exit;
  866. end
  867. else
  868. begin
  869. left.resulttype:=resulttype;
  870. result:=left;
  871. left:=nil;
  872. exit;
  873. end;
  874. end;
  875. aprocdef:=assignment_overloaded(left.resulttype.def,resulttype.def);
  876. if assigned(aprocdef) then
  877. begin
  878. procinfo.flags:=procinfo.flags or pi_do_call;
  879. hp:=ccallnode.create(ccallparanode.create(left,nil),
  880. overloaded_operators[_assignment],nil,nil);
  881. { tell explicitly which def we must use !! (PM) }
  882. tcallnode(hp).procdefinition:=aprocdef;
  883. left:=nil;
  884. result:=hp;
  885. exit;
  886. end;
  887. if isconvertable(left.resulttype.def,resulttype.def,convtype,left.nodetype,nf_explizit in flags)=0 then
  888. begin
  889. {Procedures have a resulttype.def of voiddef and functions of their
  890. own resulttype.def. They will therefore always be incompatible with
  891. a procvar. Because isconvertable cannot check for procedures we
  892. use an extra check for them.}
  893. if (m_tp_procvar in aktmodeswitches) then
  894. begin
  895. if (resulttype.def.deftype=procvardef) and
  896. (is_procsym_load(left) or is_procsym_call(left)) then
  897. begin
  898. if is_procsym_call(left) then
  899. begin
  900. currprocdef:=Tprocsym(Tcallnode(left).symtableprocentry).search_procdef_byprocvardef(Tprocvardef(resulttype.def));
  901. hp:=cloadnode.create_procvar(tprocsym(tcallnode(left).symtableprocentry),
  902. currprocdef,tcallnode(left).symtableproc);
  903. if (tcallnode(left).symtableprocentry.owner.symtabletype=objectsymtable) and
  904. assigned(tcallnode(left).methodpointer) then
  905. tloadnode(hp).set_mp(tcallnode(left).methodpointer.getcopy);
  906. resulttypepass(hp);
  907. left.free;
  908. left:=hp;
  909. aprocdef:=tprocdef(left.resulttype.def);
  910. end
  911. else
  912. begin
  913. if (left.nodetype<>addrn) then
  914. aprocdef:=tprocsym(tloadnode(left).symtableentry).first_procdef;
  915. end;
  916. convtype:=tc_proc_2_procvar;
  917. { Now check if the procedure we are going to assign to
  918. the procvar, is compatible with the procvar's type }
  919. if assigned(aprocdef) then
  920. begin
  921. if not proc_to_procvar_equal(aprocdef,tprocvardef(resulttype.def),false) then
  922. CGMessage2(type_e_incompatible_types,aprocdef.typename,resulttype.def.typename);
  923. end
  924. else
  925. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  926. exit;
  927. end;
  928. end;
  929. if nf_explizit in flags then
  930. begin
  931. { check if the result could be in a register }
  932. if not(tstoreddef(resulttype.def).is_intregable) and
  933. not(tstoreddef(resulttype.def).is_fpuregable) then
  934. make_not_regable(left);
  935. { boolean to byte are special because the
  936. location can be different }
  937. if is_integer(resulttype.def) and
  938. is_boolean(left.resulttype.def) then
  939. begin
  940. convtype:=tc_bool_2_int;
  941. exit;
  942. end;
  943. if is_char(resulttype.def) and
  944. is_boolean(left.resulttype.def) then
  945. begin
  946. convtype:=tc_bool_2_int;
  947. exit;
  948. end;
  949. { ansistring to pchar }
  950. if is_pchar(resulttype.def) and
  951. is_ansistring(left.resulttype.def) then
  952. begin
  953. convtype:=tc_ansistring_2_pchar;
  954. exit;
  955. end;
  956. { do common tc_equal cast }
  957. convtype:=tc_equal;
  958. { enum to ordinal will always be s32bit }
  959. if (left.resulttype.def.deftype=enumdef) and
  960. is_ordinal(resulttype.def) then
  961. begin
  962. if left.nodetype=ordconstn then
  963. begin
  964. hp:=cordconstnode.create(tordconstnode(left).value,
  965. resulttype,true);
  966. result:=hp;
  967. exit;
  968. end
  969. else
  970. begin
  971. if isconvertable(s32bittype.def,resulttype.def,convtype,ordconstn,false)=0 then
  972. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  973. end;
  974. end
  975. { ordinal to enumeration }
  976. else
  977. if (resulttype.def.deftype=enumdef) and
  978. is_ordinal(left.resulttype.def) then
  979. begin
  980. if left.nodetype=ordconstn then
  981. begin
  982. hp:=cordconstnode.create(tordconstnode(left).value,
  983. resulttype,true);
  984. result:=hp;
  985. exit;
  986. end
  987. else
  988. begin
  989. if IsConvertable(left.resulttype.def,s32bittype.def,convtype,ordconstn,false)=0 then
  990. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  991. end;
  992. end
  993. { nil to ordinal node }
  994. else if (left.nodetype=niln) and is_ordinal(resulttype.def) then
  995. begin
  996. hp:=cordconstnode.create(0,resulttype,true);
  997. result:=hp;
  998. exit;
  999. end
  1000. { constant pointer to ordinal }
  1001. else if is_ordinal(resulttype.def) and
  1002. (left.nodetype=pointerconstn) then
  1003. begin
  1004. hp:=cordconstnode.create(tpointerconstnode(left).value,
  1005. resulttype,true);
  1006. result:=hp;
  1007. exit;
  1008. end
  1009. { class to class or object to object, with checkobject support }
  1010. else if (resulttype.def.deftype=objectdef) and
  1011. (left.resulttype.def.deftype=objectdef) then
  1012. begin
  1013. if (cs_check_object in aktlocalswitches) then
  1014. begin
  1015. if is_class_or_interface(resulttype.def) then
  1016. begin
  1017. { we can translate the typeconvnode to 'as' when
  1018. typecasting to a class or interface }
  1019. hp:=casnode.create(left,cloadvmtnode.create(ctypenode.create(resulttype)));
  1020. left:=nil;
  1021. result:=hp;
  1022. exit;
  1023. end;
  1024. end
  1025. else
  1026. begin
  1027. { check if the types are related }
  1028. if (not(tobjectdef(left.resulttype.def).is_related(tobjectdef(resulttype.def)))) and
  1029. (not(tobjectdef(resulttype.def).is_related(tobjectdef(left.resulttype.def)))) then
  1030. CGMessage2(type_w_classes_not_related,left.resulttype.def.typename,resulttype.def.typename);
  1031. end;
  1032. end
  1033. {Are we typecasting an ordconst to a char?}
  1034. else
  1035. if is_char(resulttype.def) and
  1036. is_ordinal(left.resulttype.def) then
  1037. begin
  1038. if left.nodetype=ordconstn then
  1039. begin
  1040. hp:=cordconstnode.create(tordconstnode(left).value,
  1041. resulttype,true);
  1042. result:=hp;
  1043. exit;
  1044. end
  1045. else
  1046. begin
  1047. if IsConvertable(left.resulttype.def,u8bittype.def,convtype,ordconstn,false)=0 then
  1048. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  1049. end;
  1050. end
  1051. {Are we typecasting an ordconst to a wchar?}
  1052. else
  1053. if is_widechar(resulttype.def) and
  1054. is_ordinal(left.resulttype.def) then
  1055. begin
  1056. if left.nodetype=ordconstn then
  1057. begin
  1058. hp:=cordconstnode.create(tordconstnode(left).value,
  1059. resulttype,true);
  1060. result:=hp;
  1061. exit;
  1062. end
  1063. else
  1064. begin
  1065. if IsConvertable(left.resulttype.def,u16bittype.def,convtype,ordconstn,false)=0 then
  1066. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  1067. end;
  1068. end
  1069. { char to ordinal }
  1070. else
  1071. if is_char(left.resulttype.def) and
  1072. is_ordinal(resulttype.def) then
  1073. begin
  1074. if left.nodetype=ordconstn then
  1075. begin
  1076. hp:=cordconstnode.create(tordconstnode(left).value,
  1077. resulttype,true);
  1078. result:=hp;
  1079. exit;
  1080. end
  1081. else
  1082. begin
  1083. if IsConvertable(u8bittype.def,resulttype.def,convtype,ordconstn,false)=0 then
  1084. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  1085. end;
  1086. end
  1087. { widechar to ordinal }
  1088. else
  1089. if is_widechar(left.resulttype.def) and
  1090. is_ordinal(resulttype.def) then
  1091. begin
  1092. if left.nodetype=ordconstn then
  1093. begin
  1094. hp:=cordconstnode.create(tordconstnode(left).value,
  1095. resulttype,true);
  1096. result:=hp;
  1097. exit;
  1098. end
  1099. else
  1100. begin
  1101. if IsConvertable(u16bittype.def,resulttype.def,convtype,ordconstn,false)=0 then
  1102. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  1103. end;
  1104. end
  1105. { ordinal to pointer }
  1106. else
  1107. if (m_delphi in aktmodeswitches) and
  1108. is_ordinal(left.resulttype.def) and
  1109. (resulttype.def.deftype=pointerdef) then
  1110. begin
  1111. if left.nodetype=pointerconstn then
  1112. begin
  1113. hp:=cordconstnode.create(tpointerconstnode(left).value,
  1114. resulttype,true);
  1115. result:=hp;
  1116. exit;
  1117. end
  1118. else
  1119. begin
  1120. if IsConvertable(left.resulttype.def,ordpointertype.def,convtype,ordconstn,false)=0 then
  1121. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  1122. end;
  1123. end
  1124. { only if the same size or formal def }
  1125. { why do we allow typecasting of voiddef ?? (PM) }
  1126. else
  1127. begin
  1128. if not(
  1129. (left.resulttype.def.deftype=formaldef) or
  1130. (not(is_open_array(left.resulttype.def)) and
  1131. (left.resulttype.def.size=resulttype.def.size)) or
  1132. (is_void(left.resulttype.def) and
  1133. (left.nodetype=derefn))
  1134. ) then
  1135. CGMessage(cg_e_illegal_type_conversion);
  1136. if ((left.resulttype.def.deftype=orddef) and
  1137. (resulttype.def.deftype=pointerdef)) or
  1138. ((resulttype.def.deftype=orddef) and
  1139. (left.resulttype.def.deftype=pointerdef)) then
  1140. CGMessage(cg_d_pointer_to_longint_conv_not_portable);
  1141. end;
  1142. { the conversion into a strutured type is only }
  1143. { possible, if the source is not a register }
  1144. if ((resulttype.def.deftype in [recorddef,stringdef,arraydef]) or
  1145. ((resulttype.def.deftype=objectdef) and not(is_class(resulttype.def)))
  1146. ) and (left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) { and
  1147. it also works if the assignment is overloaded
  1148. YES but this code is not executed if assignment is overloaded (PM)
  1149. not assigned(assignment_overloaded(left.resulttype.def,resulttype.def))} then
  1150. CGMessage(cg_e_illegal_type_conversion);
  1151. end
  1152. else
  1153. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  1154. end;
  1155. { tp7 procvar support, when right is not a procvardef and we got a
  1156. loadn of a procvar then convert to a calln, the check for the
  1157. result is already done in is_convertible, also no conflict with
  1158. @procvar is here because that has an extra addrn }
  1159. if (m_tp_procvar in aktmodeswitches) and
  1160. (resulttype.def.deftype<>procvardef) and
  1161. (left.resulttype.def.deftype=procvardef) and
  1162. (left.nodetype=loadn) then
  1163. begin
  1164. hp:=ccallnode.create(nil,nil,nil,nil);
  1165. tcallnode(hp).set_procvar(left);
  1166. resulttypepass(hp);
  1167. left:=hp;
  1168. end;
  1169. { remove typeconv after niln, but not when the result is a
  1170. methodpointer. The typeconv of the methodpointer will then
  1171. take care of updateing size of niln to OS_64 }
  1172. if (left.nodetype=niln) and
  1173. not((resulttype.def.deftype=procvardef) and
  1174. (po_methodpointer in tprocvardef(resulttype.def).procoptions)) then
  1175. begin
  1176. left.resulttype:=resulttype;
  1177. result:=left;
  1178. left:=nil;
  1179. exit;
  1180. end;
  1181. { ordinal contants can be directly converted }
  1182. if (left.nodetype=ordconstn) and is_ordinal(resulttype.def) and
  1183. { but not char to char because it is a widechar to char or via versa }
  1184. { which needs extra code to do the code page transistion }
  1185. not(convtype=tc_char_2_char) then
  1186. begin
  1187. { replace the resulttype and recheck the range }
  1188. left.resulttype:=resulttype;
  1189. testrange(left.resulttype.def,tordconstnode(left).value,(nf_explizit in flags));
  1190. result:=left;
  1191. left:=nil;
  1192. exit;
  1193. end;
  1194. { fold nil to any pointer type }
  1195. if (left.nodetype=niln) and (resulttype.def.deftype=pointerdef) then
  1196. begin
  1197. hp:=cnilnode.create;
  1198. hp.resulttype:=resulttype;
  1199. result:=hp;
  1200. exit;
  1201. end;
  1202. { further, pointerconstn to any pointer is folded too }
  1203. if (left.nodetype=pointerconstn) and (resulttype.def.deftype=pointerdef) then
  1204. begin
  1205. left.resulttype:=resulttype;
  1206. result:=left;
  1207. left:=nil;
  1208. exit;
  1209. end;
  1210. { now call the resulttype helper to do constant folding }
  1211. result:=resulttype_call_helper(convtype);
  1212. end;
  1213. {$ifdef var_notification}
  1214. procedure Ttypeconvnode.mark_write;
  1215. begin
  1216. left.mark_write;
  1217. end;
  1218. {$endif}
  1219. function ttypeconvnode.first_cord_to_pointer : tnode;
  1220. begin
  1221. result:=nil;
  1222. internalerror(200104043);
  1223. end;
  1224. function ttypeconvnode.first_int_to_int : tnode;
  1225. begin
  1226. first_int_to_int:=nil;
  1227. if (left.location.loc<>LOC_REGISTER) and
  1228. (resulttype.def.size>left.resulttype.def.size) then
  1229. location.loc:=LOC_REGISTER;
  1230. if is_64bitint(resulttype.def) then
  1231. registers32:=max(registers32,2)
  1232. else
  1233. registers32:=max(registers32,1);
  1234. end;
  1235. function ttypeconvnode.first_cstring_to_pchar : tnode;
  1236. begin
  1237. first_cstring_to_pchar:=nil;
  1238. registers32:=1;
  1239. location.loc:=LOC_REGISTER;
  1240. end;
  1241. function ttypeconvnode.first_string_to_chararray : tnode;
  1242. begin
  1243. first_string_to_chararray:=nil;
  1244. registers32:=1;
  1245. location.loc:=LOC_REGISTER;
  1246. end;
  1247. function ttypeconvnode.first_char_to_string : tnode;
  1248. begin
  1249. first_char_to_string:=nil;
  1250. location.loc:=LOC_CREFERENCE;
  1251. end;
  1252. function ttypeconvnode.first_nothing : tnode;
  1253. begin
  1254. first_nothing:=nil;
  1255. end;
  1256. function ttypeconvnode.first_array_to_pointer : tnode;
  1257. begin
  1258. first_array_to_pointer:=nil;
  1259. if registers32<1 then
  1260. registers32:=1;
  1261. location.loc:=LOC_REGISTER;
  1262. end;
  1263. function ttypeconvnode.first_int_to_real: tnode;
  1264. var
  1265. fname: string[19];
  1266. typname : string[12];
  1267. begin
  1268. { Get the type name }
  1269. { Normally the typename should be one of the following:
  1270. single, double - carl
  1271. }
  1272. typname := lower(pbestrealtype^.def.gettypename);
  1273. { converting a 64bit integer to a float requires a helper }
  1274. if is_64bitint(left.resulttype.def) then
  1275. begin
  1276. if is_signed(left.resulttype.def) then
  1277. fname := 'fpc_int64_to_'+typname
  1278. else
  1279. fname := 'fpc_qword_to_'+typname;
  1280. result := ccallnode.createintern(fname,ccallparanode.create(
  1281. left,nil));
  1282. left:=nil;
  1283. firstpass(result);
  1284. exit;
  1285. end
  1286. else
  1287. { other integers are supposed to be 32 bit }
  1288. begin
  1289. if is_signed(left.resulttype.def) then
  1290. fname := 'fpc_longint_to_'+typname
  1291. else
  1292. fname := 'fpc_longword_to_'+typname;
  1293. result := ccallnode.createintern(fname,ccallparanode.create(
  1294. left,nil));
  1295. left:=nil;
  1296. firstpass(result);
  1297. exit;
  1298. end;
  1299. end;
  1300. function ttypeconvnode.first_real_to_real : tnode;
  1301. begin
  1302. first_real_to_real:=nil;
  1303. { comp isn't a floating type }
  1304. {$ifdef i386}
  1305. if (tfloatdef(resulttype.def).typ=s64comp) and
  1306. (tfloatdef(left.resulttype.def).typ<>s64comp) and
  1307. not (nf_explizit in flags) then
  1308. CGMessage(type_w_convert_real_2_comp);
  1309. {$endif}
  1310. if registersfpu<1 then
  1311. registersfpu:=1;
  1312. location.loc:=LOC_FPUREGISTER;
  1313. end;
  1314. function ttypeconvnode.first_pointer_to_array : tnode;
  1315. begin
  1316. first_pointer_to_array:=nil;
  1317. if registers32<1 then
  1318. registers32:=1;
  1319. location.loc:=LOC_REFERENCE;
  1320. end;
  1321. function ttypeconvnode.first_cchar_to_pchar : tnode;
  1322. begin
  1323. first_cchar_to_pchar:=nil;
  1324. internalerror(200104021);
  1325. end;
  1326. function ttypeconvnode.first_bool_to_int : tnode;
  1327. begin
  1328. first_bool_to_int:=nil;
  1329. { byte(boolean) or word(wordbool) or longint(longbool) must
  1330. be accepted for var parameters }
  1331. if (nf_explizit in flags) and
  1332. (left.resulttype.def.size=resulttype.def.size) and
  1333. (left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  1334. exit;
  1335. { when converting to 64bit, first convert to a 32bit int and then }
  1336. { convert to a 64bit int (only necessary for 32bit processors) (JM) }
  1337. if resulttype.def.size > sizeof(aword) then
  1338. begin
  1339. result := ctypeconvnode.create(left,u32bittype);
  1340. result.toggleflag(nf_explizit);
  1341. result := ctypeconvnode.create(result,resulttype);
  1342. left := nil;
  1343. firstpass(result);
  1344. exit;
  1345. end;
  1346. location.loc:=LOC_REGISTER;
  1347. if registers32<1 then
  1348. registers32:=1;
  1349. end;
  1350. function ttypeconvnode.first_int_to_bool : tnode;
  1351. begin
  1352. first_int_to_bool:=nil;
  1353. { byte(boolean) or word(wordbool) or longint(longbool) must
  1354. be accepted for var parameters }
  1355. if (nf_explizit in flags) and
  1356. (left.resulttype.def.size=resulttype.def.size) and
  1357. (left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  1358. exit;
  1359. location.loc:=LOC_REGISTER;
  1360. { need if bool to bool !!
  1361. not very nice !!
  1362. insertypeconv(left,s32bittype);
  1363. left.explizit:=true;
  1364. firstpass(left); }
  1365. if registers32<1 then
  1366. registers32:=1;
  1367. end;
  1368. function ttypeconvnode.first_bool_to_bool : tnode;
  1369. begin
  1370. first_bool_to_bool:=nil;
  1371. location.loc:=LOC_REGISTER;
  1372. if registers32<1 then
  1373. registers32:=1;
  1374. end;
  1375. function ttypeconvnode.first_char_to_char : tnode;
  1376. begin
  1377. first_char_to_char:=nil;
  1378. location.loc:=LOC_REGISTER;
  1379. if registers32<1 then
  1380. registers32:=1;
  1381. end;
  1382. function ttypeconvnode.first_proc_to_procvar : tnode;
  1383. begin
  1384. first_proc_to_procvar:=nil;
  1385. if (left.location.loc<>LOC_REFERENCE) then
  1386. CGMessage(cg_e_illegal_expression);
  1387. registers32:=left.registers32;
  1388. if registers32<1 then
  1389. registers32:=1;
  1390. location.loc:=LOC_REGISTER;
  1391. end;
  1392. function ttypeconvnode.first_load_smallset : tnode;
  1393. var
  1394. srsym: ttypesym;
  1395. p: tcallparanode;
  1396. begin
  1397. if not searchsystype('FPC_SMALL_SET',srsym) then
  1398. internalerror(200108313);
  1399. p := ccallparanode.create(left,nil);
  1400. { reused }
  1401. left := nil;
  1402. { convert parameter explicitely to fpc_small_set }
  1403. p.left := ctypeconvnode.create(p.left,srsym.restype);
  1404. p.left.toggleflag(nf_explizit);
  1405. { create call, adjust resulttype }
  1406. result :=
  1407. ccallnode.createinternres('fpc_set_load_small',p,resulttype);
  1408. firstpass(result);
  1409. end;
  1410. function ttypeconvnode.first_ansistring_to_pchar : tnode;
  1411. begin
  1412. first_ansistring_to_pchar:=nil;
  1413. location.loc:=LOC_REGISTER;
  1414. if registers32<1 then
  1415. registers32:=1;
  1416. end;
  1417. function ttypeconvnode.first_arrayconstructor_to_set : tnode;
  1418. begin
  1419. first_arrayconstructor_to_set:=nil;
  1420. internalerror(200104022);
  1421. end;
  1422. function ttypeconvnode.first_class_to_intf : tnode;
  1423. begin
  1424. first_class_to_intf:=nil;
  1425. location.loc:=LOC_REFERENCE;
  1426. if registers32<1 then
  1427. registers32:=1;
  1428. end;
  1429. function ttypeconvnode._first_int_to_int : tnode;
  1430. begin
  1431. result:=first_int_to_int;
  1432. end;
  1433. function ttypeconvnode._first_cstring_to_pchar : tnode;
  1434. begin
  1435. result:=first_cstring_to_pchar;
  1436. end;
  1437. function ttypeconvnode._first_string_to_chararray : tnode;
  1438. begin
  1439. result:=first_string_to_chararray;
  1440. end;
  1441. function ttypeconvnode._first_char_to_string : tnode;
  1442. begin
  1443. result:=first_char_to_string;
  1444. end;
  1445. function ttypeconvnode._first_nothing : tnode;
  1446. begin
  1447. result:=first_nothing;
  1448. end;
  1449. function ttypeconvnode._first_array_to_pointer : tnode;
  1450. begin
  1451. result:=first_array_to_pointer;
  1452. end;
  1453. function ttypeconvnode._first_int_to_real : tnode;
  1454. begin
  1455. result:=first_int_to_real;
  1456. end;
  1457. function ttypeconvnode._first_real_to_real : tnode;
  1458. begin
  1459. result:=first_real_to_real;
  1460. end;
  1461. function ttypeconvnode._first_pointer_to_array : tnode;
  1462. begin
  1463. result:=first_pointer_to_array;
  1464. end;
  1465. function ttypeconvnode._first_cchar_to_pchar : tnode;
  1466. begin
  1467. result:=first_cchar_to_pchar;
  1468. end;
  1469. function ttypeconvnode._first_bool_to_int : tnode;
  1470. begin
  1471. result:=first_bool_to_int;
  1472. end;
  1473. function ttypeconvnode._first_int_to_bool : tnode;
  1474. begin
  1475. result:=first_int_to_bool;
  1476. end;
  1477. function ttypeconvnode._first_bool_to_bool : tnode;
  1478. begin
  1479. result:=first_bool_to_bool;
  1480. end;
  1481. function ttypeconvnode._first_proc_to_procvar : tnode;
  1482. begin
  1483. result:=first_proc_to_procvar;
  1484. end;
  1485. function ttypeconvnode._first_load_smallset : tnode;
  1486. begin
  1487. result:=first_load_smallset;
  1488. end;
  1489. function ttypeconvnode._first_cord_to_pointer : tnode;
  1490. begin
  1491. result:=first_cord_to_pointer;
  1492. end;
  1493. function ttypeconvnode._first_ansistring_to_pchar : tnode;
  1494. begin
  1495. result:=first_ansistring_to_pchar;
  1496. end;
  1497. function ttypeconvnode._first_arrayconstructor_to_set : tnode;
  1498. begin
  1499. result:=first_arrayconstructor_to_set;
  1500. end;
  1501. function ttypeconvnode._first_class_to_intf : tnode;
  1502. begin
  1503. result:=first_class_to_intf;
  1504. end;
  1505. function ttypeconvnode._first_char_to_char : tnode;
  1506. begin
  1507. result:=first_char_to_char;
  1508. end;
  1509. function ttypeconvnode.first_call_helper(c : tconverttype) : tnode;
  1510. const
  1511. firstconvert : array[tconverttype] of pointer = (
  1512. @ttypeconvnode._first_nothing, {equal}
  1513. @ttypeconvnode._first_nothing, {not_possible}
  1514. nil, { removed in resulttype_string_to_string }
  1515. @ttypeconvnode._first_char_to_string,
  1516. @ttypeconvnode._first_nothing, { char_2_chararray, needs nothing extra }
  1517. nil, { removed in resulttype_chararray_to_string }
  1518. @ttypeconvnode._first_cchar_to_pchar,
  1519. @ttypeconvnode._first_cstring_to_pchar,
  1520. @ttypeconvnode._first_ansistring_to_pchar,
  1521. @ttypeconvnode._first_string_to_chararray,
  1522. nil, { removed in resulttype_chararray_to_string }
  1523. @ttypeconvnode._first_array_to_pointer,
  1524. @ttypeconvnode._first_pointer_to_array,
  1525. @ttypeconvnode._first_int_to_int,
  1526. @ttypeconvnode._first_int_to_bool,
  1527. @ttypeconvnode._first_bool_to_bool,
  1528. @ttypeconvnode._first_bool_to_int,
  1529. @ttypeconvnode._first_real_to_real,
  1530. @ttypeconvnode._first_int_to_real,
  1531. @ttypeconvnode._first_proc_to_procvar,
  1532. @ttypeconvnode._first_arrayconstructor_to_set,
  1533. @ttypeconvnode._first_load_smallset,
  1534. @ttypeconvnode._first_cord_to_pointer,
  1535. @ttypeconvnode._first_nothing,
  1536. @ttypeconvnode._first_nothing,
  1537. @ttypeconvnode._first_class_to_intf,
  1538. @ttypeconvnode._first_char_to_char,
  1539. @ttypeconvnode._first_nothing,
  1540. @ttypeconvnode._first_nothing
  1541. );
  1542. type
  1543. tprocedureofobject = function : tnode of object;
  1544. var
  1545. r : packed record
  1546. proc : pointer;
  1547. obj : pointer;
  1548. end;
  1549. begin
  1550. { this is a little bit dirty but it works }
  1551. { and should be quite portable too }
  1552. r.proc:=firstconvert[c];
  1553. r.obj:=self;
  1554. first_call_helper:=tprocedureofobject(r){$ifdef FPC}();{$endif FPC}
  1555. end;
  1556. function ttypeconvnode.pass_1 : tnode;
  1557. begin
  1558. result:=nil;
  1559. firstpass(left);
  1560. if codegenerror then
  1561. exit;
  1562. { load the value_str from the left part }
  1563. registers32:=left.registers32;
  1564. registersfpu:=left.registersfpu;
  1565. {$ifdef SUPPORT_MMX}
  1566. registersmmx:=left.registersmmx;
  1567. {$endif}
  1568. location.loc:=left.location.loc;
  1569. if nf_explizit in flags then
  1570. begin
  1571. { check if the result could be in a register }
  1572. if not(tstoreddef(resulttype.def).is_intregable) and
  1573. not(tstoreddef(resulttype.def).is_fpuregable) then
  1574. make_not_regable(left);
  1575. end;
  1576. result:=first_call_helper(convtype);
  1577. end;
  1578. function ttypeconvnode.docompare(p: tnode) : boolean;
  1579. begin
  1580. docompare :=
  1581. inherited docompare(p) and
  1582. (convtype = ttypeconvnode(p).convtype);
  1583. end;
  1584. {*****************************************************************************
  1585. TISNODE
  1586. *****************************************************************************}
  1587. constructor tisnode.create(l,r : tnode);
  1588. begin
  1589. inherited create(isn,l,r);
  1590. end;
  1591. function tisnode.det_resulttype:tnode;
  1592. var
  1593. paras: tcallparanode;
  1594. begin
  1595. result:=nil;
  1596. resulttypepass(left);
  1597. resulttypepass(right);
  1598. set_varstate(left,true);
  1599. set_varstate(right,true);
  1600. if codegenerror then
  1601. exit;
  1602. if (right.resulttype.def.deftype=classrefdef) then
  1603. begin
  1604. { left must be a class }
  1605. if is_class(left.resulttype.def) then
  1606. begin
  1607. { the operands must be related }
  1608. if (not(tobjectdef(left.resulttype.def).is_related(
  1609. tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def)))) and
  1610. (not(tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def).is_related(
  1611. tobjectdef(left.resulttype.def)))) then
  1612. CGMessage2(type_e_classes_not_related,left.resulttype.def.typename,
  1613. tclassrefdef(right.resulttype.def).pointertype.def.typename);
  1614. end
  1615. else
  1616. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  1617. { call fpc_do_is helper }
  1618. paras := ccallparanode.create(
  1619. left,
  1620. ccallparanode.create(
  1621. right,nil));
  1622. result := ccallnode.createintern('fpc_do_is',paras);
  1623. left := nil;
  1624. right := nil;
  1625. end
  1626. else if is_interface(right.resulttype.def) then
  1627. begin
  1628. { left is a class }
  1629. if is_class(left.resulttype.def) then
  1630. begin
  1631. { the operands must be related }
  1632. if not(assigned(tobjectdef(left.resulttype.def).implementedinterfaces) and
  1633. (tobjectdef(left.resulttype.def).implementedinterfaces.searchintf(right.resulttype.def)<>-1)) then
  1634. CGMessage2(type_e_classes_not_related,left.resulttype.def.typename,right.resulttype.def.typename);
  1635. end
  1636. { left is an interface }
  1637. else if is_interface(left.resulttype.def) then
  1638. begin
  1639. { the operands must be related }
  1640. if (not(tobjectdef(left.resulttype.def).is_related(tobjectdef(right.resulttype.def)))) and
  1641. (not(tobjectdef(right.resulttype.def).is_related(tobjectdef(left.resulttype.def)))) then
  1642. CGMessage(type_e_mismatch);
  1643. end
  1644. else
  1645. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  1646. { call fpc_do_is helper }
  1647. paras := ccallparanode.create(
  1648. left,
  1649. ccallparanode.create(
  1650. right,nil));
  1651. result := ccallnode.createintern('fpc_do_is',paras);
  1652. left := nil;
  1653. right := nil;
  1654. end
  1655. else
  1656. CGMessage1(type_e_class_or_interface_type_expected,right.resulttype.def.typename);
  1657. resulttype:=booltype;
  1658. end;
  1659. function tisnode.pass_1 : tnode;
  1660. begin
  1661. internalerror(200204254);
  1662. result:=nil;
  1663. end;
  1664. { dummy pass_2, it will never be called, but we need one since }
  1665. { you can't instantiate an abstract class }
  1666. procedure tisnode.pass_2;
  1667. begin
  1668. end;
  1669. {*****************************************************************************
  1670. TASNODE
  1671. *****************************************************************************}
  1672. constructor tasnode.create(l,r : tnode);
  1673. begin
  1674. inherited create(asn,l,r);
  1675. call := nil;
  1676. end;
  1677. destructor tasnode.destroy;
  1678. begin
  1679. call.free;
  1680. inherited destroy;
  1681. end;
  1682. function tasnode.det_resulttype:tnode;
  1683. var
  1684. hp : tnode;
  1685. b : boolean;
  1686. o : tobjectdef;
  1687. begin
  1688. result:=nil;
  1689. resulttypepass(right);
  1690. resulttypepass(left);
  1691. set_varstate(right,true);
  1692. set_varstate(left,true);
  1693. if codegenerror then
  1694. exit;
  1695. if (right.resulttype.def.deftype=classrefdef) then
  1696. begin
  1697. { left must be a class }
  1698. if is_class(left.resulttype.def) then
  1699. begin
  1700. { the operands must be related }
  1701. if (not(tobjectdef(left.resulttype.def).is_related(
  1702. tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def)))) and
  1703. (not(tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def).is_related(
  1704. tobjectdef(left.resulttype.def)))) then
  1705. CGMessage2(type_e_classes_not_related,left.resulttype.def.typename,
  1706. tclassrefdef(right.resulttype.def).pointertype.def.typename);
  1707. end
  1708. else
  1709. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  1710. resulttype:=tclassrefdef(right.resulttype.def).pointertype;
  1711. end
  1712. else if is_interface(right.resulttype.def) then
  1713. begin
  1714. { left is a class }
  1715. if is_class(left.resulttype.def) then
  1716. begin
  1717. { the operands must be related
  1718. no, because the class instance could be a child class of the current one which
  1719. implements additional interfaces (FK)
  1720. b:=false;
  1721. o:=tobjectdef(left.resulttype.def);
  1722. while assigned(o) do
  1723. begin
  1724. if assigned(o.implementedinterfaces) and
  1725. (o.implementedinterfaces.searchintf(right.resulttype.def)<>-1) then
  1726. begin
  1727. b:=true;
  1728. break;
  1729. end;
  1730. o:=o.childof;
  1731. end;
  1732. if not(b) then
  1733. CGMessage2(type_e_classes_not_related,left.resulttype.def.typename,right.resulttype.def.typename);
  1734. }
  1735. end
  1736. { left is an interface }
  1737. else if is_interface(left.resulttype.def) then
  1738. begin
  1739. { the operands must be related
  1740. we don't necessarily know how the both interfaces are implemented, so we can't do this check (FK)
  1741. if (not(tobjectdef(left.resulttype.def).is_related(tobjectdef(right.resulttype.def)))) and
  1742. (not(tobjectdef(right.resulttype.def).is_related(tobjectdef(left.resulttype.def)))) then
  1743. CGMessage2(type_e_classes_not_related,left.resulttype.def.typename,right.resulttype.def.typename);
  1744. }
  1745. end
  1746. else
  1747. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  1748. resulttype:=right.resulttype;
  1749. { load the GUID of the interface }
  1750. if (right.nodetype=typen) then
  1751. begin
  1752. if tobjectdef(right.resulttype.def).isiidguidvalid then
  1753. begin
  1754. hp:=cguidconstnode.create(tobjectdef(right.resulttype.def).iidguid);
  1755. right.free;
  1756. right:=hp;
  1757. end
  1758. else
  1759. internalerror(200206282);
  1760. resulttypepass(right);
  1761. end;
  1762. end
  1763. else
  1764. CGMessage1(type_e_class_or_interface_type_expected,right.resulttype.def.typename);
  1765. end;
  1766. function tasnode.getcopy: tnode;
  1767. begin
  1768. result := inherited getcopy;
  1769. if assigned(call) then
  1770. tasnode(result).call := call.getcopy
  1771. else
  1772. tasnode(result).call := nil;
  1773. end;
  1774. function tasnode.pass_1 : tnode;
  1775. var
  1776. procname: string;
  1777. begin
  1778. result:=nil;
  1779. if not assigned(call) then
  1780. begin
  1781. if is_class(left.resulttype.def) and
  1782. (right.resulttype.def.deftype=classrefdef) then
  1783. call := ccallnode.createinternres('fpc_do_as',
  1784. ccallparanode.create(left,ccallparanode.create(right,nil)),
  1785. resulttype)
  1786. else
  1787. begin
  1788. if is_class(left.resulttype.def) then
  1789. procname := 'fpc_class_as_intf'
  1790. else
  1791. procname := 'fpc_intf_as';
  1792. call := ccallnode.createinternres(procname,
  1793. ccallparanode.create(right,ccallparanode.create(left,nil)),
  1794. resulttype);
  1795. end;
  1796. left := nil;
  1797. right := nil;
  1798. firstpass(call);
  1799. if codegenerror then
  1800. exit;
  1801. location.loc:=call.location.loc;
  1802. registers32:=call.registers32;
  1803. registersfpu:=call.registersfpu;
  1804. {$ifdef SUPPORT_MMX}
  1805. registersmmx:=call.registersmmx;
  1806. {$endif SUPPORT_MMX}
  1807. end;
  1808. end;
  1809. begin
  1810. ctypeconvnode:=ttypeconvnode;
  1811. casnode:=tasnode;
  1812. cisnode:=tisnode;
  1813. end.
  1814. {
  1815. $Log$
  1816. Revision 1.86 2002-10-06 16:10:23 florian
  1817. * when compiling <interface> as <interface> we can't assume
  1818. anything about relation
  1819. Revision 1.85 2002/10/05 12:43:25 carl
  1820. * fixes for Delphi 6 compilation
  1821. (warning : Some features do not work under Delphi)
  1822. Revision 1.84 2002/10/02 20:23:50 florian
  1823. - removed the relation check for <class> as <interface> because we don't
  1824. know the runtime type of <class>! It could be a child class of the given type
  1825. which implements additional interfaces
  1826. Revision 1.83 2002/10/02 20:17:14 florian
  1827. + the as operator for <class> as <interface> has to check the parent classes as well
  1828. Revision 1.82 2002/09/30 07:00:47 florian
  1829. * fixes to common code to get the alpha compiler compiled applied
  1830. Revision 1.81 2002/09/16 14:11:13 peter
  1831. * add argument to equal_paras() to support default values or not
  1832. Revision 1.80 2002/09/07 20:40:23 carl
  1833. * cardinal -> longword
  1834. Revision 1.79 2002/09/07 15:25:03 peter
  1835. * old logs removed and tabs fixed
  1836. Revision 1.78 2002/09/07 12:16:04 carl
  1837. * second part bug report 1996 fix, testrange in cordconstnode
  1838. only called if option is set (also make parsing a tiny faster)
  1839. Revision 1.77 2002/09/05 05:56:07 jonas
  1840. - reverted my last commit, it was completely bogus :(
  1841. Revision 1.75 2002/09/02 19:24:42 peter
  1842. * array of char support for Str()
  1843. Revision 1.74 2002/09/01 08:01:16 daniel
  1844. * Removed sets from Tcallnode.det_resulttype
  1845. + Added read/write notifications of variables. These will be usefull
  1846. for providing information for several optimizations. For example
  1847. the value of the loop variable of a for loop does matter is the
  1848. variable is read after the for loop, but if it's no longer used
  1849. or written, it doesn't matter and this can be used to optimize
  1850. the loop code generation.
  1851. Revision 1.73 2002/08/23 16:14:49 peter
  1852. * tempgen cleanup
  1853. * tt_noreuse temp type added that will be used in genentrycode
  1854. Revision 1.72 2002/08/20 18:23:33 jonas
  1855. * the as node again uses a compilerproc
  1856. + (untested) support for interface "as" statements
  1857. Revision 1.71 2002/08/19 19:36:43 peter
  1858. * More fixes for cross unit inlining, all tnodes are now implemented
  1859. * Moved pocall_internconst to po_internconst because it is not a
  1860. calling type at all and it conflicted when inlining of these small
  1861. functions was requested
  1862. Revision 1.70 2002/08/17 09:23:36 florian
  1863. * first part of procinfo rewrite
  1864. Revision 1.69 2002/08/14 19:26:55 carl
  1865. + generic int_to_real type conversion
  1866. + generic unaryminus node
  1867. Revision 1.68 2002/08/11 16:08:55 florian
  1868. + support of explicit type case boolean->char
  1869. Revision 1.67 2002/08/11 15:28:00 florian
  1870. + support of explicit type case <any ordinal type>->pointer
  1871. (delphi mode only)
  1872. Revision 1.66 2002/08/09 07:33:01 florian
  1873. * a couple of interface related fixes
  1874. Revision 1.65 2002/07/29 21:23:42 florian
  1875. * more fixes for the ppc
  1876. + wrappers for the tcnvnode.first_* stuff introduced
  1877. Revision 1.64 2002/07/23 12:34:30 daniel
  1878. * Readded old set code. To use it define 'oldset'. Activated by default
  1879. for ppc.
  1880. Revision 1.63 2002/07/23 09:51:22 daniel
  1881. * Tried to make Tprocsym.defs protected. I didn't succeed but the cleanups
  1882. are worth comitting.
  1883. Revision 1.62 2002/07/22 11:48:04 daniel
  1884. * Sets are now internally sets.
  1885. Revision 1.61 2002/07/20 17:16:02 florian
  1886. + source code page support
  1887. Revision 1.60 2002/07/20 11:57:54 florian
  1888. * types.pas renamed to defbase.pas because D6 contains a types
  1889. unit so this would conflicts if D6 programms are compiled
  1890. + Willamette/SSE2 instructions to assembler added
  1891. Revision 1.59 2002/07/01 16:23:53 peter
  1892. * cg64 patch
  1893. * basics for currency
  1894. * asnode updates for class and interface (not finished)
  1895. Revision 1.58 2002/05/18 13:34:09 peter
  1896. * readded missing revisions
  1897. }