ncnv.pas 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581
  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,
  24. defutil,defcmp,
  25. nld
  26. ;
  27. type
  28. ttypeconvnode = class(tunarynode)
  29. totype : ttype;
  30. convtype : tconverttype;
  31. constructor create(node : tnode;const t : ttype);virtual;
  32. constructor create_explicit(node : tnode;const t : ttype);
  33. constructor create_internal(node : tnode;const t : ttype);
  34. constructor create_proc_to_procvar(node : tnode);
  35. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  36. procedure ppuwrite(ppufile:tcompilerppufile);override;
  37. procedure buildderefimpl;override;
  38. procedure derefimpl;override;
  39. function getcopy : tnode;override;
  40. function pass_1 : tnode;override;
  41. function det_resulttype:tnode;override;
  42. procedure mark_write;override;
  43. function docompare(p: tnode) : boolean; override;
  44. function assign_allowed:boolean;
  45. procedure second_call_helper(c : tconverttype);
  46. private
  47. function resulttype_int_to_int : tnode;
  48. function resulttype_cord_to_pointer : tnode;
  49. function resulttype_chararray_to_string : tnode;
  50. function resulttype_string_to_chararray : tnode;
  51. function resulttype_string_to_string : tnode;
  52. function resulttype_char_to_string : tnode;
  53. function resulttype_char_to_chararray : tnode;
  54. function resulttype_int_to_real : tnode;
  55. function resulttype_real_to_real : tnode;
  56. function resulttype_real_to_currency : tnode;
  57. function resulttype_cchar_to_pchar : tnode;
  58. function resulttype_cstring_to_pchar : tnode;
  59. function resulttype_char_to_char : tnode;
  60. function resulttype_arrayconstructor_to_set : tnode;
  61. function resulttype_pchar_to_string : tnode;
  62. function resulttype_interface_to_guid : tnode;
  63. function resulttype_dynarray_to_openarray : tnode;
  64. function resulttype_pwchar_to_string : tnode;
  65. function resulttype_variant_to_dynarray : tnode;
  66. function resulttype_dynarray_to_variant : tnode;
  67. function resulttype_call_helper(c : tconverttype) : tnode;
  68. function resulttype_variant_to_enum : tnode;
  69. function resulttype_enum_to_variant : tnode;
  70. function resulttype_proc_to_procvar : tnode;
  71. protected
  72. function first_int_to_int : tnode;virtual;
  73. function first_cstring_to_pchar : tnode;virtual;
  74. function first_string_to_chararray : tnode;virtual;
  75. function first_char_to_string : tnode;virtual;
  76. function first_nothing : tnode;virtual;
  77. function first_array_to_pointer : tnode;virtual;
  78. function first_int_to_real : tnode;virtual;
  79. function first_real_to_real : tnode;virtual;
  80. function first_pointer_to_array : tnode;virtual;
  81. function first_cchar_to_pchar : tnode;virtual;
  82. function first_bool_to_int : tnode;virtual;
  83. function first_int_to_bool : tnode;virtual;
  84. function first_bool_to_bool : tnode;virtual;
  85. function first_proc_to_procvar : tnode;virtual;
  86. function first_load_smallset : tnode;virtual;
  87. function first_cord_to_pointer : tnode;virtual;
  88. function first_ansistring_to_pchar : tnode;virtual;
  89. function first_arrayconstructor_to_set : tnode;virtual;
  90. function first_class_to_intf : tnode;virtual;
  91. function first_char_to_char : tnode;virtual;
  92. function first_call_helper(c : tconverttype) : tnode;
  93. { these wrapper are necessary, because the first_* stuff is called }
  94. { through a table. Without the wrappers override wouldn't have }
  95. { any effect }
  96. function _first_int_to_int : tnode;
  97. function _first_cstring_to_pchar : tnode;
  98. function _first_string_to_chararray : tnode;
  99. function _first_char_to_string : tnode;
  100. function _first_nothing : tnode;
  101. function _first_array_to_pointer : tnode;
  102. function _first_int_to_real : tnode;
  103. function _first_real_to_real: tnode;
  104. function _first_pointer_to_array : tnode;
  105. function _first_cchar_to_pchar : tnode;
  106. function _first_bool_to_int : tnode;
  107. function _first_int_to_bool : tnode;
  108. function _first_bool_to_bool : tnode;
  109. function _first_proc_to_procvar : tnode;
  110. function _first_load_smallset : tnode;
  111. function _first_cord_to_pointer : tnode;
  112. function _first_ansistring_to_pchar : tnode;
  113. function _first_arrayconstructor_to_set : tnode;
  114. function _first_class_to_intf : tnode;
  115. function _first_char_to_char : tnode;
  116. procedure _second_int_to_int;virtual;
  117. procedure _second_string_to_string;virtual;
  118. procedure _second_cstring_to_pchar;virtual;
  119. procedure _second_string_to_chararray;virtual;
  120. procedure _second_array_to_pointer;virtual;
  121. procedure _second_pointer_to_array;virtual;
  122. procedure _second_chararray_to_string;virtual;
  123. procedure _second_char_to_string;virtual;
  124. procedure _second_int_to_real;virtual;
  125. procedure _second_real_to_real;virtual;
  126. procedure _second_cord_to_pointer;virtual;
  127. procedure _second_proc_to_procvar;virtual;
  128. procedure _second_bool_to_int;virtual;
  129. procedure _second_int_to_bool;virtual;
  130. procedure _second_bool_to_bool;virtual;
  131. procedure _second_load_smallset;virtual;
  132. procedure _second_ansistring_to_pchar;virtual;
  133. procedure _second_class_to_intf;virtual;
  134. procedure _second_char_to_char;virtual;
  135. procedure _second_nothing; virtual;
  136. procedure second_int_to_int;virtual;abstract;
  137. procedure second_string_to_string;virtual;abstract;
  138. procedure second_cstring_to_pchar;virtual;abstract;
  139. procedure second_string_to_chararray;virtual;abstract;
  140. procedure second_array_to_pointer;virtual;abstract;
  141. procedure second_pointer_to_array;virtual;abstract;
  142. procedure second_chararray_to_string;virtual;abstract;
  143. procedure second_char_to_string;virtual;abstract;
  144. procedure second_int_to_real;virtual;abstract;
  145. procedure second_real_to_real;virtual;abstract;
  146. procedure second_cord_to_pointer;virtual;abstract;
  147. procedure second_proc_to_procvar;virtual;abstract;
  148. procedure second_bool_to_int;virtual;abstract;
  149. procedure second_int_to_bool;virtual;abstract;
  150. procedure second_bool_to_bool;virtual;abstract;
  151. procedure second_load_smallset;virtual;abstract;
  152. procedure second_ansistring_to_pchar;virtual;abstract;
  153. procedure second_class_to_intf;virtual;abstract;
  154. procedure second_char_to_char;virtual;abstract;
  155. procedure second_nothing; virtual;abstract;
  156. end;
  157. ttypeconvnodeclass = class of ttypeconvnode;
  158. tasnode = class(tbinarynode)
  159. constructor create(l,r : tnode);virtual;
  160. function pass_1 : tnode;override;
  161. function det_resulttype:tnode;override;
  162. function getcopy: tnode;override;
  163. destructor destroy; override;
  164. protected
  165. call: tnode;
  166. end;
  167. tasnodeclass = class of tasnode;
  168. tisnode = class(tbinarynode)
  169. constructor create(l,r : tnode);virtual;
  170. function pass_1 : tnode;override;
  171. function det_resulttype:tnode;override;
  172. procedure pass_2;override;
  173. end;
  174. tisnodeclass = class of tisnode;
  175. var
  176. ctypeconvnode : ttypeconvnodeclass;
  177. casnode : tasnodeclass;
  178. cisnode : tisnodeclass;
  179. procedure inserttypeconv(var p:tnode;const t:ttype);
  180. procedure inserttypeconv_internal(var p:tnode;const t:ttype);
  181. procedure arrayconstructor_to_set(var p : tnode);
  182. implementation
  183. uses
  184. cclasses,globtype,systems,
  185. cutils,verbose,globals,widestr,
  186. symconst,symdef,symsym,symbase,symtable,
  187. ncon,ncal,nset,nadd,ninl,nmem,nmat,nutils,
  188. cgbase,procinfo,
  189. htypechk,pass_1,cpuinfo;
  190. {*****************************************************************************
  191. Helpers
  192. *****************************************************************************}
  193. procedure inserttypeconv(var p:tnode;const t:ttype);
  194. begin
  195. if not assigned(p.resulttype.def) then
  196. begin
  197. resulttypepass(p);
  198. if codegenerror then
  199. exit;
  200. end;
  201. { don't insert obsolete type conversions }
  202. if equal_defs(p.resulttype.def,t.def) and
  203. not ((p.resulttype.def.deftype=setdef) and
  204. (tsetdef(p.resulttype.def).settype <>
  205. tsetdef(t.def).settype)) then
  206. begin
  207. p.resulttype:=t;
  208. end
  209. else
  210. begin
  211. p:=ctypeconvnode.create(p,t);
  212. resulttypepass(p);
  213. end;
  214. end;
  215. procedure inserttypeconv_internal(var p:tnode;const t:ttype);
  216. begin
  217. if not assigned(p.resulttype.def) then
  218. begin
  219. resulttypepass(p);
  220. if codegenerror then
  221. exit;
  222. end;
  223. { don't insert obsolete type conversions }
  224. if equal_defs(p.resulttype.def,t.def) and
  225. not ((p.resulttype.def.deftype=setdef) and
  226. (tsetdef(p.resulttype.def).settype <>
  227. tsetdef(t.def).settype)) then
  228. begin
  229. p.resulttype:=t;
  230. end
  231. else
  232. begin
  233. p:=ctypeconvnode.create_internal(p,t);
  234. resulttypepass(p);
  235. end;
  236. end;
  237. {*****************************************************************************
  238. Array constructor to Set Conversion
  239. *****************************************************************************}
  240. procedure arrayconstructor_to_set(var p : tnode);
  241. var
  242. constp : tsetconstnode;
  243. buildp,
  244. p2,p3,p4 : tnode;
  245. htype : ttype;
  246. constset : Pconstset;
  247. constsetlo,
  248. constsethi : TConstExprInt;
  249. procedure update_constsethi(t:ttype);
  250. begin
  251. if ((t.def.deftype=orddef) and
  252. (torddef(t.def).high>=constsethi)) then
  253. begin
  254. if torddef(t.def).typ=uwidechar then
  255. begin
  256. constsethi:=255;
  257. if htype.def=nil then
  258. htype:=t;
  259. end
  260. else
  261. begin
  262. constsethi:=torddef(t.def).high;
  263. if htype.def=nil then
  264. begin
  265. if (constsethi>255) or
  266. (torddef(t.def).low<0) then
  267. htype:=u8inttype
  268. else
  269. htype:=t;
  270. end;
  271. if constsethi>255 then
  272. constsethi:=255;
  273. end;
  274. end
  275. else if ((t.def.deftype=enumdef) and
  276. (tenumdef(t.def).max>=constsethi)) then
  277. begin
  278. if htype.def=nil then
  279. htype:=t;
  280. constsethi:=tenumdef(t.def).max;
  281. end;
  282. end;
  283. procedure do_set(pos : longint);
  284. begin
  285. if (pos and not $ff)<>0 then
  286. Message(parser_e_illegal_set_expr);
  287. if pos>constsethi then
  288. constsethi:=pos;
  289. if pos<constsetlo then
  290. constsetlo:=pos;
  291. if pos in constset^ then
  292. Message(parser_e_illegal_set_expr);
  293. include(constset^,pos);
  294. end;
  295. var
  296. l : Longint;
  297. lr,hr : TConstExprInt;
  298. hp : tarrayconstructornode;
  299. begin
  300. if p.nodetype<>arrayconstructorn then
  301. internalerror(200205105);
  302. new(constset);
  303. constset^:=[];
  304. htype.reset;
  305. constsetlo:=0;
  306. constsethi:=0;
  307. constp:=csetconstnode.create(nil,htype);
  308. constp.value_set:=constset;
  309. buildp:=constp;
  310. hp:=tarrayconstructornode(p);
  311. if assigned(hp.left) then
  312. begin
  313. while assigned(hp) do
  314. begin
  315. p4:=nil; { will contain the tree to create the set }
  316. {split a range into p2 and p3 }
  317. if hp.left.nodetype=arrayconstructorrangen then
  318. begin
  319. p2:=tarrayconstructorrangenode(hp.left).left;
  320. p3:=tarrayconstructorrangenode(hp.left).right;
  321. tarrayconstructorrangenode(hp.left).left:=nil;
  322. tarrayconstructorrangenode(hp.left).right:=nil;
  323. end
  324. else
  325. begin
  326. p2:=hp.left;
  327. hp.left:=nil;
  328. p3:=nil;
  329. end;
  330. resulttypepass(p2);
  331. if assigned(p3) then
  332. resulttypepass(p3);
  333. if codegenerror then
  334. break;
  335. case p2.resulttype.def.deftype of
  336. enumdef,
  337. orddef:
  338. begin
  339. getrange(p2.resulttype.def,lr,hr);
  340. if assigned(p3) then
  341. begin
  342. { this isn't good, you'll get problems with
  343. type t010 = 0..10;
  344. ts = set of t010;
  345. var s : ts;b : t010
  346. begin s:=[1,2,b]; end.
  347. if is_integer(p3^.resulttype.def) then
  348. begin
  349. inserttypeconv(p3,u8bitdef);
  350. end;
  351. }
  352. if assigned(htype.def) and not(equal_defs(htype.def,p3.resulttype.def)) then
  353. begin
  354. aktfilepos:=p3.fileinfo;
  355. CGMessage(type_e_typeconflict_in_set);
  356. end
  357. else
  358. begin
  359. if (p2.nodetype=ordconstn) and (p3.nodetype=ordconstn) then
  360. begin
  361. if not(is_integer(p3.resulttype.def)) then
  362. htype:=p3.resulttype
  363. else
  364. begin
  365. inserttypeconv(p3,u8inttype);
  366. inserttypeconv(p2,u8inttype);
  367. end;
  368. for l:=tordconstnode(p2).value to tordconstnode(p3).value do
  369. do_set(l);
  370. p2.free;
  371. p3.free;
  372. end
  373. else
  374. begin
  375. update_constsethi(p2.resulttype);
  376. inserttypeconv(p2,htype);
  377. update_constsethi(p3.resulttype);
  378. inserttypeconv(p3,htype);
  379. if assigned(htype.def) then
  380. inserttypeconv(p3,htype)
  381. else
  382. inserttypeconv(p3,u8inttype);
  383. p4:=csetelementnode.create(p2,p3);
  384. end;
  385. end;
  386. end
  387. else
  388. begin
  389. { Single value }
  390. if p2.nodetype=ordconstn then
  391. begin
  392. if not(is_integer(p2.resulttype.def)) then
  393. update_constsethi(p2.resulttype)
  394. else
  395. inserttypeconv(p2,u8inttype);
  396. do_set(tordconstnode(p2).value);
  397. p2.free;
  398. end
  399. else
  400. begin
  401. update_constsethi(p2.resulttype);
  402. if assigned(htype.def) then
  403. inserttypeconv(p2,htype)
  404. else
  405. inserttypeconv(p2,u8inttype);
  406. p4:=csetelementnode.create(p2,nil);
  407. end;
  408. end;
  409. end;
  410. stringdef :
  411. begin
  412. { if we've already set elements which are constants }
  413. { throw an error }
  414. if ((htype.def=nil) and assigned(buildp)) or
  415. not(is_char(htype.def)) then
  416. CGMessage(type_e_typeconflict_in_set)
  417. else
  418. for l:=1 to length(pstring(tstringconstnode(p2).value_str)^) do
  419. do_set(ord(pstring(tstringconstnode(p2).value_str)^[l]));
  420. if htype.def=nil then
  421. htype:=cchartype;
  422. p2.free;
  423. end;
  424. else
  425. CGMessage(type_e_ordinal_expr_expected);
  426. end;
  427. { insert the set creation tree }
  428. if assigned(p4) then
  429. buildp:=caddnode.create(addn,buildp,p4);
  430. { load next and dispose current node }
  431. p2:=hp;
  432. hp:=tarrayconstructornode(tarrayconstructornode(p2).right);
  433. tarrayconstructornode(p2).right:=nil;
  434. p2.free;
  435. end;
  436. if (htype.def=nil) then
  437. htype:=u8inttype;
  438. end
  439. else
  440. begin
  441. { empty set [], only remove node }
  442. p.free;
  443. end;
  444. { set the initial set type }
  445. constp.resulttype.setdef(tsetdef.create(htype,constsethi));
  446. { determine the resulttype for the tree }
  447. resulttypepass(buildp);
  448. { set the new tree }
  449. p:=buildp;
  450. end;
  451. {*****************************************************************************
  452. TTYPECONVNODE
  453. *****************************************************************************}
  454. constructor ttypeconvnode.create(node : tnode;const t:ttype);
  455. begin
  456. inherited create(typeconvn,node);
  457. convtype:=tc_none;
  458. totype:=t;
  459. if t.def=nil then
  460. internalerror(200103281);
  461. set_file_line(node);
  462. end;
  463. constructor ttypeconvnode.create_explicit(node : tnode;const t:ttype);
  464. begin
  465. self.create(node,t);
  466. include(flags,nf_explicit);
  467. end;
  468. constructor ttypeconvnode.create_internal(node : tnode;const t:ttype);
  469. begin
  470. self.create(node,t);
  471. { handle like explicit conversions }
  472. include(flags,nf_explicit);
  473. include(flags,nf_internal);
  474. end;
  475. constructor ttypeconvnode.create_proc_to_procvar(node : tnode);
  476. begin
  477. self.create(node,voidtype);
  478. convtype:=tc_proc_2_procvar;
  479. end;
  480. constructor ttypeconvnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  481. begin
  482. inherited ppuload(t,ppufile);
  483. ppufile.gettype(totype);
  484. convtype:=tconverttype(ppufile.getbyte);
  485. end;
  486. procedure ttypeconvnode.ppuwrite(ppufile:tcompilerppufile);
  487. begin
  488. inherited ppuwrite(ppufile);
  489. ppufile.puttype(totype);
  490. ppufile.putbyte(byte(convtype));
  491. end;
  492. procedure ttypeconvnode.buildderefimpl;
  493. begin
  494. inherited buildderefimpl;
  495. totype.buildderef;
  496. end;
  497. procedure ttypeconvnode.derefimpl;
  498. begin
  499. inherited derefimpl;
  500. totype.resolve;
  501. end;
  502. function ttypeconvnode.getcopy : tnode;
  503. var
  504. n : ttypeconvnode;
  505. begin
  506. n:=ttypeconvnode(inherited getcopy);
  507. n.convtype:=convtype;
  508. n.totype:=totype;
  509. getcopy:=n;
  510. end;
  511. function ttypeconvnode.resulttype_cord_to_pointer : tnode;
  512. var
  513. t : tnode;
  514. begin
  515. result:=nil;
  516. if left.nodetype=ordconstn then
  517. begin
  518. { check if we have a valid pointer constant (JM) }
  519. if (sizeof(pointer) > sizeof(TConstPtrUInt)) then
  520. if (sizeof(TConstPtrUInt) = 4) then
  521. begin
  522. if (tordconstnode(left).value < low(longint)) or
  523. (tordconstnode(left).value > high(cardinal)) then
  524. CGMessage(parser_e_range_check_error);
  525. end
  526. else if (sizeof(TConstPtrUInt) = 8) then
  527. begin
  528. if (tordconstnode(left).value < low(int64)) or
  529. (tordconstnode(left).value > high(qword)) then
  530. CGMessage(parser_e_range_check_error);
  531. end
  532. else
  533. internalerror(2001020801);
  534. t:=cpointerconstnode.create(TConstPtrUInt(tordconstnode(left).value),resulttype);
  535. result:=t;
  536. end
  537. else
  538. internalerror(200104023);
  539. end;
  540. function ttypeconvnode.resulttype_chararray_to_string : tnode;
  541. begin
  542. result := ccallnode.createinternres(
  543. 'fpc_chararray_to_'+tstringdef(resulttype.def).stringtypname,
  544. ccallparanode.create(left,nil),resulttype);
  545. left := nil;
  546. end;
  547. function ttypeconvnode.resulttype_string_to_chararray : tnode;
  548. var
  549. arrsize : aint;
  550. begin
  551. with tarraydef(resulttype.def) do
  552. begin
  553. if highrange<lowrange then
  554. internalerror(75432653);
  555. arrsize := highrange-lowrange+1;
  556. end;
  557. if (left.nodetype = stringconstn) and
  558. { left.length+1 since there's always a terminating #0 character (JM) }
  559. (tstringconstnode(left).len+1 >= arrsize) and
  560. (tstringdef(left.resulttype.def).string_typ=st_shortstring) then
  561. begin
  562. { handled separately }
  563. result := nil;
  564. exit;
  565. end;
  566. result := ccallnode.createinternres(
  567. 'fpc_'+tstringdef(left.resulttype.def).stringtypname+
  568. '_to_chararray',ccallparanode.create(left,ccallparanode.create(
  569. cordconstnode.create(arrsize,s32inttype,true),nil)),resulttype);
  570. left := nil;
  571. end;
  572. function ttypeconvnode.resulttype_string_to_string : tnode;
  573. var
  574. procname: string[31];
  575. stringpara : tcallparanode;
  576. pw : pcompilerwidestring;
  577. pc : pchar;
  578. begin
  579. result:=nil;
  580. if left.nodetype=stringconstn then
  581. begin
  582. { convert ascii 2 unicode }
  583. {$ifdef ansistring_bits}
  584. if (tstringdef(resulttype.def).string_typ=st_widestring) and
  585. (tstringconstnode(left).st_type in [st_ansistring16,st_ansistring32,
  586. st_ansistring64,st_shortstring,st_longstring]) then
  587. {$else}
  588. if (tstringdef(resulttype.def).string_typ=st_widestring) and
  589. (tstringconstnode(left).st_type in [st_ansistring,st_shortstring,st_longstring]) then
  590. {$endif}
  591. begin
  592. initwidestring(pw);
  593. ascii2unicode(tstringconstnode(left).value_str,tstringconstnode(left).len,pw);
  594. ansistringdispose(tstringconstnode(left).value_str,tstringconstnode(left).len);
  595. pcompilerwidestring(tstringconstnode(left).value_str):=pw;
  596. end
  597. else
  598. { convert unicode 2 ascii }
  599. {$ifdef ansistring_bits}
  600. if (tstringconstnode(left).st_type=st_widestring) and
  601. (tstringdef(resulttype.def).string_typ in [st_ansistring16,st_ansistring32,
  602. st_ansistring64,st_shortstring,st_longstring]) then
  603. {$else}
  604. if (tstringconstnode(left).st_type=st_widestring) and
  605. (tstringdef(resulttype.def).string_typ in [st_ansistring,st_shortstring,st_longstring]) then
  606. {$endif}
  607. begin
  608. pw:=pcompilerwidestring(tstringconstnode(left).value_str);
  609. getmem(pc,getlengthwidestring(pw)+1);
  610. unicode2ascii(pw,pc);
  611. donewidestring(pw);
  612. tstringconstnode(left).value_str:=pc;
  613. end;
  614. tstringconstnode(left).st_type:=tstringdef(resulttype.def).string_typ;
  615. tstringconstnode(left).resulttype:=resulttype;
  616. result:=left;
  617. left:=nil;
  618. end
  619. else
  620. begin
  621. { get the correct procedure name }
  622. procname := 'fpc_'+tstringdef(left.resulttype.def).stringtypname+
  623. '_to_'+tstringdef(resulttype.def).stringtypname;
  624. { create parameter (and remove left node from typeconvnode }
  625. { since it's reused as parameter) }
  626. stringpara := ccallparanode.create(left,nil);
  627. left := nil;
  628. { when converting to shortstrings, we have to pass high(destination) too }
  629. if (tstringdef(resulttype.def).string_typ = st_shortstring) then
  630. stringpara.right := ccallparanode.create(cinlinenode.create(
  631. in_high_x,false,self.getcopy),nil);
  632. { and create the callnode }
  633. result := ccallnode.createinternres(procname,stringpara,resulttype);
  634. end;
  635. end;
  636. function ttypeconvnode.resulttype_char_to_string : tnode;
  637. var
  638. procname: string[31];
  639. para : tcallparanode;
  640. hp : tstringconstnode;
  641. ws : pcompilerwidestring;
  642. begin
  643. result:=nil;
  644. if left.nodetype=ordconstn then
  645. begin
  646. if tstringdef(resulttype.def).string_typ=st_widestring then
  647. begin
  648. initwidestring(ws);
  649. concatwidestringchar(ws,tcompilerwidechar(chr(tordconstnode(left).value)));
  650. hp:=cstringconstnode.createwstr(ws);
  651. donewidestring(ws);
  652. end
  653. else
  654. hp:=cstringconstnode.createstr(chr(tordconstnode(left).value),tstringdef(resulttype.def).string_typ);
  655. result:=hp;
  656. end
  657. else
  658. { shortstrings are handled 'inline' }
  659. if tstringdef(resulttype.def).string_typ <> st_shortstring then
  660. begin
  661. { create the parameter }
  662. para := ccallparanode.create(left,nil);
  663. left := nil;
  664. { and the procname }
  665. procname := 'fpc_char_to_' +tstringdef(resulttype.def).stringtypname;
  666. { and finally the call }
  667. result := ccallnode.createinternres(procname,para,resulttype);
  668. end
  669. else
  670. begin
  671. { create word(byte(char) shl 8 or 1) for litte endian machines }
  672. { and word(byte(char) or 256) for big endian machines }
  673. left := ctypeconvnode.create_internal(left,u8inttype);
  674. if (target_info.endian = endian_little) then
  675. left := caddnode.create(orn,
  676. cshlshrnode.create(shln,left,cordconstnode.create(8,s32inttype,false)),
  677. cordconstnode.create(1,s32inttype,false))
  678. else
  679. left := caddnode.create(orn,left,
  680. cordconstnode.create(1 shl 8,s32inttype,false));
  681. left := ctypeconvnode.create_internal(left,u16inttype);
  682. resulttypepass(left);
  683. end;
  684. end;
  685. function ttypeconvnode.resulttype_char_to_chararray : tnode;
  686. begin
  687. if resulttype.def.size <> 1 then
  688. begin
  689. { convert first to string, then to chararray }
  690. inserttypeconv(left,cshortstringtype);
  691. inserttypeconv(left,resulttype);
  692. result:=left;
  693. left := nil;
  694. exit;
  695. end;
  696. result := nil;
  697. end;
  698. function ttypeconvnode.resulttype_char_to_char : tnode;
  699. var
  700. hp : tordconstnode;
  701. begin
  702. result:=nil;
  703. if left.nodetype=ordconstn then
  704. begin
  705. if (torddef(resulttype.def).typ=uchar) and
  706. (torddef(left.resulttype.def).typ=uwidechar) then
  707. begin
  708. hp:=cordconstnode.create(
  709. ord(unicode2asciichar(tcompilerwidechar(tordconstnode(left).value))),
  710. cchartype,true);
  711. result:=hp;
  712. end
  713. else if (torddef(resulttype.def).typ=uwidechar) and
  714. (torddef(left.resulttype.def).typ=uchar) then
  715. begin
  716. hp:=cordconstnode.create(
  717. asciichar2unicode(chr(tordconstnode(left).value)),
  718. cwidechartype,true);
  719. result:=hp;
  720. end
  721. else
  722. internalerror(200105131);
  723. exit;
  724. end;
  725. end;
  726. function ttypeconvnode.resulttype_int_to_int : tnode;
  727. var
  728. v : TConstExprInt;
  729. begin
  730. result:=nil;
  731. if left.nodetype=ordconstn then
  732. begin
  733. v:=tordconstnode(left).value;
  734. if is_currency(resulttype.def) then
  735. v:=v*10000;
  736. if (resulttype.def.deftype=pointerdef) then
  737. result:=cpointerconstnode.create(TConstPtrUInt(v),resulttype)
  738. else
  739. begin
  740. if is_currency(left.resulttype.def) then
  741. v:=v div 10000;
  742. result:=cordconstnode.create(v,resulttype,false);
  743. end;
  744. end
  745. else if left.nodetype=pointerconstn then
  746. begin
  747. v:=tpointerconstnode(left).value;
  748. if (resulttype.def.deftype=pointerdef) then
  749. result:=cpointerconstnode.create(v,resulttype)
  750. else
  751. begin
  752. if is_currency(resulttype.def) then
  753. v:=v*10000;
  754. result:=cordconstnode.create(v,resulttype,false);
  755. end;
  756. end
  757. else
  758. begin
  759. { multiply by 10000 for currency. We need to use getcopy to pass
  760. the argument because the current node is always disposed. Only
  761. inserting the multiply in the left node is not possible because
  762. it'll get in an infinite loop to convert int->currency }
  763. if is_currency(resulttype.def) then
  764. begin
  765. result:=caddnode.create(muln,getcopy,cordconstnode.create(10000,resulttype,false));
  766. include(result.flags,nf_is_currency);
  767. end
  768. else if is_currency(left.resulttype.def) then
  769. begin
  770. result:=cmoddivnode.create(divn,getcopy,cordconstnode.create(10000,resulttype,false));
  771. include(result.flags,nf_is_currency);
  772. end;
  773. end;
  774. end;
  775. function ttypeconvnode.resulttype_int_to_real : tnode;
  776. var
  777. rv : bestreal;
  778. begin
  779. result:=nil;
  780. if left.nodetype=ordconstn then
  781. begin
  782. rv:=tordconstnode(left).value;
  783. if is_currency(resulttype.def) then
  784. rv:=rv*10000.0
  785. else if is_currency(left.resulttype.def) then
  786. rv:=rv/10000.0;
  787. result:=crealconstnode.create(rv,resulttype);
  788. end
  789. else
  790. begin
  791. { multiply by 10000 for currency. We need to use getcopy to pass
  792. the argument because the current node is always disposed. Only
  793. inserting the multiply in the left node is not possible because
  794. it'll get in an infinite loop to convert int->currency }
  795. if is_currency(resulttype.def) then
  796. begin
  797. result:=caddnode.create(muln,getcopy,crealconstnode.create(10000.0,resulttype));
  798. include(result.flags,nf_is_currency);
  799. end
  800. else if is_currency(left.resulttype.def) then
  801. begin
  802. result:=caddnode.create(slashn,getcopy,crealconstnode.create(10000.0,resulttype));
  803. include(result.flags,nf_is_currency);
  804. end;
  805. end;
  806. end;
  807. function ttypeconvnode.resulttype_real_to_currency : tnode;
  808. begin
  809. if not is_currency(resulttype.def) then
  810. internalerror(200304221);
  811. result:=nil;
  812. left:=caddnode.create(muln,left,crealconstnode.create(10000.0,left.resulttype));
  813. include(left.flags,nf_is_currency);
  814. resulttypepass(left);
  815. { Convert constants directly, else call Round() }
  816. if left.nodetype=realconstn then
  817. result:=cordconstnode.create(round(trealconstnode(left).value_real),resulttype,false)
  818. else
  819. result:=ccallnode.createinternres('fpc_round_real',
  820. ccallparanode.create(left,nil),resulttype);
  821. left:=nil;
  822. end;
  823. function ttypeconvnode.resulttype_real_to_real : tnode;
  824. begin
  825. result:=nil;
  826. if is_currency(left.resulttype.def) and not(is_currency(resulttype.def)) then
  827. begin
  828. left:=caddnode.create(slashn,left,crealconstnode.create(10000.0,left.resulttype));
  829. include(left.flags,nf_is_currency);
  830. resulttypepass(left);
  831. end
  832. else
  833. if is_currency(resulttype.def) and not(is_currency(left.resulttype.def)) then
  834. begin
  835. left:=caddnode.create(muln,left,crealconstnode.create(10000.0,left.resulttype));
  836. include(left.flags,nf_is_currency);
  837. resulttypepass(left);
  838. end;
  839. if left.nodetype=realconstn then
  840. result:=crealconstnode.create(trealconstnode(left).value_real,resulttype);
  841. end;
  842. function ttypeconvnode.resulttype_cchar_to_pchar : tnode;
  843. begin
  844. result:=nil;
  845. if is_pwidechar(resulttype.def) then
  846. inserttypeconv(left,cwidestringtype)
  847. else
  848. inserttypeconv(left,cshortstringtype);
  849. { evaluate again, reset resulttype so the convert_typ
  850. will be calculated again and cstring_to_pchar will
  851. be used for futher conversion }
  852. convtype:=tc_none;
  853. result:=det_resulttype;
  854. end;
  855. function ttypeconvnode.resulttype_cstring_to_pchar : tnode;
  856. begin
  857. result:=nil;
  858. if is_pwidechar(resulttype.def) then
  859. inserttypeconv(left,cwidestringtype);
  860. end;
  861. function ttypeconvnode.resulttype_arrayconstructor_to_set : tnode;
  862. var
  863. hp : tnode;
  864. begin
  865. result:=nil;
  866. if left.nodetype<>arrayconstructorn then
  867. internalerror(5546);
  868. { remove typeconv node }
  869. hp:=left;
  870. left:=nil;
  871. { create a set constructor tree }
  872. arrayconstructor_to_set(hp);
  873. result:=hp;
  874. end;
  875. function ttypeconvnode.resulttype_pchar_to_string : tnode;
  876. begin
  877. result := ccallnode.createinternres(
  878. 'fpc_pchar_to_'+tstringdef(resulttype.def).stringtypname,
  879. ccallparanode.create(left,nil),resulttype);
  880. left := nil;
  881. end;
  882. function ttypeconvnode.resulttype_interface_to_guid : tnode;
  883. begin
  884. if assigned(tobjectdef(left.resulttype.def).iidguid) then
  885. result:=cguidconstnode.create(tobjectdef(left.resulttype.def).iidguid^);
  886. end;
  887. function ttypeconvnode.resulttype_dynarray_to_openarray : tnode;
  888. begin
  889. { a dynamic array is a pointer to an array, so to convert it to }
  890. { an open array, we have to dereference it (JM) }
  891. result := ctypeconvnode.create_internal(left,voidpointertype);
  892. resulttypepass(result);
  893. { left is reused }
  894. left := nil;
  895. result := cderefnode.create(result);
  896. include(result.flags,nf_no_checkpointer);
  897. result.resulttype := resulttype;
  898. end;
  899. function ttypeconvnode.resulttype_pwchar_to_string : tnode;
  900. begin
  901. result := ccallnode.createinternres(
  902. 'fpc_pwidechar_to_'+tstringdef(resulttype.def).stringtypname,
  903. ccallparanode.create(left,nil),resulttype);
  904. left := nil;
  905. end;
  906. function ttypeconvnode.resulttype_variant_to_dynarray : tnode;
  907. begin
  908. result := ccallnode.createinternres(
  909. 'fpc_variant_to_dynarray',
  910. ccallparanode.create(caddrnode.create_internal(crttinode.create(tstoreddef(resulttype.def),initrtti)),
  911. ccallparanode.create(left,nil)
  912. ),resulttype);
  913. resulttypepass(result);
  914. left:=nil;
  915. end;
  916. function ttypeconvnode.resulttype_dynarray_to_variant : tnode;
  917. begin
  918. result := ccallnode.createinternres(
  919. 'fpc_dynarray_to_variant',
  920. ccallparanode.create(caddrnode.create_internal(crttinode.create(tstoreddef(resulttype.def),initrtti)),
  921. ccallparanode.create(ctypeconvnode.create_explicit(left,voidpointertype),nil)
  922. ),resulttype);
  923. resulttypepass(result);
  924. left:=nil;
  925. end;
  926. function ttypeconvnode.resulttype_variant_to_enum : tnode;
  927. begin
  928. result := ctypeconvnode.create_internal(left,sinttype);
  929. result := ctypeconvnode.create_internal(result,resulttype);
  930. resulttypepass(result);
  931. { left is reused }
  932. left := nil;
  933. end;
  934. function ttypeconvnode.resulttype_enum_to_variant : tnode;
  935. begin
  936. result := ctypeconvnode.create_internal(left,sinttype);
  937. result := ctypeconvnode.create_internal(result,cvarianttype);
  938. resulttypepass(result);
  939. { left is reused }
  940. left := nil;
  941. end;
  942. procedure copyparasym(p:TNamedIndexItem;arg:pointer);
  943. var
  944. newparast : tsymtable absolute arg;
  945. vs : tparavarsym;
  946. begin
  947. if tsym(p).typ<>paravarsym then
  948. exit;
  949. with tparavarsym(p) do
  950. begin
  951. vs:=tparavarsym.create(realname,paranr,varspez,vartype);
  952. vs.varoptions:=varoptions;
  953. vs.defaultconstsym:=defaultconstsym;
  954. newparast.insert(vs);
  955. end;
  956. end;
  957. function ttypeconvnode.resulttype_proc_to_procvar : tnode;
  958. var
  959. pd : tabstractprocdef;
  960. begin
  961. result:=nil;
  962. pd:=tabstractprocdef(left.resulttype.def);
  963. { create procvardef }
  964. resulttype.setdef(tprocvardef.create(pd.parast.symtablelevel));
  965. tprocvardef(resulttype.def).proctypeoption:=pd.proctypeoption;
  966. tprocvardef(resulttype.def).proccalloption:=pd.proccalloption;
  967. tprocvardef(resulttype.def).procoptions:=pd.procoptions;
  968. tprocvardef(resulttype.def).rettype:=pd.rettype;
  969. { method ? then set the methodpointer flag }
  970. if (pd.owner.symtabletype=objectsymtable) then
  971. include(tprocvardef(resulttype.def).procoptions,po_methodpointer);
  972. { only need the address of the method? this is needed
  973. for @tobject.create. In this case there will be a loadn without
  974. a methodpointer. }
  975. if (left.nodetype=loadn) and
  976. not assigned(tloadnode(left).left) then
  977. include(tprocvardef(resulttype.def).procoptions,po_addressonly);
  978. { Add parameters use only references, we don't need to keep the
  979. parast. We use the parast from the original function to calculate
  980. our parameter data and reset it afterwards }
  981. pd.parast.foreach_static(@copyparasym,tprocvardef(resulttype.def).parast);
  982. tprocvardef(resulttype.def).calcparas;
  983. end;
  984. function ttypeconvnode.resulttype_call_helper(c : tconverttype) : tnode;
  985. {$ifdef fpc}
  986. const
  987. resulttypeconvert : array[tconverttype] of pointer = (
  988. {none} nil,
  989. {equal} nil,
  990. {not_possible} nil,
  991. { string_2_string } @ttypeconvnode.resulttype_string_to_string,
  992. { char_2_string } @ttypeconvnode.resulttype_char_to_string,
  993. { char_2_chararray } @ttypeconvnode.resulttype_char_to_chararray,
  994. { pchar_2_string } @ttypeconvnode.resulttype_pchar_to_string,
  995. { cchar_2_pchar } @ttypeconvnode.resulttype_cchar_to_pchar,
  996. { cstring_2_pchar } @ttypeconvnode.resulttype_cstring_to_pchar,
  997. { ansistring_2_pchar } nil,
  998. { string_2_chararray } @ttypeconvnode.resulttype_string_to_chararray,
  999. { chararray_2_string } @ttypeconvnode.resulttype_chararray_to_string,
  1000. { array_2_pointer } nil,
  1001. { pointer_2_array } nil,
  1002. { int_2_int } @ttypeconvnode.resulttype_int_to_int,
  1003. { int_2_bool } nil,
  1004. { bool_2_bool } nil,
  1005. { bool_2_int } nil,
  1006. { real_2_real } @ttypeconvnode.resulttype_real_to_real,
  1007. { int_2_real } @ttypeconvnode.resulttype_int_to_real,
  1008. { real_2_currency } @ttypeconvnode.resulttype_real_to_currency,
  1009. { proc_2_procvar } @ttypeconvnode.resulttype_proc_to_procvar,
  1010. { arrayconstructor_2_set } @ttypeconvnode.resulttype_arrayconstructor_to_set,
  1011. { load_smallset } nil,
  1012. { cord_2_pointer } @ttypeconvnode.resulttype_cord_to_pointer,
  1013. { intf_2_string } nil,
  1014. { intf_2_guid } @ttypeconvnode.resulttype_interface_to_guid,
  1015. { class_2_intf } nil,
  1016. { char_2_char } @ttypeconvnode.resulttype_char_to_char,
  1017. { normal_2_smallset} nil,
  1018. { dynarray_2_openarray} @ttypeconvnode.resulttype_dynarray_to_openarray,
  1019. { pwchar_2_string} @ttypeconvnode.resulttype_pwchar_to_string,
  1020. { variant_2_dynarray} @ttypeconvnode.resulttype_variant_to_dynarray,
  1021. { dynarray_2_variant} @ttypeconvnode.resulttype_dynarray_to_variant,
  1022. { variant_2_enum} @ttypeconvnode.resulttype_variant_to_enum,
  1023. { enum_2_variant} @ttypeconvnode.resulttype_enum_to_variant
  1024. );
  1025. type
  1026. tprocedureofobject = function : tnode of object;
  1027. var
  1028. r : packed record
  1029. proc : pointer;
  1030. obj : pointer;
  1031. end;
  1032. begin
  1033. result:=nil;
  1034. { this is a little bit dirty but it works }
  1035. { and should be quite portable too }
  1036. r.proc:=resulttypeconvert[c];
  1037. r.obj:=self;
  1038. if assigned(r.proc) then
  1039. result:=tprocedureofobject(r)();
  1040. end;
  1041. {$else}
  1042. begin
  1043. case c of
  1044. tc_string_2_string: resulttype_string_to_string;
  1045. tc_char_2_string : resulttype_char_to_string;
  1046. tc_char_2_chararray: resulttype_char_to_chararray;
  1047. tc_pchar_2_string : resulttype_pchar_to_string;
  1048. tc_cchar_2_pchar : resulttype_cchar_to_pchar;
  1049. tc_cstring_2_pchar : resulttype_cstring_to_pchar;
  1050. tc_string_2_chararray : resulttype_string_to_chararray;
  1051. tc_chararray_2_string : resulttype_chararray_to_string;
  1052. tc_real_2_real : resulttype_real_to_real;
  1053. tc_int_2_real : resulttype_int_to_real;
  1054. tc_real_2_currency : resulttype_real_to_currency;
  1055. tc_arrayconstructor_2_set : resulttype_arrayconstructor_to_set;
  1056. tc_cord_2_pointer : resulttype_cord_to_pointer;
  1057. tc_intf_2_guid : resulttype_interface_to_guid;
  1058. tc_char_2_char : resulttype_char_to_char;
  1059. tc_dynarray_2_openarray : resulttype_dynarray_to_openarray;
  1060. tc_pwchar_2_string : resulttype_pwchar_to_string;
  1061. tc_variant_2_dynarray : resulttype_variant_to_dynarray;
  1062. tc_dynarray_2_variant : resulttype_dynarray_to_variant;
  1063. end;
  1064. end;
  1065. {$Endif fpc}
  1066. function ttypeconvnode.det_resulttype:tnode;
  1067. var
  1068. htype : ttype;
  1069. hp : tnode;
  1070. currprocdef,
  1071. aprocdef : tprocdef;
  1072. eq : tequaltype;
  1073. cdoptions : tcompare_defs_options;
  1074. begin
  1075. result:=nil;
  1076. resulttype:=totype;
  1077. resulttypepass(left);
  1078. if codegenerror then
  1079. exit;
  1080. { When absolute force tc_equal }
  1081. if (nf_absolute in flags) then
  1082. begin
  1083. convtype:=tc_equal;
  1084. if not(tstoreddef(resulttype.def).is_intregable) and
  1085. not(tstoreddef(resulttype.def).is_fpuregable) then
  1086. make_not_regable(left);
  1087. exit;
  1088. end;
  1089. { tp procvar support. Skip typecasts to record or set. Those
  1090. convert on the procvar value. This is used to access the
  1091. fields of a methodpointer }
  1092. if not(nf_load_procvar in flags) and
  1093. not(resulttype.def.deftype in [recorddef,setdef]) then
  1094. maybe_call_procvar(left,true);
  1095. { convert array constructors to sets, because there is no conversion
  1096. possible for array constructors }
  1097. if (resulttype.def.deftype<>arraydef) and
  1098. is_array_constructor(left.resulttype.def) then
  1099. begin
  1100. arrayconstructor_to_set(left);
  1101. resulttypepass(left);
  1102. end;
  1103. if convtype=tc_none then
  1104. begin
  1105. cdoptions:=[cdo_check_operator,cdo_allow_variant];
  1106. if nf_explicit in flags then
  1107. include(cdoptions,cdo_explicit);
  1108. if nf_internal in flags then
  1109. include(cdoptions,cdo_internal);
  1110. eq:=compare_defs_ext(left.resulttype.def,resulttype.def,left.nodetype,convtype,aprocdef,cdoptions);
  1111. case eq of
  1112. te_exact,
  1113. te_equal :
  1114. begin
  1115. { because is_equal only checks the basetype for sets we need to
  1116. check here if we are loading a smallset into a normalset }
  1117. if (resulttype.def.deftype=setdef) and
  1118. (left.resulttype.def.deftype=setdef) and
  1119. ((tsetdef(resulttype.def).settype = smallset) xor
  1120. (tsetdef(left.resulttype.def).settype = smallset)) then
  1121. begin
  1122. { constant sets can be converted by changing the type only }
  1123. if (left.nodetype=setconstn) then
  1124. begin
  1125. left.resulttype:=resulttype;
  1126. result:=left;
  1127. left:=nil;
  1128. exit;
  1129. end;
  1130. if (tsetdef(resulttype.def).settype <> smallset) then
  1131. convtype:=tc_load_smallset
  1132. else
  1133. convtype := tc_normal_2_smallset;
  1134. exit;
  1135. end
  1136. else
  1137. begin
  1138. { Only leave when there is no conversion to do.
  1139. We can still need to call a conversion routine,
  1140. like the routine to convert a stringconstnode }
  1141. if convtype in [tc_equal,tc_not_possible] then
  1142. begin
  1143. left.resulttype:=resulttype;
  1144. result:=left;
  1145. left:=nil;
  1146. exit;
  1147. end;
  1148. end;
  1149. end;
  1150. te_convert_l1,
  1151. te_convert_l2,
  1152. te_convert_l3 :
  1153. begin
  1154. { nothing to do }
  1155. end;
  1156. te_convert_operator :
  1157. begin
  1158. include(current_procinfo.flags,pi_do_call);
  1159. inc(aprocdef.procsym.refs);
  1160. hp:=ccallnode.create(ccallparanode.create(left,nil),Tprocsym(aprocdef.procsym),nil,nil,[]);
  1161. { tell explicitly which def we must use !! (PM) }
  1162. tcallnode(hp).procdefinition:=aprocdef;
  1163. left:=nil;
  1164. result:=hp;
  1165. exit;
  1166. end;
  1167. te_incompatible :
  1168. begin
  1169. { Procedures have a resulttype.def of voiddef and functions of their
  1170. own resulttype.def. They will therefore always be incompatible with
  1171. a procvar. Because isconvertable cannot check for procedures we
  1172. use an extra check for them.}
  1173. if (m_tp_procvar in aktmodeswitches) and
  1174. (resulttype.def.deftype=procvardef) then
  1175. begin
  1176. if (left.nodetype=calln) and
  1177. (tcallnode(left).para_count=0) then
  1178. begin
  1179. if assigned(tcallnode(left).right) then
  1180. begin
  1181. { this is already a procvar, if it is really equal
  1182. is checked below }
  1183. convtype:=tc_equal;
  1184. hp:=tcallnode(left).right.getcopy;
  1185. currprocdef:=tprocdef(hp.resulttype.def);
  1186. end
  1187. else
  1188. begin
  1189. convtype:=tc_proc_2_procvar;
  1190. currprocdef:=Tprocsym(Tcallnode(left).symtableprocentry).search_procdef_byprocvardef(Tprocvardef(resulttype.def));
  1191. hp:=cloadnode.create_procvar(tprocsym(tcallnode(left).symtableprocentry),
  1192. currprocdef,tcallnode(left).symtableproc);
  1193. if (tcallnode(left).symtableprocentry.owner.symtabletype=objectsymtable) then
  1194. begin
  1195. if assigned(tcallnode(left).methodpointer) then
  1196. begin
  1197. { Under certain circumstances the methodpointer is a loadvmtaddrn
  1198. which isn't possible if it is used as a method pointer, so
  1199. fix this.
  1200. If you change this, ensure that tests/tbs/tw2669.pp still works }
  1201. if tcallnode(left).methodpointer.nodetype=loadvmtaddrn then
  1202. tloadnode(hp).set_mp(tloadvmtaddrnode(tcallnode(left).methodpointer).left.getcopy)
  1203. else
  1204. tloadnode(hp).set_mp(tcallnode(left).methodpointer.getcopy);
  1205. end
  1206. else
  1207. tloadnode(hp).set_mp(load_self_node);
  1208. end;
  1209. resulttypepass(hp);
  1210. end;
  1211. left.free;
  1212. left:=hp;
  1213. { Now check if the procedure we are going to assign to
  1214. the procvar, is compatible with the procvar's type }
  1215. if not(nf_explicit in flags) and
  1216. (proc_to_procvar_equal(currprocdef,
  1217. tprocvardef(resulttype.def),true)=te_incompatible) then
  1218. IncompatibleTypes(left.resulttype.def,resulttype.def);
  1219. exit;
  1220. end;
  1221. end;
  1222. { Handle explicit type conversions }
  1223. if nf_explicit in flags then
  1224. begin
  1225. { do common tc_equal cast }
  1226. convtype:=tc_equal;
  1227. { ordinal constants can be resized to 1,2,4,8 bytes }
  1228. if (left.nodetype=ordconstn) then
  1229. begin
  1230. { Insert typeconv for ordinal to the correct size first on left, after
  1231. that the other conversion can be done }
  1232. htype.reset;
  1233. case longint(resulttype.def.size) of
  1234. 1 :
  1235. htype:=s8inttype;
  1236. 2 :
  1237. htype:=s16inttype;
  1238. 4 :
  1239. htype:=s32inttype;
  1240. 8 :
  1241. htype:=s64inttype;
  1242. end;
  1243. { we need explicit, because it can also be an enum }
  1244. if assigned(htype.def) then
  1245. inserttypeconv_internal(left,htype)
  1246. else
  1247. CGMessage2(type_e_illegal_type_conversion,left.resulttype.def.gettypename,resulttype.def.gettypename);
  1248. end;
  1249. { check if the result could be in a register }
  1250. if (not(tstoreddef(resulttype.def).is_intregable) and
  1251. not(tstoreddef(resulttype.def).is_fpuregable)) or
  1252. ((left.resulttype.def.deftype = floatdef) and
  1253. (resulttype.def.deftype <> floatdef)) then
  1254. make_not_regable(left);
  1255. { class to class or object to object, with checkobject support }
  1256. if (resulttype.def.deftype=objectdef) and
  1257. (left.resulttype.def.deftype=objectdef) then
  1258. begin
  1259. if (cs_check_object in aktlocalswitches) then
  1260. begin
  1261. if is_class_or_interface(resulttype.def) then
  1262. begin
  1263. { we can translate the typeconvnode to 'as' when
  1264. typecasting to a class or interface }
  1265. hp:=casnode.create(left,cloadvmtaddrnode.create(ctypenode.create(resulttype)));
  1266. left:=nil;
  1267. result:=hp;
  1268. exit;
  1269. end;
  1270. end
  1271. else
  1272. begin
  1273. { check if the types are related }
  1274. if not(nf_internal in flags) and
  1275. (not(tobjectdef(left.resulttype.def).is_related(tobjectdef(resulttype.def)))) and
  1276. (not(tobjectdef(resulttype.def).is_related(tobjectdef(left.resulttype.def)))) then
  1277. begin
  1278. { Give an error when typecasting class to interface, this is compatible
  1279. with delphi }
  1280. if is_interface(resulttype.def) and
  1281. not is_interface(left.resulttype.def) then
  1282. CGMessage2(type_e_classes_not_related,
  1283. FullTypeName(left.resulttype.def,resulttype.def),
  1284. FullTypeName(resulttype.def,left.resulttype.def))
  1285. else
  1286. CGMessage2(type_w_classes_not_related,
  1287. FullTypeName(left.resulttype.def,resulttype.def),
  1288. FullTypeName(resulttype.def,left.resulttype.def))
  1289. end;
  1290. end;
  1291. end
  1292. else
  1293. begin
  1294. { only if the same size or formal def }
  1295. if not(
  1296. (left.resulttype.def.deftype=formaldef) or
  1297. (
  1298. not(is_open_array(left.resulttype.def)) and
  1299. (left.resulttype.def.size=resulttype.def.size)
  1300. ) or
  1301. (
  1302. is_void(left.resulttype.def) and
  1303. (left.nodetype=derefn)
  1304. )
  1305. ) then
  1306. CGMessage2(type_e_illegal_type_conversion,left.resulttype.def.gettypename,resulttype.def.gettypename);
  1307. end;
  1308. end
  1309. else
  1310. IncompatibleTypes(left.resulttype.def,resulttype.def);
  1311. end;
  1312. else
  1313. internalerror(200211231);
  1314. end;
  1315. end;
  1316. { Give hint or warning for unportable code, exceptions are
  1317. - typecasts from constants
  1318. - void }
  1319. if not(nf_internal in flags) and
  1320. (left.nodetype<>ordconstn) and
  1321. not(is_void(left.resulttype.def)) and
  1322. (((left.resulttype.def.deftype=orddef) and
  1323. (resulttype.def.deftype in [pointerdef,procvardef,classrefdef])) or
  1324. ((resulttype.def.deftype=orddef) and
  1325. (left.resulttype.def.deftype in [pointerdef,procvardef,classrefdef]))) then
  1326. begin
  1327. { Give a warning when sizes don't match, because then info will be lost }
  1328. if left.resulttype.def.size=resulttype.def.size then
  1329. CGMessage(type_h_pointer_to_longint_conv_not_portable)
  1330. else
  1331. CGMessage(type_w_pointer_to_longint_conv_not_portable);
  1332. end;
  1333. { Constant folding and other node transitions to
  1334. remove the typeconv node }
  1335. case left.nodetype of
  1336. niln :
  1337. begin
  1338. { nil to ordinal node }
  1339. if (resulttype.def.deftype=orddef) then
  1340. begin
  1341. hp:=cordconstnode.create(0,resulttype,true);
  1342. result:=hp;
  1343. exit;
  1344. end
  1345. else
  1346. { fold nil to any pointer type }
  1347. if (resulttype.def.deftype=pointerdef) then
  1348. begin
  1349. hp:=cnilnode.create;
  1350. hp.resulttype:=resulttype;
  1351. result:=hp;
  1352. exit;
  1353. end
  1354. else
  1355. { remove typeconv after niln, but not when the result is a
  1356. methodpointer. The typeconv of the methodpointer will then
  1357. take care of updateing size of niln to OS_64 }
  1358. if not((resulttype.def.deftype=procvardef) and
  1359. (po_methodpointer in tprocvardef(resulttype.def).procoptions)) then
  1360. begin
  1361. left.resulttype:=resulttype;
  1362. result:=left;
  1363. left:=nil;
  1364. exit;
  1365. end;
  1366. end;
  1367. ordconstn :
  1368. begin
  1369. { ordinal contants can be directly converted }
  1370. { but not char to char because it is a widechar to char or via versa }
  1371. { which needs extra code to do the code page transistion }
  1372. { constant ordinal to pointer }
  1373. if (resulttype.def.deftype=pointerdef) and
  1374. (convtype<>tc_cchar_2_pchar) then
  1375. begin
  1376. hp:=cpointerconstnode.create(TConstPtrUInt(tordconstnode(left).value),resulttype);
  1377. result:=hp;
  1378. exit;
  1379. end
  1380. else if is_ordinal(resulttype.def) and
  1381. not(convtype=tc_char_2_char) then
  1382. begin
  1383. { replace the resulttype and recheck the range }
  1384. left.resulttype:=resulttype;
  1385. testrange(left.resulttype.def,tordconstnode(left).value,(nf_explicit in flags));
  1386. result:=left;
  1387. left:=nil;
  1388. exit;
  1389. end;
  1390. end;
  1391. pointerconstn :
  1392. begin
  1393. { pointerconstn to any pointer is folded too }
  1394. if (resulttype.def.deftype=pointerdef) then
  1395. begin
  1396. left.resulttype:=resulttype;
  1397. result:=left;
  1398. left:=nil;
  1399. exit;
  1400. end
  1401. { constant pointer to ordinal }
  1402. else if is_ordinal(resulttype.def) then
  1403. begin
  1404. hp:=cordconstnode.create(TConstExprInt(tpointerconstnode(left).value),
  1405. resulttype,true);
  1406. result:=hp;
  1407. exit;
  1408. end;
  1409. end;
  1410. end;
  1411. { check if the result could be in a register }
  1412. if not(tstoreddef(resulttype.def).is_intregable) and
  1413. not(tstoreddef(resulttype.def).is_fpuregable) then
  1414. make_not_regable(left);
  1415. { now call the resulttype helper to do constant folding }
  1416. result:=resulttype_call_helper(convtype);
  1417. end;
  1418. procedure Ttypeconvnode.mark_write;
  1419. begin
  1420. left.mark_write;
  1421. end;
  1422. function ttypeconvnode.first_cord_to_pointer : tnode;
  1423. begin
  1424. result:=nil;
  1425. internalerror(200104043);
  1426. end;
  1427. function ttypeconvnode.first_int_to_int : tnode;
  1428. begin
  1429. first_int_to_int:=nil;
  1430. expectloc:=left.expectloc;
  1431. if not is_void(left.resulttype.def) then
  1432. begin
  1433. if (left.expectloc<>LOC_REGISTER) and
  1434. (resulttype.def.size>left.resulttype.def.size) then
  1435. expectloc:=LOC_REGISTER
  1436. else
  1437. if (left.expectloc=LOC_CREGISTER) and
  1438. (resulttype.def.size<left.resulttype.def.size) then
  1439. expectloc:=LOC_REGISTER;
  1440. end;
  1441. {$ifndef cpu64bit}
  1442. if is_64bit(resulttype.def) then
  1443. registersint:=max(registersint,2)
  1444. else
  1445. {$endif cpu64bit}
  1446. registersint:=max(registersint,1);
  1447. end;
  1448. function ttypeconvnode.first_cstring_to_pchar : tnode;
  1449. begin
  1450. first_cstring_to_pchar:=nil;
  1451. registersint:=1;
  1452. expectloc:=LOC_REGISTER;
  1453. end;
  1454. function ttypeconvnode.first_string_to_chararray : tnode;
  1455. begin
  1456. first_string_to_chararray:=nil;
  1457. expectloc:=left.expectloc;
  1458. end;
  1459. function ttypeconvnode.first_char_to_string : tnode;
  1460. begin
  1461. first_char_to_string:=nil;
  1462. expectloc:=LOC_REFERENCE;
  1463. end;
  1464. function ttypeconvnode.first_nothing : tnode;
  1465. begin
  1466. first_nothing:=nil;
  1467. end;
  1468. function ttypeconvnode.first_array_to_pointer : tnode;
  1469. begin
  1470. first_array_to_pointer:=nil;
  1471. if registersint<1 then
  1472. registersint:=1;
  1473. expectloc:=LOC_REGISTER;
  1474. end;
  1475. function ttypeconvnode.first_int_to_real: tnode;
  1476. var
  1477. fname: string[32];
  1478. typname : string[12];
  1479. begin
  1480. { Get the type name }
  1481. { Normally the typename should be one of the following:
  1482. single, double - carl
  1483. }
  1484. typname := lower(pbestrealtype^.def.gettypename);
  1485. { converting a 64bit integer to a float requires a helper }
  1486. if is_64bit(left.resulttype.def) then
  1487. begin
  1488. if is_signed(left.resulttype.def) then
  1489. fname := 'fpc_int64_to_'+typname
  1490. else
  1491. {$warning generic conversion from int to float does not support unsigned integers}
  1492. fname := 'fpc_int64_to_'+typname;
  1493. result := ccallnode.createintern(fname,ccallparanode.create(
  1494. left,nil));
  1495. left:=nil;
  1496. firstpass(result);
  1497. exit;
  1498. end
  1499. else
  1500. { other integers are supposed to be 32 bit }
  1501. begin
  1502. {$warning generic conversion from int to float does not support unsigned integers}
  1503. if is_signed(left.resulttype.def) then
  1504. fname := 'fpc_longint_to_'+typname
  1505. else
  1506. fname := 'fpc_longint_to_'+typname;
  1507. result := ccallnode.createintern(fname,ccallparanode.create(
  1508. left,nil));
  1509. left:=nil;
  1510. firstpass(result);
  1511. exit;
  1512. end;
  1513. end;
  1514. function ttypeconvnode.first_real_to_real : tnode;
  1515. begin
  1516. first_real_to_real:=nil;
  1517. { comp isn't a floating type }
  1518. if registersfpu<1 then
  1519. registersfpu:=1;
  1520. expectloc:=LOC_FPUREGISTER;
  1521. end;
  1522. function ttypeconvnode.first_pointer_to_array : tnode;
  1523. begin
  1524. first_pointer_to_array:=nil;
  1525. if registersint<1 then
  1526. registersint:=1;
  1527. expectloc:=LOC_REFERENCE;
  1528. end;
  1529. function ttypeconvnode.first_cchar_to_pchar : tnode;
  1530. begin
  1531. first_cchar_to_pchar:=nil;
  1532. internalerror(200104021);
  1533. end;
  1534. function ttypeconvnode.first_bool_to_int : tnode;
  1535. begin
  1536. first_bool_to_int:=nil;
  1537. { byte(boolean) or word(wordbool) or longint(longbool) must
  1538. be accepted for var parameters }
  1539. if (nf_explicit in flags) and
  1540. (left.resulttype.def.size=resulttype.def.size) and
  1541. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  1542. exit;
  1543. { when converting to 64bit, first convert to a 32bit int and then }
  1544. { convert to a 64bit int (only necessary for 32bit processors) (JM) }
  1545. if resulttype.def.size > sizeof(aint) then
  1546. begin
  1547. result := ctypeconvnode.create_internal(left,u32inttype);
  1548. result := ctypeconvnode.create(result,resulttype);
  1549. left := nil;
  1550. firstpass(result);
  1551. exit;
  1552. end;
  1553. expectloc:=LOC_REGISTER;
  1554. if registersint<1 then
  1555. registersint:=1;
  1556. end;
  1557. function ttypeconvnode.first_int_to_bool : tnode;
  1558. begin
  1559. first_int_to_bool:=nil;
  1560. { byte(boolean) or word(wordbool) or longint(longbool) must
  1561. be accepted for var parameters }
  1562. if (nf_explicit in flags) and
  1563. (left.resulttype.def.size=resulttype.def.size) and
  1564. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  1565. exit;
  1566. expectloc:=LOC_REGISTER;
  1567. { need if bool to bool !!
  1568. not very nice !!
  1569. insertypeconv(left,s32inttype);
  1570. left.explizit:=true;
  1571. firstpass(left); }
  1572. if registersint<1 then
  1573. registersint:=1;
  1574. end;
  1575. function ttypeconvnode.first_bool_to_bool : tnode;
  1576. begin
  1577. first_bool_to_bool:=nil;
  1578. expectloc:=LOC_REGISTER;
  1579. if registersint<1 then
  1580. registersint:=1;
  1581. end;
  1582. function ttypeconvnode.first_char_to_char : tnode;
  1583. begin
  1584. first_char_to_char:=first_int_to_int;
  1585. end;
  1586. function ttypeconvnode.first_proc_to_procvar : tnode;
  1587. begin
  1588. first_proc_to_procvar:=nil;
  1589. if tabstractprocdef(resulttype.def).is_addressonly then
  1590. begin
  1591. registersint:=left.registersint;
  1592. if registersint<1 then
  1593. registersint:=1;
  1594. expectloc:=LOC_REGISTER;
  1595. end
  1596. else
  1597. begin
  1598. if not(left.expectloc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  1599. CGMessage(parser_e_illegal_expression);
  1600. registersint:=left.registersint;
  1601. expectloc:=left.expectloc
  1602. end
  1603. end;
  1604. function ttypeconvnode.first_load_smallset : tnode;
  1605. var
  1606. srsym: ttypesym;
  1607. p: tcallparanode;
  1608. begin
  1609. if not searchsystype('FPC_SMALL_SET',srsym) then
  1610. internalerror(200108313);
  1611. p := ccallparanode.create(left,nil);
  1612. { reused }
  1613. left := nil;
  1614. { convert parameter explicitely to fpc_small_set }
  1615. p.left := ctypeconvnode.create_internal(p.left,srsym.restype);
  1616. { create call, adjust resulttype }
  1617. result :=
  1618. ccallnode.createinternres('fpc_set_load_small',p,resulttype);
  1619. firstpass(result);
  1620. end;
  1621. function ttypeconvnode.first_ansistring_to_pchar : tnode;
  1622. begin
  1623. first_ansistring_to_pchar:=nil;
  1624. expectloc:=LOC_REGISTER;
  1625. if registersint<1 then
  1626. registersint:=1;
  1627. end;
  1628. function ttypeconvnode.first_arrayconstructor_to_set : tnode;
  1629. begin
  1630. first_arrayconstructor_to_set:=nil;
  1631. internalerror(200104022);
  1632. end;
  1633. function ttypeconvnode.first_class_to_intf : tnode;
  1634. begin
  1635. first_class_to_intf:=nil;
  1636. expectloc:=LOC_REGISTER;
  1637. if registersint<1 then
  1638. registersint:=1;
  1639. end;
  1640. function ttypeconvnode._first_int_to_int : tnode;
  1641. begin
  1642. result:=first_int_to_int;
  1643. end;
  1644. function ttypeconvnode._first_cstring_to_pchar : tnode;
  1645. begin
  1646. result:=first_cstring_to_pchar;
  1647. end;
  1648. function ttypeconvnode._first_string_to_chararray : tnode;
  1649. begin
  1650. result:=first_string_to_chararray;
  1651. end;
  1652. function ttypeconvnode._first_char_to_string : tnode;
  1653. begin
  1654. result:=first_char_to_string;
  1655. end;
  1656. function ttypeconvnode._first_nothing : tnode;
  1657. begin
  1658. result:=first_nothing;
  1659. end;
  1660. function ttypeconvnode._first_array_to_pointer : tnode;
  1661. begin
  1662. result:=first_array_to_pointer;
  1663. end;
  1664. function ttypeconvnode._first_int_to_real : tnode;
  1665. begin
  1666. result:=first_int_to_real;
  1667. end;
  1668. function ttypeconvnode._first_real_to_real : tnode;
  1669. begin
  1670. result:=first_real_to_real;
  1671. end;
  1672. function ttypeconvnode._first_pointer_to_array : tnode;
  1673. begin
  1674. result:=first_pointer_to_array;
  1675. end;
  1676. function ttypeconvnode._first_cchar_to_pchar : tnode;
  1677. begin
  1678. result:=first_cchar_to_pchar;
  1679. end;
  1680. function ttypeconvnode._first_bool_to_int : tnode;
  1681. begin
  1682. result:=first_bool_to_int;
  1683. end;
  1684. function ttypeconvnode._first_int_to_bool : tnode;
  1685. begin
  1686. result:=first_int_to_bool;
  1687. end;
  1688. function ttypeconvnode._first_bool_to_bool : tnode;
  1689. begin
  1690. result:=first_bool_to_bool;
  1691. end;
  1692. function ttypeconvnode._first_proc_to_procvar : tnode;
  1693. begin
  1694. result:=first_proc_to_procvar;
  1695. end;
  1696. function ttypeconvnode._first_load_smallset : tnode;
  1697. begin
  1698. result:=first_load_smallset;
  1699. end;
  1700. function ttypeconvnode._first_cord_to_pointer : tnode;
  1701. begin
  1702. result:=first_cord_to_pointer;
  1703. end;
  1704. function ttypeconvnode._first_ansistring_to_pchar : tnode;
  1705. begin
  1706. result:=first_ansistring_to_pchar;
  1707. end;
  1708. function ttypeconvnode._first_arrayconstructor_to_set : tnode;
  1709. begin
  1710. result:=first_arrayconstructor_to_set;
  1711. end;
  1712. function ttypeconvnode._first_class_to_intf : tnode;
  1713. begin
  1714. result:=first_class_to_intf;
  1715. end;
  1716. function ttypeconvnode._first_char_to_char : tnode;
  1717. begin
  1718. result:=first_char_to_char;
  1719. end;
  1720. function ttypeconvnode.first_call_helper(c : tconverttype) : tnode;
  1721. const
  1722. firstconvert : array[tconverttype] of pointer = (
  1723. nil, { none }
  1724. @ttypeconvnode._first_nothing, {equal}
  1725. @ttypeconvnode._first_nothing, {not_possible}
  1726. nil, { removed in resulttype_string_to_string }
  1727. @ttypeconvnode._first_char_to_string,
  1728. @ttypeconvnode._first_nothing, { char_2_chararray, needs nothing extra }
  1729. nil, { removed in resulttype_chararray_to_string }
  1730. @ttypeconvnode._first_cchar_to_pchar,
  1731. @ttypeconvnode._first_cstring_to_pchar,
  1732. @ttypeconvnode._first_ansistring_to_pchar,
  1733. @ttypeconvnode._first_string_to_chararray,
  1734. nil, { removed in resulttype_chararray_to_string }
  1735. @ttypeconvnode._first_array_to_pointer,
  1736. @ttypeconvnode._first_pointer_to_array,
  1737. @ttypeconvnode._first_int_to_int,
  1738. @ttypeconvnode._first_int_to_bool,
  1739. @ttypeconvnode._first_bool_to_bool,
  1740. @ttypeconvnode._first_bool_to_int,
  1741. @ttypeconvnode._first_real_to_real,
  1742. @ttypeconvnode._first_int_to_real,
  1743. nil, { removed in resulttype_real_to_currency }
  1744. @ttypeconvnode._first_proc_to_procvar,
  1745. @ttypeconvnode._first_arrayconstructor_to_set,
  1746. @ttypeconvnode._first_load_smallset,
  1747. @ttypeconvnode._first_cord_to_pointer,
  1748. @ttypeconvnode._first_nothing,
  1749. @ttypeconvnode._first_nothing,
  1750. @ttypeconvnode._first_class_to_intf,
  1751. @ttypeconvnode._first_char_to_char,
  1752. @ttypeconvnode._first_nothing,
  1753. @ttypeconvnode._first_nothing,
  1754. nil,
  1755. nil,
  1756. nil,
  1757. nil,
  1758. nil
  1759. );
  1760. type
  1761. tprocedureofobject = function : tnode of object;
  1762. var
  1763. r : packed record
  1764. proc : pointer;
  1765. obj : pointer;
  1766. end;
  1767. begin
  1768. { this is a little bit dirty but it works }
  1769. { and should be quite portable too }
  1770. r.proc:=firstconvert[c];
  1771. r.obj:=self;
  1772. if not assigned(r.proc) then
  1773. internalerror(200312081);
  1774. first_call_helper:=tprocedureofobject(r){$ifdef FPC}(){$endif FPC}
  1775. end;
  1776. function ttypeconvnode.pass_1 : tnode;
  1777. begin
  1778. result:=nil;
  1779. firstpass(left);
  1780. if codegenerror then
  1781. exit;
  1782. { load the value_str from the left part }
  1783. registersint:=left.registersint;
  1784. registersfpu:=left.registersfpu;
  1785. {$ifdef SUPPORT_MMX}
  1786. registersmmx:=left.registersmmx;
  1787. {$endif}
  1788. expectloc:=left.expectloc;
  1789. result:=first_call_helper(convtype);
  1790. end;
  1791. function ttypeconvnode.assign_allowed:boolean;
  1792. begin
  1793. result:=(convtype=tc_equal) or
  1794. { typecasting from void is always allowed }
  1795. is_void(left.resulttype.def) or
  1796. (left.resulttype.def.deftype=formaldef) or
  1797. { int 2 int with same size reuses same location, or for
  1798. tp7 mode also allow size < orignal size }
  1799. (
  1800. (convtype=tc_int_2_int) and
  1801. (
  1802. (resulttype.def.size=left.resulttype.def.size) or
  1803. ((m_tp7 in aktmodeswitches) and
  1804. (resulttype.def.size<left.resulttype.def.size))
  1805. )
  1806. ) or
  1807. { int 2 bool/bool 2 int, explicit typecast, see also nx86cnv }
  1808. ((convtype in [tc_int_2_bool,tc_bool_2_int]) and
  1809. (nf_explicit in flags) and
  1810. (resulttype.def.size=left.resulttype.def.size));
  1811. { When using only a part of the value it can't be in a register since
  1812. that will load the value in a new register first }
  1813. if (resulttype.def.size<left.resulttype.def.size) then
  1814. make_not_regable(left);
  1815. end;
  1816. function ttypeconvnode.docompare(p: tnode) : boolean;
  1817. begin
  1818. docompare :=
  1819. inherited docompare(p) and
  1820. (convtype = ttypeconvnode(p).convtype);
  1821. end;
  1822. procedure ttypeconvnode._second_int_to_int;
  1823. begin
  1824. second_int_to_int;
  1825. end;
  1826. procedure ttypeconvnode._second_string_to_string;
  1827. begin
  1828. second_string_to_string;
  1829. end;
  1830. procedure ttypeconvnode._second_cstring_to_pchar;
  1831. begin
  1832. second_cstring_to_pchar;
  1833. end;
  1834. procedure ttypeconvnode._second_string_to_chararray;
  1835. begin
  1836. second_string_to_chararray;
  1837. end;
  1838. procedure ttypeconvnode._second_array_to_pointer;
  1839. begin
  1840. second_array_to_pointer;
  1841. end;
  1842. procedure ttypeconvnode._second_pointer_to_array;
  1843. begin
  1844. second_pointer_to_array;
  1845. end;
  1846. procedure ttypeconvnode._second_chararray_to_string;
  1847. begin
  1848. second_chararray_to_string;
  1849. end;
  1850. procedure ttypeconvnode._second_char_to_string;
  1851. begin
  1852. second_char_to_string;
  1853. end;
  1854. procedure ttypeconvnode._second_int_to_real;
  1855. begin
  1856. second_int_to_real;
  1857. end;
  1858. procedure ttypeconvnode._second_real_to_real;
  1859. begin
  1860. second_real_to_real;
  1861. end;
  1862. procedure ttypeconvnode._second_cord_to_pointer;
  1863. begin
  1864. second_cord_to_pointer;
  1865. end;
  1866. procedure ttypeconvnode._second_proc_to_procvar;
  1867. begin
  1868. second_proc_to_procvar;
  1869. end;
  1870. procedure ttypeconvnode._second_bool_to_int;
  1871. begin
  1872. second_bool_to_int;
  1873. end;
  1874. procedure ttypeconvnode._second_int_to_bool;
  1875. begin
  1876. second_int_to_bool;
  1877. end;
  1878. procedure ttypeconvnode._second_bool_to_bool;
  1879. begin
  1880. second_bool_to_bool;
  1881. end;
  1882. procedure ttypeconvnode._second_load_smallset;
  1883. begin
  1884. second_load_smallset;
  1885. end;
  1886. procedure ttypeconvnode._second_ansistring_to_pchar;
  1887. begin
  1888. second_ansistring_to_pchar;
  1889. end;
  1890. procedure ttypeconvnode._second_class_to_intf;
  1891. begin
  1892. second_class_to_intf;
  1893. end;
  1894. procedure ttypeconvnode._second_char_to_char;
  1895. begin
  1896. second_char_to_char;
  1897. end;
  1898. procedure ttypeconvnode._second_nothing;
  1899. begin
  1900. second_nothing;
  1901. end;
  1902. procedure ttypeconvnode.second_call_helper(c : tconverttype);
  1903. const
  1904. secondconvert : array[tconverttype] of pointer = (
  1905. @ttypeconvnode._second_nothing, {none}
  1906. @ttypeconvnode._second_nothing, {equal}
  1907. @ttypeconvnode._second_nothing, {not_possible}
  1908. @ttypeconvnode._second_nothing, {second_string_to_string, handled in resulttype pass }
  1909. @ttypeconvnode._second_char_to_string,
  1910. @ttypeconvnode._second_nothing, {char_to_charray}
  1911. @ttypeconvnode._second_nothing, { pchar_to_string, handled in resulttype pass }
  1912. @ttypeconvnode._second_nothing, {cchar_to_pchar}
  1913. @ttypeconvnode._second_cstring_to_pchar,
  1914. @ttypeconvnode._second_ansistring_to_pchar,
  1915. @ttypeconvnode._second_string_to_chararray,
  1916. @ttypeconvnode._second_nothing, { chararray_to_string, handled in resulttype pass }
  1917. @ttypeconvnode._second_array_to_pointer,
  1918. @ttypeconvnode._second_pointer_to_array,
  1919. @ttypeconvnode._second_int_to_int,
  1920. @ttypeconvnode._second_int_to_bool,
  1921. @ttypeconvnode._second_bool_to_bool,
  1922. @ttypeconvnode._second_bool_to_int,
  1923. @ttypeconvnode._second_real_to_real,
  1924. @ttypeconvnode._second_int_to_real,
  1925. @ttypeconvnode._second_nothing, { real_to_currency, handled in resulttype pass }
  1926. @ttypeconvnode._second_proc_to_procvar,
  1927. @ttypeconvnode._second_nothing, { arrayconstructor_to_set }
  1928. @ttypeconvnode._second_nothing, { second_load_smallset, handled in first pass }
  1929. @ttypeconvnode._second_cord_to_pointer,
  1930. @ttypeconvnode._second_nothing, { interface 2 string }
  1931. @ttypeconvnode._second_nothing, { interface 2 guid }
  1932. @ttypeconvnode._second_class_to_intf,
  1933. @ttypeconvnode._second_char_to_char,
  1934. @ttypeconvnode._second_nothing, { normal_2_smallset }
  1935. @ttypeconvnode._second_nothing, { dynarray_2_openarray }
  1936. @ttypeconvnode._second_nothing, { pwchar_2_string }
  1937. @ttypeconvnode._second_nothing, { variant_2_dynarray }
  1938. @ttypeconvnode._second_nothing, { dynarray_2_variant}
  1939. @ttypeconvnode._second_nothing, { variant_2_enum }
  1940. @ttypeconvnode._second_nothing { enum_2_variant }
  1941. );
  1942. type
  1943. tprocedureofobject = procedure of object;
  1944. var
  1945. r : packed record
  1946. proc : pointer;
  1947. obj : pointer;
  1948. end;
  1949. begin
  1950. { this is a little bit dirty but it works }
  1951. { and should be quite portable too }
  1952. r.proc:=secondconvert[c];
  1953. r.obj:=self;
  1954. tprocedureofobject(r)();
  1955. end;
  1956. {*****************************************************************************
  1957. TISNODE
  1958. *****************************************************************************}
  1959. constructor tisnode.create(l,r : tnode);
  1960. begin
  1961. inherited create(isn,l,r);
  1962. end;
  1963. function tisnode.det_resulttype:tnode;
  1964. var
  1965. paras: tcallparanode;
  1966. begin
  1967. result:=nil;
  1968. resulttypepass(left);
  1969. resulttypepass(right);
  1970. set_varstate(left,vs_used,true);
  1971. set_varstate(right,vs_used,true);
  1972. if codegenerror then
  1973. exit;
  1974. if (right.resulttype.def.deftype=classrefdef) then
  1975. begin
  1976. { left must be a class }
  1977. if is_class(left.resulttype.def) then
  1978. begin
  1979. { the operands must be related }
  1980. if (not(tobjectdef(left.resulttype.def).is_related(
  1981. tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def)))) and
  1982. (not(tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def).is_related(
  1983. tobjectdef(left.resulttype.def)))) then
  1984. CGMessage2(type_e_classes_not_related,left.resulttype.def.typename,
  1985. tclassrefdef(right.resulttype.def).pointertype.def.typename);
  1986. end
  1987. else
  1988. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  1989. { call fpc_do_is helper }
  1990. paras := ccallparanode.create(
  1991. left,
  1992. ccallparanode.create(
  1993. right,nil));
  1994. result := ccallnode.createintern('fpc_do_is',paras);
  1995. left := nil;
  1996. right := nil;
  1997. end
  1998. else if is_interface(right.resulttype.def) then
  1999. begin
  2000. { left is a class }
  2001. if is_class(left.resulttype.def) then
  2002. begin
  2003. { the operands must be related }
  2004. if not(assigned(tobjectdef(left.resulttype.def).implementedinterfaces) and
  2005. (tobjectdef(left.resulttype.def).implementedinterfaces.searchintf(right.resulttype.def)<>-1)) then
  2006. CGMessage2(type_e_classes_not_related,
  2007. FullTypeName(left.resulttype.def,right.resulttype.def),
  2008. FullTypeName(right.resulttype.def,left.resulttype.def))
  2009. end
  2010. { left is an interface }
  2011. else if is_interface(left.resulttype.def) then
  2012. begin
  2013. { the operands must be related }
  2014. if (not(tobjectdef(left.resulttype.def).is_related(tobjectdef(right.resulttype.def)))) and
  2015. (not(tobjectdef(right.resulttype.def).is_related(tobjectdef(left.resulttype.def)))) then
  2016. CGMessage2(type_e_classes_not_related,
  2017. FullTypeName(left.resulttype.def,right.resulttype.def),
  2018. FullTypeName(right.resulttype.def,left.resulttype.def));
  2019. end
  2020. else
  2021. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  2022. { call fpc_do_is helper }
  2023. paras := ccallparanode.create(
  2024. left,
  2025. ccallparanode.create(
  2026. right,nil));
  2027. result := ccallnode.createintern('fpc_do_is',paras);
  2028. left := nil;
  2029. right := nil;
  2030. end
  2031. else
  2032. CGMessage1(type_e_class_or_interface_type_expected,right.resulttype.def.typename);
  2033. resulttype:=booltype;
  2034. end;
  2035. function tisnode.pass_1 : tnode;
  2036. begin
  2037. internalerror(200204254);
  2038. result:=nil;
  2039. end;
  2040. { dummy pass_2, it will never be called, but we need one since }
  2041. { you can't instantiate an abstract class }
  2042. procedure tisnode.pass_2;
  2043. begin
  2044. end;
  2045. {*****************************************************************************
  2046. TASNODE
  2047. *****************************************************************************}
  2048. constructor tasnode.create(l,r : tnode);
  2049. begin
  2050. inherited create(asn,l,r);
  2051. call := nil;
  2052. end;
  2053. destructor tasnode.destroy;
  2054. begin
  2055. call.free;
  2056. inherited destroy;
  2057. end;
  2058. function tasnode.det_resulttype:tnode;
  2059. var
  2060. hp : tnode;
  2061. begin
  2062. result:=nil;
  2063. resulttypepass(right);
  2064. resulttypepass(left);
  2065. set_varstate(right,vs_used,true);
  2066. set_varstate(left,vs_used,true);
  2067. if codegenerror then
  2068. exit;
  2069. if (right.resulttype.def.deftype=classrefdef) then
  2070. begin
  2071. { left must be a class }
  2072. if is_class(left.resulttype.def) then
  2073. begin
  2074. { the operands must be related }
  2075. if (not(tobjectdef(left.resulttype.def).is_related(
  2076. tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def)))) and
  2077. (not(tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def).is_related(
  2078. tobjectdef(left.resulttype.def)))) then
  2079. CGMessage2(type_e_classes_not_related,
  2080. FullTypeName(left.resulttype.def,tclassrefdef(right.resulttype.def).pointertype.def),
  2081. FullTypeName(tclassrefdef(right.resulttype.def).pointertype.def,left.resulttype.def));
  2082. end
  2083. else
  2084. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  2085. resulttype:=tclassrefdef(right.resulttype.def).pointertype;
  2086. end
  2087. else if is_interface(right.resulttype.def) then
  2088. begin
  2089. { left is a class }
  2090. if not(is_class(left.resulttype.def) or
  2091. is_interface(left.resulttype.def)) then
  2092. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  2093. resulttype:=right.resulttype;
  2094. { load the GUID of the interface }
  2095. if (right.nodetype=typen) then
  2096. begin
  2097. if assigned(tobjectdef(right.resulttype.def).iidguid) then
  2098. begin
  2099. hp:=cguidconstnode.create(tobjectdef(right.resulttype.def).iidguid^);
  2100. right.free;
  2101. right:=hp;
  2102. end
  2103. else
  2104. internalerror(200206282);
  2105. resulttypepass(right);
  2106. end;
  2107. end
  2108. else
  2109. CGMessage1(type_e_class_or_interface_type_expected,right.resulttype.def.typename);
  2110. end;
  2111. function tasnode.getcopy: tnode;
  2112. begin
  2113. result := inherited getcopy;
  2114. if assigned(call) then
  2115. tasnode(result).call := call.getcopy
  2116. else
  2117. tasnode(result).call := nil;
  2118. end;
  2119. function tasnode.pass_1 : tnode;
  2120. var
  2121. procname: string;
  2122. begin
  2123. result:=nil;
  2124. if not assigned(call) then
  2125. begin
  2126. if is_class(left.resulttype.def) and
  2127. (right.resulttype.def.deftype=classrefdef) then
  2128. call := ccallnode.createinternres('fpc_do_as',
  2129. ccallparanode.create(left,ccallparanode.create(right,nil)),
  2130. resulttype)
  2131. else
  2132. begin
  2133. if is_class(left.resulttype.def) then
  2134. procname := 'fpc_class_as_intf'
  2135. else
  2136. procname := 'fpc_intf_as';
  2137. call := ccallnode.createinternres(procname,
  2138. ccallparanode.create(right,ccallparanode.create(left,nil)),
  2139. resulttype);
  2140. end;
  2141. left := nil;
  2142. right := nil;
  2143. firstpass(call);
  2144. if codegenerror then
  2145. exit;
  2146. expectloc:=call.expectloc;
  2147. registersint:=call.registersint;
  2148. registersfpu:=call.registersfpu;
  2149. {$ifdef SUPPORT_MMX}
  2150. registersmmx:=call.registersmmx;
  2151. {$endif SUPPORT_MMX}
  2152. end;
  2153. end;
  2154. begin
  2155. ctypeconvnode:=ttypeconvnode;
  2156. casnode:=tasnode;
  2157. cisnode:=tisnode;
  2158. end.
  2159. {
  2160. $Log$
  2161. Revision 1.166 2004-12-05 12:28:11 peter
  2162. * procvar handling for tp procvar mode fixed
  2163. * proc to procvar moved from addrnode to typeconvnode
  2164. * inlininginfo is now allocated only for inline routines that
  2165. can be inlined, introduced a new flag po_has_inlining_info
  2166. Revision 1.165 2004/12/05 12:15:11 florian
  2167. * fixed compiler side of variant <-> dyn. array conversion
  2168. Revision 1.164 2004/11/26 22:34:28 peter
  2169. * internal flag for compare_defs_ext
  2170. Revision 1.163 2004/11/21 15:35:23 peter
  2171. * float routines all use internproc and compilerproc helpers
  2172. Revision 1.162 2004/11/02 20:15:53 jonas
  2173. * copy totype field in ttypeconvnode.getcopy()
  2174. Revision 1.161 2004/11/02 12:55:16 peter
  2175. * nf_internal flag for internal inserted typeconvs. This will
  2176. supress the generation of warning/hints
  2177. Revision 1.160 2004/11/01 23:30:11 peter
  2178. * support > 32bit accesses for x86_64
  2179. * rewrote array size checking to support 64bit
  2180. Revision 1.159 2004/11/01 17:15:47 peter
  2181. * no checkpointer code for dynarr to openarr
  2182. Revision 1.158 2004/11/01 15:31:58 peter
  2183. * -Or fix for absolute
  2184. Revision 1.157 2004/10/24 11:44:28 peter
  2185. * small regvar fixes
  2186. * loadref parameter removed from concatcopy,incrrefcount,etc
  2187. Revision 1.156 2004/10/15 09:14:17 mazen
  2188. - remove $IFDEF DELPHI and related code
  2189. - remove $IFDEF FPCPROCVAR and related code
  2190. Revision 1.155 2004/10/12 14:33:41 peter
  2191. * give error when converting class to interface are not related
  2192. Revision 1.154 2004/10/11 15:48:15 peter
  2193. * small regvar for para fixes
  2194. * function tvarsym.is_regvar added
  2195. * tvarsym.getvaluesize removed, use getsize instead
  2196. Revision 1.153 2004/09/26 17:45:30 peter
  2197. * simple regvar support, not yet finished
  2198. Revision 1.152 2004/08/08 16:00:56 florian
  2199. * constant floating point assignments etc. are now overflow checked
  2200. if Q+ or R+ is turned on
  2201. Revision 1.151 2004/06/29 20:57:50 peter
  2202. * fix pchar:=char
  2203. * fix longint(smallset)
  2204. Revision 1.150 2004/06/23 16:22:45 peter
  2205. * include unit name in error messages when types are the same
  2206. Revision 1.149 2004/06/20 08:55:29 florian
  2207. * logs truncated
  2208. Revision 1.148 2004/06/16 20:07:08 florian
  2209. * dwarf branch merged
  2210. Revision 1.147 2004/05/23 18:28:41 peter
  2211. * methodpointer is loaded into a temp when it was a calln
  2212. Revision 1.146 2004/05/23 15:03:40 peter
  2213. * some typeconvs don't allow assignment or passing to var para
  2214. Revision 1.145 2004/05/23 14:14:18 florian
  2215. + added set of widechar support (limited to 256 chars, is delphi compatible)
  2216. Revision 1.144 2004/04/29 19:56:37 daniel
  2217. * Prepare compiler infrastructure for multiple ansistring types
  2218. }