ncnv.pas 96 KB

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