ncnv.pas 74 KB

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