ncnv.pas 74 KB

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