tccnv.pas 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  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. ,i386base
  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. procedure first_int_to_int(var p : ptree);
  216. begin
  217. if (p^.registers32=0) and
  218. (p^.left^.location.loc<>LOC_REGISTER) and
  219. (p^.resulttype^.size>p^.left^.resulttype^.size) then
  220. begin
  221. p^.registers32:=1;
  222. p^.location.loc:=LOC_REGISTER;
  223. end;
  224. end;
  225. procedure first_cstring_to_pchar(var p : ptree);
  226. begin
  227. p^.registers32:=1;
  228. p^.location.loc:=LOC_REGISTER;
  229. end;
  230. procedure first_string_to_chararray(var p : ptree);
  231. begin
  232. p^.registers32:=1;
  233. p^.location.loc:=LOC_REGISTER;
  234. end;
  235. procedure first_string_to_string(var p : ptree);
  236. begin
  237. if pstringdef(p^.resulttype)^.string_typ<>
  238. pstringdef(p^.left^.resulttype)^.string_typ then
  239. begin
  240. if p^.left^.treetype=stringconstn then
  241. begin
  242. p^.left^.stringtype:=pstringdef(p^.resulttype)^.string_typ;
  243. { we don't have to do anything, the const }
  244. { node generates an ansistring }
  245. p^.convtyp:=tc_equal;
  246. end
  247. else
  248. procinfo.flags:=procinfo.flags or pi_do_call;
  249. end;
  250. { for simplicity lets first keep all ansistrings
  251. as LOC_MEM, could also become LOC_REGISTER }
  252. p^.location.loc:=LOC_MEM;
  253. end;
  254. procedure first_char_to_string(var p : ptree);
  255. var
  256. hp : ptree;
  257. begin
  258. if p^.left^.treetype=ordconstn then
  259. begin
  260. hp:=genstringconstnode(chr(p^.left^.value));
  261. hp^.stringtype:=pstringdef(p^.resulttype)^.string_typ;
  262. firstpass(hp);
  263. disposetree(p);
  264. p:=hp;
  265. end
  266. else
  267. p^.location.loc:=LOC_MEM;
  268. end;
  269. procedure first_nothing(var p : ptree);
  270. begin
  271. p^.location.loc:=LOC_MEM;
  272. end;
  273. procedure first_array_to_pointer(var p : ptree);
  274. begin
  275. if p^.registers32<1 then
  276. p^.registers32:=1;
  277. p^.location.loc:=LOC_REGISTER;
  278. end;
  279. procedure first_int_to_real(var p : ptree);
  280. var
  281. t : ptree;
  282. begin
  283. if p^.left^.treetype=ordconstn then
  284. begin
  285. t:=genrealconstnode(p^.left^.value,pfloatdef(p^.resulttype));
  286. firstpass(t);
  287. disposetree(p);
  288. p:=t;
  289. exit;
  290. end;
  291. if p^.registersfpu<1 then
  292. p^.registersfpu:=1;
  293. p^.location.loc:=LOC_FPU;
  294. end;
  295. procedure first_int_to_fix(var p : ptree);
  296. var
  297. t : ptree;
  298. begin
  299. if p^.left^.treetype=ordconstn then
  300. begin
  301. t:=genfixconstnode(p^.left^.value shl 16,p^.resulttype);
  302. firstpass(t);
  303. disposetree(p);
  304. p:=t;
  305. exit;
  306. end;
  307. if p^.registers32<1 then
  308. p^.registers32:=1;
  309. p^.location.loc:=LOC_REGISTER;
  310. end;
  311. procedure first_real_to_fix(var p : ptree);
  312. var
  313. t : ptree;
  314. begin
  315. if p^.left^.treetype=fixconstn then
  316. begin
  317. t:=genfixconstnode(round(p^.left^.value_real*65536),p^.resulttype);
  318. firstpass(t);
  319. disposetree(p);
  320. p:=t;
  321. exit;
  322. end;
  323. { at least one fpu and int register needed }
  324. if p^.registers32<1 then
  325. p^.registers32:=1;
  326. if p^.registersfpu<1 then
  327. p^.registersfpu:=1;
  328. p^.location.loc:=LOC_REGISTER;
  329. end;
  330. procedure first_fix_to_real(var p : ptree);
  331. var
  332. t : ptree;
  333. begin
  334. if p^.left^.treetype=fixconstn then
  335. begin
  336. t:=genrealconstnode(round(p^.left^.value_fix/65536.0),p^.resulttype);
  337. firstpass(t);
  338. disposetree(p);
  339. p:=t;
  340. exit;
  341. end;
  342. if p^.registersfpu<1 then
  343. p^.registersfpu:=1;
  344. p^.location.loc:=LOC_FPU;
  345. end;
  346. procedure first_real_to_real(var p : ptree);
  347. var
  348. t : ptree;
  349. begin
  350. if p^.left^.treetype=realconstn then
  351. begin
  352. t:=genrealconstnode(p^.left^.value_real,p^.resulttype);
  353. firstpass(t);
  354. disposetree(p);
  355. p:=t;
  356. exit;
  357. end;
  358. { comp isn't a floating type }
  359. {$ifdef i386}
  360. if (pfloatdef(p^.resulttype)^.typ=s64comp) and
  361. (pfloatdef(p^.left^.resulttype)^.typ<>s64comp) and
  362. not (p^.explizit) then
  363. CGMessage(type_w_convert_real_2_comp);
  364. {$endif}
  365. if p^.registersfpu<1 then
  366. p^.registersfpu:=1;
  367. p^.location.loc:=LOC_FPU;
  368. end;
  369. procedure first_pointer_to_array(var p : ptree);
  370. begin
  371. if p^.registers32<1 then
  372. p^.registers32:=1;
  373. p^.location.loc:=LOC_REFERENCE;
  374. end;
  375. procedure first_chararray_to_string(var p : ptree);
  376. begin
  377. { the only important information is the location of the }
  378. { result }
  379. { other stuff is done by firsttypeconv }
  380. p^.location.loc:=LOC_MEM;
  381. end;
  382. procedure first_cchar_to_pchar(var p : ptree);
  383. begin
  384. p^.left:=gentypeconvnode(p^.left,cshortstringdef);
  385. { convert constant char to constant string }
  386. firstpass(p^.left);
  387. { evalute tree }
  388. firstpass(p);
  389. end;
  390. procedure first_bool_to_int(var p : ptree);
  391. begin
  392. { byte(boolean) or word(wordbool) or longint(longbool) must
  393. be accepted for var parameters }
  394. if (p^.explizit) and
  395. (p^.left^.resulttype^.size=p^.resulttype^.size) and
  396. (p^.left^.location.loc in [LOC_REFERENCE,LOC_MEM,LOC_CREGISTER]) then
  397. exit;
  398. p^.location.loc:=LOC_REGISTER;
  399. if p^.registers32<1 then
  400. p^.registers32:=1;
  401. end;
  402. procedure first_int_to_bool(var p : ptree);
  403. begin
  404. { byte(boolean) or word(wordbool) or longint(longbool) must
  405. be accepted for var parameters }
  406. if (p^.explizit) and
  407. (p^.left^.resulttype^.size=p^.resulttype^.size) and
  408. (p^.left^.location.loc in [LOC_REFERENCE,LOC_MEM,LOC_CREGISTER]) then
  409. exit;
  410. p^.location.loc:=LOC_REGISTER;
  411. { need if bool to bool !!
  412. not very nice !!
  413. p^.left:=gentypeconvnode(p^.left,s32bitdef);
  414. p^.left^.explizit:=true;
  415. firstpass(p^.left); }
  416. if p^.registers32<1 then
  417. p^.registers32:=1;
  418. end;
  419. procedure first_bool_to_bool(var p : ptree);
  420. begin
  421. p^.location.loc:=LOC_REGISTER;
  422. if p^.registers32<1 then
  423. p^.registers32:=1;
  424. end;
  425. procedure first_proc_to_procvar(var p : ptree);
  426. begin
  427. { hmmm, I'am not sure if that is necessary (FK) }
  428. firstpass(p^.left);
  429. if codegenerror then
  430. exit;
  431. if (p^.left^.location.loc<>LOC_REFERENCE) then
  432. CGMessage(cg_e_illegal_expression);
  433. p^.registers32:=p^.left^.registers32;
  434. if p^.registers32<1 then
  435. p^.registers32:=1;
  436. p^.location.loc:=LOC_REGISTER;
  437. end;
  438. procedure first_load_smallset(var p : ptree);
  439. begin
  440. end;
  441. procedure first_pchar_to_string(var p : ptree);
  442. begin
  443. p^.location.loc:=LOC_REFERENCE;
  444. end;
  445. procedure first_ansistring_to_pchar(var p : ptree);
  446. begin
  447. p^.location.loc:=LOC_REGISTER;
  448. if p^.registers32<1 then
  449. p^.registers32:=1;
  450. end;
  451. procedure first_arrayconstructor_to_set(var p:ptree);
  452. var
  453. hp : ptree;
  454. begin
  455. if p^.left^.treetype<>arrayconstructn then
  456. internalerror(5546);
  457. { remove typeconv node }
  458. hp:=p;
  459. p:=p^.left;
  460. putnode(hp);
  461. { create a set constructor tree }
  462. arrayconstructor_to_set(p);
  463. { now firstpass the set }
  464. firstpass(p);
  465. end;
  466. procedure firsttypeconv(var p : ptree);
  467. var
  468. hp : ptree;
  469. aprocdef : pprocdef;
  470. const
  471. firstconvert : array[tconverttype] of tfirstconvproc = (
  472. first_nothing, {equal}
  473. first_nothing, {not_possible}
  474. first_string_to_string,
  475. first_char_to_string,
  476. first_pchar_to_string,
  477. first_cchar_to_pchar,
  478. first_cstring_to_pchar,
  479. first_ansistring_to_pchar,
  480. first_string_to_chararray,
  481. first_chararray_to_string,
  482. first_array_to_pointer,
  483. first_pointer_to_array,
  484. first_int_to_int,
  485. first_int_to_bool,
  486. first_bool_to_bool,
  487. first_bool_to_int,
  488. first_real_to_real,
  489. first_int_to_real,
  490. first_int_to_fix,
  491. first_real_to_fix,
  492. first_fix_to_real,
  493. first_proc_to_procvar,
  494. first_arrayconstructor_to_set,
  495. first_load_smallset
  496. );
  497. begin
  498. aprocdef:=nil;
  499. { if explicite type cast, then run firstpass }
  500. if p^.explizit then
  501. firstpass(p^.left);
  502. if (p^.left^.treetype=typen) and (p^.left^.resulttype=generrordef) then
  503. begin
  504. codegenerror:=true;
  505. Message(parser_e_no_type_not_allowed_here);
  506. end;
  507. if codegenerror then
  508. begin
  509. p^.resulttype:=generrordef;
  510. exit;
  511. end;
  512. if not assigned(p^.left^.resulttype) then
  513. begin
  514. codegenerror:=true;
  515. internalerror(52349);
  516. exit;
  517. end;
  518. { load the value_str from the left part }
  519. p^.registers32:=p^.left^.registers32;
  520. p^.registersfpu:=p^.left^.registersfpu;
  521. {$ifdef SUPPORT_MMX}
  522. p^.registersmmx:=p^.left^.registersmmx;
  523. {$endif}
  524. set_location(p^.location,p^.left^.location);
  525. { remove obsolete type conversions }
  526. if is_equal(p^.left^.resulttype,p^.resulttype) then
  527. begin
  528. { becuase is_equal only checks the basetype for sets we need to
  529. check here if we are loading a smallset into a normalset }
  530. if (p^.resulttype^.deftype=setdef) and
  531. (p^.left^.resulttype^.deftype=setdef) and
  532. (psetdef(p^.resulttype)^.settype<>smallset) and
  533. (psetdef(p^.left^.resulttype)^.settype=smallset) then
  534. begin
  535. { try to define the set as a normalset if it's a constant set }
  536. if p^.left^.treetype=setconstn then
  537. begin
  538. p^.resulttype:=p^.left^.resulttype;
  539. psetdef(p^.resulttype)^.settype:=normset
  540. end
  541. else
  542. p^.convtyp:=tc_load_smallset;
  543. exit;
  544. end
  545. else
  546. begin
  547. hp:=p;
  548. p:=p^.left;
  549. p^.resulttype:=hp^.resulttype;
  550. putnode(hp);
  551. exit;
  552. end;
  553. end;
  554. aprocdef:=assignment_overloaded(p^.left^.resulttype,p^.resulttype);
  555. if assigned(aprocdef) then
  556. begin
  557. procinfo.flags:=procinfo.flags or pi_do_call;
  558. hp:=gencallnode(overloaded_operators[assignment],nil);
  559. { tell explicitly which def we must use !! (PM) }
  560. hp^.procdefinition:=aprocdef;
  561. hp^.left:=gencallparanode(p^.left,nil);
  562. putnode(p);
  563. p:=hp;
  564. firstpass(p);
  565. exit;
  566. end;
  567. if isconvertable(p^.left^.resulttype,p^.resulttype,p^.convtyp,p^.left^.treetype,p^.explizit)=0 then
  568. begin
  569. {Procedures have a resulttype of voiddef and functions of their
  570. own resulttype. They will therefore always be incompatible with
  571. a procvar. Because isconvertable cannot check for procedures we
  572. use an extra check for them.}
  573. if (m_tp_procvar in aktmodeswitches) then
  574. begin
  575. if (p^.resulttype^.deftype=procvardef) and
  576. (is_procsym_load(p^.left) or is_procsym_call(p^.left)) then
  577. begin
  578. if is_procsym_call(p^.left) then
  579. begin
  580. if p^.left^.right=nil then
  581. begin
  582. p^.left^.treetype:=loadn;
  583. { are at same offset so this could be spared, but
  584. it more secure to do it anyway }
  585. p^.left^.symtableentry:=p^.left^.symtableprocentry;
  586. p^.left^.resulttype:=pprocsym(p^.left^.symtableentry)^.definition;
  587. aprocdef:=pprocdef(p^.left^.resulttype);
  588. end
  589. else
  590. begin
  591. p^.left^.right^.treetype:=loadn;
  592. p^.left^.right^.symtableentry:=p^.left^.right^.symtableentry;
  593. P^.left^.right^.resulttype:=pvarsym(p^.left^.symtableentry)^.definition;
  594. hp:=p^.left^.right;
  595. putnode(p^.left);
  596. p^.left:=hp;
  597. { should we do that ? }
  598. firstpass(p^.left);
  599. if not is_equal(p^.left^.resulttype,p^.resulttype) then
  600. begin
  601. CGMessage(type_e_mismatch);
  602. exit;
  603. end
  604. else
  605. begin
  606. hp:=p;
  607. p:=p^.left;
  608. p^.resulttype:=hp^.resulttype;
  609. putnode(hp);
  610. exit;
  611. end;
  612. end;
  613. end
  614. else
  615. begin
  616. if (p^.left^.treetype<>addrn) then
  617. aprocdef:=pprocsym(p^.left^.symtableentry)^.definition;
  618. end;
  619. p^.convtyp:=tc_proc_2_procvar;
  620. { Now check if the procedure we are going to assign to
  621. the procvar, is compatible with the procvar's type }
  622. if assigned(aprocdef) then
  623. begin
  624. if not proc_to_procvar_equal(aprocdef,pprocvardef(p^.resulttype)) then
  625. CGMessage2(type_e_incompatible_types,aprocdef^.typename,p^.resulttype^.typename);
  626. firstconvert[p^.convtyp](p);
  627. end
  628. else
  629. CGMessage2(type_e_incompatible_types,p^.left^.resulttype^.typename,p^.resulttype^.typename);
  630. exit;
  631. end;
  632. end;
  633. if p^.explizit then
  634. begin
  635. { boolean to byte are special because the
  636. location can be different }
  637. if is_integer(p^.resulttype) and
  638. is_boolean(p^.left^.resulttype) then
  639. begin
  640. p^.convtyp:=tc_bool_2_int;
  641. firstconvert[p^.convtyp](p);
  642. exit;
  643. end;
  644. { ansistring to pchar }
  645. if is_pchar(p^.resulttype) and
  646. is_ansistring(p^.left^.resulttype) then
  647. begin
  648. p^.convtyp:=tc_ansistring_2_pchar;
  649. firstconvert[p^.convtyp](p);
  650. exit;
  651. end;
  652. { do common tc_equal cast }
  653. p^.convtyp:=tc_equal;
  654. { enum to ordinal will always be s32bit }
  655. if (p^.left^.resulttype^.deftype=enumdef) and
  656. is_ordinal(p^.resulttype) then
  657. begin
  658. if p^.left^.treetype=ordconstn then
  659. begin
  660. hp:=genordinalconstnode(p^.left^.value,p^.resulttype);
  661. disposetree(p);
  662. firstpass(hp);
  663. p:=hp;
  664. exit;
  665. end
  666. else
  667. begin
  668. if isconvertable(s32bitdef,p^.resulttype,p^.convtyp,ordconstn,false)=0 then
  669. CGMessage(cg_e_illegal_type_conversion);
  670. end;
  671. end
  672. { ordinal to enumeration }
  673. else
  674. if (p^.resulttype^.deftype=enumdef) and
  675. is_ordinal(p^.left^.resulttype) then
  676. begin
  677. if p^.left^.treetype=ordconstn then
  678. begin
  679. hp:=genordinalconstnode(p^.left^.value,p^.resulttype);
  680. disposetree(p);
  681. firstpass(hp);
  682. p:=hp;
  683. exit;
  684. end
  685. else
  686. begin
  687. if IsConvertable(p^.left^.resulttype,s32bitdef,p^.convtyp,ordconstn,false)=0 then
  688. CGMessage(cg_e_illegal_type_conversion);
  689. end;
  690. end
  691. {Are we typecasting an ordconst to a char?}
  692. else
  693. if is_char(p^.resulttype) and
  694. is_ordinal(p^.left^.resulttype) then
  695. begin
  696. if p^.left^.treetype=ordconstn then
  697. begin
  698. hp:=genordinalconstnode(p^.left^.value,p^.resulttype);
  699. firstpass(hp);
  700. disposetree(p);
  701. p:=hp;
  702. exit;
  703. end
  704. else
  705. begin
  706. { this is wrong because it converts to a 4 byte long var !!
  707. if not isconvertable(p^.left^.resulttype,s32bitdef,p^.convtyp,ordconstn nur Dummy ) then }
  708. if IsConvertable(p^.left^.resulttype,u8bitdef,p^.convtyp,ordconstn,false)=0 then
  709. CGMessage(cg_e_illegal_type_conversion);
  710. end;
  711. end
  712. { only if the same size or formal def }
  713. { why do we allow typecasting of voiddef ?? (PM) }
  714. else
  715. begin
  716. if not(
  717. (p^.left^.resulttype^.deftype=formaldef) or
  718. (p^.left^.resulttype^.size=p^.resulttype^.size) or
  719. (is_equal(p^.left^.resulttype,voiddef) and
  720. (p^.left^.treetype=derefn))
  721. ) then
  722. CGMessage(cg_e_illegal_type_conversion);
  723. if ((p^.left^.resulttype^.deftype=orddef) and
  724. (p^.resulttype^.deftype=pointerdef)) or
  725. ((p^.resulttype^.deftype=orddef) and
  726. (p^.left^.resulttype^.deftype=pointerdef))
  727. {$ifdef extdebug}and (p^.firstpasscount=0){$endif} then
  728. CGMessage(cg_d_pointer_to_longint_conv_not_portable);
  729. end;
  730. { the conversion into a strutured type is only }
  731. { possible, if the source is no register }
  732. if ((p^.resulttype^.deftype in [recorddef,stringdef,arraydef]) or
  733. ((p^.resulttype^.deftype=objectdef) and not(pobjectdef(p^.resulttype)^.isclass))
  734. ) and (p^.left^.location.loc in [LOC_REGISTER,LOC_CREGISTER]) { and
  735. it also works if the assignment is overloaded
  736. YES but this code is not executed if assignment is overloaded (PM)
  737. not assigned(assignment_overloaded(p^.left^.resulttype,p^.resulttype))} then
  738. CGMessage(cg_e_illegal_type_conversion);
  739. end
  740. else
  741. CGMessage2(type_e_incompatible_types,p^.left^.resulttype^.typename,p^.resulttype^.typename);
  742. end;
  743. { ordinal contants can be directly converted }
  744. if (p^.left^.treetype=ordconstn) and is_ordinal(p^.resulttype) then
  745. begin
  746. { range checking is done in genordinalconstnode (PFV) }
  747. hp:=genordinalconstnode(p^.left^.value,p^.resulttype);
  748. disposetree(p);
  749. firstpass(hp);
  750. p:=hp;
  751. exit;
  752. end;
  753. if p^.convtyp<>tc_equal then
  754. firstconvert[p^.convtyp](p);
  755. end;
  756. {*****************************************************************************
  757. FirstIs
  758. *****************************************************************************}
  759. procedure firstis(var p : ptree);
  760. var
  761. Store_valid : boolean;
  762. begin
  763. Store_valid:=Must_be_valid;
  764. Must_be_valid:=true;
  765. firstpass(p^.left);
  766. firstpass(p^.right);
  767. Must_be_valid:=Store_valid;
  768. if codegenerror then
  769. exit;
  770. if (p^.right^.resulttype^.deftype<>classrefdef) then
  771. CGMessage(type_e_mismatch);
  772. left_right_max(p);
  773. { left must be a class }
  774. if (p^.left^.resulttype^.deftype<>objectdef) or
  775. not(pobjectdef(p^.left^.resulttype)^.isclass) then
  776. CGMessage(type_e_mismatch);
  777. { the operands must be related }
  778. if (not(pobjectdef(p^.left^.resulttype)^.isrelated(
  779. pobjectdef(pclassrefdef(p^.right^.resulttype)^.definition)))) and
  780. (not(pobjectdef(pclassrefdef(p^.right^.resulttype)^.definition)^.isrelated(
  781. pobjectdef(p^.left^.resulttype)))) then
  782. CGMessage(type_e_mismatch);
  783. p^.location.loc:=LOC_FLAGS;
  784. p^.resulttype:=booldef;
  785. end;
  786. {*****************************************************************************
  787. FirstAs
  788. *****************************************************************************}
  789. procedure firstas(var p : ptree);
  790. var
  791. Store_valid : boolean;
  792. begin
  793. Store_valid:=Must_be_valid;
  794. Must_be_valid:=true;
  795. firstpass(p^.right);
  796. firstpass(p^.left);
  797. Must_be_valid:=Store_valid;
  798. if codegenerror then
  799. exit;
  800. if (p^.right^.resulttype^.deftype<>classrefdef) then
  801. CGMessage(type_e_mismatch);
  802. left_right_max(p);
  803. { left must be a class }
  804. if (p^.left^.resulttype^.deftype<>objectdef) or
  805. not(pobjectdef(p^.left^.resulttype)^.isclass) then
  806. CGMessage(type_e_mismatch);
  807. { the operands must be related }
  808. if (not(pobjectdef(p^.left^.resulttype)^.isrelated(
  809. pobjectdef(pclassrefdef(p^.right^.resulttype)^.definition)))) and
  810. (not(pobjectdef(pclassrefdef(p^.right^.resulttype)^.definition)^.isrelated(
  811. pobjectdef(p^.left^.resulttype)))) then
  812. CGMessage(type_e_mismatch);
  813. set_location(p^.location,p^.left^.location);
  814. p^.resulttype:=pclassrefdef(p^.right^.resulttype)^.definition;
  815. end;
  816. end.
  817. {
  818. $Log$
  819. Revision 1.38 1999-06-17 13:19:58 pierre
  820. * merged from 0_99_12 branch
  821. Revision 1.35.2.3 1999/06/17 12:51:48 pierre
  822. * changed is_assignment_overloaded into
  823. function assignment_overloaded : pprocdef
  824. to allow overloading of assignment with only different result type
  825. Revision 1.37 1999/06/15 18:58:35 peter
  826. * merged
  827. Revision 1.36 1999/06/13 22:41:06 peter
  828. * merged from fixes
  829. Revision 1.35.2.2 1999/06/15 18:54:53 peter
  830. * more procvar fixes
  831. Revision 1.35.2.1 1999/06/13 22:39:19 peter
  832. * use proc_to_procvar_equal
  833. Revision 1.35 1999/06/02 22:44:24 pierre
  834. * previous wrong log corrected
  835. Revision 1.34 1999/06/02 22:25:54 pierre
  836. * changed $ifdef FPC @ into $ifndef TP
  837. + debug note about longint to pointer conversion
  838. Revision 1.33 1999/05/27 19:45:15 peter
  839. * removed oldasm
  840. * plabel -> pasmlabel
  841. * -a switches to source writing automaticly
  842. * assembler readers OOPed
  843. * asmsymbol automaticly external
  844. * jumptables and other label fixes for asm readers
  845. Revision 1.32 1999/05/20 14:58:28 peter
  846. * fixed arrayconstruct->set conversion which didn't work for enum sets
  847. Revision 1.31 1999/05/13 21:59:52 peter
  848. * removed oldppu code
  849. * warning if objpas is loaded from uses
  850. * first things for new deref writing
  851. Revision 1.30 1999/05/12 00:20:00 peter
  852. * removed R_DEFAULT_SEG
  853. * uniform float names
  854. Revision 1.29 1999/05/09 11:37:05 peter
  855. * fixed order of arguments for incompatible types message
  856. Revision 1.28 1999/05/06 09:05:34 peter
  857. * generic write_float and str_float
  858. * fixed constant float conversions
  859. Revision 1.27 1999/05/01 13:24:48 peter
  860. * merged nasm compiler
  861. * old asm moved to oldasm/
  862. Revision 1.26 1999/04/26 13:31:58 peter
  863. * release storenumber,double_checksum
  864. Revision 1.25 1999/04/22 10:49:09 peter
  865. * fixed pchar to string location
  866. Revision 1.24 1999/04/21 09:44:01 peter
  867. * storenumber works
  868. * fixed some typos in double_checksum
  869. + incompatible types type1 and type2 message (with storenumber)
  870. Revision 1.23 1999/04/15 08:56:24 peter
  871. * fixed bool-bool conversion
  872. Revision 1.22 1999/04/08 09:47:31 pierre
  873. * warn if uninitilized local vars are used in IS or AS statements
  874. Revision 1.21 1999/03/06 17:25:20 peter
  875. * moved comp<->real warning so it doesn't occure everytime that
  876. isconvertable is called with
  877. Revision 1.20 1999/03/02 18:24:23 peter
  878. * fixed overloading of array of char
  879. Revision 1.19 1999/02/22 02:15:46 peter
  880. * updates for ag386bin
  881. Revision 1.18 1999/01/27 14:56:57 pierre
  882. * typo error corrected solves bug0190 and bug0204
  883. Revision 1.17 1999/01/27 14:15:25 pierre
  884. * bug0209 corrected (introduce while solving other bool to int related bugs)
  885. Revision 1.16 1999/01/27 13:02:21 pierre
  886. boolean to int conversion problems bug0205 bug0208
  887. Revision 1.15 1999/01/27 00:13:57 florian
  888. * "procedure of object"-stuff fixed
  889. Revision 1.14 1999/01/19 12:17:45 peter
  890. * removed rangecheck warning which was shown twice
  891. Revision 1.13 1998/12/30 22:13:47 peter
  892. * if explicit cnv then also handle the ordinal consts direct
  893. Revision 1.12 1998/12/11 00:03:53 peter
  894. + globtype,tokens,version unit splitted from globals
  895. Revision 1.11 1998/12/04 10:18:12 florian
  896. * some stuff for procedures of object added
  897. * bug with overridden virtual constructors fixed (reported by Italo Gomes)
  898. Revision 1.10 1998/11/29 12:40:24 peter
  899. * newcnv -> not oldcnv
  900. Revision 1.9 1998/11/26 13:10:43 peter
  901. * new int - int conversion -dNEWCNV
  902. * some function renamings
  903. Revision 1.8 1998/11/05 12:03:03 peter
  904. * released useansistring
  905. * removed -Sv, its now available in fpc modes
  906. Revision 1.7 1998/10/23 11:58:27 florian
  907. * better code generation for s:=s+[b] if b is in the range of
  908. a small set and s is also a small set
  909. Revision 1.6 1998/10/21 15:12:58 pierre
  910. * bug fix for IOCHECK inside a procedure with iocheck modifier
  911. * removed the GPF for unexistant overloading
  912. (firstcall was called with procedinition=nil !)
  913. * changed typen to what Florian proposed
  914. gentypenode(p : pdef) sets the typenodetype field
  915. and resulttype is only set if inside bt_type block !
  916. Revision 1.5 1998/10/07 10:38:55 peter
  917. * forgot a firstpass in arrayconstruct2set
  918. Revision 1.4 1998/10/05 21:33:32 peter
  919. * fixed 161,165,166,167,168
  920. Revision 1.3 1998/09/27 10:16:26 florian
  921. * type casts pchar<->ansistring fixed
  922. * ansistring[..] calls does now an unique call
  923. Revision 1.2 1998/09/24 23:49:22 peter
  924. + aktmodeswitches
  925. Revision 1.1 1998/09/23 20:42:24 peter
  926. * splitted pass_1
  927. }