ncnv.pas 41 KB

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