ncnv.pas 63 KB

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