tccnv.pas 35 KB

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