tccnv.pas 33 KB

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