tccnv.pas 35 KB

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