tccnv.pas 35 KB

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