2
0

tccnv.pas 35 KB

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