ncnv.pas 94 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701
  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_read,[vsf_must_be_valid]);
  340. if assigned(p3) then
  341. begin
  342. resulttypepass(p3);
  343. set_varstate(p3,vs_read,[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(cordconstnode.create(
  625. ord(tarraydef(left.resulttype.def).lowrange=0),booltype,false),
  626. ccallparanode.create(left,nil)),resulttype);
  627. left := nil;
  628. end;
  629. function ttypeconvnode.resulttype_string_to_chararray : tnode;
  630. var
  631. arrsize : aint;
  632. chartype : string[8];
  633. begin
  634. result := nil;
  635. with tarraydef(resulttype.def) do
  636. begin
  637. if highrange<lowrange then
  638. internalerror(200501051);
  639. arrsize := highrange-lowrange+1;
  640. end;
  641. if (left.nodetype = stringconstn) and
  642. (tstringconstnode(left).cst_type=cst_conststring) then
  643. begin
  644. { if the array of char is large enough we can use the string
  645. constant directly. This is handled in ncgcnv }
  646. if (arrsize>=tstringconstnode(left).len) and
  647. is_char(tarraydef(resulttype.def).elementtype.def) then
  648. exit;
  649. { Convert to wide/short/ansistring and call default helper }
  650. if is_widechar(tarraydef(resulttype.def).elementtype.def) then
  651. inserttypeconv(left,cwidestringtype)
  652. else
  653. begin
  654. if tstringconstnode(left).len>255 then
  655. inserttypeconv(left,cansistringtype)
  656. else
  657. inserttypeconv(left,cshortstringtype);
  658. end;
  659. end;
  660. if is_widechar(tarraydef(resulttype.def).elementtype.def) then
  661. chartype:='widechar'
  662. else
  663. chartype:='char';
  664. result := ccallnode.createinternres(
  665. 'fpc_'+tstringdef(left.resulttype.def).stringtypname+
  666. '_to_'+chartype+'array',ccallparanode.create(left,ccallparanode.create(
  667. cordconstnode.create(arrsize,s32inttype,true),nil)),resulttype);
  668. left := nil;
  669. end;
  670. function ttypeconvnode.resulttype_string_to_string : tnode;
  671. var
  672. procname: string[31];
  673. stringpara : tcallparanode;
  674. begin
  675. result:=nil;
  676. if left.nodetype=stringconstn then
  677. begin
  678. tstringconstnode(left).changestringtype(resulttype);
  679. result:=left;
  680. left:=nil;
  681. end
  682. else
  683. begin
  684. { get the correct procedure name }
  685. procname := 'fpc_'+tstringdef(left.resulttype.def).stringtypname+
  686. '_to_'+tstringdef(resulttype.def).stringtypname;
  687. { create parameter (and remove left node from typeconvnode }
  688. { since it's reused as parameter) }
  689. stringpara := ccallparanode.create(left,nil);
  690. left := nil;
  691. { when converting to shortstrings, we have to pass high(destination) too }
  692. if (tstringdef(resulttype.def).string_typ = st_shortstring) then
  693. stringpara.right := ccallparanode.create(cinlinenode.create(
  694. in_high_x,false,self.getcopy),nil);
  695. { and create the callnode }
  696. result := ccallnode.createinternres(procname,stringpara,resulttype);
  697. end;
  698. end;
  699. function ttypeconvnode.resulttype_char_to_string : tnode;
  700. var
  701. procname: string[31];
  702. para : tcallparanode;
  703. hp : tstringconstnode;
  704. ws : pcompilerwidestring;
  705. begin
  706. result:=nil;
  707. if left.nodetype=ordconstn then
  708. begin
  709. if tstringdef(resulttype.def).string_typ=st_widestring then
  710. begin
  711. initwidestring(ws);
  712. if torddef(left.resulttype.def).typ=uwidechar then
  713. concatwidestringchar(ws,tcompilerwidechar(tordconstnode(left).value))
  714. else
  715. concatwidestringchar(ws,tcompilerwidechar(chr(tordconstnode(left).value)));
  716. hp:=cstringconstnode.createwstr(ws);
  717. donewidestring(ws);
  718. end
  719. else
  720. begin
  721. hp:=cstringconstnode.createstr(chr(tordconstnode(left).value));
  722. tstringconstnode(hp).changestringtype(resulttype);
  723. end;
  724. result:=hp;
  725. end
  726. else
  727. { shortstrings are handled 'inline' }
  728. if tstringdef(resulttype.def).string_typ <> st_shortstring then
  729. begin
  730. { create the parameter }
  731. para := ccallparanode.create(left,nil);
  732. left := nil;
  733. { and the procname }
  734. procname := 'fpc_char_to_' +tstringdef(resulttype.def).stringtypname;
  735. { and finally the call }
  736. result := ccallnode.createinternres(procname,para,resulttype);
  737. end
  738. else
  739. begin
  740. { create word(byte(char) shl 8 or 1) for litte endian machines }
  741. { and word(byte(char) or 256) for big endian machines }
  742. left := ctypeconvnode.create_internal(left,u8inttype);
  743. if (target_info.endian = endian_little) then
  744. left := caddnode.create(orn,
  745. cshlshrnode.create(shln,left,cordconstnode.create(8,s32inttype,false)),
  746. cordconstnode.create(1,s32inttype,false))
  747. else
  748. left := caddnode.create(orn,left,
  749. cordconstnode.create(1 shl 8,s32inttype,false));
  750. left := ctypeconvnode.create_internal(left,u16inttype);
  751. resulttypepass(left);
  752. end;
  753. end;
  754. function ttypeconvnode.resulttype_char_to_chararray : tnode;
  755. begin
  756. if resulttype.def.size <> 1 then
  757. begin
  758. { convert first to string, then to chararray }
  759. inserttypeconv(left,cshortstringtype);
  760. inserttypeconv(left,resulttype);
  761. result:=left;
  762. left := nil;
  763. exit;
  764. end;
  765. result := nil;
  766. end;
  767. function ttypeconvnode.resulttype_char_to_char : tnode;
  768. var
  769. hp : tordconstnode;
  770. begin
  771. result:=nil;
  772. if left.nodetype=ordconstn then
  773. begin
  774. if (torddef(resulttype.def).typ=uchar) and
  775. (torddef(left.resulttype.def).typ=uwidechar) then
  776. begin
  777. hp:=cordconstnode.create(
  778. ord(unicode2asciichar(tcompilerwidechar(tordconstnode(left).value))),
  779. cchartype,true);
  780. result:=hp;
  781. end
  782. else if (torddef(resulttype.def).typ=uwidechar) and
  783. (torddef(left.resulttype.def).typ=uchar) then
  784. begin
  785. hp:=cordconstnode.create(
  786. asciichar2unicode(chr(tordconstnode(left).value)),
  787. cwidechartype,true);
  788. result:=hp;
  789. end
  790. else
  791. internalerror(200105131);
  792. exit;
  793. end;
  794. end;
  795. function ttypeconvnode.resulttype_int_to_int : tnode;
  796. var
  797. v : TConstExprInt;
  798. begin
  799. result:=nil;
  800. if left.nodetype=ordconstn then
  801. begin
  802. v:=tordconstnode(left).value;
  803. if is_currency(resulttype.def) then
  804. v:=v*10000;
  805. if (resulttype.def.deftype=pointerdef) then
  806. result:=cpointerconstnode.create(TConstPtrUInt(v),resulttype)
  807. else
  808. begin
  809. if is_currency(left.resulttype.def) then
  810. v:=v div 10000;
  811. result:=cordconstnode.create(v,resulttype,false);
  812. end;
  813. end
  814. else if left.nodetype=pointerconstn then
  815. begin
  816. v:=tpointerconstnode(left).value;
  817. if (resulttype.def.deftype=pointerdef) then
  818. result:=cpointerconstnode.create(v,resulttype)
  819. else
  820. begin
  821. if is_currency(resulttype.def) then
  822. v:=v*10000;
  823. result:=cordconstnode.create(v,resulttype,false);
  824. end;
  825. end
  826. else
  827. begin
  828. { multiply by 10000 for currency. We need to use getcopy to pass
  829. the argument because the current node is always disposed. Only
  830. inserting the multiply in the left node is not possible because
  831. it'll get in an infinite loop to convert int->currency }
  832. if is_currency(resulttype.def) then
  833. begin
  834. result:=caddnode.create(muln,getcopy,cordconstnode.create(10000,resulttype,false));
  835. include(result.flags,nf_is_currency);
  836. end
  837. else if is_currency(left.resulttype.def) then
  838. begin
  839. result:=cmoddivnode.create(divn,getcopy,cordconstnode.create(10000,resulttype,false));
  840. include(result.flags,nf_is_currency);
  841. end;
  842. end;
  843. end;
  844. function ttypeconvnode.resulttype_int_to_real : tnode;
  845. var
  846. rv : bestreal;
  847. begin
  848. result:=nil;
  849. if left.nodetype=ordconstn then
  850. begin
  851. rv:=tordconstnode(left).value;
  852. if is_currency(resulttype.def) then
  853. rv:=rv*10000.0
  854. else if is_currency(left.resulttype.def) then
  855. rv:=rv/10000.0;
  856. result:=crealconstnode.create(rv,resulttype);
  857. end
  858. else
  859. begin
  860. { multiply by 10000 for currency. We need to use getcopy to pass
  861. the argument because the current node is always disposed. Only
  862. inserting the multiply in the left node is not possible because
  863. it'll get in an infinite loop to convert int->currency }
  864. if is_currency(resulttype.def) then
  865. begin
  866. result:=caddnode.create(muln,getcopy,crealconstnode.create(10000.0,resulttype));
  867. include(result.flags,nf_is_currency);
  868. end
  869. else if is_currency(left.resulttype.def) then
  870. begin
  871. result:=caddnode.create(slashn,getcopy,crealconstnode.create(10000.0,resulttype));
  872. include(result.flags,nf_is_currency);
  873. end;
  874. end;
  875. end;
  876. function ttypeconvnode.resulttype_real_to_currency : tnode;
  877. begin
  878. if not is_currency(resulttype.def) then
  879. internalerror(200304221);
  880. result:=nil;
  881. left:=caddnode.create(muln,left,crealconstnode.create(10000.0,left.resulttype));
  882. include(left.flags,nf_is_currency);
  883. resulttypepass(left);
  884. { Convert constants directly, else call Round() }
  885. if left.nodetype=realconstn then
  886. result:=cordconstnode.create(round(trealconstnode(left).value_real),resulttype,false)
  887. else
  888. result:=ccallnode.createinternres('fpc_round_real',
  889. ccallparanode.create(left,nil),resulttype);
  890. left:=nil;
  891. end;
  892. function ttypeconvnode.resulttype_real_to_real : tnode;
  893. begin
  894. result:=nil;
  895. if is_currency(left.resulttype.def) and not(is_currency(resulttype.def)) then
  896. begin
  897. left:=caddnode.create(slashn,left,crealconstnode.create(10000.0,left.resulttype));
  898. include(left.flags,nf_is_currency);
  899. resulttypepass(left);
  900. end
  901. else
  902. if is_currency(resulttype.def) and not(is_currency(left.resulttype.def)) then
  903. begin
  904. left:=caddnode.create(muln,left,crealconstnode.create(10000.0,left.resulttype));
  905. include(left.flags,nf_is_currency);
  906. resulttypepass(left);
  907. end;
  908. if left.nodetype=realconstn then
  909. result:=crealconstnode.create(trealconstnode(left).value_real,resulttype);
  910. end;
  911. function ttypeconvnode.resulttype_cchar_to_pchar : tnode;
  912. begin
  913. result:=nil;
  914. if is_pwidechar(resulttype.def) then
  915. inserttypeconv(left,cwidestringtype)
  916. else
  917. inserttypeconv(left,cshortstringtype);
  918. { evaluate again, reset resulttype so the convert_typ
  919. will be calculated again and cstring_to_pchar will
  920. be used for futher conversion }
  921. convtype:=tc_none;
  922. result:=det_resulttype;
  923. end;
  924. function ttypeconvnode.resulttype_cstring_to_pchar : tnode;
  925. begin
  926. result:=nil;
  927. if is_pwidechar(resulttype.def) then
  928. inserttypeconv(left,cwidestringtype)
  929. else
  930. if is_pchar(resulttype.def) and
  931. is_widestring(left.resulttype.def) then
  932. inserttypeconv(left,cansistringtype);
  933. end;
  934. function ttypeconvnode.resulttype_cstring_to_int : tnode;
  935. var
  936. fcc : cardinal;
  937. pb : pbyte;
  938. begin
  939. result:=nil;
  940. if left.nodetype<>stringconstn then
  941. internalerror(200510012);
  942. if tstringconstnode(left).len=4 then
  943. begin
  944. pb:=pbyte(tstringconstnode(left).value_str);
  945. fcc:=(pb[0] shl 24) or (pb[1] shl 16) or (pb[2] shl 8) or pb[3];
  946. result:=cordconstnode.create(fcc,u32inttype,false);
  947. end
  948. else
  949. CGMessage2(type_e_illegal_type_conversion,left.resulttype.def.gettypename,resulttype.def.gettypename);
  950. end;
  951. function ttypeconvnode.resulttype_arrayconstructor_to_set : tnode;
  952. var
  953. hp : tnode;
  954. begin
  955. result:=nil;
  956. if left.nodetype<>arrayconstructorn then
  957. internalerror(5546);
  958. { remove typeconv node }
  959. hp:=left;
  960. left:=nil;
  961. { create a set constructor tree }
  962. arrayconstructor_to_set(hp);
  963. result:=hp;
  964. end;
  965. function ttypeconvnode.resulttype_pchar_to_string : tnode;
  966. begin
  967. result := ccallnode.createinternres(
  968. 'fpc_pchar_to_'+tstringdef(resulttype.def).stringtypname,
  969. ccallparanode.create(left,nil),resulttype);
  970. left := nil;
  971. end;
  972. function ttypeconvnode.resulttype_interface_to_guid : tnode;
  973. begin
  974. if assigned(tobjectdef(left.resulttype.def).iidguid) then
  975. result:=cguidconstnode.create(tobjectdef(left.resulttype.def).iidguid^);
  976. end;
  977. function ttypeconvnode.resulttype_dynarray_to_openarray : tnode;
  978. begin
  979. { a dynamic array is a pointer to an array, so to convert it to }
  980. { an open array, we have to dereference it (JM) }
  981. result := ctypeconvnode.create_internal(left,voidpointertype);
  982. resulttypepass(result);
  983. { left is reused }
  984. left := nil;
  985. result := cderefnode.create(result);
  986. include(result.flags,nf_no_checkpointer);
  987. result.resulttype := resulttype;
  988. end;
  989. function ttypeconvnode.resulttype_pwchar_to_string : tnode;
  990. begin
  991. result := ccallnode.createinternres(
  992. 'fpc_pwidechar_to_'+tstringdef(resulttype.def).stringtypname,
  993. ccallparanode.create(left,nil),resulttype);
  994. left := nil;
  995. end;
  996. function ttypeconvnode.resulttype_variant_to_dynarray : tnode;
  997. begin
  998. result := ccallnode.createinternres(
  999. 'fpc_variant_to_dynarray',
  1000. ccallparanode.create(caddrnode.create_internal(crttinode.create(tstoreddef(resulttype.def),initrtti)),
  1001. ccallparanode.create(left,nil)
  1002. ),resulttype);
  1003. resulttypepass(result);
  1004. left:=nil;
  1005. end;
  1006. function ttypeconvnode.resulttype_dynarray_to_variant : tnode;
  1007. begin
  1008. result := ccallnode.createinternres(
  1009. 'fpc_dynarray_to_variant',
  1010. ccallparanode.create(caddrnode.create_internal(crttinode.create(tstoreddef(left.resulttype.def),initrtti)),
  1011. ccallparanode.create(ctypeconvnode.create_explicit(left,voidpointertype),nil)
  1012. ),resulttype);
  1013. resulttypepass(result);
  1014. left:=nil;
  1015. end;
  1016. function ttypeconvnode.resulttype_variant_to_interface : tnode;
  1017. begin
  1018. result := ccallnode.createinternres(
  1019. 'fpc_variant_to_interface',
  1020. ccallparanode.create(left,nil)
  1021. ,resulttype);
  1022. resulttypepass(result);
  1023. left:=nil;
  1024. end;
  1025. function ttypeconvnode.resulttype_interface_to_variant : tnode;
  1026. begin
  1027. result := ccallnode.createinternres(
  1028. 'fpc_interface_to_variant',
  1029. ccallparanode.create(left,nil)
  1030. ,resulttype);
  1031. resulttypepass(result);
  1032. left:=nil;
  1033. end;
  1034. function ttypeconvnode.resulttype_variant_to_enum : tnode;
  1035. begin
  1036. result := ctypeconvnode.create_internal(left,sinttype);
  1037. result := ctypeconvnode.create_internal(result,resulttype);
  1038. resulttypepass(result);
  1039. { left is reused }
  1040. left := nil;
  1041. end;
  1042. function ttypeconvnode.resulttype_enum_to_variant : tnode;
  1043. begin
  1044. result := ctypeconvnode.create_internal(left,sinttype);
  1045. result := ctypeconvnode.create_internal(result,cvarianttype);
  1046. resulttypepass(result);
  1047. { left is reused }
  1048. left := nil;
  1049. end;
  1050. function ttypeconvnode.resulttype_array_2_dynarray : tnode;
  1051. var
  1052. newstatement : tstatementnode;
  1053. temp : ttempcreatenode;
  1054. temp2 : ttempcreatenode;
  1055. begin
  1056. { create statements with call to getmem+initialize }
  1057. result:=internalstatements(newstatement);
  1058. { create temp for result }
  1059. temp:=ctempcreatenode.create(resulttype,resulttype.def.size,tt_persistent,true);
  1060. addstatement(newstatement,temp);
  1061. { get temp for array of lengths }
  1062. temp2:=ctempcreatenode.create(sinttype,sinttype.def.size,tt_persistent,false);
  1063. addstatement(newstatement,temp2);
  1064. { one dimensional }
  1065. addstatement(newstatement,cassignmentnode.create(
  1066. ctemprefnode.create_offset(temp2,0),
  1067. cordconstnode.create
  1068. (tarraydef(left.resulttype.def).highrange+1,s32inttype,true)));
  1069. { create call to fpc_dynarr_setlength }
  1070. addstatement(newstatement,ccallnode.createintern('fpc_dynarray_setlength',
  1071. ccallparanode.create(caddrnode.create_internal
  1072. (ctemprefnode.create(temp2)),
  1073. ccallparanode.create(cordconstnode.create
  1074. (1,s32inttype,true),
  1075. ccallparanode.create(caddrnode.create_internal
  1076. (crttinode.create(tstoreddef(resulttype.def),initrtti)),
  1077. ccallparanode.create(
  1078. ctypeconvnode.create_internal(
  1079. ctemprefnode.create(temp),voidpointertype),
  1080. nil))))
  1081. ));
  1082. addstatement(newstatement,ctempdeletenode.create(temp2));
  1083. { copy ... }
  1084. addstatement(newstatement,cassignmentnode.create(
  1085. ctypeconvnode.create_internal(cderefnode.create(ctypeconvnode.create_internal(ctemprefnode.create(temp),voidpointertype)),left.resulttype),
  1086. left
  1087. ));
  1088. { left is reused }
  1089. left:=nil;
  1090. { the last statement should return the value as
  1091. location and type, this is done be referencing the
  1092. temp and converting it first from a persistent temp to
  1093. normal temp }
  1094. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  1095. addstatement(newstatement,ctemprefnode.create(temp));
  1096. end;
  1097. procedure copyparasym(p:TNamedIndexItem;arg:pointer);
  1098. var
  1099. newparast : tsymtable absolute arg;
  1100. vs : tparavarsym;
  1101. begin
  1102. if tsym(p).typ<>paravarsym then
  1103. exit;
  1104. with tparavarsym(p) do
  1105. begin
  1106. vs:=tparavarsym.create(realname,paranr,varspez,vartype,varoptions);
  1107. vs.defaultconstsym:=defaultconstsym;
  1108. newparast.insert(vs);
  1109. end;
  1110. end;
  1111. function ttypeconvnode.resulttype_proc_to_procvar : tnode;
  1112. var
  1113. pd : tabstractprocdef;
  1114. begin
  1115. result:=nil;
  1116. pd:=tabstractprocdef(left.resulttype.def);
  1117. { create procvardef }
  1118. resulttype.setdef(tprocvardef.create(pd.parast.symtablelevel));
  1119. tprocvardef(resulttype.def).proctypeoption:=pd.proctypeoption;
  1120. tprocvardef(resulttype.def).proccalloption:=pd.proccalloption;
  1121. tprocvardef(resulttype.def).procoptions:=pd.procoptions;
  1122. tprocvardef(resulttype.def).rettype:=pd.rettype;
  1123. { method ? then set the methodpointer flag }
  1124. if (pd.owner.symtabletype=objectsymtable) then
  1125. include(tprocvardef(resulttype.def).procoptions,po_methodpointer);
  1126. { was it a local procedure? }
  1127. if (pd.owner.symtabletype=localsymtable) then
  1128. include(tprocvardef(resulttype.def).procoptions,po_local);
  1129. { only need the address of the method? this is needed
  1130. for @tobject.create. In this case there will be a loadn without
  1131. a methodpointer. }
  1132. if (left.nodetype=loadn) and
  1133. not assigned(tloadnode(left).left) then
  1134. include(tprocvardef(resulttype.def).procoptions,po_addressonly);
  1135. { Add parameters use only references, we don't need to keep the
  1136. parast. We use the parast from the original function to calculate
  1137. our parameter data and reset it afterwards }
  1138. pd.parast.foreach_static(@copyparasym,tprocvardef(resulttype.def).parast);
  1139. tprocvardef(resulttype.def).calcparas;
  1140. end;
  1141. function ttypeconvnode.resulttype_call_helper(c : tconverttype) : tnode;
  1142. const
  1143. resulttypeconvert : array[tconverttype] of pointer = (
  1144. {none} nil,
  1145. {equal} nil,
  1146. {not_possible} nil,
  1147. { string_2_string } @ttypeconvnode.resulttype_string_to_string,
  1148. { char_2_string } @ttypeconvnode.resulttype_char_to_string,
  1149. { char_2_chararray } @ttypeconvnode.resulttype_char_to_chararray,
  1150. { pchar_2_string } @ttypeconvnode.resulttype_pchar_to_string,
  1151. { cchar_2_pchar } @ttypeconvnode.resulttype_cchar_to_pchar,
  1152. { cstring_2_pchar } @ttypeconvnode.resulttype_cstring_to_pchar,
  1153. { cstring_2_int } @ttypeconvnode.resulttype_cstring_to_int,
  1154. { ansistring_2_pchar } nil,
  1155. { string_2_chararray } @ttypeconvnode.resulttype_string_to_chararray,
  1156. { chararray_2_string } @ttypeconvnode.resulttype_chararray_to_string,
  1157. { array_2_pointer } nil,
  1158. { pointer_2_array } nil,
  1159. { int_2_int } @ttypeconvnode.resulttype_int_to_int,
  1160. { int_2_bool } nil,
  1161. { bool_2_bool } nil,
  1162. { bool_2_int } nil,
  1163. { real_2_real } @ttypeconvnode.resulttype_real_to_real,
  1164. { int_2_real } @ttypeconvnode.resulttype_int_to_real,
  1165. { real_2_currency } @ttypeconvnode.resulttype_real_to_currency,
  1166. { proc_2_procvar } @ttypeconvnode.resulttype_proc_to_procvar,
  1167. { arrayconstructor_2_set } @ttypeconvnode.resulttype_arrayconstructor_to_set,
  1168. { load_smallset } nil,
  1169. { cord_2_pointer } @ttypeconvnode.resulttype_cord_to_pointer,
  1170. { intf_2_string } nil,
  1171. { intf_2_guid } @ttypeconvnode.resulttype_interface_to_guid,
  1172. { class_2_intf } nil,
  1173. { char_2_char } @ttypeconvnode.resulttype_char_to_char,
  1174. { normal_2_smallset} nil,
  1175. { dynarray_2_openarray} @ttypeconvnode.resulttype_dynarray_to_openarray,
  1176. { pwchar_2_string} @ttypeconvnode.resulttype_pwchar_to_string,
  1177. { variant_2_dynarray} @ttypeconvnode.resulttype_variant_to_dynarray,
  1178. { dynarray_2_variant} @ttypeconvnode.resulttype_dynarray_to_variant,
  1179. { variant_2_enum} @ttypeconvnode.resulttype_variant_to_enum,
  1180. { enum_2_variant} @ttypeconvnode.resulttype_enum_to_variant,
  1181. { variant_2_interface} @ttypeconvnode.resulttype_interface_to_variant,
  1182. { interface_2_variant} @ttypeconvnode.resulttype_variant_to_interface,
  1183. { array_2_dynarray} @ttypeconvnode.resulttype_array_2_dynarray
  1184. );
  1185. type
  1186. tprocedureofobject = function : tnode of object;
  1187. var
  1188. r : packed record
  1189. proc : pointer;
  1190. obj : pointer;
  1191. end;
  1192. begin
  1193. result:=nil;
  1194. { this is a little bit dirty but it works }
  1195. { and should be quite portable too }
  1196. r.proc:=resulttypeconvert[c];
  1197. r.obj:=self;
  1198. if assigned(r.proc) then
  1199. result:=tprocedureofobject(r)();
  1200. end;
  1201. function ttypeconvnode.det_resulttype:tnode;
  1202. var
  1203. htype : ttype;
  1204. hp : tnode;
  1205. currprocdef : tabstractprocdef;
  1206. aprocdef : tprocdef;
  1207. eq : tequaltype;
  1208. cdoptions : tcompare_defs_options;
  1209. begin
  1210. result:=nil;
  1211. resulttype:=totype;
  1212. resulttypepass(left);
  1213. if codegenerror then
  1214. exit;
  1215. { When absolute force tc_equal }
  1216. if (nf_absolute in flags) then
  1217. begin
  1218. convtype:=tc_equal;
  1219. if not(tstoreddef(resulttype.def).is_intregable) and
  1220. not(tstoreddef(resulttype.def).is_fpuregable) then
  1221. make_not_regable(left);
  1222. exit;
  1223. end;
  1224. { tp procvar support. Skip typecasts to procvar, record or set. Those
  1225. convert on the procvar value. This is used to access the
  1226. fields of a methodpointer }
  1227. if not(nf_load_procvar in flags) and
  1228. not(resulttype.def.deftype in [procvardef,recorddef,setdef]) then
  1229. maybe_call_procvar(left,true);
  1230. { convert array constructors to sets, because there is no conversion
  1231. possible for array constructors }
  1232. if (resulttype.def.deftype<>arraydef) and
  1233. is_array_constructor(left.resulttype.def) then
  1234. begin
  1235. arrayconstructor_to_set(left);
  1236. resulttypepass(left);
  1237. end;
  1238. if convtype=tc_none then
  1239. begin
  1240. cdoptions:=[cdo_check_operator,cdo_allow_variant];
  1241. if nf_explicit in flags then
  1242. include(cdoptions,cdo_explicit);
  1243. if nf_internal in flags then
  1244. include(cdoptions,cdo_internal);
  1245. eq:=compare_defs_ext(left.resulttype.def,resulttype.def,left.nodetype,convtype,aprocdef,cdoptions);
  1246. case eq of
  1247. te_exact,
  1248. te_equal :
  1249. begin
  1250. { because is_equal only checks the basetype for sets we need to
  1251. check here if we are loading a smallset into a normalset }
  1252. if (resulttype.def.deftype=setdef) and
  1253. (left.resulttype.def.deftype=setdef) and
  1254. ((tsetdef(resulttype.def).settype = smallset) xor
  1255. (tsetdef(left.resulttype.def).settype = smallset)) then
  1256. begin
  1257. { constant sets can be converted by changing the type only }
  1258. if (left.nodetype=setconstn) then
  1259. begin
  1260. left.resulttype:=resulttype;
  1261. result:=left;
  1262. left:=nil;
  1263. exit;
  1264. end;
  1265. if (tsetdef(resulttype.def).settype <> smallset) then
  1266. convtype:=tc_load_smallset
  1267. else
  1268. convtype := tc_normal_2_smallset;
  1269. exit;
  1270. end
  1271. else
  1272. begin
  1273. { Only leave when there is no conversion to do.
  1274. We can still need to call a conversion routine,
  1275. like the routine to convert a stringconstnode }
  1276. if convtype in [tc_equal,tc_not_possible] then
  1277. begin
  1278. left.resulttype:=resulttype;
  1279. result:=left;
  1280. left:=nil;
  1281. exit;
  1282. end;
  1283. end;
  1284. end;
  1285. te_convert_l1,
  1286. te_convert_l2,
  1287. te_convert_l3 :
  1288. begin
  1289. { nothing to do }
  1290. end;
  1291. te_convert_operator :
  1292. begin
  1293. include(current_procinfo.flags,pi_do_call);
  1294. inc(aprocdef.procsym.refs);
  1295. hp:=ccallnode.create(ccallparanode.create(left,nil),Tprocsym(aprocdef.procsym),nil,nil,[]);
  1296. { tell explicitly which def we must use !! (PM) }
  1297. tcallnode(hp).procdefinition:=aprocdef;
  1298. left:=nil;
  1299. result:=hp;
  1300. exit;
  1301. end;
  1302. te_incompatible :
  1303. begin
  1304. { Procedures have a resulttype.def of voiddef and functions of their
  1305. own resulttype.def. They will therefore always be incompatible with
  1306. a procvar. Because isconvertable cannot check for procedures we
  1307. use an extra check for them.}
  1308. if (left.nodetype=calln) and
  1309. (tcallnode(left).para_count=0) and
  1310. (resulttype.def.deftype=procvardef) and
  1311. (
  1312. (m_tp_procvar in aktmodeswitches) or
  1313. (m_mac_procvar in aktmodeswitches)
  1314. ) then
  1315. begin
  1316. if assigned(tcallnode(left).right) then
  1317. begin
  1318. { this is already a procvar, if it is really equal
  1319. is checked below }
  1320. convtype:=tc_equal;
  1321. hp:=tcallnode(left).right.getcopy;
  1322. currprocdef:=tabstractprocdef(hp.resulttype.def);
  1323. end
  1324. else
  1325. begin
  1326. convtype:=tc_proc_2_procvar;
  1327. currprocdef:=Tprocsym(Tcallnode(left).symtableprocentry).search_procdef_byprocvardef(Tprocvardef(resulttype.def));
  1328. hp:=cloadnode.create_procvar(tprocsym(tcallnode(left).symtableprocentry),
  1329. tprocdef(currprocdef),tcallnode(left).symtableproc);
  1330. if (tcallnode(left).symtableprocentry.owner.symtabletype=objectsymtable) then
  1331. begin
  1332. if assigned(tcallnode(left).methodpointer) then
  1333. tloadnode(hp).set_mp(tcallnode(left).get_load_methodpointer)
  1334. else
  1335. tloadnode(hp).set_mp(load_self_node);
  1336. end;
  1337. resulttypepass(hp);
  1338. end;
  1339. left.free;
  1340. left:=hp;
  1341. { Now check if the procedure we are going to assign to
  1342. the procvar, is compatible with the procvar's type }
  1343. if not(nf_explicit in flags) and
  1344. (proc_to_procvar_equal(currprocdef,tprocvardef(resulttype.def))=te_incompatible) then
  1345. IncompatibleTypes(left.resulttype.def,resulttype.def);
  1346. exit;
  1347. end;
  1348. { Handle explicit type conversions }
  1349. if nf_explicit in flags then
  1350. begin
  1351. { do common tc_equal cast }
  1352. convtype:=tc_equal;
  1353. { ordinal constants can be resized to 1,2,4,8 bytes }
  1354. if (left.nodetype=ordconstn) then
  1355. begin
  1356. { Insert typeconv for ordinal to the correct size first on left, after
  1357. that the other conversion can be done }
  1358. htype.reset;
  1359. case longint(resulttype.def.size) of
  1360. 1 :
  1361. htype:=s8inttype;
  1362. 2 :
  1363. htype:=s16inttype;
  1364. 4 :
  1365. htype:=s32inttype;
  1366. 8 :
  1367. htype:=s64inttype;
  1368. end;
  1369. { we need explicit, because it can also be an enum }
  1370. if assigned(htype.def) then
  1371. inserttypeconv_internal(left,htype)
  1372. else
  1373. CGMessage2(type_e_illegal_type_conversion,left.resulttype.def.gettypename,resulttype.def.gettypename);
  1374. end;
  1375. { check if the result could be in a register }
  1376. if (not(tstoreddef(resulttype.def).is_intregable) and
  1377. not(tstoreddef(resulttype.def).is_fpuregable)) or
  1378. ((left.resulttype.def.deftype = floatdef) and
  1379. (resulttype.def.deftype <> floatdef)) then
  1380. make_not_regable(left);
  1381. { class/interface to class/interface, with checkobject support }
  1382. if is_class_or_interface(resulttype.def) and
  1383. is_class_or_interface(left.resulttype.def) then
  1384. begin
  1385. { check if the types are related }
  1386. if not(nf_internal in flags) and
  1387. (not(tobjectdef(left.resulttype.def).is_related(tobjectdef(resulttype.def)))) and
  1388. (not(tobjectdef(resulttype.def).is_related(tobjectdef(left.resulttype.def)))) then
  1389. begin
  1390. { Give an error when typecasting class to interface, this is compatible
  1391. with delphi }
  1392. if is_interface(resulttype.def) and
  1393. not is_interface(left.resulttype.def) then
  1394. CGMessage2(type_e_classes_not_related,
  1395. FullTypeName(left.resulttype.def,resulttype.def),
  1396. FullTypeName(resulttype.def,left.resulttype.def))
  1397. else
  1398. CGMessage2(type_w_classes_not_related,
  1399. FullTypeName(left.resulttype.def,resulttype.def),
  1400. FullTypeName(resulttype.def,left.resulttype.def))
  1401. end;
  1402. { Add runtime check? }
  1403. if (cs_check_object in aktlocalswitches) then
  1404. begin
  1405. { we can translate the typeconvnode to 'as' when
  1406. typecasting to a class or interface }
  1407. hp:=casnode.create(left,cloadvmtaddrnode.create(ctypenode.create(resulttype)));
  1408. left:=nil;
  1409. result:=hp;
  1410. exit;
  1411. end;
  1412. end
  1413. else
  1414. begin
  1415. { only if the same size or formal def }
  1416. if not(
  1417. (left.resulttype.def.deftype=formaldef) or
  1418. (
  1419. not(is_open_array(left.resulttype.def)) and
  1420. (left.resulttype.def.size=resulttype.def.size)
  1421. ) or
  1422. (
  1423. is_void(left.resulttype.def) and
  1424. (left.nodetype=derefn)
  1425. )
  1426. ) then
  1427. CGMessage2(type_e_illegal_type_conversion,left.resulttype.def.gettypename,resulttype.def.gettypename);
  1428. end;
  1429. end
  1430. else
  1431. IncompatibleTypes(left.resulttype.def,resulttype.def);
  1432. end;
  1433. else
  1434. internalerror(200211231);
  1435. end;
  1436. end;
  1437. { Give hint or warning for unportable code, exceptions are
  1438. - typecasts from constants
  1439. - void }
  1440. if not(nf_internal in flags) and
  1441. (left.nodetype<>ordconstn) and
  1442. not(is_void(left.resulttype.def)) and
  1443. (((left.resulttype.def.deftype=orddef) and
  1444. (resulttype.def.deftype in [pointerdef,procvardef,classrefdef])) or
  1445. ((resulttype.def.deftype=orddef) and
  1446. (left.resulttype.def.deftype in [pointerdef,procvardef,classrefdef]))) then
  1447. begin
  1448. { Give a warning when sizes don't match, because then info will be lost }
  1449. if left.resulttype.def.size=resulttype.def.size then
  1450. CGMessage(type_h_pointer_to_longint_conv_not_portable)
  1451. else
  1452. CGMessage(type_w_pointer_to_longint_conv_not_portable);
  1453. end;
  1454. { Constant folding and other node transitions to
  1455. remove the typeconv node }
  1456. case left.nodetype of
  1457. niln :
  1458. begin
  1459. { nil to ordinal node }
  1460. if (resulttype.def.deftype=orddef) then
  1461. begin
  1462. hp:=cordconstnode.create(0,resulttype,true);
  1463. result:=hp;
  1464. exit;
  1465. end
  1466. else
  1467. { fold nil to any pointer type }
  1468. if (resulttype.def.deftype=pointerdef) then
  1469. begin
  1470. hp:=cnilnode.create;
  1471. hp.resulttype:=resulttype;
  1472. result:=hp;
  1473. exit;
  1474. end
  1475. else
  1476. { remove typeconv after niln, but not when the result is a
  1477. methodpointer. The typeconv of the methodpointer will then
  1478. take care of updateing size of niln to OS_64 }
  1479. if not((resulttype.def.deftype=procvardef) and
  1480. (po_methodpointer in tprocvardef(resulttype.def).procoptions)) then
  1481. begin
  1482. left.resulttype:=resulttype;
  1483. result:=left;
  1484. left:=nil;
  1485. exit;
  1486. end;
  1487. end;
  1488. ordconstn :
  1489. begin
  1490. { ordinal contants can be directly converted }
  1491. { but not char to char because it is a widechar to char or via versa }
  1492. { which needs extra code to do the code page transistion }
  1493. { constant ordinal to pointer }
  1494. if (resulttype.def.deftype=pointerdef) and
  1495. (convtype<>tc_cchar_2_pchar) then
  1496. begin
  1497. hp:=cpointerconstnode.create(TConstPtrUInt(tordconstnode(left).value),resulttype);
  1498. result:=hp;
  1499. exit;
  1500. end
  1501. else if is_ordinal(resulttype.def) and
  1502. not(convtype=tc_char_2_char) then
  1503. begin
  1504. { replace the resulttype and recheck the range }
  1505. left.resulttype:=resulttype;
  1506. testrange(left.resulttype.def,tordconstnode(left).value,(nf_explicit in flags));
  1507. result:=left;
  1508. left:=nil;
  1509. exit;
  1510. end;
  1511. end;
  1512. pointerconstn :
  1513. begin
  1514. { pointerconstn to any pointer is folded too }
  1515. if (resulttype.def.deftype=pointerdef) then
  1516. begin
  1517. left.resulttype:=resulttype;
  1518. result:=left;
  1519. left:=nil;
  1520. exit;
  1521. end
  1522. { constant pointer to ordinal }
  1523. else if is_ordinal(resulttype.def) then
  1524. begin
  1525. hp:=cordconstnode.create(TConstExprInt(tpointerconstnode(left).value),
  1526. resulttype,not(nf_explicit in flags));
  1527. result:=hp;
  1528. exit;
  1529. end;
  1530. end;
  1531. end;
  1532. { now call the resulttype helper to do constant folding }
  1533. result:=resulttype_call_helper(convtype);
  1534. end;
  1535. procedure Ttypeconvnode.mark_write;
  1536. begin
  1537. left.mark_write;
  1538. end;
  1539. function ttypeconvnode.first_cord_to_pointer : tnode;
  1540. begin
  1541. result:=nil;
  1542. internalerror(200104043);
  1543. end;
  1544. function ttypeconvnode.first_int_to_int : tnode;
  1545. begin
  1546. first_int_to_int:=nil;
  1547. expectloc:=left.expectloc;
  1548. if not is_void(left.resulttype.def) then
  1549. begin
  1550. if (left.expectloc<>LOC_REGISTER) and
  1551. (resulttype.def.size>left.resulttype.def.size) then
  1552. expectloc:=LOC_REGISTER
  1553. else
  1554. if (left.expectloc=LOC_CREGISTER) and
  1555. (resulttype.def.size<left.resulttype.def.size) then
  1556. expectloc:=LOC_REGISTER;
  1557. end;
  1558. {$ifndef cpu64bit}
  1559. if is_64bit(resulttype.def) then
  1560. registersint:=max(registersint,2)
  1561. else
  1562. {$endif cpu64bit}
  1563. registersint:=max(registersint,1);
  1564. end;
  1565. function ttypeconvnode.first_cstring_to_pchar : tnode;
  1566. begin
  1567. result:=nil;
  1568. registersint:=1;
  1569. expectloc:=LOC_REGISTER;
  1570. end;
  1571. function ttypeconvnode.first_cstring_to_int : tnode;
  1572. begin
  1573. result:=nil;
  1574. internalerror(200510014);
  1575. end;
  1576. function ttypeconvnode.first_string_to_chararray : tnode;
  1577. begin
  1578. first_string_to_chararray:=nil;
  1579. expectloc:=left.expectloc;
  1580. end;
  1581. function ttypeconvnode.first_char_to_string : tnode;
  1582. begin
  1583. first_char_to_string:=nil;
  1584. expectloc:=LOC_REFERENCE;
  1585. end;
  1586. function ttypeconvnode.first_nothing : tnode;
  1587. begin
  1588. first_nothing:=nil;
  1589. end;
  1590. function ttypeconvnode.first_array_to_pointer : tnode;
  1591. begin
  1592. first_array_to_pointer:=nil;
  1593. if registersint<1 then
  1594. registersint:=1;
  1595. expectloc:=LOC_REGISTER;
  1596. end;
  1597. function ttypeconvnode.first_int_to_real: tnode;
  1598. var
  1599. fname: string[32];
  1600. typname : string[12];
  1601. begin
  1602. { Get the type name }
  1603. { Normally the typename should be one of the following:
  1604. single, double - carl
  1605. }
  1606. typname := lower(pbestrealtype^.def.gettypename);
  1607. { converting a 64bit integer to a float requires a helper }
  1608. if is_64bit(left.resulttype.def) then
  1609. begin
  1610. if is_signed(left.resulttype.def) then
  1611. fname := 'fpc_int64_to_'+typname
  1612. else
  1613. {$warning generic conversion from int to float does not support unsigned integers}
  1614. fname := 'fpc_int64_to_'+typname;
  1615. result := ccallnode.createintern(fname,ccallparanode.create(
  1616. left,nil));
  1617. left:=nil;
  1618. firstpass(result);
  1619. exit;
  1620. end
  1621. else
  1622. { other integers are supposed to be 32 bit }
  1623. begin
  1624. {$warning generic conversion from int to float does not support unsigned integers}
  1625. if is_signed(left.resulttype.def) then
  1626. fname := 'fpc_longint_to_'+typname
  1627. else
  1628. fname := 'fpc_longint_to_'+typname;
  1629. result := ccallnode.createintern(fname,ccallparanode.create(
  1630. left,nil));
  1631. left:=nil;
  1632. firstpass(result);
  1633. exit;
  1634. end;
  1635. end;
  1636. function ttypeconvnode.first_real_to_real : tnode;
  1637. begin
  1638. {$ifdef cpufpemu}
  1639. if cs_fp_emulation in aktmoduleswitches then
  1640. begin
  1641. if target_info.system in system_wince then
  1642. begin
  1643. case tfloatdef(left.resulttype.def).typ of
  1644. s32real:
  1645. case tfloatdef(resulttype.def).typ of
  1646. s64real:
  1647. result:=ccallnode.createintern('STOD',ccallparanode.create(left,nil));
  1648. s32real:
  1649. begin
  1650. result:=left;
  1651. left:=nil;
  1652. end;
  1653. else
  1654. internalerror(2005082704);
  1655. end;
  1656. s64real:
  1657. case tfloatdef(resulttype.def).typ of
  1658. s32real:
  1659. result:=ccallnode.createintern('DTOS',ccallparanode.create(left,nil));
  1660. s64real:
  1661. begin
  1662. result:=left;
  1663. left:=nil;
  1664. end;
  1665. else
  1666. internalerror(2005082703);
  1667. end;
  1668. else
  1669. internalerror(2005082702);
  1670. end;
  1671. left:=nil;
  1672. firstpass(result);
  1673. exit;
  1674. end
  1675. else
  1676. begin
  1677. {!! FIXME }
  1678. internalerror(2005082701);
  1679. end;
  1680. end
  1681. else
  1682. {$endif cpufpemu}
  1683. begin
  1684. first_real_to_real:=nil;
  1685. if registersfpu<1 then
  1686. registersfpu:=1;
  1687. expectloc:=LOC_FPUREGISTER;
  1688. end;
  1689. end;
  1690. function ttypeconvnode.first_pointer_to_array : tnode;
  1691. begin
  1692. first_pointer_to_array:=nil;
  1693. if registersint<1 then
  1694. registersint:=1;
  1695. expectloc:=LOC_REFERENCE;
  1696. end;
  1697. function ttypeconvnode.first_cchar_to_pchar : tnode;
  1698. begin
  1699. first_cchar_to_pchar:=nil;
  1700. internalerror(200104021);
  1701. end;
  1702. function ttypeconvnode.first_bool_to_int : tnode;
  1703. begin
  1704. first_bool_to_int:=nil;
  1705. { byte(boolean) or word(wordbool) or longint(longbool) must
  1706. be accepted for var parameters }
  1707. if (nf_explicit in flags) and
  1708. (left.resulttype.def.size=resulttype.def.size) and
  1709. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  1710. exit;
  1711. { when converting to 64bit, first convert to a 32bit int and then }
  1712. { convert to a 64bit int (only necessary for 32bit processors) (JM) }
  1713. if resulttype.def.size > sizeof(aint) then
  1714. begin
  1715. result := ctypeconvnode.create_internal(left,u32inttype);
  1716. result := ctypeconvnode.create(result,resulttype);
  1717. left := nil;
  1718. firstpass(result);
  1719. exit;
  1720. end;
  1721. expectloc:=LOC_REGISTER;
  1722. if registersint<1 then
  1723. registersint:=1;
  1724. end;
  1725. function ttypeconvnode.first_int_to_bool : tnode;
  1726. begin
  1727. first_int_to_bool:=nil;
  1728. { byte(boolean) or word(wordbool) or longint(longbool) must
  1729. be accepted for var parameters }
  1730. if (nf_explicit in flags) and
  1731. (left.resulttype.def.size=resulttype.def.size) and
  1732. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  1733. exit;
  1734. expectloc:=LOC_REGISTER;
  1735. { need if bool to bool !!
  1736. not very nice !!
  1737. insertypeconv(left,s32inttype);
  1738. left.explizit:=true;
  1739. firstpass(left); }
  1740. if registersint<1 then
  1741. registersint:=1;
  1742. end;
  1743. function ttypeconvnode.first_bool_to_bool : tnode;
  1744. begin
  1745. first_bool_to_bool:=nil;
  1746. expectloc:=LOC_REGISTER;
  1747. if registersint<1 then
  1748. registersint:=1;
  1749. end;
  1750. function ttypeconvnode.first_char_to_char : tnode;
  1751. begin
  1752. first_char_to_char:=first_int_to_int;
  1753. end;
  1754. function ttypeconvnode.first_proc_to_procvar : tnode;
  1755. begin
  1756. first_proc_to_procvar:=nil;
  1757. if tabstractprocdef(resulttype.def).is_addressonly then
  1758. begin
  1759. registersint:=left.registersint;
  1760. if registersint<1 then
  1761. registersint:=1;
  1762. expectloc:=LOC_REGISTER;
  1763. end
  1764. else
  1765. begin
  1766. if not(left.expectloc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  1767. CGMessage(parser_e_illegal_expression);
  1768. registersint:=left.registersint;
  1769. expectloc:=left.expectloc
  1770. end
  1771. end;
  1772. function ttypeconvnode.first_load_smallset : tnode;
  1773. var
  1774. srsym: ttypesym;
  1775. p: tcallparanode;
  1776. begin
  1777. if not searchsystype('FPC_SMALL_SET',srsym) then
  1778. internalerror(200108313);
  1779. p := ccallparanode.create(left,nil);
  1780. { reused }
  1781. left := nil;
  1782. { convert parameter explicitely to fpc_small_set }
  1783. p.left := ctypeconvnode.create_internal(p.left,srsym.restype);
  1784. { create call, adjust resulttype }
  1785. result :=
  1786. ccallnode.createinternres('fpc_set_load_small',p,resulttype);
  1787. firstpass(result);
  1788. end;
  1789. function ttypeconvnode.first_ansistring_to_pchar : tnode;
  1790. begin
  1791. first_ansistring_to_pchar:=nil;
  1792. expectloc:=LOC_REGISTER;
  1793. if registersint<1 then
  1794. registersint:=1;
  1795. end;
  1796. function ttypeconvnode.first_arrayconstructor_to_set : tnode;
  1797. begin
  1798. first_arrayconstructor_to_set:=nil;
  1799. internalerror(200104022);
  1800. end;
  1801. function ttypeconvnode.first_class_to_intf : tnode;
  1802. begin
  1803. first_class_to_intf:=nil;
  1804. expectloc:=LOC_REGISTER;
  1805. if registersint<1 then
  1806. registersint:=1;
  1807. end;
  1808. function ttypeconvnode._first_int_to_int : tnode;
  1809. begin
  1810. result:=first_int_to_int;
  1811. end;
  1812. function ttypeconvnode._first_cstring_to_pchar : tnode;
  1813. begin
  1814. result:=first_cstring_to_pchar;
  1815. end;
  1816. function ttypeconvnode._first_cstring_to_int : tnode;
  1817. begin
  1818. result:=first_cstring_to_int;
  1819. end;
  1820. function ttypeconvnode._first_string_to_chararray : tnode;
  1821. begin
  1822. result:=first_string_to_chararray;
  1823. end;
  1824. function ttypeconvnode._first_char_to_string : tnode;
  1825. begin
  1826. result:=first_char_to_string;
  1827. end;
  1828. function ttypeconvnode._first_nothing : tnode;
  1829. begin
  1830. result:=first_nothing;
  1831. end;
  1832. function ttypeconvnode._first_array_to_pointer : tnode;
  1833. begin
  1834. result:=first_array_to_pointer;
  1835. end;
  1836. function ttypeconvnode._first_int_to_real : tnode;
  1837. begin
  1838. result:=first_int_to_real;
  1839. end;
  1840. function ttypeconvnode._first_real_to_real : tnode;
  1841. begin
  1842. result:=first_real_to_real;
  1843. end;
  1844. function ttypeconvnode._first_pointer_to_array : tnode;
  1845. begin
  1846. result:=first_pointer_to_array;
  1847. end;
  1848. function ttypeconvnode._first_cchar_to_pchar : tnode;
  1849. begin
  1850. result:=first_cchar_to_pchar;
  1851. end;
  1852. function ttypeconvnode._first_bool_to_int : tnode;
  1853. begin
  1854. result:=first_bool_to_int;
  1855. end;
  1856. function ttypeconvnode._first_int_to_bool : tnode;
  1857. begin
  1858. result:=first_int_to_bool;
  1859. end;
  1860. function ttypeconvnode._first_bool_to_bool : tnode;
  1861. begin
  1862. result:=first_bool_to_bool;
  1863. end;
  1864. function ttypeconvnode._first_proc_to_procvar : tnode;
  1865. begin
  1866. result:=first_proc_to_procvar;
  1867. end;
  1868. function ttypeconvnode._first_load_smallset : tnode;
  1869. begin
  1870. result:=first_load_smallset;
  1871. end;
  1872. function ttypeconvnode._first_cord_to_pointer : tnode;
  1873. begin
  1874. result:=first_cord_to_pointer;
  1875. end;
  1876. function ttypeconvnode._first_ansistring_to_pchar : tnode;
  1877. begin
  1878. result:=first_ansistring_to_pchar;
  1879. end;
  1880. function ttypeconvnode._first_arrayconstructor_to_set : tnode;
  1881. begin
  1882. result:=first_arrayconstructor_to_set;
  1883. end;
  1884. function ttypeconvnode._first_class_to_intf : tnode;
  1885. begin
  1886. result:=first_class_to_intf;
  1887. end;
  1888. function ttypeconvnode._first_char_to_char : tnode;
  1889. begin
  1890. result:=first_char_to_char;
  1891. end;
  1892. function ttypeconvnode.first_call_helper(c : tconverttype) : tnode;
  1893. const
  1894. firstconvert : array[tconverttype] of pointer = (
  1895. nil, { none }
  1896. @ttypeconvnode._first_nothing, {equal}
  1897. @ttypeconvnode._first_nothing, {not_possible}
  1898. nil, { removed in resulttype_string_to_string }
  1899. @ttypeconvnode._first_char_to_string,
  1900. @ttypeconvnode._first_nothing, { char_2_chararray, needs nothing extra }
  1901. nil, { removed in resulttype_chararray_to_string }
  1902. @ttypeconvnode._first_cchar_to_pchar,
  1903. @ttypeconvnode._first_cstring_to_pchar,
  1904. @ttypeconvnode._first_cstring_to_int,
  1905. @ttypeconvnode._first_ansistring_to_pchar,
  1906. @ttypeconvnode._first_string_to_chararray,
  1907. nil, { removed in resulttype_chararray_to_string }
  1908. @ttypeconvnode._first_array_to_pointer,
  1909. @ttypeconvnode._first_pointer_to_array,
  1910. @ttypeconvnode._first_int_to_int,
  1911. @ttypeconvnode._first_int_to_bool,
  1912. @ttypeconvnode._first_bool_to_bool,
  1913. @ttypeconvnode._first_bool_to_int,
  1914. @ttypeconvnode._first_real_to_real,
  1915. @ttypeconvnode._first_int_to_real,
  1916. nil, { removed in resulttype_real_to_currency }
  1917. @ttypeconvnode._first_proc_to_procvar,
  1918. @ttypeconvnode._first_arrayconstructor_to_set,
  1919. @ttypeconvnode._first_load_smallset,
  1920. @ttypeconvnode._first_cord_to_pointer,
  1921. @ttypeconvnode._first_nothing,
  1922. @ttypeconvnode._first_nothing,
  1923. @ttypeconvnode._first_class_to_intf,
  1924. @ttypeconvnode._first_char_to_char,
  1925. @ttypeconvnode._first_nothing,
  1926. @ttypeconvnode._first_nothing,
  1927. nil,
  1928. nil,
  1929. nil,
  1930. nil,
  1931. nil,
  1932. nil,
  1933. nil,
  1934. nil
  1935. );
  1936. type
  1937. tprocedureofobject = function : tnode of object;
  1938. var
  1939. r : packed record
  1940. proc : pointer;
  1941. obj : pointer;
  1942. end;
  1943. begin
  1944. { this is a little bit dirty but it works }
  1945. { and should be quite portable too }
  1946. r.proc:=firstconvert[c];
  1947. r.obj:=self;
  1948. if not assigned(r.proc) then
  1949. internalerror(200312081);
  1950. first_call_helper:=tprocedureofobject(r){$ifdef FPC}(){$endif FPC}
  1951. end;
  1952. function ttypeconvnode.pass_1 : tnode;
  1953. begin
  1954. result:=nil;
  1955. firstpass(left);
  1956. if codegenerror then
  1957. exit;
  1958. { load the value_str from the left part }
  1959. registersint:=left.registersint;
  1960. registersfpu:=left.registersfpu;
  1961. {$ifdef SUPPORT_MMX}
  1962. registersmmx:=left.registersmmx;
  1963. {$endif}
  1964. expectloc:=left.expectloc;
  1965. result:=first_call_helper(convtype);
  1966. end;
  1967. function ttypeconvnode.assign_allowed:boolean;
  1968. begin
  1969. result:=(convtype=tc_equal) or
  1970. { typecasting from void is always allowed }
  1971. is_void(left.resulttype.def) or
  1972. (left.resulttype.def.deftype=formaldef) or
  1973. { int 2 int with same size reuses same location, or for
  1974. tp7 mode also allow size < orignal size }
  1975. (
  1976. (convtype=tc_int_2_int) and
  1977. (
  1978. (resulttype.def.size=left.resulttype.def.size) or
  1979. ((m_tp7 in aktmodeswitches) and
  1980. (resulttype.def.size<left.resulttype.def.size))
  1981. )
  1982. ) or
  1983. { int 2 bool/bool 2 int, explicit typecast, see also nx86cnv }
  1984. ((convtype in [tc_int_2_bool,tc_bool_2_int]) and
  1985. (nf_explicit in flags) and
  1986. (resulttype.def.size=left.resulttype.def.size));
  1987. { When using only a part of the value it can't be in a register since
  1988. that will load the value in a new register first }
  1989. if (resulttype.def.size<left.resulttype.def.size) then
  1990. make_not_regable(left);
  1991. end;
  1992. function ttypeconvnode.docompare(p: tnode) : boolean;
  1993. begin
  1994. docompare :=
  1995. inherited docompare(p) and
  1996. (convtype = ttypeconvnode(p).convtype);
  1997. end;
  1998. procedure ttypeconvnode._second_int_to_int;
  1999. begin
  2000. second_int_to_int;
  2001. end;
  2002. procedure ttypeconvnode._second_string_to_string;
  2003. begin
  2004. second_string_to_string;
  2005. end;
  2006. procedure ttypeconvnode._second_cstring_to_pchar;
  2007. begin
  2008. second_cstring_to_pchar;
  2009. end;
  2010. procedure ttypeconvnode._second_cstring_to_int;
  2011. begin
  2012. second_cstring_to_int;
  2013. end;
  2014. procedure ttypeconvnode._second_string_to_chararray;
  2015. begin
  2016. second_string_to_chararray;
  2017. end;
  2018. procedure ttypeconvnode._second_array_to_pointer;
  2019. begin
  2020. second_array_to_pointer;
  2021. end;
  2022. procedure ttypeconvnode._second_pointer_to_array;
  2023. begin
  2024. second_pointer_to_array;
  2025. end;
  2026. procedure ttypeconvnode._second_chararray_to_string;
  2027. begin
  2028. second_chararray_to_string;
  2029. end;
  2030. procedure ttypeconvnode._second_char_to_string;
  2031. begin
  2032. second_char_to_string;
  2033. end;
  2034. procedure ttypeconvnode._second_int_to_real;
  2035. begin
  2036. second_int_to_real;
  2037. end;
  2038. procedure ttypeconvnode._second_real_to_real;
  2039. begin
  2040. second_real_to_real;
  2041. end;
  2042. procedure ttypeconvnode._second_cord_to_pointer;
  2043. begin
  2044. second_cord_to_pointer;
  2045. end;
  2046. procedure ttypeconvnode._second_proc_to_procvar;
  2047. begin
  2048. second_proc_to_procvar;
  2049. end;
  2050. procedure ttypeconvnode._second_bool_to_int;
  2051. begin
  2052. second_bool_to_int;
  2053. end;
  2054. procedure ttypeconvnode._second_int_to_bool;
  2055. begin
  2056. second_int_to_bool;
  2057. end;
  2058. procedure ttypeconvnode._second_bool_to_bool;
  2059. begin
  2060. second_bool_to_bool;
  2061. end;
  2062. procedure ttypeconvnode._second_load_smallset;
  2063. begin
  2064. second_load_smallset;
  2065. end;
  2066. procedure ttypeconvnode._second_ansistring_to_pchar;
  2067. begin
  2068. second_ansistring_to_pchar;
  2069. end;
  2070. procedure ttypeconvnode._second_class_to_intf;
  2071. begin
  2072. second_class_to_intf;
  2073. end;
  2074. procedure ttypeconvnode._second_char_to_char;
  2075. begin
  2076. second_char_to_char;
  2077. end;
  2078. procedure ttypeconvnode._second_nothing;
  2079. begin
  2080. second_nothing;
  2081. end;
  2082. procedure ttypeconvnode.second_call_helper(c : tconverttype);
  2083. const
  2084. secondconvert : array[tconverttype] of pointer = (
  2085. @ttypeconvnode._second_nothing, {none}
  2086. @ttypeconvnode._second_nothing, {equal}
  2087. @ttypeconvnode._second_nothing, {not_possible}
  2088. @ttypeconvnode._second_nothing, {second_string_to_string, handled in resulttype pass }
  2089. @ttypeconvnode._second_char_to_string,
  2090. @ttypeconvnode._second_nothing, {char_to_charray}
  2091. @ttypeconvnode._second_nothing, { pchar_to_string, handled in resulttype pass }
  2092. @ttypeconvnode._second_nothing, {cchar_to_pchar}
  2093. @ttypeconvnode._second_cstring_to_pchar,
  2094. @ttypeconvnode._second_cstring_to_int,
  2095. @ttypeconvnode._second_ansistring_to_pchar,
  2096. @ttypeconvnode._second_string_to_chararray,
  2097. @ttypeconvnode._second_nothing, { chararray_to_string, handled in resulttype pass }
  2098. @ttypeconvnode._second_array_to_pointer,
  2099. @ttypeconvnode._second_pointer_to_array,
  2100. @ttypeconvnode._second_int_to_int,
  2101. @ttypeconvnode._second_int_to_bool,
  2102. @ttypeconvnode._second_bool_to_bool,
  2103. @ttypeconvnode._second_bool_to_int,
  2104. @ttypeconvnode._second_real_to_real,
  2105. @ttypeconvnode._second_int_to_real,
  2106. @ttypeconvnode._second_nothing, { real_to_currency, handled in resulttype pass }
  2107. @ttypeconvnode._second_proc_to_procvar,
  2108. @ttypeconvnode._second_nothing, { arrayconstructor_to_set }
  2109. @ttypeconvnode._second_nothing, { second_load_smallset, handled in first pass }
  2110. @ttypeconvnode._second_cord_to_pointer,
  2111. @ttypeconvnode._second_nothing, { interface 2 string }
  2112. @ttypeconvnode._second_nothing, { interface 2 guid }
  2113. @ttypeconvnode._second_class_to_intf,
  2114. @ttypeconvnode._second_char_to_char,
  2115. @ttypeconvnode._second_nothing, { normal_2_smallset }
  2116. @ttypeconvnode._second_nothing, { dynarray_2_openarray }
  2117. @ttypeconvnode._second_nothing, { pwchar_2_string }
  2118. @ttypeconvnode._second_nothing, { variant_2_dynarray }
  2119. @ttypeconvnode._second_nothing, { dynarray_2_variant}
  2120. @ttypeconvnode._second_nothing, { variant_2_enum }
  2121. @ttypeconvnode._second_nothing, { enum_2_variant }
  2122. @ttypeconvnode._second_nothing, { variant_2_interface }
  2123. @ttypeconvnode._second_nothing, { interface_2_variant }
  2124. @ttypeconvnode._second_nothing { array_2_dynarray }
  2125. );
  2126. type
  2127. tprocedureofobject = procedure of object;
  2128. var
  2129. r : packed record
  2130. proc : pointer;
  2131. obj : pointer;
  2132. end;
  2133. begin
  2134. { this is a little bit dirty but it works }
  2135. { and should be quite portable too }
  2136. r.proc:=secondconvert[c];
  2137. r.obj:=self;
  2138. tprocedureofobject(r)();
  2139. end;
  2140. {*****************************************************************************
  2141. TISNODE
  2142. *****************************************************************************}
  2143. constructor tisnode.create(l,r : tnode);
  2144. begin
  2145. inherited create(isn,l,r);
  2146. end;
  2147. function tisnode.det_resulttype:tnode;
  2148. var
  2149. paras: tcallparanode;
  2150. begin
  2151. result:=nil;
  2152. resulttypepass(left);
  2153. resulttypepass(right);
  2154. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2155. set_varstate(right,vs_read,[vsf_must_be_valid]);
  2156. if codegenerror then
  2157. exit;
  2158. if (right.resulttype.def.deftype=classrefdef) then
  2159. begin
  2160. { left must be a class }
  2161. if is_class(left.resulttype.def) then
  2162. begin
  2163. { the operands must be related }
  2164. if (not(tobjectdef(left.resulttype.def).is_related(
  2165. tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def)))) and
  2166. (not(tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def).is_related(
  2167. tobjectdef(left.resulttype.def)))) then
  2168. CGMessage2(type_e_classes_not_related,left.resulttype.def.typename,
  2169. tclassrefdef(right.resulttype.def).pointertype.def.typename);
  2170. end
  2171. else
  2172. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  2173. { call fpc_do_is helper }
  2174. paras := ccallparanode.create(
  2175. left,
  2176. ccallparanode.create(
  2177. right,nil));
  2178. result := ccallnode.createintern('fpc_do_is',paras);
  2179. left := nil;
  2180. right := nil;
  2181. end
  2182. else if is_interface(right.resulttype.def) then
  2183. begin
  2184. { left is a class }
  2185. if is_class(left.resulttype.def) then
  2186. begin
  2187. { the operands must be related }
  2188. if not(assigned(tobjectdef(left.resulttype.def).implementedinterfaces) and
  2189. (tobjectdef(left.resulttype.def).implementedinterfaces.searchintf(right.resulttype.def)<>-1)) then
  2190. CGMessage2(type_e_classes_not_related,
  2191. FullTypeName(left.resulttype.def,right.resulttype.def),
  2192. FullTypeName(right.resulttype.def,left.resulttype.def))
  2193. end
  2194. { left is an interface }
  2195. else if is_interface(left.resulttype.def) then
  2196. begin
  2197. { the operands must be related }
  2198. if (not(tobjectdef(left.resulttype.def).is_related(tobjectdef(right.resulttype.def)))) and
  2199. (not(tobjectdef(right.resulttype.def).is_related(tobjectdef(left.resulttype.def)))) then
  2200. CGMessage2(type_e_classes_not_related,
  2201. FullTypeName(left.resulttype.def,right.resulttype.def),
  2202. FullTypeName(right.resulttype.def,left.resulttype.def));
  2203. end
  2204. else
  2205. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  2206. { call fpc_do_is helper }
  2207. paras := ccallparanode.create(
  2208. left,
  2209. ccallparanode.create(
  2210. right,nil));
  2211. result := ccallnode.createintern('fpc_do_is',paras);
  2212. left := nil;
  2213. right := nil;
  2214. end
  2215. else
  2216. CGMessage1(type_e_class_or_interface_type_expected,right.resulttype.def.typename);
  2217. resulttype:=booltype;
  2218. end;
  2219. function tisnode.pass_1 : tnode;
  2220. begin
  2221. internalerror(200204254);
  2222. result:=nil;
  2223. end;
  2224. { dummy pass_2, it will never be called, but we need one since }
  2225. { you can't instantiate an abstract class }
  2226. procedure tisnode.pass_2;
  2227. begin
  2228. end;
  2229. {*****************************************************************************
  2230. TASNODE
  2231. *****************************************************************************}
  2232. constructor tasnode.create(l,r : tnode);
  2233. begin
  2234. inherited create(asn,l,r);
  2235. call := nil;
  2236. end;
  2237. destructor tasnode.destroy;
  2238. begin
  2239. call.free;
  2240. inherited destroy;
  2241. end;
  2242. function tasnode.det_resulttype:tnode;
  2243. var
  2244. hp : tnode;
  2245. begin
  2246. result:=nil;
  2247. resulttypepass(right);
  2248. resulttypepass(left);
  2249. set_varstate(right,vs_read,[vsf_must_be_valid]);
  2250. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2251. if codegenerror then
  2252. exit;
  2253. if (right.resulttype.def.deftype=classrefdef) then
  2254. begin
  2255. { left must be a class }
  2256. if is_class(left.resulttype.def) then
  2257. begin
  2258. { the operands must be related }
  2259. if (not(tobjectdef(left.resulttype.def).is_related(
  2260. tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def)))) and
  2261. (not(tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def).is_related(
  2262. tobjectdef(left.resulttype.def)))) then
  2263. CGMessage2(type_e_classes_not_related,
  2264. FullTypeName(left.resulttype.def,tclassrefdef(right.resulttype.def).pointertype.def),
  2265. FullTypeName(tclassrefdef(right.resulttype.def).pointertype.def,left.resulttype.def));
  2266. end
  2267. else
  2268. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  2269. resulttype:=tclassrefdef(right.resulttype.def).pointertype;
  2270. end
  2271. else if is_interface(right.resulttype.def) then
  2272. begin
  2273. { left is a class }
  2274. if not(is_class(left.resulttype.def) or
  2275. is_interface(left.resulttype.def)) then
  2276. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  2277. resulttype:=right.resulttype;
  2278. { load the GUID of the interface }
  2279. if (right.nodetype=typen) then
  2280. begin
  2281. if assigned(tobjectdef(right.resulttype.def).iidguid) then
  2282. begin
  2283. hp:=cguidconstnode.create(tobjectdef(right.resulttype.def).iidguid^);
  2284. right.free;
  2285. right:=hp;
  2286. end
  2287. else
  2288. internalerror(200206282);
  2289. resulttypepass(right);
  2290. end;
  2291. end
  2292. else
  2293. CGMessage1(type_e_class_or_interface_type_expected,right.resulttype.def.typename);
  2294. end;
  2295. function tasnode._getcopy: tnode;
  2296. begin
  2297. result := inherited _getcopy;
  2298. if assigned(call) then
  2299. tasnode(result).call := call.getcopy
  2300. else
  2301. tasnode(result).call := nil;
  2302. end;
  2303. function tasnode.pass_1 : tnode;
  2304. var
  2305. procname: string;
  2306. begin
  2307. result:=nil;
  2308. if not assigned(call) then
  2309. begin
  2310. if is_class(left.resulttype.def) and
  2311. (right.resulttype.def.deftype=classrefdef) then
  2312. call := ccallnode.createinternres('fpc_do_as',
  2313. ccallparanode.create(left,ccallparanode.create(right,nil)),
  2314. resulttype)
  2315. else
  2316. begin
  2317. if is_class(left.resulttype.def) then
  2318. procname := 'fpc_class_as_intf'
  2319. else
  2320. procname := 'fpc_intf_as';
  2321. call := ccallnode.createinternres(procname,
  2322. ccallparanode.create(right,ccallparanode.create(left,nil)),
  2323. resulttype);
  2324. end;
  2325. left := nil;
  2326. right := nil;
  2327. firstpass(call);
  2328. if codegenerror then
  2329. exit;
  2330. expectloc:=call.expectloc;
  2331. registersint:=call.registersint;
  2332. registersfpu:=call.registersfpu;
  2333. {$ifdef SUPPORT_MMX}
  2334. registersmmx:=call.registersmmx;
  2335. {$endif SUPPORT_MMX}
  2336. end;
  2337. end;
  2338. begin
  2339. ctypeconvnode:=ttypeconvnode;
  2340. casnode:=tasnode;
  2341. cisnode:=tisnode;
  2342. end.