tccnv.pas 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  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 (p^.left^.treetype=typen) and (p^.left^.resulttype=generrordef) then
  490. begin
  491. codegenerror:=true;
  492. Message(parser_e_no_type_not_allowed_here);
  493. end;
  494. if codegenerror then
  495. begin
  496. p^.resulttype:=generrordef;
  497. exit;
  498. end;
  499. if not assigned(p^.left^.resulttype) then
  500. begin
  501. codegenerror:=true;
  502. internalerror(52349);
  503. exit;
  504. end;
  505. { load the value_str from the left part }
  506. p^.registers32:=p^.left^.registers32;
  507. p^.registersfpu:=p^.left^.registersfpu;
  508. {$ifdef SUPPORT_MMX}
  509. p^.registersmmx:=p^.left^.registersmmx;
  510. {$endif}
  511. set_location(p^.location,p^.left^.location);
  512. { remove obsolete type conversions }
  513. if is_equal(p^.left^.resulttype,p^.resulttype) then
  514. begin
  515. { becuase is_equal only checks the basetype for sets we need to
  516. check here if we are loading a smallset into a normalset }
  517. if (p^.resulttype^.deftype=setdef) and
  518. (p^.left^.resulttype^.deftype=setdef) and
  519. (psetdef(p^.resulttype)^.settype<>smallset) and
  520. (psetdef(p^.left^.resulttype)^.settype=smallset) then
  521. begin
  522. { try to define the set as a normalset if it's a constant set }
  523. if p^.left^.treetype=setconstn then
  524. begin
  525. p^.resulttype:=p^.left^.resulttype;
  526. psetdef(p^.resulttype)^.settype:=normset
  527. end
  528. else
  529. p^.convtyp:=tc_load_smallset;
  530. exit;
  531. end
  532. else
  533. begin
  534. hp:=p;
  535. p:=p^.left;
  536. p^.resulttype:=hp^.resulttype;
  537. putnode(hp);
  538. exit;
  539. end;
  540. end;
  541. if is_assignment_overloaded(p^.left^.resulttype,p^.resulttype) then
  542. begin
  543. procinfo.flags:=procinfo.flags or pi_do_call;
  544. hp:=gencallnode(overloaded_operators[assignment],nil);
  545. hp^.left:=gencallparanode(p^.left,nil);
  546. putnode(p);
  547. p:=hp;
  548. firstpass(p);
  549. exit;
  550. end;
  551. if (not(isconvertable(p^.left^.resulttype,p^.resulttype,
  552. p^.convtyp,p^.left^.treetype,p^.explizit))) then
  553. begin
  554. {Procedures have a resulttype of voiddef and functions of their
  555. own resulttype. They will therefore always be incompatible with
  556. a procvar. Because isconvertable cannot check for procedures we
  557. use an extra check for them.}
  558. if (m_tp_procvar in aktmodeswitches) and
  559. ((is_procsym_load(p^.left) or is_procsym_call(p^.left)) and
  560. (p^.resulttype^.deftype=procvardef)) then
  561. begin
  562. { just a test: p^.explizit:=false; }
  563. if is_procsym_call(p^.left) then
  564. begin
  565. if p^.left^.right=nil then
  566. begin
  567. p^.left^.treetype:=loadn;
  568. { are at same offset so this could be spared, but
  569. it more secure to do it anyway }
  570. p^.left^.symtableentry:=p^.left^.symtableprocentry;
  571. p^.left^.resulttype:=pprocsym(p^.left^.symtableentry)^.definition;
  572. aprocdef:=pprocdef(p^.left^.resulttype);
  573. end
  574. else
  575. begin
  576. p^.left^.right^.treetype:=loadn;
  577. p^.left^.right^.symtableentry:=p^.left^.right^.symtableentry;
  578. P^.left^.right^.resulttype:=pvarsym(p^.left^.symtableentry)^.definition;
  579. hp:=p^.left^.right;
  580. putnode(p^.left);
  581. p^.left:=hp;
  582. { should we do that ? }
  583. firstpass(p^.left);
  584. if not is_equal(p^.left^.resulttype,p^.resulttype) then
  585. begin
  586. CGMessage(type_e_mismatch);
  587. exit;
  588. end
  589. else
  590. begin
  591. hp:=p;
  592. p:=p^.left;
  593. p^.resulttype:=hp^.resulttype;
  594. putnode(hp);
  595. exit;
  596. end;
  597. end;
  598. end
  599. else
  600. begin
  601. if p^.left^.treetype=addrn then
  602. begin
  603. hp:=p^.left;
  604. p^.left:=p^.left^.left;
  605. putnode(p^.left);
  606. end
  607. else
  608. aprocdef:=pprocsym(p^.left^.symtableentry)^.definition;
  609. end;
  610. p^.convtyp:=tc_proc2procvar;
  611. { Now check if the procedure we are going to assign to
  612. the procvar, is compatible with the procvar's type.
  613. Did the original procvar support do such a check?
  614. I can't find any.}
  615. { answer : is_equal works for procvardefs !! }
  616. { but both must be procvardefs, so we cheet little }
  617. if assigned(aprocdef) then
  618. begin
  619. proctype:=aprocdef^.deftype;
  620. aprocdef^.deftype:=procvardef;
  621. if not is_equal(aprocdef,p^.resulttype) then
  622. begin
  623. aprocdef^.deftype:=proctype;
  624. CGMessage(type_e_mismatch);
  625. end;
  626. aprocdef^.deftype:=proctype;
  627. firstconvert[p^.convtyp](p);
  628. end
  629. else
  630. CGMessage(type_e_mismatch);
  631. exit;
  632. end
  633. else
  634. begin
  635. if p^.explizit then
  636. begin
  637. { boolean to byte are special because the
  638. location can be different }
  639. if (p^.resulttype^.deftype=orddef) and
  640. (porddef(p^.resulttype)^.typ=u8bit) and
  641. (p^.left^.resulttype^.deftype=orddef) and
  642. (porddef(p^.left^.resulttype)^.typ=bool8bit) then
  643. begin
  644. p^.convtyp:=tc_bool_2_int;
  645. firstconvert[p^.convtyp](p);
  646. exit;
  647. end;
  648. if is_pchar(p^.resulttype) and
  649. is_ansistring(p^.left^.resulttype) then
  650. begin
  651. p^.convtyp:=tc_ansistring_2_pchar;
  652. firstconvert[p^.convtyp](p);
  653. exit;
  654. end;
  655. { normal tc_equal-Konvertierung durchf�hren }
  656. p^.convtyp:=tc_equal;
  657. { wenn Aufz„hltyp nach Ordinal konvertiert werden soll }
  658. { dann Aufz„hltyp=s32bit }
  659. if (p^.left^.resulttype^.deftype=enumdef) and
  660. is_ordinal(p^.resulttype) then
  661. begin
  662. if p^.left^.treetype=ordconstn then
  663. begin
  664. hp:=genordinalconstnode(p^.left^.value,p^.resulttype);
  665. disposetree(p);
  666. firstpass(hp);
  667. p:=hp;
  668. exit;
  669. end
  670. else
  671. begin
  672. if not isconvertable(s32bitdef,p^.resulttype,p^.convtyp,
  673. ordconstn { only Dummy},false ) then
  674. CGMessage(cg_e_illegal_type_conversion);
  675. end;
  676. end
  677. { ordinal to enumeration }
  678. else
  679. if (p^.resulttype^.deftype=enumdef) and
  680. is_ordinal(p^.left^.resulttype) then
  681. begin
  682. if p^.left^.treetype=ordconstn then
  683. begin
  684. hp:=genordinalconstnode(p^.left^.value,p^.resulttype);
  685. disposetree(p);
  686. firstpass(hp);
  687. p:=hp;
  688. exit;
  689. end
  690. else
  691. begin
  692. if not isconvertable(p^.left^.resulttype,s32bitdef,p^.convtyp,
  693. ordconstn { nur Dummy},false ) then
  694. CGMessage(cg_e_illegal_type_conversion);
  695. end;
  696. end
  697. {Are we typecasting an ordconst to a char?}
  698. else
  699. if is_equal(p^.resulttype,cchardef) and
  700. is_ordinal(p^.left^.resulttype) then
  701. begin
  702. if p^.left^.treetype=ordconstn then
  703. begin
  704. hp:=genordinalconstnode(p^.left^.value,p^.resulttype);
  705. firstpass(hp);
  706. disposetree(p);
  707. p:=hp;
  708. exit;
  709. end
  710. else
  711. begin
  712. { this is wrong because it converts to a 4 byte long var !!
  713. if not isconvertable(p^.left^.resulttype,s32bitdef,p^.convtyp,ordconstn nur Dummy ) then }
  714. if not isconvertable(p^.left^.resulttype,u8bitdef,
  715. p^.convtyp,ordconstn { nur Dummy},false ) then
  716. CGMessage(cg_e_illegal_type_conversion);
  717. end;
  718. end
  719. { only if the same size or formal def }
  720. { why do we allow typecasting of voiddef ?? (PM) }
  721. else
  722. if not(
  723. (p^.left^.resulttype^.deftype=formaldef) or
  724. (p^.left^.resulttype^.size=p^.resulttype^.size) or
  725. (is_equal(p^.left^.resulttype,voiddef) and
  726. (p^.left^.treetype=derefn))
  727. ) then
  728. CGMessage(cg_e_illegal_type_conversion);
  729. { the conversion into a strutured type is only }
  730. { possible, if the source is no register }
  731. if ((p^.resulttype^.deftype in [recorddef,stringdef,arraydef]) or
  732. ((p^.resulttype^.deftype=objectdef) and not(pobjectdef(p^.resulttype)^.isclass))
  733. ) and (p^.left^.location.loc in [LOC_REGISTER,LOC_CREGISTER]) and
  734. {it also works if the assignment is overloaded }
  735. not is_assignment_overloaded(p^.left^.resulttype,p^.resulttype) then
  736. CGMessage(cg_e_illegal_type_conversion);
  737. end
  738. else
  739. CGMessage(type_e_mismatch);
  740. end
  741. end
  742. else
  743. begin
  744. { ordinal contants can be directly converted }
  745. if (p^.left^.treetype=ordconstn) and is_ordinal(p^.resulttype) then
  746. begin
  747. { perform range checking }
  748. if not(p^.explizit and (m_tp in aktmodeswitches)) then
  749. testrange(p^.resulttype,p^.left^.value);
  750. hp:=genordinalconstnode(p^.left^.value,p^.resulttype);
  751. disposetree(p);
  752. firstpass(hp);
  753. p:=hp;
  754. exit;
  755. end;
  756. if p^.convtyp<>tc_equal then
  757. firstconvert[p^.convtyp](p);
  758. end;
  759. end;
  760. {*****************************************************************************
  761. FirstIs
  762. *****************************************************************************}
  763. procedure firstis(var p : ptree);
  764. begin
  765. firstpass(p^.left);
  766. firstpass(p^.right);
  767. if codegenerror then
  768. exit;
  769. if (p^.right^.resulttype^.deftype<>classrefdef) then
  770. CGMessage(type_e_mismatch);
  771. left_right_max(p);
  772. { left must be a class }
  773. if (p^.left^.resulttype^.deftype<>objectdef) or
  774. not(pobjectdef(p^.left^.resulttype)^.isclass) then
  775. CGMessage(type_e_mismatch);
  776. { the operands must be related }
  777. if (not(pobjectdef(p^.left^.resulttype)^.isrelated(
  778. pobjectdef(pclassrefdef(p^.right^.resulttype)^.definition)))) and
  779. (not(pobjectdef(pclassrefdef(p^.right^.resulttype)^.definition)^.isrelated(
  780. pobjectdef(p^.left^.resulttype)))) then
  781. CGMessage(type_e_mismatch);
  782. p^.location.loc:=LOC_FLAGS;
  783. p^.resulttype:=booldef;
  784. end;
  785. {*****************************************************************************
  786. FirstAs
  787. *****************************************************************************}
  788. procedure firstas(var p : ptree);
  789. begin
  790. firstpass(p^.right);
  791. firstpass(p^.left);
  792. if codegenerror then
  793. exit;
  794. if (p^.right^.resulttype^.deftype<>classrefdef) then
  795. CGMessage(type_e_mismatch);
  796. left_right_max(p);
  797. { left must be a class }
  798. if (p^.left^.resulttype^.deftype<>objectdef) or
  799. not(pobjectdef(p^.left^.resulttype)^.isclass) then
  800. CGMessage(type_e_mismatch);
  801. { the operands must be related }
  802. if (not(pobjectdef(p^.left^.resulttype)^.isrelated(
  803. pobjectdef(pclassrefdef(p^.right^.resulttype)^.definition)))) and
  804. (not(pobjectdef(pclassrefdef(p^.right^.resulttype)^.definition)^.isrelated(
  805. pobjectdef(p^.left^.resulttype)))) then
  806. CGMessage(type_e_mismatch);
  807. set_location(p^.location,p^.left^.location);
  808. p^.resulttype:=pclassrefdef(p^.right^.resulttype)^.definition;
  809. end;
  810. end.
  811. {
  812. $Log$
  813. Revision 1.6 1998-10-21 15:12:58 pierre
  814. * bug fix for IOCHECK inside a procedure with iocheck modifier
  815. * removed the GPF for unexistant overloading
  816. (firstcall was called with procedinition=nil !)
  817. * changed typen to what Florian proposed
  818. gentypenode(p : pdef) sets the typenodetype field
  819. and resulttype is only set if inside bt_type block !
  820. Revision 1.5 1998/10/07 10:38:55 peter
  821. * forgot a firstpass in arrayconstruct2set
  822. Revision 1.4 1998/10/05 21:33:32 peter
  823. * fixed 161,165,166,167,168
  824. Revision 1.3 1998/09/27 10:16:26 florian
  825. * type casts pchar<->ansistring fixed
  826. * ansistring[..] calls does now an unique call
  827. Revision 1.2 1998/09/24 23:49:22 peter
  828. + aktmodeswitches
  829. Revision 1.1 1998/09/23 20:42:24 peter
  830. * splitted pass_1
  831. }