ncnv.pas 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  1. {
  2. $Id$
  3. Copyright (c) 2000 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. unit ncnv;
  19. {$i defines.inc}
  20. interface
  21. uses
  22. node;
  23. type
  24. ttypeconvnode = class(tunarynode)
  25. convtyp : tconverttype;
  26. constructor create(node : tnode;t : pdef);virtual;
  27. function getcopy : tnode;override;
  28. function pass_1 : tnode;override;
  29. end;
  30. tasnode = class(tbinarynode)
  31. constructor create(l,r : tnode);virtual;
  32. function pass_1 : tnode;override;
  33. end;
  34. tisnode = class(tbinarynode)
  35. constructor create(l,r : tnode);virtual;
  36. function pass_1 : tnode;override;
  37. end;
  38. var
  39. ctypeconvnode : class of ttypeconvnode;
  40. casnode : class of tasnode;
  41. cisnode : class of tisnode;
  42. function gentypeconvnode(node : tnode;t : pdef) : tnode;
  43. procedure arrayconstructor_to_set(var p:ptree);
  44. implementation
  45. uses
  46. globtype,systems,tokens,
  47. cutils,cobjects,verbose,globals,
  48. symconst,symtable,aasm,types,
  49. {$ifdef newcg}
  50. cgbase,
  51. {$else newcg}
  52. hcodegen,
  53. {$endif newcg}
  54. htypechk,pass_1,cpubase;
  55. {*****************************************************************************
  56. Array constructor to Set Conversion
  57. *****************************************************************************}
  58. procedure arrayconstructor_to_set(var p:ptree);
  59. var
  60. constp,
  61. buildp,
  62. p2,p3,p4 : ptree;
  63. pd : pdef;
  64. constset : pconstset;
  65. constsetlo,
  66. constsethi : longint;
  67. procedure update_constsethi(p:pdef);
  68. begin
  69. if ((p^.deftype=orddef) and
  70. (porddef(p)^.high>=constsethi)) then
  71. begin
  72. constsethi:=porddef(p)^.high;
  73. if pd=nil then
  74. begin
  75. if (constsethi>255) or
  76. (porddef(p)^.low<0) then
  77. pd:=u8bitdef
  78. else
  79. pd:=p;
  80. end;
  81. if constsethi>255 then
  82. constsethi:=255;
  83. end
  84. else if ((p^.deftype=enumdef) and
  85. (penumdef(p)^.max>=constsethi)) then
  86. begin
  87. if pd=nil then
  88. pd:=p;
  89. constsethi:=penumdef(p)^.max;
  90. end;
  91. end;
  92. procedure do_set(pos : longint);
  93. var
  94. mask,l : longint;
  95. begin
  96. if (pos>255) or (pos<0) then
  97. Message(parser_e_illegal_set_expr);
  98. if pos>constsethi then
  99. constsethi:=pos;
  100. if pos<constsetlo then
  101. constsetlo:=pos;
  102. l:=pos shr 3;
  103. mask:=1 shl (pos mod 8);
  104. { do we allow the same twice }
  105. if (constset^[l] and mask)<>0 then
  106. Message(parser_e_illegal_set_expr);
  107. constset^[l]:=constset^[l] or mask;
  108. end;
  109. var
  110. l : longint;
  111. lr,hr : longint;
  112. begin
  113. new(constset);
  114. FillChar(constset^,sizeof(constset^),0);
  115. pd:=nil;
  116. constsetlo:=0;
  117. constsethi:=0;
  118. constp:=gensinglenode(setconstn,nil);
  119. constp^.value_set:=constset;
  120. buildp:=constp;
  121. if assigned(p^.left) then
  122. begin
  123. while assigned(p) do
  124. begin
  125. p4:=nil; { will contain the tree to create the set }
  126. { split a range into p2 and p3 }
  127. if p^.left^.treetype=arrayconstructrangen then
  128. begin
  129. p2:=p^.left^.left;
  130. p3:=p^.left^.right;
  131. { node is not used anymore }
  132. putnode(p^.left);
  133. end
  134. else
  135. begin
  136. p2:=p^.left;
  137. p3:=nil;
  138. end;
  139. firstpass(p2);
  140. if assigned(p3) then
  141. firstpass(p3);
  142. if codegenerror then
  143. break;
  144. case p2^.resulttype^.deftype of
  145. enumdef,
  146. orddef:
  147. begin
  148. getrange(p2^.resulttype,lr,hr);
  149. if assigned(p3) then
  150. begin
  151. { this isn't good, you'll get problems with
  152. type t010 = 0..10;
  153. ts = set of t010;
  154. var s : ts;b : t010
  155. begin s:=[1,2,b]; end.
  156. if is_integer(p3^.resulttype) then
  157. begin
  158. p3:=gentypeconvnode(p3,u8bitdef);
  159. firstpass(p3);
  160. end;
  161. }
  162. if assigned(pd) and not(is_equal(pd,p3^.resulttype)) then
  163. begin
  164. aktfilepos:=p3^.fileinfo;
  165. CGMessage(type_e_typeconflict_in_set);
  166. end
  167. else
  168. begin
  169. if (p2^.treetype=ordconstn) and (p3^.treetype=ordconstn) then
  170. begin
  171. if not(is_integer(p3^.resulttype)) then
  172. pd:=p3^.resulttype
  173. else
  174. begin
  175. p3:=gentypeconvnode(p3,u8bitdef);
  176. p2:=gentypeconvnode(p2,u8bitdef);
  177. firstpass(p2);
  178. firstpass(p3);
  179. end;
  180. for l:=p2^.value to p3^.value do
  181. do_set(l);
  182. disposetree(p3);
  183. disposetree(p2);
  184. end
  185. else
  186. begin
  187. update_constsethi(p2^.resulttype);
  188. p2:=gentypeconvnode(p2,pd);
  189. firstpass(p2);
  190. update_constsethi(p3^.resulttype);
  191. p3:=gentypeconvnode(p3,pd);
  192. firstpass(p3);
  193. if assigned(pd) then
  194. p3:=gentypeconvnode(p3,pd)
  195. else
  196. p3:=gentypeconvnode(p3,u8bitdef);
  197. firstpass(p3);
  198. p4:=gennode(setelementn,p2,p3);
  199. end;
  200. end;
  201. end
  202. else
  203. begin
  204. { Single value }
  205. if p2^.treetype=ordconstn then
  206. begin
  207. if not(is_integer(p2^.resulttype)) then
  208. update_constsethi(p2^.resulttype)
  209. else
  210. begin
  211. p2:=gentypeconvnode(p2,u8bitdef);
  212. firstpass(p2);
  213. end;
  214. do_set(p2^.value);
  215. disposetree(p2);
  216. end
  217. else
  218. begin
  219. update_constsethi(p2^.resulttype);
  220. if assigned(pd) then
  221. p2:=gentypeconvnode(p2,pd)
  222. else
  223. p2:=gentypeconvnode(p2,u8bitdef);
  224. firstpass(p2);
  225. p4:=gennode(setelementn,p2,nil);
  226. end;
  227. end;
  228. end;
  229. stringdef : begin
  230. { if we've already set elements which are constants }
  231. { throw an error }
  232. if ((pd=nil) and assigned(buildp)) or
  233. not(is_equal(pd,cchardef)) then
  234. CGMessage(type_e_typeconflict_in_set)
  235. else
  236. for l:=1 to length(pstring(p2^.value_str)^) do
  237. do_set(ord(pstring(p2^.value_str)^[l]));
  238. if pd=nil then
  239. pd:=cchardef;
  240. disposetree(p2);
  241. end;
  242. else
  243. CGMessage(type_e_ordinal_expr_expected);
  244. end;
  245. { insert the set creation tree }
  246. if assigned(p4) then
  247. buildp:=gennode(addn,buildp,p4);
  248. { load next and dispose current node }
  249. p2:=p;
  250. p:=p^.right;
  251. putnode(p2);
  252. end;
  253. if (pd=nil) then
  254. begin
  255. pd:=u8bitdef;
  256. constsethi:=255;
  257. end;
  258. end
  259. else
  260. begin
  261. { empty set [], only remove node }
  262. putnode(p);
  263. end;
  264. { set the initial set type }
  265. constp^.resulttype:=new(psetdef,init(pd,constsethi));
  266. { set the new tree }
  267. p:=buildp;
  268. end;
  269. {*****************************************************************************
  270. TTYPECONVNODE
  271. *****************************************************************************}
  272. type
  273. tfirstconvproc = procedure(var p : ptree);
  274. procedure first_int_to_int(var p : ptree);
  275. begin
  276. if (p^.left^.location.loc<>LOC_REGISTER) and
  277. (p^.resulttype^.size>p^.left^.resulttype^.size) then
  278. p^.location.loc:=LOC_REGISTER;
  279. if is_64bitint(p^.resulttype) then
  280. p^.registers32:=max(p^.registers32,2)
  281. else
  282. p^.registers32:=max(p^.registers32,1);
  283. end;
  284. procedure first_cstring_to_pchar(var p : ptree);
  285. begin
  286. p^.registers32:=1;
  287. p^.location.loc:=LOC_REGISTER;
  288. end;
  289. procedure first_string_to_chararray(var p : ptree);
  290. begin
  291. p^.registers32:=1;
  292. p^.location.loc:=LOC_REGISTER;
  293. end;
  294. procedure first_string_to_string(var p : ptree);
  295. var
  296. hp : ptree;
  297. begin
  298. if pstringdef(p^.resulttype)^.string_typ<>
  299. pstringdef(p^.left^.resulttype)^.string_typ then
  300. begin
  301. if p^.left^.treetype=stringconstn then
  302. begin
  303. p^.left^.stringtype:=pstringdef(p^.resulttype)^.string_typ;
  304. p^.left^.resulttype:=p^.resulttype;
  305. { remove typeconv node }
  306. hp:=p;
  307. p:=p^.left;
  308. putnode(hp);
  309. exit;
  310. end
  311. else
  312. procinfo^.flags:=procinfo^.flags or pi_do_call;
  313. end;
  314. { for simplicity lets first keep all ansistrings
  315. as LOC_MEM, could also become LOC_REGISTER }
  316. if pstringdef(p^.resulttype)^.string_typ in [st_ansistring,st_widestring] then
  317. { we may use ansistrings so no fast exit here }
  318. procinfo^.no_fast_exit:=true;
  319. p^.location.loc:=LOC_MEM;
  320. end;
  321. procedure first_char_to_string(var p : ptree);
  322. var
  323. hp : ptree;
  324. begin
  325. if p^.left^.treetype=ordconstn then
  326. begin
  327. hp:=genstringconstnode(chr(p^.left^.value),st_default);
  328. hp^.stringtype:=pstringdef(p^.resulttype)^.string_typ;
  329. firstpass(hp);
  330. disposetree(p);
  331. p:=hp;
  332. end
  333. else
  334. p^.location.loc:=LOC_MEM;
  335. end;
  336. procedure first_nothing(var p : ptree);
  337. begin
  338. p^.location.loc:=LOC_MEM;
  339. end;
  340. procedure first_array_to_pointer(var p : ptree);
  341. begin
  342. if p^.registers32<1 then
  343. p^.registers32:=1;
  344. p^.location.loc:=LOC_REGISTER;
  345. end;
  346. procedure first_int_to_real(var p : ptree);
  347. var
  348. t : ptree;
  349. begin
  350. if p^.left^.treetype=ordconstn then
  351. begin
  352. t:=genrealconstnode(p^.left^.value,pfloatdef(p^.resulttype));
  353. firstpass(t);
  354. disposetree(p);
  355. p:=t;
  356. exit;
  357. end;
  358. if p^.registersfpu<1 then
  359. p^.registersfpu:=1;
  360. p^.location.loc:=LOC_FPU;
  361. end;
  362. procedure first_int_to_fix(var p : ptree);
  363. var
  364. t : ptree;
  365. begin
  366. if p^.left^.treetype=ordconstn then
  367. begin
  368. t:=genfixconstnode(p^.left^.value shl 16,p^.resulttype);
  369. firstpass(t);
  370. disposetree(p);
  371. p:=t;
  372. exit;
  373. end;
  374. if p^.registers32<1 then
  375. p^.registers32:=1;
  376. p^.location.loc:=LOC_REGISTER;
  377. end;
  378. procedure first_real_to_fix(var p : ptree);
  379. var
  380. t : ptree;
  381. begin
  382. if p^.left^.treetype=fixconstn then
  383. begin
  384. t:=genfixconstnode(round(p^.left^.value_real*65536),p^.resulttype);
  385. firstpass(t);
  386. disposetree(p);
  387. p:=t;
  388. exit;
  389. end;
  390. { at least one fpu and int register needed }
  391. if p^.registers32<1 then
  392. p^.registers32:=1;
  393. if p^.registersfpu<1 then
  394. p^.registersfpu:=1;
  395. p^.location.loc:=LOC_REGISTER;
  396. end;
  397. procedure first_fix_to_real(var p : ptree);
  398. var
  399. t : ptree;
  400. begin
  401. if p^.left^.treetype=fixconstn then
  402. begin
  403. t:=genrealconstnode(round(p^.left^.value_fix/65536.0),p^.resulttype);
  404. firstpass(t);
  405. disposetree(p);
  406. p:=t;
  407. exit;
  408. end;
  409. if p^.registersfpu<1 then
  410. p^.registersfpu:=1;
  411. p^.location.loc:=LOC_FPU;
  412. end;
  413. procedure first_real_to_real(var p : ptree);
  414. var
  415. t : ptree;
  416. begin
  417. if p^.left^.treetype=realconstn then
  418. begin
  419. t:=genrealconstnode(p^.left^.value_real,p^.resulttype);
  420. firstpass(t);
  421. disposetree(p);
  422. p:=t;
  423. exit;
  424. end;
  425. { comp isn't a floating type }
  426. {$ifdef i386}
  427. if (pfloatdef(p^.resulttype)^.typ=s64comp) and
  428. (pfloatdef(p^.left^.resulttype)^.typ<>s64comp) and
  429. not (p^.explizit) then
  430. CGMessage(type_w_convert_real_2_comp);
  431. {$endif}
  432. if p^.registersfpu<1 then
  433. p^.registersfpu:=1;
  434. p^.location.loc:=LOC_FPU;
  435. end;
  436. procedure first_pointer_to_array(var p : ptree);
  437. begin
  438. if p^.registers32<1 then
  439. p^.registers32:=1;
  440. p^.location.loc:=LOC_REFERENCE;
  441. end;
  442. procedure first_chararray_to_string(var p : ptree);
  443. begin
  444. { the only important information is the location of the }
  445. { result }
  446. { other stuff is done by firsttypeconv }
  447. p^.location.loc:=LOC_MEM;
  448. end;
  449. procedure first_cchar_to_pchar(var p : ptree);
  450. begin
  451. p^.left:=gentypeconvnode(p^.left,cshortstringdef);
  452. { convert constant char to constant string }
  453. firstpass(p^.left);
  454. { evalute tree }
  455. firstpass(p);
  456. end;
  457. procedure first_bool_to_int(var p : ptree);
  458. begin
  459. { byte(boolean) or word(wordbool) or longint(longbool) must
  460. be accepted for var parameters }
  461. if (p^.explizit) and
  462. (p^.left^.resulttype^.size=p^.resulttype^.size) and
  463. (p^.left^.location.loc in [LOC_REFERENCE,LOC_MEM,LOC_CREGISTER]) then
  464. exit;
  465. p^.location.loc:=LOC_REGISTER;
  466. if p^.registers32<1 then
  467. p^.registers32:=1;
  468. end;
  469. procedure first_int_to_bool(var p : ptree);
  470. begin
  471. { byte(boolean) or word(wordbool) or longint(longbool) must
  472. be accepted for var parameters }
  473. if (p^.explizit) and
  474. (p^.left^.resulttype^.size=p^.resulttype^.size) and
  475. (p^.left^.location.loc in [LOC_REFERENCE,LOC_MEM,LOC_CREGISTER]) then
  476. exit;
  477. p^.location.loc:=LOC_REGISTER;
  478. { need if bool to bool !!
  479. not very nice !!
  480. p^.left:=gentypeconvnode(p^.left,s32bitdef);
  481. p^.left^.explizit:=true;
  482. firstpass(p^.left); }
  483. if p^.registers32<1 then
  484. p^.registers32:=1;
  485. end;
  486. procedure first_bool_to_bool(var p : ptree);
  487. begin
  488. p^.location.loc:=LOC_REGISTER;
  489. if p^.registers32<1 then
  490. p^.registers32:=1;
  491. end;
  492. procedure first_proc_to_procvar(var p : ptree);
  493. begin
  494. { hmmm, I'am not sure if that is necessary (FK) }
  495. firstpass(p^.left);
  496. if codegenerror then
  497. exit;
  498. if (p^.left^.location.loc<>LOC_REFERENCE) then
  499. CGMessage(cg_e_illegal_expression);
  500. p^.registers32:=p^.left^.registers32;
  501. if p^.registers32<1 then
  502. p^.registers32:=1;
  503. p^.location.loc:=LOC_REGISTER;
  504. end;
  505. procedure first_load_smallset(var p : ptree);
  506. begin
  507. end;
  508. procedure first_cord_to_pointer(var p : ptree);
  509. var
  510. t : ptree;
  511. begin
  512. if p^.left^.treetype=ordconstn then
  513. begin
  514. t:=genpointerconstnode(p^.left^.value,p^.resulttype);
  515. firstpass(t);
  516. disposetree(p);
  517. p:=t;
  518. exit;
  519. end
  520. else
  521. internalerror(432472389);
  522. end;
  523. procedure first_pchar_to_string(var p : ptree);
  524. begin
  525. p^.location.loc:=LOC_REFERENCE;
  526. end;
  527. procedure first_ansistring_to_pchar(var p : ptree);
  528. begin
  529. p^.location.loc:=LOC_REGISTER;
  530. if p^.registers32<1 then
  531. p^.registers32:=1;
  532. end;
  533. procedure first_arrayconstructor_to_set(var p:ptree);
  534. var
  535. hp : ptree;
  536. begin
  537. if p^.left^.treetype<>arrayconstructn then
  538. internalerror(5546);
  539. { remove typeconv node }
  540. hp:=p;
  541. p:=p^.left;
  542. putnode(hp);
  543. { create a set constructor tree }
  544. arrayconstructor_to_set(p);
  545. { now firstpass the set }
  546. firstpass(p);
  547. end;
  548. procedure firsttypeconv(var p : ptree);
  549. var
  550. hp : ptree;
  551. aprocdef : pprocdef;
  552. const
  553. firstconvert : array[tconverttype] of tfirstconvproc = (
  554. first_nothing, {equal}
  555. first_nothing, {not_possible}
  556. first_string_to_string,
  557. first_char_to_string,
  558. first_pchar_to_string,
  559. first_cchar_to_pchar,
  560. first_cstring_to_pchar,
  561. first_ansistring_to_pchar,
  562. first_string_to_chararray,
  563. first_chararray_to_string,
  564. first_array_to_pointer,
  565. first_pointer_to_array,
  566. first_int_to_int,
  567. first_int_to_bool,
  568. first_bool_to_bool,
  569. first_bool_to_int,
  570. first_real_to_real,
  571. first_int_to_real,
  572. first_int_to_fix,
  573. first_real_to_fix,
  574. first_fix_to_real,
  575. first_proc_to_procvar,
  576. first_arrayconstructor_to_set,
  577. first_load_smallset,
  578. first_cord_to_pointer
  579. );
  580. begin
  581. aprocdef:=nil;
  582. { if explicite type cast, then run firstpass }
  583. if (p^.explizit) or not assigned(p^.left^.resulttype) then
  584. firstpass(p^.left);
  585. if (p^.left^.treetype=typen) and (p^.left^.resulttype=generrordef) then
  586. begin
  587. codegenerror:=true;
  588. Message(parser_e_no_type_not_allowed_here);
  589. end;
  590. if codegenerror then
  591. begin
  592. p^.resulttype:=generrordef;
  593. exit;
  594. end;
  595. if not assigned(p^.left^.resulttype) then
  596. begin
  597. codegenerror:=true;
  598. internalerror(52349);
  599. exit;
  600. end;
  601. { load the value_str from the left part }
  602. p^.registers32:=p^.left^.registers32;
  603. p^.registersfpu:=p^.left^.registersfpu;
  604. {$ifdef SUPPORT_MMX}
  605. p^.registersmmx:=p^.left^.registersmmx;
  606. {$endif}
  607. set_location(p^.location,p^.left^.location);
  608. { remove obsolete type conversions }
  609. if is_equal(p^.left^.resulttype,p^.resulttype) then
  610. begin
  611. { becuase is_equal only checks the basetype for sets we need to
  612. check here if we are loading a smallset into a normalset }
  613. if (p^.resulttype^.deftype=setdef) and
  614. (p^.left^.resulttype^.deftype=setdef) and
  615. (psetdef(p^.resulttype)^.settype<>smallset) and
  616. (psetdef(p^.left^.resulttype)^.settype=smallset) then
  617. begin
  618. { try to define the set as a normalset if it's a constant set }
  619. if p^.left^.treetype=setconstn then
  620. begin
  621. p^.resulttype:=p^.left^.resulttype;
  622. psetdef(p^.resulttype)^.settype:=normset
  623. end
  624. else
  625. p^.convtyp:=tc_load_smallset;
  626. exit;
  627. end
  628. else
  629. begin
  630. hp:=p;
  631. p:=p^.left;
  632. p^.resulttype:=hp^.resulttype;
  633. putnode(hp);
  634. exit;
  635. end;
  636. end;
  637. aprocdef:=assignment_overloaded(p^.left^.resulttype,p^.resulttype);
  638. if assigned(aprocdef) then
  639. begin
  640. procinfo^.flags:=procinfo^.flags or pi_do_call;
  641. hp:=gencallnode(overloaded_operators[_assignment],nil);
  642. { tell explicitly which def we must use !! (PM) }
  643. hp^.procdefinition:=aprocdef;
  644. hp^.left:=gencallparanode(p^.left,nil);
  645. putnode(p);
  646. p:=hp;
  647. firstpass(p);
  648. exit;
  649. end;
  650. if isconvertable(p^.left^.resulttype,p^.resulttype,p^.convtyp,p^.left^.treetype,p^.explizit)=0 then
  651. begin
  652. {Procedures have a resulttype of voiddef and functions of their
  653. own resulttype. They will therefore always be incompatible with
  654. a procvar. Because isconvertable cannot check for procedures we
  655. use an extra check for them.}
  656. if (m_tp_procvar in aktmodeswitches) then
  657. begin
  658. if (p^.resulttype^.deftype=procvardef) and
  659. (is_procsym_load(p^.left) or is_procsym_call(p^.left)) then
  660. begin
  661. if is_procsym_call(p^.left) then
  662. begin
  663. {if p^.left^.right=nil then
  664. begin}
  665. if (p^.left^.symtableprocentry^.owner^.symtabletype=objectsymtable){ and
  666. (pobjectdef(p^.left^.symtableprocentry^.owner^.defowner)^.is_class) }then
  667. hp:=genloadmethodcallnode(pprocsym(p^.left^.symtableprocentry),p^.left^.symtableproc,
  668. getcopy(p^.left^.methodpointer))
  669. else
  670. hp:=genloadcallnode(pprocsym(p^.left^.symtableprocentry),p^.left^.symtableproc);
  671. disposetree(p^.left);
  672. firstpass(hp);
  673. p^.left:=hp;
  674. aprocdef:=pprocdef(p^.left^.resulttype);
  675. (* end
  676. else
  677. begin
  678. p^.left^.right^.treetype:=loadn;
  679. p^.left^.right^.symtableentry:=p^.left^.right^.symtableentry;
  680. P^.left^.right^.resulttype:=pvarsym(p^.left^.symtableentry)^.definition;
  681. hp:=p^.left^.right;
  682. putnode(p^.left);
  683. p^.left:=hp;
  684. { should we do that ? }
  685. firstpass(p^.left);
  686. if not is_equal(p^.left^.resulttype,p^.resulttype) then
  687. begin
  688. CGMessage(type_e_mismatch);
  689. exit;
  690. end
  691. else
  692. begin
  693. hp:=p;
  694. p:=p^.left;
  695. p^.resulttype:=hp^.resulttype;
  696. putnode(hp);
  697. exit;
  698. end;
  699. end; *)
  700. end
  701. else
  702. begin
  703. if (p^.left^.treetype<>addrn) then
  704. aprocdef:=pprocsym(p^.left^.symtableentry)^.definition;
  705. end;
  706. p^.convtyp:=tc_proc_2_procvar;
  707. { Now check if the procedure we are going to assign to
  708. the procvar, is compatible with the procvar's type }
  709. if assigned(aprocdef) then
  710. begin
  711. if not proc_to_procvar_equal(aprocdef,pprocvardef(p^.resulttype)) then
  712. CGMessage2(type_e_incompatible_types,aprocdef^.typename,p^.resulttype^.typename);
  713. firstconvert[p^.convtyp](p);
  714. end
  715. else
  716. CGMessage2(type_e_incompatible_types,p^.left^.resulttype^.typename,p^.resulttype^.typename);
  717. exit;
  718. end;
  719. end;
  720. if p^.explizit then
  721. begin
  722. { check if the result could be in a register }
  723. if not(p^.resulttype^.is_intregable) and
  724. not(p^.resulttype^.is_fpuregable) then
  725. make_not_regable(p^.left);
  726. { boolean to byte are special because the
  727. location can be different }
  728. if is_integer(p^.resulttype) and
  729. is_boolean(p^.left^.resulttype) then
  730. begin
  731. p^.convtyp:=tc_bool_2_int;
  732. firstconvert[p^.convtyp](p);
  733. exit;
  734. end;
  735. { ansistring to pchar }
  736. if is_pchar(p^.resulttype) and
  737. is_ansistring(p^.left^.resulttype) then
  738. begin
  739. p^.convtyp:=tc_ansistring_2_pchar;
  740. firstconvert[p^.convtyp](p);
  741. exit;
  742. end;
  743. { do common tc_equal cast }
  744. p^.convtyp:=tc_equal;
  745. { enum to ordinal will always be s32bit }
  746. if (p^.left^.resulttype^.deftype=enumdef) and
  747. is_ordinal(p^.resulttype) then
  748. begin
  749. if p^.left^.treetype=ordconstn then
  750. begin
  751. hp:=genordinalconstnode(p^.left^.value,p^.resulttype);
  752. disposetree(p);
  753. firstpass(hp);
  754. p:=hp;
  755. exit;
  756. end
  757. else
  758. begin
  759. if isconvertable(s32bitdef,p^.resulttype,p^.convtyp,ordconstn,false)=0 then
  760. CGMessage2(type_e_incompatible_types,p^.left^.resulttype^.typename,p^.resulttype^.typename);
  761. end;
  762. end
  763. { ordinal to enumeration }
  764. else
  765. if (p^.resulttype^.deftype=enumdef) and
  766. is_ordinal(p^.left^.resulttype) then
  767. begin
  768. if p^.left^.treetype=ordconstn then
  769. begin
  770. hp:=genordinalconstnode(p^.left^.value,p^.resulttype);
  771. disposetree(p);
  772. firstpass(hp);
  773. p:=hp;
  774. exit;
  775. end
  776. else
  777. begin
  778. if IsConvertable(p^.left^.resulttype,s32bitdef,p^.convtyp,ordconstn,false)=0 then
  779. CGMessage2(type_e_incompatible_types,p^.left^.resulttype^.typename,p^.resulttype^.typename);
  780. end;
  781. end
  782. { nil to ordinal node }
  783. else if is_ordinal(p^.resulttype) and
  784. (p^.left^.treetype=niln) then
  785. begin
  786. hp:=genordinalconstnode(0,p^.resulttype);
  787. firstpass(hp);
  788. disposetree(p);
  789. p:=hp;
  790. exit;
  791. end
  792. {Are we typecasting an ordconst to a char?}
  793. else
  794. if is_char(p^.resulttype) and
  795. is_ordinal(p^.left^.resulttype) then
  796. begin
  797. if p^.left^.treetype=ordconstn then
  798. begin
  799. hp:=genordinalconstnode(p^.left^.value,p^.resulttype);
  800. firstpass(hp);
  801. disposetree(p);
  802. p:=hp;
  803. exit;
  804. end
  805. else
  806. begin
  807. if IsConvertable(p^.left^.resulttype,u8bitdef,p^.convtyp,ordconstn,false)=0 then
  808. CGMessage2(type_e_incompatible_types,p^.left^.resulttype^.typename,p^.resulttype^.typename);
  809. end;
  810. end
  811. { Are we char to ordinal }
  812. else
  813. if is_char(p^.left^.resulttype) and
  814. is_ordinal(p^.resulttype) then
  815. begin
  816. if p^.left^.treetype=ordconstn then
  817. begin
  818. hp:=genordinalconstnode(p^.left^.value,p^.resulttype);
  819. firstpass(hp);
  820. disposetree(p);
  821. p:=hp;
  822. exit;
  823. end
  824. else
  825. begin
  826. if IsConvertable(u8bitdef,p^.resulttype,p^.convtyp,ordconstn,false)=0 then
  827. CGMessage2(type_e_incompatible_types,p^.left^.resulttype^.typename,p^.resulttype^.typename);
  828. end;
  829. end
  830. { only if the same size or formal def }
  831. { why do we allow typecasting of voiddef ?? (PM) }
  832. else
  833. begin
  834. if not(
  835. (p^.left^.resulttype^.deftype=formaldef) or
  836. (p^.left^.resulttype^.size=p^.resulttype^.size) or
  837. (is_equal(p^.left^.resulttype,voiddef) and
  838. (p^.left^.treetype=derefn))
  839. ) then
  840. CGMessage(cg_e_illegal_type_conversion);
  841. if ((p^.left^.resulttype^.deftype=orddef) and
  842. (p^.resulttype^.deftype=pointerdef)) or
  843. ((p^.resulttype^.deftype=orddef) and
  844. (p^.left^.resulttype^.deftype=pointerdef))
  845. {$ifdef extdebug}and (p^.firstpasscount=0){$endif} then
  846. CGMessage(cg_d_pointer_to_longint_conv_not_portable);
  847. end;
  848. { the conversion into a strutured type is only }
  849. { possible, if the source is no register }
  850. if ((p^.resulttype^.deftype in [recorddef,stringdef,arraydef]) or
  851. ((p^.resulttype^.deftype=objectdef) and not(pobjectdef(p^.resulttype)^.is_class))
  852. ) and (p^.left^.location.loc in [LOC_REGISTER,LOC_CREGISTER]) { and
  853. it also works if the assignment is overloaded
  854. YES but this code is not executed if assignment is overloaded (PM)
  855. not assigned(assignment_overloaded(p^.left^.resulttype,p^.resulttype))} then
  856. CGMessage(cg_e_illegal_type_conversion);
  857. end
  858. else
  859. CGMessage2(type_e_incompatible_types,p^.left^.resulttype^.typename,p^.resulttype^.typename);
  860. end;
  861. { tp7 procvar support, when right is not a procvardef and we got a
  862. loadn of a procvar then convert to a calln, the check for the
  863. result is already done in is_convertible, also no conflict with
  864. @procvar is here because that has an extra addrn }
  865. if (m_tp_procvar in aktmodeswitches) and
  866. (p^.resulttype^.deftype<>procvardef) and
  867. (p^.left^.resulttype^.deftype=procvardef) and
  868. (p^.left^.treetype=loadn) then
  869. begin
  870. hp:=gencallnode(nil,nil);
  871. hp^.right:=p^.left;
  872. firstpass(hp);
  873. p^.left:=hp;
  874. end;
  875. { ordinal contants can be directly converted }
  876. { but not int64/qword }
  877. if (p^.left^.treetype=ordconstn) and is_ordinal(p^.resulttype) and
  878. not(is_64bitint(p^.resulttype)) then
  879. begin
  880. { range checking is done in genordinalconstnode (PFV) }
  881. hp:=genordinalconstnode(p^.left^.value,p^.resulttype);
  882. disposetree(p);
  883. firstpass(hp);
  884. p:=hp;
  885. exit;
  886. end;
  887. if p^.convtyp<>tc_equal then
  888. firstconvert[p^.convtyp](p);
  889. end;
  890. {*****************************************************************************
  891. TISNODE
  892. *****************************************************************************}
  893. constructor tisnode.create(l,r : tnode);
  894. begin
  895. inherited create(isn,l,r);
  896. end;
  897. function tisnode.pass_1 : tnode;
  898. begin
  899. pass_1:=nil;
  900. firstpass(p^.left);
  901. set_varstate(p^.left,true);
  902. firstpass(p^.right);
  903. set_varstate(p^.right,true);
  904. if codegenerror then
  905. exit;
  906. if (p^.right^.resulttype^.deftype<>classrefdef) then
  907. CGMessage(type_e_mismatch);
  908. left_right_max(p);
  909. { left must be a class }
  910. if (p^.left^.resulttype^.deftype<>objectdef) or
  911. not(pobjectdef(p^.left^.resulttype)^.is_class) then
  912. CGMessage(type_e_mismatch);
  913. { the operands must be related }
  914. if (not(pobjectdef(p^.left^.resulttype)^.is_related(
  915. pobjectdef(pclassrefdef(p^.right^.resulttype)^.pointertype.def)))) and
  916. (not(pobjectdef(pclassrefdef(p^.right^.resulttype)^.pointertype.def)^.is_related(
  917. pobjectdef(p^.left^.resulttype)))) then
  918. CGMessage(type_e_mismatch);
  919. p^.location.loc:=LOC_FLAGS;
  920. p^.resulttype:=booldef;
  921. end;
  922. {*****************************************************************************
  923. TASNODE
  924. *****************************************************************************}
  925. constructor tasnode.create(l,r : tnode);
  926. begin
  927. inherited create(asn,l,r);
  928. end;
  929. function tasnode.pass_1 : tnode;
  930. begin
  931. pass_1:=nil;
  932. firstpass(p^.right);
  933. set_varstate(p^.right,true);
  934. firstpass(p^.left);
  935. set_varstate(p^.left,true);
  936. if codegenerror then
  937. exit;
  938. if (p^.right^.resulttype^.deftype<>classrefdef) then
  939. CGMessage(type_e_mismatch);
  940. left_right_max(p);
  941. { left must be a class }
  942. if (p^.left^.resulttype^.deftype<>objectdef) or
  943. not(pobjectdef(p^.left^.resulttype)^.is_class) then
  944. CGMessage(type_e_mismatch);
  945. { the operands must be related }
  946. if (not(pobjectdef(p^.left^.resulttype)^.is_related(
  947. pobjectdef(pclassrefdef(p^.right^.resulttype)^.pointertype.def)))) and
  948. (not(pobjectdef(pclassrefdef(p^.right^.resulttype)^.pointertype.def)^.is_related(
  949. pobjectdef(p^.left^.resulttype)))) then
  950. CGMessage(type_e_mismatch);
  951. set_location(p^.location,p^.left^.location);
  952. p^.resulttype:=pclassrefdef(p^.right^.resulttype)^.pointertype.def;
  953. end;
  954. begin
  955. ctypeconvnode:=ttypeconvnode;
  956. casnode:=tasnode;
  957. cisnode:=tisnode;
  958. end.
  959. {
  960. $Log$
  961. Revision 1.1 2000-09-25 15:37:14 florian
  962. * more fixes
  963. }