ncnv.pas 95 KB

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