ncnv.pas 64 KB

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