tccnv.pas 32 KB

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