ncnv.pas 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367
  1. {
  2. $Id$
  3. Copyright (c) 2000 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 defines.inc}
  20. interface
  21. uses
  22. node,
  23. symtype,types,
  24. nld;
  25. type
  26. ttypeconvnode = class(tunarynode)
  27. totype : ttype;
  28. convtype : tconverttype;
  29. constructor create(node : tnode;const t : ttype);virtual;
  30. function getcopy : tnode;override;
  31. function pass_1 : tnode;override;
  32. function det_resulttype:tnode;override;
  33. function docompare(p: tnode) : boolean; override;
  34. private
  35. function resulttype_cord_to_pointer : tnode;
  36. function resulttype_string_to_string : tnode;
  37. function resulttype_char_to_string : tnode;
  38. function resulttype_int_to_real : tnode;
  39. function resulttype_real_to_real : tnode;
  40. function resulttype_cchar_to_pchar : tnode;
  41. function resulttype_arrayconstructor_to_set : tnode;
  42. function resulttype_call_helper(c : tconverttype) : tnode;
  43. protected
  44. function first_int_to_int : tnode;virtual;
  45. function first_cstring_to_pchar : tnode;virtual;
  46. function first_string_to_chararray : tnode;virtual;
  47. function first_string_to_string : tnode;virtual;
  48. function first_char_to_string : tnode;virtual;
  49. function first_nothing : tnode;virtual;
  50. function first_array_to_pointer : tnode;virtual;
  51. function first_int_to_real : tnode;virtual;
  52. function first_real_to_real : tnode;virtual;
  53. function first_pointer_to_array : tnode;virtual;
  54. function first_chararray_to_string : tnode;virtual;
  55. function first_cchar_to_pchar : tnode;virtual;
  56. function first_bool_to_int : tnode;virtual;
  57. function first_int_to_bool : tnode;virtual;
  58. function first_bool_to_bool : tnode;virtual;
  59. function first_proc_to_procvar : tnode;virtual;
  60. function first_load_smallset : tnode;virtual;
  61. function first_cord_to_pointer : tnode;virtual;
  62. function first_pchar_to_string : tnode;virtual;
  63. function first_ansistring_to_pchar : tnode;virtual;
  64. function first_arrayconstructor_to_set : tnode;virtual;
  65. function first_class_to_intf : tnode;virtual;
  66. function first_call_helper(c : tconverttype) : tnode;
  67. end;
  68. tasnode = class(tbinarynode)
  69. constructor create(l,r : tnode);virtual;
  70. function pass_1 : tnode;override;
  71. function det_resulttype:tnode;override;
  72. end;
  73. tisnode = class(tbinarynode)
  74. constructor create(l,r : tnode);virtual;
  75. function pass_1 : tnode;override;
  76. function det_resulttype:tnode;override;
  77. end;
  78. var
  79. ctypeconvnode : class of ttypeconvnode;
  80. casnode : class of tasnode;
  81. cisnode : class of tisnode;
  82. procedure inserttypeconv(var p:tnode;const t:ttype);
  83. procedure arrayconstructor_to_set(var p : tarrayconstructornode);
  84. implementation
  85. uses
  86. globtype,systems,tokens,
  87. cutils,verbose,globals,
  88. symconst,symdef,symsym,symtable,
  89. ncon,ncal,nset,nadd,
  90. {$ifdef newcg}
  91. cgbase,
  92. {$else newcg}
  93. hcodegen,
  94. {$endif newcg}
  95. htypechk,pass_1,cpubase,cpuinfo;
  96. {*****************************************************************************
  97. Helpers
  98. *****************************************************************************}
  99. procedure inserttypeconv(var p:tnode;const t:ttype);
  100. begin
  101. if not assigned(p.resulttype.def) then
  102. begin
  103. resulttypepass(p);
  104. if codegenerror then
  105. exit;
  106. end;
  107. { don't insert obsolete type conversions }
  108. if is_equal(p.resulttype.def,t.def) then
  109. begin
  110. p.resulttype:=t;
  111. end
  112. else
  113. begin
  114. p:=ctypeconvnode.create(p,t);
  115. resulttypepass(p);
  116. end;
  117. end;
  118. {*****************************************************************************
  119. Array constructor to Set Conversion
  120. *****************************************************************************}
  121. procedure arrayconstructor_to_set(var p : tarrayconstructornode);
  122. var
  123. constp : tsetconstnode;
  124. buildp,
  125. p2,p3,p4 : tnode;
  126. htype : ttype;
  127. constset : pconstset;
  128. constsetlo,
  129. constsethi : longint;
  130. procedure update_constsethi(t:ttype);
  131. begin
  132. if ((t.def.deftype=orddef) and
  133. (torddef(t.def).high>=constsethi)) then
  134. begin
  135. constsethi:=torddef(t.def).high;
  136. if htype.def=nil then
  137. begin
  138. if (constsethi>255) or
  139. (torddef(t.def).low<0) then
  140. htype:=u8bittype
  141. else
  142. htype:=t;
  143. end;
  144. if constsethi>255 then
  145. constsethi:=255;
  146. end
  147. else if ((t.def.deftype=enumdef) and
  148. (tenumdef(t.def).max>=constsethi)) then
  149. begin
  150. if htype.def=nil then
  151. htype:=t;
  152. constsethi:=tenumdef(t.def).max;
  153. end;
  154. end;
  155. procedure do_set(pos : longint);
  156. var
  157. mask,l : longint;
  158. begin
  159. if (pos>255) or (pos<0) then
  160. Message(parser_e_illegal_set_expr);
  161. if pos>constsethi then
  162. constsethi:=pos;
  163. if pos<constsetlo then
  164. constsetlo:=pos;
  165. l:=pos shr 3;
  166. mask:=1 shl (pos mod 8);
  167. { do we allow the same twice }
  168. if (constset^[l] and mask)<>0 then
  169. Message(parser_e_illegal_set_expr);
  170. constset^[l]:=constset^[l] or mask;
  171. end;
  172. var
  173. l : longint;
  174. lr,hr : longint;
  175. begin
  176. new(constset);
  177. FillChar(constset^,sizeof(constset^),0);
  178. htype.reset;
  179. constsetlo:=0;
  180. constsethi:=0;
  181. constp:=csetconstnode.create(nil,htype);
  182. constp.value_set:=constset;
  183. buildp:=constp;
  184. if assigned(p.left) then
  185. begin
  186. while assigned(p) do
  187. begin
  188. p4:=nil; { will contain the tree to create the set }
  189. {split a range into p2 and p3 }
  190. if p.left.nodetype=arrayconstructorrangen then
  191. begin
  192. p2:=tarrayconstructorrangenode(p.left).left;
  193. p3:=tarrayconstructorrangenode(p.left).right;
  194. tarrayconstructorrangenode(p.left).left:=nil;
  195. tarrayconstructorrangenode(p.left).right:=nil;
  196. end
  197. else
  198. begin
  199. p2:=p.left;
  200. p.left:=nil;
  201. p3:=nil;
  202. end;
  203. resulttypepass(p2);
  204. if assigned(p3) then
  205. resulttypepass(p3);
  206. if codegenerror then
  207. break;
  208. case p2.resulttype.def.deftype of
  209. enumdef,
  210. orddef:
  211. begin
  212. getrange(p2.resulttype.def,lr,hr);
  213. if assigned(p3) then
  214. begin
  215. { this isn't good, you'll get problems with
  216. type t010 = 0..10;
  217. ts = set of t010;
  218. var s : ts;b : t010
  219. begin s:=[1,2,b]; end.
  220. if is_integer(p3^.resulttype.def) then
  221. begin
  222. inserttypeconv(p3,u8bitdef);
  223. end;
  224. }
  225. if assigned(htype.def) and not(is_equal(htype.def,p3.resulttype.def)) then
  226. begin
  227. aktfilepos:=p3.fileinfo;
  228. CGMessage(type_e_typeconflict_in_set);
  229. end
  230. else
  231. begin
  232. if (p2.nodetype=ordconstn) and (p3.nodetype=ordconstn) then
  233. begin
  234. if not(is_integer(p3.resulttype.def)) then
  235. htype:=p3.resulttype
  236. else
  237. begin
  238. inserttypeconv(p3,u8bittype);
  239. inserttypeconv(p2,u8bittype);
  240. end;
  241. for l:=tordconstnode(p2).value to tordconstnode(p3).value do
  242. do_set(l);
  243. p2.free;
  244. p3.free;
  245. end
  246. else
  247. begin
  248. update_constsethi(p2.resulttype);
  249. inserttypeconv(p2,htype);
  250. update_constsethi(p3.resulttype);
  251. inserttypeconv(p3,htype);
  252. if assigned(htype.def) then
  253. inserttypeconv(p3,htype)
  254. else
  255. inserttypeconv(p3,u8bittype);
  256. p4:=csetelementnode.create(p2,p3);
  257. end;
  258. end;
  259. end
  260. else
  261. begin
  262. { Single value }
  263. if p2.nodetype=ordconstn then
  264. begin
  265. if not(is_integer(p2.resulttype.def)) then
  266. update_constsethi(p2.resulttype)
  267. else
  268. inserttypeconv(p2,u8bittype);
  269. do_set(tordconstnode(p2).value);
  270. p2.free;
  271. end
  272. else
  273. begin
  274. update_constsethi(p2.resulttype);
  275. if assigned(htype.def) then
  276. inserttypeconv(p2,htype)
  277. else
  278. inserttypeconv(p2,u8bittype);
  279. p4:=csetelementnode.create(p2,nil);
  280. end;
  281. end;
  282. end;
  283. stringdef :
  284. begin
  285. { if we've already set elements which are constants }
  286. { throw an error }
  287. if ((htype.def=nil) and assigned(buildp)) or
  288. not(is_char(htype.def)) then
  289. CGMessage(type_e_typeconflict_in_set)
  290. else
  291. for l:=1 to length(pstring(tstringconstnode(p2).value_str)^) do
  292. do_set(ord(pstring(tstringconstnode(p2).value_str)^[l]));
  293. if htype.def=nil then
  294. htype:=cchartype;
  295. p2.free;
  296. end;
  297. else
  298. CGMessage(type_e_ordinal_expr_expected);
  299. end;
  300. { insert the set creation tree }
  301. if assigned(p4) then
  302. buildp:=caddnode.create(addn,buildp,p4);
  303. { load next and dispose current node }
  304. p2:=p;
  305. p:=tarrayconstructornode(tarrayconstructornode(p2).right);
  306. tarrayconstructornode(p2).right:=nil;
  307. p2.free;
  308. end;
  309. if (htype.def=nil) then
  310. begin
  311. htype:=u8bittype;
  312. constsethi:=255;
  313. end;
  314. end
  315. else
  316. begin
  317. { empty set [], only remove node }
  318. p.free;
  319. end;
  320. { set the initial set type }
  321. constp.resulttype.setdef(tsetdef.create(htype,constsethi));
  322. { determine the resulttype for the tree }
  323. resulttypepass(buildp);
  324. { set the new tree }
  325. p:=tarrayconstructornode(buildp);
  326. end;
  327. {*****************************************************************************
  328. TTYPECONVNODE
  329. *****************************************************************************}
  330. constructor ttypeconvnode.create(node : tnode;const t:ttype);
  331. begin
  332. inherited create(typeconvn,node);
  333. convtype:=tc_not_possible;
  334. totype:=t;
  335. if t.def=nil then
  336. internalerror(200103281);
  337. set_file_line(node);
  338. end;
  339. function ttypeconvnode.getcopy : tnode;
  340. var
  341. n : ttypeconvnode;
  342. begin
  343. n:=ttypeconvnode(inherited getcopy);
  344. n.convtype:=convtype;
  345. getcopy:=n;
  346. end;
  347. function ttypeconvnode.resulttype_cord_to_pointer : tnode;
  348. var
  349. t : tnode;
  350. begin
  351. result:=nil;
  352. if left.nodetype=ordconstn then
  353. begin
  354. { check if we have a valid pointer constant (JM) }
  355. if (sizeof(tordconstnode) > sizeof(tpointerord)) then
  356. if (sizeof(tpointerord) = 4) then
  357. begin
  358. if (tordconstnode(left).value < low(longint)) or
  359. (tordconstnode(left).value > high(cardinal)) then
  360. CGMessage(parser_e_range_check_error);
  361. end
  362. else if (sizeof(tpointerord) = 8) then
  363. begin
  364. if (tordconstnode(left).value < low(int64)) or
  365. (tordconstnode(left).value > high(qword)) then
  366. CGMessage(parser_e_range_check_error);
  367. end
  368. else
  369. internalerror(2001020801);
  370. t:=cpointerconstnode.create(tpointerord(tordconstnode(left).value),resulttype);
  371. resulttypepass(t);
  372. result:=t;
  373. end
  374. else
  375. internalerror(200104023);
  376. end;
  377. function ttypeconvnode.resulttype_string_to_string : tnode;
  378. begin
  379. result:=nil;
  380. if left.nodetype=stringconstn then
  381. begin
  382. tstringconstnode(left).st_type:=tstringdef(resulttype.def).string_typ;
  383. tstringconstnode(left).resulttype:=resulttype;
  384. result:=left;
  385. left:=nil;
  386. end;
  387. end;
  388. function ttypeconvnode.resulttype_char_to_string : tnode;
  389. var
  390. hp : tstringconstnode;
  391. begin
  392. result:=nil;
  393. if left.nodetype=ordconstn then
  394. begin
  395. hp:=cstringconstnode.createstr(chr(tordconstnode(left).value),st_default);
  396. hp.st_type:=tstringdef(resulttype.def).string_typ;
  397. resulttypepass(hp);
  398. result:=hp;
  399. end;
  400. end;
  401. function ttypeconvnode.resulttype_int_to_real : tnode;
  402. var
  403. t : trealconstnode;
  404. begin
  405. result:=nil;
  406. if left.nodetype=ordconstn then
  407. begin
  408. t:=crealconstnode.create(tordconstnode(left).value,resulttype);
  409. resulttypepass(t);
  410. result:=t;
  411. exit;
  412. end;
  413. end;
  414. function ttypeconvnode.resulttype_real_to_real : tnode;
  415. var
  416. t : tnode;
  417. begin
  418. result:=nil;
  419. if left.nodetype=realconstn then
  420. begin
  421. t:=crealconstnode.create(trealconstnode(left).value_real,resulttype);
  422. resulttypepass(t);
  423. result:=t;
  424. end;
  425. end;
  426. function ttypeconvnode.resulttype_cchar_to_pchar : tnode;
  427. begin
  428. result:=nil;
  429. inserttypeconv(left,cshortstringtype);
  430. { evaluate again, reset resulttype so the convert_typ
  431. will be calculated again }
  432. result:=det_resulttype;
  433. end;
  434. function ttypeconvnode.resulttype_arrayconstructor_to_set : tnode;
  435. var
  436. hp : tnode;
  437. begin
  438. result:=nil;
  439. if left.nodetype<>arrayconstructorn then
  440. internalerror(5546);
  441. { remove typeconv node }
  442. hp:=left;
  443. left:=nil;
  444. { create a set constructor tree }
  445. arrayconstructor_to_set(tarrayconstructornode(hp));
  446. { now resulttypepass the set }
  447. resulttypepass(hp);
  448. result:=hp;
  449. end;
  450. function ttypeconvnode.resulttype_call_helper(c : tconverttype) : tnode;
  451. const
  452. resulttypeconvert : array[tconverttype] of pointer = (
  453. {equal} nil,
  454. {not_possible} nil,
  455. { string_2_string } @ttypeconvnode.resulttype_string_to_string,
  456. { char_2_string } @ttypeconvnode.resulttype_char_to_string,
  457. { pchar_2_string } nil,
  458. { cchar_2_pchar } @ttypeconvnode.resulttype_cchar_to_pchar,
  459. { cstring_2_pchar } nil,
  460. { ansistring_2_pchar } nil,
  461. { string_2_chararray } nil,
  462. { chararray_2_string } nil,
  463. { array_2_pointer } nil,
  464. { pointer_2_array } nil,
  465. { int_2_int } nil,
  466. { int_2_bool } nil,
  467. { bool_2_bool } nil,
  468. { bool_2_int } nil,
  469. { real_2_real } @ttypeconvnode.resulttype_real_to_real,
  470. { int_2_real } @ttypeconvnode.resulttype_int_to_real,
  471. { proc_2_procvar } nil,
  472. { arrayconstructor_2_set } @ttypeconvnode.resulttype_arrayconstructor_to_set,
  473. { load_smallset } nil,
  474. { cord_2_pointer } @ttypeconvnode.resulttype_cord_to_pointer,
  475. { intf_2_string } nil,
  476. { intf_2_guid } nil,
  477. { class_2_intf } nil
  478. );
  479. type
  480. tprocedureofobject = function : tnode of object;
  481. var
  482. r : packed record
  483. proc : pointer;
  484. obj : pointer;
  485. end;
  486. begin
  487. result:=nil;
  488. { this is a little bit dirty but it works }
  489. { and should be quite portable too }
  490. r.proc:=resulttypeconvert[c];
  491. r.obj:=self;
  492. if assigned(r.proc) then
  493. result:=tprocedureofobject(r){$ifdef FPC}();{$endif FPC}
  494. end;
  495. function ttypeconvnode.det_resulttype:tnode;
  496. var
  497. hp : tnode;
  498. aprocdef : tprocdef;
  499. begin
  500. result:=nil;
  501. resulttype:=totype;
  502. resulttypepass(left);
  503. if codegenerror then
  504. exit;
  505. { remove obsolete type conversions }
  506. if is_equal(left.resulttype.def,resulttype.def) then
  507. begin
  508. { becuase is_equal only checks the basetype for sets we need to
  509. check here if we are loading a smallset into a normalset }
  510. if (resulttype.def.deftype=setdef) and
  511. (left.resulttype.def.deftype=setdef) and
  512. (tsetdef(resulttype.def).settype<>smallset) and
  513. (tsetdef(left.resulttype.def).settype=smallset) then
  514. begin
  515. { try to define the set as a normalset if it's a constant set }
  516. if left.nodetype=setconstn then
  517. begin
  518. resulttype:=left.resulttype;
  519. tsetdef(resulttype.def).settype:=normset
  520. end
  521. else
  522. convtype:=tc_load_smallset;
  523. exit;
  524. end
  525. else
  526. begin
  527. left.resulttype:=resulttype;
  528. result:=left;
  529. left:=nil;
  530. exit;
  531. end;
  532. end;
  533. aprocdef:=assignment_overloaded(left.resulttype.def,resulttype.def);
  534. if assigned(aprocdef) then
  535. begin
  536. procinfo^.flags:=procinfo^.flags or pi_do_call;
  537. hp:=ccallnode.create(ccallparanode.create(left,nil),
  538. overloaded_operators[_assignment],nil,nil);
  539. { tell explicitly which def we must use !! (PM) }
  540. tcallnode(hp).procdefinition:=aprocdef;
  541. left:=nil;
  542. resulttypepass(hp);
  543. result:=hp;
  544. exit;
  545. end;
  546. if isconvertable(left.resulttype.def,resulttype.def,convtype,left.nodetype,nf_explizit in flags)=0 then
  547. begin
  548. {Procedures have a resulttype.def of voiddef and functions of their
  549. own resulttype.def. They will therefore always be incompatible with
  550. a procvar. Because isconvertable cannot check for procedures we
  551. use an extra check for them.}
  552. if (m_tp_procvar in aktmodeswitches) then
  553. begin
  554. if (resulttype.def.deftype=procvardef) and
  555. (is_procsym_load(left) or is_procsym_call(left)) then
  556. begin
  557. if is_procsym_call(left) then
  558. begin
  559. hp:=cloadnode.create(tprocsym(tcallnode(left).symtableprocentry),
  560. tcallnode(left).symtableproc);
  561. if (tcallnode(left).symtableprocentry.owner.symtabletype=objectsymtable) and
  562. assigned(tcallnode(left).methodpointer) then
  563. tloadnode(hp).set_mp(tcallnode(left).methodpointer.getcopy);
  564. resulttypepass(hp);
  565. left.free;
  566. left:=hp;
  567. aprocdef:=tprocdef(left.resulttype.def);
  568. end
  569. else
  570. begin
  571. if (left.nodetype<>addrn) then
  572. aprocdef:=tprocsym(tloadnode(left).symtableentry).definition;
  573. end;
  574. convtype:=tc_proc_2_procvar;
  575. { Now check if the procedure we are going to assign to
  576. the procvar, is compatible with the procvar's type }
  577. if assigned(aprocdef) then
  578. begin
  579. if not proc_to_procvar_equal(aprocdef,tprocvardef(resulttype.def)) then
  580. CGMessage2(type_e_incompatible_types,aprocdef.typename,resulttype.def.typename);
  581. result:=first_call_helper(convtype);
  582. end
  583. else
  584. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  585. exit;
  586. end;
  587. end;
  588. if nf_explizit in flags then
  589. begin
  590. { check if the result could be in a register }
  591. if not(tstoreddef(resulttype.def).is_intregable) and
  592. not(tstoreddef(resulttype.def).is_fpuregable) then
  593. make_not_regable(left);
  594. { boolean to byte are special because the
  595. location can be different }
  596. if is_integer(resulttype.def) and
  597. is_boolean(left.resulttype.def) then
  598. begin
  599. convtype:=tc_bool_2_int;
  600. result:=first_call_helper(convtype);
  601. exit;
  602. end;
  603. { ansistring to pchar }
  604. if is_pchar(resulttype.def) and
  605. is_ansistring(left.resulttype.def) then
  606. begin
  607. convtype:=tc_ansistring_2_pchar;
  608. result:=first_call_helper(convtype);
  609. exit;
  610. end;
  611. { do common tc_equal cast }
  612. convtype:=tc_equal;
  613. { enum to ordinal will always be s32bit }
  614. if (left.resulttype.def.deftype=enumdef) and
  615. is_ordinal(resulttype.def) then
  616. begin
  617. if left.nodetype=ordconstn then
  618. begin
  619. hp:=cordconstnode.create(tordconstnode(left).value,resulttype);
  620. resulttypepass(hp);
  621. result:=hp;
  622. exit;
  623. end
  624. else
  625. begin
  626. if isconvertable(s32bittype.def,resulttype.def,convtype,ordconstn,false)=0 then
  627. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  628. end;
  629. end
  630. { ordinal to enumeration }
  631. else
  632. if (resulttype.def.deftype=enumdef) and
  633. is_ordinal(left.resulttype.def) then
  634. begin
  635. if left.nodetype=ordconstn then
  636. begin
  637. hp:=cordconstnode.create(tordconstnode(left).value,resulttype);
  638. resulttypepass(hp);
  639. result:=hp;
  640. exit;
  641. end
  642. else
  643. begin
  644. if IsConvertable(left.resulttype.def,s32bittype.def,convtype,ordconstn,false)=0 then
  645. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  646. end;
  647. end
  648. { nil to ordinal node }
  649. else if is_ordinal(resulttype.def) and
  650. (left.nodetype=niln) then
  651. begin
  652. hp:=cordconstnode.create(0,resulttype);
  653. resulttypepass(hp);
  654. result:=hp;
  655. exit;
  656. end
  657. { constant pointer to ordinal }
  658. else if is_ordinal(resulttype.def) and
  659. (left.nodetype=pointerconstn) then
  660. begin
  661. hp:=cordconstnode.create(tpointerconstnode(left).value,resulttype);
  662. resulttypepass(hp);
  663. result:=hp;
  664. exit;
  665. end
  666. {Are we typecasting an ordconst to a char?}
  667. else
  668. if is_char(resulttype.def) and
  669. is_ordinal(left.resulttype.def) then
  670. begin
  671. if left.nodetype=ordconstn then
  672. begin
  673. hp:=cordconstnode.create(tordconstnode(left).value,resulttype);
  674. resulttypepass(hp);
  675. result:=hp;
  676. exit;
  677. end
  678. else
  679. begin
  680. if IsConvertable(left.resulttype.def,u8bittype.def,convtype,ordconstn,false)=0 then
  681. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  682. end;
  683. end
  684. { Are we char to ordinal }
  685. else
  686. if is_char(left.resulttype.def) and
  687. is_ordinal(resulttype.def) then
  688. begin
  689. if left.nodetype=ordconstn then
  690. begin
  691. hp:=cordconstnode.create(tordconstnode(left).value,resulttype);
  692. resulttypepass(hp);
  693. result:=hp;
  694. exit;
  695. end
  696. else
  697. begin
  698. if IsConvertable(u8bittype.def,resulttype.def,convtype,ordconstn,false)=0 then
  699. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  700. end;
  701. end
  702. { only if the same size or formal def }
  703. { why do we allow typecasting of voiddef ?? (PM) }
  704. else
  705. begin
  706. if not(
  707. (left.resulttype.def.deftype=formaldef) or
  708. (left.resulttype.def.size=resulttype.def.size) or
  709. (is_void(left.resulttype.def) and
  710. (left.nodetype=derefn))
  711. ) then
  712. CGMessage(cg_e_illegal_type_conversion);
  713. if ((left.resulttype.def.deftype=orddef) and
  714. (resulttype.def.deftype=pointerdef)) or
  715. ((resulttype.def.deftype=orddef) and
  716. (left.resulttype.def.deftype=pointerdef)) then
  717. CGMessage(cg_d_pointer_to_longint_conv_not_portable);
  718. end;
  719. { the conversion into a strutured type is only }
  720. { possible, if the source is no register }
  721. if ((resulttype.def.deftype in [recorddef,stringdef,arraydef]) or
  722. ((resulttype.def.deftype=objectdef) and not(is_class(resulttype.def)))
  723. ) and (left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) { and
  724. it also works if the assignment is overloaded
  725. YES but this code is not executed if assignment is overloaded (PM)
  726. not assigned(assignment_overloaded(left.resulttype.def,resulttype.def))} then
  727. CGMessage(cg_e_illegal_type_conversion);
  728. end
  729. else
  730. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  731. end;
  732. { tp7 procvar support, when right is not a procvardef and we got a
  733. loadn of a procvar then convert to a calln, the check for the
  734. result is already done in is_convertible, also no conflict with
  735. @procvar is here because that has an extra addrn }
  736. if (m_tp_procvar in aktmodeswitches) and
  737. (resulttype.def.deftype<>procvardef) and
  738. (left.resulttype.def.deftype=procvardef) and
  739. (left.nodetype=loadn) then
  740. begin
  741. hp:=ccallnode.create(nil,nil,nil,nil);
  742. tcallnode(hp).set_procvar(left);
  743. resulttypepass(hp);
  744. left:=hp;
  745. end;
  746. { ordinal contants can be directly converted }
  747. if (left.nodetype=ordconstn) and is_ordinal(resulttype.def) then
  748. begin
  749. { replace the resulttype and recheck the range }
  750. left.resulttype:=resulttype;
  751. testrange(left.resulttype.def,tordconstnode(left).value,(nf_explizit in flags));
  752. result:=left;
  753. left:=nil;
  754. exit;
  755. end;
  756. { now call the resulttype helper to do constant folding }
  757. result:=resulttype_call_helper(convtype);
  758. end;
  759. function ttypeconvnode.first_cord_to_pointer : tnode;
  760. begin
  761. result:=nil;
  762. internalerror(200104043);
  763. end;
  764. function ttypeconvnode.first_int_to_int : tnode;
  765. begin
  766. first_int_to_int:=nil;
  767. if (left.location.loc<>LOC_REGISTER) and
  768. (resulttype.def.size>left.resulttype.def.size) then
  769. location.loc:=LOC_REGISTER;
  770. if is_64bitint(resulttype.def) then
  771. registers32:=max(registers32,2)
  772. else
  773. registers32:=max(registers32,1);
  774. end;
  775. function ttypeconvnode.first_cstring_to_pchar : tnode;
  776. begin
  777. first_cstring_to_pchar:=nil;
  778. registers32:=1;
  779. location.loc:=LOC_REGISTER;
  780. end;
  781. function ttypeconvnode.first_string_to_chararray : tnode;
  782. begin
  783. first_string_to_chararray:=nil;
  784. registers32:=1;
  785. location.loc:=LOC_REGISTER;
  786. end;
  787. function ttypeconvnode.first_string_to_string : tnode;
  788. begin
  789. first_string_to_string:=nil;
  790. if tstringdef(resulttype.def).string_typ<>
  791. tstringdef(left.resulttype.def).string_typ then
  792. begin
  793. procinfo^.flags:=procinfo^.flags or pi_do_call;
  794. end;
  795. { for simplicity lets first keep all ansistrings
  796. as LOC_MEM, could also become LOC_REGISTER }
  797. if tstringdef(resulttype.def).string_typ in [st_ansistring,st_widestring] then
  798. { we may use ansistrings so no fast exit here }
  799. procinfo^.no_fast_exit:=true;
  800. location.loc:=LOC_MEM;
  801. end;
  802. function ttypeconvnode.first_char_to_string : tnode;
  803. begin
  804. first_char_to_string:=nil;
  805. location.loc:=LOC_MEM;
  806. end;
  807. function ttypeconvnode.first_nothing : tnode;
  808. begin
  809. first_nothing:=nil;
  810. location.loc:=LOC_MEM;
  811. end;
  812. function ttypeconvnode.first_array_to_pointer : tnode;
  813. begin
  814. first_array_to_pointer:=nil;
  815. if registers32<1 then
  816. registers32:=1;
  817. location.loc:=LOC_REGISTER;
  818. end;
  819. function ttypeconvnode.first_int_to_real : tnode;
  820. begin
  821. first_int_to_real:=nil;
  822. if registersfpu<1 then
  823. registersfpu:=1;
  824. location.loc:=LOC_FPU;
  825. end;
  826. function ttypeconvnode.first_real_to_real : tnode;
  827. begin
  828. first_real_to_real:=nil;
  829. { comp isn't a floating type }
  830. {$ifdef i386}
  831. if (tfloatdef(resulttype.def).typ=s64comp) and
  832. (tfloatdef(left.resulttype.def).typ<>s64comp) and
  833. not (nf_explizit in flags) then
  834. CGMessage(type_w_convert_real_2_comp);
  835. {$endif}
  836. if registersfpu<1 then
  837. registersfpu:=1;
  838. location.loc:=LOC_FPU;
  839. end;
  840. function ttypeconvnode.first_pointer_to_array : tnode;
  841. begin
  842. first_pointer_to_array:=nil;
  843. if registers32<1 then
  844. registers32:=1;
  845. location.loc:=LOC_REFERENCE;
  846. end;
  847. function ttypeconvnode.first_chararray_to_string : tnode;
  848. begin
  849. first_chararray_to_string:=nil;
  850. { the only important information is the location of the }
  851. { result }
  852. { other stuff is done by firsttypeconv }
  853. location.loc:=LOC_MEM;
  854. end;
  855. function ttypeconvnode.first_cchar_to_pchar : tnode;
  856. begin
  857. first_cchar_to_pchar:=nil;
  858. internalerror(200104021);
  859. end;
  860. function ttypeconvnode.first_bool_to_int : tnode;
  861. begin
  862. first_bool_to_int:=nil;
  863. { byte(boolean) or word(wordbool) or longint(longbool) must
  864. be accepted for var parameters }
  865. if (nf_explizit in flags) and
  866. (left.resulttype.def.size=resulttype.def.size) and
  867. (left.location.loc in [LOC_REFERENCE,LOC_MEM,LOC_CREGISTER]) then
  868. exit;
  869. location.loc:=LOC_REGISTER;
  870. if registers32<1 then
  871. registers32:=1;
  872. end;
  873. function ttypeconvnode.first_int_to_bool : tnode;
  874. begin
  875. first_int_to_bool:=nil;
  876. { byte(boolean) or word(wordbool) or longint(longbool) must
  877. be accepted for var parameters }
  878. if (nf_explizit in flags) and
  879. (left.resulttype.def.size=resulttype.def.size) and
  880. (left.location.loc in [LOC_REFERENCE,LOC_MEM,LOC_CREGISTER]) then
  881. exit;
  882. location.loc:=LOC_REGISTER;
  883. { need if bool to bool !!
  884. not very nice !!
  885. insertypeconv(left,s32bittype);
  886. left.explizit:=true;
  887. firstpass(left); }
  888. if registers32<1 then
  889. registers32:=1;
  890. end;
  891. function ttypeconvnode.first_bool_to_bool : tnode;
  892. begin
  893. first_bool_to_bool:=nil;
  894. location.loc:=LOC_REGISTER;
  895. if registers32<1 then
  896. registers32:=1;
  897. end;
  898. function ttypeconvnode.first_proc_to_procvar : tnode;
  899. begin
  900. first_proc_to_procvar:=nil;
  901. if (left.location.loc<>LOC_REFERENCE) then
  902. CGMessage(cg_e_illegal_expression);
  903. registers32:=left.registers32;
  904. if registers32<1 then
  905. registers32:=1;
  906. location.loc:=LOC_REGISTER;
  907. end;
  908. function ttypeconvnode.first_load_smallset : tnode;
  909. begin
  910. first_load_smallset:=nil;
  911. end;
  912. function ttypeconvnode.first_pchar_to_string : tnode;
  913. begin
  914. first_pchar_to_string:=nil;
  915. location.loc:=LOC_REFERENCE;
  916. end;
  917. function ttypeconvnode.first_ansistring_to_pchar : tnode;
  918. begin
  919. first_ansistring_to_pchar:=nil;
  920. location.loc:=LOC_REGISTER;
  921. if registers32<1 then
  922. registers32:=1;
  923. end;
  924. function ttypeconvnode.first_arrayconstructor_to_set : tnode;
  925. begin
  926. first_arrayconstructor_to_set:=nil;
  927. internalerror(200104022);
  928. end;
  929. function ttypeconvnode.first_class_to_intf : tnode;
  930. begin
  931. first_class_to_intf:=nil;
  932. location.loc:=LOC_REFERENCE;
  933. if registers32<1 then
  934. registers32:=1;
  935. end;
  936. function ttypeconvnode.first_call_helper(c : tconverttype) : tnode;
  937. const
  938. firstconvert : array[tconverttype] of pointer = (
  939. @ttypeconvnode.first_nothing, {equal}
  940. @ttypeconvnode.first_nothing, {not_possible}
  941. @ttypeconvnode.first_string_to_string,
  942. @ttypeconvnode.first_char_to_string,
  943. @ttypeconvnode.first_pchar_to_string,
  944. @ttypeconvnode.first_cchar_to_pchar,
  945. @ttypeconvnode.first_cstring_to_pchar,
  946. @ttypeconvnode.first_ansistring_to_pchar,
  947. @ttypeconvnode.first_string_to_chararray,
  948. @ttypeconvnode.first_chararray_to_string,
  949. @ttypeconvnode.first_array_to_pointer,
  950. @ttypeconvnode.first_pointer_to_array,
  951. @ttypeconvnode.first_int_to_int,
  952. @ttypeconvnode.first_int_to_bool,
  953. @ttypeconvnode.first_bool_to_bool,
  954. @ttypeconvnode.first_bool_to_int,
  955. @ttypeconvnode.first_real_to_real,
  956. @ttypeconvnode.first_int_to_real,
  957. @ttypeconvnode.first_proc_to_procvar,
  958. @ttypeconvnode.first_arrayconstructor_to_set,
  959. @ttypeconvnode.first_load_smallset,
  960. @ttypeconvnode.first_cord_to_pointer,
  961. @ttypeconvnode.first_nothing,
  962. @ttypeconvnode.first_nothing,
  963. @ttypeconvnode.first_class_to_intf
  964. );
  965. type
  966. tprocedureofobject = function : tnode of object;
  967. var
  968. r : packed record
  969. proc : pointer;
  970. obj : pointer;
  971. end;
  972. begin
  973. { this is a little bit dirty but it works }
  974. { and should be quite portable too }
  975. r.proc:=firstconvert[c];
  976. r.obj:=self;
  977. first_call_helper:=tprocedureofobject(r){$ifdef FPC}();{$endif FPC}
  978. end;
  979. function ttypeconvnode.pass_1 : tnode;
  980. begin
  981. result:=nil;
  982. firstpass(left);
  983. if codegenerror then
  984. exit;
  985. { load the value_str from the left part }
  986. registers32:=left.registers32;
  987. registersfpu:=left.registersfpu;
  988. {$ifdef SUPPORT_MMX}
  989. registersmmx:=left.registersmmx;
  990. {$endif}
  991. set_location(location,left.location);
  992. if nf_explizit in flags then
  993. begin
  994. { check if the result could be in a register }
  995. if not(tstoreddef(resulttype.def).is_intregable) and
  996. not(tstoreddef(resulttype.def).is_fpuregable) then
  997. make_not_regable(left);
  998. end;
  999. if convtype=tc_equal then
  1000. begin
  1001. { remove typeconv node if left is a const. For other nodes we can't
  1002. remove it because the secondpass can still depend on the old type (PFV) }
  1003. if is_constnode(left) then
  1004. begin
  1005. left.resulttype:=resulttype;
  1006. result:=left;
  1007. left:=nil;
  1008. end;
  1009. end
  1010. else
  1011. begin
  1012. result:=first_call_helper(convtype);
  1013. end;
  1014. end;
  1015. {*****************************************************************************
  1016. TISNODE
  1017. *****************************************************************************}
  1018. constructor tisnode.create(l,r : tnode);
  1019. begin
  1020. inherited create(isn,l,r);
  1021. end;
  1022. function tisnode.det_resulttype:tnode;
  1023. begin
  1024. result:=nil;
  1025. resulttypepass(left);
  1026. resulttypepass(right);
  1027. set_varstate(left,true);
  1028. set_varstate(right,true);
  1029. if codegenerror then
  1030. exit;
  1031. if (right.resulttype.def.deftype=classrefdef) then
  1032. begin
  1033. { left must be a class }
  1034. if is_class(left.resulttype.def) then
  1035. begin
  1036. { the operands must be related }
  1037. if (not(tobjectdef(left.resulttype.def).is_related(
  1038. tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def)))) and
  1039. (not(tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def).is_related(
  1040. tobjectdef(left.resulttype.def)))) then
  1041. CGMessage(type_e_mismatch);
  1042. end
  1043. else
  1044. CGMessage(type_e_mismatch);
  1045. end
  1046. else
  1047. CGMessage(type_e_mismatch);
  1048. resulttype:=booltype;
  1049. end;
  1050. function tisnode.pass_1 : tnode;
  1051. begin
  1052. result:=nil;
  1053. firstpass(left);
  1054. firstpass(right);
  1055. if codegenerror then
  1056. exit;
  1057. left_right_max;
  1058. location.loc:=LOC_FLAGS;
  1059. end;
  1060. {*****************************************************************************
  1061. TASNODE
  1062. *****************************************************************************}
  1063. constructor tasnode.create(l,r : tnode);
  1064. begin
  1065. inherited create(asn,l,r);
  1066. end;
  1067. function tasnode.det_resulttype:tnode;
  1068. begin
  1069. result:=nil;
  1070. resulttypepass(right);
  1071. resulttypepass(left);
  1072. set_varstate(right,true);
  1073. set_varstate(left,true);
  1074. if codegenerror then
  1075. exit;
  1076. if (right.resulttype.def.deftype=classrefdef) then
  1077. begin
  1078. { left must be a class }
  1079. if is_class(left.resulttype.def) then
  1080. begin
  1081. { the operands must be related }
  1082. if (not(tobjectdef(left.resulttype.def).is_related(
  1083. tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def)))) and
  1084. (not(tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def).is_related(
  1085. tobjectdef(left.resulttype.def)))) then
  1086. CGMessage(type_e_mismatch);
  1087. end
  1088. else
  1089. CGMessage(type_e_mismatch);
  1090. resulttype:=tclassrefdef(right.resulttype.def).pointertype;
  1091. end
  1092. else
  1093. CGMessage(type_e_mismatch);
  1094. end;
  1095. function tasnode.pass_1 : tnode;
  1096. begin
  1097. result:=nil;
  1098. firstpass(right);
  1099. firstpass(left);
  1100. if codegenerror then
  1101. exit;
  1102. left_right_max;
  1103. set_location(location,left.location);
  1104. end;
  1105. function ttypeconvnode.docompare(p: tnode) : boolean;
  1106. begin
  1107. docompare :=
  1108. inherited docompare(p) and
  1109. (convtype = ttypeconvnode(p).convtype);
  1110. end;
  1111. begin
  1112. ctypeconvnode:=ttypeconvnode;
  1113. casnode:=tasnode;
  1114. cisnode:=tisnode;
  1115. end.
  1116. {
  1117. $Log$
  1118. Revision 1.24 2001-04-13 01:22:08 peter
  1119. * symtable change to classes
  1120. * range check generation and errors fixed, make cycle DEBUG=1 works
  1121. * memory leaks fixed
  1122. Revision 1.23 2001/04/04 22:42:39 peter
  1123. * move constant folding into det_resulttype
  1124. Revision 1.22 2001/04/02 21:20:30 peter
  1125. * resulttype rewrite
  1126. Revision 1.21 2001/03/08 17:44:47 jonas
  1127. * fixed web bug 1430
  1128. Revision 1.20 2001/02/21 11:49:50 jonas
  1129. * evaluate typecasts of const pointers to ordinals inline ('merged')
  1130. Revision 1.19 2001/02/20 18:37:10 peter
  1131. * removed unused code
  1132. Revision 1.18 2001/02/20 13:14:18 marco
  1133. * Fix from Peter for passing a procedure of method to a other method in a method
  1134. Revision 1.17 2001/02/08 13:09:03 jonas
  1135. * fixed web bug 1396: tpointerord is now a cardinal instead of a longint,
  1136. but added a hack in ncnv so that pointer(-1) still works
  1137. Revision 1.16 2000/12/31 11:14:10 jonas
  1138. + implemented/fixed docompare() mathods for all nodes (not tested)
  1139. + nopt.pas, nadd.pas, i386/n386opt.pas: optimized nodes for adding strings
  1140. and constant strings/chars together
  1141. * n386add.pas: don't copy temp strings (of size 256) to another temp string
  1142. when adding
  1143. Revision 1.15 2000/12/08 12:41:01 jonas
  1144. * fixed bug in sign extension patch
  1145. Revision 1.14 2000/12/07 17:19:42 jonas
  1146. * new constant handling: from now on, hex constants >$7fffffff are
  1147. parsed as unsigned constants (otherwise, $80000000 got sign extended
  1148. and became $ffffffff80000000), all constants in the longint range
  1149. become longints, all constants >$7fffffff and <=cardinal($ffffffff)
  1150. are cardinals and the rest are int64's.
  1151. * added lots of longint typecast to prevent range check errors in the
  1152. compiler and rtl
  1153. * type casts of symbolic ordinal constants are now preserved
  1154. * fixed bug where the original resulttype.def wasn't restored correctly
  1155. after doing a 64bit rangecheck
  1156. Revision 1.13 2000/11/29 00:30:32 florian
  1157. * unused units removed from uses clause
  1158. * some changes for widestrings
  1159. Revision 1.12 2000/11/20 16:06:04 jonas
  1160. + allow evaluation of 64bit constant expressions at compile time
  1161. * disable range checking for explicit typecasts of constant expressions
  1162. Revision 1.11 2000/11/12 23:24:11 florian
  1163. * interfaces are basically running
  1164. Revision 1.10 2000/11/04 14:25:20 florian
  1165. + merged Attila's changes for interfaces, not tested yet
  1166. Revision 1.9 2000/10/31 22:02:48 peter
  1167. * symtable splitted, no real code changes
  1168. Revision 1.8 2000/10/14 21:52:55 peter
  1169. * fixed memory leaks
  1170. Revision 1.7 2000/10/14 10:14:50 peter
  1171. * moehrendorf oct 2000 rewrite
  1172. Revision 1.6 2000/10/01 19:48:24 peter
  1173. * lot of compile updates for cg11
  1174. Revision 1.5 2000/09/28 19:49:52 florian
  1175. *** empty log message ***
  1176. Revision 1.4 2000/09/27 18:14:31 florian
  1177. * fixed a lot of syntax errors in the n*.pas stuff
  1178. Revision 1.3 2000/09/26 20:06:13 florian
  1179. * hmm, still a lot of work to get things compilable
  1180. Revision 1.2 2000/09/26 14:59:34 florian
  1181. * more conversion work done
  1182. Revision 1.1 2000/09/25 15:37:14 florian
  1183. * more fixes
  1184. }