ncnv.pas 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254
  1. {
  2. $Id$
  3. Copyright (c) 2000 by Florian Klaempfl
  4. Type checking and register allocation for type converting nodes
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit ncnv;
  19. {$i defines.inc}
  20. interface
  21. uses
  22. node,
  23. symtype,types,
  24. nld;
  25. type
  26. ttypeconvnode = class(tunarynode)
  27. convtype : tconverttype;
  28. constructor create(node : tnode;t : pdef);virtual;
  29. function getcopy : tnode;override;
  30. function pass_1 : tnode;override;
  31. function first_int_to_int : tnode;virtual;
  32. function first_cstring_to_pchar : tnode;virtual;
  33. function first_string_to_chararray : tnode;virtual;
  34. function first_string_to_string : tnode;virtual;
  35. function first_char_to_string : tnode;virtual;
  36. function first_nothing : tnode;virtual;
  37. function first_array_to_pointer : tnode;virtual;
  38. function first_int_to_real : tnode;virtual;
  39. function first_int_to_fix : tnode;virtual;
  40. function first_real_to_fix : tnode;virtual;
  41. function first_fix_to_real : tnode;virtual;
  42. function first_real_to_real : tnode;virtual;
  43. function first_pointer_to_array : tnode;virtual;
  44. function first_chararray_to_string : tnode;virtual;
  45. function first_cchar_to_pchar : tnode;virtual;
  46. function first_bool_to_int : tnode;virtual;
  47. function first_int_to_bool : tnode;virtual;
  48. function first_bool_to_bool : tnode;virtual;
  49. function first_proc_to_procvar : tnode;virtual;
  50. function first_load_smallset : tnode;virtual;
  51. function first_cord_to_pointer : tnode;virtual;
  52. function first_pchar_to_string : tnode;virtual;
  53. function first_ansistring_to_pchar : tnode;virtual;
  54. function first_arrayconstructor_to_set : tnode;virtual;
  55. function first_class_to_intf : tnode;virtual;
  56. function first_call_helper(c : tconverttype) : tnode;
  57. end;
  58. tasnode = class(tbinarynode)
  59. constructor create(l,r : tnode);virtual;
  60. function pass_1 : tnode;override;
  61. end;
  62. tisnode = class(tbinarynode)
  63. constructor create(l,r : tnode);virtual;
  64. function pass_1 : tnode;override;
  65. end;
  66. var
  67. ctypeconvnode : class of ttypeconvnode;
  68. casnode : class of tasnode;
  69. cisnode : class of tisnode;
  70. function gentypeconvnode(node : tnode;t : pdef) : ttypeconvnode;
  71. procedure arrayconstructor_to_set(var p : tarrayconstructornode);
  72. implementation
  73. uses
  74. globtype,systems,tokens,
  75. cutils,verbose,globals,
  76. symconst,symdef,symsym,symtable,
  77. ncon,ncal,nset,nadd,
  78. {$ifdef newcg}
  79. cgbase,
  80. {$else newcg}
  81. hcodegen,
  82. {$endif newcg}
  83. htypechk,pass_1,cpubase;
  84. function gentypeconvnode(node : tnode;t : pdef) : ttypeconvnode;
  85. begin
  86. gentypeconvnode:=ctypeconvnode.create(node,t);
  87. end;
  88. {*****************************************************************************
  89. Array constructor to Set Conversion
  90. *****************************************************************************}
  91. procedure arrayconstructor_to_set(var p : tarrayconstructornode);
  92. var
  93. constp : tsetconstnode;
  94. buildp,
  95. p2,p3,p4 : tnode;
  96. pd : pdef;
  97. constset : pconstset;
  98. constsetlo,
  99. constsethi : longint;
  100. procedure update_constsethi(p:pdef);
  101. begin
  102. if ((p^.deftype=orddef) and
  103. (porddef(p)^.high>=constsethi)) then
  104. begin
  105. constsethi:=porddef(p)^.high;
  106. if pd=nil then
  107. begin
  108. if (constsethi>255) or
  109. (porddef(p)^.low<0) then
  110. pd:=u8bitdef
  111. else
  112. pd:=p;
  113. end;
  114. if constsethi>255 then
  115. constsethi:=255;
  116. end
  117. else if ((p^.deftype=enumdef) and
  118. (penumdef(p)^.max>=constsethi)) then
  119. begin
  120. if pd=nil then
  121. pd:=p;
  122. constsethi:=penumdef(p)^.max;
  123. end;
  124. end;
  125. procedure do_set(pos : longint);
  126. var
  127. mask,l : longint;
  128. begin
  129. if (pos>255) or (pos<0) then
  130. Message(parser_e_illegal_set_expr);
  131. if pos>constsethi then
  132. constsethi:=pos;
  133. if pos<constsetlo then
  134. constsetlo:=pos;
  135. l:=pos shr 3;
  136. mask:=1 shl (pos mod 8);
  137. { do we allow the same twice }
  138. if (constset^[l] and mask)<>0 then
  139. Message(parser_e_illegal_set_expr);
  140. constset^[l]:=constset^[l] or mask;
  141. end;
  142. var
  143. l : longint;
  144. lr,hr : longint;
  145. begin
  146. new(constset);
  147. FillChar(constset^,sizeof(constset^),0);
  148. pd:=nil;
  149. constsetlo:=0;
  150. constsethi:=0;
  151. constp:=csetconstnode.create(nil,nil);
  152. constp.value_set:=constset;
  153. buildp:=constp;
  154. if assigned(p.left) then
  155. begin
  156. while assigned(p) do
  157. begin
  158. p4:=nil; { will contain the tree to create the set }
  159. {split a range into p2 and p3 }
  160. if p.left.nodetype=arrayconstructorrangen then
  161. begin
  162. p2:=tarrayconstructorrangenode(p.left).left;
  163. p3:=tarrayconstructorrangenode(p.left).right;
  164. tarrayconstructorrangenode(p.left).left:=nil;
  165. tarrayconstructorrangenode(p.left).right:=nil;
  166. end
  167. else
  168. begin
  169. p2:=p.left;
  170. p.left:=nil;
  171. p3:=nil;
  172. end;
  173. firstpass(p2);
  174. if assigned(p3) then
  175. firstpass(p3);
  176. if codegenerror then
  177. break;
  178. case p2.resulttype^.deftype of
  179. enumdef,
  180. orddef:
  181. begin
  182. getrange(p2.resulttype,lr,hr);
  183. if assigned(p3) then
  184. begin
  185. { this isn't good, you'll get problems with
  186. type t010 = 0..10;
  187. ts = set of t010;
  188. var s : ts;b : t010
  189. begin s:=[1,2,b]; end.
  190. if is_integer(p3^.resulttype) then
  191. begin
  192. p3:=gentypeconvnode(p3,u8bitdef);
  193. firstpass(p3);
  194. end;
  195. }
  196. if assigned(pd) and not(is_equal(pd,p3.resulttype)) then
  197. begin
  198. aktfilepos:=p3.fileinfo;
  199. CGMessage(type_e_typeconflict_in_set);
  200. end
  201. else
  202. begin
  203. if (p2.nodetype=ordconstn) and (p3.nodetype=ordconstn) then
  204. begin
  205. if not(is_integer(p3.resulttype)) then
  206. pd:=p3.resulttype
  207. else
  208. begin
  209. p3:=gentypeconvnode(p3,u8bitdef);
  210. p2:=gentypeconvnode(p2,u8bitdef);
  211. firstpass(p2);
  212. firstpass(p3);
  213. end;
  214. for l:=tordconstnode(p2).value to tordconstnode(p3).value do
  215. do_set(l);
  216. p2.free;
  217. p3.free;
  218. end
  219. else
  220. begin
  221. update_constsethi(p2.resulttype);
  222. p2:=gentypeconvnode(p2,pd);
  223. firstpass(p2);
  224. update_constsethi(p3.resulttype);
  225. p3:=gentypeconvnode(p3,pd);
  226. firstpass(p3);
  227. if assigned(pd) then
  228. p3:=gentypeconvnode(p3,pd)
  229. else
  230. p3:=gentypeconvnode(p3,u8bitdef);
  231. firstpass(p3);
  232. p4:=csetelementnode.create(p2,p3);
  233. end;
  234. end;
  235. end
  236. else
  237. begin
  238. { Single value }
  239. if p2.nodetype=ordconstn then
  240. begin
  241. if not(is_integer(p2.resulttype)) then
  242. update_constsethi(p2.resulttype)
  243. else
  244. begin
  245. p2:=gentypeconvnode(p2,u8bitdef);
  246. firstpass(p2);
  247. end;
  248. do_set(tordconstnode(p2).value);
  249. p2.free;
  250. end
  251. else
  252. begin
  253. update_constsethi(p2.resulttype);
  254. if assigned(pd) then
  255. p2:=gentypeconvnode(p2,pd)
  256. else
  257. p2:=gentypeconvnode(p2,u8bitdef);
  258. firstpass(p2);
  259. p4:=csetelementnode.create(p2,nil);
  260. end;
  261. end;
  262. end;
  263. stringdef : begin
  264. { if we've already set elements which are constants }
  265. { throw an error }
  266. if ((pd=nil) and assigned(buildp)) or
  267. not(is_equal(pd,cchardef)) then
  268. CGMessage(type_e_typeconflict_in_set)
  269. else
  270. for l:=1 to length(pstring(tstringconstnode(p2).value_str)^) do
  271. do_set(ord(pstring(tstringconstnode(p2).value_str)^[l]));
  272. if pd=nil then
  273. pd:=cchardef;
  274. p2.free;
  275. end;
  276. else
  277. CGMessage(type_e_ordinal_expr_expected);
  278. end;
  279. { insert the set creation tree }
  280. if assigned(p4) then
  281. buildp:=caddnode.create(addn,buildp,p4);
  282. { load next and dispose current node }
  283. p2:=p;
  284. p:=tarrayconstructornode(tarrayconstructornode(p2).right);
  285. tarrayconstructornode(p2).right:=nil;
  286. p2.free;
  287. end;
  288. if (pd=nil) then
  289. begin
  290. pd:=u8bitdef;
  291. constsethi:=255;
  292. end;
  293. end
  294. else
  295. begin
  296. { empty set [], only remove node }
  297. p.free;
  298. end;
  299. { set the initial set type }
  300. constp.resulttype:=new(psetdef,init(pd,constsethi));
  301. { set the new tree }
  302. p:=tarrayconstructornode(buildp);
  303. end;
  304. {*****************************************************************************
  305. TTYPECONVNODE
  306. *****************************************************************************}
  307. constructor ttypeconvnode.create(node : tnode;t : pdef);
  308. begin
  309. inherited create(typeconvn,node);
  310. convtype:=tc_not_possible;
  311. resulttype:=t;
  312. set_file_line(node);
  313. end;
  314. function ttypeconvnode.getcopy : tnode;
  315. var
  316. n : ttypeconvnode;
  317. begin
  318. n:=ttypeconvnode(inherited getcopy);
  319. n.convtype:=convtype;
  320. getcopy:=n;
  321. end;
  322. function ttypeconvnode.first_int_to_int : tnode;
  323. begin
  324. first_int_to_int:=nil;
  325. if (left.location.loc<>LOC_REGISTER) and
  326. (resulttype^.size>left.resulttype^.size) then
  327. location.loc:=LOC_REGISTER;
  328. if is_64bitint(resulttype) then
  329. registers32:=max(registers32,2)
  330. else
  331. registers32:=max(registers32,1);
  332. end;
  333. function ttypeconvnode.first_cstring_to_pchar : tnode;
  334. begin
  335. first_cstring_to_pchar:=nil;
  336. registers32:=1;
  337. location.loc:=LOC_REGISTER;
  338. end;
  339. function ttypeconvnode.first_string_to_chararray : tnode;
  340. begin
  341. first_string_to_chararray:=nil;
  342. registers32:=1;
  343. location.loc:=LOC_REGISTER;
  344. end;
  345. function ttypeconvnode.first_string_to_string : tnode;
  346. begin
  347. first_string_to_string:=nil;
  348. if pstringdef(resulttype)^.string_typ<>
  349. pstringdef(left.resulttype)^.string_typ then
  350. begin
  351. if left.nodetype=stringconstn then
  352. begin
  353. tstringconstnode(left).stringtype:=pstringdef(resulttype)^.string_typ;
  354. tstringconstnode(left).resulttype:=resulttype;
  355. { remove typeconv node }
  356. first_string_to_string:=left;
  357. left:=nil;
  358. exit;
  359. end
  360. else
  361. procinfo^.flags:=procinfo^.flags or pi_do_call;
  362. end;
  363. { for simplicity lets first keep all ansistrings
  364. as LOC_MEM, could also become LOC_REGISTER }
  365. if pstringdef(resulttype)^.string_typ in [st_ansistring,st_widestring] then
  366. { we may use ansistrings so no fast exit here }
  367. procinfo^.no_fast_exit:=true;
  368. location.loc:=LOC_MEM;
  369. end;
  370. function ttypeconvnode.first_char_to_string : tnode;
  371. var
  372. hp : tstringconstnode;
  373. begin
  374. first_char_to_string:=nil;
  375. if left.nodetype=ordconstn then
  376. begin
  377. hp:=genstringconstnode(chr(tordconstnode(left).value),st_default);
  378. hp.stringtype:=pstringdef(resulttype)^.string_typ;
  379. firstpass(hp);
  380. first_char_to_string:=hp;
  381. end
  382. else
  383. location.loc:=LOC_MEM;
  384. end;
  385. function ttypeconvnode.first_nothing : tnode;
  386. begin
  387. first_nothing:=nil;
  388. location.loc:=LOC_MEM;
  389. end;
  390. function ttypeconvnode.first_array_to_pointer : tnode;
  391. begin
  392. first_array_to_pointer:=nil;
  393. if registers32<1 then
  394. registers32:=1;
  395. location.loc:=LOC_REGISTER;
  396. end;
  397. function ttypeconvnode.first_int_to_real : tnode;
  398. var
  399. t : trealconstnode;
  400. begin
  401. first_int_to_real:=nil;
  402. if left.nodetype=ordconstn then
  403. begin
  404. t:=genrealconstnode(tordconstnode(left).value,pfloatdef(resulttype));
  405. firstpass(t);
  406. first_int_to_real:=t;
  407. exit;
  408. end;
  409. if registersfpu<1 then
  410. registersfpu:=1;
  411. location.loc:=LOC_FPU;
  412. end;
  413. function ttypeconvnode.first_int_to_fix : tnode;
  414. var
  415. t : tnode;
  416. begin
  417. first_int_to_fix:=nil;
  418. if left.nodetype=ordconstn then
  419. begin
  420. t:=genfixconstnode(tordconstnode(left).value shl 16,resulttype);
  421. firstpass(t);
  422. first_int_to_fix:=t;
  423. exit;
  424. end;
  425. if registers32<1 then
  426. registers32:=1;
  427. location.loc:=LOC_REGISTER;
  428. end;
  429. function ttypeconvnode.first_real_to_fix : tnode;
  430. var
  431. t : tnode;
  432. begin
  433. first_real_to_fix:=nil;
  434. if left.nodetype=realconstn then
  435. begin
  436. t:=genfixconstnode(round(trealconstnode(left).value_real*65536),resulttype);
  437. firstpass(t);
  438. first_real_to_fix:=t;
  439. exit;
  440. end;
  441. { at least one fpu and int register needed }
  442. if registers32<1 then
  443. registers32:=1;
  444. if registersfpu<1 then
  445. registersfpu:=1;
  446. location.loc:=LOC_REGISTER;
  447. end;
  448. function ttypeconvnode.first_fix_to_real : tnode;
  449. var
  450. t : tnode;
  451. begin
  452. first_fix_to_real:=nil;
  453. if left.nodetype=fixconstn then
  454. begin
  455. t:=genrealconstnode(round(tfixconstnode(left).value_fix/65536.0),resulttype);
  456. firstpass(t);
  457. first_fix_to_real:=t;
  458. exit;
  459. end;
  460. if registersfpu<1 then
  461. registersfpu:=1;
  462. location.loc:=LOC_FPU;
  463. end;
  464. function ttypeconvnode.first_real_to_real : tnode;
  465. var
  466. t : tnode;
  467. begin
  468. first_real_to_real:=nil;
  469. if left.nodetype=realconstn then
  470. begin
  471. t:=genrealconstnode(trealconstnode(left).value_real,resulttype);
  472. firstpass(t);
  473. first_real_to_real:=t;
  474. exit;
  475. end;
  476. { comp isn't a floating type }
  477. {$ifdef i386}
  478. if (pfloatdef(resulttype)^.typ=s64comp) and
  479. (pfloatdef(left.resulttype)^.typ<>s64comp) and
  480. not (nf_explizit in flags) then
  481. CGMessage(type_w_convert_real_2_comp);
  482. {$endif}
  483. if registersfpu<1 then
  484. registersfpu:=1;
  485. location.loc:=LOC_FPU;
  486. end;
  487. function ttypeconvnode.first_pointer_to_array : tnode;
  488. begin
  489. first_pointer_to_array:=nil;
  490. if registers32<1 then
  491. registers32:=1;
  492. location.loc:=LOC_REFERENCE;
  493. end;
  494. function ttypeconvnode.first_chararray_to_string : tnode;
  495. begin
  496. first_chararray_to_string:=nil;
  497. { the only important information is the location of the }
  498. { result }
  499. { other stuff is done by firsttypeconv }
  500. location.loc:=LOC_MEM;
  501. end;
  502. function ttypeconvnode.first_cchar_to_pchar : tnode;
  503. begin
  504. first_cchar_to_pchar:=nil;
  505. left:=gentypeconvnode(left,cshortstringdef);
  506. { convert constant char to constant string }
  507. firstpass(left);
  508. { evalute tree }
  509. first_cchar_to_pchar:=pass_1;
  510. end;
  511. function ttypeconvnode.first_bool_to_int : tnode;
  512. begin
  513. first_bool_to_int:=nil;
  514. { byte(boolean) or word(wordbool) or longint(longbool) must
  515. be accepted for var parameters }
  516. if (nf_explizit in flags) and
  517. (left.resulttype^.size=resulttype^.size) and
  518. (left.location.loc in [LOC_REFERENCE,LOC_MEM,LOC_CREGISTER]) then
  519. exit;
  520. location.loc:=LOC_REGISTER;
  521. if registers32<1 then
  522. registers32:=1;
  523. end;
  524. function ttypeconvnode.first_int_to_bool : tnode;
  525. begin
  526. first_int_to_bool:=nil;
  527. { byte(boolean) or word(wordbool) or longint(longbool) must
  528. be accepted for var parameters }
  529. if (nf_explizit in flags) and
  530. (left.resulttype^.size=resulttype^.size) and
  531. (left.location.loc in [LOC_REFERENCE,LOC_MEM,LOC_CREGISTER]) then
  532. exit;
  533. location.loc:=LOC_REGISTER;
  534. { need if bool to bool !!
  535. not very nice !!
  536. left:=gentypeconvnode(left,s32bitdef);
  537. left.explizit:=true;
  538. firstpass(left); }
  539. if registers32<1 then
  540. registers32:=1;
  541. end;
  542. function ttypeconvnode.first_bool_to_bool : tnode;
  543. begin
  544. first_bool_to_bool:=nil;
  545. location.loc:=LOC_REGISTER;
  546. if registers32<1 then
  547. registers32:=1;
  548. end;
  549. function ttypeconvnode.first_proc_to_procvar : tnode;
  550. begin
  551. first_proc_to_procvar:=nil;
  552. { hmmm, I'am not sure if that is necessary (FK) }
  553. firstpass(left);
  554. if codegenerror then
  555. exit;
  556. if (left.location.loc<>LOC_REFERENCE) then
  557. CGMessage(cg_e_illegal_expression);
  558. registers32:=left.registers32;
  559. if registers32<1 then
  560. registers32:=1;
  561. location.loc:=LOC_REGISTER;
  562. end;
  563. function ttypeconvnode.first_load_smallset : tnode;
  564. begin
  565. first_load_smallset:=nil;
  566. end;
  567. function ttypeconvnode.first_cord_to_pointer : tnode;
  568. var
  569. t : tnode;
  570. begin
  571. first_cord_to_pointer:=nil;
  572. if left.nodetype=ordconstn then
  573. begin
  574. t:=genpointerconstnode(tordconstnode(left).value,resulttype);
  575. firstpass(t);
  576. first_cord_to_pointer:=t;
  577. exit;
  578. end
  579. else
  580. internalerror(432472389);
  581. end;
  582. function ttypeconvnode.first_pchar_to_string : tnode;
  583. begin
  584. first_pchar_to_string:=nil;
  585. location.loc:=LOC_REFERENCE;
  586. end;
  587. function ttypeconvnode.first_ansistring_to_pchar : tnode;
  588. begin
  589. first_ansistring_to_pchar:=nil;
  590. location.loc:=LOC_REGISTER;
  591. if registers32<1 then
  592. registers32:=1;
  593. end;
  594. function ttypeconvnode.first_arrayconstructor_to_set : tnode;
  595. var
  596. hp : tnode;
  597. begin
  598. first_arrayconstructor_to_set:=nil;
  599. if left.nodetype<>arrayconstructorn then
  600. internalerror(5546);
  601. { remove typeconv node }
  602. hp:=left;
  603. left:=nil;
  604. { create a set constructor tree }
  605. arrayconstructor_to_set(tarrayconstructornode(hp));
  606. { now firstpass the set }
  607. firstpass(hp);
  608. first_arrayconstructor_to_set:=hp;
  609. end;
  610. function ttypeconvnode.first_class_to_intf : tnode;
  611. begin
  612. first_class_to_intf:=nil;
  613. location.loc:=LOC_REFERENCE;
  614. if registers32<1 then
  615. registers32:=1;
  616. end;
  617. function ttypeconvnode.first_call_helper(c : tconverttype) : tnode;
  618. const
  619. firstconvert : array[tconverttype] of pointer = (
  620. @ttypeconvnode.first_nothing, {equal}
  621. @ttypeconvnode.first_nothing, {not_possible}
  622. @ttypeconvnode.first_string_to_string,
  623. @ttypeconvnode.first_char_to_string,
  624. @ttypeconvnode.first_pchar_to_string,
  625. @ttypeconvnode.first_cchar_to_pchar,
  626. @ttypeconvnode.first_cstring_to_pchar,
  627. @ttypeconvnode.first_ansistring_to_pchar,
  628. @ttypeconvnode.first_string_to_chararray,
  629. @ttypeconvnode.first_chararray_to_string,
  630. @ttypeconvnode.first_array_to_pointer,
  631. @ttypeconvnode.first_pointer_to_array,
  632. @ttypeconvnode.first_int_to_int,
  633. @ttypeconvnode.first_int_to_bool,
  634. @ttypeconvnode.first_bool_to_bool,
  635. @ttypeconvnode.first_bool_to_int,
  636. @ttypeconvnode.first_real_to_real,
  637. @ttypeconvnode.first_int_to_real,
  638. @ttypeconvnode.first_int_to_fix,
  639. @ttypeconvnode.first_real_to_fix,
  640. @ttypeconvnode.first_fix_to_real,
  641. @ttypeconvnode.first_proc_to_procvar,
  642. @ttypeconvnode.first_arrayconstructor_to_set,
  643. @ttypeconvnode.first_load_smallset,
  644. @ttypeconvnode.first_cord_to_pointer,
  645. @ttypeconvnode.first_nothing,
  646. @ttypeconvnode.first_nothing,
  647. @ttypeconvnode.first_class_to_intf
  648. );
  649. type
  650. tprocedureofobject = function : tnode of object;
  651. var
  652. r : packed record
  653. proc : pointer;
  654. obj : pointer;
  655. end;
  656. begin
  657. { this is a little bit dirty but it works }
  658. { and should be quite portable too }
  659. r.proc:=firstconvert[c];
  660. r.obj:=self;
  661. first_call_helper:=tprocedureofobject(r){$ifdef FPC}();{$endif FPC}
  662. end;
  663. function ttypeconvnode.pass_1 : tnode;
  664. var
  665. hp : tnode;
  666. aprocdef : pprocdef;
  667. enable_range_check: boolean;
  668. begin
  669. pass_1:=nil;
  670. aprocdef:=nil;
  671. { if explicite type cast, then run firstpass }
  672. if (nf_explizit in flags) or not assigned(left.resulttype) then
  673. firstpass(left);
  674. if (left.nodetype=typen) and (left.resulttype=generrordef) then
  675. begin
  676. codegenerror:=true;
  677. Message(parser_e_no_type_not_allowed_here);
  678. end;
  679. if codegenerror then
  680. begin
  681. resulttype:=generrordef;
  682. exit;
  683. end;
  684. if not assigned(left.resulttype) then
  685. begin
  686. codegenerror:=true;
  687. internalerror(52349);
  688. exit;
  689. end;
  690. { load the value_str from the left part }
  691. registers32:=left.registers32;
  692. registersfpu:=left.registersfpu;
  693. {$ifdef SUPPORT_MMX}
  694. registersmmx:=left.registersmmx;
  695. {$endif}
  696. set_location(location,left.location);
  697. { remove obsolete type conversions }
  698. if is_equal(left.resulttype,resulttype) then
  699. begin
  700. { becuase is_equal only checks the basetype for sets we need to
  701. check here if we are loading a smallset into a normalset }
  702. if (resulttype^.deftype=setdef) and
  703. (left.resulttype^.deftype=setdef) and
  704. (psetdef(resulttype)^.settype<>smallset) and
  705. (psetdef(left.resulttype)^.settype=smallset) then
  706. begin
  707. { try to define the set as a normalset if it's a constant set }
  708. if left.nodetype=setconstn then
  709. begin
  710. resulttype:=left.resulttype;
  711. psetdef(resulttype)^.settype:=normset
  712. end
  713. else
  714. convtype:=tc_load_smallset;
  715. exit;
  716. end
  717. else
  718. begin
  719. pass_1:=left;
  720. left.resulttype:=resulttype;
  721. left:=nil;
  722. exit;
  723. end;
  724. end;
  725. aprocdef:=assignment_overloaded(left.resulttype,resulttype);
  726. if assigned(aprocdef) then
  727. begin
  728. procinfo^.flags:=procinfo^.flags or pi_do_call;
  729. hp:=gencallnode(overloaded_operators[_assignment],nil);
  730. { tell explicitly which def we must use !! (PM) }
  731. tcallnode(hp).procdefinition:=aprocdef;
  732. tcallnode(hp).left:=gencallparanode(left,nil);
  733. left:=nil;
  734. firstpass(hp);
  735. pass_1:=hp;
  736. exit;
  737. end;
  738. if isconvertable(left.resulttype,resulttype,convtype,left,left.nodetype,nf_explizit in flags)=0 then
  739. begin
  740. {Procedures have a resulttype of voiddef and functions of their
  741. own resulttype. They will therefore always be incompatible with
  742. a procvar. Because isconvertable cannot check for procedures we
  743. use an extra check for them.}
  744. if (m_tp_procvar in aktmodeswitches) then
  745. begin
  746. if (resulttype^.deftype=procvardef) and
  747. (is_procsym_load(left) or is_procsym_call(left)) then
  748. begin
  749. if is_procsym_call(left) then
  750. begin
  751. {if left.right=nil then
  752. begin}
  753. if (tcallnode(left).symtableprocentry^.owner^.symtabletype=objectsymtable){ and
  754. (pobjectdef(left.symtableprocentry^.owner^.defowner)^.is_class) }then
  755. hp:=genloadmethodcallnode(pprocsym(tcallnode(left).symtableprocentry),
  756. tcallnode(left).symtableproc,
  757. tcallnode(left).methodpointer.getcopy)
  758. else
  759. hp:=genloadcallnode(pprocsym(tcallnode(left).symtableprocentry),
  760. tcallnode(left).symtableproc);
  761. firstpass(hp);
  762. left.free;
  763. left:=hp;
  764. aprocdef:=pprocdef(left.resulttype);
  765. (* end
  766. else
  767. begin
  768. left.right.nodetype:=loadn;
  769. left.right.symtableentry:=left.right.symtableentry;
  770. left.right.resulttype:=pvarsym(left.symtableentry)^.definition;
  771. hp:=left.right;
  772. putnode(left);
  773. left:=hp;
  774. { should we do that ? }
  775. firstpass(left);
  776. if not is_equal(left.resulttype,resulttype) then
  777. begin
  778. CGMessage(type_e_mismatch);
  779. exit;
  780. end
  781. else
  782. begin
  783. hp:=p;
  784. p:=left;
  785. resulttype:=hp.resulttype;
  786. putnode(hp);
  787. exit;
  788. end;
  789. end; *)
  790. end
  791. else
  792. begin
  793. if (left.nodetype<>addrn) then
  794. aprocdef:=pprocsym(tloadnode(left).symtableentry)^.definition;
  795. end;
  796. convtype:=tc_proc_2_procvar;
  797. { Now check if the procedure we are going to assign to
  798. the procvar, is compatible with the procvar's type }
  799. if assigned(aprocdef) then
  800. begin
  801. if not proc_to_procvar_equal(aprocdef,pprocvardef(resulttype)) then
  802. CGMessage2(type_e_incompatible_types,aprocdef^.typename,resulttype^.typename);
  803. pass_1:=first_call_helper(convtype);
  804. end
  805. else
  806. CGMessage2(type_e_incompatible_types,left.resulttype^.typename,resulttype^.typename);
  807. exit;
  808. end;
  809. end;
  810. if nf_explizit in flags then
  811. begin
  812. { check if the result could be in a register }
  813. if not(pstoreddef(resulttype)^.is_intregable) and
  814. not(pstoreddef(resulttype)^.is_fpuregable) then
  815. make_not_regable(left);
  816. { boolean to byte are special because the
  817. location can be different }
  818. if is_integer(resulttype) and
  819. is_boolean(left.resulttype) then
  820. begin
  821. convtype:=tc_bool_2_int;
  822. pass_1:=first_call_helper(convtype);
  823. exit;
  824. end;
  825. { ansistring to pchar }
  826. if is_pchar(resulttype) and
  827. is_ansistring(left.resulttype) then
  828. begin
  829. convtype:=tc_ansistring_2_pchar;
  830. pass_1:=first_call_helper(convtype);
  831. exit;
  832. end;
  833. { do common tc_equal cast }
  834. convtype:=tc_equal;
  835. { enum to ordinal will always be s32bit }
  836. if (left.resulttype^.deftype=enumdef) and
  837. is_ordinal(resulttype) then
  838. begin
  839. if left.nodetype=ordconstn then
  840. begin
  841. hp:=genordinalconstnode(tordconstnode(left).value,resulttype);
  842. firstpass(hp);
  843. pass_1:=hp;
  844. exit;
  845. end
  846. else
  847. begin
  848. if isconvertable(s32bitdef,resulttype,convtype,nil,ordconstn,false)=0 then
  849. CGMessage2(type_e_incompatible_types,left.resulttype^.typename,resulttype^.typename);
  850. end;
  851. end
  852. { ordinal to enumeration }
  853. else
  854. if (resulttype^.deftype=enumdef) and
  855. is_ordinal(left.resulttype) then
  856. begin
  857. if left.nodetype=ordconstn then
  858. begin
  859. hp:=genordinalconstnode(tordconstnode(left).value,resulttype);
  860. firstpass(hp);
  861. pass_1:=hp;
  862. exit;
  863. end
  864. else
  865. begin
  866. if IsConvertable(left.resulttype,s32bitdef,convtype,nil,ordconstn,false)=0 then
  867. CGMessage2(type_e_incompatible_types,left.resulttype^.typename,resulttype^.typename);
  868. end;
  869. end
  870. { nil to ordinal node }
  871. else if is_ordinal(resulttype) and
  872. (left.nodetype=niln) then
  873. begin
  874. hp:=genordinalconstnode(0,resulttype);
  875. firstpass(hp);
  876. pass_1:=hp;
  877. exit;
  878. end
  879. {Are we typecasting an ordconst to a char?}
  880. else
  881. if is_char(resulttype) and
  882. is_ordinal(left.resulttype) then
  883. begin
  884. if left.nodetype=ordconstn then
  885. begin
  886. hp:=genordinalconstnode(tordconstnode(left).value,resulttype);
  887. firstpass(hp);
  888. pass_1:=hp;
  889. exit;
  890. end
  891. else
  892. begin
  893. if IsConvertable(left.resulttype,u8bitdef,convtype,nil,ordconstn,false)=0 then
  894. CGMessage2(type_e_incompatible_types,left.resulttype^.typename,resulttype^.typename);
  895. end;
  896. end
  897. { Are we char to ordinal }
  898. else
  899. if is_char(left.resulttype) and
  900. is_ordinal(resulttype) then
  901. begin
  902. if left.nodetype=ordconstn then
  903. begin
  904. hp:=genordinalconstnode(tordconstnode(left).value,resulttype);
  905. firstpass(hp);
  906. pass_1:=hp;
  907. exit;
  908. end
  909. else
  910. begin
  911. if IsConvertable(u8bitdef,resulttype,convtype,nil,ordconstn,false)=0 then
  912. CGMessage2(type_e_incompatible_types,left.resulttype^.typename,resulttype^.typename);
  913. end;
  914. end
  915. { only if the same size or formal def }
  916. { why do we allow typecasting of voiddef ?? (PM) }
  917. else
  918. begin
  919. if not(
  920. (left.resulttype^.deftype=formaldef) or
  921. (left.resulttype^.size=resulttype^.size) or
  922. (is_equal(left.resulttype,voiddef) and
  923. (left.nodetype=derefn))
  924. ) then
  925. CGMessage(cg_e_illegal_type_conversion);
  926. if ((left.resulttype^.deftype=orddef) and
  927. (resulttype^.deftype=pointerdef)) or
  928. ((resulttype^.deftype=orddef) and
  929. (left.resulttype^.deftype=pointerdef))
  930. {$ifdef extdebug}and (firstpasscount=0){$endif} then
  931. CGMessage(cg_d_pointer_to_longint_conv_not_portable);
  932. end;
  933. { the conversion into a strutured type is only }
  934. { possible, if the source is no register }
  935. if ((resulttype^.deftype in [recorddef,stringdef,arraydef]) or
  936. ((resulttype^.deftype=objectdef) and not(is_class(resulttype)))
  937. ) and (left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) { and
  938. it also works if the assignment is overloaded
  939. YES but this code is not executed if assignment is overloaded (PM)
  940. not assigned(assignment_overloaded(left.resulttype,resulttype))} then
  941. CGMessage(cg_e_illegal_type_conversion);
  942. end
  943. else
  944. CGMessage2(type_e_incompatible_types,left.resulttype^.typename,resulttype^.typename);
  945. end;
  946. { tp7 procvar support, when right is not a procvardef and we got a
  947. loadn of a procvar then convert to a calln, the check for the
  948. result is already done in is_convertible, also no conflict with
  949. @procvar is here because that has an extra addrn }
  950. if (m_tp_procvar in aktmodeswitches) and
  951. (resulttype^.deftype<>procvardef) and
  952. (left.resulttype^.deftype=procvardef) and
  953. (left.nodetype=loadn) then
  954. begin
  955. hp:=gencallnode(nil,nil);
  956. tcallnode(hp).right:=left;
  957. firstpass(hp);
  958. left:=hp;
  959. end;
  960. { ordinal contants can be directly converted }
  961. if (left.nodetype=ordconstn) and is_ordinal(resulttype) then
  962. begin
  963. { range checking is done in genordinalconstnode (PFV) }
  964. { disable for explicit type casts (JM) }
  965. if (nf_explizit in flags) and
  966. (cs_check_range in aktlocalswitches) then
  967. begin
  968. exclude(aktlocalswitches,cs_check_range);
  969. enable_range_check := true;
  970. end
  971. else
  972. enable_range_check := false;
  973. hp:=genordinalconstnode(tordconstnode(left).value,resulttype);
  974. { do sign extension if necessary (JM) }
  975. if not (cs_check_range in aktlocalswitches) and
  976. is_signed(resulttype) then
  977. with tordconstnode(hp) do
  978. case resulttype^.size of
  979. 1: value := shortint(value);
  980. 2: value := smallint(value);
  981. 4: value := longint(value);
  982. end;
  983. if enable_range_check then
  984. include(aktlocalswitches,cs_check_range);
  985. firstpass(hp);
  986. pass_1:=hp;
  987. exit;
  988. end;
  989. if convtype<>tc_equal then
  990. pass_1:=first_call_helper(convtype);
  991. end;
  992. {*****************************************************************************
  993. TISNODE
  994. *****************************************************************************}
  995. constructor tisnode.create(l,r : tnode);
  996. begin
  997. inherited create(isn,l,r);
  998. end;
  999. function tisnode.pass_1 : tnode;
  1000. begin
  1001. pass_1:=nil;
  1002. firstpass(left);
  1003. set_varstate(left,true);
  1004. firstpass(right);
  1005. set_varstate(right,true);
  1006. if codegenerror then
  1007. exit;
  1008. if (right.resulttype^.deftype<>classrefdef) then
  1009. CGMessage(type_e_mismatch);
  1010. left_right_max;
  1011. { left must be a class }
  1012. if (left.resulttype^.deftype<>objectdef) or
  1013. not(is_class(left.resulttype)) then
  1014. CGMessage(type_e_mismatch);
  1015. { the operands must be related }
  1016. if (not(pobjectdef(left.resulttype)^.is_related(
  1017. pobjectdef(pclassrefdef(right.resulttype)^.pointertype.def)))) and
  1018. (not(pobjectdef(pclassrefdef(right.resulttype)^.pointertype.def)^.is_related(
  1019. pobjectdef(left.resulttype)))) then
  1020. CGMessage(type_e_mismatch);
  1021. location.loc:=LOC_FLAGS;
  1022. resulttype:=booldef;
  1023. end;
  1024. {*****************************************************************************
  1025. TASNODE
  1026. *****************************************************************************}
  1027. constructor tasnode.create(l,r : tnode);
  1028. begin
  1029. inherited create(asn,l,r);
  1030. end;
  1031. function tasnode.pass_1 : tnode;
  1032. begin
  1033. pass_1:=nil;
  1034. firstpass(right);
  1035. set_varstate(right,true);
  1036. firstpass(left);
  1037. set_varstate(left,true);
  1038. if codegenerror then
  1039. exit;
  1040. if (right.resulttype^.deftype<>classrefdef) then
  1041. CGMessage(type_e_mismatch);
  1042. left_right_max;
  1043. { left must be a class }
  1044. if (left.resulttype^.deftype<>objectdef) or
  1045. not(is_class(left.resulttype)) then
  1046. CGMessage(type_e_mismatch);
  1047. { the operands must be related }
  1048. if (not(pobjectdef(left.resulttype)^.is_related(
  1049. pobjectdef(pclassrefdef(right.resulttype)^.pointertype.def)))) and
  1050. (not(pobjectdef(pclassrefdef(right.resulttype)^.pointertype.def)^.is_related(
  1051. pobjectdef(left.resulttype)))) then
  1052. CGMessage(type_e_mismatch);
  1053. set_location(location,left.location);
  1054. resulttype:=pclassrefdef(right.resulttype)^.pointertype.def;
  1055. end;
  1056. begin
  1057. ctypeconvnode:=ttypeconvnode;
  1058. casnode:=tasnode;
  1059. cisnode:=tisnode;
  1060. end.
  1061. {
  1062. $Log$
  1063. Revision 1.15 2000-12-08 12:41:01 jonas
  1064. * fixed bug in sign extension patch
  1065. Revision 1.14 2000/12/07 17:19:42 jonas
  1066. * new constant handling: from now on, hex constants >$7fffffff are
  1067. parsed as unsigned constants (otherwise, $80000000 got sign extended
  1068. and became $ffffffff80000000), all constants in the longint range
  1069. become longints, all constants >$7fffffff and <=cardinal($ffffffff)
  1070. are cardinals and the rest are int64's.
  1071. * added lots of longint typecast to prevent range check errors in the
  1072. compiler and rtl
  1073. * type casts of symbolic ordinal constants are now preserved
  1074. * fixed bug where the original resulttype wasn't restored correctly
  1075. after doing a 64bit rangecheck
  1076. Revision 1.13 2000/11/29 00:30:32 florian
  1077. * unused units removed from uses clause
  1078. * some changes for widestrings
  1079. Revision 1.12 2000/11/20 16:06:04 jonas
  1080. + allow evaluation of 64bit constant expressions at compile time
  1081. * disable range checking for explicit typecasts of constant expressions
  1082. Revision 1.11 2000/11/12 23:24:11 florian
  1083. * interfaces are basically running
  1084. Revision 1.10 2000/11/04 14:25:20 florian
  1085. + merged Attila's changes for interfaces, not tested yet
  1086. Revision 1.9 2000/10/31 22:02:48 peter
  1087. * symtable splitted, no real code changes
  1088. Revision 1.8 2000/10/14 21:52:55 peter
  1089. * fixed memory leaks
  1090. Revision 1.7 2000/10/14 10:14:50 peter
  1091. * moehrendorf oct 2000 rewrite
  1092. Revision 1.6 2000/10/01 19:48:24 peter
  1093. * lot of compile updates for cg11
  1094. Revision 1.5 2000/09/28 19:49:52 florian
  1095. *** empty log message ***
  1096. Revision 1.4 2000/09/27 18:14:31 florian
  1097. * fixed a lot of syntax errors in the n*.pas stuff
  1098. Revision 1.3 2000/09/26 20:06:13 florian
  1099. * hmm, still a lot of work to get things compilable
  1100. Revision 1.2 2000/09/26 14:59:34 florian
  1101. * more conversion work done
  1102. Revision 1.1 2000/09/25 15:37:14 florian
  1103. * more fixes
  1104. }