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. end
  582. else
  583. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  584. exit;
  585. end;
  586. end;
  587. if nf_explizit in flags then
  588. begin
  589. { check if the result could be in a register }
  590. if not(tstoreddef(resulttype.def).is_intregable) and
  591. not(tstoreddef(resulttype.def).is_fpuregable) then
  592. make_not_regable(left);
  593. { boolean to byte are special because the
  594. location can be different }
  595. if is_integer(resulttype.def) and
  596. is_boolean(left.resulttype.def) then
  597. begin
  598. convtype:=tc_bool_2_int;
  599. exit;
  600. end;
  601. { ansistring to pchar }
  602. if is_pchar(resulttype.def) and
  603. is_ansistring(left.resulttype.def) then
  604. begin
  605. convtype:=tc_ansistring_2_pchar;
  606. exit;
  607. end;
  608. { do common tc_equal cast }
  609. convtype:=tc_equal;
  610. { enum to ordinal will always be s32bit }
  611. if (left.resulttype.def.deftype=enumdef) and
  612. is_ordinal(resulttype.def) then
  613. begin
  614. if left.nodetype=ordconstn then
  615. begin
  616. hp:=cordconstnode.create(tordconstnode(left).value,resulttype);
  617. resulttypepass(hp);
  618. result:=hp;
  619. exit;
  620. end
  621. else
  622. begin
  623. if isconvertable(s32bittype.def,resulttype.def,convtype,ordconstn,false)=0 then
  624. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  625. end;
  626. end
  627. { ordinal to enumeration }
  628. else
  629. if (resulttype.def.deftype=enumdef) and
  630. is_ordinal(left.resulttype.def) then
  631. begin
  632. if left.nodetype=ordconstn then
  633. begin
  634. hp:=cordconstnode.create(tordconstnode(left).value,resulttype);
  635. resulttypepass(hp);
  636. result:=hp;
  637. exit;
  638. end
  639. else
  640. begin
  641. if IsConvertable(left.resulttype.def,s32bittype.def,convtype,ordconstn,false)=0 then
  642. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  643. end;
  644. end
  645. { nil to ordinal node }
  646. else if is_ordinal(resulttype.def) and
  647. (left.nodetype=niln) then
  648. begin
  649. hp:=cordconstnode.create(0,resulttype);
  650. resulttypepass(hp);
  651. result:=hp;
  652. exit;
  653. end
  654. { constant pointer to ordinal }
  655. else if is_ordinal(resulttype.def) and
  656. (left.nodetype=pointerconstn) then
  657. begin
  658. hp:=cordconstnode.create(tpointerconstnode(left).value,resulttype);
  659. resulttypepass(hp);
  660. result:=hp;
  661. exit;
  662. end
  663. {Are we typecasting an ordconst to a char?}
  664. else
  665. if is_char(resulttype.def) and
  666. is_ordinal(left.resulttype.def) then
  667. begin
  668. if left.nodetype=ordconstn then
  669. begin
  670. hp:=cordconstnode.create(tordconstnode(left).value,resulttype);
  671. resulttypepass(hp);
  672. result:=hp;
  673. exit;
  674. end
  675. else
  676. begin
  677. if IsConvertable(left.resulttype.def,u8bittype.def,convtype,ordconstn,false)=0 then
  678. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  679. end;
  680. end
  681. { Are we char to ordinal }
  682. else
  683. if is_char(left.resulttype.def) and
  684. is_ordinal(resulttype.def) then
  685. begin
  686. if left.nodetype=ordconstn then
  687. begin
  688. hp:=cordconstnode.create(tordconstnode(left).value,resulttype);
  689. resulttypepass(hp);
  690. result:=hp;
  691. exit;
  692. end
  693. else
  694. begin
  695. if IsConvertable(u8bittype.def,resulttype.def,convtype,ordconstn,false)=0 then
  696. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  697. end;
  698. end
  699. { only if the same size or formal def }
  700. { why do we allow typecasting of voiddef ?? (PM) }
  701. else
  702. begin
  703. if not(
  704. (left.resulttype.def.deftype=formaldef) or
  705. (left.resulttype.def.size=resulttype.def.size) or
  706. (is_void(left.resulttype.def) and
  707. (left.nodetype=derefn))
  708. ) then
  709. CGMessage(cg_e_illegal_type_conversion);
  710. if ((left.resulttype.def.deftype=orddef) and
  711. (resulttype.def.deftype=pointerdef)) or
  712. ((resulttype.def.deftype=orddef) and
  713. (left.resulttype.def.deftype=pointerdef)) then
  714. CGMessage(cg_d_pointer_to_longint_conv_not_portable);
  715. end;
  716. { the conversion into a strutured type is only }
  717. { possible, if the source is no register }
  718. if ((resulttype.def.deftype in [recorddef,stringdef,arraydef]) or
  719. ((resulttype.def.deftype=objectdef) and not(is_class(resulttype.def)))
  720. ) and (left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) { and
  721. it also works if the assignment is overloaded
  722. YES but this code is not executed if assignment is overloaded (PM)
  723. not assigned(assignment_overloaded(left.resulttype.def,resulttype.def))} then
  724. CGMessage(cg_e_illegal_type_conversion);
  725. end
  726. else
  727. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  728. end;
  729. { tp7 procvar support, when right is not a procvardef and we got a
  730. loadn of a procvar then convert to a calln, the check for the
  731. result is already done in is_convertible, also no conflict with
  732. @procvar is here because that has an extra addrn }
  733. if (m_tp_procvar in aktmodeswitches) and
  734. (resulttype.def.deftype<>procvardef) and
  735. (left.resulttype.def.deftype=procvardef) and
  736. (left.nodetype=loadn) then
  737. begin
  738. hp:=ccallnode.create(nil,nil,nil,nil);
  739. tcallnode(hp).set_procvar(left);
  740. resulttypepass(hp);
  741. left:=hp;
  742. end;
  743. { ordinal contants can be directly converted }
  744. if (left.nodetype=ordconstn) and is_ordinal(resulttype.def) then
  745. begin
  746. { replace the resulttype and recheck the range }
  747. left.resulttype:=resulttype;
  748. testrange(left.resulttype.def,tordconstnode(left).value,(nf_explizit in flags));
  749. result:=left;
  750. left:=nil;
  751. exit;
  752. end;
  753. { now call the resulttype helper to do constant folding }
  754. result:=resulttype_call_helper(convtype);
  755. end;
  756. function ttypeconvnode.first_cord_to_pointer : tnode;
  757. begin
  758. result:=nil;
  759. internalerror(200104043);
  760. end;
  761. function ttypeconvnode.first_int_to_int : tnode;
  762. begin
  763. first_int_to_int:=nil;
  764. if (left.location.loc<>LOC_REGISTER) and
  765. (resulttype.def.size>left.resulttype.def.size) then
  766. location.loc:=LOC_REGISTER;
  767. if is_64bitint(resulttype.def) then
  768. registers32:=max(registers32,2)
  769. else
  770. registers32:=max(registers32,1);
  771. end;
  772. function ttypeconvnode.first_cstring_to_pchar : tnode;
  773. begin
  774. first_cstring_to_pchar:=nil;
  775. registers32:=1;
  776. location.loc:=LOC_REGISTER;
  777. end;
  778. function ttypeconvnode.first_string_to_chararray : tnode;
  779. begin
  780. first_string_to_chararray:=nil;
  781. registers32:=1;
  782. location.loc:=LOC_REGISTER;
  783. end;
  784. function ttypeconvnode.first_string_to_string : tnode;
  785. begin
  786. first_string_to_string:=nil;
  787. if tstringdef(resulttype.def).string_typ<>
  788. tstringdef(left.resulttype.def).string_typ then
  789. begin
  790. procinfo^.flags:=procinfo^.flags or pi_do_call;
  791. end;
  792. { for simplicity lets first keep all ansistrings
  793. as LOC_MEM, could also become LOC_REGISTER }
  794. if tstringdef(resulttype.def).string_typ in [st_ansistring,st_widestring] then
  795. { we may use ansistrings so no fast exit here }
  796. procinfo^.no_fast_exit:=true;
  797. location.loc:=LOC_MEM;
  798. end;
  799. function ttypeconvnode.first_char_to_string : tnode;
  800. begin
  801. first_char_to_string:=nil;
  802. location.loc:=LOC_MEM;
  803. end;
  804. function ttypeconvnode.first_nothing : tnode;
  805. begin
  806. first_nothing:=nil;
  807. location.loc:=LOC_MEM;
  808. end;
  809. function ttypeconvnode.first_array_to_pointer : tnode;
  810. begin
  811. first_array_to_pointer:=nil;
  812. if registers32<1 then
  813. registers32:=1;
  814. location.loc:=LOC_REGISTER;
  815. end;
  816. function ttypeconvnode.first_int_to_real : tnode;
  817. begin
  818. first_int_to_real:=nil;
  819. if registersfpu<1 then
  820. registersfpu:=1;
  821. location.loc:=LOC_FPU;
  822. end;
  823. function ttypeconvnode.first_real_to_real : tnode;
  824. begin
  825. first_real_to_real:=nil;
  826. { comp isn't a floating type }
  827. {$ifdef i386}
  828. if (tfloatdef(resulttype.def).typ=s64comp) and
  829. (tfloatdef(left.resulttype.def).typ<>s64comp) and
  830. not (nf_explizit in flags) then
  831. CGMessage(type_w_convert_real_2_comp);
  832. {$endif}
  833. if registersfpu<1 then
  834. registersfpu:=1;
  835. location.loc:=LOC_FPU;
  836. end;
  837. function ttypeconvnode.first_pointer_to_array : tnode;
  838. begin
  839. first_pointer_to_array:=nil;
  840. if registers32<1 then
  841. registers32:=1;
  842. location.loc:=LOC_REFERENCE;
  843. end;
  844. function ttypeconvnode.first_chararray_to_string : tnode;
  845. begin
  846. first_chararray_to_string:=nil;
  847. { the only important information is the location of the }
  848. { result }
  849. { other stuff is done by firsttypeconv }
  850. location.loc:=LOC_MEM;
  851. end;
  852. function ttypeconvnode.first_cchar_to_pchar : tnode;
  853. begin
  854. first_cchar_to_pchar:=nil;
  855. internalerror(200104021);
  856. end;
  857. function ttypeconvnode.first_bool_to_int : tnode;
  858. begin
  859. first_bool_to_int:=nil;
  860. { byte(boolean) or word(wordbool) or longint(longbool) must
  861. be accepted for var parameters }
  862. if (nf_explizit in flags) and
  863. (left.resulttype.def.size=resulttype.def.size) and
  864. (left.location.loc in [LOC_REFERENCE,LOC_MEM,LOC_CREGISTER]) then
  865. exit;
  866. location.loc:=LOC_REGISTER;
  867. if registers32<1 then
  868. registers32:=1;
  869. end;
  870. function ttypeconvnode.first_int_to_bool : tnode;
  871. begin
  872. first_int_to_bool:=nil;
  873. { byte(boolean) or word(wordbool) or longint(longbool) must
  874. be accepted for var parameters }
  875. if (nf_explizit in flags) and
  876. (left.resulttype.def.size=resulttype.def.size) and
  877. (left.location.loc in [LOC_REFERENCE,LOC_MEM,LOC_CREGISTER]) then
  878. exit;
  879. location.loc:=LOC_REGISTER;
  880. { need if bool to bool !!
  881. not very nice !!
  882. insertypeconv(left,s32bittype);
  883. left.explizit:=true;
  884. firstpass(left); }
  885. if registers32<1 then
  886. registers32:=1;
  887. end;
  888. function ttypeconvnode.first_bool_to_bool : tnode;
  889. begin
  890. first_bool_to_bool:=nil;
  891. location.loc:=LOC_REGISTER;
  892. if registers32<1 then
  893. registers32:=1;
  894. end;
  895. function ttypeconvnode.first_proc_to_procvar : tnode;
  896. begin
  897. first_proc_to_procvar:=nil;
  898. if (left.location.loc<>LOC_REFERENCE) then
  899. CGMessage(cg_e_illegal_expression);
  900. registers32:=left.registers32;
  901. if registers32<1 then
  902. registers32:=1;
  903. location.loc:=LOC_REGISTER;
  904. end;
  905. function ttypeconvnode.first_load_smallset : tnode;
  906. begin
  907. first_load_smallset:=nil;
  908. end;
  909. function ttypeconvnode.first_pchar_to_string : tnode;
  910. begin
  911. first_pchar_to_string:=nil;
  912. location.loc:=LOC_REFERENCE;
  913. end;
  914. function ttypeconvnode.first_ansistring_to_pchar : tnode;
  915. begin
  916. first_ansistring_to_pchar:=nil;
  917. location.loc:=LOC_REGISTER;
  918. if registers32<1 then
  919. registers32:=1;
  920. end;
  921. function ttypeconvnode.first_arrayconstructor_to_set : tnode;
  922. begin
  923. first_arrayconstructor_to_set:=nil;
  924. internalerror(200104022);
  925. end;
  926. function ttypeconvnode.first_class_to_intf : tnode;
  927. begin
  928. first_class_to_intf:=nil;
  929. location.loc:=LOC_REFERENCE;
  930. if registers32<1 then
  931. registers32:=1;
  932. end;
  933. function ttypeconvnode.first_call_helper(c : tconverttype) : tnode;
  934. const
  935. firstconvert : array[tconverttype] of pointer = (
  936. @ttypeconvnode.first_nothing, {equal}
  937. @ttypeconvnode.first_nothing, {not_possible}
  938. @ttypeconvnode.first_string_to_string,
  939. @ttypeconvnode.first_char_to_string,
  940. @ttypeconvnode.first_pchar_to_string,
  941. @ttypeconvnode.first_cchar_to_pchar,
  942. @ttypeconvnode.first_cstring_to_pchar,
  943. @ttypeconvnode.first_ansistring_to_pchar,
  944. @ttypeconvnode.first_string_to_chararray,
  945. @ttypeconvnode.first_chararray_to_string,
  946. @ttypeconvnode.first_array_to_pointer,
  947. @ttypeconvnode.first_pointer_to_array,
  948. @ttypeconvnode.first_int_to_int,
  949. @ttypeconvnode.first_int_to_bool,
  950. @ttypeconvnode.first_bool_to_bool,
  951. @ttypeconvnode.first_bool_to_int,
  952. @ttypeconvnode.first_real_to_real,
  953. @ttypeconvnode.first_int_to_real,
  954. @ttypeconvnode.first_proc_to_procvar,
  955. @ttypeconvnode.first_arrayconstructor_to_set,
  956. @ttypeconvnode.first_load_smallset,
  957. @ttypeconvnode.first_cord_to_pointer,
  958. @ttypeconvnode.first_nothing,
  959. @ttypeconvnode.first_nothing,
  960. @ttypeconvnode.first_class_to_intf
  961. );
  962. type
  963. tprocedureofobject = function : tnode of object;
  964. var
  965. r : packed record
  966. proc : pointer;
  967. obj : pointer;
  968. end;
  969. begin
  970. { this is a little bit dirty but it works }
  971. { and should be quite portable too }
  972. r.proc:=firstconvert[c];
  973. r.obj:=self;
  974. first_call_helper:=tprocedureofobject(r){$ifdef FPC}();{$endif FPC}
  975. end;
  976. function ttypeconvnode.pass_1 : tnode;
  977. begin
  978. result:=nil;
  979. firstpass(left);
  980. if codegenerror then
  981. exit;
  982. { load the value_str from the left part }
  983. registers32:=left.registers32;
  984. registersfpu:=left.registersfpu;
  985. {$ifdef SUPPORT_MMX}
  986. registersmmx:=left.registersmmx;
  987. {$endif}
  988. set_location(location,left.location);
  989. if nf_explizit in flags then
  990. begin
  991. { check if the result could be in a register }
  992. if not(tstoreddef(resulttype.def).is_intregable) and
  993. not(tstoreddef(resulttype.def).is_fpuregable) then
  994. make_not_regable(left);
  995. end;
  996. if convtype=tc_equal then
  997. begin
  998. { remove typeconv node if left is a const. For other nodes we can't
  999. remove it because the secondpass can still depend on the old type (PFV) }
  1000. if is_constnode(left) then
  1001. begin
  1002. left.resulttype:=resulttype;
  1003. result:=left;
  1004. left:=nil;
  1005. end;
  1006. end
  1007. else
  1008. begin
  1009. result:=first_call_helper(convtype);
  1010. end;
  1011. end;
  1012. {*****************************************************************************
  1013. TISNODE
  1014. *****************************************************************************}
  1015. constructor tisnode.create(l,r : tnode);
  1016. begin
  1017. inherited create(isn,l,r);
  1018. end;
  1019. function tisnode.det_resulttype:tnode;
  1020. begin
  1021. result:=nil;
  1022. resulttypepass(left);
  1023. resulttypepass(right);
  1024. set_varstate(left,true);
  1025. set_varstate(right,true);
  1026. if codegenerror then
  1027. exit;
  1028. if (right.resulttype.def.deftype=classrefdef) then
  1029. begin
  1030. { left must be a class }
  1031. if is_class(left.resulttype.def) then
  1032. begin
  1033. { the operands must be related }
  1034. if (not(tobjectdef(left.resulttype.def).is_related(
  1035. tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def)))) and
  1036. (not(tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def).is_related(
  1037. tobjectdef(left.resulttype.def)))) then
  1038. CGMessage(type_e_mismatch);
  1039. end
  1040. else
  1041. CGMessage(type_e_mismatch);
  1042. end
  1043. else
  1044. CGMessage(type_e_mismatch);
  1045. resulttype:=booltype;
  1046. end;
  1047. function tisnode.pass_1 : tnode;
  1048. begin
  1049. result:=nil;
  1050. firstpass(left);
  1051. firstpass(right);
  1052. if codegenerror then
  1053. exit;
  1054. left_right_max;
  1055. location.loc:=LOC_FLAGS;
  1056. end;
  1057. {*****************************************************************************
  1058. TASNODE
  1059. *****************************************************************************}
  1060. constructor tasnode.create(l,r : tnode);
  1061. begin
  1062. inherited create(asn,l,r);
  1063. end;
  1064. function tasnode.det_resulttype:tnode;
  1065. begin
  1066. result:=nil;
  1067. resulttypepass(right);
  1068. resulttypepass(left);
  1069. set_varstate(right,true);
  1070. set_varstate(left,true);
  1071. if codegenerror then
  1072. exit;
  1073. if (right.resulttype.def.deftype=classrefdef) then
  1074. begin
  1075. { left must be a class }
  1076. if is_class(left.resulttype.def) then
  1077. begin
  1078. { the operands must be related }
  1079. if (not(tobjectdef(left.resulttype.def).is_related(
  1080. tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def)))) and
  1081. (not(tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def).is_related(
  1082. tobjectdef(left.resulttype.def)))) then
  1083. CGMessage(type_e_mismatch);
  1084. end
  1085. else
  1086. CGMessage(type_e_mismatch);
  1087. resulttype:=tclassrefdef(right.resulttype.def).pointertype;
  1088. end
  1089. else
  1090. CGMessage(type_e_mismatch);
  1091. end;
  1092. function tasnode.pass_1 : tnode;
  1093. begin
  1094. result:=nil;
  1095. firstpass(right);
  1096. firstpass(left);
  1097. if codegenerror then
  1098. exit;
  1099. left_right_max;
  1100. set_location(location,left.location);
  1101. end;
  1102. function ttypeconvnode.docompare(p: tnode) : boolean;
  1103. begin
  1104. docompare :=
  1105. inherited docompare(p) and
  1106. (convtype = ttypeconvnode(p).convtype);
  1107. end;
  1108. begin
  1109. ctypeconvnode:=ttypeconvnode;
  1110. casnode:=tasnode;
  1111. cisnode:=tisnode;
  1112. end.
  1113. {
  1114. $Log$
  1115. Revision 1.25 2001-04-13 22:20:58 peter
  1116. * remove wrongly placed first_call_helper
  1117. Revision 1.24 2001/04/13 01:22:08 peter
  1118. * symtable change to classes
  1119. * range check generation and errors fixed, make cycle DEBUG=1 works
  1120. * memory leaks fixed
  1121. Revision 1.23 2001/04/04 22:42:39 peter
  1122. * move constant folding into det_resulttype
  1123. Revision 1.22 2001/04/02 21:20:30 peter
  1124. * resulttype rewrite
  1125. Revision 1.21 2001/03/08 17:44:47 jonas
  1126. * fixed web bug 1430
  1127. Revision 1.20 2001/02/21 11:49:50 jonas
  1128. * evaluate typecasts of const pointers to ordinals inline ('merged')
  1129. Revision 1.19 2001/02/20 18:37:10 peter
  1130. * removed unused code
  1131. Revision 1.18 2001/02/20 13:14:18 marco
  1132. * Fix from Peter for passing a procedure of method to a other method in a method
  1133. Revision 1.17 2001/02/08 13:09:03 jonas
  1134. * fixed web bug 1396: tpointerord is now a cardinal instead of a longint,
  1135. but added a hack in ncnv so that pointer(-1) still works
  1136. Revision 1.16 2000/12/31 11:14:10 jonas
  1137. + implemented/fixed docompare() mathods for all nodes (not tested)
  1138. + nopt.pas, nadd.pas, i386/n386opt.pas: optimized nodes for adding strings
  1139. and constant strings/chars together
  1140. * n386add.pas: don't copy temp strings (of size 256) to another temp string
  1141. when adding
  1142. Revision 1.15 2000/12/08 12:41:01 jonas
  1143. * fixed bug in sign extension patch
  1144. Revision 1.14 2000/12/07 17:19:42 jonas
  1145. * new constant handling: from now on, hex constants >$7fffffff are
  1146. parsed as unsigned constants (otherwise, $80000000 got sign extended
  1147. and became $ffffffff80000000), all constants in the longint range
  1148. become longints, all constants >$7fffffff and <=cardinal($ffffffff)
  1149. are cardinals and the rest are int64's.
  1150. * added lots of longint typecast to prevent range check errors in the
  1151. compiler and rtl
  1152. * type casts of symbolic ordinal constants are now preserved
  1153. * fixed bug where the original resulttype.def wasn't restored correctly
  1154. after doing a 64bit rangecheck
  1155. Revision 1.13 2000/11/29 00:30:32 florian
  1156. * unused units removed from uses clause
  1157. * some changes for widestrings
  1158. Revision 1.12 2000/11/20 16:06:04 jonas
  1159. + allow evaluation of 64bit constant expressions at compile time
  1160. * disable range checking for explicit typecasts of constant expressions
  1161. Revision 1.11 2000/11/12 23:24:11 florian
  1162. * interfaces are basically running
  1163. Revision 1.10 2000/11/04 14:25:20 florian
  1164. + merged Attila's changes for interfaces, not tested yet
  1165. Revision 1.9 2000/10/31 22:02:48 peter
  1166. * symtable splitted, no real code changes
  1167. Revision 1.8 2000/10/14 21:52:55 peter
  1168. * fixed memory leaks
  1169. Revision 1.7 2000/10/14 10:14:50 peter
  1170. * moehrendorf oct 2000 rewrite
  1171. Revision 1.6 2000/10/01 19:48:24 peter
  1172. * lot of compile updates for cg11
  1173. Revision 1.5 2000/09/28 19:49:52 florian
  1174. *** empty log message ***
  1175. Revision 1.4 2000/09/27 18:14:31 florian
  1176. * fixed a lot of syntax errors in the n*.pas stuff
  1177. Revision 1.3 2000/09/26 20:06:13 florian
  1178. * hmm, still a lot of work to get things compilable
  1179. Revision 1.2 2000/09/26 14:59:34 florian
  1180. * more conversion work done
  1181. Revision 1.1 2000/09/25 15:37:14 florian
  1182. * more fixes
  1183. }