tccnv.pas 35 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. {$ifndef OLDASM}
  37. ,i386base
  38. {$else}
  39. ,i386
  40. {$endif}
  41. {$endif}
  42. {$ifdef m68k}
  43. ,m68k
  44. {$endif}
  45. ;
  46. {*****************************************************************************
  47. Array constructor to Set Conversion
  48. *****************************************************************************}
  49. procedure arrayconstructor_to_set(var p:ptree);
  50. var
  51. constp,
  52. buildp,
  53. p2,p3,p4 : ptree;
  54. pd : pdef;
  55. constset : pconstset;
  56. constsetlo,
  57. constsethi : longint;
  58. procedure update_constsethi(p:pdef);
  59. begin
  60. if ((p^.deftype=orddef) and
  61. (porddef(p)^.high>constsethi)) then
  62. constsethi:=porddef(p)^.high
  63. else
  64. if ((p^.deftype=enumdef) and
  65. (penumdef(p)^.max>constsethi)) then
  66. constsethi:=penumdef(p)^.max;
  67. end;
  68. procedure do_set(pos : longint);
  69. var
  70. mask,l : longint;
  71. begin
  72. if (pos>255) or (pos<0) then
  73. Message(parser_e_illegal_set_expr);
  74. if pos>constsethi then
  75. constsethi:=pos;
  76. if pos<constsetlo then
  77. constsetlo:=pos;
  78. l:=pos shr 3;
  79. mask:=1 shl (pos mod 8);
  80. { do we allow the same twice }
  81. if (constset^[l] and mask)<>0 then
  82. Message(parser_e_illegal_set_expr);
  83. constset^[l]:=constset^[l] or mask;
  84. end;
  85. var
  86. l : longint;
  87. lr,hr : longint;
  88. begin
  89. new(constset);
  90. FillChar(constset^,sizeof(constset^),0);
  91. pd:=nil;
  92. constsetlo:=0;
  93. constsethi:=0;
  94. constp:=gensinglenode(setconstn,nil);
  95. constp^.value_set:=constset;
  96. buildp:=constp;
  97. if assigned(p^.left) then
  98. begin
  99. while assigned(p) do
  100. begin
  101. p4:=nil; { will contain the tree to create the set }
  102. { split a range into p2 and p3 }
  103. if p^.left^.treetype=arrayconstructrangen then
  104. begin
  105. p2:=p^.left^.left;
  106. p3:=p^.left^.right;
  107. { node is not used anymore }
  108. putnode(p^.left);
  109. end
  110. else
  111. begin
  112. p2:=p^.left;
  113. p3:=nil;
  114. end;
  115. firstpass(p2);
  116. if assigned(p3) then
  117. firstpass(p3);
  118. if codegenerror then
  119. break;
  120. case p2^.resulttype^.deftype of
  121. enumdef,
  122. orddef:
  123. begin
  124. getrange(p2^.resulttype,lr,hr);
  125. if is_integer(p2^.resulttype) and
  126. ((lr<0) or (hr>255)) then
  127. begin
  128. p2:=gentypeconvnode(p2,u8bitdef);
  129. firstpass(p2);
  130. end;
  131. { set settype result }
  132. if pd=nil then
  133. pd:=p2^.resulttype;
  134. if not(is_equal(pd,p2^.resulttype)) then
  135. begin
  136. CGMessage(type_e_typeconflict_in_set);
  137. disposetree(p2);
  138. end
  139. else
  140. begin
  141. if assigned(p3) then
  142. begin
  143. if is_integer(p3^.resulttype) then
  144. begin
  145. p3:=gentypeconvnode(p3,u8bitdef);
  146. firstpass(p3);
  147. end;
  148. if not(is_equal(pd,p3^.resulttype)) then
  149. CGMessage(type_e_typeconflict_in_set)
  150. else
  151. begin
  152. if (p2^.treetype=ordconstn) and (p3^.treetype=ordconstn) then
  153. begin
  154. for l:=p2^.value to p3^.value do
  155. do_set(l);
  156. disposetree(p3);
  157. disposetree(p2);
  158. end
  159. else
  160. begin
  161. update_constsethi(p3^.resulttype);
  162. p4:=gennode(setelementn,p2,p3);
  163. end;
  164. end;
  165. end
  166. else
  167. begin
  168. { Single value }
  169. if p2^.treetype=ordconstn then
  170. begin
  171. do_set(p2^.value);
  172. disposetree(p2);
  173. end
  174. else
  175. begin
  176. update_constsethi(p2^.resulttype);
  177. p4:=gennode(setelementn,p2,nil);
  178. end;
  179. end;
  180. end;
  181. end;
  182. stringdef : begin
  183. if pd=nil then
  184. pd:=cchardef;
  185. if not(is_equal(pd,cchardef)) then
  186. CGMessage(type_e_typeconflict_in_set)
  187. else
  188. for l:=1 to length(pstring(p2^.value_str)^) do
  189. do_set(ord(pstring(p2^.value_str)^[l]));
  190. disposetree(p2);
  191. end;
  192. else
  193. CGMessage(type_e_ordinal_expr_expected);
  194. end;
  195. { insert the set creation tree }
  196. if assigned(p4) then
  197. buildp:=gennode(addn,buildp,p4);
  198. { load next and dispose current node }
  199. p2:=p;
  200. p:=p^.right;
  201. putnode(p2);
  202. end;
  203. end
  204. else
  205. begin
  206. { empty set [], only remove node }
  207. putnode(p);
  208. end;
  209. { set the initial set type }
  210. constp^.resulttype:=new(psetdef,init(pd,constsethi));
  211. { set the new tree }
  212. p:=buildp;
  213. end;
  214. {*****************************************************************************
  215. FirstTypeConv
  216. *****************************************************************************}
  217. type
  218. tfirstconvproc = procedure(var p : ptree);
  219. procedure first_int_to_int(var p : ptree);
  220. begin
  221. if (p^.registers32=0) and
  222. (p^.left^.location.loc<>LOC_REGISTER) and
  223. (p^.resulttype^.size>p^.left^.resulttype^.size) then
  224. begin
  225. p^.registers32:=1;
  226. p^.location.loc:=LOC_REGISTER;
  227. end;
  228. end;
  229. procedure first_cstring_to_pchar(var p : ptree);
  230. begin
  231. p^.registers32:=1;
  232. p^.location.loc:=LOC_REGISTER;
  233. end;
  234. procedure first_string_to_chararray(var p : ptree);
  235. begin
  236. p^.registers32:=1;
  237. p^.location.loc:=LOC_REGISTER;
  238. end;
  239. procedure first_string_to_string(var p : ptree);
  240. begin
  241. if pstringdef(p^.resulttype)^.string_typ<>
  242. pstringdef(p^.left^.resulttype)^.string_typ then
  243. begin
  244. if p^.left^.treetype=stringconstn then
  245. begin
  246. p^.left^.stringtype:=pstringdef(p^.resulttype)^.string_typ;
  247. { we don't have to do anything, the const }
  248. { node generates an ansistring }
  249. p^.convtyp:=tc_equal;
  250. end
  251. else
  252. procinfo.flags:=procinfo.flags or pi_do_call;
  253. end;
  254. { for simplicity lets first keep all ansistrings
  255. as LOC_MEM, could also become LOC_REGISTER }
  256. p^.location.loc:=LOC_MEM;
  257. end;
  258. procedure first_char_to_string(var p : ptree);
  259. var
  260. hp : ptree;
  261. begin
  262. if p^.left^.treetype=ordconstn then
  263. begin
  264. hp:=genstringconstnode(chr(p^.left^.value));
  265. hp^.stringtype:=pstringdef(p^.resulttype)^.string_typ;
  266. firstpass(hp);
  267. disposetree(p);
  268. p:=hp;
  269. end
  270. else
  271. p^.location.loc:=LOC_MEM;
  272. end;
  273. procedure first_nothing(var p : ptree);
  274. begin
  275. p^.location.loc:=LOC_MEM;
  276. end;
  277. procedure first_array_to_pointer(var p : ptree);
  278. begin
  279. if p^.registers32<1 then
  280. p^.registers32:=1;
  281. p^.location.loc:=LOC_REGISTER;
  282. end;
  283. procedure first_int_to_real(var p : ptree);
  284. var
  285. t : ptree;
  286. begin
  287. if p^.left^.treetype=ordconstn then
  288. begin
  289. t:=genrealconstnode(p^.left^.value,pfloatdef(p^.resulttype));
  290. firstpass(t);
  291. disposetree(p);
  292. p:=t;
  293. exit;
  294. end;
  295. if p^.registersfpu<1 then
  296. p^.registersfpu:=1;
  297. p^.location.loc:=LOC_FPU;
  298. end;
  299. procedure first_int_to_fix(var p : ptree);
  300. var
  301. t : ptree;
  302. begin
  303. if p^.left^.treetype=ordconstn then
  304. begin
  305. t:=genfixconstnode(p^.left^.value shl 16,p^.resulttype);
  306. firstpass(t);
  307. disposetree(p);
  308. p:=t;
  309. exit;
  310. end;
  311. if p^.registers32<1 then
  312. p^.registers32:=1;
  313. p^.location.loc:=LOC_REGISTER;
  314. end;
  315. procedure first_real_to_fix(var p : ptree);
  316. var
  317. t : ptree;
  318. begin
  319. if p^.left^.treetype=fixconstn then
  320. begin
  321. t:=genfixconstnode(round(p^.left^.value_real*65536),p^.resulttype);
  322. firstpass(t);
  323. disposetree(p);
  324. p:=t;
  325. exit;
  326. end;
  327. { at least one fpu and int register needed }
  328. if p^.registers32<1 then
  329. p^.registers32:=1;
  330. if p^.registersfpu<1 then
  331. p^.registersfpu:=1;
  332. p^.location.loc:=LOC_REGISTER;
  333. end;
  334. procedure first_fix_to_real(var p : ptree);
  335. var
  336. t : ptree;
  337. begin
  338. if p^.left^.treetype=fixconstn then
  339. begin
  340. t:=genrealconstnode(round(p^.left^.value_fix/65536.0),p^.resulttype);
  341. firstpass(t);
  342. disposetree(p);
  343. p:=t;
  344. exit;
  345. end;
  346. if p^.registersfpu<1 then
  347. p^.registersfpu:=1;
  348. p^.location.loc:=LOC_FPU;
  349. end;
  350. procedure first_real_to_real(var p : ptree);
  351. var
  352. t : ptree;
  353. begin
  354. if p^.left^.treetype=realconstn then
  355. begin
  356. t:=genrealconstnode(p^.left^.value_real,p^.resulttype);
  357. firstpass(t);
  358. disposetree(p);
  359. p:=t;
  360. exit;
  361. end;
  362. { comp isn't a floating type }
  363. {$ifdef i386}
  364. if (pfloatdef(p^.resulttype)^.typ=s64comp) and
  365. (pfloatdef(p^.left^.resulttype)^.typ<>s64comp) and
  366. not (p^.explizit) then
  367. CGMessage(type_w_convert_real_2_comp);
  368. {$endif}
  369. if p^.registersfpu<1 then
  370. p^.registersfpu:=1;
  371. p^.location.loc:=LOC_FPU;
  372. end;
  373. procedure first_pointer_to_array(var p : ptree);
  374. begin
  375. if p^.registers32<1 then
  376. p^.registers32:=1;
  377. p^.location.loc:=LOC_REFERENCE;
  378. end;
  379. procedure first_chararray_to_string(var p : ptree);
  380. begin
  381. { the only important information is the location of the }
  382. { result }
  383. { other stuff is done by firsttypeconv }
  384. p^.location.loc:=LOC_MEM;
  385. end;
  386. procedure first_cchar_to_pchar(var p : ptree);
  387. begin
  388. p^.left:=gentypeconvnode(p^.left,cshortstringdef);
  389. { convert constant char to constant string }
  390. firstpass(p^.left);
  391. { evalute tree }
  392. firstpass(p);
  393. end;
  394. procedure first_bool_to_int(var p : ptree);
  395. begin
  396. { byte(boolean) or word(wordbool) or longint(longbool) must
  397. be accepted for var parameters }
  398. if (p^.explizit) and
  399. (p^.left^.resulttype^.size=p^.resulttype^.size) and
  400. (p^.left^.location.loc in [LOC_REFERENCE,LOC_MEM,LOC_CREGISTER]) then
  401. exit;
  402. p^.location.loc:=LOC_REGISTER;
  403. if p^.registers32<1 then
  404. p^.registers32:=1;
  405. end;
  406. procedure first_int_to_bool(var p : ptree);
  407. begin
  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. p^.location.loc:=LOC_REGISTER;
  415. { need if bool to bool !!
  416. not very nice !!
  417. p^.left:=gentypeconvnode(p^.left,s32bitdef);
  418. p^.left^.explizit:=true;
  419. firstpass(p^.left); }
  420. if p^.registers32<1 then
  421. p^.registers32:=1;
  422. end;
  423. procedure first_bool_to_bool(var p : ptree);
  424. begin
  425. p^.location.loc:=LOC_REGISTER;
  426. if p^.registers32<1 then
  427. p^.registers32:=1;
  428. end;
  429. procedure first_proc_to_procvar(var p : ptree);
  430. begin
  431. { hmmm, I'am not sure if that is necessary (FK) }
  432. firstpass(p^.left);
  433. if codegenerror then
  434. exit;
  435. if (p^.left^.location.loc<>LOC_REFERENCE) then
  436. CGMessage(cg_e_illegal_expression);
  437. p^.registers32:=p^.left^.registers32;
  438. if p^.registers32<1 then
  439. p^.registers32:=1;
  440. p^.location.loc:=LOC_REGISTER;
  441. end;
  442. procedure first_load_smallset(var p : ptree);
  443. begin
  444. end;
  445. procedure first_pchar_to_string(var p : ptree);
  446. begin
  447. p^.location.loc:=LOC_REFERENCE;
  448. end;
  449. procedure first_ansistring_to_pchar(var p : ptree);
  450. begin
  451. p^.location.loc:=LOC_REGISTER;
  452. if p^.registers32<1 then
  453. p^.registers32:=1;
  454. end;
  455. procedure first_arrayconstructor_to_set(var p:ptree);
  456. var
  457. hp : ptree;
  458. begin
  459. if p^.left^.treetype<>arrayconstructn then
  460. internalerror(5546);
  461. { remove typeconv node }
  462. hp:=p;
  463. p:=p^.left;
  464. putnode(hp);
  465. { create a set constructor tree }
  466. arrayconstructor_to_set(p);
  467. end;
  468. procedure firsttypeconv(var p : ptree);
  469. var
  470. hp : ptree;
  471. aprocdef : pprocdef;
  472. proctype : tdeftype;
  473. const
  474. firstconvert : array[tconverttype] of tfirstconvproc = (
  475. first_nothing, {equal}
  476. first_nothing, {not_possible}
  477. first_string_to_string,
  478. first_char_to_string,
  479. first_pchar_to_string,
  480. first_cchar_to_pchar,
  481. first_cstring_to_pchar,
  482. first_ansistring_to_pchar,
  483. first_string_to_chararray,
  484. first_chararray_to_string,
  485. first_array_to_pointer,
  486. first_pointer_to_array,
  487. first_int_to_int,
  488. first_int_to_bool,
  489. first_bool_to_bool,
  490. first_bool_to_int,
  491. first_real_to_real,
  492. first_int_to_real,
  493. first_int_to_fix,
  494. first_real_to_fix,
  495. first_fix_to_real,
  496. first_proc_to_procvar,
  497. first_arrayconstructor_to_set,
  498. first_load_smallset
  499. );
  500. begin
  501. aprocdef:=nil;
  502. { if explicite type cast, then run firstpass }
  503. if p^.explizit then
  504. firstpass(p^.left);
  505. if (p^.left^.treetype=typen) and (p^.left^.resulttype=generrordef) then
  506. begin
  507. codegenerror:=true;
  508. Message(parser_e_no_type_not_allowed_here);
  509. end;
  510. if codegenerror then
  511. begin
  512. p^.resulttype:=generrordef;
  513. exit;
  514. end;
  515. if not assigned(p^.left^.resulttype) then
  516. begin
  517. codegenerror:=true;
  518. internalerror(52349);
  519. exit;
  520. end;
  521. { load the value_str from the left part }
  522. p^.registers32:=p^.left^.registers32;
  523. p^.registersfpu:=p^.left^.registersfpu;
  524. {$ifdef SUPPORT_MMX}
  525. p^.registersmmx:=p^.left^.registersmmx;
  526. {$endif}
  527. set_location(p^.location,p^.left^.location);
  528. { remove obsolete type conversions }
  529. if is_equal(p^.left^.resulttype,p^.resulttype) then
  530. begin
  531. { becuase is_equal only checks the basetype for sets we need to
  532. check here if we are loading a smallset into a normalset }
  533. if (p^.resulttype^.deftype=setdef) and
  534. (p^.left^.resulttype^.deftype=setdef) and
  535. (psetdef(p^.resulttype)^.settype<>smallset) and
  536. (psetdef(p^.left^.resulttype)^.settype=smallset) then
  537. begin
  538. { try to define the set as a normalset if it's a constant set }
  539. if p^.left^.treetype=setconstn then
  540. begin
  541. p^.resulttype:=p^.left^.resulttype;
  542. psetdef(p^.resulttype)^.settype:=normset
  543. end
  544. else
  545. p^.convtyp:=tc_load_smallset;
  546. exit;
  547. end
  548. else
  549. begin
  550. hp:=p;
  551. p:=p^.left;
  552. p^.resulttype:=hp^.resulttype;
  553. putnode(hp);
  554. exit;
  555. end;
  556. end;
  557. if is_assignment_overloaded(p^.left^.resulttype,p^.resulttype) then
  558. begin
  559. procinfo.flags:=procinfo.flags or pi_do_call;
  560. hp:=gencallnode(overloaded_operators[assignment],nil);
  561. hp^.left:=gencallparanode(p^.left,nil);
  562. putnode(p);
  563. p:=hp;
  564. firstpass(p);
  565. exit;
  566. end;
  567. if isconvertable(p^.left^.resulttype,p^.resulttype,p^.convtyp,p^.left^.treetype,p^.explizit)=0 then
  568. begin
  569. {Procedures have a resulttype of voiddef and functions of their
  570. own resulttype. They will therefore always be incompatible with
  571. a procvar. Because isconvertable cannot check for procedures we
  572. use an extra check for them.}
  573. if (p^.resulttype^.deftype=procvardef) and
  574. ((m_tp_procvar in aktmodeswitches) or
  575. { method pointer use always the TP syntax }
  576. ((pprocvardef(p^.resulttype)^.options and pomethodpointer)<>0)
  577. ) and
  578. ((is_procsym_load(p^.left) or is_procsym_call(p^.left))) then
  579. begin
  580. { just a test: p^.explizit:=false; }
  581. if is_procsym_call(p^.left) then
  582. begin
  583. if p^.left^.right=nil then
  584. begin
  585. p^.left^.treetype:=loadn;
  586. { are at same offset so this could be spared, but
  587. it more secure to do it anyway }
  588. p^.left^.symtableentry:=p^.left^.symtableprocentry;
  589. p^.left^.resulttype:=pprocsym(p^.left^.symtableentry)^.definition;
  590. aprocdef:=pprocdef(p^.left^.resulttype);
  591. end
  592. else
  593. begin
  594. p^.left^.right^.treetype:=loadn;
  595. p^.left^.right^.symtableentry:=p^.left^.right^.symtableentry;
  596. P^.left^.right^.resulttype:=pvarsym(p^.left^.symtableentry)^.definition;
  597. hp:=p^.left^.right;
  598. putnode(p^.left);
  599. p^.left:=hp;
  600. { should we do that ? }
  601. firstpass(p^.left);
  602. if not is_equal(p^.left^.resulttype,p^.resulttype) then
  603. begin
  604. CGMessage(type_e_mismatch);
  605. exit;
  606. end
  607. else
  608. begin
  609. hp:=p;
  610. p:=p^.left;
  611. p^.resulttype:=hp^.resulttype;
  612. putnode(hp);
  613. exit;
  614. end;
  615. end;
  616. end
  617. else
  618. begin
  619. if p^.left^.treetype=addrn then
  620. begin
  621. hp:=p^.left;
  622. p^.left:=p^.left^.left;
  623. putnode(p^.left);
  624. end
  625. else
  626. aprocdef:=pprocsym(p^.left^.symtableentry)^.definition;
  627. end;
  628. p^.convtyp:=tc_proc_2_procvar;
  629. { Now check if the procedure we are going to assign to
  630. the procvar, is compatible with the procvar's type.
  631. Did the original procvar support do such a check?
  632. I can't find any.}
  633. { answer : is_equal works for procvardefs !! }
  634. { but both must be procvardefs, so we cheet little }
  635. if assigned(aprocdef) then
  636. begin
  637. proctype:=aprocdef^.deftype;
  638. aprocdef^.deftype:=procvardef;
  639. { only methods can be assigned to method pointers }
  640. if (assigned(p^.left^.left) and
  641. ((pprocvardef(p^.resulttype)^.options and pomethodpointer)=0)) or
  642. not(is_equal(aprocdef,p^.resulttype)) then
  643. begin
  644. aprocdef^.deftype:=proctype;
  645. CGMessage(type_e_mismatch);
  646. end;
  647. aprocdef^.deftype:=proctype;
  648. firstconvert[p^.convtyp](p);
  649. end
  650. else
  651. CGMessage(type_e_mismatch);
  652. exit;
  653. end
  654. else
  655. begin
  656. if p^.explizit then
  657. begin
  658. { boolean to byte are special because the
  659. location can be different }
  660. if is_integer(p^.resulttype) and
  661. is_boolean(p^.left^.resulttype) then
  662. begin
  663. p^.convtyp:=tc_bool_2_int;
  664. firstconvert[p^.convtyp](p);
  665. exit;
  666. end;
  667. if is_pchar(p^.resulttype) and
  668. is_ansistring(p^.left^.resulttype) then
  669. begin
  670. p^.convtyp:=tc_ansistring_2_pchar;
  671. firstconvert[p^.convtyp](p);
  672. exit;
  673. end;
  674. { do common tc_equal cast }
  675. p^.convtyp:=tc_equal;
  676. { wenn Aufz„hltyp nach Ordinal konvertiert werden soll }
  677. { dann Aufz„hltyp=s32bit }
  678. if (p^.left^.resulttype^.deftype=enumdef) and
  679. is_ordinal(p^.resulttype) then
  680. begin
  681. if p^.left^.treetype=ordconstn then
  682. begin
  683. hp:=genordinalconstnode(p^.left^.value,p^.resulttype);
  684. disposetree(p);
  685. firstpass(hp);
  686. p:=hp;
  687. exit;
  688. end
  689. else
  690. begin
  691. if isconvertable(s32bitdef,p^.resulttype,p^.convtyp,ordconstn,false)=0 then
  692. CGMessage(cg_e_illegal_type_conversion);
  693. end;
  694. end
  695. { ordinal to enumeration }
  696. else
  697. if (p^.resulttype^.deftype=enumdef) and
  698. is_ordinal(p^.left^.resulttype) then
  699. begin
  700. if p^.left^.treetype=ordconstn then
  701. begin
  702. hp:=genordinalconstnode(p^.left^.value,p^.resulttype);
  703. disposetree(p);
  704. firstpass(hp);
  705. p:=hp;
  706. exit;
  707. end
  708. else
  709. begin
  710. if IsConvertable(p^.left^.resulttype,s32bitdef,p^.convtyp,ordconstn,false)=0 then
  711. CGMessage(cg_e_illegal_type_conversion);
  712. end;
  713. end
  714. {Are we typecasting an ordconst to a char?}
  715. else
  716. if is_char(p^.resulttype) and
  717. is_ordinal(p^.left^.resulttype) then
  718. begin
  719. if p^.left^.treetype=ordconstn then
  720. begin
  721. hp:=genordinalconstnode(p^.left^.value,p^.resulttype);
  722. firstpass(hp);
  723. disposetree(p);
  724. p:=hp;
  725. exit;
  726. end
  727. else
  728. begin
  729. { this is wrong because it converts to a 4 byte long var !!
  730. if not isconvertable(p^.left^.resulttype,s32bitdef,p^.convtyp,ordconstn nur Dummy ) then }
  731. if IsConvertable(p^.left^.resulttype,u8bitdef,p^.convtyp,ordconstn,false)=0 then
  732. CGMessage(cg_e_illegal_type_conversion);
  733. end;
  734. end
  735. { only if the same size or formal def }
  736. { why do we allow typecasting of voiddef ?? (PM) }
  737. else
  738. if not(
  739. (p^.left^.resulttype^.deftype=formaldef) or
  740. (p^.left^.resulttype^.size=p^.resulttype^.size) or
  741. (is_equal(p^.left^.resulttype,voiddef) and
  742. (p^.left^.treetype=derefn))
  743. ) then
  744. CGMessage(cg_e_illegal_type_conversion);
  745. { the conversion into a strutured type is only }
  746. { possible, if the source is no register }
  747. if ((p^.resulttype^.deftype in [recorddef,stringdef,arraydef]) or
  748. ((p^.resulttype^.deftype=objectdef) and not(pobjectdef(p^.resulttype)^.isclass))
  749. ) and (p^.left^.location.loc in [LOC_REGISTER,LOC_CREGISTER]) and
  750. {it also works if the assignment is overloaded }
  751. not is_assignment_overloaded(p^.left^.resulttype,p^.resulttype) then
  752. CGMessage(cg_e_illegal_type_conversion);
  753. end
  754. else
  755. {$ifndef OLDPPU}
  756. CGMessage2(type_e_incompatible_types,p^.left^.resulttype^.typename,p^.resulttype^.typename);
  757. {$else}
  758. CGMessage(type_e_mismatch);
  759. {$endif}
  760. end
  761. end;
  762. { ordinal contants can be directly converted }
  763. if (p^.left^.treetype=ordconstn) and is_ordinal(p^.resulttype) then
  764. begin
  765. { range checking is done in genordinalconstnode (PFV) }
  766. hp:=genordinalconstnode(p^.left^.value,p^.resulttype);
  767. disposetree(p);
  768. firstpass(hp);
  769. p:=hp;
  770. exit;
  771. end;
  772. if p^.convtyp<>tc_equal then
  773. firstconvert[p^.convtyp](p);
  774. end;
  775. {*****************************************************************************
  776. FirstIs
  777. *****************************************************************************}
  778. procedure firstis(var p : ptree);
  779. var
  780. Store_valid : boolean;
  781. begin
  782. Store_valid:=Must_be_valid;
  783. Must_be_valid:=true;
  784. firstpass(p^.left);
  785. firstpass(p^.right);
  786. Must_be_valid:=Store_valid;
  787. if codegenerror then
  788. exit;
  789. if (p^.right^.resulttype^.deftype<>classrefdef) then
  790. CGMessage(type_e_mismatch);
  791. left_right_max(p);
  792. { left must be a class }
  793. if (p^.left^.resulttype^.deftype<>objectdef) or
  794. not(pobjectdef(p^.left^.resulttype)^.isclass) then
  795. CGMessage(type_e_mismatch);
  796. { the operands must be related }
  797. if (not(pobjectdef(p^.left^.resulttype)^.isrelated(
  798. pobjectdef(pclassrefdef(p^.right^.resulttype)^.definition)))) and
  799. (not(pobjectdef(pclassrefdef(p^.right^.resulttype)^.definition)^.isrelated(
  800. pobjectdef(p^.left^.resulttype)))) then
  801. CGMessage(type_e_mismatch);
  802. p^.location.loc:=LOC_FLAGS;
  803. p^.resulttype:=booldef;
  804. end;
  805. {*****************************************************************************
  806. FirstAs
  807. *****************************************************************************}
  808. procedure firstas(var p : ptree);
  809. var
  810. Store_valid : boolean;
  811. begin
  812. Store_valid:=Must_be_valid;
  813. Must_be_valid:=true;
  814. firstpass(p^.right);
  815. firstpass(p^.left);
  816. Must_be_valid:=Store_valid;
  817. if codegenerror then
  818. exit;
  819. if (p^.right^.resulttype^.deftype<>classrefdef) then
  820. CGMessage(type_e_mismatch);
  821. left_right_max(p);
  822. { left must be a class }
  823. if (p^.left^.resulttype^.deftype<>objectdef) or
  824. not(pobjectdef(p^.left^.resulttype)^.isclass) then
  825. CGMessage(type_e_mismatch);
  826. { the operands must be related }
  827. if (not(pobjectdef(p^.left^.resulttype)^.isrelated(
  828. pobjectdef(pclassrefdef(p^.right^.resulttype)^.definition)))) and
  829. (not(pobjectdef(pclassrefdef(p^.right^.resulttype)^.definition)^.isrelated(
  830. pobjectdef(p^.left^.resulttype)))) then
  831. CGMessage(type_e_mismatch);
  832. set_location(p^.location,p^.left^.location);
  833. p^.resulttype:=pclassrefdef(p^.right^.resulttype)^.definition;
  834. end;
  835. end.
  836. {
  837. $Log$
  838. Revision 1.30 1999-05-12 00:20:00 peter
  839. * removed R_DEFAULT_SEG
  840. * uniform float names
  841. Revision 1.29 1999/05/09 11:37:05 peter
  842. * fixed order of arguments for incompatible types message
  843. Revision 1.28 1999/05/06 09:05:34 peter
  844. * generic write_float and str_float
  845. * fixed constant float conversions
  846. Revision 1.27 1999/05/01 13:24:48 peter
  847. * merged nasm compiler
  848. * old asm moved to oldasm/
  849. Revision 1.26 1999/04/26 13:31:58 peter
  850. * release storenumber,double_checksum
  851. Revision 1.25 1999/04/22 10:49:09 peter
  852. * fixed pchar to string location
  853. Revision 1.24 1999/04/21 09:44:01 peter
  854. * storenumber works
  855. * fixed some typos in double_checksum
  856. + incompatible types type1 and type2 message (with storenumber)
  857. Revision 1.23 1999/04/15 08:56:24 peter
  858. * fixed bool-bool conversion
  859. Revision 1.22 1999/04/08 09:47:31 pierre
  860. * warn if uninitilized local vars are used in IS or AS statements
  861. Revision 1.21 1999/03/06 17:25:20 peter
  862. * moved comp<->real warning so it doesn't occure everytime that
  863. isconvertable is called with
  864. Revision 1.20 1999/03/02 18:24:23 peter
  865. * fixed overloading of array of char
  866. Revision 1.19 1999/02/22 02:15:46 peter
  867. * updates for ag386bin
  868. Revision 1.18 1999/01/27 14:56:57 pierre
  869. * typo error corrected solves bug0190 and bug0204
  870. Revision 1.17 1999/01/27 14:15:25 pierre
  871. * bug0209 corrected (introduce while solving other bool to int related bugs)
  872. Revision 1.16 1999/01/27 13:02:21 pierre
  873. boolean to int conversion problems bug0205 bug0208
  874. Revision 1.15 1999/01/27 00:13:57 florian
  875. * "procedure of object"-stuff fixed
  876. Revision 1.14 1999/01/19 12:17:45 peter
  877. * removed rangecheck warning which was shown twice
  878. Revision 1.13 1998/12/30 22:13:47 peter
  879. * if explicit cnv then also handle the ordinal consts direct
  880. Revision 1.12 1998/12/11 00:03:53 peter
  881. + globtype,tokens,version unit splitted from globals
  882. Revision 1.11 1998/12/04 10:18:12 florian
  883. * some stuff for procedures of object added
  884. * bug with overridden virtual constructors fixed (reported by Italo Gomes)
  885. Revision 1.10 1998/11/29 12:40:24 peter
  886. * newcnv -> not oldcnv
  887. Revision 1.9 1998/11/26 13:10:43 peter
  888. * new int - int conversion -dNEWCNV
  889. * some function renamings
  890. Revision 1.8 1998/11/05 12:03:03 peter
  891. * released useansistring
  892. * removed -Sv, its now available in fpc modes
  893. Revision 1.7 1998/10/23 11:58:27 florian
  894. * better code generation for s:=s+[b] if b is in the range of
  895. a small set and s is also a small set
  896. Revision 1.6 1998/10/21 15:12:58 pierre
  897. * bug fix for IOCHECK inside a procedure with iocheck modifier
  898. * removed the GPF for unexistant overloading
  899. (firstcall was called with procedinition=nil !)
  900. * changed typen to what Florian proposed
  901. gentypenode(p : pdef) sets the typenodetype field
  902. and resulttype is only set if inside bt_type block !
  903. Revision 1.5 1998/10/07 10:38:55 peter
  904. * forgot a firstpass in arrayconstruct2set
  905. Revision 1.4 1998/10/05 21:33:32 peter
  906. * fixed 161,165,166,167,168
  907. Revision 1.3 1998/09/27 10:16:26 florian
  908. * type casts pchar<->ansistring fixed
  909. * ansistring[..] calls does now an unique call
  910. Revision 1.2 1998/09/24 23:49:22 peter
  911. + aktmodeswitches
  912. Revision 1.1 1998/09/23 20:42:24 peter
  913. * splitted pass_1
  914. }