ncnv.pas 94 KB

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