ncnv.pas 94 KB

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