ncnv.pas 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780
  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. currprocdef,
  667. aprocdef : tprocdef;
  668. begin
  669. result:=nil;
  670. resulttype:=totype;
  671. resulttypepass(left);
  672. if codegenerror then
  673. exit;
  674. { remove obsolete type conversions }
  675. if is_equal(left.resulttype.def,resulttype.def) then
  676. begin
  677. { becuase is_equal only checks the basetype for sets we need to
  678. check here if we are loading a smallset into a normalset }
  679. if (resulttype.def.deftype=setdef) and
  680. (left.resulttype.def.deftype=setdef) and
  681. ((tsetdef(resulttype.def).settype = smallset) xor
  682. (tsetdef(left.resulttype.def).settype = smallset)) then
  683. begin
  684. { try to define the set as a normalset if it's a constant set }
  685. if (tsetdef(resulttype.def).settype <> smallset) then
  686. begin
  687. if (left.nodetype=setconstn) then
  688. begin
  689. tsetdef(left.resulttype.def).changesettype(normset);
  690. result:=left;
  691. left:=nil;
  692. exit;
  693. end
  694. else
  695. convtype:=tc_load_smallset;
  696. end
  697. else
  698. convtype := tc_normal_2_smallset;
  699. exit;
  700. end
  701. else
  702. begin
  703. left.resulttype:=resulttype;
  704. result:=left;
  705. left:=nil;
  706. exit;
  707. end;
  708. end;
  709. aprocdef:=assignment_overloaded(left.resulttype.def,resulttype.def);
  710. if assigned(aprocdef) then
  711. begin
  712. procinfo^.flags:=procinfo^.flags or pi_do_call;
  713. hp:=ccallnode.create(ccallparanode.create(left,nil),
  714. overloaded_operators[_assignment],nil,nil);
  715. { tell explicitly which def we must use !! (PM) }
  716. tcallnode(hp).procdefinition:=aprocdef;
  717. left:=nil;
  718. result:=hp;
  719. exit;
  720. end;
  721. if isconvertable(left.resulttype.def,resulttype.def,convtype,left.nodetype,nf_explizit in flags)=0 then
  722. begin
  723. {Procedures have a resulttype.def of voiddef and functions of their
  724. own resulttype.def. They will therefore always be incompatible with
  725. a procvar. Because isconvertable cannot check for procedures we
  726. use an extra check for them.}
  727. if (m_tp_procvar in aktmodeswitches) then
  728. begin
  729. if (resulttype.def.deftype=procvardef) and
  730. (is_procsym_load(left) or is_procsym_call(left)) then
  731. begin
  732. if is_procsym_call(left) then
  733. begin
  734. currprocdef:=get_proc_2_procvar_def(tprocsym(tcallnode(left).symtableprocentry),tprocvardef(resulttype.def));
  735. hp:=cloadnode.create_procvar(tprocsym(tcallnode(left).symtableprocentry),
  736. currprocdef,tcallnode(left).symtableproc);
  737. if (tcallnode(left).symtableprocentry.owner.symtabletype=objectsymtable) and
  738. assigned(tcallnode(left).methodpointer) then
  739. tloadnode(hp).set_mp(tcallnode(left).methodpointer.getcopy);
  740. resulttypepass(hp);
  741. left.free;
  742. left:=hp;
  743. aprocdef:=tprocdef(left.resulttype.def);
  744. end
  745. else
  746. begin
  747. if (left.nodetype<>addrn) then
  748. aprocdef:=tprocsym(tloadnode(left).symtableentry).defs^.def;
  749. end;
  750. convtype:=tc_proc_2_procvar;
  751. { Now check if the procedure we are going to assign to
  752. the procvar, is compatible with the procvar's type }
  753. if assigned(aprocdef) then
  754. begin
  755. if not proc_to_procvar_equal(aprocdef,tprocvardef(resulttype.def),false) then
  756. CGMessage2(type_e_incompatible_types,aprocdef.typename,resulttype.def.typename);
  757. end
  758. else
  759. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  760. exit;
  761. end;
  762. end;
  763. if nf_explizit in flags then
  764. begin
  765. { check if the result could be in a register }
  766. if not(tstoreddef(resulttype.def).is_intregable) and
  767. not(tstoreddef(resulttype.def).is_fpuregable) then
  768. make_not_regable(left);
  769. { boolean to byte are special because the
  770. location can be different }
  771. if is_integer(resulttype.def) and
  772. is_boolean(left.resulttype.def) then
  773. begin
  774. convtype:=tc_bool_2_int;
  775. exit;
  776. end;
  777. { ansistring to pchar }
  778. if is_pchar(resulttype.def) and
  779. is_ansistring(left.resulttype.def) then
  780. begin
  781. convtype:=tc_ansistring_2_pchar;
  782. exit;
  783. end;
  784. { do common tc_equal cast }
  785. convtype:=tc_equal;
  786. { enum to ordinal will always be s32bit }
  787. if (left.resulttype.def.deftype=enumdef) and
  788. is_ordinal(resulttype.def) then
  789. begin
  790. if left.nodetype=ordconstn then
  791. begin
  792. hp:=cordconstnode.create(tordconstnode(left).value,resulttype);
  793. result:=hp;
  794. exit;
  795. end
  796. else
  797. begin
  798. if isconvertable(s32bittype.def,resulttype.def,convtype,ordconstn,false)=0 then
  799. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  800. end;
  801. end
  802. { ordinal to enumeration }
  803. else
  804. if (resulttype.def.deftype=enumdef) and
  805. is_ordinal(left.resulttype.def) then
  806. begin
  807. if left.nodetype=ordconstn then
  808. begin
  809. hp:=cordconstnode.create(tordconstnode(left).value,resulttype);
  810. result:=hp;
  811. exit;
  812. end
  813. else
  814. begin
  815. if IsConvertable(left.resulttype.def,s32bittype.def,convtype,ordconstn,false)=0 then
  816. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  817. end;
  818. end
  819. { nil to ordinal node }
  820. else if (left.nodetype=niln) and is_ordinal(resulttype.def) then
  821. begin
  822. hp:=cordconstnode.create(0,resulttype);
  823. result:=hp;
  824. exit;
  825. end
  826. { constant pointer to ordinal }
  827. else if is_ordinal(resulttype.def) and
  828. (left.nodetype=pointerconstn) then
  829. begin
  830. hp:=cordconstnode.create(tpointerconstnode(left).value,resulttype);
  831. result:=hp;
  832. exit;
  833. end
  834. {Are we typecasting an ordconst to a char?}
  835. else
  836. if is_char(resulttype.def) and
  837. is_ordinal(left.resulttype.def) then
  838. begin
  839. if left.nodetype=ordconstn then
  840. begin
  841. hp:=cordconstnode.create(tordconstnode(left).value,resulttype);
  842. result:=hp;
  843. exit;
  844. end
  845. else
  846. begin
  847. if IsConvertable(left.resulttype.def,u8bittype.def,convtype,ordconstn,false)=0 then
  848. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  849. end;
  850. end
  851. {Are we typecasting an ordconst to a wchar?}
  852. else
  853. if is_widechar(resulttype.def) and
  854. is_ordinal(left.resulttype.def) then
  855. begin
  856. if left.nodetype=ordconstn then
  857. begin
  858. hp:=cordconstnode.create(tordconstnode(left).value,resulttype);
  859. result:=hp;
  860. exit;
  861. end
  862. else
  863. begin
  864. if IsConvertable(left.resulttype.def,u16bittype.def,convtype,ordconstn,false)=0 then
  865. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  866. end;
  867. end
  868. { char to ordinal }
  869. else
  870. if is_char(left.resulttype.def) and
  871. is_ordinal(resulttype.def) then
  872. begin
  873. if left.nodetype=ordconstn then
  874. begin
  875. hp:=cordconstnode.create(tordconstnode(left).value,resulttype);
  876. result:=hp;
  877. exit;
  878. end
  879. else
  880. begin
  881. if IsConvertable(u8bittype.def,resulttype.def,convtype,ordconstn,false)=0 then
  882. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  883. end;
  884. end
  885. { widechar to ordinal }
  886. else
  887. if is_widechar(left.resulttype.def) and
  888. is_ordinal(resulttype.def) then
  889. begin
  890. if left.nodetype=ordconstn then
  891. begin
  892. hp:=cordconstnode.create(tordconstnode(left).value,resulttype);
  893. result:=hp;
  894. exit;
  895. end
  896. else
  897. begin
  898. if IsConvertable(u16bittype.def,resulttype.def,convtype,ordconstn,false)=0 then
  899. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  900. end;
  901. end
  902. { only if the same size or formal def }
  903. { why do we allow typecasting of voiddef ?? (PM) }
  904. else
  905. begin
  906. if not(
  907. (left.resulttype.def.deftype=formaldef) or
  908. (left.resulttype.def.size=resulttype.def.size) or
  909. (is_void(left.resulttype.def) and
  910. (left.nodetype=derefn))
  911. ) then
  912. CGMessage(cg_e_illegal_type_conversion);
  913. if ((left.resulttype.def.deftype=orddef) and
  914. (resulttype.def.deftype=pointerdef)) or
  915. ((resulttype.def.deftype=orddef) and
  916. (left.resulttype.def.deftype=pointerdef)) then
  917. CGMessage(cg_d_pointer_to_longint_conv_not_portable);
  918. end;
  919. { the conversion into a strutured type is only }
  920. { possible, if the source is not a register }
  921. if ((resulttype.def.deftype in [recorddef,stringdef,arraydef]) or
  922. ((resulttype.def.deftype=objectdef) and not(is_class(resulttype.def)))
  923. ) and (left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) { and
  924. it also works if the assignment is overloaded
  925. YES but this code is not executed if assignment is overloaded (PM)
  926. not assigned(assignment_overloaded(left.resulttype.def,resulttype.def))} then
  927. CGMessage(cg_e_illegal_type_conversion);
  928. end
  929. else
  930. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  931. end;
  932. { tp7 procvar support, when right is not a procvardef and we got a
  933. loadn of a procvar then convert to a calln, the check for the
  934. result is already done in is_convertible, also no conflict with
  935. @procvar is here because that has an extra addrn }
  936. if (m_tp_procvar in aktmodeswitches) and
  937. (resulttype.def.deftype<>procvardef) and
  938. (left.resulttype.def.deftype=procvardef) and
  939. (left.nodetype=loadn) then
  940. begin
  941. hp:=ccallnode.create(nil,nil,nil,nil);
  942. tcallnode(hp).set_procvar(left);
  943. resulttypepass(hp);
  944. left:=hp;
  945. end;
  946. { remove typeconv after niln }
  947. if (left.nodetype=niln) then
  948. begin
  949. left.resulttype:=resulttype;
  950. result:=left;
  951. left:=nil;
  952. exit;
  953. end;
  954. { ordinal contants can be directly converted }
  955. if (left.nodetype=ordconstn) and is_ordinal(resulttype.def) then
  956. begin
  957. { replace the resulttype and recheck the range }
  958. left.resulttype:=resulttype;
  959. testrange(left.resulttype.def,tordconstnode(left).value,(nf_explizit in flags));
  960. result:=left;
  961. left:=nil;
  962. exit;
  963. end;
  964. { fold nil to any pointer type }
  965. if (left.nodetype=niln) and (resulttype.def.deftype=pointerdef) then
  966. begin
  967. hp:=cnilnode.create;
  968. hp.resulttype:=resulttype;
  969. result:=hp;
  970. exit;
  971. end;
  972. { further, pointerconstn to any pointer is folded too }
  973. if (left.nodetype=pointerconstn) and (resulttype.def.deftype=pointerdef) then
  974. begin
  975. left.resulttype:=resulttype;
  976. result:=left;
  977. left:=nil;
  978. exit;
  979. end;
  980. { now call the resulttype helper to do constant folding }
  981. result:=resulttype_call_helper(convtype);
  982. end;
  983. function ttypeconvnode.first_cord_to_pointer : tnode;
  984. begin
  985. result:=nil;
  986. internalerror(200104043);
  987. end;
  988. function ttypeconvnode.first_int_to_int : tnode;
  989. begin
  990. first_int_to_int:=nil;
  991. if (left.location.loc<>LOC_REGISTER) and
  992. (resulttype.def.size>left.resulttype.def.size) then
  993. location.loc:=LOC_REGISTER;
  994. if is_64bitint(resulttype.def) then
  995. registers32:=max(registers32,2)
  996. else
  997. registers32:=max(registers32,1);
  998. end;
  999. function ttypeconvnode.first_cstring_to_pchar : tnode;
  1000. begin
  1001. first_cstring_to_pchar:=nil;
  1002. registers32:=1;
  1003. location.loc:=LOC_REGISTER;
  1004. end;
  1005. function ttypeconvnode.first_string_to_chararray : tnode;
  1006. begin
  1007. first_string_to_chararray:=nil;
  1008. registers32:=1;
  1009. location.loc:=LOC_REGISTER;
  1010. end;
  1011. function ttypeconvnode.first_char_to_string : tnode;
  1012. begin
  1013. first_char_to_string:=nil;
  1014. location.loc:=LOC_MEM;
  1015. end;
  1016. function ttypeconvnode.first_nothing : tnode;
  1017. begin
  1018. first_nothing:=nil;
  1019. location.loc:=LOC_MEM;
  1020. end;
  1021. function ttypeconvnode.first_array_to_pointer : tnode;
  1022. begin
  1023. first_array_to_pointer:=nil;
  1024. if registers32<1 then
  1025. registers32:=1;
  1026. location.loc:=LOC_REGISTER;
  1027. end;
  1028. function ttypeconvnode.first_int_to_real : tnode;
  1029. begin
  1030. first_int_to_real:=nil;
  1031. {$ifdef m68k}
  1032. if (cs_fp_emulation in aktmoduleswitches) or
  1033. (tfloatdef(resulttype.def).typ=s32real) then
  1034. begin
  1035. if registers32<1 then
  1036. registers32:=1;
  1037. end
  1038. else
  1039. if registersfpu<1 then
  1040. registersfpu:=1;
  1041. {$else not m68k}
  1042. if registersfpu<1 then
  1043. registersfpu:=1;
  1044. {$endif not m68k}
  1045. location.loc:=LOC_FPU;
  1046. end;
  1047. function ttypeconvnode.first_real_to_real : tnode;
  1048. begin
  1049. first_real_to_real:=nil;
  1050. { comp isn't a floating type }
  1051. {$ifdef i386}
  1052. if (tfloatdef(resulttype.def).typ=s64comp) and
  1053. (tfloatdef(left.resulttype.def).typ<>s64comp) and
  1054. not (nf_explizit in flags) then
  1055. CGMessage(type_w_convert_real_2_comp);
  1056. {$endif}
  1057. if registersfpu<1 then
  1058. registersfpu:=1;
  1059. location.loc:=LOC_FPU;
  1060. end;
  1061. function ttypeconvnode.first_pointer_to_array : tnode;
  1062. begin
  1063. first_pointer_to_array:=nil;
  1064. if registers32<1 then
  1065. registers32:=1;
  1066. location.loc:=LOC_REFERENCE;
  1067. end;
  1068. function ttypeconvnode.first_cchar_to_pchar : tnode;
  1069. begin
  1070. first_cchar_to_pchar:=nil;
  1071. internalerror(200104021);
  1072. end;
  1073. function ttypeconvnode.first_bool_to_int : tnode;
  1074. begin
  1075. first_bool_to_int:=nil;
  1076. { byte(boolean) or word(wordbool) or longint(longbool) must
  1077. be accepted for var parameters }
  1078. if (nf_explizit in flags) and
  1079. (left.resulttype.def.size=resulttype.def.size) and
  1080. (left.location.loc in [LOC_REFERENCE,LOC_MEM,LOC_CREGISTER]) then
  1081. exit;
  1082. { when converting to 64bit, first convert to a 32bit int and then }
  1083. { convert to a 64bit int (only necessary for 32bit processors) (JM) }
  1084. if resulttype.def.size > sizeof(aword) then
  1085. begin
  1086. result := ctypeconvnode.create(left,u32bittype);
  1087. result.toggleflag(nf_explizit);
  1088. result := ctypeconvnode.create(result,resulttype);
  1089. left := nil;
  1090. firstpass(result);
  1091. exit;
  1092. end;
  1093. location.loc:=LOC_REGISTER;
  1094. if registers32<1 then
  1095. registers32:=1;
  1096. end;
  1097. function ttypeconvnode.first_int_to_bool : tnode;
  1098. begin
  1099. first_int_to_bool:=nil;
  1100. { byte(boolean) or word(wordbool) or longint(longbool) must
  1101. be accepted for var parameters }
  1102. if (nf_explizit in flags) and
  1103. (left.resulttype.def.size=resulttype.def.size) and
  1104. (left.location.loc in [LOC_REFERENCE,LOC_MEM,LOC_CREGISTER]) then
  1105. exit;
  1106. location.loc:=LOC_REGISTER;
  1107. { need if bool to bool !!
  1108. not very nice !!
  1109. insertypeconv(left,s32bittype);
  1110. left.explizit:=true;
  1111. firstpass(left); }
  1112. if registers32<1 then
  1113. registers32:=1;
  1114. end;
  1115. function ttypeconvnode.first_bool_to_bool : tnode;
  1116. begin
  1117. first_bool_to_bool:=nil;
  1118. location.loc:=LOC_REGISTER;
  1119. if registers32<1 then
  1120. registers32:=1;
  1121. end;
  1122. function ttypeconvnode.first_char_to_char : tnode;
  1123. begin
  1124. first_char_to_char:=nil;
  1125. location.loc:=LOC_REGISTER;
  1126. if registers32<1 then
  1127. registers32:=1;
  1128. end;
  1129. function ttypeconvnode.first_proc_to_procvar : tnode;
  1130. begin
  1131. first_proc_to_procvar:=nil;
  1132. if (left.location.loc<>LOC_REFERENCE) then
  1133. CGMessage(cg_e_illegal_expression);
  1134. registers32:=left.registers32;
  1135. if registers32<1 then
  1136. registers32:=1;
  1137. location.loc:=LOC_REGISTER;
  1138. end;
  1139. function ttypeconvnode.first_load_smallset : tnode;
  1140. var
  1141. srsym: ttypesym;
  1142. p: tcallparanode;
  1143. begin
  1144. if not searchsystype('FPC_SMALL_SET',srsym) then
  1145. internalerror(200108313);
  1146. p := ccallparanode.create(left,nil);
  1147. { reused }
  1148. left := nil;
  1149. { convert parameter explicitely to fpc_small_set }
  1150. p.left := ctypeconvnode.create(p.left,srsym.restype);
  1151. p.left.toggleflag(nf_explizit);
  1152. { create call, adjust resulttype }
  1153. result :=
  1154. ccallnode.createinternres('fpc_set_load_small',p,resulttype);
  1155. firstpass(result);
  1156. end;
  1157. function ttypeconvnode.first_ansistring_to_pchar : tnode;
  1158. begin
  1159. first_ansistring_to_pchar:=nil;
  1160. location.loc:=LOC_REGISTER;
  1161. if registers32<1 then
  1162. registers32:=1;
  1163. end;
  1164. function ttypeconvnode.first_arrayconstructor_to_set : tnode;
  1165. begin
  1166. first_arrayconstructor_to_set:=nil;
  1167. internalerror(200104022);
  1168. end;
  1169. function ttypeconvnode.first_class_to_intf : tnode;
  1170. begin
  1171. first_class_to_intf:=nil;
  1172. location.loc:=LOC_REFERENCE;
  1173. if registers32<1 then
  1174. registers32:=1;
  1175. end;
  1176. function ttypeconvnode.first_call_helper(c : tconverttype) : tnode;
  1177. const
  1178. firstconvert : array[tconverttype] of pointer = (
  1179. @ttypeconvnode.first_nothing, {equal}
  1180. @ttypeconvnode.first_nothing, {not_possible}
  1181. nil, { removed in resulttype_string_to_string }
  1182. @ttypeconvnode.first_char_to_string,
  1183. nil, { removed in resulttype_chararray_to_string }
  1184. @ttypeconvnode.first_cchar_to_pchar,
  1185. @ttypeconvnode.first_cstring_to_pchar,
  1186. @ttypeconvnode.first_ansistring_to_pchar,
  1187. @ttypeconvnode.first_string_to_chararray,
  1188. nil, { removed in resulttype_chararray_to_string }
  1189. @ttypeconvnode.first_array_to_pointer,
  1190. @ttypeconvnode.first_pointer_to_array,
  1191. @ttypeconvnode.first_int_to_int,
  1192. @ttypeconvnode.first_int_to_bool,
  1193. @ttypeconvnode.first_bool_to_bool,
  1194. @ttypeconvnode.first_bool_to_int,
  1195. @ttypeconvnode.first_real_to_real,
  1196. @ttypeconvnode.first_int_to_real,
  1197. @ttypeconvnode.first_proc_to_procvar,
  1198. @ttypeconvnode.first_arrayconstructor_to_set,
  1199. @ttypeconvnode.first_load_smallset,
  1200. @ttypeconvnode.first_cord_to_pointer,
  1201. @ttypeconvnode.first_nothing,
  1202. @ttypeconvnode.first_nothing,
  1203. @ttypeconvnode.first_class_to_intf,
  1204. @ttypeconvnode.first_char_to_char,
  1205. @ttypeconvnode.first_nothing
  1206. );
  1207. type
  1208. tprocedureofobject = function : tnode of object;
  1209. var
  1210. r : packed record
  1211. proc : pointer;
  1212. obj : pointer;
  1213. end;
  1214. begin
  1215. { this is a little bit dirty but it works }
  1216. { and should be quite portable too }
  1217. r.proc:=firstconvert[c];
  1218. r.obj:=self;
  1219. first_call_helper:=tprocedureofobject(r){$ifdef FPC}();{$endif FPC}
  1220. end;
  1221. function ttypeconvnode.pass_1 : tnode;
  1222. begin
  1223. result:=nil;
  1224. firstpass(left);
  1225. if codegenerror then
  1226. exit;
  1227. { load the value_str from the left part }
  1228. registers32:=left.registers32;
  1229. registersfpu:=left.registersfpu;
  1230. {$ifdef SUPPORT_MMX}
  1231. registersmmx:=left.registersmmx;
  1232. {$endif}
  1233. set_location(location,left.location);
  1234. if nf_explizit in flags then
  1235. begin
  1236. { check if the result could be in a register }
  1237. if not(tstoreddef(resulttype.def).is_intregable) and
  1238. not(tstoreddef(resulttype.def).is_fpuregable) then
  1239. make_not_regable(left);
  1240. end;
  1241. if convtype=tc_equal then
  1242. begin
  1243. { remove typeconv node if left is a const. For other nodes we can't
  1244. remove it because the secondpass can still depend on the old type (PFV) }
  1245. if is_constnode(left) then
  1246. begin
  1247. left.resulttype:=resulttype;
  1248. result:=left;
  1249. left:=nil;
  1250. end;
  1251. end
  1252. else
  1253. begin
  1254. result:=first_call_helper(convtype);
  1255. end;
  1256. end;
  1257. function ttypeconvnode.docompare(p: tnode) : boolean;
  1258. begin
  1259. docompare :=
  1260. inherited docompare(p) and
  1261. (convtype = ttypeconvnode(p).convtype);
  1262. end;
  1263. procedure ttypeconvnode.second_nothing;
  1264. begin
  1265. end;
  1266. {*****************************************************************************
  1267. TISNODE
  1268. *****************************************************************************}
  1269. constructor tisnode.create(l,r : tnode);
  1270. begin
  1271. inherited create(isn,l,r);
  1272. end;
  1273. function tisnode.det_resulttype:tnode;
  1274. begin
  1275. result:=nil;
  1276. resulttypepass(left);
  1277. resulttypepass(right);
  1278. set_varstate(left,true);
  1279. set_varstate(right,true);
  1280. if codegenerror then
  1281. exit;
  1282. if (right.resulttype.def.deftype=classrefdef) then
  1283. begin
  1284. { left must be a class }
  1285. if is_class(left.resulttype.def) then
  1286. begin
  1287. { the operands must be related }
  1288. if (not(tobjectdef(left.resulttype.def).is_related(
  1289. tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def)))) and
  1290. (not(tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def).is_related(
  1291. tobjectdef(left.resulttype.def)))) then
  1292. CGMessage(type_e_mismatch);
  1293. end
  1294. else
  1295. CGMessage(type_e_mismatch);
  1296. end
  1297. else
  1298. CGMessage(type_e_mismatch);
  1299. resulttype:=booltype;
  1300. end;
  1301. function tisnode.pass_1 : tnode;
  1302. var
  1303. paras: tcallparanode;
  1304. begin
  1305. paras := ccallparanode.create(left,ccallparanode.create(right,nil));
  1306. left := nil;
  1307. right := nil;
  1308. result := ccallnode.createintern('fpc_do_is',paras);
  1309. firstpass(result);
  1310. end;
  1311. { dummy pass_2, it will never be called, but we need one since }
  1312. { you can't instantiate an abstract class }
  1313. procedure tisnode.pass_2;
  1314. begin
  1315. end;
  1316. {*****************************************************************************
  1317. TASNODE
  1318. *****************************************************************************}
  1319. constructor tasnode.create(l,r : tnode);
  1320. begin
  1321. inherited create(asn,l,r);
  1322. end;
  1323. function tasnode.det_resulttype:tnode;
  1324. begin
  1325. result:=nil;
  1326. resulttypepass(right);
  1327. resulttypepass(left);
  1328. set_varstate(right,true);
  1329. set_varstate(left,true);
  1330. if codegenerror then
  1331. exit;
  1332. if (right.resulttype.def.deftype=classrefdef) then
  1333. begin
  1334. { left must be a class }
  1335. if is_class(left.resulttype.def) then
  1336. begin
  1337. { the operands must be related }
  1338. if (not(tobjectdef(left.resulttype.def).is_related(
  1339. tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def)))) and
  1340. (not(tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def).is_related(
  1341. tobjectdef(left.resulttype.def)))) then
  1342. CGMessage(type_e_mismatch);
  1343. end
  1344. else
  1345. CGMessage(type_e_mismatch);
  1346. resulttype:=tclassrefdef(right.resulttype.def).pointertype;
  1347. end
  1348. else
  1349. CGMessage(type_e_mismatch);
  1350. end;
  1351. function tasnode.pass_1 : tnode;
  1352. var
  1353. paras: tcallparanode;
  1354. begin
  1355. paras := ccallparanode.create(left,ccallparanode.create(right,nil));
  1356. left := nil;
  1357. right := nil;
  1358. result := ccallnode.createinternres('fpc_do_as',paras,
  1359. resulttype);
  1360. firstpass(result);
  1361. end;
  1362. { dummy pass_2, it will never be called, but we need one since }
  1363. { you can't instantiate an abstract class }
  1364. procedure tasnode.pass_2;
  1365. begin
  1366. end;
  1367. begin
  1368. ctypeconvnode:=ttypeconvnode;
  1369. casnode:=tasnode;
  1370. cisnode:=tisnode;
  1371. end.
  1372. {
  1373. $Log$
  1374. Revision 1.43 2001-11-02 22:58:02 peter
  1375. * procsym definition rewrite
  1376. Revision 1.42 2001/10/28 17:22:25 peter
  1377. * allow assignment of overloaded procedures to procvars when we know
  1378. which procedure to take
  1379. Revision 1.41 2001/10/20 19:28:37 peter
  1380. * interface 2 guid support
  1381. * guid constants support
  1382. Revision 1.40 2001/10/20 17:21:54 peter
  1383. * fixed size of constset when change from small to normalset
  1384. Revision 1.39 2001/09/30 16:12:46 jonas
  1385. - removed unnecessary i386 pass_2 of as- and isnode and added dummy generic ones
  1386. Revision 1.38 2001/09/29 21:32:46 jonas
  1387. * almost all second pass typeconvnode helpers are now processor independent
  1388. * fixed converting boolean to int64/qword
  1389. * fixed register allocation bugs which could cause internalerror 10
  1390. * isnode and asnode are completely processor indepent now as well
  1391. * fpc_do_as now returns its class argument (necessary to be able to use it
  1392. properly with compilerproc)
  1393. Revision 1.37 2001/09/03 13:27:42 jonas
  1394. * compilerproc implementation of set addition/substraction/...
  1395. * changed the declaration of some set helpers somewhat to accomodate the
  1396. above change
  1397. * i386 still uses the old code for comparisons of sets, because its
  1398. helpers return the results in the flags
  1399. * dummy tc_normal_2_small_set type conversion because I need the original
  1400. resulttype of the set add nodes
  1401. NOTE: you have to start a cycle with 1.0.5!
  1402. Revision 1.36 2001/09/02 21:12:06 peter
  1403. * move class of definitions into type section for delphi
  1404. Revision 1.35 2001/08/29 19:49:03 jonas
  1405. * some fixes in compilerprocs for chararray to string conversions
  1406. * conversion from string to chararray is now also done via compilerprocs
  1407. Revision 1.34 2001/08/29 12:18:07 jonas
  1408. + new createinternres() constructor for tcallnode to support setting a
  1409. custom resulttype
  1410. * compilerproc typeconversions now set the resulttype from the type
  1411. conversion for the generated call node, because the resulttype of
  1412. of the compilerproc helper isn't always exact (e.g. the ones that
  1413. return shortstrings, actually return a shortstring[x], where x is
  1414. specified by the typeconversion node)
  1415. * ti386callnode.pass_2 now always uses resulttype instead of
  1416. procsym.definition.rettype (so the custom resulttype, if any, is
  1417. always used). Note that this "rettype" stuff is only for use with
  1418. compilerprocs.
  1419. Revision 1.33 2001/08/28 13:24:46 jonas
  1420. + compilerproc implementation of most string-related type conversions
  1421. - removed all code from the compiler which has been replaced by
  1422. compilerproc implementations (using (ifdef hascompilerproc) is not
  1423. necessary in the compiler)
  1424. Revision 1.32 2001/08/26 13:36:40 florian
  1425. * some cg reorganisation
  1426. * some PPC updates
  1427. Revision 1.31 2001/08/05 13:19:51 peter
  1428. * partly fix for proc of obj=nil
  1429. Revision 1.30 2001/07/30 20:59:27 peter
  1430. * m68k updates from v10 merged
  1431. Revision 1.29 2001/07/08 21:00:15 peter
  1432. * various widestring updates, it works now mostly without charset
  1433. mapping supported
  1434. Revision 1.28 2001/05/13 15:43:46 florian
  1435. * made resultype_char_to_char a little bit robuster
  1436. Revision 1.27 2001/05/08 21:06:30 florian
  1437. * some more support for widechars commited especially
  1438. regarding type casting and constants
  1439. Revision 1.26 2001/05/04 15:52:03 florian
  1440. * some Delphi incompatibilities fixed:
  1441. - out, dispose and new can be used as idenfiers now
  1442. - const p = apointerype(nil); is supported now
  1443. + support for const p = apointertype(pointer(1234)); added
  1444. Revision 1.25 2001/04/13 22:20:58 peter
  1445. * remove wrongly placed first_call_helper
  1446. Revision 1.24 2001/04/13 01:22:08 peter
  1447. * symtable change to classes
  1448. * range check generation and errors fixed, make cycle DEBUG=1 works
  1449. * memory leaks fixed
  1450. Revision 1.23 2001/04/04 22:42:39 peter
  1451. * move constant folding into det_resulttype
  1452. Revision 1.22 2001/04/02 21:20:30 peter
  1453. * resulttype rewrite
  1454. Revision 1.21 2001/03/08 17:44:47 jonas
  1455. * fixed web bug 1430
  1456. Revision 1.20 2001/02/21 11:49:50 jonas
  1457. * evaluate typecasts of const pointers to ordinals inline ('merged')
  1458. Revision 1.19 2001/02/20 18:37:10 peter
  1459. * removed unused code
  1460. Revision 1.18 2001/02/20 13:14:18 marco
  1461. * Fix from Peter for passing a procedure of method to a other method in a method
  1462. Revision 1.17 2001/02/08 13:09:03 jonas
  1463. * fixed web bug 1396: tpointerord is now a cardinal instead of a longint,
  1464. but added a hack in ncnv so that pointer(-1) still works
  1465. Revision 1.16 2000/12/31 11:14:10 jonas
  1466. + implemented/fixed docompare() mathods for all nodes (not tested)
  1467. + nopt.pas, nadd.pas, i386/n386opt.pas: optimized nodes for adding strings
  1468. and constant strings/chars together
  1469. * n386add.pas: don't copy temp strings (of size 256) to another temp string
  1470. when adding
  1471. Revision 1.15 2000/12/08 12:41:01 jonas
  1472. * fixed bug in sign extension patch
  1473. Revision 1.14 2000/12/07 17:19:42 jonas
  1474. * new constant handling: from now on, hex constants >$7fffffff are
  1475. parsed as unsigned constants (otherwise, $80000000 got sign extended
  1476. and became $ffffffff80000000), all constants in the longint range
  1477. become longints, all constants >$7fffffff and <=cardinal($ffffffff)
  1478. are cardinals and the rest are int64's.
  1479. * added lots of longint typecast to prevent range check errors in the
  1480. compiler and rtl
  1481. * type casts of symbolic ordinal constants are now preserved
  1482. * fixed bug where the original resulttype.def wasn't restored correctly
  1483. after doing a 64bit rangecheck
  1484. Revision 1.13 2000/11/29 00:30:32 florian
  1485. * unused units removed from uses clause
  1486. * some changes for widestrings
  1487. Revision 1.12 2000/11/20 16:06:04 jonas
  1488. + allow evaluation of 64bit constant expressions at compile time
  1489. * disable range checking for explicit typecasts of constant expressions
  1490. Revision 1.11 2000/11/12 23:24:11 florian
  1491. * interfaces are basically running
  1492. Revision 1.10 2000/11/04 14:25:20 florian
  1493. + merged Attila's changes for interfaces, not tested yet
  1494. Revision 1.9 2000/10/31 22:02:48 peter
  1495. * symtable splitted, no real code changes
  1496. Revision 1.8 2000/10/14 21:52:55 peter
  1497. * fixed memory leaks
  1498. Revision 1.7 2000/10/14 10:14:50 peter
  1499. * moehrendorf oct 2000 rewrite
  1500. Revision 1.6 2000/10/01 19:48:24 peter
  1501. * lot of compile updates for cg11
  1502. Revision 1.5 2000/09/28 19:49:52 florian
  1503. *** empty log message ***
  1504. Revision 1.4 2000/09/27 18:14:31 florian
  1505. * fixed a lot of syntax errors in the n*.pas stuff
  1506. Revision 1.3 2000/09/26 20:06:13 florian
  1507. * hmm, still a lot of work to get things compilable
  1508. Revision 1.2 2000/09/26 14:59:34 florian
  1509. * more conversion work done
  1510. Revision 1.1 2000/09/25 15:37:14 florian
  1511. * more fixes
  1512. }