ncnv.pas 99 KB

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