tccnv.pas 36 KB

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