tccnv.pas 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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. unit tccnv;
  19. {$i defines.inc}
  20. interface
  21. uses
  22. tree;
  23. procedure arrayconstructor_to_set(var p:ptree);
  24. procedure firsttypeconv(var p : ptree);
  25. procedure firstas(var p : ptree);
  26. procedure firstis(var p : ptree);
  27. implementation
  28. uses
  29. globtype,systems,tokens,
  30. cutils,cobjects,verbose,globals,
  31. symconst,symtable,aasm,types,
  32. {$ifdef newcg}
  33. cgbase,
  34. {$else newcg}
  35. hcodegen,
  36. {$endif newcg}
  37. htypechk,pass_1,cpubase;
  38. {*****************************************************************************
  39. Array constructor to Set Conversion
  40. *****************************************************************************}
  41. procedure arrayconstructor_to_set(var p:ptree);
  42. var
  43. constp,
  44. buildp,
  45. p2,p3,p4 : ptree;
  46. pd : pdef;
  47. constset : pconstset;
  48. constsetlo,
  49. constsethi : longint;
  50. procedure update_constsethi(p:pdef);
  51. begin
  52. if ((p^.deftype=orddef) and
  53. (porddef(p)^.high>=constsethi)) then
  54. begin
  55. constsethi:=porddef(p)^.high;
  56. if pd=nil then
  57. begin
  58. if (constsethi>255) or
  59. (porddef(p)^.low<0) then
  60. pd:=u8bitdef
  61. else
  62. pd:=p;
  63. end;
  64. if constsethi>255 then
  65. constsethi:=255;
  66. end
  67. else if ((p^.deftype=enumdef) and
  68. (penumdef(p)^.max>=constsethi)) then
  69. begin
  70. if pd=nil then
  71. pd:=p;
  72. constsethi:=penumdef(p)^.max;
  73. end;
  74. end;
  75. procedure do_set(pos : longint);
  76. var
  77. mask,l : longint;
  78. begin
  79. if (pos>255) or (pos<0) then
  80. Message(parser_e_illegal_set_expr);
  81. if pos>constsethi then
  82. constsethi:=pos;
  83. if pos<constsetlo then
  84. constsetlo:=pos;
  85. l:=pos shr 3;
  86. mask:=1 shl (pos mod 8);
  87. { do we allow the same twice }
  88. if (constset^[l] and mask)<>0 then
  89. Message(parser_e_illegal_set_expr);
  90. constset^[l]:=constset^[l] or mask;
  91. end;
  92. var
  93. l : longint;
  94. lr,hr : longint;
  95. begin
  96. new(constset);
  97. FillChar(constset^,sizeof(constset^),0);
  98. pd:=nil;
  99. constsetlo:=0;
  100. constsethi:=0;
  101. constp:=gensinglenode(setconstn,nil);
  102. constp^.value_set:=constset;
  103. buildp:=constp;
  104. if assigned(p^.left) then
  105. begin
  106. while assigned(p) do
  107. begin
  108. p4:=nil; { will contain the tree to create the set }
  109. { split a range into p2 and p3 }
  110. if p^.left^.treetype=arrayconstructrangen then
  111. begin
  112. p2:=p^.left^.left;
  113. p3:=p^.left^.right;
  114. { node is not used anymore }
  115. putnode(p^.left);
  116. end
  117. else
  118. begin
  119. p2:=p^.left;
  120. p3:=nil;
  121. end;
  122. firstpass(p2);
  123. if assigned(p3) then
  124. firstpass(p3);
  125. if codegenerror then
  126. break;
  127. case p2^.resulttype^.deftype of
  128. enumdef,
  129. orddef:
  130. begin
  131. getrange(p2^.resulttype,lr,hr);
  132. if assigned(p3) then
  133. begin
  134. { this isn't good, you'll get problems with
  135. type t010 = 0..10;
  136. ts = set of t010;
  137. var s : ts;b : t010
  138. begin s:=[1,2,b]; end.
  139. if is_integer(p3^.resulttype) then
  140. begin
  141. p3:=gentypeconvnode(p3,u8bitdef);
  142. firstpass(p3);
  143. end;
  144. }
  145. if assigned(pd) and not(is_equal(pd,p3^.resulttype)) then
  146. begin
  147. aktfilepos:=p3^.fileinfo;
  148. CGMessage(type_e_typeconflict_in_set);
  149. end
  150. else
  151. begin
  152. if (p2^.treetype=ordconstn) and (p3^.treetype=ordconstn) then
  153. begin
  154. if not(is_integer(p3^.resulttype)) then
  155. pd:=p3^.resulttype
  156. else
  157. begin
  158. p3:=gentypeconvnode(p3,u8bitdef);
  159. p2:=gentypeconvnode(p2,u8bitdef);
  160. firstpass(p2);
  161. firstpass(p3);
  162. end;
  163. for l:=p2^.value to p3^.value do
  164. do_set(l);
  165. disposetree(p3);
  166. disposetree(p2);
  167. end
  168. else
  169. begin
  170. update_constsethi(p2^.resulttype);
  171. p2:=gentypeconvnode(p2,pd);
  172. firstpass(p2);
  173. update_constsethi(p3^.resulttype);
  174. p3:=gentypeconvnode(p3,pd);
  175. firstpass(p3);
  176. if assigned(pd) then
  177. p3:=gentypeconvnode(p3,pd)
  178. else
  179. p3:=gentypeconvnode(p3,u8bitdef);
  180. firstpass(p3);
  181. p4:=gennode(setelementn,p2,p3);
  182. end;
  183. end;
  184. end
  185. else
  186. begin
  187. { Single value }
  188. if p2^.treetype=ordconstn then
  189. begin
  190. if not(is_integer(p2^.resulttype)) then
  191. update_constsethi(p2^.resulttype)
  192. else
  193. begin
  194. p2:=gentypeconvnode(p2,u8bitdef);
  195. firstpass(p2);
  196. end;
  197. do_set(p2^.value);
  198. disposetree(p2);
  199. end
  200. else
  201. begin
  202. update_constsethi(p2^.resulttype);
  203. if assigned(pd) then
  204. p2:=gentypeconvnode(p2,pd)
  205. else
  206. p2:=gentypeconvnode(p2,u8bitdef);
  207. firstpass(p2);
  208. p4:=gennode(setelementn,p2,nil);
  209. end;
  210. end;
  211. end;
  212. stringdef : begin
  213. { if we've already set elements which are constants }
  214. { throw an error }
  215. if ((pd=nil) and assigned(buildp)) or
  216. not(is_equal(pd,cchardef)) then
  217. CGMessage(type_e_typeconflict_in_set)
  218. else
  219. for l:=1 to length(pstring(p2^.value_str)^) do
  220. do_set(ord(pstring(p2^.value_str)^[l]));
  221. if pd=nil then
  222. pd:=cchardef;
  223. disposetree(p2);
  224. end;
  225. else
  226. CGMessage(type_e_ordinal_expr_expected);
  227. end;
  228. { insert the set creation tree }
  229. if assigned(p4) then
  230. buildp:=gennode(addn,buildp,p4);
  231. { load next and dispose current node }
  232. p2:=p;
  233. p:=p^.right;
  234. putnode(p2);
  235. end;
  236. if (pd=nil) then
  237. begin
  238. pd:=u8bitdef;
  239. constsethi:=255;
  240. end;
  241. end
  242. else
  243. begin
  244. { empty set [], only remove node }
  245. putnode(p);
  246. end;
  247. { set the initial set type }
  248. constp^.resulttype:=new(psetdef,init(pd,constsethi));
  249. { set the new tree }
  250. p:=buildp;
  251. end;
  252. {*****************************************************************************
  253. FirstTypeConv
  254. *****************************************************************************}
  255. type
  256. tfirstconvproc = procedure(var p : ptree);
  257. procedure first_int_to_int(var p : ptree);
  258. begin
  259. if (p^.left^.location.loc<>LOC_REGISTER) and
  260. (p^.resulttype^.size>p^.left^.resulttype^.size) then
  261. p^.location.loc:=LOC_REGISTER;
  262. if is_64bitint(p^.resulttype) then
  263. p^.registers32:=max(p^.registers32,2)
  264. else
  265. p^.registers32:=max(p^.registers32,1);
  266. end;
  267. procedure first_cstring_to_pchar(var p : ptree);
  268. begin
  269. p^.registers32:=1;
  270. p^.location.loc:=LOC_REGISTER;
  271. end;
  272. procedure first_string_to_chararray(var p : ptree);
  273. begin
  274. p^.registers32:=1;
  275. p^.location.loc:=LOC_REGISTER;
  276. end;
  277. procedure first_string_to_string(var p : ptree);
  278. var
  279. hp : ptree;
  280. begin
  281. if pstringdef(p^.resulttype)^.string_typ<>
  282. pstringdef(p^.left^.resulttype)^.string_typ then
  283. begin
  284. if p^.left^.treetype=stringconstn then
  285. begin
  286. p^.left^.stringtype:=pstringdef(p^.resulttype)^.string_typ;
  287. p^.left^.resulttype:=p^.resulttype;
  288. { remove typeconv node }
  289. hp:=p;
  290. p:=p^.left;
  291. putnode(hp);
  292. exit;
  293. end
  294. else
  295. procinfo^.flags:=procinfo^.flags or pi_do_call;
  296. end;
  297. { for simplicity lets first keep all ansistrings
  298. as LOC_MEM, could also become LOC_REGISTER }
  299. if pstringdef(p^.resulttype)^.string_typ in [st_ansistring,st_widestring] then
  300. { we may use ansistrings so no fast exit here }
  301. procinfo^.no_fast_exit:=true;
  302. p^.location.loc:=LOC_MEM;
  303. end;
  304. procedure first_char_to_string(var p : ptree);
  305. var
  306. hp : ptree;
  307. begin
  308. if p^.left^.treetype=ordconstn then
  309. begin
  310. hp:=genstringconstnode(chr(p^.left^.value),st_default);
  311. hp^.stringtype:=pstringdef(p^.resulttype)^.string_typ;
  312. firstpass(hp);
  313. disposetree(p);
  314. p:=hp;
  315. end
  316. else
  317. p^.location.loc:=LOC_MEM;
  318. end;
  319. procedure first_nothing(var p : ptree);
  320. begin
  321. p^.location.loc:=LOC_MEM;
  322. end;
  323. procedure first_array_to_pointer(var p : ptree);
  324. begin
  325. if p^.registers32<1 then
  326. p^.registers32:=1;
  327. p^.location.loc:=LOC_REGISTER;
  328. end;
  329. procedure first_int_to_real(var p : ptree);
  330. var
  331. t : ptree;
  332. begin
  333. if p^.left^.treetype=ordconstn then
  334. begin
  335. t:=genrealconstnode(p^.left^.value,pfloatdef(p^.resulttype));
  336. firstpass(t);
  337. disposetree(p);
  338. p:=t;
  339. exit;
  340. end;
  341. if p^.registersfpu<1 then
  342. p^.registersfpu:=1;
  343. p^.location.loc:=LOC_FPU;
  344. end;
  345. procedure first_int_to_fix(var p : ptree);
  346. var
  347. t : ptree;
  348. begin
  349. if p^.left^.treetype=ordconstn then
  350. begin
  351. t:=genfixconstnode(p^.left^.value shl 16,p^.resulttype);
  352. firstpass(t);
  353. disposetree(p);
  354. p:=t;
  355. exit;
  356. end;
  357. if p^.registers32<1 then
  358. p^.registers32:=1;
  359. p^.location.loc:=LOC_REGISTER;
  360. end;
  361. procedure first_real_to_fix(var p : ptree);
  362. var
  363. t : ptree;
  364. begin
  365. if p^.left^.treetype=fixconstn then
  366. begin
  367. t:=genfixconstnode(round(p^.left^.value_real*65536),p^.resulttype);
  368. firstpass(t);
  369. disposetree(p);
  370. p:=t;
  371. exit;
  372. end;
  373. { at least one fpu and int register needed }
  374. if p^.registers32<1 then
  375. p^.registers32:=1;
  376. if p^.registersfpu<1 then
  377. p^.registersfpu:=1;
  378. p^.location.loc:=LOC_REGISTER;
  379. end;
  380. procedure first_fix_to_real(var p : ptree);
  381. var
  382. t : ptree;
  383. begin
  384. if p^.left^.treetype=fixconstn then
  385. begin
  386. t:=genrealconstnode(round(p^.left^.value_fix/65536.0),p^.resulttype);
  387. firstpass(t);
  388. disposetree(p);
  389. p:=t;
  390. exit;
  391. end;
  392. if p^.registersfpu<1 then
  393. p^.registersfpu:=1;
  394. p^.location.loc:=LOC_FPU;
  395. end;
  396. procedure first_real_to_real(var p : ptree);
  397. var
  398. t : ptree;
  399. begin
  400. if p^.left^.treetype=realconstn then
  401. begin
  402. t:=genrealconstnode(p^.left^.value_real,p^.resulttype);
  403. firstpass(t);
  404. disposetree(p);
  405. p:=t;
  406. exit;
  407. end;
  408. { comp isn't a floating type }
  409. {$ifdef i386}
  410. if (pfloatdef(p^.resulttype)^.typ=s64comp) and
  411. (pfloatdef(p^.left^.resulttype)^.typ<>s64comp) and
  412. not (p^.explizit) then
  413. CGMessage(type_w_convert_real_2_comp);
  414. {$endif}
  415. if p^.registersfpu<1 then
  416. p^.registersfpu:=1;
  417. p^.location.loc:=LOC_FPU;
  418. end;
  419. procedure first_pointer_to_array(var p : ptree);
  420. begin
  421. if p^.registers32<1 then
  422. p^.registers32:=1;
  423. p^.location.loc:=LOC_REFERENCE;
  424. end;
  425. procedure first_chararray_to_string(var p : ptree);
  426. begin
  427. { the only important information is the location of the }
  428. { result }
  429. { other stuff is done by firsttypeconv }
  430. p^.location.loc:=LOC_MEM;
  431. end;
  432. procedure first_cchar_to_pchar(var p : ptree);
  433. begin
  434. p^.left:=gentypeconvnode(p^.left,cshortstringdef);
  435. { convert constant char to constant string }
  436. firstpass(p^.left);
  437. { evalute tree }
  438. firstpass(p);
  439. end;
  440. procedure first_bool_to_int(var p : ptree);
  441. begin
  442. { byte(boolean) or word(wordbool) or longint(longbool) must
  443. be accepted for var parameters }
  444. if (p^.explizit) and
  445. (p^.left^.resulttype^.size=p^.resulttype^.size) and
  446. (p^.left^.location.loc in [LOC_REFERENCE,LOC_MEM,LOC_CREGISTER]) then
  447. exit;
  448. p^.location.loc:=LOC_REGISTER;
  449. if p^.registers32<1 then
  450. p^.registers32:=1;
  451. end;
  452. procedure first_int_to_bool(var p : ptree);
  453. begin
  454. { byte(boolean) or word(wordbool) or longint(longbool) must
  455. be accepted for var parameters }
  456. if (p^.explizit) and
  457. (p^.left^.resulttype^.size=p^.resulttype^.size) and
  458. (p^.left^.location.loc in [LOC_REFERENCE,LOC_MEM,LOC_CREGISTER]) then
  459. exit;
  460. p^.location.loc:=LOC_REGISTER;
  461. { need if bool to bool !!
  462. not very nice !!
  463. p^.left:=gentypeconvnode(p^.left,s32bitdef);
  464. p^.left^.explizit:=true;
  465. firstpass(p^.left); }
  466. if p^.registers32<1 then
  467. p^.registers32:=1;
  468. end;
  469. procedure first_bool_to_bool(var p : ptree);
  470. begin
  471. p^.location.loc:=LOC_REGISTER;
  472. if p^.registers32<1 then
  473. p^.registers32:=1;
  474. end;
  475. procedure first_proc_to_procvar(var p : ptree);
  476. begin
  477. { hmmm, I'am not sure if that is necessary (FK) }
  478. firstpass(p^.left);
  479. if codegenerror then
  480. exit;
  481. if (p^.left^.location.loc<>LOC_REFERENCE) then
  482. CGMessage(cg_e_illegal_expression);
  483. p^.registers32:=p^.left^.registers32;
  484. if p^.registers32<1 then
  485. p^.registers32:=1;
  486. p^.location.loc:=LOC_REGISTER;
  487. end;
  488. procedure first_load_smallset(var p : ptree);
  489. begin
  490. end;
  491. procedure first_cord_to_pointer(var p : ptree);
  492. var
  493. t : ptree;
  494. begin
  495. if p^.left^.treetype=ordconstn then
  496. begin
  497. t:=genpointerconstnode(p^.left^.value,p^.resulttype);
  498. firstpass(t);
  499. disposetree(p);
  500. p:=t;
  501. exit;
  502. end
  503. else
  504. internalerror(432472389);
  505. end;
  506. procedure first_pchar_to_string(var p : ptree);
  507. begin
  508. p^.location.loc:=LOC_REFERENCE;
  509. end;
  510. procedure first_ansistring_to_pchar(var p : ptree);
  511. begin
  512. p^.location.loc:=LOC_REGISTER;
  513. if p^.registers32<1 then
  514. p^.registers32:=1;
  515. end;
  516. procedure first_arrayconstructor_to_set(var p:ptree);
  517. var
  518. hp : ptree;
  519. begin
  520. if p^.left^.treetype<>arrayconstructn then
  521. internalerror(5546);
  522. { remove typeconv node }
  523. hp:=p;
  524. p:=p^.left;
  525. putnode(hp);
  526. { create a set constructor tree }
  527. arrayconstructor_to_set(p);
  528. { now firstpass the set }
  529. firstpass(p);
  530. end;
  531. procedure firsttypeconv(var p : ptree);
  532. var
  533. hp : ptree;
  534. aprocdef : pprocdef;
  535. const
  536. firstconvert : array[tconverttype] of tfirstconvproc = (
  537. first_nothing, {equal}
  538. first_nothing, {not_possible}
  539. first_string_to_string,
  540. first_char_to_string,
  541. first_pchar_to_string,
  542. first_cchar_to_pchar,
  543. first_cstring_to_pchar,
  544. first_ansistring_to_pchar,
  545. first_string_to_chararray,
  546. first_chararray_to_string,
  547. first_array_to_pointer,
  548. first_pointer_to_array,
  549. first_int_to_int,
  550. first_int_to_bool,
  551. first_bool_to_bool,
  552. first_bool_to_int,
  553. first_real_to_real,
  554. first_int_to_real,
  555. first_int_to_fix,
  556. first_real_to_fix,
  557. first_fix_to_real,
  558. first_proc_to_procvar,
  559. first_arrayconstructor_to_set,
  560. first_load_smallset,
  561. first_cord_to_pointer
  562. );
  563. begin
  564. aprocdef:=nil;
  565. { if explicite type cast, then run firstpass }
  566. if (p^.explizit) or not assigned(p^.left^.resulttype) then
  567. firstpass(p^.left);
  568. if (p^.left^.treetype=typen) and (p^.left^.resulttype=generrordef) then
  569. begin
  570. codegenerror:=true;
  571. Message(parser_e_no_type_not_allowed_here);
  572. end;
  573. if codegenerror then
  574. begin
  575. p^.resulttype:=generrordef;
  576. exit;
  577. end;
  578. if not assigned(p^.left^.resulttype) then
  579. begin
  580. codegenerror:=true;
  581. internalerror(52349);
  582. exit;
  583. end;
  584. { load the value_str from the left part }
  585. p^.registers32:=p^.left^.registers32;
  586. p^.registersfpu:=p^.left^.registersfpu;
  587. {$ifdef SUPPORT_MMX}
  588. p^.registersmmx:=p^.left^.registersmmx;
  589. {$endif}
  590. set_location(p^.location,p^.left^.location);
  591. { remove obsolete type conversions }
  592. if is_equal(p^.left^.resulttype,p^.resulttype) then
  593. begin
  594. { becuase is_equal only checks the basetype for sets we need to
  595. check here if we are loading a smallset into a normalset }
  596. if (p^.resulttype^.deftype=setdef) and
  597. (p^.left^.resulttype^.deftype=setdef) and
  598. (psetdef(p^.resulttype)^.settype<>smallset) and
  599. (psetdef(p^.left^.resulttype)^.settype=smallset) then
  600. begin
  601. { try to define the set as a normalset if it's a constant set }
  602. if p^.left^.treetype=setconstn then
  603. begin
  604. p^.resulttype:=p^.left^.resulttype;
  605. psetdef(p^.resulttype)^.settype:=normset
  606. end
  607. else
  608. p^.convtyp:=tc_load_smallset;
  609. exit;
  610. end
  611. else
  612. begin
  613. hp:=p;
  614. p:=p^.left;
  615. p^.resulttype:=hp^.resulttype;
  616. putnode(hp);
  617. exit;
  618. end;
  619. end;
  620. aprocdef:=assignment_overloaded(p^.left^.resulttype,p^.resulttype);
  621. if assigned(aprocdef) then
  622. begin
  623. procinfo^.flags:=procinfo^.flags or pi_do_call;
  624. hp:=gencallnode(overloaded_operators[_assignment],nil);
  625. { tell explicitly which def we must use !! (PM) }
  626. hp^.procdefinition:=aprocdef;
  627. hp^.left:=gencallparanode(p^.left,nil);
  628. putnode(p);
  629. p:=hp;
  630. firstpass(p);
  631. exit;
  632. end;
  633. if isconvertable(p^.left^.resulttype,p^.resulttype,p^.convtyp,p^.left^.treetype,p^.explizit)=0 then
  634. begin
  635. {Procedures have a resulttype of voiddef and functions of their
  636. own resulttype. They will therefore always be incompatible with
  637. a procvar. Because isconvertable cannot check for procedures we
  638. use an extra check for them.}
  639. if (m_tp_procvar in aktmodeswitches) then
  640. begin
  641. if (p^.resulttype^.deftype=procvardef) and
  642. (is_procsym_load(p^.left) or is_procsym_call(p^.left)) then
  643. begin
  644. if is_procsym_call(p^.left) then
  645. begin
  646. {if p^.left^.right=nil then
  647. begin}
  648. if (p^.left^.symtableprocentry^.owner^.symtabletype=objectsymtable){ and
  649. (pobjectdef(p^.left^.symtableprocentry^.owner^.defowner)^.is_class) }then
  650. hp:=genloadmethodcallnode(pprocsym(p^.left^.symtableprocentry),p^.left^.symtableproc,
  651. getcopy(p^.left^.methodpointer))
  652. else
  653. hp:=genloadcallnode(pprocsym(p^.left^.symtableprocentry),p^.left^.symtableproc);
  654. disposetree(p^.left);
  655. firstpass(hp);
  656. p^.left:=hp;
  657. aprocdef:=pprocdef(p^.left^.resulttype);
  658. (* end
  659. else
  660. begin
  661. p^.left^.right^.treetype:=loadn;
  662. p^.left^.right^.symtableentry:=p^.left^.right^.symtableentry;
  663. P^.left^.right^.resulttype:=pvarsym(p^.left^.symtableentry)^.definition;
  664. hp:=p^.left^.right;
  665. putnode(p^.left);
  666. p^.left:=hp;
  667. { should we do that ? }
  668. firstpass(p^.left);
  669. if not is_equal(p^.left^.resulttype,p^.resulttype) then
  670. begin
  671. CGMessage(type_e_mismatch);
  672. exit;
  673. end
  674. else
  675. begin
  676. hp:=p;
  677. p:=p^.left;
  678. p^.resulttype:=hp^.resulttype;
  679. putnode(hp);
  680. exit;
  681. end;
  682. end; *)
  683. end
  684. else
  685. begin
  686. if (p^.left^.treetype<>addrn) then
  687. aprocdef:=pprocsym(p^.left^.symtableentry)^.definition;
  688. end;
  689. p^.convtyp:=tc_proc_2_procvar;
  690. { Now check if the procedure we are going to assign to
  691. the procvar, is compatible with the procvar's type }
  692. if assigned(aprocdef) then
  693. begin
  694. if not proc_to_procvar_equal(aprocdef,pprocvardef(p^.resulttype)) then
  695. CGMessage2(type_e_incompatible_types,aprocdef^.typename,p^.resulttype^.typename);
  696. firstconvert[p^.convtyp](p);
  697. end
  698. else
  699. CGMessage2(type_e_incompatible_types,p^.left^.resulttype^.typename,p^.resulttype^.typename);
  700. exit;
  701. end;
  702. end;
  703. if p^.explizit then
  704. begin
  705. { check if the result could be in a register }
  706. if not(p^.resulttype^.is_intregable) and
  707. not(p^.resulttype^.is_fpuregable) then
  708. make_not_regable(p^.left);
  709. { boolean to byte are special because the
  710. location can be different }
  711. if is_integer(p^.resulttype) and
  712. is_boolean(p^.left^.resulttype) then
  713. begin
  714. p^.convtyp:=tc_bool_2_int;
  715. firstconvert[p^.convtyp](p);
  716. exit;
  717. end;
  718. { ansistring to pchar }
  719. if is_pchar(p^.resulttype) and
  720. is_ansistring(p^.left^.resulttype) then
  721. begin
  722. p^.convtyp:=tc_ansistring_2_pchar;
  723. firstconvert[p^.convtyp](p);
  724. exit;
  725. end;
  726. { do common tc_equal cast }
  727. p^.convtyp:=tc_equal;
  728. { enum to ordinal will always be s32bit }
  729. if (p^.left^.resulttype^.deftype=enumdef) and
  730. is_ordinal(p^.resulttype) then
  731. begin
  732. if p^.left^.treetype=ordconstn then
  733. begin
  734. hp:=genordinalconstnode(p^.left^.value,p^.resulttype);
  735. disposetree(p);
  736. firstpass(hp);
  737. p:=hp;
  738. exit;
  739. end
  740. else
  741. begin
  742. if isconvertable(s32bitdef,p^.resulttype,p^.convtyp,ordconstn,false)=0 then
  743. CGMessage2(type_e_incompatible_types,p^.left^.resulttype^.typename,p^.resulttype^.typename);
  744. end;
  745. end
  746. { ordinal to enumeration }
  747. else
  748. if (p^.resulttype^.deftype=enumdef) and
  749. is_ordinal(p^.left^.resulttype) then
  750. begin
  751. if p^.left^.treetype=ordconstn then
  752. begin
  753. hp:=genordinalconstnode(p^.left^.value,p^.resulttype);
  754. disposetree(p);
  755. firstpass(hp);
  756. p:=hp;
  757. exit;
  758. end
  759. else
  760. begin
  761. if IsConvertable(p^.left^.resulttype,s32bitdef,p^.convtyp,ordconstn,false)=0 then
  762. CGMessage2(type_e_incompatible_types,p^.left^.resulttype^.typename,p^.resulttype^.typename);
  763. end;
  764. end
  765. { nil to ordinal node }
  766. else if is_ordinal(p^.resulttype) and
  767. (p^.left^.treetype=niln) then
  768. begin
  769. hp:=genordinalconstnode(0,p^.resulttype);
  770. firstpass(hp);
  771. disposetree(p);
  772. p:=hp;
  773. exit;
  774. end
  775. {Are we typecasting an ordconst to a char?}
  776. else
  777. if is_char(p^.resulttype) and
  778. is_ordinal(p^.left^.resulttype) then
  779. begin
  780. if p^.left^.treetype=ordconstn then
  781. begin
  782. hp:=genordinalconstnode(p^.left^.value,p^.resulttype);
  783. firstpass(hp);
  784. disposetree(p);
  785. p:=hp;
  786. exit;
  787. end
  788. else
  789. begin
  790. if IsConvertable(p^.left^.resulttype,u8bitdef,p^.convtyp,ordconstn,false)=0 then
  791. CGMessage2(type_e_incompatible_types,p^.left^.resulttype^.typename,p^.resulttype^.typename);
  792. end;
  793. end
  794. { Are we char to ordinal }
  795. else
  796. if is_char(p^.left^.resulttype) and
  797. is_ordinal(p^.resulttype) then
  798. begin
  799. if p^.left^.treetype=ordconstn then
  800. begin
  801. hp:=genordinalconstnode(p^.left^.value,p^.resulttype);
  802. firstpass(hp);
  803. disposetree(p);
  804. p:=hp;
  805. exit;
  806. end
  807. else
  808. begin
  809. if IsConvertable(u8bitdef,p^.resulttype,p^.convtyp,ordconstn,false)=0 then
  810. CGMessage2(type_e_incompatible_types,p^.left^.resulttype^.typename,p^.resulttype^.typename);
  811. end;
  812. end
  813. { only if the same size or formal def }
  814. { why do we allow typecasting of voiddef ?? (PM) }
  815. else
  816. begin
  817. if not(
  818. (p^.left^.resulttype^.deftype=formaldef) or
  819. (p^.left^.resulttype^.size=p^.resulttype^.size) or
  820. (is_equal(p^.left^.resulttype,voiddef) and
  821. (p^.left^.treetype=derefn))
  822. ) then
  823. CGMessage(cg_e_illegal_type_conversion);
  824. if ((p^.left^.resulttype^.deftype=orddef) and
  825. (p^.resulttype^.deftype=pointerdef)) or
  826. ((p^.resulttype^.deftype=orddef) and
  827. (p^.left^.resulttype^.deftype=pointerdef))
  828. {$ifdef extdebug}and (p^.firstpasscount=0){$endif} then
  829. CGMessage(cg_d_pointer_to_longint_conv_not_portable);
  830. end;
  831. { the conversion into a strutured type is only }
  832. { possible, if the source is no register }
  833. if ((p^.resulttype^.deftype in [recorddef,stringdef,arraydef]) or
  834. ((p^.resulttype^.deftype=objectdef) and not(pobjectdef(p^.resulttype)^.is_class))
  835. ) and (p^.left^.location.loc in [LOC_REGISTER,LOC_CREGISTER]) { and
  836. it also works if the assignment is overloaded
  837. YES but this code is not executed if assignment is overloaded (PM)
  838. not assigned(assignment_overloaded(p^.left^.resulttype,p^.resulttype))} then
  839. CGMessage(cg_e_illegal_type_conversion);
  840. end
  841. else
  842. CGMessage2(type_e_incompatible_types,p^.left^.resulttype^.typename,p^.resulttype^.typename);
  843. end;
  844. { tp7 procvar support, when right is not a procvardef and we got a
  845. loadn of a procvar then convert to a calln, the check for the
  846. result is already done in is_convertible, also no conflict with
  847. @procvar is here because that has an extra addrn }
  848. if (m_tp_procvar in aktmodeswitches) and
  849. (p^.resulttype^.deftype<>procvardef) and
  850. (p^.left^.resulttype^.deftype=procvardef) and
  851. (p^.left^.treetype=loadn) then
  852. begin
  853. hp:=gencallnode(nil,nil);
  854. hp^.right:=p^.left;
  855. firstpass(hp);
  856. p^.left:=hp;
  857. end;
  858. { ordinal contants can be directly converted }
  859. { but not int64/qword }
  860. if (p^.left^.treetype=ordconstn) and is_ordinal(p^.resulttype) and
  861. not(is_64bitint(p^.resulttype)) then
  862. begin
  863. { range checking is done in genordinalconstnode (PFV) }
  864. hp:=genordinalconstnode(p^.left^.value,p^.resulttype);
  865. disposetree(p);
  866. firstpass(hp);
  867. p:=hp;
  868. exit;
  869. end;
  870. if p^.convtyp<>tc_equal then
  871. firstconvert[p^.convtyp](p);
  872. end;
  873. {*****************************************************************************
  874. FirstIs
  875. *****************************************************************************}
  876. procedure firstis(var p : ptree);
  877. begin
  878. firstpass(p^.left);
  879. set_varstate(p^.left,true);
  880. firstpass(p^.right);
  881. set_varstate(p^.right,true);
  882. if codegenerror then
  883. exit;
  884. if (p^.right^.resulttype^.deftype<>classrefdef) then
  885. CGMessage(type_e_mismatch);
  886. left_right_max(p);
  887. { left must be a class }
  888. if (p^.left^.resulttype^.deftype<>objectdef) or
  889. not(pobjectdef(p^.left^.resulttype)^.is_class) then
  890. CGMessage(type_e_mismatch);
  891. { the operands must be related }
  892. if (not(pobjectdef(p^.left^.resulttype)^.is_related(
  893. pobjectdef(pclassrefdef(p^.right^.resulttype)^.pointertype.def)))) and
  894. (not(pobjectdef(pclassrefdef(p^.right^.resulttype)^.pointertype.def)^.is_related(
  895. pobjectdef(p^.left^.resulttype)))) then
  896. CGMessage(type_e_mismatch);
  897. p^.location.loc:=LOC_FLAGS;
  898. p^.resulttype:=booldef;
  899. end;
  900. {*****************************************************************************
  901. FirstAs
  902. *****************************************************************************}
  903. procedure firstas(var p : ptree);
  904. begin
  905. firstpass(p^.right);
  906. set_varstate(p^.right,true);
  907. firstpass(p^.left);
  908. set_varstate(p^.left,true);
  909. if codegenerror then
  910. exit;
  911. if (p^.right^.resulttype^.deftype<>classrefdef) then
  912. CGMessage(type_e_mismatch);
  913. left_right_max(p);
  914. { left must be a class }
  915. if (p^.left^.resulttype^.deftype<>objectdef) or
  916. not(pobjectdef(p^.left^.resulttype)^.is_class) then
  917. CGMessage(type_e_mismatch);
  918. { the operands must be related }
  919. if (not(pobjectdef(p^.left^.resulttype)^.is_related(
  920. pobjectdef(pclassrefdef(p^.right^.resulttype)^.pointertype.def)))) and
  921. (not(pobjectdef(pclassrefdef(p^.right^.resulttype)^.pointertype.def)^.is_related(
  922. pobjectdef(p^.left^.resulttype)))) then
  923. CGMessage(type_e_mismatch);
  924. set_location(p^.location,p^.left^.location);
  925. p^.resulttype:=pclassrefdef(p^.right^.resulttype)^.pointertype.def;
  926. end;
  927. end.
  928. {
  929. $Log$
  930. Revision 1.9 2000-09-24 21:19:52 peter
  931. * delphi compile fixes
  932. Revision 1.8 2000/09/24 15:06:31 peter
  933. * use defines.inc
  934. Revision 1.7 2000/08/27 16:11:55 peter
  935. * moved some util functions from globals,cobjects to cutils
  936. * splitted files into finput,fmodule
  937. Revision 1.6 2000/08/26 19:40:19 peter
  938. * integer(char) explicit typecast support (tp7,delphi compatible)
  939. Revision 1.5 2000/08/02 07:20:32 jonas
  940. - undid my changes from the previous two commits because it was a bug
  941. in cg386cnv which I've now fixed (previous changes only masked it in
  942. some cases) (merged from fixes branch)
  943. Revision 1.4 2000/08/01 10:41:35 jonas
  944. * refined my previous IE(10) fix (in some cases, too many registers could
  945. be reserved) (merged from fixes branch)
  946. Revision 1.3 2000/07/21 09:23:47 jonas
  947. * merged from fixes branch
  948. Revision 1.2 2000/07/13 11:32:51 michael
  949. + removed logs
  950. }