tccnv.pas 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  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 (p^.resulttype^.deftype=orddef) and
  687. (porddef(p^.resulttype)^.typ=u8bit) and
  688. (p^.left^.resulttype^.deftype=orddef) and
  689. (porddef(p^.left^.resulttype)^.typ=bool8bit) then
  690. begin
  691. p^.convtyp:=tc_bool_2_int;
  692. firstconvert[p^.convtyp](p);
  693. exit;
  694. end;
  695. if is_pchar(p^.resulttype) and
  696. is_ansistring(p^.left^.resulttype) then
  697. begin
  698. p^.convtyp:=tc_ansistring_2_pchar;
  699. firstconvert[p^.convtyp](p);
  700. exit;
  701. end;
  702. { do common tc_equal cast }
  703. p^.convtyp:=tc_equal;
  704. { wenn Aufz„hltyp nach Ordinal konvertiert werden soll }
  705. { dann Aufz„hltyp=s32bit }
  706. if (p^.left^.resulttype^.deftype=enumdef) and
  707. is_ordinal(p^.resulttype) then
  708. begin
  709. if p^.left^.treetype=ordconstn then
  710. begin
  711. hp:=genordinalconstnode(p^.left^.value,p^.resulttype);
  712. disposetree(p);
  713. firstpass(hp);
  714. p:=hp;
  715. exit;
  716. end
  717. else
  718. begin
  719. if not isconvertable(s32bitdef,p^.resulttype,p^.convtyp,
  720. ordconstn { only Dummy},false ) then
  721. CGMessage(cg_e_illegal_type_conversion);
  722. end;
  723. end
  724. { ordinal to enumeration }
  725. else
  726. if (p^.resulttype^.deftype=enumdef) and
  727. is_ordinal(p^.left^.resulttype) then
  728. begin
  729. if p^.left^.treetype=ordconstn then
  730. begin
  731. hp:=genordinalconstnode(p^.left^.value,p^.resulttype);
  732. disposetree(p);
  733. firstpass(hp);
  734. p:=hp;
  735. exit;
  736. end
  737. else
  738. begin
  739. if not isconvertable(p^.left^.resulttype,s32bitdef,p^.convtyp,
  740. ordconstn { nur Dummy},false ) then
  741. CGMessage(cg_e_illegal_type_conversion);
  742. end;
  743. end
  744. {Are we typecasting an ordconst to a char?}
  745. else
  746. if is_equal(p^.resulttype,cchardef) and
  747. is_ordinal(p^.left^.resulttype) then
  748. begin
  749. if p^.left^.treetype=ordconstn then
  750. begin
  751. hp:=genordinalconstnode(p^.left^.value,p^.resulttype);
  752. firstpass(hp);
  753. disposetree(p);
  754. p:=hp;
  755. exit;
  756. end
  757. else
  758. begin
  759. { this is wrong because it converts to a 4 byte long var !!
  760. if not isconvertable(p^.left^.resulttype,s32bitdef,p^.convtyp,ordconstn nur Dummy ) then }
  761. if not isconvertable(p^.left^.resulttype,u8bitdef,
  762. p^.convtyp,ordconstn { nur Dummy},false ) then
  763. CGMessage(cg_e_illegal_type_conversion);
  764. end;
  765. end
  766. { only if the same size or formal def }
  767. { why do we allow typecasting of voiddef ?? (PM) }
  768. else
  769. if not(
  770. (p^.left^.resulttype^.deftype=formaldef) or
  771. (p^.left^.resulttype^.size=p^.resulttype^.size) or
  772. (is_equal(p^.left^.resulttype,voiddef) and
  773. (p^.left^.treetype=derefn))
  774. ) then
  775. CGMessage(cg_e_illegal_type_conversion);
  776. { the conversion into a strutured type is only }
  777. { possible, if the source is no register }
  778. if ((p^.resulttype^.deftype in [recorddef,stringdef,arraydef]) or
  779. ((p^.resulttype^.deftype=objectdef) and not(pobjectdef(p^.resulttype)^.isclass))
  780. ) and (p^.left^.location.loc in [LOC_REGISTER,LOC_CREGISTER]) and
  781. {it also works if the assignment is overloaded }
  782. not is_assignment_overloaded(p^.left^.resulttype,p^.resulttype) then
  783. CGMessage(cg_e_illegal_type_conversion);
  784. end
  785. else
  786. CGMessage(type_e_mismatch);
  787. end
  788. end
  789. else
  790. begin
  791. { ordinal contants can be directly converted }
  792. if (p^.left^.treetype=ordconstn) and is_ordinal(p^.resulttype) then
  793. begin
  794. { perform range checking }
  795. if not(p^.explizit and (m_tp in aktmodeswitches)) then
  796. testrange(p^.resulttype,p^.left^.value);
  797. hp:=genordinalconstnode(p^.left^.value,p^.resulttype);
  798. disposetree(p);
  799. firstpass(hp);
  800. p:=hp;
  801. exit;
  802. end;
  803. if p^.convtyp<>tc_equal then
  804. firstconvert[p^.convtyp](p);
  805. end;
  806. end;
  807. {*****************************************************************************
  808. FirstIs
  809. *****************************************************************************}
  810. procedure firstis(var p : ptree);
  811. begin
  812. firstpass(p^.left);
  813. firstpass(p^.right);
  814. if codegenerror then
  815. exit;
  816. if (p^.right^.resulttype^.deftype<>classrefdef) then
  817. CGMessage(type_e_mismatch);
  818. left_right_max(p);
  819. { left must be a class }
  820. if (p^.left^.resulttype^.deftype<>objectdef) or
  821. not(pobjectdef(p^.left^.resulttype)^.isclass) then
  822. CGMessage(type_e_mismatch);
  823. { the operands must be related }
  824. if (not(pobjectdef(p^.left^.resulttype)^.isrelated(
  825. pobjectdef(pclassrefdef(p^.right^.resulttype)^.definition)))) and
  826. (not(pobjectdef(pclassrefdef(p^.right^.resulttype)^.definition)^.isrelated(
  827. pobjectdef(p^.left^.resulttype)))) then
  828. CGMessage(type_e_mismatch);
  829. p^.location.loc:=LOC_FLAGS;
  830. p^.resulttype:=booldef;
  831. end;
  832. {*****************************************************************************
  833. FirstAs
  834. *****************************************************************************}
  835. procedure firstas(var p : ptree);
  836. begin
  837. firstpass(p^.right);
  838. firstpass(p^.left);
  839. if codegenerror then
  840. exit;
  841. if (p^.right^.resulttype^.deftype<>classrefdef) then
  842. CGMessage(type_e_mismatch);
  843. left_right_max(p);
  844. { left must be a class }
  845. if (p^.left^.resulttype^.deftype<>objectdef) or
  846. not(pobjectdef(p^.left^.resulttype)^.isclass) then
  847. CGMessage(type_e_mismatch);
  848. { the operands must be related }
  849. if (not(pobjectdef(p^.left^.resulttype)^.isrelated(
  850. pobjectdef(pclassrefdef(p^.right^.resulttype)^.definition)))) and
  851. (not(pobjectdef(pclassrefdef(p^.right^.resulttype)^.definition)^.isrelated(
  852. pobjectdef(p^.left^.resulttype)))) then
  853. CGMessage(type_e_mismatch);
  854. set_location(p^.location,p^.left^.location);
  855. p^.resulttype:=pclassrefdef(p^.right^.resulttype)^.definition;
  856. end;
  857. end.
  858. {
  859. $Log$
  860. Revision 1.12 1998-12-11 00:03:53 peter
  861. + globtype,tokens,version unit splitted from globals
  862. Revision 1.11 1998/12/04 10:18:12 florian
  863. * some stuff for procedures of object added
  864. * bug with overridden virtual constructors fixed (reported by Italo Gomes)
  865. Revision 1.10 1998/11/29 12:40:24 peter
  866. * newcnv -> not oldcnv
  867. Revision 1.9 1998/11/26 13:10:43 peter
  868. * new int - int conversion -dNEWCNV
  869. * some function renamings
  870. Revision 1.8 1998/11/05 12:03:03 peter
  871. * released useansistring
  872. * removed -Sv, its now available in fpc modes
  873. Revision 1.7 1998/10/23 11:58:27 florian
  874. * better code generation for s:=s+[b] if b is in the range of
  875. a small set and s is also a small set
  876. Revision 1.6 1998/10/21 15:12:58 pierre
  877. * bug fix for IOCHECK inside a procedure with iocheck modifier
  878. * removed the GPF for unexistant overloading
  879. (firstcall was called with procedinition=nil !)
  880. * changed typen to what Florian proposed
  881. gentypenode(p : pdef) sets the typenodetype field
  882. and resulttype is only set if inside bt_type block !
  883. Revision 1.5 1998/10/07 10:38:55 peter
  884. * forgot a firstpass in arrayconstruct2set
  885. Revision 1.4 1998/10/05 21:33:32 peter
  886. * fixed 161,165,166,167,168
  887. Revision 1.3 1998/09/27 10:16:26 florian
  888. * type casts pchar<->ansistring fixed
  889. * ansistring[..] calls does now an unique call
  890. Revision 1.2 1998/09/24 23:49:22 peter
  891. + aktmodeswitches
  892. Revision 1.1 1998/09/23 20:42:24 peter
  893. * splitted pass_1
  894. }