tccnv.pas 36 KB

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