ncnv.pas 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  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,cobjects,verbose,globals,
  76. symconst,symdef,symsym,symtable,aasm,
  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. begin
  668. pass_1:=nil;
  669. aprocdef:=nil;
  670. { if explicite type cast, then run firstpass }
  671. if (nf_explizit in flags) or not assigned(left.resulttype) then
  672. firstpass(left);
  673. if (left.nodetype=typen) and (left.resulttype=generrordef) then
  674. begin
  675. codegenerror:=true;
  676. Message(parser_e_no_type_not_allowed_here);
  677. end;
  678. if codegenerror then
  679. begin
  680. resulttype:=generrordef;
  681. exit;
  682. end;
  683. if not assigned(left.resulttype) then
  684. begin
  685. codegenerror:=true;
  686. internalerror(52349);
  687. exit;
  688. end;
  689. { load the value_str from the left part }
  690. registers32:=left.registers32;
  691. registersfpu:=left.registersfpu;
  692. {$ifdef SUPPORT_MMX}
  693. registersmmx:=left.registersmmx;
  694. {$endif}
  695. set_location(location,left.location);
  696. { remove obsolete type conversions }
  697. if is_equal(left.resulttype,resulttype) then
  698. begin
  699. { becuase is_equal only checks the basetype for sets we need to
  700. check here if we are loading a smallset into a normalset }
  701. if (resulttype^.deftype=setdef) and
  702. (left.resulttype^.deftype=setdef) and
  703. (psetdef(resulttype)^.settype<>smallset) and
  704. (psetdef(left.resulttype)^.settype=smallset) then
  705. begin
  706. { try to define the set as a normalset if it's a constant set }
  707. if left.nodetype=setconstn then
  708. begin
  709. resulttype:=left.resulttype;
  710. psetdef(resulttype)^.settype:=normset
  711. end
  712. else
  713. convtype:=tc_load_smallset;
  714. exit;
  715. end
  716. else
  717. begin
  718. pass_1:=left;
  719. left.resulttype:=resulttype;
  720. left:=nil;
  721. exit;
  722. end;
  723. end;
  724. aprocdef:=assignment_overloaded(left.resulttype,resulttype);
  725. if assigned(aprocdef) then
  726. begin
  727. procinfo^.flags:=procinfo^.flags or pi_do_call;
  728. hp:=gencallnode(overloaded_operators[_assignment],nil);
  729. { tell explicitly which def we must use !! (PM) }
  730. tcallnode(hp).procdefinition:=aprocdef;
  731. tcallnode(hp).left:=gencallparanode(left,nil);
  732. left:=nil;
  733. firstpass(hp);
  734. pass_1:=hp;
  735. exit;
  736. end;
  737. if isconvertable(left.resulttype,resulttype,convtype,left,left.nodetype,nf_explizit in flags)=0 then
  738. begin
  739. {Procedures have a resulttype of voiddef and functions of their
  740. own resulttype. They will therefore always be incompatible with
  741. a procvar. Because isconvertable cannot check for procedures we
  742. use an extra check for them.}
  743. if (m_tp_procvar in aktmodeswitches) then
  744. begin
  745. if (resulttype^.deftype=procvardef) and
  746. (is_procsym_load(left) or is_procsym_call(left)) then
  747. begin
  748. if is_procsym_call(left) then
  749. begin
  750. {if left.right=nil then
  751. begin}
  752. if (tcallnode(left).symtableprocentry^.owner^.symtabletype=objectsymtable){ and
  753. (pobjectdef(left.symtableprocentry^.owner^.defowner)^.is_class) }then
  754. hp:=genloadmethodcallnode(pprocsym(tcallnode(left).symtableprocentry),
  755. tcallnode(left).symtableproc,
  756. tcallnode(left).methodpointer.getcopy)
  757. else
  758. hp:=genloadcallnode(pprocsym(tcallnode(left).symtableprocentry),
  759. tcallnode(left).symtableproc);
  760. firstpass(hp);
  761. left.free;
  762. left:=hp;
  763. aprocdef:=pprocdef(left.resulttype);
  764. (* end
  765. else
  766. begin
  767. left.right.nodetype:=loadn;
  768. left.right.symtableentry:=left.right.symtableentry;
  769. left.right.resulttype:=pvarsym(left.symtableentry)^.definition;
  770. hp:=left.right;
  771. putnode(left);
  772. left:=hp;
  773. { should we do that ? }
  774. firstpass(left);
  775. if not is_equal(left.resulttype,resulttype) then
  776. begin
  777. CGMessage(type_e_mismatch);
  778. exit;
  779. end
  780. else
  781. begin
  782. hp:=p;
  783. p:=left;
  784. resulttype:=hp.resulttype;
  785. putnode(hp);
  786. exit;
  787. end;
  788. end; *)
  789. end
  790. else
  791. begin
  792. if (left.nodetype<>addrn) then
  793. aprocdef:=pprocsym(tloadnode(left).symtableentry)^.definition;
  794. end;
  795. convtype:=tc_proc_2_procvar;
  796. { Now check if the procedure we are going to assign to
  797. the procvar, is compatible with the procvar's type }
  798. if assigned(aprocdef) then
  799. begin
  800. if not proc_to_procvar_equal(aprocdef,pprocvardef(resulttype)) then
  801. CGMessage2(type_e_incompatible_types,aprocdef^.typename,resulttype^.typename);
  802. pass_1:=first_call_helper(convtype);
  803. end
  804. else
  805. CGMessage2(type_e_incompatible_types,left.resulttype^.typename,resulttype^.typename);
  806. exit;
  807. end;
  808. end;
  809. if nf_explizit in flags then
  810. begin
  811. { check if the result could be in a register }
  812. if not(pstoreddef(resulttype)^.is_intregable) and
  813. not(pstoreddef(resulttype)^.is_fpuregable) then
  814. make_not_regable(left);
  815. { boolean to byte are special because the
  816. location can be different }
  817. if is_integer(resulttype) and
  818. is_boolean(left.resulttype) then
  819. begin
  820. convtype:=tc_bool_2_int;
  821. pass_1:=first_call_helper(convtype);
  822. exit;
  823. end;
  824. { ansistring to pchar }
  825. if is_pchar(resulttype) and
  826. is_ansistring(left.resulttype) then
  827. begin
  828. convtype:=tc_ansistring_2_pchar;
  829. pass_1:=first_call_helper(convtype);
  830. exit;
  831. end;
  832. { do common tc_equal cast }
  833. convtype:=tc_equal;
  834. { enum to ordinal will always be s32bit }
  835. if (left.resulttype^.deftype=enumdef) and
  836. is_ordinal(resulttype) then
  837. begin
  838. if left.nodetype=ordconstn then
  839. begin
  840. hp:=genordinalconstnode(tordconstnode(left).value,resulttype);
  841. firstpass(hp);
  842. pass_1:=hp;
  843. exit;
  844. end
  845. else
  846. begin
  847. if isconvertable(s32bitdef,resulttype,convtype,nil,ordconstn,false)=0 then
  848. CGMessage2(type_e_incompatible_types,left.resulttype^.typename,resulttype^.typename);
  849. end;
  850. end
  851. { ordinal to enumeration }
  852. else
  853. if (resulttype^.deftype=enumdef) and
  854. is_ordinal(left.resulttype) then
  855. begin
  856. if left.nodetype=ordconstn then
  857. begin
  858. hp:=genordinalconstnode(tordconstnode(left).value,resulttype);
  859. firstpass(hp);
  860. pass_1:=hp;
  861. exit;
  862. end
  863. else
  864. begin
  865. if IsConvertable(left.resulttype,s32bitdef,convtype,nil,ordconstn,false)=0 then
  866. CGMessage2(type_e_incompatible_types,left.resulttype^.typename,resulttype^.typename);
  867. end;
  868. end
  869. { nil to ordinal node }
  870. else if is_ordinal(resulttype) and
  871. (left.nodetype=niln) then
  872. begin
  873. hp:=genordinalconstnode(0,resulttype);
  874. firstpass(hp);
  875. pass_1:=hp;
  876. exit;
  877. end
  878. {Are we typecasting an ordconst to a char?}
  879. else
  880. if is_char(resulttype) and
  881. is_ordinal(left.resulttype) then
  882. begin
  883. if left.nodetype=ordconstn then
  884. begin
  885. hp:=genordinalconstnode(tordconstnode(left).value,resulttype);
  886. firstpass(hp);
  887. pass_1:=hp;
  888. exit;
  889. end
  890. else
  891. begin
  892. if IsConvertable(left.resulttype,u8bitdef,convtype,nil,ordconstn,false)=0 then
  893. CGMessage2(type_e_incompatible_types,left.resulttype^.typename,resulttype^.typename);
  894. end;
  895. end
  896. { Are we char to ordinal }
  897. else
  898. if is_char(left.resulttype) and
  899. is_ordinal(resulttype) then
  900. begin
  901. if left.nodetype=ordconstn then
  902. begin
  903. hp:=genordinalconstnode(tordconstnode(left).value,resulttype);
  904. firstpass(hp);
  905. pass_1:=hp;
  906. exit;
  907. end
  908. else
  909. begin
  910. if IsConvertable(u8bitdef,resulttype,convtype,nil,ordconstn,false)=0 then
  911. CGMessage2(type_e_incompatible_types,left.resulttype^.typename,resulttype^.typename);
  912. end;
  913. end
  914. { only if the same size or formal def }
  915. { why do we allow typecasting of voiddef ?? (PM) }
  916. else
  917. begin
  918. if not(
  919. (left.resulttype^.deftype=formaldef) or
  920. (left.resulttype^.size=resulttype^.size) or
  921. (is_equal(left.resulttype,voiddef) and
  922. (left.nodetype=derefn))
  923. ) then
  924. CGMessage(cg_e_illegal_type_conversion);
  925. if ((left.resulttype^.deftype=orddef) and
  926. (resulttype^.deftype=pointerdef)) or
  927. ((resulttype^.deftype=orddef) and
  928. (left.resulttype^.deftype=pointerdef))
  929. {$ifdef extdebug}and (firstpasscount=0){$endif} then
  930. CGMessage(cg_d_pointer_to_longint_conv_not_portable);
  931. end;
  932. { the conversion into a strutured type is only }
  933. { possible, if the source is no register }
  934. if ((resulttype^.deftype in [recorddef,stringdef,arraydef]) or
  935. ((resulttype^.deftype=objectdef) and not(is_class(resulttype)))
  936. ) and (left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) { and
  937. it also works if the assignment is overloaded
  938. YES but this code is not executed if assignment is overloaded (PM)
  939. not assigned(assignment_overloaded(left.resulttype,resulttype))} then
  940. CGMessage(cg_e_illegal_type_conversion);
  941. end
  942. else
  943. CGMessage2(type_e_incompatible_types,left.resulttype^.typename,resulttype^.typename);
  944. end;
  945. { tp7 procvar support, when right is not a procvardef and we got a
  946. loadn of a procvar then convert to a calln, the check for the
  947. result is already done in is_convertible, also no conflict with
  948. @procvar is here because that has an extra addrn }
  949. if (m_tp_procvar in aktmodeswitches) and
  950. (resulttype^.deftype<>procvardef) and
  951. (left.resulttype^.deftype=procvardef) and
  952. (left.nodetype=loadn) then
  953. begin
  954. hp:=gencallnode(nil,nil);
  955. tcallnode(hp).right:=left;
  956. firstpass(hp);
  957. left:=hp;
  958. end;
  959. { ordinal contants can be directly converted }
  960. { but not int64/qword }
  961. if (left.nodetype=ordconstn) and is_ordinal(resulttype) and
  962. not(is_64bitint(resulttype)) then
  963. begin
  964. { range checking is done in genordinalconstnode (PFV) }
  965. hp:=genordinalconstnode(tordconstnode(left).value,resulttype);
  966. firstpass(hp);
  967. pass_1:=hp;
  968. exit;
  969. end;
  970. if convtype<>tc_equal then
  971. pass_1:=first_call_helper(convtype);
  972. end;
  973. {*****************************************************************************
  974. TISNODE
  975. *****************************************************************************}
  976. constructor tisnode.create(l,r : tnode);
  977. begin
  978. inherited create(isn,l,r);
  979. end;
  980. function tisnode.pass_1 : tnode;
  981. begin
  982. pass_1:=nil;
  983. firstpass(left);
  984. set_varstate(left,true);
  985. firstpass(right);
  986. set_varstate(right,true);
  987. if codegenerror then
  988. exit;
  989. if (right.resulttype^.deftype<>classrefdef) then
  990. CGMessage(type_e_mismatch);
  991. left_right_max;
  992. { left must be a class }
  993. if (left.resulttype^.deftype<>objectdef) or
  994. not(is_class(left.resulttype)) then
  995. CGMessage(type_e_mismatch);
  996. { the operands must be related }
  997. if (not(pobjectdef(left.resulttype)^.is_related(
  998. pobjectdef(pclassrefdef(right.resulttype)^.pointertype.def)))) and
  999. (not(pobjectdef(pclassrefdef(right.resulttype)^.pointertype.def)^.is_related(
  1000. pobjectdef(left.resulttype)))) then
  1001. CGMessage(type_e_mismatch);
  1002. location.loc:=LOC_FLAGS;
  1003. resulttype:=booldef;
  1004. end;
  1005. {*****************************************************************************
  1006. TASNODE
  1007. *****************************************************************************}
  1008. constructor tasnode.create(l,r : tnode);
  1009. begin
  1010. inherited create(asn,l,r);
  1011. end;
  1012. function tasnode.pass_1 : tnode;
  1013. begin
  1014. pass_1:=nil;
  1015. firstpass(right);
  1016. set_varstate(right,true);
  1017. firstpass(left);
  1018. set_varstate(left,true);
  1019. if codegenerror then
  1020. exit;
  1021. if (right.resulttype^.deftype<>classrefdef) then
  1022. CGMessage(type_e_mismatch);
  1023. left_right_max;
  1024. { left must be a class }
  1025. if (left.resulttype^.deftype<>objectdef) or
  1026. not(is_class(left.resulttype)) then
  1027. CGMessage(type_e_mismatch);
  1028. { the operands must be related }
  1029. if (not(pobjectdef(left.resulttype)^.is_related(
  1030. pobjectdef(pclassrefdef(right.resulttype)^.pointertype.def)))) and
  1031. (not(pobjectdef(pclassrefdef(right.resulttype)^.pointertype.def)^.is_related(
  1032. pobjectdef(left.resulttype)))) then
  1033. CGMessage(type_e_mismatch);
  1034. set_location(location,left.location);
  1035. resulttype:=pclassrefdef(right.resulttype)^.pointertype.def;
  1036. end;
  1037. begin
  1038. ctypeconvnode:=ttypeconvnode;
  1039. casnode:=tasnode;
  1040. cisnode:=tisnode;
  1041. end.
  1042. {
  1043. $Log$
  1044. Revision 1.11 2000-11-12 23:24:11 florian
  1045. * interfaces are basically running
  1046. Revision 1.10 2000/11/04 14:25:20 florian
  1047. + merged Attila's changes for interfaces, not tested yet
  1048. Revision 1.9 2000/10/31 22:02:48 peter
  1049. * symtable splitted, no real code changes
  1050. Revision 1.8 2000/10/14 21:52:55 peter
  1051. * fixed memory leaks
  1052. Revision 1.7 2000/10/14 10:14:50 peter
  1053. * moehrendorf oct 2000 rewrite
  1054. Revision 1.6 2000/10/01 19:48:24 peter
  1055. * lot of compile updates for cg11
  1056. Revision 1.5 2000/09/28 19:49:52 florian
  1057. *** empty log message ***
  1058. Revision 1.4 2000/09/27 18:14:31 florian
  1059. * fixed a lot of syntax errors in the n*.pas stuff
  1060. Revision 1.3 2000/09/26 20:06:13 florian
  1061. * hmm, still a lot of work to get things compilable
  1062. Revision 1.2 2000/09/26 14:59:34 florian
  1063. * more conversion work done
  1064. Revision 1.1 2000/09/25 15:37:14 florian
  1065. * more fixes
  1066. }