tccnv.pas 36 KB

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