tccnv.pas 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  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. {$ifndef OLDBOOL}
  408. { byte(boolean) or word(wordbool) or longint(longbool) must
  409. be accepted for var parameters }
  410. if (p^.explizit) and
  411. (p^.left^.resulttype^.size=p^.resulttype^.size) and
  412. (p^.left^.location.loc in [LOC_REFERENCE,LOC_MEM,LOC_CREGISTER]) then
  413. exit;
  414. {$endif ndef OLDBOOL}
  415. p^.location.loc:=LOC_REGISTER;
  416. { Florian I think this is overestimated
  417. but I still do not really understand how to get this right (PM) }
  418. { Hmmm, I think we need only one reg to return the result of }
  419. { this node => so }
  420. if p^.registers32<1 then
  421. p^.registers32:=1;
  422. { should work (FK)
  423. p^.registers32:=p^.left^.registers32+1;}
  424. end;
  425. procedure first_int_to_bool(var p : ptree);
  426. begin
  427. {$ifndef OLDBOOL}
  428. { byte(boolean) or word(wordbool) or longint(longbool) must
  429. be accepted for var parameters }
  430. if (p^.explizit) and
  431. (p^.left^.resulttype^.size=p^.resulttype^.size) and
  432. (p^.left^.location.loc in [LOC_REFERENCE,LOC_MEM,LOC_CREGISTER]) then
  433. exit;
  434. {$endif ndef OLDBOOL}
  435. p^.location.loc:=LOC_REGISTER;
  436. { Florian I think this is overestimated
  437. but I still do not really understand how to get this right (PM) }
  438. { Hmmm, I think we need only one reg to return the result of }
  439. { this node => so }
  440. p^.left:=gentypeconvnode(p^.left,s32bitdef);
  441. { need if bool to bool !!
  442. not very nice !! }
  443. p^.left^.explizit:=true;
  444. firstpass(p^.left);
  445. if p^.registers32<1 then
  446. p^.registers32:=1;
  447. { p^.resulttype:=booldef; }
  448. { should work (FK)
  449. p^.registers32:=p^.left^.registers32+1;}
  450. end;
  451. procedure first_proc_to_procvar(var p : ptree);
  452. begin
  453. { hmmm, I'am not sure if that is necessary (FK) }
  454. firstpass(p^.left);
  455. if codegenerror then
  456. exit;
  457. if (p^.left^.location.loc<>LOC_REFERENCE) then
  458. CGMessage(cg_e_illegal_expression);
  459. p^.registers32:=p^.left^.registers32;
  460. if p^.registers32<1 then
  461. p^.registers32:=1;
  462. p^.location.loc:=LOC_REGISTER;
  463. end;
  464. procedure first_load_smallset(var p : ptree);
  465. begin
  466. end;
  467. procedure first_pchar_to_string(var p : ptree);
  468. begin
  469. p^.location.loc:=LOC_MEM;
  470. end;
  471. procedure first_ansistring_to_pchar(var p : ptree);
  472. begin
  473. p^.location.loc:=LOC_REGISTER;
  474. if p^.registers32<1 then
  475. p^.registers32:=1;
  476. end;
  477. procedure first_arrayconstructor_to_set(var p:ptree);
  478. var
  479. hp : ptree;
  480. begin
  481. if p^.left^.treetype<>arrayconstructn then
  482. internalerror(5546);
  483. { remove typeconv node }
  484. hp:=p;
  485. p:=p^.left;
  486. putnode(hp);
  487. { create a set constructor tree }
  488. arrayconstructor_to_set(p);
  489. end;
  490. procedure firsttypeconv(var p : ptree);
  491. var
  492. hp : ptree;
  493. aprocdef : pprocdef;
  494. proctype : tdeftype;
  495. const
  496. firstconvert : array[tconverttype] of tfirstconvproc = (
  497. {$ifndef OLDCNV}
  498. first_nothing, {equal}
  499. first_nothing, {not_possible}
  500. first_string_to_string,
  501. first_char_to_string,
  502. first_pchar_to_string,
  503. first_cchar_to_pchar,
  504. first_cstring_to_pchar,
  505. first_ansistring_to_pchar,
  506. first_string_to_chararray,
  507. first_chararray_to_string,
  508. first_array_to_pointer,
  509. first_pointer_to_array,
  510. first_int_to_int,
  511. first_bool_to_int,
  512. first_int_to_bool,
  513. first_real_to_real,
  514. first_int_to_real,
  515. first_int_to_fix,
  516. first_real_to_fix,
  517. first_fix_to_real,
  518. first_proc_to_procvar,
  519. first_arrayconstructor_to_set,
  520. first_load_smallset
  521. );
  522. {$else}
  523. first_nothing,first_nothing,
  524. first_bigger_smaller,first_nothing,first_bigger_smaller,
  525. first_bigger_smaller,first_bigger_smaller,
  526. first_bigger_smaller,first_bigger_smaller,
  527. first_bigger_smaller,first_string_to_string,
  528. first_cstring_to_pchar,first_string_to_chararray,
  529. first_array_to_pointer,first_pointer_to_array,
  530. first_char_to_string,first_bigger_smaller,
  531. first_bigger_smaller,first_bigger_smaller,
  532. first_bigger_smaller,first_bigger_smaller,
  533. first_bigger_smaller,first_bigger_smaller,
  534. first_bigger_smaller,first_bigger_smaller,
  535. first_bigger_smaller,first_bigger_smaller,
  536. first_bigger_smaller,first_bigger_smaller,
  537. first_bigger_smaller,first_bigger_smaller,
  538. first_bigger_smaller,first_bigger_smaller,
  539. first_bigger_smaller,first_bigger_smaller,
  540. first_bool_to_int,first_int_to_bool,
  541. first_int_to_real,first_real_to_fix,
  542. first_fix_to_real,first_int_to_fix,first_real_to_real,
  543. first_locmem,first_proc_to_procvar,
  544. first_cchar_to_pchar,
  545. first_load_smallset,
  546. first_ansistring_to_pchar,
  547. first_pchar_to_string,
  548. first_arrayconstructor_to_set);
  549. {$endif}
  550. begin
  551. aprocdef:=nil;
  552. { if explicite type cast, then run firstpass }
  553. if p^.explizit then
  554. firstpass(p^.left);
  555. if (p^.left^.treetype=typen) and (p^.left^.resulttype=generrordef) then
  556. begin
  557. codegenerror:=true;
  558. Message(parser_e_no_type_not_allowed_here);
  559. end;
  560. if codegenerror then
  561. begin
  562. p^.resulttype:=generrordef;
  563. exit;
  564. end;
  565. if not assigned(p^.left^.resulttype) then
  566. begin
  567. codegenerror:=true;
  568. internalerror(52349);
  569. exit;
  570. end;
  571. { load the value_str from the left part }
  572. p^.registers32:=p^.left^.registers32;
  573. p^.registersfpu:=p^.left^.registersfpu;
  574. {$ifdef SUPPORT_MMX}
  575. p^.registersmmx:=p^.left^.registersmmx;
  576. {$endif}
  577. set_location(p^.location,p^.left^.location);
  578. { remove obsolete type conversions }
  579. if is_equal(p^.left^.resulttype,p^.resulttype) then
  580. begin
  581. { becuase is_equal only checks the basetype for sets we need to
  582. check here if we are loading a smallset into a normalset }
  583. if (p^.resulttype^.deftype=setdef) and
  584. (p^.left^.resulttype^.deftype=setdef) and
  585. (psetdef(p^.resulttype)^.settype<>smallset) and
  586. (psetdef(p^.left^.resulttype)^.settype=smallset) then
  587. begin
  588. { try to define the set as a normalset if it's a constant set }
  589. if p^.left^.treetype=setconstn then
  590. begin
  591. p^.resulttype:=p^.left^.resulttype;
  592. psetdef(p^.resulttype)^.settype:=normset
  593. end
  594. else
  595. p^.convtyp:=tc_load_smallset;
  596. exit;
  597. end
  598. else
  599. begin
  600. hp:=p;
  601. p:=p^.left;
  602. p^.resulttype:=hp^.resulttype;
  603. putnode(hp);
  604. exit;
  605. end;
  606. end;
  607. if is_assignment_overloaded(p^.left^.resulttype,p^.resulttype) then
  608. begin
  609. procinfo.flags:=procinfo.flags or pi_do_call;
  610. hp:=gencallnode(overloaded_operators[assignment],nil);
  611. hp^.left:=gencallparanode(p^.left,nil);
  612. putnode(p);
  613. p:=hp;
  614. firstpass(p);
  615. exit;
  616. end;
  617. if (not(isconvertable(p^.left^.resulttype,p^.resulttype,
  618. p^.convtyp,p^.left^.treetype,p^.explizit))) then
  619. begin
  620. {Procedures have a resulttype of voiddef and functions of their
  621. own resulttype. They will therefore always be incompatible with
  622. a procvar. Because isconvertable cannot check for procedures we
  623. use an extra check for them.}
  624. if (p^.resulttype^.deftype=procvardef) and
  625. ((m_tp_procvar in aktmodeswitches) or
  626. { method pointer use always the TP syntax }
  627. ((pprocvardef(p^.resulttype)^.options and pomethodpointer)<>0)
  628. ) and
  629. ((is_procsym_load(p^.left) or is_procsym_call(p^.left))) then
  630. begin
  631. { just a test: p^.explizit:=false; }
  632. if is_procsym_call(p^.left) then
  633. begin
  634. if p^.left^.right=nil then
  635. begin
  636. p^.left^.treetype:=loadn;
  637. { are at same offset so this could be spared, but
  638. it more secure to do it anyway }
  639. p^.left^.symtableentry:=p^.left^.symtableprocentry;
  640. p^.left^.resulttype:=pprocsym(p^.left^.symtableentry)^.definition;
  641. aprocdef:=pprocdef(p^.left^.resulttype);
  642. end
  643. else
  644. begin
  645. p^.left^.right^.treetype:=loadn;
  646. p^.left^.right^.symtableentry:=p^.left^.right^.symtableentry;
  647. P^.left^.right^.resulttype:=pvarsym(p^.left^.symtableentry)^.definition;
  648. hp:=p^.left^.right;
  649. putnode(p^.left);
  650. p^.left:=hp;
  651. { should we do that ? }
  652. firstpass(p^.left);
  653. if not is_equal(p^.left^.resulttype,p^.resulttype) then
  654. begin
  655. CGMessage(type_e_mismatch);
  656. exit;
  657. end
  658. else
  659. begin
  660. hp:=p;
  661. p:=p^.left;
  662. p^.resulttype:=hp^.resulttype;
  663. putnode(hp);
  664. exit;
  665. end;
  666. end;
  667. end
  668. else
  669. begin
  670. if p^.left^.treetype=addrn then
  671. begin
  672. hp:=p^.left;
  673. p^.left:=p^.left^.left;
  674. putnode(p^.left);
  675. end
  676. else
  677. aprocdef:=pprocsym(p^.left^.symtableentry)^.definition;
  678. end;
  679. p^.convtyp:=tc_proc_2_procvar;
  680. { Now check if the procedure we are going to assign to
  681. the procvar, is compatible with the procvar's type.
  682. Did the original procvar support do such a check?
  683. I can't find any.}
  684. { answer : is_equal works for procvardefs !! }
  685. { but both must be procvardefs, so we cheet little }
  686. if assigned(aprocdef) then
  687. begin
  688. proctype:=aprocdef^.deftype;
  689. aprocdef^.deftype:=procvardef;
  690. { only methods can be assigned to method pointers }
  691. if (assigned(p^.left^.left) and
  692. ((pprocvardef(p^.resulttype)^.options and pomethodpointer)=0)) or
  693. not(is_equal(aprocdef,p^.resulttype)) then
  694. begin
  695. aprocdef^.deftype:=proctype;
  696. CGMessage(type_e_mismatch);
  697. end;
  698. aprocdef^.deftype:=proctype;
  699. firstconvert[p^.convtyp](p);
  700. end
  701. else
  702. CGMessage(type_e_mismatch);
  703. exit;
  704. end
  705. else
  706. begin
  707. if p^.explizit then
  708. begin
  709. { boolean to byte are special because the
  710. location can be different }
  711. if is_integer(p^.resulttype) and
  712. is_boolean(p^.left^.resulttype) then
  713. begin
  714. p^.convtyp:=tc_bool_2_int;
  715. firstconvert[p^.convtyp](p);
  716. exit;
  717. end;
  718. if is_pchar(p^.resulttype) and
  719. is_ansistring(p^.left^.resulttype) then
  720. begin
  721. p^.convtyp:=tc_ansistring_2_pchar;
  722. firstconvert[p^.convtyp](p);
  723. exit;
  724. end;
  725. { do common tc_equal cast }
  726. p^.convtyp:=tc_equal;
  727. { wenn Aufz„hltyp nach Ordinal konvertiert werden soll }
  728. { dann Aufz„hltyp=s32bit }
  729. if (p^.left^.resulttype^.deftype=enumdef) and
  730. is_ordinal(p^.resulttype) then
  731. begin
  732. if p^.left^.treetype=ordconstn then
  733. begin
  734. hp:=genordinalconstnode(p^.left^.value,p^.resulttype);
  735. disposetree(p);
  736. firstpass(hp);
  737. p:=hp;
  738. exit;
  739. end
  740. else
  741. begin
  742. if not isconvertable(s32bitdef,p^.resulttype,p^.convtyp,
  743. ordconstn { only Dummy},false ) then
  744. CGMessage(cg_e_illegal_type_conversion);
  745. end;
  746. end
  747. { ordinal to enumeration }
  748. else
  749. if (p^.resulttype^.deftype=enumdef) and
  750. is_ordinal(p^.left^.resulttype) then
  751. begin
  752. if p^.left^.treetype=ordconstn then
  753. begin
  754. hp:=genordinalconstnode(p^.left^.value,p^.resulttype);
  755. disposetree(p);
  756. firstpass(hp);
  757. p:=hp;
  758. exit;
  759. end
  760. else
  761. begin
  762. if not isconvertable(p^.left^.resulttype,s32bitdef,p^.convtyp,
  763. ordconstn { nur Dummy},false ) then
  764. CGMessage(cg_e_illegal_type_conversion);
  765. end;
  766. end
  767. {Are we typecasting an ordconst to a char?}
  768. else
  769. if is_char(p^.resulttype) and
  770. is_ordinal(p^.left^.resulttype) then
  771. begin
  772. if p^.left^.treetype=ordconstn then
  773. begin
  774. hp:=genordinalconstnode(p^.left^.value,p^.resulttype);
  775. firstpass(hp);
  776. disposetree(p);
  777. p:=hp;
  778. exit;
  779. end
  780. else
  781. begin
  782. { this is wrong because it converts to a 4 byte long var !!
  783. if not isconvertable(p^.left^.resulttype,s32bitdef,p^.convtyp,ordconstn nur Dummy ) then }
  784. if not isconvertable(p^.left^.resulttype,u8bitdef,
  785. p^.convtyp,ordconstn { nur Dummy},false ) then
  786. CGMessage(cg_e_illegal_type_conversion);
  787. end;
  788. end
  789. { only if the same size or formal def }
  790. { why do we allow typecasting of voiddef ?? (PM) }
  791. else
  792. if not(
  793. (p^.left^.resulttype^.deftype=formaldef) or
  794. (p^.left^.resulttype^.size=p^.resulttype^.size) or
  795. (is_equal(p^.left^.resulttype,voiddef) and
  796. (p^.left^.treetype=derefn))
  797. ) then
  798. CGMessage(cg_e_illegal_type_conversion);
  799. { the conversion into a strutured type is only }
  800. { possible, if the source is no register }
  801. if ((p^.resulttype^.deftype in [recorddef,stringdef,arraydef]) or
  802. ((p^.resulttype^.deftype=objectdef) and not(pobjectdef(p^.resulttype)^.isclass))
  803. ) and (p^.left^.location.loc in [LOC_REGISTER,LOC_CREGISTER]) and
  804. {it also works if the assignment is overloaded }
  805. not is_assignment_overloaded(p^.left^.resulttype,p^.resulttype) then
  806. CGMessage(cg_e_illegal_type_conversion);
  807. end
  808. else
  809. CGMessage(type_e_mismatch);
  810. end
  811. end;
  812. { ordinal contants can be directly converted }
  813. if (p^.left^.treetype=ordconstn) and is_ordinal(p^.resulttype) then
  814. begin
  815. { range checking is done in genordinalconstnode (PFV) }
  816. hp:=genordinalconstnode(p^.left^.value,p^.resulttype);
  817. disposetree(p);
  818. firstpass(hp);
  819. p:=hp;
  820. exit;
  821. end;
  822. if p^.convtyp<>tc_equal then
  823. firstconvert[p^.convtyp](p);
  824. end;
  825. {*****************************************************************************
  826. FirstIs
  827. *****************************************************************************}
  828. procedure firstis(var p : ptree);
  829. begin
  830. firstpass(p^.left);
  831. firstpass(p^.right);
  832. if codegenerror then
  833. exit;
  834. if (p^.right^.resulttype^.deftype<>classrefdef) then
  835. CGMessage(type_e_mismatch);
  836. left_right_max(p);
  837. { left must be a class }
  838. if (p^.left^.resulttype^.deftype<>objectdef) or
  839. not(pobjectdef(p^.left^.resulttype)^.isclass) then
  840. CGMessage(type_e_mismatch);
  841. { the operands must be related }
  842. if (not(pobjectdef(p^.left^.resulttype)^.isrelated(
  843. pobjectdef(pclassrefdef(p^.right^.resulttype)^.definition)))) and
  844. (not(pobjectdef(pclassrefdef(p^.right^.resulttype)^.definition)^.isrelated(
  845. pobjectdef(p^.left^.resulttype)))) then
  846. CGMessage(type_e_mismatch);
  847. p^.location.loc:=LOC_FLAGS;
  848. p^.resulttype:=booldef;
  849. end;
  850. {*****************************************************************************
  851. FirstAs
  852. *****************************************************************************}
  853. procedure firstas(var p : ptree);
  854. begin
  855. firstpass(p^.right);
  856. firstpass(p^.left);
  857. if codegenerror then
  858. exit;
  859. if (p^.right^.resulttype^.deftype<>classrefdef) then
  860. CGMessage(type_e_mismatch);
  861. left_right_max(p);
  862. { left must be a class }
  863. if (p^.left^.resulttype^.deftype<>objectdef) or
  864. not(pobjectdef(p^.left^.resulttype)^.isclass) then
  865. CGMessage(type_e_mismatch);
  866. { the operands must be related }
  867. if (not(pobjectdef(p^.left^.resulttype)^.isrelated(
  868. pobjectdef(pclassrefdef(p^.right^.resulttype)^.definition)))) and
  869. (not(pobjectdef(pclassrefdef(p^.right^.resulttype)^.definition)^.isrelated(
  870. pobjectdef(p^.left^.resulttype)))) then
  871. CGMessage(type_e_mismatch);
  872. set_location(p^.location,p^.left^.location);
  873. p^.resulttype:=pclassrefdef(p^.right^.resulttype)^.definition;
  874. end;
  875. end.
  876. {
  877. $Log$
  878. Revision 1.18 1999-01-27 14:56:57 pierre
  879. * typo error corrected solves bug0190 and bug0204
  880. Revision 1.17 1999/01/27 14:15:25 pierre
  881. * bug0209 corrected (introduce while solving other bool to int related bugs)
  882. Revision 1.16 1999/01/27 13:02:21 pierre
  883. boolean to int conversion problems bug0205 bug0208
  884. Revision 1.15 1999/01/27 00:13:57 florian
  885. * "procedure of object"-stuff fixed
  886. Revision 1.14 1999/01/19 12:17:45 peter
  887. * removed rangecheck warning which was shown twice
  888. Revision 1.13 1998/12/30 22:13:47 peter
  889. * if explicit cnv then also handle the ordinal consts direct
  890. Revision 1.12 1998/12/11 00:03:53 peter
  891. + globtype,tokens,version unit splitted from globals
  892. Revision 1.11 1998/12/04 10:18:12 florian
  893. * some stuff for procedures of object added
  894. * bug with overridden virtual constructors fixed (reported by Italo Gomes)
  895. Revision 1.10 1998/11/29 12:40:24 peter
  896. * newcnv -> not oldcnv
  897. Revision 1.9 1998/11/26 13:10:43 peter
  898. * new int - int conversion -dNEWCNV
  899. * some function renamings
  900. Revision 1.8 1998/11/05 12:03:03 peter
  901. * released useansistring
  902. * removed -Sv, its now available in fpc modes
  903. Revision 1.7 1998/10/23 11:58:27 florian
  904. * better code generation for s:=s+[b] if b is in the range of
  905. a small set and s is also a small set
  906. Revision 1.6 1998/10/21 15:12:58 pierre
  907. * bug fix for IOCHECK inside a procedure with iocheck modifier
  908. * removed the GPF for unexistant overloading
  909. (firstcall was called with procedinition=nil !)
  910. * changed typen to what Florian proposed
  911. gentypenode(p : pdef) sets the typenodetype field
  912. and resulttype is only set if inside bt_type block !
  913. Revision 1.5 1998/10/07 10:38:55 peter
  914. * forgot a firstpass in arrayconstruct2set
  915. Revision 1.4 1998/10/05 21:33:32 peter
  916. * fixed 161,165,166,167,168
  917. Revision 1.3 1998/09/27 10:16:26 florian
  918. * type casts pchar<->ansistring fixed
  919. * ansistring[..] calls does now an unique call
  920. Revision 1.2 1998/09/24 23:49:22 peter
  921. + aktmodeswitches
  922. Revision 1.1 1998/09/23 20:42:24 peter
  923. * splitted pass_1
  924. }