ncnv.pas 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634
  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. totype : ttype;
  28. convtype : tconverttype;
  29. constructor create(node : tnode;const t : ttype);virtual;
  30. function getcopy : tnode;override;
  31. function pass_1 : tnode;override;
  32. function det_resulttype:tnode;override;
  33. function docompare(p: tnode) : boolean; override;
  34. private
  35. function resulttype_cord_to_pointer : tnode;
  36. function resulttype_chararray_to_string : tnode;
  37. function resulttype_string_to_chararray : tnode;
  38. function resulttype_string_to_string : tnode;
  39. function resulttype_char_to_string : tnode;
  40. function resulttype_int_to_real : tnode;
  41. function resulttype_real_to_real : tnode;
  42. function resulttype_cchar_to_pchar : tnode;
  43. function resulttype_cstring_to_pchar : tnode;
  44. function resulttype_char_to_char : tnode;
  45. function resulttype_arrayconstructor_to_set : tnode;
  46. function resulttype_pchar_to_string : tnode;
  47. function resulttype_call_helper(c : tconverttype) : tnode;
  48. protected
  49. function first_int_to_int : tnode;virtual;
  50. function first_cstring_to_pchar : tnode;virtual;
  51. function first_string_to_chararray : tnode;virtual;
  52. function first_char_to_string : tnode;virtual;
  53. function first_nothing : tnode;virtual;
  54. function first_array_to_pointer : tnode;virtual;
  55. function first_int_to_real : tnode;virtual;
  56. function first_real_to_real : tnode;virtual;
  57. function first_pointer_to_array : tnode;virtual;
  58. function first_cchar_to_pchar : tnode;virtual;
  59. function first_bool_to_int : tnode;virtual;
  60. function first_int_to_bool : tnode;virtual;
  61. function first_bool_to_bool : tnode;virtual;
  62. function first_proc_to_procvar : tnode;virtual;
  63. function first_load_smallset : tnode;virtual;
  64. function first_cord_to_pointer : tnode;virtual;
  65. function first_ansistring_to_pchar : tnode;virtual;
  66. function first_arrayconstructor_to_set : tnode;virtual;
  67. function first_class_to_intf : tnode;virtual;
  68. function first_char_to_char : tnode;virtual;
  69. function first_call_helper(c : tconverttype) : tnode;
  70. end;
  71. ttypeconvnodeclass = class of ttypeconvnode;
  72. tasnode = class(tbinarynode)
  73. constructor create(l,r : tnode);virtual;
  74. function pass_1 : tnode;override;
  75. function det_resulttype:tnode;override;
  76. end;
  77. tasnodeclass = class of tasnode;
  78. tisnode = class(tbinarynode)
  79. constructor create(l,r : tnode);virtual;
  80. function pass_1 : tnode;override;
  81. function det_resulttype:tnode;override;
  82. end;
  83. tisnodeclass = class of tisnode;
  84. var
  85. ctypeconvnode : ttypeconvnodeclass;
  86. casnode : tasnodeclass;
  87. cisnode : tisnodeclass;
  88. procedure inserttypeconv(var p:tnode;const t:ttype);
  89. procedure arrayconstructor_to_set(var p : tarrayconstructornode);
  90. implementation
  91. uses
  92. globtype,systems,tokens,
  93. cutils,verbose,globals,widestr,
  94. symconst,symdef,symsym,symtable,
  95. ncon,ncal,nset,nadd,ninl,
  96. cgbase,
  97. htypechk,pass_1,cpubase,cpuinfo;
  98. {*****************************************************************************
  99. Helpers
  100. *****************************************************************************}
  101. procedure inserttypeconv(var p:tnode;const t:ttype);
  102. begin
  103. if not assigned(p.resulttype.def) then
  104. begin
  105. resulttypepass(p);
  106. if codegenerror then
  107. exit;
  108. end;
  109. { don't insert obsolete type conversions }
  110. if is_equal(p.resulttype.def,t.def) and
  111. not ((p.resulttype.def.deftype=setdef) and
  112. (tsetdef(p.resulttype.def).settype <>
  113. tsetdef(t.def).settype)) then
  114. begin
  115. p.resulttype:=t;
  116. end
  117. else
  118. begin
  119. p:=ctypeconvnode.create(p,t);
  120. resulttypepass(p);
  121. end;
  122. end;
  123. {*****************************************************************************
  124. Array constructor to Set Conversion
  125. *****************************************************************************}
  126. procedure arrayconstructor_to_set(var p : tarrayconstructornode);
  127. var
  128. constp : tsetconstnode;
  129. buildp,
  130. p2,p3,p4 : tnode;
  131. htype : ttype;
  132. constset : pconstset;
  133. constsetlo,
  134. constsethi : longint;
  135. procedure update_constsethi(t:ttype);
  136. begin
  137. if ((t.def.deftype=orddef) and
  138. (torddef(t.def).high>=constsethi)) then
  139. begin
  140. constsethi:=torddef(t.def).high;
  141. if htype.def=nil then
  142. begin
  143. if (constsethi>255) or
  144. (torddef(t.def).low<0) then
  145. htype:=u8bittype
  146. else
  147. htype:=t;
  148. end;
  149. if constsethi>255 then
  150. constsethi:=255;
  151. end
  152. else if ((t.def.deftype=enumdef) and
  153. (tenumdef(t.def).max>=constsethi)) then
  154. begin
  155. if htype.def=nil then
  156. htype:=t;
  157. constsethi:=tenumdef(t.def).max;
  158. end;
  159. end;
  160. procedure do_set(pos : longint);
  161. var
  162. mask,l : longint;
  163. begin
  164. if (pos>255) or (pos<0) then
  165. Message(parser_e_illegal_set_expr);
  166. if pos>constsethi then
  167. constsethi:=pos;
  168. if pos<constsetlo then
  169. constsetlo:=pos;
  170. { to do this correctly we use the 32bit array }
  171. l:=pos shr 5;
  172. mask:=1 shl (pos mod 32);
  173. { do we allow the same twice }
  174. if (pconst32bitset(constset)^[l] and mask)<>0 then
  175. Message(parser_e_illegal_set_expr);
  176. pconst32bitset(constset)^[l]:=pconst32bitset(constset)^[l] or mask;
  177. end;
  178. var
  179. l : longint;
  180. lr,hr : longint;
  181. begin
  182. new(constset);
  183. FillChar(constset^,sizeof(constset^),0);
  184. htype.reset;
  185. constsetlo:=0;
  186. constsethi:=0;
  187. constp:=csetconstnode.create(nil,htype);
  188. constp.value_set:=constset;
  189. buildp:=constp;
  190. if assigned(p.left) then
  191. begin
  192. while assigned(p) do
  193. begin
  194. p4:=nil; { will contain the tree to create the set }
  195. {split a range into p2 and p3 }
  196. if p.left.nodetype=arrayconstructorrangen then
  197. begin
  198. p2:=tarrayconstructorrangenode(p.left).left;
  199. p3:=tarrayconstructorrangenode(p.left).right;
  200. tarrayconstructorrangenode(p.left).left:=nil;
  201. tarrayconstructorrangenode(p.left).right:=nil;
  202. end
  203. else
  204. begin
  205. p2:=p.left;
  206. p.left:=nil;
  207. p3:=nil;
  208. end;
  209. resulttypepass(p2);
  210. if assigned(p3) then
  211. resulttypepass(p3);
  212. if codegenerror then
  213. break;
  214. case p2.resulttype.def.deftype of
  215. enumdef,
  216. orddef:
  217. begin
  218. getrange(p2.resulttype.def,lr,hr);
  219. if assigned(p3) then
  220. begin
  221. { this isn't good, you'll get problems with
  222. type t010 = 0..10;
  223. ts = set of t010;
  224. var s : ts;b : t010
  225. begin s:=[1,2,b]; end.
  226. if is_integer(p3^.resulttype.def) then
  227. begin
  228. inserttypeconv(p3,u8bitdef);
  229. end;
  230. }
  231. if assigned(htype.def) and not(is_equal(htype.def,p3.resulttype.def)) then
  232. begin
  233. aktfilepos:=p3.fileinfo;
  234. CGMessage(type_e_typeconflict_in_set);
  235. end
  236. else
  237. begin
  238. if (p2.nodetype=ordconstn) and (p3.nodetype=ordconstn) then
  239. begin
  240. if not(is_integer(p3.resulttype.def)) then
  241. htype:=p3.resulttype
  242. else
  243. begin
  244. inserttypeconv(p3,u8bittype);
  245. inserttypeconv(p2,u8bittype);
  246. end;
  247. for l:=tordconstnode(p2).value to tordconstnode(p3).value do
  248. do_set(l);
  249. p2.free;
  250. p3.free;
  251. end
  252. else
  253. begin
  254. update_constsethi(p2.resulttype);
  255. inserttypeconv(p2,htype);
  256. update_constsethi(p3.resulttype);
  257. inserttypeconv(p3,htype);
  258. if assigned(htype.def) then
  259. inserttypeconv(p3,htype)
  260. else
  261. inserttypeconv(p3,u8bittype);
  262. p4:=csetelementnode.create(p2,p3);
  263. end;
  264. end;
  265. end
  266. else
  267. begin
  268. { Single value }
  269. if p2.nodetype=ordconstn then
  270. begin
  271. if not(is_integer(p2.resulttype.def)) then
  272. update_constsethi(p2.resulttype)
  273. else
  274. inserttypeconv(p2,u8bittype);
  275. do_set(tordconstnode(p2).value);
  276. p2.free;
  277. end
  278. else
  279. begin
  280. update_constsethi(p2.resulttype);
  281. if assigned(htype.def) then
  282. inserttypeconv(p2,htype)
  283. else
  284. inserttypeconv(p2,u8bittype);
  285. p4:=csetelementnode.create(p2,nil);
  286. end;
  287. end;
  288. end;
  289. stringdef :
  290. begin
  291. { if we've already set elements which are constants }
  292. { throw an error }
  293. if ((htype.def=nil) and assigned(buildp)) or
  294. not(is_char(htype.def)) then
  295. CGMessage(type_e_typeconflict_in_set)
  296. else
  297. for l:=1 to length(pstring(tstringconstnode(p2).value_str)^) do
  298. do_set(ord(pstring(tstringconstnode(p2).value_str)^[l]));
  299. if htype.def=nil then
  300. htype:=cchartype;
  301. p2.free;
  302. end;
  303. else
  304. CGMessage(type_e_ordinal_expr_expected);
  305. end;
  306. { insert the set creation tree }
  307. if assigned(p4) then
  308. buildp:=caddnode.create(addn,buildp,p4);
  309. { load next and dispose current node }
  310. p2:=p;
  311. p:=tarrayconstructornode(tarrayconstructornode(p2).right);
  312. tarrayconstructornode(p2).right:=nil;
  313. p2.free;
  314. end;
  315. if (htype.def=nil) then
  316. begin
  317. htype:=u8bittype;
  318. constsethi:=255;
  319. end;
  320. end
  321. else
  322. begin
  323. { empty set [], only remove node }
  324. p.free;
  325. end;
  326. { set the initial set type }
  327. constp.resulttype.setdef(tsetdef.create(htype,constsethi));
  328. { determine the resulttype for the tree }
  329. resulttypepass(buildp);
  330. { set the new tree }
  331. p:=tarrayconstructornode(buildp);
  332. end;
  333. {*****************************************************************************
  334. TTYPECONVNODE
  335. *****************************************************************************}
  336. constructor ttypeconvnode.create(node : tnode;const t:ttype);
  337. begin
  338. inherited create(typeconvn,node);
  339. convtype:=tc_not_possible;
  340. totype:=t;
  341. if t.def=nil then
  342. internalerror(200103281);
  343. set_file_line(node);
  344. end;
  345. function ttypeconvnode.getcopy : tnode;
  346. var
  347. n : ttypeconvnode;
  348. begin
  349. n:=ttypeconvnode(inherited getcopy);
  350. n.convtype:=convtype;
  351. getcopy:=n;
  352. end;
  353. function ttypeconvnode.resulttype_cord_to_pointer : tnode;
  354. var
  355. t : tnode;
  356. begin
  357. result:=nil;
  358. if left.nodetype=ordconstn then
  359. begin
  360. { check if we have a valid pointer constant (JM) }
  361. if (sizeof(pointer) > sizeof(TConstPtrUInt)) then
  362. if (sizeof(TConstPtrUInt) = 4) then
  363. begin
  364. if (tordconstnode(left).value < low(longint)) or
  365. (tordconstnode(left).value > high(cardinal)) then
  366. CGMessage(parser_e_range_check_error);
  367. end
  368. else if (sizeof(TConstPtrUInt) = 8) then
  369. begin
  370. if (tordconstnode(left).value < low(int64)) or
  371. (tordconstnode(left).value > high(qword)) then
  372. CGMessage(parser_e_range_check_error);
  373. end
  374. else
  375. internalerror(2001020801);
  376. t:=cpointerconstnode.create(TConstPtrUInt(tordconstnode(left).value),resulttype);
  377. result:=t;
  378. end
  379. else
  380. internalerror(200104023);
  381. end;
  382. function ttypeconvnode.resulttype_chararray_to_string : tnode;
  383. begin
  384. result := ccallnode.createinternres(
  385. 'fpc_chararray_to_'+lower(tstringdef(resulttype.def).stringtypname),
  386. ccallparanode.create(left,nil),resulttype);
  387. left := nil;
  388. end;
  389. function ttypeconvnode.resulttype_string_to_chararray : tnode;
  390. var
  391. arrsize: longint;
  392. begin
  393. with tarraydef(resulttype.def) do
  394. begin
  395. if highrange<lowrange then
  396. internalerror(75432653);
  397. arrsize := highrange-lowrange+1;
  398. end;
  399. if (left.nodetype = stringconstn) and
  400. { left.length+1 since there's always a terminating #0 character (JM) }
  401. (tstringconstnode(left).len+1 >= arrsize) and
  402. (tstringdef(left.resulttype.def).string_typ=st_shortstring) then
  403. begin
  404. { handled separately }
  405. result := nil;
  406. exit;
  407. end;
  408. result := ccallnode.createinternres(
  409. 'fpc_'+lower(tstringdef(left.resulttype.def).stringtypname)+
  410. '_to_chararray',ccallparanode.create(left,ccallparanode.create(
  411. cordconstnode.create(arrsize,s32bittype),nil)),resulttype);
  412. left := nil;
  413. end;
  414. function ttypeconvnode.resulttype_string_to_string : tnode;
  415. var
  416. procname: string[31];
  417. stringpara : tcallparanode;
  418. pw : pcompilerwidestring;
  419. pc : pchar;
  420. begin
  421. result:=nil;
  422. if left.nodetype=stringconstn then
  423. begin
  424. { convert ascii 2 unicode }
  425. if (tstringdef(resulttype.def).string_typ=st_widestring) and
  426. (tstringconstnode(left).st_type in [st_ansistring,st_shortstring,st_longstring]) then
  427. begin
  428. initwidestring(pw);
  429. ascii2unicode(tstringconstnode(left).value_str,tstringconstnode(left).len,pw);
  430. ansistringdispose(tstringconstnode(left).value_str,tstringconstnode(left).len);
  431. pcompilerwidestring(tstringconstnode(left).value_str):=pw;
  432. end
  433. else
  434. { convert unicode 2 ascii }
  435. if (tstringconstnode(left).st_type=st_widestring) and
  436. (tstringdef(resulttype.def).string_typ in [st_ansistring,st_shortstring,st_longstring]) then
  437. begin
  438. pw:=pcompilerwidestring(tstringconstnode(left).value_str);
  439. getmem(pc,getlengthwidestring(pw)+1);
  440. unicode2ascii(pw,pc);
  441. donewidestring(pw);
  442. tstringconstnode(left).value_str:=pc;
  443. end;
  444. tstringconstnode(left).st_type:=tstringdef(resulttype.def).string_typ;
  445. tstringconstnode(left).resulttype:=resulttype;
  446. result:=left;
  447. left:=nil;
  448. end
  449. else
  450. begin
  451. { get the correct procedure name }
  452. procname := 'fpc_'+
  453. lower(tstringdef(left.resulttype.def).stringtypname+
  454. '_to_'+tstringdef(resulttype.def).stringtypname);
  455. { create parameter (and remove left node from typeconvnode }
  456. { since it's reused as parameter) }
  457. stringpara := ccallparanode.create(left,nil);
  458. left := nil;
  459. { hen converting to shortstrings, we have to pass high(destination) too }
  460. if (tstringdef(resulttype.def).string_typ =
  461. st_shortstring) then
  462. stringpara.right := ccallparanode.create(cinlinenode.create(
  463. in_high_x,false,self.getcopy),nil);
  464. { and create the callnode }
  465. result := ccallnode.createinternres(procname,stringpara,resulttype);
  466. end;
  467. end;
  468. function ttypeconvnode.resulttype_char_to_string : tnode;
  469. var
  470. procname: string[31];
  471. para : tcallparanode;
  472. hp : tstringconstnode;
  473. ws : pcompilerwidestring;
  474. begin
  475. result:=nil;
  476. if left.nodetype=ordconstn then
  477. begin
  478. if tstringdef(resulttype.def).string_typ=st_widestring then
  479. begin
  480. initwidestring(ws);
  481. concatwidestringchar(ws,tcompilerwidechar(chr(tordconstnode(left).value)));
  482. hp:=cstringconstnode.createwstr(ws);
  483. donewidestring(ws);
  484. end
  485. else
  486. hp:=cstringconstnode.createstr(chr(tordconstnode(left).value),tstringdef(resulttype.def).string_typ);
  487. result:=hp;
  488. end
  489. else
  490. { shortstrings are handled 'inline' }
  491. if tstringdef(resulttype.def).string_typ <> st_shortstring then
  492. begin
  493. { create the parameter }
  494. para := ccallparanode.create(left,nil);
  495. left := nil;
  496. { and the procname }
  497. procname := 'fpc_char_to_' +
  498. lower(tstringdef(resulttype.def).stringtypname);
  499. { and finally the call }
  500. result := ccallnode.createinternres(procname,para,resulttype);
  501. end;
  502. end;
  503. function ttypeconvnode.resulttype_char_to_char : tnode;
  504. var
  505. hp : tordconstnode;
  506. begin
  507. result:=nil;
  508. if left.nodetype=ordconstn then
  509. begin
  510. if (torddef(resulttype.def).typ=uchar) and
  511. (torddef(left.resulttype.def).typ=uwidechar) then
  512. begin
  513. hp:=cordconstnode.create(
  514. ord(unicode2asciichar(tcompilerwidechar(tordconstnode(left).value))),cchartype);
  515. result:=hp;
  516. end
  517. else if (torddef(resulttype.def).typ=uwidechar) and
  518. (torddef(left.resulttype.def).typ=uchar) then
  519. begin
  520. hp:=cordconstnode.create(
  521. asciichar2unicode(chr(tordconstnode(left).value)),cwidechartype);
  522. result:=hp;
  523. end
  524. else
  525. internalerror(200105131);
  526. exit;
  527. end;
  528. end;
  529. function ttypeconvnode.resulttype_int_to_real : tnode;
  530. var
  531. t : trealconstnode;
  532. begin
  533. result:=nil;
  534. if left.nodetype=ordconstn then
  535. begin
  536. t:=crealconstnode.create(tordconstnode(left).value,resulttype);
  537. result:=t;
  538. exit;
  539. end;
  540. end;
  541. function ttypeconvnode.resulttype_real_to_real : tnode;
  542. var
  543. t : tnode;
  544. begin
  545. result:=nil;
  546. if left.nodetype=realconstn then
  547. begin
  548. t:=crealconstnode.create(trealconstnode(left).value_real,resulttype);
  549. result:=t;
  550. end;
  551. end;
  552. function ttypeconvnode.resulttype_cchar_to_pchar : tnode;
  553. begin
  554. result:=nil;
  555. if is_pwidechar(resulttype.def) then
  556. inserttypeconv(left,cwidestringtype)
  557. else
  558. inserttypeconv(left,cshortstringtype);
  559. { evaluate again, reset resulttype so the convert_typ
  560. will be calculated again and cstring_to_pchar will
  561. be used for futher conversion }
  562. result:=det_resulttype;
  563. end;
  564. function ttypeconvnode.resulttype_cstring_to_pchar : tnode;
  565. begin
  566. result:=nil;
  567. if is_pwidechar(resulttype.def) then
  568. inserttypeconv(left,cwidestringtype);
  569. end;
  570. function ttypeconvnode.resulttype_arrayconstructor_to_set : tnode;
  571. var
  572. hp : tnode;
  573. begin
  574. result:=nil;
  575. if left.nodetype<>arrayconstructorn then
  576. internalerror(5546);
  577. { remove typeconv node }
  578. hp:=left;
  579. left:=nil;
  580. { create a set constructor tree }
  581. arrayconstructor_to_set(tarrayconstructornode(hp));
  582. result:=hp;
  583. end;
  584. function ttypeconvnode.resulttype_pchar_to_string : tnode;
  585. begin
  586. result := ccallnode.createinternres(
  587. 'fpc_pchar_to_'+lower(tstringdef(resulttype.def).stringtypname),
  588. ccallparanode.create(left,nil),resulttype);
  589. left := nil;
  590. end;
  591. function ttypeconvnode.resulttype_call_helper(c : tconverttype) : tnode;
  592. const
  593. resulttypeconvert : array[tconverttype] of pointer = (
  594. {equal} nil,
  595. {not_possible} nil,
  596. { string_2_string } @ttypeconvnode.resulttype_string_to_string,
  597. { char_2_string } @ttypeconvnode.resulttype_char_to_string,
  598. { pchar_2_string } @ttypeconvnode.resulttype_pchar_to_string,
  599. { cchar_2_pchar } @ttypeconvnode.resulttype_cchar_to_pchar,
  600. { cstring_2_pchar } @ttypeconvnode.resulttype_cstring_to_pchar,
  601. { ansistring_2_pchar } nil,
  602. { string_2_chararray } @ttypeconvnode.resulttype_string_to_chararray,
  603. { chararray_2_string } @ttypeconvnode.resulttype_chararray_to_string,
  604. { array_2_pointer } nil,
  605. { pointer_2_array } nil,
  606. { int_2_int } nil,
  607. { int_2_bool } nil,
  608. { bool_2_bool } nil,
  609. { bool_2_int } nil,
  610. { real_2_real } @ttypeconvnode.resulttype_real_to_real,
  611. { int_2_real } @ttypeconvnode.resulttype_int_to_real,
  612. { proc_2_procvar } nil,
  613. { arrayconstructor_2_set } @ttypeconvnode.resulttype_arrayconstructor_to_set,
  614. { load_smallset } nil,
  615. { cord_2_pointer } @ttypeconvnode.resulttype_cord_to_pointer,
  616. { intf_2_string } nil,
  617. { intf_2_guid } nil,
  618. { class_2_intf } nil,
  619. { char_2_char } @ttypeconvnode.resulttype_char_to_char,
  620. { nomal_2_smallset} nil
  621. );
  622. type
  623. tprocedureofobject = function : tnode of object;
  624. var
  625. r : packed record
  626. proc : pointer;
  627. obj : pointer;
  628. end;
  629. begin
  630. result:=nil;
  631. { this is a little bit dirty but it works }
  632. { and should be quite portable too }
  633. r.proc:=resulttypeconvert[c];
  634. r.obj:=self;
  635. if assigned(r.proc) then
  636. result:=tprocedureofobject(r){$ifdef FPC}();{$endif FPC}
  637. end;
  638. function ttypeconvnode.det_resulttype:tnode;
  639. var
  640. hp : tnode;
  641. aprocdef : tprocdef;
  642. begin
  643. result:=nil;
  644. resulttype:=totype;
  645. resulttypepass(left);
  646. if codegenerror then
  647. exit;
  648. { remove obsolete type conversions }
  649. if is_equal(left.resulttype.def,resulttype.def) then
  650. begin
  651. { becuase is_equal only checks the basetype for sets we need to
  652. check here if we are loading a smallset into a normalset }
  653. if (resulttype.def.deftype=setdef) and
  654. (left.resulttype.def.deftype=setdef) and
  655. ((tsetdef(resulttype.def).settype = smallset) xor
  656. (tsetdef(left.resulttype.def).settype = smallset)) then
  657. begin
  658. { try to define the set as a normalset if it's a constant set }
  659. if (tsetdef(resulttype.def).settype <> smallset) then
  660. begin
  661. if (left.nodetype=setconstn) then
  662. begin
  663. resulttype:=left.resulttype;
  664. tsetdef(resulttype.def).settype:=normset
  665. end
  666. else
  667. convtype:=tc_load_smallset;
  668. end
  669. else
  670. convtype := tc_normal_2_smallset;
  671. exit;
  672. end
  673. else
  674. begin
  675. left.resulttype:=resulttype;
  676. result:=left;
  677. left:=nil;
  678. exit;
  679. end;
  680. end;
  681. aprocdef:=assignment_overloaded(left.resulttype.def,resulttype.def);
  682. if assigned(aprocdef) then
  683. begin
  684. procinfo^.flags:=procinfo^.flags or pi_do_call;
  685. hp:=ccallnode.create(ccallparanode.create(left,nil),
  686. overloaded_operators[_assignment],nil,nil);
  687. { tell explicitly which def we must use !! (PM) }
  688. tcallnode(hp).procdefinition:=aprocdef;
  689. left:=nil;
  690. result:=hp;
  691. exit;
  692. end;
  693. if isconvertable(left.resulttype.def,resulttype.def,convtype,left.nodetype,nf_explizit in flags)=0 then
  694. begin
  695. {Procedures have a resulttype.def of voiddef and functions of their
  696. own resulttype.def. They will therefore always be incompatible with
  697. a procvar. Because isconvertable cannot check for procedures we
  698. use an extra check for them.}
  699. if (m_tp_procvar in aktmodeswitches) then
  700. begin
  701. if (resulttype.def.deftype=procvardef) and
  702. (is_procsym_load(left) or is_procsym_call(left)) then
  703. begin
  704. if is_procsym_call(left) then
  705. begin
  706. hp:=cloadnode.create(tprocsym(tcallnode(left).symtableprocentry),
  707. tcallnode(left).symtableproc);
  708. if (tcallnode(left).symtableprocentry.owner.symtabletype=objectsymtable) and
  709. assigned(tcallnode(left).methodpointer) then
  710. tloadnode(hp).set_mp(tcallnode(left).methodpointer.getcopy);
  711. resulttypepass(hp);
  712. left.free;
  713. left:=hp;
  714. aprocdef:=tprocdef(left.resulttype.def);
  715. end
  716. else
  717. begin
  718. if (left.nodetype<>addrn) then
  719. aprocdef:=tprocsym(tloadnode(left).symtableentry).definition;
  720. end;
  721. convtype:=tc_proc_2_procvar;
  722. { Now check if the procedure we are going to assign to
  723. the procvar, is compatible with the procvar's type }
  724. if assigned(aprocdef) then
  725. begin
  726. if not proc_to_procvar_equal(aprocdef,tprocvardef(resulttype.def)) then
  727. CGMessage2(type_e_incompatible_types,aprocdef.typename,resulttype.def.typename);
  728. end
  729. else
  730. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  731. exit;
  732. end;
  733. end;
  734. if nf_explizit in flags then
  735. begin
  736. { check if the result could be in a register }
  737. if not(tstoreddef(resulttype.def).is_intregable) and
  738. not(tstoreddef(resulttype.def).is_fpuregable) then
  739. make_not_regable(left);
  740. { boolean to byte are special because the
  741. location can be different }
  742. if is_integer(resulttype.def) and
  743. is_boolean(left.resulttype.def) then
  744. begin
  745. convtype:=tc_bool_2_int;
  746. exit;
  747. end;
  748. { ansistring to pchar }
  749. if is_pchar(resulttype.def) and
  750. is_ansistring(left.resulttype.def) then
  751. begin
  752. convtype:=tc_ansistring_2_pchar;
  753. exit;
  754. end;
  755. { do common tc_equal cast }
  756. convtype:=tc_equal;
  757. { enum to ordinal will always be s32bit }
  758. if (left.resulttype.def.deftype=enumdef) and
  759. is_ordinal(resulttype.def) then
  760. begin
  761. if left.nodetype=ordconstn then
  762. begin
  763. hp:=cordconstnode.create(tordconstnode(left).value,resulttype);
  764. result:=hp;
  765. exit;
  766. end
  767. else
  768. begin
  769. if isconvertable(s32bittype.def,resulttype.def,convtype,ordconstn,false)=0 then
  770. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  771. end;
  772. end
  773. { ordinal to enumeration }
  774. else
  775. if (resulttype.def.deftype=enumdef) and
  776. is_ordinal(left.resulttype.def) then
  777. begin
  778. if left.nodetype=ordconstn then
  779. begin
  780. hp:=cordconstnode.create(tordconstnode(left).value,resulttype);
  781. result:=hp;
  782. exit;
  783. end
  784. else
  785. begin
  786. if IsConvertable(left.resulttype.def,s32bittype.def,convtype,ordconstn,false)=0 then
  787. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  788. end;
  789. end
  790. { nil to ordinal node }
  791. else if (left.nodetype=niln) and is_ordinal(resulttype.def) then
  792. begin
  793. hp:=cordconstnode.create(0,resulttype);
  794. result:=hp;
  795. exit;
  796. end
  797. { constant pointer to ordinal }
  798. else if is_ordinal(resulttype.def) and
  799. (left.nodetype=pointerconstn) then
  800. begin
  801. hp:=cordconstnode.create(tpointerconstnode(left).value,resulttype);
  802. result:=hp;
  803. exit;
  804. end
  805. {Are we typecasting an ordconst to a char?}
  806. else
  807. if is_char(resulttype.def) and
  808. is_ordinal(left.resulttype.def) then
  809. begin
  810. if left.nodetype=ordconstn then
  811. begin
  812. hp:=cordconstnode.create(tordconstnode(left).value,resulttype);
  813. result:=hp;
  814. exit;
  815. end
  816. else
  817. begin
  818. if IsConvertable(left.resulttype.def,u8bittype.def,convtype,ordconstn,false)=0 then
  819. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  820. end;
  821. end
  822. {Are we typecasting an ordconst to a wchar?}
  823. else
  824. if is_widechar(resulttype.def) and
  825. is_ordinal(left.resulttype.def) then
  826. begin
  827. if left.nodetype=ordconstn then
  828. begin
  829. hp:=cordconstnode.create(tordconstnode(left).value,resulttype);
  830. result:=hp;
  831. exit;
  832. end
  833. else
  834. begin
  835. if IsConvertable(left.resulttype.def,u16bittype.def,convtype,ordconstn,false)=0 then
  836. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  837. end;
  838. end
  839. { char to ordinal }
  840. else
  841. if is_char(left.resulttype.def) and
  842. is_ordinal(resulttype.def) then
  843. begin
  844. if left.nodetype=ordconstn then
  845. begin
  846. hp:=cordconstnode.create(tordconstnode(left).value,resulttype);
  847. result:=hp;
  848. exit;
  849. end
  850. else
  851. begin
  852. if IsConvertable(u8bittype.def,resulttype.def,convtype,ordconstn,false)=0 then
  853. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  854. end;
  855. end
  856. { widechar to ordinal }
  857. else
  858. if is_widechar(left.resulttype.def) and
  859. is_ordinal(resulttype.def) then
  860. begin
  861. if left.nodetype=ordconstn then
  862. begin
  863. hp:=cordconstnode.create(tordconstnode(left).value,resulttype);
  864. result:=hp;
  865. exit;
  866. end
  867. else
  868. begin
  869. if IsConvertable(u16bittype.def,resulttype.def,convtype,ordconstn,false)=0 then
  870. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  871. end;
  872. end
  873. { only if the same size or formal def }
  874. { why do we allow typecasting of voiddef ?? (PM) }
  875. else
  876. begin
  877. if not(
  878. (left.resulttype.def.deftype=formaldef) or
  879. (left.resulttype.def.size=resulttype.def.size) or
  880. (is_void(left.resulttype.def) and
  881. (left.nodetype=derefn))
  882. ) then
  883. CGMessage(cg_e_illegal_type_conversion);
  884. if ((left.resulttype.def.deftype=orddef) and
  885. (resulttype.def.deftype=pointerdef)) or
  886. ((resulttype.def.deftype=orddef) and
  887. (left.resulttype.def.deftype=pointerdef)) then
  888. CGMessage(cg_d_pointer_to_longint_conv_not_portable);
  889. end;
  890. { the conversion into a strutured type is only }
  891. { possible, if the source is not a register }
  892. if ((resulttype.def.deftype in [recorddef,stringdef,arraydef]) or
  893. ((resulttype.def.deftype=objectdef) and not(is_class(resulttype.def)))
  894. ) and (left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) { and
  895. it also works if the assignment is overloaded
  896. YES but this code is not executed if assignment is overloaded (PM)
  897. not assigned(assignment_overloaded(left.resulttype.def,resulttype.def))} then
  898. CGMessage(cg_e_illegal_type_conversion);
  899. end
  900. else
  901. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  902. end;
  903. { tp7 procvar support, when right is not a procvardef and we got a
  904. loadn of a procvar then convert to a calln, the check for the
  905. result is already done in is_convertible, also no conflict with
  906. @procvar is here because that has an extra addrn }
  907. if (m_tp_procvar in aktmodeswitches) and
  908. (resulttype.def.deftype<>procvardef) and
  909. (left.resulttype.def.deftype=procvardef) and
  910. (left.nodetype=loadn) then
  911. begin
  912. hp:=ccallnode.create(nil,nil,nil,nil);
  913. tcallnode(hp).set_procvar(left);
  914. resulttypepass(hp);
  915. left:=hp;
  916. end;
  917. { remove typeconv after niln }
  918. if (left.nodetype=niln) then
  919. begin
  920. left.resulttype:=resulttype;
  921. result:=left;
  922. left:=nil;
  923. exit;
  924. end;
  925. { ordinal contants can be directly converted }
  926. if (left.nodetype=ordconstn) and is_ordinal(resulttype.def) then
  927. begin
  928. { replace the resulttype and recheck the range }
  929. left.resulttype:=resulttype;
  930. testrange(left.resulttype.def,tordconstnode(left).value,(nf_explizit in flags));
  931. result:=left;
  932. left:=nil;
  933. exit;
  934. end;
  935. { fold nil to any pointer type }
  936. if (left.nodetype=niln) and (resulttype.def.deftype=pointerdef) then
  937. begin
  938. hp:=cnilnode.create;
  939. hp.resulttype:=resulttype;
  940. result:=hp;
  941. exit;
  942. end;
  943. { further, pointerconstn to any pointer is folded too }
  944. if (left.nodetype=pointerconstn) and (resulttype.def.deftype=pointerdef) then
  945. begin
  946. left.resulttype:=resulttype;
  947. result:=left;
  948. left:=nil;
  949. exit;
  950. end;
  951. { now call the resulttype helper to do constant folding }
  952. result:=resulttype_call_helper(convtype);
  953. end;
  954. function ttypeconvnode.first_cord_to_pointer : tnode;
  955. begin
  956. result:=nil;
  957. internalerror(200104043);
  958. end;
  959. function ttypeconvnode.first_int_to_int : tnode;
  960. begin
  961. first_int_to_int:=nil;
  962. if (left.location.loc<>LOC_REGISTER) and
  963. (resulttype.def.size>left.resulttype.def.size) then
  964. location.loc:=LOC_REGISTER;
  965. if is_64bitint(resulttype.def) then
  966. registers32:=max(registers32,2)
  967. else
  968. registers32:=max(registers32,1);
  969. end;
  970. function ttypeconvnode.first_cstring_to_pchar : tnode;
  971. begin
  972. first_cstring_to_pchar:=nil;
  973. registers32:=1;
  974. location.loc:=LOC_REGISTER;
  975. end;
  976. function ttypeconvnode.first_string_to_chararray : tnode;
  977. begin
  978. first_string_to_chararray:=nil;
  979. registers32:=1;
  980. location.loc:=LOC_REGISTER;
  981. end;
  982. function ttypeconvnode.first_char_to_string : tnode;
  983. begin
  984. first_char_to_string:=nil;
  985. location.loc:=LOC_MEM;
  986. end;
  987. function ttypeconvnode.first_nothing : tnode;
  988. begin
  989. first_nothing:=nil;
  990. location.loc:=LOC_MEM;
  991. end;
  992. function ttypeconvnode.first_array_to_pointer : tnode;
  993. begin
  994. first_array_to_pointer:=nil;
  995. if registers32<1 then
  996. registers32:=1;
  997. location.loc:=LOC_REGISTER;
  998. end;
  999. function ttypeconvnode.first_int_to_real : tnode;
  1000. begin
  1001. first_int_to_real:=nil;
  1002. {$ifdef m68k}
  1003. if (cs_fp_emulation in aktmoduleswitches) or
  1004. (tfloatdef(resulttype.def).typ=s32real) then
  1005. begin
  1006. if registers32<1 then
  1007. registers32:=1;
  1008. end
  1009. else
  1010. if registersfpu<1 then
  1011. registersfpu:=1;
  1012. {$else not m68k}
  1013. if registersfpu<1 then
  1014. registersfpu:=1;
  1015. {$endif not m68k}
  1016. location.loc:=LOC_FPU;
  1017. end;
  1018. function ttypeconvnode.first_real_to_real : tnode;
  1019. begin
  1020. first_real_to_real:=nil;
  1021. { comp isn't a floating type }
  1022. {$ifdef i386}
  1023. if (tfloatdef(resulttype.def).typ=s64comp) and
  1024. (tfloatdef(left.resulttype.def).typ<>s64comp) and
  1025. not (nf_explizit in flags) then
  1026. CGMessage(type_w_convert_real_2_comp);
  1027. {$endif}
  1028. if registersfpu<1 then
  1029. registersfpu:=1;
  1030. location.loc:=LOC_FPU;
  1031. end;
  1032. function ttypeconvnode.first_pointer_to_array : tnode;
  1033. begin
  1034. first_pointer_to_array:=nil;
  1035. if registers32<1 then
  1036. registers32:=1;
  1037. location.loc:=LOC_REFERENCE;
  1038. end;
  1039. function ttypeconvnode.first_cchar_to_pchar : tnode;
  1040. begin
  1041. first_cchar_to_pchar:=nil;
  1042. internalerror(200104021);
  1043. end;
  1044. function ttypeconvnode.first_bool_to_int : tnode;
  1045. begin
  1046. first_bool_to_int:=nil;
  1047. { byte(boolean) or word(wordbool) or longint(longbool) must
  1048. be accepted for var parameters }
  1049. if (nf_explizit in flags) and
  1050. (left.resulttype.def.size=resulttype.def.size) and
  1051. (left.location.loc in [LOC_REFERENCE,LOC_MEM,LOC_CREGISTER]) then
  1052. exit;
  1053. location.loc:=LOC_REGISTER;
  1054. if registers32<1 then
  1055. registers32:=1;
  1056. end;
  1057. function ttypeconvnode.first_int_to_bool : tnode;
  1058. begin
  1059. first_int_to_bool:=nil;
  1060. { byte(boolean) or word(wordbool) or longint(longbool) must
  1061. be accepted for var parameters }
  1062. if (nf_explizit in flags) and
  1063. (left.resulttype.def.size=resulttype.def.size) and
  1064. (left.location.loc in [LOC_REFERENCE,LOC_MEM,LOC_CREGISTER]) then
  1065. exit;
  1066. location.loc:=LOC_REGISTER;
  1067. { need if bool to bool !!
  1068. not very nice !!
  1069. insertypeconv(left,s32bittype);
  1070. left.explizit:=true;
  1071. firstpass(left); }
  1072. if registers32<1 then
  1073. registers32:=1;
  1074. end;
  1075. function ttypeconvnode.first_bool_to_bool : tnode;
  1076. begin
  1077. first_bool_to_bool:=nil;
  1078. location.loc:=LOC_REGISTER;
  1079. if registers32<1 then
  1080. registers32:=1;
  1081. end;
  1082. function ttypeconvnode.first_char_to_char : tnode;
  1083. begin
  1084. first_char_to_char:=nil;
  1085. location.loc:=LOC_REGISTER;
  1086. if registers32<1 then
  1087. registers32:=1;
  1088. end;
  1089. function ttypeconvnode.first_proc_to_procvar : tnode;
  1090. begin
  1091. first_proc_to_procvar:=nil;
  1092. if (left.location.loc<>LOC_REFERENCE) then
  1093. CGMessage(cg_e_illegal_expression);
  1094. registers32:=left.registers32;
  1095. if registers32<1 then
  1096. registers32:=1;
  1097. location.loc:=LOC_REGISTER;
  1098. end;
  1099. function ttypeconvnode.first_load_smallset : tnode;
  1100. begin
  1101. first_load_smallset:=nil;
  1102. end;
  1103. function ttypeconvnode.first_ansistring_to_pchar : tnode;
  1104. begin
  1105. first_ansistring_to_pchar:=nil;
  1106. location.loc:=LOC_REGISTER;
  1107. if registers32<1 then
  1108. registers32:=1;
  1109. end;
  1110. function ttypeconvnode.first_arrayconstructor_to_set : tnode;
  1111. begin
  1112. first_arrayconstructor_to_set:=nil;
  1113. internalerror(200104022);
  1114. end;
  1115. function ttypeconvnode.first_class_to_intf : tnode;
  1116. begin
  1117. first_class_to_intf:=nil;
  1118. location.loc:=LOC_REFERENCE;
  1119. if registers32<1 then
  1120. registers32:=1;
  1121. end;
  1122. function ttypeconvnode.first_call_helper(c : tconverttype) : tnode;
  1123. const
  1124. firstconvert : array[tconverttype] of pointer = (
  1125. @ttypeconvnode.first_nothing, {equal}
  1126. @ttypeconvnode.first_nothing, {not_possible}
  1127. nil, { removed in resulttype_string_to_string }
  1128. @ttypeconvnode.first_char_to_string,
  1129. nil, { removed in resulttype_chararray_to_string }
  1130. @ttypeconvnode.first_cchar_to_pchar,
  1131. @ttypeconvnode.first_cstring_to_pchar,
  1132. @ttypeconvnode.first_ansistring_to_pchar,
  1133. @ttypeconvnode.first_string_to_chararray,
  1134. nil, { removed in resulttype_chararray_to_string }
  1135. @ttypeconvnode.first_array_to_pointer,
  1136. @ttypeconvnode.first_pointer_to_array,
  1137. @ttypeconvnode.first_int_to_int,
  1138. @ttypeconvnode.first_int_to_bool,
  1139. @ttypeconvnode.first_bool_to_bool,
  1140. @ttypeconvnode.first_bool_to_int,
  1141. @ttypeconvnode.first_real_to_real,
  1142. @ttypeconvnode.first_int_to_real,
  1143. @ttypeconvnode.first_proc_to_procvar,
  1144. @ttypeconvnode.first_arrayconstructor_to_set,
  1145. @ttypeconvnode.first_load_smallset,
  1146. @ttypeconvnode.first_cord_to_pointer,
  1147. @ttypeconvnode.first_nothing,
  1148. @ttypeconvnode.first_nothing,
  1149. @ttypeconvnode.first_class_to_intf,
  1150. @ttypeconvnode.first_char_to_char,
  1151. @ttypeconvnode.first_nothing
  1152. );
  1153. type
  1154. tprocedureofobject = function : tnode of object;
  1155. var
  1156. r : packed record
  1157. proc : pointer;
  1158. obj : pointer;
  1159. end;
  1160. begin
  1161. { this is a little bit dirty but it works }
  1162. { and should be quite portable too }
  1163. r.proc:=firstconvert[c];
  1164. r.obj:=self;
  1165. first_call_helper:=tprocedureofobject(r){$ifdef FPC}();{$endif FPC}
  1166. end;
  1167. function ttypeconvnode.pass_1 : tnode;
  1168. begin
  1169. result:=nil;
  1170. firstpass(left);
  1171. if codegenerror then
  1172. exit;
  1173. { load the value_str from the left part }
  1174. registers32:=left.registers32;
  1175. registersfpu:=left.registersfpu;
  1176. {$ifdef SUPPORT_MMX}
  1177. registersmmx:=left.registersmmx;
  1178. {$endif}
  1179. set_location(location,left.location);
  1180. if nf_explizit in flags then
  1181. begin
  1182. { check if the result could be in a register }
  1183. if not(tstoreddef(resulttype.def).is_intregable) and
  1184. not(tstoreddef(resulttype.def).is_fpuregable) then
  1185. make_not_regable(left);
  1186. end;
  1187. if convtype=tc_equal then
  1188. begin
  1189. { remove typeconv node if left is a const. For other nodes we can't
  1190. remove it because the secondpass can still depend on the old type (PFV) }
  1191. if is_constnode(left) then
  1192. begin
  1193. left.resulttype:=resulttype;
  1194. result:=left;
  1195. left:=nil;
  1196. end;
  1197. end
  1198. else
  1199. begin
  1200. result:=first_call_helper(convtype);
  1201. end;
  1202. end;
  1203. {*****************************************************************************
  1204. TISNODE
  1205. *****************************************************************************}
  1206. constructor tisnode.create(l,r : tnode);
  1207. begin
  1208. inherited create(isn,l,r);
  1209. end;
  1210. function tisnode.det_resulttype:tnode;
  1211. begin
  1212. result:=nil;
  1213. resulttypepass(left);
  1214. resulttypepass(right);
  1215. set_varstate(left,true);
  1216. set_varstate(right,true);
  1217. if codegenerror then
  1218. exit;
  1219. if (right.resulttype.def.deftype=classrefdef) then
  1220. begin
  1221. { left must be a class }
  1222. if is_class(left.resulttype.def) then
  1223. begin
  1224. { the operands must be related }
  1225. if (not(tobjectdef(left.resulttype.def).is_related(
  1226. tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def)))) and
  1227. (not(tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def).is_related(
  1228. tobjectdef(left.resulttype.def)))) then
  1229. CGMessage(type_e_mismatch);
  1230. end
  1231. else
  1232. CGMessage(type_e_mismatch);
  1233. end
  1234. else
  1235. CGMessage(type_e_mismatch);
  1236. resulttype:=booltype;
  1237. end;
  1238. function tisnode.pass_1 : tnode;
  1239. begin
  1240. result:=nil;
  1241. firstpass(left);
  1242. firstpass(right);
  1243. if codegenerror then
  1244. exit;
  1245. left_right_max;
  1246. location.loc:=LOC_FLAGS;
  1247. end;
  1248. {*****************************************************************************
  1249. TASNODE
  1250. *****************************************************************************}
  1251. constructor tasnode.create(l,r : tnode);
  1252. begin
  1253. inherited create(asn,l,r);
  1254. end;
  1255. function tasnode.det_resulttype:tnode;
  1256. begin
  1257. result:=nil;
  1258. resulttypepass(right);
  1259. resulttypepass(left);
  1260. set_varstate(right,true);
  1261. set_varstate(left,true);
  1262. if codegenerror then
  1263. exit;
  1264. if (right.resulttype.def.deftype=classrefdef) then
  1265. begin
  1266. { left must be a class }
  1267. if is_class(left.resulttype.def) then
  1268. begin
  1269. { the operands must be related }
  1270. if (not(tobjectdef(left.resulttype.def).is_related(
  1271. tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def)))) and
  1272. (not(tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def).is_related(
  1273. tobjectdef(left.resulttype.def)))) then
  1274. CGMessage(type_e_mismatch);
  1275. end
  1276. else
  1277. CGMessage(type_e_mismatch);
  1278. resulttype:=tclassrefdef(right.resulttype.def).pointertype;
  1279. end
  1280. else
  1281. CGMessage(type_e_mismatch);
  1282. end;
  1283. function tasnode.pass_1 : tnode;
  1284. begin
  1285. result:=nil;
  1286. firstpass(right);
  1287. firstpass(left);
  1288. if codegenerror then
  1289. exit;
  1290. left_right_max;
  1291. set_location(location,left.location);
  1292. end;
  1293. function ttypeconvnode.docompare(p: tnode) : boolean;
  1294. begin
  1295. docompare :=
  1296. inherited docompare(p) and
  1297. (convtype = ttypeconvnode(p).convtype);
  1298. end;
  1299. begin
  1300. ctypeconvnode:=ttypeconvnode;
  1301. casnode:=tasnode;
  1302. cisnode:=tisnode;
  1303. end.
  1304. {
  1305. $Log$
  1306. Revision 1.37 2001-09-03 13:27:42 jonas
  1307. * compilerproc implementation of set addition/substraction/...
  1308. * changed the declaration of some set helpers somewhat to accomodate the
  1309. above change
  1310. * i386 still uses the old code for comparisons of sets, because its
  1311. helpers return the results in the flags
  1312. * dummy tc_normal_2_small_set type conversion because I need the original
  1313. resulttype of the set add nodes
  1314. NOTE: you have to start a cycle with 1.0.5!
  1315. Revision 1.36 2001/09/02 21:12:06 peter
  1316. * move class of definitions into type section for delphi
  1317. Revision 1.35 2001/08/29 19:49:03 jonas
  1318. * some fixes in compilerprocs for chararray to string conversions
  1319. * conversion from string to chararray is now also done via compilerprocs
  1320. Revision 1.34 2001/08/29 12:18:07 jonas
  1321. + new createinternres() constructor for tcallnode to support setting a
  1322. custom resulttype
  1323. * compilerproc typeconversions now set the resulttype from the type
  1324. conversion for the generated call node, because the resulttype of
  1325. of the compilerproc helper isn't always exact (e.g. the ones that
  1326. return shortstrings, actually return a shortstring[x], where x is
  1327. specified by the typeconversion node)
  1328. * ti386callnode.pass_2 now always uses resulttype instead of
  1329. procsym.definition.rettype (so the custom resulttype, if any, is
  1330. always used). Note that this "rettype" stuff is only for use with
  1331. compilerprocs.
  1332. Revision 1.33 2001/08/28 13:24:46 jonas
  1333. + compilerproc implementation of most string-related type conversions
  1334. - removed all code from the compiler which has been replaced by
  1335. compilerproc implementations (using (ifdef hascompilerproc) is not
  1336. necessary in the compiler)
  1337. Revision 1.32 2001/08/26 13:36:40 florian
  1338. * some cg reorganisation
  1339. * some PPC updates
  1340. Revision 1.31 2001/08/05 13:19:51 peter
  1341. * partly fix for proc of obj=nil
  1342. Revision 1.30 2001/07/30 20:59:27 peter
  1343. * m68k updates from v10 merged
  1344. Revision 1.29 2001/07/08 21:00:15 peter
  1345. * various widestring updates, it works now mostly without charset
  1346. mapping supported
  1347. Revision 1.28 2001/05/13 15:43:46 florian
  1348. * made resultype_char_to_char a little bit robuster
  1349. Revision 1.27 2001/05/08 21:06:30 florian
  1350. * some more support for widechars commited especially
  1351. regarding type casting and constants
  1352. Revision 1.26 2001/05/04 15:52:03 florian
  1353. * some Delphi incompatibilities fixed:
  1354. - out, dispose and new can be used as idenfiers now
  1355. - const p = apointerype(nil); is supported now
  1356. + support for const p = apointertype(pointer(1234)); added
  1357. Revision 1.25 2001/04/13 22:20:58 peter
  1358. * remove wrongly placed first_call_helper
  1359. Revision 1.24 2001/04/13 01:22:08 peter
  1360. * symtable change to classes
  1361. * range check generation and errors fixed, make cycle DEBUG=1 works
  1362. * memory leaks fixed
  1363. Revision 1.23 2001/04/04 22:42:39 peter
  1364. * move constant folding into det_resulttype
  1365. Revision 1.22 2001/04/02 21:20:30 peter
  1366. * resulttype rewrite
  1367. Revision 1.21 2001/03/08 17:44:47 jonas
  1368. * fixed web bug 1430
  1369. Revision 1.20 2001/02/21 11:49:50 jonas
  1370. * evaluate typecasts of const pointers to ordinals inline ('merged')
  1371. Revision 1.19 2001/02/20 18:37:10 peter
  1372. * removed unused code
  1373. Revision 1.18 2001/02/20 13:14:18 marco
  1374. * Fix from Peter for passing a procedure of method to a other method in a method
  1375. Revision 1.17 2001/02/08 13:09:03 jonas
  1376. * fixed web bug 1396: tpointerord is now a cardinal instead of a longint,
  1377. but added a hack in ncnv so that pointer(-1) still works
  1378. Revision 1.16 2000/12/31 11:14:10 jonas
  1379. + implemented/fixed docompare() mathods for all nodes (not tested)
  1380. + nopt.pas, nadd.pas, i386/n386opt.pas: optimized nodes for adding strings
  1381. and constant strings/chars together
  1382. * n386add.pas: don't copy temp strings (of size 256) to another temp string
  1383. when adding
  1384. Revision 1.15 2000/12/08 12:41:01 jonas
  1385. * fixed bug in sign extension patch
  1386. Revision 1.14 2000/12/07 17:19:42 jonas
  1387. * new constant handling: from now on, hex constants >$7fffffff are
  1388. parsed as unsigned constants (otherwise, $80000000 got sign extended
  1389. and became $ffffffff80000000), all constants in the longint range
  1390. become longints, all constants >$7fffffff and <=cardinal($ffffffff)
  1391. are cardinals and the rest are int64's.
  1392. * added lots of longint typecast to prevent range check errors in the
  1393. compiler and rtl
  1394. * type casts of symbolic ordinal constants are now preserved
  1395. * fixed bug where the original resulttype.def wasn't restored correctly
  1396. after doing a 64bit rangecheck
  1397. Revision 1.13 2000/11/29 00:30:32 florian
  1398. * unused units removed from uses clause
  1399. * some changes for widestrings
  1400. Revision 1.12 2000/11/20 16:06:04 jonas
  1401. + allow evaluation of 64bit constant expressions at compile time
  1402. * disable range checking for explicit typecasts of constant expressions
  1403. Revision 1.11 2000/11/12 23:24:11 florian
  1404. * interfaces are basically running
  1405. Revision 1.10 2000/11/04 14:25:20 florian
  1406. + merged Attila's changes for interfaces, not tested yet
  1407. Revision 1.9 2000/10/31 22:02:48 peter
  1408. * symtable splitted, no real code changes
  1409. Revision 1.8 2000/10/14 21:52:55 peter
  1410. * fixed memory leaks
  1411. Revision 1.7 2000/10/14 10:14:50 peter
  1412. * moehrendorf oct 2000 rewrite
  1413. Revision 1.6 2000/10/01 19:48:24 peter
  1414. * lot of compile updates for cg11
  1415. Revision 1.5 2000/09/28 19:49:52 florian
  1416. *** empty log message ***
  1417. Revision 1.4 2000/09/27 18:14:31 florian
  1418. * fixed a lot of syntax errors in the n*.pas stuff
  1419. Revision 1.3 2000/09/26 20:06:13 florian
  1420. * hmm, still a lot of work to get things compilable
  1421. Revision 1.2 2000/09/26 14:59:34 florian
  1422. * more conversion work done
  1423. Revision 1.1 2000/09/25 15:37:14 florian
  1424. * more fixes
  1425. }