ncnv.pas 67 KB

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