tccnv.pas 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  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=s64bitcomp) and
  365. (pfloatdef(p^.left^.resulttype)^.typ<>s64bitcomp) 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^.resulttype^.typename,p^.left^.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.28 1999-05-06 09:05:34 peter
  839. * generic write_float and str_float
  840. * fixed constant float conversions
  841. Revision 1.27 1999/05/01 13:24:48 peter
  842. * merged nasm compiler
  843. * old asm moved to oldasm/
  844. Revision 1.26 1999/04/26 13:31:58 peter
  845. * release storenumber,double_checksum
  846. Revision 1.25 1999/04/22 10:49:09 peter
  847. * fixed pchar to string location
  848. Revision 1.24 1999/04/21 09:44:01 peter
  849. * storenumber works
  850. * fixed some typos in double_checksum
  851. + incompatible types type1 and type2 message (with storenumber)
  852. Revision 1.23 1999/04/15 08:56:24 peter
  853. * fixed bool-bool conversion
  854. Revision 1.22 1999/04/08 09:47:31 pierre
  855. * warn if uninitilized local vars are used in IS or AS statements
  856. Revision 1.21 1999/03/06 17:25:20 peter
  857. * moved comp<->real warning so it doesn't occure everytime that
  858. isconvertable is called with
  859. Revision 1.20 1999/03/02 18:24:23 peter
  860. * fixed overloading of array of char
  861. Revision 1.19 1999/02/22 02:15:46 peter
  862. * updates for ag386bin
  863. Revision 1.18 1999/01/27 14:56:57 pierre
  864. * typo error corrected solves bug0190 and bug0204
  865. Revision 1.17 1999/01/27 14:15:25 pierre
  866. * bug0209 corrected (introduce while solving other bool to int related bugs)
  867. Revision 1.16 1999/01/27 13:02:21 pierre
  868. boolean to int conversion problems bug0205 bug0208
  869. Revision 1.15 1999/01/27 00:13:57 florian
  870. * "procedure of object"-stuff fixed
  871. Revision 1.14 1999/01/19 12:17:45 peter
  872. * removed rangecheck warning which was shown twice
  873. Revision 1.13 1998/12/30 22:13:47 peter
  874. * if explicit cnv then also handle the ordinal consts direct
  875. Revision 1.12 1998/12/11 00:03:53 peter
  876. + globtype,tokens,version unit splitted from globals
  877. Revision 1.11 1998/12/04 10:18:12 florian
  878. * some stuff for procedures of object added
  879. * bug with overridden virtual constructors fixed (reported by Italo Gomes)
  880. Revision 1.10 1998/11/29 12:40:24 peter
  881. * newcnv -> not oldcnv
  882. Revision 1.9 1998/11/26 13:10:43 peter
  883. * new int - int conversion -dNEWCNV
  884. * some function renamings
  885. Revision 1.8 1998/11/05 12:03:03 peter
  886. * released useansistring
  887. * removed -Sv, its now available in fpc modes
  888. Revision 1.7 1998/10/23 11:58:27 florian
  889. * better code generation for s:=s+[b] if b is in the range of
  890. a small set and s is also a small set
  891. Revision 1.6 1998/10/21 15:12:58 pierre
  892. * bug fix for IOCHECK inside a procedure with iocheck modifier
  893. * removed the GPF for unexistant overloading
  894. (firstcall was called with procedinition=nil !)
  895. * changed typen to what Florian proposed
  896. gentypenode(p : pdef) sets the typenodetype field
  897. and resulttype is only set if inside bt_type block !
  898. Revision 1.5 1998/10/07 10:38:55 peter
  899. * forgot a firstpass in arrayconstruct2set
  900. Revision 1.4 1998/10/05 21:33:32 peter
  901. * fixed 161,165,166,167,168
  902. Revision 1.3 1998/09/27 10:16:26 florian
  903. * type casts pchar<->ansistring fixed
  904. * ansistring[..] calls does now an unique call
  905. Revision 1.2 1998/09/24 23:49:22 peter
  906. + aktmodeswitches
  907. Revision 1.1 1998/09/23 20:42:24 peter
  908. * splitted pass_1
  909. }