ncnv.pas 65 KB

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