ncnv.pas 94 KB

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