tccnv.pas 32 KB

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