tccnv.pas 34 KB

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