ncnv.pas 60 KB

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